-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPort.c
More file actions
225 lines (175 loc) · 5.75 KB
/
Port.c
File metadata and controls
225 lines (175 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/******************************************************************************
*
* Module: Port
*
* File Name: Port.c
*
* Description: Source file for Port device driver APIs implementation,
* implementation is based on AUTOSAR specfications.
*
* Author: Ahmed Wasfy
*
* Date: May 26, 2019
******************************************************************************/
#include "Port.h"
#include "..\DET\Det.h"
#if (PORT_DEV_ERROR_DETECT == STD_ON)
#include "Det.h"
/* AUTOSAR version checking */
#if ((DET_AR_MAJOR_VERSION != PORT_AR_RELEASE_MAJOR_VERSION)\
|| (DET_AR_MINOR_VERSION != PORT_AR_RELEASE_MINOR_VERSION)\
|| (DET_AR_PATCH_VERSION != PORT_AR_RELEASE_PATCH_VERSION))
#error "The AR version of Det.h does not match the expected version"
#endif /* AUTOSAR version checking */
/* SW module version checking */
#if ((DET_SW_MAJOR_VERSION != PORT_SW_MAJOR_VERSION)\
|| (DET_SW_MINOR_VERSION != PORT_SW_MINOR_VERSION)\
|| (DET_SW_PATCH_VERSION != PORT_SW_PATCH_VERSION))
#error "The AR version of Det.h does not match the expected version"
#endif /* SW module version checking */
#endif /* (DET_DEV_ERROR_DETECT == STD_ON) */
#if (PORT_SET_PIN_DIRECTION_API == STD_ON) || defined(__DOXYGEN__)
extern FUNC(void, PORT_CODE) Port_SetPinDirection
(
VAR(Port_PinType, PORT_VAR) Pin,
VAR(Port_PinDirectionType, PORT_VAR) Direction
){
uint32 B[6] = {BASE_A,BASE_B,BASE_C,BASE_D,BASE_E,BASE_F};
uint32 Base;
if (Pin<PortF_PortPin0){
Base=B[Pin/8];
Pin%=8;
}
else {
Base=BASE_F;
Pin=(Pin-PortF_PortPin0)%5;
}
if(Direction == PORT_PIN_OUT){
SET_BIT(Base + GPIODIR_offset,Pin);
}else{
CLEAR_BIT(Base + GPIODIR_offset,Pin);
}
}
#endif
extern FUNC(void, PORT_CODE) Port_SetPinMode
(
VAR(Port_PinType, PORT_VAR) Pin,
VAR(Port_PinModeType, PORT_VAR) Mode
){
uint32 B[6] = {BASE_A,BASE_B,BASE_C,BASE_D,BASE_E,BASE_F};
uint32 Base;
if (Pin<PortF_PortPin0){
Base=B[Pin/8];
Pin%=8;
}
else {
Base=BASE_F;
Pin=(Pin-PortF_PortPin0)%5;
}
if(Mode == PORT_PIN_MODE_ADC){
SET_BIT(Base + GPIOAMSEL_offset,Pin);
}else if(Mode == PORT_PIN_MODE_ADC){
SET_BIT(Base + GPIOADCCTL_offset,Pin);
}else if(Mode == PORT_PIN_MODE_DIO){
CLEAR_BIT(Base + GPIOADCCTL_offset,Pin);
CLEAR_BIT(Base + GPIOAMSEL_offset,Pin);
SET_BIT(Base + GPIODEN_offset,Pin);
}else{
SET_BIT(Base + GPIOAFSEL_offset,Pin);
SET_BIT(Base + GPIODEN_offset,Pin);
WRITE_REG_32BIT(Base + GPIOPCTL_offset,READ_REG_32BIT(Base + GPIOPCTL_offset) | ( (uint32) Mode << (Pin << 2) ) );
}
}
extern FUNC(void, PORT_CODE) Port_Init
(
P2CONST(Port_ConfigType,AUTOMATIC,PORT_APPL_DATA) ConfigPtr
){
uint32 B[6] = {BASE_A,BASE_B,BASE_C,BASE_D,BASE_E,BASE_F};
uint32 Base,pin;
// Port_PinDirectionType Dir;
// Port_PinModeType Mode;
uint8 i=0;
for(i=0;i<PORT_NUMBER_OF_PORT_PINS;i++){
pin=ConfigPtr[i].PortPinId;
if (pin<PortF_PortPin0){
Base=B[pin/8];
pin%=8;
}
else {
Base=BASE_F;
pin=(pin-PortF_PortPin0)%5;
}
if (ConfigPtr[i].PortPinInitialMode==PORT_PIN_NOT_ACTIVE)continue;
WRITE_REG_32BIT((Base+GPIOLOCK_offset),GPIO_PORTC_LOCK_R);
//(*((volatile uint32 *)(Base+GPIOLOCK_offset)))=GPIO_PORTC_LOCK_R;
SET_BIT((Base + GPIOCR_offset),pin);
if (ConfigPtr[i].PortPinInitialMode==PORT_PIN_MODE_DIO) {
READ_REG_32BIT(Base + GPIOPCTL_offset)&=(~(3<<((pin << 2)))) ;
CLEAR_BIT(Base + GPIOAMSEL_offset,pin);
CLEAR_BIT(Base + GPIOPCTL_offset,pin);
CLEAR_BIT(Base + GPIOAFSEL_offset,pin);
//WRITE_REG_32BIT(Base + GPIOPCTL,READ_REG_32BIT(Base + GPIOPCTL) | ( (uint32) Mode << (Pin << 2) ) );
//WRITE_REG_32BIT(Base + GPIOPCTL,READ_REG_32BIT(Base + GPIOPCTL) | ( (uint32) Mode << (Pin << 2) ) );
SET_BIT(Base + GPIOPUR_offset,pin);
SET_BIT(Base + GPIODEN_offset,pin);
if(ConfigPtr[i].PortPinDirection==PORT_PIN_OUT){
SET_BIT(Base + GPIODIR_offset,pin);
}
else{
CLEAR_BIT(Base + GPIODIR_offset,pin);
}
}
else if (ConfigPtr[i].PortPinInitialMode==PORT_PIN_MODE_ADC){
READ_REG_32BIT(Base + GPIOPCTL_offset)&=(~(3<<((pin << 2)))) ;
SET_BIT((Base + GPIOAMSEL_offset),pin);
if(ConfigPtr[i].PortPinDirection==PORT_PIN_OUT){
SET_BIT(Base + GPIODIR_offset,pin);
}
else{
CLEAR_BIT(Base + GPIODIR_offset,pin);
}
}
else{
SET_BIT(Base + GPIOAFSEL_offset,pin);
SET_BIT(Base + GPIODEN_offset,pin);
//volatile int mode = ( (uint32) ConfigPtr[i].PortPinInitialMode << (pin << 2) );
//volatile int reading=READ_REG_32BIT(Base + GPIOPCTL_offset)&=(~(3<<((pin << 2)))) ;
//int x=reading;
WRITE_REG_32BIT((Base + GPIOPCTL_offset),( (READ_REG_32BIT(Base + GPIOPCTL_offset)&=(~(3<<((pin << 2))))) | ( (uint32) ConfigPtr[i].PortPinInitialMode << (pin << 2) ) ));
//int x=6;
if(ConfigPtr[i].PortPinDirection==PORT_PIN_OUT){
SET_BIT(Base + GPIODIR_offset,pin);
}
else{
CLEAR_BIT(Base + GPIODIR_offset,pin);
}
}
}
}
void Port_RefreshPortDirection(void){}/*{
}*/
/*{
uint32 B[6] = {BASE_A,BASE_B,BASE_C,BASE_D,BASE_E,BASE_F};
uint32 i = 0,j,Pin,Base;
for(j = 0;j < PORT_NUMBER_OF_PORT_PINS;j++){
pin=PortConfig[i].PortPin;
if (pin<PortF_PortPin0){
Base=B[pin/8];
pin%=8;
}
else {
Base=BASE_F;
pin=(pin-PortF_PortPin0)%5;
}
for(Pin = 0;Pin < PORT_PIN_NUMBER;Pin++){
if(PortConfig.PortPin[i].PortPinInitialMode == PORT_PIN_NOT_ACTIVE){
}else if(PortConfig.PortPin[i].PortPinDirection == PORT_PIN_OUT){
SET_BIT(Base + GPIODIR,Pin);
}else{
CLEAR_BIT(Base + GPIODIR,Pin);
}
i++;
}
}
}
*/