update bootloader

This commit is contained in:
hathach 2018-02-15 16:27:51 +07:00
parent 8f4d0c6e3a
commit 59915c9acb
23 changed files with 2577 additions and 2440 deletions

View File

@ -57,7 +57,6 @@
#define NRF_LOG_LEVEL 0 #define NRF_LOG_LEVEL 0
#endif //UART_CONFIG_LOG_ENABLED #endif //UART_CONFIG_LOG_ENABLED
#include "nrf_log.h" #include "nrf_log.h"
NRF_LOG_MODULE_REGISTER(); NRF_LOG_MODULE_REGISTER();
#if (defined(UARTE_IN_USE) && defined(UART_IN_USE)) #if (defined(UARTE_IN_USE) && defined(UART_IN_USE))

View File

@ -10,6 +10,7 @@
* *
*/ */
#include "sdk_common.h"
#include "bootloader.h" #include "bootloader.h"
#include "bootloader_types.h" #include "bootloader_types.h"
#include "bootloader_util.h" #include "bootloader_util.h"
@ -25,9 +26,8 @@
#include "pstorage.h" #include "pstorage.h"
#include "app_scheduler.h" #include "app_scheduler.h"
#include "nrf_delay.h" #include "nrf_delay.h"
#include "sdk_common.h"
#include "app_timer_appsh.h" #include "app_timer.h"
#define APP_TIMER_PRESCALER 0 #define APP_TIMER_PRESCALER 0
@ -326,7 +326,7 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms)
_terminate_startup_dfu = false; _terminate_startup_dfu = false;
(void) app_timer_create(&_forced_startup_dfu_timer, APP_TIMER_MODE_SINGLE_SHOT, forced_startup_dfu_timer_handler); (void) app_timer_create(&_forced_startup_dfu_timer, APP_TIMER_MODE_SINGLE_SHOT, forced_startup_dfu_timer_handler);
app_timer_start(_forced_startup_dfu_timer, APP_TIMER_TICKS(timeout_ms, APP_TIMER_PRESCALER), NULL); app_timer_start(_forced_startup_dfu_timer, APP_TIMER_TICKS(timeout_ms), NULL);
} }
err_code = dfu_transport_serial_update_start(); err_code = dfu_transport_serial_update_start();

View File

@ -41,7 +41,7 @@ typedef enum
} dfu_state_t; } dfu_state_t;
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define DFU_TIMEOUT_INTERVAL APP_TIMER_TICKS(300000, APP_TIMER_PRESCALER) /**< DFU timeout interval in units of timer ticks. */ #define DFU_TIMEOUT_INTERVAL APP_TIMER_TICKS(300000) /**< DFU timeout interval in units of timer ticks. */
#define IS_UPDATING_SD(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_SD) /**< Macro for determining if a SoftDevice update is ongoing. */ #define IS_UPDATING_SD(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_SD) /**< Macro for determining if a SoftDevice update is ongoing. */
#define IS_UPDATING_BL(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_BL) /**< Macro for determining if a Bootloader update is ongoing. */ #define IS_UPDATING_BL(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_BL) /**< Macro for determining if a Bootloader update is ongoing. */

View File

@ -65,8 +65,8 @@ enum { BLE_CONN_CFG_HIGH_BANDWIDTH = 1 };
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(100, APP_TIMER_PRESCALER) /**< Time from the Connected event to first time sd_ble_gap_conn_param_update is called (100 milliseconds). */ #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(100) /**< Time from the Connected event to first time sd_ble_gap_conn_param_update is called (100 milliseconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(500, APP_TIMER_PRESCALER) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (500 milliseconds). */ #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(500) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (500 milliseconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */ #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
#define APP_ADV_INTERVAL MSEC_TO_UNITS(25, UNIT_0_625_MS) /**< The advertising interval (25 ms.). */ #define APP_ADV_INTERVAL MSEC_TO_UNITS(25, UNIT_0_625_MS) /**< The advertising interval (25 ms.). */

View File

@ -1,43 +1,53 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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(HCI_MEM_POOL)
#include "hci_mem_pool.h" #include "hci_mem_pool.h"
//#include "hci_mem_pool_internal.h"
#include "nordic_common.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
// Adafruit for dual transport serial + ble
#define BLE_TX_BUF_SIZE 4u /**< TX buffer size in bytes. */
#define BLE_RX_BUF_SIZE 32u /**< RX buffer size in bytes. */
#define BLE_RX_BUF_QUEUE_SIZE 8u /**< RX buffer element size. */
#define SERIAL_TX_BUF_SIZE 32u /**< TX buffer size in bytes. */
#define SERIAL_RX_BUF_SIZE 600u /**< RX buffer size in bytes. */
#define SERIAL_RX_BUF_QUEUE_SIZE 2u /**< RX buffer element size. */
extern bool is_ota(void);
#define RX_BUF_SIZE (is_ota() ? BLE_RX_BUF_SIZE : SERIAL_RX_BUF_SIZE)
#define TX_BUF_SIZE (is_ota() ? BLE_TX_BUF_SIZE : SERIAL_TX_BUF_SIZE)
#define RX_BUF_QUEUE_SIZE (is_ota() ? BLE_RX_BUF_QUEUE_SIZE : SERIAL_RX_BUF_QUEUE_SIZE)
/**@brief RX buffer element instance structure. /**@brief RX buffer element instance structure.
*/ */
typedef struct typedef struct
{ {
// uint8_t rx_buffer[RX_BUF_SIZE]; /**< RX buffer memory array. */ uint8_t rx_buffer[HCI_RX_BUF_SIZE]; /**< RX buffer memory array. */
uint8_t rx_buffer[MAX(BLE_RX_BUF_SIZE, SERIAL_RX_BUF_SIZE)];
uint32_t length; /**< Length of the RX buffer memory array. */ uint32_t length; /**< Length of the RX buffer memory array. */
} rx_buffer_elem_t; } rx_buffer_elem_t;
@ -55,7 +65,7 @@ typedef struct
} rx_buffer_queue_t; } rx_buffer_queue_t;
static bool m_is_tx_allocated; /**< Boolean value to determine if the TX buffer is allocated. */ static bool m_is_tx_allocated; /**< Boolean value to determine if the TX buffer is allocated. */
static rx_buffer_elem_t m_rx_buffer_elem_queue[MAX(BLE_RX_BUF_QUEUE_SIZE, SERIAL_RX_BUF_QUEUE_SIZE)]; /**< RX buffer element instances. */ static rx_buffer_elem_t m_rx_buffer_elem_queue[HCI_RX_BUF_QUEUE_SIZE]; /**< RX buffer element instances. */
static rx_buffer_queue_t m_rx_buffer_queue; /**< RX buffer queue element instance. */ static rx_buffer_queue_t m_rx_buffer_queue; /**< RX buffer queue element instance. */
@ -63,7 +73,7 @@ uint32_t hci_mem_pool_open(void)
{ {
m_is_tx_allocated = false; m_is_tx_allocated = false;
m_rx_buffer_queue.p_buffer = m_rx_buffer_elem_queue; m_rx_buffer_queue.p_buffer = m_rx_buffer_elem_queue;
m_rx_buffer_queue.free_window_count = RX_BUF_QUEUE_SIZE; m_rx_buffer_queue.free_window_count = HCI_RX_BUF_QUEUE_SIZE;
m_rx_buffer_queue.free_available_count = 0; m_rx_buffer_queue.free_available_count = 0;
m_rx_buffer_queue.read_available_count = 0; m_rx_buffer_queue.read_available_count = 0;
m_rx_buffer_queue.write_index = 0; m_rx_buffer_queue.write_index = 0;
@ -82,7 +92,7 @@ uint32_t hci_mem_pool_close(void)
uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer) uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer)
{ {
static uint8_t tx_buffer[MAX(BLE_TX_BUF_SIZE, SERIAL_TX_BUF_SIZE)]; static uint8_t tx_buffer[HCI_TX_BUF_SIZE];
uint32_t err_code; uint32_t err_code;
@ -126,7 +136,7 @@ uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
if (m_rx_buffer_queue.free_window_count != 0) if (m_rx_buffer_queue.free_window_count != 0)
{ {
if (length <= RX_BUF_SIZE) if (length <= HCI_RX_BUF_SIZE)
{ {
--(m_rx_buffer_queue.free_window_count); --(m_rx_buffer_queue.free_window_count);
++(m_rx_buffer_queue.read_available_count); ++(m_rx_buffer_queue.read_available_count);
@ -140,7 +150,7 @@ uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
// power of two and two's complement arithmetic. For details refer example to book // power of two and two's complement arithmetic. For details refer example to book
// "Making embedded systems: Elicia White". // "Making embedded systems: Elicia White".
m_rx_buffer_queue.write_index = m_rx_buffer_queue.write_index =
(m_rx_buffer_queue.write_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u); (m_rx_buffer_queue.write_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
err_code = NRF_SUCCESS; err_code = NRF_SUCCESS;
} }
@ -170,7 +180,7 @@ uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer)
// Start at read_index minus free_available_count and then increment until read index. // Start at read_index minus free_available_count and then increment until read index.
err_code = NRF_ERROR_INVALID_ADDR; err_code = NRF_ERROR_INVALID_ADDR;
consume_index = (m_rx_buffer_queue.read_index - m_rx_buffer_queue.free_available_count) & consume_index = (m_rx_buffer_queue.read_index - m_rx_buffer_queue.free_available_count) &
(RX_BUF_QUEUE_SIZE - 1u); (HCI_RX_BUF_QUEUE_SIZE - 1u);
start_index = consume_index; start_index = consume_index;
do do
@ -183,7 +193,7 @@ uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer)
} }
else else
{ {
consume_index = (consume_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u); consume_index = (consume_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
} }
} }
while (consume_index != m_rx_buffer_queue.read_index); while (consume_index != m_rx_buffer_queue.read_index);
@ -193,7 +203,7 @@ uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer)
{ {
--(m_rx_buffer_queue.free_available_count); --(m_rx_buffer_queue.free_available_count);
++(m_rx_buffer_queue.free_window_count); ++(m_rx_buffer_queue.free_window_count);
start_index = (consume_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u); start_index = (consume_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
} }
} }
else else
@ -210,7 +220,7 @@ uint32_t hci_mem_pool_rx_data_size_set(uint32_t length)
// @note: Adjust the write_index making use of the fact that the buffer size is of power // @note: Adjust the write_index making use of the fact that the buffer size is of power
// of two and two's complement arithmetic. For details refer example to book // of two and two's complement arithmetic. For details refer example to book
// "Making embedded systems: Elicia White". // "Making embedded systems: Elicia White".
const uint32_t index = (m_rx_buffer_queue.write_index - 1u) & (RX_BUF_QUEUE_SIZE - 1u); const uint32_t index = (m_rx_buffer_queue.write_index - 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
m_rx_buffer_queue.p_buffer[index].length = length; m_rx_buffer_queue.p_buffer[index].length = length;
return NRF_SUCCESS; return NRF_SUCCESS;
@ -240,7 +250,7 @@ uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length)
// of two and two's complement arithmetic. For details refer example to book // of two and two's complement arithmetic. For details refer example to book
// "Making embedded systems: Elicia White". // "Making embedded systems: Elicia White".
m_rx_buffer_queue.read_index = m_rx_buffer_queue.read_index =
(m_rx_buffer_queue.read_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u); (m_rx_buffer_queue.read_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u);
err_code = NRF_SUCCESS; err_code = NRF_SUCCESS;
} }
@ -251,3 +261,4 @@ uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length)
return err_code; return err_code;
} }
#endif //NRF_MODULE_ENABLED(HCI_MEM_POOL)

View File

