Can Stable Einstein Version
Dependencies: mbed-STM32F103C8T6 CANMsg
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 * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> 00011 * 00012 * NOTE: When using an STM32F103C8T6 board uncomment line 22 and import the mbed-STM32F103C8T6 library 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 21 before compiling 00017 * 00018 * Once the binaries have been downloaded to the boards reset both boards at the same time. 00019 * 00020 */ 00021 00022 #define TARGET_STM32F103C8T6 1 // uncomment this line and import the mbed-STM32F103C8T6 library when using STM32F103C8T6 boards! 00023 00024 //#define BOARD1 1 // comment out this line when compiling for board #2 00025 00026 #if defined(TARGET_STM32F103C8T6) 00027 //#include "stm32f103c8t6.h" 00028 #define LED_PIN PC_13 00029 const int OFF = 1; 00030 const int ON = 0; 00031 #else 00032 #define LED_PIN LED1 00033 const int OFF = 0; 00034 const int ON = 1; 00035 #endif 00036 #if defined(BOARD1) 00037 const unsigned int RX_ID = 0x100; 00038 const unsigned int TX_ID = 0x101; 00039 #else 00040 const unsigned int RX_ID = 0x101; 00041 const unsigned int TX_ID = 0x100; 00042 #endif 00043 #include "mbed.h" 00044 #include "CANMsg.h" 00045 00046 #define CAN_RF0R (*((volatile unsigned long *)0x4000640C)) 00047 #define CAN_RF1R (*((volatile unsigned long *)0x40006410)) 00048 #define CAN_IER (*((volatile unsigned long *)0x40006414)) 00049 00050 00051 Serial pc(PA_9, PA_10); 00052 CAN can2(PA_11, PA_12); 00053 CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name 00054 CANMsg rxMsg; 00055 CANMsg txMsg; 00056 DigitalOut led(LED_PIN); 00057 Timer timer; 00058 uint8_t counter = 0; 00059 AnalogIn analogIn(A0); 00060 float voltage; 00061 volatile bool got_message = false; 00062 /** 00063 * @brief Prints CAN msg to PC's serial terminal 00064 * @note 00065 * @param CANMessage to print 00066 * @retval 00067 */ 00068 void printMsg(CANMessage& msg) { 00069 pc.printf(" ID = 0x%.3x\r\n", msg.id); 00070 pc.printf(" Type = %d\r\n", msg.type); 00071 pc.printf(" Format = %d\r\n", msg.format); 00072 pc.printf(" Length = %d\r\n", msg.len); 00073 pc.printf(" Data ="); 00074 for(int i = 0; i < msg.len; i++) 00075 pc.printf(" 0x%.2X", msg.data[i]); 00076 pc.printf("\r\n"); 00077 } 00078 00079 void RxIsr(){ 00080 CANMsg temp; 00081 led = ON; 00082 can.read(temp); 00083 got_message = true; 00084 return; 00085 } 00086 00087 /** 00088 * @brief Main 00089 * @note 00090 * @param 00091 * @retval 00092 */ 00093 int main(void) 00094 { 00095 #if defined(TARGET_STM32F103C8T6) 00096 // confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) 00097 #endif 00098 pc.baud(9600); // set Serial speed 00099 can.frequency(5000000); // set bit rate to 1Mbps 00100 can.mode(CAN::Normal); 00101 can.attach(&RxIsr, CAN::RxIrq); 00102 #if defined(BOARD1) 00103 led = ON; // turn the LED on 00104 timer.start(); // start timert 00105 pc.printf("CAN_Hello board #1\r\n"); 00106 #else 00107 led = OFF; // turn LED off 00108 pc.printf("CAN_Hello board #2\r\n"); 00109 #endif 00110 00111 while(1) { 00112 pc.printf("%d\n",got_message); 00113 if(timer.read_ms() >= 1000) { // check for timeout 00114 timer.stop(); // stop timer 00115 timer.reset(); // reset timer 00116 counter++; // increment counter 00117 voltage = analogIn * 3.3f; // read the small drifting voltage from analog input 00118 txMsg.clear(); // clear Tx message storage 00119 txMsg.id = TX_ID; // set ID 00120 txMsg << counter << voltage; // append data (total data length must not exceed 8 bytes!) 00121 if(can.write(txMsg)) { // transmit message 00122 led = OFF; // turn the LED off 00123 pc.printf("-------------------------------------\r\n"); 00124 pc.printf("CAN message sent\r\n"); 00125 printMsg(txMsg); 00126 pc.printf(" counter = %d\r\n", counter); 00127 pc.printf(" voltage = %e V\r\n", voltage); 00128 } 00129 else 00130 pc.printf("Transmission error\r\n"); 00131 } 00132 00133 if(got_message){ 00134 got_message = false; 00135 can.read(rxMsg); 00136 pc.printf("-------------------------------------\r\n"); 00137 pc.printf("CAN message received\r\n"); 00138 printMsg(rxMsg); 00139 00140 // Filtering performed by software: 00141 //if(rxMsg.id == RX_ID) { 00142 rxMsg >> counter >> voltage; // extract data from the received CAN message 00143 pc.printf(" counter = %d\r\n", counter); 00144 pc.printf(" voltage = %e V\r\n", voltage); 00145 timer.start(); // transmission lag 00146 // } 00147 } 00148 00149 } 00150 }
Generated on Fri Aug 12 2022 04:29:59 by 1.7.2