LPCOpen Platform for LPC18XX/43XX microcontrollers: C:/w/lpcopen_docs/software/lpc_core/lpc_board/boards_43xx/nxp_lpcxpresso_4337/board.c Source File

| LPCOpen Platform for LPC18XX/43XX microcontrollers 18XX43XX LPCOpen Platform for the NXP LPC18XX/43XX family of Microcontrollers

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

board.c
Go to the documentation of this file.
1 /*
2 * Copyright(C) NXP Semiconductors, 2012
3 * All rights reserved.
4 *
5 * Software that is described herein is for illustrative purposes only
6 * which provides customers with programming information regarding the
7 * LPC products. This software is supplied "AS IS" without any warranties of
8 * any kind, and NXP Semiconductors and its licensor disclaim any and
9 * all warranties, express or implied, including all implied warranties of
10 * merchantability, fitness for a particular purpose and non-infringement of
11 * intellectual property rights. NXP Semiconductors assumes no responsibility
12 * or liability for the use of the software, conveys no license or rights under any
13 * patent, copyright, mask work right, or any other intellectual property rights in
14 * or to any products. NXP Semiconductors reserves the right to make changes
15 * in the software without notification. NXP Semiconductors also makes no
16 * representation or warranty that such application will be suitable for the
17 * specified use without further testing or modification.
18 *
19 * Permission to use, copy, modify, and distribute this software and its
20 * documentation is hereby granted, under NXP Semiconductors' and its
21 * licensor's relevant copyrights in the software, without fee, provided that it
22 * is used in conjunction with NXP Semiconductors microcontrollers. This
23 * copyright, permission, and disclaimer notice must appear in all copies of
24 * this code.
25 */
26
27 #include "board.h"
28 #include "string.h"
29
30 #include "retarget.h"
31 #include "wm8904.h"
32
37 /* SDIO Data pin configuration bits */
38 #define SDIO_DAT_PINCFG (SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_PULLUP | SCU_MODE_FUNC7)
39
40 /*****************************************************************************
41 * Public types/enumerations/variables
42 ****************************************************************************/
43
44 /* System configuration variables used by chip driver */
45 const uint32_t ExtRateIn = 0;
46 const uint32_t OscRateIn = 12000000;
47
48 typedef struct {
49 uint8_t port;
50 uint8_t pin;
51 } io_port_t;
52
53 static const io_port_t gpioLEDBits[] = {{3, 5}, {0, 7}, {3, 7}};
54 static uint32_t lcd_cfg_val;
55
56 void Board_UART_Init(LPC_USART_T *pUART)
57 {
58 Chip_SCU_PinMuxSet(0x6, 4, (SCU_MODE_INACT | SCU_MODE_FUNC2)); /* P6,4 : UART0_TXD */
59 Chip_SCU_PinMuxSet(0x2, 1, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC1));/* P2.1 : UART0_RXD */
60 }
61
62 /* Initialize debug output via UART for board */
63 void Board_Debug_Init(void)
64 {
65 #if defined(DEBUG_UART)
66 Board_UART_Init(DEBUG_UART);
67
68 Chip_UART_Init(DEBUG_UART);
69 Chip_UART_SetBaudFDR(DEBUG_UART, 115200);
70 Chip_UART_ConfigData(DEBUG_UART, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS);
71
72 /* Enable UART Transmit */
73 Chip_UART_TXEnable(DEBUG_UART);
74 #endif
75 }
76
77 /* Sends a character on the UART */
78 void Board_UARTPutChar(char ch)
79 {
80 #if defined(DEBUG_UART)
81 /* Wait for space in FIFO */
82 while ((Chip_UART_ReadLineStatus(DEBUG_UART) & UART_LSR_THRE) == 0) {}
83 Chip_UART_SendByte(DEBUG_UART, (uint8_t) ch);
84 #endif
85 }
86
87 /* Gets a character from the UART, returns EOF if no character is ready */
88 int Board_UARTGetChar(void)
89 {
90 #if defined(DEBUG_UART)
91 if (Chip_UART_ReadLineStatus(DEBUG_UART) & UART_LSR_RDR) {
92 return (int) Chip_UART_ReadByte(DEBUG_UART);
93 }
94 #endif
95 return EOF;
96 }
97
98 /* Outputs a string on the debug UART */
99 void Board_UARTPutSTR(const char *str)
100 {
101 #if defined(DEBUG_UART)
102 while (*str != '\0') {
103 Board_UARTPutChar(*str++);
104 }
105 #endif
106 }
107
108 static void Board_LED_Init()
109 {
110 uint32_t idx;
111
112 for (idx = 0; idx < (sizeof(gpioLEDBits) / sizeof(io_port_t)); ++idx) {
113 /* Set pin direction and init to off */
114 Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, gpioLEDBits[idx].port, gpioLEDBits[idx].pin);
115 Chip_GPIO_SetPinState(LPC_GPIO_PORT, gpioLEDBits[idx].port, gpioLEDBits[idx].pin, (bool) true);
116 }
117 }
118
119 void Board_LED_Set(uint8_t LEDNumber, bool On)
120 {
121 if (LEDNumber < (sizeof(gpioLEDBits) / sizeof(io_port_t)))
122 Chip_GPIO_SetPinState(LPC_GPIO_PORT, gpioLEDBits[LEDNumber].port, gpioLEDBits[LEDNumber].pin, (bool) !On);
123 }
124
125 bool Board_LED_Test(uint8_t LEDNumber)
126 {
127 if (LEDNumber < (sizeof(gpioLEDBits) / sizeof(io_port_t)))
128 return (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, gpioLEDBits[LEDNumber].port, gpioLEDBits[LEDNumber].pin);
129
130 return false;
131 }
132
133 void Board_LED_Toggle(uint8_t LEDNumber)
134 {
135 Board_LED_Set(LEDNumber, !Board_LED_Test(LEDNumber));
136 }
137
138 void Board_Buttons_Init(void)
139 {
140 Chip_SCU_PinMuxSet(0x2, 7, (SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC0)); // P2_7 as GPIO0[7]
141 Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTONS_BUTTON1_GPIO_PORT_NUM, BUTTONS_BUTTON1_GPIO_BIT_NUM); // input
142 }
143
144 uint32_t Buttons_GetStatus(void)
145 {
146 uint8_t ret = NO_BUTTON_PRESSED;
147 if (Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTONS_BUTTON1_GPIO_PORT_NUM, BUTTONS_BUTTON1_GPIO_BIT_NUM) == 0) {
148 ret |= BUTTONS_BUTTON1;
149 }
150 return ret;
151 }
152
153 void Board_Joystick_Init(void)
154 {}
155
156 uint8_t Joystick_GetStatus(void)
157 {
158 return NO_BUTTON_PRESSED;
159 }
160
161 /* Returns the MAC address assigned to this board */
162 void Board_ENET_GetMacADDR(uint8_t *mcaddr)
163 {
164 uint8_t boardmac[] = {0x00, 0x60, 0x37, 0x12, 0x34, 0x56};
165
166 memcpy(mcaddr, boardmac, 6);
167 }
168
169 /* Set up and initialize all required blocks and functions related to the
170 board hardware */
171 void Board_Init(void)
172 {
173 /* Sets up DEBUG UART */
174 DEBUGINIT();
175
176 /* Initializes GPIO */
177 Chip_GPIO_Init(LPC_GPIO_PORT);
178
179 /* Initialize LEDs */
180 Board_LED_Init();
181 Chip_ENET_RMIIEnable(LPC_ETHERNET);
182 }
183
184 void Board_I2C_Init(I2C_ID_T id)
185 {
186 if (id == I2C1) {
187 /* Configure pin function for I2C1*/
188 Chip_SCU_PinMuxSet(0x2, 3, (SCU_MODE_ZIF_DIS | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC1)); /* P2.3 : I2C1_SDA */
189 Chip_SCU_PinMuxSet(0x2, 4, (SCU_MODE_ZIF_DIS | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC1)); /* P2.4 : I2C1_SCL */
190 }
191 else {
192 Chip_SCU_I2C0PinConfig(I2C0_STANDARD_FAST_MODE);
193 }
194 }
195
196 void Board_SDMMC_Init(void)
197 {
198 Chip_SCU_PinMuxSet(0x1, 9, SDIO_DAT_PINCFG); /* P1.9 connected to SDIO_D0 */
199 Chip_SCU_PinMuxSet(0x1, 10, SDIO_DAT_PINCFG); /* P1.10 connected to SDIO_D1 */
200 Chip_SCU_PinMuxSet(0x1, 11, SDIO_DAT_PINCFG); /* P1.11 connected to SDIO_D2 */
201 Chip_SCU_PinMuxSet(0x1, 12, SDIO_DAT_PINCFG); /* P1.12 connected to SDIO_D3 */
202
203 Chip_SCU_ClockPinMuxSet(2, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC4)); /* CLK2 connected to SDIO_CLK */
204 Chip_SCU_PinMuxSet(0x1, 6, SDIO_DAT_PINCFG); /* P1.6 connected to SDIO_CMD */
205 Chip_SCU_PinMuxSet(0x1, 13, (SCU_MODE_INBUFF_EN | SCU_MODE_FUNC7)); /* P1.13 connected to SDIO_CD */
206 }
207
208 void Board_SSP_Init(LPC_SSP_T *pSSP)
209 {
210 if (pSSP == LPC_SSP1) {
211 Chip_SCU_PinMuxSet(0x1, 5, (SCU_PINIO_FAST | SCU_MODE_FUNC5)); /* P1.5 => SSEL1 */
212 Chip_SCU_PinMuxSet(0xF, 4, (SCU_PINIO_FAST | SCU_MODE_FUNC0)); /* PF.4 => SCK1 */
213
214 Chip_SCU_PinMuxSet(0x1, 4, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC5)); /* P1.4 => MOSI1 */
215 Chip_SCU_PinMuxSet(0x1, 3, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC5)); /* P1.3 => MISO1 */
216 }
217 else {
218 return;
219 }
220 }
221
222 /* Initialize DAC interface for the board */
223 void Board_DAC_Init(LPC_DAC_T *pDAC)
224 {
225 Chip_SCU_DAC_Analog_Config();
226 }
227
228 /* Initialize Audio Codec */
229 static Status Board_Audio_CodecInit(int micIn)
230 {
231
232 if (!WM8904_Init(micIn)){
233 return ERROR;
234 }
235
236 return SUCCESS;
237 }
238
239 /* Board Audio initialization */
240 void Board_Audio_Init(LPC_I2S_T *pI2S, int micIn)
241 {
242
243 if (pI2S == LPC_I2S0) {
244 /* TODO :Add pin mux for I2S0 later */
245 } else if (pI2S == LPC_I2S1) {
246 Chip_SCU_PinMuxSet (0x1, 19, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* I2S1_TX_SCK */
247 Chip_SCU_PinMuxSet (0x0, 1, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* I2S1_TX_SDA */
248 Chip_SCU_PinMuxSet (0x3, 4, (SCU_PINIO_FAST | SCU_MODE_FUNC6)); /* I2S1_RX_SDA */
249 Chip_SCU_PinMuxSet (0x0, 0, (SCU_PINIO_FAST | SCU_MODE_FUNC7)); /* I2S1_TX_WS */
250
251 Chip_SCU_ClockPinMuxSet(0,(SCU_MODE_FUNC1|SCU_MODE_INACT));
252 /* Setup base clock for CLKOUT */
253 Chip_Clock_SetBaseClock(CLK_BASE_OUT, CLKIN_CRYSTAL, false, false);
254 } else {
255 /* It is a BUG catch it */
256 while(1);
257 }
258 /* Init WM8904 CODEC */
259 while (Board_Audio_CodecInit(micIn) != SUCCESS) {}
260 }
261
262 /* Initialize Pin Muxing for LCD */
263 void Board_LCD_Init(void)
264 {
265 uint32_t val;
266
267 Board_SSP_Init(LCD_SSP);
268 val = LCD_SSP->CR0 & 0xFFFF;
269 Chip_SCU_PinMuxSet(LCD_CDM_PORT, LCD_CMD_PIN, LCD_CMD_CFG);
270 Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, LCD_CMD_GPIO_PORT, LCD_CMD_GPIO_PIN);
271 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, LCD_CMD_GPIO_PORT, LCD_CMD_GPIO_PIN);
272
273 /* Enable the SSP interface */
274 Chip_SSP_Init(LCD_SSP);
275 Chip_SSP_Set_Mode(LCD_SSP, SSP_MODE_MASTER);
276 Chip_SSP_SetFormat(LCD_SSP, SSP_BITS_8, SSP_FRAMEFORMAT_SPI, SSP_CLOCK_CPHA1_CPOL1);
277 Chip_SSP_SetBitRate(LCD_SSP, LCD_BIT_RATE);
278 Chip_SSP_Enable(LCD_SSP);
279
280 lcd_cfg_val = LCD_SSP->CR0 & 0xFFFF;
281 LCD_SSP->CR0 = val;
282 }
283
284 /* Write data to LCD module */
285 void Board_LCD_WriteData(const uint8_t *data, uint16_t size)
286 {
287 uint32_t val = LCD_SSP->CR0 & 0xFFFF;
288 LCD_SSP->CR0 = lcd_cfg_val;
289 Chip_SSP_WriteFrames_Blocking(LCD_SSP, data, size);
290 LCD_SSP->CR0 = val;
291 }
292