| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
board_keil_mcb_18574357.c
Go to the documentation of this file.
1 /*
2 * @brief Keil MCB 1857/4357 board file
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #include "board.h"
33 #include "string.h"
34
35 /* Keil board uses 83848 PHY and retarget */
36 #include "lpc_phy_dp83848.c"
37 #include "retarget.c"
38
43 /*****************************************************************************
44 * Private types/enumerations/variables
45 ****************************************************************************/
46 /* Port and bit mapping for LEDs on GPIOs */
47 static const uint8_t ledports[] = {6, 6, 6, 6, 6, 4, 4, 4};
48 static const uint8_t ledbits[] = {24, 25, 26, 27, 28, 12, 13, 14};
49
51 #define LCD_CS(x) ((x) ? (Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 7, 16, true)) : (Chip_GPIO_WritePortBit(LPC_GPIO_PORT, \
52 7, 16, false)))
53
55 uint32_t g_isPenDn;
56 uint32_t g_isNewPenDn;
57 const int32_t ad_left = 3813;
58 const int32_t ad_top = 3805;// 237;
59 const int32_t ad_right = 360;
60 const int32_t ad_bottom = 237; // 3805;
61
62 const LCD_Config_T MCB4300_LCD = {
63 8,
64 4,
65 4,
66 240,
67 4,
68 3,
69 4,
70 320,
71 0,
72 1,
73 1,
74 1,
75 1,
76 6,
77 LCD_TFT,
78 LCD_COLOR_FORMAT_RGB,
79 0
80 };
81
82 /*****************************************************************************
83 * Public types/enumerations/variables
84 ****************************************************************************/
85
87 uint32_t SystemCoreClock;
88
89 /*****************************************************************************
90 * Private functions
91 ****************************************************************************/
92
93 /* Very simple (inaccurate) delay function */
94 static void delay(uint32_t i) {
95 while (i--) {}
96 }
97
98 /* Initializes default settings for UDA1380 */
99 static Status Board_Audio_CodecInit(int micIn)
100 {
101 /* Reset UDA1380 on board Keil */
102 Chip_SCU_PinMuxSet(0x8, 0, (SCU_MODE_MODE_INACT | SCU_MODE_FUNC0));
103 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 4, 0, true);
104 Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 4, 0, true);
105 /* delay 1us */
106 delay(100000);
107 Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 4, 0, false);
108 delay(100000);
109
110 if (!UDA1380_Init(UDA1380_MIC_IN_LR & - (micIn != 0))) {
111 return ERROR;
112 }
113
114 return SUCCESS;
115 }
116
118 /* Write to LCD controller, with A0 = 0 */
119 static void writeLCD00_16(uint16_t c) {
120
121 uint8_t buf[1];
122 LCD_CS(0);
123 buf[0] = 0x70;
124 Chip_SSP_WriteFrames_Blocking(SSP_ID, buf, 1); // Start + WR Register
125 buf[0] = (uint8_t) (c);
126 Chip_SSP_WriteFrames_Blocking(SSP_ID, buf, 1);
127 LCD_CS(1);
128 }
129
130 /* Write to LCD controller, with A0 = 1 */
131 static void writeLCD01_16(uint16_t c) {
132
133 uint8_t buf[1];
134 LCD_CS(0);
135 buf[0] = 0x72;
136 Chip_SSP_WriteFrames_Blocking(SSP_ID, buf, 1); /* Start + WR Data */
137 buf[0] = (uint8_t) (c >> 8);
138 Chip_SSP_WriteFrames_Blocking(SSP_ID, buf, 1);
139 buf[0] = (uint8_t) (c);
140 Chip_SSP_WriteFrames_Blocking(SSP_ID, buf, 1);
141 LCD_CS(1);
142 }
143
144 /* Write to LCD controller's register */
145 static void writeLCDReg(uint16_t reg, uint16_t dat) {
146 writeLCD00_16(reg);
147 writeLCD01_16(dat);
148 }
149
150 /* Pin configuration to communicate with LCD Controller */
151 static void pinConfig(void)
152 {
153 /* (DC) */
154 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 7, 16, true);
155 }
156
157 /* Writes a value to the STMPE811 register */
158 static uint32_t writeTSCReg(uint8_t regAddr, uint8_t value)
159 {
160 uint8_t val[2];
161 val[0] = regAddr;
162 val[1] = value;
163 return Chip_I2C_MasterSend(TSC_I2C_BUS, TSC_I2C_ADDR, val, sizeof(val));
164 }
165
166 /* Reads a value to the STMPE811 register */
167 static uint32_t readTSCReg(uint8_t regAddr, uint8_t *value)
168 {
169 return Chip_I2C_MasterCmdRead(TSC_I2C_BUS, TSC_I2C_ADDR, regAddr, value, 1);
170 }
171
172 /* Check if touch is detected or not */
173 static bool detectTSCTouch(void)
174 {
175 uint8_t CtrRegVal = 0;
176
177 if (readTSCReg(TSC_CTRL, &CtrRegVal) == 1) {
178 if (CtrRegVal & (1 << 7)) {
179 return true;
180 }
181 }
182 return false;
183 }
184
185 /* Get the touch coordinates from STMPE811 registers */
186 static Status getTSCCoord(int16_t *x, int16_t *y)
187 {
188 uint8_t fifo_size, tscData[4], i;
189
190 /* Read all samples except the last one */
191 readTSCReg(FIFO_SIZE, &fifo_size);
192 for (i = 0; i < fifo_size; ++i) {
193 if (Chip_I2C_MasterCmdRead(TSC_I2C_BUS, TSC_I2C_ADDR, DATA_XYZ, tscData, 4) == 0) {
194 return ERROR;
195 }
196 }
197
198 /* Retrieve last taken sample */
199 Chip_I2C_MasterCmdRead(TSC_I2C_BUS, TSC_I2C_ADDR, DATA_XYZ, tscData, 4);
200 *x = (tscData[0] << 4) | ((tscData[1] & 0xF0) >> 4);
201 *y = ((tscData[1] & 0x0F) << 8) | tscData[2];
202
203 /* Clear interrupt flags */
204 writeTSCReg(INT_STA, 0x1F);
205
206 return SUCCESS;
207 }
208
209 /*****************************************************************************
210 * Public functions
211 ****************************************************************************/
212
213 /* Update system core clock rate, should be called if the system has
214 a clock rate change */
215 void SystemCoreClockUpdate(void)
216 {
217 /* CPU core speed */
218 SystemCoreClock = Chip_Clock_GetRate(CLK_MX_MXCORE);
219 }
220
221 /* Initialize UART pins */
222 void Board_UART_Init(LPC_USART_T *pUART)
223 {
224 if (pUART == LPC_USART0) {
225 Chip_SCU_PinMuxSet(0x2, 0, (SCU_MODE_MODE_REPEATER | SCU_MODE_FUNC1)); /* P2.0 : UART0_TXD */
226 Chip_SCU_PinMuxSet(0x2, 1, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC1)); /* P2.1 : UART0_RXD */
227 }
228 else if (pUART == LPC_USART3) {
229 Chip_SCU_PinMuxSet(0x2, 3, (SCU_MODE_MODE_REPEATER | SCU_MODE_FUNC2)); /* P2.3 : UART3_TXD */
230 Chip_SCU_PinMuxSet(0x2, 4, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2)); /* P2.4 : UART3_RXD */
231 }
232 }
233
234 /* Initialize debug output via UART for board */
235 void Board_Debug_Init(void)
236 {
237 #if defined(DEBUG_UART)
238 Board_UART_Init(DEBUG_UART);
239
240 Chip_UART_Init(DEBUG_UART);
241 Chip_UART_SetBaud(DEBUG_UART, 115200);
242 Chip_UART_ConfigData(DEBUG_UART, UART_DATABIT_8, UART_PARITY_NONE, UART_STOPBIT_1);
243
244 /* Enable UART Transmit */
245 Chip_UART_TxCmd(DEBUG_UART, ENABLE);
246 #endif
247 }
248
249 /* Sends a character on the UART */
250 void Board_UARTPutChar(char ch)
251 {
252 #if defined(DEBUG_UART)
253 while (Chip_UART_SendByte(DEBUG_UART, (uint8_t) ch) == ERROR) {}
254 #endif
255 }
256
257 /* Gets a character from the UART, returns EOF if no character is ready */
258 int Board_UARTGetChar(void)
259 {
260 #if defined(DEBUG_UART)
261 uint8_t data;
262
263 if (Chip_UART_ReceiveByte(DEBUG_UART, &data) == SUCCESS) {
264 return (int) data;
265 }
266 #endif
267 return EOF;
268 }
269
270 /* Outputs a string on the debug UART */
271 void Board_UARTPutSTR(char *str)
272 {
273 #if defined(DEBUG_UART)
274 while (*str != '\0') {
275 Board_UARTPutChar(*str++);
276 }
277 #endif
278 }
279
280 /* Initializes board LED(s) */
281 static void Board_LED_Init()
282 {
283 int i;
284
285 /* Must make sure J21 is installed to enabled LEDs */
286 /* PD.10 : LED 0 (leftmost) */
287 /* PD.11 : LED 1 */
288 /* PD.12 : LED 2 */
289 /* PD.13 : LED 3 */
290 /* PD.14 : LED 4 */
291 /* P9.0 : LED 5 */
292 /* P9.1 : LED 6 */
293 /* P9.2 : LED 7 (rightmost) */
294 for (i = 0; i < (sizeof(ledports) / sizeof(ledports[0])); i++) {
295 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, ledports[i], ledbits[i], true);
296 }
297 }
298
299 #ifndef BOARD_LED_TEST_FUNCTION_WORKS
300 /* FIXME: temporary code for toggle LED support only */
301 static uint8_t LEDStates; /* shadow variable for LED states */
302 #endif
303
304 /* Sets the state of a board LED to on or off */
305 void Board_LED_Set(uint8_t LEDNumber, bool On)
306 {
307 if (LEDNumber <= 7) {
308 Chip_GPIO_WritePortBit(LPC_GPIO_PORT, ledports[LEDNumber], ledbits[LEDNumber], On);
309 #ifndef BOARD_LED_TEST_FUNCTION_WORKS
310 if (On) {
311 LEDStates |= (1 << LEDNumber); /* set the state */
312 }
313 else {
314 LEDStates &= ~(1 << LEDNumber); /* clear the state */
315 }
316 #endif
317 }
318 }
319
320 /* Returns the current state of a board LED */
321 bool Board_LED_Test(uint8_t LEDNumber)
322 {
323 if (LEDNumber <= 7) {
324 #ifndef BOARD_LED_TEST_FUNCTION_WORKS
325 if (LEDStates & (1 << LEDNumber)) { /* LED is on */
326 return true;
327 }
328 else { /* LED is off */
329 return false;
330 }
331 #else
332 return (bool) Chip_GPIO_ReadPortBit(ledports[LEDNumber], ledbits[LEDNumber]);
333 #endif
334 }
335 return false;
336 }
337
338 /* Returns the MAC address assigned to this board */
339 void Board_ENET_GetMacADDR(uint8_t *mcaddr)
340 {
341 const uint8_t boardmac[] = {0x00, 0x60, 0x37, 0x12, 0x34, 0x56};
342
343 memcpy(mcaddr, boardmac, 6);
344 }
345
346 /* Set up and initialize all required blocks and functions related to the
347 board hardware */
348 void Board_Init(void)
349 {
350 /* Sets up DEBUG UART */
351 DEBUGINIT();
352
353 /* Updates SystemCoreClock global var with current clock speed */
354 SystemCoreClockUpdate();
355
356 /* Initializes GPIO */
357 Chip_GPIO_Init(LPC_GPIO_PORT);
358
359 /* Setup GPIOs for USB demos */
360 Chip_SCU_PinMuxSet(0x9, 5, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC2)); /* P9_5 USB1_VBUS_EN, USB1 VBus function */
361 Chip_SCU_PinMuxSet(0x2, 5, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2)); /* P2_5 USB1_VBUS, MUST CONFIGURE THIS SIGNAL FOR USB1 NORMAL OPERATION */
362 Chip_SCU_PinMuxSet(0x6, 3, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC1)); /* P6_3 USB0_PWR_EN, USB0 VBus function */
363
364 /* Initialize LEDs */
365 Board_LED_Init();
366 }
367
368 /* Sets up board specific ADC interface */
369 void Board_ADC_Init(void)
370 {}
371
372 /* Sets up board specific I2C interface */
373 void Board_I2C_Init(I2C_ID_T id)
374 {
375 if (id == I2C1) {
376 /* Configure pin function for I2C1 on PE.13 (I2C1_SDA) and PE.15 (I2C1_SCL) */
377 Chip_SCU_PinMuxSet(0xE, 13, (SCU_MODE_ZIF_DIS | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC2));
378 Chip_SCU_PinMuxSet(0xE, 15, (SCU_MODE_ZIF_DIS | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC2));
379 }
380 else {
381 Chip_SCU_I2C0PinConfig(I2C0_STANDARD_FAST_MODE);
382 }
383 }
384
385 /* Sets up board specific I2S interface and UDA1380 */
386 void Board_Audio_Init(LPC_I2S_T *pI2S, int micIn)
387 {
388 Chip_I2S_Audio_Format_T I2S_Config;
389
390 I2S_Config.SampleRate = 48000;
391 I2S_Config.ChannelNumber = 2; /* 1 is mono, 2 is stereo */
392 I2S_Config.WordWidth = 16; /* 8, 16 or 32 bits */
393 Chip_I2S_Init(LPC_I2S0);
394 Chip_I2S_Config(LPC_I2S0, I2S_TX_MODE, &I2S_Config);
395
396 /* Init UDA1380 CODEC */
397 while (Board_Audio_CodecInit(micIn) != SUCCESS) {}
398 }
399
400 /* Initialize the LCD interface */
401 void Board_LCD_Init(void)
402 {
403 /* Reset LCD and wait for reset to complete */
404 Chip_RGU_TriggerReset(RGU_LCD_RST);
405 while (Chip_RGU_InReset(RGU_LCD_RST)) {}
406
407 /* Set backlight GPIO as an output */
408 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 3, 8, true);
409
410 Board_InitLCDController();
411 }
412
413 /* Initialize the LCD controller on the external QVGA (320x240) TFT LCD*/
414 void Board_InitLCDController(void)
415 {
416 /* LCD with HX8347-D LCD Controller */
417 SSP_ConfigFormat ssp_format;
418
419 delay(5);
420
421 pinConfig();
422 /* TBD Externally */
423 Chip_SSP_Init(SSP_ID);
424 // NVIC_EnableIRQ(SSP_ID);
425 Chip_SSP_SetMaster(SSP_ID, true);
426 Chip_SSP_SetBitRate(SSP_ID, 1000000);
427
428 ssp_format.frameFormat = SSP_FRAMEFORMAT_SPI;
429 ssp_format.bits = SSP_BITS_8;
430 ssp_format.clockMode = SSP_CLOCK_MODE0;
431
432 Chip_SSP_SetFormat(SSP_ID, &ssp_format);
433 Chip_SSP_Enable(SSP_ID);
434
435 delay(200);
436
437 /* Driving ability settings ------------------------------------------------*/
438 writeLCDReg(0xEA, 0x00); /* Power control internal used (1) */
439 writeLCDReg(0xEB, 0x20); /* Power control internal used (2) */
440 writeLCDReg(0xEC, 0x0C); /* Source control internal used (1) */
441 writeLCDReg(0xED, 0xC7); /* Source control internal used (2) */
442 writeLCDReg(0xE8, 0x38); /* Source output period Normal mode */
443 writeLCDReg(0xE9, 0x10); /* Source output period Idle mode */
444 writeLCDReg(0xF1, 0x01); /* RGB 18-bit interface ;0x0110 */
445 writeLCDReg(0xF2, 0x10);
446
447 /* Adjust the Gamma Curve --------------------------------------------------*/
448 writeLCDReg(0x40, 0x01);
449 writeLCDReg(0x41, 0x00);
450 writeLCDReg(0x42, 0x00);
451 writeLCDReg(0x43, 0x10);
452 writeLCDReg(0x44, 0x0E);
453 writeLCDReg(0x45, 0x24);
454 writeLCDReg(0x46, 0x04);
455 writeLCDReg(0x47, 0x50);
456 writeLCDReg(0x48, 0x02);
457 writeLCDReg(0x49, 0x13);
458 writeLCDReg(0x4A, 0x19);
459 writeLCDReg(0x4B, 0x19);
460 writeLCDReg(0x4C, 0x16);
461
462 writeLCDReg(0x50, 0x1B);
463 writeLCDReg(0x51, 0x31);
464 writeLCDReg(0x52, 0x2F);
465 writeLCDReg(0x53, 0x3F);
466 writeLCDReg(0x54, 0x3F);
467 writeLCDReg(0x55, 0x3E);
468 writeLCDReg(0x56, 0x2F);
469 writeLCDReg(0x57, 0x7B);
470 writeLCDReg(0x58, 0x09);
471 writeLCDReg(0x59, 0x06);
472 writeLCDReg(0x5A, 0x06);
473 writeLCDReg(0x5B, 0x0C);
474 writeLCDReg(0x5C, 0x1D);
475 writeLCDReg(0x5D, 0xCC);
476
477 /* Power voltage setting ---------------------------------------------------*/
478 writeLCDReg(0x1B, 0x1B);
479 writeLCDReg(0x1A, 0x01);
480 writeLCDReg(0x24, 0x2F);
481 writeLCDReg(0x25, 0x57);
482 writeLCDReg(0x23, 0x88);
483
484 /* Power on setting --------------------------------------------------------*/
485 writeLCDReg(0x18, 0x36); /* Internal oscillator frequency adj */
486 writeLCDReg(0x19, 0x01); /* Enable internal oscillator */
487 writeLCDReg(0x01, 0x00); /* Normal mode, no scrool */
488 writeLCDReg(0x1F, 0x88); /* Power control 6 - DDVDH Off */
489 delay(20);
490 writeLCDReg(0x1F, 0x82); /* Power control 6 - Step-up: 3 x VCI */
491 delay(5);
492 writeLCDReg(0x1F, 0x92); /* Power control 6 - Step-up: On */
493 delay(5);
494 writeLCDReg(0x1F, 0xD2); /* Power control 6 - VCOML active */
495 delay(5);
496
497 /* Color selection ---------------------------------------------------------*/
498 writeLCDReg(0x17, 0x55); /* RGB, System interface: 16 Bit/Pixel*/
499 writeLCDReg(0x00, 0x00); /* Scrolling off, no standby */
500
501 /* Interface config --------------------------------------------------------*/
502 writeLCDReg(0x2F, 0x11); /* LCD Drive: 1-line inversion */
503 writeLCDReg(0x31, 0x02); /* Value for SPI: 0x00, RBG: 0x02 */
504 writeLCDReg(0x32, 0x00); /* DPL=0, HSPL=0, VSPL=0, EPL=0 */
505
506 /* Display on setting ------------------------------------------------------*/
507 writeLCDReg(0x28, 0x38); /* PT(0,0) active, VGL/VGL */
508 delay(20);
509 writeLCDReg(0x28, 0x3C); /* Display active, VGL/VGL */
510
511 writeLCDReg(0x16, 0x00); /* Mem Access Control (MX/Y/V/L,BGR) */
512
513 /* Display scrolling settings ----------------------------------------------*/
514 writeLCDReg(0x0E, 0x00); /* TFA MSB */
515 writeLCDReg(0x0F, 0x00); /* TFA LSB */
516 writeLCDReg(0x10, 320 >> 8); /* VSA MSB */
517 writeLCDReg(0x11, 320 & 0xFF); /* VSA LSB */
518 writeLCDReg(0x12, 0x00); /* BFA MSB */
519 writeLCDReg(0x13, 0x00); /* BFA LSB */
520
521 }
522
523 /* Initializes the STMPE811 touch screen controller */
524 void Board_InitTouchController(void)
525 {
526 volatile int32_t i;
527 I2C_EVENTHANDLER_T old = Chip_I2C_GetMasterEventHandler(UDA1380_I2C_BUS);
528
529 /* Initialize Pin mux and other board related I2C stuff */
530 Board_I2C_Init(TSC_I2C_BUS);
531
532 /* Init I2C */
533 Chip_I2C_Init(TSC_I2C_BUS);
534 Chip_I2C_SetClockRate(TSC_I2C_BUS, 100000);
535 Chip_I2C_SetMasterEventHandler(TSC_I2C_BUS, Chip_I2C_EventHandlerPolling);
536
537 /* Reset Touch-screen controller */
538 writeTSCReg(SYS_CTRL1, 0x02);
539
540 for (i = 0; i < 200000; i++) {}
541
542 /* Enable TSC and ADC */
543 writeTSCReg(SYS_CTRL2, 0x0C);
544 /* Enable Touch detect, FIFO */
545 writeTSCReg(INT_EN, 0x07);
546 /* Set sample time , 12-bit mode */
547 writeTSCReg(ADC_CTRL1, 0x69);
548
549 for (i = 0; i < 40000; i++) {}
550
551 /* ADC frequency 3.25 MHz */
552 writeTSCReg(ADC_CTRL2, 0x01);
553 /* Tracking index: 8, operation mode : XY only */
554 writeTSCReg(TSC_CTRL, 0x22);
555 /* Detect delay 10us Settle time 500us*/
556 writeTSCReg(TSC_CFG, 0xC2);
557 /* Threshold for FIFO */
558 writeTSCReg(FIFO_TH, 0x01);
559 /* FIFO reset */
560 writeTSCReg(FIFO_STA, 0x01);
561 /* FIFO not reset */
562 writeTSCReg(FIFO_STA, 0x00);
563 /* Drive 50 mA typical */
564 writeTSCReg(TSC_I_DRIVE, 0x01);
565 /* Pins are used for touchscreen */
566 writeTSCReg(GPIO_ALT_FUNCT, 0x00);
567 /* Enable TSC */
568 writeTSCReg(TSC_CTRL, 0x01);
569 /* Clear interrupt status */
570 writeTSCReg(INT_STA, 0xFF);
571 /* Restore I2C to its default mode */
572 Chip_I2C_SetMasterEventHandler(TSC_I2C_BUS, old);
573 }
574
575 /* Poll for Touch position */
576 bool Board_GetTouchPos(int16_t *pX, int16_t *pY)
577 {
578 bool res;
579 I2C_EVENTHANDLER_T old = Chip_I2C_GetMasterEventHandler(UDA1380_I2C_BUS);
580
581 /* It is very unlikely that this will loop more than once */
582 while (!Chip_I2C_SetMasterEventHandler(TSC_I2C_BUS, Chip_I2C_EventHandlerPolling)) {}
583 res = Board_I2C_GetTouchPos(pX, pY);
584 /* It is very unlikely that this will loop more than once */
585 while (!Chip_I2C_SetMasterEventHandler(TSC_I2C_BUS, old)) {}
586 return res;
587 }
588
589 /* Get touch screen position */
590 bool Board_I2C_GetTouchPos(int16_t *pX, int16_t *pY)
591 {
592 int16_t x, y, rng;
593 if (detectTSCTouch()) {
594 getTSCCoord(&x, &y);
595 g_isPenDn = 1;
596 g_isNewPenDn = 1;
597
598 /* calibrate X */
599 rng = ad_right - ad_left;
600 if (rng < 0) {
601 rng = -rng;
602 }
603 x -= (ad_right < ad_left) ? ad_right : ad_left;
604 *pX = (x * C_GLCD_H_SIZE) / rng;
605 if (ad_left > ad_right) {
606 *pX = C_GLCD_H_SIZE - *pX;
607 }
608
609 /* calibrate Y */
610 rng = ad_bottom - ad_top;
611 if (rng < 0) {
612 rng = -rng;
613 }
614 y -= (ad_bottom < ad_top) ? ad_bottom : ad_top;
615 *pY = (y * C_GLCD_V_SIZE) / rng;
616 if (ad_top > ad_bottom) {
617 *pY = C_GLCD_V_SIZE - *pY;
618 }
619 }
620 else {
621 g_isPenDn = 0;
622 }
623
624 if (g_isNewPenDn) {
625 g_isNewPenDn = 0;
626 if (*pX < 0) {
627 *pX = -*pX;
628 }
629 if (*pY < 0) {
630 *pY = -*pY;
631 }
632 return true;
633 }
634 return false;
635 }
636
637 /* Turn on LCD backlight */
638 void Board_SetLCDBacklight(uint8_t Intensity)
639 {
640 bool OnOff = (bool) (Intensity != 0);
641
642 Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 3, 8, OnOff);
643 }
644
645 #ifndef CORE_M0
646 /* PIN0 Interrupt not available in M0 core */
647 /* Interrupt handler for GPIO0 */
648 void GPIO0_IRQHandler(void)
649 {
650 static bool On;
651
652 if (Chip_GPIO_IntGetStatus(LPC_GPIO_PIN_INT, 0, 0, 0)) {
653 Chip_GPIO_IntClear(LPC_GPIO_PIN_INT, 0, 0);
654 On = (bool) !On;
655 Board_LED_Set(1, On);
656 }
657 }
658
659 /* Initializes board specific GPIO Interrupt */
660 void Board_GPIO_Int_Init()
661 {
662 Chip_SCU_PinMuxSet(0xF, 9, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC0)); /* PF.9 : POTI button */
663 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 7, 23, false); /* PF.9 -> GPIO7[23] : input */
664 Chip_SCU_GPIOIntPinSel(0, 7, 23);
665 Chip_GPIO_IntCmd(LPC_GPIO_PIN_INT, 0, 0, GPIOPININT_FALLING_EDGE); /* Configure GPIO0[7] to interrupt pin (SW2 switch) */
666
667 NVIC_EnableIRQ(PIN_INT0_IRQn); /* enable GPIO interrupt 0 */
668 }
669
670 #endif
671
672 /* Initializes SDMMC interface */
673 void Board_SDMMC_Init(void)
674 {
675 Chip_SCU_PinMuxSet(0xC, 4, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* PC.4 connected to SDIO_D0 */
676 Chip_SCU_PinMuxSet(0xC, 5, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* PC.5 connected to SDIO_D1 */
677 Chip_SCU_PinMuxSet(0xC, 6, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* PC.6 connected to SDIO_D2 */
678 Chip_SCU_PinMuxSet(0xC, 7, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* PC.7 connected to SDIO_D3 */
679
680 Chip_SCU_PinMuxSet(0xC, 8, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC7)); /* PC.4 connected to SDIO_CD */
681 Chip_SCU_PinMuxSet(0xC, 10, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* PC.10 connected to SDIO_CMD */
682 Chip_SCU_PinMuxSet(0xC, 0, (SCU_MODE_MODE_PULLUP | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC7));/* PC.0 connected to SDIO_CLK */
683 }
684
685 /* Initializes SSP interface */
686 void Board_SSP_Init(LPC_SSP_T *pSSP)
687 {
688 if (pSSP == LPC_SSP0) {
689 /* Set up clock and power for SSP0 module */
690 /* Configure SSP0 pins*/
691 Chip_SCU_PinMuxSet(0x3, 3, (SCU_PINIO_FAST | SCU_MODE_FUNC2)); /* P3.3 connected to SCL/SCLK SCU_MODE_FUNC2=SSP0 SCK0 */
692 Chip_SCU_PinMuxSet(0x3, 6, (SCU_PINIO_FAST | SCU_MODE_FUNC2)); /* P3.6 connected to nCS SCU_MODE_FUNC2=SSP0 SSEL0 */
693 Chip_SCU_PinMuxSet(0x3, 7, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2)); /* P3.7 connected to SO SCU_MODE_FUNC2=SSP0 MISO0 */
694 Chip_SCU_PinMuxSet(0x3, 8, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2)); /* P3.8 connected to nSI SCU_MODE_FUNC2=SSP0 MOSI0 */
695 }
696 else if (pSSP == LPC_SSP1) {
697 /* Set up clock and power for SSP1 module */
698 /* Configure SSP1 pins*/
699 Chip_SCU_PinMuxSet(0xF, 4, (SCU_PINIO_FAST | SCU_MODE_FUNC0)); /* PF.4 connected to SCL/SCLK SCU_MODE_FUNC0 = SSP1 SCK1 */
700 Chip_SCU_PinMuxSet(0xF, 5, (SCU_PINIO_FAST | SCU_MODE_FUNC2)); /* PF.5 connected to nCS SCU_MODE_FUNC2 = SSP1 SSEL1 */
701 Chip_SCU_PinMuxSet(0xF, 6, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2)); /* PF.6 connected to SO SCU_MODE_FUNC2 = SSP1 MISO1 */
702 Chip_SCU_PinMuxSet(0xF, 7, (SCU_MODE_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2)); /* PF.7 connected to nSI SCU_MODE_FUNC2 = SSP1 MOSI1 */
703 }
704 else {
705 return;
706 }
707 }
708
709 /* Initializes board specific buttons */
710 void Board_Buttons_Init(void)
711 {
712 Chip_SCU_PinMuxSet(0x4, 0, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC0)); /* P9.1 : LED 6 */
713 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, BUTTONS_BUTTON1_GPIO_PORT_NUM, BUTTONS_BUTTON1_GPIO_BIT_NUM, false);
714 }
715
716 /* Sets up default states for joystick */
717 void Board_Joystick_Init(void)
718 {
719 Chip_SCU_PinMuxSet(0xC, 9, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC4)); /* PC_9 as GPIO6[8] */
720 Chip_SCU_PinMuxSet(0xC, 11, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC4)); /* PC_11 as GPIO6[10] */
721 Chip_SCU_PinMuxSet(0xC, 12, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC4)); /* PC_12 as GPIO6[11] */
722 Chip_SCU_PinMuxSet(0xC, 13, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC4)); /* PC_13 as GPIO6[12] */
723 Chip_SCU_PinMuxSet(0xC, 14, (SCU_MODE_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC4)); /* PC_14 as GPIO6[13] */
724
725 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, JOYSTICK_UP_GPIO_PORT_NUM, JOYSTICK_UP_GPIO_BIT_NUM, false); /* input */
726 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, JOYSTICK_DOWN_GPIO_PORT_NUM, JOYSTICK_DOWN_GPIO_BIT_NUM, false); /* input */
727 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, JOYSTICK_LEFT_GPIO_PORT_NUM, JOYSTICK_LEFT_GPIO_BIT_NUM, false); /* input */
728 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, JOYSTICK_RIGHT_GPIO_PORT_NUM, JOYSTICK_RIGHT_GPIO_BIT_NUM, false); /* input */
729 Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, JOYSTICK_PRESS_GPIO_PORT_NUM, JOYSTICK_PRESS_GPIO_BIT_NUM, false); /* input */
730 }
731
732 /* Gets joystick status */
733 uint8_t Joystick_GetStatus(void)
734 {
735
736 uint8_t ret = NO_BUTTON_PRESSED;
737
738 if (Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, JOYSTICK_UP_GPIO_PORT_NUM, JOYSTICK_UP_GPIO_BIT_NUM) == 0) {
739 ret |= JOY_UP;
740 }
741 else if (Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, JOYSTICK_DOWN_GPIO_PORT_NUM, JOYSTICK_DOWN_GPIO_BIT_NUM) == 0) {
742 ret |= JOY_DOWN;
743 }
744 else if (Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, JOYSTICK_LEFT_GPIO_PORT_NUM, JOYSTICK_LEFT_GPIO_BIT_NUM) == 0) {
745 ret |= JOY_LEFT;
746 }
747 else if (Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, JOYSTICK_RIGHT_GPIO_PORT_NUM, JOYSTICK_RIGHT_GPIO_BIT_NUM) == 0) {
748 ret |= JOY_RIGHT;
749 }
750 else if (Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, JOYSTICK_PRESS_GPIO_PORT_NUM, JOYSTICK_PRESS_GPIO_BIT_NUM) == 0) {
751 ret |= JOY_PRESS;
752 }
753
754 return ret;
755 }
756
757 /* Gets buttons status */
758 uint32_t Buttons_GetStatus(void)
759 {
760 uint8_t ret = NO_BUTTON_PRESSED;
761
762 if (Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, BUTTONS_BUTTON1_GPIO_PORT_NUM, BUTTONS_BUTTON1_GPIO_BIT_NUM) == 0) {
763 ret |= BUTTONS_BUTTON1;
764 }
765 return ret;
766 }
767
768 /* Initialize DAC interface for the board */
769 void Board_DAC_Init(LPC_DAC_T *pDAC)
770 {
771 Chip_SCU_DAC_Analog_Config();
772 }
773
774 /* FIXME Should we remove this function? */
775 void Serial_CreateStream(void *Stream)
776 {
777 // implement later
778 }
779