Example how we use hardware CAN Acceptance Mask capability on LPC1768. Documentation is not clear !!!!! Polytech PAris SUd Orsay France

Dependencies:   mbed

Revision:
1:168415f9f4f0
Parent:
0:01ac4cd13c69
--- a/main.cpp	Sun Mar 20 20:41:53 2011 +0000
+++ b/main.cpp	Wed Apr 20 08:48:44 2016 +0000
@@ -1,30 +1,55 @@
 #include "mbed.h"
-#include "CAN.h"
 
 // We use can on mbed pins 29(CAN_TXD) and 30(CAN_RXD).
 CAN can2(p30, p29);
 Serial pc(USBTX, USBRX); // tx, rx
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
+
+// chaque mot 16 bits : (CANnum << 13)| (ID );
+// CANnum : CAN1 ou CAN2
+// ID sur 11 bits
+#define CANNUM_1 0
+#define CANNUM_2 1
+//********* MAcro pour insertion d'ientifiant à accepter ********************************
+#define MCANSET(NUMCAN,IDCAN) (((unsigned)NUMCAN << 13)| ((unsigned)IDCAN&0x7FF ) | (1<<11))
 
-void init_AF( void )
+// ident par ordre croissant
+// ************** Exemple de table d'identifiants à trier par ordre croissant ***********
+//***************************************************************************************
+unsigned short tableAM[]={MCANSET(CANNUM_2,0x2F0),
+                          MCANSET(CANNUM_2,0x2F8),
+                          MCANSET(CANNUM_2,0x300)};
+#define nb_AM 3
+
+//***************************************************************************************
+//---- Fonction d'insertion d'une table d'identifiant à accepter dans le
+//---- filtrage matériel du CAN
+//***************************************************************************************
+void init_AF(unsigned short *t,unsigned n )
 {
-uint32_t address = 4;
+uint32_t address = 0;//adresse debut table des mask sur le CAN
+unsigned i;
+long maskl;
 
 //off mode
    LPC_CANAF->AFMR = 0x00000001;
 // Set explicit standard Frame
    LPC_CANAF->SFF_sa = address;// word 1
-                          // CAN2         IDENT           FORCE1
-  LPC_CANAF_RAM->mask[1]=(0X001 << 29) | (0X400 << 16)| (1<<27)|
-                          (0X001 << 13)| (0X500 )     | (1<<11);
-// Only Frame ID 0x400 and 0x500 are received in MBED.
-   address+=4;//add 4 bytes (1 long word) to point to none used word.
+     
+     for(i=0;i<n;i+=2){
+                    maskl=(unsigned)t[i]<<16;
+                    if(i+1<n) {
+                            maskl=maskl | (unsigned)t[i+1];
+                    }else{
+                          maskl=maskl | 0xFFFF ;
+                    }
+                    LPC_CANAF_RAM->mask[i/2]=maskl;
+                    address+=4;//add 4 bytes (1 long word) to point to none used word.
+
+     }
 // Set group standard Frame
    LPC_CANAF->SFF_GRP_sa = address;
+   
 // Set explicit extended Frame
    LPC_CANAF->EFF_sa = address;
 // Set group extended Frame
@@ -39,29 +64,17 @@
 
 // ISR to read frame but don't work beacause no interrupts
 CANMessage can_MsgRx;
-bool recmess;
 
-void lecture(void){
-   can2.read(can_MsgRx);
-   pc.printf("REC %u\n\r",can_MsgRx.id);
-   recmess=1;
-}
 
 // Main program
+//******************************* exemple utilisation filtrage CAN
 int main() {
-    // 500kbit
-    can2.frequency(500000);
+    can2.frequency(1000000);
    // Prepare MASK
-    init_AF();
+    init_AF(tableAM,3);   // je filtre par rapport à la table donnée
 
     while (1) {
-       pc.printf("%8x  %8x ",LPC_CAN2->RFS,LPC_CANAF->LUTerr);
-       pc.printf("%8x %8x %8x %8x\n\r", LPC_CANAF->LUTerrAd,LPC_CAN2->ICR,LPC_CAN2->IER,LPC_CAN2->GSR);
-       wait(0.5);
-       if(recmess){
-            led4=!led4;
-            recmess=0;
-       }
+
        if(can2.read(can_MsgRx)){
                  pc.printf("REC %x\n\r",can_MsgRx.id);
        }