|
【瑞萨RA4系列开发板体验】CAN总线测试
作者:lugl
在前一篇printf的基础上增加CAN的配置,根据@jyaxz 的帖子以及example中can的示例修改而成:
1、在keil中配置好can,然后新建bsp_can.c、bsp_can.h,函数代码如下:
bsp_can.c
- #include "bsp_can.h"
- #include "bsp_debug_uart.h"
- #define RESET_VALUE (0x00)
- /***********************************************************************************************************************
- * Private global variables
- **********************************************************************************************************************/
- /* Flags, set from Callback function */
- #define WAIT_TIME (5000U) //wait time value
- #define CAN_DESTINATION_MAILBOX_3 (3U) //destination mail box number
- #define CAN_MAILBOX_NUMBER_0 (0U) //mail box number
- #define CAN_FRAME_TRANSMIT_DATA_BYTES (8U) //data length
- #define ZERO (0U)
- #define NULL_CHAR ('\0')
- /***********************************************************************************************************************
- * 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 = {0x00}; //CAN transmit frame
- static can_frame_t g_can_rx_frame = {0x00}; //CAN receive frame
- /***********************************************************************************************************************
- * Private local functions
- **********************************************************************************************************************/
- static void can_deinit(void);
- void R_BSP_WarmStart(bsp_warm_start_event_t event);
- static void can_deinit(void)
- {
- fsp_err_t err = FSP_SUCCESS;
- err = R_CAN_Close(&g_can0_ctrl);
- if (FSP_SUCCESS != err)
- {
- printf("\r\n **CAN Close API failed**");
- }
- }
- /* 调试串口 UART4 初始化 */
- void bsp_can0_init(void)
- {
- fsp_err_t err = FSP_SUCCESS;
- /* Initialize CAN module */
- err = R_CAN_Open(&g_can0_ctrl, &g_can0_cfg);
- /* Error trap */
- if(FSP_SUCCESS != err)
- {
- printf("\r\nCAN Open API failed");
- printf(err);
- }
- }
- /*******************************************************************************************************************//**
- * This function is called when an CAN event is occurred and SET the respective flags.
- **********************************************************************************************************************/
- 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;
- memcpy(&g_can_rx_frame, &p_args->frame, sizeof(can_frame_t)); //copy the received data to rx_frame
- break;
- }
- // case CAN_EVENT_MAILBOX_MESSAGE_LOST: //overwrite/overrun error event
- // break;
- // 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
- // case CAN_EVENT_ERR_BUS_LOCK: //error bus lock
- // case CAN_EVENT_ERR_CHANNEL: //error channel
- // case CAN_EVENT_ERR_GLOBAL: //error global
- // case CAN_EVENT_TX_ABORTED: //error transmit abort
- // case CAN_EVENT_TX_FIFO_EMPTY: //error transmit FIFO is empty
- // case CAN_EVENT_FIFO_MESSAGE_LOST: //error FIFO message lost
- // {
- // b_can_err = true; //set flag bit
- // break;
- // }
- }
- }
- void can_send(void)
- {
- fsp_err_t err = FSP_SUCCESS;
- uint32_t time_out = WAIT_TIME; // time out
- fsp_pack_version_t version = {RESET_VALUE};
- uint8_t can_tx_msg[CAN_FRAME_TRANSMIT_DATA_BYTES] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; //data to be load in tx_frame while transmitting
- uint8_t can_rx_msg[CAN_FRAME_TRANSMIT_DATA_BYTES] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; //data to be load in rx_frame while acknowledging
- /* Initialize CAN module */
- err = R_CAN_Open(&g_can0_ctrl, &g_can0_cfg);
- /* Error trap */
- if(FSP_SUCCESS != err)
- {
- printf("\r\nCAN Open API failed");
- printf(err);
- }
- printf("\r\nTo start CAN transmission, please enter any key on RTTViewer\r\n");
- g_can_tx_frame.id = CAN_DESTINATION_MAILBOX_3;
- 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_can0_ctrl, CAN_MAILBOX_NUMBER_0, &g_can_tx_frame);
- if (FSP_SUCCESS != err)
- {
- printf("\r\n CAN Write API FAILED");
- can_deinit();
- printf(err);
- }
- /* wait for transmit flag bit to set */
- while ((true != b_can_tx) && (--time_out));
- if (RESET_VALUE == time_out)
- {
- printf("CAN transmission failed due to timeout");
- printf(true);
- }
- /* Reset flag bit */
- b_can_tx = false;
- printf("\r\n CAN transmission is successful");
- printf("\r\n To start CAN transmission, please enter any key on RTTViewer\n");
-
- }
复制代码 bsp_can.h:
- #ifndef __BSP_CAN_H__
- #define __BSP_CAN_H__
- #include "hal_data.h"
- #include "stdio.h"
- void bsp_can0_init(void);
- void can_send(void);
- #endif
复制代码 修如hal_enty.c如下:
- #include "hal_data.h"
- #include <stdio.h>
- #include "bsp_debug_uart.h"
- #include "bsp_can.h"
- FSP_CPP_HEADER
- void R_BSP_WarmStart(bsp_warm_start_event_t event);
- FSP_CPP_FOOTER
- /*******************************************************************************************************************//**
- * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
- * is called by main() when no RTOS is used.
- **********************************************************************************************************************/
- void hal_entry(void)
- {
- bsp_uart_init();
- bsp_can0_init();
- char char_i[]="hello e2studio";
- int int_i=55;
- float float_i=66.20f;
- printf("hello RA4\r\n");
- while(1)
- {
- can_send();
- printf("int_i=%d\n",int_i);
- printf("float_i=%.2f\n",float_i);
- printf("char_i='%s'\n",char_i);
- R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
- // NOLINT100->160
- }
- #if BSP_TZ_SECURE_BUILD
- /* Enter non-secure code */
- R_BSP_NonSecureEnter();
- #endif
- }
- /*******************************************************************************************************************//**
- * This function is called at various points during the startup process. This implementation uses the event that is
- * called right before main() to set up the pins.
- *
- * @param[in] event Where at in the start up process the code is currently at
- **********************************************************************************************************************/
- void R_BSP_WarmStart (bsp_warm_start_event_t event)
- {
- if (BSP_WARM_START_RESET == event)
- {
- #if BSP_FEATURE_FLASH_LP_VERSION != 0
- /* Enable reading from data flash. */
- R_FACI_LP->DFLCTL = 1U;
- /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
- * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
- #endif
- }
- if (BSP_WARM_START_POST_C == event)
- {
- /* C runtime environment and system clocks are setup. */
- /* Configure pins. */
- R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
- }
- }
- #if BSP_TZ_SECURE_BUILD
- BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
- /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
- BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
- {
- }
- #endif
复制代码 下载后,用TTL转CAN与CAN分析仪接好,效果如下:
【注】本来上面截了好多图的,一不小心按了一下返回,结果都没有了,最好只能把代码帖出来。
下一步,与迪文屏进行can通信,展示数据。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|