]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/Port.c
Added multiple units for ADC module and removed hard configed ADC_GROUP0 forever....
[arc.git] / arch / ppc / mpc55xx / 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 \r
17 \r
18 \r
19 \r
20 \r
21 \r
22 \r
23 #include "Std_Types.h"\r
24 #include "Port.h"\r
25 #include "mpc55xx.h"\r
26 #include "Det.h"\r
27 #if defined(USE_DEM)\r
28 #include "Dem.h"\r
29 #endif\r
30 #include "Cpu.h"\r
31 #include <string.h>\r
32 /* SHORT ON HW\r
33  *  Have a bunch of ports:\r
34  *  - PAxx, input only port\r
35  *  - PBxx to PKxx, i/o ports\r
36  *  where xx is 0 to 15\r
37  *  Regs:\r
38  *    PCRx( Pad Config Register ) - register function(in, out, pull up/down, etc )\r
39  *     0  - 15 : Port A\r
40  *     16 - 43 : Port B to J\r
41  *     144-145 : Port K\r
42  *    GPD0( Pin Data Output Registers ) -\r
43  *\r
44  */\r
45 \r
46 typedef enum\r
47 {\r
48     PORT_UNINITIALIZED = 0, PORT_INITIALIZED,\r
49 } Port_StateType;\r
50 \r
51 static Port_StateType _portState = PORT_UNINITIALIZED;\r
52 static const Port_ConfigType * _configPtr = &PortConfigData;\r
53 \r
54 #if (PORT_DEV_ERROR_DETECT)\r
55 #define VALIDATE_PARAM_CONFIG(_ptr,_api) \\r
56         if( (_ptr)==((void *)0) ) { \\r
57                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_PARAM_CONFIG ); \\r
58                 return; \\r
59         }\r
60 \r
61 #define VALIDATE_STATE_INIT(_api)\\r
62         if(PORT_INITIALIZED!=_portState){\\r
63                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_UNINIT ); \\r
64                 return; \\r
65         }\r
66 \r
67 #define VALIDATE_PARAM_PIN(_pin, _api)\\r
68         if(_pin>sizeof(SIU.PCR)){\\r
69                 Det_ReportError(MODULE_ID_PORT, 0, _api, PORT_E_PARAM_PIN ); \\r
70                 return; \\r
71         }\r
72 #else\r
73 #define VALIDATE_PARAM_CONFIG(_ptr,_api)\r
74 #define VALIDATE_STATE_INIT(_api)\r
75 #define VALIDATE_PARAM_PIN(_pin, _api)\r
76 #endif\r
77 \r
78 #if PORT_VERSION_INFO_API == STD_ON\r
79 static Std_VersionInfoType _Port_VersionInfo =\r
80 {\r
81   .vendorID   = (uint16)1,\r
82   .moduleID   = (uint16) MODULE_ID_PORT,\r
83   .instanceID = (uint8)1,\r
84   .sw_major_version = (uint8)PORT_SW_MAJOR_VERSION,\r
85   .sw_minor_version = (uint8)PORT_SW_MINOR_VERSION,\r
86   .sw_patch_version = (uint8)PORT_SW_PATCH_VERSION,\r
87   .ar_major_version = (uint8)PORT_AR_MAJOR_VERSION,\r
88   .ar_minor_version = (uint8)PORT_AR_MINOR_VERSION,\r
89   .ar_patch_version = (uint8)PORT_AR_PATCH_VERSION,\r
90 };\r
91 #endif\r
92 \r
93 void Port_Init(const Port_ConfigType *configType)\r
94 {\r
95   VALIDATE_PARAM_CONFIG(configType, PORT_INIT_ID);\r
96 \r
97 #if defined(CFG_MPC560X)\r
98         vuint16_t i = 0;\r
99         vuint16_t j = 0;\r
100 \r
101         while(i < (configType->padCnt/sizeof(uint16_t)))\r
102     {\r
103                 SIU.PCR[i].R = configType->padConfig[i];\r
104         ++i;\r
105 \r
106 #if defined(CFG_MPC560XB)\r
107         if(32 == i || 33 == i) i=34;\r
108         if(121 == i || 122 == i) i=123;\r
109 #elif defined(CFG_MPC5606S)\r
110         // Out of reset pins PH[0:3](PCR99~PCR102) are available as JTAG pins(TCK,TDI,TDO and TMS respectively)\r
111         if(99 == i || 100 == i || 101 == i || 102 == i) i=103;\r
112 #endif\r
113         }\r
114 \r
115         while(j < configType->outCnt)\r
116         {\r
117         SIU.GPDO[j].B.PDO = configType->outConfig[j];\r
118         ++j;\r
119         }\r
120 \r
121 #else\r
122         // Pointers to the register memory areas\r
123         vuint16_t * padConfig = &(SIU.PCR[0].R);\r
124         vuint8_t * outConfig = &(SIU.GPDO[0].R);\r
125 \r
126         //  vuint8_t * inConfig = &(SIU.GPDI[0].R); // Read only\r
127         // Copy config to register areas\r
128         memcpy((void *)outConfig, configType->outConfig, configType->outCnt);\r
129     memcpy((void *)padConfig, configType->padConfig, configType->padCnt);\r
130         //memcpy((void *)inConfig, configType->inConfig, configType->inCnt);\r
131 #endif\r
132 \r
133         _portState = PORT_INITIALIZED;\r
134         _configPtr = configType;\r
135 \r
136         return;\r
137 }\r
138 \r
139 #if ( PORT_SET_PIN_DIRECTION_API == STD_ON )\r
140 void Port_SetPinDirection( Port_PinType pin, Port_PinDirectionType direction )\r
141 {\r
142   VALIDATE_STATE_INIT(PORT_SET_PIN_DIRECTION_ID);\r
143   VALIDATE_PARAM_PIN(pin, PORT_SET_PIN_DIRECTION_ID);\r
144   unsigned long state;\r
145 \r
146   Irq_Save(state); // Lock interrupts\r
147   if (direction==PORT_PIN_IN)\r
148   {\r
149     SIU.PCR[pin].B.IBE = 1;\r
150     SIU.PCR[pin].B.OBE = 0;\r
151   }\r
152   else\r
153   {\r
154     SIU.PCR[pin].B.IBE = 0;\r
155     SIU.PCR[pin].B.OBE = 1;\r
156   }\r
157   Irq_Restore(state); // Restore interrupts\r
158   return;\r
159 }\r
160 #endif\r
161 \r
162 void Port_RefreshPortDirection( void )\r
163 {\r
164   VALIDATE_STATE_INIT(PORT_REFRESH_PORT_DIRECTION_ID);\r
165   vuint16_t * pcrPtr = &(SIU.PCR[0].R);\r
166   const uint16_t * padCfgPtr = _configPtr->padConfig;\r
167   uint16_t bitMask = PORT_IBE_ENABLE|PORT_OBE_ENABLE;\r
168   int i,j=0;\r
169   unsigned long state;\r
170   for (i=0; i < sizeof(SIU.PCR)/sizeof(SIU.PCR[0]); i++)\r
171   {\r
172         Irq_Save(state); // Lock interrupts\r
173     *pcrPtr = (*pcrPtr & ~bitMask) | (*padCfgPtr & bitMask);\r
174     Irq_Restore(state); // Restore interrupts\r
175     pcrPtr++;\r
176     padCfgPtr++;\r
177 #if defined(CFG_MPC560XB)\r
178     if(32 == i)\r
179     {\r
180         i=34;\r
181         pcrPtr = pcrPtr+2;\r
182         padCfgPtr = padCfgPtr+2;\r
183     }\r
184     if(121 == i)\r
185     {\r
186         i=123;\r
187         pcrPtr = pcrPtr+2;\r
188         padCfgPtr = padCfgPtr+2;\r
189     }\r
190 #elif defined(CFG_MPC5606S)\r
191     if(98 == i)\r
192     {\r
193         i=103;\r
194         pcrPtr = pcrPtr+5;\r
195         padCfgPtr = padCfgPtr+5;\r
196     }\r
197 #endif\r
198 \r
199     j++;\r
200   }\r
201 \r
202   return;\r
203 }\r
204 \r
205 #if PORT_VERSION_INFO_API == STD_ON\r
206 void Port_GetVersionInfo(Std_VersionInfoType* versionInfo)\r
207 {\r
208   VALIDATE_STATE_INIT(PORT_GET_VERSION_INFO_ID);\r
209   memcpy(versionInfo, &_Port_VersionInfo, sizeof(Std_VersionInfoType));\r
210   return;\r
211 }\r
212 #endif\r
213 \r
214 #if (PORT_SET_PIN_MODE_API == STD_ON)\r
215 void Port_SetPinMode(Port_PinType Pin, Port_PinModeType Mode)\r
216 {\r
217   VALIDATE_STATE_INIT(PORT_SET_PIN_MODE_ID);\r
218   VALIDATE_PARAM_PIN(Pin, PORT_SET_PIN_MODE_ID);\r
219   //The pad configuration registers (SIU_PCR) in the SIU allow software control of the static electrical\r
220   //characteristics of external pins. The PCRs can select the multiplexed function of a pin, selection of pullup\r
221   //or pulldown devices, the slew rate of I/O signals, open drain mode for output pins, and hysteresis.\r
222   SIU.PCR[Pin].R = Mode; // Put the selected mode to the PCR register\r
223   return;\r
224 }\r
225 #endif\r