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