int CLI_Read(unsigned char *pBuff) { if(pBuff == NULL) return -1; #ifdef _USE_CLI_ g_ucUARTBuffer = pBuff; while(LOOP_FOREVER) { *g_ucUARTBuffer = (unsigned char)ROM_UARTCharGet(UART0_BASE); if(*g_ucUARTBuffer == ASCII_ENTER) { *g_ucUARTBuffer = 0x00; break; } g_ucUARTBuffer++; } return strlen((const char *)pBuff); #else return 0; #endif }
// Called when UART RX FIFO has data in it static void flushReadFIFO(uint8_t UART) { // Read from the FIFO and place in the circular buffer in memory while (ROM_UARTCharsAvail(UARTBASE[UART])) { if (rxQSize(UART) < UART_BUFFSIZE - 1) rxEnqueue(UART, ROM_UARTCharGet(UARTBASE[UART])); else ROM_UARTCharGet(UARTBASE[UART]); } // If the circular queue is full, set the overflow flag and return if (rxQSize(UART) == UART_BUFFSIZE - 1) overflow |= bit8[UART]; // Clear the appropriate interrupt flag ROM_UARTIntClear(UARTBASE[UART], UART_INT_RX); }
unsigned int Terminal_Recv(char* buffer, unsigned int ui32Count) { unsigned int i; for(i = 0; i < ui32Count; i++) { buffer[i] = (char)ROM_UARTCharGet(UART0_BASE); if(buffer[i] == '\r') { break; } } return i + 1; }
//***************************************************************************** // // Handles interrupts from the UART. // //***************************************************************************** void UART0IntHandler(void) { unsigned long ulStatus; unsigned char ucChar; // // Get the interrupts that are being asserted by the UART. // ulStatus = ROM_UARTIntStatus(UART0_BASE, true); // // Clear the asserted interrupts. // ROM_UARTIntClear(UART0_BASE, ulStatus); // // Indicate that the UART link is good. // ControllerLinkGood(LINK_TYPE_UART); // // See if the receive interrupt has been asserted. // if(ulStatus & (UART_INT_RX | UART_INT_RT)) { // // Loop while there are more characters to be read from the UART. // while(ROM_UARTCharsAvail(UART0_BASE)) { // // Read the next character from the UART. // ucChar = ROM_UARTCharGet(UART0_BASE); // // See if this is a start of packet byte. // if(ucChar == 0xff) { // // Reset the length of the UART message. // g_ulUARTLength = 0; // // Set the state such that the next byte received is the size // of the message. // g_ulUARTState = UART_STATE_LENGTH; } // // See if this byte is the size of the message. // else if(g_ulUARTState == UART_STATE_LENGTH) { // // Save the size of the message. // g_ulUARTSize = ucChar; // // Subsequent bytes received are the message data. // g_ulUARTState = UART_STATE_DATA; } // // See if the previous character was an escape character. // else if(g_ulUARTState == UART_STATE_ESCAPE) { // // See if this 0xfe, the escaped version of 0xff. // if(ucChar == 0xfe) { // // Store a 0xff in the message buffer. // g_pucUARTMessage[g_ulUARTLength++] = 0xff; // // Subsequent bytes received are the message data. // g_ulUARTState = UART_STATE_DATA; } // // Otherwise, see if this is 0xfd, the escaped version of 0xfe. // else if(ucChar == 0xfd) { // // Store a 0xfe in the message buffer. // g_pucUARTMessage[g_ulUARTLength++] = 0xfe; // // Subsequent bytes received are the message data. // g_ulUARTState = UART_STATE_DATA; } // // Otherwise, this is a corrupted sequence. Set the receiver // to idle so this message is dropped, and subsequent data is // ignored until another start of packet is received. // else { g_ulUARTState = UART_STATE_IDLE; } } // // See if this is a part of the message data. // else if(g_ulUARTState == UART_STATE_DATA) { // // See if this character is an escape character. // if(ucChar == 0xfe) { // // The next byte is an escaped byte. // g_ulUARTState = UART_STATE_ESCAPE; } else { // // Store this byte in the message buffer. // g_pucUARTMessage[g_ulUARTLength++] = ucChar; } } // // See if the entire message has been received but has not been // processed (i.e. the most recent byte received was the end of the // message). // if((g_ulUARTLength == g_ulUARTSize) && (g_ulUARTState == UART_STATE_DATA)) { // // Process this message. // UARTIFCommandHandler(); // // The UART interface is idle, meaning all bytes will be // dropped until the next start of packet byte. // g_ulUARTState = UART_STATE_IDLE; } } // // Tell the controller that CAN activity was detected. // ControllerWatchdog(LINK_TYPE_UART); } // // See if the transmit interrupt has been asserted. // if(ulStatus & UART_INT_TX) { // // Loop while there are more characters to be transmitted and more // space in the UART FIFO. // while((g_ulUARTXmitRead != g_ulUARTXmitWrite) && ROM_UARTSpaceAvail(UART0_BASE)) { // // Put the next character into the UART FIFO. // ROM_UARTCharPut(UART0_BASE, g_pucUARTXmit[g_ulUARTXmitRead]); // // Increment the read pointer. // g_ulUARTXmitRead = (g_ulUARTXmitRead + 1) % UART_XMIT_SIZE; } } // // See if an enumeration response needs to be sent. // if(HWREGBITW(&g_ulUARTFlags, UART_FLAG_ENUM) != 0) { // // Send the enumeration response for this device. // UARTIFSendMessage(CAN_MSGID_API_ENUMERATE | g_sParameters.ucDeviceNumber, 0, 0); // // Clear the enumeration response flag. // HWREGBITW(&g_ulUARTFlags, UART_FLAG_ENUM) = 0; } // // See if periodic status messages need to be sent. // if(HWREGBITW(&g_ulUARTFlags, UART_FLAG_PSTATUS) != 0) { // // Send out the first periodic status message if it needs to be sent. // if(g_ulPStatFlags & 1) { UARTIFSendMessage(LM_API_PSTAT_DATA_S0 | g_sParameters.ucDeviceNumber, g_ppucPStatMessages[0], g_pucPStatMessageLen[0]); } // // Send out the second periodic status message if it needs to be sent. // if(g_ulPStatFlags & 2) { UARTIFSendMessage(LM_API_PSTAT_DATA_S1 | g_sParameters.ucDeviceNumber, g_ppucPStatMessages[1], g_pucPStatMessageLen[1]); } // // Send out the third periodic status message if it needs to be sent. // if(g_ulPStatFlags & 4) { UARTIFSendMessage(LM_API_PSTAT_DATA_S2 | g_sParameters.ucDeviceNumber, g_ppucPStatMessages[2], g_pucPStatMessageLen[2]); } // // Send out the fourth periodic status message if it needs to be sent. // if(g_ulPStatFlags & 8) { UARTIFSendMessage(LM_API_PSTAT_DATA_S3 | g_sParameters.ucDeviceNumber, g_ppucPStatMessages[3], g_pucPStatMessageLen[3]); } // // Clear the periodic status message flag. // HWREGBITW(&g_ulUARTFlags, UART_FLAG_PSTATUS) = 0; } }
// main routine int main(void) { // enable lazy stacking ROM_FPUEnable(); ROM_FPULazyStackingEnable(); // run from crystal, 80 MHz ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // enable peripherals ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // set UART pins GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // init PORTB ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIO_PORTB_DIR_R = 0x00; GPIO_PORTB_DEN_R = 0xff; // configure uart ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); // Enable interrupts to the processor. - not sure if this is needed... ROM_IntMasterEnable(); // main loop while (1) { if (ROM_UARTCharsAvail(UART0_BASE)) { unsigned char cmd; cmd = ROM_UARTCharGetNonBlocking(UART0_BASE); switch (cmd) { case SUMP_RESET: break; case SUMP_ARM: doticksampling(); break; case SUMP_QUERY: uart_puts("1ALS"); break; case SUMP_GET_METADATA: // device name uart_putch(0x01); uart_puts(VERSION); uart_putch(0x00); // amount of sample memory available (bytes) sump_sendmeta_uint32(0x21, BUFFERSIZE); // maximum sample rate (hz) sump_sendmeta_uint32(0x23, MAX_SAMPLERATE); // number of usable probes (short) sump_sendmeta_uint8(0x40, 0x08); // protocol version (short) sump_sendmeta_uint8(0x41, 0x02); // end of meta data uart_putch(0x00); break; case SUMP_SET_DIVIDER: { /* Set Divider (80h) When x is written, the sampling frequency is set to f = clock / (x + 1) */ unsigned long clock = 100000000; //no idea where this comes from... //these three bytes are our clock divider - lsb first unsigned char b0 = ROM_UARTCharGet(UART0_BASE); unsigned char b1 = ROM_UARTCharGet(UART0_BASE); unsigned char b2 = ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); //eat last byte unsigned long rate = b0 | (b1 << 8) | (b2 << 16); rate = clock / (rate+1); ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / rate); break; } // long commands.. consume bytes from uart case 0xC0: case 0xC4: case 0xC8: case 0xCC: case 0xC1: case 0xC5: case 0xC9: case 0xCD: case 0xC2: case 0xC6: case 0xCA: case 0xCE: case 0x81: case 0x82: ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); break; } } } }
int uart_read_blocking(uart_t uart, char *data) { *data = (char)ROM_UARTCharGet(UART0_BASE); return 1; }