|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2022, Rafael Silva |
| 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 | + * This file is part of the TinyUSB stack. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | + |
| 29 | +#include "bsp/board.h" |
| 30 | +#include "bsp_api.h" |
| 31 | +#include "r_ioport.h" |
| 32 | +#include "r_ioport_api.h" |
| 33 | +#include "renesas.h" |
| 34 | + |
| 35 | +/* Key code for writing PRCR register. */ |
| 36 | +#define BSP_PRV_PRCR_KEY (0xA500U) |
| 37 | +#define BSP_PRV_PRCR_PRC1_UNLOCK ((BSP_PRV_PRCR_KEY) | 0x2U) |
| 38 | +#define BSP_PRV_PRCR_LOCK ((BSP_PRV_PRCR_KEY) | 0x0U) |
| 39 | + |
| 40 | +#define SW1 (BSP_IO_PORT_00_PIN_05) |
| 41 | +#define SW2 (BSP_IO_PORT_00_PIN_06) |
| 42 | +#define LED1 (BSP_IO_PORT_04_PIN_15) |
| 43 | +#define LED3 (BSP_IO_PORT_04_PIN_00) |
| 44 | +#define LED2 (BSP_IO_PORT_04_PIN_04) |
| 45 | + |
| 46 | +/* ISR prototypes */ |
| 47 | +void usbfs_interrupt_handler(void); |
| 48 | +void usbfs_resume_handler(void); |
| 49 | +void usbfs_d0fifo_handler(void); |
| 50 | +void usbfs_d1fifo_handler(void); |
| 51 | + |
| 52 | +BSP_DONT_REMOVE const |
| 53 | + fsp_vector_t g_vector_table[BSP_ICU_VECTOR_MAX_ENTRIES] BSP_PLACE_IN_SECTION(BSP_SECTION_APPLICATION_VECTORS) = { |
| 54 | + [0] = usbfs_interrupt_handler, /* USBFS INT (USBFS interrupt) */ |
| 55 | + [1] = usbfs_resume_handler, /* USBFS RESUME (USBFS resume interrupt) */ |
| 56 | + [2] = usbfs_d0fifo_handler, /* USBFS FIFO 0 (DMA transfer request 0) */ |
| 57 | + [3] = usbfs_d1fifo_handler, /* USBFS FIFO 1 (DMA transfer request 1) */ |
| 58 | +}; |
| 59 | +const bsp_interrupt_event_t g_interrupt_event_link_select[BSP_ICU_VECTOR_MAX_ENTRIES] = { |
| 60 | + [0] = BSP_PRV_IELS_ENUM(EVENT_USBFS_INT), /* USBFS INT (USBFS interrupt) */ |
| 61 | + [1] = BSP_PRV_IELS_ENUM(EVENT_USBFS_RESUME), /* USBFS RESUME (USBFS resume interrupt) */ |
| 62 | + [2] = BSP_PRV_IELS_ENUM(EVENT_USBFS_FIFO_0), /* USBFS FIFO 0 (DMA transfer request 0) */ |
| 63 | + [3] = BSP_PRV_IELS_ENUM(EVENT_USBFS_FIFO_1) /* USBFS FIFO 1 (DMA transfer request 1) */ |
| 64 | +}; |
| 65 | + |
| 66 | +const ioport_pin_cfg_t g_bsp_pin_cfg_data[] = { |
| 67 | + {.pin = BSP_IO_PORT_04_PIN_07, |
| 68 | + .pin_cfg = ((uint32_t) IOPORT_CFG_PERIPHERAL_PIN | (uint32_t) IOPORT_PERIPHERAL_USB_FS)}, |
| 69 | + {.pin = BSP_IO_PORT_05_PIN_00, |
| 70 | + .pin_cfg = ((uint32_t) IOPORT_CFG_PERIPHERAL_PIN | (uint32_t) IOPORT_PERIPHERAL_USB_FS)}, |
| 71 | + {.pin = BSP_IO_PORT_05_PIN_01, |
| 72 | + .pin_cfg = ((uint32_t) IOPORT_CFG_PERIPHERAL_PIN | (uint32_t) IOPORT_PERIPHERAL_USB_FS)}, |
| 73 | + {.pin = LED1, .pin_cfg = ((uint32_t) IOPORT_CFG_PORT_DIRECTION_OUTPUT | (uint32_t) IOPORT_CFG_PORT_OUTPUT_LOW)}, |
| 74 | + {.pin = LED2, .pin_cfg = ((uint32_t) IOPORT_CFG_PORT_DIRECTION_OUTPUT | (uint32_t) IOPORT_CFG_PORT_OUTPUT_LOW)}, |
| 75 | + {.pin = LED3, .pin_cfg = ((uint32_t) IOPORT_CFG_PORT_DIRECTION_OUTPUT | (uint32_t) IOPORT_CFG_PORT_OUTPUT_LOW)}, |
| 76 | + {.pin = SW1, .pin_cfg = ((uint32_t) IOPORT_CFG_PORT_DIRECTION_INPUT)}, |
| 77 | + {.pin = SW2, .pin_cfg = ((uint32_t) IOPORT_CFG_PORT_DIRECTION_INPUT)}}; |
| 78 | + |
| 79 | +const ioport_cfg_t g_bsp_pin_cfg = { |
| 80 | + .number_of_pins = sizeof(g_bsp_pin_cfg_data) / sizeof(ioport_pin_cfg_t), |
| 81 | + .p_pin_cfg_data = &g_bsp_pin_cfg_data[0], |
| 82 | +}; |
| 83 | +ioport_instance_ctrl_t g_ioport_ctrl; |
| 84 | +const ioport_instance_t g_ioport = {.p_api = &g_ioport_on_ioport, .p_ctrl = &g_ioport_ctrl, .p_cfg = &g_bsp_pin_cfg}; |
| 85 | + |
| 86 | +//--------------------------------------------------------------------+ |
| 87 | +// Forward USB interrupt events to TinyUSB IRQ Handler |
| 88 | +//--------------------------------------------------------------------+ |
| 89 | +void usbfs_interrupt_handler(void) |
| 90 | +{ |
| 91 | + IRQn_Type irq = R_FSP_CurrentIrqGet(); |
| 92 | + R_BSP_IrqStatusClear(irq); |
| 93 | + |
| 94 | + tud_int_handler(0); |
| 95 | +} |
| 96 | +void usbfs_resume_handler(void) |
| 97 | +{ |
| 98 | + IRQn_Type irq = R_FSP_CurrentIrqGet(); |
| 99 | + R_BSP_IrqStatusClear(irq); |
| 100 | + |
| 101 | + tud_int_handler(0); |
| 102 | +} |
| 103 | +void usbfs_d0fifo_handler(void) |
| 104 | +{ |
| 105 | + IRQn_Type irq = R_FSP_CurrentIrqGet(); |
| 106 | + R_BSP_IrqStatusClear(irq); |
| 107 | + |
| 108 | + tud_int_handler(0); |
| 109 | +} |
| 110 | +void usbfs_d1fifo_handler(void) |
| 111 | +{ |
| 112 | + IRQn_Type irq = R_FSP_CurrentIrqGet(); |
| 113 | + R_BSP_IrqStatusClear(irq); |
| 114 | + |
| 115 | + tud_int_handler(0); |
| 116 | +} |
| 117 | + |
| 118 | +void board_init(void) |
| 119 | +{ |
| 120 | + /* Configure pins. */ |
| 121 | + R_IOPORT_Open(&g_ioport_ctrl, &g_bsp_pin_cfg); |
| 122 | + |
| 123 | + /* Enable USB_BASE */ |
| 124 | + R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK; |
| 125 | + R_MSTP->MSTPCRB &= ~(1U << 11U); |
| 126 | + R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK; |
| 127 | + |
| 128 | +#if CFG_TUSB_OS == OPT_OS_FREERTOS |
| 129 | + // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) |
| 130 | + NVIC_SetPriority(TU_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); |
| 131 | + NVIC_SetPriority(USBFS_RESUME_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); |
| 132 | + NVIC_SetPriority(USBFS_FIFO_0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); |
| 133 | + NVIC_SetPriority(USBFS_FIFO_1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); |
| 134 | +#endif |
| 135 | + |
| 136 | +#if CFG_TUSB_OS == OPT_OS_NONE |
| 137 | + /* Init systick */ |
| 138 | + SysTick_Config(SystemCoreClock / 1000); |
| 139 | +#endif |
| 140 | +} |
| 141 | + |
| 142 | +//--------------------------------------------------------------------+ |
| 143 | +// Board porting API |
| 144 | +//--------------------------------------------------------------------+ |
| 145 | + |
| 146 | +void board_led_write(bool state) |
| 147 | +{ |
| 148 | + R_IOPORT_PinWrite(&g_ioport_ctrl, LED1, state); |
| 149 | + R_IOPORT_PinWrite(&g_ioport_ctrl, LED2, state); |
| 150 | + R_IOPORT_PinWrite(&g_ioport_ctrl, LED3, state); |
| 151 | +} |
| 152 | + |
| 153 | +uint32_t board_button_read(void) |
| 154 | +{ |
| 155 | + bsp_io_level_t lvl; |
| 156 | + R_IOPORT_PinRead(&g_ioport_ctrl, SW1, &lvl); |
| 157 | + return lvl; |
| 158 | +} |
| 159 | + |
| 160 | +int board_uart_read(uint8_t *buf, int len) |
| 161 | +{ |
| 162 | + (void) buf; |
| 163 | + (void) len; |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +int board_uart_write(void const *buf, int len) |
| 168 | +{ |
| 169 | + (void) buf; |
| 170 | + (void) len; |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
| 174 | +#if CFG_TUSB_OS == OPT_OS_NONE |
| 175 | +volatile uint32_t system_ticks = 0; |
| 176 | +void SysTick_Handler(void) |
| 177 | +{ |
| 178 | + system_ticks++; |
| 179 | +} |
| 180 | + |
| 181 | +uint32_t board_millis(void) |
| 182 | +{ |
| 183 | + return system_ticks; |
| 184 | +} |
| 185 | +#else |
| 186 | +#endif |
| 187 | + |
| 188 | +int close(int fd) |
| 189 | +{ |
| 190 | + (void) fd; |
| 191 | + return -1; |
| 192 | +} |
| 193 | +int fstat(int fd, void *pstat) |
| 194 | +{ |
| 195 | + (void) fd; |
| 196 | + (void) pstat; |
| 197 | + return 0; |
| 198 | +} |
| 199 | +off_t lseek(int fd, off_t pos, int whence) |
| 200 | +{ |
| 201 | + (void) fd; |
| 202 | + (void) pos; |
| 203 | + (void) whence; |
| 204 | + return 0; |
| 205 | +} |
| 206 | +int isatty(int fd) |
| 207 | +{ |
| 208 | + (void) fd; |
| 209 | + return 1; |
| 210 | +} |
0 commit comments