You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.0 KiB

2 years ago
  1. #include "zport.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "zboard.h"
  5. uint8_t g_port_exit_critical_count;
  6. uint32_t sys_haspassedms(uint32_t ticket)
  7. {
  8. uint32_t nowticket = HAL_GetTick();
  9. if (nowticket >= ticket)
  10. {
  11. return nowticket - ticket;
  12. }
  13. return UINT32_MAX - ticket + nowticket;
  14. }
  15. ///**********************************************************************************************************************
  16. // * ===================================================printf重定向=================================================== *
  17. // **********************************************************************************************************************/
  18. int fputc(int ch, FILE *stream)
  19. {
  20. uint8_t c = ch;
  21. HAL_UART_Transmit(&DEBUG_UART, &c, 1, 100);
  22. return ch;
  23. }
  24. ///***********************************************************************************************************************
  25. // * ====================================================调试指示灯===================================================== *
  26. // ***********************************************************************************************************************/
  27. void port_do_debug_light_state(void)
  28. {
  29. static uint32_t lastprocess = 0;
  30. if (sys_haspassedms(lastprocess) > 300)
  31. {
  32. lastprocess = HAL_GetTick();
  33. HAL_GPIO_TogglePin(DEBUG_LIGHT_PORT, DEBUG_LIGHT_PIN);
  34. }
  35. }
  36. /***********************************************************************************************************************
  37. * *************************************************************************************************************** *
  38. ***********************************************************************************************************************/
  39. static uart_t m_uarts[] = {
  40. {&DEBUG_UART, 0}
  41. // {&ENV_SENSOR_UART485, 0},
  42. };
  43. __weak void port_mock_on_uart_rx(uart_t *uart) {}
  44. static void uarts_start_receive(uart_t *uart) { HAL_UART_Receive_IT(uart->uarthandler, &uart->rxbuf, 1); }
  45. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  46. {
  47. for (size_t i = 0; i < (sizeof(m_uarts) / sizeof(uart_t)); i++)
  48. {
  49. if (m_uarts[i].uarthandler == huart)
  50. {
  51. port_mock_on_uart_rx(&m_uarts[i]);
  52. uarts_start_receive(&m_uarts[i]);
  53. return;
  54. }
  55. }
  56. }
  57. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  58. {
  59. for (size_t i = 0; i < (sizeof(m_uarts) / sizeof(uart_t)); i++)
  60. {
  61. if (m_uarts[i].uarthandler == huart)
  62. {
  63. uarts_start_receive(&m_uarts[i]);
  64. return;
  65. }
  66. }
  67. }
  68. // export
  69. void port_uart_start_all_uart_receive(void)
  70. {
  71. for (size_t i = 0; i < (sizeof(m_uarts) / sizeof(uart_t)); i++)
  72. {
  73. uarts_start_receive(&m_uarts[i]);
  74. }
  75. }
  76. void sys_critical_enter(void)
  77. {
  78. if (g_port_exit_critical_count == 0)
  79. {
  80. // __disable_irq();
  81. HAL_NVIC_DisableIRQ(USART1_IRQn);
  82. }
  83. g_port_exit_critical_count++;
  84. }
  85. void sys_critical_exit(void)
  86. {
  87. g_port_exit_critical_count--;
  88. if (g_port_exit_critical_count == 0)
  89. {
  90. // __enable_irq();
  91. HAL_NVIC_EnableIRQ(USART1_IRQn);
  92. }
  93. }