1 /*
2  * @brief LPCXPresso LPC1549 Sysinit 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 "string.h"
34 
35 /* The System initialization code is called prior to the application and
36    initializes the board for run-time operation. Board initialization
37    includes clock setup and default pin muxing configuration. */
38 
39 /*****************************************************************************
40  * Private types/enumerations/variables
41  ****************************************************************************/
42 
43 /* IOCON setup table, only items that need changing from their default pin
44    state are in this table. */
45 STATIC const PINMUX_GRP_T ioconSetup[] = {
46 	/* LEDs */
47 	{0, 25,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_25-BREAK_CTRL-RED (low enable) */
48 	{0, 3,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_3-SCT1_OUT4-GRN */
49 	{1, 1,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_1-BREAK_STS1-BLUE */
50 
51 	/* QEI, motor controler, I2C, CAN */
52 	{0, 2,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_2-QEI-SCT0_IN */
53 	{0, 30,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_30-QEI-SCT0_IN */
54 	{0, 17,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_17-QEI-SCT0_IN */
55 	{0, 25,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_25-BREAK_CTRL-RED */
56 	{1, 1,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_1-BREAK_STS1-BLUE */
57 	{0, 23,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_23-I2C_SDA */
58 	{0, 22,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_22-I2C_SCL */
59 	{0, 11,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_11-CAN_RD */
60 	{0, 31,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_31-CAN_TD */
61 
62 	/* ADC */
63 	{1, 3,   (IOCON_MODE_INACT)},							/* PIO1_3-ADC1_5 */
64 	{0, 4,   (IOCON_MODE_INACT)},							/* PIO0_4-ADC0_4 */
65 	{0, 5,   (IOCON_MODE_INACT)},							/* PIO0_5-ADC0_3 */
66 	{0, 7,   (IOCON_MODE_INACT)},							/* PIO0_7-ADC0_1 */
67 	{0, 8,   (IOCON_MODE_INACT)},							/* PIO0_8-ADC0_0 */
68 	{0, 9,   (IOCON_MODE_INACT)},							/* PIO0_9-ADC1_1 */
69 	{0, 10,  (IOCON_MODE_INACT)},							/* PIO0_10-ADC1_2 */
70 
71 	/* Joystick */
72 	{1, 4,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_4-JOY_U */
73 	{1, 5,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_5-JOY_C */
74 	{1, 6,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_6-JOY_D */
75 	{1, 7,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_7-JOY_R */
76 	{1, 8,   (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO1_8-JOY_L */
77 
78 	/* UART */
79 	{0, 13,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_13-ISP_RX */
80 	{0, 18,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},		/* PIO0_18-ISP_TX */
81 	{0, 11,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},
82 	{0, 31,  (IOCON_MODE_INACT | IOCON_DIGMODE_EN)},
83 
84 	/* USB related */
85 	{1, 11,  (IOCON_MODE_PULLDOWN | IOCON_DIGMODE_EN)},	/* PIO1_11-ISP_1 (VBUS) */
86 };
87 
88 /* SWIM pin assignment definitions for pin assignment/muxing */
89 typedef struct {
90 	uint16_t assignedpin : 9;		/* Function and mode */
91 	uint16_t port : 2;				/* Pin port */
92 	uint16_t pin : 5;				/* Pin number */
93 } SWM_GRP_T;
94 
95 /* Pin muxing table, only items that need changing from their default pin
96    state are in this table. */
97 STATIC const SWM_GRP_T swmSetup[] = {
98 	/* USB related */
99 	{(uint16_t) SWM_USB_VBUS_I, 1, 11},		/* PIO1_11-ISP_1-AIN_CTRL */
100 
101 	/* UART */
102 	{(uint16_t) SWM_UART0_RXD_I, 0, 13},		/* PIO0_13-ISP_RX */
103 	{(uint16_t) SWM_UART0_TXD_O, 0, 18},		/* PIO0_18-ISP_TX */
104 };
105 
106 /* Setup fixed pin functions (GPIOs are fixed) */
107 /* No fixed pins except GPIOs */
108 #define PINENABLE0_VAL 0xFFFFFFFF
109 
110 /* No fixed pins except GPIOs */
111 #define PINENABLE1_VAL 0x00FFFFFF
112 
113 /*****************************************************************************
114  * Public types/enumerations/variables
115  ****************************************************************************/
116 
117 /*****************************************************************************
118  * Private functions
119  ****************************************************************************/
120 
121 /*****************************************************************************
122  * Public functions
123  ****************************************************************************/
124 
125 /* Sets up system pin muxing */
Board_SetupMuxing(void)126 void Board_SetupMuxing(void)
127 {
128 	int i;
129 
130 	/* Enable SWM and IOCON clocks */
131 	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON);
132 	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
133 	Chip_SYSCTL_PeriphReset(RESET_IOCON);
134 
135 	/* IOCON setup */
136 	Chip_IOCON_SetPinMuxing(LPC_IOCON, ioconSetup, sizeof(ioconSetup) / sizeof(PINMUX_GRP_T));
137 
138 	/* SWM assignable pin setup */
139 	for (i = 0; i < (sizeof(swmSetup) / sizeof(SWM_GRP_T)); i++) {
140 		Chip_SWM_MovablePortPinAssign((CHIP_SWM_PIN_MOVABLE_T) swmSetup[i].assignedpin,
141 									  swmSetup[i].port, swmSetup[i].pin);
142 	}
143 
144 	/* SWM fixed pin setup */
145 	//	LPC_SWM->PINENABLE[0] = PINENABLE0_VAL;
146 	//	LPC_SWM->PINENABLE[1] = PINENABLE1_VAL;
147 
148 	/* Note SWM and IOCON clocks are left on */
149 }
150 
151 /* Set up and initialize clocking prior to call to main */
Board_SetupClocking(void)152 void Board_SetupClocking(void)
153 {
154 	Chip_SetupXtalClocking();
155 
156 	/* Set USB PLL input to main oscillator */
157 	Chip_Clock_SetUSBPLLSource(SYSCTL_PLLCLKSRC_MAINOSC);
158 	/* Setup USB PLL  (FCLKIN = 12MHz) * 4 = 48MHz
159 	   MSEL = 3 (this is pre-decremented), PSEL = 1 (for P = 2)
160 	   FCLKOUT = FCLKIN * (MSEL + 1) = 12MHz * 4 = 48MHz
161 	   FCCO = FCLKOUT * 2 * P = 48MHz * 2 * 2 = 192MHz (within FCCO range) */
162 	Chip_Clock_SetupUSBPLL(3, 1);
163 
164 	/* Powerup USB PLL */
165 	Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_USBPLL_PD);
166 
167 	/* Wait for PLL to lock */
168 	while (!Chip_Clock_IsUSBPLLLocked()) {}
169 
170 	/* Set default system tick divder to 1 */
171 	Chip_Clock_SetSysTickClockDiv(1);
172 }
173 
174 /* Set up and initialize hardware prior to call to main */
Board_SystemInit(void)175 void Board_SystemInit(void)
176 {
177 	/* Setup system clocking and muxing */
178 	Board_SetupMuxing();
179 	Board_SetupClocking();
180 
181 	/* Set SYSTICKDIV to 1 so CMSIS Systick functions work */
182 	LPC_SYSCTL->SYSTICKCLKDIV = 1;
183 }
184