在线时间0 小时
UID3515102
注册时间2018-10-23
NXP金币0
该用户从未签到
新手上路
- 积分
- 4
- 最后登录
- 2018-10-23
|
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供电肯定不行的。
按照指导文档来确认硬件配置/跳线正确先。
|
|