Hey all!
I'm having issues sending as well (saw Foyds problems). From my analysis of the TRM of CAN and reviewing the driver it seems as if the message buffers aren't being emptied (TX process isn't starting due to arbitration nothing working). I added some debug messages to the driver to figure out what's going on:
Code:
This is UART Log Task 54
.
starting can tx
found buffer
sending message
starting can tx
found buffer
sending message
starting can tx
no buffers :(
starting can tx
no buffers :(
starting can tx
no buffers :(
starting can tx
no buffers :(
This is UART Log Task 60
.
starting can tx
no buffers :(
starting can tx
no buffers :(
starting can tx
no buffers :(
starting can tx
no buffers :(
starting can tx
no buffers :(
I didn't have it hooked up to a transceiver (TxR) first just to see if it was sending bits, it wasn't so I figured the arbitration wasn't starting due to missing TxR. I've designed CAN communications systems before but never wrote the software for it but I have knowledge of uC intereworkings.
I've attempted this with just the example and I've tried it with some modifications and using FreeRTOS implementation. It gets into the driver, the driver setups the message buffer but the hardware never sends.
Code:
//Teensy 3.1 FreeRTOS
#define TEENSY_FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include <FlexCAN.h>
#include <kinetis_flexcan.h>
#define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
#define UARTLog_Task_Priority ( tskIDLE_PRIORITY + 2 )
#define mainLED_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
#define mainDONT_BLOCK (0)
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
#define mainCAN_COUNTER_TX_ID 0x200
static xTimerHandle xLEDTimer = NULL;
FlexCAN CANbus(125000);
static CAN_message_t msg;
//
//static void prvQueueReceiveTask(void *pvParameters)
//{
// while(1)
// {
// digitalWriteFast(13, HIGH);
// //delay(10);
// vTaskDelay(1000);
// digitalWriteFast(13, LOW);
// //delay(1000);
// vTaskDelay(1000);
// }
//}
static void UARTLogTask(void *pvParameters)
{
int count = 0;
int txCount = 0;
while(1)
{
Serial.print("This is UART Log Task ");
Serial.println(msg.buf[0], DEC);
Serial.print("\n");
HWSERIAL.print("This is UART Log Task ");
HWSERIAL.println(count, DEC);
HWSERIAL.print("\n");
count++;
//CAN MESSAGE
msg.len = 8;
msg.id = mainCAN_COUNTER_TX_ID;
txCount = 6;
digitalWriteFast(13, HIGH);
Serial.println(".");
while ( txCount-- ) {
CANbus.write(msg);
msg.buf[0]++;
}
digitalWriteFast(13, LOW);
//digitalWriteFast(13, LOW);
//delay(200);
vTaskDelay(5000);
}
}
static void prvLEDTimerCallback( xTimerHandle xTimer )
{
static int toggle = 0;
if(toggle == 0) {
digitalWriteFast(12, HIGH);
toggle = 1;
}
else {
digitalWriteFast(12, LOW);
toggle = 0;
}
}
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
Serial.begin(38400);
HWSERIAL.begin(38400);
CANbus.begin();
for( int idx=0; idx<8; ++idx ) {
msg.buf[idx] = '0'+idx;
}
//xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
xTaskCreate( UARTLogTask, "ULog", configMINIMAL_STACK_SIZE, NULL, UARTLog_Task_Priority, NULL );
xLEDTimer = xTimerCreate( "LEDTimer", /* A text name, purely to help debugging. */
( mainLED_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
pdTRUE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
( void * ) 0, /* The ID is not used, so can be set to anything. */
prvLEDTimerCallback /* The callback function that switches the LED off. */
);
Serial.print("End of Setup\n");
HWSERIAL.print("End of Setup\n");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("MainLoop Start\n");
HWSERIAL.print("MainLoop Start\n");
xTimerStart( xLEDTimer, mainDONT_BLOCK );
vTaskStartScheduler();
}
Any thoughts would be greatly appreciated!