replaced the default pico uart libs with a custom tinyusb-based serial lib

This commit is contained in:
FizzyApple12
2022-07-02 17:40:12 -04:00
parent c188e9e782
commit c20632e9b4
8 changed files with 255 additions and 60 deletions
+8 -8
View File
@@ -10,10 +10,10 @@ pico_sdk_init()
add_executable(wacca_protocol_translator
main.c
waccaserial.h
serial.c
waccaserial.c
queue.h
queue.c
usbDescriptors.c
)
# Add pico_stdlib library which aggregates commonly used features
@@ -21,12 +21,12 @@ target_link_libraries(wacca_protocol_translator
pico_stdlib
hardware_i2c
pico_multicore
pico_stdio
pico_stdio_usb
tinyusb_device
)
target_include_directories(wacca_protocol_translator PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(wacca_protocol_translator)
pico_enable_stdio_usb(wacca_protocol_translator 1)
pico_enable_stdio_uart(wacca_protocol_translator 0)
pico_add_extra_outputs(wacca_protocol_translator)
-1
View File
@@ -2,7 +2,6 @@
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "waccaserial.h"
#include <stdio.h>
#define TOUCH_I2C_PORT i2c0
#define TOUCH_I2C_SDA 4
+31
View File
@@ -0,0 +1,31 @@
#include "serial.h"
#include "pico.h"
#include "tusb.h"
void setup() {
tusb_init();
}
void update() {
tud_task();
}
void write(uint8_t data) {
tud_cdc_n_write_char(0, data);
}
void sendRemaining() {
tud_cdc_n_write_flush(0);
}
uint8_t read() {
if (!tud_cdc_n_available(0)) return 0xff;
uint8_t buffer[1];
uint32_t count = tud_cdc_n_read(0, buffer, 1);
if (count == 0) return 0xff;
return buffer[0];
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef _CUSTOM_SERIAL_
#define _CUSTOM_SERIAL_
#include "pico.h"
void setup();
void update();
void write(uint8_t data);
void sendRemaining();
uint8_t read();
#endif
+60
View File
@@ -0,0 +1,60 @@
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
//--------------------------------------------------------------------
// 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
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
// 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_NONE
#endif
#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
#define CFG_TUD_CDC 1
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#endif /* _TUSB_CONFIG_H_ */
+108
View File
@@ -0,0 +1,108 @@
#include "tusb.h"
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
#define USB_VID 0xCafe
#define USB_BCD 0x0200
//--------------------------------------------------------------------+
// Device Descriptors
//--------------------------------------------------------------------+
tusb_desc_device_t const desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = USB_BCD,
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USB_VID,
.idProduct = USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01
};
uint8_t const * tud_descriptor_device_cb(void) {
return (uint8_t const *) &desc_device;
}
//--------------------------------------------------------------------+
// Configuration Descriptor
//--------------------------------------------------------------------+
enum {
ITF_NUM_CDC_0 = 0,
ITF_NUM_CDC_0_DATA,
// ITF_NUM_CDC_1,
// ITF_NUM_CDC_1_DATA,
ITF_NUM_TOTAL
};
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN)
#define EPNUM_CDC_0_NOTIF 0x81
#define EPNUM_CDC_0_OUT 0x02
#define EPNUM_CDC_0_IN 0x82
//#define EPNUM_CDC_1_NOTIF 0x83
//#define EPNUM_CDC_1_OUT 0x04
//#define EPNUM_CDC_1_IN 0x84
uint8_t const desc_fs_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, 4, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64),
//TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, 4, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64),
};
uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
(void) index;
return desc_fs_configuration;
}
//--------------------------------------------------------------------+
// String Descriptors
//--------------------------------------------------------------------+
char const* string_desc_arr [] = {
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
"FizzyApple12", // 1: Manufacturer
"Wacca Controller", // 2: Product
"mb-rp2040", // 3: Serials, should use chip ID
"Wacca CDC", // 4: CDC Interface
};
static uint16_t _desc_str[32];
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
(void) langid;
uint8_t chr_count;
if (index == 0) {
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
} else {
if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0]))) return NULL;
const char* str = string_desc_arr[index];
chr_count = strlen(str);
if (chr_count > 31) chr_count = 31;
for(uint8_t i = 0; i < chr_count; i++) {
_desc_str[1 + i] = str[i];
}
}
_desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * chr_count + 2);
return _desc_str;
}
+31 -40
View File
@@ -1,4 +1,5 @@
#include "waccaserial.h"
#include "pico.h"
#include "pico/time.h"
#include "pico/multicore.h"
@@ -6,10 +7,9 @@
#include <stdbool.h>
#include <string.h>
#include "queue.h"
#include "pico/stdio.h"
#include "pico/stdio_usb.h"
#include "pico/stdlib.h"
#include <stdio.h>
#include "serial.h"
// Massive thank you to the contributors for the WACVR project <3
@@ -32,10 +32,7 @@ bool runningMulticore = false;
#endif
void ws_start() {
stdio_init_all();
stdio_flush();
stdio_filter_driver(NULL);
setup();
#ifdef DEBUG_MODE
gpio_init(PICO_DEFAULT_LED_PIN);
@@ -56,6 +53,8 @@ void ws_touchThreadLoop() {
blink0();
#endif
update(); // update tinyusb
uint32_t currentTime = to_ms_since_boot(get_absolute_time());
elapsedTime += currentTime - previousTime;
previousTime = currentTime;
@@ -69,12 +68,6 @@ void ws_touchThreadLoop() {
dequeue(&touchQueueHead);
ws_sendTouchState();
}
//if (StartUp) ws_sendTouchState();
//ws_sendTouchState();
//if (stdio_usb_connected()) ws_readHead();
ws_readHead();
}
}
@@ -84,14 +77,14 @@ void ws_sendTouchState() {
}
void ws_readHead() {
inByte = getchar_timeout_us(0);
inByte = read();
if (inByte != 0xff && inByte != 0) {
uint8_t data = 0;
uint8_t importantData = 0;
int i = 0;
do {
data = getchar_timeout_us(0);
data = read();
if (i == 2) importantData = data;
i++;
} while (data != 0xff && data != 0);
@@ -113,13 +106,13 @@ void ws_sendResp(char importantNextReadData) {
StartUp = false;
putchar_raw(inByte);
write(inByte);
for (int i = 0; i < 6; i++) {
putchar_raw(SYNC_BOARD_VER[i]);
write(SYNC_BOARD_VER[i]);
}
putchar_raw(44);
write(44);
break;
case CMD_NEXT_READ:
#ifdef DEBUG_MODE
@@ -131,24 +124,24 @@ void ws_sendResp(char importantNextReadData) {
switch (importantNextReadData) {
case 0x30:
for (int i = 0; i < 80; i++) {
putchar_raw(read1[i]);
write(read1[i]);
}
putchar_raw(bh_calcChecksum((uint8_t *) read1, 80));
write(bh_calcChecksum((uint8_t *) read1, 80));
break;
case 0x31:
for (int i = 0; i < 80; i++) {
putchar_raw(read2[i]);
write(read2[i]);
}
putchar_raw(bh_calcChecksum((uint8_t *) read2, 80));
write(bh_calcChecksum((uint8_t *) read2, 80));
break;
case 0x33:
for (int i = 0; i < 80; i++) {
putchar_raw(read3[i]);
write(read3[i]);
}
putchar_raw(bh_calcChecksum((uint8_t *) read3, 80));
write(bh_calcChecksum((uint8_t *) read3, 80));
break;
default:
break;
@@ -161,30 +154,30 @@ void ws_sendResp(char importantNextReadData) {
blink();
#endif
putchar_raw(inByte);
write(inByte);
for (int i = 0; i < 6; i++) {
putchar_raw(SYNC_BOARD_VER[i]);
write(SYNC_BOARD_VER[i]);
}
#ifdef RIGHT
putchar_raw('R');
write('R');
#endif
#ifndef RIGHT
putchar_raw('L');
write('L');
#endif
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 6; i++) {
putchar_raw(UNIT_BOARD_VER[i]);
write(UNIT_BOARD_VER[i]);
}
}
#ifdef RIGHT
putchar_raw(118);
write(118);
#endif
#ifndef RIGHT
putchar_raw(104);
write(104);
#endif
break;
@@ -199,7 +192,7 @@ void ws_sendResp(char importantNextReadData) {
StartUp = false;
for (int i = 0; i < 7; i++) {
putchar_raw(SettingData_162[i]);
write(SettingData_162[i]);
}
break;
case CMD_MYSTERY2:
@@ -210,7 +203,7 @@ void ws_sendResp(char importantNextReadData) {
StartUp = false;
for (int i = 0; i < 7; i++) {
putchar_raw(SettingData_148[i]);
write(SettingData_148[i]);
}
break;
case CMD_START_AUTO_SCAN:
@@ -223,15 +216,10 @@ void ws_sendResp(char importantNextReadData) {
#endif
for (int i = 0; i < 7; i++) {
putchar_raw(SettingData_201[i]);
write(SettingData_201[i]);
}
StartUp = true;
/*if (!runningMulticore) {
multicore_launch_core1(ws_touchThreadLoop);
runningMulticore = true;
}*/
break;
case CMD_BEGIN_WRITE:
break;
@@ -241,6 +229,7 @@ void ws_sendResp(char importantNextReadData) {
StartUp = false;
break;
}
sendRemaining();
}
void ws_getTouchPack() {
@@ -256,8 +245,10 @@ void ws_sendTouch() {
ws_getTouchPack();
for (int i = 0; i < 36; i++) {
putchar_raw(TouchPack[i]);
write(TouchPack[i]);
}
sendRemaining();
}
}
+1 -11
View File
@@ -2,22 +2,12 @@
#define _WACCA_SERIAL_
#include "pico.h"
#include "pico/time.h"
#include "pico/multicore.h"
#include "hardware/i2c.h"
#include <stdbool.h>
#include <string.h>
#include "queue.h"
#include "pico/stdio.h"
#include "pico/stdio_usb.h"
#include "pico/stdlib.h"
#include <stdio.h>
// Massive thank you to the contributors for the WACVR project <3
//#define DEBUG_MODE // uncomment for debug messages (note: this will slow things down!)
#define RIGHT //uncomment to enable righty flip on interface and sync board data swap
//#define RIGHT //uncomment to enable righty flip on interface and sync board data swap
#define CMD_GET_SYNC_BOARD_VER 0xa0
#define CMD_NEXT_READ 0x72