Hackover-Badge 2013
[hackover2013-badge-firmware.git] / 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 "lcd/display.h"
55
56 #include "r0ketports.h"
57
58 uint8_t getInputRaw(void) {
59 uint8_t result = BTN_NONE;
60
61 if (gpioGetValue(RB_BTN3)==0) {
62 result |= BTN_UP;
63 }
64
65 if (gpioGetValue(RB_BTN2)==0) {
66 result |= BTN_DOWN;
67 }
68
69 if (gpioGetValue(RB_BTN4)==0) {
70 result |= BTN_ENTER;
71 }
72
73 if (gpioGetValue(RB_BTN0)==0) {
74 result |= BTN_LEFT;
75 }
76
77 if (gpioGetValue(RB_BTN1)==0) {
78 result |= BTN_RIGHT;
79 }
80
81 return result;
82 }
83
84 void backlightInit(void) {
85 /* Enable the clock for CT16B1 */
86 SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
87
88 /* Configure PIO1.9 as Timer1_16 MAT0 Output */
89 IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
90 IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
91
92 /* Set default duty cycle (MR1) */
93 TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
94
95 /* External Match Register Settings for PWM */
96 TMR_TMR16B1EMR = TMR_TMR16B1EMR_EMC0_TOGGLE | TMR_TMR16B1EMR_EM0;
97
98 /* enable Timer1 */
99 TMR_TMR16B1TCR = TMR_TMR16B1TCR_COUNTERENABLE_ENABLED;
100
101 /* Enable PWM0 */
102 TMR_TMR16B1PWMC = TMR_TMR16B1PWMC_PWM0_ENABLED;
103
104 // Enable Step-UP
105 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Output);
106 gpioSetValue(RB_PWR_LCDBL, 0);
107 }
108
109
110
111 void rbInit() {
112 // TODO FIXME special port disable ? LEDs BTNs ?
113
114 // prepare power
115 // TODO FIXME more power init needed ? chrg + volt input ?
116 // enable external vcc
117 gpioSetDir(RB_PWR_GOOD, gpioDirection_Output);
118 gpioSetValue (RB_PWR_GOOD, 0);
119
120 // Disable USB Connect (we don't want USB by default)
121 gpioSetDir(USB_CONNECT, gpioDirection_Output);
122 gpioSetValue(USB_CONNECT, 1);
123
124 static uint8_t ports[] = { RB_BTN0, RB_BTN1, RB_BTN2, RB_BTN3, RB_BTN4,
125 RB_LED0, RB_LED1, RB_LED2,
126 RB_SPI_SS0, RB_SPI_SS1, RB_SPI_SS2,
127 RB_SPI_SS3, RB_SPI_SS4, RB_SPI_SS5,
128 RB_HB0, RB_HB1, RB_HB2,
129 RB_HB3, RB_HB4, RB_HB5};
130
131 volatile uint32_t * regs[] = {&RB_BTN0_IO, &RB_BTN1_IO, &RB_BTN2_IO,
132 &RB_BTN3_IO, &RB_BTN4_IO};
133
134 int i = 0;
135 while( i<10 ){
136 gpioSetDir(ports[i], ports[i+1], gpioDirection_Input);
137 gpioSetPullup(regs[i/2], gpioPullupMode_PullUp);
138 i+=2;
139 }
140
141 // prepate chrg_stat
142 gpioSetDir(RB_PWR_CHRG, gpioDirection_Input);
143 gpioSetPullup (&RB_PWR_CHRG_IO, gpioPullupMode_PullUp);
144
145 gpioSetDir(RB_LED3, gpioDirection_Input);
146 IOCON_PIO1_11 = 0x41;
147
148 // prepare LEDs
149 IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
150 IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
151
152 while( i<16 ){
153 gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
154 gpioSetValue (ports[i], ports[i+1], 0);
155 i+=2;
156 }
157
158 // Set LED3 to ?
159 IOCON_PIO1_11 = 0x41;
160
161 // prepare lcd
162 // TODO FIXME more init needed ?
163 gpioSetDir(RB_LCD_BL, gpioDirection_Output);
164 gpioSetValue (RB_LCD_BL, 0);
165
166 // Set P0.0 to GPIO
167 RB_PWR_LCDBL_IO&= ~RB_PWR_LCDBL_IO_FUNC_MASK;
168 RB_PWR_LCDBL_IO|= RB_PWR_LCDBL_IO_FUNC_GPIO;
169 gpioSetDir(RB_PWR_LCDBL, gpioDirection_Input);
170 gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
171
172 // prepare SPI/SS
173 // TODO FIXME init miso/mosi/sck somehow ?
174 // prepare hackerbus
175 while(i<sizeof(ports)){
176 gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
177 gpioSetValue (ports[i], ports[i+1], 1);
178 i+=2;
179 }
180
181 backlightInit();
182 //font=&Font_7x8;
183 }
184
185
186 /**************************************************************************/
187 /*!
188 Main program entry point. After reset, normal code execution will
189 begin here.
190 */
191 /**************************************************************************/
192 int main(void)
193 {
194 // Configure cpu and mandatory peripherals
195 //systemInit();
196
197 cpuInit();
198 systickInit(CFG_SYSTICK_DELAY_IN_MS);
199 //gpioInit();
200 // pmuInit();
201 // adcInit();
202 rbInit();
203
204 badge_display_init();
205 gpioSetDir(0, 11, gpioDirection_Output);
206
207
208 badge_framebuffer fb = {
209 {
210 {
211 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
212 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
213 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
214 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
215 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
216 }, {
217 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
218 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
219 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
220 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
221 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
222 }, {
223 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
224 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
225 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
226 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
227 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
228 }, {
229 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
230 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
231 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
232 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
233 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
234 }, {
235 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
236 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
237 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
238 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
239 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
240 }, {
241 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
242 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
243 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
244 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
245 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
246 }, {
247 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
248 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
249 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
250 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
251 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
252 }, {
253 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
254 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
255 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
256 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
257 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
258 }, {
259 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
260 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
261 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
262 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
263 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
264 }
265 }
266 };
267
268 badge_framebuffer_flush(&fb);
269
270 usbMSCInit();
271 for(;;);
272
273 for(uint8_t i = 0; ; ++i) {
274 gpioSetValue(0, 11, i & 1);
275 systickDelay(500);
276 }
277
278 return 0;
279 }
This page took 0.066827 seconds and 5 git commands to generate.