From 1397aea9459de9e48b7f339ebacdbb0dc8ae8982 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 2 Sep 2020 22:50:00 +0200 Subject: [PATCH] stm32-sx1280: extract hal_timer.h --- port/stm32-sx1280/Makefile | 1 + port/stm32-sx1280/controller/hal_timer.c | 85 ++++++++++++++++++++++++ port/stm32-sx1280/controller/hal_timer.h | 85 ++++++++++++++++++++++++ port/stm32-sx1280/controller/ll_sx1280.c | 34 ++++------ 4 files changed, 183 insertions(+), 22 deletions(-) create mode 100644 port/stm32-sx1280/controller/hal_timer.c create mode 100644 port/stm32-sx1280/controller/hal_timer.h diff --git a/port/stm32-sx1280/Makefile b/port/stm32-sx1280/Makefile index 1a207ce10..1b72fa49c 100644 --- a/port/stm32-sx1280/Makefile +++ b/port/stm32-sx1280/Makefile @@ -83,6 +83,7 @@ controller/controller.c \ controller/hci_event.c \ controller/hopping.c \ controller/ll_sx1280.c \ +controller/hal_timer.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_cortex.c \ Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_dma.c \ diff --git a/port/stm32-sx1280/controller/hal_timer.c b/port/stm32-sx1280/controller/hal_timer.c new file mode 100644 index 000000000..f7ba85a47 --- /dev/null +++ b/port/stm32-sx1280/controller/hal_timer.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2020 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD 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. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +#define BTSTACK_FILE__ "hal_timer.c" + +/* + * hal_timer.c + * HAL for 32.768 kHz low power timer with 16 bit resolution + */ + +#include "hal_timer.h" + +#include "stm32l4xx.h" + +// access to timers +extern LPTIM_HandleTypeDef hlptim1; + +static void (*hal_timer_callback)(void); + +void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim){ + UNUSED(hlptim); + (*hal_timer_callback)(); +} + +void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim){ + UNUSED(hlptim); + static uint32_t time_seconds = 0; + time_seconds += 2; + // printf("Time: %4u s\n", time_seconds); +} + +void hal_timer_init(void){ +} + +void hal_timer_set_callback(void (*callback)(void)){ + hal_timer_callback = callback; +} + +uint16_t hal_timer_get_ticks(void){ + return HAL_LPTIM_ReadCounter(&hlptim1); +} + +void hal_timer_stop(void){ + __HAL_LPTIM_DISABLE_IT(&hlptim1, LPTIM_IT_CMPM); + __HAL_LPTIM_CLEAR_FLAG(&hlptim1, LPTIM_IT_CMPM); +} + +void hal_timer_start(uint16_t timeout_ticks){ + __HAL_LPTIM_COMPARE_SET(&hlptim1, timeout_ticks); + __HAL_LPTIM_ENABLE_IT(&hlptim1, LPTIM_IT_CMPM); +} \ No newline at end of file diff --git a/port/stm32-sx1280/controller/hal_timer.h b/port/stm32-sx1280/controller/hal_timer.h new file mode 100644 index 000000000..35018fd36 --- /dev/null +++ b/port/stm32-sx1280/controller/hal_timer.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2020 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD 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. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +/* + * hci_event.h + */ + +#ifndef HAL_TIMER_H +#define HCI_TIMER_H + +#include "bluetooth.h" + +#include +#include + +#if defined __cplusplus +extern "C" { +#endif + +/* + * @brief Initialize 32.768 kHz timer, usually low power and used by RTC and in deep sleep + */ +void hal_timer_init(void); + +/* + * @brief Set Timer Callback + * @param callback + */ +void hal_timer_set_callback(void (*callback)(void)); + +/** + * @brief Get current ticks + * @return num_ticks + */ +uint16_t hal_timer_get_ticks(void); + +/** + * @brief Stop Timer + */ +void hal_timer_stop(void); + +/** + * @brief Start Timer and fire at given timeout + * @param timeout_ticks timeout in ticks + */ +void hal_timer_start(uint16_t timeout_ticks); + +#if defined __cplusplus +} +#endif +#endif // HAL_TIMER_H diff --git a/port/stm32-sx1280/controller/ll_sx1280.c b/port/stm32-sx1280/controller/ll_sx1280.c index 0aa69ebd0..f3ee45a64 100644 --- a/port/stm32-sx1280/controller/ll_sx1280.c +++ b/port/stm32-sx1280/controller/ll_sx1280.c @@ -56,6 +56,8 @@ #include "hal_cpu.h" #include "hci_event.h" #include "hopping.h" +#include "hal_timer.h" + // // configuration @@ -69,9 +71,6 @@ #define TX_PARAMS_OUTPUT_POWER 10 -// access to timers -extern TIM_HandleTypeDef htim2; -extern LPTIM_HandleTypeDef hlptim1; #define ACL_LE_MAX_PAYLOAD 31 #define ADV_MAX_PAYLOAD (6+6+22) @@ -497,7 +496,7 @@ static void start_advertising(void){ // prepare ctx.channel = 36; - ctx.anchor_ticks = HAL_LPTIM_ReadCounter(&hlptim1); + ctx.anchor_ticks = hal_timer_get_ticks(); // and get started ll_advertising_statemachine(); @@ -513,17 +512,16 @@ static void start_hopping(void){ Radio.SetPacketParams( &packetParams ); } + static void radio_stop_timer(void){ - __HAL_LPTIM_DISABLE_IT(&hlptim1, LPTIM_IT_CMPM); - __HAL_LPTIM_CLEAR_FLAG(&hlptim1, LPTIM_IT_CMPM); + hal_timer_stop(); } static void radio_set_timer_ticks(uint32_t anchor_offset_ticks){ radio_stop_timer(); // set timer for next radio event relative to anchor uint16_t timeout_ticks = (uint16_t) (ctx.anchor_ticks + anchor_offset_ticks); - __HAL_LPTIM_COMPARE_SET(&hlptim1, timeout_ticks); - __HAL_LPTIM_ENABLE_IT(&hlptim1, LPTIM_IT_CMPM); + hal_timer_start(timeout_ticks); } static void ll_terminate(void){ @@ -554,7 +552,7 @@ static void ll_terminate(void){ static void radio_timer_handler(void){ - uint16_t t0 = HAL_LPTIM_ReadCounter(&hlptim1); + uint16_t t0 = hal_timer_get_ticks(); switch (ll_state){ case LL_STATE_CONNECTED: @@ -617,18 +615,6 @@ static void radio_timer_handler(void){ } -void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim){ - UNUSED(hlptim); - radio_timer_handler(); -} - -void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim){ - UNUSED(hlptim); - static uint32_t time_seconds = 0; - time_seconds += 2; - // printf("Time: %4u s\n", time_seconds); -} - /** Radio IRQ handlers */ static void radio_on_tx_done(void ){ switch (radio_state){ @@ -641,7 +627,7 @@ static void radio_on_tx_done(void ){ } static void radio_on_rx_done(void ){ - uint16_t packet_end_ticks = HAL_LPTIM_ReadCounter(&hlptim1); + uint16_t packet_end_ticks = hal_timer_get_ticks(); ll_pdu_t * rx_packet; bool tx_acked; @@ -798,6 +784,10 @@ void ll_init(void){ // default channels, advertising interval ctx.adv_map = 0x7; ctx.adv_interval_us = 1280000; + + // init timer + hal_timer_init(); + hal_timer_set_callback(&radio_timer_handler); } void ll_radio_on(void){