S32K148 CAN_PAL SDK_RTM300 PE配置

前言

S32K148----SDK筆記----CAN收發
前面寫過這個S32K flexcan組件的用法, 其實有對flexcan進一步封裝的can_pal組件, 本篇就介紹下它的用法. 以CAN0(PTE4, PTE5)爲例, 不用CANFD.

建立工程

打開S32 Design Studio for ARM, 接下來步驟:

  • File -> New -> S32DS Application Project
  • Processors 選擇 S32K148, Project Name 填你自己的工程名
  • Select SDK: SDKs 選擇 S32K148_SDK 3.0.0, Debugger選擇J-Link, Finish.

ProcessorExpert配置

雙擊工程名, 點擊Components窗口Components目錄下的 pin_mux:PinSetting, CAN配置:
在這裏插入圖片描述
Component Library中雙擊添加一組can_pal組件(用幾路CAN就添加幾組):
在這裏插入圖片描述
can_pal1:can_pal組件默認配置如下:
在這裏插入圖片描述
上圖中默認的位速率500kbit/s, Number of buffers可選[1, 32], 默認16, 可以設置接收16個CAN ID, 改成32可以接收32個CAN ID, 一般情況下是夠用的.

如果不夠用, 可以勾選Enable Rx FIFO extension進行濾波器的片段(mask)過濾, 還不太會使, 如果想接收所有CAN ID, 這個, 暫時不太清楚怎麼設置, 有了解的麻煩評論下.

RXFIFO ID filters number 範圍(8, 16, 24, …, 104, 112, 120, 128) Rx FIFO Filters, 但其實最多104, 且這個數值越大, Number of buffers的最大值越小(會變紅, 鼠標懸停提示有最大值).

RxFIFO ID filter formatFormat A, B, C, D四種選擇, 解釋可參考:

/*! @brief FlexCAN Rx FIFO filters number
 * Implements : flexcan_rx_fifo_id_filter_num_t_Class
 */
typedef enum {
    FLEXCAN_RX_FIFO_ID_FILTERS_8   = 0x0,         /*!<   8 Rx FIFO Filters. @internal gui name="8 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_16  = 0x1,         /*!<  16 Rx FIFO Filters. @internal gui name="16 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_24  = 0x2,         /*!<  24 Rx FIFO Filters. @internal gui name="24 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_32  = 0x3,         /*!<  32 Rx FIFO Filters. @internal gui name="32 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_40  = 0x4,         /*!<  40 Rx FIFO Filters. @internal gui name="40 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_48  = 0x5,         /*!<  48 Rx FIFO Filters. @internal gui name="48 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_56  = 0x6,         /*!<  56 Rx FIFO Filters. @internal gui name="56 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_64  = 0x7,         /*!<  64 Rx FIFO Filters. @internal gui name="64 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_72  = 0x8,         /*!<  72 Rx FIFO Filters. @internal gui name="72 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_80  = 0x9,         /*!<  80 Rx FIFO Filters. @internal gui name="80 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_88  = 0xA,         /*!<  88 Rx FIFO Filters. @internal gui name="88 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_96  = 0xB,         /*!<  96 Rx FIFO Filters. @internal gui name="96 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_104 = 0xC,         /*!< 104 Rx FIFO Filters. @internal gui name="104 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_112 = 0xD,         /*!< 112 Rx FIFO Filters. @internal gui name="112 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_120 = 0xE,         /*!< 120 Rx FIFO Filters. @internal gui name="120 Rx FIFO Filters" */
    FLEXCAN_RX_FIFO_ID_FILTERS_128 = 0xF          /*!< 128 Rx FIFO Filters. @internal gui name="128 Rx FIFO Filters" */
} flexcan_rx_fifo_id_filter_num_t;

/*! @brief FlexCAN Rx mask type.
 * Implements : flexcan_rx_mask_type_t_Class
 */
typedef enum {
    FLEXCAN_RX_MASK_GLOBAL,      /*!< Rx global mask*/
    FLEXCAN_RX_MASK_INDIVIDUAL   /*!< Rx individual mask*/
} flexcan_rx_mask_type_t;

/*! @brief ID formats for Rx FIFO
 * Implements : flexcan_rx_fifo_id_element_format_t_Class
 */
typedef enum {
    FLEXCAN_RX_FIFO_ID_FORMAT_A, /*!< One full ID (standard and extended) per ID Filter Table element.*/
    FLEXCAN_RX_FIFO_ID_FORMAT_B, /*!< Two full standard IDs or two partial 14-bit (standard and
                                      extended) IDs per ID Filter Table element.*/
    FLEXCAN_RX_FIFO_ID_FORMAT_C, /*!< Four partial 8-bit Standard IDs per ID Filter Table element.*/
    FLEXCAN_RX_FIFO_ID_FORMAT_D  /*!< All frames rejected.*/
} flexcan_rx_fifo_id_element_format_t;