@ -1,18 +1,45 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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.
* *
*/ */
/** @file /** @file
* *
* @defgroup memory_pool Memory pool * @defgroup hci_mem_pool Memory pool
* @{ * @{
* @ingroup app_common * @ingroup app_common
* *
@ -24,7 +51,7 @@
* design of the circular buffer implementing the RX memory layout is illustrated in the picture * design of the circular buffer implementing the RX memory layout is illustrated in the picture
* below. * below.
* *
* @image html memory_pool.png "Circular buffer design" * @image html memory_pool.svg "Circular buffer design"
* *
* The expected call order for the RX APIs is as follows: * The expected call order for the RX APIs is as follows:
* - hci_mem_pool_rx_produce * - hci_mem_pool_rx_produce
@ -48,6 +75,10 @@
#include <stdint.h> #include <stdint.h>
#include "nrf_error.h" #include "nrf_error.h"
#ifdef __cplusplus
extern "C" {
#endif
/**@brief Function for opening the module. /**@brief Function for opening the module.
* *
* @retval NRF_SUCCESS Operation success. * @retval NRF_SUCCESS Operation success.
@ -127,6 +158,11 @@ uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length);
*/ */
uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer); uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer);
#ifdef __cplusplus
}
#endif
#endif // HCI_MEM_POOL_H__ #endif // HCI_MEM_POOL_H__
/** @} */ /** @} */

View File

@ -1,18 +1,46 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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(HCI_SLIP)
#include "hci_slip.h" #include "hci_slip.h"
#include <stdlib.h> #include <stdlib.h>
#include "hci_transport_config.h"
#include "app_uart.h" #include "app_uart.h"
#include "nrf_error.h" #include "nrf_error.h"
@ -121,7 +149,7 @@ static uint32_t send_tx_byte_encoded(void)
{ {
uint32_t err_code; uint32_t err_code;
switch(mp_tx_buffer[m_tx_buffer_index]) switch (mp_tx_buffer[m_tx_buffer_index])
{ {
case APP_SLIP_END: case APP_SLIP_END:
err_code = app_uart_put(APP_SLIP_ESC_END); err_code = app_uart_put(APP_SLIP_ESC_END);
@ -333,19 +361,19 @@ static uint32_t slip_uart_open(void)
app_uart_comm_params_t comm_params = app_uart_comm_params_t comm_params =
{ {
HCI_SLIP_UART_RX_PIN_NUMBER, HCI_UART_RX_PIN,
HCI_SLIP_UART_TX_PIN_NUMBER, HCI_UART_TX_PIN,
HCI_SLIP_UART_RTS_PIN_NUMBER, HCI_UART_RTS_PIN,
HCI_SLIP_UART_CTS_PIN_NUMBER, HCI_UART_CTS_PIN,
HCI_SLIP_UART_MODE, (app_uart_flow_control_t)HCI_UART_FLOW_CONTROL,
false, false,
HCI_SLIP_UART_BAUDRATE HCI_UART_BAUDRATE
}; };
err_code = app_uart_init(&comm_params, err_code = app_uart_init(&comm_params,
NULL, NULL,
slip_uart_eventhandler, slip_uart_eventhandler,
APP_IRQ_PRIORITY_LOW); APP_IRQ_PRIORITY_LOWEST);
if (err_code == NRF_SUCCESS) if (err_code == NRF_SUCCESS)
{ {
@ -426,3 +454,4 @@ uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length)
handle_rx_byte = handle_rx_byte_wait_start; handle_rx_byte = handle_rx_byte_wait_start;
return NRF_SUCCESS; return NRF_SUCCESS;
} }
#endif //NRF_MODULE_ENABLED(HCI_SLIP)

View File

@ -1,15 +1,42 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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.
* *
*/ */
/** @file /** @file
* *
* @defgroup hci_slip SLIP module * @defgroup hci_slip SLIP module
@ -33,6 +60,10 @@
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**@brief Event types from the SLIP Layer. */ /**@brief Event types from the SLIP Layer. */
typedef enum typedef enum
{ {
@ -124,6 +155,11 @@ uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length);
*/ */
uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length); uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length);
#ifdef __cplusplus
}
#endif
#endif // HCI_SLIP_H__ #endif // HCI_SLIP_H__
/** @} */ /** @} */

View File

@ -1,25 +1,51 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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"
#include "hci_transport_config.h" #if NRF_MODULE_ENABLED(HCI_TRANSPORT)
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_slip.h" #include "hci_slip.h"
#include "crc16.h" #include "crc16.h"
#include "hci_mem_pool.h" #include "hci_mem_pool.h"
#include "hci_mem_pool_internal.h"
#include "app_timer.h" #include "app_timer.h"
#include "app_error.h" #include "app_error.h"
#include <stdio.h> #include <stdio.h>
#include "sdk_common.h"
#define PKT_HDR_SIZE 4u /**< Packet header size in number of bytes. */ #define PKT_HDR_SIZE 4u /**< Packet header size in number of bytes. */
#define PKT_CRC_SIZE 2u /**< Packet CRC size in number of bytes. */ #define PKT_CRC_SIZE 2u /**< Packet CRC size in number of bytes. */
@ -30,10 +56,12 @@
#define INITIAL_ACK_NUMBER_EXPECTED 1u /**< Initial acknowledge number expected. */ #define INITIAL_ACK_NUMBER_EXPECTED 1u /**< Initial acknowledge number expected. */
#define INITIAL_ACK_NUMBER_TX INITIAL_ACK_NUMBER_EXPECTED /**< Initial acknowledge number transmitted. */ #define INITIAL_ACK_NUMBER_TX INITIAL_ACK_NUMBER_EXPECTED /**< Initial acknowledge number transmitted. */
#define INVALID_PKT_TYPE 0xFFFFFFFFu /**< Internal invalid packet type value. */ #define INVALID_PKT_TYPE 0xFFFFFFFFu /**< Internal invalid packet type value. */
#define MAX_TRANSMISSION_TIME (ROUNDED_DIV((MAX_PACKET_SIZE_IN_BITS * 1000u), USED_BAUD_RATE)) /**< Max transmission time of a single application packet over UART in units of mseconds. */ #define HCI_UART_REG_VALUE_TO_BAUDRATE(BAUDRATE) ((BAUDRATE)/268) /**< Estimated relation between UART baudrate register value and actual baudrate */
#define MAX_TRANSMISSION_TIME \
(ROUNDED_DIV((HCI_MAX_PACKET_SIZE_IN_BITS * 1000u), \
HCI_UART_REG_VALUE_TO_BAUDRATE(HCI_UART_BAUDRATE))) /**< Max transmission time of a single application packet over UART in units of mseconds. */
#define RETRANSMISSION_TIMEOUT_IN_MS (3u * MAX_TRANSMISSION_TIME) /**< Retransmission timeout for application packet in units of mseconds. */ #define RETRANSMISSION_TIMEOUT_IN_MS (3u * MAX_TRANSMISSION_TIME) /**< Retransmission timeout for application packet in units of mseconds. */
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ #define RETRANSMISSION_TIMEOUT_IN_TICKS APP_TIMER_TICKS(RETRANSMISSION_TIMEOUT_IN_MS) /**< Retransmission timeout for application packet in units of timer ticks. */
#define RETRANSMISSION_TIMEOUT_IN_TICKS APP_TIMER_TICKS(RETRANSMISSION_TIMEOUT_IN_MS, APP_TIMER_PRESCALER) /**< Retransmission timeout for application packet in units of timer ticks. */
#define MAX_RETRY_COUNT 5u /**< Max retransmission retry count for application packets. */ #define MAX_RETRY_COUNT 5u /**< Max retransmission retry count for application packets. */
#define ACK_BUF_SIZE 5u /**< Length of module internal RX buffer which is big enough to hold an acknowledgement packet. */ #define ACK_BUF_SIZE 5u /**< Length of module internal RX buffer which is big enough to hold an acknowledgement packet. */
@ -255,14 +283,14 @@ static void rx_vendor_specific_pkt_type_handle(const uint8_t * p_buffer, uint32_
err_code = hci_mem_pool_rx_data_size_set(length); err_code = hci_mem_pool_rx_data_size_set(length);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
err_code = hci_mem_pool_rx_produce(RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer); err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer);
APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM)); APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM));
// If memory pool RX buffer produce succeeded we register that buffer to slip layer // If memory pool RX buffer produce succeeded we register that buffer to slip layer
// otherwise we register the internal acknowledgement buffer. // otherwise we register the internal acknowledgement buffer.
err_code = hci_slip_rx_buffer_register( err_code = hci_slip_rx_buffer_register(
(err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer, (err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer,
(err_code == NRF_SUCCESS) ? RX_BUF_SIZE : ACK_BUF_SIZE); (err_code == NRF_SUCCESS) ? HCI_RX_BUF_SIZE : ACK_BUF_SIZE);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
@ -277,7 +305,7 @@ static void rx_vendor_specific_pkt_type_handle(const uint8_t * p_buffer, uint32_
{ {
// RX packet discarded: sequence number not valid, set the same buffer to slip layer in // RX packet discarded: sequence number not valid, set the same buffer to slip layer in
// order to avoid buffer overrun. // order to avoid buffer overrun.
err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE); err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
// As packet did not have expected sequence number: send acknowledgement with the // As packet did not have expected sequence number: send acknowledgement with the
@ -289,7 +317,7 @@ static void rx_vendor_specific_pkt_type_handle(const uint8_t * p_buffer, uint32_
{ {
// RX packet discarded: reset the same buffer to slip layer in order to avoid buffer // RX packet discarded: reset the same buffer to slip layer in order to avoid buffer
// overrun. // overrun.
err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE); err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
} }
} }
@ -502,19 +530,19 @@ void slip_event_handle(hci_slip_evt_t event)
// producing fails use the internal acknowledgement buffer. // producing fails use the internal acknowledgement buffer.
if (mp_slip_used_rx_buffer != NULL) if (mp_slip_used_rx_buffer != NULL)
{ {
err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE); err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
} }
else else
{ {
err_code = hci_mem_pool_rx_produce(RX_BUF_SIZE, err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE,
(void **)&mp_slip_used_rx_buffer); (void **)&mp_slip_used_rx_buffer);
APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) ||
(err_code == NRF_ERROR_NO_MEM)); (err_code == NRF_ERROR_NO_MEM));
err_code = hci_slip_rx_buffer_register( err_code = hci_slip_rx_buffer_register(
(err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer, (err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer,
(err_code == NRF_SUCCESS) ? RX_BUF_SIZE : ACK_BUF_SIZE); (err_code == NRF_SUCCESS) ? HCI_RX_BUF_SIZE : ACK_BUF_SIZE);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
} }
break; break;
@ -603,14 +631,14 @@ uint32_t hci_transport_open(void)
err_code = hci_slip_open(); err_code = hci_slip_open();
VERIFY_SUCCESS(err_code); VERIFY_SUCCESS(err_code);
err_code = hci_mem_pool_rx_produce(RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer); err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer);
if (err_code != NRF_SUCCESS) if (err_code != NRF_SUCCESS)
{ {
// @note: conduct required interface adjustment. // @note: conduct required interface adjustment.
return NRF_ERROR_INTERNAL; return NRF_ERROR_INTERNAL;
} }
err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE); err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE);
return err_code; return err_code;
} }
@ -777,3 +805,4 @@ uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer)
{ {
return (hci_mem_pool_rx_consume(p_buffer - PKT_HDR_SIZE)); return (hci_mem_pool_rx_consume(p_buffer - PKT_HDR_SIZE));
} }
#endif //NRF_MODULE_ENABLED(HCI_TRANSPORT)

View File

@ -1,15 +1,42 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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.
* *
*/ */
/**@file /**@file
* *
* @defgroup hci_transport HCI Transport * @defgroup hci_transport HCI Transport
@ -57,7 +84,7 @@
* *
* The application TX packet processing flow is illustrated by the statemachine below. * The application TX packet processing flow is illustrated by the statemachine below.
* *
* @image html hci_transport_tx_sm.png "TX - application packet statemachine" * @image html hci_transport_tx_sm.svg "TX - application packet statemachine"
* *
* \par Component specific configuration options * \par Component specific configuration options
* *
@ -78,6 +105,10 @@
#include <stdint.h> #include <stdint.h>
#include "nrf_error.h" #include "nrf_error.h"
#ifdef __cplusplus
extern "C" {
#endif
/**@brief Generic event callback function events. */ /**@brief Generic event callback function events. */
typedef enum typedef enum
{ {
@ -215,6 +246,11 @@ uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length)
*/ */
uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer); uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer);
#ifdef __cplusplus
}
#endif
#endif // HCI_TRANSPORT_H__ #endif // HCI_TRANSPORT_H__
/** @} */ /** @} */

