Hello,
Just got working my hardware, I got a new issue with CAN bus. I want to receive a frame from my CAN analyzer and send a response if byte [0] of the frame is 0xFF. I works, but only there is a "printf" debug output to the console. In addition, it lasts almost half a second until this frame is sent. Another frame at the beginning of this function will be sent immediately. I really don't know what I'm doing wrong. I started this project with a S124 and used a thread especially for this CAN message. There are no other threads.
Here is my code:
#include "can_thread.h"
#include "hal_data.h"
#include "stdio.h"
#include <string.h>
#include "hal_data.h"
#include "can_hal_api_mg.h"
#include "can_tx_rx.h"
//#define SEMI_HOSTING
extern void initialise_monitor_handles(void);
bool rx_flag = false, tx_flag = false, bus_off_error_flag = false,bus_recovery_flag = false;
bool err_passive_flag = false, mailbox_overwrite_overrun_flag= false, unexpected_event_flag= false;
ssp_err_t err, err1, err2;
bsp_leds_t leds;
uint8_t tx_data[8], rx_data[8];
can_id_t tx_id;
can_frame_t rx_frame, tx_frame, tx1_frame, tx2_frame;
uint32_t tx_count = 0, rx_count = 0;
uint16_t zaehler = 0;
/* CAN Threaad entry function */
void can_thread_entry(void)
{
initialise_monitor_handles();
// printf("\n CAN 0 Write error vor open: %d",err);
err = g_can0.p_api->open(g_can0.p_ctrl, g_can0.p_cfg);
/* TODO: add your own code here */
CAN_HAL_control();
while (1)
{
//err1 = g_cgc.p_api->clockCheck(CGC_CLOCK_MAIN_OSC);
// printf("\n clock %d", err1);
//CAN_HAL_control();
//printf("\n CAN 0 Write error vor open: %d",err);
//err = g_can0.p_api->open(g_can0.p_ctrl, g_can0.p_cfg);
// printf("\n ERR vor IF %d", err);
if(err != 0)
{
while (1)
{
printf("\n in while main program");
}
}
// printf("\n ERR nach tx1 %d", err);
//tx_thread_sleep (1);
//can_tx1();
can_rx();
//can_tx2();
tx_thread_sleep (1);
}
}
void can_rx()
{
// Check for received CAN data
if (rx_flag == true)
{
can_tx2();
// Store received frame data
// memcpy(&rx_data, &rx_frame.data, sizeof(rx_data));
//if(rx_data[0] == 0xFF)
{
can_tx3();
}
rx_data[0] = '\0';
memcpy(&rx_frame.data, &rx_data, sizeof(rx_data));
rx_flag = false;
}
}
void can_tx1()
{
tx_data[0]= 0x22;
tx_data[1]= 0x22;
tx_data[2]= 0x22;
tx_data[3]= 0x22;
tx_data[4]= 0x22;
tx_data[5]= 0x22;
tx_data[6]= 0x22;
tx_data[7]= 0x22;
tx_id = 0x7FA;
//Change length with respect to number of data bytes to be transmitted
can_tx(tx_id, tx_data,8);
}
void can_tx2()
{
tx_data[0]= 0x33;
tx_data[1]= 0x33;
tx_data[2]= 0x33;
tx_data[3]= 0x33;
tx_data[4]= 0x33;
tx_data[5]= 0x33;
tx_data[6]= 0x33;
tx_data[7]= 0x33;
tx_id = 0x7FB;
//Change length with respect to number of data bytes to be transmitted
can_tx(tx_id, tx_data,8);
}
void can_tx3()
{
tx_data[0]= 0x00;
tx_data[1]= 0x00;
tx_data[2]= 0x00;
tx_data[3]= 0x00;
tx_data[4]= 0x00;
tx_data[5]= 0x00;
tx_data[6]= 0x00;
tx_data[7]= 0x00;
tx_id = 0x7FC;
//Change length with respect to number of data bytes to be transmitted
can_tx(tx_id, tx_data,8);
}
void can_tx(can_id_t ID, uint8_t *frame, uint8_t data_length)
{
// Initialize CAN transmission frame
tx_frame.type = CAN_FRAME_TYPE_DATA;
tx_frame.id = ID;
memcpy(&tx_frame.data, frame, sizeof(tx_data));
tx_frame.data_length_code = data_length;
// Start CAN transmission
printf("\n ERR nach tx1 vor tx %d", err);
err = g_can0.p_api->write(g_can0.p_ctrl, 0, &tx_frame);
printf("\n ERR nach tx %d", err);//printf("\n ERR nach tx1 nach tx %d", err);
if(err != SSP_SUCCESS)
{
printf("\n CAN 0 Write error nach tx : %d",err);
while (1)
{
printf("\n CAN 0 Write error in while: %d",err);
}
}
// wait for transmission to be completed
while (tx_flag != true);
// Transmission done. Increment tx_count and clear tx_flag.
tx_flag = false;
//printf("\nflag%d", tx_flag);
}
void can_callback(can_callback_args_t * p_args)
{
if(p_args->channel != 0)
{
while(1)
{
printf("\nERROR in callback %d", err);
}
}
switch(p_args->event)
{
case CAN_EVENT_RX_COMPLETE:
// Read received CAN message
err = g_can0.p_api->read (g_can0.p_ctrl, p_args->mailbox, &rx_frame);
rx_flag = true;
break;
case CAN_EVENT_TX_COMPLETE:
tx_flag = true;
break;
case CAN_EVENT_ERR_BUS_OFF:
bus_off_error_flag = true;
break;
case CAN_EVENT_BUS_RECOVERY:
bus_recovery_flag= true;
break;
case CAN_EVENT_ERR_PASSIVE:
err_passive_flag = true;
break;
case CAN_EVENT_MAILBOX_OVERWRITE_OVERRUN:
mailbox_overwrite_overrun_flag = true;
break;
default:
unexpected_event_flag = true;
break;
};
}