| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "string.h"
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "hal_key.h"
- #include "sys_log.h"
- #include "sys_queue.h"
- #include "sys_timer.h"
- #include "sys_ver.h"
- #include "hal_led.h"
- #include "hal_spi.h"
- #include "hal_can.h"
- #include "hal_timer.h"
- #include "hal_ble.h"
- #include "hal_wdg.h"
- #include "hal_adc.h"
- #include "mod_candataprocess.h"
- #include "mod_spi.h"
- #include "mod_atcmd.h"
- #include "mod_key.h"
- #include "mod_sys.h"
- #define RX_HEART_CAN_ID_EN 1
- #define OBD_HEART_CAN_ID 0x130
- queue_list_t g_s_uart_rx_list;
- queue_list_t g_s_spi_rx_list;
- queue_list_t g_s_spi_tx_list;
- queue_list_t g_s_can_rx_list;
- static uint8_t rx_heart_id_cnt = 0;
- static void spi_inter_callback(uint8_t type)
- {
- uint8_t rx_buf[QUEUE_MAX_DATA_LEN];
- memset(rx_buf, 0, QUEUE_MAX_DATA_LEN);
- HAL_SpiRxData(rx_buf, QUEUE_MAX_DATA_LEN);
- queue_in(&g_s_spi_rx_list, rx_buf, QUEUE_MAX_DATA_LEN);
- }
- static void led_toggle_timer_callback()
- {
- HAL_LedToggle(LED_BLUE);
- }
- static void print_log_timer_callback()
- {
- static uint32_t run_second;
- run_second += 5;
- DLOG_I("mcu run seconds:%ds", run_second);
- if(60*60*24*7 == run_second)
- {
- run_second = 0;
- }
- }
- void spi_send_callback(uint8_t* data, uint8_t len)
- {
- queue_in(&g_s_spi_tx_list, data, len);
- }
- void uart_isr_callback(uint8_t* data, uint8_t len)
- {
- queue_in(&g_s_uart_rx_list, data, len);
- }
- //static void key_long_press_callback(void)
- //{
- // DLOG_I("key_long_pressed");
- //}
- static void can_rx_callback(uint8_t can_id_type, uint32_t can_id, uint8_t* can_data, uint8_t data_len)
- {
- //buf格式:can帧id类型,can帧id,can帧长度,can帧数据
- //接收到数据以后,将数据放入队列.
- uint8_t buf_len = 0;
- uint8_t can_buf[32];
- // if(CAN_ID_EXT == can_id_type)
- // {
- // DLOG_E("can_id_type:0x%x", can_id_type);
- // return;
- // }
- #if RX_HEART_CAN_ID_EN
- //宝马摩托车的心跳帧为100ms一次,在这做下统计,每3s上报一次.
- if(can_id == OBD_HEART_CAN_ID)
- {
- rx_heart_id_cnt += 1;
- if(rx_heart_id_cnt < 30)
- {
- return;
- }
- else
- {
- rx_heart_id_cnt = 0;
- }
- }
- #endif
- //帧类型
- can_buf[0] = can_id_type;
- buf_len += 1;
- //帧id
- memcpy(&can_buf[1], &can_id, 4);
- buf_len += 4;
- //帧数据包长度
- can_buf[5] = data_len;
- buf_len += 1;
- //帧数据包内容
- memcpy(&can_buf[6], can_data, data_len);
- buf_len += data_len;
- queue_in(&g_s_can_rx_list, can_buf, buf_len);
- }
- static void can_tx_callback(void)
- {
- // DLOG_I("can_tx_cb");
- }
- static void can_error_callback(uint32_t err_code)
- {
- DLOG_E("can_error:0x%x",err_code);
- }
- int main(void)
- {
- int err_code;
- CAN_RxHeaderTypeDef can_rx_header;
- memset(&can_rx_header, 0, sizeof(CAN_RxHeaderTypeDef));
- HAL_Init();
- Stm32_Clock_Init(RCC_PLL_MUL9);
- delay_init(72);
- uart_init(115200);
- HAL_WdgInit(IWDG_PRESCALER_64, 2000);
- HAL_WdgFeed();
- queue_init(&g_s_uart_rx_list);
- queue_init(&g_s_spi_rx_list);
- queue_init(&g_s_spi_tx_list);
- queue_init(&g_s_can_rx_list);
- HAL_AdcInit();
- HAL_UartIsrCallbackRegister(uart_isr_callback);
- Sys_LogPrintCallbackRegister(HAL_Usart1SendData);
- HAL_BleEnGpioInit();
- HAL_BleGpioEn(true);
- HAL_LedInit();
- HAL_LedOff(LED_BLUE);
- HAL_Timer3Init(10-1, 7200-1);
- MOD_SpiDataSendCallbackRegister(spi_send_callback);
- HAL_SpiInit(spi_inter_callback);
- HAL_CanCallbackRegister(can_rx_callback, can_tx_callback, can_error_callback);
- err_code = HAL_CanInit();
- if (0 != err_code)
- {
- DLOG_E("can_init_error");
- while (1) {}
- }
- err_code = HAL_CanStart();
- if (0 != err_code)
- {
- DLOG_E("can_start_error");
- while (1) {}
- }
- MOD_SysInit();
- // MOD_KeyInit(key_long_press_callback);
- HAL_TimerRegister(led_toggle_timer_callback, 200);
- HAL_TimerRegister(print_log_timer_callback, 5000);
- DLOG_I("init finished, ver:0x%x", get_ver());
- while (1)
- {
- QUEUE_MSG_PROCESS(&g_s_can_rx_list, MOD_CanRxDataProcess);
- QUEUE_MSG_PROCESS(&g_s_spi_tx_list, HAL_SpiTxData);
- QUEUE_MSG_PROCESS(&g_s_spi_rx_list, MOD_SpiDataProcess);
- QUEUE_MSG_PROCESS(&g_s_uart_rx_list, MOD_AtCmdDataProcess);
- }
- }
|