f092d362fba14e2fa9264fb938a7bc3208a173b4
[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 "fahrplan.h"
57 #include "init.h"
58 #include "ui/browser.h"
59 #include "ui/display.h"
60 #include "ui/event.h"
61 #include "ui/font.h"
62 #include "ui/menu.h"
63 #include "ui/sprite.h"
64 #include "ui/vanity.h"
65 #include "util/util.h"
66 #include "jumpnrun/jumpnrun.h"
67
68 #include "dataflash/at45db041d.h"
69
70 #include "drivers/fatfs/ff.h"
71
72 #include "badge/pinconfig.h"
73 #include <funk/nrf24l01p.h>
74
75 #ifdef R0KET
76
77 #include "r0ketports.h"
78 void rbBacklightInit(void) {
79 /* Enable the clock for CT16B1 */
80 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
81
82 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
83 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
84 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
85
86 /* Set default duty cycle (MR1) */
87 TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
88
89 /* External Match Register Settings for PWM */
90 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
91
92 /* enable Timer1 */
93 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
94
95 /* Enable PWM0 */
96 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;
97
98 // Enable Step-UP
99 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
100 gpioSetValue(RB_PWR_LCDBL, 0);
101 }
102
103 void rbInit() {
104 RB_HB0_IO &= ~IOCON_SWDIO_PIO1_3_FUNC_MASK;
105 RB_HB0_IO |= IOCON_SWDIO_PIO1_3_FUNC_GPIO;
106 RB_HB1_IO &= ~IOCON_JTAG_TCK_PIO0_10_FUNC_MASK;
107 RB_HB1_IO |= IOCON_JTAG_TCK_PIO0_10_FUNC_GPIO;
108
109 struct {
110 int port;
111 int pin;
112 uint32_t volatile *reg;
113 gpioPullupMode_t mode;
114 } const input_pins[] = {
115 #ifdef HOB_REV2
116 { RB_BTN0 , &RB_BTN0_IO , gpioPullupMode_PullDown },
117 { RB_BTN1 , &RB_BTN1_IO , gpioPullupMode_PullDown },
118 { RB_BTN2 , &RB_BTN2_IO , gpioPullupMode_PullDown },
119 { RB_BTN3 , &RB_BTN3_IO , gpioPullupMode_PullDown },
120 { RB_BTN4 , &RB_BTN4_IO , gpioPullupMode_PullDown },
121 { RB_HB0 , &RB_HB0_IO , gpioPullupMode_PullDown },
122 { RB_HB1 , &RB_HB1_IO , gpioPullupMode_PullDown },
123 { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullDown }
124 #else
125 { RB_BTN0 , &RB_BTN0_IO , gpioPullupMode_PullUp },
126 { RB_BTN1 , &RB_BTN1_IO , gpioPullupMode_PullUp },
127 { RB_BTN2 , &RB_BTN2_IO , gpioPullupMode_PullUp },
128 { RB_BTN3 , &RB_BTN3_IO , gpioPullupMode_PullUp },
129 { RB_BTN4 , &RB_BTN4_IO , gpioPullupMode_PullUp },
130 { RB_HB0 , &RB_HB0_IO , gpioPullupMode_PullUp },
131 { RB_HB1 , &RB_HB1_IO , gpioPullupMode_PullUp },
132 { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullUp }
133 #endif
134 };
135
136 for(int i = 0; i < ARRAY_SIZE(input_pins); ++i) {
137 gpioSetDir(input_pins[i].port, input_pins[i].pin, gpioDirection_Input);
138 gpioSetPullup(input_pins[i].reg, input_pins[i].mode);
139 }
140
141 // LED3 zur Bestimmung der Umgebungshelligkeit.
142 gpioSetDir(RB_LED3, gpioDirection_Input);
143 RB_LED3_IO = (RB_LED3_IO & IOCON_PIO1_11_FUNC_MASK) | IOCON_PIO1_11_FUNC_AD7;
144
145 // prepare LEDs
146 IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
147 IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
148
149 struct {
150 int port;
151 int pin;
152 int value;
153 } const output_pins[] = {
154 { RB_PWR_GOOD , 0 },
155 { USB_CONNECT , 1 },
156 { RB_LCD_CS , 1 },
157 { RB_SPI_CS_DF, 1 },
158 { RB_SPI_SS2 , 1 },
159 { RB_SPI_SS3 , 1 },
160 { RB_SPI_SS4 , 1 },
161 { RB_SPI_SS5 , 1 },
162 { RB_LED0 , 0 },
163 { RB_LED1 , 0 },
164 { RB_LED2 , 0 },
165 { RB_LCD_BL , 0 },
166 { RB_HB2 , 1 },
167 { RB_HB3 , 1 },
168 { RB_HB4 , 1 },
169 { RB_HB5 , 1 }
170 };
171
172 for(int i = 0; i < ARRAY_SIZE(output_pins); ++i) {
173 gpioSetDir (output_pins[i].port, output_pins[i].pin, gpioDirection_Output);
174 gpioSetValue(output_pins[i].port, output_pins[i].pin, output_pins[i].value);
175 }
176
177 // Set P0.0 to GPIO
178 RB_PWR_LCDBL_IO &= ~RB_PWR_LCDBL_IO_FUNC_MASK;
179 RB_PWR_LCDBL_IO |= RB_PWR_LCDBL_IO_FUNC_GPIO;
180
181 gpioSetDir ( RB_PWR_LCDBL , gpioDirection_Input);
182 gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
183
184 rbBacklightInit();
185 badge_display_init();
186 }
187
188 #endif
189
190 static void usbmode(void) {
191 badge_event_stop();
192
193 badge_framebuffer fb = { { { 0 } } };
194 badge_framebuffer_render_text(&fb, 23, 30, "USB-Modus");
195 /*
196 badge_framebuffer_render_number(&fb, 23, 50, sizeof(jumpnrun_tile));
197 badge_framebuffer_render_number(&fb, 33, 50, sizeof(jumpnrun_item));
198 badge_framebuffer_render_number(&fb, 48, 50, sizeof(jumpnrun_enemy));
199 */
200 badge_framebuffer_flush(&fb);
201
202 usbMSCInit();
203
204 for(;;) {
205 pmuSleep();
206 }
207 }
208
209 uint8_t main_menu_show(uint8_t selected) {
210 // first_visible = 0, weil das Menü so kurz ist. Sollte es
211 // größer werden: Parameter aus main_menu empfangen und merken.
212 uint8_t first_visible = 0;
213 char const *const menu[] = {
214 "Vanity-Screen",
215 "Super Hackio",
216 "Fahrplan",
217 "USB-Modus"
218 };
219
220 f_chdir("/");
221 return (uint8_t) badge_menu(menu, ARRAY_SIZE(menu), &first_visible, selected);
222 }
223
224 void main_menu(void) {
225 uint8_t selected = 0;
226
227 for(;;) {
228 selected = main_menu_show(selected);
229 switch(selected) {
230 case 0: badge_vanity_show(); break;
231 case 1: jumpnrun_play (); break;
232 case 2: badge_fahrplan (); break;
233 case 3: usbmode (); break;
234 }
235 }
236 }
237
238 int main(void)
239 {
240 cpuInit();
241 systickInit(CFG_SYSTICK_DELAY_IN_MS);
242
243 pmuInit();
244 // adcInit();
245 #ifdef R0KET
246 rbInit();
247 #else
248 badge_init();
249 #endif
250
251 FATFS fs;
252 f_mount(0, &fs);
253
254 if(badge_input_raw() & BADGE_EVENT_KEY_DOWN) {
255 usbmode();
256 }
257
258 badge_event_start();
259 main_menu();
260
261 return 0;
262 }
This page took 0.054313 seconds and 3 git commands to generate.