View File

@ -1,22 +1,50 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2012 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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_SCHEDULER)
#include "app_scheduler.h" #include "app_scheduler.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "nrf_soc.h" #include "nrf_soc.h"
#include "nrf_assert.h" #include "nrf_assert.h"
#include "app_util.h"
#include "app_util_platform.h" #include "app_util_platform.h"
/**@brief Structure for holding a scheduled event header. */ /**@brief Structure for holding a scheduled event header. */
@ -35,10 +63,15 @@ static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the
static uint16_t m_queue_event_size; /**< Maximum event size in queue. */ static uint16_t m_queue_event_size; /**< Maximum event size in queue. */
static uint16_t m_queue_size; /**< Number of queue entries. */ static uint16_t m_queue_size; /**< Number of queue entries. */
#ifdef APP_SCHEDULER_WITH_PROFILER #if APP_SCHEDULER_WITH_PROFILER
static uint16_t m_max_queue_utilization; /**< Maximum observed queue utilization. */ static uint16_t m_max_queue_utilization; /**< Maximum observed queue utilization. */
#endif #endif
#if APP_SCHEDULER_WITH_PAUSE
static uint32_t m_scheduler_paused_counter = 0; /**< Counter storing the difference between pausing
and resuming the scheduler. */
#endif
/**@brief Function for incrementing a queue index, and handle wrap-around. /**@brief Function for incrementing a queue index, and handle wrap-around.
* *
* @param[in] index Old index. * @param[in] index Old index.
@ -89,7 +122,7 @@ uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event
m_queue_event_size = event_size; m_queue_event_size = event_size;
m_queue_size = queue_size; m_queue_size = queue_size;
#ifdef APP_SCHEDULER_WITH_PROFILER #if APP_SCHEDULER_WITH_PROFILER
m_max_queue_utilization = 0; m_max_queue_utilization = 0;
#endif #endif
@ -97,7 +130,17 @@ uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event
} }
#ifdef APP_SCHEDULER_WITH_PROFILER uint16_t app_sched_queue_space_get()
{
uint16_t start = m_queue_start_index;
uint16_t end = m_queue_end_index;
uint16_t free_space = m_queue_size - ((end >= start) ?
(end - start) : (m_queue_size + 1 - start + end));
return free_space;
}
#if APP_SCHEDULER_WITH_PROFILER
static void queue_utilization_check(void) static void queue_utilization_check(void)
{ {
uint16_t start = m_queue_start_index; uint16_t start = m_queue_start_index;
@ -115,10 +158,10 @@ uint16_t app_sched_queue_utilization_get(void)
{ {
return m_max_queue_utilization; return m_max_queue_utilization;
} }
#endif #endif // APP_SCHEDULER_WITH_PROFILER
uint32_t app_sched_event_put(void * p_event_data, uint32_t app_sched_event_put(void const * p_event_data,
uint16_t event_data_size, uint16_t event_data_size,
app_sched_event_handler_t handler) app_sched_event_handler_t handler)
{ {
@ -135,7 +178,7 @@ uint32_t app_sched_event_put(void * p_event_data,
event_index = m_queue_end_index; event_index = m_queue_end_index;
m_queue_end_index = next_index(m_queue_end_index); m_queue_end_index = next_index(m_queue_end_index);
#ifdef APP_SCHEDULER_WITH_PROFILER #if APP_SCHEDULER_WITH_PROFILER
// This function call must be protected with critical region because // This function call must be protected with critical region because
// it modifies 'm_max_queue_utilization'. // it modifies 'm_max_queue_utilization'.
queue_utilization_check(); queue_utilization_check();
@ -177,51 +220,69 @@ uint32_t app_sched_event_put(void * p_event_data,
} }
/**@brief Function for reading the next event from specified event queue. #if APP_SCHEDULER_WITH_PAUSE
* void app_sched_pause(void)
* @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; CRITICAL_REGION_ENTER();
if (!APP_SCHED_QUEUE_EMPTY()) if (m_scheduler_paused_counter < UINT32_MAX)
{ {
uint16_t event_index; m_scheduler_paused_counter++;
// 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;
} }
CRITICAL_REGION_EXIT();
}
return err_code; void app_sched_resume(void)
{
CRITICAL_REGION_ENTER();
if (m_scheduler_paused_counter > 0)
{
m_scheduler_paused_counter--;
}
CRITICAL_REGION_EXIT();
}
#endif //APP_SCHEDULER_WITH_PAUSE
/**@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)
{
#if APP_SCHEDULER_WITH_PAUSE
return (m_scheduler_paused_counter > 0);
#else
return false;
#endif
} }
void app_sched_execute(void) void app_sched_execute(void)
{ {
void * p_event_data; while (!is_app_sched_paused() && !APP_SCHED_QUEUE_EMPTY())
uint16_t event_data_size;
app_sched_event_handler_t event_handler;
// Get next event (if any), and execute handler
while ((app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS))
{ {
// Since this function is only called from the main loop, there is no
// need for a critical region here, however a special care must be taken
// regarding update of the queue start index (see the end of the loop).
uint16_t event_index = m_queue_start_index;
void * p_event_data;
uint16_t event_data_size;
app_sched_event_handler_t event_handler;
p_event_data = &m_queue_event_data[event_index * m_queue_event_size];
event_data_size = m_queue_event_headers[event_index].event_data_size;
event_handler = m_queue_event_headers[event_index].handler;
event_handler(p_event_data, event_data_size); event_handler(p_event_data, event_data_size);
// Event processed, now it is safe to move the queue start index,
// so the queue entry occupied by this event can be used to store
// a next one.
m_queue_start_index = next_index(m_queue_start_index);
} }
} }
#endif //NRF_MODULE_ENABLED(APP_SCHEDULER)

View File

@ -1,15 +1,42 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2012 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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.
* *
*/ */
/** @file /** @file
* *
* @defgroup app_scheduler Scheduler * @defgroup app_scheduler Scheduler
@ -44,16 +71,21 @@
* @ref ble_sdk_app_hids_mouse and @ref ble_sdk_app_hids_keyboard. * @ref ble_sdk_app_hids_mouse and @ref ble_sdk_app_hids_keyboard.
* @endif * @endif
* *
* @image html scheduler_working.jpg The high level design of the scheduler * @image html scheduler_working.svg The high level design of the scheduler
*/ */
#ifndef APP_SCHEDULER_H__ #ifndef APP_SCHEDULER_H__
#define APP_SCHEDULER_H__ #define APP_SCHEDULER_H__
#include "sdk_config.h"
#include <stdint.h> #include <stdint.h>
#include "app_error.h" #include "app_error.h"
#include "app_util.h" #include "app_util.h"
#ifdef __cplusplus
extern "C" {
#endif
#define APP_SCHED_EVENT_HEADER_SIZE 8 /**< Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()). */ #define APP_SCHED_EVENT_HEADER_SIZE 8 /**< Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()). */
/**@brief Compute number of bytes required to hold the scheduler buffer. /**@brief Compute number of bytes required to hold the scheduler buffer.
@ -123,31 +155,41 @@ void app_sched_execute(void);
* @details Puts an event into the event queue. * @details Puts an event into the event queue.
* *
* @param[in] p_event_data Pointer to event data to be scheduled. * @param[in] p_event_data Pointer to event data to be scheduled.
* @param[in] event_size Size of event data to be scheduled. * @param[in] event_size Size of event data to be scheduled.
* @param[in] handler Event handler to receive the event. * @param[in] handler Event handler to receive the event.
* *
* @return NRF_SUCCESS on success, otherwise an error code. * @return NRF_SUCCESS on success, otherwise an error code.
*/ */
uint32_t app_sched_event_put(void * p_event_data, uint32_t app_sched_event_put(void const * p_event_data,
uint16_t event_size, uint16_t event_size,
app_sched_event_handler_t handler); app_sched_event_handler_t handler);
#ifdef APP_SCHEDULER_WITH_PROFILER
/**@brief Function for getting the maximum observed queue utilization. /**@brief Function for getting the maximum observed queue utilization.
* *
* Function for tuning the module and determining QUEUE_SIZE value and thus module RAM usage. * Function for tuning the module and determining QUEUE_SIZE value and thus module RAM usage.
* *
* @note @ref APP_SCHEDULER_WITH_PROFILER must be enabled to use this functionality.
*
* @return Maximum number of events in queue observed so far. * @return Maximum number of events in queue observed so far.
*/ */
uint16_t app_sched_queue_utilization_get(void); uint16_t app_sched_queue_utilization_get(void);
#endif
#ifdef APP_SCHEDULER_WITH_PAUSE /**@brief Function for getting the current amount of free space in the queue.
*
* @details The real amount of free space may be less if entries are being added from an interrupt.
* To get the sxact value, this function should be called from the critical section.
*
* @return Amount of free space in the queue.
*/
uint16_t app_sched_queue_space_get(void);
/**@brief A function to pause the scheduler. /**@brief A function to pause the scheduler.
* *
* @details When the scheduler is paused events are not pulled from the scheduler queue for * @details When the scheduler is paused events are not pulled from the scheduler queue for
* processing. The function can be called multiple times. To unblock the scheduler the * processing. The function can be called multiple times. To unblock the scheduler the
* function @ref app_sched_resume has to be called the same number of times. * function @ref app_sched_resume has to be called the same number of times.
*
* @note @ref APP_SCHEDULER_WITH_PAUSE must be enabled to use this functionality.
*/ */
void app_sched_pause(void); void app_sched_pause(void);
@ -155,9 +197,15 @@ void app_sched_pause(void);
* *
* @details To unblock the scheduler this function has to be called the same number of times as * @details To unblock the scheduler this function has to be called the same number of times as
* @ref app_sched_pause function. * @ref app_sched_pause function.
*
* @note @ref APP_SCHEDULER_WITH_PAUSE must be enabled to use this functionality.
*/ */
void app_sched_resume(void); void app_sched_resume(void);
#ifdef __cplusplus
}
#endif #endif
#endif // APP_SCHEDULER_H__ #endif // APP_SCHEDULER_H__
/** @} */ /** @} */

View File

