CAN library containing a CAN controller object handling a FIFO, and CAN peripherals attached to it.

Files at this revision

API Documentation at this revision

Comitter:
garivetm
Date:
Sat Sep 12 12:36:45 2020 +0000
Parent:
4:0ed21bbd917b
Commit message:
Enable Bus Off auto management ; Pass Rx and TX as constructor parameters ; Add a timeout to the write method.

Changed in this revision

ControllerCAN.cpp Show annotated file Show diff for this revision Revisions of this file
ControllerCAN.h Show annotated file Show diff for this revision Revisions of this file
PeripheralCAN.cpp Show annotated file Show diff for this revision Revisions of this file
PeripheralCAN.h Show annotated file Show diff for this revision Revisions of this file
--- a/ControllerCAN.cpp	Sat May 05 13:46:16 2018 +0000
+++ b/ControllerCAN.cpp	Sat Sep 12 12:36:45 2020 +0000
@@ -5,7 +5,7 @@
 INSTITUTION : IUT de CACHAN - 9 av. de la div. Leclerc - 94230 CACHAN                
                                                                                       
 VERSIONS    : v1 (03/07/2012) : FIFO organisation                             
-              v2 (18/02/2016) : Controller aand Peripheral organisation
+              v2 (18/02/2016) : Controller and Peripheral organisation
 
 ****************************************************************************
 Copyright 2016 LARNAUDIE GARIVET
@@ -25,11 +25,13 @@
 #include "mbed.h"
 #include "ControllerCAN.h"
 
-CAN ControllerCAN::can(PA_8, PA_9);
-
-ControllerCAN::ControllerCAN(){
-    can.frequency(250000);              // Baud rate : kbits/s
+ControllerCAN::ControllerCAN(PinName rd, PinName td) : can(rd, td, 250000) {
+    // Enable bus off auto mangement
+    unsigned long* CAN_MCR = (unsigned long*) 0x40006400;
+    *CAN_MCR |= CAN_MCR_ABOM;
+    
     can.attach(callback(this, &ControllerCAN::can_ISR_Reader));        // CAN ISR
+    
     FIFO_ecriture = 0;
     FIFO_lecture = 0;
     FIFO_occupation = 0;
@@ -64,9 +66,9 @@
     if(FIFO_occupation!=0)
     {
         char res = 0;
-        for (int i = 0; i < peripherals.size(); i++){
+        for (uint32_t i = 0; i < peripherals.size(); i++){
             vector<unsigned short*> IdsRead = peripherals[i]->getIdsRead();
-            for (int j = 0; j < IdsRead.size() ; j++){
+            for (uint32_t j = 0; j < IdsRead.size() ; j++){
                 if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
                     peripherals[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]);
                     res = 1;
--- a/ControllerCAN.h	Sat May 05 13:46:16 2018 +0000
+++ b/ControllerCAN.h	Sat Sep 12 12:36:45 2020 +0000
@@ -69,8 +69,11 @@
 class ControllerCAN {
     public :
     /** Create ControllerCAN instance
+     *
+     * @param rx Rd pin
+     * @param tx td pin
      */
-    ControllerCAN();
+    ControllerCAN(PinName rd, PinName td);
     
     /** Destroy ControllerCAN instance
      */
@@ -114,7 +117,7 @@
     
     private :
     
-    static CAN can;
+    CAN can;
     vector<PeripheralCAN*> peripherals;
     unsigned char FIFO_ecriture;
     signed char FIFO_lecture;
--- a/PeripheralCAN.cpp	Sat May 05 13:46:16 2018 +0000
+++ b/PeripheralCAN.cpp	Sat Sep 12 12:36:45 2020 +0000
@@ -5,7 +5,7 @@
 INSTITUTION : IUT de CACHAN - 9 av. de la div. Leclerc - 94230 CACHAN                
                                                                                       
 VERSIONS    : v1 (03/07/2012) : FIFO organisation                             
-               v2 (18/02/2016) : Controller aand Peripheral organisation
+              v2 (18/02/2016) : Controller aand Peripheral organisation
 
 ****************************************************************************
 Copyright 2016 LARNAUDIE GARIVET
@@ -36,13 +36,22 @@
     IdsRead.push_back(Id);
 }
 
-void PeripheralCAN::writeOnCAN(unsigned short Id, const char *data, char len){
-    if (len != 0){
-        while(!controllerCAN->writeData(Id, data, len));
-    }
-    else{
-        while(!controllerCAN->writeRemote(Id));
-    }
+bool PeripheralCAN::writeOnCAN(unsigned short Id, const char *data, char len, unsigned int timeout){
+    uint32_t tickstart = 0;
+
+    /* Get start tick value */
+    tickstart = HAL_GetTick();
+    
+    do{
+        if (len != 0){
+            if(controllerCAN->writeData(Id, data, len)) return true;
+        }
+        else{
+            if(controllerCAN->writeRemote(Id)) return true;
+        }
+    } while((HAL_GetTick() - tickstart) < timeout);
+    
+    return false;
 }
 
 void PeripheralCAN::init(void){
--- a/PeripheralCAN.h	Sat May 05 13:46:16 2018 +0000
+++ b/PeripheralCAN.h	Sat Sep 12 12:36:45 2020 +0000
@@ -63,8 +63,11 @@
      * @param Id Id message
      * @param data char array containing data to be send
      * @param len size of the data array 
+     * @param timeout timeout value in ms (if 0 only one write attempt)
+     *
+     * @returns Write status : true if message was successfully sent, false else.
      */
-    void writeOnCAN(unsigned short Id, const char *data, char len);
+    bool writeOnCAN(unsigned short Id, const char *data, char len, unsigned int timeout = 0);
     //short readOnCAN(unsigned short Id, CANMessage& msg);
     
     /** Get the IdsRead vector
@@ -75,6 +78,8 @@
     
     private :
     vector<unsigned short*> IdsRead;
+
+    protected:
     ControllerCAN* controllerCAN;
 };