d72e643ab673a1b817d751ef5fe5deee7e71098d
[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/wdt/wdt.h"
47 #include "core/gpio/gpio.h"
48 #include "core/systick/systick.h"
49 #include "core/usbhid-rom/usbmsc.h"
50
51 #ifdef CFG_INTERFACE
52 #include "core/cmd/cmd.h"
53 #endif
54
55 #include "ui/display.h"
56 #include "ui/sprite.h"
57 #include "ui/event.h"
58 #include "jumpnrun/jumpnrun.h"
59
60 #include "r0ketports.h"
61 #include "drivers/fatfs/ff.h"
62
63 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof*(arr))
64
65 void backlightInit(void) {
66 #if HW_IS_PROTOTYPE
67
68
69 // prototype uses WDT CLKOUT to drive the LCD backlight
70 // init without a reset
71
72 IOCON_PIO0_1 &= ~(IOCON_PIO0_1_FUNC_MASK);
73 IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CLKOUT;
74
75 wdtInit(false);
76
77 // use the WDT clock as the default WDT frequency is best frequency
78 // XXX this register value is missing in lpc1343.h
79 #define SCB_CLKOUTCLKSEL_SOURCE_WDTCLOCK ((unsigned int) 0x00000002) // Use the WDT clock
80
81 SCB_CLKOUTCLKSEL = SCB_CLKOUTCLKSEL_SOURCE_WDTCLOCK;
82
83 // toggle from LOW to HIGH to update source
84 SCB_CLKOUTCLKUEN = SCB_CLKOUTCLKUEN_DISABLE;
85 SCB_CLKOUTCLKUEN = SCB_CLKOUTCLKUEN_UPDATE;
86
87 // divide by 30, to get almost 10 kHz
88 SCB_CLKOUTCLKDIV = 30;
89
90
91 #else
92
93
94 /* Enable the clock for CT16B1 */
95 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
96
97 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
98 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
99 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
100
101 /* Set default duty cycle (MR1) */
102 TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
103
104 /* External Match Register Settings for PWM */
105 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
106
107 /* enable Timer1 */
108 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
109
110 /* Enable PWM0 */
111 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;
112
113 // Enable Step-UP
114 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
115 gpioSetValue(RB_PWR_LCDBL, 0);
116
117
118 #endif
119 }
120
121 void rbInit() {
122 #if !HW_IS_PROTOTYPE
123 RB_HB0_IO &= ~IOCON_SWDIO_PIO1_3_FUNC_MASK;
124 RB_HB0_IO |= IOCON_SWDIO_PIO1_3_FUNC_GPIO;
125 RB_HB1_IO &= ~IOCON_JTAG_TCK_PIO0_10_FUNC_MASK;
126 RB_HB1_IO |= IOCON_JTAG_TCK_PIO0_10_FUNC_GPIO;
127 #endif
128
129 struct {
130 int port;
131 int pin;
132 uint32_t volatile *reg;
133 gpioPullupMode_t mode;
134 } const input_pins[] = {
135 #if HW_IS_PROTOTYPE
136 { RB_BTN0 , &RB_BTN0_IO , gpioPullupMode_PullDown },
137 { RB_BTN1 , &RB_BTN1_IO , gpioPullupMode_PullDown },
138 { RB_BTN2 , &RB_BTN2_IO , gpioPullupMode_PullDown },
139 { RB_BTN3 , &RB_BTN3_IO , gpioPullupMode_PullDown },
140 { RB_BTN4 , &RB_BTN4_IO , gpioPullupMode_PullDown },
141 { RB_BTN_A , &RB_BTN_A_IO , gpioPullupMode_PullDown },
142 { RB_BTN_B , &RB_BTN_B_IO , gpioPullupMode_PullDown },
143 #else
144 { RB_BTN0 , &RB_BTN0_IO , gpioPullupMode_PullUp },
145 { RB_BTN1 , &RB_BTN1_IO , gpioPullupMode_PullUp },
146 { RB_BTN2 , &RB_BTN2_IO , gpioPullupMode_PullUp },
147 { RB_BTN3 , &RB_BTN3_IO , gpioPullupMode_PullUp },
148 { RB_BTN4 , &RB_BTN4_IO , gpioPullupMode_PullUp },
149 { RB_HB0 , &RB_HB0_IO , gpioPullupMode_PullUp },
150 { RB_HB1 , &RB_HB1_IO , gpioPullupMode_PullUp },
151 { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullUp }
152 #endif
153 };
154
155 for(int i = 0; i < ARRAY_SIZE(input_pins); ++i) {
156 gpioSetDir(input_pins[i].port, input_pins[i].pin, gpioDirection_Input);
157 gpioSetPullup(input_pins[i].reg, input_pins[i].mode);
158 }
159
160 #if HW_IS_PROTOTYPE
161 IOCON_JTAG_TMS_PIO1_0 &= ~(IOCON_JTAG_TMS_PIO1_0_FUNC_MASK);
162 IOCON_JTAG_TMS_PIO1_0 |= IOCON_JTAG_TMS_PIO1_0_FUNC_GPIO;
163 #else
164 // LED3 zur Bestimmung der Umgebungshelligkeit.
165 gpioSetDir(RB_LED3, gpioDirection_Input);
166 RB_LED3_IO = (RB_LED3_IO & IOCON_PIO1_11_FUNC_MASK) | IOCON_PIO1_11_FUNC_AD7;
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 #endif
172
173 struct {
174 int port;
175 int pin;
176 int value;
177 } const output_pins[] = {
178 { RB_PWR_GOOD, 0 },
179 { USB_CONNECT, 1 },
180 { RB_LCD_CS , 1 },
181 #if !HW_IS_PROTOTYPE
182 { RB_SPI_SS2 , 1 },
183 { RB_SPI_SS3 , 1 },
184 { RB_SPI_SS4 , 1 },
185 { RB_SPI_SS5 , 1 },
186 { RB_LED0 , 0 },
187 { RB_LED1 , 0 },
188 { RB_LED2 , 0 },
189 { RB_LCD_BL , 0 },
190 { RB_HB2 , 1 },
191 { RB_HB3 , 1 },
192 { RB_HB4 , 1 },
193 { RB_HB5 , 1 }
194 #endif
195 };
196
197 for(int i = 0; i < ARRAY_SIZE(output_pins); ++i) {
198 gpioSetDir (output_pins[i].port, output_pins[i].pin, gpioDirection_Output);
199 gpioSetValue(output_pins[i].port, output_pins[i].pin, output_pins[i].value);
200 }
201
202 #if !HW_IS_PROTOTYPE
203 // Set P0.0 to GPIO
204 RB_PWR_LCDBL_IO &= ~RB_PWR_LCDBL_IO_FUNC_MASK;
205 RB_PWR_LCDBL_IO |= RB_PWR_LCDBL_IO_FUNC_GPIO;
206
207 gpioSetDir ( RB_PWR_LCDBL , gpioDirection_Input);
208 gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
209 #endif
210
211 backlightInit();
212 }
213
214
215 /**************************************************************************/
216 /*!
217 Main program entry point. After reset, normal code execution will
218 begin here.
219 */
220 /**************************************************************************/
221 int main(void)
222 {
223 // Configure cpu and mandatory peripherals
224 //systemInit();
225
226 cpuInit();
227 systickInit(CFG_SYSTICK_DELAY_IN_MS);
228
229 // pmuInit();
230 // adcInit();
231 rbInit();
232
233 // usbMSCInit();
234
235 badge_display_init();
236
237 /*
238 gpioSetDir(0, 11, gpioDirection_Output);
239 gpioSetValue(0, 11, 0);
240
241
242 {
243 badge_framebuffer fb;
244 int res = 0;
245 FATFS fatvol;
246
247 if(FR_OK == f_mount(0, &fatvol)) {
248 FIL fil;
249 if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
250 UINT readbytes;
251
252 if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
253 gpioSetDir(RB_LED3, gpioDirection_Output);
254 gpioSetValue(RB_LED3, 1);
255 }
256
257 f_close(&fil);
258 } else {
259 fb.data[0][0] = res;
260 gpioSetDir(RB_LED1, gpioDirection_Output);
261 gpioSetValue(RB_LED1, 1);
262 }
263 } else {
264 gpioSetDir(RB_LED2, gpioDirection_Output);
265 gpioSetValue(RB_LED2, 1);
266 }
267
268 badge_framebuffer_flush(&fb);
269 }
270 */
271
272 badge_event_start();
273 /*
274 for(;;) {
275 if(JUMPNRUN_ERROR == jumpnrun_play("smb.lvl")) {
276 break;
277 }
278 }
279 */
280
281 uint8_t buttons = 0;
282
283 for(uint8_t i = 0; ; ++i) {
284 badge_event_t event = badge_event_wait();
285
286 // SCB_CLKOUTCLKDIV = i;
287
288 switch(badge_event_type(event)) {
289 case BADGE_EVENT_USER_INPUT: {
290 buttons = badge_event_current_input_state();
291 break;
292 }
293 case BADGE_EVENT_GAME_TICK: {
294 badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
295 badge_framebuffer fb = { { { 0xcc } } };
296
297 /*
298 for(int i = 0; i < 9 * 96; ++i) {
299 fb.data[0][i] = 0xbb;
300 }
301 */
302
303 if(buttons & BADGE_EVENT_KEY_UP) { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
304 if(buttons & BADGE_EVENT_KEY_DOWN) { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
305 if(buttons & BADGE_EVENT_KEY_LEFT) { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
306 if(buttons & BADGE_EVENT_KEY_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
307 if(buttons & BADGE_EVENT_KEY_CENTER){ badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
308 if(buttons & BADGE_EVENT_KEY_BTN_A) { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
309 if(buttons & BADGE_EVENT_KEY_BTN_B) { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
310
311 badge_framebuffer_flush(&fb);
312 break;
313 }
314 }
315 }
316
317 return 0;
318 }
This page took 0.065953 seconds and 3 git commands to generate.