]> rtime.felk.cvut.cz Git - arc.git/blobdiff - arch/arm/arm_cr4/drivers/Can.c
Added an example of CAN communication for the TMS570LS31x HDK
[arc.git] / arch / arm / arm_cr4 / drivers / Can.c
index 5e08c9685240571051872bea419cb1e27259f636..3f17ac90be9aefc49e5a4a7490308361774704d3 100644 (file)
@@ -22,8 +22,9 @@
 #include "Det.h"\r
 #include "CanIf_Cbk.h"\r
 #include "Os.h"\r
-#include "irq.h"\r
+#include "isr.h"\r
 #include "Mcu.h"\r
+#include "arc.h"\r
 \r
 #define DCAN1_MAX_MESSAGEBOXES 64\r
 #define DCAN2_MAX_MESSAGEBOXES 64\r
@@ -40,6 +41,8 @@
 #define Dem_ReportErrorStatus(...)\r
 #endif\r
 \r
+static sint8 IfRegId = 0;\r
+\r
 /* Macro for waiting until busy flag is 0 */\r
 #define DCAN_WAIT_UNTIL_NOT_BUSY(ControllerId, IfRegId) \\r
     { \\r
@@ -103,7 +106,6 @@ static Can_StateType            ModuleState = CAN_UNINIT;
 static CanIf_ControllerModeType ControllerMode[CAN_ARC_CTRL_CONFIG_CNT];\r
 \r
 /* Used to switch between IF1 and IF2 of DCAN */\r
-static uint8 IfRegId = 0;\r
 \r
 /* Used to order Data Bytes according to hardware registers in DCAN */\r
 static const uint8 ElementIndex[] = {3, 2, 1, 0, 7, 6, 5, 4};\r
@@ -202,99 +204,146 @@ static inline const Can_HardwareObjectType * Can_FindRxHoh(CanControllerIdType C
        return 0;\r
 }\r
 \r
-uint32 usedBoxes[64] = {0};\r
+#define DCAN_MC_NEWDAT 15\r
+#define DCAN_MC_EOB            7\r
+\r
+uint32 usedRxBoxes[64] = {0};\r
+uint32 usedTxBoxes[64] = {0};\r
+\r
+static inline Can_ReturnType handleRxMsgObject(uint8 MsgNr, const Can_HardwareObjectType *hoh, CanControllerIdType controller) {\r
+       uint32  MsgId;\r
+       uint8   MsgDlc;\r
+       uint8   DataByteIndex;\r
+       uint8  *SduPtr;\r
+\r
+       /* Wait until Busy Flag is 0 */\r
+       DCAN_WAIT_UNTIL_NOT_BUSY(controller, IfRegId);\r
+\r
+       // Read message control\r
+       uint32 mc = CanRegs[controller]->IFx[IfRegId].MC;\r
+       uint32 arb = CanRegs[controller]->IFx[IfRegId].ARB;\r
+\r
+       // Is there a new message waiting?\r
+       if (!(mc & (1 << DCAN_MC_NEWDAT))) {\r
+               return CAN_NOT_OK; // Nothing more to be done.\r
+       }\r
+\r
+       // For debug\r
+       if (MsgNr == 0) {\r
+               usedRxBoxes[MsgNr]++;\r
+       } else {\r
+               usedRxBoxes[MsgNr]++;\r
+       }\r
+\r
+\r
+       /* Extended Id */\r
+       if(arb & 0x40000000) {\r
+               /* Bring Id to standardized format (MSB marks extended Id) */\r
+               MsgId = (arb & 0x1FFFFFFF) | 0x80000000;\r
+\r
+       } else { /* Standard Id */\r
+               /* Bring Id to standardized format (MSB marks extended Id) */\r
+               MsgId = (arb & 0x1FFC0000) >> 18;\r
+       }\r
+\r
+       /* DLC (Max 8) */\r
+       MsgDlc = mc & 0x000F;\r
+       if(MsgDlc > 8) {\r
+               MsgDlc = 8;\r
+       }\r
+\r
+       /* Let SduPtr point to Shadow Buffer */\r
+       SduPtr = RxShadowBuf[controller];\r
+\r
+       /* Copy Message Data to Shadow Buffer */\r
+       for(DataByteIndex = 0; DataByteIndex < MsgDlc; DataByteIndex++)\r
+       {\r
+               SduPtr[DataByteIndex] = CanRegs[controller]->IFx[IfRegId].DATx[ElementIndex[DataByteIndex]];\r
+       }\r
+\r
+       /* Indicate successful Reception */\r
+       CanIf_RxIndication(hoh->CanObjectId, MsgId, MsgDlc, SduPtr);\r
+\r
+       // Is this the last message object of the FIFO?\r
+       if (mc & (1 << DCAN_MC_EOB)) {\r
+               return CAN_NOT_OK;\r
+       }\r
+\r
+       return CAN_OK;\r
+}\r
+\r
 \r
 void Can_InterruptHandler(CanControllerIdType controller)\r
 {\r
     uint32  MsgNr;\r
-    uint32  MsgId;\r
-    uint8   MsgDlc;\r
-    uint8   DataByteIndex;\r
-    uint8  *SduPtr;\r
-\r
-    //Can_DisableControllerInterrupts(controller);\r
 \r
     uint32 ir = CanRegs[controller]->IR;\r
 \r
-    if(ir == 0x8000)\r
-    {\r
+\r
+    if(ir == 0x8000) { // This is an error interrupt\r
+\r
        uint32 sr = CanRegs[controller]->SR;\r
-        /* WakeUp Pending */\r
-        if(sr & 0x00000200) {\r
+\r
+        if(sr & 0x00000200) { /* WakeUp Pending */\r
             /* Set Init Bit, so that Controller is in Stop state */\r
             CanRegs[controller]->CTL |= 0x1;\r
            // EcuM_CheckWakeUp(ControllerConfig[0].WakeupSrc);\r
 \r
         }\r
-        /* Bus Off */\r
-        if(sr & 0x00000080) {\r
+\r
+        if(sr & 0x00000080) { /* Bus Off */\r
                Can_SetControllerMode(controller, CAN_T_STOP); // CANIF272\r
             //CanIf_ControllerBusOff(0); // Not implemented in Arctic Core\r
 \r
         }\r
-    }\r
-    else\r
-    {\r
-        MsgNr = ir;\r
-\r
 \r
-        if (MsgNr == 0) {\r
-               usedBoxes[MsgNr]++;\r
-        } else {\r
-               usedBoxes[MsgNr]++;\r
-        }\r
-        \r
+    } else if (ir > 0 && ir < 0x8000){ // This interrupt is from a message object.\r
+        MsgNr = ir;\r
 \r
-        /* Read Arbitration, Control and Data Bits and clear IntPnd and NewDat*/\r
-        CanRegs[controller]->IFx[IfRegId].COM = 0x003F0000 | MsgNr;\r
+        /* Read Arbitration and control */\r
+               CanRegs[controller]->IFx[IfRegId].COM = 0x003F0000 | MsgNr;\r
 \r
-        /* Wait until Busy Flag is 0 */\r
-        DCAN_WAIT_UNTIL_NOT_BUSY_NO_RV(controller, IfRegId);\r
+               /* Wait until Busy Flag is 0 */\r
+               DCAN_WAIT_UNTIL_NOT_BUSY_NO_RV(controller, IfRegId);\r
 \r
         /* Transmit Object */\r
         if(CanRegs[controller]->IFx[IfRegId].ARB & 0x20000000)\r
         {\r
+               // For debug\r
+               if (MsgNr == 0) {\r
+                       usedTxBoxes[MsgNr]++;\r
+               } else {\r
+                       usedTxBoxes[MsgNr]++;\r
+               }\r
+\r
             /* Reset TxRqst-Array Element */\r
                ControllerConfig[controller].TxPtr[MsgNr - 1] = 0;\r
             /* A Message was successfully transmitted */\r
             CanIf_TxConfirmation(ControllerConfig[controller].PduPtr[MsgNr - 1].swPduHandle);\r
-        }\r
+\r
         /* Receive Object */\r
-        else\r
-        {\r
-            /* Extended Id */\r
-            if(CanRegs[controller]->IFx[IfRegId].ARB & 0x40000000)\r
-            {\r
-                /* Bring Id to standardized format (MSB marks extended Id) */\r
-                MsgId = (CanRegs[controller]->IFx[IfRegId].ARB & 0x1FFFFFFF) | 0x80000000;\r
-            }\r
-            /* Standard Id */\r
-            else\r
-            {\r
-                /* Bring Id to standardized format (MSB marks extended Id) */\r
-                MsgId = (CanRegs[controller]->IFx[IfRegId].ARB & 0x1FFC0000) >> 18;\r
-            }\r
-            /* DLC (Max 8) */\r
-            MsgDlc = CanRegs[controller]->IFx[IfRegId].MC & 0x000F;\r
-            if(MsgDlc > 8)\r
-            {\r
-                MsgDlc = 8;\r
-            }\r
-            /* Let SduPtr point to Shadow Buffer */\r
-            SduPtr = RxShadowBuf[controller];\r
+        } else {\r
 \r
-            /* Copy Message Data to Shadow Buffer */\r
-            for(DataByteIndex = 0; DataByteIndex < MsgDlc; DataByteIndex++)\r
-            {\r
-               SduPtr[DataByteIndex] = CanRegs[controller]->IFx[IfRegId].DATx[ElementIndex[DataByteIndex]];\r
-            }\r
-            /* Indicate successful Reception */\r
-            const Can_HardwareObjectType *hoh = Can_FindRxHoh(controller, MsgNr);\r
-            CanIf_RxIndication(hoh->CanObjectId, MsgId, MsgDlc, SduPtr);\r
+               // Handle all of the message objects in this FIFO buffer.\r
+               const Can_HardwareObjectType *hoh = Can_FindRxHoh(controller, MsgNr);\r
+               for(; MsgNr < ControllerConfig[controller].MaxBoxes; MsgNr++) {\r
+                       if (!(hoh->Can_Arc_MbMask & (1 << (MsgNr - 1)))) {\r
+                               continue;\r
+                       }\r
+\r
+                       /* Read setup hardware to read arbitration, control and data Bits of the message object.\r
+                        * Clear IntPnd and Tx */\r
+                       if (MsgNr != ir) { // Don't do this the first time.\r
+                               CanRegs[controller]->IFx[IfRegId].COM = 0x003F0000 | MsgNr;\r
+                       }\r
+\r
+                       if (handleRxMsgObject(MsgNr, hoh, controller) == CAN_NOT_OK) {\r
+                               break; // We have parsed the last object of this FIFO.\r
+                       }\r
+                       }\r
 \r
         }\r
     }\r
-    //Can_EnableControllerInterrupts(controller);\r
 }\r
 \r
 void Can1_InterruptHandler() {\r
@@ -328,6 +377,7 @@ void Can_Init(const Can_ConfigType *Config)
     uint8                     MsgNr;\r
     uint32                    ErrCounter;\r
     uint32                    Eob;\r
+    imask_t state;\r
 \r
 /* DET Error Check */\r
 #if(CAN_DEV_ERROR_DETECT == STD_ON)\r
@@ -342,7 +392,9 @@ void Can_Init(const Can_ConfigType *Config)
         return;\r
     }\r
 #endif \r
-     \r
+\r
+    Irq_Save(state);\r
+\r
     // TODO This should be used instead of other variables in the Can_Lcfg file.\r
     CurConfig        = Config;\r
 \r
@@ -467,27 +519,17 @@ void Can_Init(const Can_ConfigType *Config)
 #endif\r
 \r
         // Install interrupt handlers\r
-               TaskType tid;\r
                if (CanControllerConfigData[Controller].CanControllerId == DCAN1) {\r
-                       tid = Os_Arc_CreateIsr(Can1_InterruptHandler, 2 ,"DCAN1Level0");\r
-                       Irq_AttachIsr2(tid, NULL, 16);\r
-\r
-                       tid = Os_Arc_CreateIsr(Can1_InterruptHandler, 2, "DCAN1Level1");\r
-                       Irq_AttachIsr2(tid, NULL, 29);\r
+                       ISR_INSTALL_ISR2("DCAN1Level0",Can1_InterruptHandler,CAN1_LEVEL_0,2,0);\r
+                       ISR_INSTALL_ISR2("DCAN1Level1",Can1_InterruptHandler,CAN1_LEVEL_1,2,0);\r
 \r
                } else if (CanControllerConfigData[Controller].CanControllerId == DCAN2) {\r
-                       tid = Os_Arc_CreateIsr(Can2_InterruptHandler, 2 ,"DCAN2Level0");\r
-                       Irq_AttachIsr2(tid, NULL, 35);\r
-\r
-                       tid = Os_Arc_CreateIsr(Can2_InterruptHandler, 2, "DCAN2Level1");\r
-                       Irq_AttachIsr2(tid, NULL, 42);\r
+                       ISR_INSTALL_ISR2("DCAN2Level0",Can2_InterruptHandler,CAN2_LEVEL_0,2,0);\r
+                       ISR_INSTALL_ISR2("DCAN2Level1",Can2_InterruptHandler,CAN2_LEVEL_1,2,0);\r
 \r
                } else if (CanControllerConfigData[Controller].CanControllerId == DCAN3) {\r
-                       tid = Os_Arc_CreateIsr(Can3_InterruptHandler, 2 ,"DCAN3Level0");\r
-                       Irq_AttachIsr2(tid, NULL, 45);\r
-\r
-                       tid = Os_Arc_CreateIsr(Can3_InterruptHandler, 2, "DCAN3Level1");\r
-                       Irq_AttachIsr2(tid, NULL, 55);\r
+                       ISR_INSTALL_ISR2("DCAN3Level0",Can3_InterruptHandler,CAN3_LEVEL_0,2,0);\r
+                       ISR_INSTALL_ISR2("DCAN3Level1",Can3_InterruptHandler,CAN3_LEVEL_1,2,0);\r
 \r
                }\r
 \r
@@ -497,8 +539,15 @@ void Can_Init(const Can_ConfigType *Config)
     ModuleState = CAN_READY;\r
 #endif\r
 \r
+    Irq_Restore(state);\r
 \r
+}\r
 \r
+// Unitialize the module\r
+void Can_DeInit()\r
+{\r
+\r
+  return;\r
 }\r
 \r
 \r
@@ -507,6 +556,7 @@ void Can_InitController(uint8 Controller, const Can_ControllerConfigType* Config
 {\r
     uint8   MsgNr;\r
     uint32  ErrCounter;\r
+    imask_t state;\r
 \r
 #if(CAN_DEV_ERROR_DETECT == STD_ON)\r
     if(Config == NULL)\r
@@ -531,6 +581,8 @@ void Can_InitController(uint8 Controller, const Can_ControllerConfigType* Config
     }\r
 #endif \r
 \r
+    Irq_Save(state);\r
+\r
     ErrCounter = CAN_TIMEOUT_DURATION;\r
     \r
     //for(MsgNr = 0; MsgNr < ControllerConfig[Controller].MessageBoxCount; MsgNr++)\r
@@ -574,6 +626,7 @@ void Can_InitController(uint8 Controller, const Can_ControllerConfigType* Config
     /* Clear CCE Bit */\r
     CanRegs[Controller]->CTL &= ~0x00000040;\r
 \r
+    Irq_Restore(state);\r
 }\r
 \r
 \r
@@ -751,6 +804,7 @@ Can_ReturnType Can_Write(Can_Arc_HTHType Hth, Can_PduType *PduInfo)
     Can_PduType           *CurPduArrayPtr;\r
     uint8                 *CurCancelRqstPtr;\r
     uint8                 *CurTxRqstPtr;\r
+    imask_t state;\r
 \r
     CurSduPtr       = PduInfo->sdu;\r
     \r
@@ -792,8 +846,20 @@ Can_ReturnType Can_Write(Can_Arc_HTHType Hth, Can_PduType *PduInfo)
                if (!(mbMask & 1)) {\r
                        continue; // This message object is not part of this hoh.\r
                }\r
+               /* Check if TxRqst Bit of MsgObject is set */\r
+               if(CanRegs[ControllerId]->TRx[MsgNr >> 5] & (1 << (MsgNr & 0x1F)))\r
+               {\r
+                       continue;\r
+               }\r
+               break;\r
     }\r
 \r
+       /* Check if TxRqst Bit of MsgObject is set */\r
+       if(CanRegs[ControllerId]->TRx[MsgNr >> 5] & (1 << (MsgNr & 0x1F)))\r
+       {\r
+               return CAN_BUSY;\r
+       }\r
+\r
     CurPduArrayPtr   = ControllerConfig[ControllerId].PduPtr    + (MsgNr - 1);\r
     CurCancelRqstPtr = ControllerConfig[ControllerId].CancelPtr + (MsgNr - 1);\r
     CurTxRqstPtr     = ControllerConfig[ControllerId].TxPtr     + (MsgNr - 1);\r
@@ -807,14 +873,12 @@ Can_ReturnType Can_Write(Can_Arc_HTHType Hth, Can_PduType *PduInfo)
         ArbRegValue = 0xA0000000 | ((PduInfo->id & 0x7FF) << 18);\r
     }\r
 \r
-    /* Check if TxRqst Bit of MsgObject is set */\r
-    if(CanRegs[ControllerId]->TRx[MsgNr >> 5] & (1 << (MsgNr & 0x1F)))\r
-    {\r
-        return CAN_BUSY;\r
-    }\r
 \r
     DCAN_WAIT_UNTIL_NOT_BUSY(ControllerId, IfRegId);\r
 \r
+    // We cannot allow an interrupt or other task to play with the COM, MC and ARB registers here.\r
+    Irq_Save(state);\r
+\r
 \r
     /* Set NewDat, TxIE (dep on ControllerConfig), TxRqst, EoB and DLC */\r
     CanRegs[ControllerId]->IFx[IfRegId].MC =     0x00000100 // Tx request\r
@@ -822,10 +886,10 @@ Can_ReturnType Can_Write(Can_Arc_HTHType Hth, Can_PduType *PduInfo)
                                                                                        | (0x000F & PduInfo->length) // Set DLC\r
                                                                                        | (CanControllerConfigData[ControllerId].CanTxProcessing << 1); // Tx confirmation interrupt enabled\r
 \r
-\r
     /* Set ArbitrationRegister */\r
     CanRegs[ControllerId]->IFx[IfRegId].ARB = ArbRegValue;\r
 \r
+\r
     /* Set Databytes */\r
     for(DataByteIndex = 0; DataByteIndex < PduInfo->length; DataByteIndex++)\r
     {\r
@@ -842,6 +906,7 @@ Can_ReturnType Can_Write(Can_Arc_HTHType Hth, Can_PduType *PduInfo)
     \r
     IfRegId ^= 1;\r
        \r
+    Irq_Restore(state);\r
     return CAN_OK;\r
 }\r
 \r