esp32s2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2020, 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 "../../board.h"
  27. #include "board.h"
  28. #include "esp_rom_gpio.h"
  29. #include "hal/gpio_ll.h"
  30. #include "hal/usb_hal.h"
  31. #include "soc/usb_periph.h"
  32. #include "driver/rmt.h"
  33. #if ESP_IDF_VERSION_MAJOR > 4
  34. #include "esp_private/periph_ctrl.h"
  35. #else
  36. #include "driver/periph_ctrl.h"
  37. #endif
  38. #ifdef NEOPIXEL_PIN
  39. #include "led_strip.h"
  40. static led_strip_t *strip;
  41. #endif
  42. //--------------------------------------------------------------------+
  43. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  44. //--------------------------------------------------------------------+
  45. static void configure_pins(usb_hal_context_t *usb);
  46. // Initialize on-board peripherals : led, button, uart and USB
  47. void board_init(void)
  48. {
  49. #ifdef NEOPIXEL_PIN
  50. #ifdef NEOPIXEL_POWER_PIN
  51. gpio_reset_pin(NEOPIXEL_POWER_PIN);
  52. gpio_set_direction(NEOPIXEL_POWER_PIN, GPIO_MODE_OUTPUT);
  53. gpio_set_level(NEOPIXEL_POWER_PIN, NEOPIXEL_POWER_STATE);
  54. #endif
  55. // WS2812 Neopixel driver with RMT peripheral
  56. rmt_config_t config = RMT_DEFAULT_CONFIG_TX(NEOPIXEL_PIN, RMT_CHANNEL_0);
  57. config.clk_div = 2; // set counter clock to 40MHz
  58. rmt_config(&config);
  59. rmt_driver_install(config.channel, 0, 0);
  60. led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(1, (led_strip_dev_t) config.channel);
  61. strip = led_strip_new_rmt_ws2812(&strip_config);
  62. strip->clear(strip, 100); // off led
  63. #endif
  64. // Button
  65. esp_rom_gpio_pad_select_gpio(BUTTON_PIN);
  66. gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
  67. gpio_set_pull_mode(BUTTON_PIN, BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN_ONLY : GPIO_PULLUP_ONLY);
  68. // USB Controller Hal init
  69. periph_module_reset(PERIPH_USB_MODULE);
  70. periph_module_enable(PERIPH_USB_MODULE);
  71. usb_hal_context_t hal = {
  72. .use_external_phy = false // use built-in PHY
  73. };
  74. usb_hal_init(&hal);
  75. configure_pins(&hal);
  76. }
  77. static void configure_pins(usb_hal_context_t *usb)
  78. {
  79. /* usb_periph_iopins currently configures USB_OTG as USB Device.
  80. * Introduce additional parameters in usb_hal_context_t when adding support
  81. * for USB Host.
  82. */
  83. for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
  84. if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
  85. esp_rom_gpio_pad_select_gpio(iopin->pin);
  86. if (iopin->is_output) {
  87. esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
  88. } else {
  89. esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
  90. #if ESP_IDF_VERSION_MAJOR > 4
  91. if ((iopin->pin != GPIO_MATRIX_CONST_ZERO_INPUT) && (iopin->pin != GPIO_MATRIX_CONST_ONE_INPUT))
  92. #else
  93. if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH))
  94. #endif
  95. {
  96. gpio_ll_input_enable(&GPIO, iopin->pin);
  97. }
  98. }
  99. esp_rom_gpio_pad_unhold(iopin->pin);
  100. }
  101. }
  102. if (!usb->use_external_phy) {
  103. gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
  104. gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
  105. }
  106. }
  107. // Turn LED on or off
  108. void board_led_write(bool state)
  109. {
  110. #ifdef NEOPIXEL_PIN
  111. strip->set_pixel(strip, 0, (state ? 0x88 : 0x00), 0x00, 0x00);
  112. strip->refresh(strip, 100);
  113. #endif
  114. }
  115. // Get the current state of button
  116. // a '1' means active (pressed), a '0' means inactive.
  117. uint32_t board_button_read(void)
  118. {
  119. return gpio_get_level(BUTTON_PIN) == BUTTON_STATE_ACTIVE;
  120. }
  121. // Get characters from UART
  122. int board_uart_read(uint8_t* buf, int len)
  123. {
  124. (void) buf; (void) len;
  125. return 0;
  126. }
  127. // Send characters to UART
  128. int board_uart_write(void const * buf, int len)
  129. {
  130. (void) buf; (void) len;
  131. return 0;
  132. }