api-hal-console.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <api-hal-console.h>
  2. #include <stdbool.h>
  3. #include <stm32wbxx_ll_gpio.h>
  4. #include <stm32wbxx_ll_usart.h>
  5. #include <furi.h>
  6. volatile bool api_hal_console_alive = false;
  7. void api_hal_console_init() {
  8. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  9. GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
  10. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  11. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  12. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  13. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  14. GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
  15. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  16. LL_USART_InitTypeDef USART_InitStruct = {0};
  17. USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
  18. USART_InitStruct.BaudRate = 230400;
  19. USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  20. USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  21. USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  22. USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;
  23. USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  24. USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  25. LL_USART_Init(USART1, &USART_InitStruct);
  26. LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_2);
  27. LL_USART_EnableFIFO(USART1);
  28. LL_USART_ConfigAsyncMode(USART1);
  29. LL_USART_Enable(USART1);
  30. while(!LL_USART_IsActiveFlag_TEACK(USART1)) ;
  31. api_hal_console_alive = true;
  32. FURI_LOG_I("FuriHalConsole", "Init OK");
  33. }
  34. void api_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
  35. if (!api_hal_console_alive)
  36. return;
  37. while(buffer_size > 0) {
  38. while (!LL_USART_IsActiveFlag_TXE(USART1));
  39. LL_USART_TransmitData8(USART1, *buffer);
  40. buffer++;
  41. buffer_size--;
  42. }
  43. /* Wait for TC flag to be raised for last char */
  44. while (!LL_USART_IsActiveFlag_TC(USART1));
  45. }