Skip to content

Commit 667ae49

Browse files
committed
Adding CH341 support. Direct copy of hathach#1507
1 parent 20c3644 commit 667ae49

File tree

13 files changed

+1681
-0
lines changed

13 files changed

+1681
-0
lines changed

examples/device/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ family_add_subdirectory(usbtmc)
3434
family_add_subdirectory(video_capture)
3535
family_add_subdirectory(video_capture_2ch)
3636
family_add_subdirectory(webusb_serial)
37+
family_add_subdirectory(ch341_serial)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
4+
5+
# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
6+
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
7+
8+
project(${PROJECT})
9+
10+
# Checks this example is valid for the family and initializes the project
11+
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
12+
13+
add_executable(${PROJECT})
14+
15+
# Example source
16+
target_sources(${PROJECT} PUBLIC
17+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
18+
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
19+
)
20+
21+
# Example include
22+
target_include_directories(${PROJECT} PUBLIC
23+
${CMAKE_CURRENT_SOURCE_DIR}/src
24+
)
25+
26+
# Configure compilation flags and libraries for the example... see the corresponding function
27+
# in hw/bsp/FAMILY/family.cmake for details.
28+
family_configure_device_example(${PROJECT})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
include ../../../tools/top.mk
2+
include ../../make.mk
3+
4+
INC += \
5+
src \
6+
$(TOP)/hw \
7+
8+
# Example source
9+
EXAMPLE_SOURCE += $(wildcard src/*.c)
10+
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
11+
12+
include ../../rules.mk
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <stdlib.h>
27+
#include <stdio.h>
28+
#include <string.h>
29+
#include <ctype.h>
30+
31+
#include "bsp/board.h"
32+
#include "tusb.h"
33+
34+
// Change this to 1 and event callback will be logged to stdout
35+
#define CH341_LOG_EVENTS 0
36+
37+
#if (CH341_LOG_EVENTS)
38+
static const char * _par_str[] =
39+
{
40+
"N",
41+
"O",
42+
"E",
43+
"M",
44+
"S"
45+
};
46+
#endif
47+
48+
//------------- prototypes -------------//
49+
static void ch341_task(void);
50+
51+
/*------------- MAIN -------------*/
52+
int main(void)
53+
{
54+
board_init();
55+
56+
// init device stack on configured roothub port
57+
tud_init(BOARD_TUD_RHPORT);
58+
59+
while (1)
60+
{
61+
tud_task(); // tinyusb device task
62+
ch341_task();
63+
}
64+
65+
return 0;
66+
}
67+
68+
// Echo back to terminal
69+
static void echo_serial_port( uint8_t buf[], uint32_t count)
70+
{
71+
tud_ch341_write(buf, count);
72+
tud_ch341_write_flush();
73+
}
74+
75+
//--------------------------------------------------------------------+
76+
// USB CH341
77+
//--------------------------------------------------------------------+
78+
static void ch341_task(void)
79+
{
80+
// connected() The serial port has been opened atleast once
81+
// Will continue to return true even after the serial port is closed.
82+
if ( tud_ch341_connected())
83+
{
84+
if ( tud_ch341_available() )
85+
{
86+
uint8_t buf[64];
87+
88+
uint32_t count = tud_ch341_read(buf, sizeof(buf));
89+
90+
// echo back to terminal
91+
echo_serial_port(buf, count);
92+
}
93+
}
94+
}
95+
96+
// Invoked when DTR/RTS changes
97+
void tud_ch341_line_state_cb(ch341_line_state_t line_state)
98+
{
99+
#if (CH341_LOG_EVENTS)
100+
printf("DTR=%u, RTS=%u\r\n",
101+
line_state & CH341_LINE_STATE_DTR_ACTIVE ? 1 : 0,
102+
line_state & CH341_LINE_STATE_RTS_ACTIVE ? 1 : 0);
103+
#else
104+
(void)(line_state);
105+
#endif
106+
}
107+
108+
// Invoked when line coding changes
109+
void tud_ch341_line_coding_cb(ch341_line_coding_t const* p_line_coding)
110+
{
111+
#if (CH341_LOG_EVENTS)
112+
printf("BITRATE=%lu (%s%u%u) RX:%s TX:%s\r\n",
113+
(unsigned long)p_line_coding->bit_rate,
114+
_par_str[p_line_coding->parity],
115+
p_line_coding->data_bits,
116+
p_line_coding->stop_bits ? 2 : 1,
117+
p_line_coding->rx_en ? "ON":"OFF",
118+
p_line_coding->tx_en ? "ON":"OFF");
119+
#else
120+
(void)(p_line_coding);
121+
#endif
122+
}
123+
124+
// Invoked when a break signal is received
125+
void tud_ch341_send_break_cb(bool is_break_active)
126+
{
127+
#if (CH341_LOG_EVENTS)
128+
printf("RCV BREAK=%s\r\n", is_break_active ? "ON" : "OFF");
129+
#else
130+
(void)(is_break_active);
131+
#endif
132+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef _TUSB_CONFIG_H_
27+
#define _TUSB_CONFIG_H_
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
34+
//--------------------------------------------------------------------+
35+
// Board Specific Configuration
36+
//--------------------------------------------------------------------+
37+
38+
// RHPort number used for device can be defined by board.mk, default to port 0
39+
#ifndef BOARD_TUD_RHPORT
40+
#define BOARD_TUD_RHPORT 0
41+
#endif
42+
43+
// RHPort max operational speed can defined by board.mk
44+
#ifndef BOARD_TUD_MAX_SPEED
45+
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
46+
#endif
47+
48+
//--------------------------------------------------------------------
49+
// COMMON CONFIGURATION
50+
//--------------------------------------------------------------------
51+
52+
// defined by board.mk
53+
#ifndef CFG_TUSB_MCU
54+
#error CFG_TUSB_MCU must be defined
55+
#endif
56+
57+
#ifndef CFG_TUSB_OS
58+
#define CFG_TUSB_OS OPT_OS_NONE
59+
#endif
60+
61+
#ifndef CFG_TUSB_DEBUG
62+
#define CFG_TUSB_DEBUG 0
63+
#endif
64+
65+
// Enable Device stack
66+
#define CFG_TUD_ENABLED 1
67+
68+
// Default is max speed that hardware controller could support with on-chip PHY
69+
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
70+
71+
#ifndef CFG_TUSB_RHPORT0_MODE
72+
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED
73+
#endif
74+
75+
//--------------------------------------------------------------------
76+
// DEVICE CONFIGURATION
77+
//--------------------------------------------------------------------
78+
79+
#ifndef CFG_TUD_ENDPOINT0_SIZE
80+
#define CFG_TUD_ENDPOINT0_SIZE 64
81+
#endif
82+
83+
//------------- CLASS -------------//
84+
#define CFG_TUD_CDC 0
85+
#define CFG_TUD_MSC 0
86+
#define CFG_TUD_HID 0
87+
#define CFG_TUD_MIDI 0
88+
#define CFG_TUD_VENDOR 0
89+
#define CFG_TUD_CH341 1
90+
91+
// CH341 Endpoint max packet sizes (should not change)
92+
#define CFG_TUD_CH341_EP_RX_MAX_PACKET (TUD_OPT_HIGH_SPEED ? 512 : 64)
93+
#define CFG_TUD_CH341_EP_TX_MAX_PACKET CFG_TUD_CH341_EP_RX_MAX_PACKET
94+
#define CFG_TUD_CH341_EP_TXNOTIFY_MAX_PACKET (8)
95+
96+
// CH341 buffer size for TX and RX data (must be an interval of max packet size)
97+
// more is faster
98+
#define CFG_TUD_CH341_FIFO_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
99+
100+
#ifdef __cplusplus
101+
}
102+
#endif
103+
104+
#endif /* _TUSB_CONFIG_H_ */

0 commit comments

Comments
 (0)