adding log support with uart

started with pca10056
This commit is contained in:
hathach 2019-10-12 00:00:08 +07:00
parent d9ef34276b
commit 195d0f5a14
13 changed files with 233 additions and 195 deletions

View File

@ -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

58
hw/bsp/board.c Normal file
View File

@ -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);
}

View File

@ -42,7 +42,7 @@
#include "tusb.h"
#define CFG_UART_BAUDRATE 115200
#define CFG_BOARD_UART_BAUDRATE 115200
//--------------------------------------------------------------------+
// Board Porting API

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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 += \

View File

@ -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

View File

@ -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<length; i++)
{
if (buf[i] == '\n') retarget_putchar('\r');
retarget_putchar( buf[i] );
}
return length;
}
// Called by bottom level of scanf routine within RedLib C library to read
// a character. With the default semihosting stub, this would read the character
// from the debugger console window (which acts as stdin). But this version reads
// the character from the UART.
int __sys_readc (void)
{
return (int) retarget_getchar();
}
#elif defined __SES_ARM && 0
#include <stdarg.h>
#include <stdio.h>
#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 <stddef.h>
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<length; i++)
{
if (buf[i] == '\n') retarget_putchar('\r');
retarget_putchar( buf[i] );
}
return length;
}
size_t __read(int handle, unsigned char *buf, size_t bufSize)
{
/* Check for stdin (only necessary if FILE descriptors are enabled) */
if (handle != 0) return -1;
size_t i;
for (i=0; i<bufSize; i++)
{
int8_t ch = board_uart_getchar();
if (ch == -1) break;
buf[i] = ch;
}
return i;
}
#endif
#endif
#endif // CFG_PRINTF_TARGET != PRINTF_TARGET_SEMIHOST

135
hw/mcu/nordic/nrfx_log.h Normal file
View File

@ -0,0 +1,135 @@
/*
* Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef NRFX_LOG_H__
#define NRFX_LOG_H__
// THIS IS A TEMPLATE FILE.
// It should be copied to a suitable location within the host environment into
// which nrfx is integrated, and the following macros should be provided with
// appropriate implementations.
// And this comment should be removed from the customized file.
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup nrfx_log nrfx_log.h
* @{
* @ingroup nrfx
*
* @brief This file contains macros that should be implemented according to
* the needs of the host environment into which @em nrfx is integrated.
*/
/**
* @brief Macro for logging a message with the severity level ERROR.
*
* @param format printf-style format string, optionally followed by arguments
* to be formatted and inserted in the resulting string.
*/
#define NRFX_LOG_ERROR(format, ...)
/**
* @brief Macro for logging a message with the severity level WARNING.
*
* @param format printf-style format string, optionally followed by arguments
* to be formatted and inserted in the resulting string.
*/
#define NRFX_LOG_WARNING(format, ...)
/**
* @brief Macro for logging a message with the severity level INFO.
*
* @param format printf-style format string, optionally followed by arguments
* to be formatted and inserted in the resulting string.
*/
#define NRFX_LOG_INFO(format, ...)
/**
* @brief Macro for logging a message with the severity level DEBUG.
*
* @param format printf-style format string, optionally followed by arguments
* to be formatted and inserted in the resulting string.
*/
#define NRFX_LOG_DEBUG(format, ...)
/**
* @brief Macro for logging a memory dump with the severity level ERROR.
*
* @param[in] p_memory Pointer to the memory region to be dumped.
* @param[in] length Length of the memory region in bytes.
*/
#define NRFX_LOG_HEXDUMP_ERROR(p_memory, length)
/**
* @brief Macro for logging a memory dump with the severity level WARNING.
*
* @param[in] p_memory Pointer to the memory region to be dumped.
* @param[in] length Length of the memory region in bytes.
*/
#define NRFX_LOG_HEXDUMP_WARNING(p_memory, length)
/**
* @brief Macro for logging a memory dump with the severity level INFO.
*
* @param[in] p_memory Pointer to the memory region to be dumped.
* @param[in] length Length of the memory region in bytes.
*/
#define NRFX_LOG_HEXDUMP_INFO(p_memory, length)
/**
* @brief Macro for logging a memory dump with the severity level DEBUG.
*
* @param[in] p_memory Pointer to the memory region to be dumped.
* @param[in] length Length of the memory region in bytes.
*/
#define NRFX_LOG_HEXDUMP_DEBUG(p_memory, length)
/**
* @brief Macro for getting the textual representation of a given error code.
*
* @param[in] error_code Error code.
*
* @return String containing the textual representation of the error code.
*/
#define NRFX_LOG_ERROR_STRING_GET(error_code)
/** @} */
#ifdef __cplusplus
}
#endif
#endif // NRFX_LOG_H__

View File

@ -67,6 +67,7 @@
#define TU_ATTR_WEAK __attribute__ ((weak))
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
#define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
// Endian conversion use well-known host to network (big endian) naming
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@ -75,8 +76,8 @@
#define TU_BYTE_ORDER TU_BIG_ENDIAN
#endif
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
#elif defined(__TI_COMPILER_VERSION__)
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
@ -86,6 +87,7 @@
#define TU_ATTR_WEAK __attribute__ ((weak))
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
#define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
#define TU_ATTR_USED __attribute__ ((used))
// __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
#if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
@ -123,7 +125,6 @@
#define tu_htonl(u32) (u32)
#define tu_ntohl(u32) (u32)
#define tu_htole16(u16) (tu_bswap16(u16))
#define tu_le16toh(u16) (tu_bswap16(u16))