This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
/
gpio.c
186 lines (133 loc) · 8.33 KB
/
gpio.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
/***************************************************************************************************
ExploreEmbedded
****************************************************************************************************
* File: gpio.h
* Version: 15.0
* Author: ExploreEmbedded
* Website: http://www.exploreembedded.com/wiki
* Description: File contains the the accessing the GPIO pins of a controller.
First the GPIO pins needs to configured(Input/Output) and later used accordingly.
The libraries have been tested on ExploreEmbedded development boards. We strongly believe that the
library works on any of development boards for respective controllers. However, ExploreEmbedded
disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly.
Files may be subject to change without prior notice. The revision history contains the information
related to updates.
GNU GENERAL PUBLIC LICENSE:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Errors and omissions should be reported to codelibraries@exploreembedded.com
**************************************************************************************************/
#include "chip.h"
#include "stdutils.h"
#include "gpio.h"
#include "BitBanding.h"
inline void GPIO_PinFunction(gpioPins_et enm_pinNumber, uint8_t var_pinFunction_u8)
{
uint8_t var_portNumber_u8 = (enm_pinNumber >> 5); //Divide the pin number by 32 go get the PORT number
uint8_t var_pinNumber_u8 = (enm_pinNumber & 0x1f); //lower 5-bits contains the bit number of a 32bit port
var_portNumber_u8 = var_portNumber_u8 * 2;
if(var_pinNumber_u8 >= 16)
{
var_portNumber_u8++;
var_pinNumber_u8 = var_pinNumber_u8 - 16;
}
var_pinNumber_u8 = var_pinNumber_u8 * 2;
//Each pin in PINSEL has 2 bits to configure
//Get the bitband alias address of both bits (IOCON is in peripheral region)
volatile uint32_t* bb_pin_b1 = bb_alias_periph_dword(&LPC_IOCON->PINSEL[var_portNumber_u8], var_pinNumber_u8);
volatile uint32_t* bb_pin_b2 = bb_alias_periph_dword(&LPC_IOCON->PINSEL[var_portNumber_u8], var_pinNumber_u8+1);
*bb_pin_b1 = (var_pinFunction_u8 & 0x01); //1st bit of pinFunction
*bb_pin_b2 = (var_pinFunction_u8 >> 1) & 0x01; //2nd bit of pinFunction
}
/***************************************************************************************************
void GPIO_PinDirection(gpioPins_et enm_pinNumber, uint8_t var_pinDirn_u8)
***************************************************************************************************
* I/P Arguments:
gpioPins_et: pin number which needs to be updated.
Refer the gpio.h for pin numbering.
gpioDirection_et: Direction to be configured ie. INPUT/OUTPUT.
* Return value : none
* description :This function sets the specified direction as INPUT/OUTPUT.
***************************************************************************************************/
inline volatile uint32_t *GPIO_BB_PinDirectionPtr(gpioPins_et enm_pinNumber)
{
const uint8_t var_portNumber_u8 = (enm_pinNumber >> 5); //Divide the pin number by 32 go get the PORT number
const uint8_t var_pinNumber_u8 = (enm_pinNumber & 0x1f); //lower 5-bits contains the bit number of a 32bit port
LPC_GPIO_T *LPC_GPIO_PORT = (LPC_GPIO_T*)(LPC_GPIO0_BASE + ((var_portNumber_u8) << 5));
//Get the bitband alias address
return bb_alias_sram_dword(&LPC_GPIO_PORT->DIR, var_pinNumber_u8);
}
inline void GPIO_PinDirection(gpioPins_et enm_pinNumber, uint8_t var_pinDirn_u8)
{
const uint8_t var_portNumber_u8 = (enm_pinNumber >> 5); //Divide the pin number by 32 go get the PORT number
const uint8_t var_pinNumber_u8 = (enm_pinNumber & 0x1f); //lower 5-bits contains the bit number of a 32bit port
/* Go to particular port after decoding from the pin number and set the direction as specified*/
LPC_GPIO_T *LPC_GPIO_PORT = (LPC_GPIO_T*)(LPC_GPIO0_BASE + ((var_portNumber_u8) << 5));
//Get the bitband alias address
volatile uint32_t* bb_pin = bb_alias_sram_dword(&LPC_GPIO_PORT->DIR, var_pinNumber_u8);
*bb_pin = var_pinDirn_u8 & 0x01; //set the BB alias word which sets the bit in LPC_GPIO_PORT->DIR
}
/***************************************************************************************************
void GPIO_PinWrite(gpioPins_et enm_pinNumber, uint8_t var_pinValue_u8)
***************************************************************************************************
* I/P Arguments:
gpioPins_et: pin number which needs to be updated.
Refer the gpio.h for pin numbering.
gpioValue_et: Value to be updated(LOW/HIGH) on the selected pin.
* Return value : none
* description :This function updates the specified value on the selected pin.
Before updating the pins status, the pin function should be selected and then
the pin should be configured as OUTPUT
***************************************************************************************************/
inline void GPIO_PinWrite(gpioPins_et enm_pinNumber, uint8_t var_pinValue_u8)
{
const uint8_t var_portNumber_u8 = (enm_pinNumber >> 5); //Divide the pin number by 32 go get the PORT number
const uint8_t var_pinNumber_u8 = (enm_pinNumber & 0x1f); //lower 5-bits contains the bit number of a 32bit port
LPC_GPIO_T *LPC_GPIO_PORT = (LPC_GPIO_T*)(LPC_GPIO0_BASE + ((var_portNumber_u8) << 5));
if (var_pinValue_u8 & 0x1)
{
LPC_GPIO_PORT->SET = 1 << var_pinNumber_u8;
}
else
{
LPC_GPIO_PORT->CLR = 1 << var_pinNumber_u8;
}
}
inline volatile uint32_t *GPIO_BB_PinValuePtr(gpioPins_et enm_pinNumber)
{
const uint8_t var_portNumber_u8 = (enm_pinNumber >> 5); //Divide the pin number by 32 go get the PORT number
const uint8_t var_pinNumber_u8 = (enm_pinNumber & 0x1f); //lower 5-bits contains the bit number of a 32bit port
LPC_GPIO_T * LPC_GPIO_PORT = (LPC_GPIO_T*)(LPC_GPIO0_BASE + ((var_portNumber_u8) << 5));
//Get the bitband alias address
return bb_alias_sram_dword(&LPC_GPIO_PORT->PIN, var_pinNumber_u8);
}
/***************************************************************************************************
uint8_t GPIO_PinRead(gpioPins_et enm_pinNumber)
***************************************************************************************************
* I/P Arguments:
gpioPins_et: pin number which needs to be read.
Refer the gpio.h for pin numbering.
* Return value :
uint8_t: Status of the specified pin.
* description :This function returns the status of the selected pin.
Before reading the pins status, the pin function should be selected and accordingly
the pin should be configured as INPUT
***************************************************************************************************/
inline uint8_t GPIO_PinRead(gpioPins_et enm_pinNumber)
{
uint8_t returnStatus = 0;
const uint8_t var_portNumber_u8 = (enm_pinNumber >> 5); //Divide the pin number by 32 go get the PORT number
const uint8_t var_pinNumber_u8 = (enm_pinNumber & 0x1f); //lower 5-bits contains the bit number of a 32bit port
/* Go to particular port after decoding from the pin number and read the pins status */
LPC_GPIO_T * LPC_GPIO_PORT = (LPC_GPIO_T*)(LPC_GPIO0_BASE + ((var_portNumber_u8) << 5));
returnStatus = util_IsBitSet(LPC_GPIO_PORT->PIN,var_pinNumber_u8);
return returnStatus;
}