diff options
| author | HiFiPhile <[email protected]> | 2026-06-22 21:30:58 +0200 |
|---|---|---|
| committer | HiFiPhile <[email protected]> | 2026-06-22 21:30:58 +0200 |
| commit | 693cdce08e14833f26f4e8a1f26e4fd546be4c35 (patch) | |
| tree | 7667d2223dc32d9f21b5b60ab200e64ed46e3e20 /hw/bsp/rx | |
| parent | 41e9eaa65a935136085d78ec4b99c81ff991b560 (diff) | |
| parent | cd3561bf158afd5a5718904b8139a338d1e3b67c (diff) | |
Merge remote-tracking branch 'tinyusb/master' into pr-osal-spin-deinit
Signed-off-by: HiFiPhile <[email protected]>
Diffstat (limited to 'hw/bsp/rx')
| -rw-r--r-- | hw/bsp/rx/FreeRTOSConfig/FreeRTOSConfig.h | 2 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/gr_citrus/board.cmake | 19 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/gr_citrus/board.h | 21 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/gr_citrus/gr_citrus.c | 199 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/gr_citrus/hwinit.c | 31 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/rx65n_target/board.cmake | 22 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/rx65n_target/board.h | 28 | ||||
| -rw-r--r-- | hw/bsp/rx/boards/rx65n_target/rx65n_target.c | 209 | ||||
| -rw-r--r-- | hw/bsp/rx/family.c | 234 | ||||
| -rw-r--r-- | hw/bsp/rx/family.cmake | 83 | ||||
| -rw-r--r-- | hw/bsp/rx/family.mk | 6 |
11 files changed, 446 insertions, 408 deletions
diff --git a/hw/bsp/rx/FreeRTOSConfig/FreeRTOSConfig.h b/hw/bsp/rx/FreeRTOSConfig/FreeRTOSConfig.h index edf0a8433..d9de30099 100644 --- a/hw/bsp/rx/FreeRTOSConfig/FreeRTOSConfig.h +++ b/hw/bsp/rx/FreeRTOSConfig/FreeRTOSConfig.h @@ -109,7 +109,7 @@ extern uint32_t SystemCoreClock; #define INCLUDE_vTaskDelayUntil 1 #define INCLUDE_vTaskDelay 1 #define INCLUDE_xTaskGetSchedulerState 0 -#define INCLUDE_xTaskGetCurrentTaskHandle 0 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 #define INCLUDE_uxTaskGetStackHighWaterMark 0 #define INCLUDE_xTaskGetIdleTaskHandle 0 #define INCLUDE_xTimerGetTimerDaemonTaskHandle 0 diff --git a/hw/bsp/rx/boards/gr_citrus/board.cmake b/hw/bsp/rx/boards/gr_citrus/board.cmake new file mode 100644 index 000000000..d6200e386 --- /dev/null +++ b/hw/bsp/rx/boards/gr_citrus/board.cmake @@ -0,0 +1,19 @@ +set(MCU_VARIANT rx63n) +set(MCU_FAMILY RX63X) + +set(CMAKE_SYSTEM_CPU rx610 CACHE INTERNAL "System Processor") + +set(LD_FILE_GNU ${CMAKE_CURRENT_LIST_DIR}/r5f5631fd.ld) + +set(JLINK_DEVICE R5F5631F) +set(JLINK_IF JTAG) + +set(BOARD_SOURCES + ${CMAKE_CURRENT_LIST_DIR}/gr_citrus.c + ) + +function(update_board TARGET) + target_include_directories(${TARGET} PUBLIC + ${CMAKE_CURRENT_FUNCTION_LIST_DIR} + ) +endfunction() diff --git a/hw/bsp/rx/boards/gr_citrus/board.h b/hw/bsp/rx/boards/gr_citrus/board.h index 617d309c3..cc0139e18 100644 --- a/hw/bsp/rx/boards/gr_citrus/board.h +++ b/hw/bsp/rx/boards/gr_citrus/board.h @@ -32,10 +32,31 @@ #ifndef BOARD_H_ #define BOARD_H_ +#include "iodefine.h" + #ifdef __cplusplus extern "C" { #endif +// LED: PA0, active high +#define BOARD_LED_WRITE(state) (PORTA.PODR.BIT.B0 = (state) ? 1 : 0) + +// No user button +#define BOARD_BUTTON_READ() 0 + +// UART: SCI0 +#define BOARD_UART_SCI SCI0 +#define BOARD_SCI_TXI_HANDLER INT_Excep_SCI0_TXI0 +#define BOARD_SCI_TEI_HANDLER INT_Excep_SCI0_TEI0 +#define BOARD_SCI_RXI_HANDLER INT_Excep_SCI0_RXI0 + +// USB interrupt handler +#define BOARD_USB_IRQ_HANDLER INT_Excep_USB0_USBI0 + +// Clocks +#define BOARD_PCLK 48000000 +#define BOARD_CPUCLK 96000000 + #ifdef __cplusplus } #endif diff --git a/hw/bsp/rx/boards/gr_citrus/gr_citrus.c b/hw/bsp/rx/boards/gr_citrus/gr_citrus.c index 26ad4a6aa..af8c108d0 100644 --- a/hw/bsp/rx/boards/gr_citrus/gr_citrus.c +++ b/hw/bsp/rx/boards/gr_citrus/gr_citrus.c @@ -52,141 +52,63 @@ * regarding downloading. */ -#include "bsp/board_api.h" #include "iodefine.h" -#include "interrupt_handlers.h" - -#define IRQ_PRIORITY_CMT0 5 -#define IRQ_PRIORITY_USBI0 6 -#define IRQ_PRIORITY_SCI0 5 +#include "board.h" #define SYSTEM_PRCR_PRC1 (1<<1) #define SYSTEM_PRCR_PRKEY (0xA5u<<8) - -#define CMT_PCLK 48000000 -#define CMT_CMCR_CKS_DIV_128 2 -#define CMT_CMCR_CMIE (1<<6) #define MPC_PFS_ISEL (1<<6) +#define IRQ_PRIORITY_SCI0 5 #define SCI_PCLK 48000000 -#define SCI_SSR_FER (1<<4) -#define SCI_SSR_ORER (1<<5) - -#define SCI_SCR_TEIE (1u<<2) -#define SCI_SCR_RE (1u<<4) -#define SCI_SCR_TE (1u<<5) -#define SCI_SCR_RIE (1u<<6) -#define SCI_SCR_TIE (1u<<7) - -//--------------------------------------------------------------------+ -// SCI0 handling -//--------------------------------------------------------------------+ -typedef struct { - uint8_t *buf; - uint32_t cnt; -} sci_buf_t; -static volatile sci_buf_t sci0_buf[2]; - -void INT_Excep_SCI0_TXI0(void) -{ - uint8_t *buf = sci0_buf[0].buf; - uint32_t cnt = sci0_buf[0].cnt; - - if (!buf || !cnt) { - SCI0.SCR.BYTE &= ~(SCI_SCR_TEIE | SCI_SCR_TE | SCI_SCR_TIE); - return; - } - SCI0.TDR = *buf; - if (--cnt) { - ++buf; - } else { - buf = NULL; - SCI0.SCR.BIT.TIE = 0; - SCI0.SCR.BIT.TEIE = 1; - } - sci0_buf[0].buf = buf; - sci0_buf[0].cnt = cnt; -} - -void INT_Excep_SCI0_TEI0(void) -{ - SCI0.SCR.BYTE &= ~(SCI_SCR_TEIE | SCI_SCR_TE | SCI_SCR_TIE); -} - -void INT_Excep_SCI0_RXI0(void) -{ - uint8_t *buf = sci0_buf[1].buf; - uint32_t cnt = sci0_buf[1].cnt; - - if (!buf || !cnt || - (SCI0.SSR.BYTE & (SCI_SSR_FER | SCI_SSR_ORER))) { - sci0_buf[1].buf = NULL; - SCI0.SSR.BYTE = 0; - SCI0.SCR.BYTE &= ~(SCI_SCR_RE | SCI_SCR_RIE); - return; - } - *buf = SCI0.RDR; - if (--cnt) { - ++buf; - } else { - buf = NULL; - SCI0.SCR.BYTE &= ~(SCI_SCR_RE | SCI_SCR_RIE); - } - sci0_buf[1].buf = buf; - sci0_buf[1].cnt = cnt; -} -//--------------------------------------------------------------------+ -// Forward USB interrupt events to TinyUSB IRQ Handler -//--------------------------------------------------------------------+ -void INT_Excep_USB0_USBI0(void) +void HardwareSetup(void) { - tud_int_handler(0); + SYSTEM.PRCR.WORD = 0xA503u; + SYSTEM.SOSCCR.BYTE = 0x01u; + SYSTEM.MOSCWTCR.BYTE = 0x0Du; + SYSTEM.PLLWTCR.BYTE = 0x0Eu; + SYSTEM.PLLCR.WORD = 0x0F00u; + SYSTEM.MOSCCR.BYTE = 0x00u; + SYSTEM.PLLCR2.BYTE = 0x00u; + for (unsigned i = 0; i < 2075u; ++i) __asm("nop"); + SYSTEM.SCKCR.LONG = 0x21021211u; + SYSTEM.SCKCR2.WORD = 0x0033u; + SYSTEM.SCKCR3.WORD = 0x0400u; + SYSTEM.SYSCR0.WORD = 0x5A01; + SYSTEM.MSTPCRB.BIT.MSTPB15 = 0; + SYSTEM.PRCR.WORD = 0xA500u; } -void board_init(void) +void board_pin_init(void) { -#if CFG_TUSB_OS == OPT_OS_NONE - /* Enable CMT0 */ - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; - MSTP(CMT0) = 0; - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY; - /* Setup 1ms tick timer */ - CMT0.CMCNT = 0; - CMT0.CMCOR = CMT_PCLK / 1000 / 128; - CMT0.CMCR.WORD = CMT_CMCR_CMIE | CMT_CMCR_CKS_DIV_128; - IR(CMT0, CMI0) = 0; - IPR(CMT0, CMI0) = IRQ_PRIORITY_CMT0; - IEN(CMT0, CMI0) = 1; - CMT.CMSTR0.BIT.STR0 = 1; -#endif - /* Unlock MPC registers */ MPC.PWPR.BIT.B0WI = 0; MPC.PWPR.BIT.PFSWE = 1; + /* LED PA0 */ PORTA.PMR.BIT.B0 = 0U; PORTA.PODR.BIT.B0 = 0U; PORTA.PDR.BIT.B0 = 1U; + /* UART TXD0 => P20, RXD0 => P21 */ PORT2.PMR.BIT.B0 = 1U; PORT2.PCR.BIT.B0 = 1U; MPC.P20PFS.BYTE = 0b01010; PORT2.PMR.BIT.B1 = 1U; MPC.P21PFS.BYTE = 0b01010; + /* USB VBUS -> P16 DPUPE -> P14 */ PORT1.PMR.BIT.B4 = 1U; PORT1.PMR.BIT.B6 = 1U; MPC.P14PFS.BYTE = 0b10001; MPC.P16PFS.BYTE = MPC_PFS_ISEL | 0b10001; MPC.PFUSB0.BIT.PUPHZS = 1; + /* Lock MPC registers */ MPC.PWPR.BIT.PFSWE = 0; MPC.PWPR.BIT.B0WI = 1; - IR(USB0, USBI0) = 0; - IPR(USB0, USBI0) = IRQ_PRIORITY_USBI0; - /* Enable SCI0 */ SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; MSTP(SCI0) = 0; @@ -201,81 +123,4 @@ void board_init(void) IEN(SCI0, RXI0) = 1; IEN(SCI0, TXI0) = 1; IEN(SCI0, TEI0) = 1; - - /* Enable USB0 */ - unsigned short oldPRCR = SYSTEM.PRCR.WORD; - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; - MSTP(USB0) = 0; - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | oldPRCR; -} - -//--------------------------------------------------------------------+ -// Board porting API -//--------------------------------------------------------------------+ - -void board_led_write(bool state) -{ - PORTA.PODR.BIT.B0 = state ? 1 : 0; -} - -uint32_t board_button_read(void) -{ - return 0; -} - -int board_uart_read(uint8_t* buf, int len) -{ - sci0_buf[1].buf = buf; - sci0_buf[1].cnt = len; - SCI0.SCR.BYTE |= SCI_SCR_RE | SCI_SCR_RIE; - while (SCI0.SCR.BIT.RE) ; - return len - sci0_buf[1].cnt; -} - -int board_uart_write(void const *buf, int len) -{ - sci0_buf[0].buf = (uint8_t*)(uintptr_t) buf; - sci0_buf[0].cnt = len; - SCI0.SCR.BYTE |= SCI_SCR_TE | SCI_SCR_TIE; - while (SCI0.SCR.BIT.TE) ; - return len; -} - -#if CFG_TUSB_OS == OPT_OS_NONE -volatile uint32_t system_ticks = 0; -void INT_Excep_CMT0_CMI0(void) -{ - ++system_ticks; -} - -uint32_t board_millis(void) -{ - return system_ticks; -} -#else -uint32_t SystemCoreClock = 96000000; -#endif - -int close(int fd) -{ - (void)fd; - return -1; -} -int fstat(int fd, void *pstat) -{ - (void)fd; - (void)pstat; - return 0; -} -off_t lseek(int fd, off_t pos, int whence) -{ - (void)fd; - (void)pos; - (void)whence; - return 0; -} -int isatty(int fd) -{ - (void)fd; - return 1; } diff --git a/hw/bsp/rx/boards/gr_citrus/hwinit.c b/hw/bsp/rx/boards/gr_citrus/hwinit.c deleted file mode 100644 index 8245d7744..000000000 --- a/hw/bsp/rx/boards/gr_citrus/hwinit.c +++ /dev/null @@ -1,31 +0,0 @@ -/************************************************************************/ -/* File Version: V1.00 */ -/* Date Generated: 08/07/2013 */ -/************************************************************************/ - -#include "iodefine.h" -#ifdef __cplusplus -extern "C" { -#endif -extern void HardwareSetup(void); -#ifdef __cplusplus -} -#endif - -void HardwareSetup(void) -{ - SYSTEM.PRCR.WORD = 0xA503u; - SYSTEM.SOSCCR.BYTE = 0x01u; - SYSTEM.MOSCWTCR.BYTE = 0x0Du; - SYSTEM.PLLWTCR.BYTE = 0x0Eu; - SYSTEM.PLLCR.WORD = 0x0F00u; - SYSTEM.MOSCCR.BYTE = 0x00u; - SYSTEM.PLLCR2.BYTE = 0x00u; - for (unsigned i = 0; i < 2075u; ++i) __asm("nop"); - SYSTEM.SCKCR.LONG = 0x21021211u; - SYSTEM.SCKCR2.WORD = 0x0033u; - SYSTEM.SCKCR3.WORD = 0x0400u; - SYSTEM.SYSCR0.WORD = 0x5A01; - SYSTEM.MSTPCRB.BIT.MSTPB15 = 0; - SYSTEM.PRCR.WORD = 0xA500u; -} diff --git a/hw/bsp/rx/boards/rx65n_target/board.cmake b/hw/bsp/rx/boards/rx65n_target/board.cmake new file mode 100644 index 000000000..c8b9286bc --- /dev/null +++ b/hw/bsp/rx/boards/rx65n_target/board.cmake @@ -0,0 +1,22 @@ +set(MCU_VARIANT rx65n) +set(MCU_FAMILY RX65X) + +set(CMAKE_SYSTEM_CPU rx64m CACHE INTERNAL "System Processor") + +set(LD_FILE_GNU ${CMAKE_CURRENT_LIST_DIR}/r5f565ne.ld) + +set(JLINK_DEVICE R5F565NE) +set(JLINK_IF JTAG) + +set(RFP_DEVICE rx65x) +set(RFP_TOOL e2l) + +set(BOARD_SOURCES + ${CMAKE_CURRENT_LIST_DIR}/rx65n_target.c + ) + +function(update_board TARGET) + target_include_directories(${TARGET} PUBLIC + ${CMAKE_CURRENT_FUNCTION_LIST_DIR} + ) +endfunction() diff --git a/hw/bsp/rx/boards/rx65n_target/board.h b/hw/bsp/rx/boards/rx65n_target/board.h index 8c8e7b95f..4f1367fbd 100644 --- a/hw/bsp/rx/boards/rx65n_target/board.h +++ b/hw/bsp/rx/boards/rx65n_target/board.h @@ -32,10 +32,38 @@ #ifndef BOARD_H_ #define BOARD_H_ +#include "iodefine.h" + #ifdef __cplusplus extern "C" { #endif +// LED: PD6, active low (open-drain) +#define BOARD_LED_WRITE(state) (PORTD.PODR.BIT.B6 = (state) ? 0 : 1) + +// Button: PB1, active low +#define BOARD_BUTTON_READ() (PORTB.PIDR.BIT.B1 ? 0 : 1) + +// UART: SCI5 +#define BOARD_UART_SCI SCI5 +#define BOARD_SCI_TXI_HANDLER INT_Excep_SCI5_TXI5 +#define BOARD_SCI_TEI_HANDLER INT_Excep_ICU_GROUPBL0 // SCI5 TEI uses group interrupt +#define BOARD_SCI_RXI_HANDLER INT_Excep_SCI5_RXI5 + +// USB interrupt handler (software configurable vector) +#define IRQ_USB0_USBI0 62 +#define SLIBR_USBI0 SLIBR185 +#define IR_USB0_USBI0 IR_PERIB_INTB185 +#define IER_USB0_USBI0 IER_PERIB_INTB185 +#define IEN_USB0_USBI0 IEN_PERIB_INTB185 +#define IPR_USB0_USBI0 IPR_PERIB_INTB185 +#define INT_Excep_USB0_USBI0 INT_Excep_PERIB_INTB185 +#define BOARD_USB_IRQ_HANDLER INT_Excep_USB0_USBI0 + +// Clocks +#define BOARD_PCLK 60000000 +#define BOARD_CPUCLK 120000000 + #ifdef __cplusplus } #endif diff --git a/hw/bsp/rx/boards/rx65n_target/rx65n_target.c b/hw/bsp/rx/boards/rx65n_target/rx65n_target.c index 66a319541..318cd1b6d 100644 --- a/hw/bsp/rx/boards/rx65n_target/rx65n_target.c +++ b/hw/bsp/rx/boards/rx65n_target/rx65n_target.c @@ -50,37 +50,15 @@ * regarding downloading. */ -#include "bsp/board_api.h" #include "iodefine.h" -#include "interrupt_handlers.h" - -#define IRQ_PRIORITY_CMT0 5 -#define IRQ_PRIORITY_USBI0 6 -#define IRQ_PRIORITY_SCI5 5 +#include "board.h" #define SYSTEM_PRCR_PRC1 (1<<1) #define SYSTEM_PRCR_PRKEY (0xA5u<<8) - -#define CMT_PCLK 60000000 -#define CMT_CMCR_CKS_DIV_128 2 -#define CMT_CMCR_CMIE (1<<6) #define MPC_PFS_ISEL (1<<6) +#define IRQ_PRIORITY_SCI5 5 #define SCI_PCLK 60000000 -#define SCI_SSR_FER (1<<4) -#define SCI_SSR_ORER (1<<5) - -#define SCI_SCR_TEIE (1u<<2) -#define SCI_SCR_RE (1u<<4) -#define SCI_SCR_TE (1u<<5) -#define SCI_SCR_RIE (1u<<6) -#define SCI_SCR_TIE (1u<<7) -#define INT_Excep_SCI5_TEI5 INT_Excep_ICU_GROUPBL0 - -#define IRQ_USB0_USBI0 62 -#define SLIBR_USBI0 SLIBR185 -#define IPR_USB0_USBI0 IPR_PERIB_INTB185 -#define INT_Excep_USB0_USBI0 INT_Excep_PERIB_INTB185 void HardwareSetup(void) { @@ -113,118 +91,37 @@ void HardwareSetup(void) SYSTEM.PRCR.WORD = 0xA500u; } -//--------------------------------------------------------------------+ -// SCI handling -//--------------------------------------------------------------------+ -typedef struct { - uint8_t *buf; - uint32_t cnt; -} sci_buf_t; -static volatile sci_buf_t sci_buf[2]; - -void INT_Excep_SCI5_TXI5(void) -{ - uint8_t *buf = sci_buf[0].buf; - uint32_t cnt = sci_buf[0].cnt; - - if (!buf || !cnt) { - SCI5.SCR.BYTE &= ~(SCI_SCR_TEIE | SCI_SCR_TE | SCI_SCR_TIE); - return; - } - SCI5.TDR = *buf; - if (--cnt) { - ++buf; - } else { - buf = NULL; - SCI5.SCR.BIT.TIE = 0; - SCI5.SCR.BIT.TEIE = 1; - } - sci_buf[0].buf = buf; - sci_buf[0].cnt = cnt; -} - -void INT_Excep_SCI5_TEI5(void) -{ - SCI5.SCR.BYTE &= ~(SCI_SCR_TEIE | SCI_SCR_TE | SCI_SCR_TIE); -} - -void INT_Excep_SCI5_RXI5(void) +void board_pin_init(void) { - uint8_t *buf = sci_buf[1].buf; - uint32_t cnt = sci_buf[1].cnt; - - if (!buf || !cnt || - (SCI5.SSR.BYTE & (SCI_SSR_FER | SCI_SSR_ORER))) { - sci_buf[1].buf = NULL; - SCI5.SSR.BYTE = 0; - SCI5.SCR.BYTE &= ~(SCI_SCR_RE | SCI_SCR_RIE); - return; - } - *buf = SCI5.RDR; - if (--cnt) { - ++buf; - } else { - buf = NULL; - SCI5.SCR.BYTE &= ~(SCI_SCR_RE | SCI_SCR_RIE); - } - sci_buf[1].buf = buf; - sci_buf[1].cnt = cnt; -} - -//--------------------------------------------------------------------+ -// Forward USB interrupt events to TinyUSB IRQ Handler -//--------------------------------------------------------------------+ -void INT_Excep_USB0_USBI0(void) -{ -#if CFG_TUH_ENABLED - tuh_int_handler(0, true); -#endif -#if CFG_TUD_ENABLED - tud_int_handler(0); -#endif -} - -void board_init(void) -{ - /* setup software configurable interrupts */ + /* Setup software configurable interrupts for USB */ ICU.SLIBR_USBI0.BYTE = IRQ_USB0_USBI0; ICU.SLIPRCR.BYTE = 1; -#if CFG_TUSB_OS == OPT_OS_NONE - /* Enable CMT0 */ - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; - MSTP(CMT0) = 0; - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY; - /* Setup 1ms tick timer */ - CMT0.CMCNT = 0; - CMT0.CMCOR = CMT_PCLK / 1000 / 128; - CMT0.CMCR.WORD = CMT_CMCR_CMIE | CMT_CMCR_CKS_DIV_128; - IR(CMT0, CMI0) = 0; - IPR(CMT0, CMI0) = IRQ_PRIORITY_CMT0; - IEN(CMT0, CMI0) = 1; - CMT.CMSTR0.BIT.STR0 = 1; -#endif - /* Unlock MPC registers */ MPC.PWPR.BIT.B0WI = 0; MPC.PWPR.BIT.PFSWE = 1; - // SW PB1 + + /* Button PB1 */ PORTB.PMR.BIT.B1 = 0U; PORTB.PDR.BIT.B1 = 0U; - // LED PD6 + + /* LED PD6 (open-drain, active low) */ PORTD.PODR.BIT.B6 = 1U; PORTD.ODR1.BIT.B4 = 1U; PORTD.PMR.BIT.B6 = 0U; PORTD.PDR.BIT.B6 = 1U; + /* UART TXD5 => PA4, RXD5 => PA3 */ PORTA.PMR.BIT.B4 = 1U; PORTA.PCR.BIT.B4 = 1U; MPC.PA4PFS.BYTE = 0b01010; PORTA.PMR.BIT.B3 = 1U; - MPC.PA5PFS.BYTE = 0b01010; + MPC.PA3PFS.BYTE = 0b01010; + /* USB VBUS -> P16 */ PORT1.PMR.BIT.B6 = 1U; MPC.P16PFS.BYTE = MPC_PFS_ISEL | 0b10001; + /* Lock MPC registers */ MPC.PWPR.BIT.PFSWE = 0; MPC.PWPR.BIT.B0WI = 1; @@ -247,86 +144,4 @@ void board_init(void) IEN(SCI5, TXI5) = 1; IEN(ICU,GROUPBL0) = 1; EN(SCI5, TEI5) = 1; - - /* Enable USB0 */ - unsigned short oldPRCR = SYSTEM.PRCR.WORD; - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; - MSTP(USB0) = 0; - SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | oldPRCR; - - /* setup USBI0 interrupt. */ - IR(USB0, USBI0) = 0; - IPR(USB0, USBI0) = IRQ_PRIORITY_USBI0; -} - -//--------------------------------------------------------------------+ -// Board porting API -//--------------------------------------------------------------------+ - -void board_led_write(bool state) -{ - PORTD.PODR.BIT.B6 = state ? 0 : 1; -} - -uint32_t board_button_read(void) -{ - return PORTB.PIDR.BIT.B1 ? 0 : 1; -} - -int board_uart_read(uint8_t* buf, int len) -{ - sci_buf[1].buf = buf; - sci_buf[1].cnt = len; - SCI5.SCR.BYTE |= SCI_SCR_RE | SCI_SCR_RIE; - // TODO change to non blocking, return -1 immediately if no data - while (SCI5.SCR.BIT.RE) ; - return len - sci_buf[1].cnt; -} - -int board_uart_write(void const *buf, int len) -{ - sci_buf[0].buf = (uint8_t*)(uintptr_t) buf; - sci_buf[0].cnt = len; - SCI5.SCR.BYTE |= SCI_SCR_TE | SCI_SCR_TIE; - while (SCI5.SCR.BIT.TE) ; - return len; -} - -#if CFG_TUSB_OS == OPT_OS_NONE -volatile uint32_t system_ticks = 0; -void INT_Excep_CMT0_CMI0(void) -{ - ++system_ticks; -} - -uint32_t board_millis(void) -{ - return system_ticks; -} -#else -uint32_t SystemCoreClock = 120000000; -#endif - -int close(int fd) -{ - (void)fd; - return -1; -} -int fstat(int fd, void *pstat) -{ - (void)fd; - (void)pstat; - return 0; -} -off_t lseek(int fd, off_t pos, int whence) -{ - (void)fd; - (void)pos; - (void)whence; - return 0; -} -int isatty(int fd) -{ - (void)fd; - return 1; } diff --git a/hw/bsp/rx/family.c b/hw/bsp/rx/family.c new file mode 100644 index 000000000..5741b44ef --- /dev/null +++ b/hw/bsp/rx/family.c @@ -0,0 +1,234 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2021, Koji Kitayama + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#include "bsp/board_api.h" +#include "board.h" +#include "interrupt_handlers.h" + +#define SYSTEM_PRCR_PRC1 (1<<1) +#define SYSTEM_PRCR_PRKEY (0xA5u<<8) + +#define CMT_CMCR_CKS_DIV_128 2 +#define CMT_CMCR_CMIE (1<<6) + +#define IRQ_PRIORITY_CMT0 5 +#define IRQ_PRIORITY_USBI0 6 + +#define SCI_SSR_FER (1<<4) +#define SCI_SSR_ORER (1<<5) +#define SCI_SCR_TEIE (1u<<2) +#define SCI_SCR_RE (1u<<4) +#define SCI_SCR_TE (1u<<5) +#define SCI_SCR_RIE (1u<<6) +#define SCI_SCR_TIE (1u<<7) + +// Board-specific pin/peripheral init (implemented per board) +void board_pin_init(void); + +//--------------------------------------------------------------------+ +// SCI UART interrupt handlers +//--------------------------------------------------------------------+ +typedef struct { + uint8_t *buf; + uint32_t cnt; +} sci_buf_t; +static volatile sci_buf_t sci_buf[2]; + +void BOARD_SCI_TXI_HANDLER(void) +{ + uint8_t *buf = sci_buf[0].buf; + uint32_t cnt = sci_buf[0].cnt; + + if (!buf || !cnt) { + BOARD_UART_SCI.SCR.BYTE &= ~(SCI_SCR_TEIE | SCI_SCR_TE | SCI_SCR_TIE); + return; + } + BOARD_UART_SCI.TDR = *buf; + if (--cnt) { + ++buf; + } else { + buf = NULL; + BOARD_UART_SCI.SCR.BIT.TIE = 0; + BOARD_UART_SCI.SCR.BIT.TEIE = 1; + } + sci_buf[0].buf = buf; + sci_buf[0].cnt = cnt; +} + +void BOARD_SCI_TEI_HANDLER(void) +{ + BOARD_UART_SCI.SCR.BYTE &= ~(SCI_SCR_TEIE | SCI_SCR_TE | SCI_SCR_TIE); +} + +void BOARD_SCI_RXI_HANDLER(void) +{ + uint8_t *buf = sci_buf[1].buf; + uint32_t cnt = sci_buf[1].cnt; + + if (!buf || !cnt || + (BOARD_UART_SCI.SSR.BYTE & (SCI_SSR_FER | SCI_SSR_ORER))) { + sci_buf[1].buf = NULL; + BOARD_UART_SCI.SSR.BYTE = 0; + BOARD_UART_SCI.SCR.BYTE &= ~(SCI_SCR_RE | SCI_SCR_RIE); + return; + } + *buf = BOARD_UART_SCI.RDR; + if (--cnt) { + ++buf; + } else { + buf = NULL; + BOARD_UART_SCI.SCR.BYTE &= ~(SCI_SCR_RE | SCI_SCR_RIE); + } + sci_buf[1].buf = buf; + sci_buf[1].cnt = cnt; +} + +//--------------------------------------------------------------------+ +// Forward USB interrupt events to TinyUSB IRQ Handler +//--------------------------------------------------------------------+ +void BOARD_USB_IRQ_HANDLER(void) +{ +#if CFG_TUH_ENABLED + tuh_int_handler(0, true); +#endif +#if CFG_TUD_ENABLED + tud_int_handler(0); +#endif +} + +//--------------------------------------------------------------------+ +// Board init +//--------------------------------------------------------------------+ +void board_init(void) +{ +#if CFG_TUSB_OS == OPT_OS_NONE + /* Enable CMT0 */ + SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; + MSTP(CMT0) = 0; + SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY; + /* Setup 1ms tick timer */ + CMT0.CMCNT = 0; + CMT0.CMCOR = BOARD_PCLK / 1000 / 128; + CMT0.CMCR.WORD = CMT_CMCR_CMIE | CMT_CMCR_CKS_DIV_128; + IR(CMT0, CMI0) = 0; + IPR(CMT0, CMI0) = IRQ_PRIORITY_CMT0; + IEN(CMT0, CMI0) = 1; + CMT.CMSTR0.BIT.STR0 = 1; +#endif + + /* Board-specific: pin mux, SCI, USB pin config */ + board_pin_init(); + + /* Enable USB0 module */ + unsigned short oldPRCR = SYSTEM.PRCR.WORD; + SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | SYSTEM_PRCR_PRC1; + MSTP(USB0) = 0; + SYSTEM.PRCR.WORD = SYSTEM_PRCR_PRKEY | oldPRCR; + + /* USB IRQ */ + IR(USB0, USBI0) = 0; + IPR(USB0, USBI0) = IRQ_PRIORITY_USBI0; +} + +//--------------------------------------------------------------------+ +// Board porting API +//--------------------------------------------------------------------+ + +void board_led_write(bool state) +{ + BOARD_LED_WRITE(state); +} + +uint32_t board_button_read(void) +{ + return BOARD_BUTTON_READ(); +} + +int board_uart_read(uint8_t* buf, int len) +{ + sci_buf[1].buf = buf; + sci_buf[1].cnt = len; + BOARD_UART_SCI.SCR.BYTE |= SCI_SCR_RE | SCI_SCR_RIE; + while (BOARD_UART_SCI.SCR.BIT.RE) ; + return len - sci_buf[1].cnt; +} + +int board_uart_write(void const *buf, int len) +{ + sci_buf[0].buf = (uint8_t*)(uintptr_t) buf; + sci_buf[0].cnt = len; + BOARD_UART_SCI.SCR.BYTE |= SCI_SCR_TE | SCI_SCR_TIE; + while (BOARD_UART_SCI.SCR.BIT.TE) ; + return len; +} + +//--------------------------------------------------------------------+ +// Tick timer +//--------------------------------------------------------------------+ +#if CFG_TUSB_OS == OPT_OS_NONE +volatile uint32_t system_ticks = 0; +void INT_Excep_CMT0_CMI0(void) +{ + ++system_ticks; +} + +uint32_t tusb_time_millis_api(void) +{ + return system_ticks; +} +#else +uint32_t SystemCoreClock = BOARD_CPUCLK; +#endif + +//--------------------------------------------------------------------+ +// Newlib syscall stubs +//--------------------------------------------------------------------+ +int close(int fd) +{ + (void)fd; + return -1; +} + +int fstat(int fd, void *pstat) +{ + (void)fd; + (void)pstat; + return 0; +} + +off_t lseek(int fd, off_t pos, int whence) +{ + (void)fd; + (void)pos; + (void)whence; + return 0; +} + +int isatty(int fd) +{ + (void)fd; + return 1; +} diff --git a/hw/bsp/rx/family.cmake b/hw/bsp/rx/family.cmake new file mode 100644 index 000000000..9bb0dcebb --- /dev/null +++ b/hw/bsp/rx/family.cmake @@ -0,0 +1,83 @@ +include_guard() + +set(MCU_DIR ${TOP}/hw/mcu/renesas/rx) + +# include board specific +include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake) + +# toolchain set up +set(CMAKE_TOOLCHAIN_FILE ${TOP}/examples/build_system/cmake/toolchain/rx_gcc.cmake) + +set(FAMILY_MCUS ${MCU_FAMILY} CACHE INTERNAL "") + +#------------------------------------ +# Startup & Linker script +#------------------------------------ +set(LD_FILE_Clang ${LD_FILE_GNU}) + +#------------------------------------ +# Board Target +#------------------------------------ +function(family_add_board BOARD_TARGET) + add_library(${BOARD_TARGET} STATIC + ${MCU_DIR}/${MCU_VARIANT}/vects.c + ) + target_include_directories(${BOARD_TARGET} PUBLIC + ${CMAKE_CURRENT_FUNCTION_LIST_DIR} + ${MCU_DIR}/${MCU_VARIANT} + ) + target_compile_definitions(${BOARD_TARGET} PUBLIC + SSIZE_MAX=__INT_MAX__ + ) + + update_board(${BOARD_TARGET}) +endfunction() + +#------------------------------------ +# Functions +#------------------------------------ +function(family_configure_example TARGET RTOS) + family_configure_common(${TARGET} ${RTOS}) + family_add_tinyusb(${TARGET} OPT_MCU_${MCU_FAMILY}) + + target_sources(${TARGET} PUBLIC + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../board.c + ${TOP}/src/portable/renesas/rusb2/rusb2_common.c + ${TOP}/src/portable/renesas/rusb2/dcd_rusb2.c + ${TOP}/src/portable/renesas/rusb2/hcd_rusb2.c + ${MCU_DIR}/${MCU_VARIANT}/start.S + ${BOARD_SOURCES} + ) + target_include_directories(${TARGET} PUBLIC + ${CMAKE_CURRENT_FUNCTION_LIST_DIR} + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../../ + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/boards/${BOARD} + ) + + if (CMAKE_C_COMPILER_ID STREQUAL "GNU") + target_compile_options(${TARGET} PUBLIC + -Wno-error=redundant-decls + ) + target_link_options(${TARGET} PUBLIC + "LINKER:--script=${LD_FILE_GNU}" + -nostartfiles + --specs=nosys.specs --specs=nano.specs + ) + endif () + + set_source_files_properties(${MCU_DIR}/${MCU_VARIANT}/start.S PROPERTIES + SKIP_LINTING ON + COMPILE_OPTIONS -w) + + # Suppress warnings for board-specific and family source files + set_source_files_properties( + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c + ${BOARD_SOURCES} + PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes") + + # Flashing + family_add_bin_hex(${TARGET}) + family_flash_jlink(${TARGET}) + family_flash_rfp(${TARGET}) +endfunction() diff --git a/hw/bsp/rx/family.mk b/hw/bsp/rx/family.mk index 4ecf80409..a357460e8 100644 --- a/hw/bsp/rx/family.mk +++ b/hw/bsp/rx/family.mk @@ -14,12 +14,14 @@ CFLAGS += \ # suppress warning caused by vendor mcu driver CFLAGS += -Wno-error=redundant-decls -LDFLAGS_GCC += -specs=nosys.specs -specs=nano.specs +LDFLAGS += -specs=nosys.specs -specs=nano.specs SRC_C += \ src/portable/renesas/rusb2/dcd_rusb2.c \ src/portable/renesas/rusb2/hcd_rusb2.c \ - $(MCU_DIR)/vects.c + src/portable/renesas/rusb2/rusb2_common.c \ + $(MCU_DIR)/vects.c \ + $(FAMILY_PATH)/family.c INC += \ $(TOP)/$(BOARD_PATH) \ |
