查看: 6807|回复: 10

[S32] S32K144 EVB评估板 实例CAN驱动

[复制链接]

该用户从未签到

1

主题

1

帖子

0

新手上路

Rank: 1

积分
4
最后登录
2018-10-23
发表于 2018-10-23 15:50:24 | 显示全部楼层 |阅读模式
S32K144 EVB评估板 上示例程序 flexcan_encrypted_s32k144采用 openSDA进行下载,CAN分析仪和用示波器检测发出的can信号,发现没有信号输出。
main.c
-------------------------------
/* ###################################################################
**     Filename    : main.c
**     Project     : flexcan_encrypted_s32k144
**     Processor   : S32K144_100
**     Version     : Driver 01.00
**     Compiler    : GNU C Compiler
**     Date/Time   : 2016-06-03, 14:05, # CodeGen: 0
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/

/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "clockMan1.h"
#include "osif1.h"
#include "sbc_uja11691.h"
#include "canCom1.h"
#include "dmaController1.h"
#include "lpspiCom1.h"
#include "csec1.h"
#include "pin_mux.h"
#if CPU_INIT_CONFIG
  #include "Init_Config.h"
#endif

#include <stdint.h>
#include <stdbool.h>

/******************************************************************************
* Definitions
******************************************************************************/

/* This example is setup to work by default with EVB. To use it with other boards
   please comment the following line
*/

#define EVB

#ifdef EVB
    #define LED_PORT        PORTD
    #define GPIO_PORT       PTD
    #define PCC_INDEX       PCC_PORTD_INDEX
    #define LED0            15U
    #define LED1            16U
    #define LED2            0U

    #define BTN_GPIO        PTC
    #define BTN1_PIN        13U
    #define BTN2_PIN        12U
    #define BTN_PORT        PORTC
    #define BTN_PORT_IRQn   PORTC_IRQn
#else
    #define LED_PORT        PORTC
    #define GPIO_PORT       PTC
    #define PCC_INDEX       PCC_PORTC_INDEX
    #define LED0            0U
    #define LED1            1U
    #define LED2            2U

    #define BTN_GPIO        PTC
    #define BTN1_PIN        13U
    #define BTN2_PIN        12U
    #define BTN_PORT        PORTC
    #define BTN_PORT_IRQn   PORTC_IRQn
#endif

/* Use this define to specify if the application runs as master or slave */
#define MASTER
/* #define SLAVE */

/* Definition of the TX and RX message buffers depending on the bus role */
#if defined(MASTER)
    #define TX_MAILBOX  (1UL)
    #define TX_MSG_ID   (555UL)
    #define RX_MAILBOX  (0UL)
    #define RX_MSG_ID   (556UL)
#elif defined(SLAVE)
    #define TX_MAILBOX  (0UL)
    #define TX_MSG_ID   (2UL)
    #define RX_MAILBOX  (1UL)
    #define RX_MSG_ID   (1UL)
#endif

typedef enum
{
    LED0_CHANGE_REQUESTED = 0x00U,
    LED1_CHANGE_REQUESTED = 0x01U
} can_commands_list;

uint8_t ledRequested = (uint8_t)LED0_CHANGE_REQUESTED;

bool useEncryption = false;

/******************************************************************************
* Function prototypes
******************************************************************************/
void SendCANData(uint32_t mailbox, uint32_t messageId, uint8_t * data, uint32_t len);
void buttonISR(void);
void BoardInit(void);
void SBCInit(void);
void GPIOInit(void);
void FlexCANInit(void);

/******************************************************************************
* Functions
******************************************************************************/

