]> rtime.felk.cvut.cz Git - arc.git/blob - include/Can.h
Merge branch 'mikulka' of git@rtime.felk.cvut.cz:arc into mikulka
[arc.git] / include / Can.h
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 \r
17 #ifndef CAN_H_\r
18 #define CAN_H_\r
19 \r
20 \r
21 #define CAN_VENDOR_ID                       1\r
22 #define CAN_MODULE_ID                       MODULE_ID_CAN\r
23 #define CAN_AR_MAJOR_VERSION  2\r
24 #define CAN_AR_MINOR_VERSION    2\r
25 #define CAN_AR_PATCH_VERSION    2\r
26 \r
27 #define CAN_SW_MAJOR_VERSION    1\r
28 #define CAN_SW_MINOR_VERSION    0\r
29 #define CAN_SW_PATCH_VERSION    0\r
30 \r
31 #define CAN_E_PARAM_POINTER     0x01\r
32 #define CAN_E_PARAM_HANDLE     0x02\r
33 #define CAN_E_PARAM_DLC         0x03\r
34 #define CAN_E_PARAM_CONTROLLER 0x04\r
35 // API service used without initialization\r
36 #define CAN_E_UNINIT           0x05\r
37 // Init transition for current mode\r
38 #define CAN_E_TRANSITION       0x06\r
39 \r
40 #define CAN_E_DATALOST         0x07     /** @req 4.0.3/CAN395 */\r
41 \r
42 /** @name Service id's */\r
43 //@{\r
44 #define CAN_INIT_SERVICE_ID                         0x00\r
45 #define CAN_MAINFUNCTION_WRITE_SERVICE_ID           0x01\r
46 #define CAN_INITCONTROLLER_SERVICE_ID               0x02\r
47 #define CAN_SETCONTROLLERMODE_SERVICE_ID            0x03\r
48 #define CAN_DISABLECONTROLLERINTERRUPTS_SERVICE_ID  0x04\r
49 #define CAN_ENABLECONTROLLERINTERRUPTS_SERVICE_ID   0x05\r
50 #define CAN_WRITE_SERVICE_ID                        0x06\r
51 #define CAN_GETVERSIONINFO_SERVICE_ID               0x07\r
52 #define CAN_MAINFUNCTION_READ_SERVICE_ID            0x08\r
53 #define CAN_MAINFUNCTION_BUSOFF_SERVICE_ID          0x09\r
54 #define CAN_MAINFUNCTION_WAKEUP_SERVICE_ID          0x0a\r
55 #define CAN_CBK_CHECKWAKEUP_SERVICE_ID              0x0b\r
56 //@}\r
57 \r
58 \r
59 #if defined(CFG_PPC)\r
60 \r
61 /* HOH flags */\r
62 #define CAN_HOH_FIFO_MASK           (1UL<<0)\r
63 //#define CAN_HOH_EOL_MASK            (1<<9)\r
64 \r
65 /* Controller flags */\r
66 #define CAN_CTRL_RX_PROCESSING_INTERRUPT        (1UL<<0)\r
67 #define CAN_CTRL_RX_PROCESSING_POLLING          0\r
68 #define CAN_CTRL_TX_PROCESSING_INTERRUPT        (1UL<<1)\r
69 #define CAN_CTRL_TX_PROCESSING_POLLING          0\r
70 #define CAN_CTRL_WAKEUP_PROCESSING_INTERRUPT    (1UL<<2)\r
71 #define CAN_CTRL_WAKEUP_PROCESSING_POLLING      0\r
72 #define CAN_CTRL_BUSOFF_PROCESSING_INTERRUPT    (1UL<<3)\r
73 #define CAN_CTRL_BUSOFF_PROCESSING_POLLING      0\r
74 #define CAN_CTRL_ACTIVATION                     (1UL<<4)\r
75 \r
76 #define CAN_CTRL_LOOPBACK                       (1UL<<5)\r
77 #define CAN_CTRL_FIFO                           (1UL<<6)\r
78 \r
79 #define CAN_CTRL_ERROR_PROCESSING_INTERRUPT    (1UL<<7)\r
80 #define CAN_CTRL_ERROR_PROCESSING_POLLING      0\r
81 \r
82 #endif\r
83 \r
84 #include "Std_Types.h"\r
85 #include "CanIf_Types.h"\r
86 #include "ComStack_Types.h"\r
87 #include "Mcu.h"\r
88 \r
89 \r
90 typedef struct {\r
91         uint32 txSuccessCnt;\r
92         uint32 rxSuccessCnt;\r
93         uint32 txErrorCnt;\r
94         uint32 rxErrorCnt;\r
95         uint32 boffCnt;\r
96         uint32 fifoOverflow;\r
97         uint32 fifoWarning;\r
98 } Can_Arc_StatisticsType;\r
99 \r
100 \r
101 #if defined(CFG_CAN_TEST)\r
102 typedef struct {\r
103         uint64_t mbMaskTx;\r
104         uint64_t mbMaskRx;\r
105 } Can_TestType;\r
106 #endif\r
107 \r
108 // uint16: if only Standard IDs are used\r
109 // uint32: if also Extended IDs are used\r
110 typedef uint32 Can_IdType;\r
111 \r
112 /* Good things to know\r
113  * L-PDU  - Link PDU\r
114  *\r
115  *\r
116  */\r
117 \r
118 typedef struct Can_PduType_s {\r
119         // the CAN ID, 29 or 11-bit\r
120         Can_IdType      id;\r
121         // Length, max 8 bytes\r
122         uint8           length;\r
123         // data ptr\r
124         uint8           *sdu;\r
125         // private data for CanIf,just save and use for callback\r
126         PduIdType   swPduHandle;\r
127 } Can_PduType;\r
128 \r
129 \r
130 typedef enum {\r
131         CAN_T_START,\r
132         CAN_T_STOP,\r
133         CAN_T_SLEEP,\r
134         CAN_T_WAKEUP\r
135 } Can_StateTransitionType;\r
136 \r
137 typedef enum {\r
138         CAN_OK,\r
139         CAN_NOT_OK,\r
140         CAN_BUSY\r
141 //      CAN_WAKEUP,             // Removed in 3.0\r
142 } Can_ReturnType;\r
143 \r
144 /* Error from  CAN controller */\r
145 typedef union {\r
146      volatile uint32_t R;\r
147      struct {\r
148          volatile uint32_t:24;\r
149          volatile uint32_t BIT1ERR:1;\r
150          volatile uint32_t BIT0ERR:1;\r
151          volatile uint32_t ACKERR:1;\r
152          volatile uint32_t CRCERR:1;\r
153          volatile uint32_t FRMERR:1;\r
154          volatile uint32_t STFERR:1;\r
155          volatile uint32_t TXWRN:1;\r
156          volatile uint32_t RXWRN:1;\r
157      } B;\r
158  } Can_Arc_ErrorType;\r
159 \r
160 \r
161 \r
162 #if defined(CFG_PPC)\r
163 \r
164 \r
165  typedef enum {\r
166      CAN_ID_TYPE_EXTENDED,\r
167      CAN_ID_TYPE_MIXED,\r
168      CAN_ID_TYPE_STANDARD\r
169  } Can_IdTypeType;\r
170 \r
171  typedef enum {\r
172       CAN_ARC_HANDLE_TYPE_BASIC,\r
173       CAN_ARC_HANDLE_TYPE_FULL\r
174   } Can_Arc_HohType;\r
175 \r
176 \r
177  typedef struct Can_Callback {\r
178      void (*CancelTxConfirmation)( const Can_PduType *);\r
179      void (*RxIndication)( uint8 ,Can_IdType ,uint8 , const uint8 * );\r
180      void (*ControllerBusOff)(uint8);\r
181      void (*TxConfirmation)(PduIdType);\r
182      void (*ControllerWakeup)(uint8);\r
183      void (*Arc_Error)(uint8,Can_Arc_ErrorType);\r
184  } Can_CallbackType;\r
185 \r
186 \r
187 #include "Can_Cfg.h"\r
188 \r
189 \r
190  typedef struct Can_HardwareObjectStruct {\r
191      /* PC/PB, Specifies the type (Full-CAN or Basic-CAN) of a hardware object. */\r
192      Can_Arc_HohType CanHandleType;\r
193 \r
194      /* PC/PB, Specifies whether the IdValue is of type - standard identifier - extended\r
195       * identifier - mixed mode ImplementationType: Can_IdType */\r
196      Can_IdTypeType CanIdType;\r
197 \r
198      /* PC/PB Specifies (together with the filter mask) the identifiers range that passes\r
199       *  the hardware filter. */\r
200      uint32 CanIdValue;\r
201 \r
202      /* PC/PB Holds the handle ID of HRH or HTH. The value of this parameter is unique\r
203       *  in a given CAN Driver, and it should start with 0 and continue without any\r
204       *  gaps. The HRH and HTH Ids are defined under two different name-spaces.\r
205       *  Example: HRH0-0, HRH1-1, HTH0-2, HTH1-3 */\r
206      uint32 CanObjectId;\r
207 \r
208      /* PC/PB, Specifies if the HardwareObject is used as Transmit or as Receive object */\r
209      Can_ObjectTypeType CanObjectType;\r
210 \r
211      /* Reference to CAN Controller to which the HOH is associated to.  */\r
212      uint8 Can_ControllerRef;\r
213 \r
214      /* PC/PB Reference to the filter mask that is used for hardware filtering togerther\r
215       * with the CAN_ID_VALUE */\r
216      Can_FilterMaskType *CanFilterMaskRef;\r
217 \r
218      /* PC,  See CAN_HOH_XX macros */\r
219      uint32 Can_Arc_Flags;\r
220 \r
221      /* PC, Number of mailboxes that is owned by this HOH */\r
222 //     uint8  ArcCanNumMailboxes;\r
223      uint64  ArcMailboxMask;\r
224  } Can_HardwareObjectType;\r
225 \r
226 \r
227 \r
228  typedef struct Can_ControllerConfig {\r
229 \r
230      bool CanControllerActivation;\r
231 \r
232      // Specifies the buadrate of the controller in kbps.\r
233      uint32 CanControllerBaudRate;\r
234 \r
235      //  This parameter provides the controller ID which is unique in a given CAN\r
236      //  Driver. The value for this parameter starts with 0 and continue without any\r
237      //  gaps.\r
238 \r
239      CanControllerIdType CanControllerId;\r
240 \r
241      // Specifies propagation delay in time quantas.\r
242      uint8 CanControllerPropSeg;\r
243 \r
244      // Specifies phase segment 1 in time quantas.\r
245      uint8 CanControllerSeg1;\r
246 \r
247      // Specifies phase segment 2 in time quantas.\r
248      uint8 CanControllerSeg2;\r
249 \r
250      //  Specifies the time quanta for the controller. The calculation of the resulting\r
251      //  prescaler value depending on module clocking and time quanta shall be\r
252      //  done offline Hardware specific.\r
253 //     uint32 CanControllerTimeQuanta;\r
254 \r
255      //  Reference to the CPU clock configuration, which is set in the MCU driver\r
256      //  configuration\r
257      uint32 CanCpuClockRef;\r
258 \r
259      //  This parameter contains a reference to the Wakeup Source for this\r
260      //  controller as defined in the ECU State Manager. Implementation Type:\r
261      //  reference to EcuM_WakeupSourceType\r
262      uint32 CanWakeupSourceRef;\r
263 \r
264      /* Flags, See CAN_CTRL_XX macros */\r
265      uint32 Can_Arc_Flags;\r
266 \r
267      /* Number of FIFO MB in the HOH list */\r
268      uint8 Can_Arc_HohFifoCnt;\r
269 \r
270      /* Total number of HOHs in Can_Arc_Hoh */\r
271      uint8 Can_Arc_HohCnt;\r
272 \r
273      // List of Hoh id's that belong to this controller\r
274      const Can_HardwareObjectType  * const Can_Arc_Hoh;\r
275 \r
276      uint64 Can_Arc_RxMailBoxMask;\r
277      uint64 Can_Arc_TxMailBoxMask;\r
278 \r
279      const uint8 * const Can_Arc_MailBoxToHrh;\r
280 \r
281      PduIdType * const Can_Arc_TxPduHandles;\r
282 \r
283      uint8 Can_Arc_TxMailboxStart;\r
284 \r
285      uint8 Can_Arc_MailboxMax;\r
286 \r
287 #if 1\r
288     //uint32 flags;\r
289 #else\r
290      boolean Can_Arc_Loopback;\r
291 \r
292      // Set this to use the fifo\r
293      boolean Can_Arc_Fifo;\r
294 #endif\r
295 \r
296  } Can_ControllerConfigType;\r
297 \r
298 \r
299 #else\r
300 #include "Can_Cfg.h"\r
301 \r
302 #endif /* defined(PPC) */\r
303 \r
304 // Each controller has 32 hth's, so the division of 32 will give the\r
305 // controller.\r
306 #define GET_CANCONTROLLER(a) (a / HTH_DIVIDER)\r
307 \r
308 \r
309 void Can_Init( const Can_ConfigType *Config );\r
310 void Can_DeInit(void);\r
311 \r
312 #if ( CAN_VERSION_INFO_API == STD_ON )\r
313 #define Can_GetVersionInfo(_vi) STD_GET_VERSION_INFO(_vi,CAN)\r
314 #endif\r
315 \r
316 void Can_InitController( uint8 controller, const Can_ControllerConfigType *config);\r
317 Can_ReturnType Can_SetControllerMode( uint8 Controller, Can_StateTransitionType transition );\r
318 void Can_DisableControllerInterrupts( uint8 controller );\r
319 void Can_EnableControllerInterrupts( uint8 controller );\r
320 // Hth - for Flexcan, the hardware message box number... .We don't care\r
321 \r
322 \r
323 Can_ReturnType Can_Write( Can_Arc_HTHType hth, Can_PduType *pduInfo );\r
324 \r
325 void Can_Cbk_CheckWakeup( uint8 controller );\r
326 void Can_MainFunction_Write( void );\r
327 void Can_MainFunction_Read( void );\r
328 void Can_MainFunction_BusOff( void );\r
329 void Can_MainFunction_Error( void );\r
330 void Can_MainFunction_Wakeup( void );\r
331 \r
332 void Can_Arc_GetStatistics( uint8 controller, Can_Arc_StatisticsType * stat);\r
333 #if defined(CFG_CAN_TEST)\r
334 Can_TestType *Can_Arc_GetTestInfo( void  );\r
335 #endif\r
336 \r
337 \r
338 #endif /*CAN_H_*/\r