]> rtime.felk.cvut.cz Git - arc.git/blob - arch/hc1x/hcs12d/drivers/Port.c
Starting 'hcs12x' branch
[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 #include "Det.h"\r
20 #include <string.h>\r
21 \r
22 /* SHORT ON HW\r
23  *  Have a bunch of ports:\r
24  *  - PA, PB, PE, PK  is CPU registers (used as address bus if external RAM is used)\r
25  *  - PH, PJ, PM, PP, PS, PT is peripheral registers\r
26  */\r
27 \r
28 typedef enum\r
29 {\r
30     PORT_UNINITIALIZED = 0, PORT_INITIALIZED,\r
31 } Port_StateType;\r
32 \r
33 static Port_StateType _portState = PORT_UNINITIALIZED;\r
34 static const Port_ConfigType * _configPtr = NULL;\r
35 \r
36 /** @req PORT107 */\r
37 #if (PORT_DEV_ERROR_DETECT == STD_ON)\r
38 #define VALIDATE_PARAM_CONFIG(_ptr,_api) \\r
39         if( (_ptr)==((void *)0) ) { \\r
40                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_PARAM_CONFIG ); \\r
41                 goto cleanup; \\r
42         }\r
43 \r
44 #define VALIDATE_STATE_INIT(_api)\\r
45         if(PORT_INITIALIZED!=_portState){\\r
46                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_UNINIT ); \\r
47                 goto cleanup; \\r
48         }\r
49 \r
50 #else\r
51 #define VALIDATE_PARAM_CONFIG(_ptr,_api)\r
52 #define VALIDATE_STATE_INIT(_api)\r
53 #define VALIDATE_PARAM_PIN(_api)\r
54 #endif\r
55 \r
56 #if PORT_VERSION_INFO_API == STD_ON\r
57 static Std_VersionInfoType _Port_VersionInfo =\r
58 {\r
59                 .vendorID = (uint16)1,\r
60                 .moduleID = (uint16) MODULE_ID_PORT,\r
61         .instanceID = (uint8)1,\r
62         .sw_major_version = (uint8)PORT_SW_MAJOR_VERSION,\r
63         .sw_minor_version = (uint8)PORT_SW_MINOR_VERSION,\r
64         .sw_patch_version = (uint8)PORT_SW_PATCH_VERSION,\r
65         .ar_major_version = (uint8)PORT_AR_MAJOR_VERSION,\r
66         .ar_minor_version = (uint8)PORT_AR_MINOR_VERSION,\r
67         .ar_patch_version = (uint8)PORT_AR_PATCH_VERSION, };\r
68 #endif\r
69 \r
70 /** @req PORT140 */\r
71 /** @req PORT041 Comment: To reduce flash usage the configuration tool can disable configuration of some ports  */\r
72 /** @req PORT078 See environment i.e Ecu State Manager */\r
73 /** @req PORT042 */\r
74 /** @req PORT113 Number 2 in list is applicable for all pins. */\r
75 /** @req PORT043 Comment: Output value is set before direction */\r
76 /** @req PORT071 See environment i.e Ecu State Manager */\r
77 /** @req PORT002 The _portState varialble is initialised. */\r
78 /** @req PORT003 See environment i.e Ecu State Manager */\r
79 /** @req PORT055 Comment: Output value is set before direction */\r
80 /** @req PORT121 */\r
81 void Port_Init(const Port_ConfigType *configType)\r
82 {\r
83     VALIDATE_PARAM_CONFIG(configType, PORT_INIT_ID); /** @req PORT105 */\r
84 \r
85     // Setup pin drives and pullups\r
86     RDRIV = configType->coreReducedDriveRegister;\r
87     PUCR = configType->corePullUpRegister;\r
88 \r
89     // CAN and SPI routing of pins\r
90     MODDR = configType->modeRoutingRegister;\r
91 \r
92     // Setup ports\r
93 #if ( PORTA_CONFIGURABLE == STD_ON )\r
94     PORTA = configType->portAOutValue;\r
95     DDRA = configType->portADirection;\r
96 #endif\r
97 \r
98 #if ( PORTB_CONFIGURABLE == STD_ON )\r
99     PORTB = configType->portBOutValue;\r
100     DDRB = configType->portBDirection;\r
101 #endif\r
102 \r
103 #if ( PORTE_CONFIGURABLE == STD_ON )\r
104     PORTE = configType->portEOutValue;\r
105     DDRE = configType->portEDirection;\r
106 #endif\r
107 \r
108 #if ( PORTK_CONFIGURABLE == STD_ON )\r
109     PORTK = configType->portKOutValue;\r
110     DDRK = configType->portKDirection;\r
111 #endif\r
112 \r
113 #if ( PORTH_CONFIGURABLE == STD_ON )\r
114     RDRH = configType->portHReducedDriveRegister;\r
115     PERH = configType->portHPullEnableRegister;\r
116     PPSH = configType->portHPullPolarityRegsiter;\r
117     PTH = configType->portHOutValue;\r
118     DDRH = configType->portHDirection;\r
119 #endif\r
120 \r
121 #if ( PORTJ_CONFIGURABLE == STD_ON )\r
122     RDRJ = configType->portJReducedDriveRegister;\r
123     PERJ = configType->portJPullEnableRegister;\r
124     PPSJ = configType->portJPullPolarityRegsiter;\r
125     PTJ = configType->portJOutValue;\r
126     DDRJ = configType->portJDirection;\r
127 #endif\r
128 \r
129 #if ( PORTM_CONFIGURABLE == STD_ON )\r
130     RDRM = configType->portMReducedDriveRegister;\r
131     PERM = configType->portMPullEnableRegister;\r
132     PPSM = configType->portMPullPolarityRegsiter;\r
133     WOMM = configType->portMWiredModeRegsiter;\r
134     PTM = configType->portMOutValue;\r
135     DDRM = configType->portMDirection;\r
136 #endif\r
137 \r
138 #if ( PORTP_CONFIGURABLE == STD_ON )\r
139     RDRP = configType->portPReducedDriveRegister;\r
140     PERP = configType->portPPullEnableRegister;\r
141     PPSP = configType->portPPullPolarityRegsiter;\r
142     PTP = configType->portPOutValue;\r
143     DDRP = configType->portPDirection;\r
144 #endif\r
145 \r
146 #if ( PORTS_CONFIGURABLE == STD_ON )\r
147     RDRS = configType->portSReducedDriveRegister;\r
148     PERS = configType->portSPullEnableRegister;\r
149     PPSS = configType->portSPullPolarityRegsiter;\r
150     WOMS = configType->portSWiredModeRegsiter;\r
151     PTS = configType->portSOutValue;\r
152     DDRS = configType->portSDirection;\r
153 #endif\r
154 \r
155 #if ( PORTT_CONFIGURABLE == STD_ON )\r
156     RDRT = configType->portTReducedDriveRegister;\r
157     PERT = configType->portTPullEnableRegister;\r
158     PPST = configType->portTPullPolarityRegsiter;\r
159     PTT = configType->portTOutValue;\r
160     DDRT = configType->portTDirection;\r
161 #endif\r
162 \r
163     _portState = PORT_INITIALIZED;\r
164     _configPtr = configType;\r
165 #if (PORT_DEV_ERROR_DETECT == STD_ON)\r
166     cleanup:\r
167 #endif\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     cleanup: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 #if (PORT_DEV_ERROR_DETECT == STD_ON)\r
334     cleanup:\r
335 #endif\r
336     return;\r
337 }\r
338 \r
339 /** req PORT143 */\r
340 /** req PORT102 */\r
341 /** req PORT103 */\r
342 #if (PORT_VERSION_INFO_API == STD_ON)\r
343 void Port_GetVersionInfo(Std_VersionInfoType* versionInfo)\r
344 {\r
345     VALIDATE_STATE_INIT(PORT_GET_VERSION_INFO_ID);\r
346     memcpy(versionInfo, &_Port_VersionInfo, sizeof(Std_VersionInfoType));\r
347     cleanup: return;\r
348 }\r
349 #endif\r
350 \r
351 /** req PORT145 */\r
352 /** req PORT125 */\r
353 /** req PORT128 */\r
354 #if (PORT_SET_PIN_MODE_API == STD_ON)\r
355 void Port_SetPinMode(Port_PinType Pin, Port_PinModeType Mode)\r
356 {\r
357     VALIDATE_STATE_INIT(PORT_SET_PIN_MODE_ID);\r
358 \r
359     // Mode of pins not changeable on this CPU\r
360 #if (PORT_DEV_ERROR_DETECT == STD_ON)\r
361     Det_ReportError(MODULE_ID_PORT, 0, PORT_SET_PIN_MODE_ID, PORT_E_MODE_UNCHANGEABLE );\r
362 #endif\r
363 \r
364     cleanup: return;\r
365 }\r
366 #endif\r