[INFO] Soft-USB-OTG on ESP32 (ESP-IDF & Arduino ) done ...

User avatar
rudi ;-)
Posts: 1727
Joined: Fri Nov 13, 2015 3:25 pm

[INFO] Soft-USB-OTG on ESP32 (ESP-IDF & Arduino ) done ...

Postby rudi ;-) » Tue Mar 16, 2021 9:36 pm

short & dirty

hi folks,

from now you can use USB-OTG by SOFTWARE on an ESP32.
ESP32 can do usb-soft-host, it's still limited to some HID classes but very promising
it is based on GPIO and the timer in this projects.
details:

ESP32 software USB host through general IO pins. We can connect up to 4 USB-LS HID (keyboard mouse joystick) devices simultaneously.

you find the work for ESP-IDF here
and you find the Arduino wrapp there

credits:
ESP-IDF Version: Samsonov Dima
ESP32 USB Soft Host library for Arduino IDE: TobozoTagada

now you can combine ESP32 which has no USB - with a simply software solution by
SOFT-USB-HOST


following work comes also for MicroPython..

best wishes
rudi ;-)

IMG_20210303_184755_1.jpg
credit: Samsonov Dima
IMG_20210303_184755_1.jpg (191.83 KiB) Viewed 13207 times
PulseView.jpg
credit: Samsonov Dima
PulseView.jpg (277.53 KiB) Viewed 13207 times
ESP-IDF example (credit: Samsonov Dima)

Code: Select all

/* Blink Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "hal/timer_ll.h"
/* Can run 'make menuconfig' to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/periph_ctrl.h"
#include "driver/timer.h"
#include "driver/gpio.h"
#include "soc/soc.h"
#include "soc/periph_defs.h"
#include "esp_log.h"

#include "usb_host.h"

void IRAM_ATTR timer_group0_isr(void *para)
{
	timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
	usb_process();
	timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_0);
}

#define BLINK_GPIO CONFIG_BLINK_GPIO
#define DP_P  16
#define DM_P  17

#define DP_P1  22
#define DM_P1  23

#define DP_P2  18
#define DM_P2  19

#define DP_P3  13
#define DM_P3  15

void led(int on_fff)
{
			gpio_set_level(BLINK_GPIO, on_fff);
}

void timer_task(void *pvParameter)
{
	    timer_config_t config = {
        .divider = TIMER_DIVIDER,
        .counter_dir = TIMER_COUNT_UP,
        .counter_en = TIMER_PAUSE,
        .alarm_en = TIMER_ALARM_EN,
        .auto_reload = 1,
    }; // default clock source is APB
    
    initStates(DP_P,DM_P,DP_P1,DM_P1,DP_P2,DM_P2,DP_P3,DM_P3);
  //  initStates(DP_P,DM_P,-1,-1,-1,-1,-1,-1);
    gpio_pad_select_gpio(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);


    int timer_idx = TIMER_0;
    double timer_interval_sec = TIMER_INTERVAL0_SEC;

     timer_init(TIMER_GROUP_0, timer_idx, &config);
    timer_set_counter_value(TIMER_GROUP_0, timer_idx, 0x00000000ULL);
    timer_set_alarm_value(TIMER_GROUP_0, timer_idx, timer_interval_sec * TIMER_SCALE);
    timer_enable_intr(TIMER_GROUP_0, timer_idx);
    timer_isr_register(TIMER_GROUP_0, timer_idx, timer_group0_isr,(void *) timer_idx, ESP_INTR_FLAG_IRAM, NULL);
    timer_start(TIMER_GROUP_0, timer_idx);
    while(1)
    {
	    printState();
	    vTaskDelay(10 / portTICK_PERIOD_MS);
     };
    
}


void blink_task(void *pvParameter)
{
	
	
    /* Configure the IOMUX register for pad BLINK_GPIO (some pads are
       muxed to GPIO on reset already, but some default to other
       functions and need to be switched to GPIO. Consult the
       Technical Reference for a list of pads and their default
       functions.)
    */
    while(1) {
	{
		vTaskDelay(100 / portTICK_PERIOD_MS);
	}
	
    }
}

void app_main()
{
    xTaskCreatePinnedToCore(&timer_task, "timer_task", 4096, "task 0", 5, NULL,1);
    xTaskCreatePinnedToCore(&blink_task, "blink_task", 4096, "task 1", 5, NULL,0);
   printf("Hello world end!\n");	
}



TobozoTagada_Ardu_Ex.png
credit: TobozoTagada
TobozoTagada_Ardu_Ex.png (573.65 KiB) Viewed 13207 times


Arduino example

Code: Select all

#include <ESP32-USBSoftHost.hpp>

// set pin numbers here
#define DP_P0  16  // always enabled
#define DM_P0  17  // always enabled
#define DP_P1  22 // -1 to disable
#define DM_P1  23 // -1 to disable
#define DP_P2  18 // -1 to disable
#define DM_P2  19 // -1 to disable
#define DP_P3  13 // -1 to disable
#define DM_P3  15 // -1 to disable

usb_pins_config_t USB_Pins_Config =
{
  DP_P0, DM_P0,
  DP_P1, DM_P1,
  DP_P2, DM_P2,
  DP_P3, DM_P3
};

static void USB_SoftHostTimerTask(void *arg)
{
  while (1) {
    timer_event_t evt;
    xQueueReceive(timer_queue, &evt, portMAX_DELAY);
    printState();
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }
}

static void my_USB_PrintCB( uint8_t* data, uint8_t data_len )
{
  printf("in :");
  for(int k=0;k<data_len;k++) {
    printf("%02x ",data[k]);
  }
  printf("\n");
}

void setup()
{

  setPrintCb( &my_USB_PrintCB );
  usb_init( USB_Pins_Config );

  xTaskCreatePinnedToCore(&USB_SoftHostTimerTask, "USB Soft Host Timer Task", 4096, NULL, 5, NULL, 1);

}

void loop()
{
  vTaskDelete(NULL);
}
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: No registered users and 52 guests