/**
* Button interrupt handler
*/
void buttonISR(void)
{
    /* Check if one of the buttons was pressed */
    uint32_t buttonsPressed = PINS_DRV_GetPortIntFlag(BTN_PORT) &
                                           ((1 << BTN1_PIN) | (1 << BTN2_PIN));
    bool sendFrame = false;

        /* Set FlexCAN TX value according to the button pressed */
        switch (buttonsPressed){
                case (1 << BTN1_PIN):
                        sendFrame = true;
                        PINS_DRV_TogglePins(GPIO_PORT, (1 << LED0));
                        ledRequested = LED0_CHANGE_REQUESTED;
                        /* Clear interrupt flag */
                        PINS_DRV_ClearPinIntFlagCmd(BTN_PORT, BTN1_PIN);
                        break;
                case (1 << BTN2_PIN):
                        sendFrame = true;
                        PINS_DRV_TogglePins(GPIO_PORT, (1 << LED1));
                        ledRequested = LED1_CHANGE_REQUESTED;
                        /* Clear interrupt flag */
                        PINS_DRV_ClearPinIntFlagCmd(BTN_PORT, BTN2_PIN);
                        break;
                default:
                        PINS_DRV_TogglePins(GPIO_PORT, (1 << LED2));
                        PINS_DRV_ClearPortIntFlagCmd(BTN_PORT);
                        break;
        }

        if ((ledRequested == LED0_CHANGE_REQUESTED) && sendFrame)
        {
                uint8_t ciphertext[16] = {123};

                /* Send the information via CAN */
                SendCANData(TX_MAILBOX, TX_MSG_ID, ciphertext, 8UL);
        }else if ((ledRequested == LED1_CHANGE_REQUESTED) && sendFrame){
                /* Send the information via CAN */
                SendCANData(TX_MAILBOX, TX_MSG_ID, &ledRequested, 8UL);
        }
}

/*
* @brief: Send data via CAN to the specified mailbox with the specified message id
* @param mailbox   : Destination mailbox number
* @param messageId : Message ID
* @param data      : Pointer to the TX data
* @param len       : Length of the TX data
* @return          : None
*/
void SendCANData(uint32_t mailbox, uint32_t messageId, uint8_t * data, uint32_t len)
{
    /* Set information about the data to be sent
     *  - 1 byte in length
     *  - Standard message ID
     *  - Bit rate switch enabled to use a different bitrate for the data segment
     *  - Flexible data rate enabled
     *  - Use zeros for FD padding
     */
    flexcan_data_info_t dataInfo =
    {
            .data_length = len,
            .msg_id_type = FLEXCAN_MSG_ID_STD,
            .enable_brs  = false,
            .fd_enable   = false,
            .fd_padding  = 0U
    };

    /* Configure TX message buffer with index TX_MSG_ID and TX_MAILBOX*/
    FLEXCAN_DRV_ConfigTxMb(INST_CANCOM1, mailbox, &dataInfo, messageId);

    /* Execute send non-blocking */
    FLEXCAN_DRV_Send(INST_CANCOM1, mailbox, &dataInfo, messageId, data);
}

/*
* @brief : Initialize clocks, pins and power modes
*/
void BoardInit(void)
{

    /* Initialize and configure clocks
     *  -   Setup system clocks, dividers
     *  -   Configure FlexCAN clock, GPIO, LPSPI
     *  -   see clock manager component for more details
     */
    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                        g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE);

    /* Initialize pins
     *  -   Init FlexCAN, LPSPI and GPIO pins
     *  -   See PinSettings component for more info
     */
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
}

#ifdef EVB
/*
* @brief Function to initialize and configure the SBC
*/
void SBCInit(void)
{
    LPSPI_DRV_MasterInit(LPSPICOM1, &lpspiCom1State, &lpspiCom1_MasterConfig0);

    /* Initialize SBC */
    SBC_Init(&sbc_uja11691_InitConfig0, LPSPICOM1);
}
#endif

/*
* @brief Function which configures the LEDs and Buttons
*/
void GPIOInit(void)
{
    /* Output direction for LEDs */
    PINS_DRV_SetPinsDirection(GPIO_PORT, (1 << LED2) | (1 << LED1) | (1 << LED0));

    /* Set Output value LEDs */
    PINS_DRV_ClearPins(GPIO_PORT, 1 << LED1);
    PINS_DRV_SetPins(GPIO_PORT, 1 << LED2);

    /* Setup button pin */
    PINS_DRV_SetPinsDirection(BTN_GPIO, ~((1 << BTN1_PIN)|(1 << BTN2_PIN)));

    /* Setup button pins interrupt */
    PINS_DRV_SetPinIntSel(BTN_PORT, BTN1_PIN, PORT_INT_RISING_EDGE);
    PINS_DRV_SetPinIntSel(BTN_PORT, BTN2_PIN, PORT_INT_RISING_EDGE);

    /* Install buttons ISR */
    INT_SYS_InstallHandler(BTN_PORT_IRQn, &buttonISR, NULL);

    /* Enable buttons interrupt */
    INT_SYS_EnableIRQ(BTN_PORT_IRQn);
}

