This commit is contained in:
hathach 2018-02-15 15:26:39 +07:00
parent 12dee23ef2
commit 8f4d0c6e3a
9 changed files with 0 additions and 1251 deletions

View File

@ -1,262 +0,0 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "app_scheduler.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "nrf_soc.h"
#include "nrf_assert.h"
#include "app_util.h"
#include "app_util_platform.h"
/**@brief Structure for holding a scheduled event header. */
typedef struct
{
app_sched_event_handler_t handler; /**< Pointer to event handler to receive the event. */
uint16_t event_data_size; /**< Size of event data. */
} event_header_t;
STATIC_ASSERT(sizeof (event_header_t) <= APP_SCHED_EVENT_HEADER_SIZE);
static event_header_t * m_queue_event_headers; /**< Array for holding the queue event headers. */
static uint8_t * m_queue_event_data; /**< Array for holding the queue event data. */
static volatile uint8_t m_queue_start_index; /**< Index of queue entry at the start of the queue. */
static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the end of the queue. */
static uint16_t m_queue_event_size; /**< Maximum event size in queue. */
static uint16_t m_queue_size; /**< Number of queue entries. */
#ifdef APP_SCHEDULER_WITH_PROFILER
static uint16_t m_max_queue_utilization; /**< Maximum observed queue utilization. */
#endif
static uint32_t m_scheduler_paused_counter = 0; /**< Counter storing the difference between pausing
and resuming the scheduler. */
/**@brief Function for incrementing a queue index, and handle wrap-around.
*
* @param[in] index Old index.
*
* @return New (incremented) index.
*/
static __INLINE uint8_t next_index(uint8_t index)
{
return (index < m_queue_size) ? (index + 1) : 0;
}
static __INLINE uint8_t app_sched_queue_full(void)
{
uint8_t tmp = m_queue_start_index;
return next_index(m_queue_end_index) == tmp;
}
/**@brief Macro for checking if a queue is full. */
#define APP_SCHED_QUEUE_FULL() app_sched_queue_full()
static __INLINE uint8_t app_sched_queue_empty(void)
{
uint8_t tmp = m_queue_start_index;
return m_queue_end_index == tmp;
}
/**@brief Macro for checking if a queue is empty. */
#define APP_SCHED_QUEUE_EMPTY() app_sched_queue_empty()
uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer)
{
uint16_t data_start_index = (queue_size + 1) * sizeof (event_header_t);
//Check that buffer is correctly aligned
if (!is_word_aligned(p_event_buffer))
{
return NRF_ERROR_INVALID_PARAM;
}
//Initialize event scheduler
m_queue_event_headers = p_event_buffer;
m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index];
m_queue_end_index = 0;
m_queue_start_index = 0;
m_queue_event_size = event_size;
m_queue_size = queue_size;
#ifdef APP_SCHEDULER_WITH_PROFILER
m_max_queue_utilization = 0;
#endif
return NRF_SUCCESS;
}
#ifdef APP_SCHEDULER_WITH_PROFILER
static void check_queue_utilization(void)
{
uint16_t start = m_queue_start_index;
uint16_t end = m_queue_end_index;
uint16_t queue_utilization = (end >= start) ? (end - start) :
(m_queue_size + 1 - start + end);
if (queue_utilization > m_max_queue_utilization)
{
m_max_queue_utilization = queue_utilization;
}
}
uint16_t app_sched_queue_utilization_get(void)
{
return m_max_queue_utilization;
}
#endif
uint32_t app_sched_event_put(void * p_event_data,
uint16_t event_data_size,
app_sched_event_handler_t handler)
{
uint32_t err_code;
if (event_data_size <= m_queue_event_size)
{
uint16_t event_index = 0xFFFF;
CRITICAL_REGION_ENTER();
if (!APP_SCHED_QUEUE_FULL())
{
event_index = m_queue_end_index;
m_queue_end_index = next_index(m_queue_end_index);
}
CRITICAL_REGION_EXIT();
if (event_index != 0xFFFF)
{
//NOTE: This can be done outside the critical region since the event consumer will
//always be called from the main loop, and will thus never interrupt this code.
m_queue_event_headers[event_index].handler = handler;
if ((p_event_data != NULL) && (event_data_size > 0))
{
memcpy(&m_queue_event_data[event_index * m_queue_event_size],
p_event_data,
event_data_size);
m_queue_event_headers[event_index].event_data_size = event_data_size;
}
else
{
m_queue_event_headers[event_index].event_data_size = 0;
}
#ifdef APP_SCHEDULER_WITH_PROFILER
check_queue_utilization();
#endif
err_code = NRF_SUCCESS;
}
else
{
err_code = NRF_ERROR_NO_MEM;
}
}
else
{
err_code = NRF_ERROR_INVALID_LENGTH;
}
return err_code;
}
/**@brief Function for reading the next event from specified event queue.
*
* @param[out] pp_event_data Pointer to pointer to event data.
* @param[out] p_event_data_size Pointer to size of event data.
* @param[out] p_event_handler Pointer to event handler function pointer.
*
* @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty.
*/
static uint32_t app_sched_event_get(void * * pp_event_data,
uint16_t * p_event_data_size,
app_sched_event_handler_t * p_event_handler)
{
uint32_t err_code = NRF_ERROR_NOT_FOUND;
if (!APP_SCHED_QUEUE_EMPTY())
{
uint16_t event_index;
//NOTE: There is no need for a critical region here, as this function will only be called
//from app_sched_execute() from inside the main loop, so it will never interrupt
//app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be
//an atomic operation.
event_index = m_queue_start_index;
m_queue_start_index = next_index(m_queue_start_index);
*pp_event_data = &m_queue_event_data[event_index * m_queue_event_size];
*p_event_data_size = m_queue_event_headers[event_index].event_data_size;
*p_event_handler = m_queue_event_headers[event_index].handler;
err_code = NRF_SUCCESS;
}
return err_code;
}
void app_sched_pause(void)
{
CRITICAL_REGION_ENTER();
if (m_scheduler_paused_counter < UINT32_MAX)
{
m_scheduler_paused_counter++;
}
CRITICAL_REGION_EXIT();
}
void app_sched_resume(void)
{
CRITICAL_REGION_ENTER();
if (m_scheduler_paused_counter > 0)
{
m_scheduler_paused_counter--;
}
CRITICAL_REGION_EXIT();
}
/**@brief Function for checking if scheduler is paused which means that should break processing
* events.
*
* @return Boolean value - true if scheduler is paused, false otherwise.
*/
static __INLINE bool is_app_sched_paused(void)
{
return (m_scheduler_paused_counter > 0);
}
void app_sched_execute(void)
{
void * p_event_data;
uint16_t event_data_size;
app_sched_event_handler_t event_handler;
//Get next event (if any), and execute handler
while ((!is_app_sched_paused()) &&
(app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS))
{
event_handler(p_event_data, event_data_size);
}
}

