├── .gitignore ├── src ├── r_gpt_cfg.h ├── r_dtc_cfg.h ├── r_iic_master_cfg.h ├── r_sci_i2c_cfg.h ├── r_sci_uart_cfg.h ├── r_flash_lp_cfg.h ├── flash.h ├── tusb_config.h ├── r_flash_hp_cfg.h ├── usb_descriptors.c ├── irq_table.c ├── bossa.c └── main.c ├── README.md ├── compile.sh ├── Makefile.minima ├── Makefile.c33 ├── Makefile.wifi ├── Makefile.opta-analog ├── Makefile.opta-digital └── 0001-fix-arduino-bootloaders.patch /.gitignore: -------------------------------------------------------------------------------- 1 | _build/ 2 | build/ 3 | -------------------------------------------------------------------------------- /src/r_gpt_cfg.h: -------------------------------------------------------------------------------- 1 | #define GPT_CFG_PARAM_CHECKING_ENABLE (1) 2 | #define GPT_CFG_OUTPUT_SUPPORT_ENABLE (2) 3 | #define GPT_CFG_WRITE_PROTECT_ENABLE (0) 4 | #define GPT_CFG_GPTCLK_BYPASS 1 -------------------------------------------------------------------------------- /src/r_dtc_cfg.h: -------------------------------------------------------------------------------- 1 | /* generated configuration header file - do not edit */ 2 | #ifndef R_DTC_CFG_H_ 3 | #define R_DTC_CFG_H_ 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define DTC_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) 9 | #define DTC_CFG_VECTOR_TABLE_SECTION_NAME ".fsp_dtc_vector_table" 10 | 11 | #ifdef __cplusplus 12 | } 13 | #endif 14 | #endif /* R_DTC_CFG_H_ */ 15 | -------------------------------------------------------------------------------- /src/r_iic_master_cfg.h: -------------------------------------------------------------------------------- 1 | #ifndef R_IIC_MASTER_CFG_H_ 2 | #define R_IIC_MASTER_CFG_H_ 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #define IIC_MASTER_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) 8 | #define IIC_MASTER_CFG_DTC_ENABLE (1) 9 | #define IIC_MASTER_CFG_ADDR_MODE_10_BIT_ENABLE (0) 10 | 11 | #ifdef __cplusplus 12 | } 13 | #endif 14 | #endif /* R_IIC_MASTER_CFG_H_ */ 15 | -------------------------------------------------------------------------------- /src/r_sci_i2c_cfg.h: -------------------------------------------------------------------------------- 1 | #ifndef R_SCI_I2C_CFG_H_ 2 | #define R_SCI_I2C_CFG_H_ 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #define SCI_SIIC_CFG_PREREQUISITE_CHECKING_ENABLE 1 8 | #define SCI_I2C_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) 9 | #define SCI_I2C_CFG_DTC_ENABLE (0) 10 | #define SCI_I2C_CFG_ADDR_MODE_10_BIT_ENABLE (0) 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | #endif /* R_SCI_I2C_CFG_H_ */ 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Arduino bootloader for Renesas boards** 2 | 3 | ``` 4 | git clone https://github.com/arduino/arduino-renesas-bootloader 5 | git clone https://github.com/hathach/tinyusb.git 6 | cd tinyusb 7 | git checkout 0.17.0 8 | python ./tools/get_deps.py ra 9 | export TINYUSB_ROOT=$PWD 10 | patch -p1 < ../arduino-renesas-bootloader/0001-fix-arduino-bootloaders.patch 11 | cd .. 12 | cd arduino-renesas-bootloader 13 | ./compile.sh 14 | ``` 15 | -------------------------------------------------------------------------------- /src/r_sci_uart_cfg.h: -------------------------------------------------------------------------------- 1 | /* generated configuration header file - do not edit */ 2 | #ifndef R_SCI_UART_CFG_H_ 3 | #define R_SCI_UART_CFG_H_ 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define SCI_UART_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) 9 | #define SCI_UART_CFG_FIFO_SUPPORT (1) 10 | #define SCI_UART_CFG_DTC_SUPPORTED (1) 11 | #define SCI_UART_CFG_FLOW_CONTROL_SUPPORT (0) 12 | #define SCI_UART_CFG_RS485_SUPPORT (0) 13 | 14 | #ifdef __cplusplus 15 | } 16 | #endif 17 | #endif /* R_SCI_UART_CFG_H_ */ 18 | -------------------------------------------------------------------------------- /src/r_flash_lp_cfg.h: -------------------------------------------------------------------------------- 1 | /* generated configuration header file - do not edit */ 2 | #ifndef R_FLASH_LP_CFG_H_ 3 | #define R_FLASH_LP_CFG_H_ 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #define FLASH_LP_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) 9 | #ifndef FLASH_LP_CFG_CODE_FLASH_PROGRAMMING_ENABLE 10 | #define FLASH_LP_CFG_CODE_FLASH_PROGRAMMING_ENABLE (1) 11 | #endif 12 | #ifndef FLASH_LP_CFG_DATA_FLASH_PROGRAMMING_ENABLE 13 | #define FLASH_LP_CFG_DATA_FLASH_PROGRAMMING_ENABLE (1) 14 | #endif 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | #endif /* R_FLASH_LP_CFG_H_ */ 20 | -------------------------------------------------------------------------------- /compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ x$TINYUSB_ROOT == x ]; then 4 | echo Please set TINYUSB_ROOT env variable 5 | exit 1 6 | fi 7 | 8 | PROJECT_NAME=`basename $PWD` 9 | 10 | if [ -d _build ]; then 11 | rm -R _build 12 | fi 13 | 14 | if [ -d distrib ]; then 15 | rm -R distrib 16 | fi 17 | 18 | mkdir distrib 19 | 20 | make -f Makefile.wifi -j8 21 | mv _build/uno_r4/$PROJECT_NAME.hex distrib/dfu_wifi.hex 22 | rm -rf _build 23 | 24 | make -f Makefile.minima -j8 25 | mv _build/uno_r4/$PROJECT_NAME.hex distrib/dfu_minima.hex 26 | rm -rf _build 27 | 28 | make -f Makefile.c33 -j8 29 | mv _build/portenta_c33/$PROJECT_NAME.hex distrib/dfu_c33.hex 30 | rm -rf _build 31 | 32 | make -f Makefile.opta-analog -j8 33 | mv _build/uno_r4/$PROJECT_NAME.hex distrib/opta-analog.hex 34 | rm -rf _build 35 | 36 | make -f Makefile.opta-digital -j8 37 | mv _build/uno_r4/$PROJECT_NAME.hex distrib/opta-digital.hex 38 | rm -rf _build -------------------------------------------------------------------------------- /src/flash.h: -------------------------------------------------------------------------------- 1 | #if BSP_FEATURE_FLASH_LP_VERSION 2 | #include "r_flash_lp.h" 3 | #define flash_instance_ctrl_t flash_lp_instance_ctrl_t 4 | #define flash_apis g_flash_on_flash_lp 5 | #define PROGRAM_BLOCK_SIZE (2 * 1024) 6 | #if defined BOSSA_LOADER && defined DFU_LOADER 7 | #define SKETCH_FLASH_OFFSET (32 * 1024) 8 | #else 9 | #define SKETCH_FLASH_OFFSET (16 * 1024) 10 | #endif 11 | #endif 12 | 13 | #if BSP_FEATURE_FLASH_HP_VERSION 14 | #include "r_flash_hp.h" 15 | #define flash_instance_ctrl_t flash_hp_instance_ctrl_t 16 | #define flash_apis g_flash_on_flash_hp 17 | #define PROGRAM_BLOCK_SIZE (32 * 1024) 18 | #define SKETCH_FLASH_OFFSET (64 * 1024) 19 | #define TURN_OFF_CHARGER_LED 20 | void i2c_begin(); 21 | void i2c_write(uint8_t address, uint8_t reg, uint8_t value); 22 | #endif 23 | 24 | /* Key code for writing PRCR register. */ 25 | #define BSP_PRV_PRCR_KEY (0xA500U) 26 | #define BSP_PRV_PRCR_PRC1_UNLOCK ((BSP_PRV_PRCR_KEY) | 0x2U) 27 | #define BSP_PRV_PRCR_LOCK ((BSP_PRV_PRCR_KEY) | 0x0U) -------------------------------------------------------------------------------- /Makefile.minima: -------------------------------------------------------------------------------- 1 | TOP ?= $(TINYUSB_ROOT) 2 | BOARD ?= uno_r4 3 | 4 | # run 5 | # python tools/get_deps.py ra 6 | # once in the tinyusb root folder 7 | 8 | include $(TOP)/examples/build_system/make/make.mk 9 | 10 | INC += \ 11 | src \ 12 | $(TOP)/hw \ 13 | 14 | EXAMPLE_SOURCE += $(wildcard src/*.c) 15 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_flash_lp/r_flash_lp.c 16 | CFLAGS += -DFLASH_LP_CFG_CODE_FLASH_PROGRAMMING_ENABLE -DFLASH_LP_CFG_DATA_FLASH_PROGRAMMING_ENABLE 17 | CFLAGS += -DUSB_PID=0x0369 -DBOARD_NAME=\"UNO\ R4\ Minima\" 18 | CFLAGS += -DLED_FADE_PWM_OUT_A=1 -DLED_FADE_PWM_OUT_B=0 -DLED_FADE_PWM_CHANNEL=3 -DLED_TIMER_SOURCE_DIV=2 19 | CFLAGS += -DLED_FADE_GPIO=BSP_IO_PORT_01_PIN_11 20 | CFLAGS += -DDFU_LOADER 21 | CFLAGS += -DOVERRIDE_VECTOR_TABLE=1 22 | CFLAGS += -DCODE_FLASH_DESCRIPTOR=\"@CodeFlash\ /0x00000000/8*2Ka,120*2Kg\" 23 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_gpt/r_gpt.c 24 | 25 | 26 | SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) 27 | SRC_C += $(FSP_SOURCE) 28 | 29 | CFLAGS += -Os -flto 30 | CFLAGS += -ffunction-sections -fdata-sections 31 | LDFLAGS += -Wl,--gc-sections -flto 32 | 33 | CFLAGS += --warn-no-undef 34 | 35 | include $(TOP)/examples/build_system/make/rules.mk 36 | 37 | LD_FILE = $(FAMILY_PATH)/linker/gcc/$(MCU_VARIANT).ld -------------------------------------------------------------------------------- /Makefile.c33: -------------------------------------------------------------------------------- 1 | TOP ?= $(TINYUSB_ROOT) 2 | BOARD ?= portenta_c33 3 | 4 | # run 5 | # python tools/get_deps.py ra 6 | # once in the tinyusb root folder 7 | 8 | include $(TOP)/examples/build_system/make/make.mk 9 | 10 | INC += \ 11 | src \ 12 | $(TOP)/hw \ 13 | 14 | EXAMPLE_SOURCE += $(wildcard src/*.c) 15 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_flash_hp/r_flash_hp.c $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_sci_i2c/r_sci_i2c.c 16 | CFLAGS += -DFLASH_HP_CFG_CODE_FLASH_PROGRAMMING_ENABLE -DFLASH_HP_CFG_DATA_FLASH_PROGRAMMING_ENABLE 17 | CFLAGS += -DUSB_PID=0x0368 -DBOARD_NAME=\"Portenta\ C33\" 18 | CFLAGS += -DLED_FADE_PWM_OUT_A=1 -DLED_FADE_PWM_OUT_B=0 -DLED_FADE_PWM_CHANNEL=6 -DLED_TIMER_SOURCE_DIV=1 19 | CFLAGS += -DLED_FADE_GPIO=BSP_IO_PORT_04_PIN_00 20 | CFLAGS += -DUSE_HS_PORT_FOR_DFU 21 | CFLAGS += -DOVERRIDE_VECTOR_TABLE=1 22 | CFLAGS += -DDFU_LOADER 23 | CFLAGS += -DCODE_FLASH_DESCRIPTOR=\"@CodeFlash\ /0x00000000/8*8Ka,63*32Kg\" 24 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_gpt/r_gpt.c 25 | 26 | 27 | SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) 28 | SRC_C += $(FSP_SOURCE) 29 | 30 | CFLAGS += -Os -flto 31 | CFLAGS += -ffunction-sections -fdata-sections 32 | LDFLAGS += -Wl,--gc-sections -flto 33 | 34 | CFLAGS += --warn-no-undef 35 | 36 | include $(TOP)/examples/build_system/make/rules.mk 37 | 38 | LD_FILE = $(FAMILY_PATH)/linker/gcc/$(MCU_VARIANT).ld -------------------------------------------------------------------------------- /Makefile.wifi: -------------------------------------------------------------------------------- 1 | TOP ?= $(TINYUSB_ROOT) 2 | BOARD ?= uno_r4 3 | 4 | # run 5 | # python tools/get_deps.py ra 6 | # once in the tinyusb root folder 7 | 8 | include $(TOP)/examples/build_system/make/make.mk 9 | 10 | INC += \ 11 | src \ 12 | $(TOP)/hw \ 13 | 14 | EXAMPLE_SOURCE += $(wildcard src/*.c) 15 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_flash_lp/r_flash_lp.c 16 | CFLAGS += -DFLASH_LP_CFG_CODE_FLASH_PROGRAMMING_ENABLE -DFLASH_LP_CFG_DATA_FLASH_PROGRAMMING_ENABLE 17 | CFLAGS += -DUSB_PID=0x0369 -DBOARD_NAME=\"UNO\ R4\ WiFi\" 18 | CFLAGS += -DLED_FADE_PWM_OUT_A=0 -DLED_FADE_PWM_OUT_B=1 -DLED_FADE_PWM_CHANNEL=2 -DLED_TIMER_SOURCE_DIV=2 19 | CFLAGS += -DLED_FADE_GPIO=BSP_IO_PORT_01_PIN_02 20 | CFLAGS += -DCODE_FLASH_DESCRIPTOR=\"@CodeFlash\ /0x00000000/8*2Ka,120*2Kg\" 21 | CFLAGS += -DBOSSA_LOADER=1 22 | CFLAGS += -DBOSSA_TX_PIN=BSP_IO_PORT_01_PIN_09 23 | CFLAGS += -DBOSSA_RX_PIN=BSP_IO_PORT_01_PIN_10 24 | CFLAGS += -DBOSSA_PERIPHERAL_FLAGS=IOPORT_PERIPHERAL_SCI1_3_5_7_9 25 | CFLAGS += -DOVERRIDE_VECTOR_TABLE=1 26 | CFLAGS += -DBOSSA_UART_CHANNEL=9 27 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_gpt/r_gpt.c 28 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_sci_uart/r_sci_uart.c 29 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_dtc/r_dtc.c 30 | 31 | SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) 32 | SRC_C += $(FSP_SOURCE) 33 | 34 | CFLAGS += -Os -flto 35 | CFLAGS += -ffunction-sections -fdata-sections 36 | LDFLAGS += -Wl,--gc-sections -flto 37 | 38 | CFLAGS += --warn-no-undef 39 | CFLAGS += -Wno-error=discarded-qualifiers -Wno-error=pointer-sign -Wno-error=null-dereference 40 | 41 | include $(TOP)/examples/build_system/make/rules.mk 42 | 43 | LD_FILE = $(FAMILY_PATH)/linker/gcc/$(MCU_VARIANT).ld 44 | -------------------------------------------------------------------------------- /Makefile.opta-analog: -------------------------------------------------------------------------------- 1 | TOP ?= $(TINYUSB_ROOT) 2 | BOARD ?= uno_r4 3 | 4 | # run 5 | # python tools/get_deps.py ra 6 | # once in the tinyusb root folder 7 | 8 | include $(TOP)/examples/build_system/make/make.mk 9 | 10 | INC += \ 11 | src \ 12 | $(TOP)/hw \ 13 | 14 | EXAMPLE_SOURCE += $(wildcard src/*.c) 15 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_flash_lp/r_flash_lp.c 16 | CFLAGS += -DFLASH_LP_CFG_CODE_FLASH_PROGRAMMING_ENABLE -DFLASH_LP_CFG_DATA_FLASH_PROGRAMMING_ENABLE 17 | CFLAGS += -DUSB_PID=0x0171 -DBOARD_NAME=\"OPTA\ ANALOG\ V2.0\" 18 | CFLAGS += -DLED_FADE_PWM_OUT_A=1 -DLED_FADE_PWM_OUT_B=0 -DLED_FADE_PWM_CHANNEL=6 -DLED_TIMER_SOURCE_DIV=2 19 | CFLAGS += -DLED_FADE_GPIO=BSP_IO_PORT_04_PIN_11 20 | CFLAGS += -DCODE_FLASH_DESCRIPTOR=\"@CodeFlash\ /0x00000000/8*2Ka,120*2Kg\" 21 | CFLAGS += -DBOSSA_LOADER=1 22 | CFLAGS += -DDFU_LOADER 23 | CFLAGS += -DOVERRIDE_VECTOR_TABLE=1 24 | CFLAGS += -DBOSSA_TX_PIN=BSP_IO_PORT_03_PIN_02 25 | CFLAGS += -DBOSSA_RX_PIN=BSP_IO_PORT_03_PIN_01 26 | CFLAGS += -DBOSSA_UART_CHANNEL=2 27 | CFLAGS += -DBOSSA_PERIPHERAL_FLAGS=IOPORT_PERIPHERAL_SCI0_2_4_6_8 28 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_gpt/r_gpt.c 29 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_sci_uart/r_sci_uart.c 30 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_dtc/r_dtc.c 31 | 32 | SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) 33 | SRC_C += $(FSP_SOURCE) 34 | 35 | CFLAGS += -Os -flto 36 | CFLAGS += -ffunction-sections -fdata-sections 37 | LDFLAGS += -Wl,--gc-sections -flto 38 | 39 | CFLAGS += --warn-no-undef 40 | # because compiled with BOSSA_LOADER some usb initializion are missing 41 | CFLAGS += -Wno-error=null-dereference 42 | 43 | include $(TOP)/examples/build_system/make/rules.mk 44 | 45 | LD_FILE = $(FAMILY_PATH)/linker/gcc/$(MCU_VARIANT).ld 46 | -------------------------------------------------------------------------------- /Makefile.opta-digital: -------------------------------------------------------------------------------- 1 | TOP ?= $(TINYUSB_ROOT) 2 | BOARD ?= uno_r4 3 | 4 | # run 5 | # python tools/get_deps.py ra 6 | # once in the tinyusb root folder 7 | 8 | include $(TOP)/examples/build_system/make/make.mk 9 | 10 | INC += \ 11 | src \ 12 | $(TOP)/hw \ 13 | 14 | EXAMPLE_SOURCE += $(wildcard src/*.c) 15 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_flash_lp/r_flash_lp.c 16 | CFLAGS += -DFLASH_LP_CFG_CODE_FLASH_PROGRAMMING_ENABLE -DFLASH_LP_CFG_DATA_FLASH_PROGRAMMING_ENABLE 17 | CFLAGS += -DUSB_PID=0x016e -DBOARD_NAME=\"OPTA\ DIGITAL\ v2.0\" 18 | CFLAGS += -DLED_FADE_PWM_OUT_A=1 -DLED_FADE_PWM_OUT_B=0 -DLED_FADE_PWM_CHANNEL=6 -DLED_TIMER_SOURCE_DIV=2 19 | CFLAGS += -DLED_FADE_GPIO=BSP_IO_PORT_04_PIN_11 20 | CFLAGS += -DCODE_FLASH_DESCRIPTOR=\"@CodeFlash\ /0x00000000/8*2Ka,120*2Kg\" 21 | CFLAGS += -DBOSSA_LOADER=1 22 | CFLAGS += -DDFU_LOADER 23 | CFLAGS += -DOVERRIDE_VECTOR_TABLE=1 24 | CFLAGS += -DBOSSA_TX_PIN=BSP_IO_PORT_03_PIN_02 25 | CFLAGS += -DBOSSA_RX_PIN=BSP_IO_PORT_03_PIN_01 26 | CFLAGS += -DBOSSA_UART_CHANNEL=2 27 | CFLAGS += -DBOSSA_PERIPHERAL_FLAGS=IOPORT_PERIPHERAL_SCI0_2_4_6_8 28 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_gpt/r_gpt.c 29 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_sci_uart/r_sci_uart.c 30 | FSP_SOURCE += $(TOP)/hw/mcu/renesas/fsp/ra/fsp/src/r_dtc/r_dtc.c 31 | 32 | SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) 33 | SRC_C += $(FSP_SOURCE) 34 | 35 | CFLAGS += -Os -flto 36 | CFLAGS += -ffunction-sections -fdata-sections 37 | LDFLAGS += -Wl,--gc-sections -flto 38 | 39 | CFLAGS += --warn-no-undef 40 | # because compiled with BOSSA_LOADER some usb initializion are missing 41 | CFLAGS += -Wno-error=null-dereference 42 | 43 | include $(TOP)/examples/build_system/make/rules.mk 44 | 45 | LD_FILE = $(FAMILY_PATH)/linker/gcc/$(MCU_VARIANT).ld 46 | -------------------------------------------------------------------------------- /0001-fix-arduino-bootloaders.patch: -------------------------------------------------------------------------------- 1 | diff --git a/hw/bsp/ra/boards/portenta_c33/board.h b/hw/bsp/ra/boards/portenta_c33/board.h 2 | index 7841ec8b8..8442afe5e 100644 3 | --- a/hw/bsp/ra/boards/portenta_c33/board.h 4 | +++ b/hw/bsp/ra/boards/portenta_c33/board.h 5 | @@ -32,7 +32,7 @@ extern "C" { 6 | #endif 7 | 8 | #define LED1 BSP_IO_PORT_01_PIN_07 // Red LED 9 | -#define LED_STATE_ON 1 10 | +#define LED_STATE_ON 0 11 | 12 | #define SW1 BSP_IO_PORT_04_PIN_08 // D12 13 | #define BUTTON_STATE_ACTIVE 0 14 | diff --git a/hw/bsp/ra/family.c b/hw/bsp/ra/family.c 15 | index 16332be17..07d6fd65d 100644 16 | --- a/hw/bsp/ra/family.c 17 | +++ b/hw/bsp/ra/family.c 18 | @@ -60,6 +60,7 @@ static ioport_instance_ctrl_t port_ctrl; 19 | // Vector Data 20 | //--------------------------------------------------------------------+ 21 | 22 | +#ifndef OVERRIDE_VECTOR_TABLE 23 | BSP_DONT_REMOVE BSP_PLACE_IN_SECTION(BSP_SECTION_APPLICATION_VECTORS) 24 | const fsp_vector_t g_vector_table[BSP_ICU_VECTOR_MAX_ENTRIES] = { 25 | [0] = usbfs_interrupt_handler, /* USBFS INT (USBFS interrupt) */ 26 | @@ -89,6 +90,7 @@ const bsp_interrupt_event_t g_interrupt_event_link_select[BSP_ICU_VECTOR_MAX_ENT 27 | [6] = BSP_PRV_IELS_ENUM(EVENT_USBHS_FIFO_1), /* USBHS FIFO 1 (DMA transfer request 1) */ 28 | #endif 29 | }; 30 | +#endif 31 | 32 | //--------------------------------------------------------------------+ 33 | // Board porting API 34 | diff --git a/src/portable/renesas/rusb2/dcd_rusb2.c b/src/portable/renesas/rusb2/dcd_rusb2.c 35 | index 24edc30e7..e3edde467 100644 36 | --- a/src/portable/renesas/rusb2/dcd_rusb2.c 37 | +++ b/src/portable/renesas/rusb2/dcd_rusb2.c 38 | @@ -452,6 +452,7 @@ static void process_status_completion(uint8_t rhport) 39 | 40 | static bool process_pipe0_xfer(rusb2_reg_t* rusb, int buffer_type, uint8_t ep_addr, void* buffer, uint16_t total_bytes) 41 | { 42 | + assert (rusb != NULL); 43 | /* configure fifo direction and access unit settings */ 44 | if ( ep_addr ) { 45 | /* IN, 2 bytes */ 46 | -------------------------------------------------------------------------------- /src/tusb_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * tusb_config.h 3 | * 4 | * Created on: May 5, 2021 5 | * Author: Jeremiah McCarthy 6 | */ 7 | 8 | #ifndef TUSB_CONFIG_H_ 9 | #define TUSB_CONFIG_H_ 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | //--------------------------------------------------------------------+ 16 | // Board Specific Configuration 17 | //--------------------------------------------------------------------+ 18 | 19 | #ifdef USE_HS_PORT_FOR_DFU 20 | 21 | // RHPort number used for device can be defined by board.mk, default to port 0 22 | #ifndef BOARD_TUD_RHPORT 23 | #define BOARD_TUD_RHPORT 1 24 | #endif 25 | 26 | #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) 27 | 28 | // RHPort max operational speed can defined by board.mk 29 | #ifndef BOARD_TUD_MAX_SPEED 30 | #define BOARD_TUD_MAX_SPEED OPT_MODE_HIGH_SPEED 31 | #endif 32 | 33 | #else 34 | 35 | // RHPort number used for device can be defined by board.mk, default to port 0 36 | #ifndef BOARD_TUD_RHPORT 37 | #define BOARD_TUD_RHPORT 0 38 | #endif 39 | 40 | #define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE 41 | 42 | // RHPort max operational speed can defined by board.mk 43 | #ifndef BOARD_TUD_MAX_SPEED 44 | #define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED 45 | #endif 46 | 47 | #endif 48 | 49 | //-------------------------------------------------------------------- 50 | // COMMON CONFIGURATION 51 | //-------------------------------------------------------------------- 52 | 53 | // defined by compiler flags for flexibility 54 | #ifndef CFG_TUSB_MCU 55 | #error CFG_TUSB_MCU must be defined 56 | #endif 57 | 58 | #ifndef CFG_TUSB_OS 59 | #define CFG_TUSB_OS OPT_OS_NONE 60 | #endif 61 | 62 | #ifndef CFG_TUSB_DEBUG 63 | #define CFG_TUSB_DEBUG 0 64 | #endif 65 | 66 | // Enable Device stack 67 | #define CFG_TUD_ENABLED 1 68 | 69 | // Default is max speed that hardware controller could support with on-chip PHY 70 | #define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED 71 | 72 | /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. 73 | * Tinyusb use follows macros to declare transferring memory so that they can be put 74 | * into those specific section. 75 | * e.g 76 | * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) 77 | * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) 78 | */ 79 | #ifndef CFG_TUSB_MEM_SECTION 80 | #define CFG_TUSB_MEM_SECTION 81 | #endif 82 | 83 | #ifndef CFG_TUSB_MEM_ALIGN 84 | #define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) 85 | #endif 86 | 87 | //-------------------------------------------------------------------- 88 | // DEVICE CONFIGURATION 89 | //-------------------------------------------------------------------- 90 | 91 | #ifndef CFG_TUD_ENDPOINT0_SIZE 92 | #define CFG_TUD_ENDPOINT0_SIZE 64 93 | #endif 94 | 95 | //------------- CLASS -------------// 96 | #define CFG_TUD_DFU 1 97 | 98 | // CDC FIFO size of TX and RX 99 | #define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 100 | #define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 101 | 102 | // CDC Endpoint transfer buffer size, more is faster 103 | #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 104 | 105 | // DFU buffer size, it has to be set to the buffer size used in TUD_DFU_DESCRIPTOR 106 | #define CFG_TUD_DFU_XFER_BUFSIZE 64 //(TUD_OPT_HIGH_SPEED ? 512 : 64) 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif /* TUSB_CONFIG_H_ */ 113 | -------------------------------------------------------------------------------- /src/r_flash_hp_cfg.h: -------------------------------------------------------------------------------- 1 | #ifndef FLASH_HP_EP_H_ 2 | #define FLASH_HP_EP_H_ 3 | /*******************************************************************************************************************//** 4 | * @ingroup FLASH_HP_EP 5 | * @{ 6 | **********************************************************************************************************************/ 7 | /* Code Flash */ 8 | #define FLASH_HP_CF_BLOCK_SIZE_32KB (32*1024) /* Block Size 32 KB */ 9 | #define FLASH_HP_CF_BLOCK_SIZE_8KB (8*1024) /* Block Size 8KB */ 10 | 11 | #define FLASH_HP_CF_BLCOK_0 0x00000000U /* 8 KB: 0x00000000 - 0x00001FFF */ 12 | #define FLASH_HP_CF_BLOCK_1 0x00002000U /* 8 KB: 0x00002000 - 0x00003FFF */ 13 | #define FLASH_HP_CF_BLOCK_2 0x00004000U /* 8 KB: 0x00004000 - 0x00005FFF */ 14 | #define FLASH_HP_CF_BLOCK_3 0x00006000U /* 8 KB: 0x00006000 - 0x00007FFF */ 15 | #define FLASH_HP_CF_BLOCK_4 0x00008000U /* 8 KB: 0x00008000 - 0x00009FFF */ 16 | #define FLASH_HP_CF_BLOCK_5 0x0000A000U /* 8 KB: 0x0000A000 - 0x0000BFFF */ 17 | #define FLASH_HP_CF_BLOCK_6 0x0000C000U /* 8 KB: 0x0000C000 - 0x0000DFFF */ 18 | #define FLASH_HP_CF_BLOCK_7 0x0000E000U /* 8 KB: 0x0000E000 - 0x0000FFFF */ 19 | #define FLASH_HP_CF_BLOCK_8 0x00010000U /* 32 KB: 0x00010000 - 0x00017FFF */ 20 | #define FLASH_HP_CF_BLOCK_9 0x00018000U /* 32 KB: 0x00018000 - 0x0001FFFF */ 21 | #define FLASH_HP_CF_BLCOK_10 0x00020000U /* 32 KB: 0x00020000 - 0x0004FFFF */ 22 | 23 | #define FLASH_HP_DF_BLOCK_SIZE (64) 24 | /* Data Flash */ 25 | #if (defined (BOARD_RA6M4_EK) || defined (BOARD_RA6M5_EK) || defined (BOARD_RA4M3_EK)||defined(BOARD_RA4M2_EK)) 26 | 27 | #define FLASH_HP_DF_BLOCK_0 0x08000000U /* 64 B: 0x40100000 - 0x4010003F */ 28 | #define FLASH_HP_DF_BLOCK_1 0x08000040U /* 64 B: 0x40100040 - 0x4010007F */ 29 | #define FLASH_HP_DF_BLOCK_2 0x08000080U /* 64 B: 0x40100080 - 0x401000BF */ 30 | #define FLASH_HP_DF_BLOCK_3 0x080000C0U /* 64 B: 0x401000C0 - 0x401000FF */ 31 | 32 | 33 | #else 34 | 35 | #define FLASH_HP_DF_BLOCK_0 0x40100000U /* 64 B: 0x40100000 - 0x4010003F */ 36 | #define FLASH_HP_DF_BLOCK_1 0x40100040U /* 64 B: 0x40100040 - 0x4010007F */ 37 | #define FLASH_HP_DF_BLOCK_2 0x40100080U /* 64 B: 0x40100080 - 0x401000BF */ 38 | #define FLASH_HP_DF_BLOCK_3 0x401000C0U /* 64 B: 0x401000C0 - 0x401000FF */ 39 | 40 | #endif 41 | 42 | #define BLOCK_SIZE (128) 43 | #define BLOCK_NUM (2) 44 | 45 | 46 | /* Switch Commands */ 47 | #define CODE_FLASH (1U) 48 | #define DATA_FLASH (2U) 49 | #define EXIT (3U) 50 | #define BUFF_SIZE 0x0F 51 | #define BUFF_INDEX 0x00 52 | 53 | /* Printing read data */ 54 | #define READ_DATA_PRINT(buff) for(uint8_t index = RESET_VALUE; BLOCK_SIZE > index; index++) \ 55 | { \ 56 | APP_PRINT("%x ",*(read_buffer+index)); \ 57 | if((index + 1) % 8 == 0) \ 58 | { APP_PRINT("\r\n"); } \ 59 | } 60 | /*flash_hp operating functions */ 61 | fsp_err_t flash_hp_code_flash_operations(void); 62 | fsp_err_t flash_hp_data_flash_operations(void); 63 | void flash_hp_deinit(void); 64 | void exit_flash_hp(void); 65 | /*******************************************************************************************************************//** 66 | * @} (end defgroup FLASH_HP_EP) 67 | **********************************************************************************************************************/ 68 | 69 | #endif /* FLASH_HP_EP_H_ */ -------------------------------------------------------------------------------- /src/usb_descriptors.c: -------------------------------------------------------------------------------- 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 "tusb.h" 27 | #include "class/dfu/dfu_device.h" 28 | #include "bsp_api.h" 29 | 30 | //--------------------------------------------------------------------+ 31 | // Device Descriptors 32 | //--------------------------------------------------------------------+ 33 | tusb_desc_device_t const desc_device = 34 | { 35 | .bLength = sizeof(tusb_desc_device_t), 36 | .bDescriptorType = TUSB_DESC_DEVICE, 37 | .bcdUSB = 0x0200, 38 | 39 | #if CFG_TUD_CDC 40 | // Use Interface Association Descriptor (IAD) for CDC 41 | // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) 42 | .bDeviceClass = TUSB_CLASS_MISC, 43 | .bDeviceSubClass = MISC_SUBCLASS_COMMON, 44 | .bDeviceProtocol = MISC_PROTOCOL_IAD, 45 | #else 46 | .bDeviceClass = 0x00, 47 | .bDeviceSubClass = 0x00, 48 | .bDeviceProtocol = 0x00, 49 | #endif 50 | 51 | .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, 52 | 53 | .idVendor = 0x2341, 54 | .idProduct = USB_PID, 55 | .bcdDevice = 0x0100, 56 | 57 | .iManufacturer = 0x01, 58 | .iProduct = 0x02, 59 | .iSerialNumber = 0x03, 60 | 61 | .bNumConfigurations = 0x01 62 | }; 63 | 64 | // Invoked when received GET DEVICE DESCRIPTOR 65 | // Application return pointer to descriptor 66 | uint8_t const * tud_descriptor_device_cb(void) 67 | { 68 | return (uint8_t const *) &desc_device; 69 | } 70 | 71 | //--------------------------------------------------------------------+ 72 | // Configuration Descriptor 73 | //--------------------------------------------------------------------+ 74 | 75 | // Number of Alternate Interface (each for 1 flash partition) 76 | #define ALT_COUNT 2 77 | 78 | enum 79 | { 80 | ITF_NUM_DFU_MODE, 81 | ITF_NUM_TOTAL 82 | }; 83 | 84 | #define EPNUM_CDC_NOTIF 0x81 85 | #define EPNUM_CDC_OUT 0x02 86 | #define EPNUM_CDC_IN 0x82 87 | 88 | #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_DFU_DESC_LEN(ALT_COUNT)) 89 | 90 | #define FUNC_ATTRS (DFU_ATTR_CAN_UPLOAD | DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_MANIFESTATION_TOLERANT) 91 | 92 | uint8_t const desc_configuration[] = 93 | { 94 | // Config number, interface count, string index, total length, attribute, power in mA 95 | TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), 96 | 97 | // Interface number, Alternate count, starting string index, attributes, detach timeout, transfer size 98 | TUD_DFU_DESCRIPTOR(ITF_NUM_DFU_MODE, ALT_COUNT, 4, FUNC_ATTRS, 1000, CFG_TUD_DFU_XFER_BUFSIZE), 99 | }; 100 | 101 | // Invoked when received GET CONFIGURATION DESCRIPTOR 102 | // Application return pointer to descriptor 103 | // Descriptor contents must exist long enough for transfer to complete 104 | uint8_t const * tud_descriptor_configuration_cb(uint8_t index) 105 | { 106 | (void) index; // for multiple configurations 107 | return desc_configuration; 108 | } 109 | 110 | //--------------------------------------------------------------------+ 111 | // String Descriptors 112 | //--------------------------------------------------------------------+ 113 | 114 | static void utox8(uint32_t val, char* s) { 115 | for (int i = 0; i < 8; i++) { 116 | int d = val & 0XF; 117 | val = (val >> 4); 118 | 119 | s[7 - i] = d > 9 ? 'A' + d - 10 : '0' + d; 120 | } 121 | } 122 | 123 | static char idString[32 * 2 + 1]; 124 | 125 | // array of pointer to string descriptors 126 | char const* string_desc_arr [] = 127 | { 128 | (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) 129 | "Arduino", // 1: Manufacturer 130 | BOARD_NAME" DFU", // 2: Product 131 | idString, // 3: Serials, should use chip ID 132 | CODE_FLASH_DESCRIPTOR, // 4: DFU Partition 1 133 | "@DataFlash /0x08000000/8*1Kg", // 5: DFU Partition 2 134 | }; 135 | 136 | static uint16_t _desc_str[128]; 137 | 138 | // Invoked when received GET STRING DESCRIPTOR request 139 | // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete 140 | uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) 141 | { 142 | (void) langid; 143 | 144 | size_t chr_count; 145 | 146 | if ( index == 0) 147 | { 148 | memcpy(&_desc_str[1], string_desc_arr[0], 2); 149 | chr_count = 1; 150 | } 151 | else 152 | { 153 | // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. 154 | // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors 155 | 156 | if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; 157 | 158 | const char* str = string_desc_arr[index]; 159 | 160 | if (!idString[0]) { 161 | const bsp_unique_id_t* t = R_BSP_UniqueIdGet(); 162 | utox8(t->unique_id_words[0], &idString[0]); 163 | utox8(t->unique_id_words[1], &idString[8]); 164 | utox8(t->unique_id_words[2], &idString[16]); 165 | utox8(t->unique_id_words[3], &idString[24]); 166 | } 167 | 168 | // Cap at max char 169 | chr_count = (uint8_t) strlen(str); 170 | 171 | // Convert ASCII string into UTF-16 172 | for(uint8_t i=0; ievent == I2C_MASTER_EVENT_TX_COMPLETE) { 161 | status = true; 162 | } 163 | } 164 | 165 | static iic_master_instance_ctrl_t p_api_ctrl; 166 | static sci_i2c_extended_cfg_t m_sci_i2c_extend = { 167 | .clock_settings = { 168 | .clk_divisor_value = 0, 169 | .brr_value = 14, 170 | .mddr_value = 255, 171 | .bitrate_modulation = false, 172 | .cycles_value = 15, 173 | .snfr_value = (1), 174 | } 175 | }; 176 | static i2c_master_cfg_t m_i2c_cfg = { 177 | .p_extend = &m_sci_i2c_extend, 178 | .p_callback = sci_i2c_callback, 179 | .channel = 3, 180 | .rate = I2C_MASTER_RATE_STANDARD, 181 | .slave = 0, 182 | .addr_mode = I2C_MASTER_ADDR_MODE_7BIT, 183 | .txi_irq = 9, 184 | .tei_irq = 10, 185 | .ipl = (12), 186 | .p_context = &m_i2c_cfg, 187 | }; 188 | 189 | void i2c_begin() { 190 | R_SCI_I2C_Open(&p_api_ctrl, &m_i2c_cfg); 191 | } 192 | 193 | void i2c_write(uint8_t address, uint8_t reg, uint8_t value) { 194 | R_SCI_I2C_SlaveAddressSet(&p_api_ctrl, address, m_i2c_cfg.addr_mode); 195 | uint8_t data[2] = { reg, value }; 196 | R_SCI_I2C_Write(&p_api_ctrl, data, 2, false); 197 | R_BSP_SoftwareDelay(5, BSP_DELAY_UNITS_MILLISECONDS); 198 | } 199 | #else // BSP_FEATURE_FLASH_HP_VERSION 200 | 201 | /* UNO R4 MINIMA */ 202 | //void usbfs_interrupt_handler(void); 203 | //void usbfs_resume_handler(void); 204 | //void usbfs_d0fifo_handler(void); 205 | //void usbfs_d1fifo_handler(void); 206 | //void usbhs_interrupt_handler(void); 207 | //void usbhs_d0fifo_handler(void); 208 | //void usbhs_d1fifo_handler(void); 209 | 210 | 211 | /* interrupt table */ 212 | 213 | BSP_DONT_REMOVE BSP_PLACE_IN_SECTION(BSP_SECTION_APPLICATION_VECTORS) 214 | const fsp_vector_t g_vector_table[BSP_ICU_VECTOR_MAX_ENTRIES] = { 215 | [0] = usbfs_interrupt_handler, /* USBFS INT (USBFS interrupt) */ 216 | [1] = usbfs_resume_handler, /* USBFS RESUME (USBFS resume interrupt) */ 217 | #ifndef RENESAS_CORTEX_M23 218 | [2] = usbfs_d0fifo_handler, /* USBFS FIFO 0 (DMA transfer request 0) */ 219 | [3] = usbfs_d1fifo_handler, /* USBFS FIFO 1 (DMA transfer request 1) */ 220 | #endif 221 | #ifdef BOARD_HAS_USB_HIGHSPEED 222 | [4] = usbhs_interrupt_handler, /* USBHS INT (USBHS interrupt) */ 223 | [5] = usbhs_d0fifo_handler, /* USBHS FIFO 0 (DMA transfer request 0) */ 224 | [6] = usbhs_d1fifo_handler, /* USBHS FIFO 1 (DMA transfer request 1) */ 225 | #endif 226 | }; 227 | 228 | const bsp_interrupt_event_t g_interrupt_event_link_select[BSP_ICU_VECTOR_MAX_ENTRIES] = { 229 | [0] = BSP_PRV_IELS_ENUM(EVENT_USBFS_INT), /* USBFS INT (USBFS interrupt) */ 230 | [1] = BSP_PRV_IELS_ENUM(EVENT_USBFS_RESUME), /* USBFS RESUME (USBFS resume interrupt) */ 231 | #ifndef RENESAS_CORTEX_M23 232 | [2] = BSP_PRV_IELS_ENUM(EVENT_USBFS_FIFO_0), /* USBFS FIFO 0 (DMA transfer request 0) */ 233 | [3] = BSP_PRV_IELS_ENUM(EVENT_USBFS_FIFO_1), /* USBFS FIFO 1 (DMA transfer request 1) */ 234 | #endif 235 | #ifdef BOARD_HAS_USB_HIGHSPEED 236 | [4] = BSP_PRV_IELS_ENUM(EVENT_USBHS_USB_INT_RESUME), /* USBHS USB INT RESUME (USBHS interrupt) */ 237 | [5] = BSP_PRV_IELS_ENUM(EVENT_USBHS_FIFO_0), /* USBHS FIFO 0 (DMA transfer request 0) */ 238 | [6] = BSP_PRV_IELS_ENUM(EVENT_USBHS_FIFO_1), /* USBHS FIFO 1 (DMA transfer request 1) */ 239 | #endif 240 | }; 241 | 242 | 243 | #endif // BSP_FEATURE_FLASH_HP_VERSION 244 | 245 | #endif 246 | 247 | -------------------------------------------------------------------------------- /src/bossa.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * Copyright (c) 2023 Arduino SA 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | /* This file is used by UNO R4 WIFI (only uses bossa as bootloader) and 33 | OPTA Expansions (both ANALOG and DIGITAL) 34 | VERY IMPORTANT NOTE: 35 | - UNO R4 WIFI uses 203400 as baudrate 36 | - Expansions use 115200 as baudrate 37 | The reason is that Opta controller (the programmer) does not correctly 38 | open the UART at 203400. 39 | THOSE VALUES are HARDCODED at the end of this file */ 40 | 41 | 42 | #if BOSSA_LOADER 43 | 44 | #include "bsp_api.h" 45 | #include "r_ioport.h" 46 | #include "flash.h" 47 | 48 | /* Instance structure to use this module. */ 49 | extern const flash_instance_t g_flash; 50 | 51 | #include "r_sci_uart.h" 52 | 53 | sci_uart_instance_ctrl_t g_uart_ctrl; 54 | 55 | baud_setting_t uart_baud = { 56 | .semr_baudrate_bits_b.abcse = 0, 57 | .semr_baudrate_bits_b.abcs = 0, 58 | .semr_baudrate_bits_b.bgdm = 1, 59 | .cks = 0, 60 | .brr = 25, 61 | .mddr = (uint8_t) 256, 62 | .semr_baudrate_bits_b.brme = false, 63 | }; 64 | 65 | const sci_uart_extended_cfg_t uart_cfg_extend = { 66 | .clock = SCI_UART_CLOCK_INT, 67 | .rx_edge_start = SCI_UART_START_BIT_FALLING_EDGE, 68 | .noise_cancel = SCI_UART_NOISE_CANCELLATION_DISABLE, 69 | .rx_fifo_trigger = SCI_UART_RX_FIFO_TRIGGER_1, 70 | .p_baud_setting = &uart_baud, 71 | .flow_control = SCI_UART_FLOW_CONTROL_RTS, 72 | .flow_control_pin = (bsp_io_port_pin_t) UINT16_MAX, 73 | .rs485_setting.enable = SCI_UART_RS485_DISABLE, 74 | .rs485_setting.polarity = SCI_UART_RS485_DE_POLARITY_HIGH, 75 | .rs485_setting.de_control_pin = (bsp_io_port_pin_t) UINT16_MAX, 76 | }; 77 | 78 | volatile uint8_t command_buffer[64]; 79 | volatile uint32_t command_buffer_index = 0; 80 | volatile bool rx_complete = false; 81 | volatile uint8_t tx_debug = 12; 82 | volatile bool rx_data = false; 83 | volatile uint32_t data_index = 0; 84 | volatile uint32_t data_received = 0; 85 | volatile uint32_t data_expected = 0; 86 | volatile uint8_t data_buffer[8192]; 87 | uint8_t flash_buffer[4096]; 88 | uint32_t copyOffset = 0; 89 | 90 | void user_uart_callback(uart_callback_args_t *p_args) { 91 | if(UART_EVENT_RX_CHAR == p_args->event) { 92 | if(rx_data) { 93 | if(data_index < 8192) { 94 | data_buffer[data_index++] = p_args->data; 95 | data_received++; 96 | if(data_received >= data_expected) { 97 | rx_data = false; 98 | rx_complete = true; 99 | } 100 | } 101 | } 102 | else { 103 | if (command_buffer_index < sizeof(command_buffer)) { 104 | command_buffer[command_buffer_index++] = p_args->data; 105 | } 106 | } 107 | } 108 | } 109 | 110 | #include "r_dtc.h" 111 | 112 | dtc_instance_ctrl_t g_transfer21_ctrl; 113 | 114 | transfer_info_t g_transfer21_info = 115 | { .transfer_settings_word_b.dest_addr_mode = TRANSFER_ADDR_MODE_INCREMENTED, 116 | .transfer_settings_word_b.repeat_area = TRANSFER_REPEAT_AREA_DESTINATION, 117 | .transfer_settings_word_b.irq = TRANSFER_IRQ_END, 118 | .transfer_settings_word_b.chain_mode = TRANSFER_CHAIN_MODE_DISABLED, 119 | .transfer_settings_word_b.src_addr_mode = TRANSFER_ADDR_MODE_FIXED, 120 | .transfer_settings_word_b.size = TRANSFER_SIZE_1_BYTE, 121 | .transfer_settings_word_b.mode = TRANSFER_MODE_NORMAL, 122 | .p_dest = (void*) NULL, 123 | .p_src = (void const*) NULL, 124 | .num_blocks = 0, 125 | .length = 0, }; 126 | 127 | const dtc_extended_cfg_t g_transfer21_cfg_extend = 128 | { .activation_source = 2, }; 129 | const transfer_cfg_t g_transfer21_cfg = 130 | { .p_info = &g_transfer21_info, .p_extend = &g_transfer21_cfg_extend, }; 131 | 132 | /* Instance structure to use this module. */ 133 | const transfer_instance_t g_transfer21 = 134 | { .p_ctrl = &g_transfer21_ctrl, .p_cfg = &g_transfer21_cfg, .p_api = &g_transfer_on_dtc }; 135 | 136 | const uart_cfg_t g_uart_cfg = { 137 | .channel = BOSSA_UART_CHANNEL, 138 | .p_context = NULL, 139 | .p_extend = &uart_cfg_extend, 140 | .p_transfer_tx = NULL, 141 | .p_transfer_rx = &g_transfer21, 142 | 143 | .data_bits = UART_DATA_BITS_8, 144 | .parity = UART_PARITY_OFF, 145 | .stop_bits = UART_STOP_BITS_1, 146 | .p_callback = user_uart_callback, 147 | #if defined BOSSA_LOADER 148 | #if defined DFU_LOADER 149 | .rxi_ipl = (2), 150 | .txi_ipl = (2), 151 | .tei_ipl = (2), 152 | .eri_ipl = (2), 153 | .txi_irq = 7, 154 | .tei_irq = 8, 155 | .rxi_irq = 9, 156 | .eri_irq = 10, 157 | #else 158 | .rxi_ipl = (2), 159 | .txi_ipl = (2), 160 | .tei_ipl = (2), 161 | .eri_ipl = (2), 162 | .txi_irq = 0, 163 | .tei_irq = 1, 164 | .rxi_irq = 2, 165 | .eri_irq = 3, 166 | #endif 167 | 168 | 169 | #endif 170 | }; 171 | 172 | static const uint16_t crc16Table[256]= 173 | { 174 | 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, 175 | 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, 176 | 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, 177 | 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, 178 | 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, 179 | 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, 180 | 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, 181 | 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, 182 | 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, 183 | 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, 184 | 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, 185 | 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, 186 | 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, 187 | 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, 188 | 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, 189 | 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, 190 | 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, 191 | 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, 192 | 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, 193 | 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, 194 | 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, 195 | 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, 196 | 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, 197 | 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, 198 | 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, 199 | 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, 200 | 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, 201 | 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, 202 | 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, 203 | 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, 204 | 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, 205 | 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 206 | }; 207 | 208 | unsigned short serial_add_crc(char ptr, unsigned short crc) 209 | { 210 | return (crc << 8) ^ crc16Table[((crc >> 8) ^ ptr) & 0xff]; 211 | } 212 | 213 | void pulse_led(); 214 | 215 | void bossa_task() { 216 | 217 | if (command_buffer_index > 0) { 218 | 219 | // printf("%c", b); 220 | 221 | if (command_buffer[command_buffer_index-1] == '#') { 222 | command_buffer[command_buffer_index] = '\0'; 223 | char* str; 224 | 225 | switch (command_buffer[0]) { 226 | case 'N': 227 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)"\n\r", 2); 228 | break; 229 | 230 | case 'V': 231 | str = (char *)"Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]\n\r"; 232 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)str, strlen(str)); 233 | break; 234 | 235 | case 'I': 236 | str = (char *)"nRF52840-QIAA\n\r"; 237 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)str, strlen(str)); 238 | break; 239 | 240 | case 'X': 241 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)"X\n\r", 3); 242 | break; 243 | 244 | case 'S': { 245 | uint32_t address = 0; 246 | uint32_t length = 0; 247 | 248 | data_received = 0; 249 | rx_data = true; 250 | data_index = strtoul((const char*)&command_buffer[1], NULL, 16); 251 | data_expected = strtoul((const char*)&command_buffer[10], NULL, 16); 252 | rx_complete = false; 253 | 254 | while (rx_complete == false) { 255 | } 256 | 257 | break; 258 | } 259 | 260 | case 'Y': { 261 | uint32_t arg1 = 0; 262 | uint32_t arg2 = 0; 263 | 264 | arg1 = strtoul((const char*)&command_buffer[1], NULL, 16); 265 | arg2 = strtoul((const char*)&command_buffer[10], NULL, 16); 266 | 267 | if (arg2 != 0) { 268 | __disable_irq(); 269 | if ((SKETCH_FLASH_OFFSET + arg1) % PROGRAM_BLOCK_SIZE == 0) { 270 | R_FLASH_LP_Erase(g_flash.p_ctrl, SKETCH_FLASH_OFFSET + arg1, 2); 271 | } 272 | R_FLASH_LP_Write(g_flash.p_ctrl, (uint32_t)&data_buffer[copyOffset], SKETCH_FLASH_OFFSET + arg1, arg2); 273 | __enable_irq(); 274 | } else { 275 | copyOffset = arg1; 276 | } 277 | 278 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)"Y\n\r", 3); 279 | 280 | break; 281 | } 282 | 283 | case 'Q': { 284 | for (size_t i = 0; i < 8192; i++) { 285 | uint8_t* ptr = (uint8_t*)(SKETCH_FLASH_OFFSET + i); 286 | char result[4]; 287 | result[2] = '\r'; 288 | result[3] = '\n'; 289 | itoa(*ptr, result, 16); 290 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)result, 4); 291 | R_BSP_SoftwareDelay(5, BSP_DELAY_UNITS_MILLISECONDS); 292 | } 293 | break; 294 | } 295 | 296 | case 'Z': { 297 | uint32_t address = 0; 298 | uint32_t size = 0; 299 | uint16_t crc = 0; 300 | uint8_t result[1 + 8 + 3 + 1]; 301 | 302 | address = strtoul((const char*)&command_buffer[1], NULL, 16); 303 | size = strtoul((const char*)&command_buffer[10], NULL, 16); 304 | 305 | memcpy(flash_buffer, (void*)(SKETCH_FLASH_OFFSET + address), size); 306 | 307 | for (uint32_t i = 0; i < size; i++) { 308 | crc = serial_add_crc(flash_buffer[i], crc); 309 | } 310 | 311 | sprintf((char*)result, "Z%08X#\n\r", crc); 312 | 313 | R_SCI_UART_Write(&g_uart_ctrl, (uint8_t *)result, sizeof(result) - 1); 314 | 315 | break; 316 | } 317 | 318 | case 'K': { 319 | #ifdef CLEAN_UP_BOSSA 320 | R_SCI_UART_Close(&g_uart_ctrl); 321 | #if defined DFU_LOADER 322 | R_BSP_IrqDisable((IRQn_Type)7); 323 | R_BSP_IrqDisable((IRQn_Type)8); 324 | R_BSP_IrqDisable((IRQn_Type)9); 325 | R_BSP_IrqDisable((IRQn_Type)10); 326 | #else 327 | R_BSP_IrqDisable((IRQn_Type)0); 328 | R_BSP_IrqDisable((IRQn_Type)1); 329 | R_BSP_IrqDisable((IRQn_Type)2); 330 | R_BSP_IrqDisable((IRQn_Type)3); 331 | #endif 332 | #endif 333 | 334 | NVIC_SystemReset(); 335 | break; 336 | } 337 | } 338 | 339 | command_buffer_index = 0; 340 | } 341 | } 342 | } 343 | 344 | extern ioport_instance_ctrl_t port_ctrl; 345 | 346 | void restore_usb_switch() { 347 | if (R_SYSTEM->VBTBKR[1] == 40) { 348 | R_IOPORT_PinCfg(&port_ctrl, BSP_IO_PORT_04_PIN_08, IOPORT_CFG_PORT_DIRECTION_OUTPUT); 349 | R_IOPORT_PinWrite(&port_ctrl, BSP_IO_PORT_04_PIN_08, BSP_IO_LEVEL_HIGH); 350 | R_BSP_SoftwareDelay((uint32_t) 2, BSP_DELAY_UNITS_MILLISECONDS); 351 | R_IOPORT_PinWrite(&port_ctrl, BSP_IO_PORT_04_PIN_08, BSP_IO_LEVEL_LOW); 352 | R_IOPORT_PinCfg(&port_ctrl, BSP_IO_PORT_04_PIN_08, IOPORT_CFG_PORT_DIRECTION_INPUT); 353 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK; 354 | R_SYSTEM->VBTBKR[1] = 0; 355 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK; 356 | } 357 | } 358 | 359 | void led_blinking_task(); 360 | 361 | #ifdef DFU_LOADER 362 | #include "bsp/board_api.h" 363 | #include "tusb.h" 364 | 365 | /* 366 | * OPTA Expansions 367 | */ 368 | 369 | void run_bootloader() { 370 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK; 371 | R_SYSTEM->VBTBKR[1] = 0; 372 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK; 373 | tud_init(BOARD_TUD_RHPORT); 374 | if (board_init_after_tusb) { 375 | board_init_after_tusb(); 376 | } 377 | R_SCI_UART_BaudCalculate(115200, true, 5000, &uart_baud); 378 | R_SCI_UART_Open(&g_uart_ctrl, &g_uart_cfg); 379 | R_SCI_UART_BaudSet(&g_uart_ctrl, (void *) &uart_baud); 380 | 381 | while (1) { 382 | tud_task(); // tinyusb device task 383 | bossa_task(); 384 | led_blinking_task(); 385 | } 386 | } 387 | 388 | #else 389 | 390 | /* 391 | * UNO R4 WIFI 392 | */ 393 | 394 | void run_bootloader() { 395 | restore_usb_switch(); 396 | R_SCI_UART_BaudCalculate(230400, true, 5000, &uart_baud); 397 | R_SCI_UART_Open(&g_uart_ctrl, &g_uart_cfg); 398 | R_SCI_UART_BaudSet(&g_uart_ctrl, (void *) &uart_baud); 399 | 400 | while (1) { 401 | bossa_task(); 402 | led_blinking_task(); 403 | } 404 | } 405 | 406 | #endif 407 | 408 | #endif -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * Copyright (c) 2023 Arduino SA 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "bsp/board_api.h" 32 | #include "tusb.h" 33 | #include "bsp_api.h" 34 | #include "r_gpt.h" 35 | 36 | #include "flash.h" 37 | 38 | uint8_t aucbuffer[PROGRAM_BLOCK_SIZE]; 39 | uint16_t auclen = 0; 40 | uint16_t auc_block_num = 0; 41 | 42 | void bgo_callback(flash_callback_args_t *p_args) 43 | { 44 | if (p_args->event == FLASH_EVENT_WRITE_COMPLETE && auclen > 0) { 45 | tud_dfu_finish_flashing(DFU_STATUS_OK); 46 | auclen = 0; 47 | } 48 | } 49 | 50 | void flash_ready_interrupt_handler(); 51 | void flash_error_interrupt_handler(); 52 | 53 | flash_instance_ctrl_t g_flash_ctrl; 54 | const flash_cfg_t g_flash_cfg = 55 | { .data_flash_bgo = false, .p_callback = bgo_callback, .p_context = NULL, 56 | .irq = 7, 57 | .err_irq = 8, 58 | .err_ipl = (2), 59 | .ipl = (2), 60 | }; 61 | 62 | /* Instance structure to use this module. */ 63 | const flash_instance_t g_flash ={ .p_ctrl = &g_flash_ctrl, .p_cfg = &g_flash_cfg, .p_api = &flash_apis }; 64 | 65 | //--------------------------------------------------------------------+ 66 | // MACRO CONSTANT TYPEDEF PROTYPES 67 | //--------------------------------------------------------------------+ 68 | 69 | /* Blink pattern 70 | * - 250 ms : device not mounted 71 | * - 1000 ms : device mounted 72 | * - 2500 ms : device is suspended 73 | */ 74 | enum { 75 | BLINK_NOT_MOUNTED = 250, 76 | BLINK_MOUNTED = 1000, 77 | BLINK_SUSPENDED = 2500, 78 | }; 79 | 80 | static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; 81 | 82 | void led_blinking_task(void); 83 | void set_double_tap_data(uint32_t data); 84 | 85 | void boot5(uint32_t address) { 86 | 87 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK; 88 | R_BSP_MODULE_STOP(FSP_IP_USBFS, 0); 89 | R_BSP_MODULE_STOP(FSP_IP_USBHS, 0); 90 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK; 91 | 92 | /* Disable MSP monitoring. */ 93 | #if BSP_FEATURE_TZ_HAS_TRUSTZONE 94 | __set_MSPLIM(0); 95 | #else 96 | R_MPU_SPMON->SP[0].CTL = 0; 97 | #endif 98 | 99 | __disable_irq(); // Note: remember to enable IRQ in application 100 | __DSB(); 101 | __ISB(); 102 | 103 | // Disable SysTick 104 | SysTick->CTRL = 0; 105 | 106 | SCB->VTOR = address; 107 | 108 | uint32_t mainStackPointer = *(volatile uint32_t *)(address); 109 | __set_MSP(mainStackPointer); 110 | uint32_t programResetHandlerAddress = *(volatile uint32_t *) (address + 4); 111 | void (* programResetHandler)(void) = (void (*)(void)) programResetHandlerAddress; 112 | programResetHandler(); 113 | } 114 | 115 | #if defined(RENESAS_CORTEX_M23) 116 | #define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *)0x20007FF0)) 117 | #else 118 | #define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *) &R_SYSTEM->VBTBKR[0])) 119 | #endif 120 | #define DOUBLE_TAP_MAGIC 0x07738135 121 | 122 | #define USEC_PER_SEC (1000000U) 123 | #define PERIOD (USEC_PER_SEC / 50U) 124 | #define FADESTEP 2000 125 | 126 | const gpt_extended_cfg_t g_timer_extend = { 127 | .gtioca = { .output_enabled = LED_FADE_PWM_OUT_A ? true : false, .stop_level = GPT_PIN_LEVEL_LOW }, 128 | .gtiocb = { .output_enabled = LED_FADE_PWM_OUT_B ? true : false, .stop_level = GPT_PIN_LEVEL_LOW }, 129 | }; 130 | 131 | 132 | const timer_cfg_t pwm_cfg = { 133 | .mode = TIMER_MODE_PWM, 134 | .period_counts = (uint32_t) 0x9896, .duty_cycle_counts = 0x4c4b, 135 | .source_div = (timer_source_div_t) LED_TIMER_SOURCE_DIV, .channel = LED_FADE_PWM_CHANNEL, .p_callback = NULL, 136 | .p_context = NULL, .p_extend = &g_timer_extend, .cycle_end_ipl = (BSP_IRQ_DISABLED), 137 | }; 138 | 139 | 140 | static gpt_instance_ctrl_t pwm_ctrl; 141 | #include "r_ioport.h" 142 | 143 | static const ioport_pin_cfg_t extra_pin_cfg[] = { 144 | #ifdef BOSSA_LOADER 145 | { .pin = BOSSA_TX_PIN, .pin_cfg = (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_PULLUP_ENABLE | BOSSA_PERIPHERAL_FLAGS) }, 146 | { .pin = BOSSA_RX_PIN, .pin_cfg = (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_PULLUP_ENABLE | BOSSA_PERIPHERAL_FLAGS) }, 147 | #endif 148 | { .pin = LED_FADE_GPIO, .pin_cfg = IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_GPT1 } 149 | }; 150 | 151 | static const ioport_cfg_t pin_cfg = { 152 | .number_of_pins = sizeof(extra_pin_cfg) / sizeof(ioport_pin_cfg_t), 153 | .p_pin_cfg_data = extra_pin_cfg, 154 | }; 155 | ioport_instance_ctrl_t port_ctrl; 156 | 157 | int pwm_channel = LED_FADE_PWM_OUT_A ? GPT_IO_PIN_GTIOCA : GPT_IO_PIN_GTIOCB; 158 | 159 | /* *** 160 | * UNO R4 MINIMA AND C33 BOOTLOADER ** ONLY DFU ** 161 | * *** */ 162 | __WEAK void run_bootloader() { 163 | // init device stack on configured roothub port 164 | tud_init(BOARD_TUD_RHPORT); 165 | if (board_init_after_tusb) { 166 | board_init_after_tusb(); 167 | } 168 | 169 | while (1) 170 | { 171 | tud_task(); // tinyusb device task 172 | led_blinking_task(); 173 | } 174 | } 175 | 176 | /*------------- MAIN -------------*/ 177 | int main(void) 178 | { 179 | board_init(); 180 | 181 | // set magic for double tap 182 | if (BOOT_DOUBLE_TAP_DATA == DOUBLE_TAP_MAGIC) { 183 | set_double_tap_data(0); 184 | goto bootloader; 185 | } 186 | 187 | if (!R_SYSTEM->RSTSR0_b.PORF) { 188 | set_double_tap_data(DOUBLE_TAP_MAGIC); 189 | } 190 | #if !defined(RENESAS_CORTEX_M23) /* no double tap support in Muxto */ 191 | #ifdef TURN_OFF_CHARGER_LED 192 | i2c_begin(); 193 | i2c_write(0x8, 0x9C, (1 << 7)); 194 | i2c_write(0x8, 0x9E, (1 << 5)); 195 | #endif 196 | while (board_millis() < 500) { 197 | } 198 | #endif 199 | set_double_tap_data(0); 200 | 201 | int app_valid = (((*(uint32_t *) SKETCH_FLASH_OFFSET) & 0xFF000000) == 0x20000000); 202 | 203 | if (app_valid) { 204 | boot5(SKETCH_FLASH_OFFSET); 205 | } 206 | 207 | bootloader: 208 | 209 | R_IOPORT_Open(&port_ctrl, &pin_cfg); 210 | 211 | R_GPT_Open(&pwm_ctrl, &pwm_cfg); 212 | R_GPT_PeriodSet(&pwm_ctrl, PERIOD); 213 | R_GPT_DutyCycleSet(&pwm_ctrl, 0, pwm_channel); 214 | R_GPT_Start(&pwm_ctrl); 215 | 216 | #if BSP_FEATURE_FLASH_HP_VERSION 217 | R_FLASH_HP_Open(g_flash.p_ctrl, g_flash.p_cfg); 218 | R_FLASH_HP_Reset(g_flash.p_ctrl); 219 | R_FLASH_HP_StartUpAreaSelect(g_flash.p_ctrl, FLASH_STARTUP_AREA_BLOCK0, true); 220 | #endif 221 | 222 | #if BSP_FEATURE_FLASH_LP_VERSION 223 | R_FLASH_LP_Open(g_flash.p_ctrl, g_flash.p_cfg); 224 | R_FLASH_LP_Reset(g_flash.p_ctrl); 225 | #endif 226 | 227 | run_bootloader(); 228 | 229 | return 0; 230 | } 231 | 232 | 233 | #ifdef DFU_LOADER 234 | 235 | /* !!! 236 | * 237 | * UNO R4 MINIMA / C33 / OPTA ANALOG / OPTA DIGITAL -> define this so that they have DFU callbacks 238 | * 239 | * UNO WIFI does ** NOT ** define this so it avoids DFU callback 240 | * 241 | * !!! */ 242 | 243 | 244 | //--------------------------------------------------------------------+ 245 | // Device callbacks 246 | //--------------------------------------------------------------------+ 247 | 248 | // Invoked when device is mounted 249 | void tud_mount_cb(void) 250 | { 251 | blink_interval_ms = BLINK_MOUNTED; 252 | } 253 | 254 | // Invoked when device is unmounted 255 | void tud_umount_cb(void) 256 | { 257 | blink_interval_ms = BLINK_NOT_MOUNTED; 258 | } 259 | 260 | // Invoked when usb bus is suspended 261 | // remote_wakeup_en : if host allow us to perform remote wakeup 262 | // Within 7ms, device must draw an average of current less than 2.5 mA from bus 263 | void tud_suspend_cb(bool remote_wakeup_en) 264 | { 265 | (void) remote_wakeup_en; 266 | blink_interval_ms = BLINK_SUSPENDED; 267 | } 268 | 269 | // Invoked when usb bus is resumed 270 | void tud_resume_cb(void) 271 | { 272 | blink_interval_ms = BLINK_MOUNTED; 273 | } 274 | 275 | //--------------------------------------------------------------------+ 276 | // DFU callbacks 277 | // Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc. 278 | //--------------------------------------------------------------------+ 279 | 280 | // Invoked right before tud_dfu_download_cb() (state=DFU_DNBUSY) or tud_dfu_manifest_cb() (state=DFU_MANIFEST) 281 | // Application return timeout in milliseconds (bwPollTimeout) for the next download/manifest operation. 282 | // During this period, USB host won't try to communicate with us. 283 | uint32_t tud_dfu_get_timeout_cb(uint8_t alt, uint8_t state) 284 | { 285 | if ( state == DFU_DNBUSY ) 286 | { 287 | // For this example 288 | // - Atl0 Flash is fast : 1 ms 289 | // - Alt1 EEPROM is slow: 100 ms 290 | return (alt == 0) ? 0 : 100; 291 | } 292 | else if (state == DFU_MANIFEST) 293 | { 294 | // since we don't buffer entire image and do any flashing in manifest stage 295 | return 0; 296 | } 297 | 298 | return 0; 299 | } 300 | 301 | #if BSP_FEATURE_FLASH_HP_VERSION 302 | int flash_write_block() { 303 | return R_FLASH_HP_Write(g_flash.p_ctrl, (uint32_t)aucbuffer, SKETCH_FLASH_OFFSET + ((auc_block_num * CFG_TUD_DFU_XFER_BUFSIZE)/sizeof(aucbuffer))*sizeof(aucbuffer) , auclen); 304 | } 305 | #endif 306 | 307 | #if BSP_FEATURE_FLASH_LP_VERSION 308 | int flash_write_block() { 309 | return R_FLASH_LP_Write(g_flash.p_ctrl, (uint32_t)aucbuffer, SKETCH_FLASH_OFFSET + ((auc_block_num * CFG_TUD_DFU_XFER_BUFSIZE)/sizeof(aucbuffer))*sizeof(aucbuffer) , PROGRAM_BLOCK_SIZE); 310 | } 311 | #endif 312 | 313 | // Invoked when received DFU_DNLOAD (wLength>0) following by DFU_GETSTATUS (state=DFU_DNBUSY) requests 314 | // This callback could be returned before flashing op is complete (async). 315 | // Once finished flashing, application must call tud_dfu_finish_flashing() 316 | void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, uint16_t length) 317 | { 318 | (void) alt; 319 | auc_block_num = block_num; 320 | 321 | //printf("\r\nReceived Alt %u BlockNum %u of length %u\r\n", alt, wBlockNum, length); 322 | 323 | if ((SKETCH_FLASH_OFFSET + (block_num * CFG_TUD_DFU_XFER_BUFSIZE)) % PROGRAM_BLOCK_SIZE == 0) { 324 | // erase block 325 | __disable_irq(); 326 | #if BSP_FEATURE_FLASH_HP_VERSION 327 | int err = R_FLASH_HP_Erase(g_flash.p_ctrl, SKETCH_FLASH_OFFSET + (block_num * CFG_TUD_DFU_XFER_BUFSIZE), 1); 328 | #endif 329 | #if BSP_FEATURE_FLASH_LP_VERSION 330 | int err = R_FLASH_LP_Erase(g_flash.p_ctrl, SKETCH_FLASH_OFFSET + (block_num * CFG_TUD_DFU_XFER_BUFSIZE), 1); 331 | #endif 332 | __enable_irq(); 333 | } 334 | memcpy(&aucbuffer[auclen], data, length); 335 | auclen += length; 336 | 337 | if (auclen == sizeof(aucbuffer)) { 338 | __disable_irq(); 339 | flash_write_block(); 340 | __enable_irq(); 341 | auclen = 0; 342 | } 343 | 344 | tud_dfu_finish_flashing(DFU_STATUS_OK); 345 | 346 | // flashing op for download complete without error 347 | } 348 | 349 | // Invoked when download process is complete, received DFU_DNLOAD (wLength=0) following by DFU_GETSTATUS (state=Manifest) 350 | // Application can do checksum, or actual flashing if buffered entire image previously. 351 | // Once finished flashing, application must call tud_dfu_finish_flashing() 352 | void tud_dfu_manifest_cb(uint8_t alt) 353 | { 354 | (void) alt; 355 | //printf("Download completed, enter manifestation\r\n"); 356 | 357 | __disable_irq(); 358 | flash_write_block(); 359 | __enable_irq(); 360 | 361 | // flashing op for manifest is complete without error 362 | // Application can perform checksum, should it fail, use appropriate status such as errVERIFY. 363 | tud_dfu_finish_flashing(DFU_STATUS_OK); 364 | 365 | // Try booting the application 366 | // boot5(SKETCH_FLASH_OFFSET); 367 | #if BSP_FEATURE_FLASH_HP_VERSION 368 | R_FLASH_HP_Close(g_flash.p_ctrl); 369 | #endif 370 | #if BSP_FEATURE_FLASH_LP_VERSION 371 | R_FLASH_LP_Close(g_flash.p_ctrl); 372 | #endif 373 | } 374 | 375 | // Invoked when received DFU_UPLOAD request 376 | // Application must populate data with up to length bytes and 377 | // Return the number of written bytes 378 | uint16_t tud_dfu_upload_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length) 379 | { 380 | (void) block_num; 381 | (void) alt; 382 | 383 | memcpy(data, (void*)(block_num * length), length); 384 | 385 | return length; 386 | } 387 | 388 | // Invoked when the Host has terminated a download or upload transfer 389 | void tud_dfu_abort_cb(uint8_t alt) 390 | { 391 | (void) alt; 392 | //printf("Host aborted transfer\r\n"); 393 | } 394 | 395 | // Invoked when a DFU_DETACH request is received 396 | void tud_dfu_detach_cb(void) 397 | { 398 | //printf("Host detach, we should probably reboot\r\n"); 399 | uint32_t start_ms = board_millis(); 400 | while (board_millis() - start_ms < 100) { 401 | tud_task(); 402 | } 403 | NVIC_SystemReset(); 404 | } 405 | 406 | #endif 407 | 408 | //--------------------------------------------------------------------+ 409 | // BLINKING TASK + Indicator pulse 410 | //--------------------------------------------------------------------+ 411 | 412 | void pulse_led() 413 | { 414 | static uint32_t pulse_width; 415 | static uint8_t dir; 416 | 417 | R_GPT_DutyCycleSet(&pwm_ctrl, pulse_width, pwm_channel); 418 | 419 | if (dir) { 420 | if (pulse_width < FADESTEP) { 421 | dir = 0U; 422 | pulse_width = FADESTEP; 423 | } else { 424 | pulse_width -= FADESTEP; 425 | } 426 | } else { 427 | pulse_width += FADESTEP; 428 | 429 | if (pulse_width >= PERIOD) { 430 | dir = 1U; 431 | pulse_width = PERIOD; 432 | } 433 | } 434 | } 435 | 436 | void led_blinking_task(void) 437 | { 438 | static uint32_t start_ms = 0; 439 | 440 | // Blink every interval ms 441 | if ( board_millis() - start_ms < 100) return; 442 | start_ms += 100; 443 | 444 | pulse_led(); 445 | } 446 | 447 | //--------------------------------------------------------------------+ 448 | // Double tap magic 449 | //--------------------------------------------------------------------+ 450 | void set_double_tap_data(uint32_t data) 451 | { 452 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK; 453 | BOOT_DOUBLE_TAP_DATA = data; 454 | R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK; 455 | } 456 | --------------------------------------------------------------------------------