Using CAN bus with (not just NUCLEO) mbed boards support Cortex-M0

Dependencies:   CANMsg mbed

Fork of CAN_Hello by QT Chan

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 (less than $3 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  * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/>
00011  *
00012  * NOTE: If you'd like to use an STM32F103C8T6 board uncomment line 23
00013  *
00014  * The same code is used for both mbed boards, but:
00015  *      For board #1 compile the example without any change.
00016  *      For board #2 comment out line 23 before compiling
00017  *
00018  * Once the binaries have been downloaded to the boards reset board #1.
00019  *
00020  */
00021 #define BOARD1                  1       // comment out this line when compiling for board #2
00022 
00023 //#define TARGET_STM32F103C8T6    1       // uncomment this line when using STM32F103C8T6 boards!
00024 
00025 #if defined(TARGET_STM32F103C8T6)
00026 #include "stm32f103c8t6.h"
00027 #define LED_PIN PC_13
00028 const int           OFF = 1;
00029 const int           ON = 0;
00030 #else
00031 #define LED_PIN LED1
00032 const int           OFF = 0;
00033 const int           ON = 1;
00034 #endif
00035 #if defined(BOARD1)
00036 const unsigned int  RX_ID = 0x100;
00037 const unsigned int  TX_ID = 0x101;
00038 #else
00039 const unsigned int  RX_ID = 0x101;
00040 const unsigned int  TX_ID = 0x100;
00041 #endif
00042 #include "mbed.h"
00043 #include "CANMsg.h"
00044 
00045 Serial              pc(PA_2, PA_3);
00046 CAN                 can(PA_11, PA_12);  // 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 msg to PC's serial terminal
00057  * @note
00058  * @param   CANMessage to print
00059  * @retval  none
00060  */
00061 void printMsg(CANMessage& msg) {
00062     pc.printf("  ID      = 0x%.3x\r\n", msg.id);
00063     pc.printf("  Type    = %d\r\n", msg.type);
00064     pc.printf("  Format  = %d\r\n", msg.format);
00065     pc.printf("  Length  = %d\r\n", msg.len);
00066     pc.printf("  Data    =");
00067     for(int i = 0; i < msg.len; i++)
00068         pc.printf(" 0x%.2X", msg.data[i]);
00069     pc.printf("\r\n");
00070 }
00071 
00072 /**
00073  * @brief   Main
00074  * @note
00075  * @param
00076  * @retval
00077  */
00078 int main(void)
00079 {
00080 #if defined(TARGET_STM32F103C8T6)
00081     confSysClock();         //Configure system clock (72MHz HSE clock, 48MHz USB clock)
00082 #endif
00083     pc.baud(9600);          // set Serial speed
00084     can.frequency(1000000); // set bit rate to 1Mbps
00085 #if defined(BOARD1)
00086     led = ON;               // turn the LED on
00087     timer.start();          // start timer
00088     pc.printf("CAN_Hello board #1\r\n");
00089 #else
00090     led = OFF;      // turn LED off
00091     pc.printf("CAN_Hello board #2\r\n");
00092 #endif
00093     while(1) {
00094         if(timer.read_ms() >= 1000) {   // check for timeout
00095             timer.stop();               // stop timer
00096             timer.reset();              // reset timer
00097             counter++;                  // increment counter
00098             voltage = (analogIn * 3.3f) / 4096.0f;    // read the small drifting voltage from analog input
00099             txMsg.clear();              // clear Tx message storage
00100             txMsg.id = TX_ID;           // set ID
00101             txMsg << voltage << counter;// append data (total data length must be <= 8 bytes!)
00102             if(can.write(txMsg)) {      // transmit message
00103                 led = OFF;              // turn the LED off
00104                 pc.printf("-------------------------------------\r\n");
00105                 pc.printf("CAN message sent\r\n");
00106                 printMsg(txMsg);
00107                 pc.printf("  counter = %d\r\n", counter);
00108                 pc.printf("  voltage = %e V\r\n", voltage);
00109             }
00110             else
00111                 pc.printf("Transmission error\r\n");
00112         }
00113 
00114         if(can.read(rxMsg)) {
00115             led = ON;       // turn the LED on
00116             pc.printf("-------------------------------------\r\n");
00117             pc.printf("CAN message received\r\n");
00118             printMsg(rxMsg);
00119 
00120             // Filtering performed by software:
00121             if(rxMsg.id == RX_ID) {
00122                 rxMsg >> voltage >> counter;    // extract data from the received CAN message
00123                 pc.printf("  counter = %d\r\n", counter);
00124                 pc.printf("  voltage = %e V\r\n", voltage);
00125                 timer.start();                  // transmission lag
00126             }
00127         }
00128     }
00129 }