board_lpc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * @brief NXP LPCXpresso LPC824 board file
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2013
  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. // Must define _BOARD_C_ before ANY include
  32. #define _BOARD_C_
  33. #include "board_lpc.h"
  34. #include "string.h"
  35. #include <stdint.h>
  36. /*****************************************************************************
  37. * Private types/enumerations/variables
  38. ****************************************************************************/
  39. #define BOARD_LED_CNT 8
  40. /*****************************************************************************
  41. * Public types/enumerations/variables
  42. ****************************************************************************/
  43. /* System oscillator rate and clock rate on the CLKIN pin */
  44. const uint32_t OscRateIn = MAIN_OSC_XTAL_FREQ_HZ;
  45. const uint32_t ExtRateIn = EXT_CLOCK_IN_FREQ_HZ;
  46. /*****************************************************************************
  47. * Private functions
  48. ****************************************************************************/
  49. static void Board_Key_Init(void)
  50. {
  51. int i;
  52. LPC_IOCON_T *pIOCON = LPC_IOCON;
  53. for (i = 0; i < BOARD_KEY_CNT; i++) {
  54. Chip_GPIO_PinSetDIR(LPC_GPIO_PORT, 0, cs_keyBits[i], 0);
  55. pIOCON->PIO0[cs_keyIoConNdce[i]] = 0x80; // weak pUp
  56. }
  57. }
  58. /* Initialize the LEDs on the NXP LPC824 LPCXpresso Board */
  59. static void Board_LED_Init(void)
  60. {
  61. int i;
  62. for (i = 0; i < BOARD_LED_CNT; i++) {
  63. Chip_GPIO_PinSetDIR(LPC_GPIO_PORT, 0, ledBits[i], 1);
  64. Chip_GPIO_PinSetState(LPC_GPIO_PORT, 0, ledBits[i], true);
  65. }
  66. }
  67. uint32_t Board_Key_GetKeyDown(uint32_t keyNdx)
  68. {
  69. LPC_GPIO_T *pGP = LPC_GPIO_PORT;
  70. return pGP->W[0][cs_keyBits[keyNdx]] == 0 ? 1 : 0;
  71. }
  72. /* Board Debug UART Initialisation function */
  73. STATIC void Board_UART_Init(void)
  74. {
  75. /* Enable the clock to the Switch Matrix */
  76. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
  77. /* Connect the TXD_O and RXD_I signals to port pins(P0.4, P0.0) */
  78. //Chip_SWM_DisableFixedPin(SWM_FIXED_XTALIN);
  79. //Chip_SWM_DisableFixedPin(SWM_FIXED_XTALOUT);
  80. Chip_SWM_DisableFixedPin(SWM_FIXED_ACMP_I1);
  81. Chip_SWM_DisableFixedPin(SWM_FIXED_ADC11);
  82. /* Enable UART Divider clock, divided by 1 */
  83. Chip_Clock_SetUARTClockDiv(1);
  84. /* Divided by 1 */
  85. if (DEBUG_UART == LPC_USART0) {
  86. Chip_SWM_MovablePinAssign(SWM_U0_TXD_O, 4);
  87. Chip_SWM_MovablePinAssign(SWM_U0_RXD_I, 0);
  88. } else if (DEBUG_UART == LPC_USART1) {
  89. Chip_SWM_MovablePinAssign(SWM_U1_TXD_O, 4);
  90. Chip_SWM_MovablePinAssign(SWM_U1_RXD_I, 0);
  91. } else {
  92. Chip_SWM_MovablePinAssign(SWM_U2_TXD_O, 4);
  93. Chip_SWM_MovablePinAssign(SWM_U2_RXD_I, 0);
  94. }
  95. /* Disable the clock to the Switch Matrix to save power */
  96. Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
  97. }
  98. /* Initializes pin muxing for SPI1 interface - note that SystemInit() may
  99. already setup your pin muxing at system startup */
  100. static void Init_SPI_PinMux(void)
  101. {
  102. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
  103. Chip_SWM_MovablePinAssign(SWM_SPI1_SSEL0_IO, 15);
  104. Chip_SWM_MovablePinAssign(SWM_SPI1_SCK_IO, 24);
  105. Chip_SWM_MovablePinAssign(SWM_SPI1_MISO_IO, 25);
  106. Chip_SWM_MovablePinAssign(SWM_SPI1_MOSI_IO, 26);
  107. Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
  108. }
  109. /* Initializes pin muxing for I2C interface */
  110. static void Init_I2C_PinMux(void)
  111. {
  112. /* Enable the clock to the Switch Matrix */
  113. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
  114. Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA);
  115. Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL);
  116. /* Enable Fast Mode Plus for I2C pins */
  117. Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO10, PIN_I2CMODE_FASTPLUS);
  118. Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO11, PIN_I2CMODE_FASTPLUS);
  119. /* Disable the clock to the Switch Matrix to save power */
  120. Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
  121. }
  122. /*****************************************************************************
  123. * Public functions
  124. ****************************************************************************/
  125. /* Set the LED to the state of "On" */
  126. void Board_LED_Set(uint8_t LEDNumber, bool On)
  127. {
  128. if (LEDNumber < BOARD_LED_CNT) {
  129. Chip_GPIO_PinSetState(LPC_GPIO_PORT, 0, ledBits[LEDNumber], (bool) !On);
  130. }
  131. }
  132. /* Return the state of LEDNumber */
  133. bool Board_LED_Test(uint8_t LEDNumber)
  134. {
  135. bool state = false;
  136. if (LEDNumber < BOARD_LED_CNT) {
  137. state = (bool) !Chip_GPIO_PinGetState(LPC_GPIO_PORT, 0, ledBits[LEDNumber]);
  138. }
  139. return state;
  140. }
  141. /* Toggles the current state of a board LED */
  142. void Board_LED_Toggle(uint8_t LEDNumber)
  143. {
  144. if (LEDNumber < BOARD_LED_CNT) {
  145. Chip_GPIO_PinToggleState(LPC_GPIO_PORT, 0, ledBits[LEDNumber]);
  146. }
  147. }
  148. /* Classic implementation of itoa -- integer to ASCII */
  149. char *Board_itoa(int value, char *result, int base)
  150. {
  151. char* ptr = result, *ptr1 = result, tmp_char;
  152. int tmp_value;
  153. if (base < 2 || base > 36) { *result = '\0'; return result; }
  154. do {
  155. tmp_value = value;
  156. value /= base;
  157. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
  158. } while ( value );
  159. if (tmp_value < 0) *ptr++ = '-';
  160. *ptr-- = '\0';
  161. while (ptr1 < ptr) {
  162. tmp_char = *ptr;
  163. *ptr--= *ptr1;
  164. *ptr1++ = tmp_char;
  165. }
  166. return result;
  167. }
  168. /* Sends a character on the UART */
  169. void Board_UARTPutChar(char ch)
  170. {
  171. #if defined(DEBUG_UART)
  172. Chip_UART_SendBlocking(DEBUG_UART, &ch, 1);
  173. #endif
  174. }
  175. void Board_UARTPutTextChar(char ch)
  176. {
  177. #if defined(DEBUG_UART)
  178. static char prevChar = 0;
  179. if (ch == '\n' && prevChar != '\r')
  180. {
  181. prevChar = '\r';
  182. Chip_UART_SendBlocking(DEBUG_UART, &prevChar, 1);
  183. Chip_UART_SendBlocking(DEBUG_UART, &ch, 1);
  184. } else {
  185. Chip_UART_SendBlocking(DEBUG_UART, &ch, 1);
  186. prevChar = ch;
  187. }
  188. #endif
  189. }
  190. /* Gets a character from the UART, returns EOF if no character is ready */
  191. int Board_UARTGetChar(void)
  192. {
  193. #if defined(DEBUG_UART)
  194. uint8_t data;
  195. if (Chip_UART_ReadBlocking(DEBUG_UART, &data, 1) == 1) {
  196. Board_UARTPutChar(data); // echo back the char
  197. return (int) data;
  198. }
  199. #endif
  200. return EOF;
  201. }
  202. /* Outputs a string on the debug UART */
  203. void Board_UARTPutSTR(const char *str)
  204. {
  205. #if defined(DEBUG_UART)
  206. while (*str != '\0') {
  207. Board_UARTPutTextChar(*str++);
  208. }
  209. #endif
  210. }
  211. /* Initialize debug output via UART for board */
  212. void Board_Debug_Init(void)
  213. {
  214. #if defined(DEBUG_UART)
  215. Board_UART_Init();
  216. Chip_UART_Init(DEBUG_UART);
  217. Chip_UART_ConfigData(DEBUG_UART, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1);
  218. Chip_Clock_SetUSARTNBaseClockRate((115200 * 6 * 16), true);
  219. Chip_UART_SetBaud(DEBUG_UART, 115200);
  220. Chip_UART_Enable(DEBUG_UART);
  221. Chip_UART_TXEnable(DEBUG_UART);
  222. #endif
  223. }
  224. /* Set up and initialize all required blocks and functions related to the
  225. board hardware */
  226. void Board_Init(void)
  227. {
  228. LPC_SWM_T *pSWM = LPC_SWM;
  229. /* Sets up DEBUG UART */
  230. #if defined(DEBUG_ENABLE)
  231. DEBUGINIT();
  232. //pSWM->PINENABLE0 |= ~(1UL<<SWM_FIXED_SWCLK | 1UL<<SWM_FIXED_SWDIO | 1UL<<SWM_FIXED_RST);
  233. #ifdef USE_IRC_AS_ROOT_CLOCK
  234. pSWM->PINENABLE0 |= 1UL<<SWM_FIXED_XTALIN | 1UL<<SWM_FIXED_XTALOUT;
  235. #endif
  236. #endif
  237. /* Initialize GPIO */
  238. Chip_GPIO_Init(LPC_GPIO_PORT);
  239. /* Initialize the LEDs */
  240. Board_LED_Init();
  241. //Board_Key_Init();
  242. Init_SPI_PinMux();
  243. Init_I2C_PinMux();
  244. }