mbed1768.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #define LED_PORT 1
  29. #define LED_PIN 18
  30. #define LED_STATE_ON 1
  31. // JOYSTICK_DOWN if using LPCXpresso Base Board
  32. #define BUTTON_PORT 0
  33. #define BUTTON_PIN 15
  34. #define BUTTON_STATE_ACTIVE 0
  35. #define BOARD_UART_PORT LPC_UART3
  36. /* System oscillator rate and RTC oscillator rate */
  37. const uint32_t OscRateIn = 10000000;
  38. const uint32_t RTCOscRateIn = 32768;
  39. /* Pin muxing configuration */
  40. static const PINMUX_GRP_T pinmuxing[] =
  41. {
  42. {LED_PORT, LED_PIN, IOCON_MODE_INACT | IOCON_FUNC0},
  43. {BUTTON_PORT, BUTTON_PIN, IOCON_FUNC0 | IOCON_MODE_PULLUP},
  44. };
  45. static const PINMUX_GRP_T pin_usb_mux[] =
  46. {
  47. {0, 29, IOCON_MODE_INACT | IOCON_FUNC1}, // D+
  48. {0, 30, IOCON_MODE_INACT | IOCON_FUNC1}, // D-
  49. {2, 9, IOCON_MODE_INACT | IOCON_FUNC1}, // Connect
  50. {1, 19, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PPWR
  51. {1, 22, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PWRD
  52. /* VBUS is not connected on this board, so leave the pin at default setting. */
  53. /*Chip_IOCON_PinMux(LPC_IOCON, 1, 30, IOCON_MODE_INACT, IOCON_FUNC2);*/ /* USB VBUS */
  54. };
  55. // Invoked by startup code
  56. void SystemInit(void)
  57. {
  58. #ifdef __USE_LPCOPEN
  59. extern void (* const g_pfnVectors[])(void);
  60. unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08;
  61. *pSCB_VTOR = (unsigned int) g_pfnVectors;
  62. #endif
  63. Chip_IOCON_Init(LPC_IOCON);
  64. Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
  65. Chip_SetupXtalClocking();
  66. }
  67. void board_init(void)
  68. {
  69. SystemCoreClockUpdate();
  70. #if CFG_TUSB_OS == OPT_OS_NONE
  71. // 1ms tick timer
  72. SysTick_Config(SystemCoreClock / 1000);
  73. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  74. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  75. NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
  76. #endif
  77. Chip_GPIO_Init(LPC_GPIO);
  78. // LED
  79. Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_PORT, LED_PIN);
  80. // Button
  81. Chip_GPIO_SetPinDIRInput(LPC_GPIO, BUTTON_PORT, BUTTON_PIN);
  82. #if 0
  83. //------------- UART -------------//
  84. PINSEL_CFG_Type PinCfg =
  85. {
  86. .Portnum = 0,
  87. .Pinnum = 0, // TXD is P0.0
  88. .Funcnum = 2,
  89. .OpenDrain = 0,
  90. .Pinmode = 0
  91. };
  92. PINSEL_ConfigPin(&PinCfg);
  93. PinCfg.Portnum = 0;
  94. PinCfg.Pinnum = 1; // RXD is P0.1
  95. PINSEL_ConfigPin(&PinCfg);
  96. UART_CFG_Type UARTConfigStruct;
  97. UART_ConfigStructInit(&UARTConfigStruct);
  98. UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE;
  99. UART_Init(BOARD_UART_PORT, &UARTConfigStruct);
  100. UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit
  101. #endif
  102. //------------- USB -------------//
  103. Chip_IOCON_SetPinMuxing(LPC_IOCON, pin_usb_mux, sizeof(pin_usb_mux) / sizeof(PINMUX_GRP_T));
  104. Chip_USB_Init();
  105. enum {
  106. USBCLK_DEVCIE = 0x12, // AHB + Device
  107. USBCLK_HOST = 0x19, // AHB + Host + OTG
  108. // 0x1B // Host + Device + OTG + AHB
  109. };
  110. uint32_t const clk_en = CFG_TUD_ENABLED ? USBCLK_DEVCIE : USBCLK_HOST;
  111. LPC_USB->OTGClkCtrl = clk_en;
  112. while ( (LPC_USB->OTGClkSt & clk_en) != clk_en );
  113. #if CFG_TUH_ENABLED
  114. // set portfunc to host !!!
  115. LPC_USB->StCtrl = 0x3; // should be 1
  116. #endif
  117. }
  118. //--------------------------------------------------------------------+
  119. // USB Interrupt Handler
  120. //--------------------------------------------------------------------+
  121. void USB_IRQHandler(void)
  122. {
  123. #if CFG_TUD_ENABLED
  124. tud_int_handler(0);
  125. #endif
  126. #if CFG_TUH_ENABLED
  127. tuh_int_handler(0);
  128. #endif
  129. }
  130. //--------------------------------------------------------------------+
  131. // Board porting API
  132. //--------------------------------------------------------------------+
  133. void board_led_write(bool state)
  134. {
  135. Chip_GPIO_SetPinState(LPC_GPIO, LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
  136. }
  137. uint32_t board_button_read(void)
  138. {
  139. return BUTTON_STATE_ACTIVE == Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_PORT, BUTTON_PIN);
  140. }
  141. int board_uart_read(uint8_t* buf, int len)
  142. {
  143. // return UART_ReceiveByte(BOARD_UART_PORT);
  144. (void) buf; (void) len;
  145. return 0;
  146. }
  147. int board_uart_write(void const * buf, int len)
  148. {
  149. // UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING);
  150. (void) buf; (void) len;
  151. return 0;
  152. }
  153. #if CFG_TUSB_OS == OPT_OS_NONE
  154. volatile uint32_t system_ticks = 0;
  155. void SysTick_Handler (void)
  156. {
  157. system_ticks++;
  158. }
  159. uint32_t board_millis(void)
  160. {
  161. return system_ticks;
  162. }
  163. #endif