Moving again to TinyUSB

This commit is contained in:
Pol Henarejos
2022-05-29 01:52:44 +02:00
parent 152a2fa031
commit e0bff38384
5 changed files with 524 additions and 726 deletions

View File

@@ -38,6 +38,8 @@
#include "random.h"
#include "ccid2040.h"
#include "hardware/rtc.h"
#include "bsp/board.h"
#include "tusb.h"
extern void do_flash();
extern void low_flash_init();
@@ -120,44 +122,6 @@ void led_set_blink(uint32_t mode) {
void execute_tasks();
#include "hardware/structs/ioqspi.h"
#define BUTTON_STATE_ACTIVE 0
bool __no_inline_not_in_flash_func(get_bootsel_button)() {
const uint CS_PIN_INDEX = 1;
// Must disable interrupts, as interrupt handlers may be in flash, and we
// are about to temporarily disable flash access!
uint32_t flags = save_and_disable_interrupts();
// Set chip select to Hi-Z
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
GPIO_OVERRIDE_LOW << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
// Note we can't call into any sleep functions in flash right now
for (volatile int i = 0; i < 1000; ++i);
// The HI GPIO registers in SIO can observe and control the 6 QSPI pins.
// Note the button pulls the pin *low* when pressed.
bool button_state = (sio_hw->gpio_hi_in & (1u << CS_PIN_INDEX));
// Need to restore the state of chip select, else we are going to have a
// bad time when we return to code in flash!
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
restore_interrupts(flags);
return button_state;
}
static uint32_t board_button_read(void)
{
return BUTTON_STATE_ACTIVE == get_bootsel_button();
}
static bool wait_button() {
uint32_t start_button = board_millis();
bool timeout = false;
@@ -221,10 +185,10 @@ uint16_t rdata_bk = 0x0;
static int usb_event_handle() {
uint16_t rx_read = usb_read_available();
if (rx_read >= 10) {
struct ccid_header *tccid = (struct ccid_header *)usb_get_rx();
usb_read(rx_copy, sizeof(rx_copy));
//printf("%d %d %x\r\n",tccid->dwLength,rx_read-10,tccid->bMessageType);
if (tccid->dwLength <= rx_read-10) {
usb_read(rx_copy, sizeof(rx_copy));
if (ccid_header->dwLength <= rx_read-10) {
if (ccid_header->bMessageType != 0x65)
DEBUG_PAYLOAD(rx_copy,rx_read);
if (ccid_header->bMessageType == 0x65) {
@@ -416,24 +380,25 @@ void card_thread() {
uint32_t flag = EV_EXEC_FINISHED;
queue_add_blocking(&card_to_ccid_q, &flag);
}
printf("EXIT !!!!!!\r\n");
//printf("EXIT !!!!!!\r\n");
if (current_app && current_app->unload)
current_app->unload();
}
void ccid_task(void) {
if (usb_is_configured()) {
if (tud_vendor_mounted()) {
if (usb_event_handle() != 0) {
}
usb_write_flush();
if (timeout == 0) {
timeout = USB_CCID_TIMEOUT;
timeout_cnt++;
}
uint32_t m = 0x0;
bool has_m = queue_try_remove(&card_to_ccid_q, &m);
if (m != 0)
printf("\r\n ------ M = %lu\r\n",m);
//if (m != 0)
// printf("\r\n ------ M = %lu\r\n",m);
if (has_m) {
if (m == EV_EXEC_FINISHED) {
//printf("sw %x %d, %d\r\n",apdu.sw,apdu.rlen,apdu.ne);
@@ -652,7 +617,7 @@ pico_unique_board_id_t unique_id;
void execute_tasks() {
prev_millis = board_millis();
ccid_task();
//tud_task(); // tinyusb device task
tud_task(); // tinyusb device task
led_blinking_task();
}
@@ -669,7 +634,7 @@ int main(void) {
queue_init(&card_to_ccid_q, sizeof(uint32_t), 64);
queue_init(&ccid_to_card_q, sizeof(uint32_t), 64);
//board_init();
board_init();
stdio_init_all();
#ifdef PIMORONI_TINY2040
@@ -688,7 +653,7 @@ int main(void) {
led_off_all();
usb_init();
tusb_init();
//prepare_ccid();

View File

@@ -23,12 +23,6 @@
#include "pico/util/queue.h"
#include <string.h>
#include "pico/time.h"
static inline uint32_t board_millis(void)
{
return to_ms_since_boot(get_absolute_time());
}
#define USB_REQ_CCID 0xA1
typedef struct app {

115
src/usb/tusb_config.h Normal file
View File

@@ -0,0 +1,115 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.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.
*
*/
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
// defined by board.mk
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif
// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_DEVICE_RHPORT_NUM
#define BOARD_DEVICE_RHPORT_NUM 0
#endif
// RHPort max operational speed can defined by board.mk
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
#ifndef BOARD_DEVICE_RHPORT_SPEED
#if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 || CFG_TUSB_MCU == OPT_MCU_SAMX7X)
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED
#else
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
#endif
#endif
// Device mode with rhport and speed defined by board.mk
#if BOARD_DEVICE_RHPORT_NUM == 0
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#elif BOARD_DEVICE_RHPORT_NUM == 1
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#else
#error "Incorrect RHPort configuration"
#endif
#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_PICO
#endif
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
// #define CFG_TUSB_DEBUG 0
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put
* into those specific section.
* e.g
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/
#ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION
#endif
#ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
#endif
//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------
#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif
//------------- CLASS -------------//
#define CFG_TUD_HID 0
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 1
// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_EP_BUFSIZE 16
#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_CONFIG_H_ */

File diff suppressed because it is too large Load Diff

View File

@@ -318,6 +318,8 @@ extern uint8_t *usb_get_rx();
extern uint32_t usb_send_tx_buffer();
extern uint8_t *usb_get_tx();
extern void usb_clear_rx();
extern bool usb_write_available();
extern uint32_t usb_write_flush();
#endif