1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Software License Agreement (BSD License)
10 Copyright (c) 2011, microBuilder SARL
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.
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.
35 /**************************************************************************/
40 #include "projectconfig.h"
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"
53 #include "core/cmd/cmd.h"
56 #include "ui/display.h"
57 #include "ui/sprite.h"
59 #include "jumpnrun/jumpnrun.h"
61 #include "r0ketports.h"
62 #include "drivers/fatfs/ff.h"
64 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof*(arr))
66 void backlightInit(void) {
70 // prototype uses WDT CLKOUT to drive the LCD backlight
71 // init without a reset
73 IOCON_PIO0_1
&= ~(IOCON_PIO0_1_FUNC_MASK
);
74 IOCON_PIO0_1
|= IOCON_PIO0_1_FUNC_CLKOUT
;
78 // use the WDT clock as the default WDT frequency is best frequency
79 // XXX this register value is missing in lpc1343.h
80 #define SCB_CLKOUTCLKSEL_SOURCE_WDTCLOCK ((unsigned int) 0x00000002) // Use the WDT clock
82 SCB_CLKOUTCLKSEL
= SCB_CLKOUTCLKSEL_SOURCE_WDTCLOCK
;
84 // toggle from LOW to HIGH to update source
85 SCB_CLKOUTCLKUEN
= SCB_CLKOUTCLKUEN_DISABLE
;
86 SCB_CLKOUTCLKUEN
= SCB_CLKOUTCLKUEN_UPDATE
;
88 // divide by 30, to get almost 10 kHz
89 SCB_CLKOUTCLKDIV
= 30;
95 /* Enable the clock for CT16B1 */
96 SCB_SYSAHBCLKCTRL
|= (SCB_SYSAHBCLKCTRL_CT16B1
);
98 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
99 IOCON_PIO1_9
&= ~IOCON_PIO1_9_FUNC_MASK
;
100 IOCON_PIO1_9
|= IOCON_PIO1_9_FUNC_CT16B1_MAT0
;
102 /* Set default duty cycle (MR1) */
103 TMR_TMR16B1MR0
= 0; //(0xFFFF * (100 - brightness)) / 100;
105 /* External Match Register Settings for PWM */
106 TMR_TMR16B1EMR
= TMR_TMR16B1EMR_EMC0_TOGGLE
| TMR_TMR16B1EMR_EM0
;
109 TMR_TMR16B1TCR
= TMR_TMR16B1TCR_COUNTERENABLE_ENABLED
;
112 TMR_TMR16B1PWMC
= TMR_TMR16B1PWMC_PWM0_ENABLED
;
115 gpioSetDir(RB_PWR_LCDBL
, gpioDirection_Output
);
116 gpioSetValue(RB_PWR_LCDBL
, 0);
124 RB_HB0_IO
&= ~IOCON_SWDIO_PIO1_3_FUNC_MASK
;
125 RB_HB0_IO
|= IOCON_SWDIO_PIO1_3_FUNC_GPIO
;
126 RB_HB1_IO
&= ~IOCON_JTAG_TCK_PIO0_10_FUNC_MASK
;
127 RB_HB1_IO
|= IOCON_JTAG_TCK_PIO0_10_FUNC_GPIO
;
133 uint32_t volatile *reg
;
134 gpioPullupMode_t mode
;
135 } const input_pins
[] = {
137 { RB_BTN0
, &RB_BTN0_IO
, gpioPullupMode_PullDown
},
138 { RB_BTN1
, &RB_BTN1_IO
, gpioPullupMode_PullDown
},
139 { RB_BTN2
, &RB_BTN2_IO
, gpioPullupMode_PullDown
},
140 { RB_BTN3
, &RB_BTN3_IO
, gpioPullupMode_PullDown
},
141 { RB_BTN4
, &RB_BTN4_IO
, gpioPullupMode_PullDown
},
142 { RB_BTN_A
, &RB_BTN_A_IO
, gpioPullupMode_PullDown
},
143 { RB_BTN_B
, &RB_BTN_B_IO
, gpioPullupMode_PullDown
},
145 { RB_BTN0
, &RB_BTN0_IO
, gpioPullupMode_PullUp
},
146 { RB_BTN1
, &RB_BTN1_IO
, gpioPullupMode_PullUp
},
147 { RB_BTN2
, &RB_BTN2_IO
, gpioPullupMode_PullUp
},
148 { RB_BTN3
, &RB_BTN3_IO
, gpioPullupMode_PullUp
},
149 { RB_BTN4
, &RB_BTN4_IO
, gpioPullupMode_PullUp
},
150 { RB_HB0
, &RB_HB0_IO
, gpioPullupMode_PullUp
},
151 { RB_HB1
, &RB_HB1_IO
, gpioPullupMode_PullUp
},
152 { RB_PWR_CHRG
, &RB_PWR_CHRG_IO
, gpioPullupMode_PullUp
}
156 for(int i
= 0; i
< ARRAY_SIZE(input_pins
); ++i
) {
157 gpioSetDir(input_pins
[i
].port
, input_pins
[i
].pin
, gpioDirection_Input
);
158 gpioSetPullup(input_pins
[i
].reg
, input_pins
[i
].mode
);
162 IOCON_JTAG_TMS_PIO1_0
&= ~(IOCON_JTAG_TMS_PIO1_0_FUNC_MASK
);
163 IOCON_JTAG_TMS_PIO1_0
|= IOCON_JTAG_TMS_PIO1_0_FUNC_GPIO
;
165 // LED3 zur Bestimmung der Umgebungshelligkeit.
166 gpioSetDir(RB_LED3
, gpioDirection_Input
);
167 RB_LED3_IO
= (RB_LED3_IO
& IOCON_PIO1_11_FUNC_MASK
) | IOCON_PIO1_11_FUNC_AD7
;
170 IOCON_JTAG_TDI_PIO0_11
&= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK
;
171 IOCON_JTAG_TDI_PIO0_11
|= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO
;
178 } const output_pins
[] = {
199 for(int i
= 0; i
< ARRAY_SIZE(output_pins
); ++i
) {
200 gpioSetDir (output_pins
[i
].port
, output_pins
[i
].pin
, gpioDirection_Output
);
201 gpioSetValue(output_pins
[i
].port
, output_pins
[i
].pin
, output_pins
[i
].value
);
206 RB_PWR_LCDBL_IO
&= ~RB_PWR_LCDBL_IO_FUNC_MASK
;
207 RB_PWR_LCDBL_IO
|= RB_PWR_LCDBL_IO_FUNC_GPIO
;
209 gpioSetDir ( RB_PWR_LCDBL
, gpioDirection_Input
);
210 gpioSetPullup(&RB_PWR_LCDBL_IO
, gpioPullupMode_Inactive
);
217 /**************************************************************************/
219 Main program entry point. After reset, normal code execution will
222 /**************************************************************************/
225 // Configure cpu and mandatory peripherals
229 systickInit(CFG_SYSTICK_DELAY_IN_MS
);
237 badge_display_init();
240 gpioSetDir(0, 11, gpioDirection_Output);
241 gpioSetValue(0, 11, 0);
245 badge_framebuffer fb;
249 if(FR_OK == f_mount(0, &fatvol)) {
251 if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
254 if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
255 gpioSetDir(RB_LED3, gpioDirection_Output);
256 gpioSetValue(RB_LED3, 1);
262 gpioSetDir(RB_LED1, gpioDirection_Output);
263 gpioSetValue(RB_LED1, 1);
266 gpioSetDir(RB_LED2, gpioDirection_Output);
267 gpioSetValue(RB_LED2, 1);
270 badge_framebuffer_flush(&fb);
277 if(JUMPNRUN_ERROR == jumpnrun_play("smb.lvl")) {
285 for(uint8_t i
= 0; ; ++i
) {
286 badge_event_t event
= badge_event_wait();
288 // SCB_CLKOUTCLKDIV = i;
290 switch(badge_event_type(event
)) {
291 case BADGE_EVENT_USER_INPUT
: {
292 buttons
= badge_event_current_input_state();
295 case BADGE_EVENT_GAME_TICK
: {
296 badge_sprite
const sp
= { 4, 4, (uint8_t const *) "\xff\xff" };
297 badge_framebuffer fb
= { { { 0x80 } } };
300 SSP_SSP0CR0_DSS_8BIT
// Data size = 8-bit
301 | SSP_SSP0CR0_FRF_SPI
// Frame format = SPI
304 gpioSetValue(RB_SPI_CS_DF
, 0);
307 sspSend(0, "\x05", 1);
308 sspReceive(0, &st
, 1);
310 gpioSetValue(RB_SPI_CS_DF
, 1);
314 for(int i = 0; i < 9 * 96; ++i) {
315 fb.data[0][i] = 0xbb;
319 if(buttons
& BADGE_EVENT_KEY_UP
) { badge_framebuffer_blt(&fb
, 30, 10, &sp
, 0); }
320 if(buttons
& BADGE_EVENT_KEY_DOWN
) { badge_framebuffer_blt(&fb
, 30, 50, &sp
, 0); }
321 if(buttons
& BADGE_EVENT_KEY_LEFT
) { badge_framebuffer_blt(&fb
, 10, 30, &sp
, 0); }
322 if(buttons
& BADGE_EVENT_KEY_RIGHT
) { badge_framebuffer_blt(&fb
, 50, 30, &sp
, 0); }
323 if(buttons
& BADGE_EVENT_KEY_CENTER
){ badge_framebuffer_blt(&fb
, 30, 30, &sp
, 0); }
324 if(buttons
& BADGE_EVENT_KEY_BTN_A
) { badge_framebuffer_blt(&fb
, 70, 10, &sp
, 0); }
325 if(buttons
& BADGE_EVENT_KEY_BTN_B
) { badge_framebuffer_blt(&fb
, 70, 50, &sp
, 0); }
327 badge_framebuffer_flush(&fb
);