1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-24 16:55:36 +03:00

PICO: Adding tinyUSB library

This commit is contained in:
blckmn 2025-01-12 07:56:01 +11:00
parent 1774693395
commit 0109f50909
1507 changed files with 299535 additions and 0 deletions

View file

@ -0,0 +1,80 @@
/*
* FreeRTOS Kernel V10.0.0
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* 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. If you wish to use our Amazon
* FreeRTOS name, please do so in a fair use way that does not cause confusion.
*
* 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.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html
*----------------------------------------------------------*/
// skip if included from IAR assembler
#ifndef __IASMARM__
#include "msp430.h"
#endif
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 1
#define configUSE_TICK_HOOK 0
#define configUSE_MALLOC_FAILED_HOOK 0
#define configCPU_CLOCK_HZ ( ( unsigned long ) 7995392 ) /* Clock setup from main.c in the demo application. */
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES ( 4 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 50 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 1700 ) )
#define configMAX_TASK_NAME_LEN ( 8 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 1
#define configIDLE_SHOULD_YIELD 1
#define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 0
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Set the following definitions to 1 to include the API function, or zero to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 0
#define INCLUDE_uxTaskPriorityGet 0
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 0
#define INCLUDE_vTaskSuspend 0
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#endif /* FREERTOS_CONFIG_H */

View file

@ -0,0 +1,9 @@
set(MCU_VARIANT msp430f5529)
set(LD_FILE_GNU ${SDK_DIR}/msp430f5529.ld)
function(update_board TARGET)
target_compile_definitions(${TARGET} INTERFACE
__MSP430F5529__
)
endfunction()

View file

