furi-hal-console.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <furi-hal-console.h>
  2. #include <stdbool.h>
  3. #include <stm32wbxx_ll_gpio.h>
  4. #include <stm32wbxx_ll_usart.h>
  5. #include <m-string.h>
  6. #include <furi.h>
  7. volatile bool furi_hal_console_alive = false;
  8. void furi_hal_console_init() {
  9. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  10. GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
  11. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  12. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  13. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  14. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  15. GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
  16. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  17. LL_USART_InitTypeDef USART_InitStruct = {0};
  18. USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
  19. USART_InitStruct.BaudRate = 230400;
  20. USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  21. USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  22. USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  23. USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;
  24. USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  25. USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  26. LL_USART_Init(USART1, &USART_InitStruct);
  27. LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_2);
  28. LL_USART_EnableFIFO(USART1);
  29. LL_USART_ConfigAsyncMode(USART1);
  30. LL_USART_Enable(USART1);
  31. while(!LL_USART_IsActiveFlag_TEACK(USART1)) ;
  32. furi_hal_console_alive = true;
  33. FURI_LOG_I("FuriHalConsole", "Init OK");
  34. }
  35. static void furi_hal_console_uart_tx(const uint8_t* buffer, size_t buffer_size) {
  36. while(buffer_size > 0) {
  37. while (!LL_USART_IsActiveFlag_TXE(USART1));
  38. LL_USART_TransmitData8(USART1, *buffer);
  39. buffer++;
  40. buffer_size--;
  41. }
  42. }
  43. void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
  44. if (!furi_hal_console_alive)
  45. return;
  46. // Transmit data
  47. furi_hal_console_uart_tx(buffer, buffer_size);
  48. // Wait for TC flag to be raised for last char
  49. while (!LL_USART_IsActiveFlag_TC(USART1));
  50. }
  51. void furi_hal_console_tx_with_new_line(const uint8_t* buffer, size_t buffer_size) {
  52. if (!furi_hal_console_alive)
  53. return;
  54. // Transmit data
  55. furi_hal_console_uart_tx(buffer, buffer_size);
  56. // Transmit new line symbols
  57. furi_hal_console_uart_tx((const uint8_t*)"\r\n", 2);
  58. // Wait for TC flag to be raised for last char
  59. while (!LL_USART_IsActiveFlag_TC(USART1));
  60. }
  61. void furi_hal_console_printf(const char format[], ...) {
  62. string_t string;
  63. va_list args;
  64. va_start(args, format);
  65. string_init_vprintf(string, format, args);
  66. va_end(args);
  67. furi_hal_console_tx((const uint8_t*)string_get_cstr(string), string_size(string));
  68. string_clear(string);
  69. }
  70. void furi_hal_console_puts(const char *data) {
  71. furi_hal_console_tx((const uint8_t*)data, strlen(data));
  72. }