1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
24 #include "allocator.h"
27 #include "freertos/FreeRTOSConfig.h"
28 #include "freertos/xtensa_api.h"
29 #include "rom/ets_sys.h"
31 #define RTC_TIMER_TICKS_TO_MS(ticks) (((ticks/625)<<1) + (ticks-(ticks/625)*625)/312)
34 #define BT_ALARM_START_WAIT_TICKS 100
35 #define BT_ALARM_STOP_WAIT_TICKS 100
36 #define BT_ALARM_FREE_WAIT_TICKS 100
37 #define BT_ALARM_CHG_PERIOD_WAIT_TICKS 100
39 static struct alarm_t alarm_cbs[ALARM_CBS_NUM];
41 void osi_alarm_init(void)
43 memset(&alarm_cbs[0], 0x00, sizeof(alarm_cbs));
46 static struct alarm_t *alarm_cbs_lookfor_available(void)
50 for (i = 0; i < ALARM_CBS_NUM; i++) {
51 if (alarm_cbs[i].alarm_hdl == NULL) { //available
52 LOG_DEBUG("%s %d %p\n", __func__, i, &alarm_cbs[i]);
60 static void alarm_cb_handler(TimerHandle_t xTimer)
62 struct alarm_t *alarm;
65 LOG_ERROR("TimerName: NULL\n");
69 alarm = pvTimerGetTimerID(xTimer);
70 LOG_DEBUG("TimerID %p, Name %s\n", alarm, pcTimerGetTimerName(xTimer));
72 alarm->cb(alarm->cb_data);
76 osi_alarm_t *osi_alarm_new(char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire, bool is_periodic)
78 struct alarm_t *timer_id;
81 if (timer_expire == 0) {
86 timer_id = alarm_cbs_lookfor_available();
88 LOG_ERROR("%s full\n", __func__);
92 portBASE_TYPE auto_reload = is_periodic ? pdTRUE : pdFALSE;
93 t = xTimerCreate(alarm_name, timer_expire / portTICK_PERIOD_MS, auto_reload, timer_id, alarm_cb_handler);
95 LOG_ERROR("%s error\n", __func__);
99 timer_id->alarm_hdl = t;
100 timer_id->cb = callback;
101 timer_id->cb_data = data;
102 /* TODO mutex unlock */
107 int osi_alarm_free(osi_alarm_t *alarm)
110 LOG_ERROR("%s null\n", __func__);
114 if (xTimerDelete(alarm->alarm_hdl, BT_ALARM_FREE_WAIT_TICKS) != pdPASS) {
115 LOG_ERROR("%s error\n", __func__);
119 /* TODO mutex lock */
120 memset(alarm, 0x00, sizeof(osi_alarm_t));
121 /* TODO mutex unlock */
127 int osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout)
130 LOG_ERROR("%s null\n", __func__);
134 if (xTimerChangePeriod(alarm->alarm_hdl, timeout / portTICK_PERIOD_MS, BT_ALARM_CHG_PERIOD_WAIT_TICKS) != pdPASS) {
135 LOG_ERROR("%s chg period error\n", __func__);
139 if (xTimerStart(alarm->alarm_hdl, BT_ALARM_START_WAIT_TICKS) != pdPASS) {
140 LOG_ERROR("%s start error\n", __func__);
148 int osi_alarm_cancel(osi_alarm_t *alarm)
151 LOG_ERROR("%s null\n", __func__);
155 if (xTimerStop(alarm->alarm_hdl, BT_ALARM_STOP_WAIT_TICKS) != pdPASS) {
156 LOG_ERROR("%s error\n", __func__);
163 static uint32_t alarm_current_tick(void)
165 return xTaskGetTickCount();
168 // todo: this is not accurate
169 // max return value: 0xffffffff / 312 = 13765920 = 0xD20D20
170 period_ms_t osi_alarm_now(void)
172 return RTC_TIMER_TICKS_TO_MS((alarm_current_tick()));
175 period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm)
177 /* TODO: use FreeRTOS timer.c implement ??? */
181 // pre-condition: 0 <= t1, t2 <= 0xD20D20
182 // return value: 0<= ret <=0XD20D20
183 period_ms_t osi_alarm_time_diff(period_ms_t t1, period_ms_t t2)
185 #define MAX_ALARM_TIME_MS (0xD20D20)
186 int32_t diff = (int32_t)(t1) - (int32_t)(t2);
188 diff += MAX_ALARM_TIME_MS;
190 return (period_ms_t)diff;