/*
* @brief Initialize FlexCAN driver and configure the bit rate
*/
void FlexCANInit(void)
{
    /*
     * Initialize FlexCAN driver
     *  - 8 byte payload size
     *  - FD enabled
     *  - Bus clock as peripheral engine clock
     */
    FLEXCAN_DRV_Init(INST_CANCOM1, &canCom1_State, &canCom1_InitConfig0);
}

volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */

/*!
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
* - __start (startup asm routine)
* - __init_hardware()
* - main()
*   - PE_low_level_init()
*     - Common_Init()
*     - Peripherals_Init()
*/
int main(void)
{

    /* Do the initializations required for this application */
    BoardInit();
#ifdef EVB
    SBCInit();
#endif
    GPIOInit();
    FlexCANInit();

    CSEC_DRV_Init(&csec1_State);

    /* Set information about the data to be received
     *  - 1 byte in length
     *  - Standard message ID
     *  - Bit rate switch enabled to use a different bitrate for the data segment
     *  - Flexible data rate enabled
     *  - Use zeros for FD padding
     */
    flexcan_data_info_t dataInfo =
    {
            .data_length = 1U,
            .msg_id_type = FLEXCAN_MSG_ID_STD,
            .enable_brs  = false,
            .fd_enable   = false,
            .fd_padding  = 0U
    };

    /* Configure RX message buffer with index RX_MSG_ID and RX_MAILBOX */
    FLEXCAN_DRV_ConfigRxMb(INST_CANCOM1, RX_MAILBOX, &dataInfo, RX_MSG_ID);

    while(1)
    {
        /* Define receive buffer */
        flexcan_msgbuff_t recvBuff;

        /* Start receiving data in RX_MAILBOX. */
        FLEXCAN_DRV_Receive(INST_CANCOM1, RX_MAILBOX, &recvBuff);

        /* Wait until the previous FlexCAN receive is completed */
        while(FLEXCAN_DRV_GetTransferStatus(INST_CANCOM1, RX_MAILBOX) == STATUS_BUSY);
        if (useEncryption)
        {
            /* Check the length of the received message */
            if (recvBuff.dataLen == 16)
            {
                status_t stat;

                /* Decrypt data using AES-128 ECB and the first non-volatile user key */
                stat = CSEC_DRV_DecryptECB(CSEC_KEY_1, recvBuff.data, 16UL, recvBuff.data);

                if (stat != STATUS_SUCCESS)
                {
                    continue;
                }
            }
            else
            {
                continue;
            }
        }

        /* Check the received message ID and payload */
        if((recvBuff.data[0] == LED0_CHANGE_REQUESTED) &&
                recvBuff.msgId == RX_MSG_ID)
        {
            /* Toggle output value LED1 */
            PINS_DRV_TogglePins(GPIO_PORT, (1 << LED0));
        }
        else if((recvBuff.data[0] == LED1_CHANGE_REQUESTED) &&
                recvBuff.msgId == RX_MSG_ID)
        {
            /* Toggle output value LED0 */
            PINS_DRV_TogglePins(GPIO_PORT, (1 << LED1));
        }
    }

  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 */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.1 [05.21]
**     for the Freescale S32K series of microcontrollers.
**
** ###################################################################
*/

---------------------------------------------------------------------------------------------------------------------
#配置文件是如下:
#include "canCom1.h"

flexcan_state_t canCom1_State;


const flexcan_user_config_t canCom1_InitConfig0 = {
    .fd_enable = false,
    .pe_clock = FLEXCAN_CLK_SOURCE_SOSCDIV2,
    .max_num_mb = 16,
    .num_id_filters = FLEXCAN_RX_FIFO_ID_FILTERS_32,
    .is_rx_fifo_needed = true,
    .flexcanMode = FLEXCAN_NORMAL_MODE,
    .payload = FLEXCAN_PAYLOAD_SIZE_8,
    .bitrate = {
        .propSeg = 7,
        .phaseSeg1 = 4,
        .phaseSeg2 = 1,
        .preDivider = 0,
        .rJumpwidth = 1
    },
    .bitrate_cbt = {
                .propSeg = 7,
                .phaseSeg1 = 4,
                .phaseSeg2 = 1,
                .preDivider = 0,
                .rJumpwidth = 1
    },
    .transfer_type = FLEXCAN_RXFIFO_USING_INTERRUPTS,
    .rxFifoDMAChannel = 0U
};


接入CAN信号引脚:
请问我是哪里弄错了,CAN发不出信号,示例程序不能用;

最佳答案

12V供电用了么?板载的USB供电肯定不行的。 按照指导文档来确认硬件配置/跳线正确先。
捕获.PNG
回复

使用道具 举报

  • TA的每日心情
    奋斗
    昨天 09:53
  • 签到天数: 1479 天

    [LV.10]以坛为家III

    203

    主题

    2万

    帖子

    64

    超级版主

    Rank: 8Rank: 8

    积分
    92703
    最后登录
    2024-4-26
    发表于 2018-10-23 17:13:01 | 显示全部楼层
    环回测试通过了吗?

    评分

    参与人数 1 +1 收起 理由
    NXP管管 + 1

    查看全部评分

    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2021-12-23 09:57
  • 签到天数: 1587 天

    [LV.Master]伴坛终老

    5

    主题

    3046

    帖子

    23

    金牌会员

    Rank: 6Rank: 6

    积分
    8201
    最后登录
    2024-4-17
    发表于 2018-10-24 13:07:40 | 显示全部楼层
    12V供电用了么?板载的USB供电肯定不行的。
    按照指导文档来确认硬件配置/跳线正确先。

    评分

    参与人数 1 +1 收起 理由
    NXP管管 + 1

    查看全部评分

    回复 支持 反对

    使用道具 举报

    该用户从未签到

    656

    主题

    6312

    帖子

    0

    超级版主

    Rank: 8Rank: 8

    积分
    20036
    最后登录
    2024-4-26
    发表于 2018-10-24 16:39:17 | 显示全部楼层
    应该是板子的供电有问题,你的外加12V电源试试。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2018-12-8 13:54
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    3

    帖子

    0

    注册会员

    Rank: 2

    积分
    84
    最后登录
    2018-12-12
    发表于 2018-12-8 14:57:35 | 显示全部楼层
    第一次来
    回复

    使用道具 举报

    该用户从未签到

    0

    主题

    1

    帖子

    0

    新手上路

    Rank: 1

    积分
    15
    最后登录
    2019-9-5
    发表于 2019-7-16 16:31:31 | 显示全部楼层
    请问这个例程是在哪里可以找到呢?最近才开始学,麻烦告诉一下
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    2019-9-19 15:56
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    2

    主题

    20

    帖子

    0

    注册会员

    Rank: 2

    积分
    74
    最后登录
    2019-11-6
    发表于 2019-9-10 15:39:56 | 显示全部楼层
    llllzzz 发表于 2019-7-16 16:31
    请问这个例程是在哪里可以找到呢?最近才开始学,麻烦告诉一下

    S32DS的S32DS project from examole
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    2019-9-19 15:56
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    2

    主题

    20

    帖子

    0

    注册会员

    Rank: 2

    积分
    74
    最后登录
    2019-11-6
    发表于 2019-9-10 19:49:17 | 显示全部楼层
    本帖最后由 tao475824827 于 2019-9-10 19:50 编辑

    我看的也是这个官方DEMO例子,

    实测效果很奇怪。
    按下任意按键1次后,
    Can0的TX脚出现波形,但是是一直有,can在一直发送,不停。
    再按下任意按键1次,
    波形消失。

    之后不管怎么按,都再没有波形。但是中断其实都是进了的,我加了LED的变化能看到。

    请问楼主现象也是这样吗?
    我看代码怎么也不会这样啊。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    2019-9-19 15:56
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    2

    主题

    20

    帖子

    0

    注册会员

    Rank: 2

    积分
    74
    最后登录
    2019-11-6
    发表于 2019-9-10 19:52:51 | 显示全部楼层
    微信图片_20190910195152.jpg

    按一下按键,就不停的有波形
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    0

    主题

    17

    帖子

    0

    新手上路

    Rank: 1

    积分
    30
    最后登录
    2021-7-2
    发表于 2020-6-30 09:01:56 | 显示全部楼层
    我也出现同样问题,  注释掉while(FLEXCAN_DRV_GetTransferStatus(INST_CANCOM1, RX_MAILBOX) == STATUS_BUSY);  这行代码, 按一次键可以发送一帧,   否则无法发送, 不知何故.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-27 09:11 , Processed in 0.166116 second(s), 35 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表