ea4357.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #ifdef BOARD_TUD_RHPORT
  52. #define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
  53. #else
  54. #define PORT_SUPPORT_DEVICE(_n) 0
  55. #endif
  56. #ifdef BOARD_TUH_RHPORT
  57. #define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
  58. #else
  59. #define PORT_SUPPORT_HOST(_n) 0
  60. #endif
  61. /*------------------------------------------------------------------*/
  62. /* BOARD API
  63. *------------------------------------------------------------------*/
  64. /* System configuration variables used by chip driver */
  65. const uint32_t OscRateIn = 12000000;
  66. const uint32_t ExtRateIn = 0;
  67. static const PINMUX_GRP_T pinmuxing[] =
  68. {
  69. // Button ( Joystick down )
  70. {0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP)},
  71. // UART
  72. {UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC1},
  73. {UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC1},
  74. // USB
  75. };
  76. /* Pin clock mux values, re-used structure, value in first index is meaningless */
  77. static const PINMUX_GRP_T pinclockmuxing[] =
  78. {
  79. {0, 0, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  80. {0, 1, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  81. {0, 2, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  82. {0, 3, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)},
  83. };
  84. // Invoked by startup code
  85. void SystemInit(void)
  86. {
  87. #ifdef __USE_LPCOPEN
  88. extern void (* const g_pfnVectors[])(void);
  89. unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08;
  90. *pSCB_VTOR = (unsigned int) g_pfnVectors;
  91. #if __FPU_USED == 1
  92. fpuInit();
  93. #endif
  94. #endif // __USE_LPCOPEN
  95. /* Setup system level pin muxing */
  96. Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
  97. /* Clock pins only, group field not used */
  98. for (int i = 0; i <(int) (sizeof(pinclockmuxing) / sizeof(pinclockmuxing[0])); i++)
  99. {
  100. Chip_SCU_ClockPinMuxSet(pinclockmuxing[i].pinnum, pinclockmuxing[i].modefunc);
  101. }
  102. Chip_SetupXtalClocking();
  103. }
  104. void board_init(void)
  105. {
  106. SystemCoreClockUpdate();
  107. #if CFG_TUSB_OS == OPT_OS_NONE
  108. // 1ms tick timer
  109. SysTick_Config(SystemCoreClock / 1000);
  110. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  111. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  112. NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  113. #endif
  114. Chip_GPIO_Init(LPC_GPIO_PORT);
  115. // LED via pca9532 I2C
  116. Chip_SCU_I2C0PinConfig(I2C0_STANDARD_FAST_MODE);
  117. Chip_I2C_Init(I2C0);
  118. Chip_I2C_SetClockRate(I2C0, 100000);
  119. Chip_I2C_SetMasterEventHandler(I2C0, Chip_I2C_EventHandlerPolling);
  120. pca9532_init();
  121. // Button
  122. Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN);
  123. //------------- UART -------------//
  124. Chip_UART_Init(UART_DEV);
  125. Chip_UART_SetBaud(UART_DEV, CFG_BOARD_UART_BAUDRATE);
  126. Chip_UART_ConfigData(UART_DEV, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS);
  127. Chip_UART_TXEnable(UART_DEV);
  128. //------------- USB -------------//
  129. enum {
  130. USBMODE_DEVICE = 2,
  131. USBMODE_HOST = 3
  132. };
  133. enum {
  134. USBMODE_VBUS_LOW = 0,
  135. USBMODE_VBUS_HIGH = 1
  136. };
  137. /* From EA4357 user manual
  138. *
  139. * USB0 Device operation:
  140. * - Insert jumpers in position 1-2 in JP17/JP18/JP19.
  141. * - GPIO28 controls USB connect functionality
  142. * - LED32 lights when the USB Device is connected. SJ4 has pads 1-2 shorted by default.
  143. * - LED33 is controlled by GPIO27 and signals USB-up state. GPIO54 is used for VBUS
  144. * sensing.
  145. *
  146. * USB0 Host operation:
  147. * - insert jumpers in position 2-3 in JP17/JP18/JP19.
  148. * - USB Host power is controlled via distribution switch U20 (found in schematic page 11).
  149. * - Signal GPIO26 is active low and enables +5V on VBUS2.
  150. * - LED35 light whenever +5V is present on VBUS2.
  151. * - GPIO55 is connected to status feedback from the distribution switch.
  152. * - GPIO54 is used for VBUS sensing. 15Kohm pull-down resistors are always active
  153. *
  154. * Note:
  155. * - Insert jumpers in position 2-3 in JP17/JP18/JP19
  156. * - Insert jumpers in JP31 (OTG)
  157. */
  158. #if PORT_SUPPORT_DEVICE(0) || PORT_SUPPORT_HOST(0)
  159. Chip_USB0_Init();
  160. #endif
  161. /* From EA4357 user manual
  162. *
  163. * For USB1 Device:
  164. * - a 1.5Kohm pull-up resistor is needed on the USB DP data signal. There are two methods to create this.
  165. * JP15 is inserted and the pull-up resistor is always enabled. Alternatively, the pull-up resistor is activated
  166. * inside the USB OTG chip (U31), and this has to be done via the I2C interface of GPIO52/GPIO53. In the latter case,
  167. * JP15 shall not be inserted.
  168. * - J19 is the connector to use when USB Device is used. Normally it should be a USB-B connector for
  169. * creating a USB Device interface, but the mini-AB connector can also be used in this case. The status
  170. * of VBUS can be read via U31.
  171. * - JP16 shall not be inserted.
  172. *
  173. * For USB1 Host:
  174. * - 15Kohm pull-down resistors are needed on the USB data signals. These are activated inside the USB OTG chip (U31),
  175. * and this has to be done via the I2C interface of GPIO52/GPIO53.
  176. * - J20 is the connector to use when USB Host is used. In order to provide +5V to the external USB
  177. * device connected to this connector (J20), channel A of U20 must be enabled. It is enabled by default
  178. * since SJ5 is normally connected between pin 1-2.
  179. * - LED34 lights green when +5V is available on J20.
  180. * - JP15 shall not be inserted. JP16 has no effect
  181. */
  182. #if PORT_SUPPORT_DEVICE(1) || PORT_SUPPORT_HOST(1)
  183. Chip_USB1_Init();
  184. #endif
  185. // USB0 Vbus Power: P2_3 on EA4357 channel B U20 GPIO26 active low (base board)
  186. Chip_SCU_PinMuxSet(2, 3, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC7);
  187. #if PORT_SUPPORT_DEVICE(0)
  188. // P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low.
  189. Chip_SCU_PinMuxSet(9, 5, SCU_MODE_PULLDOWN | SCU_MODE_FUNC4);
  190. Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 5, 18);
  191. #endif
  192. // USB1 Power: EA4357 channel A U20 is enabled by SJ5 connected to pad 1-2, no more action required
  193. // TODO Remove R170, R171, solder a pair of 15K to USB1 D+/D- to test with USB1 Host
  194. }
  195. //--------------------------------------------------------------------+
  196. // USB Interrupt Handler
  197. //--------------------------------------------------------------------+
  198. void USB0_IRQHandler(void)
  199. {
  200. #if PORT_SUPPORT_DEVICE(0)
  201. tud_int_handler(0);
  202. #endif
  203. #if PORT_SUPPORT_HOST(0)
  204. tuh_int_handler(0);
  205. #endif
  206. }
  207. void USB1_IRQHandler(void)
  208. {
  209. #if PORT_SUPPORT_DEVICE(1)
  210. tud_int_handler(1);
  211. #endif
  212. #if PORT_SUPPORT_HOST(1)
  213. tuh_int_handler(1);
  214. #endif
  215. }
  216. //--------------------------------------------------------------------+
  217. // Board porting API
  218. //--------------------------------------------------------------------+
  219. void board_led_write(bool state)
  220. {
  221. if (state)
  222. {
  223. pca9532_setLeds( LED1, 0 );
  224. }else
  225. {
  226. pca9532_setLeds( 0, LED1);
  227. }
  228. }
  229. uint32_t board_button_read(void)
  230. {
  231. // active low
  232. return Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN) ? 0 : 1;
  233. }
  234. int board_uart_read(uint8_t* buf, int len)
  235. {
  236. //return UART_ReceiveByte(BOARD_UART_DEV);
  237. (void) buf; (void) len;
  238. return 0;
  239. }
  240. int board_uart_write(void const * buf, int len)
  241. {
  242. uint8_t const* buf8 = (uint8_t const*) buf;
  243. for(int i=0; i<len; i++)
  244. {
  245. while ((Chip_UART_ReadLineStatus(UART_DEV) & UART_LSR_THRE) == 0) {}
  246. Chip_UART_SendByte(UART_DEV, buf8[i]);
  247. }
  248. return len;
  249. }
  250. #if CFG_TUSB_OS == OPT_OS_NONE
  251. volatile uint32_t system_ticks = 0;
  252. void SysTick_Handler (void)
  253. {
  254. system_ticks++;
  255. }
  256. uint32_t board_millis(void)
  257. {
  258. return system_ticks;
  259. }
  260. #endif