@ -1,15 +1,44 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2012 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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_TIMER)
#include "app_timer.h" #include "app_timer.h"
#include <stdlib.h> #include <stdlib.h>
#include "nrf.h" #include "nrf.h"
@ -17,10 +46,12 @@
#include "app_error.h" #include "app_error.h"
#include "nrf_delay.h" #include "nrf_delay.h"
#include "app_util_platform.h" #include "app_util_platform.h"
#include "sdk_common.h" #if APP_TIMER_CONFIG_USE_SCHEDULER
#include "app_scheduler.h"
#endif
#define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the RTC1 interrupt (used for checking for timeouts and executing timeout handlers). */ #define RTC1_IRQ_PRI APP_TIMER_CONFIG_IRQ_PRIORITY /**< Priority of the RTC1 interrupt (used for checking for timeouts and executing timeout handlers). */
#define SWI_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the SWI interrupt (used for updating the timer list). */ #define SWI_IRQ_PRI APP_TIMER_CONFIG_IRQ_PRIORITY /**< Priority of the SWI interrupt (used for updating the timer list). */
// The current design assumes that both interrupt handlers run at the same interrupt level. // The current design assumes that both interrupt handlers run at the same interrupt level.
// If this is to be changed, protection must be added to prevent them from interrupting each other // If this is to be changed, protection must be added to prevent them from interrupting each other
@ -29,22 +60,22 @@ STATIC_ASSERT(RTC1_IRQ_PRI == SWI_IRQ_PRI);
#define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */ #define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */
#define APP_HIGH_USER_ID 0 /**< User Id for the Application High "user". */
#define APP_LOW_USER_ID 1 /**< User Id for the Application Low "user". */
#define THREAD_MODE_USER_ID 2 /**< User Id for the Thread Mode "user". */
#define RTC_COMPARE_OFFSET_MIN 3 /**< Minimum offset between the current RTC counter value and the Capture Compare register. Although the nRF51 Series User Specification recommends this value to be 2, we use 3 to be safer.*/ #define RTC_COMPARE_OFFSET_MIN 3 /**< Minimum offset between the current RTC counter value and the Capture Compare register. Although the nRF51 Series User Specification recommends this value to be 2, we use 3 to be safer.*/
#define MAX_RTC_TASKS_DELAY 47 /**< Maximum delay until an RTC task is executed. */ #define MAX_RTC_TASKS_DELAY 47 /**< Maximum delay until an RTC task is executed. */
#ifdef NRF51 #if (APP_TIMER_CONFIG_SWI_NUMBER == 0)
#define SWI_IRQn SWI0_IRQn #define SWI_IRQn SWI0_IRQn
#define SWI_IRQHandler SWI0_IRQHandler #define SWI_IRQHandler SWI0_IRQHandler
#elif defined(NRF52832_XXAA) || defined(NRF52840_XXAA) #elif (APP_TIMER_CONFIG_SWI_NUMBER == 1)
#define SWI_IRQn SWI0_EGU0_IRQn #define SWI_IRQn SWI1_IRQn
#define SWI_IRQHandler SWI0_EGU0_IRQHandler #define SWI_IRQHandler SWI1_IRQHandler
#else
#error "Unsupported SWI number."
#endif #endif
#define MODULE_INITIALIZED (m_op_queue.size != 0) /**< Macro designating whether the module has been initialized properly. */
/**@brief Timer node type. The nodes will be used form a linked list of running timers. */ /**@brief Timer node type. The nodes will be used form a linked list of running timers. */
typedef struct typedef struct
{ {
@ -90,57 +121,36 @@ typedef struct
} params; } params;
} timer_user_op_t; } timer_user_op_t;
STATIC_ASSERT(sizeof(timer_user_op_t) <= APP_TIMER_USER_OP_SIZE); /**@brief Structure describing a timer operations queue.
STATIC_ASSERT(sizeof(timer_user_op_t) % 4 == 0);
/**@brief Structure describing a timer user.
* *
* @details For each user of the timer module, there will be a timer operations queue. This queue * @details This queue will hold timer operations issued by the application
* will hold timer operations issued by this user until the timer interrupt handler * until the timer interrupt handler processes these operations.
* processes these operations. For the current implementation, there will be one user for
* each interrupt level available to the application (APP_HIGH, APP_LOW and THREAD_MODE),
* but the module can easily be modified to e.g. have one queue per process when using an
* RTOS. The purpose of the queues is to be able to have a completely lockless timer
* implementation.
*/ */
typedef struct typedef struct
{ {
uint8_t first; /**< Index of first entry to have been inserted in the queue (i.e. the next entry to be executed). */ uint8_t first; /**< Index of first entry to have been inserted in the queue (i.e. the next entry to be executed). */
uint8_t last; /**< Index of last entry to have been inserted in the queue. */ uint8_t last; /**< Index of last entry to have been inserted in the queue. */
uint8_t user_op_queue_size; /**< Queue size. */ uint8_t size; /**< Queue size. */
timer_user_op_t * p_user_op_queue; /**< Queue buffer. */ timer_user_op_t user_op_queue[APP_TIMER_CONFIG_OP_QUEUE_SIZE+1]; /**< Queue buffer. */
} timer_user_t; } timer_op_queue_t;
STATIC_ASSERT(sizeof(timer_user_t) == APP_TIMER_USER_SIZE); STATIC_ASSERT(sizeof(timer_op_queue_t) % 4 == 0);
STATIC_ASSERT(sizeof(timer_user_t) % 4 == 0);
/**@brief User id type.
*
* @details In the current implementation, this will automatically be generated from the current
* interrupt level.
*/
typedef uint32_t timer_user_id_t;
#define CONTEXT_QUEUE_SIZE_MAX (2) #define CONTEXT_QUEUE_SIZE_MAX (2)
static uint8_t m_user_array_size; /**< Size of timer user array. */ static timer_op_queue_t m_op_queue; /**< Timer operations queue. */
static timer_user_t * mp_users = NULL; /**< Array of timer users. */
static timer_node_t * mp_timer_id_head; /**< First timer in list of running timers. */ static timer_node_t * mp_timer_id_head; /**< First timer in list of running timers. */
static uint32_t m_ticks_latest; /**< Last known RTC counter value. */ static uint32_t m_ticks_latest; /**< Last known RTC counter value. */
static uint32_t m_ticks_elapsed[CONTEXT_QUEUE_SIZE_MAX]; /**< Timer internal elapsed ticks queue. */ static uint32_t m_ticks_elapsed[CONTEXT_QUEUE_SIZE_MAX]; /**< Timer internal elapsed ticks queue. */
static uint8_t m_ticks_elapsed_q_read_ind; /**< Timer internal elapsed ticks queue read index. */ static uint8_t m_ticks_elapsed_q_read_ind; /**< Timer internal elapsed ticks queue read index. */
static uint8_t m_ticks_elapsed_q_write_ind; /**< Timer internal elapsed ticks queue write index. */ static uint8_t m_ticks_elapsed_q_write_ind; /**< Timer internal elapsed ticks queue write index. */
static app_timer_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating timeout events to the scheduler. */
static bool m_rtc1_running; /**< Boolean indicating if RTC1 is running. */ static bool m_rtc1_running; /**< Boolean indicating if RTC1 is running. */
static bool m_rtc1_reset; /**< Boolean indicating if RTC1 counter has been reset due to last timer removed from timer list during the timer list handling. */ static bool m_rtc1_reset; /**< Boolean indicating if RTC1 counter has been reset due to last timer removed from timer list during the timer list handling. */
#ifdef APP_TIMER_WITH_PROFILER #if APP_TIMER_WITH_PROFILER
static uint8_t m_max_user_op_queue_utilization; /**< Maximum observed timer user operations queue utilization. */ static uint8_t m_max_user_op_queue_utilization; /**< Maximum observed timer user operations queue utilization. */
#endif #endif
#define MODULE_INITIALIZED (mp_users != NULL)
#include "sdk_macros.h"
/**@brief Function for initializing the RTC1 counter. /**@brief Function for initializing the RTC1 counter.
* *
* @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling. * @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling.
@ -272,14 +282,18 @@ static void timer_list_insert(timer_node_t * p_timer)
/**@brief Function for removing a timer from the timer queue. /**@brief Function for removing a timer from the timer queue.
* *
* @param[in] timer_id Id of timer to remove. * @param[in] timer_id Id of timer to remove.
*
* @return TRUE if Capture Compare register must be updated, FALSE otherwise.
*/ */
static void timer_list_remove(timer_node_t * p_timer) static bool timer_list_remove(timer_node_t * p_timer)
{ {
timer_node_t * p_old_head;
timer_node_t * p_previous; timer_node_t * p_previous;
timer_node_t * p_current; timer_node_t * p_current;
uint32_t timeout; uint32_t timeout;
// Find the timer's position in timer list. // Find the timer's position in timer list.
p_old_head = mp_timer_id_head;
p_previous = mp_timer_id_head; p_previous = mp_timer_id_head;
p_current = p_previous; p_current = p_previous;
@ -296,7 +310,7 @@ static void timer_list_remove(timer_node_t * p_timer)
// Timer not in active list. // Timer not in active list.
if (p_current == NULL) if (p_current == NULL)
{ {
return; return false;
} }
// Timer is the first in the list // Timer is the first in the list
@ -310,6 +324,7 @@ static void timer_list_remove(timer_node_t * p_timer)
NRF_RTC1->TASKS_CLEAR = 1; NRF_RTC1->TASKS_CLEAR = 1;
m_ticks_latest = 0; m_ticks_latest = 0;
m_rtc1_reset = true; m_rtc1_reset = true;
nrf_delay_us(MAX_RTC_TASKS_DELAY);
} }
} }
@ -325,6 +340,8 @@ static void timer_list_remove(timer_node_t * p_timer)
{ {
p_current->ticks_to_expire += timeout; p_current->ticks_to_expire += timeout;
} }
return (p_old_head != mp_timer_id_head);
} }
@ -343,6 +360,15 @@ static void timer_list_handler_sched(void)
NVIC_SetPendingIRQ(SWI_IRQn); NVIC_SetPendingIRQ(SWI_IRQn);
} }
#if APP_TIMER_CONFIG_USE_SCHEDULER
static void timeout_handler_scheduled_exec(void * p_event_data, uint16_t event_size)
{
APP_ERROR_CHECK_BOOL(event_size == sizeof(app_timer_event_t));
app_timer_event_t const * p_timer_event = (app_timer_event_t *)p_event_data;
p_timer_event->timeout_handler(p_timer_event->p_context);
}
#endif
/**@brief Function for executing an application timeout handler, either by calling it directly, or /**@brief Function for executing an application timeout handler, either by calling it directly, or
* by passing an event to the @ref app_scheduler. * by passing an event to the @ref app_scheduler.
@ -351,15 +377,16 @@ static void timer_list_handler_sched(void)
*/ */
static void timeout_handler_exec(timer_node_t * p_timer) static void timeout_handler_exec(timer_node_t * p_timer)
{ {
if (m_evt_schedule_func != NULL) #if APP_TIMER_CONFIG_USE_SCHEDULER
{ app_timer_event_t timer_event;
uint32_t err_code = m_evt_schedule_func(p_timer->p_timeout_handler, p_timer->p_context);
APP_ERROR_CHECK(err_code); timer_event.timeout_handler = p_timer->p_timeout_handler;
} timer_event.p_context = p_timer->p_context;
else uint32_t err_code = app_sched_event_put(&timer_event, sizeof(timer_event), timeout_handler_scheduled_exec);
{ APP_ERROR_CHECK(err_code);
p_timer->p_timeout_handler(p_timer->p_context); #else
} p_timer->p_timeout_handler(p_timer->p_context);
#endif
} }
@ -467,65 +494,6 @@ static bool elapsed_ticks_acquire(uint32_t * p_ticks_elapsed)
} }
/**@brief Function for handling the timer list deletions.
*
* @return TRUE if Capture Compare register must be updated, FALSE otherwise.
*/
static bool list_deletions_handler(void)
{
timer_node_t * p_timer_old_head;
uint8_t user_id;
// Remember the old head, so as to decide if new compare needs to be set.
p_timer_old_head = mp_timer_id_head;
user_id = m_user_array_size;
while (user_id--)
{
timer_user_t * p_user = &mp_users[user_id];
uint8_t user_ops_first = p_user->first;
while (user_ops_first != p_user->last)
{
timer_user_op_t * p_user_op = &p_user->p_user_op_queue[user_ops_first];
// Traverse to next operation in queue.
user_ops_first++;
if (user_ops_first == p_user->user_op_queue_size)
{
user_ops_first = 0;
}
switch (p_user_op->op_type)
{
case TIMER_USER_OP_TYPE_STOP:
// Delete node if timer is running.
timer_list_remove(p_user_op->p_node);
break;
case TIMER_USER_OP_TYPE_STOP_ALL:
// Delete list of running timers, and mark all timers as not running.
while (mp_timer_id_head != NULL)
{
timer_node_t * p_head = mp_timer_id_head;
p_head->is_running = false;
mp_timer_id_head = p_head->next;
}
break;
default:
// No implementation needed.
break;
}
}
}
// Detect change in head of the list.
return (mp_timer_id_head != p_timer_old_head);
}
/**@brief Function for updating the timer list for expired timers. /**@brief Function for updating the timer list for expired timers.
* *
* @param[in] ticks_elapsed Number of elapsed ticks. * @param[in] ticks_elapsed Number of elapsed ticks.
@ -584,91 +552,115 @@ static void expired_timers_handler(uint32_t ticks_elapsed,
*/ */
static bool list_insertions_handler(timer_node_t * p_restart_list_head) static bool list_insertions_handler(timer_node_t * p_restart_list_head)
{ {
bool compare_update = false;
timer_node_t * p_timer_id_old_head; timer_node_t * p_timer_id_old_head;
uint8_t user_id;
// Remember the old head, so as to decide if new compare needs to be set. // Remember the old head, so as to decide if new compare needs to be set.
p_timer_id_old_head = mp_timer_id_head; p_timer_id_old_head = mp_timer_id_head;
user_id = m_user_array_size; // Handle insertions of timers.
while (user_id--) while ((p_restart_list_head != NULL) || (m_op_queue.first != m_op_queue.last))
{ {
timer_user_t * p_user = &mp_users[user_id]; timer_node_t * p_timer;
// Handle insertions of timers. if (p_restart_list_head != NULL)
while ((p_restart_list_head != NULL) || (p_user->first != p_user->last))
{ {
timer_node_t * p_timer; p_timer = p_restart_list_head;
p_restart_list_head = p_timer->next;
if (p_restart_list_head != NULL)
{
p_timer = p_restart_list_head;
p_restart_list_head = p_timer->next;
}
else
{
timer_user_op_t * p_user_op = &p_user->p_user_op_queue[p_user->first];
p_user->first++;
if (p_user->first == p_user->user_op_queue_size)
{
p_user->first = 0;
}
p_timer = p_user_op->p_node;
if ((p_user_op->op_type != TIMER_USER_OP_TYPE_START) || p_timer->is_running)
{
continue;
}
p_timer->ticks_at_start = p_user_op->params.start.ticks_at_start;
p_timer->ticks_first_interval = p_user_op->params.start.ticks_first_interval;
p_timer->ticks_periodic_interval = p_user_op->params.start.ticks_periodic_interval;
p_timer->p_context = p_user_op->params.start.p_context;
if (m_rtc1_reset)
{
p_timer->ticks_at_start = 0;
}
}
// Prepare the node to be inserted.
if (
((p_timer->ticks_at_start - m_ticks_latest) & MAX_RTC_COUNTER_VAL)
<
(MAX_RTC_COUNTER_VAL / 2)
)
{
p_timer->ticks_to_expire = ticks_diff_get(p_timer->ticks_at_start, m_ticks_latest) +
p_timer->ticks_first_interval;
}
else
{
uint32_t delta_current_start;
delta_current_start = ticks_diff_get(m_ticks_latest, p_timer->ticks_at_start);
if (p_timer->ticks_first_interval > delta_current_start)
{
p_timer->ticks_to_expire = p_timer->ticks_first_interval - delta_current_start;
}
else
{
p_timer->ticks_to_expire = 0;
}
}
p_timer->ticks_at_start = 0;
p_timer->ticks_first_interval = 0;
p_timer->is_running = true;
p_timer->next = NULL;
// Insert into list
timer_list_insert(p_timer);
} }
else
{
timer_user_op_t * p_user_op = &m_op_queue.user_op_queue[m_op_queue.first];
m_op_queue.first++;
if (m_op_queue.first == m_op_queue.size)
{
m_op_queue.first = 0;
}
p_timer = p_user_op->p_node;
switch (p_user_op->op_type)
{
case TIMER_USER_OP_TYPE_STOP:
// Delete node if timer is running.
if (timer_list_remove(p_user_op->p_node))
{
compare_update = true;
}
p_timer->is_running = false;
continue;
case TIMER_USER_OP_TYPE_STOP_ALL:
// Delete list of running timers, and mark all timers as not running.
while (mp_timer_id_head != NULL)
{
timer_node_t * p_head = mp_timer_id_head;
p_head->is_running = false;
mp_timer_id_head = p_head->next;
}
continue;
case TIMER_USER_OP_TYPE_START:
break;
default:
// No implementation needed.
continue;
}
if (p_timer->is_running)
{
continue;
}
p_timer->ticks_at_start = p_user_op->params.start.ticks_at_start;
p_timer->ticks_first_interval = p_user_op->params.start.ticks_first_interval;
p_timer->ticks_periodic_interval = p_user_op->params.start.ticks_periodic_interval;
p_timer->p_context = p_user_op->params.start.p_context;
if (m_rtc1_reset)
{
p_timer->ticks_at_start = 0;
}
}
// Prepare the node to be inserted.
if (
((p_timer->ticks_at_start - m_ticks_latest) & MAX_RTC_COUNTER_VAL)
<
(MAX_RTC_COUNTER_VAL / 2)
)
{
p_timer->ticks_to_expire = ticks_diff_get(p_timer->ticks_at_start, m_ticks_latest) +
p_timer->ticks_first_interval;
}
else
{
uint32_t delta_current_start;
delta_current_start = ticks_diff_get(m_ticks_latest, p_timer->ticks_at_start);
if (p_timer->ticks_first_interval > delta_current_start)
{
p_timer->ticks_to_expire = p_timer->ticks_first_interval - delta_current_start;
}
else
{
p_timer->ticks_to_expire = 0;
}
}
p_timer->ticks_at_start = 0;
p_timer->ticks_first_interval = 0;
p_timer->is_running = true;
p_timer->next = NULL;
// Insert into list
timer_list_insert(p_timer);
} }
return (mp_timer_id_head != p_timer_id_old_head); return (compare_update || (mp_timer_id_head != p_timer_id_old_head));
} }
@ -705,7 +697,7 @@ static void compare_reg_update(timer_node_t * p_timer_id_head_old)
{ {
// When this happens the COMPARE event may not be triggered by the RTC. // When this happens the COMPARE event may not be triggered by the RTC.
// The nRF51 Series User Specification states that if the COUNTER value is N // The nRF51 Series User Specification states that if the COUNTER value is N
// (i.e post_counter_val = N), writing N or N+1 to a CC register may not trigger a // (i.e post_counter_val = N), writing N or N + 1 to a CC register may not trigger a
// COMPARE event. Hence the RTC interrupt is forcefully pended by calling the following // COMPARE event. Hence the RTC interrupt is forcefully pended by calling the following
// function. // function.
rtc1_compare0_set(rtc1_counter_get()); // this should prevent CC to fire again in the background while the code is in RTC-ISR rtc1_compare0_set(rtc1_counter_get()); // this should prevent CC to fire again in the background while the code is in RTC-ISR
@ -715,8 +707,10 @@ static void compare_reg_update(timer_node_t * p_timer_id_head_old)
} }
else else
{ {
#if (APP_TIMER_KEEPS_RTC_ACTIVE == 0)
// No timers are running, stop RTC // No timers are running, stop RTC
rtc1_stop(); rtc1_stop();
#endif //(APP_TIMER_KEEPS_RTC_ACTIVE == 0)
} }
} }
@ -730,25 +724,19 @@ static void timer_list_handler(void)
uint32_t ticks_elapsed; uint32_t ticks_elapsed;
uint32_t ticks_previous; uint32_t ticks_previous;
bool ticks_have_elapsed; bool ticks_have_elapsed;
bool compare_update; bool compare_update = false;
timer_node_t * p_timer_id_head_old; timer_node_t * p_timer_id_head_old;
#ifdef APP_TIMER_WITH_PROFILER #if APP_TIMER_WITH_PROFILER
{ {
unsigned int i; uint8_t size = m_op_queue.size;
uint8_t first = m_op_queue.first;
uint8_t last = m_op_queue.last;
uint8_t utilization = (first <= last) ? (last - first) : (size + 1 - first + last);
for (i = 0; i < APP_TIMER_INT_LEVELS; i++) if (utilization > m_max_user_op_queue_utilization)
{ {
timer_user_t *p_user = &mp_users[i]; m_max_user_op_queue_utilization = utilization;
uint8_t size = p_user->user_op_queue_size;
uint8_t first = p_user->first;
uint8_t last = p_user->last;
uint8_t utilization = (first <= last) ? (last - first) : (size + 1 - first + last);
if (utilization > m_max_user_op_queue_utilization)
{
m_max_user_op_queue_utilization = utilization;
}
} }
} }
#endif #endif
@ -760,9 +748,6 @@ static void timer_list_handler(void)
// Get number of elapsed ticks // Get number of elapsed ticks
ticks_have_elapsed = elapsed_ticks_acquire(&ticks_elapsed); ticks_have_elapsed = elapsed_ticks_acquire(&ticks_elapsed);
// Handle list deletions
compare_update = list_deletions_handler();
// Handle expired timers // Handle expired timers
if (ticks_have_elapsed) if (ticks_have_elapsed)
{ {
@ -770,6 +755,7 @@ static void timer_list_handler(void)
compare_update = true; compare_update = true;
} }
// Handle list insertions // Handle list insertions
if (list_insertions_handler(p_restart_list_head)) if (list_insertions_handler(p_restart_list_head))
{ {
@ -787,41 +773,39 @@ static void timer_list_handler(void)
/**@brief Function for enqueueing a new operations queue entry. /**@brief Function for enqueueing a new operations queue entry.
* *
* @param[in] p_user User that the entry is to be enqueued for.
* @param[in] last_index Index of the next last index to be enqueued. * @param[in] last_index Index of the next last index to be enqueued.
*/ */
static void user_op_enque(timer_user_t * p_user, uint8_t last_index) static void user_op_enque(uint8_t last_index)
{ {
p_user->last = last_index; m_op_queue.last = last_index;
} }
/**@brief Function for allocating a new operations queue entry. /**@brief Function for allocating a new operations queue entry.
* *
* @param[in] p_user User that the entry is to be allocated for.
* @param[out] p_last_index Index of the next last index to be enqueued. * @param[out] p_last_index Index of the next last index to be enqueued.
* *
* @return Pointer to allocated queue entry, or NULL if queue is full. * @return Pointer to allocated queue entry, or NULL if queue is full.
*/ */
static timer_user_op_t * user_op_alloc(timer_user_t * p_user, uint8_t * p_last_index) static timer_user_op_t * user_op_alloc( uint8_t * p_last_index)
{ {
uint8_t last; uint8_t last;
timer_user_op_t * p_user_op; timer_user_op_t * p_user_op;
last = p_user->last + 1; last = m_op_queue.last + 1;
if (last == p_user->user_op_queue_size) if (last == m_op_queue.size)
{ {
// Overflow case. // Overflow case.
last = 0; last = 0;
} }
if (last == p_user->first) if (last == m_op_queue.first)
{ {
// Queue is full. // Queue is full.
return NULL; return NULL;
} }
*p_last_index = last; *p_last_index = last;
p_user_op = &p_user->p_user_op_queue[p_user->last]; p_user_op = &m_op_queue.user_op_queue[m_op_queue.last];
return p_user_op; return p_user_op;
} }
@ -829,7 +813,6 @@ static timer_user_op_t * user_op_alloc(timer_user_t * p_user, uint8_t * p_last_i
/**@brief Function for scheduling a Timer Start operation. /**@brief Function for scheduling a Timer Start operation.
* *
* @param[in] user_id Id of user calling this function.
* @param[in] timer_id Id of timer to start. * @param[in] timer_id Id of timer to start.
* @param[in] timeout_initial Time (in ticks) to first timer expiry. * @param[in] timeout_initial Time (in ticks) to first timer expiry.
* @param[in] timeout_periodic Time (in ticks) between periodic expiries. * @param[in] timeout_periodic Time (in ticks) between periodic expiries.
@ -837,89 +820,80 @@ static timer_user_op_t * user_op_alloc(timer_user_t * p_user, uint8_t * p_last_i
* the timer expires. * the timer expires.
* @return NRF_SUCCESS on success, otherwise an error code. * @return NRF_SUCCESS on success, otherwise an error code.
*/ */
static uint32_t timer_start_op_schedule(timer_user_id_t user_id,
timer_node_t * p_node, static uint32_t timer_start_op_schedule(timer_node_t * p_node,
uint32_t timeout_initial, uint32_t timeout_initial,
uint32_t timeout_periodic, uint32_t timeout_periodic,
void * p_context) void * p_context)
{ {
uint8_t last_index; uint8_t last_index;
uint32_t err_code = NRF_SUCCESS;
timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); CRITICAL_REGION_ENTER();
timer_user_op_t * p_user_op = user_op_alloc(&last_index);
if (p_user_op == NULL) if (p_user_op == NULL)
{ {
return NRF_ERROR_NO_MEM; err_code = NRF_ERROR_NO_MEM;
}
else
{
p_user_op->op_type = TIMER_USER_OP_TYPE_START;
p_user_op->p_node = p_node;
p_user_op->params.start.ticks_at_start = rtc1_counter_get();
p_user_op->params.start.ticks_first_interval = timeout_initial;
p_user_op->params.start.ticks_periodic_interval = timeout_periodic;
p_user_op->params.start.p_context = p_context;
user_op_enque(last_index);
}
CRITICAL_REGION_EXIT();
if (err_code == NRF_SUCCESS)
{
timer_list_handler_sched();
} }
p_user_op->op_type = TIMER_USER_OP_TYPE_START; return err_code;
p_user_op->p_node = p_node;
p_user_op->params.start.ticks_at_start = rtc1_counter_get();
p_user_op->params.start.ticks_first_interval = timeout_initial;
p_user_op->params.start.ticks_periodic_interval = timeout_periodic;
p_user_op->params.start.p_context = p_context;
user_op_enque(&mp_users[user_id], last_index);
timer_list_handler_sched();
return NRF_SUCCESS;
} }
/**@brief Function for scheduling a Timer Stop operation. /**@brief Function for scheduling a Timer Stop operation.
* *
* @param[in] user_id Id of user calling this function.
* @param[in] timer_id Id of timer to stop. * @param[in] timer_id Id of timer to stop.
* @param[in] op_type Type of stop operation
* *
* @return NRF_SUCCESS on successful scheduling a timer stop operation. NRF_ERROR_NO_MEM when there * @return NRF_SUCCESS on successful scheduling a timer stop operation. NRF_ERROR_NO_MEM when there
* is no memory left to schedule the timer stop operation. * is no memory left to schedule the timer stop operation.
*/ */
static uint32_t timer_stop_op_schedule(timer_user_id_t user_id, timer_node_t * p_node) static uint32_t timer_stop_op_schedule(timer_node_t * p_node,
timer_user_op_type_t op_type)
{ {
uint8_t last_index; uint8_t last_index;
uint32_t err_code = NRF_SUCCESS;
timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); CRITICAL_REGION_ENTER();
timer_user_op_t * p_user_op = user_op_alloc(&last_index);
if (p_user_op == NULL) if (p_user_op == NULL)
{ {
return NRF_ERROR_NO_MEM; err_code = NRF_ERROR_NO_MEM;
} }
else
p_user_op->op_type = TIMER_USER_OP_TYPE_STOP;
p_user_op->p_node = p_node;
user_op_enque(&mp_users[user_id], last_index);
timer_list_handler_sched();
return NRF_SUCCESS;
}
/**@brief Function for scheduling a Timer Stop All operation.
*
* @param[in] user_id Id of user calling this function.
*/
static uint32_t timer_stop_all_op_schedule(timer_user_id_t user_id)
{
uint8_t last_index;
timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index);
if (p_user_op == NULL)
{ {
return NRF_ERROR_NO_MEM; p_user_op->op_type = op_type;
p_user_op->p_node = p_node;
user_op_enque(last_index);
}
CRITICAL_REGION_EXIT();
if (err_code == NRF_SUCCESS)
{
timer_list_handler_sched();
} }
p_user_op->op_type = TIMER_USER_OP_TYPE_STOP_ALL; return err_code;
p_user_op->p_node = NULL;
user_op_enque(&mp_users[user_id], last_index);
timer_list_handler_sched();
return NRF_SUCCESS;
} }
/**@brief Function for handling the RTC1 interrupt. /**@brief Function for handling the RTC1 interrupt.
* *
* @details Checks for timeouts, and executes timeout handlers for expired timers. * @details Checks for timeouts, and executes timeout handlers for expired timers.
@ -949,56 +923,21 @@ void SWI_IRQHandler(void)
} }
uint32_t app_timer_init(uint32_t prescaler, ret_code_t app_timer_init(void)
uint8_t op_queues_size,
void * p_buffer,
app_timer_evt_schedule_func_t evt_schedule_func)
{ {
int i;
// Check that buffer is correctly aligned
if (!is_word_aligned(p_buffer))
{
return NRF_ERROR_INVALID_PARAM;
}
// Check for NULL buffer
if (p_buffer == NULL)
{
mp_users = NULL;
return NRF_ERROR_INVALID_PARAM;
}
// Stop RTC to prevent any running timers from expiring (in case of reinitialization) // Stop RTC to prevent any running timers from expiring (in case of reinitialization)
rtc1_stop(); rtc1_stop();
m_evt_schedule_func = evt_schedule_func; // Initialize operation queue
m_op_queue.first = 0;
m_op_queue.last = 0;
m_op_queue.size = APP_TIMER_CONFIG_OP_QUEUE_SIZE+1;
// Initialize users array mp_timer_id_head = NULL;
m_user_array_size = APP_TIMER_INT_LEVELS;
mp_users = p_buffer;
// Skip user array
p_buffer = &((uint8_t *)p_buffer)[APP_TIMER_INT_LEVELS * sizeof(timer_user_t)];
// Initialize operation queues
for (i = 0; i < APP_TIMER_INT_LEVELS; i++)
{
timer_user_t * p_user = &mp_users[i];
p_user->first = 0;
p_user->last = 0;
p_user->user_op_queue_size = op_queues_size;
p_user->p_user_op_queue = p_buffer;
// Skip operation queue
p_buffer = &((uint8_t *)p_buffer)[op_queues_size * sizeof(timer_user_op_t)];
}
mp_timer_id_head = NULL;
m_ticks_elapsed_q_read_ind = 0; m_ticks_elapsed_q_read_ind = 0;
m_ticks_elapsed_q_write_ind = 0; m_ticks_elapsed_q_write_ind = 0;
#ifdef APP_TIMER_WITH_PROFILER #if APP_TIMER_WITH_PROFILER
m_max_user_op_queue_utilization = 0; m_max_user_op_queue_utilization = 0;
#endif #endif
@ -1006,7 +945,7 @@ uint32_t app_timer_init(uint32_t prescaler,
NVIC_SetPriority(SWI_IRQn, SWI_IRQ_PRI); NVIC_SetPriority(SWI_IRQn, SWI_IRQ_PRI);
NVIC_EnableIRQ(SWI_IRQn); NVIC_EnableIRQ(SWI_IRQn);
rtc1_init(prescaler); rtc1_init(APP_TIMER_CONFIG_RTC_FREQUENCY);
m_ticks_latest = rtc1_counter_get(); m_ticks_latest = rtc1_counter_get();
@ -1014,9 +953,9 @@ uint32_t app_timer_init(uint32_t prescaler,
} }
uint32_t app_timer_create(app_timer_id_t const * p_timer_id, ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
app_timer_mode_t mode, app_timer_mode_t mode,
app_timer_timeout_handler_t timeout_handler) app_timer_timeout_handler_t timeout_handler)
{ {
// Check state and parameters // Check state and parameters
VERIFY_MODULE_INITIALIZED(); VERIFY_MODULE_INITIALIZED();
@ -1041,37 +980,7 @@ uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
return NRF_SUCCESS; return NRF_SUCCESS;
} }
ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
/**@brief Function for creating a timer user id from the current interrupt level.
*
* @return Timer user id.
*/
static timer_user_id_t user_id_get(void)
{
timer_user_id_t ret;
STATIC_ASSERT(APP_TIMER_INT_LEVELS == 3);
switch (current_int_priority_get())
{
case APP_IRQ_PRIORITY_HIGH:
ret = APP_HIGH_USER_ID;
break;
case APP_IRQ_PRIORITY_LOW:
ret = APP_LOW_USER_ID;
break;
default:
ret = THREAD_MODE_USER_ID;
break;
}
return ret;
}
uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
{ {
uint32_t timeout_periodic; uint32_t timeout_periodic;
timer_node_t * p_node = (timer_node_t*)timer_id; timer_node_t * p_node = (timer_node_t*)timer_id;
@ -1095,15 +1004,14 @@ uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void *
// Schedule timer start operation // Schedule timer start operation
timeout_periodic = (p_node->mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0; timeout_periodic = (p_node->mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0;
return timer_start_op_schedule(user_id_get(), return timer_start_op_schedule(p_node,
p_node,
timeout_ticks, timeout_ticks,
timeout_periodic, timeout_periodic,
p_context); p_context);
} }
uint32_t app_timer_stop(app_timer_id_t timer_id) ret_code_t app_timer_stop(app_timer_id_t timer_id)
{ {
timer_node_t * p_node = (timer_node_t*)timer_id; timer_node_t * p_node = (timer_node_t*)timer_id;
// Check state and parameters // Check state and parameters
@ -1115,38 +1023,48 @@ uint32_t app_timer_stop(app_timer_id_t timer_id)
} }
p_node->is_running = false; p_node->is_running = false;
// Schedule timer stop operation // Schedule timer stop operation
return timer_stop_op_schedule(user_id_get(), p_node); return timer_stop_op_schedule(p_node, TIMER_USER_OP_TYPE_STOP);
} }
uint32_t app_timer_stop_all(void) ret_code_t app_timer_stop_all(void)
{ {
// Check state // Check state
VERIFY_MODULE_INITIALIZED(); VERIFY_MODULE_INITIALIZED();
return timer_stop_all_op_schedule(user_id_get()); return timer_stop_op_schedule(NULL, TIMER_USER_OP_TYPE_STOP_ALL);
} }
uint32_t app_timer_cnt_get(uint32_t * p_ticks) uint32_t app_timer_cnt_get(void)
{ {
*p_ticks = rtc1_counter_get(); return rtc1_counter_get();
return NRF_SUCCESS;
} }
uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to, uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
uint32_t ticks_from, uint32_t ticks_from)
uint32_t * p_ticks_diff)
{ {
*p_ticks_diff = ticks_diff_get(ticks_to, ticks_from); return ticks_diff_get(ticks_to, ticks_from);
return NRF_SUCCESS;
} }
#ifdef APP_TIMER_WITH_PROFILER #if APP_TIMER_WITH_PROFILER
uint8_t app_timer_op_queue_utilization_get(void) uint8_t app_timer_op_queue_utilization_get(void)
{ {
return m_max_user_op_queue_utilization; return m_max_user_op_queue_utilization;
} }
#endif #endif
void app_timer_pause(void)
{
NRF_RTC1->TASKS_STOP = 1;
}
void app_timer_resume(void)
{
NRF_RTC1->TASKS_START = 1;
}
#endif //NRF_MODULE_ENABLED(APP_TIMER)

