/* * usbComms.c * * Created on: May 17, 2023 * Author: Brian.Bailey */ /* * USB Comms * Data is received by APP_and placed in rxBuffer[] */ #include #include //Drivers #include "fsl_gpio.h" #include "fsl_device_registers.h" #include "fsl_debug_console.h" #include "pin_mux.h" #include "clock_config.h" #include "board.h" //Application Includes #include "virtual_com.h" #include "flashUpdate.h" #include "bootloader.h" #include "eeprom.h" #include "System/system.h" //#include "frq.h" #include "hwFixes.h" #include "usbComms.h" /******************************************************************************* * Definitions ******************************************************************************/ #define USB_DETECT_PORT 1 //USB VBUS on GPIO1_6. Not sure we can read this #define USB_DETECT_PIN 6 #define CR '\r' //carriage return #define LF '\n' //line feed #define null 0 //NULL is defined at (void *)0 so it causes errors /******************************************************************************* * Variables ******************************************************************************/ USB_t usb; extern SYSTEM_DATA_t sys; extern BL_DATA_t blData; extern usb_cdc_vcom_struct_t s_cdcVcom; extern uint8_t frq_update_flag; extern HARDWARE_FIX_t hwf; //temp storage for string data uint8_t usbTempString[USB_RX_BUFFER_SIZE]; /******************************************************************************* * Static Function Declarations ******************************************************************************/ /******************************************************************************* * Static Functions ******************************************************************************/ /******************************************************************************* * Public Functions ******************************************************************************/ uint8_t USB_IsConnected(void) { return GPIO_PinRead(GPIO, USB_DETECT_PORT, USB_DETECT_PIN); } /* Process incoming USB string * Commands are LF terminated ASCII text * @param size number of bytes to process */ void USB_ProcessString(uint32_t size) { //operates on usb.rxDataBuffer[] //index is usb.rxCommandIndex uint32_t usbBusyCount = 0; usb.rxStringReceived = false; for(uint32_t i = 0; i < size; i++) //Look for LF character in rxDataBuffer { if(LF == usb.rxDataBuffer[i]) { usb.rxDataBuffer[i] = 0; //replace LF with NULL to terminate string usb.rxStringReceived = true; break; } } if(usb.rxStringReceived) { //Process the command USB_ProcessCommand(); } else { //This is an error. search the ringbuffer for the next LF and throw away everything before it. } } /* * Process Incoming USB commands */ void USB_ProcessCommand(void) { if(USB_CommandCompare("dID")) //device ID { USB_SendString("UM_TX"); } else if(USB_CommandPrefixCompare("p")) { USB_ProcessProgramCommand(); } else if(USB_CommandPrefixCompare("s")) { USB_ProcessSystemCommand(); } else if(USB_CommandPrefixCompare("f")) { USB_ProcessFrequencyCommand(); } else { //Bad Command USB_SendString(NAK); } } /* * Process Program commands */ void USB_ProcessProgramCommand(void) { if (USB_CommandCompare("pLoaderRunRequest")) //Request bootloader run { blData.loaderRunRequest = 1; EE_WriteMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest); //TESTING: Verify write EE_ReadMemoryUINT32(BL_LOADERRUNREQUEST_EEPROM_ADDR, &blData.loaderRunRequest); if(blData.loaderRunRequest == 1) { USB_SendString(ACK); } else { USB_SendString(NAK); } } else if (USB_CommandPrefixCompare("pProgramSetup,")) //Program setup information - RX can ONLY update the bootloader at 0x6000 0000 { FU_ProgramSetup(usb.rxDataBuffer); } else if(USB_CommandCompare("pVerifyImage")) //verify image in FLASH - compare hashes { FU_VerifyImage(); } else if(USB_CommandCompare("pReset")) { FU_ResetProcessor(); } else if (USB_CommandCompare("pClearProgramFlag")) { blData.transmitterProgrammed = false; EE_WriteMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed); //TESTING: Verify write EE_ReadMemoryUINT32(BL_PROGRAMMED_EEPROM_ADDR, &blData.transmitterProgrammed); if(blData.transmitterProgrammed == false) { USB_SendString(ACK); } else { USB_SendString(NAK); } } else if (USB_CommandCompare("pEraseAllData")) //Erase settings and factory data. Leave bootloader data { EE_EraseAllData(); // Erase EEPROM data USB_SendString(ACK); } else { USB_SendString(NAK); } } /* * Process System commands */ void USB_ProcessSystemCommand(void) { static uint8_t *p; //pointer for strtok - static so it doesn't get optimized out. if (USB_CommandPrefixCompare("sSetMfg,")) { p = strtok(usb.rxDataBuffer, ","); //points to command p = strtok(null, ","); //points to arg strncpy(sys.manufacturer, p, SYS_INFO_LENGTH); USB_SendString(ACK); } else if (USB_CommandPrefixCompare("sSetModelNumber,")) { char* token = strtok(usb.rxDataBuffer, ","); token = strtok(null, ","); //points to arg strncpy(sys.modelNumber, token, SYS_INFO_LENGTH); USB_SendString(ACK); } else if (USB_CommandPrefixCompare("sSetModelName,")) { char* token = strtok(usb.rxDataBuffer, ","); token = strtok(null, ","); //points to arg strncpy(sys.modelName, token, SYS_INFO_LENGTH); USB_SendString(ACK); } else if (USB_CommandPrefixCompare("sSetSerial,")) { char* token = strtok(usb.rxDataBuffer, ","); token = strtok(null, ","); //points to arg strncpy(sys.serialNumber, token, SYS_INFO_LENGTH); USB_SendString(ACK); } else if (USB_CommandPrefixCompare("sSetMfgDate,")) { char* token = strtok(usb.rxDataBuffer, ","); token = strtok(null, ","); //points to arg strncpy(sys.mfgDate, token, SYS_INFO_LENGTH); USB_SendString(ACK); } else if (USB_CommandPrefixCompare("sSetHourCount,")) { //do nothing USB_SendString(ACK); } else if(USB_CommandPrefixCompare("sSetMainPcbaPN,")) { char* token = strtok(usb.rxDataBuffer, ","); token = strtok(null, ","); //points to arg hwf.mainPcbaPN = atoi(token); EE_WriteUINT32(EE_HWFIX_MAIN_PCBA_PN, hwf.mainPcbaPN); USB_SendString(ACK); } else if (USB_CommandCompare("sSaveSettings")) { //Save settings data EE_SaveData(); //frq_update_flag = false; //Change_To_Next_BC_Freq(); USB_SendString(ACK); } else if (USB_CommandCompare("sGetSwVersion")) { USB_SendString(SW_VERSION); } else if (USB_CommandCompare("sGetMfg")) { USB_SendString(sys.manufacturer); } else if (USB_CommandCompare("sGetModelNumber")) { USB_SendString(sys.modelNumber); } else if (USB_CommandCompare("sGetModelName")) { USB_SendString(sys.modelName); } else if (USB_CommandCompare("sGetSerial")) { USB_SendString(sys.serialNumber); } else if (USB_CommandCompare("sGetMfgDate")) { USB_SendString(sys.mfgDate); } else if(USB_CommandCompare("sGetMainPcbaPN")) { sprintf(usbTempString, "%d", hwf.mainPcbaPN); USB_SendString(usbTempString); } else if(USB_CommandCompare("sGetbootloaderVerion")) { sprintf(usbTempString, "%d", sys.bootloaderVersion); USB_SendString(usbTempString); } else //Unknown command { USB_SendString(NAK); } } /* * Process Frequency Commands */ void USB_ProcessFrequencyCommand(void) { static uint8_t *p; //pointer for strtok - static so it doesn't get optimized out. if (USB_CommandPrefixCompare("fAdd,")) { //Add a frequency to FreqArray[] //Parse message p = strtok(usb.rxDataBuffer, ","); //points to "fAdd" p = strtok(null, ","); //frequency uint32_t frequency = atoi(p); p = strtok(null, ","); //type uint32_t type = atoi(p); p = strtok(null, ","); //enabled uint32_t enabled = atoi(p); p = strtok(null, ","); //inMenu uint32_t inMenu = atoi(p); fgen_addFrequency(frequency, enabled, inMenu, type); USB_SendString(ACK); } else if (USB_CommandCompare("fClear")) { //Clear the frequency list //frq_update_flag = true; fgen_clearFrequencies(); USB_SendString(ACK); } else if (USB_CommandCompare("fSave")) { //Save frequency data to EEPROM EE_SaveData(); USB_SendString(ACK); } else if (USB_CommandCompare("fGetNum")) //Get number of frequencies in the receiver { sprintf(usbTempString, "%d", fgen_getNumFrequencies()); USB_SendString(usbTempString); } else if (USB_CommandPrefixCompare("fGet,")) //Get string representing a single frequency { p = strtok(usb.rxDataBuffer, ","); //points to "fGet" p = strtok(null, ","); //index uint32_t index = atoi(p); //FREQUENCY_t frq = FREQ_GetFreqByIndex(index); //sprintf(usbTempString, "%d,%d,%d,%d", frq.frequency1, 0, frq.enabled, 1); //FREQ_TYPE-t always 0, inMenu always 1 USB_SendString(usbTempString); } else //Unknown command { USB_SendString(NAK); } } /* * Return true if str matches data in usb.rxDataBuffer */ bool USB_CommandCompare(uint8_t *str) { return (0 == strncmp(str, usb.rxDataBuffer, USB_RX_BUFFER_SIZE)); } /* Compare str to contents of usb.rxDataBuffer * Return true if str matches data in usb.rxDataBuffer */ bool USB_CommandPrefixCompare(uint8_t *str) { return (0 == strncmp(str, usb.rxDataBuffer, strlen(str))); }