Skip to content

Commit

Permalink
Port USBHOST to rtos APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Dec 3, 2020
1 parent 2a8fe4b commit bba78c4
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void setup()
}

void loop() {
usb.Task();
//usb.Task();
}

#define MOD_CTRL (0x01 | 0x10)
Expand Down
16 changes: 14 additions & 2 deletions libraries/USBHOST/src/USBHost.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "USBHost.h"
#include "USB251xB.h"

static rtos::Thread t(osPriorityHigh);

void USBHost::InternalTask() {
while (1) {
tusbh_msg_loop(mq);
}
}

uint32_t USBHost::Init(uint8_t id, const tusbh_class_reg_t class_table[]) {

mq = tusbh_mq_create();
Expand Down Expand Up @@ -30,16 +38,20 @@ uint32_t USBHost::Init(uint8_t id, const tusbh_class_reg_t class_table[]) {
tusb_host_init(_hs, &root_hs);
tusb_open_host(_hs);
}

t.start(mbed::callback(this, &USBHost::InternalTask));
}



uint32_t USBHost::Task() {
tusbh_msg_loop(mq);

}

extern "C" {
// host need accurate delay
void tusb_delay_ms(uint32_t ms)
{
delayMicroseconds(ms*1000);
delay(ms);
}
}
2 changes: 2 additions & 0 deletions libraries/USBHOST/src/USBHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class USBHost {
};

private:

void InternalTask();
tusbh_msg_q_t* mq;
tusb_host_t* _fs;
tusb_host_t* _hs;
Expand Down
203 changes: 203 additions & 0 deletions libraries/USBHOST/src/class/host/tusbh_mbed_os.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* _______ _ _ _____ ____
* |__ __| | | | |/ ____| _ \
* | | ___ ___ _ __ _ _| | | | (___ | |_) |
* | |/ _ \/ _ \ '_ \| | | | | | |\___ \| _ <
* | | __/ __/ | | | |_| | |__| |____) | |_) |
* |_|\___|\___|_| |_|\__, |\____/|_____/|____/
* __/ |
* |___/
*
* TeenyUSB - light weight usb stack for STM32 micro controllers
*
* Copyright (c) 2019 XToolBox - admin@xtoolbox.org
* www.tusb.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

extern "C" {
#include "tusbh.h"
}
#include <stdarg.h>
#include "string.h"
#include <stdlib.h>

#include "Arduino.h"

#ifdef TUSB_HAS_OS

#define TUSBH_MSG_Q_LENGTH 16

static rtos::Mail<tusbh_message_t, TUSBH_MSG_Q_LENGTH> mail_box;

tusbh_msg_q_t* tusbh_mq_create()
{
return NULL;
}

void tusbh_mq_free(tusbh_msg_q_t* mq)
{
}


int tusbh_mq_init(tusbh_msg_q_t* mq)
{
return 0;
}

int tusbh_mq_post(tusbh_msg_q_t* mq, const tusbh_message_t* msg)
{
tusbh_message_t *mail = mail_box.alloc();
memcpy(mail, msg, sizeof(tusbh_message_t));
mail_box.put(mail);
return 1;
}

int tusbh_mq_get(tusbh_msg_q_t* mq, tusbh_message_t* msg)
{
osEvent evt = mail_box.get();
if (evt.status == osEventMail) {
tusbh_message_t *mail = (tusbh_message_t *)evt.value.p;
memcpy(msg, mail, sizeof(tusbh_message_t));
mail_box.free(mail);
return 1;
}
return 0;
}

#define MAX_DEVICE_COUNT 16
static tusbh_device_t* device_pool[MAX_DEVICE_COUNT] = { NULL };

tusbh_device_t* tusbh_new_device()
{
for(int i=0;i<MAX_DEVICE_COUNT;i++){
if(device_pool[i] == NULL){
device_pool[i] = (tusbh_device_t*)calloc(sizeof(tusbh_device_t), 1);
return device_pool[i];
}
}
TUSB_OS_INFO("Error: no free device space\n");
return 0;
}

void tusbh_free_device(tusbh_device_t* device)
{
for(int i=0;i<MAX_DEVICE_COUNT;i++){
if(device == device_pool[i] && device != NULL){
delete device;
device_pool[i] = NULL;
return;
}
}
TUSB_OS_INFO("Error: device memory out bound\n");
}

struct _tusbh_evt
{
rtos::EventFlags* event;
};

#define MAX_EVENTS_COUNT 16
static tusbh_evt_t* event_pool[MAX_EVENTS_COUNT] = { NULL };

tusbh_evt_t* tusbh_evt_create()
{
for(int i=0;i<MAX_EVENTS_COUNT;i++){
if(event_pool[i] == NULL){
event_pool[i] = (tusbh_evt_t*)calloc(sizeof(tusbh_evt_t), 1);
event_pool[i]->event = new rtos::EventFlags();
return event_pool[i];
}
}
TUSB_OS_INFO("Error: no free event space\n");
return 0;
}

void tusbh_evt_free(tusbh_evt_t* evt)
{
for(int i=0;i<MAX_EVENTS_COUNT;i++){
if(evt == event_pool[i] && evt != NULL){
delete evt->event;
delete evt;
event_pool[i] = NULL;
return;
}
}
TUSB_OS_INFO("Error: Event memory out bound\n");
}

int tusbh_evt_init(tusbh_evt_t* evt)
{
return 0;
}

int tusbh_evt_set(tusbh_evt_t* evt)
{
evt->event->set(1);
return 0;
}


int tusbh_evt_clear(tusbh_evt_t* evt)
{
evt->event->set(0);
return 0;
}

int tusbh_evt_wait(tusbh_evt_t* evt, uint32_t timeout_ms)
{
evt->event->wait_any(0xFF, timeout_ms);
}

static int mem_used;
static int mem_max;
void* tusbh_malloc(uint32_t size)
{
size = (size + 3) & (~3);
mem_used+=size;
if(mem_max < mem_used){
mem_max = mem_used;
}
void* r = malloc(size+8);
TUSB_ASSERT( (r != 0) && (((uint32_t)r) & 3) == 0 );
uint32_t* p = (uint32_t*)r;
*p = size;
*(p + (size/4) + 1) = 0xdeadbeef;
//TUSB_OS_INFO("Allocate %p %d\n", p, size);
return (void*)(p+1);
}

void tusbh_free(void* ptr)
{
TUSB_ASSERT(ptr != 0);
uint32_t* p = (uint32_t*)ptr;
p = p - 1;
uint32_t size = *p;
mem_used -= size;
TUSB_ASSERT(*(p+(size/4)+1) == 0xdeadbeef);
//TUSB_OS_INFO("Free %p %d\n", p, size);
free(p);
}

void show_memory(void)
{
}

#endif //TUSB_HAS_OS
1 change: 1 addition & 0 deletions libraries/USBHOST/src/teeny_usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#ifdef ARDUINO_ARCH_MBED
#define NO_DEVICE
#define TUSB_HAS_OS
#endif

#include "teeny_usb_platform.h"
Expand Down

0 comments on commit bba78c4

Please sign in to comment.