comit to publish

Dependencies:   CANnucleo CANnucleo_Hello mbed

Fork of CANnucleo_Hello by Zoltan Hudak

Revision:
0:c5e5d0df6f2a
Child:
1:267d6288df33
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Jul 19 09:09:42 2015 +0000
@@ -0,0 +1,97 @@
+/*
+ * An example showing how to use the CANnucleo library:
+ *
+ * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MPC2551 or TJA1040, or etc.).
+ * Transceivers are not part of the NUCLEO boards, therefore must be added by you.
+ * Remember also that CAN bus must be terminated with 120 Ohm resitors on both ends.
+ * See <https://developer.mbed.org/users/WiredHome/notebook/can---getting-started/>
+ * The same source code is used for both NUCLEO boards, but:
+ *      For board #1 compile the example without any change.
+ *      For board #2 set BOARD1 to 0
+ *
+ * Note:
+ *  To simplify adding/getting data to/from a CAN message
+ *  inserter "<<" and extractor ">>" operators have been defined.
+ *  Please be aware that CAN message maximum data length is limited to eight bytes.
+ *  To make sure this limitation is not violated I recommend to first compile
+ *  your application with DEBUG enabled in "CAN.h" file.
+ *  Then run it and check for error messages.
+ */ 
+
+#include "mbed.h"
+#include "CAN.h"
+
+#define BOARD1    1     // please change to 0 when compiling for board #2
+
+#if defined(BOARD1)
+    #define RX_ID   0x100
+    #define TX_ID   0x101
+#else
+    #define RX_ID   0x101
+    #define TX_ID   0x100
+#endif
+
+DigitalOut      led(LED1);
+Timer           timer;
+CAN             can(PA_11, PA_12);  // rx, tx
+CANMessage      rxMsg;
+CANMessage      txMsg;
+long int        counter;
+volatile bool   msgAvailable = false;
+
+/**
+ * @brief   'CAN receive-complete' interrup handler.
+ * @note    Called on arrival of new CAN message.
+ *          Keep it as short as possible.
+ * @param   
+ * @retval  
+ */
+void onMsgReceived() {
+    msgAvailable = true;
+}
+
+/**
+ * @brief   Main
+ * @note
+ * @param 
+ * @retval
+ */
+int main() {
+    can.frequency(500000);                      // Initialize CAN and set bit rate to 500kbs
+    can.attach(&onMsgReceived, CAN::RxIrq);     // attach 'CAN receive complete' interrupt handler
+    timer.reset();
+#if defined(BOARD1)
+        led = 1;
+        timer.start();
+#else
+        led = 0;
+#endif
+
+    while(1) {
+        if(timer.read() >= 2.0) {               // check for timeout
+            timer.stop();                       // stop timer
+            timer.reset();                      // reset timer (to avaoid repeated send)
+            counter++;                          // increment counter
+            txMsg.clear();                      // clear Tx message storage
+            txMsg.id = TX_ID;                   // set ID
+            txMsg << counter;                   // append first data item (always make sure that CAN message total data lenght <= 8 bytes!)
+            txMsg << led.read();                // append second data item (always make sure that CAN message total data lenght <= 8 bytes!)
+            can.write(txMsg);                   // transmit message
+            printf("CAN message sent\r\n");
+            led = 0;                            // turn off led
+        }
+        if(msgAvailable) {
+            msgAvailable = false;               // reset flag for next use
+            can.read(rxMsg);                    // read message into Rx message storage
+            printf("CAN message with ID = %x received\r\n", rxMsg.id);
+            if(rxMsg.id == RX_ID) {             // if ID matches
+                rxMsg >> counter;               // extract first data item
+                rxMsg >> led;                   // extract second data item (and set led status)
+                printf("counter = %d", counter);
+                timer.start();
+            }
+        }
+    }
+}
+
+