ea4088qs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //--------------------------------------------------------------------+
  29. // USB Interrupt Handler
  30. //--------------------------------------------------------------------+
  31. void USB_IRQHandler(void)
  32. {
  33. #if CFG_TUD_ENABLED
  34. tud_int_handler(0);
  35. #endif
  36. #if CFG_TUH_ENABLED
  37. tuh_int_handler(0);
  38. #endif
  39. }
  40. //--------------------------------------------------------------------+
  41. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  42. //--------------------------------------------------------------------+
  43. #define LED_PORT 2
  44. #define LED_PIN 19
  45. #define BUTTON_PORT 2
  46. #define BUTTON_PIN 10
  47. /* System oscillator rate and RTC oscillator rate */
  48. const uint32_t OscRateIn = 12000000;
  49. const uint32_t RTCOscRateIn = 32768;
  50. /* Pin muxing configuration */
  51. static const PINMUX_GRP_T pinmuxing[] =
  52. {
  53. // LED
  54. {2, 19, (IOCON_FUNC0 | IOCON_MODE_INACT)},
  55. // Button
  56. {2, 10, (IOCON_FUNC0 | IOCON_MODE_INACT | IOCON_MODE_PULLUP)},
  57. };
  58. static const PINMUX_GRP_T pin_usb_mux[] =
  59. {
  60. // USB1 as Host
  61. {0, 29, (IOCON_FUNC1 | IOCON_MODE_INACT)}, // D+1
  62. {0, 30, (IOCON_FUNC1 | IOCON_MODE_INACT)}, // D-1
  63. {1, 18, (IOCON_FUNC1 | IOCON_MODE_INACT)}, // UP LED1
  64. {1, 19, (IOCON_FUNC2 | IOCON_MODE_INACT)}, // PPWR1
  65. // {2, 14, (IOCON_FUNC2 | IOCON_MODE_INACT)}, // VBUS1
  66. // {2, 15, (IOCON_FUNC2 | IOCON_MODE_INACT)}, // OVRCR1
  67. // USB2 as Device
  68. {0, 31, (IOCON_FUNC1 | IOCON_MODE_INACT)}, // D+2
  69. {0, 13, (IOCON_FUNC1 | IOCON_MODE_INACT)}, // UP LED
  70. {0, 14, (IOCON_FUNC3 | IOCON_MODE_INACT)}, // CONNECT2
  71. /* VBUS is not connected on this board, so leave the pin at default setting. */
  72. /*Chip_IOCON_PinMux(LPC_IOCON, 1, 30, IOCON_MODE_INACT, IOCON_FUNC2);*/ /* USB VBUS */
  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. Chip_IOCON_Init(LPC_IOCON);
  86. Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
  87. /* CPU clock source starts with IRC */
  88. /* Enable PBOOST for CPU clock over 100MHz */
  89. Chip_SYSCTL_EnableBoost();
  90. Chip_SetupXtalClocking();
  91. }
  92. void board_init(void)
  93. {
  94. SystemCoreClockUpdate();
  95. #if CFG_TUSB_OS == OPT_OS_NONE
  96. // 1ms tick timer
  97. SysTick_Config(SystemCoreClock / 1000);
  98. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  99. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  100. NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  101. #endif
  102. Chip_GPIO_Init(LPC_GPIO);
  103. // LED
  104. Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_PORT, LED_PIN);
  105. // Button
  106. Chip_GPIO_SetPinDIRInput(LPC_GPIO, BUTTON_PORT, BUTTON_PIN);
  107. // UART
  108. //------------- USB -------------//
  109. Chip_IOCON_SetPinMuxing(LPC_IOCON, pin_usb_mux, sizeof(pin_usb_mux) / sizeof(PINMUX_GRP_T));
  110. // Port1 as Host, Port2: Device
  111. Chip_USB_Init();
  112. enum {
  113. USBCLK_DEVCIE = 0x12, // AHB + Device
  114. USBCLK_HOST = 0x19 , // AHB + OTG + Host
  115. USBCLK_ALL = 0x1B // Host + Device + OTG + AHB
  116. };
  117. LPC_USB->OTGClkCtrl = USBCLK_ALL;
  118. while ( (LPC_USB->OTGClkSt & USBCLK_ALL) != USBCLK_ALL ) {}
  119. // set portfunc: USB1 = host, USB2 = device
  120. LPC_USB->StCtrl = 0x3;
  121. }
  122. //--------------------------------------------------------------------+
  123. // Board porting API
  124. //--------------------------------------------------------------------+
  125. void board_led_write(bool state)
  126. {
  127. Chip_GPIO_SetPinState(LPC_GPIO, LED_PORT, LED_PIN, state);
  128. }
  129. uint32_t board_button_read(void)
  130. {
  131. // active low
  132. return Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_PORT, BUTTON_PIN) ? 0 : 1;
  133. }
  134. int board_uart_read(uint8_t* buf, int len)
  135. {
  136. //return UART_ReceiveByte(BOARD_UART_PORT);
  137. (void) buf; (void) len;
  138. return 0;
  139. }
  140. int board_uart_write(void const * buf, int len)
  141. {
  142. //UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING);
  143. (void) buf; (void) len;
  144. return 0;
  145. }
  146. #if CFG_TUSB_OS == OPT_OS_NONE
  147. volatile uint32_t system_ticks = 0;
  148. void SysTick_Handler (void)
  149. {
  150. system_ticks++;
  151. }
  152. uint32_t board_millis(void)
  153. {
  154. return system_ticks;
  155. }
  156. #endif