Ejemplo CAN Hello W!

Dependencies:   mbed-dev CANnucleo

Revision:
21:7120a0dcc8ee
Parent:
20:eb1a8042605e
Child:
22:f4682a5ddda6
--- a/main.cpp	Tue Aug 16 21:13:47 2016 +0000
+++ b/main.cpp	Thu Dec 01 21:14:59 2016 +0000
@@ -9,7 +9,7 @@
  *
  * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/>
  *
- * NOTE: If you'd like to use the official NUCLEO boards comment out line 22
+ * NOTE: If you'd like to use an STM32F103C8T6 board uncomment line 23
  *
  * The same code is used for both NUCLEO boards, but:
  *      For board #1 compile the example without any change.
@@ -19,10 +19,11 @@
  *
  */ 
 
-//#define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!                                    
-#define BOARD1                1     // comment out this line when compiling for board #2
+//#define BOARD1                1     // comment out this line when compiling for board #2
+#define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!                                    
 
 #if defined(TARGET_STM32F103C8T6)
+    #include "stm32f103c8t6.h"
     #define LED_PIN PC_13
     const int OFF = 1;
     const int ON  = 0;
@@ -48,7 +49,7 @@
  * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
  * Remember to qualify them with the CANnucleo namespace.
  */
-CANnucleo::CAN          can(PA_11, PA_12);  // CAN Rx pin name, CAN Tx pin name
+CANnucleo::CAN*         can;
 CANnucleo::CANMessage   rxMsg;
 CANnucleo::CANMessage   txMsg;
 DigitalOut              led(LED_PIN);
@@ -75,8 +76,12 @@
  * @retval
  */
 int main() {
-    can.frequency(1000000);                     // set bit rate to 1Mbps
-    can.attach(&onMsgReceived);                 // attach 'CAN receive-complete' interrupt handler
+#if defined(TARGET_STM32F103C8T6)
+    confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
+#endif
+    can = new CANnucleo::CAN(PA_11, PA_12);      // CAN Rx pin name, CAN Tx pin name
+    can->frequency(1000000);                     // set bit rate to 1Mbps
+    can->attach(&onMsgReceived);                 // attach 'CAN receive-complete' interrupt handler
     
 #if defined(BOARD1)
     led = ON;       // turn LED on
@@ -88,7 +93,7 @@
 #endif
 
     while(1) {
-        if(timer.read() >= 1.0) {               // check for timeout
+        if(timer.read_ms() >= 1000) {               // check for timeout
             timer.stop();                       // stop timer
             timer.reset();                      // reset timer
             counter++;                          // increment counter
@@ -98,14 +103,14 @@
             txMsg << counter;                   // append first data item
             txMsg << ledState;                  // append second data item (total data lenght must be <= 8 bytes!)
             led = OFF;                          // turn LED off
-            if(can.write(txMsg))                // transmit message
+            if(can->write(txMsg))               // transmit message
                 printf("CAN message sent\r\n"); 
             else
                 printf("Transmission error\r\n");
         }
         if(msgAvailable) {
             msgAvailable = false;               // reset flag for next use
-            can.read(rxMsg);                    // read message into Rx message storage
+            can->read(rxMsg);                    // read message into Rx message storage
             printf("CAN message received\r\n");
             printf("  ID      = 0x%.3x\r\n", rxMsg.id);
             printf("  Type    = %d\r\n", rxMsg.type);
@@ -116,7 +121,7 @@
                 printf(" %.2x", rxMsg.data[i]);
             printf("\r\n");
             // Filtering performed by software:           
-            if(rxMsg.id == RX_ID) {             // See comments in CAN.cpp for filtering performed by hardware
+            if(rxMsg.id == RX_ID) {             // See comments in CANnucleo.cpp for filtering performed by hardware
                 rxMsg >> counter;               // extract first data item
                 rxMsg >> ledState;              // extract second data item
                 printf("  counter = %d\r\n", counter);