CAN Receive using SDK Functions

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CAN Receive using SDK Functions

Jump to solution
1,812 Views
rmaryniu
Contributor I

Hello,

I am trying to use the SDK function FLEXCAN_DRV_Receive in my program by mimicking what is done in flexcan_encrypted_s32k144 (which runs correctly). I copied and pasted the code into my own program but it doesn't seem to work. The program correctly waits for the message to be sent, but then triggers the DefaultISR when the message is sent. FLEXCAN_DRV_Send works fine.

Attached is runnable code that triggers the green LED when irqNumber = CAN0_ORed_0_15_MB_IRQn occurs for visualization purposes.

A summary of my code is as follows:

Thanks in advance,

Rhyse

/* ###################################################################
 **     Filename    : main.c
 **     Processor   : S32K14x
 **     Abstract    :
 **         Main module.
 **         This module contains user's application code.
 **     Settings    :
 **     Contents    :
 **         No public methods
 **
 ** ###################################################################*/
/*!
 ** @file main.c
 ** @version 01.00
 ** @brief
 **         Main module.
 **         This module contains user's application code.
 */
/*!
 **  @addtogroup main_module main module documentation
 **  @{
 */
/* 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) */
#include "initialization.h"
#include "canFunctions.h"
#include "timingFunctions.h"
#include "watchdog.h"
#include "pins.h"
#include <stdbool.h>
#include <stdio.h>

/*!
 \brief The main function for the project.
 \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
 */
int main(void) {
#define INST_CANCOM1 (0U)
#define RX_MAILBOX  (0UL)
#define RX_MSG_ID   (2UL)
#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

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

void systemInit(volatile int *exit_code) {

    /* Initialize and configure clocks
     *  -   Setup system clocks
     *  -   Enable clock feed for Ports
     *  -   See Clock Manager component for more info
     */
    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
            g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

    /* Initialize pins
     *  -   Setup output pins for LEDs
     *  -   See PinSettings component for more info
     */
    //pins_Init();
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

        // Initialize all the CANbuses as defined by user

    flexcan_state_t canCom1_State;
    uint32_t preDividerValue;

    const flexcan_user_config_t canCom1_InitConfig = {
        .fd_enable = true,
        .pe_clock = FLEXCAN_CLK_SOURCE_SOSCDIV2,
        .max_num_mb = 2,
        .num_id_filters = FLEXCAN_RX_FIFO_ID_FILTERS_8,
        .is_rx_fifo_needed = false,
        .flexcanMode = FLEXCAN_NORMAL_MODE,
        .payload = FLEXCAN_PAYLOAD_SIZE_16,
        .bitrate = {
            .propSeg = 7,
            .phaseSeg1 = 4,
            .phaseSeg2 = 1,
            .preDivider = 0,
            .rJumpwidth = 1
        },
        .bitrate_cbt = {
            .propSeg = 11,
            .phaseSeg1 = 1,
            .phaseSeg2 = 1,
            .preDivider = 0,
            .rJumpwidth = 1
        },
        .transfer_type = FLEXCAN_RXFIFO_USING_INTERRUPTS,
        .rxFifoDMAChannel = 0U
    };

    FLEXCAN_DRV_Init(0U, &canCom1_State, &canCom1_InitConfig);

   
}


    /* Write your local variable definitions here */

    /* End of local variable definition */

    /*#################################################################################################################*/
    /*** 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 */
    systemInit(&exit_code);

    flexcan_data_info_t dataInfo = { .data_length = 1U, .msg_id_type =
            FLEXCAN_MSG_ID_STD, .enable_brs = true, .fd_enable = true,
            .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)
            ;


        /* 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));
        }
    }

    /* End of your code */

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

Labels (1)
0 Kudos
1 Solution
1,328 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi Rhyse,

why do you use Processor Expert and its drivers, when you finally do not use PE generated variables/structures and add your own. Then the driver cannot work properly.

Do following changes:

1. do not install flexcan interrupts in your systemInit(), it is done within flexcan driver already.

so comment below lines

/* Install buttons ISR */
// INT_SYS_InstallHandler(CAN0_ORed_0_15_MB_IRQn, &ignoreISR, NULL);

/* Enable buttons interrupt */
// INT_SYS_EnableIRQ(CAN0_ORed_0_15_MB_IRQn);

2. use PE generated variables/structures when calling FLEXCAN_DRV_Init()

within canFunctions.c

- add #include "can0.h"

- comment your added flexcan_state_t canCom1_State; and flexcan_user_config_t canCom1_InitConfig

- use just FLEXCAN_DRV_Init(canbus, &CAN0_State, &CAN0_InitConfig0);

After those changes the code runs normally and when message is received you can pass  FLEXCAN_DRV_GetTransferStatus().

BR, Petr  

View solution in original post

2 Replies
1,329 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi Rhyse,

why do you use Processor Expert and its drivers, when you finally do not use PE generated variables/structures and add your own. Then the driver cannot work properly.

Do following changes:

1. do not install flexcan interrupts in your systemInit(), it is done within flexcan driver already.

so comment below lines

/* Install buttons ISR */
// INT_SYS_InstallHandler(CAN0_ORed_0_15_MB_IRQn, &ignoreISR, NULL);

/* Enable buttons interrupt */
// INT_SYS_EnableIRQ(CAN0_ORed_0_15_MB_IRQn);

2. use PE generated variables/structures when calling FLEXCAN_DRV_Init()

within canFunctions.c

- add #include "can0.h"

- comment your added flexcan_state_t canCom1_State; and flexcan_user_config_t canCom1_InitConfig

- use just FLEXCAN_DRV_Init(canbus, &CAN0_State, &CAN0_InitConfig0);

After those changes the code runs normally and when message is received you can pass  FLEXCAN_DRV_GetTransferStatus().

BR, Petr  

1,328 Views
rmaryniu
Contributor I

Hi Petr,

This worked great. Thank you for the answer.

Regards,

Rhyse

0 Kudos