View File

@ -1,15 +1,42 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. /**
* Copyright (c) 2012 - 2017, Nordic Semiconductor ASA
* *
* The information contained herein is property of Nordic Semiconductor ASA. * All rights reserved.
* 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 * Redistribution and use in source and binary forms, with or without modification,
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from * are permitted provided that the following conditions are met:
* the file. *
* 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.
* *
*/ */
/** @file /** @file
* *
* @defgroup app_timer Application Timer * @defgroup app_timer Application Timer
@ -19,7 +46,7 @@
* @brief Application timer functionality. * @brief Application timer functionality.
* *
* @details This module enables the application to create multiple timer instances based on the RTC1 * @details This module enables the application to create multiple timer instances based on the RTC1
* peripheral. Checking for time-outs and invokation of user time-out handlers is performed * peripheral. Checking for time-outs and invocation of user time-out handlers is performed
* in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0). * in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0).
* Both interrupt handlers are running in APP_LOW priority level. * Both interrupt handlers are running in APP_LOW priority level.
* *
@ -39,60 +66,48 @@
#ifndef APP_TIMER_H__ #ifndef APP_TIMER_H__
#define APP_TIMER_H__ #define APP_TIMER_H__
#include "sdk_config.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "app_error.h" #include "app_error.h"
#include "app_util.h" #include "app_util.h"
#include "compiler_abstraction.h" #include "compiler_abstraction.h"
#include "nordic_common.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#define APP_TIMER_CLOCK_FREQ 32768 /**< Clock frequency of the RTC timer used to implement the app timer module. */ #ifdef __cplusplus
#define APP_TIMER_MIN_TIMEOUT_TICKS 5 /**< Minimum value of the timeout_ticks parameter of app_timer_start(). */ extern "C" {
#endif
#define APP_TIMER_NODE_SIZE 32 /**< Size of app_timer.timer_node_t (used to allocate data). */ #define APP_TIMER_CLOCK_FREQ 32768 /**< Clock frequency of the RTC timer used to implement the app timer module. */
#define APP_TIMER_USER_OP_SIZE 24 /**< Size of app_timer.timer_user_op_t (only for use inside APP_TIMER_BUF_SIZE()). */ #define APP_TIMER_MIN_TIMEOUT_TICKS 5 /**< Minimum value of the timeout_ticks parameter of app_timer_start(). */
#define APP_TIMER_USER_SIZE 8 /**< Size of app_timer.timer_user_t (only for use inside APP_TIMER_BUF_SIZE()). */
#define APP_TIMER_INT_LEVELS 3 /**< Number of interrupt levels from where timer operations may be initiated (only for use inside APP_TIMER_BUF_SIZE()). */
/**@brief Compute number of bytes required to hold the application timer data structures. #ifdef RTX
* #define APP_TIMER_NODE_SIZE 40 /**< Size of app_timer.timer_node_t (used to allocate data). */
* @param[in] OP_QUEUE_SIZE Size of queues holding timer operations that are pending execution. #else
* Note that due to the queue implementation, this size must be one more #define APP_TIMER_NODE_SIZE 32 /**< Size of app_timer.timer_node_t (used to allocate data). */
* than the size that is actually needed. #endif // RTX
*
* @return Required application timer buffer size (in bytes). #define APP_TIMER_SCHED_EVENT_DATA_SIZE sizeof(app_timer_event_t) /**< Size of event data when scheduler is used. */
*/
#define APP_TIMER_BUF_SIZE(OP_QUEUE_SIZE) \
( \
( \
APP_TIMER_INT_LEVELS \
* \
(APP_TIMER_USER_SIZE + ((OP_QUEUE_SIZE) + 1) * APP_TIMER_USER_OP_SIZE) \
) \
)
/**@brief Convert milliseconds to timer ticks. /**@brief Convert milliseconds to timer ticks.
* *
* This macro uses 64-bit integer arithmetic, but as long as the macro parameters are * This macro uses 64-bit integer arithmetic, but as long as the macro parameters are
* constants (i.e. defines), the computation will be done by the preprocessor. * constants (i.e. defines), the computation will be done by the preprocessor.
* *
* When using this macro, ensure that the
* values provided as input result in an output value that is supported by the
* @ref app_timer_start function. For example, when the ticks for 1 ms is needed, the
* maximum possible value of PRESCALER must be 6, when @ref APP_TIMER_CLOCK_FREQ is 32768.
* This will result in a ticks value as 5. Any higher value for PRESCALER will result in a
* ticks value that is not supported by this module.
*
* @param[in] MS Milliseconds. * @param[in] MS Milliseconds.
* @param[in] PRESCALER Value of the RTC1 PRESCALER register (must be the same value that was
* passed to APP_TIMER_INIT()).
* *
* @return Number of timer ticks. * @return Number of timer ticks.
*/ */
#define APP_TIMER_TICKS(MS, PRESCALER)\ #ifndef FREERTOS
((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, ((PRESCALER) + 1) * 1000)) #define APP_TIMER_TICKS(MS) \
((uint32_t)ROUNDED_DIV( \
(MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
#else
#include "FreeRTOSConfig.h"
#define APP_TIMER_TICKS(MS) (uint32_t)ROUNDED_DIV((MS)*configTICK_RATE_HZ,1000)
#endif
typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t; typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t;
/**@brief Timer ID type. /**@brief Timer ID type.
@ -104,17 +119,20 @@ typedef app_timer_t * app_timer_id_t;
* *
* @param timer_id Name of the timer identifier variable that will be used to control the timer. * @param timer_id Name of the timer identifier variable that will be used to control the timer.
*/ */
#define APP_TIMER_DEF(timer_id) \ #define APP_TIMER_DEF(timer_id) \
static app_timer_t timer_id##_data = { {0} }; \ static app_timer_t CONCAT_2(timer_id,_data) = { {0} }; \
static const app_timer_id_t timer_id = &timer_id##_data static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
/**@brief Application time-out handler type. */ /**@brief Application time-out handler type. */
typedef void (*app_timer_timeout_handler_t)(void * p_context); typedef void (*app_timer_timeout_handler_t)(void * p_context);
/**@brief Type of function for passing events from the timer module to the scheduler. */ /**@brief Structure passed to app_scheduler. */
typedef uint32_t (*app_timer_evt_schedule_func_t) (app_timer_timeout_handler_t timeout_handler, typedef struct
void * p_context); {
app_timer_timeout_handler_t timeout_handler;
void * p_context;
} app_timer_event_t;
/**@brief Timer modes. */ /**@brief Timer modes. */
typedef enum typedef enum
@ -123,69 +141,11 @@ typedef enum
APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */ APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
} app_timer_mode_t; } app_timer_mode_t;
/**@brief Initialize the application timer module.
*
* @details This macro handles dimensioning and allocation of the memory buffer required by the timer,
* making sure that the buffer is correctly aligned. It will also connect the timer module
* to the scheduler (if specified).
*
* @note This module assumes that the LFCLK is already running. If it is not, the module will
* be non-functional, since the RTC will not run. If you do not use a SoftDevice, you
* must start the LFCLK manually. See the rtc_example's lfclk_config() function
* for an example of how to do this. If you use a SoftDevice, the LFCLK is started on
* SoftDevice init.
*
*
* @param[in] PRESCALER Value of the RTC1 PRESCALER register. This will decide the
* timer tick rate. Set to 0 for no prescaling.
* @param[in] OP_QUEUES_SIZE Size of queues holding timer operations that are pending execution.
* @param[in] SCHEDULER_FUNC Pointer to scheduler event handler
*
* @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
* several times as long as it is from the same location, for example, to do a re-initialization).
*/
/*lint -emacro(506, APP_TIMER_INIT) */ /* Suppress "Constant value Boolean */
#define APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, SCHEDULER_FUNC) \
do \
{ \
static uint32_t APP_TIMER_BUF[CEIL_DIV(APP_TIMER_BUF_SIZE((OP_QUEUES_SIZE) + 1), \
sizeof(uint32_t))]; \
uint32_t ERR_CODE = app_timer_init((PRESCALER), \
(OP_QUEUES_SIZE) + 1, \
APP_TIMER_BUF, \
SCHEDULER_FUNC); \
APP_ERROR_CHECK(ERR_CODE); \
} while (0)
/**@brief Function for initializing the timer module. /**@brief Function for initializing the timer module.
*
* Normally, initialization should be done using the APP_TIMER_INIT() macro, because that macro will both
* allocate the buffers needed by the timer module (including aligning the buffers correctly)
* and take care of connecting the timer module to the scheduler (if specified).
*
* @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling.
* @param[in] op_queues_size Size of queues holding timer operations that are pending
* execution. Note that due to the queue implementation, this size must
* be one more than the size that is actually needed.
* @param[in] p_buffer Pointer to memory buffer for internal use in the app_timer
* module. The size of the buffer can be computed using the
* APP_TIMER_BUF_SIZE() macro. The buffer must be aligned to a
* 4 byte boundary.
* @param[in] evt_schedule_func Function for passing time-out events to the scheduler. Point to
* app_timer_evt_schedule() to connect to the scheduler. Set to NULL
* to make the timer module call the time-out handler directly from
* the timer interrupt handler.
* *
* @retval NRF_SUCCESS If the module was initialized successfully. * @retval NRF_SUCCESS If the module was initialized successfully.
* @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid (buffer not aligned to a 4 byte
* boundary or NULL).
*/ */
uint32_t app_timer_init(uint32_t prescaler, ret_code_t app_timer_init(void);
uint8_t op_queues_size,
void * p_buffer,
app_timer_evt_schedule_func_t evt_schedule_func);
/**@brief Function for creating a timer instance. /**@brief Function for creating a timer instance.
* *
@ -206,9 +166,9 @@ uint32_t app_timer_init(uint32_t prescaler,
* @attention The FreeRTOS and RTX app_timer implementation does not allow app_timer_create to * @attention The FreeRTOS and RTX app_timer implementation does not allow app_timer_create to
* be called on the previously initialized instance. * be called on the previously initialized instance.
*/ */
uint32_t app_timer_create(app_timer_id_t const * p_timer_id, ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
app_timer_mode_t mode, app_timer_mode_t mode,
app_timer_timeout_handler_t timeout_handler); app_timer_timeout_handler_t timeout_handler);
/**@brief Function for starting a timer. /**@brief Function for starting a timer.
* *
@ -230,7 +190,7 @@ uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
* @note When calling this method on a timer that is already running, the second start operation * @note When calling this method on a timer that is already running, the second start operation
* is ignored. * is ignored.
*/ */
uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context); ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context);
/**@brief Function for stopping the specified timer. /**@brief Function for stopping the specified timer.
* *
@ -242,7 +202,7 @@ uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void *
* has not been created. * has not been created.
* @retval NRF_ERROR_NO_MEM If the timer operations queue was full. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
*/ */
uint32_t app_timer_stop(app_timer_id_t timer_id); ret_code_t app_timer_stop(app_timer_id_t timer_id);
/**@brief Function for stopping all running timers. /**@brief Function for stopping all running timers.
* *
@ -250,36 +210,53 @@ uint32_t app_timer_stop(app_timer_id_t timer_id);
* @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized.
* @retval NRF_ERROR_NO_MEM If the timer operations queue was full. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
*/ */
uint32_t app_timer_stop_all(void); ret_code_t app_timer_stop_all(void);
/**@brief Function for returning the current value of the RTC1 counter. /**@brief Function for returning the current value of the RTC1 counter.
* *
* @param[out] p_ticks Current value of the RTC1 counter. * @return Current value of the RTC1 counter.
*
* @retval NRF_SUCCESS If the counter was successfully read.
*/ */
uint32_t app_timer_cnt_get(uint32_t * p_ticks); uint32_t app_timer_cnt_get(void);
/**@brief Function for computing the difference between two RTC1 counter values. /**@brief Function for computing the difference between two RTC1 counter values.
* *
* @param[in] ticks_to Value returned by app_timer_cnt_get(). * @param[in] ticks_to Value returned by app_timer_cnt_get().
* @param[in] ticks_from Value returned by app_timer_cnt_get(). * @param[in] ticks_from Value returned by app_timer_cnt_get().
* @param[out] p_ticks_diff Number of ticks from ticks_from to ticks_to.
* *
* @retval NRF_SUCCESS If the counter difference was successfully computed. * @return Number of ticks from ticks_from to ticks_to.
*/ */
uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to, uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
uint32_t ticks_from, uint32_t ticks_from);
uint32_t * p_ticks_diff);
#ifdef APP_TIMER_WITH_PROFILER
/**@brief Function for getting the maximum observed operation queue utilization. /**@brief Function for getting the maximum observed operation queue utilization.
* *
* Function for tuning the module and determining OP_QUEUE_SIZE value and thus module RAM usage. * Function for tuning the module and determining OP_QUEUE_SIZE value and thus module RAM usage.
* *
* @note APP_TIMER_WITH_PROFILER must be enabled to use this functionality.
*
* @return Maximum number of events in queue observed so far. * @return Maximum number of events in queue observed so far.
*/ */
uint8_t app_timer_op_queue_utilization_get(void); uint8_t app_timer_op_queue_utilization_get(void);
/**
* @brief Function for pausing RTC activity which drives app_timer.
*
* @note This function can be used for debugging purposes to ensure
* that application is halted when entering a breakpoint.
*/
void app_timer_pause(void);
/**
* @brief Function for resuming RTC activity which drives app_timer.
*
* @note This function can be used for debugging purposes to resume
* application activity.
*/
void app_timer_resume(void);
#ifdef __cplusplus
}
#endif #endif
#endif // APP_TIMER_H__ #endif // APP_TIMER_H__

