main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "string.h"
  2. #include "sys.h"
  3. #include "delay.h"
  4. #include "usart.h"
  5. #include "hal_key.h"
  6. #include "sys_log.h"
  7. #include "sys_queue.h"
  8. #include "sys_timer.h"
  9. #include "sys_ver.h"
  10. #include "hal_led.h"
  11. #include "hal_spi.h"
  12. #include "hal_can.h"
  13. #include "hal_timer.h"
  14. #include "hal_ble.h"
  15. #include "hal_wdg.h"
  16. #include "hal_adc.h"
  17. #include "mod_candataprocess.h"
  18. #include "mod_spi.h"
  19. #include "mod_atcmd.h"
  20. #include "mod_key.h"
  21. #include "mod_sys.h"
  22. #define RX_HEART_CAN_ID_EN 0
  23. #define OBD_HEART_CAN_ID 0x130
  24. #define ISO15765_PARSE
  25. queue_list_t g_s_uart_rx_list;
  26. queue_list_t g_s_spi_rx_list;
  27. queue_list_t g_s_spi_tx_list;
  28. queue_list_t g_s_can_rx_list;
  29. static uint8_t rx_heart_id_cnt = 0;
  30. static void spi_inter_callback(uint8_t type)
  31. {
  32. uint8_t rx_buf[QUEUE_MAX_DATA_LEN];
  33. memset(rx_buf, 0, QUEUE_MAX_DATA_LEN);
  34. HAL_SpiRxData(rx_buf, QUEUE_MAX_DATA_LEN);
  35. queue_in(&g_s_spi_rx_list, rx_buf, QUEUE_MAX_DATA_LEN);
  36. }
  37. static void led_toggle_timer_callback()
  38. {
  39. HAL_LedToggle(LED_BLUE);
  40. }
  41. static void print_log_timer_callback()
  42. {
  43. static uint32_t run_second;
  44. run_second += 5;
  45. DLOG_I("mcu run seconds:%ds", run_second);
  46. if(60*60*24*7 == run_second)
  47. {
  48. run_second = 0;
  49. }
  50. }
  51. void spi_send_callback(uint8_t* data, uint8_t len)
  52. {
  53. queue_in(&g_s_spi_tx_list, data, len);
  54. }
  55. void uart_isr_callback(uint8_t* data, uint8_t len)
  56. {
  57. queue_in(&g_s_uart_rx_list, data, len);
  58. }
  59. //static void key_long_press_callback(void)
  60. //{
  61. // DLOG_I("key_long_pressed");
  62. //}
  63. static void can_rx_callback(uint8_t can_id_type, uint32_t can_id, uint8_t* can_data, uint8_t data_len)
  64. {
  65. //buf格式:can帧id类型,can帧id,can帧长度,can帧数据
  66. //接收到数据以后,将数据放入队列.
  67. uint8_t buf_len = 0;
  68. uint8_t can_buf[32];
  69. // if(CAN_ID_EXT == can_id_type)
  70. // {
  71. // DLOG_E("can_id_type:0x%x", can_id_type);
  72. // return;
  73. // }
  74. #if RX_HEART_CAN_ID_EN
  75. //宝马摩托车的心跳帧为100ms一次,在这做下统计,每3s上报一次.
  76. if(can_id == OBD_HEART_CAN_ID)
  77. {
  78. rx_heart_id_cnt += 1;
  79. if(rx_heart_id_cnt < 30)
  80. {
  81. return;
  82. }
  83. else
  84. {
  85. rx_heart_id_cnt = 0;
  86. }
  87. }
  88. #else
  89. if(can_id == OBD_HEART_CAN_ID)
  90. {
  91. return;
  92. }
  93. #endif
  94. //帧类型
  95. can_buf[0] = can_id_type;
  96. buf_len += 1;
  97. //帧id
  98. memcpy(&can_buf[1], &can_id, 4);
  99. buf_len += 4;
  100. //帧数据包长度
  101. can_buf[5] = data_len;
  102. buf_len += 1;
  103. //帧数据包内容
  104. memcpy(&can_buf[6], can_data, data_len);
  105. buf_len += data_len;
  106. DLOG_I("rx can_id_type:%d,can_id:0x%x,data_len:%d,data:%s", can_id_type, can_id, data_len, SYS_Hex2str(can_data, data_len));
  107. #ifndef ISO15765_PARSE
  108. queue_in(&g_s_can_rx_list, can_buf, buf_len);
  109. #else
  110. //放到iso15765处理队列中
  111. MOD_CanPutDataToIso15765Parse(can_buf, buf_len);
  112. #endif //ISO15765_PARSE
  113. }
  114. static void can_tx_callback(void)
  115. {
  116. // DLOG_I("can_tx_cb");
  117. }
  118. static void can_error_callback(uint32_t err_code)
  119. {
  120. DLOG_E("can_error:0x%x",err_code);
  121. }
  122. int main(void)
  123. {
  124. int err_code;
  125. CAN_RxHeaderTypeDef can_rx_header;
  126. memset(&can_rx_header, 0, sizeof(CAN_RxHeaderTypeDef));
  127. HAL_Init();
  128. Stm32_Clock_Init(RCC_PLL_MUL9);
  129. delay_init(72);
  130. uart_init(115200);
  131. HAL_WdgInit(IWDG_PRESCALER_64, 2000);
  132. HAL_WdgFeed();
  133. queue_init(&g_s_uart_rx_list);
  134. queue_init(&g_s_spi_rx_list);
  135. queue_init(&g_s_spi_tx_list);
  136. #ifndef ISO15765_PARSE
  137. queue_init(&g_s_can_rx_list);
  138. #endif //ISO15765_PARSE
  139. HAL_AdcInit();
  140. HAL_UartIsrCallbackRegister(uart_isr_callback);
  141. Sys_LogPrintCallbackRegister(HAL_Usart1SendData);
  142. HAL_BleEnGpioInit();
  143. HAL_BleGpioEn(true);
  144. HAL_LedInit();
  145. HAL_LedOff(LED_BLUE);
  146. HAL_Timer3Init(10-1, 7200-1);
  147. MOD_SpiDataSendCallbackRegister(spi_send_callback);
  148. HAL_SpiInit(spi_inter_callback);
  149. HAL_CanCallbackRegister(can_rx_callback, can_tx_callback, can_error_callback);
  150. #ifdef ISO15765_PARSE
  151. MOD_CanIso15765ParseInit();
  152. #endif //ISO15765_PARSE
  153. err_code = HAL_CanInit();
  154. if (0 != err_code)
  155. {
  156. DLOG_E("can_init_error");
  157. while (1) {}
  158. }
  159. err_code = HAL_CanStart();
  160. if (0 != err_code)
  161. {
  162. DLOG_E("can_start_error");
  163. while (1) {}
  164. }
  165. MOD_SysInit();
  166. // MOD_KeyInit(key_long_press_callback);
  167. HAL_TimerRegister(led_toggle_timer_callback, 200);
  168. HAL_TimerRegister(print_log_timer_callback, 5000);
  169. DLOG_I("init finished, ver:0x%x", get_ver());
  170. while (1)
  171. {
  172. #ifndef ISO15765_PARSE
  173. QUEUE_MSG_PROCESS(&g_s_can_rx_list, MOD_CanRxDataProcess);
  174. #else
  175. MOD_CanIso15765Parse();
  176. #endif //ISO15765_PARSE
  177. QUEUE_MSG_PROCESS(&g_s_spi_tx_list, HAL_SpiTxData);
  178. QUEUE_MSG_PROCESS(&g_s_spi_rx_list, MOD_SpiDataProcess);
  179. QUEUE_MSG_PROCESS(&g_s_uart_rx_list, MOD_AtCmdDataProcess);
  180. }
  181. }