diff --git a/examples/make.mk b/examples/make.mk index 699cc43d6..552483965 100644 --- a/examples/make.mk +++ b/examples/make.mk @@ -46,6 +46,7 @@ BUILD = _build/build-$(BOARD) include $(TOP)/hw/bsp/$(BOARD)/board.mk # Include all source C in board folder +SRC_C += hw/bsp/board.c SRC_C += $(subst $(TOP)/,,$(wildcard $(TOP)/hw/bsp/$(BOARD)/*.c)) # Compiler Flags diff --git a/hw/bsp/board.c b/hw/bsp/board.c new file mode 100644 index 000000000..16e713de5 --- /dev/null +++ b/hw/bsp/board.c @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018, hathach (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 "board.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +//#if CFG_PRINTF_TARGET == PRINTF_TARGET_UART +// #define retarget_getchar board_uart_getchar +// #define retarget_putchar board_uart_putchar +//#elif CFG_PRINTF_TARGET == PRINTF_TARGET_SWO +// volatile int32_t ITM_RxBuffer; // keil variable to read from SWO +// #define retarget_getchar ITM_ReceiveChar +// #define retarget_putchar ITM_SendChar +//#else +// #error Target is not implemented yet +//#endif + +//------------- IMPLEMENTATION -------------// + +// newlib read()/write() retarget + +TU_ATTR_USED int _write (int fhdl, const void *buf, size_t count) +{ + (void) fhdl; + return board_uart_write(buf, count); +} + +TU_ATTR_USED int _read (int fhdl, char *buf, size_t count) +{ + (void) fhdl; + return board_uart_read((uint8_t*) buf, count); +} diff --git a/hw/bsp/board.h b/hw/bsp/board.h index 15e3a2511..e0455b80d 100644 --- a/hw/bsp/board.h +++ b/hw/bsp/board.h @@ -42,7 +42,7 @@ #include "tusb.h" -#define CFG_UART_BAUDRATE 115200 +#define CFG_BOARD_UART_BAUDRATE 115200 //--------------------------------------------------------------------+ // Board Porting API diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index 2164d3e09..df80815d8 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -134,7 +134,7 @@ void board_init(void) UART_CFG_Type UARTConfigStruct; UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE; + UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; UARTConfigStruct.Clock_Speed = 0; UART_Init(BOARD_UART_PORT, &UARTConfigStruct); diff --git a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c index bc6b1da7e..13b8fc9ff 100644 --- a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c +++ b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c @@ -116,7 +116,7 @@ void board_init(void) UART_CFG_Type UARTConfigStruct; UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE; + UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; UART_Init(BOARD_UART_PORT, &UARTConfigStruct); UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit diff --git a/hw/bsp/mbed1768/mbed1768.c b/hw/bsp/mbed1768/mbed1768.c index 05fb4ef35..647334a42 100644 --- a/hw/bsp/mbed1768/mbed1768.c +++ b/hw/bsp/mbed1768/mbed1768.c @@ -108,7 +108,7 @@ void board_init(void) UART_CFG_Type UARTConfigStruct; UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE; + UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; UART_Init(BOARD_UART_PORT, &UARTConfigStruct); UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index 6f1ef4244..44f5b11bb 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -118,7 +118,7 @@ void board_init(void) UART_CFG_Type UARTConfigStruct; UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE; + UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; UARTConfigStruct.Clock_Speed = 0; UART_Init(BOARD_UART_PORT, &UARTConfigStruct); diff --git a/hw/bsp/ngx4330/ngx4330.c b/hw/bsp/ngx4330/ngx4330.c index 0e39ea711..609b4dac6 100644 --- a/hw/bsp/ngx4330/ngx4330.c +++ b/hw/bsp/ngx4330/ngx4330.c @@ -132,7 +132,7 @@ void board_init(void) UART_CFG_Type UARTConfigStruct; UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE; + UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; UARTConfigStruct.Clock_Speed = 0; UART_Init(BOARD_UART_PORT, &UARTConfigStruct); diff --git a/hw/bsp/pca10056/board.mk b/hw/bsp/pca10056/board.mk index c0616abee..3cd3e51c6 100644 --- a/hw/bsp/pca10056/board.mk +++ b/hw/bsp/pca10056/board.mk @@ -24,6 +24,7 @@ LDFLAGS += -L$(TOP)/hw/mcu/nordic/nrfx/mdk SRC_C += \ hw/mcu/nordic/nrfx/drivers/src/nrfx_power.c \ + hw/mcu/nordic/nrfx/drivers/src/nrfx_uart.c \ hw/mcu/nordic/nrfx/mdk/system_nrf52840.c \ INC += \ diff --git a/hw/bsp/pca10056/pca10056.c b/hw/bsp/pca10056/pca10056.c index 57426732c..78d9cc2cd 100644 --- a/hw/bsp/pca10056/pca10056.c +++ b/hw/bsp/pca10056/pca10056.c @@ -29,6 +29,7 @@ #include "nrfx.h" #include "nrfx/hal/nrf_gpio.h" #include "nrfx/drivers/include/nrfx_power.h" +#include "nrfx/drivers/include/nrfx_uart.h" #ifdef SOFTDEVICE_PRESENT #include "nrf_sdm.h" @@ -44,6 +45,12 @@ #define BUTTON_PIN 11 #define BUTTON_STATE_ACTIVE 0 +#define UART_RX_PIN 8 +#define UART_TX_PIN 6 + +static nrfx_uart_t _uart_id = NRFX_UART_INSTANCE(0); +//static void uart_handler(nrfx_uart_event_t const * p_event, void* p_context); + // tinyusb function that handles power event (detected, ready, removed) // We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. extern void tusb_hal_nrf_power_event(uint32_t event); @@ -69,6 +76,21 @@ void board_init(void) SysTick_Config(SystemCoreClock/1000); #endif + // UART + nrfx_uart_config_t uart_cfg = + { + .pseltxd = UART_TX_PIN, + .pselrxd = UART_RX_PIN, + .pselcts = NRF_UART_PSEL_DISCONNECTED, + .pselrts = NRF_UART_PSEL_DISCONNECTED, + .p_context = NULL, + .hwfc = NRF_UART_HWFC_DISABLED, + .parity = NRF_UART_PARITY_EXCLUDED, + .baudrate = NRF_UART_BAUDRATE_115200 // CFG_BOARD_UART_BAUDRATE + }; + + nrfx_uart_init(&_uart_id, &uart_cfg, NULL); //uart_handler); + #if TUSB_OPT_DEVICE_ENABLED // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice // 2 is highest for application @@ -124,6 +146,11 @@ uint32_t board_button_read(void) return BUTTON_STATE_ACTIVE == nrf_gpio_pin_read(BUTTON_PIN); } +//static void uart_handler(nrfx_uart_event_t const * p_event, void* p_context) +//{ +// +//} + int board_uart_read(uint8_t* buf, int len) { (void) buf; @@ -133,9 +160,7 @@ int board_uart_read(uint8_t* buf, int len) int board_uart_write(void const * buf, int len) { - (void) buf; - (void) len; - return 0; + return (NRFX_SUCCESS == nrfx_uart_tx(&_uart_id, (uint8_t const*) buf, (size_t) len)) ? len : 0; } #if CFG_TUSB_OS == OPT_OS_NONE diff --git a/hw/bsp/printf_retarget.c b/hw/bsp/printf_retarget.c deleted file mode 100644 index 707a1e099..000000000 --- a/hw/bsp/printf_retarget.c +++ /dev/null @@ -1,183 +0,0 @@ -/* - * 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 "board.h" - -#if CFG_PRINTF_TARGET != PRINTF_TARGET_SEMIHOST - -#if CFG_PRINTF_TARGET == PRINTF_TARGET_UART - #define retarget_getchar() board_uart_getchar() - #define retarget_putchar(c) board_uart_putchar(c); -#elif CFG_PRINTF_TARGET == PRINTF_TARGET_SWO - volatile int32_t ITM_RxBuffer; // keil variable to read from SWO - #define retarget_getchar() ITM_ReceiveChar() - #define retarget_putchar(c) ITM_SendChar(c) -#else - #error Target is not implemented yet -#endif - -//--------------------------------------------------------------------+ -// LPCXPRESSO / RED SUITE -//--------------------------------------------------------------------+ -#if defined __CODE_RED - -#if CFG_PRINTF_TARGET == PRINTF_TARGET_SWO - #error author does not know how to retarget SWO with lpcxpresso/red-suite -#endif - -// Called by bottom level of printf routine within RedLib C library to write -// a character. With the default semihosting stub, this would write the character -// to the debugger console window . But this version writes -// the character to the UART. -int __sys_write (int iFileHandle, char *buf, int length) -{ - (void) iFileHandle; - - for (int i=0; i -#include -#include "__libc.h" - -int printf(const char *fmt,...) { - char buffer[128]; - va_list args; - va_start (args, fmt); - int n = vsnprintf(buffer, sizeof(buffer), fmt, args); - - for(int i=0; i < n; i++) - { - retarget_putchar( buffer[i] ); - } - - va_end(args); - return n; -} - -int __putchar(int ch, __printf_tag_ptr ctx) -{ - (void)ctx; - retarget_putchar( (uint8_t) ch ); - return 1; -} - -int __getchar() -{ - return retarget_getchar(); -} - -//--------------------------------------------------------------------+ -// KEIL -//--------------------------------------------------------------------+ -#elif defined __CC_ARM // keil - -struct __FILE { - uint32_t handle; -}; - -void _ttywrch(int ch) -{ - if ( ch == '\n' ) retarget_putchar('\r'); - - retarget_putchar(ch); -} - -int fgetc(FILE *f) -{ - return retarget_getchar(); -} - -int fputc(int ch, FILE *f) -{ - _ttywrch(ch); - return ch; -} - -//--------------------------------------------------------------------+ -// IAR -//--------------------------------------------------------------------+ -#elif defined __ICCARM__ // TODO could not able to retarget to UART with IAR - -#if CFG_PRINTF_TARGET == PRINTF_TARGET_UART -#include - -size_t __write(int handle, const unsigned char *buf, size_t length) -{ - /* Check for the command to flush all handles */ - if (handle == -1) return 0; - - /* Check for stdout and stderr (only necessary if FILE descriptors are enabled.) */ - if (handle != 1 && handle != 2) return -1; - - for (size_t i=0; i