Hello Jan,
Sorry again for my questions.
First , after trying to put the frequency n the constructor, CAN bus stop sending and receiving.
Second, Whenever I add a printf to my below program:
#include"mbed.h"
#include "EthernetInterface.h"
#define ECHO_SERVER_PORT 11000
//Serial pc(USBTX, USBRX); //tx,rx for Tera Term output
DigitalOut led1(LED1);// status LED
DigitalOut led2(LED2);// status LED
DigitalOut led3(LED3);// status LED
//Threads
Thread UDP_Client_Thread(osPriorityNormal, 2000);
Thread UDP_Server_Thread(osPriorityNormal, 2000);
Thread CAN_Read(osPriorityNormal, 2000);
Thread CAN_Write(osPriorityNormal, 2000);
Thread Process_UDP(osPriorityNormal, 2000);
char counter =0;
CAN can1(PB_8, PB_9); // CAN interface
//Mutex
Mutex Network_Mutex;
Mutex RecIP_Mutex;
// Network interface
EthernetInterface net;
static const char* mbedIP = "192.168.220.45"; //IP
static const char* mbedMask = "255.255.252.0"; // Mask
static const char* mbedGateway = "192.168.220.1"; //Gateway
static const char* recvIP = "192.168.220.100";
typedef struct {
char buffer[256];
char* pFields[5];
int can_id;
char can_msg[8];
int can_len;
} message_t;
MemoryPool<message_t, 32> mpool;
Queue<message_t, 32> queue;
message_t *message;
//define CAN ID
#define DCE_Steering_ID 0x600
// /////////////////////////////////////////////////////////////////////////////Splitting string Function//////////////////////////////////////////////////////////////////////////////////
void ParseFields(char* inputBuffer, char** pFields, uint32_t numFields, char* delimiterChars)
{
char* pString = inputBuffer;
char* pField;
for(uint32_t i=0; i<numFields; i++)
{
pField = strtok(pString, delimiterChars);
if(pField != NULL)
{
pFields[i] = pField;
message_t *message = mpool.alloc();
message->pFields[i]= pFields[i]; // copy the rxbuffer (recieved data from the truck) in the queue message
queue.put(message);
printf("splitted message: %s\r\n", message);
}
else
{
pFields[i] = "";
}
pString = NULL; //to make strtok continue parsing the next field rather than start again on the original string (see strtok documentation for more details)
}
}
void Processing_UDP_DATA_Thread()
{
char* pFields[5];
while(true)
{
osEvent evt = queue.get();
if (evt.status == osEventMessage)
{
message_t *message = (message_t*)evt.value.p;
ParseFields(message->buffer, pFields, 5, ",");
mpool.free(message);
}
}
}
//////////////////////////////////////////////////////////////////////////CAN Read////////////////////////////////////////////////////////////////////////////////////
void receive()
{
CANMessage msg;//create empty CAN message
while(1)
{
if(can1.read(msg)) // if message is available, read Into msg
{
//printf("Message received: %d\r\n" , msg.data[0]); //display message data
led2 = ! led2; //toggle status LED
}
}
}
///////////////////////////////////////////////////////////////////////////CAN Write/////////////////////////////////////////////////////////////////////////////////////////////
void send()
{
//printf("send....\r\n");
while(1)
{//send value to CAN bus AND monitor return value to check if CAN message was sent successfully. If so display, increment and toggle.
if(can1.write(CANMessage(5 ,&counter,1)))
{
//printf("Message sent: %d\r\n",counter);//display
counter++; //increment
led1=!led1; //toggle status LEDelsecan1.reset();
}
ThisThread::sleep_for(500);
}
}
//////////////////////////////////////////////////////////////////////////////UDP_Server///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UDP_Server()
{
UDPSocket server;
Network_Mutex.lock();
server.open(&net);
Network_Mutex.unlock();
server.bind(10110);
SocketAddress sockAddr;
while(true)
{
char buffer[256];
//printf("\nWaiting for UDP packet...\r\n");
int n = server.recvfrom(&sockAddr, buffer, sizeof(buffer));
buffer[n] = '\0';
message_t *message = mpool.alloc();
strcpy(message->buffer, buffer); // copy the rxbuffer (recieved data from the truck) in the queue message
queue.put(message); // put the message in the queue
// pc.printf("Received packet from: %s\n", sockAddr.get_ip_address());
// printf("Packet contents : '%s'\r\n",buffer);
//pc.printf("Sending Packet back to Client\n");
server.sendto(sockAddr, buffer, n);
}
// Bring down the ethernet interface
//net.disconnect();
//pc.printf("Done\n");
}
//////////////////////////////////////////////////////////////////////////UDP_Client//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UDP_Client()
{
//Buffer for output messages
const char out_buffer[] = "very important data";
//address of destination ip address and port
RecIP_Mutex.lock();
SocketAddress td_addr(recvIP, 11000);
RecIP_Mutex.unlock();
Network_Mutex.lock();
UDPSocket td_sock(&net);
Network_Mutex.unlock();
// Loop to send data to Socket address "td_sock"
while(true)
{
int ret = td_sock.sendto(td_addr, &out_buffer, sizeof(out_buffer));
if(ret < 0)
{
Thread::wait(500);
}
}
}
int main()
{
can1.frequency(500000); // set CAN bit rate to 1Mbps
// printf("read....\r\n");
net.set_network(mbedIP, mbedMask, mbedGateway);// my device address
if (0 != net.connect())
{
printf("Error connecting\n\r");
// ThisThread::sleep_for(1000);
return -1;
}
else
{
printf("Connected Ethernet \n\r");
led3=!led3;
}
UDP_Server_Thread.start(UDP_Server);
UDP_Client_Thread.start(UDP_Client);
CAN_Read.start(receive);
CAN_Write.start(send);
Process_UDP.start(Processing_UDP_DATA_Thread);
/* while(1)
{
//display message data
led3 = ! led3; //toggle status LED
ThisThread::sleep_for(100);
}*/
}
I got this error
++ MbedOS Fault Handler ++
FaultType: HardFault
Context:
R0 : 00000000
R1 : 2000C650
R2 : 00000000
R3 : 32312E33
R4 : 01010101
R5 : 01000100
R6 : 2000C64C
R7 : 00000020
R8 : 2000C7F4
R9 : 00000000
R10 : 20008268
R11 : 2000B034
R12 : 08014CC9
SP : 2000C634
LR : 08004D5D
PC : 080010E2
xPSR : 41000200
PSP : 2000C610
MSP : 2002FFD8
CPUID: 410FC241
HFSR : 40000000
MMFSR: 00000082
BFSR : 00000000
UFSR : 00000000
DFSR : 00000008
AFSR : 00000000
MMFAR: 00000000
Mode : Thread
Priv : Privileged
Stack: PSP
-- MbedOS Fault Handler --
++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x80010E2
Error Value: 0x200001B0
Current Thread: application_unnamed_thread Id: 0x20003A20 Entry: 0x8009CFD StackSize: 0x7D0 StackMem: 0x2000C030 SP: 0x2000C634
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=NUCLEO_F429ZI
-- MbedOS Error Info --
= System will be rebooted due to a fatal error =
= Reboot count(=2) reached maximum, system will halt after rebooting
I don’t know what I’m doing wrong.
With the LPC1768 I was not getting this kind of errors, but I need to use the nucleo.
Please, help and your advise.
I will keep working on that.
Regards
Nada