]> rtime.felk.cvut.cz Git - arc.git/blob - include/Can.h
Merge with 7250e20c985c4a8c7c4d9655ca50c49ef37a0443
[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 #endif\r
80 \r
81 #include "Std_Types.h"\r
82 #include "CanIf_Types.h"\r
83 #include "ComStack_Types.h"\r
84 #include "Mcu.h"\r
85 \r
86 \r
87 typedef struct {\r
88         uint32 txSuccessCnt;\r
89         uint32 rxSuccessCnt;\r
90         uint32 txErrorCnt;\r
91         uint32 rxErrorCnt;\r
92         uint32 boffCnt;\r
93         uint32 fifoOverflow;\r
94         uint32 fifoWarning;\r
95 } Can_Arc_StatisticsType;\r
96 \r
97 \r
98 // uint16: if only Standard IDs are used\r
99 // uint32: if also Extended IDs are used\r
100 typedef uint32 Can_IdType;\r
101 \r
102 /* Good things to know\r
103  * L-PDU  - Link PDU\r
104  *\r
105  *\r
106  */\r
107 \r
108 typedef struct Can_PduType_s {\r
109         // the CAN ID, 29 or 11-bit\r
110         Can_IdType      id;\r
111         // Length, max 8 bytes\r
112         uint8           length;\r
113         // data ptr\r
114         uint8           *sdu;\r
115         // private data for CanIf,just save and use for callback\r
116         PduIdType   swPduHandle;\r
117 } Can_PduType;\r
118 \r
119 \r
120 typedef enum {\r
121         CAN_T_START,\r
122         CAN_T_STOP,\r
123         CAN_T_SLEEP,\r
124         CAN_T_WAKEUP\r
125 } Can_StateTransitionType;\r
126 \r
127 typedef enum {\r
128         CAN_OK,\r
129         CAN_NOT_OK,\r
130         CAN_BUSY\r
131 //      CAN_WAKEUP,             // Removed in 3.0\r
132 } Can_ReturnType;\r
133 \r
134 /* Error from  CAN controller */\r
135 typedef union {\r
136      volatile uint32_t R;\r
137      struct {\r
138          volatile uint32_t:24;\r
139          volatile uint32_t BIT1ERR:1;\r
140          volatile uint32_t BIT0ERR:1;\r
141          volatile uint32_t ACKERR:1;\r
142          volatile uint32_t CRCERR:1;\r
143          volatile uint32_t FRMERR:1;\r
144          volatile uint32_t STFERR:1;\r
145          volatile uint32_t TXWRN:1;\r
146          volatile uint32_t RXWRN:1;\r
147      } B;\r
148  } Can_Arc_ErrorType;\r
149 \r
150 \r
151 \r
152 #if defined(CFG_PPC)\r
153 \r
154 \r
155  typedef enum {\r
156      CAN_ID_TYPE_EXTENDED,\r
157      CAN_ID_TYPE_MIXED,\r
158      CAN_ID_TYPE_STANDARD\r
159  } Can_IdTypeType;\r
160 \r
161  typedef enum {\r
162       CAN_ARC_HANDLE_TYPE_BASIC,\r
163       CAN_ARC_HANDLE_TYPE_FULL\r
164   } Can_Arc_HohType;\r
165 \r
166 \r
167  typedef struct Can_Callback {\r
168      void (*CancelTxConfirmation)( const Can_PduType *);\r
169      void (*RxIndication)( uint8 ,Can_IdType ,uint8 , const uint8 * );\r
170      void (*ControllerBusOff)(uint8);\r
171      void (*TxConfirmation)(PduIdType);\r
172      void (*ControllerWakeup)(uint8);\r
173      void (*Arc_Error)(uint8,Can_Arc_ErrorType);\r
174  } Can_CallbackType;\r
175 \r
176 \r
177 #include "Can_Cfg.h"\r
178 \r
179 \r
180  typedef struct Can_HardwareObjectStruct {\r
181      /* PC/PB, Specifies the type (Full-CAN or Basic-CAN) of a hardware object. */\r
182      Can_Arc_HohType CanHandleType;\r
183 \r
184      /* PC/PB, Specifies whether the IdValue is of type - standard identifier - extended\r
185       * identifier - mixed mode ImplementationType: Can_IdType */\r
186      Can_IdTypeType CanIdType;\r
187 \r
188      /* PC/PB Specifies (together with the filter mask) the identifiers range that passes\r
189       *  the hardware filter. */\r
190      uint32 CanIdValue;\r
191 \r
192      /* PC/PB Holds the handle ID of HRH or HTH. The value of this parameter is unique\r
193       *  in a given CAN Driver, and it should start with 0 and continue without any\r
194       *  gaps. The HRH and HTH Ids are defined under two different name-spaces.\r
195       *  Example: HRH0-0, HRH1-1, HTH0-2, HTH1-3 */\r
196      uint32 CanObjectId;\r
197 \r
198      /* PC/PB, Specifies if the HardwareObject is used as Transmit or as Receive object */\r
199      Can_ObjectTypeType CanObjectType;\r
200 \r
201      /* Reference to CAN Controller to which the HOH is associated to.  */\r
202      uint8 Can_ControllerRef;\r
203 \r
204      /* PC/PB Reference to the filter mask that is used for hardware filtering togerther\r
205       * with the CAN_ID_VALUE */\r
206      Can_FilterMaskType *CanFilterMaskRef;\r
207 \r
208      /* PC,  See CAN_HOH_XX macros */\r
209      uint32 Can_Arc_Flags;\r
210 \r
211      /* PC, Number of mailboxes that is owned by this HOH */\r
212 //     uint8  ArcCanNumMailboxes;\r
213      uint64  ArcMailboxMask;\r
214  } Can_HardwareObjectType;\r
215 \r
216 \r
217 \r
218  typedef struct Can_ControllerConfig {\r
219 \r
220      bool CanControllerActivation;\r
221 \r
222      // Specifies the buadrate of the controller in kbps.\r
223      uint32 CanControllerBaudRate;\r
224 \r
225      //  This parameter provides the controller ID which is unique in a given CAN\r
226      //  Driver. The value for this parameter starts with 0 and continue without any\r
227      //  gaps.\r
228 \r
229      CanControllerIdType CanControllerId;\r
230 \r
231      // Specifies propagation delay in time quantas.\r
232      uint8 CanControllerPropSeg;\r
233 \r
234      // Specifies phase segment 1 in time quantas.\r
235      uint8 CanControllerSeg1;\r
236 \r
237      // Specifies phase segment 2 in time quantas.\r
238      uint8 CanControllerSeg2;\r
239 \r
240      //  Specifies the time quanta for the controller. The calculation of the resulting\r
241      //  prescaler value depending on module clocking and time quanta shall be\r
242      //  done offline Hardware specific.\r
243 //     uint32 CanControllerTimeQuanta;\r
244 \r
245      //  Reference to the CPU clock configuration, which is set in the MCU driver\r
246      //  configuration\r
247      uint32 CanCpuClockRef;\r
248 \r
249      //  This parameter contains a reference to the Wakeup Source for this\r
250      //  controller as defined in the ECU State Manager. Implementation Type:\r
251      //  reference to EcuM_WakeupSourceType\r
252      uint32 CanWakeupSourceRef;\r
253 \r
254      /* Flags, See CAN_CTRL_XX macros */\r
255      uint32 Can_Arc_Flags;\r
256 \r
257      /* Number of FIFO MB in the HOH list */\r
258      uint8 Can_Arc_HohFifoCnt;\r
259 \r
260      /* Total number of HOHs in Can_Arc_Hoh */\r
261      uint8 Can_Arc_HohCnt;\r
262 \r
263      // List of Hoh id's that belong to this controller\r
264      const Can_HardwareObjectType  * const Can_Arc_Hoh;\r
265 \r
266      uint64 Can_Arc_RxMailBoxMask;\r
267      uint64 Can_Arc_TxMailBoxMask;\r
268 \r
269      const uint8 * const Can_Arc_MailBoxToHrh;\r
270 \r
271      PduIdType * const Can_Arc_TxPduHandles;\r
272 \r
273      uint8 Can_Arc_TxMailboxStart;\r
274 \r
275      uint8 Can_Arc_MailboxMax;\r
276 \r
277 #if 1\r
278     //uint32 flags;\r
279 #else\r
280      boolean Can_Arc_Loopback;\r
281 \r
282      // Set this to use the fifo\r
283      boolean Can_Arc_Fifo;\r
284 #endif\r
285 \r
286  } Can_ControllerConfigType;\r
287 \r
288 \r
289 #else\r
290 #include "Can_Cfg.h"\r
291 \r
292 #endif /* defined(PPC) */\r
293 \r
294 // Each controller has 32 hth's, so the division of 32 will give the\r
295 // controller.\r
296 #define GET_CANCONTROLLER(a) (a / HTH_DIVIDER)\r
297 \r
298 \r
299 void Can_Init( const Can_ConfigType *Config );\r
300 void Can_DeInit(void);\r
301 \r
302 #if ( CAN_VERSION_INFO_API == STD_ON )\r
303 #define Can_GetVersionInfo(_vi) STD_GET_VERSION_INFO(_vi,CAN)\r
304 #endif\r
305 \r
306 void Can_InitController( uint8 controller, const Can_ControllerConfigType *config);\r
307 Can_ReturnType Can_SetControllerMode( uint8 Controller, Can_StateTransitionType transition );\r
308 void Can_DisableControllerInterrupts( uint8 controller );\r
309 void Can_EnableControllerInterrupts( uint8 controller );\r
310 // Hth - for Flexcan, the hardware message box number... .We don't care\r
311 \r
312 \r
313 Can_ReturnType Can_Write( Can_Arc_HTHType hth, Can_PduType *pduInfo );\r
314 \r
315 void Can_Cbk_CheckWakeup( uint8 controller );\r
316 void Can_MainFunction_Write( void );\r
317 void Can_MainFunction_Read( void );\r
318 void Can_MainFunction_BusOff( void );\r
319 void Can_MainFunction_Wakeup( void );\r
320 \r
321 void Can_Arc_GetStatistics( uint8 controller, Can_Arc_StatisticsType * stat);\r
322 \r
323 #endif /*CAN_H_*/\r