Update preset.
[bertos.git] / boards / arduino-mega / hw / hw_led_7seg.h
1 /**
2  * \file hw_led_7seg.h
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief led 7 segment display low-level
33  *
34  * This file has the functions that must be
35  * implemented to drive the 7 segments display by your
36  * hardware
37  *
38  * \author Fabio Bizzi <fbizzi@bizzi.org>
39  *
40  * \addtogroup SevenSegDisplay 7 Segments LED Displays Driver
41  * \{
42  *
43  * Example implementation for AtMEGA 1280
44  * (Arduino MEGA) with a 4 digit display.
45  * We use PORTA to connect the 8 pins of
46  * the 7 segments display and 4 bit of
47  * PORTC to select which digit of the
48  * display we have to write on.
49  *
50  * \code
51  *  7 Seg LED Pin
52  *  ----------------
53  *  LED SEGMENT
54  *  ----------------
55  *  DP G F E D C B A
56  *  7  6 5 4 3 2 1 0
57  *  ----------------
58  *  PORT A Pin
59  *  ----------------
60  *
61  *  7 Seg Display Selection
62  *  ----------------
63  *  Display Nr.
64  *  ----------------
65  *  N N N N 3 2 1 0
66  *  7 6 5 4 3 2 1 0
67  *  ----------------
68  *  PORT C Pin
69  *  ----------------
70  * \endcode
71  *
72  * The implementation of the sseg_on procedure that set the PROPER PIN of PORT C
73  * to enable the requested digit of the display, after write the encoded character
74  * to PORT A
75  *
76  * \code
77  *
78  * INLINE void sseg_on(uint8_t dgt, uint8_t n_dgt)
79  * {
80  *  switch (n_dgt)
81  *  {
82  * //Common Cathode
83  * #ifdef CONFIG_LED_7SEG_CCAT
84  *
85  *              case 0:
86  *                      PORTC &= ~(BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
87  *                      PORTC |= BV(PORTC0);
88  *                      break;
89  *              case 1:
90  *                      PORTC &= ~(BV(PORTC0) | BV(PORTC2) | BV(PORTC3));
91  *                      PORTC |= BV(PORTC1);
92  *                      break;
93  *              case 2:
94  *                      PORTC &= ~(BV(PORTC0) | BV(PORTC1) | BV(PORTC3));
95  *                      PORTC |= BV(PORTC2);
96  *                      break;
97  *              case 3:
98  *                      PORTC &= ~(BV(PORTC0) | BV(PORTC1) | BV(PORTC2));
99  *                      PORTC |= BV(PORTC3);
100  *                      break;
101  *
102  * //Common Anode
103  * #else
104  *
105  *              case 0:
106  *                      PORTC |= (BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
107  *                      PORTC &= ~(BV(PORTC0));
108  *                      break;
109  *              case 1:
110  *                      PORTC |= (BV(PORTC0) | BV(PORTC2) | BV(PORTC3));
111  *                      PORTC &= ~(BV(PORTC1));
112  *                      break;
113  *              case 2:
114  *                      PORTC |= (BV(PORTC0) | BV(PORTC1) | BV(PORTC3));
115  *                      PORTC &= ~(BV(PORTC2));
116  *                      break;
117  *              case 3:
118  *                      PORTC |= (BV(PORTC0) | BV(PORTC1) | BV(PORTC2));
119  *                      PORTC &= ~(BV(PORTC3));
120  *                      break;
121  *
122  * #endif
123  *
124  *      }
125  *      //Write the charater
126  *      PORTA = dgt;
127  * }
128  *
129  * \endcode
130  *
131  * The implementation of the sseg_init procedure that set the DIRECTION of PORT C
132  * and PORT A to output
133  *
134  * \code
135  *
136  * INLINE void sseg_init(void)
137  * {
138  * //Initialize PIN Direction to OUTPUT
139  *      DDRA = 0xFF;
140  *      DDRC |= (BV(DDC0) | BV(DDC1) | BV(DDC2) | BV(DDC3));
141  *      //Set the display OFF
142  *      SSEG_OFF();
143  * }
144  *
145  * \endcode
146  *
147  * The implementation of the sseg_off procedure that set the reset PORT A
148  * to clean the display and turn off all the pin of PORT C that drive the
149  * display's digits
150  *
151  * \code
152  *
153  * INLINE void sseg_off(void)
154  * {
155  * //Set the display OFF
156  * //Common Cathode
157  * #ifdef CONFIG_LED_7SEG_CCAT
158  *    PORTA = 0x00;
159  *    PORTC &= ~(BV(PORTC0) | BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
160  * //Common Anode
161  * #else
162  *    PORTA = 0xFF;
163  *    PORTC |= (BV(PORTC0) | BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
164  * #endif
165  *
166  * }
167  *
168  * \endcode
169  *
170  */
171
172 #ifndef HW_LED_7SEG_H
173 #define HW_LED_7SEG_H
174
175 #include "cfg/cfg_led_7seg.h"
176 #include "cfg/cfg_arch.h"
177 #include <cpu/types.h>
178 #include <cpu/irq.h>
179 #include <drv/timer.h>
180 #include <string.h>
181
182 /*
183  * INLINE HW Functions
184  */
185
186 /**
187  * \brief Clean the display
188  *
189  * This is the procedure that clean the display in HW mode.
190  * you have to write it according with your hardware and micro.
191  */
192 INLINE void sseg_off(void)
193 {
194 /* Example implementation for AtMEGA 1280
195  * (Arduino MEGA) with a 4 digit display.
196  * We use PORTA to connect the 8 pins of
197  * the 7 segments display and 4 bit of
198  * PORTC to select which digit of the
199  * display we have to write on.
200  */
201
202         /* Set the display OFF */
203 /* Common Cathode */
204 #ifdef CONFIG_LED_7SEG_CCAT
205         PORTA = 0x00;
206         PORTC &= ~(BV(PORTC0) | BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
207 /* Common Anode */
208 #else
209         PORTA = 0xFF;
210         PORTC |= (BV(PORTC0) | BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
211 #endif
212 }
213
214 /**
215  * \brief writes the character to the single digit of the display
216  *
217  * This is the procedure that writes the character to the single digit
218  * of the display, you have to write it according with your hardware and micro.
219  *
220  *      \param dgt the character that has to be displayed
221  *      \param n_dgt the digit where to disply the character of the display's digits.
222  */
223 INLINE void sseg_on(uint8_t dgt, uint8_t n_dgt)
224 {
225 /* Example implementation for AtMEGA 1280
226  * (Arduino MEGA) with a 4 digit display.
227  * We use PORTA to connect the 8 pins of
228  * the 7 segments display and 4 bit of
229  * PORTC to select which digit of the
230  * display we have to write on.
231  *
232  * 7 Seg LED Pin
233  * ----------------
234  * DP G F E D C B A
235  * 7  6 5 4 3 2 1 0
236  * ----------------
237  * PORT A Pin
238  *
239  */
240         switch (n_dgt)
241         {
242 /* Common Cathode */
243 #ifdef CONFIG_LED_7SEG_CCAT
244
245                 case 0:
246                         PORTC &= ~(BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
247                         PORTC |= BV(PORTC0);
248                         break;
249                 case 1:
250                         PORTC &= ~(BV(PORTC0) | BV(PORTC2) | BV(PORTC3));
251                         PORTC |= BV(PORTC1);
252                         break;
253                 case 2:
254                         PORTC &= ~(BV(PORTC0) | BV(PORTC1) | BV(PORTC3));
255                         PORTC |= BV(PORTC2);
256                         break;
257                 case 3:
258                         PORTC &= ~(BV(PORTC0) | BV(PORTC1) | BV(PORTC2));
259                         PORTC |= BV(PORTC3);
260                         break;
261
262 /* Common Anode */
263 #else
264
265                 case 0:
266                         PORTC |= (BV(PORTC1) | BV(PORTC2) | BV(PORTC3));
267                         PORTC &= ~(BV(PORTC0));
268                         break;
269                 case 1:
270                         PORTC |= (BV(PORTC0) | BV(PORTC2) | BV(PORTC3));
271                         PORTC &= ~(BV(PORTC1));
272                         break;
273                 case 2:
274                         PORTC |= (BV(PORTC0) | BV(PORTC1) | BV(PORTC3));
275                         PORTC &= ~(BV(PORTC2));
276                         break;
277                 case 3:
278                         PORTC |= (BV(PORTC0) | BV(PORTC1) | BV(PORTC2));
279                         PORTC &= ~(BV(PORTC3));
280                         break;
281
282 #endif
283         }
284         /* Write the charater */
285         PORTA = dgt;
286 }
287
288 /**
289  * \brief initalize the HW regsiters
290  *
291  * This is the procedure that initalize the HW regsiters.
292  * you have to write it according with your hardware and micro.
293  */
294 INLINE void sseg_init(void)
295 {
296 /* Example implementation for AtMEGA 1280
297  * (Arduino MEGA) with a 4 digit display.
298  * We use PORTA to connect the 8 pins of
299  * the 7 segments display and 4 bit of
300  * PORTC to select which digit of the
301  * display we have to write on.
302  */
303
304         /* Initialize PIN Direction to OUTPUT*/
305         DDRA = 0xFF;
306         DDRC |= (BV(DDC0) | BV(DDC1) | BV(DDC2) | BV(DDC3));
307         /* Set the display OFF */
308         sseg_off();
309 }
310
311 #endif /* HW_LED_7SEG_H */
312
313  /** \} */ //defgroup drivers