Skip to content

Commit

Permalink
usb: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
avtolstoy committed Nov 24, 2021
1 parent dc6daa8 commit 9bf71be
Show file tree
Hide file tree
Showing 51 changed files with 3,221 additions and 306 deletions.
14 changes: 11 additions & 3 deletions bootloader/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
#include "module.h"
#endif

#include "timer_hal.h"

extern void DFU_Check_Reset();
extern void HAL_DFU_Process();

void platform_startup();

#if PLATFORM_ID == PLATFORM_TRON
Expand Down Expand Up @@ -509,12 +514,13 @@ int main(void)
// Main loop
while (1)
{
//Do nothing
#if HAL_PLATFORM_BOOTLOADER_USB_PROCESS_IN_MAIN_THREAD
HAL_DFU_Process();
DFU_Check_Reset();
#endif // HAL_PLATFORM_BOOTLOADER_USB_PROCESS_IN_MAIN_THREAD
}
}

extern void DFU_Check_Reset();

/*******************************************************************************
* Function Name : Timing_Decrement
* Description : Decrements the various Timing variables related to SysTick.
Expand Down Expand Up @@ -547,7 +553,9 @@ void Timing_Decrement(void)
}
}

#if !HAL_PLATFORM_BOOTLOADER_USB_PROCESS_IN_MAIN_THREAD
DFU_Check_Reset();
#endif // !HAL_PLATFORM_BOOTLOADER_USB_PROCESS_IN_MAIN_THREAD
}

#ifdef USE_FULL_ASSERT
Expand Down
2 changes: 1 addition & 1 deletion bootloader/src/rtl872x/include.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INCLUDE_DIRS += $(BOOTLOADER_MODULE_PATH)/src/rtl872x

MAIN_STACK_SIZE = 4096
MAIN_STACK_SIZE = 8192

ASFLAGS += -D__STACKSIZE__=$(MAIN_STACK_SIZE) -D__STACK_SIZE=$(MAIN_STACK_SIZE)

Expand Down
17 changes: 17 additions & 0 deletions bootloader/src/rtl872x/nonsecure.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2021 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "nonsecure.h"

extern "C" {
Expand Down
17 changes: 17 additions & 0 deletions bootloader/src/rtl872x/nonsecure.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2021 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdint.h>
Expand Down
188 changes: 188 additions & 0 deletions bootloader/src/rtl872x/osdep_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Copyright (c) 2021 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include <cstdlib>
#include "osdep_service.h"
#include "simple_pool_allocator.h"
#include "timer_hal.cpp"
#include <atomic>
#include "static_recursive_cs.h"

namespace {

uint8_t sStaticPool[64 * 1024];
AtomicSimpleStaticPool sPool(sStaticPool, sizeof(sStaticPool));
particle::StaticRecursiveCriticalSectionLock sCsLock;
thread_func_t sFunc = nullptr;
void *sCtx = nullptr;

const size_t RTW_USBD_TASK_MAIN_LOOP_SEMAPHORE_OFFSET = 364;
const uint32_t RTW_USBD_TASK_MAIN_LOOP_PERIOD_MS = 1;

class AtomicCountingSemaphore {
public:
AtomicCountingSemaphore(int initVal = 0)
: counter_(initVal) {
}

bool acquire(uint32_t timeout = 0xffffffff) {
auto start = hal_timer_millis(nullptr);
do {
auto v = counter_.load();
if (v > 0) {
if (counter_.compare_exchange_weak(v, v - 1, std::memory_order_acquire, std::memory_order_relaxed)) {
return true;
}
}
} while (hal_timer_millis(nullptr) < start + timeout);
std::atomic_thread_fence(std::memory_order_acquire);
return false;
}

void release() {
std::atomic_thread_fence(std::memory_order_release);
counter_.fetch_add(1, std::memory_order_relaxed);
}
private:
std::atomic_int counter_;
};

} // anonymous

extern "C" {
// To simplify access from assembly to avoid mangling
__attribute__((used)) void* rtwUsbBackupSp = nullptr;
__attribute__((used)) void* rtwUsbBackupLr = nullptr;
__attribute__((used)) void rtw_usb_task();
} // extern "C"

uint8_t* rtw_zvmalloc(uint32_t sz) {
auto p = sPool.alloc(sz);
if (p) {
memset(p, 0, sz);
}
return (uint8_t*)p;
}

uint8_t* rtw_zmalloc(uint32_t sz) {
return rtw_zvmalloc(sz);
}

void rtw_mfree(uint8_t* pbuf, uint32_t size) {
sPool.free(pbuf);
}

void rtw_usleep_os(int us) {
// XXX: this is only used by the USB driver
// There appears to be some issues with the ROM DelayXxx usage
// DelayUs(us);
}

void rtw_msleep_os(int ms) {
// XXX: this is only used by the USB driver
// There appears to be some issues with the ROM DelayXxx usage
// DelayMs(ms);
}

void rtw_init_sema(_sema *sema, int init_val) {
auto ptr = rtw_zmalloc(sizeof(AtomicCountingSemaphore));
if (ptr) {
AtomicCountingSemaphore* sem = new(ptr) AtomicCountingSemaphore(init_val);
*sema = (void*)sem;
} else {
*sema = nullptr;
}
}

void rtw_free_sema(_sema* sema) {
auto sem = static_cast<AtomicCountingSemaphore*>(*sema);
sem->~AtomicCountingSemaphore();
rtw_mfree((uint8_t*)sem, 0);
}


void rtw_up_sema_from_isr(_sema* sema) {
auto sem = static_cast<AtomicCountingSemaphore*>(*sema);
sem->release();
}

u32 rtw_down_sema(_sema* sema) {
auto sem = static_cast<AtomicCountingSemaphore*>(*sema);
uint8_t* tmp = (uint8_t*)sCtx;
if ((void*)sema == (void*)(tmp + RTW_USBD_TASK_MAIN_LOOP_SEMAPHORE_OFFSET)) {
bool v = sem->acquire(RTW_USBD_TASK_MAIN_LOOP_PERIOD_MS);
if (!v) {
// Break out of USB driver task loop if there is nothing to process from the USB peripheral
// by restoring stack pointer captured just before jumping into
// it and jumping back.
asm volatile ("mov sp, %0\n\t"
"bx %1\n\t" :: "r" (rtwUsbBackupSp), "r" (rtwUsbBackupLr));
// Unreachable
SPARK_ASSERT(false);
}
return v;
}
return sem->acquire();
}

void rtw_spinlock_init(_lock *plock) {
rtw_init_sema((_sema*)plock, 1);
}

void rtw_spinlock_free(_lock *plock) {
rtw_free_sema((_sema*)plock);
}

void rtw_spin_lock(_lock *plock) {
rtw_down_sema((_sema*)plock);
}

void rtw_spin_unlock(_lock *plock) {
rtw_up_sema_from_isr((_sema*)plock);
}

void rtw_enter_critical(_lock *plock, _irqL *pirqL) {
sCsLock.lock();
}

void rtw_exit_critical(_lock *plock, _irqL *pirqL) {
sCsLock.unlock();
}

void rtw_memcpy(void* dst, void* src, uint32_t sz) {
memcpy(dst, src, sz);
}

void rtw_thread_exit(void) {
sFunc = nullptr;
}

int rtw_create_task(struct task_struct* task, const char* name, uint32_t stack_size, uint32_t priority, thread_func_t func, void *thctx) {
sFunc = func;
sCtx = thctx;
return 1;
}

void rtw_delete_task(struct task_struct* task) {
sFunc = nullptr;
}

void rtw_usb_task() {
if (sFunc) {
sFunc(sCtx);
}
}
2 changes: 2 additions & 0 deletions bootloader/src/rtl872x/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,rtl_
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,timer_hal.cpp)
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/littlefs/,*.cpp)
CSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/littlefs/,*.c)
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,usbd_device.cpp)
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,usbd_driver.cpp)

LDFLAGS += -T$(BOOTLOADER_SRC_PATH)/linker.ld
LINKER_DEPS += $(BOOTLOADER_SRC_PATH)/linker.ld
1 change: 1 addition & 0 deletions bootloader/src/rtl872x/spark_wiring_interrupts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../../../wiring/inc/spark_wiring_interrupts.h"
7 changes: 0 additions & 7 deletions bootloader/src/rtl872x/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,3 @@
#include "flash_access.h"
#include "dfu_hal.h"
#include "dct_hal.h"

void HAL_DFU_USB_Init(void) {
}

void DFU_Check_Reset(void) {

}
Loading

0 comments on commit 9bf71be

Please sign in to comment.