View File

@ -1,187 +0,0 @@
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "app_simple_timer.h"
#include "nrf.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_timer.h"
#include "nrf_drv_timer.h"
#include "sdk_common.h"
/**@brief States of simple timer state machine.
*/
typedef enum
{
SIMPLE_TIMER_STATE_IDLE = 0,
SIMPLE_TIMER_STATE_INITIALIZED,
SIMPLE_TIMER_STATE_STOPPED,
SIMPLE_TIMER_STATE_STARTED
}simple_timer_states_t;
static app_simple_timer_mode_t m_mode; /**< Registered timer mode. */
static app_simple_timer_timeout_handler_t m_timeout_handler = NULL; /**< Registered time-out handler. */
static void * mp_timeout_handler_context = NULL; /**< Registered time-out handler context. */
static simple_timer_states_t m_simple_timer_state = SIMPLE_TIMER_STATE_IDLE; /**< State machine state. */
#define APP_SIMPLE_TIMER_INSTANCE 1
#if (APP_SIMPLE_TIMER_INSTANCE == 0)
#if (TIMER_CONFIG_MODE(0) != TIMER_MODE_MODE_Timer)
#error "Unsupported timer mode."
#endif
#if (TIMER_CONFIG_BIT_WIDTH(0) != TIMER_BITMODE_BITMODE_16Bit)
#error "Unsupported timer bit width."
#endif
const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(0);
#elif (APP_SIMPLE_TIMER_INSTANCE == 1)
#if (TIMER_CONFIG_MODE(1) != TIMER_MODE_MODE_Timer)
#error "Unsupported timer mode."
#endif
#if (TIMER_CONFIG_BIT_WIDTH(1) != TIMER_BITMODE_BITMODE_16Bit)
#error "Unsupported timer bit width."
#endif
const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(1);
#elif (APP_SIMPLE_TIMER_INSTANCE == 2)
#if (TIMER_CONFIG_MODE(2) != TIMER_MODE_MODE_Timer)
#error "Unsupported timer mode."
#endif
#if (TIMER_CONFIG_BIT_WIDTH(2) != TIMER_BITMODE_BITMODE_16Bit)
#error "Unsupported timer bit width."
#endif
const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(2);
#else
#error "Wrong timer instance id."
#endif
/**
* @brief Handler for timer events.
*/
static void app_simple_timer_event_handler(nrf_timer_event_t event_type, void * p_context)
{
switch(event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
if (m_mode == APP_SIMPLE_TIMER_MODE_SINGLE_SHOT)
{
m_simple_timer_state = SIMPLE_TIMER_STATE_STOPPED;
}
//@note: No NULL check required as performed in timer_start(...).
m_timeout_handler(mp_timeout_handler_context);
break;
default:
//Do nothing.
break;
}
}
uint32_t app_simple_timer_init(void)
{
uint32_t err_code = NRF_SUCCESS;
err_code = nrf_drv_timer_init(&SIMPLE_TIMER, NULL, app_simple_timer_event_handler);
if(NRF_SUCCESS == err_code)
{
m_simple_timer_state = SIMPLE_TIMER_STATE_INITIALIZED;
}
return err_code;
}
uint32_t app_simple_timer_start(app_simple_timer_mode_t mode,
app_simple_timer_timeout_handler_t timeout_handler,
uint16_t timeout_ticks,
void * p_context)
{
uint32_t err_code = NRF_SUCCESS;
nrf_timer_short_mask_t timer_short;
VERIFY_PARAM_NOT_NULL(timeout_handler);
if (APP_SIMPLE_TIMER_MODE_REPEATED == mode)
{
timer_short = NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK;
}
else if(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT == mode)
{
timer_short = NRF_TIMER_SHORT_COMPARE0_STOP_MASK;
}
else
{
return NRF_ERROR_INVALID_PARAM;
}
if(SIMPLE_TIMER_STATE_IDLE == m_simple_timer_state)
{
return NRF_ERROR_INVALID_STATE;
}
if(SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state)
{
err_code = app_simple_timer_stop();
APP_ERROR_CHECK(err_code);
}
if(SIMPLE_TIMER_STATE_STOPPED == m_simple_timer_state)
{
nrf_drv_timer_clear(&SIMPLE_TIMER);
}
m_mode = mode;
m_timeout_handler = timeout_handler;
mp_timeout_handler_context = p_context;
nrf_drv_timer_extended_compare(
&SIMPLE_TIMER, NRF_TIMER_CC_CHANNEL0, (uint32_t)timeout_ticks, timer_short, true);
if (m_simple_timer_state == SIMPLE_TIMER_STATE_STOPPED)
{
nrf_drv_timer_resume(&SIMPLE_TIMER);
}
else
{
nrf_drv_timer_enable(&SIMPLE_TIMER);
}
m_simple_timer_state = SIMPLE_TIMER_STATE_STARTED;
return NRF_SUCCESS;
}
uint32_t app_simple_timer_stop(void)
{
if(SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state)
{
nrf_drv_timer_pause(&SIMPLE_TIMER);
m_simple_timer_state = SIMPLE_TIMER_STATE_STOPPED;
}
return NRF_SUCCESS;
}
uint32_t app_simple_timer_uninit(void)
{
uint32_t err_code = NRF_SUCCESS;
if(SIMPLE_TIMER_STATE_IDLE != m_simple_timer_state)
{
nrf_drv_timer_uninit(&SIMPLE_TIMER);
m_simple_timer_state = SIMPLE_TIMER_STATE_IDLE;
}
return err_code;
}

