summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/lpc24xx/include/io.h
blob: c40d29e63160aecd8c5597545a89a6012ffc1fe6 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * @file
 *
 * @ingroup lpc24xx
 *
 * Input and output module.
 */

/*
 * Copyright (c) 2009
 * embedded brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#ifndef LIBBSP_ARM_LPC24XX_IO_H
#define LIBBSP_ARM_LPC24XX_IO_H

#include <rtems.h>

#include <bsp/lpc24xx.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define LPC24XX_IO_PORT_COUNT 5U

#define LPC24XX_IO_INDEX_MAX (LPC24XX_IO_PORT_COUNT * 32U)

#define LPC24XX_IO_INDEX_BY_PORT( port, bit) (((port) << 5U) + (bit))

#define LPC24XX_IO_PORT( index) (index >> 5U)

#define LPC24XX_IO_PORT_BIT( index) (index & 0x1fU)

typedef enum {
  LPC24XX_MODULE_ACF,
  LPC24XX_MODULE_ADC,
  LPC24XX_MODULE_BAT_RAM,
  LPC24XX_MODULE_CAN,
  LPC24XX_MODULE_DAC,
  LPC24XX_MODULE_EMC,
  LPC24XX_MODULE_ETHERNET,
  LPC24XX_MODULE_GPDMA,
  LPC24XX_MODULE_GPIO,
  LPC24XX_MODULE_I2C,
  LPC24XX_MODULE_I2S,
  LPC24XX_MODULE_LCD,
  LPC24XX_MODULE_MCI,
  LPC24XX_MODULE_PCB,
  LPC24XX_MODULE_PWM,
  LPC24XX_MODULE_RTC,
  LPC24XX_MODULE_SPI,
  LPC24XX_MODULE_SSP,
  LPC24XX_MODULE_SYSCON,
  LPC24XX_MODULE_TIMER,
  LPC24XX_MODULE_UART,
  LPC24XX_MODULE_USB,
  LPC24XX_MODULE_WDT,
  LPC24XX_MODULE_NUMBER
} lpc24xx_module;

typedef enum {
  LPC24XX_MODULE_PCLK_DEFAULT = 0x0U,
  LPC24XX_MODULE_CCLK = 0x1U,
  LPC24XX_MODULE_CCLK_2 = 0x2U,
  LPC24XX_MODULE_CCLK_4 = 0x0U,
  LPC24XX_MODULE_CCLK_6 = 0x3U,
  LPC24XX_MODULE_CCLK_8 = 0x3U
} lpc24xx_module_clock;

#define LPC24XX_MODULE_CLOCK_MASK 0x3U

typedef enum {
  LPC24XX_GPIO_DEFAULT = 0x0U,
  LPC24XX_GPIO_RESISTOR_DEFAULT = 0x0U,
  LPC24XX_GPIO_RESISTOR_NONE = 0x1U,
  LPC24XX_GPIO_RESISTOR_PULL_UP = 0x2U,
  LPC24XX_GPIO_RESISTOR_PULL_DOWN = 0x3U,
  LPC24XX_GPIO_INPUT = 0x0U,
  LPC24XX_GPIO_OUTPUT = 0x8U
} lpc24xx_gpio_settings;

#define LPC24XX_GPIO_RESISTOR_MASK 0x3U

rtems_status_code lpc24xx_module_enable(
  lpc24xx_module module,
  unsigned index,
  lpc24xx_module_clock clock
);

rtems_status_code lpc24xx_module_disable(
  lpc24xx_module module,
  unsigned index
);

rtems_status_code lpc24xx_io_config(
  lpc24xx_module module,
  unsigned index,
  unsigned config
);

rtems_status_code lpc24xx_io_release(
  lpc24xx_module module,
  unsigned index,
  unsigned config
);

rtems_status_code lpc24xx_gpio_config(
  unsigned index,
  lpc24xx_gpio_settings settings
);

static inline void lpc24xx_gpio_set( unsigned index)
{
  if (index <= LPC24XX_IO_INDEX_MAX) {
    unsigned port = LPC24XX_IO_PORT( index);
    unsigned bit = LPC24XX_IO_PORT_BIT( index);

    LPC24XX_FIO [port].set = 1U << bit;
  }
}

static inline void lpc24xx_gpio_clear( unsigned index)
{
  if (index <= LPC24XX_IO_INDEX_MAX) {
    unsigned port = LPC24XX_IO_PORT( index);
    unsigned bit = LPC24XX_IO_PORT_BIT( index);

    LPC24XX_FIO [port].clr = 1U << bit;
  }
}

static inline void lpc24xx_gpio_write( unsigned index, bool value)
{
  if (value) {
    lpc24xx_gpio_set( index);
  } else {
    lpc24xx_gpio_clear( index);
  }
}

static inline bool lpc24xx_gpio_get( unsigned index)
{
  if (index <= LPC24XX_IO_INDEX_MAX) {
    unsigned port = LPC24XX_IO_PORT( index);
    unsigned bit = LPC24XX_IO_PORT_BIT( index);

    return (LPC24XX_FIO [port].pin & (1U << bit)) != 0;
  } else {
    return false;
  }
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LIBBSP_ARM_LPC24XX_IO_H */