summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/lm3s69xx/startup/io.c
blob: b476e7b6dd7f54fe9f8906a2d0754ddc8baf47a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright © 2013 Eugeniy Meshcheryakov <eugen@debian.org>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <bsp/io.h>
#include <bsp/lm3s69xx.h>
#include <bsp/syscon.h>
#include <rtems.h>

static void set_bit(volatile uint32_t *reg, unsigned index, uint32_t set)
{
  uint32_t mask = 1U;
  uint32_t val = *reg;

  val &= ~(mask << index);
  val |= set << index;

  *reg = val;
}

static void set_config(unsigned int pin, const lm3s69xx_gpio_config *config)
{
  unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin);
  volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port);
  unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin);
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);

  lm3s69xx_syscon_enable_gpio_clock(port, true);

  /* Disable digital and analog functions before reconfiguration. */
  set_bit(&gpio->den, index, 0);
  set_bit(&gpio->amsel, index, 0);

  set_bit(&gpio->afsel, index, config->alternate);
  set_bit(&gpio->dir, index, config->dir);
  set_bit(&gpio->odr, index, config->otype);

  switch (config->drive) {
  case LM3S69XX_GPIO_DRIVE_4MA:
    gpio->dr4r |= 1 << index;
    break;
  case LM3S69XX_GPIO_DRIVE_8MA:
    gpio->dr8r |= 1 << index;
    break;
  default:
    gpio->dr2r |= 1 << index;
    break;
  }

  switch (config->pull) {
  case LM3S69XX_GPIO_PULL_UP:
    gpio->pur |= 1 << index;
    break;
  case LM3S69XX_GPIO_PULL_DOWN:
    gpio->pdr |= 1 << index;
    break;
  default:
    set_bit(&gpio->pdr, index, 0);
    set_bit(&gpio->pur, index, 0);
    break;
  }

  set_bit(&gpio->slr, index, config->slr);

  set_bit(&gpio->den, index, config->digital);
  set_bit(&gpio->amsel, index, config->analog);

  rtems_interrupt_enable(level);
}

void lm3s69xx_gpio_set_config(const lm3s69xx_gpio_config *config)
{
  unsigned int current = config->pin_first;
  unsigned int last = config->pin_last;

  while (current <= last) {
    set_config(current, config);
    current++;
  }
}

void lm3s69xx_gpio_set_config_array(const lm3s69xx_gpio_config *configs, unsigned int count)
{
  unsigned int i;

  for (i = 0; i < count; i++)
    lm3s69xx_gpio_set_config(&configs[i]);
}

/**
 * Enables/disables digital function on the specified pin.
 */
void lm3s69xx_gpio_digital_enable(unsigned int pin, bool enable)
{
  unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin);
  volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port);
  unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin);
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  set_bit(&gpio->den, index, enable);
  rtems_interrupt_enable(level);
}

/**
 * Enables/disables analog mode on the specified pin.
 */
void lm3s69xx_gpio_analog_mode_select(unsigned int pin, bool enable)
{
  unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin);
  volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port);
  unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin);
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  set_bit(&gpio->amsel, index, enable);
  rtems_interrupt_enable(level);
}

void lm3s69xx_gpio_set_pin(unsigned int pin, bool set)
{
  unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin);
  volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port);
  unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin);
  uint32_t mask = 1U << index;

  gpio->data[mask] = set ? mask : 0;
}

bool lm3s69xx_gpio_get_pin(unsigned int pin)
{
  unsigned int port = LM3S69XX_GPIO_PORT_OF_PIN(pin);
  volatile lm3s69xx_gpio *gpio = LM3S69XX_GPIO(port);
  unsigned int index = LM3S69XX_GPIO_INDEX_OF_PIN(pin);
  uint32_t mask = 1U << index;

  return gpio->data[mask] != 0;
}