View File

@ -1,34 +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 "app_timer_appsh.h"
#include "app_scheduler.h"
static void app_timer_evt_get(void * p_event_data, uint16_t event_size)
{
app_timer_event_t * p_timer_event = (app_timer_event_t *)p_event_data;
APP_ERROR_CHECK_BOOL(event_size == sizeof(app_timer_event_t));
p_timer_event->timeout_handler(p_timer_event->p_context);
}
uint32_t app_timer_evt_schedule(app_timer_timeout_handler_t timeout_handler,
void * p_context)
{
app_timer_event_t timer_event;
timer_event.timeout_handler = timeout_handler;
timer_event.p_context = p_context;
return app_sched_event_put(&timer_event, sizeof(timer_event), app_timer_evt_get);
}

View File

@ -1,44 +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.
*
*/
#ifndef APP_TIMER_APPSH_H
#define APP_TIMER_APPSH_H
#include "app_timer.h"
#define APP_TIMER_SCHED_EVT_SIZE sizeof(app_timer_event_t) /**< Size of button events being passed through the scheduler (is to be used for computing the maximum size of scheduler events). */
/**@brief Macro for initializing the application timer module to use with app_scheduler.
*
* @param[in] PRESCALER Value of the RTC1 PRESCALER register. This will decide the
* timer tick rate. Set to 0 for no prescaling.
* @param[in] OP_QUEUES_SIZE Size of queues holding timer operations that are pending execution.
* @param[in] USE_SCHEDULER TRUE if the application is using the app_scheduler,
* FALSE otherwise.
*
* @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
* several times as long as it is from the same location, e.g. to do a reinitialization).
*/
#define APP_TIMER_APPSH_INIT(PRESCALER, OP_QUEUES_SIZE, USE_SCHEDULER) \
APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, \
(USE_SCHEDULER) ? app_timer_evt_schedule : NULL)
typedef struct
{
app_timer_timeout_handler_t timeout_handler;
void * p_context;
} app_timer_event_t;
uint32_t app_timer_evt_schedule(app_timer_timeout_handler_t timeout_handler,
void * p_context);
#endif // APP_TIMER_APPSH_H

