今天添加了freertos,并且进行了event触发的测试,led灯的状态表明事件触发有效,同时上位机通过usb2can成功接收每次的can数据,测试基本成功。
void Can1TaskFunc(void *argument)
{
/* USER CODE BEGIN Can1TaskFunc */
EventBits_t r_event = pdPASS;
/* Infinite loop */
for(;;)
{
r_event = osEventFlagsWait(SPI_EventHandle, EVENT1, osFlagsWaitAny, portMAX_DELAY);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
CANx_SendStdData(&hcan1, 0x01, tx_data, 8);
delay_us(20);
CANx_SendStdData(&hcan1, 0x02, tx_data, 8);
delay_us(20);
CANx_SendStdData(&hcan1, 0x03, tx_data, 8);
delay_us(20);
osEventFlagsSet(CAN1_EventHandle, EVENT1);
}
/* USER CODE END Can1TaskFunc */
}
void Can2TaskFunc(void *argument)
{
/* USER CODE BEGIN Can2TaskFunc */
EventBits_t r_event = pdPASS;
/* Infinite loop */
for(;;)
{
r_event = osEventFlagsWait(SPI_EventHandle, EVENT2, osFlagsWaitAny, portMAX_DELAY);
CANx_SendStdData(&hcan2, 0x01, tx_data, 8);
delay_us(20);
CANx_SendStdData(&hcan2, 0x02, tx_data, 8);
delay_us(20);
CANx_SendStdData(&hcan2, 0x03, tx_data, 8);
delay_us(20);
osEventFlagsSet(CAN2_EventHandle, EVENT1);
}
/* USER CODE END Can2TaskFunc */
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == SPI_CS_Pin)
{
//HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_SPI_TransmitReceive(&hspi1, spi_tx, spi_rx, 132, 100);
hspi1.Instance->DR = 0x00;
// decode rx
memcpy(&g_cmd, spi_rx, sizeof(spine_cmd_t));
uint32_t crc = calculate((uint8_t*)&g_cmd, sizeof(spine_cmd_t) - 4);
if(crc != g_cmd.crc)
{
HAL_UART_Transmit(&huart2, "crc error", 10, 10000);
//HAL_UART_Transmit(&huart2, (uint8_t*) &g_cmd, sizeof(spine_cmd_t), 10000);
}
// trigger event to main task
osEventFlagsSet(SPI_EventHandle, EVENT1);
osEventFlagsSet(SPI_EventHandle, EVENT2);
// copy data to tx
g_state.state[0].state[0].p = 1;
g_state.state[0].state[0].v = 2;
g_state.state[0].state[0].t = 3;
g_state.crc = calculate((uint8_t*)&g_state, sizeof(spine_state_t) - 4);
memcpy(spi_tx, &g_state, sizeof(spine_state_t));
//HAL_UART_Transmit(&huart2, tx, sizeof(spine_state_t), 10000);
}
}