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

tinyusb via Arduino library #359

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

µCNC - Universal CNC firmware for microcontrollers

# VERSION 1.6+ TINYUSB NOTES

From version 1.6 and newer TinyUSB integration is now managed via a and external Arduino IDE library customized for µCNC and can be downloaded at [uCNC-tinyusb](https://github.com/Paciente8159/uCNC-tinyusb).
This core change makes TinyUSB future updates much more easier and quicker. This also may help USB hosting implementation in a near future to allow USB drives to be plugged and read by the driver.

# IMPORTANT NOTE

By default and as a safety measure µCNC control inputs (Emergency stop, Safety door, Hold, Cycle start-resume), as well as limit switches and probe, are held high by the microcontroller input weak-pull up resistors. If left unconnected or connected to normally opened switches these inputs will be in an active/triggered state and the controller may lock all motions.
Expand Down
4 changes: 3 additions & 1 deletion makefiles/samd21/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ BUILD_OPTIONS ?=

C_DEFS = $(BUILD_OPTIONS)\
-DSAMD21 \
-DCFG_TUSB_MCU=OPT_MCU_SAMD21 \
-D__SAMD21E18A__ \
-DBOARD=BOARD_$(BOARD)

Expand All @@ -122,7 +123,8 @@ C_INCLUDES = \
-Idrivers/ \
-Iasf/ \
-I"../../uCNC/" \
-I"../../uCNC/src/"
-I"../../uCNC/src/"\
-I"../../uCNC-tinyusb/src"

C_INCLUDES += $(foreach d,$(C_HEADERS),$(addsuffix $(dir $(d)),"-I"))

Expand Down
4 changes: 3 additions & 1 deletion makefiles/stm32f1x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ BUILD_OPTIONS ?=

C_DEFS = $(BUILD_OPTIONS)\
-DSTM32F103xB \
-DCFG_TUSB_MCU=OPT_MCU_STM32F1 \
-DBOARD=BOARD_$(BOARD)

# AS includes
Expand All @@ -124,7 +125,8 @@ C_INCLUDES = \
-I"./" \
-Idrivers/ \
-I"../../uCNC/" \
-I"../../uCNC/src/"
-I"../../uCNC/src/"\
-I"../../uCNC-tinyusb/src"

C_INCLUDES += $(foreach d,$(C_HEADERS),$(addsuffix $(dir $(d)),"-I"))

Expand Down
4 changes: 3 additions & 1 deletion makefiles/stm32f4x/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ BUILD_OPTIONS ?=

C_DEFS = $(BUILD_OPTIONS)\
-DSTM32F401xC \
-DCFG_TUSB_MCU=OPT_MCU_STM32F4 \
-DBOARD=BOARD_$(BOARD)

# AS includes
Expand All @@ -125,7 +126,8 @@ C_INCLUDES = \
-I"./" \
-Idrivers/ \
-I"../../uCNC/" \
-I"../../uCNC/src/"
-I"../../uCNC/src/"\
-I"../../uCNC-tinyusb/src"

C_INCLUDES += $(foreach d,$(C_HEADERS),$(addsuffix $(dir $(d)),"-I"))

Expand Down
4 changes: 2 additions & 2 deletions uCNC/src/cnc_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ extern "C"
{
#endif

#define CNC_MAJOR_MINOR_VERSION "1.5"
#define CNC_PATCH_VERSION ".7"
#define CNC_MAJOR_MINOR_VERSION "1.6"
#define CNC_PATCH_VERSION ".pre"

#define CNC_VERSION CNC_MAJOR_MINOR_VERSION CNC_PATCH_VERSION

Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/boards/avr/avr.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
platform = atmelavr
; platform_packages = platformio/tool-simavr
framework = arduino
build_src_filter = +<*>-<src/tinyusb>
; build_src_filter = +<*>-<src/tinyusb>
extra_scripts = avr_compiler.py
; debug_tool = simavr
debug_build_flags = -Og -g3 -ggdb3 -gdwarf-2
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/boards/esp32/esp32.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
platform = espressif32
framework = arduino
board = wemos_d1_uno32
build_src_filter = +<*>-<src/tinyusb>
; build_src_filter = +<*>-<src/tinyusb>
build_flags = -mlongcalls -Wno-frame-address -ffunction-sections -fdata-sections -ggdb -Os -freorder-blocks -Wwrite-strings -fstack-protector -fstrict-volatile-bitfields -Wall -fno-jump-tables -fno-tree-switch-conversion -std=gnu++11 -fexceptions -MMD -c -DENABLE_WIFI -DENABLE_BLUETOOTH -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
board_build.f_flash = 80000000L
board_build.f_cpu = 240000000L
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/boards/esp8266/esp8266.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
platform = espressif8266
framework = arduino
board = d1
build_src_filter = +<*>-<src/tinyusb>
; build_src_filter = +<*>-<src/tinyusb>
lib_deps =
https://github.com/tzapu/WiFiManager/archive/refs/heads/master.zip
build_flags = -DBOARD=BOARD_WEMOS_D1 -DENABLE_WIFI
Expand Down
1 change: 1 addition & 0 deletions uCNC/src/hal/boards/lpc176x/lpc176x.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[common_lpc176x]
platform = https://github.com/p3p/pio-nxplpc-arduino-lpc176x/archive/0.1.3.zip
platform_packages = Paciente8159/framework-arduino-lpc176x @ https://github.com/Paciente8159/pio-framework-arduino-lpc176x/archive/0.2.10.zip
lib_deps = https://github.com/Paciente8159/uCNC-tinyusb/archive/refs/heads/master.zip
lib_ldf_mode = off
lib_compat_mode = strict
build_flags = ${common.build_flags} -D NXP_LPC17xx
Expand Down
1 change: 1 addition & 0 deletions uCNC/src/hal/boards/samd21/samd21.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
platform = atmelsam
board = zeroUSB
build_flags = ${common.build_flags} -nostdlib -fno-exceptions
lib_deps = https://github.com/Paciente8159/uCNC-tinyusb/archive/refs/heads/master.zip
; platform_packages = platformio/tool-openocd
; debug_build_flags = -Og -g3 -ggdb3 -gdwarf-2
; debug_tool = cmsis-dap
Expand Down
1 change: 1 addition & 0 deletions uCNC/src/hal/boards/stm32/stm32.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ debug_init_cmds =
load
monitor reset init
build_flags = ${common.build_flags} -D HAL_TIM_MODULE_DISABLED -D HAL_EXTI_MODULE_DISABLED -D HAL_UART_MODULE_ONLY -D FRAMEWORK_CLOCKS_INIT
lib_deps = https://github.com/Paciente8159/uCNC-tinyusb/archive/refs/heads/master.zip

[env:bluepill_f103c8]
extends = common_stm32
Expand Down
19 changes: 9 additions & 10 deletions uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include "system_LPC17xx.h"

#ifdef MCU_HAS_USB
#include "../../../tinyusb/tusb_config.h"
#include "../../../tinyusb/src/tusb.h"
#include <tusb_ucnc.h>
#endif

/**
Expand Down Expand Up @@ -264,7 +263,7 @@ void MCU_COM_ISR(void)
void USB_IRQHandler(void)
{
mcu_disable_global_isr();
tud_int_handler(0);
tusb_cdc_isr_handler();
mcu_enable_global_isr();
}
#endif
Expand Down Expand Up @@ -345,7 +344,7 @@ void mcu_usart_init(void)
NVIC_ClearPendingIRQ(USB_IRQn);
NVIC_EnableIRQ(USB_IRQn);

tusb_init();
tusb_cdc_init();
#endif
}

Expand Down Expand Up @@ -525,11 +524,11 @@ void mcu_putc(char c)
#ifdef MCU_HAS_USB
if (c != 0)
{
tud_cdc_write_char(c);
tusb_cdc_write(c);
}
if (c == '\r' || c == 0)
{
tud_cdc_write_flush();
tusb_cdc_flush();
}
#endif
}
Expand Down Expand Up @@ -680,12 +679,12 @@ void mcu_delay_us(uint16_t delay)
void mcu_dotasks()
{
#ifdef MCU_HAS_USB
tud_cdc_write_flush();
tud_task(); // tinyusb device task
tusb_cdc_flush();
tusb_cdc_task(); // tinyusb device task

while (tud_cdc_available())
while (tusb_cdc_available())
{
unsigned char c = (unsigned char)tud_cdc_read_char();
unsigned char c = (unsigned char)tusb_cdc_read();
mcu_com_rx_cb(c);
}
#endif
Expand Down
8 changes: 3 additions & 5 deletions uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h
Original file line number Diff line number Diff line change
Expand Up @@ -3819,18 +3819,16 @@ extern "C"
#define mcu_get_global_isr() lpc_global_isr_enabled

#if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB))
extern uint32_t tud_cdc_n_write_available(uint8_t itf);
extern uint32_t tud_cdc_n_available(uint8_t itf);
#define mcu_rx_ready() (CHECKBIT(COM_UART->LSR, 0) || tud_cdc_n_available(0))
#define mcu_rx_ready() (CHECKBIT(COM_UART->LSR, 0) || tusb_cdc_write_available())
#define mcu_tx_ready() (CHECKBIT(COM_UART->LSR, 5))
#elif defined(MCU_HAS_UART)
#define mcu_rx_ready() (CHECKBIT(COM_UART->LSR, 0))
#define mcu_tx_ready() (CHECKBIT(COM_UART->LSR, 5))
#elif defined(MCU_HAS_USB)
extern uint32_t tud_cdc_n_write_available(uint8_t itf);
extern uint32_t tud_cdc_n_available(uint8_t itf);
#define mcu_rx_ready() tud_cdc_n_available(0)
#define mcu_tx_ready() tud_cdc_n_write_available(0)
#define mcu_rx_ready() tusb_cdc_available()
#define mcu_tx_ready() tusb_cdc_write_available()
#endif

#define mcu_spi_xmit(X) \
Expand Down
21 changes: 0 additions & 21 deletions uCNC/src/hal/mcus/mcudefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,30 @@ extern "C"
*/
#if (MCU == MCU_AVR)
#include "avr/mcumap_avr.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_NONE
#endif
#endif

#if (MCU == MCU_STM32F1X)
#include "stm32f1x/mcumap_stm32f1x.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_STM32F1
#endif
#endif

#if (MCU == MCU_STM32F4X)
#include "stm32f4x/mcumap_stm32f4x.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_STM32F4
#endif
#endif

#if (MCU == MCU_SAMD21)
#include "samd21/mcumap_samd21.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_SAMD21
#endif
#endif

#if (MCU == MCU_LPC176X)
#include "lpc176x/mcumap_lpc176x.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_LPC175X_6X
#endif
#endif

#if (MCU == MCU_ESP8266)
#include "esp8266/mcumap_esp8266.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_NONE
#endif
#endif

#if (MCU == MCU_ESP32)
#include "esp32/mcumap_esp32.h"
#ifndef CFG_TUSB_MCU
#define CFG_TUSB_MCU OPT_MCU_NONE
#endif
#endif

#if (MCU == MCU_VIRTUAL_WIN)
Expand Down
19 changes: 9 additions & 10 deletions uCNC/src/hal/mcus/samd21/mcu_samd21.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
#define NVM_MEMORY ((volatile uint16_t *)FLASH_ADDR)

#ifdef MCU_HAS_USB
#include "../../../tinyusb/tusb_config.h"
#include "../../../tinyusb/src/tusb.h"
#include <tusb_ucnc.h>
#endif

volatile bool samd21_global_isr_enabled;
Expand Down Expand Up @@ -299,15 +298,15 @@ void mcu_usart_init(void)
// USB->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_SPDCONF_FS;
while (USB->DEVICE.SYNCBUSY.bit.SWRST)
;
tusb_init();
tusb_cdc_init();
#endif
}

#ifdef MCU_HAS_USB
void USB_Handler(void)
{
mcu_disable_global_isr();
tud_int_handler(0);
tusb_cdc_isr_handler();
mcu_enable_global_isr();
}
#endif
Expand Down Expand Up @@ -705,11 +704,11 @@ void mcu_putc(char c)
#ifdef MCU_HAS_USB
if (c != 0)
{
tud_cdc_write_char(c);
tusb_cdc_write(c);
}
if (c == '\r' || c == 0)
{
tud_cdc_write_flush();
tusb_cdc_flush();
}
#endif
}
Expand Down Expand Up @@ -919,12 +918,12 @@ void mcu_delay_us(uint16_t delay)
void mcu_dotasks(void)
{
#ifdef MCU_HAS_USB
tud_cdc_write_flush();
tud_task(); // tinyusb device task
tusb_cdc_flush();
tusb_cdc_task(); // tinyusb device task

while (tud_cdc_available())
while (tusb_cdc_available())
{
unsigned char c = (unsigned char)tud_cdc_read_char();
unsigned char c = (unsigned char)tusb_cdc_read();
mcu_com_rx_cb(c);
}
#endif
Expand Down
13 changes: 6 additions & 7 deletions uCNC/src/hal/mcus/samd21/mcumap_samd21.h
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,9 @@ extern "C"
#define USB_DP_PMUXVAL (pinmuxval(USB_DP_MUX))
#define DIO203_PMUX USB_DP_PMUX
#define DIO203_PMUXVAL USB_DP_PMUXVAL
#ifdef USBCON
#undef USBCON
#endif
#endif

#ifdef MCU_HAS_UART
Expand Down Expand Up @@ -3048,18 +3051,14 @@ extern "C"
#define mcu_get_global_isr() samd21_global_isr_enabled

#if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB))
extern uint32_t tud_cdc_n_write_available(uint8_t itf);
extern uint32_t tud_cdc_n_available(uint8_t itf);
#define mcu_rx_ready() ((COM_UART->USART.INTFLAG.bit.RXC) || tud_cdc_n_available(0))
#define mcu_rx_ready() ((COM_UART->USART.INTFLAG.bit.RXC) || tusb_cdc_write_available())
#define mcu_tx_ready() (COM_UART->USART.INTFLAG.bit.DRE)
#elif defined(MCU_HAS_UART)
#define mcu_rx_ready() (COM_UART->USART.INTFLAG.bit.RXC)
#define mcu_tx_ready() (COM_UART->USART.INTFLAG.bit.DRE)
#elif defined(MCU_HAS_USB)
extern uint32_t tud_cdc_n_write_available(uint8_t itf);
extern uint32_t tud_cdc_n_available(uint8_t itf);
#define mcu_rx_ready() tud_cdc_n_available(0)
#define mcu_tx_ready() tud_cdc_n_write_available(0)
#define mcu_rx_ready() tusb_cdc_available()
#define mcu_tx_ready() tusb_cdc_write_available()
#endif

#ifdef MCU_HAS_SPI
Expand Down
Loading