1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
 *******************************************************************************
 * @file     app_timer.c
 * @create   2023-08-01    
 * @author   Panchip BLE GROUP
 * @note
 * Copyright (c) Shanghai Panchip Microelectronics Co.,Ltd.
 *
 *******************************************************************************
 */
#include "app_timer.h"
#include "PanSeries.h"
#include "utils/fifo.h"
#include "utils/critical.h"
#include "sort_queue.h"
#include "soc_api.h"
 
#if defined(IP_107x) || defined(IP_101x)
static app_timer_t *cur_active_timer = NULL;
static bool global_active = false;
 
/**
 * @brief Timer requests types.
 */
typedef enum{
    TIMER_REQ_START,
    TIMER_REQ_STOP,
    TIMER_REQ_STOP_ALL
}timer_req_type_t;
 
/**
 * @brief Operation request structure.
 */
typedef struct{
    app_timer_t         *p_timer; /**< Timer instance. */
    timer_req_type_t      type;    /**< Request type. */
}timer_req_t;
 
FIFO_DEFINE(timer_req_fifo, MEM_ALIGNED4(sizeof(timer_req_t)), APP_TIMER_REQ_FIFO_ITEM_NUM);
 
bool sort_list_compare_func(sort_queue_item_t *item0, sort_queue_item_t *item1);
SORT_QUEUE_INIT(app_timer_sort_queue, sort_list_compare_func);
 
 
static bool time_little(uint32_t t1, uint32_t t2)
{
    return (uint32_t)(t1 - t2) > BIT(30) ? true:false;
}
 
bool sort_list_compare_func(sort_queue_item_t *item0, sort_queue_item_t *item1)
{
    app_timer_t *ptimer0 = (app_timer_t *)item0;
    app_timer_t *ptimer1 = (app_timer_t *)item1;
 
    if(time_little(ptimer0->endVal, ptimer1->endVal)){
        return true;
    }
    return false;
}
 
uint32_t sort_list_cnt(void)
{
    return sort_queue_cnt(&app_timer_sort_queue);
}
 
void sort_list_add(app_timer_t *ptimer)
{
    sort_queue_add(&app_timer_sort_queue, (sort_queue_item_t *)ptimer);
}
 
app_timer_t *sort_list_pop(void)
{
    return (app_timer_t *)sort_queue_pop(&app_timer_sort_queue);
}
 
app_timer_t *sort_list_peek(void)
{
    return (app_timer_t *)sort_queue_peek(&app_timer_sort_queue);
}
 
void sort_list_remove(app_timer_t *ptimer)
{
    sort_queue_remove(&app_timer_sort_queue, (sort_queue_item_t *)ptimer);
}
 
void sort_list_remove_all(void)
{
    app_timer_t *p_cur_timer = NULL;
    do {
        p_cur_timer = sort_list_pop();
        if(p_cur_timer){
            p_cur_timer->endVal = APP_TIMER_IDLE_VAL;
        }
    }while(p_cur_timer);
}
 
static void timer_req_trigger(void)
{
    NVIC_SetPendingIRQ(SLPTMR_IRQn);
}
 
static int timer_req_send(app_timer_t *ptimer, timer_req_type_t type)
{
    fifo_t *pfifo = &timer_req_fifo;
    if(FIFO_IsFull(pfifo)){
        return APP_TIMER_NO_MEM;
    }
 
    timer_req_t *ptimer_req = (timer_req_t *)FIFO_GetWriteBuf(pfifo);
 
    ptimer_req->type = type;
    ptimer_req->p_timer = ptimer;
 
    FIFO_MoveToNextWriteBuf(pfifo); 
 
    timer_req_trigger();
 
    return APP_TIMER_OK;
}
 
static void timer_req_handler(void)
{
    fifo_t *pfifo = &timer_req_fifo;
 
    while(pfifo->w != pfifo->r)
    {
        timer_req_t *ptimer_req = (timer_req_t *)FIFO_GetReadBuf(pfifo);
        app_timer_t *ptimer = ptimer_req->p_timer;
 
        switch(ptimer_req->type)
        {
        case TIMER_REQ_START:
            if(!APP_TIMER_IS_IDLE(ptimer)){
                sort_list_add(ptimer);
            }
            break;
 
        case TIMER_REQ_STOP:
            if(ptimer == cur_active_timer){
                cur_active_timer = NULL;
            }
            else{
                sort_list_remove(ptimer);
            }
            break;
 
        case TIMER_REQ_STOP_ALL:
            sort_list_remove_all();
            global_active = true;
            break;
 
        default:
            break;
        }
 
        FIFO_MoveToNextReadBuf(pfifo); // must
    }
}
 
uint32_t app_timer_get_cnt_tick(void)
{
#if 1
    soc_busy_wait(35);//must wait 1 32k cycle when read 32k cnt
    return ANA->LP_SLPTMR;
#else
    return soc_lptmr_cycle_get(); //!!!Note: BLE MAC clock must be enabled.
#endif
}
 
void app_timer_set_cmp(uint32_t tick)
{
    ANA->LP_SPACING_TIME1 = tick;
}
 
static void app_timer_expired(app_timer_t *ptimer)
{
    if(global_active == false)
        return;
 
    uint32_t cpu_state = 0;
 
    if(ptimer)
    {
        ENTER_CRITICAL(cpu_state);
        if(ptimer->reloadPeriod == 0){
            ptimer->endVal = APP_TIMER_IDLE_VAL;
        }
        EXIT_CRITICAL(cpu_state);
 
        if(ptimer->handler){
            ptimer->handler(ptimer->ctxt);
        }
 
        ENTER_CRITICAL(cpu_state);
        /* check active flag as it may have been stopped in the user handler */
        if(ptimer->reloadPeriod && !APP_TIMER_IS_IDLE(ptimer)){
            ptimer->endVal += ptimer->reloadPeriod;
 
            //push timer to queue
            sort_list_add(ptimer);
        }
        EXIT_CRITICAL(cpu_state);
    }
}
 
