| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- #include "nrf.h"
- #include "nrf_log.h"
- #include "nrf_error.h"
- #include <stdarg.h>
- #include <string.h>
- #include <stdio.h>
- #include "proc.h"
- #include "type.h"
- #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
- #include <SEGGER_RTT_Conf.h>
- #include <SEGGER_RTT.h>
- static char buf_normal_up[BUFFER_SIZE_UP];
- static char buf_down[BUFFER_SIZE_DOWN];
- uint32_t log_rtt_init(void)
- {
- static bool initialized = false;
- if (initialized)
- {
- return NRF_SUCCESS;
- }
- if (SEGGER_RTT_ConfigUpBuffer(LOG_TERMINAL_NORMAL,
- "Normal",
- buf_normal_up,
- BUFFER_SIZE_UP,
- SEGGER_RTT_MODE_NO_BLOCK_TRIM
- )
- != 0)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- if (SEGGER_RTT_ConfigDownBuffer(LOG_TERMINAL_INPUT,
- "Input",
- buf_down,
- BUFFER_SIZE_DOWN,
- SEGGER_RTT_MODE_NO_BLOCK_SKIP
- )
- != 0)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- initialized = true;
- return NRF_SUCCESS;
- }
- // Forward declaration of SEGGER RTT vprintf function
- int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
- void log_rtt_printf(int terminal_index, char * format_msg, ...)
- {
- //lint -save -e526 -e628 -e530
- va_list p_args;
- va_start(p_args, format_msg);
- (void)SEGGER_RTT_vprintf(terminal_index, format_msg, &p_args);
- va_end(p_args);
- //lint -restore
- }
- __INLINE void log_rtt_write_string(int terminal_index, int num_args, ...)
- {
- const char* msg;
- //lint -save -e516 -e530
- va_list p_args;
- va_start(p_args, num_args);
- //lint -restore
- for (int i = 0; i < num_args; i++)
- {
- //lint -save -e26 -e10 -e64 -e526 -e628 -e530
- msg = va_arg(p_args, const char*);
- //lint -restore
- (void)SEGGER_RTT_WriteString(terminal_index, msg);
- }
- va_end(p_args);
- }
- void log_rtt_write_hex(int terminal_index, uint32_t value)
- {
- char temp[11];
- temp[0] = '0';
- temp[1] = 'x';
- temp[10] = 0; // Null termination
- uint8_t nibble;
- uint8_t i = 8;
- while(i-- != 0)
- {
- nibble = (value >> (4 * i)) & 0x0F;
- temp[9-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
- }
- (void)SEGGER_RTT_WriteString(terminal_index, temp);
- }
- void log_rtt_write_hex_char(int terminal_index, uint8_t value)
- {
- char temp[3];
- temp[2] = 0; // Null termination
- uint8_t nibble;
- uint8_t i = 2;
- while(i-- != 0)
- {
- nibble = (value >> (4 * i)) & 0x0F;
- temp[1-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
- }
- (void)SEGGER_RTT_WriteString(terminal_index, temp);
- }
- __INLINE int log_rtt_has_input()
- {
- return SEGGER_RTT_HasKey();
- }
- uint32_t log_rtt_read_input(char * c)
- {
- int r;
- r = SEGGER_RTT_Read(LOG_TERMINAL_INPUT, c, 1);
- if (r == 1)
- return NRF_SUCCESS;
- else
- return NRF_ERROR_NULL;
- }
- #elif defined(NRF_LOG_USES_UART) && NRF_LOG_USES_UART == 1
- #include "app_uart.h"
- #include "app_error.h"
- #include <stdio.h>
- #include <string.h>
- #include "nrf.h"
- #include "bsp.h"
- #define MAX_TEST_DATA_BYTES (15U) /**< max number of test bytes to be used for tx and rx. */
- #define UART_TX_BUF_SIZE 128 /**< UART TX buffer size. */
- #define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */
- static uint8_t m_uart_data;
- static bool m_uart_has_input;
- void uart_error_cb(app_uart_evt_t * p_event)
- {
- if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
- {
- APP_ERROR_HANDLER(p_event->data.error_communication);
- }
- else if (p_event->evt_type == APP_UART_FIFO_ERROR)
- {
- APP_ERROR_HANDLER(p_event->data.error_code);
- }
- }
- uint32_t log_uart_init()
- {
- static bool initialized = false;
- if (initialized)
- {
- return NRF_SUCCESS;
- }
- uint32_t err_code;
- const app_uart_comm_params_t comm_params =
- {
- RX_PIN_NUMBER,
- TX_PIN_NUMBER,
- RTS_PIN_NUMBER,
- CTS_PIN_NUMBER,
- //APP_UART_FLOW_CONTROL_ENABLED,
- APP_UART_FLOW_CONTROL_DISABLED,
- false,
- UART_BAUDRATE_BAUDRATE_Baud115200 //UART_BAUDRATE_BAUDRATE_Baud115200
- };
- APP_UART_FIFO_INIT(&comm_params,
- UART_RX_BUF_SIZE,
- UART_TX_BUF_SIZE,
- uart_error_cb,
- APP_IRQ_PRIORITY_LOW,
- err_code);
- initialized = true;
- return err_code;
- }
- //lint -save -e530 -e64
- void log_uart_printf(const char * format_msg, ...)
- {
- va_list p_args;
- va_start(p_args, format_msg);
- (void)vprintf(format_msg, p_args);
- va_end(p_args);
- }
- __INLINE void log_uart_write_string_many(int num_args, ...)
- {
- const char* msg;
- va_list p_args;
- va_start(p_args, num_args);
- for (int i = 0; i < num_args; i++)
- {
- msg = va_arg(p_args, const char*);
- log_uart_write_string(msg);
- }
- va_end(p_args);
- }
- __INLINE void log_uart_write_string(const char* msg)
- {
- while( *msg )
- {
- (void)app_uart_put(*msg++);
- }
- }
- //lint -restore
- void log_uart_write_hex(uint32_t value)
- {
- uint8_t nibble;
- uint8_t i = 8;
- (void)app_uart_put('0');
- (void)app_uart_put('x');
- while( i-- != 0 )
- {
- nibble = (value >> (4 * i)) & 0x0F;
- (void)app_uart_put( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
- }
- }
- void log_uart_write_hex_char(uint8_t c)
- {
- uint8_t nibble;
- uint8_t i = 2;
- while( i-- != 0 )
- {
- nibble = (c >> (4 * i)) & 0x0F;
- (void)app_uart_put( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
- }
- }
- __INLINE int log_uart_has_input()
- {
- if (m_uart_has_input) return 1;
- if (app_uart_get(&m_uart_data) == NRF_SUCCESS)
- {
- m_uart_has_input = true;
- return 1;
- }
- return 0;
- }
- uint32_t log_uart_read_input(char * c)
- {
- if (m_uart_has_input)
- {
- *c = (char)m_uart_data;
- m_uart_has_input = false;
- return NRF_SUCCESS;
- }
- if (app_uart_get((uint8_t *)c) == NRF_SUCCESS)
- {
- return NRF_SUCCESS;
- }
- return NRF_ERROR_NULL;
- }
- void log_uart_write_hexs(const char* msg, uint8_t* data, uint32_t length)
- {
- uint32_t i;
- log_uart_write_string(msg);
- log_uart_printf(":");
- for(i=0; i<length; i++)
- {
- log_uart_printf("%02X", data[i]);
- }
- log_uart_printf("\n");
- }
- #elif defined(NRF_LOG_USES_RAW_UART) && NRF_LOG_USES_RAW_UART == 1
- #include "app_uart.h"
- #include <stdio.h>
- #include <string.h>
- #include "bsp.h"
- uint32_t log_raw_uart_init()
- {
- // Disable UART
- NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled;
- // Configure RX/TX pins
- nrf_gpio_cfg_output( TX_PIN_NUMBER );
- nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
- // Set a default baud rate of UART0_CONFIG_BAUDRATE
- NRF_UART0->PSELTXD = TX_PIN_NUMBER;
- NRF_UART0->BAUDRATE = UART0_CONFIG_BAUDRATE;
- NRF_UART0->PSELRTS = 0xFFFFFFFF;
- NRF_UART0->PSELCTS = 0xFFFFFFFF;
- // Disable parity and interrupt
- NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos );
- NRF_UART0->CONFIG |= (UART_CONFIG_HWFC_Disabled << UART_CONFIG_HWFC_Pos );
- // Re-enable the UART
- NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
- NRF_UART0->INTENSET = 0;
- NRF_UART0->TASKS_STARTTX = 1;
- NRF_UART0->TASKS_STARTRX = 1;
- return NRF_SUCCESS;
- }
- void log_raw_uart_printf(const char * format_msg, ...)
- {
- static char buffer[256];
- va_list p_args;
- va_start(p_args, format_msg);
- sprintf(buffer, format_msg, p_args);
- va_end(p_args);
- log_raw_uart_write_string(buffer);
- }
- __INLINE void log_raw_uart_write_char(const char c)
- {
- NRF_UART0->TXD = c;
- while( NRF_UART0->EVENTS_TXDRDY != 1 );
- NRF_UART0->EVENTS_TXDRDY = 0;
- }
- __INLINE void log_raw_uart_write_string_many(int num_args, ...)
- {
- const char* msg;
- va_list p_args;
- va_start(p_args, num_args);
- for (int i = 0; i < num_args; i++)
- {
- msg = va_arg(p_args, const char*);
- log_raw_uart_write_string(msg);
- }
- va_end(p_args);
- }
- __INLINE void log_raw_uart_write_string(const char* msg)
- {
- while( *msg )
- {
- NRF_UART0->TXD = *msg++;
- while( NRF_UART0->EVENTS_TXDRDY != 1 );
- NRF_UART0->EVENTS_TXDRDY = 0;
- }
- }
- void log_raw_uart_write_hex(uint32_t value)
- {
- uint8_t nibble;
- uint8_t i = 8;
- log_raw_uart_write_string( "0x" );
- while( i-- != 0 )
- {
- nibble = (value >> (4 * i)) & 0x0F;
- log_raw_uart_write_char( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
- }
- }
- void log_raw_uart_write_hex_char(uint8_t c)
- {
- uint8_t nibble;
- uint8_t i = 2;
- while( i-- != 0 )
- {
- nibble = (c >> (4 * i)) & 0x0F;
- log_raw_uart_write_hex( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
- }
- }
- __INLINE int log_raw_uart_has_input()
- {
- return 0;
- }
- uint32_t log_raw_uart_read_input(char * c)
- {
- return NRF_ERROR_NULL;
- }
- #endif // NRF_LOG_USES_RAW_UART == 1
- void log_spi_printf(const char * format_msg, ...)
- {
- uint8_t buffer[128];
- va_list p_args;
- va_start(p_args, format_msg);
- sprintf((char *restrict) buffer, (uint8_t*)format_msg, p_args);
- va_end(p_args);
- // spis_transfer_data(BLE_CMD_LOG, 0x01, buffer, strlen((const char*)buffer));
- }
- const char* log_hex_char(const char c)
- {
- static volatile char hex_string[3];
- hex_string[2] = 0; // Null termination
- uint8_t nibble;
- uint8_t i = 2;
- while(i-- != 0)
- {
- nibble = (c >> (4 * i)) & 0x0F;
- hex_string[1-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
- }
- return (const char*) hex_string;
- }
- const char* log_hex(uint32_t value)
- {
- static volatile char hex_string[11];
- hex_string[0] = '0';
- hex_string[1] = 'x';
- hex_string[10] = 0;
- uint8_t nibble;
- uint8_t i = 8;
- while(i-- != 0)
- {
- nibble = (value >> (4 * i)) & 0x0F;
- hex_string[9-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
- }
- return (const char*)hex_string;
- }
|