RA2L1產品組基于ArmCortex-M23 核心(現今 ArmCortex-M系列中功耗最低的 CPU)。這款產品采用優化的制程和瑞薩電子的低功耗工藝技術,是業界一流水平的超低功耗微控制器。RA2L1產品組能夠支持1.6V至5.5V寬電壓工作,CPU時鐘頻率最高48MHz,且運行模式電流和待機模式電流更低。RA2L1產品群配備了增強型電容式觸摸感應單元(CTSU2)、CAN控制器局域網總線,串行通信接口、高精度模擬電路和定時器。產品封裝從48引腳到100引腳。
控制器局域網CAN模塊簡介
控制器局域網(CAN)模塊使用基于消息的協議在電磁噪聲應用中的多個從機和主機之間接收和傳輸數據。
該模塊符合ISO11898-1(CAN2.0A / CAN2.0B)標準,最多支持32個郵箱,可配置為普通郵箱和FIFO模式下的發送或接收。支持標準(11位)和擴展(29位)消息格式。CAN模塊需要額外的外部CAN收發器。
CAN模塊框圖
RA2L1 CAN通信應用例程
使用官方e2studio開發工具創建RA2L1工程,并添加CAN外設模塊底層應用。
配置CAN模塊參數(包括通信速率、引腳配置等)。
CAN應用參考代碼
#define WAIT_TIME (500U) //wait time value #define CAN_MAILBOX_NUMBER_TX (0U) //mail box number #define CAN_MAILBOX_NUMBER_RX (1U) #define CAN_FRAME_TRANSMIT_DATA_BYTES (8U) //data length #define ZERO (0U) /* Private global variables*/ /* Flags, set from Callback function */ static volatile bool b_can_tx = false; //CAN transmission status static volatile bool b_can_rx = false; //CAN receive status static volatile bool b_can_err = false; //CAN error status /* CAN frames for tx and rx */ static can_frame_t g_can_tx_frame; //CAN transmit frame static can_frame_t g_can_rx_frame; //CAN receive frame void hal_entry(void) { /* TODO: add your own code here */ fsp_err_t err = FSP_SUCCESS; uint32_t time_out = WAIT_TIME; // time out uint8_t can_tx_msg[CAN_FRAME_TRANSMIT_DATA_BYTES] = {0,1,2,3,4,5,6,7}; uint8_t can_rx_msg[CAN_FRAME_TRANSMIT_DATA_BYTES] = {0}; /* Initializes the CGC module. */ err = R_CGC_Open(&g_cgc0_ctrl, &g_cgc0_cfg); /* Handle any errors. This function should be defined by the user. */ assert(FSP_SUCCESS == err); /* Start the CGC_CLOCK_MAIN_OSC. */ err = R_CGC_ClockStart(&g_cgc0_ctrl, CGC_CLOCK_MAIN_OSC, NULL); assert(FSP_SUCCESS == err); /* Initialize CAN module */ err = R_CAN_Open(&g_can_ctrl, &g_can_cfg); /* Error trap */ if(FSP_SUCCESS != err) { __asm("BKPT #0\n"); } g_can_tx_frame.id = CAN_MAILBOX_NUMBER_TX; g_can_tx_frame.type = CAN_FRAME_TYPE_DATA; g_can_tx_frame.data_length_code = CAN_FRAME_TRANSMIT_DATA_BYTES; /* copy the tx data frame with TX_MSG */ memcpy((uint8_t*)&g_can_tx_frame.data[ZERO], (uint8_t*)&can_tx_msg[ZERO], CAN_FRAME_TRANSMIT_DATA_BYTES); err = R_CAN_Write(&g_can_ctrl, CAN_MAILBOX_NUMBER_TX, &g_can_tx_frame); /* Error trap */ if (FSP_SUCCESS != err) { err = R_CAN_Close(&g_can_ctrl); if (FSP_SUCCESS != err) { __asm("BKPT #0\n"); } } while(1) { /* check if receive flag is set */ if (true == b_can_rx) { /* Reset flag bit */ b_can_rx = false; g_can_rx_frame.data[CAN_FRAME_TRANSMIT_DATA_BYTES-1] = g_can_rx_frame.id; /* Transmit the rx data frame as acknowledging the data transfer is successful */ err = R_CAN_Write (&g_can_ctrl, CAN_MAILBOX_NUMBER_TX, &g_can_rx_frame); /* Error trap */ if (FSP_SUCCESS != err) { err = R_CAN_Close(&g_can_ctrl); if (FSP_SUCCESS != err) { __asm("BKPT #0\n"); } } /* wait for transmit flag bit to set */ while ((true != b_can_tx) && (time_out--)); if (0 == time_out) { __asm("BKPT #0\n"); } /* Reset flag bit */ b_can_tx = false; } } } void can_callback(can_callback_args_t *p_args) { switch (p_args->event) { case CAN_EVENT_TX_COMPLETE: { b_can_tx = true; //set flag bit break; } case CAN_EVENT_RX_COMPLETE: { b_can_rx = true; //copy the received data to rx_frame memcpy(&g_can_rx_frame, p_args->p_frame, sizeof(can_frame_t)); break; } case CAN_EVENT_MAILBOX_MESSAGE_LOST: //overwrite/overrun error event case CAN_EVENT_BUS_RECOVERY: //Bus recovery error event case CAN_EVENT_ERR_BUS_OFF: //error Bus Off event case CAN_EVENT_ERR_PASSIVE: //error passive event case CAN_EVENT_ERR_WARNING: //error warning event { b_can_err = true; //set flag bit break; } } }
CAN通信實測驗證
RA2L1芯片內部只有CAN控制器,需要與外部CAN設備通信時,還需要外接CAN收發器。
CAN收發器應用參考原理圖
使用USB-CAN工具測試CAN通信
a. 接收不到ID為0的數據
b. 禁止Mask功能后,可接收所有ID的數據.
-
mcu
+關注
關注
146文章
16675瀏覽量
347854 -
CAN通信
+關注
關注
5文章
93瀏覽量
17757 -
瑞薩
+關注
關注
33文章
22274瀏覽量
85520
發布評論請先 登錄
相關推薦
評論