1 /*
2  * @brief LPCXPresso LPC1549 board file
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2014
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 "retarget.h"
34 
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
39 /*****************************************************************************
40  * Public types/enumerations/variables
41  ****************************************************************************/
42 
43 /* System oscillator rate and RTC oscillator rate */
44 const uint32_t OscRateIn = 12000000;
45 const uint32_t RTCOscRateIn = 32768;
46 
47 /*****************************************************************************
48  * Private functions
49  ****************************************************************************/
50 
51 /*****************************************************************************
52  * Public functions
53  ****************************************************************************/
54 
55 /* Sends a character on the UART */
Board_UARTPutChar(char ch)56 void Board_UARTPutChar(char ch)
57 {
58 #if defined(DEBUG_UART)
59 	Chip_UART_SendBlocking(DEBUG_UART, &ch, 1);
60 #endif
61 }
62 
63 /* Gets a character from the UART, returns EOF if no character is ready */
Board_UARTGetChar(void)64 int Board_UARTGetChar(void)
65 {
66 #if defined(DEBUG_UART)
67 	uint8_t data;
68 
69 	if (Chip_UART_Read(DEBUG_UART, &data, 1) == 1) {
70 		return (int) data;
71 	}
72 #endif
73 	return EOF;
74 }
75 
76 /* Outputs a string on the debug UART */
Board_UARTPutSTR(char * str)77 void Board_UARTPutSTR(char *str)
78 {
79 #if defined(DEBUG_UART)
80 	while (*str != '\0') {
81 		Board_UARTPutChar(*str++);
82 	}
83 #endif
84 }
85 
86 /* Initialize debug output via UART for board */
Board_Debug_Init(void)87 void Board_Debug_Init(void)
88 {
89 #if defined(DEBUG_UART)
90 	/* Disables pullups/pulldowns and enable digitial mode */
91 	Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 13, (IOCON_MODE_INACT | IOCON_DIGMODE_EN));
92 	Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 18, (IOCON_MODE_INACT | IOCON_DIGMODE_EN));
93 
94 	/* UART signal muxing via SWM */
95 	Chip_SWM_MovablePortPinAssign(SWM_UART0_RXD_I, 0, 13);
96 	Chip_SWM_MovablePortPinAssign(SWM_UART0_TXD_O, 0, 18);
97 
98 	/* Use main clock rate as base for UART baud rate divider */
99 	Chip_Clock_SetUARTBaseClockRate(Chip_Clock_GetMainClockRate(), false);
100 
101 	/* Setup UART */
102 	Chip_UART_Init(DEBUG_UART);
103 	Chip_UART_ConfigData(DEBUG_UART, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1);
104 	Chip_UART_SetBaud(DEBUG_UART, 115200);
105 	Chip_UART_Enable(DEBUG_UART);
106 	Chip_UART_TXEnable(DEBUG_UART);
107 #endif
108 }
109 
110 #define MAXLEDS 3
111 static const uint8_t ledpins[MAXLEDS] = {25, 3, 1};
112 static const uint8_t ledports[MAXLEDS] = {0, 0, 1};
113 
114 /* Initializes board LED(s) */
Board_LED_Init(void)115 static void Board_LED_Init(void)
116 {
117 	int idx;
118 
119 	for (idx = 0; idx < MAXLEDS; idx++) {
120 		/* Set the GPIO as output with initial state off (high) */
121 		Chip_GPIO_SetPinDIROutput(LPC_GPIO, ledports[idx], ledpins[idx]);
122 		Chip_GPIO_SetPinState(LPC_GPIO, ledports[idx], ledpins[idx], true);
123 	}
124 }
125 
126 /* Sets the state of a board LED to on or off */
Board_LED_Set(uint8_t LEDNumber,bool On)127 void Board_LED_Set(uint8_t LEDNumber, bool On)
128 {
129 	if (LEDNumber < MAXLEDS) {
130 		/* Toggle state, low is on, high is off */
131 		Chip_GPIO_SetPinState(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber], !On);
132 	}
133 }
134 
135 /* Returns the current state of a board LED */
Board_LED_Test(uint8_t LEDNumber)136 bool Board_LED_Test(uint8_t LEDNumber)
137 {
138 	bool state = false;
139 
140 	if (LEDNumber < MAXLEDS) {
141 		state = !Chip_GPIO_GetPinState(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber]);
142 	}
143 
144 	return state;
145 }
146 
147 /* Toggles the current state of a board LED */
Board_LED_Toggle(uint8_t LEDNumber)148 void Board_LED_Toggle(uint8_t LEDNumber)
149 {
150 	Chip_GPIO_SetPinToggle(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber]);
151 }
152 
153 /* Set up and initialize all required blocks and functions related to the
154    board hardware */
Board_Init(void)155 void Board_Init(void)
156 {
157 	/* Sets up DEBUG UART */
158 	DEBUGINIT();
159 
160 	/* Initialize GPIO */
161 	Chip_GPIO_Init(LPC_GPIO);
162 
163 	/* Initialize LEDs */
164 	Board_LED_Init();
165 }
166 
167 /* Ordered up, down, left, right, press */
168 #define NUM_BUTTONS 5
169 static const uint8_t portButton[NUM_BUTTONS] = {1, 1, 1, 1, 1};
170 static const uint8_t pinButton[NUM_BUTTONS] = {4, 6, 8, 7, 5};
171 static const uint8_t stateButton[NUM_BUTTONS] = {JOY_UP, JOY_DOWN, JOY_LEFT,
172 												 JOY_RIGHT, JOY_PRESS};
173 
174 /* Initialize Joystick */
Board_Joystick_Init(void)175 void Board_Joystick_Init(void)
176 {
177 	int i;
178 
179 	/* IOCON states already selected in SystemInit(), GPIO setup only. Pullups
180 	   are external, so IOCON with no states */
181 	for (i = 0; i < NUM_BUTTONS; i++) {
182 		Chip_GPIO_SetPinDIRInput(LPC_GPIO, portButton[i], pinButton[i]);
183 	}
184 }
185 
186 /* Get Joystick status */
Joystick_GetStatus(void)187 uint8_t Joystick_GetStatus(void)
188 {
189 	uint8_t i, ret = 0;
190 
191 	for (i = 0; i < NUM_BUTTONS; i++) {
192 		if ((Chip_GPIO_GetPinState(LPC_GPIO, portButton[i], pinButton[i])) == 0x00) {
193 			ret |= stateButton[i];
194 		}
195 	}
196 
197 	return ret;
198 }
199