example showing how to use the mbed CAN API

Dependencies:   mbed CANMsg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * An example showing how to use the mbed CAN API:
00003  *
00004  * Two affordable (about $2 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
00005  * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
00006  * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
00007  * CAN transceivers are not part of NUCLEO boards, therefore must be added by you.
00008  * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
00009  *
00010  *
00011  * The same code is used for both mbed boards, but:
00012  *      For board #1 compile the example without any change.
00013  *      For board #2 comment out line 21 before compiling
00014  *
00015  * Once the binaries have been downloaded to the boards reset both boards at the same time.
00016  *
00017  */
00018 
00019 //#define TARGET_STM32F103C8T6    1       // uncomment this line to use STM32F103C8T6 boards
00020 
00021 #define BOARD1                  1       // comment out this line when compiling for board #2
00022 
00023 #if defined(TARGET_STM32F103C8T6)
00024     #define LED_PIN     PC_13
00025     const int           OFF = 1;
00026     const int           ON = 0;
00027 #else
00028     #define LED_PIN     LED1
00029     const int           OFF = 0;
00030     const int           ON = 1;
00031 #endif
00032 
00033 #if defined(BOARD1)
00034     const unsigned int  RX_ID = 0x100;
00035     const unsigned int  TX_ID = 0x101;
00036 #else
00037     const unsigned int  RX_ID = 0x101;
00038     const unsigned int  TX_ID = 0x100;
00039 #endif
00040 
00041 #include "mbed.h"
00042 #include "CANMsg.h"
00043 
00044 Serial              pc(USBTX, USBRX);
00045 CAN                 can(PB_8, PB_9);  // CAN Rx pin name, CAN Tx pin name
00046 //CAN                 can(p30, p29);  // CAN Rx pin name, CAN Tx pin name
00047 CANMsg              rxMsg;
00048 CANMsg              txMsg;
00049 DigitalOut          led(LED_PIN);
00050 Timer               timer;
00051 uint8_t             counter = 0;
00052 AnalogIn            analogIn(A0);
00053 float               voltage;
00054 
00055 /**
00056  * @brief   Prints CAN message to PC's serial terminal
00057  * @note
00058  * @param   CANMessage to print
00059  * @retval
00060  */
00061 void printMsg(CANMessage& msg)
00062 {
00063     pc.printf("  ID      = 0x%.3x\r\n", msg.id);
00064     pc.printf("  Type    = %d\r\n", msg.type);
00065     pc.printf("  Format  = %d\r\n", msg.format);
00066     pc.printf("  Length  = %d\r\n", msg.len);
00067     pc.printf("  Data    =");
00068     for(int i = 0; i < msg.len; i++)
00069         pc.printf(" 0x%.2X", msg.data[i]);
00070     pc.printf("\r\n");
00071 }
00072 
00073 /**
00074  * @brief   Handles received CAN messages
00075  * @note    Called on 'CAN message received' interrupt.
00076  * @param
00077  * @retval
00078  */
00079 void onCanReceived(void)
00080 {
00081     can.read(rxMsg);
00082     pc.printf("-------------------------------------\r\n");
00083     pc.printf("CAN message received\r\n");
00084     printMsg(rxMsg);
00085 
00086     if (rxMsg.id == RX_ID) {
00087         // extract data from the received CAN message 
00088         // in the same order as it was added on the transmitter side
00089         rxMsg >> counter;
00090         rxMsg >> voltage;    
00091         pc.printf("  counter = %d\r\n", counter);
00092         pc.printf("  voltage = %e V\r\n", voltage);
00093     }
00094     timer.start(); // to transmit next message in main
00095 }
00096 
00097 
00098 /**
00099  * @brief   Main
00100  * @note
00101  * @param
00102  * @retval
00103  */
00104 int main(void)
00105 {
00106     pc.baud(9600);          // set serial speed
00107     can.frequency(1000000); // set CAN bit rate to 1Mbps
00108     can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID
00109     can.attach(onCanReceived);                // attach ISR to handle received messages
00110 
00111 #if defined(BOARD1)
00112     led = ON;               // turn the LED on
00113     timer.start();          // start timer
00114     pc.printf("CAN_Hello board #1\r\n");
00115 #else
00116     led = OFF;      // turn LED off
00117     pc.printf("CAN_Hello board #2\r\n");
00118 #endif
00119     while(1) {
00120         if(timer.read_ms() >= 2000) {    // check for timeout
00121             timer.stop();                // stop timer
00122             timer.reset();               // reset timer
00123             counter++;                   // increment counter
00124             voltage = analogIn * 3.3f;   // read the small drift voltage from analog input
00125             txMsg.clear();               // clear Tx message storage
00126             txMsg.id = TX_ID;            // set ID
00127             // append data (total data length must not exceed 8 bytes!)
00128             txMsg << counter;   // one byte
00129             txMsg << voltage;   // four bytes
00130              
00131             if(can.write(txMsg)) {       // transmit message
00132                 led = OFF;               // turn the LED off
00133                 pc.printf("-------------------------------------\r\n");
00134                 pc.printf("-------------------------------------\r\n");
00135                 pc.printf("CAN message sent\r\n");
00136                 printMsg(txMsg);
00137                 pc.printf("  counter = %d\r\n", counter);
00138                 pc.printf("  voltage = %e V\r\n", voltage);
00139             }
00140             else
00141                 pc.printf("Transmission error\r\n");
00142         }
00143     }
00144 }