-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dio.c
377 lines (322 loc) · 9.95 KB
/
Dio.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/******************************************************************************
*
* Module: Dio
*
* File Name: Dio.c
*
* Description: Source file for Dio Module.
*
* Author: Abdelrahman Bakr
******************************************************************************/
#include "Dio.h"
#if (DIO_DEV_ERROR_DETECT == STD_ON)
#include "Det.h"
/* AUTOSAR Version checking between Det and Dio Modules */
#if ((DET_AR_MAJOR_VERSION != DIO_AR_RELEASE_MAJOR_VERSION)\
|| (DET_AR_MINOR_VERSION != DIO_AR_RELEASE_MINOR_VERSION)\
|| (DET_AR_PATCH_VERSION != DIO_AR_RELEASE_PATCH_VERSION))
#error "The AR version of Det.h does not match the expected version"
#endif
#endif
STATIC uint8 Dio_Status = DIO_NOT_INITIALIZED;
STATIC Dio_ConfigChannel * Dio_PortChannels = NULL_PTR;
/************************************************************************************
* Service Name: Dio_Init
* Service ID[hex]: 0x10
* Sync/Async: Synchronous
* Reentrancy: Non reentrant
* Parameters (in): ConfigPtr - Pointer to post-build configuration data
* Parameters (inout): None
* Parameters (out): None
* Return value: None
* Description: Function to Initialize the Dio module.
************************************************************************************/
void Dio_Init(const Dio_ConfigType * ConfigPtr)
{
#if (DIO_DEV_ERROR_DETECT == STD_ON)
/* check if the input configuration pointer is not a NULL_PTR */
if (NULL_PTR == ConfigPtr)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID, DIO_INIT_SID,
DIO_E_PARAM_CONFIG);
}
else
#endif
{
/*
* Set the module state to initialized and point to the PB configuration structure using a global pointer.
* This global pointer is global to be used by other functions to read the PB configuration structures
*/
Dio_Status = DIO_INITIALIZED;
Dio_PortChannels = ConfigPtr->Channels ; /* address of the first Channels structure --> Channels[0] */
}
}
/************************************************************************************
* Service Name: Dio_WriteChannel
* Service ID[hex]: 0x01
* Sync/Async: Synchronous
* Reentrancy: Reentrant
* Parameters (in): ChannelId - ID of DIO channel.
* Level - Value to be written.
* Parameters (inout): None
* Parameters (out): None
* Return value: None
* Description: Function to set a level of a channel.
************************************************************************************/
void Dio_WriteChannel(Dio_ChannelType ChannelId, Dio_LevelType Level){
volatile Dio_PortLevelType * ODR_Ptr = NULL_PTR;
boolean error = FALSE;
#if (DIO_DEV_ERROR_DETECT == STD_ON)
/* Check if the Driver is initialized before using this function */
if (DIO_NOT_INITIALIZED == Dio_Status)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID, DIO_WRITE_CHANNEL_SID, DIO_E_UNINIT);
error = TRUE;
}
else
{
/* No Action Required */
}
/* Check if the used channel is within the valid range */
if (DIO_CONFIGURED_CHANNLES <= ChannelId)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID,DIO_WRITE_CHANNEL_SID, DIO_E_PARAM_INVALID_CHANNEL_ID);
error = TRUE;
}
else
{
/* No Action Required */
}
#endif
/* In-case there are no errors */
if(FALSE == error)
{
/* Point to the correct PORT ODR register according to the Port Id stored in the Port_Num member */
switch(Dio_PortChannels[ChannelId].Port_Num)
{
case 0: ODR_Ptr = &(GPIOA->ODR);
break;
case 1: ODR_Ptr = &(GPIOB->ODR);
break;
case 2: ODR_Ptr = &(GPIOC->ODR);
break;
case 3: ODR_Ptr = &(GPIOD->ODR);
break;
case 4: ODR_Ptr = &(GPIOE->ODR);
break;
case 5: ODR_Ptr = &(GPIOF->ODR);
break;
case 6: ODR_Ptr = &(GPIOG->ODR);
break;
case 7: ODR_Ptr = &(GPIOH->ODR);
break;
}
if(Level == STD_HIGH)
{
/* Write Logic High */
SET_BIT(*ODR_Ptr,Dio_PortChannels[ChannelId].Ch_Num);
}
else if(Level == STD_LOW)
{
/* Write Logic Low */
CLEAR_BIT(*ODR_Ptr,Dio_PortChannels[ChannelId].Ch_Num);
}
}
else{
// do nothing
}
}
/************************************************************************************
* Service Name: Dio_ReadChannel
* Service ID[hex]: 0x00
* Sync/Async: Synchronous
* Reentrancy: Reentrant
* Parameters (in): ChannelId - ID of DIO channel.
* Parameters (inout): None
* Parameters (out): None
* Return value: Dio_LevelType
* Description: Function to return the value of the specified DIO channel.
************************************************************************************/
Dio_LevelType Dio_ReadChannel(Dio_ChannelType ChannelId){
volatile Dio_PortLevelType * IDR_Ptr = NULL_PTR;
Dio_LevelType output = STD_LOW;
boolean error = FALSE;
#if (DIO_DEV_ERROR_DETECT == STD_ON)
/* Check if the Driver is initialized before using this function */
if (DIO_NOT_INITIALIZED == Dio_Status)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID, DIO_READ_CHANNEL_SID, DIO_E_UNINIT);
error = TRUE;
}
else
{
/* No Action Required */
}
/* Check if the used channel is within the valid range */
if (DIO_CONFIGURED_CHANNLES <= ChannelId)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID,DIO_READ_CHANNEL_SID, DIO_E_PARAM_INVALID_CHANNEL_ID);
error = TRUE;
}
else
{
/* No Action Required */
}
#endif
/* In-case there are no errors */
if(FALSE == error)
{
/* Point to the correct Port IDR register according to the Port Id stored in the Port_Num member */
switch(Dio_PortChannels[ChannelId].Port_Num)
{
case 0: IDR_Ptr = &(GPIOA->IDR);
break;
case 1: IDR_Ptr = &(GPIOB->IDR);
break;
case 2: IDR_Ptr = &(GPIOC->IDR);
break;
case 3: IDR_Ptr = &(GPIOD->IDR);
break;
case 4: IDR_Ptr = &(GPIOE->IDR);
break;
case 5: IDR_Ptr = &(GPIOF->IDR);
break;
case 6: IDR_Ptr = &(GPIOG->IDR);
break;
case 7: IDR_Ptr = &(GPIOH->IDR);
break;
}
/* Read the required channel */
if(BIT_IS_SET(*IDR_Ptr,Dio_PortChannels[ChannelId].Ch_Num))
{
output = STD_HIGH;
}
else
{
output = STD_LOW;
}
return output;
}
else
{
/* No Action Required */
}
}
/************************************************************************************
* Service Name: Dio_GetVersionInfo
* Service ID[hex]: 0x12
* Sync/Async: Synchronous
* Reentrancy: Reentrant
* Parameters (in): None
* Parameters (inout): None
* Parameters (out): VersionInfo - Pointer to where to store the version information of this module.
* Return value: None
* Description: Function to get the version information of this module.
************************************************************************************/
#if (DIO_VERSION_INFO_API == STD_ON)
void Dio_GetVersionInfo(Std_VersionInfoType *versioninfo)
{
#if (DIO_DEV_ERROR_DETECT == STD_ON)
/* Check if input pointer is not Null pointer */
if(NULL_PTR == versioninfo)
{
/* Report to DET */
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID,
DIO_GET_VERSION_INFO_SID, DIO_E_PARAM_POINTER);
}
else
#endif /* (DIO_DEV_ERROR_DETECT == STD_ON) */
{
/* Copy the vendor Id */
versioninfo->vendorID = (uint16)DIO_VENDOR_ID;
/* Copy the module Id */
versioninfo->moduleID = (uint16)DIO_MODULE_ID;
/* Copy Software Major Version */
versioninfo->sw_major_version = (uint8)DIO_SW_MAJOR_VERSION;
/* Copy Software Minor Version */
versioninfo->sw_minor_version = (uint8)DIO_SW_MINOR_VERSION;
/* Copy Software Patch Version */
versioninfo->sw_patch_version = (uint8)DIO_SW_PATCH_VERSION;
}
}
#endif
/************************************************************************************
* Service Name: Dio_FlipChannel
* Service ID[hex]: 0x11
* Sync/Async: Synchronous
* Reentrancy: Reentrant
* Parameters (in): ChannelId - ID of DIO channel.
* Parameters (inout): None
* Parameters (out): None
* Return value: Dio_LevelType
* Description: Function to flip the level of a channel and return the level of the channel after flip.
************************************************************************************/
#if (DIO_FLIP_CHANNEL_API == STD_ON)
Dio_LevelType Dio_FlipChannel(Dio_ChannelType ChannelId){
volatile Dio_PortLevelType * ODR_Ptr = NULL_PTR;
Dio_LevelType output = STD_LOW;
boolean error = FALSE;
#if (DIO_DEV_ERROR_DETECT == STD_ON)
/* Check if the Driver is initialized before using this function */
if (DIO_NOT_INITIALIZED == Dio_Status)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID,
DIO_FLIP_CHANNEL_SID, DIO_E_UNINIT);
error = TRUE;
}
else
{
/* No Action Required */
}
/* Check if the used channel is within the valid range */
if (DIO_CONFIGURED_CHANNLES <= ChannelId)
{
Det_ReportError(DIO_MODULE_ID, DIO_INSTANCE_ID,
DIO_FLIP_CHANNEL_SID, DIO_E_PARAM_INVALID_CHANNEL_ID);
error = TRUE;
}
else
{
/* No Action Required */
}
#endif
if(FALSE == error)
{
switch(Dio_PortChannels[ChannelId].Port_Num)
{
case 0: ODR_Ptr = &(GPIOA->ODR);
break;
case 1: ODR_Ptr = &(GPIOB->ODR);
break;
case 2: ODR_Ptr = &(GPIOC->ODR);
break;
case 3: ODR_Ptr = &(GPIOD->ODR);
break;
case 4: ODR_Ptr = &(GPIOE->ODR);
break;
case 5: ODR_Ptr = &(GPIOF->ODR);
break;
case 6: ODR_Ptr = &(GPIOG->ODR);
break;
case 7: ODR_Ptr = &(GPIOH->ODR);
break;
}
/* Read the required channel and write the required level */
if(BIT_IS_SET(*ODR_Ptr,Dio_PortChannels[ChannelId].Ch_Num))
{
CLEAR_BIT(*ODR_Ptr,Dio_PortChannels[ChannelId].Ch_Num);
output = STD_LOW;
}
else
{
SET_BIT(*ODR_Ptr,Dio_PortChannels[ChannelId].Ch_Num);
output = STD_HIGH;
}
return output;
}
else
{
/* No Action Required */
}
}
#endif