测试AG32VH407开发板的CAN收发。
一、硬件部分
1.1、CAN收发端口
使用开发板的IO29(CAN_TX)和IO33(CAN_RX)
1.2、CAN收发器
IO29和IO33引脚连接到收发器的TXD和RXD,使用的收发器型号是TJA1050。
二、程序部分
2.1、引脚配置
修改example_board.ve文件,增加CAN引脚定义
2.2、fun_can.c
设置CAN波特率1Mbps,接收中断。
- #include "board.h"
- #include "can/fun_can.h"
-
- void can_txdat(uint32_t can_id, CAN_FrameFormatTypeDef frame_format, CAN_RemoteTypeDef rtr, CAN_DataLengthTypeDef dlc, const char *data_bytes)
- {
- CAN_TxMessageTypeDef tx_msg;
- tx_msg.ID = can_id;
- tx_msg.FF = frame_format;
- tx_msg.DLC = dlc;
- tx_msg.RTR = rtr;
- for (int i = 0; i < dlc; ++i) {
- tx_msg.Data[i] = data_bytes[i];
- }
-
- CAN_Transmit(CAN0, &tx_msg);
- }
-
- void CAN0_isr()
- {
- uint32_t can_ir = CAN_GetIntStatus(CAN0);
- if (can_ir & CAN_IR_TI)
- {
- printf("can tx done\n");
- }
- if (can_ir & CAN_IR_RI)
- {
- CAN_RxMessageTypeDef rx_msg;
-
- CAN_Receive(CAN0, &rx_msg);
-
- printf("can rx data:");
- for (int i = 0; i < rx_msg.DLC; ++i)
- {
- printf(" %02x", rx_msg.Data[i]);
- }
- printf("\r\n");
-
- can_txdat(rx_msg.ID+1, CAN_EXTENDTED_FRAME_FORMAT, CAN_DATA_FRAME, CAN_DATA_LENGTH_8, rx_msg.Data);
-
- }
- }
-
- void init_can(void)
- {
- CAN_InitTypeDef init;
- int baud_rate = 1000000;
-
- PERIPHERAL_ENABLE(CAN, 0);
- INT_EnableIRQ(CAN0_IRQn, CAN_PRIORITY);
-
- CAN_StructInit(&init);
- init.SJW = CAN_SJW_1TQ;
- init.TSeg1 = CAN_TSEG1_6TQ;
- init.TSeg2 = CAN_TSEG2_3TQ;
- init.Prescaler = CAN_CalculatePrescaler(SYS_GetPclkFreq(), baud_rate, init.TSeg1, init.TSeg2);
-
- CAN_Init(CAN0, &init);
- CAN_EnableIntRx(CAN0);
-
- CAN_EnableIntErrorWarning(CAN0);
-
- }
-
-
2.3、fun_can.h
- #ifndef __FUN_TASK_H
- #define __FUN_TASK_H
-
- void task_create(void);
-
- #endif
-
-
-
2.4、fun_task.c
- #include "example.h"
- #include "led/led.h"
- #include "key/key.h"
- #include "timer/mtimer.h"
- #include "lcd/jlx12864g.h"
- #include "oled/oled.h"
- #include "rtc/fun_rtc.h"
- #include "can/fun_can.h"
-
- #include "FreeRTOS.h"
- #include "task.h"
- #include "queue.h"
- #include "timers.h"
- #include "semphr.h"
-
- #define START_TASK_PRO 1
- #define START_STK_SIZE 512
- TaskHandle_t StartTask_Handler;
-
- #define TASK1_PRIO 4
- #define TASK1_STK_SIZE 128
- static TaskHandle_t Task1Task_Handler = NULL;
-
- #define TASK2_PRIO 3
- #define TASK2_STK_SIZE 256
- static TaskHandle_t Task2Task_Handler = NULL;
-
- #define TASK3_PRIO 5
- #define TASK3_STK_SIZE 256
- static TaskHandle_t Task3Task_Handler = NULL;
-
- SemaphoreHandle_t semaphore_bin_rtc;
-
-
- void start_task(void *pvParameters);
- void task1(void *pvParameters);
- void task2(void *pvParameters);
- void task3(void *pvParameters);
-
- void task_create(void)
- {
-
- xTaskCreate((TaskFunction_t )start_task,
- (const char* )"start_task",
- (uint16_t )START_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )START_TASK_PRO,
- (TaskHandle_t* )&StartTask_Handler);
-
- vTaskStartScheduler();
- }
-
- void start_task(void *pvParameters)
- {
- taskENTER_CRITICAL();
-
- semaphore_bin_rtc = xSemaphoreCreateBinary();
-
- xTaskCreate((TaskFunction_t )task1,
- (const char* )"task1",
- (uint16_t )TASK1_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )TASK1_PRIO,
- (TaskHandle_t* )&Task1Task_Handler);
-
- xTaskCreate((TaskFunction_t )task2,
- (const char* )"task2",
- (uint16_t )TASK2_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )TASK2_PRIO,
- (TaskHandle_t* )&Task2Task_Handler);
-
- xTaskCreate((TaskFunction_t )task3,
- (const char* )"task3",
- (uint16_t )TASK3_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )TASK3_PRIO,
- (TaskHandle_t* )&Task3Task_Handler);
-
- vTaskDelete(StartTask_Handler);
- taskEXIT_CRITICAL();
- }
-
-
- void task1(void *pvParameters)
- {
- while (1)
- {
-
- vTaskDelay(200);
- }
- }
-
-
- void task2(void *pvParameters)
- {
- init_can();
- while (1)
- {
-
- vTaskDelay(500);
- }
- }
-
-
- void task3(void *pvParameters)
- {
- BaseType_t err = pdFALSE;
- init_oled();
- oled_test();
- init_rtc();
- while (1)
- {
- if(semaphore_bin_rtc != NULL)
- {
- err = xSemaphoreTake(semaphore_bin_rtc, portMAX_DELAY);
- if(err == pdTRUE)
- {
- rtc_disp();
- oled_showstr(0,4,rtc_date1,2);
- oled_showstr(0,6,rtc_date2,2);
- memset(rtc_date1, 0, sizeof(rtc_date1));
- memset(rtc_date2, 0, sizeof(rtc_date2));
- }
- else if(err == pdFALSE)
- {
- vTaskDelay(10);
- }
- }
- }
- }
-
- void vApplicationTickHook(void)
- {
- return;
- }
-
- void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
- StackType_t **ppxIdleTaskStackBuffer,
- uint32_t *pulIdleTaskStackSize)
- {
- static StaticTask_t xIdleTaskTCB;
- static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE];
-
- *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
- *ppxIdleTaskStackBuffer = uxIdleTaskStack;
- *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
- }
-
- void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,
- StackType_t **ppxTimerTaskStackBuffer,
- uint32_t *pulTimerTaskStackSize)
- {
- static StaticTask_t xTimerTaskTCB;
- static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH];
-
- *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
- *ppxTimerTaskStackBuffer = uxTimerTaskStack;
- *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
- }
三、运行结果
编译后,下载ve和code代码后,CAN通信收发测试。开发板接收到的ID数据据,地址+1后重新从CAN发出。
|