-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNHW_54L_CLOCK.c
More file actions
574 lines (490 loc) · 19.6 KB
/
NHW_54L_CLOCK.c
File metadata and controls
574 lines (490 loc) · 19.6 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* nRF54L
* CLOCK - Clock control
* POWER - POWER control
* RESET - RESET control
*
* Note: CLOCK:
* 1. This model ignores the system ON/OFF status
*
* 2. We assume the 32.768 KHz clock does not drift relative to the 64MHz one.
* This would only be the case if they had the same source or there was a tracking
* and adjustment loop of one based on the other.
*
* 3. By default all tasks complete in 1 delta cycle, but it is possible to
* cause them to take time by using the nhw_clock_cheat_* APIs
*
* 4. LFCLK.SRC is mostly ignored (beyond copying LFCLK.SRC to LFCLK.SRCCOPY and LFCLK.STAT)
* and optionally taking a different amount of time to start.
*
* 5. Turning the clocks on/off has no effect on other peripherals models. Other peripherals
* do not request clocks to this model.
* So triggering a clock STOP task will always be done right away, without needing
* to wait for a peripheral which is using that clock to stop requesting it.
*
* There is no relationship to the OSCILLATORS peripheral yet.
*
* 6. From the spec, it is unclear if the *.RUN.STATUS register fields are kept on even
* after the START operation is completed or they are cleared.
* But it seems (and this is what the model does), that they are cleared after the STOP task is triggered.
*
* 7. XOTUNE does nothing more than generate the XOTUNED/XOTUNEFAILED event. It will only fail
* if set to do so with the nhw_clock_cheat_set_xotune_fail() interface.
*
* 8. The models do not check the requirement of having the HFXO clock running to be
* able to run the RADIO. The RADIO models will run just fine without it.
*
* Notes: POWER & RESET
* * Only a register stub without functionality is provided
*/
#include <string.h>
#include <stdint.h>
#include "NHW_config.h"
#include "NHW_peri_types.h"
#include "NHW_54L_CLOCK.h"
#include "NHW_templates.h"
#include "NHW_xPPI.h"
#include "irq_ctrl.h"
#include "bs_tracing.h"
#include "bs_utils.h"
#include "nsi_tasks.h"
#include "nsi_hws_models_if.h"
union NRF_CLKPWR_Type NRF_CLKPWR_regs[NHW_CLKPWR_TOTAL_INST];
NRF_CLOCK_Type *NRF_CLOCK_regs[NHW_CLKPWR_TOTAL_INST];
#if (NHW_CLKPWR_HAS_POWER)
NRF_POWER_Type *NRF_POWER_regs[NHW_CLKPWR_TOTAL_INST];
#endif
NRF_RESET_Type *NRF_RESET_regs[NHW_CLKPWR_TOTAL_INST];
enum clock_states {Stopped = 0, Starting, Started, Stopping};
enum tuning_states {Tunning_stopped = 0, Tuning_ok, Tuning_fail};
struct clkpwr_status {
bs_time_t Timer_PLL_128MXO; //Note nhw_CLOCK_update_master_timer() assumes this is first
bs_time_t Timer_PLL_128Mfree;
bs_time_t Timer_LFCLK;
#if (NHW_CLKPWR_HAS_PLL_24M)
bs_time_t Timer_PLL_24M;
#endif
bs_time_t Timer_CAL;
bs_time_t Timer_XOTUNE; //Note nhw_CLOCK_update_master_timer() assumes this is last
int HFXO_users; //How many PLLs (0, 1 or 2) are currently requesting the HFXO
enum clock_states PLL_128MXO_state; //State of the 128M PLL locked to the HFXO
enum clock_states PLL_128Mfree_state; //State of the 128M PLL freerunning
enum clock_states LFCLK_state;
enum clock_states PLL_24M_state; //State of the 24M PLL locked to the HFXO
enum clock_states CAL_state;
enum tuning_states HFXOTUNE_state;
bs_time_t CLOCK_start_times[NHW_CLKPWR_N_CLKS][NHW_CLKPWR_CLK_MAX_N_SRCS];
bs_time_t XOtuning_durations[2]; /* 0: Success, 1: Fail */
bs_time_t XOtuning_pending_fails;
bs_time_t CAL_duration;
};
static bs_time_t Timer_PWRCLK = TIME_NEVER;
static struct clkpwr_status nhw_clkpwr_st;
static uint nhw_CLOCK_dppi_map[] = NHW_CLKPWR_DPPI_MAP;
static void nhw_CLOCK_PLL_128MXO_Timer_triggered(void);
static void nhw_CLOCK_LFCLK_triggered(void);
static void nhw_CLOCK_PLL_128Mfree_Timer_triggered(void);
#if (NHW_CLKPWR_HAS_PLL_24M)
static void nhw_CLOCK_PLL_24MXO_Timer_triggered(void);
#endif
static void nhw_CLOCK_update_master_timer(void) {
Timer_PWRCLK = nhw_clkpwr_st.Timer_PLL_128MXO;
for (bs_time_t *t = &nhw_clkpwr_st.Timer_PLL_128MXO + 1; t <= &nhw_clkpwr_st.Timer_XOTUNE; t++) {
Timer_PWRCLK = BS_MIN(Timer_PWRCLK, *t);
}
nsi_hws_find_next_event();
}
static void nhw_CLOCK_init(void) {
NRF_CLOCK_regs[0] = (NRF_CLOCK_Type *)&NRF_CLKPWR_regs[0];
#if (NHW_CLKPWR_HAS_POWER)
NRF_POWER_regs[0] = (NRF_POWER_Type *)&NRF_CLKPWR_regs[0];
#endif
NRF_RESET_regs[0] = (NRF_RESET_Type *)&NRF_CLKPWR_regs[0];
memset(NRF_CLKPWR_regs, 0, sizeof(NRF_CLKPWR_regs));
nhw_clkpwr_st.Timer_PLL_128MXO = TIME_NEVER;
nhw_clkpwr_st.Timer_PLL_128Mfree = TIME_NEVER;
nhw_clkpwr_st.Timer_LFCLK = TIME_NEVER;
#if (NHW_CLKPWR_HAS_PLL_24M)
nhw_clkpwr_st.Timer_PLL_24M = TIME_NEVER;
#endif
nhw_clkpwr_st.Timer_CAL = TIME_NEVER;
nhw_clkpwr_st.Timer_XOTUNE= TIME_NEVER;
nhw_clkpwr_st.HFXO_users = 0;
nhw_clkpwr_st.PLL_128MXO_state = Stopped;
nhw_clkpwr_st.LFCLK_state = Stopped;
nhw_clkpwr_st.PLL_24M_state = Stopped;
nhw_clkpwr_st.PLL_128Mfree_state = Stopped;
nhw_clkpwr_st.CAL_state = Stopped;
nhw_clkpwr_st.HFXOTUNE_state= Tunning_stopped;
nhw_CLOCK_update_master_timer();
bs_time_t nhw_clock_start_times[NHW_CLKPWR_N_CLKS][NHW_CLKPWR_CLK_MAX_N_SRCS] = NHW_CLKPWR_CLK_START_TIMES;
memcpy(nhw_clkpwr_st.CLOCK_start_times, nhw_clock_start_times, sizeof(nhw_clock_start_times));
bs_time_t nhw_clock_tune_durs[2] = NHW_CLKPWR_CLK_XOTUNE_TIMES;
memcpy(nhw_clkpwr_st.XOtuning_durations, nhw_clock_tune_durs, sizeof(nhw_clock_tune_durs));
nhw_clkpwr_st.CAL_duration = NHW_CLKPWR_CLK_CAL_TIME;
}
NSI_TASK(nhw_CLOCK_init, HW_INIT, 100);
static void nhw_CLOCK_eval_interrupt(uint inst) {
(void) inst;
static struct nhw_irq_mapping nhw_CLOCK_irq_map[] = NHW_CLKPWR_INT_MAP;
static bool clock_int_line; /* Is the CLOCK currently driving its interrupt line high */
bool new_int_line = false;
NRF_CLOCK_regs[0]->INTPEND = 0;
#define check_interrupt(x) \
if (NRF_CLOCK_regs[0]->EVENTS_ ##x && (NRF_CLOCK_regs[0]->INTEN & CLOCK_INTENSET_## x ##_Msk)){ \
new_int_line = 1; \
NRF_CLOCK_regs[0]->INTPEND |= CLOCK_INTENSET_## x ##_Msk; \
}
check_interrupt(XOSTARTED)
check_interrupt(PLLSTARTED)
#if (NHW_CLKPWR_HAS_LFCLK)
check_interrupt(LFCLKSTARTED)
#endif
check_interrupt(DONE)
check_interrupt(XOTUNED)
check_interrupt(XOTUNEERROR)
check_interrupt(XOTUNEFAILED)
#if (NHW_CLKPWR_HAS_PLL_24M)
check_interrupt(XO24MSTARTED)
#endif
hw_irq_ctrl_toggle_level_irq_line_if(&clock_int_line,
new_int_line,
&nhw_CLOCK_irq_map[0]);
}
static void nhw_CLOCK_TASK_XOSTART(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.PLL_128MXO_state == Stopped ) || (nhw_clkpwr_st.PLL_128MXO_state == Stopping)) {
nhw_clkpwr_st.PLL_128MXO_state = Starting;
NRF_CLOCK_regs[0]->XO.RUN = CLOCK_XO_RUN_STATUS_Msk;
nhw_clkpwr_st.Timer_PLL_128MXO = nsi_hws_get_time() + nhw_clkpwr_st.CLOCK_start_times[NHW_CLKPWR_CLK_IDX_XO][0];
nhw_CLOCK_update_master_timer();
}
}
static void nhw_CLOCK_TASK_XOSTOP(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.PLL_128MXO_state != Stopping) && (nhw_clkpwr_st.PLL_128MXO_state != Stopped)) {
nhw_clkpwr_st.PLL_128MXO_state = Stopping;
NRF_CLOCK_regs[0]->XO.RUN = 0;
/* Instantaneous stop */
nhw_CLOCK_PLL_128MXO_Timer_triggered();
}
}
static void nhw_CLOCK_TASK_PLLSTART(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.PLL_128Mfree_state == Stopped ) || (nhw_clkpwr_st.PLL_128Mfree_state == Stopping)) {
nhw_clkpwr_st.PLL_128Mfree_state = Starting;
NRF_CLOCK_regs[0]->PLL.RUN = CLOCK_PLL_RUN_STATUS_Msk;
nhw_clkpwr_st.Timer_PLL_128Mfree = nsi_hws_get_time();
nhw_CLOCK_update_master_timer();
}
}
static void nhw_CLOCK_TASK_PLLSTOP(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.PLL_128Mfree_state == Started) || (nhw_clkpwr_st.PLL_128Mfree_state == Starting)) {
nhw_clkpwr_st.PLL_128Mfree_state = Stopping;
NRF_CLOCK_regs[0]->PLL.RUN = 0;
/* Instantaneous stop */
nhw_CLOCK_PLL_128Mfree_Timer_triggered();
}
}
#if (NHW_CLKPWR_HAS_LFCLK)
static void nhw_CLOCK_TASK_LFCLKSTART(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.LFCLK_state == Stopped ) || (nhw_clkpwr_st.LFCLK_state == Stopping)) {
uint src = NRF_CLOCK_regs[0]->LFCLK.SRC & CLOCK_LFCLK_SRC_SRC_Msk;
nhw_clkpwr_st.LFCLK_state = Starting;
NRF_CLOCK_regs[0]->LFCLK.RUN = CLOCK_LFCLK_RUN_STATUS_Msk;
NRF_CLOCK_regs[0]->LFCLK.SRCCOPY = src;
nhw_clkpwr_st.Timer_LFCLK = nsi_hws_get_time() + nhw_clkpwr_st.CLOCK_start_times[NHW_CLKPWR_CLK_IDX_LF][src];
nhw_CLOCK_update_master_timer();
}
}
static void nhw_CLOCK_TASK_LFCLKSTOP(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.LFCLK_state == Started) || (nhw_clkpwr_st.LFCLK_state == Starting)) {
nhw_clkpwr_st.LFCLK_state = Stopping;
NRF_CLOCK_regs[0]->LFCLK.RUN = 0;
/* Instantaneous stop */
nhw_CLOCK_LFCLK_triggered();
}
}
#endif
#if (NHW_CLKPWR_HAS_PLL_24M)
static void nhw_CLOCK_TASK_XO24MSTART(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.PLL_24M_state == Stopped ) || (nhw_clkpwr_st.PLL_24M_state == Stopping)) {
nhw_clkpwr_st.PLL_24M_state = Starting;
NRF_CLOCK_regs[0]->PLL24M.RUN = CLOCK_PLL24M_RUN_STATUS_Msk;
nhw_clkpwr_st.Timer_PLL_24M = nsi_hws_get_time() + nhw_clkpwr_st.CLOCK_start_times[NHW_CLKPWR_CLK_IDX_XO24M][0];
nhw_CLOCK_update_master_timer();
}
}
static void nhw_CLOCK_TASK_XO24MSTOP(uint inst) {
(void) inst;
if ((nhw_clkpwr_st.PLL_24M_state != Stopping) && (nhw_clkpwr_st.PLL_24M_state != Stopped)) {
nhw_clkpwr_st.PLL_24M_state = Stopping;
NRF_CLOCK_regs[0]->PLL24M.RUN = 0;
/* Instantaneous stop */
nhw_CLOCK_PLL_24MXO_Timer_triggered();
}
}
#endif
static void nhw_CLOCK_TASK_CAL(uint inst) {
(void) inst;
if (nhw_clkpwr_st.PLL_128MXO_state != Started) { /* LCOV_EXCL_START */
bs_trace_warning_line("%s: Triggered RC oscillator calibration with the HFXO CLK stopped "
"(the model does not have a problem with this, but this is against "
"the spec)\n", __func__);
} /* LCOV_EXCL_STOP */
if ((nhw_clkpwr_st.CAL_state == Stopped ) || (nhw_clkpwr_st.CAL_state == Stopping)) {
nhw_clkpwr_st.CAL_state = Starting;
nhw_clkpwr_st.Timer_CAL = nsi_hws_get_time() + nhw_clkpwr_st.CAL_duration;
nhw_CLOCK_update_master_timer();
}
}
static void nhw_CLOCK_TASK_XOTUNE(uint inst) {
(void)inst;
if (nhw_clkpwr_st.HFXO_users == 0) {
bs_trace_warning_time_line("TASK XOTUNE triggered but HFXO was not started\n");
}
if (nhw_clkpwr_st.HFXOTUNE_state == Tunning_stopped) {
if (nhw_clkpwr_st.XOtuning_pending_fails > 0) {
nhw_clkpwr_st.XOtuning_pending_fails--;
nhw_clkpwr_st.HFXOTUNE_state = Tuning_fail;
nhw_clkpwr_st.Timer_XOTUNE = nsi_hws_get_time() + nhw_clkpwr_st.XOtuning_durations[1];
} else {
nhw_clkpwr_st.HFXOTUNE_state = Tuning_ok;
nhw_clkpwr_st.Timer_XOTUNE = nsi_hws_get_time() + nhw_clkpwr_st.XOtuning_durations[0];
}
nhw_CLOCK_update_master_timer();
}
}
static void nhw_CLOCK_TASK_XOTUNEABORT(uint inst) {
(void) inst;
nhw_clkpwr_st.HFXOTUNE_state = Tunning_stopped;
nhw_clkpwr_st.Timer_XOTUNE = TIME_NEVER;
nhw_CLOCK_update_master_timer();
}
NHW_SIDEEFFECTS_INTSET(CLOCK, NRF_CLOCK_regs[0]->, NRF_CLOCK_regs[0]->INTEN)
NHW_SIDEEFFECTS_INTCLR(CLOCK, NRF_CLOCK_regs[0]->, NRF_CLOCK_regs[0]->INTEN)
NHW_SIDEEFFECTS_INTEN(CLOCK, NRF_CLOCK_regs[0]->, NRF_CLOCK_regs[0]->INTEN)
NHW_SIDEEFFECTS_EVENTS(CLOCK)
void nhw_pwrclk_regw_sideeffects_EVENTS_all(uint inst) {
nhw_CLOCK_eval_interrupt(inst);
}
#if !defined(CLOCK_PUBLISH_XOSTARTED_ResetValue) /* no PUBLISH registers in MDK */
#undef NHW_SIGNAL_EVENT
#define NHW_SIGNAL_EVENT(peri, peri_regs, event) \
void nhw_##peri##_signal_EVENTS_##event(unsigned int inst) \
{ \
peri_regs EVENTS_##event = 1; \
nhw_##peri##_eval_interrupt(inst); \
}
#endif
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, XOSTARTED)
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, PLLSTARTED)
#if (NHW_CLKPWR_HAS_LFCLK)
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, LFCLKSTARTED)
#endif
#if (NHW_CLKPWR_HAS_PLL_24M)
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, XO24MSTARTED)
#endif
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, DONE)
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, XOTUNED)
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, XOTUNEERROR)
NHW_SIGNAL_EVENT(CLOCK, NRF_CLOCK_regs[0]->, XOTUNEFAILED)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, XOSTART)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, XOSTOP)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, PLLSTART)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, PLLSTOP)
#if (NHW_CLKPWR_HAS_LFCLK)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, LFCLKSTART)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, LFCLKSTOP)
#endif
#if (NHW_CLKPWR_HAS_PLL_24M)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, XO24MSTART)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, XO24MSTOP)
#endif
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, CAL)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, XOTUNE)
NHW_SIDEEFFECTS_TASKS(CLOCK, NRF_CLOCK_regs[0]->, XOTUNEABORT)
#define NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(task) \
static void nhw_CLOCK_TASK_##task##_wrap(void *param) { \
nhw_CLOCK_TASK_##task((int) param); \
} \
void nhw_CLOCK_regw_sideeffects_SUBSCRIBE_##task(unsigned int inst) { \
static struct nhw_subsc_mem task##_subscribed[NHW_CLKPWR_TOTAL_INST]; \
nhw_dppi_common_subscribe_sideeffect(nhw_CLOCK_dppi_map[inst], \
NRF_CLOCK_regs[0]->SUBSCRIBE_##task, \
&task##_subscribed[inst], \
nhw_CLOCK_TASK_##task##_wrap, \
(void*) inst); \
}
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(XOSTART)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(XOSTOP)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(PLLSTART)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(PLLSTOP)
#if (NHW_CLKPWR_HAS_LFCLK)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(LFCLKSTART)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(LFCLKSTOP)
#endif
#if (NHW_CLKPWR_HAS_PLL_24M)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(XO24MSTART)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(XO24MSTOP)
#endif
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(CAL)
#if defined(CLOCK_SUBSCRIBE_XOTUNE_EN_Msk)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(XOTUNE)
#endif
#if defined(CLOCK_SUBSCRIBE_XOTUNEABORT_EN_Msk)
NHW_CLOCK_SIDEEFFECTS_SUBSCRIBE(XOTUNEABORT)
#endif
static void nhw_CLOCK_XOTUNEtimer_triggered(void);
static void nhw_CLOCK_request_HFXO(uint32_t mask /* bit mask, with one bit for each user */) {
bool start = (nhw_clkpwr_st.HFXO_users == 0);
nhw_clkpwr_st.HFXO_users |= mask;
if (start) {
nhw_CLOCK_TASK_XOTUNE(0);
if ((nhw_clkpwr_st.XOtuning_durations[0] == 0) && (nhw_clkpwr_st.HFXOTUNE_state == Tuning_ok)) {
//Let's raise the event in this same delta cycle in this particular case
// This is just to avoid a possibly backwards incompatible change in behavior.
// Without this, nhw_CLOCK_XOTUNEtimer_triggered() would be called in 1 delta
nhw_CLOCK_XOTUNEtimer_triggered();
}
}
}
static void nhw_CLOCK_release_HFXO(uint32_t mask /* bit mask, with one bit for each user */) {
nhw_clkpwr_st.HFXO_users &= ~mask;
}
static void nhw_CLOCK_PLL_128MXO_Timer_triggered(void) {
nhw_clkpwr_st.Timer_PLL_128MXO = TIME_NEVER;
nhw_CLOCK_update_master_timer();
if ( nhw_clkpwr_st.PLL_128MXO_state == Starting ){
nhw_clkpwr_st.PLL_128MXO_state = Started;
NRF_CLOCK_regs[0]->XO.STAT = CLOCK_XO_STAT_STATE_Msk;
nhw_CLOCK_request_HFXO(1 /*128M PLL*/);
nhw_CLOCK_signal_EVENTS_XOSTARTED(0);
} else if ( nhw_clkpwr_st.PLL_128MXO_state == Stopping ){
nhw_CLOCK_release_HFXO(1 /*128M PLL*/);
nhw_clkpwr_st.PLL_128MXO_state = Stopped;
NRF_CLOCK_regs[0]->XO.STAT = 0;
}
}
static void nhw_CLOCK_PLL_128Mfree_Timer_triggered(void) {
nhw_clkpwr_st.Timer_PLL_128Mfree = TIME_NEVER;
nhw_CLOCK_update_master_timer();
if ( nhw_clkpwr_st.PLL_128Mfree_state == Starting ){
nhw_clkpwr_st.PLL_128Mfree_state = Started;
NRF_CLOCK_regs[0]->PLL.STAT = CLOCK_PLL_STAT_STATE_Msk;
nhw_CLOCK_signal_EVENTS_PLLSTARTED(0);
} else if ( nhw_clkpwr_st.PLL_128Mfree_state == Stopping ){
nhw_clkpwr_st.PLL_128Mfree_state = Stopped;
NRF_CLOCK_regs[0]->PLL.STAT = 0;
}
}
static void nhw_CLOCK_LFCLK_triggered(void) {
#if (NHW_CLKPWR_HAS_LFCLK)
nhw_clkpwr_st.Timer_LFCLK = TIME_NEVER;
nhw_CLOCK_update_master_timer();
if ( nhw_clkpwr_st.LFCLK_state == Starting ){
nhw_clkpwr_st.LFCLK_state = Started;
NRF_CLOCK_regs[0]->LFCLK.STAT = CLOCK_LFCLK_STAT_STATE_Msk
| (NRF_CLOCK_regs[0]->LFCLK.SRCCOPY << CLOCK_LFCLK_STAT_SRC_Pos);
nhw_CLOCK_signal_EVENTS_LFCLKSTARTED(0);
} else if ( nhw_clkpwr_st.LFCLK_state == Stopping ){
nhw_clkpwr_st.LFCLK_state = Stopped;
NRF_CLOCK_regs[0]->LFCLK.STAT = 0;
}
#endif
}
#if (NHW_CLKPWR_HAS_PLL_24M)
static void nhw_CLOCK_PLL_24MXO_Timer_triggered(void) {
nhw_clkpwr_st.Timer_PLL_24M = TIME_NEVER;
nhw_CLOCK_update_master_timer();
if (nhw_clkpwr_st.PLL_24M_state == Starting) {
nhw_clkpwr_st.PLL_24M_state = Started;
NRF_CLOCK_regs[0]->PLL24M.STAT = CLOCK_PLL24M_STAT_STATE_Msk;
nhw_CLOCK_request_HFXO(2 /*24M PLL*/);
nhw_CLOCK_signal_EVENTS_XO24MSTARTED(0);
} else if (nhw_clkpwr_st.PLL_24M_state == Stopping) {
nhw_CLOCK_release_HFXO(2 /*24M PLL*/);
nhw_clkpwr_st.PLL_24M_state = Stopped;
NRF_CLOCK_regs[0]->PLL24M.STAT = 0;
}
}
#endif
static void nhw_CLOCK_CALtimer_triggered(void) {
nhw_clkpwr_st.CAL_state = Stopped;
nhw_clkpwr_st.Timer_CAL = TIME_NEVER;
nhw_CLOCK_update_master_timer();
nhw_CLOCK_signal_EVENTS_DONE(0);
}
static void nhw_CLOCK_XOTUNEtimer_triggered(void) {
bool failed = (nhw_clkpwr_st.HFXOTUNE_state == Tuning_fail);
nhw_clkpwr_st.HFXOTUNE_state = Tunning_stopped;
nhw_clkpwr_st.Timer_XOTUNE = TIME_NEVER;
nhw_CLOCK_update_master_timer();
if (failed) {
nhw_CLOCK_signal_EVENTS_XOTUNEFAILED(0);
} else {
nhw_CLOCK_signal_EVENTS_XOTUNED(0);
}
}
static void nhw_pwrclk_timer_triggered(void) {
if (Timer_PWRCLK == nhw_clkpwr_st.Timer_PLL_128MXO) {
nhw_CLOCK_PLL_128MXO_Timer_triggered();
} else if (Timer_PWRCLK == nhw_clkpwr_st.Timer_PLL_128Mfree) {
nhw_CLOCK_PLL_128Mfree_Timer_triggered();
} else if (Timer_PWRCLK == nhw_clkpwr_st.Timer_LFCLK) {
nhw_CLOCK_LFCLK_triggered();
#if (NHW_CLKPWR_HAS_PLL_24M)
} else if (Timer_PWRCLK == nhw_clkpwr_st.Timer_PLL_24M) {
nhw_CLOCK_PLL_24MXO_Timer_triggered();
#endif
} else if (Timer_PWRCLK == nhw_clkpwr_st.Timer_CAL) {
nhw_CLOCK_CALtimer_triggered();
} else if (Timer_PWRCLK == nhw_clkpwr_st.Timer_XOTUNE) {
nhw_CLOCK_XOTUNEtimer_triggered();
} else { /* LCOV_EXCL_START */
bs_trace_error_time_line("%s programming error\n", __func__);
} /* LCOV_EXCL_STOP */
}
NSI_HW_EVENT(Timer_PWRCLK, nhw_pwrclk_timer_triggered, 50);
void nhw_clock_cheat_set_start_time(uint inst, int clock, int source, bs_time_t time) {
(void)inst;
for (int c = 0; c < NHW_CLKPWR_N_CLKS; c++) {
if ((clock == -1) || (c == clock)) {
for (int s = 0; s < NHW_CLKPWR_CLK_MAX_N_SRCS; s++) {
if ((source == -1) || (s == source)) {
nhw_clkpwr_st.CLOCK_start_times[c][s] = time;
}
}
}
}
}
void nhw_clock_cheat_set_xotune_time(uint inst, bs_time_t success_time, bs_time_t fail_time) {
(void)inst;
nhw_clkpwr_st.XOtuning_durations[0] = success_time;
nhw_clkpwr_st.XOtuning_durations[1] = fail_time;
}
void nhw_clock_cheat_set_xotune_fail(uint inst, uint fail_count) {
(void)inst;
nhw_clkpwr_st.XOtuning_pending_fails = fail_count;
}
void nhw_clock_cheat_trigger_xotune_error(uint inst)
{
(void)inst;
if (nhw_clkpwr_st.PLL_128MXO_state != Started) {
bs_trace_warning_line("TUNEERROR event can only be generated when running");
}
nhw_CLOCK_signal_EVENTS_XOTUNEERROR(0);
}
void nhw_clock_cheat_set_calibrate_time(uint inst, bs_time_t time) {
(void)inst;
nhw_clkpwr_st.CAL_duration = time;
}