Communication problems between CAN, UART and upper computer

cancel
Showing results for 
Search instead for 
Did you mean: 

Communication problems between CAN, UART and upper computer

Jump to solution
292 Views
kalajiao
Contributor II

I am trying to communicate with the computer by CAN and UART. The codes are in the attachment and below. I would like to know why I have the situation in the picture

QQ截图20210716133229.png

#include "Cpu.h"
#include "clockMan1.h"
#include "dmaController1.h"
#include "pin_mux.h"
#include "lpuart1.h"

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

volatile int exit_code = 0;

typedef enum
{
helloa = 0x00U,
hia = 0x01U
} can_commands_list;

#define TX_MAILBOX (1UL)
#define TX_MSG_ID (1UL)
#define RX_MAILBOX (0UL)
#define RX_MSG_ID (2UL)

#define welcomeMsg "Now you can begin typing:\r\n"

#define errorMsg "An error occurred! The application will stop!\r\n"
#define BUFFER_SIZE 256U
#define TIMEOUT 100U
uint8_t buffer[BUFFER_SIZE];
uint8_t bufferIdx;
uint8_t xinxi = (uint8_t)helloa;
/* User includes (#include below this line is not maintained by Processor Expert) */
void Init(void);

void rxCallback(void *driverState, uart_event_t event, void *userData)
{
/* Unused parameters */
(void)driverState;
(void)userData;

/* Check the event type */
if (event == UART_EVENT_RX_FULL)
{
/* The reception stops when newline is received or the buffer is full */
if ((buffer[bufferIdx] != '\n') && (bufferIdx != (BUFFER_SIZE - 2U)))
{
/* Update the buffer index and the rx buffer */
bufferIdx++;
LPUART_DRV_SetRxBuffer(INST_LPUART1, &buffer[bufferIdx], 1U);
}
}
}

void Init(void)
{
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE);

PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);

CAN_Init(&can_pal1_instance, &can_pal1_Config0);
}


/*!
\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. ***/
uint32_t bytesRemaining;
status_t status;

/* Write your code here */
Init();
LPUART_DRV_InstallRxCallback(INST_LPUART1,rxCallback,NULL);
LPUART_DRV_SendDataBlocking(INST_LPUART1, (uint8_t *)welcomeMsg, strlen(welcomeMsg), TIMEOUT);

can_buff_config_t buffCfg = {
.enableFD = true,
.enableBRS = true,
.fdPadding = 0U,
.idType = CAN_MSG_ID_STD,
.isRemote = false
};
CAN_ConfigTxBuff(&can_pal1_instance,TX_MAILBOX, &buffCfg);
can_message_t message = {
.cs = 0U,
.id = TX_MSG_ID,
.data[0] = helloa,
.length = 1U
};
while(1)
{
LPUART_DRV_ReceiveData(INST_LPUART1,buffer,1U);
while(LPUART_DRV_GetReceiveStatus(INST_LPUART1,&bytesRemaining)==STATUS_BUSY);

status=LPUART_DRV_GetReceiveStatus(INST_LPUART1,&bytesRemaining);

if (status != STATUS_SUCCESS)
{
/* If an error occurred, send the error message and exit the loop */
LPUART_DRV_SendDataBlocking(INST_LPUART1, (uint8_t *)errorMsg, strlen(errorMsg), TIMEOUT);
break;
}

/* Append string terminator to the received data */
bufferIdx++;
buffer[bufferIdx] = 0U;

if(strcmp((char *)buffer, "Hello") != 0)
{
CAN_Send(&can_pal1_instance, TX_MAILBOX, &message);
CAN_ConfigRxBuff(&can_pal1_instance, RX_MAILBOX, &buffCfg, RX_MSG_ID);
can_message_t recvMsg;
CAN_Receive(&can_pal1_instance, RX_MAILBOX, &recvMsg);

while(CAN_GetTransferStatus(&can_pal1_instance, RX_MAILBOX) == STATUS_BUSY);
if((recvMsg.data[0] == helloa) &&
recvMsg.id == RX_MSG_ID)
{
/* Toggle output value LED1 */
PINS_DRV_WritePin(PTD,0,1);
}
else if((recvMsg.data[0] == hia) &&
recvMsg.id == RX_MSG_ID)
{
/* Toggle output value LED0 */
PINS_DRV_WritePin(PTD,16,1);
}
}
else if(strcmp((char *)buffer, "Hello world") != 0)
{
xinxi=hia;
}
else
{
PINS_DRV_WritePin(PTD,15,1);
}

 

/* Send the received data back */
LPUART_DRV_SendDataBlocking(INST_LPUART1, buffer, bufferIdx, TIMEOUT);
/* Reset the buffer index to start a new reception */
bufferIdx = 0U;
}
/* For example: for(;;) { } */

/*** 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!!! ***/

}

0 Kudos
1 Solution
229 Views
Senlent
NXP Employee
NXP Employee

Hi@kalajiao

       I replied to you by email, please reply if you have any questions.

BR!

Jim.

View solution in original post

0 Kudos
8 Replies
253 Views
Senlent
NXP Employee
NXP Employee

Hi@kalajiao

Modified:

Step1:

Senlent_0-1626660155597.png

Step 2:

Senlent_1-1626660250307.png

Step3:

Senlent_2-1626660279453.png

 

result:

Senlent_3-1626660363832.png

 

suggestion:

             If you don’t know the peripheral modules, please refer to the official routines and data manuals first.

Best Regards!

Jim.

if this answer is useful to you, please click Kudos.

0 Kudos
249 Views
kalajiao
Contributor II

I wanted to know how to use UART to communicate with CAN. I tried it but failed.

0 Kudos
230 Views
Senlent
NXP Employee
NXP Employee

Hi@kalajiao

       I replied to you by email, please reply if you have any questions.

BR!

Jim.

View solution in original post

0 Kudos
216 Views
kalajiao
Contributor II

Can you send me your email address? I can't reply to you by email. My email address is 1815701792@qq.com

0 Kudos
216 Views
kalajiao
Contributor II

I can't reply to the email you sent to me. Could you please send me your email address? My address is 1815701792@qq.com

0 Kudos
284 Views
Senlent
NXP Employee
NXP Employee

Hi@kalajiao

           Can you provide complete project and the information of IDE? I will get back to you next Monday.

BR,

Jim,

0 Kudos
281 Views
kalajiao
Contributor II

Sure, I'm in a situation where after running the code I can't send any data beyond the initial welcome message

0 Kudos
278 Views
Senlent
NXP Employee
NXP Employee

OK. there are many module examples in the project directory ,you can refer it first. 

0 Kudos