mirror of
https://github.com/hathach/tinyusb.git
synced 2025-03-14 04:18:56 +00:00
added tud_cdc_write_str, tu_fifo only use mutex for RTOS config
This commit is contained in:
parent
5a046799f6
commit
3fe7cd1659
@ -32,12 +32,6 @@
|
||||
target_reset_script="Reset();"
|
||||
target_script_file="$(ProjectDir)/nRF_Target.js"
|
||||
target_trace_initialize_script="EnableTrace("$(TraceInterfaceType)")" />
|
||||
<folder Name="RTT Files">
|
||||
<file file_name="SEGGER_RTT.c" />
|
||||
<file file_name="SEGGER_RTT.h" />
|
||||
<file file_name="SEGGER_RTT_Conf.h" />
|
||||
<file file_name="SEGGER_RTT_SES.c" />
|
||||
</folder>
|
||||
<folder Name="Script Files">
|
||||
<file file_name="nRF_Target.js">
|
||||
<configuration Name="Common" file_type="Reset Script" />
|
||||
@ -61,6 +55,12 @@
|
||||
<file file_name="../src/msc_app.h" />
|
||||
<file file_name="../src/msc_flash_ram.c" />
|
||||
<file file_name="../src/msc_flash_qspi.c" />
|
||||
<folder Name="segger_rtt">
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT.c" />
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT_Conf.h" />
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT.h" />
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT_SES.c" />
|
||||
</folder>
|
||||
</folder>
|
||||
<folder Name="hw">
|
||||
<folder Name="bsp">
|
||||
|
@ -55,56 +55,51 @@
|
||||
//--------------------------------------------------------------------+
|
||||
void print_greeting(void);
|
||||
void led_blinking_task(void);
|
||||
void virtual_com_task(void);
|
||||
void usb_hid_task(void);
|
||||
|
||||
/*------------- MAIN -------------*/
|
||||
int main(void)
|
||||
{
|
||||
board_init();
|
||||
print_greeting();
|
||||
|
||||
tusb_init();
|
||||
|
||||
while (1)
|
||||
{
|
||||
tusb_task();
|
||||
|
||||
led_blinking_task();
|
||||
virtual_com_task();
|
||||
|
||||
usb_hid_task();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USB CDC
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUD_CDC
|
||||
void virtual_com_task(void)
|
||||
{
|
||||
#if CFG_TUD_CDC
|
||||
// connected and there are data available
|
||||
if ( tud_mounted() && tud_cdc_available() )
|
||||
if ( tud_cdc_connected() )
|
||||
{
|
||||
uint8_t buf[64];
|
||||
if ( tud_cdc_available() )
|
||||
{
|
||||
uint8_t buf[64];
|
||||
|
||||
// read and echo back
|
||||
uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
||||
// read and echo back
|
||||
uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
||||
|
||||
tud_cdc_write(buf, count);
|
||||
}
|
||||
|
||||
tud_cdc_write(buf, count);
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
|
||||
{
|
||||
(void) itf;
|
||||
|
||||
// connected
|
||||
if ( dtr && rts )
|
||||
{
|
||||
// print greeting
|
||||
tud_cdc_write_str("tinyusb usb cdc\n");
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define virtual_com_task()
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// USB HID
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUD_HID
|
||||
void usb_hid_task(void)
|
||||
{
|
||||
#if CFG_TUD_HID
|
||||
// Poll every 10ms
|
||||
static tu_timeout_t tm = { .start = 0, .interval = 10 };
|
||||
|
||||
@ -144,10 +139,8 @@ void usb_hid_task(void)
|
||||
if ( btn & 0x04 ) tud_hid_mouse_move( 0 , -DELTA); // up
|
||||
if ( btn & 0x08 ) tud_hid_mouse_move( 0 , DELTA); // down
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if CFG_TUD_HID
|
||||
uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
||||
{
|
||||
// TODO not Implemented
|
||||
@ -158,8 +151,32 @@ void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_t
|
||||
{
|
||||
// TODO not Implemented
|
||||
}
|
||||
|
||||
#else
|
||||
#define usb_hid_task()
|
||||
#endif
|
||||
|
||||
|
||||
/*------------- MAIN -------------*/
|
||||
int main(void)
|
||||
{
|
||||
board_init();
|
||||
print_greeting();
|
||||
|
||||
tusb_init();
|
||||
|
||||
while (1)
|
||||
{
|
||||
tusb_task();
|
||||
|
||||
led_blinking_task();
|
||||
virtual_com_task();
|
||||
usb_hid_task();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// tinyusb callbacks
|
||||
//--------------------------------------------------------------------+
|
||||
|
@ -32,12 +32,6 @@
|
||||
target_reset_script="Reset();"
|
||||
target_script_file="$(ProjectDir)/nRF_Target.js"
|
||||
target_trace_initialize_script="EnableTrace("$(TraceInterfaceType)")" />
|
||||
<folder Name="RTT Files">
|
||||
<file file_name="SEGGER_RTT.c" />
|
||||
<file file_name="SEGGER_RTT.h" />
|
||||
<file file_name="SEGGER_RTT_Conf.h" />
|
||||
<file file_name="SEGGER_RTT_SES.c" />
|
||||
</folder>
|
||||
<folder Name="Script Files">
|
||||
<file file_name="nRF_Target.js">
|
||||
<configuration Name="Common" file_type="Reset Script" />
|
||||
@ -60,6 +54,12 @@
|
||||
<file file_name="../src/msc_flash_qspi.c" />
|
||||
<file file_name="../src/msc_app.c" />
|
||||
<file file_name="../src/msc_app.h" />
|
||||
<folder Name="segger_rtt">
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT.c" />
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT_Conf.h" />
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT.h" />
|
||||
<file file_name="../src/segger_rtt/SEGGER_RTT_SES.c" />
|
||||
</folder>
|
||||
</folder>
|
||||
<folder Name="hw">
|
||||
<folder Name="bsp">
|
||||
|
@ -101,18 +101,32 @@ void cdc_task(void* params)
|
||||
while ( 1 )
|
||||
{
|
||||
// connected and there are data available
|
||||
if ( tud_mounted() && tud_cdc_available() )
|
||||
if ( tud_cdc_connected() )
|
||||
{
|
||||
uint8_t buf[64];
|
||||
if ( tud_cdc_available() )
|
||||
{
|
||||
uint8_t buf[64];
|
||||
|
||||
// read and echo back
|
||||
uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
||||
// read and echo back
|
||||
uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
||||
|
||||
tud_cdc_write(buf, count);
|
||||
}
|
||||
|
||||
tud_cdc_write(buf, count);
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
taskYIELD();
|
||||
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
|
||||
{
|
||||
(void) itf;
|
||||
|
||||
// connected
|
||||
if ( dtr && rts )
|
||||
{
|
||||
// print greeting
|
||||
tud_cdc_write_str("tinyusb usb cdc\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -55,7 +55,7 @@
|
||||
|
||||
/*------------- RTOS -------------*/
|
||||
#define CFG_TUSB_OS OPT_OS_FREERTOS
|
||||
#define CFG_TUD_TASK_PRIO (configMAX_PRIORITIES-3)
|
||||
#define CFG_TUD_TASK_PRIO (configMAX_PRIORITIES-1)
|
||||
//#define CFG_TUD_TASK_QUEUE_SZ 16
|
||||
//#define CFG_TUD_TASK_STACK_SZ 150
|
||||
|
||||
|
@ -98,6 +98,11 @@ void board_init(void)
|
||||
nrf_gpio_cfg_output(BOARD_LED2);
|
||||
nrf_gpio_cfg_output(BOARD_LED3);
|
||||
|
||||
board_led_control(BOARD_LED0, false);
|
||||
board_led_control(BOARD_LED1, false);
|
||||
board_led_control(BOARD_LED2, false);
|
||||
board_led_control(BOARD_LED3, false);
|
||||
|
||||
// Button
|
||||
for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++) nrf_gpio_cfg_input(_button_pins[i], NRF_GPIO_PIN_PULLUP);
|
||||
|
||||
|
@ -71,8 +71,10 @@ typedef struct
|
||||
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
|
||||
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
|
||||
|
||||
#if CFG_FIFO_MUTEX
|
||||
osal_mutex_def_t rx_ff_mutex;
|
||||
osal_mutex_def_t tx_ff_mutex;
|
||||
#endif
|
||||
|
||||
// Endpoint Transfer buffer
|
||||
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EPSIZE];
|
||||
@ -151,6 +153,11 @@ uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
|
||||
return tud_cdc_n_write(itf, &ch, 1);
|
||||
}
|
||||
|
||||
uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
|
||||
{
|
||||
return tud_cdc_n_write(itf, str, strlen(str));
|
||||
}
|
||||
|
||||
uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
|
||||
{
|
||||
uint16_t ret = tu_fifo_write_n(&_cdcd_itf[itf].tx_ff, buffer, bufsize);
|
||||
@ -203,10 +210,12 @@ void cdcd_init(void)
|
||||
|
||||
// config fifo
|
||||
tu_fifo_config(&ser->rx_ff, ser->rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
|
||||
tu_fifo_config_mutex(&ser->rx_ff, osal_mutex_create(&ser->rx_ff_mutex));
|
||||
|
||||
tu_fifo_config(&ser->tx_ff, ser->tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
|
||||
|
||||
#if CFG_FIFO_MUTEX
|
||||
tu_fifo_config_mutex(&ser->rx_ff, osal_mutex_create(&ser->rx_ff_mutex));
|
||||
tu_fifo_config_mutex(&ser->tx_ff, osal_mutex_create(&ser->tx_ff_mutex));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,7 @@ char tud_cdc_n_peek (uint8_t itf, int pos);
|
||||
|
||||
uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
|
||||
uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
|
||||
uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);
|
||||
bool tud_cdc_n_write_flush (uint8_t itf);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@ -95,6 +96,7 @@ static inline char tud_cdc_peek (int pos)
|
||||
|
||||
static inline uint32_t tud_cdc_write_char (char ch) { return tud_cdc_n_write_char(0, ch); }
|
||||
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) { return tud_cdc_n_write(0, buffer, bufsize); }
|
||||
static inline uint32_t tud_cdc_write_str (char const* str) { return tud_cdc_n_write_str(0, str); }
|
||||
static inline bool tud_cdc_write_flush (void) { return tud_cdc_n_write_flush(0); }
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
|
@ -42,25 +42,16 @@
|
||||
#include "tusb_fifo.h"
|
||||
|
||||
// implement mutex lock and unlock
|
||||
// For OSAL_NONE: if mutex is locked by other, function return immediately (since there is no task context)
|
||||
// For Real RTOS: fifo lock is a blocking API
|
||||
#if CFG_FIFO_MUTEX
|
||||
|
||||
static bool tu_fifo_lock(tu_fifo_t *f)
|
||||
static void tu_fifo_lock(tu_fifo_t *f)
|
||||
{
|
||||
if (f->mutex)
|
||||
{
|
||||
#if CFG_TUSB_OS == OPT_OS_NONE
|
||||
// There is no subtask context for blocking mutex, we will check and return if cannot lock the mutex
|
||||
if ( !osal_mutex_lock_notask(f->mutex) ) return false;
|
||||
#else
|
||||
uint32_t err;
|
||||
(void) err;
|
||||
osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER, &err);
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void tu_fifo_unlock(tu_fifo_t *f)
|
||||
@ -73,14 +64,14 @@ static void tu_fifo_unlock(tu_fifo_t *f)
|
||||
|
||||
#else
|
||||
|
||||
#define tu_fifo_lock(_ff) true
|
||||
#define tu_fifo_lock(_ff)
|
||||
#define tu_fifo_unlock(_ff)
|
||||
|
||||
#endif
|
||||
|
||||
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
|
||||
{
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
tu_fifo_lock(f);
|
||||
|
||||
f->buffer = (uint8_t*) buffer;
|
||||
f->depth = depth;
|
||||
@ -115,7 +106,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
|
||||
{
|
||||
if( tu_fifo_empty(f) ) return false;
|
||||
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
tu_fifo_lock(f);
|
||||
|
||||
memcpy(p_buffer,
|
||||
f->buffer + (f->rd_idx * f->item_size),
|
||||
@ -216,7 +207,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
|
||||
{
|
||||
if ( tu_fifo_full(f) && !f->overwritable ) return false;
|
||||
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
tu_fifo_lock(f);
|
||||
|
||||
memcpy( f->buffer + (f->wr_idx * f->item_size),
|
||||
p_data,
|
||||
@ -279,7 +270,7 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
|
||||
/******************************************************************************/
|
||||
bool tu_fifo_clear(tu_fifo_t *f)
|
||||
{
|
||||
if ( !tu_fifo_lock(f) ) return false;
|
||||
tu_fifo_lock(f);
|
||||
|
||||
f->rd_idx = f->wr_idx = f->count = 0;
|
||||
|
||||
|
@ -43,7 +43,9 @@
|
||||
#ifndef _TUSB_FIFO_H_
|
||||
#define _TUSB_FIFO_H_
|
||||
|
||||
#define CFG_FIFO_MUTEX 1
|
||||
// mutex is only needed for RTOS
|
||||
// for OS None, we don't get preempted
|
||||
#define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -178,19 +178,6 @@ static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
|
||||
#define osal_mutex_unlock(_mutex_hdl) osal_semaphore_post(_mutex_hdl, false)
|
||||
#define osal_mutex_lock osal_semaphore_wait
|
||||
|
||||
// check if mutex is available for non-thread/substask usage in some cases
|
||||
static inline bool osal_mutex_lock_notask(osal_mutex_t mutex_hdl)
|
||||
{
|
||||
if (mutex_hdl->count)
|
||||
{
|
||||
mutex_hdl->count--;
|
||||
return true;
|
||||
}else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// QUEUE API
|
||||
//--------------------------------------------------------------------+
|
||||
|
Loading…
x
Reference in New Issue
Block a user