Kram auch eingecheckt.
[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 #include "ui/event.h"
57 #include "jumpnrun/jumpnrun.h"
58
59 #include "r0ketports.h"
60 #include "drivers/fatfs/ff.h"
61
62 void backlightInit(void) {
63 /* Enable the clock for CT16B1 */
64 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
65
66 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
67 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
68 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
69
70 /* Set default duty cycle (MR1) */
71 TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
72
73 /* External Match Register Settings for PWM */
74 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
75
76 /* enable Timer1 */
77 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
78
79 /* Enable PWM0 */
80 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;
81
82 // Enable Step-UP
83 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
84 gpioSetValue(RB_PWR_LCDBL, 0);
85 }
86
87 void rbInit() {
88 // TODO FIXME special port disable ? LEDs BTNs ?
89
90 // prepare power
91 // TODO FIXME more power init needed ? chrg + volt input ?
92 // enable external vcc
93 gpioSetDir(RB_PWR_GOOD, gpioDirection_Output);
94 gpioSetValue (RB_PWR_GOOD, 0);
95
96 // Disable USB Connect (we don't want USB by default)
97 gpioSetDir(USB_CONNECT, gpioDirection_Output);
98 gpioSetValue(USB_CONNECT, 1);
99
100 static uint8_t ports[] = { RB_BTN0, RB_BTN1, RB_BTN2, RB_BTN3, RB_BTN4,
101 RB_LED0, RB_LED1, RB_LED2,
102 RB_HB3, RB_HB4, RB_SPI_SS2,
103 RB_SPI_SS3, RB_SPI_SS4, RB_SPI_SS5,
104 RB_HB0, RB_HB1, RB_HB2,
105 RB_HB3, RB_HB4, RB_HB5};
106
107 volatile uint32_t * regs[] = {&RB_BTN0_IO, &RB_BTN1_IO, &RB_BTN2_IO,
108 &RB_BTN3_IO, &RB_BTN4_IO};
109
110 int i = 0;
111 while( i<10 ){
112 gpioSetDir(ports[i], ports[i+1], gpioDirection_Input);
113 gpioSetPullup(regs[i/2], gpioPullupMode_PullUp);
114 i+=2;
115 }
116
117 // prepate chrg_stat
118 gpioSetDir(RB_PWR_CHRG, gpioDirection_Input);
119 gpioSetPullup (&RB_PWR_CHRG_IO, gpioPullupMode_PullUp);
120
121 gpioSetDir(RB_LED3, gpioDirection_Input);
122 IOCON_PIO1_11 = 0x41;
123
124 // prepare LEDs
125 IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
126 IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
127
128 while( i<16 ){
129 gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
130 gpioSetValue (ports[i], ports[i+1], 0);
131 i+=2;
132 }
133
134 // Set LED3 to ?
135 IOCON_PIO1_11 = 0x41;
136
137 // prepare lcd
138 // TODO FIXME more init needed ?
139 gpioSetDir(RB_LCD_BL, gpioDirection_Output);
140 gpioSetValue (RB_LCD_BL, 0);
141
142 // Set P0.0 to GPIO
143 RB_PWR_LCDBL_IO&= ~RB_PWR_LCDBL_IO_FUNC_MASK;
144 RB_PWR_LCDBL_IO|= RB_PWR_LCDBL_IO_FUNC_GPIO;
145 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Input);
146 gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
147
148 // prepare SPI/SS
149 // TODO FIXME init miso/mosi/sck somehow ?
150 // prepare hackerbus
151 while(i<sizeof(ports)){
152 gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
153 gpioSetValue (ports[i], ports[i+1], 1);
154 i+=2;
155 }
156
157 backlightInit();
158 //font=&Font_7x8;
159 }
160
161
162 /**************************************************************************/
163 /*!
164 Main program entry point. After reset, normal code execution will
165 begin here.
166 */
167 /**************************************************************************/
168 int main(void)
169 {
170 // Configure cpu and mandatory peripherals
171 //systemInit();
172
173 cpuInit();
174 systickInit(CFG_SYSTICK_DELAY_IN_MS);
175
176 // pmuInit();
177 // adcInit();
178 rbInit();
179 usbMSCInit();
180
181 badge_display_init();
182
183 gpioSetDir(0, 11, gpioDirection_Output);
184 gpioSetValue(0, 11, 0);
185
186 {
187 badge_framebuffer fb;
188 int res = 0;
189 FATFS fatvol;
190
191 if(FR_OK == f_mount(0, &fatvol)) {
192 FIL fil;
193 if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
194 UINT readbytes;
195
196 if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
197 gpioSetDir(RB_LED3, gpioDirection_Output);
198 gpioSetValue(RB_LED3, 1);
199 }
200
201 f_close(&fil);
202 } else {
203 fb.data[0][0] = res;
204 gpioSetDir(RB_LED1, gpioDirection_Output);
205 gpioSetValue(RB_LED1, 1);
206 }
207 } else {
208 gpioSetDir(RB_LED2, gpioDirection_Output);
209 gpioSetValue(RB_LED2, 1);
210 }
211
212 badge_framebuffer_flush(&fb);
213 }
214
215 badge_event_start();
216
217 for(;;) {
218 if(JUMPNRUN_ERROR == jumpnrun_play("smb.lvl")) {
219 break;
220 }
221 }
222
223 /*
224 uint8_t buttons = 0;
225
226 for(uint8_t i = 0; ; ++i) {
227 badge_event_t event = badge_event_wait();
228
229 switch(badge_event_type(event)) {
230 case BADGE_EVENT_USER_INPUT: {
231 buttons = badge_event_current_input_state();
232 break;
233 }
234 case BADGE_EVENT_GAME_TICK: {
235 badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
236 badge_framebuffer fb = { { { 0 } } };
237
238 if(buttons & BADGE_EVENT_KEY_UP) { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
239 if(buttons & BADGE_EVENT_KEY_DOWN) { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
240 if(buttons & BADGE_EVENT_KEY_LEFT) { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
241 if(buttons & BADGE_EVENT_KEY_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
242 if(buttons & BADGE_EVENT_KEY_CENTER){ badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
243 if(buttons & BADGE_EVENT_KEY_BTN_A) { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
244 if(buttons & BADGE_EVENT_KEY_BTN_B) { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
245
246 badge_framebuffer_flush(&fb);
247 break;
248 }
249 }
250 }
251 */
252
253 return 0;
254 }
This page took 0.063462 seconds and 5 git commands to generate.