From e67904f2c1341fe9fa2fc7903cba78a3a54a6724 Mon Sep 17 00:00:00 2001 From: Greg Steiert Date: Thu, 26 Aug 2021 16:18:36 -0700 Subject: [PATCH] initial commit for K32L2 support, compiles --- ports/k32l2/Makefile | 63 ++++++ ports/k32l2/board_flash.c | 136 +++++++++++++ ports/k32l2/boards.c | 245 ++++++++++++++++++++++++ ports/k32l2/boards.h | 42 ++++ ports/k32l2/boards/frdm-k32l2b/board.h | 89 +++++++++ ports/k32l2/boards/frdm-k32l2b/board.mk | 16 ++ ports/k32l2/tusb_config.h | 94 +++++++++ 7 files changed, 685 insertions(+) create mode 100644 ports/k32l2/Makefile create mode 100644 ports/k32l2/board_flash.c create mode 100644 ports/k32l2/boards.c create mode 100644 ports/k32l2/boards.h create mode 100644 ports/k32l2/boards/frdm-k32l2b/board.h create mode 100644 ports/k32l2/boards/frdm-k32l2b/board.mk create mode 100644 ports/k32l2/tusb_config.h diff --git a/ports/k32l2/Makefile b/ports/k32l2/Makefile new file mode 100644 index 0000000..6e763fb --- /dev/null +++ b/ports/k32l2/Makefile @@ -0,0 +1,63 @@ +# List of git submodules that is included as part of the UF2 version +GIT_SUBMODULES = nxp/mcux-sdk tinyusb + +include ../make.mk + +# inline port details +UF2_FAMILY_ID = 0x7f83e793 +CROSS_COMPILE = arm-none-eabi- + +SDK_DIR = lib/nxp/mcux-sdk +MCU_DIR = $(SDK_DIR)/devices/$(MCU) + +# Port Compiler Flags +CFLAGS += \ + -flto \ + -mthumb \ + -mabi=aapcs \ + -mcpu=cortex-m0plus + +# suppress warning caused by vendor mcu driver +CFLAGS += -Wno-error=cast-align -Wno-error=unused-parameter + +# Port source +SRC_C += \ + $(MCU_DIR)/system_$(MCU).c \ + $(MCU_DIR)/project_template/clock_config.c \ + $(MCU_DIR)/drivers/fsl_clock.c \ + $(SDK_DIR)/drivers/gpio/fsl_gpio.c \ + $(SDK_DIR)/drivers/flash/fsl_ftfx_controller.c \ + $(SDK_DIR)/drivers/flash/fsl_ftfx_flash.c \ + $(SDK_DIR)/drivers/flash/fsl_ftfx_cache.c \ + $(SDK_DIR)/drivers/flash/fsl_ftfx_flexnvm.c \ + $(SDK_DIR)/drivers/lpuart/fsl_lpuart.c \ + $(PORT_DIR)/boards.c \ + $(PORT_DIR)/board_flash.c + +SRC_C += \ + lib/tinyusb/src/portable/nxp/khci/dcd_khci.c + +SRC_S += $(MCU_DIR)/gcc/startup_$(MCU).S + +# Port include +INC += \ + $(TOP)/$(PORT_DIR) \ + $(TOP)/$(BOARD_DIR) \ + $(TOP)/$(SDK_DIR)/CMSIS/Include \ + $(TOP)/$(MCU_DIR) \ + $(TOP)/$(MCU_DIR)/drivers \ + $(TOP)/$(MCU_DIR)/project_template \ + $(TOP)/$(SDK_DIR)/drivers/smc \ + $(TOP)/$(SDK_DIR)/drivers/common \ + $(TOP)/$(SDK_DIR)/drivers/gpio \ + $(TOP)/$(SDK_DIR)/drivers/port \ + $(TOP)/$(SDK_DIR)/drivers/lpuart \ + $(TOP)/$(SDK_DIR)/drivers/flash + +LD_FILES ?= $(MCU_DIR)/gcc/$(LD_FNAME) + +include ../rules.mk + +#-------------- Self-update -------------- +self-update: + @echo "not implemented yet" diff --git a/ports/k32l2/board_flash.c b/ports/k32l2/board_flash.c new file mode 100644 index 0000000..334b01c --- /dev/null +++ b/ports/k32l2/board_flash.c @@ -0,0 +1,136 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Ha Thach for Adafruit Industries + * + * 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. + */ + +#include "board_api.h" +#include "fsl_flash.h" +#include "tusb.h" // for logging + +// FLASH +#define NO_CACHE 0xffffffff + +#define FLASH_PAGE_SIZE 1024 +#define FILESYSTEM_BLOCK_SIZE 256 + +static uint32_t _flash_page_addr = NO_CACHE; +static uint8_t _flash_cache[FLASH_PAGE_SIZE] __attribute__((aligned(4))); +/*! @brief Flash driver Structure */ +static flash_config_t _flash_config; +/*! @brief Flash cache driver Structure */ +//static ftfx_cache_config_t _cache_config; + +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ +void board_flash_init(void) +{ + status_t result; /* Return code from each flash driver function */ + /* Clean up Flash, Cache driver Structure*/ + memset(&_flash_config, 0, sizeof(flash_config_t)); + // memset(&_cache_config, 0, sizeof(ftfx_cache_config_t)); + + /* Setup flash driver structure for device and initialize variables. */ + result = FLASH_Init(&_flash_config); + if (kStatus_FTFx_Success != result) { + TU_LOG1("FLASH_Init failed"); + } + /* Setup flash cache driver structure for device and initialize variables. */ + // result = FTFx_CACHE_Init(&_cache_config); + // if (kStatus_FTFx_Success != result) + // { + // TU_LOG1("FTFx_CACHE_Init failed"); + // } + /* Get flash properties*/ + //FLASH_GetProperty(&_flash_config, kFLASH_PropertyPflash0BlockBaseAddr, &pflashBlockBase); + //FLASH_GetProperty(&_flash_config, kFLASH_PropertyPflash0TotalSize, &pflashTotalSize); + //FLASH_GetProperty(&_flash_config, kFLASH_PropertyPflash0SectorSize, &pflashSectorSize); + +} + +uint32_t board_flash_size(void) +{ + return BOARD_FLASH_SIZE; +} + +void board_flash_read(uint32_t addr, void* buffer, uint32_t len) +{ + memcpy(buffer, (void*) addr, len); +} + +void board_flash_flush(void) +{ + status_t result; +// uint32_t failedAddress, failedData; + + if ( _flash_page_addr == NO_CACHE ) return; + +// result = FLASH_VerifyProgram(&_flash_config, _flash_page_addr, FLASH_PAGE_SIZE, (const uint8_t *)_flash_cache, &failedAddress, &failedData); +// if (result != kStatus_Success) { + + // skip matching contents + if ( memcmp(_flash_cache, (void*) _flash_page_addr, FLASH_PAGE_SIZE) ) { + TU_LOG1("Erase and Write at address = 0x%08lX\r\n",_flash_page_addr); + result = FLASH_Erase(&_flash_config, _flash_page_addr, FLASH_PAGE_SIZE, kFLASH_ApiEraseKey); + if (kStatus_FTFx_Success != result) { + TU_LOG1("FLASH_Erase failed at address = 0x%08lX\r\n",_flash_page_addr); + } + result = FLASH_Program(&_flash_config, _flash_page_addr, _flash_cache, FLASH_PAGE_SIZE); + if (kStatus_FTFx_Success != result) { + TU_LOG1("FLASH_Program failed at address = 0x%08lX\r\n",_flash_page_addr); + } + } + + _flash_page_addr = NO_CACHE; +} + + +void board_flash_write (uint32_t addr, void const *data, uint32_t len) +{ + uint32_t newAddr = addr & ~(FLASH_PAGE_SIZE - 1); + // int32_t result; + + if (newAddr != _flash_page_addr) { + board_flash_flush(); + _flash_page_addr = newAddr; + board_flash_read(newAddr, _flash_cache, FLASH_PAGE_SIZE); + // result = FLASH_Read(&_flash_config, newAddr, _flash_cache, FLASH_PAGE_SIZE); + // if (result != kStatus_Success) { + // TU_LOG1("Flash read error at address = 0x%08lX\r\n", _flash_page_addr); + // } + } + memcpy(_flash_cache + (addr & (FLASH_PAGE_SIZE - 1)), data, len); +} + + +void board_flash_erase_app(void) +{ + // TODO implement later +} + +#ifdef TINYUF2_SELF_UPDATE +void board_self_update(const uint8_t * bootloader_bin, uint32_t bootloader_len) +{ + (void) bootloader_bin; + (void) bootloader_len; +} +#endif diff --git a/ports/k32l2/boards.c b/ports/k32l2/boards.c new file mode 100644 index 0000000..a7de4c1 --- /dev/null +++ b/ports/k32l2/boards.c @@ -0,0 +1,245 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Ha Thach for Adafruit Industries + * + * 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. + */ + +#include "board_api.h" +#include "fsl_gpio.h" +#include "fsl_port.h" +#include "fsl_clock.h" +#include "fsl_lpuart.h" +#include "clock_config.h" +#include "tusb.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ +void board_init(void) +{ + BOARD_BootClockRUN(); + + SystemCoreClockUpdate(); + + board_timer_stop(); + +#ifdef LED_PIN + CLOCK_EnableClock(LED_CLK_PORT); + gpio_pin_config_t led_config = { kGPIO_DigitalOutput, 0 }; + GPIO_PinInit(LED_GPIO, LED_PIN, &led_config); + PORT_SetPinMux(LED_PORT, LED_PIN, kPORT_MuxAsGpio); +#endif + +#ifdef BUTTON_PIN + CLOCK_EnableClock(BUTTON_CLK_PORT); + gpio_pin_config_t button_config = { kGPIO_DigitalInput, 0 }; + GPIO_PinInit(BUTTON_GPIO, BUTTON_PIN, &button_config); + const port_pin_config_t BUTTON_CFG = { + kPORT_PullUp, + kPORT_FastSlewRate, + kPORT_PassiveFilterDisable, + kPORT_LowDriveStrength, + kPORT_MuxAsGpio + }; + PORT_SetPinConfig(BUTTON_PORT, BUTTON_PIN, &BUTTON_CFG); +#endif + +#if defined(UART_DEV) && CFG_TUSB_DEBUG + CLOCK_SetLpuart0Clock(1); + CLOCK_EnableClock(UART_CLK_PORT); + PORT_SetPinMux(UART_PIN_PORT, UART_RX_PIN, UART_RX_MUX); + PORT_SetPinMux(UART_PIN_PORT, UART_TX_PIN, UART_TX_MUX); +#ifdef UART_SOPT_INIT + UART_SOPT_INIT; +#endif + lpuart_config_t uart_config; + LPUART_GetDefaultConfig(&uart_config); + uart_config.baudRate_Bps = BOARD_UART_BAUDRATE; + uart_config.enableTx = true; + uart_config.enableRx = true; + LPUART_Init(UART_DEV, &uart_config, CLOCK_GetFreq(kCLOCK_McgIrc48MClk)); + TU_LOG2("UART Configured\r\n"); +#endif + +} + +void board_usb_init(void) +{ + CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U); +} + +void board_dfu_init(void) +{ + board_usb_init(); +} + +void board_reset(void) +{ + NVIC_SystemReset(); +} + +void board_dfu_complete(void) +{ + // Mostly reset + NVIC_SystemReset(); +} + +bool board_app_valid(void) +{ + volatile uint32_t const * app_vector = (volatile uint32_t const*) BOARD_FLASH_APP_START; + + // 1st word is stack pointer (should be in SRAM region) + + // 2nd word is App entry point (reset) + if (app_vector[1] < BOARD_FLASH_APP_START || app_vector[1] > BOARD_FLASH_APP_START + BOARD_FLASH_SIZE) { + return false; + } + + return true; +} + +void board_app_jump(void) +{ + // Create the function call to the user application. + // Static variables are needed since changed the stack pointer out from under the compiler + // we need to ensure the values we are using are not stored on the previous stack + static uint32_t stack_pointer; + static uint32_t app_entry; + + uint32_t const * app_vector = (uint32_t const*) BOARD_FLASH_APP_START; + stack_pointer = app_vector[0]; + app_entry = app_vector[1]; + + // TODO protect bootloader region + + /* switch exception handlers to the application */ + SCB->VTOR = (uint32_t) BOARD_FLASH_APP_START; + + // Set stack pointer + __set_MSP(stack_pointer); + __set_PSP(stack_pointer); + + // Jump to Application Entry + asm("bx %0" ::"r"(app_entry)); +} + +uint8_t board_usb_get_serial(uint8_t serial_id[16]) +{ + uint32_t wr = SIM->UIDL; + serial_id[0] = wr & 0xFF; + wr >>= 8; + serial_id[1] = wr & 0xFF; + wr >>= 8; + serial_id[2] = wr & 0xFF; + wr >>= 8; + serial_id[3] = wr & 0xFF; + wr >>= SIM->UIDML; + serial_id[4] = wr & 0xFF; + wr >>= 8; + serial_id[5] = wr & 0xFF; + wr >>= 8; + serial_id[6] = wr & 0xFF; + wr >>= 8; + serial_id[7] = wr & 0xFF; + wr >>= SIM->UIDMH; + serial_id[8] = wr & 0xFF; + wr >>= 8; + serial_id[9] = wr & 0xFF; + return 10; +} + +//--------------------------------------------------------------------+ +// LED pattern +//--------------------------------------------------------------------+ + +void board_led_write(uint32_t state) +{ + GPIO_PinWrite(LED_GPIO, LED_PIN, ((state)? LED_STATE_ON : (1-LED_STATE_ON))); +} + +void board_rgb_write(uint8_t const rgb[]) +{ + (void) rgb; +} + +//--------------------------------------------------------------------+ +// Timer +//--------------------------------------------------------------------+ + +void board_timer_start(uint32_t ms) +{ + SysTick_Config( (SystemCoreClock/1000) * ms ); +} + +void board_timer_stop(void) +{ + //SysTick->CTRL = 0; + SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; +} + +void SysTick_Handler (void) +{ + board_timer_handler(); +} + + +int board_uart_write(void const * buf, int len) +{ +#ifdef UART_DEV + uint8_t const* buf8 = (uint8_t const*) buf; + int count = 0; + + while(count < len) + { + if (LPUART_GetStatusFlags(UART_DEV) & kLPUART_TxDataRegEmptyFlag) + { + LPUART_WriteByte(UART_DEV, *buf8++); + count++; + } + } + + return len; + +#else + + (void) buf; (void) len; + return 0; +#endif +} + +// optional API, not included in board_api.h +int board_uart_read(uint8_t* buf, int len) +{ + (void) buf; (void) len; + return 0; +} + +//--------------------------------------------------------------------+ +// USB Interrupt Handler +//--------------------------------------------------------------------+ +#ifndef BUILD_NO_TINYUSB + +void USB0_IRQHandler(void) +{ + tud_int_handler(0); +} + +#endif diff --git a/ports/k32l2/boards.h b/ports/k32l2/boards.h new file mode 100644 index 0000000..2b64bcf --- /dev/null +++ b/ports/k32l2/boards.h @@ -0,0 +1,42 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020 Ha Thach (tinyusb.org) for Adafruit Industries + * + * 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. + */ + +#ifndef BOARDS_H_ +#define BOARDS_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +#include "board.h" + +#define TINYUF2_DFU_DOUBLE_TAP 0 +#define BOARD_FLASH_APP_START 0x8000 +#define BOARD_FLASH_SIZE (BOARD_FLASH_TOTAL - BOARD_FLASH_APP_START) + +#ifdef __cplusplus + } +#endif + +#endif /* BOARDS_H_ */ diff --git a/ports/k32l2/boards/frdm-k32l2b/board.h b/ports/k32l2/boards/frdm-k32l2b/board.h new file mode 100644 index 0000000..fa659f6 --- /dev/null +++ b/ports/k32l2/boards/frdm-k32l2b/board.h @@ -0,0 +1,89 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Ha Thach for Adafruit Industries + * + * 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. + */ + +#ifndef BOARD_H_ +#define BOARD_H_ + +//--------------------------------------------------------------------+ +// Flash +//--------------------------------------------------------------------+ + +#define BOARD_FLASH_TOTAL 0x40000U + +//--------------------------------------------------------------------+ +// Button +//--------------------------------------------------------------------+ + +#define BUTTON_GPIO GPIOC +#define BUTTON_PORT PORTC +#define BUTTON_CLK_PORT kCLOCK_PortC +#define BUTTON_PIN 3 +#define BUTTON_STATE_ACTIVE 0 + +//--------------------------------------------------------------------+ +// LED +//--------------------------------------------------------------------+ + +#define LED_GPIO GPIOD +#define LED_PORT PORTD +#define LED_CLK_PORT kCLOCK_PortD +#define LED_PIN 5 +#define LED_STATE_ON 0 + +//--------------------------------------------------------------------+ +// Neopixel +//--------------------------------------------------------------------+ + +// Number of neopixels +#define NEOPIXEL_NUMBER 0 + +//--------------------------------------------------------------------+ +// USB UF2 +//--------------------------------------------------------------------+ + +#define USB_VID 0x1FC9 +#define USB_PID 0x009E +#define USB_MANUFACTURER "NXP" +#define USB_PRODUCT "FRDM-K32L2B3" + +#define UF2_PRODUCT_NAME USB_MANUFACTURER " " USB_PRODUCT +#define UF2_BOARD_ID "FRDM-K32L2B3" +#define UF2_VOLUME_LABEL "K32L2B3BOOT" +#define UF2_INDEX_URL "https://www.nxp.com/FRDM-K32L2B3" + +//--------------------------------------------------------------------+ +// UART +//--------------------------------------------------------------------+ + +#define UART_DEV LPUART0 +#define UART_CLK_PORT kCLOCK_PortA +#define UART_PIN_PORT PORTA +#define UART_TX_PIN 2 +#define UART_TX_MUX kPORT_MuxAlt2 +#define UART_RX_PIN 1 +#define UART_RX_MUX kPORT_MuxAlt2 +#define UART_SOPT_INIT \ + SIM->SOPT5 &= ~(SIM_SOPT5_LPUART0TXSRC_MASK | SIM_SOPT5_LPUART0RXSRC_MASK) + +#endif diff --git a/ports/k32l2/boards/frdm-k32l2b/board.mk b/ports/k32l2/boards/frdm-k32l2b/board.mk new file mode 100644 index 0000000..f6ea462 --- /dev/null +++ b/ports/k32l2/boards/frdm-k32l2b/board.mk @@ -0,0 +1,16 @@ +MCU = K32L2B31A + +CFLAGS += \ + -DCPU_K32L2B31VLH0A \ + -DCFG_TUSB_MCU=OPT_MCU_K32L2BXX + +SRC_S += + +LD_FNAME = K32L2B31xxxxA_flash.ld + +# For flash-pyocd target +PYOCD_TARGET = K32L2B + +# flash using pyocd +flash: flash-pyocd +erase: erase-jlink diff --git a/ports/k32l2/tusb_config.h b/ports/k32l2/tusb_config.h new file mode 100644 index 0000000..0822b32 --- /dev/null +++ b/ports/k32l2/tusb_config.h @@ -0,0 +1,94 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * + * 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. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- + +#ifndef CFG_TUSB_MCU +#error CFG_TUSB_MCU must be defined in board.mk +#endif + +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#define CFG_TUSB_OS OPT_OS_NONE + +// can be defined by compiler in DEBUG build +#ifndef CFG_TUSB_DEBUG + #define CFG_TUSB_DEBUG 0 +#endif + +/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. + * Tinyusb use follows macros to declare transferring memory so that they can be put + * into those specific section. + * e.g + * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) + * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) + */ +#ifndef CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_SECTION +#endif + +#ifndef CFG_TUSB_MEM_ALIGN +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) +#endif + +//-------------------------------------------------------------------- +// DEVICE CONFIGURATION +//-------------------------------------------------------------------- + +#ifndef CFG_TUD_ENDPOINT0_SIZE +#define CFG_TUD_ENDPOINT0_SIZE 64 +#endif + +//------------- CLASS -------------// +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 1 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 + +// MSC Buffer size of Device Mass storage +#define CFG_TUD_MSC_BUFSIZE 512 + +// HID buffer size Should be sufficient to hold ID (if any) + Data +#define CFG_TUD_HID_BUFSIZE 64 + +// Vendor FIFO size of TX and RX +// If not configured vendor endpoints will not be buffered +#define CFG_TUD_VENDOR_RX_BUFSIZE 64 +#define CFG_TUD_VENDOR_TX_BUFSIZE 64 + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_CONFIG_H_ */