16b8177c5SMatthias Ringwald /*
26b8177c5SMatthias Ringwald * Copyright (C) 2020 BlueKitchen GmbH
36b8177c5SMatthias Ringwald *
46b8177c5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
56b8177c5SMatthias Ringwald * modification, are permitted provided that the following conditions
66b8177c5SMatthias Ringwald * are met:
76b8177c5SMatthias Ringwald *
86b8177c5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
96b8177c5SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
106b8177c5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
116b8177c5SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
126b8177c5SMatthias Ringwald * documentation and/or other materials provided with the distribution.
136b8177c5SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
146b8177c5SMatthias Ringwald * contributors may be used to endorse or promote products derived
156b8177c5SMatthias Ringwald * from this software without specific prior written permission.
166b8177c5SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
176b8177c5SMatthias Ringwald * personal benefit and not for any commercial purpose or for
186b8177c5SMatthias Ringwald * monetary gain.
196b8177c5SMatthias Ringwald *
206b8177c5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
216b8177c5SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
226b8177c5SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
256b8177c5SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
266b8177c5SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
276b8177c5SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
286b8177c5SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
296b8177c5SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
306b8177c5SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316b8177c5SMatthias Ringwald * SUCH DAMAGE.
326b8177c5SMatthias Ringwald *
336b8177c5SMatthias Ringwald * Please inquire about commercial licensing options at
346b8177c5SMatthias Ringwald * [email protected]
356b8177c5SMatthias Ringwald *
366b8177c5SMatthias Ringwald */
376b8177c5SMatthias Ringwald
386b8177c5SMatthias Ringwald #define BTSTACK_FILE__ "hal_timer.c"
396b8177c5SMatthias Ringwald
406b8177c5SMatthias Ringwald /*
416b8177c5SMatthias Ringwald * hal_timer.c
426b8177c5SMatthias Ringwald * HAL for 32.768 kHz low power timer with 16 bit resolution
436b8177c5SMatthias Ringwald */
446b8177c5SMatthias Ringwald
456b8177c5SMatthias Ringwald #include "hal_timer.h"
466b8177c5SMatthias Ringwald
476b8177c5SMatthias Ringwald #include "stm32l4xx.h"
486b8177c5SMatthias Ringwald
496b8177c5SMatthias Ringwald // access to timers
506b8177c5SMatthias Ringwald extern LPTIM_HandleTypeDef hlptim1;
516b8177c5SMatthias Ringwald
526b8177c5SMatthias Ringwald static void (*hal_timer_callback)(void);
536b8177c5SMatthias Ringwald
HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef * hlptim)546b8177c5SMatthias Ringwald void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim){
556b8177c5SMatthias Ringwald UNUSED(hlptim);
566b8177c5SMatthias Ringwald (*hal_timer_callback)();
576b8177c5SMatthias Ringwald }
586b8177c5SMatthias Ringwald
HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef * hlptim)596b8177c5SMatthias Ringwald void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim){
606b8177c5SMatthias Ringwald UNUSED(hlptim);
616b8177c5SMatthias Ringwald static uint32_t time_seconds = 0;
626b8177c5SMatthias Ringwald time_seconds += 2;
636b8177c5SMatthias Ringwald // printf("Time: %4u s\n", time_seconds);
646b8177c5SMatthias Ringwald }
656b8177c5SMatthias Ringwald
hal_timer_init(void)666b8177c5SMatthias Ringwald void hal_timer_init(void){
676b8177c5SMatthias Ringwald }
686b8177c5SMatthias Ringwald
hal_timer_set_callback(void (* callback)(void))696b8177c5SMatthias Ringwald void hal_timer_set_callback(void (*callback)(void)){
706b8177c5SMatthias Ringwald hal_timer_callback = callback;
716b8177c5SMatthias Ringwald }
726b8177c5SMatthias Ringwald
hal_timer_get_ticks(void)736b8177c5SMatthias Ringwald uint16_t hal_timer_get_ticks(void){
746b8177c5SMatthias Ringwald return HAL_LPTIM_ReadCounter(&hlptim1);
756b8177c5SMatthias Ringwald }
766b8177c5SMatthias Ringwald
hal_timer_stop(void)776b8177c5SMatthias Ringwald void hal_timer_stop(void){
786b8177c5SMatthias Ringwald __HAL_LPTIM_DISABLE_IT(&hlptim1, LPTIM_IT_CMPM);
796b8177c5SMatthias Ringwald __HAL_LPTIM_CLEAR_FLAG(&hlptim1, LPTIM_IT_CMPM);
806b8177c5SMatthias Ringwald }
816b8177c5SMatthias Ringwald
hal_timer_start(uint16_t timeout_ticks)826b8177c5SMatthias Ringwald void hal_timer_start(uint16_t timeout_ticks){
836b8177c5SMatthias Ringwald __HAL_LPTIM_COMPARE_SET(&hlptim1, timeout_ticks);
846b8177c5SMatthias Ringwald __HAL_LPTIM_ENABLE_IT(&hlptim1, LPTIM_IT_CMPM);
856b8177c5SMatthias Ringwald }