Pilz verfolgt Spieler.
[hackover2013-badge-firmware.git] / sysinit.c
1 /**************************************************************************/
2 /*!
3 @file sysinit.c
4 @author K. Townsend (microBuilder.eu)
5 @date 22 March 2010
6 @version 0.10
7
8 @section LICENSE
9
10 Software License Agreement (BSD License)
11
12 Copyright (c) 2010, microBuilder SARL
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. Neither the name of the copyright holders nor the
23 names of its contributors may be used to endorse or promote products
24 derived from this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
27 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
30 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 /**************************************************************************/
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdarg.h>
43
44 #include "sysinit.h"
45
46 #include "core/cpu/cpu.h"
47 #include "core/pmu/pmu.h"
48
49 #ifdef CFG_PRINTF_UART
50 #include "core/uart/uart.h"
51 #endif
52
53 #ifdef CFG_INTERFACE
54 #include "core/cmd/cmd.h"
55 #endif
56
57 #ifdef CFG_CHIBI
58 #include "drivers/rf/chibi/chb.h"
59 #endif
60
61 #ifdef CFG_USBHID
62 #include "core/usbhid-rom/usbhid.h"
63 #endif
64
65 #ifdef CFG_USBCDC
66 volatile unsigned int lastTick;
67 #include "core/usbcdc/usb.h"
68 #include "core/usbcdc/usbcore.h"
69 #include "core/usbcdc/usbhw.h"
70 #include "core/usbcdc/cdcuser.h"
71 #include "core/usbcdc/cdc_buf.h"
72 #endif
73
74 #ifdef CFG_ST7565
75 #include "drivers/displays/bitmap/st7565/st7565.h"
76 #include "drivers/displays/smallfonts.h"
77 #endif
78
79 #ifdef CFG_SSD1306
80 #include "drivers/displays/bitmap/ssd1306/ssd1306.h"
81 #include "drivers/displays/smallfonts.h"
82 #endif
83
84 #ifdef CFG_TFTLCD
85 #include "drivers/displays/tft/lcd.h"
86 #include "drivers/displays/tft/touchscreen.h"
87 #include "drivers/displays/tft/drawing.h"
88 #endif
89
90 #ifdef CFG_I2CEEPROM
91 #include "drivers/storage/eeprom/mcp24aa/mcp24aa.h"
92 #include "drivers/storage/eeprom/eeprom.h"
93 #endif
94
95 #ifdef CFG_PWM
96 #include "core/pwm/pwm.h"
97 #endif
98
99 #ifdef CFG_SDCARD
100 #include "core/ssp/ssp.h"
101 #include "drivers/fatfs/diskio.h"
102 #include "drivers/fatfs/ff.h"
103
104 DWORD get_fattime ()
105 {
106 DWORD tmr = 0;
107
108 // tmr = (((DWORD)rtcYear - 80) << 25)
109 // | ((DWORD)rtcMon << 21)
110 // | ((DWORD)rtcMday << 16)
111 // | (WORD)(rtcHour << 11)
112 // | (WORD)(rtcMin << 5)
113 // | (WORD)(rtcSec >> 1);
114
115 return tmr;
116 }
117 #endif
118
119 /**************************************************************************/
120 /*!
121 Configures the core system clock and sets up any mandatory
122 peripherals like the systick timer, UART for printf, etc.
123
124 This function should set the HW to the default state you wish to be
125 in coming out of reset/startup, such as disabling or enabling LEDs,
126 setting specific pin states, etc.
127 */
128 /**************************************************************************/
129 void systemInit()
130 {
131 cpuInit(); // Configure the CPU
132 systickInit(CFG_SYSTICK_DELAY_IN_MS); // Start systick timer
133 gpioInit(); // Enable GPIO
134 pmuInit(); // Configure power management
135
136 // Set LED pin as output and turn LED off
137 gpioSetDir(CFG_LED_PORT, CFG_LED_PIN, 1);
138 gpioSetValue(CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);
139
140 // Config alt reset pin if requested (really only relevant to LPC1343 LCD Board)
141 #ifdef CFG_ALTRESET
142 gpioSetDir (CFG_ALTRESET_PORT, CFG_ALTRESET_PIN, gpioDirection_Input);
143 gpioSetInterrupt (CFG_ALTRESET_PORT, CFG_ALTRESET_PIN, gpioInterruptSense_Level, gpioInterruptEdge_Single, gpioInterruptEvent_ActiveHigh);
144 gpioIntEnable (CFG_ALTRESET_PORT, CFG_ALTRESET_PIN);
145 #endif
146
147 // Initialise EEPROM
148 #ifdef CFG_I2CEEPROM
149 mcp24aaInit();
150 #endif
151
152 // Initialise UART with the default baud rate
153 #ifdef CFG_PRINTF_UART
154 #ifdef CFG_I2CEEPROM
155 uint32_t uart = eepromReadU32(CFG_EEPROM_UART_SPEED);
156 if ((uart == 0xFFFFFFFF) || (uart > 115200))
157 {
158 uartInit(CFG_UART_BAUDRATE); // Use default baud rate
159 }
160 else
161 {
162 uartInit(uart); // Use baud rate from EEPROM
163 }
164 #else
165 uartInit(CFG_UART_BAUDRATE);
166 #endif
167 #endif
168
169 // Initialise PWM (requires 16-bit Timer 1 and P1.9)
170 #ifdef CFG_PWM
171 pwmInit();
172 #endif
173
174 // Initialise USB HID
175 #ifdef CFG_USBHID
176 usbHIDInit();
177 #endif
178
179 // Initialise USB CDC
180 #ifdef CFG_USBCDC
181 lastTick = systickGetTicks(); // Used to control output/printf timing
182 CDC_Init(); // Initialise VCOM
183 USB_Init(); // USB Initialization
184 USB_Connect(TRUE); // USB Connect
185 // Wait until USB is configured or timeout occurs
186 uint32_t usbTimeout = 0;
187 while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 )
188 {
189 if (USB_Configuration) break;
190 systickDelay(10); // Wait 10ms
191 usbTimeout++;
192 }
193 #endif
194
195 // Printf can now be used with UART or USBCDC
196
197 // Initialise the ST7565 128x64 pixel display
198 #ifdef CFG_ST7565
199 st7565Init();
200 st7565ClearScreen(); // Clear the screen
201 st7565Backlight(1); // Enable the backlight
202 #endif
203
204 // Initialise the SSD1306 OLED display
205 #ifdef CFG_SSD1306
206 ssd1306Init(SSD1306_INTERNALVCC);
207 ssd1306ClearScreen(); // Clear the screen
208 #endif
209
210 // Initialise TFT LCD Display
211 #ifdef CFG_TFTLCD
212 lcdInit();
213 // You may need to call the tsCalibrate() function to calibrate
214 // the touch screen is this has never been done. This only needs
215 // to be done once and the values are saved to EEPROM. This
216 // function can also be called from tsInit if it's more
217 // convenient
218 /*
219 #ifdef CFG_I2CEEPROM
220 if (eepromReadU8(CFG_EEPROM_TOUCHSCREEN_CALIBRATED) != 1)
221 {
222 tsCalibrate();
223 }
224 #endif
225 */
226 #endif
227
228 // Initialise Chibi
229 // Warning: CFG_CHIBI must be disabled if no antenna is connected,
230 // otherwise the SW will halt during initialisation
231 #ifdef CFG_CHIBI
232 // Write addresses to EEPROM for the first time if necessary
233 // uint16_t addr_short = 0x0025;
234 // uint64_t addr_ieee = 0x0000000000000025;
235 // mcp24aaWriteBuffer(CFG_EEPROM_CHIBI_SHORTADDR, (uint8_t *)&addr_short, 2);
236 // mcp24aaWriteBuffer(CFG_EEPROM_CHIBI_IEEEADDR, (uint8_t *)&addr_ieee, 8);
237 chb_init();
238 // chb_pcb_t *pcb = chb_get_pcb();
239 // printf("%-40s : 0x%04X%s", "Chibi Initialised", pcb->src_addr, CFG_PRINTF_NEWLINE);
240 #endif
241
242 // Start the command line interface
243 #ifdef CFG_INTERFACE
244 cmdInit();
245 #endif
246 }
247
248 /**************************************************************************/
249 /*!
250 @brief Sends a single byte to a pre-determined peripheral (UART, etc.).
251
252 @param[in] byte
253 Byte value to send
254 */
255 /**************************************************************************/
256 void __putchar(const char c)
257 {
258 #ifdef CFG_PRINTF_UART
259 // Send output to UART
260 uartSendByte(c);
261 #endif
262 }
263
264 /**************************************************************************/
265 /*!
266 @brief Sends a string to a pre-determined end point (UART, etc.).
267
268 @param[in] str
269 Text to send
270
271 @note This function is only called when using the GCC-compiler
272 in Codelite or running the Makefile manually. This function
273 will not be called when using the C library in Crossworks for
274 ARM.
275 */
276 /**************************************************************************/
277 int puts(const char * str)
278 {
279 // There must be at least 1ms between USB frames (of up to 64 bytes)
280 // This buffers all data and writes it out from the buffer one frame
281 // and one millisecond at a time
282 #ifdef CFG_PRINTF_USBCDC
283 if (USB_Configuration)
284 {
285 while(*str)
286 cdcBufferWrite(*str++);
287 // Check if we can flush the buffer now or if we need to wait
288 unsigned int currentTick = systickGetTicks();
289 if (currentTick != lastTick)
290 {
291 uint8_t frame[64];
292 uint32_t bytesRead = 0;
293 while (cdcBufferDataPending())
294 {
295 // Read up to 64 bytes as long as possible
296 bytesRead = cdcBufferReadLen(frame, 64);
297 USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
298 systickDelay(1);
299 }
300 lastTick = currentTick;
301 }
302 }
303 #else
304 // Handle output character by character in __putchar
305 while(*str) __putchar(*str++);
306 #endif
307
308 return 0;
309 }
310
311 // Override printf here if we're using Crossworks for ARM
312 // so that we can still use the custom libc libraries.
313 // For Codelite and compiling from the makefile (Yagarto, etc.)
314 // this is done in /core/libc
315
316 #ifdef __CROSSWORKS_ARM
317
318 /**************************************************************************/
319 /*!
320 @brief Outputs a formatted string on the DBGU stream. Format arguments
321 are given in a va_list instance.
322
323 @param[in] pFormat
324 Format string
325 @param[in] ap
326 Argument list
327 */
328 /**************************************************************************/
329 signed int vprintf(const char *pFormat, va_list ap)
330 {
331 char pStr[CFG_PRINTF_MAXSTRINGSIZE];
332 char pError[] = "stdio.c: increase CFG_PRINTF_MAXSTRINGSIZE\r\n";
333
334 // Write formatted string in buffer
335 if (vsprintf(pStr, pFormat, ap) >= CFG_PRINTF_MAXSTRINGSIZE) {
336
337 puts(pError);
338 while (1); // Increase CFG_PRINTF_MAXSTRINGSIZE
339 }
340
341 // Display string
342 return puts(pStr);
343 }
344
345 /**************************************************************************/
346 /*!
347 @brief Outputs a formatted string on the DBGU stream, using a
348 variable number of arguments
349
350 @param[in] pFormat
351 Format string
352 */
353 /**************************************************************************/
354 signed int printf(const char *pFormat, ...)
355 {
356 va_list ap;
357 signed int result;
358
359 // Forward call to vprintf
360 va_start(ap, pFormat);
361 result = vprintf(pFormat, ap);
362 va_end(ap);
363
364 return result;
365 }
366
367 #endif
This page took 0.065127 seconds and 5 git commands to generate.