summaryrefslogtreecommitdiff
path: root/nuttx/configs/z8encore000zco/src/z8_leds.c
blob: c6f7f1819ed8512542edb33cae60120056a2b4ef (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
/****************************************************************************
 * configs/z8encore000zco/src/z8_leds.c
 *
 *   Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * 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.
 *
 ****************************************************************************/

/* The z16f2800100zcog board has four LEDs:
 *
 * - Green LED D1 which illuminates in the presence of Vcc
 * - Red LED D2 connected to chip port PA0_T0IN
 * - Yellow LED D3 connected to chip port PA1_T0OUT
 * - Green LED D4 connected to chip port PA2_DE0
 */

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

#include <nuttx/config.h>
#include <stdint.h>
#include <arch/board/board.h>
#include "up_internal.h"

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

/* Port G:  Anode Bit Assignments (1 enables) */

#define Z8_PORTG_ANODE_MASK      0x7f
#define Z8_PORTG_ANODE_ROW0      0x01
#define Z8_PORTG_ANODE_ROW1      0x02
#define Z8_PORTG_ANODE_ROW2      0x04
#define Z8_PORTG_ANODE_ROW3      0x08
#define Z8_PORTG_ANODE_ROW4      0x10
#define Z8_PORTG_ANODE_ROW5      0x20
#define Z8_PORTG_ANODE_ROW6      0x40

/* Port E: Cathode Bit Assignements (0 enables) */

#define Z8_PORTE_CATHODE_MASK    0x1f
#define Z8_PORTE_CATHODE_COLUMN0 0x01
#define Z8_PORTE_CATHODE_COLUMN1 0x02
#define Z8_PORTE_CATHODE_COLUMN2 0x04
#define Z8_PORTE_CATHODE_COLUMN3 0x08
#define Z8_PORTE_CATHODE_COLUMN4 0x10

/* Port E: LED Addressing */

#define Z8_PORTE_LED_MASK        0xe0
#define Z8_PORTE_LED_D3          0x20
#define Z8_PORTE_LED_D4          0x40
#define Z8_PORTE_LED_D1          0x80

/* Port G: LED Addressing */

#define Z8_PORTG_LED_MASK        0x80
#define Z8_PORTG_LED_D2          0x80

/* Special values for display */

#define LED_ALLON                { Z8_PORTG_ANODE_MASK, 0x00 }
#define LED_ALLOFF               { 0x00, Z8_PORTE_CATHODE_MASK }
#define LED_LEVEL1               { 0x10, 0x00 }
#define LED_LEVEL2               { 0x08, 0x00 }
#define LED_LEVEL3               { 0x04, 0x00 }
#define LED_LEVEL4               { 0x02, 0x00 }
#define LED_LEVEL1I              { 0x14, 0x00 }
#define LED_LEVEL2I              { 0x0c, 0x00 }
#define LED_LEVEL3I              { 0x04, 0x00 }
#define LED_LEVEL4I              { 0x06, 0x00 }
#define LED_LEVEL1S              { 0x11, 0x00 }
#define LED_LEVEL2S              { 0x09, 0x00 }
#define LED_LEVEL3S              { 0x05, 0x00 }
#define LED_LEVEL4S              { 0x03, 0x00 }
#define LED_LEVEL1A              { 0x10, 0x11 }
#define LED_LEVEL2A              { 0x08, 0x11 }
#define LED_LEVEL3A              { 0x04, 0x11 }
#define LED_LEVEL4A              { 0x02, 0x11 }
#define LED_SNAKEEYES            { 0x06, 0x11 }

/****************************************************************************
 * Private Types
 ****************************************************************************/

struct z8_ledbits_s
{
  uint8_t anode;
  uint8_t cathode;
};

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

static const struct z8_ledbits_s g_ledarray[10][4] =
{
  { LED_ALLON,     LED_ALLON,     LED_ALLON,     LED_ALLON     },
  { LED_ALLOFF,    LED_ALLOFF,    LED_ALLOFF,    LED_ALLOFF    },
  { LED_LEVEL1,    LED_ALLOFF,    LED_ALLOFF,    LED_ALLOFF    },
  { LED_LEVEL1,    LED_LEVEL2,    LED_ALLOFF,    LED_ALLOFF    },
  { LED_LEVEL1,    LED_LEVEL2,    LED_LEVEL3,    LED_ALLOFF    },
  { LED_LEVEL1,    LED_LEVEL2,    LED_LEVEL3,    LED_LEVEL4    },
  { LED_LEVEL1I,   LED_LEVEL2I,   LED_LEVEL3I,   LED_LEVEL4I   },
  { LED_LEVEL1S,   LED_LEVEL2S,   LED_LEVEL3S,   LED_LEVEL4S   },
  { LED_LEVEL1A,   LED_LEVEL2A,   LED_LEVEL3A,   LED_LEVEL4A   },
  { LED_SNAKEEYES, LED_SNAKEEYES, LED_SNAKEEYES, LED_SNAKEEYES }
};

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

/****************************************************************************
 * Name: z8_putled134
 ****************************************************************************/

#ifdef CONFIG_ARCH_LEDS
static void z8_putled134(FAR const struct z8_ledbits_s *bits, uint8_t addr)
{
  uint8_t porte;
  
  porte = bits->cathode;
  putreg8(porte, PEOD);          /* Load porte data */
  putreg8(bits->anode, PGOD);    /* Load portg data */

  porte |= addr;
  putreg8(porte, PEOD);          /* Latch data */
}
#endif

/****************************************************************************
 * Name: z8_lputed2
 ****************************************************************************/

#ifdef CONFIG_ARCH_LEDS
static void z8_putled2(FAR const struct z8_ledbits_s *bits, uint8_t addr)
{
  uint8_t portg;

  putreg8(bits->cathode, PEOD);  /* Load porte data */
  portg = bits->anode;
  putreg8(porte, PGOD);          /* Load portg data */

  portg |= addr;
  putreg8(portg, PGOD);          /* Latch data */
}
#endif

/****************************************************************************
 * Name: z8_putarray
 ****************************************************************************/

#ifdef CONFIG_ARCH_LEDS
static void z8_putarray(FAR const struct z8_ledarray_s *array)
{
  z8_putled134(&array->led[0], Z8_PORTE_LED_D1);
  z8_putled2(&array->led[1], Z8_PORTG_LED_D2);
  z8_putled134(&array->led[2], Z8_PORTE_LED_D3);
  z8_putled134(&array->led[3], Z8_PORTE_LED_D4);
}
#endif

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

/****************************************************************************
 * Name: board_led_initialize
 ****************************************************************************/

#ifdef CONFIG_ARCH_LEDS
void board_led_initialize(void)
{
  putreg8(0x00, PEAF);          /* PE Alt func = Port */
  putreg8(0x00, PGAF);          /* PG Alt func = Port */
                      
  putreg8(0x00, PEOC);          /* PE Out Ctrl = push-pull */
  putreg8(0x00, PGOC);          /* PG Out Ctrl = push-pull */

/*putreg8(0x00, PEDD);           * PA Data Dir = output */
  putreg8(0x01, PEADDR);        /* PA Data Dir = output */
  putreg8(0x00, PECTL);         /* OUTPUT */
/*putreg8(0x00, PGDD);           * PA Data Dir = output */
  putreg8(0x01, PGADDR);        /* PA Data Dir = output */
  putreg8(0x00, PGCTL);         /* OUTPUT */
  
  z8_putarray(&g_ledarray[0][0]);
}

/****************************************************************************
 * Name: board_led_on
 ****************************************************************************/

void board_led_on(int led)
{
  if ((unsigned)led <= 8)
    {
      z8_putarray(&g_ledarray[led+1][0]);
    }
}

/****************************************************************************
 * Name: board_led_off
 ****************************************************************************/

void board_led_off(int led)
{
  if (led >= 1)
    {
      board_led_on(led-1);
    }
}
#endif /* CONFIG_ARCH_LEDS */