First commit
[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
43 #include "sysinit.h"
44
45 #include "core/cpu/cpu.h"
46 #include "core/pmu/pmu.h"
47 #include "core/adc/adc.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/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/lcd/bitmap/st7565/st7565.h"
76 #include "drivers/lcd/smallfonts.h"
77 #endif
78
79 #ifdef CFG_SSD1306
80 #include "drivers/lcd/bitmap/ssd1306/ssd1306.h"
81 #include "drivers/lcd/smallfonts.h"
82 #endif
83
84 #ifdef CFG_TFTLCD
85 #include "drivers/lcd/tft/lcd.h"
86 #include "drivers/lcd/tft/touchscreen.h"
87 #include "drivers/lcd/tft/drawing.h"
88 #endif
89
90 #ifdef CFG_I2CEEPROM
91 #include "drivers/eeprom/mcp24aa/mcp24aa.h"
92 #include "drivers/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 // ToDo!
107 return 0;
108 }
109 #endif
110
111 /**************************************************************************/
112 /*!
113 Configures the core system clock and sets up any mandatory
114 peripherals like the systick timer, UART for printf, etc.
115
116 This function should set the HW to the default state you wish to be
117 in coming out of reset/startup, such as disabling or enabling LEDs,
118 setting specific pin states, etc.
119 */
120 /**************************************************************************/
121 void systemInit()
122 {
123 cpuInit(); // Configure the CPU
124 systickInit(CFG_SYSTICK_DELAY_IN_MS); // Start systick timer
125 gpioInit(); // Enable GPIO
126 pmuInit(); // Configure power management
127
128 // Set LED pin as output and turn LED off
129 gpioSetDir(CFG_LED_PORT, CFG_LED_PIN, 1);
130 gpioSetValue(CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);
131
132 // Config alt reset pin if requested (really only relevant to LPC1343 LCD Board)
133 #ifdef CFG_ALTRESET
134 gpioSetDir (CFG_ALTRESET_PORT, CFG_ALTRESET_PIN, gpioDirection_Input);
135 gpioSetInterrupt (CFG_ALTRESET_PORT, CFG_ALTRESET_PIN, gpioInterruptSense_Level, gpioInterruptEdge_Single, gpioInterruptEvent_ActiveHigh);
136 gpioIntEnable (CFG_ALTRESET_PORT, CFG_ALTRESET_PIN);
137 #endif
138
139 // Initialise EEPROM
140 #ifdef CFG_I2CEEPROM
141 mcp24aaInit();
142 #endif
143
144 // Initialise UART with the default baud rate
145 #ifdef CFG_PRINTF_UART
146 #ifdef CFG_I2CEEPROM
147 uint32_t uart = eepromReadU32(CFG_EEPROM_UART_SPEED);
148 if ((uart == 0xFFFFFFFF) || (uart > 115200))
149 {
150 uartInit(CFG_UART_BAUDRATE); // Use default baud rate
151 }
152 else
153 {
154 uartInit(uart); // Use baud rate from EEPROM
155 }
156 #else
157 uartInit(CFG_UART_BAUDRATE);
158 #endif
159 #endif
160
161 // Initialise PWM (requires 16-bit Timer 1 and P1.9)
162 #ifdef CFG_PWM
163 pwmInit();
164 #endif
165
166 // Initialise USB HID
167 #ifdef CFG_USBHID
168 usbHIDInit();
169 #endif
170
171 // Initialise USB CDC
172 #ifdef CFG_USBCDC
173 lastTick = systickGetTicks(); // Used to control output/printf timing
174 CDC_Init(); // Initialise VCOM
175 USB_Init(); // USB Initialization
176 USB_Connect(TRUE); // USB Connect
177 // Wait until USB is configured or timeout occurs
178 uint32_t usbTimeout = 0;
179 while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 )
180 {
181 if (USB_Configuration) break;
182 systickDelay(10); // Wait 10ms
183 usbTimeout++;
184 }
185 #endif
186
187 // Printf can now be used with UART or USBCDC
188
189 // Initialise the ST7565 128x64 pixel display
190 #ifdef CFG_ST7565
191 st7565Init();
192 st7565ClearScreen(); // Clear the screen
193 st7565Backlight(1); // Enable the backlight
194 #endif
195
196 // Initialise the SSD1306 OLED display
197 #ifdef CFG_SSD1306
198 ssd1306Init(SSD1306_INTERNALVCC);
199 ssd1306ClearScreen(); // Clear the screen
200 #endif
201
202 // Initialise TFT LCD Display
203 #ifdef CFG_TFTLCD
204 lcdInit();
205 // You may need to call the tsCalibrate() function to calibrate
206 // the touch screen is this has never been done. This only needs
207 // to be done once and the values are saved to EEPROM. This
208 // function can also be called from tsInit if it's more
209 // convenient
210 /*
211 #ifdef CFG_I2CEEPROM
212 if (eepromReadU8(CFG_EEPROM_TOUCHSCREEN_CALIBRATED) != 1)
213 {
214 tsCalibrate();
215 }
216 #endif
217 */
218 #endif
219
220 // Initialise Chibi
221 // Warning: CFG_CHIBI must be disabled if no antenna is connected,
222 // otherwise the SW will halt during initialisation
223 #ifdef CFG_CHIBI
224 // Write addresses to EEPROM for the first time if necessary
225 // uint16_t addr_short = 0x0025;
226 // uint64_t addr_ieee = 0x0000000000000025;
227 // mcp24aaWriteBuffer(CFG_EEPROM_CHIBI_SHORTADDR, (uint8_t *)&addr_short, 2);
228 // mcp24aaWriteBuffer(CFG_EEPROM_CHIBI_IEEEADDR, (uint8_t *)&addr_ieee, 8);
229 chb_init();
230 // chb_pcb_t *pcb = chb_get_pcb();
231 // printf("%-40s : 0x%04X%s", "Chibi Initialised", pcb->src_addr, CFG_PRINTF_NEWLINE);
232 #endif
233
234 // Start the command line interface
235 #ifdef CFG_INTERFACE
236 cmdInit();
237 #endif
238 }
239
240 /**************************************************************************/
241 /*!
242 @brief Sends a single byte to a pre-determined peripheral (UART, etc.).
243
244 @param[in] byte
245 Byte value to send
246 */
247 /**************************************************************************/
248 void __putchar(const char c)
249 {
250 #ifdef CFG_PRINTF_UART
251 // Send output to UART
252 uartSendByte(c);
253 #endif
254 }
255
256 /**************************************************************************/
257 /*!
258 @brief Sends a string to a pre-determined end point (UART, etc.).
259
260 @param[in] str
261 Text to send
262
263 @note This function is only called when using the GCC-compiler
264 in Codelite or running the Makefile manually. This function
265 will not be called when using the C library in Crossworks for
266 ARM.
267 */
268 /**************************************************************************/
269 int puts(const char * str)
270 {
271 // There must be at least 1ms between USB frames (of up to 64 bytes)
272 // This buffers all data and writes it out from the buffer one frame
273 // and one millisecond at a time
274 #ifdef CFG_PRINTF_USBCDC
275 if (USB_Configuration)
276 {
277 while(*str)
278 cdcBufferWrite(*str++);
279 // Check if we can flush the buffer now or if we need to wait
280 unsigned int currentTick = systickGetTicks();
281 if (currentTick != lastTick)
282 {
283 uint8_t frame[64];
284 uint32_t bytesRead = 0;
285 while (cdcBufferDataPending())
286 {
287 // Read up to 64 bytes as long as possible
288 bytesRead = cdcBufferReadLen(frame, 64);
289 USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
290 systickDelay(1);
291 }
292 lastTick = currentTick;
293 }
294 }
295 #else
296 // Handle output character by character in __putchar
297 while(*str) __putchar(*str++);
298 #endif
299
300 return 0;
301 }
This page took 0.066918 seconds and 5 git commands to generate.