source: rtems/c/src/lib/libbsp/arm/stm32f4/startup/io.c @ bce41f4

4.115
Last change on this file since bce41f4 was 228ece9, checked in by Sebastian Huber <sebastian.huber@…>, on 04/12/12 at 19:27:56

bsp/stm32f4: Add IO and RCC

  • Property mode set to 100644
File size: 1.9 KB
Line 
1/*
2 * Copyright (c) 2012 Sebastian Huber.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.com/license/LICENSE.
13 */
14
15#include <bsp/io.h>
16
17#include <rtems.h>
18
19static void clear_and_set(
20  volatile uint32_t *reg,
21  unsigned index,
22  unsigned width,
23  uint32_t set
24)
25{
26  uint32_t one = 1;
27  uint32_t mask = (one << width) - one;
28  unsigned shift = width * index;
29  uint32_t val = *reg;
30
31  val &= ~(mask << shift);
32  val |= set << shift;
33
34  *reg = val;
35}
36
37void stm32f4_gpio_set_config(const stm32f4_gpio_config *config)
38{
39  unsigned pin = config->pin;
40  unsigned port = STM32F4_GPIO_PORT_OF_PIN(pin);
41  volatile stm32f4_gpio *gpio = STM32F4_GPIO(port);
42  unsigned index = STM32F4_GPIO_INDEX_OF_PIN(pin);
43  unsigned af_reg = index >> 8;
44  unsigned af_index = index & 0x3;
45  rtems_interrupt_level level;
46
47  rtems_interrupt_disable(level);
48  clear_and_set(&gpio->moder, index, 2, config->mode);
49  clear_and_set(&gpio->afr [af_reg], af_index, 4, config->af);
50  clear_and_set(&gpio->pupdr, index, 2, config->pupd);
51  clear_and_set(&gpio->otyper, index, 1, config->otype);
52  clear_and_set(&gpio->ospeedr, index, 2, config->ospeed);
53  rtems_interrupt_enable(level);
54}
55
56void stm32f4_gpio_set_output(int pin, bool set)
57{
58  int port = STM32F4_GPIO_PORT_OF_PIN(pin);
59  volatile stm32f4_gpio *gpio = STM32F4_GPIO(port);
60  int index = STM32F4_GPIO_INDEX_OF_PIN(pin);
61  int offset = set ? 0 : 16;
62  uint32_t one = 1;
63
64  gpio->bsrr = one << (index + offset);
65}
66
67bool stm32f4_gpio_get_input(int pin)
68{
69  int port = STM32F4_GPIO_PORT_OF_PIN(pin);
70  volatile stm32f4_gpio *gpio = STM32F4_GPIO(port);
71  int index = STM32F4_GPIO_INDEX_OF_PIN(pin);
72  uint32_t one = 1;
73
74  return (gpio->idr & (one << index)) != 0;
75}
Note: See TracBrowser for help on using the repository browser.