static void app_timer_update(void)
{
    app_timer_t *ptimer = NULL;
    bool reconfig = false;
    while(1)
    {
        ptimer = sort_list_peek();
        if(ptimer == NULL){
            break;
        }
 
        if(APP_TIMER_IS_IDLE(ptimer)){
            sort_list_pop();
            continue;
        }
        else if(cur_active_timer == NULL){
            reconfig = true;
        }
        else if(time_little(ptimer->endVal, cur_active_timer->endVal)){
            if(!APP_TIMER_IS_IDLE(cur_active_timer)){
                sort_list_add(cur_active_timer);
            }
            reconfig = true;
        }
 
        if(!reconfig){
            break;
        }
 
        ptimer = sort_list_pop();
        if((uint32_t)((app_timer_get_cnt_tick()+APP_TIMER_MIN_TIMEOUT_TICKS) - ptimer->endVal) > BIT(30)){
            cur_active_timer = ptimer;
            app_timer_set_cmp((uint32_t)(ptimer->endVal - app_timer_get_cnt_tick()));
            break; //select ok
        }
        else{
            //timer expired
            app_timer_expired(ptimer);
            cur_active_timer = NULL;
 
            // select next timer object
        }
    }
}
 
void sleep_timer1_handler(void)
{
    if(cur_active_timer){
        app_timer_expired(cur_active_timer);
        cur_active_timer = NULL;
    }
}
 
void sleep_timer_post_irq_handler(void)
{
    timer_req_handler();
    app_timer_update();
}
 
void sleep_timer_it_config(uint8_t en)
{
    #define LP_INT_REG_WR_BIT_MASK  (BIT(0) | BIT(16) | BIT(20))
    
    uint32_t state = ANA->LP_INT_CTRL;
 
    if(en){
        ANA->LP_INT_CTRL = (state & LP_INT_REG_WR_BIT_MASK) | ANAC_INT_SLEEP_TMR_INT_EN_Msk;
    }
    else{
        ANA->LP_INT_CTRL = (state & LP_INT_REG_WR_BIT_MASK) & (~ANAC_INT_SLEEP_TMR_INT_EN_Msk);
    }
}
 
int app_timer_init(void)
{
    sleep_timer_it_config(ENABLE);
    NVIC_ClearPendingIRQ(SLPTMR_IRQn);
    NVIC_EnableIRQ(SLPTMR_IRQn);
 
    global_active = true;
    
    return APP_TIMER_OK;
}
 
int app_timer_create(app_timer_t *ptimer, app_timer_mode_t mode, app_timer_timeout_handler_t timeout_handler)
{
    ptimer->endVal  = APP_TIMER_IDLE_VAL;
    ptimer->handler = timeout_handler;
    ptimer->reloadPeriod = (mode == APP_TIMER_MODE_REPEATED ? 1:0);
    
    return APP_TIMER_OK;
}
 
int app_timer_start(app_timer_t *ptimer, uint32_t timeout_ticks, void *ctxt)
{
    uint32_t cpu_state = 0;
 
    if(APP_TIMER_IS_IDLE(ptimer))
    {
        ENTER_CRITICAL(cpu_state);
        ptimer->endVal = app_timer_get_cnt_tick() + timeout_ticks;
        EXIT_CRITICAL(cpu_state);
 
        ptimer->ctxt = ctxt;
 
        if(ptimer->reloadPeriod){
            ptimer->reloadPeriod = timeout_ticks;
        }
 
        return timer_req_send(ptimer, TIMER_REQ_START);
    }
    return APP_TIMER_OK;
}
 
int app_timer_stop(app_timer_t *ptimer)
{
    if(APP_TIMER_IS_IDLE(ptimer)){
        return APP_TIMER_OK;
    }
 
    uint32_t cpu_state = 0;
    ENTER_CRITICAL(cpu_state);
    ptimer->endVal = APP_TIMER_IDLE_VAL;
    EXIT_CRITICAL(cpu_state);
 
    return timer_req_send(ptimer, TIMER_REQ_STOP);
}
 
int app_timer_stop_all(void)
{
    global_active = false;
    return timer_req_send(NULL, TIMER_REQ_STOP_ALL);
}
 
 
 
 
#if APP_TIMER_TEST_EN
#include "pan_ble.h"
app_timer_t app_timer1;
app_timer_t app_timer2;
 
 
void app_timer1_cb(void *ctxt)
{
    static uint8_t flag = 1;
    
    DBG_CHN6_TOGGLE
 
    if(flag){
        app_timer_stop(&app_timer2);
        flag = 0;
    }
    else{
        flag = 1;
        app_timer_start(&app_timer2, (100*32), NULL);
    }
}
 
void app_timer2_cb(void *ctxt)
{
    DBG_CHN5_TOGGLE
}
 
void app_timer_test(void)
{
    app_timer_init();
 
    app_timer_create(&app_timer1, APP_TIMER_MODE_REPEATED, app_timer1_cb);
    app_timer_create(&app_timer2, APP_TIMER_MODE_REPEATED, app_timer2_cb);
 
    app_timer_start(&app_timer1, (500*32), NULL);
    app_timer_start(&app_timer2, (100*32), NULL);
}
#endif
 
#endif