由於這個還沒搞懂, 先飛一會, 這裏默認不勾選Enable Rx FIFO extension. S32K的CAN1, CAN2和CAN0應該是平行關係, 不太像STM32的兩路CAN有主從關係, 待確認. CAN濾波器設置有興趣的可以參考NXP論壇這篇文章, 待驗證:

S32K Can Filtering: https://community.nxp.com/thread/500761

點擊生成代碼:
在這裏插入圖片描述

發送接收代碼參考

can_pal 可用(拖拽)的函數有下面這些:
在這裏插入圖片描述
由於用法和flexcan差別不算太大, 就不分開介紹了, 代碼參考如下:

/* MODULE main */

/* Including necessary module. Cpu.h contains other modules needed for compiling.*/
#include "Cpu.h"

volatile int exit_code = 0;

/* User includes (#include below this line is not maintained by Processor Expert) */
#define TX_STD_MAILBOX (0UL)
#define TX_EXT_MAILBOX (1UL)

can_buff_config_t stdBuffCfg = {
    .enableFD = false,
    .enableBRS = false,
    .fdPadding = 0U,
    .idType = CAN_MSG_ID_STD,
    .isRemote = false};

can_buff_config_t extBuffCfg = {
    .enableFD = false,
    .enableBRS = false,
    .fdPadding = 0U,
    .idType = CAN_MSG_ID_EXT,
    .isRemote = false};

can_message_t recvMsg;

// void can_send_msg(void)
// {
//   can_message_t message = {
//       .cs = 0U,
//       .id = TX_STD_MSG_ID,
//       .data[0] = 0x01,
//       .data[1] = 0x02,
//       .length = 2U};
//   CAN_Send(&can_pal1_instance, TX_STD_MAILBOX, &message);

//   message.id = 0x1234567;
//   message.length = 8;
//   static uint8_t kk = 0;
//   message.data[7] = kk;
//   ++kk;
//   CAN_Send(&can_pal1_instance, TX_EXT_MAILBOX, &message);
// }

//refer to can_callback_t
void canRxCallback(uint32_t instance,
                   can_event_t eventType,
                   uint32_t objIdx,
                   void *driverState)
{
  if(eventType == CAN_EVENT_RX_COMPLETE) {
    if(instance == can_pal1_instance.instIdx) {
    	if(recvMsg.id < 0x800) {	//not sure
    		CAN_Send(&can_pal1_instance, TX_STD_MAILBOX, &recvMsg);
    	} else {
    		CAN_Send(&can_pal1_instance, TX_EXT_MAILBOX, &recvMsg);
    	}
        CAN_Receive(&can_pal1_instance, objIdx, &recvMsg);
    }
  }
}
/*! 
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
int main(void)
{
/* Write your local variable definition here */

/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
  PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */
  CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                 g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
  CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
  PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

  CAN_Init(&can_pal1_instance, &can_pal1_Config0);

  CAN_ConfigTxBuff(&can_pal1_instance, TX_STD_MAILBOX, &stdBuffCfg);
  CAN_ConfigTxBuff(&can_pal1_instance, TX_EXT_MAILBOX, &extBuffCfg);

  CAN_ConfigRxBuff(&can_pal1_instance, 2, &stdBuffCfg, 0x119);
  CAN_ConfigRxBuff(&can_pal1_instance, 3, &stdBuffCfg, 0x120);
  CAN_ConfigRxBuff(&can_pal1_instance, 4, &extBuffCfg, 0x1234567);
  CAN_ConfigRxBuff(&can_pal1_instance, 5, &extBuffCfg, 0x1234568);

  CAN_InstallEventCallback(&can_pal1_instance, canRxCallback, NULL);
  CAN_Receive(&can_pal1_instance, 2, &recvMsg);
  CAN_Receive(&can_pal1_instance, 3, &recvMsg);
  CAN_Receive(&can_pal1_instance, 4, &recvMsg);
  CAN_Receive(&can_pal1_instance, 5, &recvMsg);

/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;) {
    if(exit_code != 0) {
      break;
    }
  }
  return exit_code;
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */

代碼中, 0郵箱用於標準幀發送, 1郵箱用於擴展幀發送, 2郵箱用於接收ID爲0x119的CAN報文… 郵箱數不大於上面設置的Number of buffers, 默認16, 最大可手動設置32.

接收中斷中把接收到的CAN報文原封不動返還, 然後重新開始接收.

調試驗證

連接CAN0到CAN分析儀, 記得總線上至少有120Ω終端電阻, 打開CAN上位機工具, 波特率設置爲500k, 打開, 可以看到0x119, 0x120, 0x1234567, 0x1234568都可以正常回傳, 0x121和0x1234569被濾掉了:
在這裏插入圖片描述

微信公衆號

歡迎掃描關注我的微信公衆號, 及時獲取最新文章:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章