aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/LPC11xx/ext_lld.c
blob: c22cd83be73fde304319df29c694cf8852b92f36 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
    ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
    LPC11xx EXT driver - Copyright (C) 2013 Marcin Jokel

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    LPC11xx/ext_lld.c
 * @brief   LPC11xx EXT subsystem low level driver source.
 *
 * @addtogroup EXT
 * @{
 */

#include "ch.h"
#include "hal.h"

#if HAL_USE_EXT || defined(__DOXYGEN__)

#include "ext_lld_isr.h"

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/**
 * @brief   EXTD0 driver identifier.
 */
#if LPC11xx_EXT_USE_EXT0 || defined(__DOXYGEN__)
EXTDriver EXTD0;
#endif

/**
 * @brief   EXTD1 driver identifier.
 */
#if LPC11xx_EXT_USE_EXT1 || defined(__DOXYGEN__)
EXTDriver EXTD1;
#endif

/**
 * @brief   EXTD2 driver identifier.
 */
#if LPC11xx_EXT_USE_EXT2 || defined(__DOXYGEN__)
EXTDriver EXTD2;
#endif

/**
 * @brief   EXTD3 driver identifier.
 */
#if LPC11xx_EXT_USE_EXT3 || defined(__DOXYGEN__)
EXTDriver EXTD3;
#endif

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level EXT driver initialization.
 *
 * @notapi
 */
void ext_lld_init(void) {

  /* Driver initialization.*/
#if LPC11xx_EXT_USE_EXT0
  extObjectInit(&EXTD0);
  EXTD0.gpio = LPC_GPIO0;
#endif

#if LPC11xx_EXT_USE_EXT1
  extObjectInit(&EXTD1);
  EXTD1.gpio = LPC_GPIO1;
#endif

#if LPC11xx_EXT_USE_EXT2
  extObjectInit(&EXTD2);
  EXTD2.gpio = LPC_GPIO2;
#endif

#if LPC11xx_EXT_USE_EXT3
  extObjectInit(&EXTD3);
  EXTD3.gpio = LPC_GPIO3;
#endif
}

/**
 * @brief   Configures and activates the EXT peripheral.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
void ext_lld_start(EXTDriver *extp) {
  int i;

  /* Configure all pins as edge sensitive */
#if LPC11xx_EXT_USE_EXT0
  if (extp == &EXTD0) {
    LPC_GPIO0->IS = 0;
    ext_lld_exti_irq_enable(EXTI0_IRQ);
  }
#endif

#if LPC11xx_EXT_USE_EXT1
  if (extp == &EXTD1) {
    LPC_GPIO1->IS = 0;
    ext_lld_exti_irq_enable(EXTI1_IRQ);
  }
#endif

#if LPC11xx_EXT_USE_EXT2
  if (extp == &EXTD2) {
    LPC_GPIO2->IS = 0;
    ext_lld_exti_irq_enable(EXTI2_IRQ);
  }
#endif

#if LPC11xx_EXT_USE_EXT3
  if (extp == &EXTD3) {
    LPC_GPIO3->IS = 0;
    ext_lld_exti_irq_enable(EXTI3_IRQ);
  }
#endif

  /* Configuration of autostart channels.*/
  for (i = 0; i < EXT_MAX_CHANNELS; i++)
    if (extp->config->channels[i].mode & EXT_CH_MODE_AUTOSTART)
      ext_lld_channel_enable(extp, i);
    else
      ext_lld_channel_disable(extp, i);
}

/**
 * @brief   Deactivates the EXT peripheral.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
void ext_lld_stop(EXTDriver *extp) {

  LPC_GPIO_TypeDef * gp = extp->gpio;

  if (extp->state == EXT_ACTIVE) {
#if LPC11xx_EXT_USE_EXT0
    if (extp == &EXTD0) {
      ext_lld_exti_irq_disable(EXTI0_IRQ);
    }
#endif

#if LPC11xx_EXT_USE_EXT1
    if (extp == &EXTD1) {
      ext_lld_exti_irq_disable(EXTI1_IRQ);
    }
#endif

#if LPC11xx_EXT_USE_EXT2
    if (extp == &EXTD2) {
      ext_lld_exti_irq_disable(EXTI2_IRQ);
    }
#endif

#if LPC11xx_EXT_USE_EXT3
    if (extp == &EXTD3) {
      ext_lld_exti_irq_disable(EXTI3_IRQ);
    }
#endif
  }

  gp->IE = 0;
  gp->IC = 0xFFFFFFFF;
  __NOP();
  __NOP();
}

/**
 * @brief   Enables an EXT channel.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be enabled
 *
 * @notapi
 */
void ext_lld_channel_enable(EXTDriver *extp, expchannel_t channel) {

  LPC_GPIO_TypeDef * gp;

  gp = extp->gpio;
  
  /* Programming edge irq enables */
  if (extp->config->channels[channel].mode & EXT_CH_MODE_BOTH_EDGES)
    gp->IBE |=  (1 << channel);
  else {
    gp->IBE &=  ~(1 << channel);
    if (extp->config->channels[channel].mode & EXT_CH_MODE_RISING_EDGE)
      gp->IEV |= (1 << channel);
    else
      gp->IEV &= (1 << channel);
  }

  gp->IC = (1 << channel);    /* Clear interrupt on selected channel */
  __NOP();
  __NOP();

  gp->IE |= (1 << channel);  /* Interrupt on selected channel
                                    is not masked */
}

/**
 * @brief   Disables an EXT channel.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be disabled
 *
 * @notapi
 */
void ext_lld_channel_disable(EXTDriver *extp, expchannel_t channel) {

  LPC_GPIO_TypeDef * gp;

  gp = extp->gpio;

  gp->IE &= ~(1 << channel);   /* Mask interrupt on selected channel */
  gp->IC = (1 << channel);      /* Clear interrupt on selected channel */
  __NOP();
  __NOP();
}

#endif /* HAL_USE_EXT */

/** @} */