]> rtime.felk.cvut.cz Git - arc.git/blob - arch/hc1x/hcs12d/drivers/Port.c
#ifdef:ed all Det includes
[arc.git] / arch / hc1x / hcs12d / drivers / Port.c
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 #include "Std_Types.h"\r
17 #include "Port.h" /** @req PORT131 */\r
18 #include "regs.h"\r
19 #if defined(USE_DET)\r
20 #include "Det.h"\r
21 #endif\r
22 #include <string.h>\r
23 \r
24 /* SHORT ON HW\r
25  *  Have a bunch of ports:\r
26  *  - PA, PB, PE, PK  is CPU registers (used as address bus if external RAM is used)\r
27  *  - PH, PJ, PM, PP, PS, PT is peripheral registers\r
28  */\r
29 \r
30 typedef enum\r
31 {\r
32     PORT_UNINITIALIZED = 0, PORT_INITIALIZED,\r
33 } Port_StateType;\r
34 \r
35 static Port_StateType _portState = PORT_UNINITIALIZED;\r
36 static const Port_ConfigType * _configPtr = NULL;\r
37 \r
38 /** @req PORT107 */\r
39 #if (PORT_DEV_ERROR_DETECT == STD_ON)\r
40 #define VALIDATE_PARAM_CONFIG(_ptr,_api) \\r
41         if( (_ptr)==((void *)0) ) { \\r
42                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_PARAM_CONFIG ); \\r
43                 return; \\r
44         }\r
45 \r
46 #define VALIDATE_STATE_INIT(_api)\\r
47         if(PORT_INITIALIZED!=_portState){\\r
48                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_UNINIT ); \\r
49                 return; \\r
50         }\r
51 \r
52 #else\r
53 #define VALIDATE_PARAM_CONFIG(_ptr,_api)\r
54 #define VALIDATE_STATE_INIT(_api)\r
55 #define VALIDATE_PARAM_PIN(_api)\r
56 #endif\r
57 \r
58 #if PORT_VERSION_INFO_API == STD_ON\r
59 static Std_VersionInfoType _Port_VersionInfo =\r
60 {\r
61                 .vendorID = (uint16)1,\r
62                 .moduleID = (uint16) MODULE_ID_PORT,\r
63         .instanceID = (uint8)1,\r
64         .sw_major_version = (uint8)PORT_SW_MAJOR_VERSION,\r
65         .sw_minor_version = (uint8)PORT_SW_MINOR_VERSION,\r
66         .sw_patch_version = (uint8)PORT_SW_PATCH_VERSION,\r
67         .ar_major_version = (uint8)PORT_AR_MAJOR_VERSION,\r
68         .ar_minor_version = (uint8)PORT_AR_MINOR_VERSION,\r
69         .ar_patch_version = (uint8)PORT_AR_PATCH_VERSION, };\r
70 #endif\r
71 \r
72 /** @req PORT140 */\r
73 /** @req PORT041 Comment: To reduce flash usage the configuration tool can disable configuration of some ports  */\r
74 /** @req PORT078 See environment i.e Ecu State Manager */\r
75 /** @req PORT042 */\r
76 /** @req PORT113 Number 2 in list is applicable for all pins. */\r
77 /** @req PORT043 Comment: Output value is set before direction */\r
78 /** @req PORT071 See environment i.e Ecu State Manager */\r
79 /** @req PORT002 The _portState varialble is initialised. */\r
80 /** @req PORT003 See environment i.e Ecu State Manager */\r
81 /** @req PORT055 Comment: Output value is set before direction */\r
82 /** @req PORT121 */\r
83 void Port_Init(const Port_ConfigType *configType)\r
84 {\r
85     VALIDATE_PARAM_CONFIG(configType, PORT_INIT_ID); /** @req PORT105 */\r
86 \r
87     // Setup pin drives and pullups\r
88     RDRIV = configType->coreReducedDriveRegister;\r
89     PUCR = configType->corePullUpRegister;\r
90 \r
91     // CAN and SPI routing of pins\r
92     MODDR = configType->modeRoutingRegister;\r
93 \r
94     // Setup ports\r
95 #if ( PORTA_CONFIGURABLE == STD_ON )\r
96     PORTA = configType->portAOutValue;\r
97     DDRA = configType->portADirection;\r
98 #endif\r
99 \r
100 #if ( PORTB_CONFIGURABLE == STD_ON )\r
101     PORTB = configType->portBOutValue;\r
102     DDRB = configType->portBDirection;\r
103 #endif\r
104 \r
105 #if ( PORTE_CONFIGURABLE == STD_ON )\r
106     PORTE = configType->portEOutValue;\r
107     DDRE = configType->portEDirection;\r
108 #endif\r
109 \r
110 #if ( PORTK_CONFIGURABLE == STD_ON )\r
111     PORTK = configType->portKOutValue;\r
112     DDRK = configType->portKDirection;\r
113 #endif\r
114 \r
115 #if ( PORTH_CONFIGURABLE == STD_ON )\r
116     RDRH = configType->portHReducedDriveRegister;\r
117     PERH = configType->portHPullEnableRegister;\r
118     PPSH = configType->portHPullPolarityRegsiter;\r
119     PTH = configType->portHOutValue;\r
120     DDRH = configType->portHDirection;\r
121 #endif\r
122 \r
123 #if ( PORTJ_CONFIGURABLE == STD_ON )\r
124     RDRJ = configType->portJReducedDriveRegister;\r
125     PERJ = configType->portJPullEnableRegister;\r
126     PPSJ = configType->portJPullPolarityRegsiter;\r
127     PTJ = configType->portJOutValue;\r
128     DDRJ = configType->portJDirection;\r
129 #endif\r
130 \r
131 #if ( PORTM_CONFIGURABLE == STD_ON )\r
132     RDRM = configType->portMReducedDriveRegister;\r
133     PERM = configType->portMPullEnableRegister;\r
134     PPSM = configType->portMPullPolarityRegsiter;\r
135     WOMM = configType->portMWiredModeRegsiter;\r
136     PTM = configType->portMOutValue;\r
137     DDRM = configType->portMDirection;\r
138 #endif\r
139 \r
140 #if ( PORTP_CONFIGURABLE == STD_ON )\r
141     RDRP = configType->portPReducedDriveRegister;\r
142     PERP = configType->portPPullEnableRegister;\r
143     PPSP = configType->portPPullPolarityRegsiter;\r
144     PTP = configType->portPOutValue;\r
145     DDRP = configType->portPDirection;\r
146 #endif\r
147 \r
148 #if ( PORTS_CONFIGURABLE == STD_ON )\r
149     RDRS = configType->portSReducedDriveRegister;\r
150     PERS = configType->portSPullEnableRegister;\r
151     PPSS = configType->portSPullPolarityRegsiter;\r
152     WOMS = configType->portSWiredModeRegsiter;\r
153     PTS = configType->portSOutValue;\r
154     DDRS = configType->portSDirection;\r
155 #endif\r
156 \r
157 #if ( PORTT_CONFIGURABLE == STD_ON )\r
158     RDRT = configType->portTReducedDriveRegister;\r
159     PERT = configType->portTPullEnableRegister;\r
160     PPST = configType->portTPullPolarityRegsiter;\r
161     PTT = configType->portTOutValue;\r
162     DDRT = configType->portTDirection;\r
163 #endif\r
164 \r
165     _portState = PORT_INITIALIZED;\r
166     _configPtr = configType;\r
167 \r
168     return;\r
169 }\r
170 \r
171 /** @req PORT141 */\r
172 /** @req PORT063 */\r
173 /** @req PORT054 */\r
174 /** @req PORT086 */\r
175 #if ( PORT_SET_PIN_DIRECTION_API == STD_ON )\r
176 void Port_SetPinDirection( Port_PinType pin, Port_PinDirectionType direction )\r
177 {\r
178     VALIDATE_STATE_INIT(PORT_SET_PIN_DIRECTION_ID);\r
179     uint16_t base;\r
180     uint8_t bitmask;\r
181     uint8_t curValue;\r
182 \r
183     base = pin & PORT_BASEMASK;\r
184     bitmask = 1 << (pin & PORT_BITMASK);\r
185     if (base == PORT_A_BASE)\r
186     {\r
187         curValue = DDRA & ~(bitmask);\r
188         if (direction == PORT_PIN_OUT)\r
189         curValue = curValue | bitmask;\r
190         DDRA = curValue;\r
191     }\r
192     else if (base == PORT_B_BASE)\r
193     {\r
194         curValue = DDRB & ~(bitmask);\r
195         if (direction == PORT_PIN_OUT)\r
196         curValue = curValue | bitmask;\r
197         DDRB = curValue;\r
198     }\r
199     else if (base == PORT_E_BASE)\r
200     {\r
201         curValue = DDRE & ~(bitmask);\r
202         if (direction == PORT_PIN_OUT)\r
203         curValue = curValue | bitmask;\r
204         DDRE = curValue;\r
205     }\r
206     else if (base == PORT_K_BASE)\r
207     {\r
208         curValue = DDRK & ~(bitmask);\r
209         if (direction == PORT_PIN_OUT)\r
210         curValue = curValue | bitmask;\r
211         DDRK = curValue;\r
212     }\r
213     else if (base == PORT_H_BASE)\r
214     {\r
215         curValue = DDRH & ~(bitmask);\r
216         if (direction == PORT_PIN_OUT)\r
217         curValue = curValue | bitmask;\r
218         DDRH = curValue;\r
219     }\r
220     else if (base == PORT_J_BASE)\r
221     {\r
222         curValue = DDRJ & ~(bitmask);\r
223         if (direction == PORT_PIN_OUT)\r
224         curValue = curValue | bitmask;\r
225         DDRJ = curValue;\r
226     }\r
227     else if (base == PORT_M_BASE)\r
228     {\r
229         curValue = DDRM & ~(bitmask);\r
230         if (direction == PORT_PIN_OUT)\r
231         curValue = curValue | bitmask;\r
232         DDRM = curValue;\r
233     }\r
234     else if (base == PORT_P_BASE)\r
235     {\r
236         curValue = DDRP & ~(bitmask);\r
237         if (direction == PORT_PIN_OUT)\r
238         curValue = curValue | bitmask;\r
239         DDRP = curValue;\r
240     }\r
241     else if (base == PORT_S_BASE)\r
242     {\r
243         curValue = DDRS & ~(bitmask);\r
244         if (direction == PORT_PIN_OUT)\r
245         curValue = curValue | bitmask;\r
246         DDRS = curValue;\r
247     }\r
248     else if (base == PORT_T_BASE)\r
249     {\r
250         curValue = DDRT & ~(bitmask);\r
251         if (direction == PORT_PIN_OUT)\r
252         curValue = curValue | bitmask;\r
253         DDRT = curValue;\r
254     }\r
255     else\r
256     {\r
257         Det_ReportError(MODULE_ID_PORT, 0, PORT_SET_PIN_DIRECTION_ID, PORT_E_PARAM_PIN );\r
258     }\r
259 \r
260     return;\r
261 }\r
262 #endif\r
263 \r
264 /** @req PORT066 */\r
265 /** @req PORT142 */\r
266 /** @req PORT060 */\r
267 /** @req PORT061 */\r
268 void Port_RefreshPortDirection(void)\r
269 {\r
270     uint8_t curValue;\r
271     VALIDATE_STATE_INIT(PORT_REFRESH_PORT_DIRECTION_ID);\r
272 \r
273 #if ( PORTA_CONFIGURABLE == STD_ON )\r
274     curValue = DDRA & ~(_configPtr->portAMask);\r
275     curValue |= (_configPtr->portADirection & _configPtr->portAMask);\r
276     DDRA = curValue;\r
277 #endif\r
278 \r
279 #if ( PORTB_CONFIGURABLE == STD_ON )\r
280     curValue = DDRB & ~(_configPtr->portBMask);\r
281     curValue |= (_configPtr->portBDirection & _configPtr->portBMask);\r
282     DDRB = curValue;\r
283 #endif\r
284 \r
285 #if ( PORTE_CONFIGURABLE == STD_ON )\r
286     curValue = DDRE & ~(_configPtr->portEMask);\r
287     curValue |= (_configPtr->portEDirection & _configPtr->portEMask);\r
288     DDRE = curValue;\r
289 #endif\r
290 \r
291 #if ( PORTK_CONFIGURABLE == STD_ON )\r
292     curValue = DDRK & ~(_configPtr->portKMask);\r
293     curValue |= (_configPtr->portKDirection & _configPtr->portKMask);\r
294     DDRK = curValue;\r
295 #endif\r
296 \r
297 #if ( PORTH_CONFIGURABLE == STD_ON )\r
298     curValue = DDRH & ~(_configPtr->portHMask);\r
299     curValue |= (_configPtr->portHDirection & _configPtr->portHMask);\r
300     DDRH = curValue;\r
301 #endif\r
302 \r
303 #if ( PORTJ_CONFIGURABLE == STD_ON )\r
304     curValue = DDRJ & ~(_configPtr->portJMask);\r
305     curValue |= (_configPtr->portJDirection & _configPtr->portJMask);\r
306     DDRJ = curValue;\r
307 #endif\r
308 \r
309 #if ( PORTM_CONFIGURABLE == STD_ON )\r
310     curValue = DDRM & ~(_configPtr->portMMask);\r
311     curValue |= (_configPtr->portMDirection & _configPtr->portMMask);\r
312     DDRM = curValue;\r
313 #endif\r
314 \r
315 #if ( PORTP_CONFIGURABLE == STD_ON )\r
316     curValue = DDRP & ~(_configPtr->portPMask);\r
317     curValue |= (_configPtr->portPDirection & _configPtr->portPMask);\r
318     DDRP =\r
319 #endif\r
320 \r
321 #if ( PORTS_CONFIGURABLE == STD_ON )\r
322     curValue = DDRS & ~(_configPtr->portSMask);\r
323     curValue |= (_configPtr->portSDirection & _configPtr->portSMask);\r
324     DDRS = curValue;\r
325 #endif\r
326 \r
327 #if ( PORTT_CONFIGURABLE == STD_ON )\r
328     curValue = DDRT & ~(_configPtr->portTMask);\r
329     curValue |= (_configPtr->portTDirection & _configPtr->portTMask);\r
330     DDRT = curValue;\r
331 #endif\r
332 \r
333     return;\r
334 }\r
335 \r
336 /** req PORT143 */\r
337 /** req PORT102 */\r
338 /** req PORT103 */\r
339 #if (PORT_VERSION_INFO_API == STD_ON)\r
340 void Port_GetVersionInfo(Std_VersionInfoType* versionInfo)\r
341 {\r
342     VALIDATE_STATE_INIT(PORT_GET_VERSION_INFO_ID);\r
343     memcpy(versionInfo, &_Port_VersionInfo, sizeof(Std_VersionInfoType));\r
344     return;\r
345 }\r
346 #endif\r
347 \r
348 /** req PORT145 */\r
349 /** req PORT125 */\r
350 /** req PORT128 */\r
351 #if (PORT_SET_PIN_MODE_API == STD_ON)\r
352 void Port_SetPinMode(Port_PinType Pin, Port_PinModeType Mode)\r
353 {\r
354     VALIDATE_STATE_INIT(PORT_SET_PIN_MODE_ID);\r
355 \r
356     // Mode of pins not changeable on this CPU\r
357 #if (PORT_DEV_ERROR_DETECT == STD_ON)\r
358     Det_ReportError(MODULE_ID_PORT, 0, PORT_SET_PIN_MODE_ID, PORT_E_MODE_UNCHANGEABLE );\r
359 #endif\r
360 \r
361     return;\r
362 }\r
363 #endif\r