View File

@ -1,98 +0,0 @@
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup lib_driver_simple_timer Simple Timer
* @{
* @ingroup app_common
*
* @brief Simple timer module.
*
* Supported features and limitations:
* - Two modes: single shot mode and repeated mode.
* - No more than one timer can run simultaneously.
* - The timer is hard-coded to use the TIMER1 peripheral and compare channel 0.
*/
#ifndef TIMER_H__
#define TIMER_H__
#include <stdint.h>
/**@brief Timer time-out handler type. */
typedef void (*app_simple_timer_timeout_handler_t)(void * p_context);
/**@brief Timer modes. */
typedef enum
{
APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
APP_SIMPLE_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
} app_simple_timer_mode_t;
/**@brief Function for configuring and setting up the timer hardware.
*
* @note Configuration parameters should be set in nrf_drv_config.h file.
* The TIMER1_CONFIG_MODE has to be set to NRF_TIMER_MODE_TIMER value.
* The TIMER1_CONFIG_BIT_WIDTH has to be set to NRF_TIMER_BIT_WIDTH_16 value.
*
* @retval NRF_SUCCESS If the operation is successful.
* @retval NRF_ERROR_INVALID_STATE If the operation fails because the timer is already initialized.
* @retval NRF_ERROR_INVALID_PARAM If the operation fails because some configuration parameter is
* not valid.
*/
uint32_t app_simple_timer_init(void);
/**@brief Function for starting a timer.
*
* @note If this function is called for a timer that is already running, the currently running
* timer is stopped before starting the new one.
*
* @param[in] mode Timer mode (see @ref app_simple_timer_mode_t).
* @param[in] timeout_handler Function to be executed when the timer expires
* (see @ref app_simple_timer_timeout_handler_t).
* @param[in] timeout_ticks Number of timer ticks to time-out event.
* @param[in] p_context General purpose pointer. Will be passed to the time-out handler
* when the timer expires.
*
* @retval NRF_SUCCESS If the operation is successful.
* @retval NRF_ERROR_INVALID_STATE If the operation fails because @ref app_simple_timer_init has not
* been called and the operation is not allowed in this state.
* @retval NRF_ERROR_NULL If the operation fails because timeout_handler is NULL.
* @retval NRF_ERROR_INVALID_PARAM If the operation fails because "mode" parameter is not valid.
*/
uint32_t app_simple_timer_start(app_simple_timer_mode_t mode,
app_simple_timer_timeout_handler_t timeout_handler,
uint16_t timeout_ticks,
void * p_context);
/**@brief Function for stopping the timer.
*
* @retval NRF_SUCCESS If the operation is successful.
*/
uint32_t app_simple_timer_stop(void);
/**@brief Function for uninitializing the timer. Should be called also when the timer is not used
* anymore to reach lowest power consumption in system.
*
* @note The function switches off the internal core of the timer to reach lowest power consumption
* in system. The startup time from this state may be longer compared to starting the timer
* from the stopped state.
*
* @retval NRF_SUCCESS If the operation is successful.
*/
uint32_t app_simple_timer_uninit(void);
#endif // TIMER_H__
/** @} */

View File

@ -1,112 +0,0 @@
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "slip.h"
#include "nrf_error.h"
#define SLIP_END 0300 /* indicates end of packet */
#define SLIP_ESC 0333 /* indicates byte stuffing */
#define SLIP_ESC_END 0334 /* ESC ESC_END means END data byte */
#define SLIP_ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
uint32_t slip_encode(uint8_t * p_output, uint8_t * p_input, uint32_t input_length, uint32_t output_buffer_length)
{
uint32_t input_index;
uint32_t output_index;
for (input_index = 0, output_index = 0; input_index < input_length && output_index < output_buffer_length; input_index++)
{
switch (p_input[input_index])
{
case SLIP_END:
p_output[output_index++] = SLIP_END;
p_output[output_index++] = SLIP_ESC_END;
break;
case SLIP_ESC:
p_output[output_index++] = SLIP_ESC;
p_output[output_index++] = SLIP_ESC_ESC;
break;
default:
p_output[output_index++] = p_input[input_index];
}
}
p_output[output_index++] = (uint8_t)SLIP_END;
p_output[output_index++] = (uint8_t)SLIP_END; // clarify that the packet has ended.
return output_index;
}
uint32_t slip_decoding_add_char(uint8_t c, buffer_t * p_buf, slip_state_t * current_state)
{
switch (*current_state)
{
case SLIP_DECODING:
if (c == SLIP_END)
{
*current_state = SLIP_END_RECEIVED;
}
else if (c == SLIP_ESC)
{
*current_state = SLIP_END_RECEIVED;
}
else
{
p_buf->p_buffer[p_buf->current_index++] = c;
p_buf->current_length++;
}
break;
case SLIP_ESC_RECEIVED:
if (c == SLIP_ESC_ESC)
{
p_buf->p_buffer[p_buf->current_index++] = SLIP_ESC;
p_buf->current_length++;
*current_state = SLIP_DECODING;
}
else
{
// violation of protocol
*current_state = SLIP_CLEARING_INVALID_PACKET;
return NRF_ERROR_INVALID_DATA;
}
break;
case SLIP_END_RECEIVED:
if (c == SLIP_ESC_END)
{
p_buf->p_buffer[p_buf->current_index++] = SLIP_END;
p_buf->current_length++;
*current_state = SLIP_DECODING;
}
else
{
// packet is finished
*current_state = SLIP_DECODING;
return NRF_SUCCESS;
}
break;
case SLIP_CLEARING_INVALID_PACKET:
if (c == SLIP_END)
{
*current_state = SLIP_DECODING;
p_buf->current_index = 0;
p_buf->current_length = 0;
}
break;
}
return NRF_ERROR_BUSY;
}

View File

@ -1,66 +0,0 @@
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#ifndef SLIP_H__
#define SLIP_H__
#include <stdint.h>
#include "app_fifo.h"
/** @file
*
* @defgroup slip SLIP encoding decoding
* @{
* @ingroup app_common
*
* @brief This module encodes and decodes slip packages (RFC1055).
*
* @details The standard is described in https://tools.ietf.org/html/rfc1055
*/
typedef enum {
SLIP_DECODING,
SLIP_END_RECEIVED,
SLIP_ESC_RECEIVED,
SLIP_CLEARING_INVALID_PACKET,
} slip_state_t;
typedef struct {
uint8_t * p_buffer;
uint32_t current_index;
uint32_t current_length;
uint32_t len;
} buffer_t;
/**@brief Encodes a slip packet.
*
* @details Note that the encoded output data will be longer than the input data.
*
* @retval The length of the encoded packet. If it is smaller than the input length, an error has occurred.
*/
uint32_t slip_encode(uint8_t * p_output, uint8_t * p_input, uint32_t input_length, uint32_t output_buffer_length);
/**@brief Decodes a slip packet.
*
* @details When decoding a slip packet, a state must be preserved. Initial state must be set to SLIP_DECODING.
*
* @retval NRF_SUCCESS when a packet is parsed. The length of the packet can be read out from p_buf->current_index
* @retval NRF_ERROR_BUSY when packet is not finished parsing
* @retval NRF_ERROR_INVALID_DATA when packet is encoded wrong.
This moves the decoding to SLIP_CLEARING_INVALID_PACKET, and will stay in this state until SLIP_END is encountered.
*/
uint32_t slip_decoding_add_char(uint8_t c, buffer_t * p_buf, slip_state_t * current_state);
#endif // SLIP_H__
/** @} */

View File

@ -1,43 +0,0 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#ifdef ENABLE_DEBUG_LOG_SUPPORT
#include "app_trace.h"
#include "nrf_log.h"
void app_trace_init(void)
{
(void)NRF_LOG_INIT();
}
void app_trace_dump(uint8_t * p_buffer, uint32_t len)
{
app_trace_log("\r\n");
for (uint32_t index = 0; index < len; index++)
{
app_trace_log("0x%02X ", p_buffer[index]);
}
app_trace_log("\r\n");
}
#endif // ENABLE_DEBUG_LOG_SUPPORT
/**
*@}
**/

View File

@ -1,56 +0,0 @@
#ifndef __DEBUG_H_
#define __DEBUG_H_
#include <stdint.h>
#include <stdio.h>
/**
* @defgroup app_trace Debug Logger
* @ingroup app_common
* @{
* @brief Enables debug logs/ trace over UART.
* @details Enables debug logs/ trace over UART. Tracing is enabled only if
* ENABLE_DEBUG_LOG_SUPPORT is defined in the project.
*/
#ifdef ENABLE_DEBUG_LOG_SUPPORT
#include "nrf_log.h"
/**
* @brief Module Initialization.
*
* @details Initializes the module to use UART as trace output.
*
* @warning This function will configure UART using default board configuration.
* Do not call this function if UART is configured from a higher level in the application.
*/
void app_trace_init(void);
/**
* @brief Log debug messages.
*
* @details This API logs messages over UART. The module must be initialized before using this API.
*
* @note Though this is currently a macro, it should be used used and treated as function.
*/
#define app_trace_log NRF_LOG_PRINTF
/**
* @brief Dump auxiliary byte buffer to the debug trace.
*
* @details This API logs messages over UART. The module must be initialized before using this API.
*
* @param[in] p_buffer Buffer to be dumped on the debug trace.
* @param[in] len Size of the buffer.
*/
void app_trace_dump(uint8_t * p_buffer, uint32_t len);
#else // ENABLE_DEBUG_LOG_SUPPORT
#define app_trace_init(...)
#define app_trace_log(...)
#define app_trace_dump(...)
#endif // ENABLE_DEBUG_LOG_SUPPORT
/** @} */
#endif //__DEBUG_H_

View File

@ -1,251 +0,0 @@
/**
* Copyright (c) 2015 - 2017, 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, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, 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 Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
*
*/
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(APP_UART)
#include "app_uart.h"
#include "app_fifo.h"
#include "nrf_drv_uart.h"
#include "nrf_assert.h"
static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
{
uint32_t tmp = fifo->read_pos;
return fifo->write_pos - tmp;
}
#define FIFO_LENGTH(F) fifo_length(&F) /**< Macro to calculate length of a FIFO. */
static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
static uint8_t tx_buffer[1];
static uint8_t rx_buffer[1];
static bool m_rx_ovf;
static app_fifo_t m_rx_fifo; /**< RX FIFO buffer for storing data received on the UART until the application fetches them using app_uart_get(). */
static app_fifo_t m_tx_fifo; /**< TX FIFO buffer for storing data to be transmitted on the UART when TXD is ready. Data is put to the buffer on using app_uart_put(). */
static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
{
app_uart_evt_t app_uart_event;
uint32_t err_code;
switch (p_event->type)
{
case NRF_DRV_UART_EVT_RX_DONE:
// Write received byte to FIFO.
err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
if (err_code != NRF_SUCCESS)
{
app_uart_event.evt_type = APP_UART_FIFO_ERROR;
app_uart_event.data.error_code = err_code;
m_event_handler(&app_uart_event);
}
// Notify that there are data available.
else if (FIFO_LENGTH(m_rx_fifo) != 0)
{
app_uart_event.evt_type = APP_UART_DATA_READY;
m_event_handler(&app_uart_event);
}
// Start new RX if size in buffer.
if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
{
(void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
}
else
{
// Overflow in RX FIFO.
m_rx_ovf = true;
}
break;
case NRF_DRV_UART_EVT_ERROR:
app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
app_uart_event.data.error_communication = p_event->data.error.error_mask;
(void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
m_event_handler(&app_uart_event);
break;
case NRF_DRV_UART_EVT_TX_DONE:
// Get next byte from FIFO.
if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
{
(void)nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
}
else
{
// Last byte from FIFO transmitted, notify the application.
app_uart_event.evt_type = APP_UART_TX_EMPTY;
m_event_handler(&app_uart_event);
}
break;
default:
break;
}
}
uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
app_uart_buffers_t * p_buffers,
app_uart_event_handler_t event_handler,
app_irq_priority_t irq_priority)
{
uint32_t err_code;
m_event_handler = event_handler;
if (p_buffers == NULL)
{
return NRF_ERROR_INVALID_PARAM;
}
// Configure buffer RX buffer.
err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size);
VERIFY_SUCCESS(err_code);
// Configure buffer TX buffer.
err_code = app_fifo_init(&m_tx_fifo, p_buffers->tx_buf, p_buffers->tx_buf_size);
VERIFY_SUCCESS(err_code);
nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
config.interrupt_priority = irq_priority;
config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
config.pselcts = p_comm_params->cts_pin_no;
config.pselrts = p_comm_params->rts_pin_no;
config.pselrxd = p_comm_params->rx_pin_no;
config.pseltxd = p_comm_params->tx_pin_no;
err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
VERIFY_SUCCESS(err_code);
m_rx_ovf = false;
// Turn on receiver if RX pin is connected
if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
{
#ifdef UARTE_PRESENT
if (!config.use_easy_dma)
#endif
{
nrf_drv_uart_rx_enable(&app_uart_inst);
}
return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
}
else
{
return NRF_SUCCESS;
}
}
uint32_t app_uart_flush(void)
{
uint32_t err_code;
err_code = app_fifo_flush(&m_rx_fifo);
VERIFY_SUCCESS(err_code);
err_code = app_fifo_flush(&m_tx_fifo);
VERIFY_SUCCESS(err_code);
return NRF_SUCCESS;
}
uint32_t app_uart_get(uint8_t * p_byte)
{
ASSERT(p_byte);
bool rx_ovf = m_rx_ovf;
ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);
// If FIFO was full new request to receive one byte was not scheduled. Must be done here.
if (rx_ovf)
{
m_rx_ovf = false;
uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
// RX resume should never fail.
APP_ERROR_CHECK(uart_err_code);
}
return err_code;
}
uint32_t app_uart_put(uint8_t byte)
{
uint32_t err_code;
err_code = app_fifo_put(&m_tx_fifo, byte);
if (err_code == NRF_SUCCESS)
{
// The new byte has been added to FIFO. It will be picked up from there
// (in 'uart_event_handler') when all preceding bytes are transmitted.
// But if UART is not transmitting anything at the moment, we must start
// a new transmission here.
if (!nrf_drv_uart_tx_in_progress(&app_uart_inst))
{
// This operation should be almost always successful, since we've
// just added a byte to FIFO, but if some bigger delay occurred
// (some heavy interrupt handler routine has been executed) since
// that time, FIFO might be empty already.
if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
{
err_code = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
}
}
}
return err_code;
}
uint32_t app_uart_close(void)
{
nrf_drv_uart_uninit(&app_uart_inst);
return NRF_SUCCESS;
}
#endif //NRF_MODULE_ENABLED(APP_UART)

