23 #include <libopencm3/stm32/gpio.h>
24 #include <libopencm3/stm32/rcc.h>
47 if (0 == strcmp(c->
argv[1],
"on"))
49 else if (0 == strcmp(c->
argv[1],
"off"))
51 else if (0 == strcmp(c->
argv[1],
"toggle"))
53 else if (0 == strcmp(c->
argv[1],
"pulse"))
55 else if (0 == strcmp(c->
argv[1],
"read"))
57 else if (0 == strcmp(c->
argv[1],
"float"))
59 else if (0 == strcmp(c->
argv[1],
"pullupdown"))
61 else if (0 == strcmp(c->
argv[1],
"opendrain"))
63 else if (0 == strcmp(c->
argv[1],
"pushpull"))
65 else if (0 == strcmp(c->
argv[1],
"detect"))
84 static bool gpio_is_connected(uint32_t gpioport, uint16_t gpio)
87 uint32_t readings = 0;
89 gpio_set_mode(gpioport, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
91 gpio_set(gpioport, gpio);
92 for (i = 0; i < 8; i++)
93 readings = (readings << 1) | !gpio_get(gpioport, gpio);
94 gpio_clear(gpioport, gpio);
95 for (i = 0; i < 8; i++)
96 readings = (readings << 1) | !!gpio_get(gpioport, gpio);
97 gpio_set_mode(gpioport, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, gpio);
107 static void do_opendrain_toggle(uintptr_t port, uint32_t pin)
109 for (
int i=0; i<32; i++) {
110 if (pin & (1 << i)) {
111 if (GPIO_MODER(port) & GPIO_MODE_MASK(i))
112 gpio_mode_setup(port, GPIO_MODE_INPUT,
113 GPIO_PUPD_NONE, 1 << i);
115 gpio_mode_setup(port, GPIO_MODE_OUTPUT,
116 GPIO_PUPD_NONE, 1 << i);
128 int action = parse_args(c, gpio);
130 gpio_mode_setup(gpio->
port, GPIO_MODE_INPUT, GPIO_PUPD_NONE,
132 }
else if (action ==
off) {
133 gpio_mode_setup(gpio->
port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
135 }
else if (action ==
toggle) {
136 do_opendrain_toggle(gpio->
port, gpio->
pin);
137 }
else if (action ==
pulse) {
138 do_opendrain_toggle(gpio->
port, gpio->
pin);
141 do_opendrain_toggle(gpio->
port, gpio->
pin);
143 fprintf(c->
out,
"Usage: %s on|off|toggle|pulse\n",
158 return do_opendrain_cmd(c);
163 int action = parse_args(c, gpio);
165 gpio_set(gpio->
port, gpio->
pin);
166 }
else if (action ==
off) {
167 gpio_clear(gpio->
port, gpio->
pin);
168 }
else if (action ==
toggle) {
169 gpio_toggle(gpio->
port, gpio->
pin);
170 }
else if (action ==
pulse) {
171 gpio_toggle(gpio->
port, gpio->
pin);
174 gpio_toggle(gpio->
port, gpio->
pin);
177 (
int) gpio_get(gpio->
port, gpio->
pin));
179 }
else if (action ==
hiz) {
180 gpio_set_mode(gpio->
port, GPIO_MODE_INPUT,
181 GPIO_CNF_INPUT_FLOAT, gpio->
pin);
182 }
else if (action ==
pullup) {
183 gpio_set_mode(gpio->
port, GPIO_MODE_INPUT,
184 GPIO_CNF_INPUT_PULL_UPDOWN, gpio->
pin);
186 gpio_set_mode(gpio->
port, GPIO_MODE_OUTPUT_2_MHZ,
187 GPIO_CNF_OUTPUT_OPENDRAIN, gpio->
pin);
189 gpio_set_mode(gpio->
port, GPIO_MODE_OUTPUT_2_MHZ,
190 GPIO_CNF_OUTPUT_PUSHPULL, gpio->
pin);
191 }
else if (action ==
detect) {
193 gpio_is_connected(gpio->
port, gpio->
pin) ?
"connected"
196 fprintf(c->
out,
"Usage: %s "
197 "on|off|toggle|pulse|hiz|pullup|opendrain|"
203 fprintf(c->
out,
"Usage: %s on|off|toggle|pulse\n",
213 switch (gpio->
port) {
216 rcc_periph_clock_enable(RCC_GPIOA);
221 rcc_periph_clock_enable(RCC_GPIOB);
226 rcc_periph_clock_enable(RCC_GPIOC);
231 rcc_periph_clock_enable(RCC_GPIOD);
236 rcc_periph_clock_enable(RCC_GPIOE);
241 rcc_periph_clock_enable(RCC_GPIOF);
248 gpio_set(gpio->
port, gpio->
pin);
250 gpio_clear(gpio->
port, gpio->
pin);
253 int mode = GPIO_MODE_OUTPUT_2_MHZ;
254 int cnf = GPIO_CNF_OUTPUT_PUSHPULL;
256 cnf = GPIO_CNF_OUTPUT_OPENDRAIN;
258 mode = GPIO_MODE_INPUT;
259 cnf = GPIO_CNF_INPUT_FLOAT;
261 gpio_set_mode(gpio->
port, mode, cnf, gpio->
pin);
262 #elif defined(STM32F4)
263 int mode = GPIO_MODE_OUTPUT;
265 mode = GPIO_MODE_INPUT;
266 gpio_mode_setup(gpio->
port, mode, GPIO_PUPD_NONE, gpio->
pin);
268 #error Unsupported part
#define containerof(ptr, type, member)
uint32_t u32[SCRATCH_SIZE/4]
const console_cmd_t * cmd
int console_gpio_register(const console_gpio_t *gpio)
Register a new GPIO command.
union console::@0 scratch
int console_register(const console_cmd_t *cmd)
Register a new command.
pt_state_t console_gpio_do_cmd(console_t *c)
bool fibre_timeout(uint32_t duetime)