1 /* -------------------------------- Arctic Core ------------------------------
\r
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\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
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
14 * -------------------------------- Arctic Core ------------------------------*/
\r
16 #include "Std_Types.h"
\r
17 #include "Port.h" /** @req PORT131 */
\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
30 PORT_UNINITIALIZED = 0, PORT_INITIALIZED,
\r
33 static Port_StateType _portState = PORT_UNINITIALIZED;
\r
34 static const Port_ConfigType * _configPtr = NULL;
\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
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
51 #define VALIDATE_PARAM_CONFIG(_ptr,_api)
\r
52 #define VALIDATE_STATE_INIT(_api)
\r
53 #define VALIDATE_PARAM_PIN(_api)
\r
56 #if PORT_VERSION_INFO_API == STD_ON
\r
57 static Std_VersionInfoType _Port_VersionInfo =
\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
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
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
81 void Port_Init(const Port_ConfigType *configType)
\r
83 VALIDATE_PARAM_CONFIG(configType, PORT_INIT_ID); /** @req PORT105 */
\r
85 // Setup pin drives and pullups
\r
86 RDRIV = configType->coreReducedDriveRegister;
\r
87 PUCR = configType->corePullUpRegister;
\r
89 // CAN and SPI routing of pins
\r
90 MODDR = configType->modeRoutingRegister;
\r
93 #if ( PORTA_CONFIGURABLE == STD_ON )
\r
94 PORTA = configType->portAOutValue;
\r
95 DDRA = configType->portADirection;
\r
98 #if ( PORTB_CONFIGURABLE == STD_ON )
\r
99 PORTB = configType->portBOutValue;
\r
100 DDRB = configType->portBDirection;
\r
103 #if ( PORTE_CONFIGURABLE == STD_ON )
\r
104 PORTE = configType->portEOutValue;
\r
105 DDRE = configType->portEDirection;
\r
108 #if ( PORTK_CONFIGURABLE == STD_ON )
\r
109 PORTK = configType->portKOutValue;
\r
110 DDRK = configType->portKDirection;
\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
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
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
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
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
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
163 _portState = PORT_INITIALIZED;
\r
164 _configPtr = configType;
\r
165 #if (PORT_DEV_ERROR_DETECT == STD_ON)
\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
178 VALIDATE_STATE_INIT(PORT_SET_PIN_DIRECTION_ID);
\r
183 base = pin & PORT_BASEMASK;
\r
184 bitmask = 1 << (pin & PORT_BITMASK);
\r
185 if (base == PORT_A_BASE)
\r
187 curValue = DDRA & ~(bitmask);
\r
188 if (direction == PORT_PIN_OUT)
\r
189 curValue = curValue | bitmask;
\r
192 else if (base == PORT_B_BASE)
\r
194 curValue = DDRB & ~(bitmask);
\r
195 if (direction == PORT_PIN_OUT)
\r
196 curValue = curValue | bitmask;
\r
199 else if (base == PORT_E_BASE)
\r
201 curValue = DDRE & ~(bitmask);
\r
202 if (direction == PORT_PIN_OUT)
\r
203 curValue = curValue | bitmask;
\r
206 else if (base == PORT_K_BASE)
\r
208 curValue = DDRK & ~(bitmask);
\r
209 if (direction == PORT_PIN_OUT)
\r
210 curValue = curValue | bitmask;
\r
213 else if (base == PORT_H_BASE)
\r
215 curValue = DDRH & ~(bitmask);
\r
216 if (direction == PORT_PIN_OUT)
\r
217 curValue = curValue | bitmask;
\r
220 else if (base == PORT_J_BASE)
\r
222 curValue = DDRJ & ~(bitmask);
\r
223 if (direction == PORT_PIN_OUT)
\r
224 curValue = curValue | bitmask;
\r
227 else if (base == PORT_M_BASE)
\r
229 curValue = DDRM & ~(bitmask);
\r
230 if (direction == PORT_PIN_OUT)
\r
231 curValue = curValue | bitmask;
\r
234 else if (base == PORT_P_BASE)
\r
236 curValue = DDRP & ~(bitmask);
\r
237 if (direction == PORT_PIN_OUT)
\r
238 curValue = curValue | bitmask;
\r
241 else if (base == PORT_S_BASE)
\r
243 curValue = DDRS & ~(bitmask);
\r
244 if (direction == PORT_PIN_OUT)
\r
245 curValue = curValue | bitmask;
\r
248 else if (base == PORT_T_BASE)
\r
250 curValue = DDRT & ~(bitmask);
\r
251 if (direction == PORT_PIN_OUT)
\r
252 curValue = curValue | bitmask;
\r
257 Det_ReportError(MODULE_ID_PORT, 0, PORT_SET_PIN_DIRECTION_ID, PORT_E_PARAM_PIN );
\r
264 /** @req PORT066 */
\r
265 /** @req PORT142 */
\r
266 /** @req PORT060 */
\r
267 /** @req PORT061 */
\r
268 void Port_RefreshPortDirection(void)
\r
271 VALIDATE_STATE_INIT(PORT_REFRESH_PORT_DIRECTION_ID);
\r
273 #if ( PORTA_CONFIGURABLE == STD_ON )
\r
274 curValue = DDRA & ~(_configPtr->portAMask);
\r
275 curValue |= (_configPtr->portADirection & _configPtr->portAMask);
\r
279 #if ( PORTB_CONFIGURABLE == STD_ON )
\r
280 curValue = DDRB & ~(_configPtr->portBMask);
\r
281 curValue |= (_configPtr->portBDirection & _configPtr->portBMask);
\r
285 #if ( PORTE_CONFIGURABLE == STD_ON )
\r
286 curValue = DDRE & ~(_configPtr->portEMask);
\r
287 curValue |= (_configPtr->portEDirection & _configPtr->portEMask);
\r
291 #if ( PORTK_CONFIGURABLE == STD_ON )
\r
292 curValue = DDRK & ~(_configPtr->portKMask);
\r
293 curValue |= (_configPtr->portKDirection & _configPtr->portKMask);
\r
297 #if ( PORTH_CONFIGURABLE == STD_ON )
\r
298 curValue = DDRH & ~(_configPtr->portHMask);
\r
299 curValue |= (_configPtr->portHDirection & _configPtr->portHMask);
\r
303 #if ( PORTJ_CONFIGURABLE == STD_ON )
\r
304 curValue = DDRJ & ~(_configPtr->portJMask);
\r
305 curValue |= (_configPtr->portJDirection & _configPtr->portJMask);
\r
309 #if ( PORTM_CONFIGURABLE == STD_ON )
\r
310 curValue = DDRM & ~(_configPtr->portMMask);
\r
311 curValue |= (_configPtr->portMDirection & _configPtr->portMMask);
\r
315 #if ( PORTP_CONFIGURABLE == STD_ON )
\r
316 curValue = DDRP & ~(_configPtr->portPMask);
\r
317 curValue |= (_configPtr->portPDirection & _configPtr->portPMask);
\r
321 #if ( PORTS_CONFIGURABLE == STD_ON )
\r
322 curValue = DDRS & ~(_configPtr->portSMask);
\r
323 curValue |= (_configPtr->portSDirection & _configPtr->portSMask);
\r
327 #if ( PORTT_CONFIGURABLE == STD_ON )
\r
328 curValue = DDRT & ~(_configPtr->portTMask);
\r
329 curValue |= (_configPtr->portTDirection & _configPtr->portTMask);
\r
333 #if (PORT_DEV_ERROR_DETECT == STD_ON)
\r
342 #if (PORT_VERSION_INFO_API == STD_ON)
\r
343 void Port_GetVersionInfo(Std_VersionInfoType* versionInfo)
\r
345 VALIDATE_STATE_INIT(PORT_GET_VERSION_INFO_ID);
\r
346 memcpy(versionInfo, &_Port_VersionInfo, sizeof(Std_VersionInfoType));
\r
354 #if (PORT_SET_PIN_MODE_API == STD_ON)
\r
355 void Port_SetPinMode(Port_PinType Pin, Port_PinModeType Mode)
\r
357 VALIDATE_STATE_INIT(PORT_SET_PIN_MODE_ID);
\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