api-hal-console.c 1.8 KB

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