View File

@ -60,7 +60,7 @@
#define TX_PIN_NUMBER 6 #define TX_PIN_NUMBER 6
#define CTS_PIN_NUMBER 7 #define CTS_PIN_NUMBER 7
#define RTS_PIN_NUMBER 5 #define RTS_PIN_NUMBER 5
#define HWFC true #define HWFC false
// Used as model string in OTA mode // Used as model string in OTA mode
#define DIS_MODEL "Bluefruit Feather 52840" #define DIS_MODEL "Bluefruit Feather 52840"

View File

@ -32,11 +32,11 @@
#include <string.h> #include <string.h>
#include <stddef.h> #include <stddef.h>
#include "nordic_common.h"
#include "sdk_common.h"
#include "dfu_transport.h" #include "dfu_transport.h"
#include "bootloader.h" #include "bootloader.h"
#include "bootloader_util.h" #include "bootloader_util.h"
#include "nordic_common.h"
#include "sdk_common.h"
#include "nrf.h" #include "nrf.h"
#include "nrf_soc.h" #include "nrf_soc.h"
@ -46,7 +46,7 @@
#include "nrf.h" #include "nrf.h"
#include "ble_hci.h" #include "ble_hci.h"
#include "app_scheduler.h" #include "app_scheduler.h"
#include "app_timer_appsh.h" #include "app_timer.h"
#include "nrf_error.h" #include "nrf_error.h"
#include "boards.h" #include "boards.h"
@ -87,7 +87,7 @@
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */ #define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
#define SCHED_MAX_EVENT_DATA_SIZE MAX(APP_TIMER_SCHED_EVT_SIZE, 0) /**< Maximum size of scheduler events. */ #define SCHED_MAX_EVENT_DATA_SIZE sizeof(app_timer_event_t) /**< Maximum size of scheduler events. */
#define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. */ #define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. */
// Helper function // Helper function
@ -249,10 +249,17 @@ static uint32_t ble_stack_init(bool init_softdevice)
uint32_t err_code; uint32_t err_code;
nrf_clock_lf_cfg_t clock_lf_cfg = nrf_clock_lf_cfg_t clock_lf_cfg =
{ {
#if 0
.source = NRF_CLOCK_LF_SRC_RC, .source = NRF_CLOCK_LF_SRC_RC,
.rc_ctiv = 16, .rc_ctiv = 16,
.rc_temp_ctiv = 2, .rc_temp_ctiv = 2,
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM .accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
#else
.source = NRF_CLOCK_LF_SRC_XTAL,
.rc_ctiv = 0,
.rc_temp_ctiv = 0,
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
#endif
}; };
if (init_softdevice) if (init_softdevice)
@ -322,7 +329,7 @@ static void scheduler_init(void)
/* Initialize a blinky timer to show that we're in bootloader */ /* Initialize a blinky timer to show that we're in bootloader */
(void) app_timer_create(&blinky_timer_id, APP_TIMER_MODE_REPEATED, blinky_handler); (void) app_timer_create(&blinky_timer_id, APP_TIMER_MODE_REPEATED, blinky_handler);
app_timer_start(blinky_timer_id, APP_TIMER_TICKS(LED_BLINK_INTERVAL, APP_TIMER_PRESCALER), NULL); app_timer_start(blinky_timer_id, APP_TIMER_TICKS(LED_BLINK_INTERVAL), NULL);
} }
@ -368,7 +375,9 @@ int main(void)
led_pin_init(LED_BLUE); // on metro52 will override FRESET led_pin_init(LED_BLUE); // on metro52 will override FRESET
// Initialize timer module, already configred to use with the scheduler. // Initialize timer module, already configred to use with the scheduler.
APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, true); // APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, true);
app_timer_init();
(void) bootloader_init(); (void) bootloader_init();
@ -474,18 +483,16 @@ void freset_erase_and_wait(pstorage_handle_t* hdl, uint32_t addr, uint32_t size)
// Time to erase a page is 100 ms max // Time to erase a page is 100 ms max
// It is better to force a timeout to prevent lock-up // It is better to force a timeout to prevent lock-up
uint32_t timeout_tck = (size/CODE_PAGE_SIZE)*100; uint32_t timeout_tck = (size/CODE_PAGE_SIZE)*100;
timeout_tck = APP_TIMER_TICKS(timeout_tck, APP_TIMER_PRESCALER); timeout_tck = APP_TIMER_TICKS(timeout_tck);
uint32_t start_tck; uint32_t start_tck = app_timer_cnt_get();
app_timer_cnt_get(&start_tck);
while(!_freset_erased_complete) while(!_freset_erased_complete)
{ {
sd_app_evt_wait(); sd_app_evt_wait();
app_sched_execute(); app_sched_execute();
uint32_t now_tck; uint32_t now_tck = app_timer_cnt_get();
app_timer_cnt_get(&now_tck);
if ( (now_tck - start_tck) > timeout_tck ) break; if ( (now_tck - start_tck) > timeout_tck ) break;
} }
} }