View File

@ -1,176 +0,0 @@
/**
* Copyright (c) 2014 - 2017, 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, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, 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 Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
*
*/
#include "sdk_common.h"
/** @file
*
* @defgroup retarget Retarget layer for stdio functions
* @{
* @ingroup app_common
* @} */
#if NRF_MODULE_ENABLED(RETARGET)
#if !defined(NRF_LOG_USES_RTT) || NRF_LOG_USES_RTT != 1
#if !defined(HAS_SIMPLE_UART_RETARGET)
#include <stdio.h>
#include <stdint.h>
#include "app_uart.h"
#include "nrf_error.h"
#if defined(__CC_ARM)
// This part is taken from MDK-ARM template file and is required here to prevent
// linker from selecting libraries functions that use semihosting and failing
// because of multiple definitions of fgetc() and fputc().
// Refer to: http://www.keil.com/support/man/docs/gsac/gsac_retargetcortex.htm
// -- BEGIN --
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
// --- END ---
int fgetc(FILE * p_file)
{
uint8_t input;
while (app_uart_get(&input) == NRF_ERROR_NOT_FOUND)
{
// No implementation needed.
}
return input;
}
int fputc(int ch, FILE * p_file)
{
UNUSED_PARAMETER(p_file);
UNUSED_VARIABLE(app_uart_put((uint8_t)ch));
return ch;
}
#elif defined(__GNUC__) && defined(__SES_ARM)
int __getchar(FILE * p_file)
{
uint8_t input;
while (app_uart_get(&input) == NRF_ERROR_NOT_FOUND)
{
// No implementation needed.
}
return input;
}
int __putchar(int ch, FILE * p_file)
{
UNUSED_PARAMETER(p_file);
UNUSED_VARIABLE(app_uart_put((uint8_t)ch));
return ch;
}
#elif defined(__GNUC__) && !defined(__SES_ARM)
int _write(int file, const char * p_char, int len)
{
int i;
UNUSED_PARAMETER(file);
for (i = 0; i < len; i++)
{
UNUSED_VARIABLE(app_uart_put(*p_char++));
}
return len;
}
int _read(int file, char * p_char, int len)
{
UNUSED_PARAMETER(file);
while (app_uart_get((uint8_t *)p_char) == NRF_ERROR_NOT_FOUND)
{
// No implementation needed.
}
return 1;
}
#elif defined(__ICCARM__)
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
int i;
UNUSED_PARAMETER(handle);
for (i = 0; i < size; i++)
{
UNUSED_VARIABLE(app_uart_put(*buffer++));
}
return size;
}
size_t __read(int handle, unsigned char * buffer, size_t size)
{
UNUSED_PARAMETER(handle);
UNUSED_PARAMETER(size);
while (app_uart_get((uint8_t *)buffer) == NRF_ERROR_NOT_FOUND)
{
// No implementation needed.
}
return 1;
}
long __lseek(int handle, long offset, int whence)
{
return -1;
}
int __close(int handle)
{
return 0;
}
int remove(const char * filename)
{
return 0;
}
#endif
#endif // !defined(HAS_SIMPLE_UART_RETARGET)
#endif // NRF_LOG_USES_RTT != 1
#endif //NRF_MODULE_ENABLED(RETARGET)