CARME-M4 BSP  V1.5
can.c

Explains how to communicate over the CAN bus.

/*
* This example shows how to communicate over the CAN bus.
*/
#include <stm32f4xx.h>
#include <stdio.h>
#include <carme.h>
#include <can.h>
#include <lcd.h>
/* maximal number of chars per line on lcd */
#define LCD_STRING_LENGTH (LCD_HOR_RESOLUTION / FONT_MIN_WIDTH)
static __IO uint32_t TimingDelay;
/*
* make a delay with the frequency of the systick
*/
void Delay(__IO uint32_t nTime) {
TimingDelay = nTime;
while (TimingDelay != 0);
}
void SysTick_Handler(void) {
if (TimingDelay) {
TimingDelay--;
}
}
int main(void) {
uint16_t counter = 0;
char LCDmsg[LCD_STRING_LENGTH];
uint8_t i;
/* initialize systick with 1 kHz */
if (SysTick_Config(SystemCoreClock / 1000)) {
while (1);
}
/* initialize the LCD */
LCD_Init();
LCD_SetFont(&font_6x12);
/* initialize the SJA1000 chip */
/* initialize the CAN message */
CANmsg.data[2] = 0;
CANmsg.data[3] = 0;
CANmsg.data[4] = 0;
CANmsg.data[5] = 0;
CANmsg.data[6] = 0;
CANmsg.data[7] = 0;
/* set the SJA1000 chip in running mode */
while (1) {
/* generate the CAN message */
CANmsg.id = 0x100;
CANmsg.rtr = 0;
CANmsg.ext = 0;
CANmsg.dlc = 2;
counter++;
CANmsg.data[1] = counter >> 8;
CANmsg.data[0] = counter;
/* send the message to the LCD */
snprintf(LCDmsg, sizeof(LCDmsg),
"CAN Tx: ID=0x%04lX DLC=%d D0=0x%02X D1=0x%02X", CANmsg.id,
CANmsg.dlc, CANmsg.data[0], CANmsg.data[1]);
LCD_Log_AddMsg(LCDmsg);
/* send the message to the CAN bus */
CARME_CAN_Write(&CANmsg);
/* check if anything is received from the CAN bus */
if (CARME_CAN_Read(&CANmsg) == CARME_NO_ERROR) {
/* send it to LCD */
snprintf(LCDmsg, sizeof(LCDmsg), "CAN Rx: ID=0x%04lX DLC=%d",
CANmsg.id, CANmsg.dlc);
for (i = 0; i < CANmsg.dlc; i++) {
snprintf(LCDmsg, sizeof(LCDmsg), "%s D%1u=0x%02X", LCDmsg, i,
CANmsg.data[i]);
}
LCD_Log_AddMsg(LCDmsg);
}
Delay(2000);
}
return 0;
}