156d8e2b4822159a70977f92ee0c8dfb6c8f1140
[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/ssp/ssp.h"
49 #include "core/systick/systick.h"
50 #include "core/usbhid-rom/usbmsc.h"
51
52 #ifdef CFG_INTERFACE
53 #include "core/cmd/cmd.h"
54 #endif
55
56 #include "init.h"
57 #include "ui/display.h"
58 #include "ui/sprite.h"
59 #include "ui/event.h"
60 #include "util/util.h"
61 #include "jumpnrun/jumpnrun.h"
62
63 #include "dataflash/at45db041d.h"
64
65 #include "drivers/fatfs/ff.h"
66
67 #ifdef R0KET
68
69 #include "r0ketports.h"
70 void rbBacklightInit(void) {
71 /* Enable the clock for CT16B1 */
72 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
73
74 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
75 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
76 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
77
78 /* Set default duty cycle (MR1) */
79 TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
80
81 /* External Match Register Settings for PWM */
82 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
83
84 /* enable Timer1 */
85 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
86
87 /* Enable PWM0 */
88 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;
89
90 // Enable Step-UP
91 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
92 gpioSetValue(RB_PWR_LCDBL, 0);
93 }
94
95 void rbInit() {
96 RB_HB0_IO &= ~IOCON_SWDIO_PIO1_3_FUNC_MASK;
97 RB_HB0_IO |= IOCON_SWDIO_PIO1_3_FUNC_GPIO;
98 RB_HB1_IO &= ~IOCON_JTAG_TCK_PIO0_10_FUNC_MASK;
99 RB_HB1_IO |= IOCON_JTAG_TCK_PIO0_10_FUNC_GPIO;
100
101 struct {
102 int port;
103 int pin;
104 uint32_t volatile *reg;
105 gpioPullupMode_t mode;
106 } const input_pins[] = {
107 #ifdef HOB_REV2
108 { RB_BTN0 , &RB_BTN0_IO , gpioPullupMode_PullDown },
109 { RB_BTN1 , &RB_BTN1_IO , gpioPullupMode_PullDown },
110 { RB_BTN2 , &RB_BTN2_IO , gpioPullupMode_PullDown },
111 { RB_BTN3 , &RB_BTN3_IO , gpioPullupMode_PullDown },
112 { RB_BTN4 , &RB_BTN4_IO , gpioPullupMode_PullDown },
113 { RB_HB0 , &RB_HB0_IO , gpioPullupMode_PullDown },
114 { RB_HB1 , &RB_HB1_IO , gpioPullupMode_PullDown },
115 { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullDown }
116 #else
117 { RB_BTN0 , &RB_BTN0_IO , gpioPullupMode_PullUp },
118 { RB_BTN1 , &RB_BTN1_IO , gpioPullupMode_PullUp },
119 { RB_BTN2 , &RB_BTN2_IO , gpioPullupMode_PullUp },
120 { RB_BTN3 , &RB_BTN3_IO , gpioPullupMode_PullUp },
121 { RB_BTN4 , &RB_BTN4_IO , gpioPullupMode_PullUp },
122 { RB_HB0 , &RB_HB0_IO , gpioPullupMode_PullUp },
123 { RB_HB1 , &RB_HB1_IO , gpioPullupMode_PullUp },
124 { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullUp }
125 #endif
126 };
127
128 for(int i = 0; i < ARRAY_SIZE(input_pins); ++i) {
129 gpioSetDir(input_pins[i].port, input_pins[i].pin, gpioDirection_Input);
130 gpioSetPullup(input_pins[i].reg, input_pins[i].mode);
131 }
132
133 // LED3 zur Bestimmung der Umgebungshelligkeit.
134 gpioSetDir(RB_LED3, gpioDirection_Input);
135 RB_LED3_IO = (RB_LED3_IO & IOCON_PIO1_11_FUNC_MASK) | IOCON_PIO1_11_FUNC_AD7;
136
137 // prepare LEDs
138 IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
139 IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
140
141 struct {
142 int port;
143 int pin;
144 int value;
145 } const output_pins[] = {
146 { RB_PWR_GOOD , 0 },
147 { USB_CONNECT , 1 },
148 { RB_LCD_CS , 1 },
149 { RB_SPI_CS_DF, 1 },
150 { RB_SPI_SS2 , 1 },
151 { RB_SPI_SS3 , 1 },
152 { RB_SPI_SS4 , 1 },
153 { RB_SPI_SS5 , 1 },
154 { RB_LED0 , 0 },
155 { RB_LED1 , 0 },
156 { RB_LED2 , 0 },
157 { RB_LCD_BL , 0 },
158 { RB_HB2 , 1 },
159 { RB_HB3 , 1 },
160 { RB_HB4 , 1 },
161 { RB_HB5 , 1 }
162 };
163
164 for(int i = 0; i < ARRAY_SIZE(output_pins); ++i) {
165 gpioSetDir (output_pins[i].port, output_pins[i].pin, gpioDirection_Output);
166 gpioSetValue(output_pins[i].port, output_pins[i].pin, output_pins[i].value);
167 }
168
169 // Set P0.0 to GPIO
170 RB_PWR_LCDBL_IO &= ~RB_PWR_LCDBL_IO_FUNC_MASK;
171 RB_PWR_LCDBL_IO |= RB_PWR_LCDBL_IO_FUNC_GPIO;
172
173 gpioSetDir ( RB_PWR_LCDBL , gpioDirection_Input);
174 gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
175
176 rbBacklightInit();
177 badge_display_init();
178 }
179
180 #endif
181
182 /**************************************************************************/
183 /*!
184 Main program entry point. After reset, normal code execution will
185 begin here.
186 */
187 /**************************************************************************/
188 int main(void)
189 {
190 // Configure cpu and mandatory peripherals
191 //systemInit();
192
193 cpuInit();
194 systickInit(CFG_SYSTICK_DELAY_IN_MS);
195
196 // pmuInit();
197 // adcInit();
198 #ifdef R0KET
199 rbInit();
200 #else
201 badge_init();
202 #endif
203
204 {
205 // f_mkfs(0, 1, 0);
206 badge_framebuffer fb;
207 int res = 0;
208 FATFS fatvol;
209
210 while(FR_OK != f_mount(0, &fatvol)) {
211 f_mkfs(0, 1, 0);
212 }
213
214 FIL fil;
215 if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
216 UINT readbytes;
217
218 if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
219 }
220
221 f_close(&fil);
222 }
223
224 badge_framebuffer_flush(&fb);
225 }
226
227 if(badge_input_raw() & BADGE_EVENT_KEY_DOWN) {
228 usbMSCInit();
229 for(;;);
230 }
231
232 badge_event_start();
233
234 for(;;) {
235 if(JUMPNRUN_ERROR == jumpnrun_play("smb.lvl")) {
236 break;
237 }
238 }
239
240 uint8_t buttons = 0;
241
242 for(uint8_t i = 0; ; ++i) {
243 badge_event_t event = badge_event_wait();
244
245 switch(badge_event_type(event)) {
246 case BADGE_EVENT_USER_INPUT: {
247 buttons = badge_event_current_input_state();
248 break;
249 }
250 case BADGE_EVENT_GAME_TICK: {
251 badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
252 badge_framebuffer fb = { { { 0x80 } } };
253
254 if(buttons & BADGE_EVENT_KEY_UP) { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
255 if(buttons & BADGE_EVENT_KEY_DOWN) { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
256 if(buttons & BADGE_EVENT_KEY_LEFT) { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
257 if(buttons & BADGE_EVENT_KEY_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
258 if(buttons & BADGE_EVENT_KEY_CENTER){ badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
259 if(buttons & BADGE_EVENT_KEY_BTN_A) { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
260 if(buttons & BADGE_EVENT_KEY_BTN_B) { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
261
262 badge_framebuffer_flush(&fb);
263 break;
264 }
265 }
266 }
267
268 return 0;
269 }
This page took 0.049059 seconds and 3 git commands to generate.