int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init USART, Pins not initialized yet, 921600 bauds */ TM_USART_Init(USART6, TM_USART_PinsPack_Custom, 921600); /* Put test string */ TM_USART_Puts(USART6, "Hello world\n"); while (1) { /* Check if anything received */ while (!TM_USART_BufferEmpty(USART6)) { /* Send data back from buffer */ TM_USART_Putc(USART6, TM_USART_Getc(USART6)); } } }
int main(void) { /* Init system */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init delay */ TM_DELAY_Init(); /* Init USB peripheral */ TM_USB_Init(); /* Init VCP on HS port */ TM_USBD_CDC_Init(TM_USB_HS); /* Start USB device mode on HS port */ TM_USBD_Start(TM_USB_HS); while (1) { /* Process USB CDC device, send remaining data if needed */ /* It is better if you call this in periodic timer, like each ms in SYSTICK handler */ TM_USBD_CDC_Process(TM_USB_HS); /* Check if device is ready, if drivers are installed if needed on HS port */ if (TM_USBD_IsDeviceReady(TM_USB_HS) == TM_USBD_Result_Ok) { /* Turn on green LED */ TM_DISCO_LedOn(LED_GREEN); /* Check if user has changed parameters for COM port */ TM_USBD_CDC_GetSettings(TM_USB_HS, &USB_Settings); /* Check if settings updated from user terminal */ if (USB_Settings.Updated) { /* Reinit USART, only baudrate works in this example */ TM_USART_Init(USART6, TM_USART_PinsPack_1, USB_Settings.Baudrate); } /* Check if anything received on HS port from user */ while (TM_USBD_CDC_Getc(TM_USB_HS, &ch)) { /* One character received */ /* Send character to USART */ TM_USART_Putc(USART6, ch); } /* CHeck if any character received from USART */ while (!TM_USART_BufferEmpty(USART6)) { /* Send data over USB CDC */ TM_USBD_CDC_Putc(TM_USB_HS, TM_USART_Getc(USART6)); } } else { /* Turn off green LED */ TM_DISCO_LedOff(LED_GREEN); } } }
int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init USART, TX: PC6, RX: PC7, 921600 bauds */ TM_USART_Init(USART2, TM_USART_PinsPack_1, 115200); /* Put test string */ TM_USART_Puts(USART2, "Hello world\n"); while (1) { /* Check if we have string "OK" in USART6 buffer */ if (TM_USART_FindString(USART2, "OK")) { /* Send data back from buffer */ while (!TM_USART_BufferEmpty(USART2)) { /* Send to computer */ TM_USART_Putc(USART2, TM_USART_Getc(USART6)); } } } }
uint16_t TM_USART_Gets(USART_TypeDef* USARTx, char* buffer, uint16_t bufsize) { uint16_t i = 0; /* Check for any data on USART */ if (TM_USART_BufferEmpty(USARTx) || !TM_USART_FindCharacter(USARTx, '\n')) { return 0; } /* If available buffer size is more than 0 characters */ while (i < (bufsize - 1)) { /* We have available data */ buffer[i] = (char) TM_USART_Getc(USARTx); /* Check for end of string */ if (buffer[i] == '\n') { /* Done */ break; } /* Next index */ i++; } /* Add zero to the end of string */ buffer[++i] = 0; return (i); }
TM_GPS_Result_t TM_GPS_Update(TM_GPS_Data_t* GPS_Data) { if (!TM_USART_BufferEmpty(GPS_USART)) { while (!TM_USART_BufferEmpty(GPS_USART)) { TM_GPS_INT_Do(GPS_Data, (char)TM_USART_Getc(GPS_USART)); if (GPS_Data->Status == TM_GPS_Result_NewData) { return GPS_Data->Status; } } } if (TM_GPS_FirstTime) { /* No any valid data, return First Data Waiting */ /* Returning only after power up and calling when no all data is received */ return TM_GPS_INT_ReturnWithStatus(GPS_Data, TM_GPS_Result_FirstDataWaiting); } /* We have old data */ return TM_GPS_INT_ReturnWithStatus(GPS_Data, TM_GPS_Result_OldData); }
/* Handle USART6 stream input = custom function name, linked with USART6 stream in the beginning of main() function */ int USART6_Stream_InputFunction(FILE* f) { /* If any data at USART, return them */ /* Do your custom implementation here when string ends */ if (!TM_USART_BufferEmpty(USART6)) { return (int)TM_USART_Getc(USART6); } /* End of data, string is valid */ /* You have to send -1 at the end of string */ return -1; }
/* Handle stdin actions */ int TM_STDIO_StdinHandler(FILE* f) { /* If any data at USART, return them */ /* Do your custom implementation here when string ends */ if (!TM_USART_BufferEmpty(USART1)) { return (int)TM_USART_Getc(USART1); } /* End of data, string is valid */ /* You have to send -1 at the end of string */ return -1; }
uint16_t TM_USART_Gets(USART_TypeDef* USARTx, char* buffer, uint16_t bufsize) { uint16_t i = 0; uint8_t eol = 0; if (TM_USART_BufferEmpty(USARTx)) { return 0; } if (bufsize > 0) { while (!eol) { while (TM_USART_BufferEmpty(USARTx)); buffer[i] = (char) TM_USART_Getc(USARTx); if (buffer[i] == '\n') { eol = 1; } else { if (i < (bufsize - 1)) { i++; } } } //Add zero to the end of string buffer[i] = 0; } return (i); }