@ -0,0 +1,46 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021, 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.
*
* This file is part of the TinyUSB stack.
*/
#ifndef BOARD_H_
#define BOARD_H_
#ifdef __cplusplus
extern "C" {
#endif
#define LED_PORT P1OUT
#define LED_PIN BIT0
#define LED_STATE_ON 1
#define BUTTON_PORT P1IN
#define BUTTON_PIN BIT1
#define BUTTON_STATE_ACTIVE 0
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,4 @@
CFLAGS += \
-D__MSP430F5529__ \
LD_FILE = ${SDK_DIR}/msp430f5529.ld

View file

@ -0,0 +1,218 @@
/*
* 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.
*
* This file is part of the TinyUSB stack.
*/
#include "bsp/board_api.h"
#include "board.h"
#include "msp430.h"
//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void __attribute__ ((interrupt(USB_UBM_VECTOR))) USB_UBM_ISR(void)
{
tud_int_handler(0);
}
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
uint32_t cnt = 0;
static void SystemClock_Config(void)
{
WDTCTL = WDTPW + WDTHOLD; // Disable watchdog.
// Increase VCore to level 2- required for 16 MHz operation on this MCU.
PMMCTL0 = PMMPW + PMMCOREV_2;
UCSCTL3 = SELREF__XT2CLK; // FLL is fed by XT2.
// XT1 used for ACLK (default- not used in this demo)
P5SEL |= BIT4; // Required to enable XT1
// Loop until XT1 fault flag is cleared.
do
{
UCSCTL7 &= ~XT1LFOFFG;
}while(UCSCTL7 & XT1LFOFFG);
// XT2 is 4 MHz an external oscillator, use PLL to boost to 16 MHz.
P5SEL |= BIT2; // Required to enable XT2.
// Loop until XT2 fault flag is cleared
do
{
UCSCTL7 &= ~XT2OFFG;
}while(UCSCTL7 & XT2OFFG);
// Kickstart the DCO into the correct frequency range, otherwise a
// fault will occur.
// FIXME: DCORSEL_6 should work according to datasheet params, but generates
// a fault. I am not sure why it faults.
UCSCTL1 = DCORSEL_7;
UCSCTL2 = FLLD_2 + 3; // DCO freq = D * (N + 1) * (FLLREFCLK / n)
// DCOCLKDIV freq = (N + 1) * (FLLREFCLK / n)
// N = 3, D = 2, thus DCO freq = 32 MHz.
// MCLK configured for 16 MHz using XT2.
// SMCLK configured for 8 MHz using XT2.
UCSCTL4 |= SELM__DCOCLKDIV + SELS__DCOCLKDIV;
UCSCTL5 |= DIVM__16 + DIVS__2;
// Now wait till everything's stabilized.
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
SFRIFG1 &= ~OFIFG;
}while(SFRIFG1 & OFIFG);
// Configure Timer A to use SMCLK as a source. Count 1000 ticks at 1 MHz.
TA0CCTL0 |= CCIE;
TA0CCR0 = 999; // 1000 ticks.
TA0CTL |= TASSEL_2 + ID_3 + MC__UP; // Use SMCLK, divide by 8, start timer.
// Initialize USB power and PLL.
USBKEYPID = USBKEY;
// VUSB enabled automatically.
// Wait two milliseconds to stabilize, per manual recommendation.
uint32_t ms_elapsed = board_millis();
do
{
while((board_millis() - ms_elapsed) < 2);
}while(!(USBPWRCTL & USBBGVBV));
// USB uses XT2 (4 MHz) directly. Enable the PLL.
USBPLLDIVB |= USBPLL_SETCLK_4_0;
USBPLLCTL |= (UPFDEN | UPLLEN);
// Wait until PLL locks. Check every 2ms, per manual.
ms_elapsed = board_millis();
do
{
USBPLLIR &= ~USBOOLIFG;
while((board_millis() - ms_elapsed) < 2);
}while(USBPLLIR & USBOOLIFG);
USBKEYPID = 0;
}
uint32_t wait = 0;
void board_init(void)
{
__bis_SR_register(GIE); // Enable interrupts.
SystemClock_Config();
// Enable basic I/O.
P1DIR |= LED_PIN; // LED output.
P1REN |= BUTTON_PIN; // Internal resistor enable.
P1OUT |= BUTTON_PIN; // Pullup.
// Enable the backchannel UART (115200)
P4DIR |= BIT5;
P4SEL |= (BIT5 | BIT4);
UCA1CTL1 |= (UCSSEL__SMCLK | UCSWRST); // Hold in reset, use SMCLK.
UCA1BRW = 4;
UCA1MCTL |= (UCBRF_3 | UCBRS_5 | UCOS16); // Overampling mode, 115200 baud.
// Copied from manual.
UCA1CTL1 &= ~UCSWRST;
// Set up USB pins.
USBKEYPID = USBKEY;
USBPHYCTL |= PUSEL; // Convert USB D+/D- pins to USB functionality.
USBKEYPID = 0;
}
//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+
void board_led_write(bool state)
{
if(state)
{
LED_PORT |= LED_PIN;
}
else
{
LED_PORT &= ~LED_PIN;
}
}
uint32_t board_button_read(void)
{
return ((P1IN & BIT1) >> 1) == BUTTON_STATE_ACTIVE;
}
int board_uart_read(uint8_t * buf, int len)
{
for(int i = 0; i < len; i++)
{
// Wait until something to receive (cleared by reading buffer).
while(!(UCA1IFG & UCRXIFG));
buf[i] = UCA1RXBUF;
}
return len;
}
int board_uart_write(void const * buf, int len)
{
const char * char_buf = (const char *) buf;
for(int i = 0; i < len; i++)
{
// Wait until TX buffer is empty (cleared by writing buffer).
while(!(UCA1IFG & UCTXIFG));
UCA1TXBUF = char_buf[i];
}
return len;
}
#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) TIMER0_A0_ISR (void)
{
system_ticks++;
// TAxCCR0 CCIFG resets itself as soon as interrupt is invoked.
}
uint32_t board_millis(void)
{
uint32_t systick_mirror;
// 32-bit update is not atomic on MSP430. We can read the bottom 16-bits,
// an interrupt occurs, updates _all_ 32 bits, and then we return a
// garbage value. And I've seen it happen!
TA0CCTL0 &= ~CCIE;
systick_mirror = system_ticks;
TA0CCTL0 |= CCIE;
return systick_mirror;
}
#endif

View file

