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