View File

@ -92,7 +92,6 @@ C_SOURCE_FILES += $(SDK_PATH)/libraries/bootloader_dfu/dfu_transport_serial.c
C_SOURCE_FILES += $(SDK_PATH)/libraries/bootloader_dfu/dfu_transport_ble.c C_SOURCE_FILES += $(SDK_PATH)/libraries/bootloader_dfu/dfu_transport_ble.c
C_SOURCE_FILES += $(SDK_PATH)/libraries/timer/app_timer.c C_SOURCE_FILES += $(SDK_PATH)/libraries/timer/app_timer.c
C_SOURCE_FILES += $(SDK_PATH)/libraries/timer/app_timer_appsh.c
C_SOURCE_FILES += $(SDK_PATH)/libraries/scheduler/app_scheduler.c C_SOURCE_FILES += $(SDK_PATH)/libraries/scheduler/app_scheduler.c
C_SOURCE_FILES += $(SDK_PATH)/libraries/util/app_error.c C_SOURCE_FILES += $(SDK_PATH)/libraries/util/app_error.c
C_SOURCE_FILES += $(SDK_PATH)/libraries/util/app_error_weak.c C_SOURCE_FILES += $(SDK_PATH)/libraries/util/app_error_weak.c

View File

@ -47,6 +47,72 @@
#include "app_config.h" #include "app_config.h"
#endif #endif
// <e> HCI_SLIP_ENABLED - hci_slip - SLIP protocol implementation used by HCI
//==========================================================
#ifndef HCI_SLIP_ENABLED
#define HCI_SLIP_ENABLED 1
#endif
// <o> HCI_UART_BAUDRATE - Default Baudrate
// <323584=> 1200 baud
// <643072=> 2400 baud
// <1290240=> 4800 baud
// <2576384=> 9600 baud
// <3862528=> 14400 baud
// <5152768=> 19200 baud
// <7716864=> 28800 baud
// <10289152=> 38400 baud
// <15400960=> 57600 baud
// <20615168=> 76800 baud
// <30801920=> 115200 baud
// <61865984=> 230400 baud
// <67108864=> 250000 baud
// <121634816=> 460800 baud
// <251658240=> 921600 baud
// <268435456=> 1000000 baud
#ifndef HCI_UART_BAUDRATE
#define HCI_UART_BAUDRATE 30801920
#endif
// <o> HCI_UART_FLOW_CONTROL - Hardware Flow Control
// <0=> Disabled
// <1=> Enabled
#ifndef HCI_UART_FLOW_CONTROL
#define HCI_UART_FLOW_CONTROL 0
#endif
// <o> HCI_UART_RX_PIN - UART RX pin
#ifndef HCI_UART_RX_PIN
#define HCI_UART_RX_PIN 8
#endif
// <o> HCI_UART_TX_PIN - UART TX pin
#ifndef HCI_UART_TX_PIN
#define HCI_UART_TX_PIN 6
#endif
// <o> HCI_UART_RTS_PIN - UART RTS pin
#ifndef HCI_UART_RTS_PIN
#define HCI_UART_RTS_PIN 5
#endif
// <o> HCI_UART_CTS_PIN - UART CTS pin
#ifndef HCI_UART_CTS_PIN
#define HCI_UART_CTS_PIN 7
#endif
#ifndef HCI_TRANSPORT_ENABLED
#define HCI_TRANSPORT_ENABLED 1
#endif
// <o> HCI_MAX_PACKET_SIZE_IN_BITS - Maximum size of a single application packet in bits.
#ifndef HCI_MAX_PACKET_SIZE_IN_BITS
#define HCI_MAX_PACKET_SIZE_IN_BITS 8000
#endif
//#include "nrf_drv_config.h" //#include "nrf_drv_config.h"
// <h> nRF_BLE_DFU // <h> nRF_BLE_DFU
@ -1585,7 +1651,7 @@
#ifndef UART0_CONFIG_USE_EASY_DMA #ifndef UART0_CONFIG_USE_EASY_DMA
#define UART0_CONFIG_USE_EASY_DMA 1 #define UART0_CONFIG_USE_EASY_DMA 0
#endif #endif
// </e> // </e>