bl_iot_sdk/components/3rdparty/aws-iot/port/timer.c

93 lines
2.5 KiB
C
Raw Normal View History

2020-10-26 12:35:25 +00:00
/*
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
/**
* @file timer.c
* @brief FreeRTOS implementation of the timer interface uses ticks.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <limits.h>
#include "timer_platform.h"
#include <FreeRTOS.h>
#include <task.h>
#define BFL_LOGD(TAG, format, ...) puts(TAG);printf("DEBUG" format "\r\n", ##__VA_ARGS__)
#define BFL_LOGE(TAG, format, ...) puts(TAG);printf("ERROR" format "\r\n", ##__VA_ARGS__)
#define BFL_LOGW(TAG, format, ...) puts(TAG);printf("WARNN" format "\r\n", ##__VA_ARGS__)
const static char *TAG = "aws_timer";
bool has_timer_expired(Timer *timer)
{
uint32_t now = xTaskGetTickCount();
bool expired = (now - timer->start_ticks) >= timer->timeout_ticks;
/* AWS IoT SDK isn't very RTOS friendly because it polls for "done
timers" a lot without ever sleeping on them. So we hack in some
amount of sleeping here: if it seems like the caller is polling
an unexpired timer in a tight loop then we delay a tick to let
things progress elsewhere.
*/
if (!expired && now == timer->last_polled_ticks) {
vTaskDelay(1);
}
timer->last_polled_ticks = now;
return expired;
}
void countdown_ms(Timer *timer, uint32_t timeout)
{
timer->start_ticks = xTaskGetTickCount();
timer->timeout_ticks = timeout / portTICK_PERIOD_MS;
timer->last_polled_ticks = 0;
}
uint32_t left_ms(Timer *timer)
{
uint32_t now = xTaskGetTickCount();
uint32_t elapsed = now - timer->start_ticks;
if (elapsed < timer->timeout_ticks) {
return (timer->timeout_ticks - elapsed) * portTICK_PERIOD_MS;
} else {
return 0;
}
}
void countdown_sec(Timer *timer, uint32_t timeout)
{
if (timeout > UINT32_MAX / 1000) {
BFL_LOGE(TAG, "timeout is out of range: %lus", timeout);
}
countdown_ms(timer, timeout * 1000);
}
void init_timer(Timer *timer)
{
timer->start_ticks = 0;
timer->timeout_ticks = 0;
timer->last_polled_ticks = 0;
}
#ifdef __cplusplus
}
#endif