summaryrefslogtreecommitdiff
path: root/nuttx/configs/nucleus2g/src/up_leds.c
blob: fbc5a30afa09070482ff0b8bb0d1cb0a279ba98f (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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/****************************************************************************
 * configs/nucleus2g/src/up_leds.c
 * arch/arm/src/board/up_leds.c
 *
 *   Copyright (C) 2010 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <stdbool.h>
#include <debug.h>

#include <arch/board/board.h>

#include "chip.h"
#include "up_arch.h"
#include "up_internal.h"

#include "lpc17_internal.h"

#include "nucleus2g_internal.h"

#ifdef CONFIG_ARCH_LEDS

/****************************************************************************
 * Definitions
 ****************************************************************************/

#define LED_OFF        0
#define LED_ON         1
#define LED_GREEN      2
#define LED_PLUSGREEN  3
#define LED_MINUSGREEN 4
#define LED_RED        5
#define LED_PLUSRED    6
#define LED_MINUSRED   7
#define LED_NC         8
#define LED_PREV       9

/* Enables debug output from this file (needs CONFIG_DEBUG with
 * CONFIG_DEBUG_VERBOSE too)
 */

#undef LED_DEBUG   /* Define to enable debug */
#undef LED_VERBOSE /* Define to enable verbose debug */

#ifdef LED_DEBUG
#  define leddbg  lldbg
#  ifdef LED_VERBOSE
#    define ledvdbg lldbg
#  else
#    define ledvdbg(x...)
#  endif
#else
#  undef LED_VERBOSE
#  define leddbg(x...)
#  define ledvdbg(x...)
#endif

/* Dump GPIO registers */

#ifdef LED_VERBOSE
#  define led_dumpgpio(m) lpc17_dumpgpio(NUCLEUS2G_LED1_A, m)
#else
#  define led_dumpgpio(m)
#endif

/****************************************************************************
 * Private Data
 ****************************************************************************/

/* The Nucleus2G has 3 LEDs... two on the Babel CAN board and a "heartbeat" LED."
 * The LEDs on the Babel CAN board are capabl of OFF/GREEN/RED/AMBER status.
 * In normal usage, the two LEDs on the Babel CAN board would show CAN status, but if
 * CONFIG_ARCH_LEDS is defined, these LEDs will be controlled as follows for NuttX
 * debug functionality (where NC means "No Change").
 * 
 *                      LED1   LED2   HEARTBEAT
 *                    +------- ------ -----------------------
 *   LED_STARTED      | OFF    OFF    OFF
 *   LED_HEAPALLOCATE | GREEN  OFF    OFF
 *   LED_IRQSENABLED  | OFF    GREEN  OFF
 *   LED_STACKCREATED | GREEN  GREEN  OFF
 *   LED_INIRQ        | NC     NC     ON 
 *   LED_SIGNAL       | NC     RED    NC 
 *   LED_ASSERTION    | RED    NC     NC
 *   LED_PANIC        | RED    RED    NC  (1Hz flashing)
 */

static const uint8_t g_led1on[8] =
{
  LED_OFF, LED_GREEN,    LED_OFF,      LED_GREEN,
  LED_NC,  LED_NC,       LED_PLUSRED,  LED_PLUSRED
};

static const uint8_t g_led1off[8] =
{
  LED_OFF, LED_OFF,      LED_GREEN,    LED_OFF,
  LED_NC,  LED_NC,       LED_MINUSRED, LED_PREV
};

static const uint8_t g_led2on[8] =
{
  LED_OFF, LED_OFF,      LED_GREEN,    LED_GREEN,
  LED_NC,  LED_PLUSRED,  LED_NC,       LED_PLUSRED
};

static const uint8_t g_led2off[8] =
{
  LED_OFF, LED_OFF,      LED_OFF,      LED_GREEN,
  LED_NC,  LED_MINUSRED, LED_NC,       LED_PREV
};

static const uint8_t g_ledhbon[8] =
{
  LED_OFF, LED_OFF,      LED_OFF,      LED_OFF,
  LED_ON,  LED_NC,       LED_NC,       LED_NC
};

static const uint8_t g_ledhboff[8] =
{
  LED_OFF, LED_OFF,      LED_OFF,      LED_OFF,
  LED_OFF, LED_NC,       LED_NC,       LED_NC
};

static bool g_prevled1a;
static bool g_currled1a;
static bool g_prevled1b;
static bool g_currled1b;
static bool g_prevled2a;
static bool g_currled2a;
static bool g_prevled2b;
static bool g_currled2b;

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: up_led1
 ****************************************************************************/

void up_led1(uint8_t newstate)
{
  bool led1a = false;
  bool led1b = false;

  switch (newstate)
    {
      default:
      case LED_OFF:
      case LED_ON:
        break;

     case LED_GREEN:
        led1b = true;
        break;

     case LED_PLUSGREEN:
        led1b = true;
     case LED_MINUSGREEN:
        led1a = g_currled1a;
        break;

     case LED_RED:
        break;

     case LED_PLUSRED:
        led1a = true;
     case LED_MINUSRED:
        led1b = g_currled1b;
        break;

     case LED_NC:
        led1a = g_currled1a;
        led1b = g_currled1b;
        break;

     case LED_PREV:
        led1a = g_prevled1a;
        led1b = g_prevled1b;
        break;
    }
        
  lpc17_gpiowrite(NUCLEUS2G_LED1_A, led1a);
  lpc17_gpiowrite(NUCLEUS2G_LED1_B, led1b);
  g_prevled1a = g_currled1a;
  g_currled1a = led1a;
  g_prevled1b = g_currled1b;
  g_currled1b = led1b;
}

/****************************************************************************
 * Name: up_led2
 ****************************************************************************/

void up_led2(uint8_t newstate)
{
  bool led2a = false;
  bool led2b = false;

  switch (newstate)
    {
      default:
      case LED_OFF:
      case LED_ON:
        break;

     case LED_GREEN:
        led2b = true;
        break;

     case LED_PLUSGREEN:
        led2b = true;
     case LED_MINUSGREEN:
        led2a = g_currled2a;
        break;

     case LED_RED:
        break;

     case LED_PLUSRED:
        led2a = true;
     case LED_MINUSRED:
        led2b = g_currled2b;
        break;

     case LED_NC:
        led2a = g_currled2a;
        led2b = g_currled2b;
        break;

     case LED_PREV:
        led2a = g_prevled2a;
        led2b = g_prevled2b;
        break;
    }
        
  lpc17_gpiowrite(NUCLEUS2G_LED2_A, led2a);
  lpc17_gpiowrite(NUCLEUS2G_LED2_B, led2b);
  g_prevled2a = g_currled2a;
  g_currled2a = led2a;
  g_prevled2b = g_currled2b;
  g_currled2b = led2b;
}

/****************************************************************************
 * Name: up_ledhb
 ****************************************************************************/

void up_ledhb(uint8_t newstate)
{
  bool ledhb = false;

  switch (newstate)
    {
      default:
      case LED_OFF:
        break;

      case LED_ON:
        ledhb = true;
        break;

      case LED_NC:
        return;
    }

  lpc17_gpiowrite(NUCLEUS2G_HEARTBEAT, ledhb);
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: up_ledinit
 ****************************************************************************/

void up_ledinit(void)
{
  /* Configure all LED GPIO lines */

  led_dumpgpio("up_ledinit() Entry)");

  lpc17_configgpio(NUCLEUS2G_LED1_A);
  lpc17_configgpio(NUCLEUS2G_LED1_B);
  lpc17_configgpio(NUCLEUS2G_LED2_A);
  lpc17_configgpio(NUCLEUS2G_LED2_B);
  lpc17_configgpio(NUCLEUS2G_HEARTBEAT);
  lpc17_configgpio(NUCLEUS2G_EXTRA_LED);

  led_dumpgpio("up_ledinit() Exit");
}

/****************************************************************************
 * Name: up_ledon
 ****************************************************************************/

void up_ledon(int led)
{
  up_led1(g_led1on[led]);
  up_led2(g_led2on[led]);
  up_ledhb(g_ledhbon[led]);

#ifdef LED_VERBOSE
  if (led != LED_INIRQ)
    {
      ledvdbg("led: %d LED1: %d LED2: %d HB: %d\n",
              led, g_led1on[led], g_led2on[led], g_ledhbon[led]);
      ledvdbg("LED1: {(%d,%d), (%d,%d)} LED2: {(%d,%d), (%d,%d)}\n",
              g_prevled1a, g_currled1a, g_prevled1b, g_currled1b,
              g_prevled2a, g_currled2a, g_prevled2b, g_currled2b);
    }
#endif
}

/****************************************************************************
 * Name: up_ledoff
 ****************************************************************************/

void up_ledoff(int led)
{
  up_led1(g_led1off[led]);
  up_led2(g_led2off[led]);
  up_ledhb(g_ledhboff[led]);

#ifdef LED_VERBOSE
  if (led != LED_INIRQ)
    {
      ledvdbg("led: %d LED1: %d LED2: %d HB: %d\n",
              led, g_led1off[led], g_led2off[led], g_ledhboff[led]);
      ledvdbg("LED1: {(%d,%d), (%d,%d)} LED2: {(%d,%d), (%d,%d)}\n",
              g_prevled1a, g_currled1a, g_prevled1b, g_currled1b,
              g_prevled2a, g_currled2a, g_prevled2b, g_currled2b);
    }
#endif
}

#endif /* CONFIG_ARCH_LEDS */