RA2L1產(chǎn)品組基于Arm Cortex-M23 核心(現(xiàn)今 Arm Cortex-M系列中功耗最低的 CPU)。這款產(chǎn)品采用優(yōu)化的制程和瑞薩電子的低功耗工藝技術(shù),是業(yè)界一流水平的超低功耗微控制器。RA2L1產(chǎn)品組能夠支持1.6V至5.5V寬電壓工作,CPU時(shí)鐘頻率最高48MHz,且運(yùn)行模式電流和待機(jī)模式電流更低。RA2L1產(chǎn)品群配備了增強(qiáng)型電容式觸摸感應(yīng)單元(CTSU2)、CAN控制器局域網(wǎng)總線,串行通信接口、高精度模擬電路和定時(shí)器。產(chǎn)品封裝從48引腳到100引腳。
一控制器局域網(wǎng)CAN模塊簡(jiǎn)介
控制器局域網(wǎng)(CAN)模塊使用基于消息的協(xié)議在電磁噪聲應(yīng)用中的多個(gè)從機(jī)和主機(jī)之間接收和傳輸數(shù)據(jù)。
該模塊符合ISO11898-1(CAN2.0A / CAN2.0B)標(biāo)準(zhǔn),最多支持32個(gè)郵箱,可配置為普通郵箱和FIFO模式下的發(fā)送或接收。支持標(biāo)準(zhǔn)(11位)和擴(kuò)展(29位)消息格式。CAN模塊需要額外的外部CAN收發(fā)器。
CAN模塊框圖
注意事項(xiàng):
CAN需要外部高速晶體作為時(shí)鐘源,使用CAN模塊前需要先配置好外部高速時(shí)鐘。
二RA2L1 CAN通信應(yīng)用例程
使用官方e2 studio開發(fā)工具創(chuàng)建RA2L1工程,并添加CAN外設(shè)模塊底層應(yīng)用。
配置CAN模塊參數(shù)(包括通信速率、引腳配置等)。
三CAN應(yīng)用參考代碼
左右滑動(dòng)查看完整內(nèi)容
#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 "); } 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 "); } } 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 "); } } /* wait for transmit flag bit to set */ while ((true != b_can_tx) && (time_out--)); if (0 == time_out) { __asm("BKPT #0 "); } /* 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通信實(shí)測(cè)驗(yàn)證
RA2L1芯片內(nèi)部只有CAN控制器,需要與外部CAN設(shè)備通信時(shí),還需要外接CAN收發(fā)器。
CAN收發(fā)器應(yīng)用參考原理圖
使用USB-CAN工具測(cè)試CAN通信
a. 接收不到ID為0的數(shù)據(jù)
b. 禁止Mask功能后,可接收所有ID的數(shù)據(jù).
審核編輯:湯梓紅
-
mcu
+關(guān)注
關(guān)注
146文章
17123瀏覽量
350979 -
ARM
+關(guān)注
關(guān)注
134文章
9084瀏覽量
367380 -
CAN通信
+關(guān)注
關(guān)注
5文章
93瀏覽量
17829 -
瑞薩
+關(guān)注
關(guān)注
35文章
22308瀏覽量
86238
原文標(biāo)題:瑞薩RA2L1系列CAN通信應(yīng)用
文章出處:【微信號(hào):瑞薩MCU小百科,微信公眾號(hào):瑞薩MCU小百科】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論