1 | /* |
---|
2 | * Copyright (c) 2013 Eugeniy Meshcheryakov <eugen@debian.org> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <bsp/io.h> |
---|
10 | #include <bsp/lm3s69xx.h> |
---|
11 | #include <bsp/syscon.h> |
---|
12 | #include <rtems.h> |
---|
13 | |
---|
14 | static void set_bit(volatile uint32_t *reg, unsigned index, uint32_t set) |
---|
15 | { |
---|
16 | uint32_t mask = 1U; |
---|
17 | uint32_t val = *reg; |
---|
18 | |
---|
19 | val &= ~(mask << index); |
---|
20 | val |= set << index; |
---|
21 | |
---|
22 | *reg = val; |
---|
23 | } |
---|
24 | |
---|
25 | static void set_config(unsigned int pin, const lm3s69xx_gpio_config *config) |
---|
26 | { |
---|
27 | unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin); |
---|
28 | volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port); |
---|
29 | unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin); |
---|
30 | rtems_interrupt_level level; |
---|
31 | |
---|
32 | rtems_interrupt_disable(level); |
---|
33 | |
---|
34 | lm3s69xx_syscon_enable_gpio_clock(port, true); |
---|
35 | |
---|
36 | /* Disable digital and analog functions before reconfiguration. */ |
---|
37 | set_bit(&gpio->den, index, 0); |
---|
38 | set_bit(&gpio->amsel, index, 0); |
---|
39 | |
---|
40 | set_bit(&gpio->afsel, index, config->alternate); |
---|
41 | set_bit(&gpio->dir, index, config->dir); |
---|
42 | set_bit(&gpio->odr, index, config->otype); |
---|
43 | |
---|
44 | switch (config->drive) { |
---|
45 | case LM3S69XX_GPIO_DRIVE_4MA: |
---|
46 | gpio->dr4r |= 1 << index; |
---|
47 | break; |
---|
48 | case LM3S69XX_GPIO_DRIVE_8MA: |
---|
49 | gpio->dr8r |= 1 << index; |
---|
50 | break; |
---|
51 | default: |
---|
52 | gpio->dr2r |= 1 << index; |
---|
53 | break; |
---|
54 | } |
---|
55 | |
---|
56 | switch (config->pull) { |
---|
57 | case LM3S69XX_GPIO_PULL_UP: |
---|
58 | gpio->pur |= 1 << index; |
---|
59 | break; |
---|
60 | case LM3S69XX_GPIO_PULL_DOWN: |
---|
61 | gpio->pdr |= 1 << index; |
---|
62 | break; |
---|
63 | default: |
---|
64 | set_bit(&gpio->pdr, index, 0); |
---|
65 | set_bit(&gpio->pur, index, 0); |
---|
66 | break; |
---|
67 | } |
---|
68 | |
---|
69 | set_bit(&gpio->slr, index, config->slr); |
---|
70 | |
---|
71 | set_bit(&gpio->den, index, config->digital); |
---|
72 | set_bit(&gpio->amsel, index, config->analog); |
---|
73 | |
---|
74 | rtems_interrupt_enable(level); |
---|
75 | } |
---|
76 | |
---|
77 | void lm3s69xx_gpio_set_config(const lm3s69xx_gpio_config *config) |
---|
78 | { |
---|
79 | unsigned int current = config->pin_first; |
---|
80 | unsigned int last = config->pin_last; |
---|
81 | |
---|
82 | while (current <= last) { |
---|
83 | set_config(current, config); |
---|
84 | current++; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void lm3s69xx_gpio_set_config_array(const lm3s69xx_gpio_config *configs, unsigned int count) |
---|
89 | { |
---|
90 | unsigned int i; |
---|
91 | |
---|
92 | for (i = 0; i < count; i++) |
---|
93 | lm3s69xx_gpio_set_config(&configs[i]); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Enables/disables digital function on the specified pin. |
---|
98 | */ |
---|
99 | void lm3s69xx_gpio_digital_enable(unsigned int pin, bool enable) |
---|
100 | { |
---|
101 | unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin); |
---|
102 | volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port); |
---|
103 | unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin); |
---|
104 | rtems_interrupt_level level; |
---|
105 | |
---|
106 | rtems_interrupt_disable(level); |
---|
107 | set_bit(&gpio->den, index, enable); |
---|
108 | rtems_interrupt_enable(level); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Enables/disables analog mode on the specified pin. |
---|
113 | */ |
---|
114 | void lm3s69xx_gpio_analog_mode_select(unsigned int pin, bool enable) |
---|
115 | { |
---|
116 | unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin); |
---|
117 | volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port); |
---|
118 | unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin); |
---|
119 | rtems_interrupt_level level; |
---|
120 | |
---|
121 | rtems_interrupt_disable(level); |
---|
122 | set_bit(&gpio->amsel, index, enable); |
---|
123 | rtems_interrupt_enable(level); |
---|
124 | } |
---|
125 | |
---|
126 | void lm3s69xx_gpio_set_pin(unsigned int pin, bool set) |
---|
127 | { |
---|
128 | unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin); |
---|
129 | volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port); |
---|
130 | unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin); |
---|
131 | uint32_t mask = 1U << index; |
---|
132 | |
---|
133 | gpio->data[mask] = set ? mask : 0; |
---|
134 | } |
---|
135 | |
---|
136 | bool lm3s69xx_gpio_get_pin(unsigned int pin) |
---|
137 | { |
---|
138 | unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin); |
---|
139 | volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port); |
---|
140 | unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin); |
---|
141 | uint32_t mask = 1U << index; |
---|
142 | |
---|
143 | return gpio->data[mask] != 0; |
---|
144 | } |
---|