From 440fbfd69476fab0aebbc6d0f38d25dd13e8a2cb Mon Sep 17 00:00:00 2001 From: Jarkko Paso Date: Thu, 11 Oct 2018 12:55:15 +0300 Subject: [PATCH 1/5] NS hal: Implemented FHSS driver --- .../arm_hal_fhss_timer.cpp | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp new file mode 100644 index 00000000000..fc82962db34 --- /dev/null +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ns_types.h" +#include "fhss_api.h" +#include "fhss_config.h" +#include "mbed.h" +#include "mbed_trace.h" +#include "platform/arm_hal_interrupt.h" +#include + +#define TRACE_GROUP "fhdr" +#define NUMBER_OF_SIMULTANEOUS_TIMEOUTS 2 + +static Timer timer; +static bool timer_initialized = false; +static const fhss_api_t *fhss_active_handle = NULL; +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT +static EventQueue *equeue; +#endif + +struct fhss_timeout_s +{ + void (*fhss_timer_callback)(const fhss_api_t *fhss_api, uint16_t); + uint32_t start_time; + uint32_t stop_time; + bool active; + Timeout timeout; +}; + +fhss_timeout_s fhss_timeout[NUMBER_OF_SIMULTANEOUS_TIMEOUTS]; + +static uint32_t read_current_time(void) +{ + return timer.read_us(); +} + +static fhss_timeout_s *find_timeout(void (*callback)(const fhss_api_t *api, uint16_t)) +{ + for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) { + if (fhss_timeout[i].fhss_timer_callback == callback) { + return &fhss_timeout[i]; + } + } + return NULL; +} + +static fhss_timeout_s *allocate_timeout(void) +{ + for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) { + if (fhss_timeout[i].fhss_timer_callback == NULL) { + memset(&fhss_timeout[i], sizeof(fhss_timeout_s), 0); + return &fhss_timeout[i]; + } + } + return NULL; +} + +static void fhss_timeout_handler(void) +{ + for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) { + if (fhss_timeout[i].active && ((fhss_timeout[i].stop_time - fhss_timeout[i].start_time) <= (read_current_time() - fhss_timeout[i].start_time))) { + fhss_timeout[i].active = false; + fhss_timeout[i].fhss_timer_callback(fhss_active_handle, read_current_time() - fhss_timeout[i].stop_time); + } + } +} + +static void timer_callback(void) +{ +#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT + fhss_timeout_handler(); +#else + equeue->call(fhss_timeout_handler); +#endif +} + +static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *callback_param) +{ + int ret_val = -1; + platform_enter_critical(); + if (timer_initialized == false) { +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT + equeue = mbed_highprio_event_queue(); + MBED_ASSERT(equeue != NULL); +#endif + timer.start(); + timer_initialized = true; + } + fhss_timeout_s *fhss_tim = find_timeout(callback); + if (!fhss_tim) { + fhss_tim = allocate_timeout(); + } + fhss_tim->fhss_timer_callback = callback; + fhss_tim->start_time = read_current_time(); + fhss_tim->stop_time = fhss_tim->start_time + slots; + fhss_tim->active = true; + fhss_tim->timeout.attach_us(timer_callback, slots); + fhss_active_handle = callback_param; + ret_val = 0; + platform_exit_critical(); + return ret_val; +} + +static int platform_fhss_timer_stop(void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *api) +{ + (void)api; + platform_enter_critical(); + fhss_timeout_s *fhss_tim = find_timeout(callback); + if (!fhss_tim) { + platform_exit_critical(); + return -1; + } + fhss_tim->timeout.detach(); + fhss_tim->active = false; + platform_exit_critical(); + return 0; +} + +static uint32_t platform_fhss_get_remaining_slots(void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *api) +{ + (void)api; + platform_enter_critical(); + fhss_timeout_s *fhss_tim = find_timeout(callback); + if (!fhss_tim) { + platform_exit_critical(); + return 0; + } + platform_exit_critical(); + return fhss_tim->stop_time - read_current_time(); +} + +static uint32_t platform_fhss_timestamp_read(const fhss_api_t *api) +{ + (void)api; + return read_current_time(); +} + +fhss_timer_t fhss_functions = { + .fhss_timer_start = platform_fhss_timer_start, + .fhss_timer_stop = platform_fhss_timer_stop, + .fhss_get_remaining_slots = platform_fhss_get_remaining_slots, + .fhss_get_timestamp = platform_fhss_timestamp_read, + .fhss_resolution_divider = 1 +}; From ea0f4863a22c330ac8960a670467ba09a49875a2 Mon Sep 17 00:00:00 2001 From: Jarkko Paso Date: Wed, 17 Oct 2018 15:20:44 +0300 Subject: [PATCH 2/5] FHSS timer: Calculation in critical state --- .../nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp index fc82962db34..d5b99e27683 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp @@ -138,8 +138,9 @@ static uint32_t platform_fhss_get_remaining_slots(void (*callback)(const fhss_ap platform_exit_critical(); return 0; } + uint32_t remaining_slots = fhss_tim->stop_time - read_current_time(); platform_exit_critical(); - return fhss_tim->stop_time - read_current_time(); + return remaining_slots; } static uint32_t platform_fhss_timestamp_read(const fhss_api_t *api) From 1b01709b3bbee98396d67c683fd9f17191239aca Mon Sep 17 00:00:00 2001 From: Jarkko Paso Date: Thu, 18 Oct 2018 09:32:00 +0300 Subject: [PATCH 3/5] FHSS timer driver: Make number of timeouts configurable --- .../nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp index d5b99e27683..3577854940a 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp @@ -22,7 +22,9 @@ #include #define TRACE_GROUP "fhdr" +#ifndef NUMBER_OF_SIMULTANEOUS_TIMEOUTS #define NUMBER_OF_SIMULTANEOUS_TIMEOUTS 2 +#endif //NUMBER_OF_SIMULTANEOUS_TIMEOUTS static Timer timer; static bool timer_initialized = false; From 61f4af03207fd4a531a2139c1fb844f81a72dffe Mon Sep 17 00:00:00 2001 From: Jarkko Paso Date: Thu, 18 Oct 2018 09:49:48 +0300 Subject: [PATCH 4/5] FHSS timer driver: Check if timeout alloc fails --- .../nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp index 3577854940a..8b133e4f829 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_fhss_timer.cpp @@ -105,6 +105,11 @@ static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss if (!fhss_tim) { fhss_tim = allocate_timeout(); } + if (!fhss_tim) { + platform_exit_critical(); + tr_error("Failed to allocate timeout"); + return ret_val; + } fhss_tim->fhss_timer_callback = callback; fhss_tim->start_time = read_current_time(); fhss_tim->stop_time = fhss_tim->start_time + slots; From de2fce2104fdfbcedf51ee70a546dd1c6ac20c4d Mon Sep 17 00:00:00 2001 From: Jarkko Paso Date: Tue, 23 Oct 2018 15:09:12 +0300 Subject: [PATCH 5/5] LPC408X: Cstack size reduced from 8K to 1K with IAR --- .../TARGET_LPC408X/device/TOOLCHAIN_IAR/LPC4088.icf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_NXP/TARGET_LPC408X/device/TOOLCHAIN_IAR/LPC4088.icf b/targets/TARGET_NXP/TARGET_LPC408X/device/TOOLCHAIN_IAR/LPC4088.icf index 97484feaaf1..53324c750d3 100644 --- a/targets/TARGET_NXP/TARGET_LPC408X/device/TOOLCHAIN_IAR/LPC4088.icf +++ b/targets/TARGET_NXP/TARGET_LPC408X/device/TOOLCHAIN_IAR/LPC4088.icf @@ -11,8 +11,8 @@ define symbol __ICFEDIT_region_NVIC_end__ = 0x100000E7; define symbol __ICFEDIT_region_RAM_start__ = 0x100000E8; define symbol __ICFEDIT_region_RAM_end__ = 0x1000FFDF; /*-Sizes-*/ -/*Heap 1/4 of ram and stack 1/8*/ -define symbol __ICFEDIT_size_cstack__ = 0x2000; +/*Heap 1/4 of ram and stack 1/64*/ +define symbol __ICFEDIT_size_cstack__ = 0x400; define symbol __ICFEDIT_size_heap__ = 0x4000; /**** End of ICF editor section. ###ICF###*/