Update:
been working on this yet, and I got it to send 1 message, and then it freezes. I received the first message on my Due. I tried different serial ports and no serial ports, but it still only sends 1 before freezing. I also added the digitalWrite in the main loop that fires every second to make sure it wasn’t just failing to send. It does not continue to blink. It is also not geting stuck in the No MBX loop anymore, as I put the digitalWrite there (with a small delay to make sure I could see the blink.)
Anyone out there?
EDIT: re-reading above posts and saw that jongjejung had the same issue, however its not clear what was wrong in his case. I definitely have 5V at the MPC2551.
#include <HardwareCAN.h>
/*
* Uses STM32duino with Phono patch. Must add 33 and 95 CAN speeds
*/
#define BPIN 0
#define SPIN 1
byte msgD0 ; // variable to be used in the example.
uint32_t lastSent = 0;
int count = 1;
int ledState = 0;
// Instanciation of CAN interface
HardwareCAN canBus(CAN1_BASE);
CanMsg msg ;
void CANSetup(void)
{
CAN_STATUS Stat ;
// Initialize CAN module
//Serial1.println("test1");
canBus.map(CAN_GPIO_PB8_PB9);
// Serial1.println("test2");// This setting is already wired in the Olimexino-STM32 board
Stat = canBus.begin(CAN_SPEED_250, CAN_MODE_NORMAL); // Other speeds go from 125 kbps to 1000 kbps. CAN allows even more choices.
// Serial1.print("Status: ");
// Serial1.println(Stat);
//Serial1.println("test3");
canBus.filter(0, 0, 0);
canBus.set_irq_mode(); // Use irq mode (recommended), so the handling of incoming messages
// will be performed at ease in a task or in the loop. The software fifo is 16 cells long,
// allowing at least 15 ms before processing the fifo is needed at 125 kbps
Stat = canBus.status();
if (Stat != CAN_OK){
// Serial1.println("CAN initialization Failed");
/* Your own error processing here */ ; // Initialization failed
}
}
// Send one frame. Parameter is a pointer to a frame structure (above), that has previously been updated with data.
// If no mailbox is available, wait until one becomes empty. There are 3 mailboxes.
CAN_TX_MBX CANsend(CanMsg *pmsg) // Should be moved to the library?!
{
CAN_TX_MBX mbx;
do
{
mbx = canBus.send(pmsg) ;
// Serial1.print("MBX: ");
// Serial1.println(mbx);
#ifdef USE_MULTITASK
vTaskDelay( 1 ) ; // Infinite loops are not multitasking-friendly
#endif
}
while(mbx == CAN_TX_NO_MBX) ;
// Waiting outbound frames will eventually be sent, unless there is a CAN bus failure.
return mbx ;
}
// Send message
// Prepare and send a frame containing some value
void SendCANmessage(long id=0x001, byte dlength=8, byte d0=0x00, byte d1=0x00, byte d2=0x00, byte d3=0x00, byte d4=0x00, byte d5=0x00, byte d6=0x00, byte d7=0x00)
{
// Initialize the message structure
// A CAN structure includes the following fields:
msg.IDE = CAN_ID_EXT; // Indicates a standard identifier ; CAN_ID_EXT would mean this frame uses an extended identifier
msg.RTR = CAN_RTR_DATA; // Indicated this is a data frame, as opposed to a remote frame (would then be CAN_RTR_REMOTE)
msg.ID = id ; // Identifier of the frame : 0-2047 (0-0x3ff) for standard idenfiers; 0-0x1fffffff for extended identifiers
msg.DLC = dlength; // Number of data bytes to follow
// Prepare frame : send something
msg.Data[0] = d0 ;
msg.Data[1] = d1 ;
msg.Data[2] = d2 ;
msg.Data[3] = d3 ;
msg.Data[4] = d4 ;
msg.Data[5] = d5 ;
msg.Data[6] = d6 ;
msg.Data[7] = d7 ;
Serial2.println("before CANSend");
CANsend(&msg) ; // Send this frame
// Serial1.println("after CANSend");
}
// The application program starts here
int bState = 0; // variable for reading the pushbutton status
int sState = 0; // variable for reading the switch status
byte st = 0x31; // buttot 1 on the CD30MP3
void setup() {
// put your setup code here, to run once:
Serial2.begin(115200);
// Serial1.println("before CANSetup");
CANSetup(); // Initialize the CAN module and prepare the message structures.
pinMode(PC13, OUTPUT);
// pinMode(BPIN, INPUT); // input for hardware button
// pinMode(SPIN, INPUT); // input for hardware switch
// Serial1.println("Hello World!");
msgD0 = 0x01;
delay(500);
}
void loop() {
// bState = digitalRead(BPIN);
// sState = digitalRead(SPIN);
//Serial1.println("loop");
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
uint32_t currentMillis = millis();
if ((currentMillis - lastSent) >= 1000) {
digitalWrite(PC13, ledState);
ledState != ledState;
lastSent = currentMillis;
uint32_t msgID = 0x1CFFEEF0 ;
// Serial1.print("start CAN Message: ");
// Serial1.println(count);
SendCANmessage(msgID, 3, 0x01, 0x6f, count) ;
count++;
}
// check if the switch is high.
// if it is:
// try to read message and output to serial
CanMsg *r_msg;
if ((r_msg = canBus.recv()) != NULL){
// Serial1.print(r_msg->ID);
// Serial1.print("#");
// Serial1.print(r_msg->Data[0]);
// Serial1.print(".");
// Serial1.print(r_msg->Data[1]);
// Serial1.print(".");
// Serial1.print(r_msg->Data[2]);
// Serial1.print(".");
// Serial1.print(r_msg->Data[3]);
// Serial1.print(".");
// Serial1.print(r_msg->Data[4]);
// Serial1.print(".");
// Serial1.print(r_msg->Data[5]);
// Serial1.print(".");
// Serial1.print(r_msg->Data[6]);
// Serial1.print(".");
// Serial1.println(r_msg->Data[7]);
canBus.free();
}
}