2020-2021 Sunseeker Telemetry and Lighting System
gpio.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // gpio.c - Driver for the gpio Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_PORT1_R__
17 #include "gpio.h"
18 
19 #include <assert.h>
20 
21 static const uint16_t GPIO_PORT_TO_BASE[] = {
22  0x00,
23 #if defined(__MSP430_HAS_PORT1_R__)
24  __MSP430_BASEADDRESS_PORT1_R__,
25 #elif defined(__MSP430_HAS_PORT1__)
26  __MSP430_BASEADDRESS_PORT1__,
27 #else
28  0xFFFF,
29 #endif
30 #if defined(__MSP430_HAS_PORT2_R__)
31  __MSP430_BASEADDRESS_PORT2_R__,
32 #elif defined(__MSP430_HAS_PORT2__)
33  __MSP430_BASEADDRESS_PORT2__,
34 #else
35  0xFFFF,
36 #endif
37 #if defined(__MSP430_HAS_PORT3_R__)
38  __MSP430_BASEADDRESS_PORT3_R__,
39 #elif defined(__MSP430_HAS_PORT3__)
40  __MSP430_BASEADDRESS_PORT3__,
41 #else
42  0xFFFF,
43 #endif
44 #if defined(__MSP430_HAS_PORT4_R__)
45  __MSP430_BASEADDRESS_PORT4_R__,
46 #elif defined(__MSP430_HAS_PORT4__)
47  __MSP430_BASEADDRESS_PORT4__,
48 #else
49  0xFFFF,
50 #endif
51 #if defined(__MSP430_HAS_PORT5_R__)
52  __MSP430_BASEADDRESS_PORT5_R__,
53 #elif defined(__MSP430_HAS_PORT5__)
54  __MSP430_BASEADDRESS_PORT5__,
55 #else
56  0xFFFF,
57 #endif
58 #if defined(__MSP430_HAS_PORT6_R__)
59  __MSP430_BASEADDRESS_PORT6_R__,
60 #elif defined(__MSP430_HAS_PORT6__)
61  __MSP430_BASEADDRESS_PORT6__,
62 #else
63  0xFFFF,
64 #endif
65 #if defined(__MSP430_HAS_PORT7_R__)
66  __MSP430_BASEADDRESS_PORT7_R__,
67 #elif defined(__MSP430_HAS_PORT7__)
68  __MSP430_BASEADDRESS_PORT7__,
69 #else
70  0xFFFF,
71 #endif
72 #if defined(__MSP430_HAS_PORT8_R__)
73  __MSP430_BASEADDRESS_PORT8_R__,
74 #elif defined(__MSP430_HAS_PORT8__)
75  __MSP430_BASEADDRESS_PORT8__,
76 #else
77  0xFFFF,
78 #endif
79 #if defined(__MSP430_HAS_PORT9_R__)
80  __MSP430_BASEADDRESS_PORT9_R__,
81 #elif defined(__MSP430_HAS_PORT9__)
82  __MSP430_BASEADDRESS_PORT9__,
83 #else
84  0xFFFF,
85 #endif
86 #if defined(__MSP430_HAS_PORT10_R__)
87  __MSP430_BASEADDRESS_PORT10_R__,
88 #elif defined(__MSP430_HAS_PORT10__)
89  __MSP430_BASEADDRESS_PORT10__,
90 #else
91  0xFFFF,
92 #endif
93 #if defined(__MSP430_HAS_PORT11_R__)
94  __MSP430_BASEADDRESS_PORT11_R__,
95 #elif defined(__MSP430_HAS_PORT11__)
96  __MSP430_BASEADDRESS_PORT11__,
97 #else
98  0xFFFF,
99 #endif
100  0xFFFF,
101 #if defined(__MSP430_HAS_PORTJ_R__)
102  __MSP430_BASEADDRESS_PORTJ_R__
103 #elif defined(__MSP430_HAS_PORTJ__)
104  __MSP430_BASEADDRESS_PORTJ__
105 #else
106  0xFFFF
107 #endif
108 };
109 
110 void GPIO_setAsOutputPin(uint8_t selectedPort, uint16_t selectedPins) {
111 
112  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
113 
114  #ifndef NDEBUG
115  if(baseAddress == 0xFFFF) {
116  return;
117  }
118  #endif
119 
120  // Shift by 8 if port is even (upper 8-bits)
121  if((selectedPort & 1) ^ 1) {
122  selectedPins <<= 8;
123  }
124 
125  HWREG16(baseAddress + OFS_PASEL) &= ~selectedPins;
126  HWREG16(baseAddress + OFS_PADIR) |= selectedPins;
127 
128  return;
129 }
130 
131 void GPIO_setAsInputPin(uint8_t selectedPort, uint16_t selectedPins) {
132 
133  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
134 
135  #ifndef NDEBUG
136  if(baseAddress == 0xFFFF) {
137  return;
138  }
139  #endif
140 
141  // Shift by 8 if port is even (upper 8-bits)
142  if((selectedPort & 1) ^ 1) {
143  selectedPins <<= 8;
144  }
145 
146  HWREG16(baseAddress + OFS_PASEL) &= ~selectedPins;
147  HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
148  HWREG16(baseAddress + OFS_PAREN) &= ~selectedPins;
149 }
150 
151 void GPIO_setAsPeripheralModuleFunctionOutputPin(uint8_t selectedPort,
152  uint16_t selectedPins
153 ) {
154 
155  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
156 
157  #ifndef NDEBUG
158  if(baseAddress == 0xFFFF) {
159  return;
160  }
161  #endif
162 
163  // Shift by 8 if port is even (upper 8-bits)
164  if((selectedPort & 1) ^ 1) {
165  selectedPins <<= 8;
166  }
167 
168  HWREG16(baseAddress + OFS_PADIR) |= selectedPins;
169  HWREG16(baseAddress + OFS_PASEL) |= selectedPins;
170 }
171 
172 void GPIO_setAsPeripheralModuleFunctionInputPin(uint8_t selectedPort,
173  uint16_t selectedPins
174 ) {
175  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
176 
177  #ifndef NDEBUG
178  if(baseAddress == 0xFFFF) {
179  return;
180  }
181  #endif
182 
183  // Shift by 8 if port is even (upper 8-bits)
184  if((selectedPort & 1) ^ 1) {
185  selectedPins <<= 8;
186  }
187 
188  HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
189  HWREG16(baseAddress + OFS_PASEL) |= selectedPins;
190 }
191 
192 void GPIO_setOutputHighOnPin (uint8_t selectedPort,
193  uint16_t selectedPins) {
194 
195  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
196 
197  #ifndef NDEBUG
198  if(baseAddress == 0xFFFF) {
199  return;
200  }
201  #endif
202 
203  // Shift by 8 if port is even (upper 8-bits)
204  if((selectedPort & 1) ^ 1) {
205  selectedPins <<= 8;
206  }
207 
208  HWREG16(baseAddress + OFS_PAOUT) |= selectedPins;
209 }
210 
211 void GPIO_setOutputLowOnPin (uint8_t selectedPort, uint16_t selectedPins) {
212 
213  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
214 
215  #ifndef NDEBUG
216  if(baseAddress == 0xFFFF) {
217  return;
218  }
219  #endif
220 
221  // Shift by 8 if port is even (upper 8-bits)
222  if((selectedPort & 1) ^ 1) {
223  selectedPins <<= 8;
224  }
225 
226  HWREG16(baseAddress + OFS_PAOUT) &= ~selectedPins;
227 }
228 
229 void GPIO_toggleOutputOnPin (uint8_t selectedPort, uint16_t selectedPins) {
230 
231  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
232 
233  #ifndef NDEBUG
234  if(baseAddress == 0xFFFF) {
235  return;
236  }
237  #endif
238 
239  // Shift by 8 if port is even (upper 8-bits)
240  if((selectedPort & 1) ^ 1) {
241  selectedPins <<= 8;
242  }
243 
244  HWREG16(baseAddress + OFS_PAOUT) ^= selectedPins;
245 }
246 
247 void GPIO_setAsInputPinWithPullDownResistor(uint8_t selectedPort,
248  uint16_t selectedPins) {
249 
250  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
251 
252  #ifndef NDEBUG
253  if(baseAddress == 0xFFFF) {
254  return;
255  }
256  #endif
257 
258  // Shift by 8 if port is even (upper 8-bits)
259  if((selectedPort & 1) ^ 1) {
260  selectedPins <<= 8;
261  }
262 
263  HWREG16(baseAddress + OFS_PASEL) &= ~selectedPins;
264 
265  HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
266  HWREG16(baseAddress + OFS_PAREN) |= selectedPins;
267  HWREG16(baseAddress + OFS_PAOUT) &= ~selectedPins;
268 }
269 
270 void GPIO_setAsInputPinWithPullUpResistor(uint8_t selectedPort,
271  uint16_t selectedPins) {
272 
273  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
274 
275  #ifndef NDEBUG
276  if(baseAddress == 0xFFFF) {
277  return;
278  }
279  #endif
280 
281  // Shift by 8 if port is even (upper 8-bits)
282  if((selectedPort & 1) ^ 1) {
283  selectedPins <<= 8;
284  }
285 
286  HWREG16(baseAddress + OFS_PASEL) &= ~selectedPins;
287  HWREG16(baseAddress + OFS_PADIR) &= ~selectedPins;
288  HWREG16(baseAddress + OFS_PAREN) |= selectedPins;
289  HWREG16(baseAddress + OFS_PAOUT) |= selectedPins;
290 }
291 
292 uint8_t GPIO_getInputPinValue(uint8_t selectedPort,
293  uint16_t selectedPins) {
294 
295  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
296 
297  #ifndef NDEBUG
298  if(baseAddress == 0xFFFF) {
299  return;
300  }
301  #endif
302 
303  // Shift by 8 if port is even (upper 8-bits)
304  if((selectedPort & 1) ^ 1) {
305  selectedPins <<= 8;
306  }
307 
308  uint16_t inputPinValue = HWREG16(baseAddress + OFS_PAIN) & (selectedPins);
309 
310  if(inputPinValue > 0){
311  return (GPIO_INPUT_PIN_HIGH);
312  }
313  return (GPIO_INPUT_PIN_LOW);
314 }
315 
316 void GPIO_enableInterrupt(uint8_t selectedPort, uint16_t selectedPins) {
317 
318  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
319 
320  #ifndef NDEBUG
321  if(baseAddress == 0xFFFF) {
322  return;
323  }
324  #endif
325 
326  // Shift by 8 if port is even (upper 8-bits)
327  if((selectedPort & 1) ^ 1) {
328  selectedPins <<= 8;
329  }
330 
331  HWREG16(baseAddress + OFS_PAIE) |= selectedPins;
332 }
333 
334 void GPIO_disableInterrupt(uint8_t selectedPort, uint16_t selectedPins) {
335 
336  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
337 
338  #ifndef NDEBUG
339  if(baseAddress == 0xFFFF) {
340  return;
341  }
342  #endif
343 
344  // Shift by 8 if port is even (upper 8-bits)
345  if((selectedPort & 1) ^ 1) {
346  selectedPins <<= 8;
347  }
348 
349  HWREG16(baseAddress + OFS_PAIE) &= ~selectedPins;
350 }
351 
352 uint16_t GPIO_getInterruptStatus(uint8_t selectedPort, uint16_t selectedPins) {
353 
354  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
355 
356  #ifndef NDEBUG
357  if(baseAddress == 0xFFFF) {
358  return;
359  }
360  #endif
361 
362  // Shift by 8 if port is even (upper 8-bits)
363  if((selectedPort & 1) ^ 1) {
364  if((baseAddress & 0x1) ^ 0x1)
365  {
366  return (HWREG8(baseAddress + OFS_PAIFG_H) & selectedPins);
367  }
368  else
369  {
370  return (HWREG8(baseAddress + OFS_PAIFG) & selectedPins);
371  }
372  }
373  else {
374  return (HWREG16(baseAddress + OFS_PAIFG) & selectedPins);
375  }
376 }
377 
378 void GPIO_clearInterrupt(uint8_t selectedPort, uint16_t selectedPins) {
379 
380  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
381 
382  #ifndef NDEBUG
383  if(baseAddress == 0xFFFF) {
384  return;
385  }
386  #endif
387 
388  // Shift by 8 if port is even (upper 8-bits)
389  if((selectedPort & 1) ^ 1) {
390  selectedPins <<= 8;
391  }
392 
393  HWREG16(baseAddress + OFS_PAIFG) &= ~selectedPins;
394 }
395 
396 void GPIO_selectInterruptEdge(uint8_t selectedPort, uint16_t selectedPins,
397  uint8_t edgeSelect) {
398 
399  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
400 
401  #ifndef NDEBUG
402  if(baseAddress == 0xFFFF) {
403  return;
404  }
405  #endif
406 
407  // Shift by 8 if port is even (upper 8-bits)
408  if((selectedPort & 1) ^ 1) {
409  selectedPins <<= 8;
410  }
411 
412  if (GPIO_LOW_TO_HIGH_TRANSITION == edgeSelect){
413  HWREG16(baseAddress + OFS_PAIES) &= ~selectedPins;
414  }
415  else {
416  HWREG16(baseAddress + OFS_PAIES) |= selectedPins;
417  }
418 }
419 
420 void GPIO_setDriveStrength(uint8_t selectedPort, uint16_t selectedPins,
421  uint8_t driveStrength) {
422 
423  uint16_t baseAddress = GPIO_PORT_TO_BASE[selectedPort];
424 
425  #ifndef NDEBUG
426  if(baseAddress == 0xFFFF) {
427  return;
428  }
429  #endif
430 
431  // Shift by 8 if port is even (upper 8-bits)
432  if((selectedPort & 1) ^ 1) {
433  selectedPins <<= 8;
434  }
435 
436  if(GPIO_REDUCED_OUTPUT_DRIVE_STRENGTH == driveStrength) {
437  HWREG16(baseAddress + OFS_PADS) &= ~selectedPins;
438  } else {
439  HWREG16(baseAddress + OFS_PADS) |= selectedPins;
440  }
441 }
442 
443 #endif
444 //*****************************************************************************
445 //
448 //
449 //*****************************************************************************
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)