Contiki 2.5
gpio-util.c
1 
2 #include <mc1322x.h>
3 #include <board.h>
4 
5 #include "gpio-util.h"
6 
7 void gpio_select_function(uint8_t gpio, uint8_t func) {
8  uint32_t mask = 3;
9  uint8_t major, minor, shift;
10  volatile uint32_t *base = GPIO_FUNC_SEL0;
11  uint32_t value;
12  major = gpio >> 4;
13  minor = gpio & 0xF;
14  shift = 2 * minor;
15 
16  value = base[major];
17  value &= ~(mask << shift);
18  value |= (func << shift);
19  base[major] = value;
20 }
21 
22 void gpio_reg_set(volatile uint32_t* reg, uint8_t bit) {
23  uint8_t major, minor;
24  major = bit / 32;
25  minor = bit % 32;
26  *(reg + major) |= (1UL << minor);
27 }
28 
29 void gpio_reg_clear(volatile uint32_t* reg, uint8_t bit) {
30  uint8_t major, minor;
31  major = bit / 32;
32  minor = bit % 32;
33  *(reg + major) &= ~(1UL << minor);
34 }
35 
36 void gpio_set_pad_dir(uint8_t gpio, uint8_t dir) {
37  uint8_t major, minor;
38  major = gpio / 32;
39  minor = gpio % 32;
40  if (dir) gpio_reg_set(GPIO_PAD_DIR0 + major, minor);
41  else gpio_reg_clear(GPIO_PAD_DIR0 + major, minor);
42 }
43 
44 void gpio_set(uint8_t gpio) {
45  uint8_t major, minor;
46  major = gpio / 32;
47  minor = gpio % 32;
48  *(GPIO_DATA_SET0 + major) = (1UL << minor);
49 }
50 
51 void gpio_reset(uint8_t gpio) {
52  uint8_t major, minor;
53  major = gpio / 32;
54  minor = gpio % 32;
55  *(GPIO_DATA_RESET0 + major) = (1UL << minor);
56 }
57 
58 bool gpio_read(uint8_t gpio) {
59  uint8_t major, minor;
60  major = gpio / 32;
61  minor = gpio % 32;
62  return (*(GPIO_DATA0 + major) >> minor) & 1;
63 }