@ -0,0 +1,84 @@
include_guard()
set(SDK_DIR ${TOP}/hw/mcu/ti/msp430/msp430-gcc-support-files/include)
# include board specific
include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake)
# toolchain set up
set(CMAKE_SYSTEM_PROCESSOR msp430 CACHE INTERNAL "System Processor")
set(CMAKE_TOOLCHAIN_FILE ${TOP}/examples/build_system/cmake/toolchain/msp430_${TOOLCHAIN}.cmake)
set(FAMILY_MCUS MSP430x5xx CACHE INTERNAL "")
#------------------------------------
# BOARD_TARGET
#------------------------------------
# only need to be built ONCE for all examples
function(add_board_target BOARD_TARGET)
if (NOT TARGET ${BOARD_TARGET})
add_library(${BOARD_TARGET} INTERFACE)
target_compile_definitions(${BOARD_TARGET} INTERFACE
CFG_TUD_ENDPOINT0_SIZE=8
CFG_EXAMPLE_VIDEO_READONLY
CFG_EXAMPLE_MSC_READONLY
)
target_include_directories(${BOARD_TARGET} INTERFACE
${CMAKE_CURRENT_FUNCTION_LIST_DIR}
${SDK_DIR}
)
update_board(${BOARD_TARGET})
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_link_options(${BOARD_TARGET} INTERFACE
"LINKER:--script=${LD_FILE_GNU}"
-L${SDK_DIR}
)
elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR")
target_link_options(${BOARD_TARGET} INTERFACE
"LINKER:--config=${LD_FILE_IAR}"
)
endif ()
endif ()
endfunction()
#------------------------------------
# Functions
#------------------------------------
function(family_configure_example TARGET RTOS)
family_configure_common(${TARGET} ${RTOS})
# Board target
add_board_target(board_${BOARD})
#---------- Port Specific ----------
# These files are built for each example since it depends on example's tusb_config.h
target_sources(${TARGET} PUBLIC
# BSP
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../board.c
)
target_include_directories(${TARGET} PUBLIC
# family, hw, board
${CMAKE_CURRENT_FUNCTION_LIST_DIR}
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../../
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/boards/${BOARD}
)
# Add TinyUSB target and port source
family_add_tinyusb(${TARGET} OPT_MCU_MSP430x5xx ${RTOS})
target_sources(${TARGET}-tinyusb PUBLIC
${TOP}/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c
)
target_link_libraries(${TARGET}-tinyusb PUBLIC board_${BOARD})
# Link dependencies
target_link_libraries(${TARGET} PUBLIC board_${BOARD} ${TARGET}-tinyusb)
# Flashing
family_add_bin_hex(${TARGET})
family_flash_msp430flasher(${TARGET})
endfunction()

View file

@ -0,0 +1,35 @@
CROSS_COMPILE = msp430-elf-
DEPS_SUBMODULES += hw/mcu/ti
SKIP_NANOLIB = 1
SDK_DIR = hw/mcu/ti/msp430/msp430-gcc-support-files/include
include $(TOP)/$(BOARD_PATH)/board.mk
CFLAGS += \
-DCFG_TUSB_MCU=OPT_MCU_MSP430x5xx \
-DCFG_EXAMPLE_MSC_READONLY \
-DCFG_TUD_ENDPOINT0_SIZE=8
LDFLAGS += -L${TOP}/${SDK_DIR}
SRC_C += src/portable/ti/msp430x5xx/dcd_msp430x5xx.c
INC += \
${TOP}/${SDK_DIR} \
$(TOP)/$(BOARD_PATH)
# export for libmsp430.so to same installation
ifneq ($(OS),Windows_NT)
export LD_LIBRARY_PATH=$(dir $(shell which MSP430Flasher))
endif
# flash target using TI MSP430-Flasher
# http://www.ti.com/tool/MSP430-FLASHER
# Please add its installation dir to PATH
flash: $(BUILD)/$(PROJECT).hex
MSP430Flasher -w $< -z [VCC]
# flash target using mspdebug.
flash-mspdebug: $(BUILD)/$(PROJECT).elf
$(MSPDEBUG) tilib "prog $<" --allow-fw-update