S32K144 CAN PAL Sample Project Basic issue with a for loop to package the date into the buffer

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

S32K144 CAN PAL Sample Project Basic issue with a for loop to package the date into the buffer

535 次查看
RML_SPARKY
Contributor I

Good afternoon everyone, i am looking for a little help on what must be a pretty basic issue.

I am running the CAN PAL sample project and can send a receive messages but when i try to modify the main.c to package the data from an array using a for loop, it flashes to the device successfully but when i hit debug, i get the below in the console and the debug stops automatically.

UsageFault:An Unaligned access error has occurred

HardFault: A fault has been escalated to a hard fault

All i have added is a manual method for packaging data bytes into the transmit buffer but im having to do it in 8 separate commands, ive added a simple for loop but that kills the program. Im very used to using the Arduino IDE and this is my attempt to move forward with a more professional platform so any help would be great

Thanks
Rich

 

(*(uint32_t *)&CAN_TX_Buffer.id = 0x600);// sets the CAN ID to transmit

 

unsigned char data2[8] = {7, 6, 5, 4, 3, 2, 1, 0};// Sets the data to send into a character array

for(int t=0; t<7; t++){

(*(uint64_t *)&CAN_TX_Buffer.data[t] = data2[t]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[0] = data2[0]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[1] = data2[1]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[2] = data2[2]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[3] = data2[3]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[4] = data2[4]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[5] = data2[5]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[6] = data2[6]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
//(*(uint64_t *)&CAN_TX_Buffer.data[7] = data2[7]);/* increase the data, [2] is the byte of data in the 8 byte message to update */
}

CAN_Send(&can_pal1_instance, TX_MB, &CAN_TX_Buffer);// Sends the CAN message using non blocking type

 

0 项奖励
回复
6 回复数

478 次查看
RML_SPARKY
Contributor I

Hi Petr, this fixed my issue, thank you very much.

Regards

Rich

0 项奖励
回复

506 次查看
PetrS
NXP TechSupport
NXP TechSupport

Hi,

it should be enough to use

unsigned char data2[8] = {7, 6, 5, 4, 3, 2, 1, 0};// Sets the data to send into a character array
for(int t=0; t<7; t++){
     CAN_TX_Buffer.data[t] = data2[t]);

}

BR, Petr

0 项奖励
回复

458 次查看
RML_SPARKY
Contributor I

Hi Petr, 

I have simplified my code for the CAN message send now but i am struggling to achieve a similar function with receiving CAN messages, i effectively want to receive in all messages and then check the ID and assign the data to a character array depending on the ID.

I have tried a couple of options and looked through the different CAN examples but im not able to access the CAN ID variable. I am not using flex CAN just standard CAN 11 bit ID's .

 

I need to set up a CAN receive instance, set up the message buffer and then do something different based on the IS of the message that comes through.

 

a bit like

CAN_RECEIVE (SETUP)             What is this command???

CAN_RECEIVE (MESSAGE)         What is this command??

if (CAN_RECEIVE.ID == 0x800){     what is this command??

for (j=0;j,7;j++){

char message1[j] = CAN_RECEIVE.data[j]   What is this command???

}

else if (CAN_RECEIVE.ID == 0x700){

for (j=0;j,7;j++){

char message2[j] = CAN_RECEIVE.data[j]

}

 

Are you able to point me in the right direction please?

Thanks
Rich

0 项奖励
回复

436 次查看
PetrS
NXP TechSupport
NXP TechSupport

Hi,

refer to SDK demo code, it shows how to config and use MB for receiving.
If you want to receive all IDs into single MB then clear respective mask acceptance register.

below is possible code, but using FlexCAN driver

#define RX_MAILBOX 0UL
 
/* Configure RX message buffer with index RX_MSG_ID and RX_MAILBOX */
FLEXCAN_DRV_ConfigRxMb(INST_CANCOM1, RX_MAILBOX, &dataInfo, RX_MSG_ID);
 
/* Start receiving data in RX_MAILBOX. */
FLEXCAN_DRV_Receive(INST_CANCOM1, RX_MAILBOX, &recvBuff);
 
while(1)
{
          FLEXCAN_DRV_SendBlocking(INST_FLEXCAN_CONFIG_1, TX_MAILBOX, &dataInfo, TX_MSG_ID,  data, 1000 ); 
          if(FLEXCAN_DRV_GetTransferStatus(INST_CANCOM1, RX_MAILBOX) == STATUS_SUCCESS);
          {
               // process received data 
 
               /* Start receiving data in RX_MAILBOX again */
               FLEXCAN_DRV_Receive(INST_CANCOM1, RX_MAILBOX, &recvBuff);
          }
}
 
BR, Petr
0 项奖励
回复

433 次查看
RML_SPARKY
Contributor I

HI Petr, 

Could you advise which SDK CAN example i should use with the code you have provided please?

Thanks
Rich

0 项奖励
回复

345 次查看
PetrS
NXP TechSupport
NXP TechSupport

Hi,

you can refer to flexcan_encrypted_s32k demo

BR, Petr

0 项奖励
回复