furi-hal-console.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
  36. if (!furi_hal_console_alive)
  37. return;
  38. while(buffer_size > 0) {
  39. while (!LL_USART_IsActiveFlag_TXE(USART1));
  40. LL_USART_TransmitData8(USART1, *buffer);
  41. buffer++;
  42. buffer_size--;
  43. }
  44. /* Wait for TC flag to be raised for last char */
  45. while (!LL_USART_IsActiveFlag_TC(USART1));
  46. }
  47. void furi_hal_console_printf(const char format[], ...) {
  48. string_t string;
  49. va_list args;
  50. va_start(args, format);
  51. string_init_vprintf(string, format, args);
  52. va_end(args);
  53. furi_hal_console_tx((const uint8_t*)string_get_cstr(string), string_size(string));
  54. string_clear(string);
  55. }
  56. void furi_hal_console_puts(const char *data) {
  57. furi_hal_console_tx((const uint8_t*)data, strlen(data));
  58. }