Projektstruktur.
[hackover2013-badge-firmware.git] / badge / main.c
1 /**************************************************************************/
2 /*!
3 @file main.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2011, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "projectconfig.h"
41 #include "sysinit.h"
42
43 #include "core/adc/adc.h"
44 #include "core/cpu/cpu.h"
45 #include "core/pmu/pmu.h"
46 #include "core/gpio/gpio.h"
47 #include "core/systick/systick.h"
48 #include "core/usbhid-rom/usbmsc.h"
49
50 #ifdef CFG_INTERFACE
51 #include "core/cmd/cmd.h"
52 #endif
53
54 #include "ui/display.h"
55 #include "ui/sprite.h"
56
57 #include "r0ketports.h"
58 #include "drivers/fatfs/ff.h"
59
60 enum {
61 BADGE_BTN_UP = 1,
62 BADGE_BTN_LEFT = 2,
63 BADGE_BTN_DOWN = 4,
64 BADGE_BTN_RIGHT = 8,
65 BADGE_BTN_A = 16,
66 BADGE_BTN_B = 32,
67 BADGE_BTN_MID = 64
68 };
69
70 uint8_t getInputRaw(void) {
71 uint8_t result = BTN_NONE;
72
73 if (gpioGetValue(RB_BTN3)==0) {
74 result |= BADGE_BTN_UP;
75 }
76
77 if (gpioGetValue(RB_BTN2)==0) {
78 result |= BADGE_BTN_DOWN;
79 }
80
81 if (gpioGetValue(RB_BTN4)==0) {
82 result |= BADGE_BTN_MID;
83 }
84
85 if (gpioGetValue(RB_BTN0)==0) {
86 result |= BADGE_BTN_LEFT;
87 }
88
89 if (gpioGetValue(RB_BTN1)==0) {
90 result |= BADGE_BTN_RIGHT;
91 }
92
93 if(gpioGetValue(RB_HB3) == 0) {
94 result |= BADGE_BTN_A;
95 }
96
97 if(gpioGetValue(RB_HB4) == 0 || gpioGetValue(RB_HB5) == 0) {
98 result |= BADGE_BTN_B;
99 }
100
101 return result;
102 }
103
104 void backlightInit(void) {
105 /* Enable the clock for CT16B1 */
106 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
107
108 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
109 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
110 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
111
112 /* Set default duty cycle (MR1) */
113 TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
114
115 /* External Match Register Settings for PWM */
116 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
117
118 /* enable Timer1 */
119 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
120
121 /* Enable PWM0 */
122 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;
123
124 // Enable Step-UP
125 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
126 gpioSetValue(RB_PWR_LCDBL, 0);
127 }
128
129
130
131 void rbInit() {
132 // TODO FIXME special port disable ? LEDs BTNs ?
133
134 // prepare power
135 // TODO FIXME more power init needed ? chrg + volt input ?
136 // enable external vcc
137 gpioSetDir(RB_PWR_GOOD, gpioDirection_Output);
138 gpioSetValue (RB_PWR_GOOD, 0);
139
140 // Disable USB Connect (we don't want USB by default)
141 gpioSetDir(USB_CONNECT, gpioDirection_Output);
142 gpioSetValue(USB_CONNECT, 1);
143
144 static uint8_t ports[] = { RB_BTN0, RB_BTN1, RB_BTN2, RB_BTN3, RB_BTN4,
145 RB_LED0, RB_LED1, RB_LED2,
146 RB_HB3, RB_HB4, RB_SPI_SS2,
147 RB_SPI_SS3, RB_SPI_SS4, RB_SPI_SS5,
148 RB_HB0, RB_HB1, RB_HB2,
149 RB_HB3, RB_HB4, RB_HB5};
150
151 volatile uint32_t * regs[] = {&RB_BTN0_IO, &RB_BTN1_IO, &RB_BTN2_IO,
152 &RB_BTN3_IO, &RB_BTN4_IO};
153
154 int i = 0;
155 while( i<10 ){
156 gpioSetDir(ports[i], ports[i+1], gpioDirection_Input);
157 gpioSetPullup(regs[i/2], gpioPullupMode_PullUp);
158 i+=2;
159 }
160
161 // prepate chrg_stat
162 gpioSetDir(RB_PWR_CHRG, gpioDirection_Input);
163 gpioSetPullup (&RB_PWR_CHRG_IO, gpioPullupMode_PullUp);
164
165 gpioSetDir(RB_LED3, gpioDirection_Input);
166 IOCON_PIO1_11 = 0x41;
167
168 // prepare LEDs
169 IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
170 IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
171
172 while( i<16 ){
173 gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
174 gpioSetValue (ports[i], ports[i+1], 0);
175 i+=2;
176 }
177
178 // Set LED3 to ?
179 IOCON_PIO1_11 = 0x41;
180
181 // prepare lcd
182 // TODO FIXME more init needed ?
183 gpioSetDir(RB_LCD_BL, gpioDirection_Output);
184 gpioSetValue (RB_LCD_BL, 0);
185
186 // Set P0.0 to GPIO
187 RB_PWR_LCDBL_IO&= ~RB_PWR_LCDBL_IO_FUNC_MASK;
188 RB_PWR_LCDBL_IO|= RB_PWR_LCDBL_IO_FUNC_GPIO;
189 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Input);
190 gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
191
192 // prepare SPI/SS
193 // TODO FIXME init miso/mosi/sck somehow ?
194 // prepare hackerbus
195 while(i<sizeof(ports)){
196 gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
197 gpioSetValue (ports[i], ports[i+1], 1);
198 i+=2;
199 }
200
201 backlightInit();
202 //font=&Font_7x8;
203 }
204
205
206 /**************************************************************************/
207 /*!
208 Main program entry point. After reset, normal code execution will
209 begin here.
210 */
211 /**************************************************************************/
212 int main(void)
213 {
214 // Configure cpu and mandatory peripherals
215 //systemInit();
216
217 cpuInit();
218 systickInit(CFG_SYSTICK_DELAY_IN_MS);
219
220 //pmuInit();
221 //adcInit();
222 rbInit();
223
224 badge_display_init();
225
226 gpioSetDir(0, 11, gpioDirection_Output);
227
228 // usbMSCInit();
229
230 RB_HB3_IO&= ~IOCON_PIO0_2_FUNC_MASK;
231 RB_HB3_IO|= IOCON_PIO0_2_FUNC_GPIO;
232 gpioSetDir(RB_HB3, gpioDirection_Input);
233 gpioSetPullup(&RB_HB3_IO, gpioPullupMode_PullUp);
234
235 RB_HB4_IO&= ~IOCON_PIO1_4_FUNC_MASK;
236 RB_HB4_IO|= IOCON_PIO1_4_FUNC_GPIO;
237 gpioSetDir(RB_HB4, gpioDirection_Input);
238 gpioSetPullup(&RB_HB4_IO, gpioPullupMode_PullUp);
239
240
241 badge_framebuffer fb = {
242 {
243 {
244 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
245 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
246 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
247 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
248 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
249 }, {
250 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
251 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
252 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
253 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
254 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
255 }, {
256 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
257 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
258 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
259 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
260 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
261 }, {
262 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
263 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
264 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
265 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
266 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
267 }, {
268 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
269 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
270 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
271 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
272 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
273 }, {
274 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
275 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
276 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
277 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
278 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
279 }, {
280 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
281 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
282 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
283 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
284 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
285 }, {
286 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
287 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
288 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
289 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
290 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
291 }, {
292 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
293 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
294 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
295 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
296 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
297 }
298 }
299 };
300
301 // badge_framebuffer_flush(&fb);
302
303 int res = 0;
304 FATFS fatvol;
305
306 if(FR_OK == f_mount(0, &fatvol)) {
307 FIL fil;
308 if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
309 UINT readbytes;
310
311 if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
312 gpioSetDir(RB_LED3, gpioDirection_Output);
313 gpioSetValue(RB_LED3, 1);
314 }
315
316 f_close(&fil);
317 } else {
318 fb.data[0][0] = res;
319 gpioSetDir(RB_LED1, gpioDirection_Output);
320 gpioSetValue(RB_LED1, 1);
321 }
322 } else {
323 gpioSetDir(RB_LED2, gpioDirection_Output);
324 gpioSetValue(RB_LED2, 1);
325 }
326
327 badge_framebuffer_flush(&fb);
328
329 for(uint8_t i = 0; ; ++i) {
330 gpioSetValue(0, 11, i & 1);
331
332 uint8_t buttons = getInputRaw();
333 memset(&fb, 0, sizeof(fb));
334
335 badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
336
337 if(buttons & BADGE_BTN_UP) { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
338 if(buttons & BADGE_BTN_DOWN) { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
339 if(buttons & BADGE_BTN_LEFT) { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
340 if(buttons & BADGE_BTN_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
341 if(buttons & BADGE_BTN_MID) { badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
342 if(buttons & BADGE_BTN_A) { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
343 if(buttons & BADGE_BTN_B) { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
344
345 badge_framebuffer_flush(&fb);
346
347 systickDelay(500);
348 }
349
350 return 0;
351 }
This page took 0.066098 seconds and 5 git commands to generate.