Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CH341 emulation class driver #1507

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4783615
Add CH341 class driver
TravisRo Jun 10, 2022
85ad2c6
Add CH341 to built-in driver list
TravisRo Jun 10, 2022
08447d9
Add CH341 example
TravisRo Jun 10, 2022
28fdb19
Add CH341 example to CMakelist and rules
TravisRo Jun 10, 2022
3b84243
include ch341_device
TravisRo Jun 10, 2022
42ed9a5
Fix compile errors when using latest branch
TravisRo Jun 10, 2022
d63da66
Fix compile errors related to sof/sof_isr renaming
TravisRo Jun 10, 2022
176d245
Merge branch 'hathach:master' into master
TravisRo Jun 10, 2022
4c66812
Add missing CFG_TUD_CH341 define
TravisRo Jun 16, 2022
7572956
Merge branch 'hathach:master' into master
TravisRo Jun 16, 2022
74bf3cc
Merge branch 'master' of https://github.com/TravisRo/tinyusb
TravisRo Jun 16, 2022
103153e
Fixed compiler errors
TravisRo Jun 16, 2022
8f8990a
Fix compile errors for high speed devices
TravisRo Jun 16, 2022
e6e36f0
fix unused variable and conversion compile errors
TravisRo Jun 16, 2022
89eb35d
Misc. improvements
TravisRo Jun 17, 2022
6510e87
logging cleanup. remove printfs from example
TravisRo Jun 18, 2022
d8e8057
Add missing static inline class functions
TravisRo Jun 21, 2022
bc84902
Merge branch 'hathach:master' into master
TravisRo Jun 29, 2022
03d9dcc
Merge branch 'hathach:master' into master
TravisRo Jul 2, 2022
696d3d9
Merge branch 'hathach:master' into master
TravisRo Jul 22, 2022
ee5a144
Allow setting CFG_TUSB_OS from parent cmakefile
TravisRo Aug 12, 2022
ab6a91f
Merge branch 'hathach:master' into master
TravisRo Aug 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ family_add_subdirectory(uac2_headset)
family_add_subdirectory(usbtmc)
family_add_subdirectory(video_capture)
family_add_subdirectory(webusb_serial)
family_add_subdirectory(ch341_serial)
28 changes: 28 additions & 0 deletions examples/device/ch341_serial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.5)

include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)

# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})

project(${PROJECT})

# Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})

add_executable(${PROJECT})

# Example source
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
)

# Example include
target_include_directories(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_device_example(${PROJECT})
12 changes: 12 additions & 0 deletions examples/device/ch341_serial/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include ../../../tools/top.mk
include ../../make.mk

INC += \
src \
$(TOP)/hw \

# Example source
EXAMPLE_SOURCE += $(wildcard src/*.c)
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))

include ../../rules.mk
132 changes: 132 additions & 0 deletions examples/device/ch341_serial/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.
*
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "bsp/board.h"
#include "tusb.h"

// Change this to 1 and event callback will be logged to stdout
#define CH341_LOG_EVENTS 0

#if (CH341_LOG_EVENTS)
static const char * _par_str[] =
{
"N",
"O",
"E",
"M",
"S"
};
#endif

//------------- prototypes -------------//
static void ch341_task(void);

/*------------- MAIN -------------*/
int main(void)
{
board_init();

// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);

while (1)
{
tud_task(); // tinyusb device task
ch341_task();
}

return 0;
}

// Echo back to terminal
static void echo_serial_port( uint8_t buf[], uint32_t count)
{
tud_ch341_write(buf, count);
tud_ch341_write_flush();
}

//--------------------------------------------------------------------+
// USB CH341
//--------------------------------------------------------------------+
static void ch341_task(void)
{
// connected() The serial port has been opened atleast once
// Will continue to return true even after the serial port is closed.
if ( tud_ch341_connected())
{
if ( tud_ch341_available() )
{
uint8_t buf[64];

uint32_t count = tud_ch341_read(buf, sizeof(buf));

// echo back to terminal
echo_serial_port(buf, count);
}
}
}

// Invoked when DTR/RTS changes
void tud_ch341_line_state_cb(ch341_line_state_t line_state)
{
#if (CH341_LOG_EVENTS)
printf("DTR=%u, RTS=%u\r\n",
line_state & CH341_LINE_STATE_DTR_ACTIVE ? 1 : 0,
line_state & CH341_LINE_STATE_RTS_ACTIVE ? 1 : 0);
#else
(void)(line_state);
#endif
}

// Invoked when line coding changes
void tud_ch341_line_coding_cb(ch341_line_coding_t const* p_line_coding)
{
#if (CH341_LOG_EVENTS)
printf("BITRATE=%lu (%s%u%u) RX:%s TX:%s\r\n",
(unsigned long)p_line_coding->bit_rate,
_par_str[p_line_coding->parity],
p_line_coding->data_bits,
p_line_coding->stop_bits ? 2 : 1,
p_line_coding->rx_en ? "ON":"OFF",
p_line_coding->tx_en ? "ON":"OFF");
#else
(void)(p_line_coding);
#endif
}

// Invoked when a break signal is received
void tud_ch341_send_break_cb(bool is_break_active)
{
#if (CH341_LOG_EVENTS)
printf("RCV BREAK=%s\r\n", is_break_active ? "ON" : "OFF");
#else
(void)(is_break_active);
#endif
}
104 changes: 104 additions & 0 deletions examples/device/ch341_serial/src/tusb_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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


//--------------------------------------------------------------------+
// Board Specific Configuration
//--------------------------------------------------------------------+

// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_TUD_RHPORT
#define BOARD_TUD_RHPORT 0
#endif

// RHPort max operational speed can defined by board.mk
#ifndef BOARD_TUD_MAX_SPEED
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif

//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------

// defined by board.mk
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif

#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE
#endif

#ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 0
#endif

// Enable Device stack
#define CFG_TUD_ENABLED 1

// Default is max speed that hardware controller could support with on-chip PHY
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED

#ifndef CFG_TUSB_RHPORT0_MODE
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED
#endif

//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------

#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif

//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0
#define CFG_TUD_CH341 1

// CH341 Endpoint max packet sizes (should not change)
#define CFG_TUD_CH341_EP_RX_MAX_PACKET (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CH341_EP_TX_MAX_PACKET CFG_TUD_CH341_EP_RX_MAX_PACKET
#define CFG_TUD_CH341_EP_TXNOTIFY_MAX_PACKET (8)

// CH341 buffer size for TX and RX data (must be an interval of max packet size)
// more is faster
#define CFG_TUD_CH341_FIFO_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

#ifdef __cplusplus
}
#endif

#endif /* _TUSB_CONFIG_H_ */
Loading