summaryrefslogtreecommitdiff
path: root/firmware/drivers/GPIO.c
blob: f89b0382b72931863d1eb4cbeee4918058fee4f0 (plain)
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
#include <LPC17xx.h>

//#include "lpc_types.h"
#define FALSE 0
#define TRUE 1

#include "GPIO.h"

static LPC_GPIO_TypeDef (* const LPC_GPIO[5]) = { LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3, LPC_GPIO4  };


void GPIOSetValue( uint32_t portNum, uint32_t bitPosi, uint32_t bitVal )
{
  if (bitVal == 0)
  {
	  LPC_GPIO[portNum]->FIOCLR = (1<<bitPosi);
  }
  else if (bitVal >= 1)
  {
	  LPC_GPIO[portNum]->FIOSET = (1<<bitPosi);
  }
}

void GPIOSetDir( uint32_t portNum, uint32_t bitPosi, uint32_t dir )
{
  if(dir)
	LPC_GPIO[portNum]->FIODIR |= 1<<bitPosi;
  else
	LPC_GPIO[portNum]->FIODIR &= ~(1<<bitPosi);
}

void GPIOSetPull( uint32_t portNum, uint32_t bitPosi, uint32_t dir)
{

	if (dir == 0) {								//no Pull
		dir = 10;
	} else if(dir == 1){   						//Pull up
		dir = 00;
	} else if(dir == 2){						//Pull down
		dir = 11;
	}

	switch (portNum)
	{
		case 0:

			if (bitPosi < 16 ) {
				bitPosi = bitPosi * 2;
				LPC_PINCON->PINMODE0 |= dir<<bitPosi;
			} else if (bitPosi > 15){
				bitPosi = bitPosi - 16;
				bitPosi = bitPosi * 2;
				LPC_PINCON->PINMODE1 |= dir<<bitPosi;
			}

		break;

		case 1:

			if (bitPosi < 16 ) {
				bitPosi = bitPosi * 2;
				LPC_PINCON->PINMODE2 |= dir<<bitPosi;
			} else if (bitPosi > 15){
				bitPosi = bitPosi - 16;
				bitPosi = bitPosi * 2;
				LPC_PINCON->PINMODE3 |= dir<<bitPosi;
			}

		break;

		case 2:

			if (bitPosi < 14 ) {
				bitPosi = bitPosi * 2;
				LPC_PINCON->PINMODE4 |= dir<<bitPosi;
			}

		break;

		case 3:

			if (bitPosi == 25){
				LPC_PINCON->PINMODE7 |= dir<<18;
			}else if (bitPosi == 26){
				LPC_PINCON->PINMODE7 |= dir<<20;
			}

		break;

		case 4:
			if (bitPosi == 28){
				LPC_PINCON->PINMODE9 |= dir<<24;
			}else if (bitPosi == 29){
				LPC_PINCON->PINMODE9 |= dir<<26;
			}

	}
}

uint32_t GPIOGetValue (uint32_t portNum, uint32_t bitPosi)
{
    uint32_t val;
    LPC_GPIO[portNum]->FIOMASK = ~(1<<bitPosi);
    val = LPC_GPIO[portNum]->FIOPIN;
    val = val >> bitPosi;
    LPC_GPIO[portNum]->FIOMASK = 0x00000000;
    return val;
}

void GPIOSetInterrupt (  uint32_t portNum, uint32_t bitPosi, uint32_t dir )
{
	//  Dir is 0 for falling edge interrupt and 1 for rising edge interrupt
	if (portNum == 0)
	{
		if (dir == 0)
		{
			LPC_GPIOINT->IO0IntEnF |= (1<<bitPosi);
		}
		else if (dir == 1)
		{
			LPC_GPIOINT->IO0IntEnR |= (1<<bitPosi);
		}
	}
	else if (portNum == 2)
	{
		if (dir == 0)
		{
			LPC_GPIOINT->IO2IntEnF |= (1<<bitPosi);
		}
		else if (dir == 1)
		{
			LPC_GPIOINT->IO2IntEnR |= (1<<bitPosi);
		}
	}

	NVIC_EnableIRQ(EINT3_IRQn);
}

void GPIOClearInterrupt( void )
{
	LPC_GPIOINT->IO0IntClr = 0x7FFF8FFF;
	LPC_GPIOINT->IO2IntClr = 0x3fff;
}

uint32_t GPIOCheckInterrupts ( uint32_t portNum, uint32_t dir)
{
	//  Dir is 0 for falling edge interrupt and 1 for rising edge interrupt
	if (portNum == 0)
	{
		if (dir == 0)
		{
			return LPC_GPIOINT->IO0IntStatF;
		}
		else if (dir == 1)
		{
			return LPC_GPIOINT->IO0IntStatR;
		}
	}
	else if (portNum == 2)
	{
		if (dir == 0)
		{
			return LPC_GPIOINT->IO0IntStatF;
		}
		else if (dir == 1)
		{
			return LPC_GPIOINT->IO0IntStatR;
		}
	}
	return FALSE;
}