ea4357.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. #include "chip.h"
  27. #include "../board.h"
  28. #include "pca9532.h"
  29. #define UART_DEV LPC_USART0
  30. #define UART_PORT 0x0f
  31. #define UART_PIN_TX 10
  32. #define UART_PIN_RX 11
  33. // P9_1 joystick down
  34. #define BUTTON_PORT 4
  35. #define BUTTON_PIN 13
  36. //static const struct {
  37. // uint8_t mux_port;
  38. // uint8_t mux_pin;
  39. //
  40. // uint8_t gpio_port;
  41. // uint8_t gpio_pin;
  42. //}buttons[] =
  43. //{
  44. // {0x0a, 3, 4, 10 }, // Joystick up
  45. // {0x09, 1, 4, 13 }, // Joystick down
  46. // {0x0a, 2, 4, 9 }, // Joystick left
  47. // {0x09, 0, 4, 12 }, // Joystick right
  48. // {0x0a, 1, 4, 8 }, // Joystick press
  49. // {0x02, 7, 0, 7 }, // SW6
  50. //};
  51. /*------------------------------------------------------------------*/
  52. /* BOARD API
  53. *------------------------------------------------------------------*/
  54. /* System configuration variables used by chip driver */
  55. const uint32_t OscRateIn = 12000000;
  56. const uint32_t ExtRateIn = 0;
  57. static const PINMUX_GRP_T pinmuxing[] =
  58. {
  59. // Button ( Joystick down )
  60. {0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP)},
  61. // UART
  62. {UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC1},
  63. {UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC1},
  64. // USB
  65. };
  66. /* Pin clock mux values, re-used structure, value in first index is meaningless */
  67. static const PINMUX_GRP_T pinclockmuxing[] =
  68. {
  69. {0, 0, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  70. {0, 1, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  71. {0, 2, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  72. {0, 3, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  73. };
  74. // Invoked by startup code
  75. void SystemInit(void)
  76. {
  77. #ifdef __USE_LPCOPEN
  78. extern void (* const g_pfnVectors[])(void);
  79. unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08;
  80. *pSCB_VTOR = (unsigned int) g_pfnVectors;
  81. #if __FPU_USED == 1
  82. fpuInit();
  83. #endif
  84. #endif // __USE_LPCOPEN
  85. /* Setup system level pin muxing */
  86. Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
  87. /* Clock pins only, group field not used */
  88. for (int i = 0; i <(int) (sizeof(pinclockmuxing) / sizeof(pinclockmuxing[0])); i++)
  89. {
  90. Chip_SCU_ClockPinMuxSet(pinclockmuxing[i].pinnum, pinclockmuxing[i].modefunc);
  91. }
  92. Chip_SetupXtalClocking();
  93. }
  94. void board_init(void)
  95. {
  96. SystemCoreClockUpdate();
  97. #if CFG_TUSB_OS == OPT_OS_NONE
  98. // 1ms tick timer
  99. SysTick_Config(SystemCoreClock / 1000);
  100. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  101. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  102. //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
  103. #endif
  104. Chip_GPIO_Init(LPC_GPIO_PORT);
  105. // LED via pca9532 I2C
  106. Chip_SCU_I2C0PinConfig(I2C0_STANDARD_FAST_MODE);
  107. Chip_I2C_Init(I2C0);
  108. Chip_I2C_SetClockRate(I2C0, 100000);
  109. Chip_I2C_SetMasterEventHandler(I2C0, Chip_I2C_EventHandlerPolling);
  110. pca9532_init();
  111. // Button
  112. Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN);
  113. //------------- UART -------------//
  114. Chip_UART_Init(UART_DEV);
  115. Chip_UART_SetBaud(UART_DEV, CFG_BOARD_UART_BAUDRATE);
  116. Chip_UART_ConfigData(UART_DEV, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS);
  117. Chip_UART_TXEnable(UART_DEV);
  118. //------------- USB -------------//
  119. enum {
  120. USBMODE_DEVICE = 2,
  121. USBMODE_HOST = 3
  122. };
  123. enum {
  124. USBMODE_VBUS_LOW = 0,
  125. USBMODE_VBUS_HIGH = 1
  126. };
  127. /* From EA4357 user manual
  128. *
  129. * USB0 Device operation:
  130. * - Insert jumpers in position 1-2 in JP17/JP18/JP19.
  131. * - GPIO28 controls USB connect functionality
  132. * - LED32 lights when the USB Device is connected. SJ4 has pads 1-2 shorted by default.
  133. * - LED33 is controlled by GPIO27 and signals USB-up state. GPIO54 is used for VBUS
  134. * sensing.
  135. *
  136. * USB0 Host operation:
  137. * - insert jumpers in position 2-3 in JP17/JP18/JP19.
  138. * - USB Host power is controlled via distribution switch U20 (found in schematic page 11).
  139. * - Signal GPIO26 is active low and enables +5V on VBUS2.
  140. * - LED35 light whenever +5V is present on VBUS2.
  141. * - GPIO55 is connected to status feedback from the distribution switch.
  142. * - GPIO54 is used for VBUS sensing. 15Kohm pull-down resistors are always active
  143. *
  144. * Note:
  145. * - Insert jumpers in position 2-3 in JP17/JP18/JP19
  146. * - Insert jumpers in JP31 (OTG)
  147. */
  148. #if CFG_TUSB_RHPORT0_MODE
  149. Chip_USB0_Init();
  150. #endif
  151. /* From EA4357 user manual
  152. *
  153. * For USB1 Device:
  154. * - a 1.5Kohm pull-up resistor is needed on the USB DP data signal. There are two methods to create this.
  155. * JP15 is inserted and the pull-up resistor is always enabled. Alternatively, the pull-up resistor is activated
  156. * inside the USB OTG chip (U31), and this has to be done via the I2C interface of GPIO52/GPIO53. In the latter case,
  157. * JP15 shall not be inserted.
  158. * - J19 is the connector to use when USB Device is used. Normally it should be a USB-B connector for
  159. * creating a USB Device interface, but the mini-AB connector can also be used in this case. The status
  160. * of VBUS can be read via U31.
  161. * - JP16 shall not be inserted.
  162. *
  163. * For USB1 Host:
  164. * - 15Kohm pull-down resistors are needed on the USB data signals. These are activated inside the USB OTG chip (U31),
  165. * and this has to be done via the I2C interface of GPIO52/GPIO53.
  166. * - J20 is the connector to use when USB Host is used. In order to provide +5V to the external USB
  167. * device connected to this connector (J20), channel A of U20 must be enabled. It is enabled by default
  168. * since SJ5 is normally connected between pin 1-2.
  169. * - LED34 lights green when +5V is available on J20.
  170. * - JP15 shall not be inserted. JP16 has no effect
  171. */
  172. #if CFG_TUSB_RHPORT1_MODE
  173. Chip_USB1_Init();
  174. #endif
  175. // USB0 Vbus Power: P2_3 on EA4357 channel B U20 GPIO26 active low (base board)
  176. Chip_SCU_PinMuxSet(2, 3, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC7);
  177. #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE
  178. // P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low.
  179. Chip_SCU_PinMuxSet(9, 5, SCU_MODE_PULLDOWN | SCU_MODE_FUNC4);
  180. Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 5, 18);
  181. #endif
  182. // USB1 Power: EA4357 channel A U20 is enabled by SJ5 connected to pad 1-2, no more action required
  183. // TODO Remove R170, R171, solder a pair of 15K to USB1 D+/D- to test with USB1 Host
  184. }
  185. //--------------------------------------------------------------------+
  186. // USB Interrupt Handler
  187. //--------------------------------------------------------------------+
  188. void USB0_IRQHandler(void)
  189. {
  190. #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST
  191. tuh_int_handler(0);
  192. #endif
  193. #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE
  194. tud_int_handler(0);
  195. #endif
  196. }
  197. void USB1_IRQHandler(void)
  198. {
  199. #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST
  200. tuh_int_handler(1);
  201. #endif
  202. #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE
  203. tud_int_handler(1);
  204. #endif
  205. }
  206. //--------------------------------------------------------------------+
  207. // Board porting API
  208. //--------------------------------------------------------------------+
  209. void board_led_write(bool state)
  210. {
  211. if (state)
  212. {
  213. pca9532_setLeds( LED1, 0 );
  214. }else
  215. {
  216. pca9532_setLeds( 0, LED1);
  217. }
  218. }
  219. uint32_t board_button_read(void)
  220. {
  221. // active low
  222. return Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN) ? 0 : 1;
  223. }
  224. int board_uart_read(uint8_t* buf, int len)
  225. {
  226. //return UART_ReceiveByte(BOARD_UART_DEV);
  227. (void) buf; (void) len;
  228. return 0;
  229. }
  230. int board_uart_write(void const * buf, int len)
  231. {
  232. uint8_t const* buf8 = (uint8_t const*) buf;
  233. for(int i=0; i<len; i++)
  234. {
  235. while ((Chip_UART_ReadLineStatus(UART_DEV) & UART_LSR_THRE) == 0) {}
  236. Chip_UART_SendByte(UART_DEV, buf8[i]);
  237. }
  238. return len;
  239. }
  240. #if CFG_TUSB_OS == OPT_OS_NONE
  241. volatile uint32_t system_ticks = 0;
  242. void SysTick_Handler (void)
  243. {
  244. system_ticks++;
  245. }
  246. uint32_t board_millis(void)
  247. {
  248. return system_ticks;
  249. }
  250. #endif