Linux Audio

Check our new training course

Loading...
   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
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
/*
 * Copyright (c) 2016-2018 Nordic Semiconductor ASA
 * Copyright (c) 2016 Vinayak Kariappa Chettimada
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdbool.h>
#include <zephyr/types.h>
#include <soc.h>

#include "hal/cntr.h"
#include "hal/ticker.h"

#include "ticker.h"

#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER)
#define LOG_MODULE_NAME bt_ctlr_ticker
#include "common/log.h"
#include "hal/debug.h"

/*****************************************************************************
 * Defines
 ****************************************************************************/
#define DOUBLE_BUFFER_SIZE 2

/*****************************************************************************
 * Types
 ****************************************************************************/

struct ticker_node {
	u8_t  next;			 /* Next ticker node */

	u8_t  req;			 /* Request counter */
	u8_t  ack;			 /* Acknowledge counter. Imbalance
					  * between req and ack indicates
					  * ongoing operation
					  */
	u8_t  force;			 /* If non-zero, node timeout should
					  * be forced at next expiration
					  */
	u32_t ticks_periodic;		 /* If non-zero, interval
					  * between expirations
					  */
	u32_t ticks_to_expire;		 /* Ticks until expiration */
	ticker_timeout_func timeout_func;/* User timeout function */
	void  *context;			 /* Context delivered to timeout
					  * function
					  */
	u32_t ticks_to_expire_minus;	 /* Negative drift correction */
	u32_t ticks_slot;		 /* Air-time reservation for node */
	u16_t lazy_periodic;		 /* Number of timeouts to allow
					  * skipping
					  */
	u16_t lazy_current;		 /* Current number of timeouts
					  * skipped = slave latency
					  */
	u32_t remainder_periodic;	 /* Sub-microsecond tick remainder
					  * for each period
					  */
	u32_t remainder_current;	 /* Current sub-microsecond tick
					  * remainder
					  */
#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
#if  defined(CONFIG_BT_TICKER_EXT)
	struct ticker_ext *ext_data;	 /* Ticker extension data */
#endif /* CONFIG_BT_TICKER_EXT */
	u8_t  must_expire;		 /* Node must expire, even if it
					  * collides with other nodes
					  */
	s8_t  priority;			 /* Ticker node priority. 0 is default.
					  * Lower value is higher priority
					  */
#endif /* CONFIG_BT_TICKER_COMPATIBILITY_MODE */
};

/* Operations to be performed in ticker_job.
 * Possible values for field "op" in struct ticker_user_op
 */
#define TICKER_USER_OP_TYPE_NONE         0
#define TICKER_USER_OP_TYPE_IDLE_GET     1
#define TICKER_USER_OP_TYPE_SLOT_GET     2
#define TICKER_USER_OP_TYPE_PRIORITY_SET 3
#define TICKER_USER_OP_TYPE_START        4
#define TICKER_USER_OP_TYPE_UPDATE       5
#define TICKER_USER_OP_TYPE_STOP         6

/* Slot window re-schedule states */
#define TICKER_RESCHEDULE_STATE_NONE     0
#define TICKER_RESCHEDULE_STATE_PENDING  1
#define TICKER_RESCHEDULE_STATE_DONE     2

#if defined(CONFIG_BT_TICKER_EXT)
#define TICKER_RESCHEDULE_PENDING(_ticker) \
	(_ticker->ext_data && (_ticker->ext_data->reschedule_state == \
		TICKER_RESCHEDULE_STATE_PENDING))
#else
#define TICKER_RESCHEDULE_PENDING(_ticker) 0
#endif

/* User operation data structure for start opcode. Used for passing start
 * requests to ticker_job
 */
struct ticker_user_op_start {
	u32_t ticks_at_start;		/* Anchor ticks (absolute) */
	u32_t ticks_first;		/* Initial timeout ticks */
	u32_t ticks_periodic;		/* Ticker period ticks */
	u32_t remainder_periodic;	/* Sub-microsecond tick remainder */
	u16_t lazy;			/* Periodic latency in number of
					 * periods
					 */
	u32_t ticks_slot;		/* Air-time reservation ticks */
	ticker_timeout_func fp_timeout_func; /* Timeout callback function */
	void  *context;			/* Context passed in timeout callback */
#if defined(CONFIG_BT_TICKER_EXT)
	struct ticker_ext *ext_data;	/* Ticker extension data instance */
#endif /* CONFIG_BT_TICKER_EXT */
};

/* User operation data structure for update opcode. Used for passing update
 * requests to ticker_job
 */
struct ticker_user_op_update {
	u32_t ticks_drift_plus;		/* Requested positive drift in ticks */
	u32_t ticks_drift_minus;	/* Requested negatice drift in ticks */
	u32_t ticks_slot_plus;		/* Number of ticks to add to slot
					 * reservation (air-time)
					 */
	u32_t ticks_slot_minus;		/* Number of ticks to subtract from
					 * slot reservation (air-time)
					 */
	u16_t lazy;			/* Slave latency:
					 *  0: Do nothing
					 *  1: latency = 0
					 * >1: latency = lazy - 1
					 */
	u8_t  force;			/* Force update */
};

/* User operation data structure for slot_get opcode. Used for passing request
 * to get next ticker with slot ticks via ticker_job
 */
struct ticker_user_op_slot_get {
	u8_t  *ticker_id;
	u32_t *ticks_current;
	u32_t *ticks_to_expire;
};

/* User operation data structure for priority_set opcode. Used for passing
 * request to set ticker node priority via ticker_job
 */
struct ticker_user_op_priority_set {
	s8_t priority;		   /* Node priority. Defaults to 0 */
};

/* User operation top level data structure. Used for passing requests to
 * ticker_job
 */
struct ticker_user_op {
	u8_t op;		   /* User operation */
	u8_t id;		   /* Ticker node id */
	union {
		struct ticker_user_op_start start;
		struct ticker_user_op_update update;
		struct ticker_user_op_slot_get slot_get;
		struct ticker_user_op_priority_set priority_set;
	} params;		   /* User operation parameters */
	u32_t status;		   /* Operation result */
	ticker_op_func fp_op_func; /* Operation completion callback */
	void  *op_context;	   /* Context passed in completion callback */
};

/* User data structure for operations
 */
struct ticker_user {
	u8_t count_user_op;	   /* Number of user operation slots */
	u8_t first;		   /* Slot index of first user operation */
	u8_t middle;		   /* Slot index of last managed user op.
				    * Updated by ticker_job_list_manage
				    * for use in ticker_job_list_insert
				    */
	u8_t last;		   /* Slot index of last user operation */
	struct ticker_user_op *user_op; /* Pointer to user operation array */
};

/* Ticker instance
 */
struct ticker_instance {
	struct ticker_node *nodes; /* Pointer to ticker nodes */
	struct ticker_user *users; /* Pointer to user nodes */
	u8_t  count_node;	   /* Number of ticker nodes */
	u8_t  count_user;	   /* Number of user nodes */
	u8_t  ticks_elapsed_first; /* Index from which elapsed ticks count is
				    * pulled
				    */
	u8_t  ticks_elapsed_last;  /* Index to which elapsed ticks count is
				    * pushed
				    */
	u32_t ticks_elapsed[DOUBLE_BUFFER_SIZE]; /* Buffer for elapsed ticks */
	u32_t ticks_current;	   /* Absolute ticks elapsed at last
				    * ticker_job
				    */
	u32_t ticks_slot_previous; /* Number of ticks previously reserved by a
				    * ticker node (active air-time)
				    */
	u8_t  ticker_id_slot_previous; /* Id of previous slot reserving ticker
					* node
					*/
	u8_t  ticker_id_head;      /* Index of first ticker node (next to
				    * expire)
				    */
	u8_t  job_guard;	   /* Flag preventing ticker_worker from
				    * running if ticker_job is active
				    */
	u8_t  worker_trigger;	   /* Flag preventing ticker_job from starting
				    * if ticker_worker was requested, and to
				    * trigger ticker_worker at end of job, if
				    * requested
				    */

	ticker_caller_id_get_cb_t caller_id_get_cb; /* Function for retrieving
						     * the caller id from user
						     * id
						     */
	ticker_sched_cb_t         sched_cb;	    /* Function for scheduling
						     * ticker_worker and
						     * ticker_job
						     */
	ticker_trigger_set_cb_t   trigger_set_cb;   /* Function for setting
						     * the trigger (compare
						     * value)
						     */
};

BUILD_ASSERT(sizeof(struct ticker_node)    == TICKER_NODE_T_SIZE);
BUILD_ASSERT(sizeof(struct ticker_user)    == TICKER_USER_T_SIZE);
BUILD_ASSERT(sizeof(struct ticker_user_op) == TICKER_USER_OP_T_SIZE);

/*****************************************************************************
 * Global instances
 ****************************************************************************/
#define TICKER_INSTANCE_MAX 1
static struct ticker_instance _instance[TICKER_INSTANCE_MAX];

/*****************************************************************************
 * Static Functions
 ****************************************************************************/

/**
 * @brief Update elapsed index
 *
 * @param ticks_elapsed_index Pointer to current index
 *
 * @internal
 */
static inline void ticker_next_elapsed(u8_t *ticks_elapsed_index)
{
	u8_t idx = *ticks_elapsed_index + 1;

	if (idx == DOUBLE_BUFFER_SIZE) {
		idx = 0U;
	}
	*ticks_elapsed_index = idx;
}

#if defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
/**
 * @brief Get ticker expiring in a specific slot
 *
 * @details Searches for a ticker which expires in a specific slot starting
 * at 'ticks_slot'.
 *
 * @param node           Pointer to ticker node array
 * @param ticker_id_head Id of initial ticker node
 * @param ticks_slot     Ticks indicating slot to get
 *
 * @return Id of ticker expiring within slot or TICKER_NULL
 * @internal
 */
static u8_t ticker_by_slot_get(struct ticker_node *node, u8_t ticker_id_head,
			       u32_t ticks_slot)
{
	while (ticker_id_head != TICKER_NULL) {
		struct ticker_node *ticker;
		u32_t ticks_to_expire;

		ticker = &node[ticker_id_head];
		ticks_to_expire = ticker->ticks_to_expire;

		if (ticks_slot <= ticks_to_expire) {
			/* Next ticker expiration is outside the checked slot */
			return TICKER_NULL;
		}

		if (ticker->ticks_slot) {
			/* This ticker node has slot defined and expires within
			 * checked slot
			 */
			break;
		}

		ticks_slot -= ticks_to_expire;
		ticker_id_head = ticker->next;
	}

	return ticker_id_head;
}
#endif /* CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Get next ticker with slot ticks
 *
 * @details Gets the next ticker which has slot ticks specified and
 * return the ticker id and accumulated ticks until expiration. If no
 * ticker nodes have slot ticks, the next ticker node is returned.
 * If no head id is provided (TICKER_NULL) the first node is returned.
 *
 * @param instance          Pointer to ticker instance
 * @param ticker_id_head    Pointer to id of first ticker node [in/out]
 * @param ticks_current     Pointer to current ticks count [in/out]
 * @param ticks_to_expire   Pointer to ticks to expire [in/out]
 *
 * @internal
 */
static void ticker_by_next_slot_get(struct ticker_instance *instance,
				    u8_t *ticker_id_head, u32_t *ticks_current,
				    u32_t *ticks_to_expire)
{
	struct ticker_node *ticker;
	struct ticker_node *node;
	u32_t _ticks_to_expire;
	u8_t _ticker_id_head;

	node = instance->nodes;

	_ticker_id_head = *ticker_id_head;
	_ticks_to_expire = *ticks_to_expire;
	if ((_ticker_id_head == TICKER_NULL) ||
	    (*ticks_current != instance->ticks_current)) {
		/* Initialize with instance head */
		_ticker_id_head = instance->ticker_id_head;
		*ticks_current = instance->ticks_current;
		_ticks_to_expire = 0U;
	} else {
		/* Get ticker id for next node */
		ticker = &node[_ticker_id_head];
		_ticker_id_head = ticker->next;
	}

	/* Find first ticker node with slot ticks */
	while ((_ticker_id_head != TICKER_NULL) &&
	       ((ticker = &node[_ticker_id_head])->ticks_slot == 0U)) {
		/* Accumulate expire ticks */
		_ticks_to_expire += ticker->ticks_to_expire;
		_ticker_id_head = ticker->next;
	}

	if (_ticker_id_head != TICKER_NULL) {
		/* Add ticks for found ticker */
		_ticks_to_expire += ticker->ticks_to_expire;
	}

	*ticker_id_head = _ticker_id_head;
	*ticks_to_expire = _ticks_to_expire;
}

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
/**
 * @brief Enqueue ticker node
 *
 * @details Finds insertion point for new ticker node and inserts the
 * node in the linked node list.
 *
 * @param instance Pointer to ticker instance
 * @param id       Ticker node id to enqueue
 *
 * @return Id of enqueued ticker node
 * @internal
 */
static u8_t ticker_enqueue(struct ticker_instance *instance, u8_t id)
{
	struct ticker_node *ticker_current;
	struct ticker_node *ticker_new;
	u32_t ticks_to_expire_current;
	struct ticker_node *node;
	u32_t ticks_to_expire;
	u8_t previous;
	u8_t current;

	node = &instance->nodes[0];
	ticker_new = &node[id];
	ticks_to_expire = ticker_new->ticks_to_expire;
	current = instance->ticker_id_head;

	/* Find insertion point for new ticker node and adjust ticks_to_expire
	 * relative to insertion point
	 */
	previous = TICKER_NULL;

	while ((current != TICKER_NULL) && (ticks_to_expire >=
		(ticks_to_expire_current =
		(ticker_current = &node[current])->ticks_to_expire))) {

		ticks_to_expire -= ticks_to_expire_current;

		/* Check for timeout in same tick - prioritize according to
		 * latency
		 */
		if (ticks_to_expire == 0 && (ticker_new->lazy_current >
					     ticker_current->lazy_current)) {
			ticks_to_expire = ticker_current->ticks_to_expire;
			break;
		}

		previous = current;
		current = ticker_current->next;
	}

	/* Link in new ticker node and adjust ticks_to_expire to relative value
	 */
	ticker_new->ticks_to_expire = ticks_to_expire;
	ticker_new->next = current;

	if (previous == TICKER_NULL) {
		instance->ticker_id_head = id;
	} else {
		node[previous].next = id;
	}

	if (current != TICKER_NULL) {
		node[current].ticks_to_expire -= ticks_to_expire;
	}

	return id;
}
#else /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Enqueue ticker node
 *
 * @details Finds insertion point for new ticker node and inserts the
 * node in the linked node list. However, if the new ticker node collides
 * with an existing node or the expiration is inside the previous slot,
 * the node is not inserted.
 *
 * @param instance Pointer to ticker instance
 * @param id       Ticker node id to enqueue
 *
 * @return Id of enqueued ticker node, or id of previous- or colliding
 * ticker node if new node was not enqueued
 * @internal
 */
static u8_t ticker_enqueue(struct ticker_instance *instance, u8_t id)
{
	struct ticker_node *ticker_current;
	struct ticker_node *ticker_new;
	u32_t ticks_to_expire_current;
	u8_t ticker_id_slot_previous;
	u32_t ticks_slot_previous;
	struct ticker_node *node;
	u32_t ticks_to_expire;
	u8_t previous;
	u8_t current;
	u8_t collide;

	node = &instance->nodes[0];
	ticker_new = &node[id];
	ticks_to_expire = ticker_new->ticks_to_expire;

	collide = ticker_id_slot_previous = TICKER_NULL;
	current = instance->ticker_id_head;
	previous = current;
	ticks_slot_previous = instance->ticks_slot_previous;

	/* Find insertion point for new ticker node and adjust ticks_to_expire
	 * relative to insertion point
	 */
	while ((current != TICKER_NULL) &&
	       (ticks_to_expire >
		(ticks_to_expire_current =
		 (ticker_current = &node[current])->ticks_to_expire))) {
		ticks_to_expire -= ticks_to_expire_current;

		if (ticker_current->ticks_slot != 0U) {
			ticks_slot_previous = ticker_current->ticks_slot;
			ticker_id_slot_previous = current;
		} else {
			if (ticks_slot_previous > ticks_to_expire_current) {
				ticks_slot_previous -= ticks_to_expire_current;
			} else {
				ticks_slot_previous = 0U;
			}
		}
		previous = current;
		current = ticker_current->next;
	}

	/* Check for collision for new ticker node at insertion point */
	collide = ticker_by_slot_get(&node[0], current,
				     ticks_to_expire + ticker_new->ticks_slot);

	if ((ticker_new->ticks_slot == 0U) ||
	    ((ticks_slot_previous <= ticks_to_expire) &&
	     (collide == TICKER_NULL))) {
		/* New ticker node has no slot ticks or there is no collision -
		 * link it in and adjust ticks_to_expire to relative value
		 */
		ticker_new->ticks_to_expire = ticks_to_expire;
		ticker_new->next = current;

		if (previous == current) {
			instance->ticker_id_head = id;
		} else {
			node[previous].next = id;
		}

		if (current != TICKER_NULL) {
			node[current].ticks_to_expire -= ticks_to_expire;
		}
	} else {
		/* Collision - no ticker node insertion, set id to that of
		 * colliding node
		 */
		if (ticks_slot_previous > ticks_to_expire) {
			id = ticker_id_slot_previous;
		} else {
			id = collide;
		}
	}

	return id;
}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Dequeue ticker node
 *
 * @details Finds extraction point for ticker node to be dequeued, unlinks
 * the node and adjusts the links and ticks_to_expire. Returns the ticks
 * until expiration for dequeued ticker node.
 *
 * @param instance Pointer to ticker instance
 * @param id       Ticker node id to dequeue
 *
 * @return Total ticks until expiration for dequeued ticker node, or 0 if
 * node was not found
 * @internal
 */
static u32_t ticker_dequeue(struct ticker_instance *instance, u8_t id)
{
	struct ticker_node *ticker_current;
	struct ticker_node *node;
	u8_t previous;
	u32_t timeout;
	u8_t current;
	u32_t total;

	/* Find the ticker's position in ticker node list while accumulating
	 * ticks_to_expire
	 */
	node = &instance->nodes[0];
	previous = instance->ticker_id_head;
	current = previous;
	total = 0U;
	ticker_current = 0;
	while (current != TICKER_NULL) {
		ticker_current = &node[current];

		if (current == id) {
			break;
		}

		total += ticker_current->ticks_to_expire;
		previous = current;
		current = ticker_current->next;
	}

	if (current == TICKER_NULL) {
		/* Ticker not in active list */
		return 0;
	}

	if (previous == current) {
		/* Ticker is the first in the list */
		instance->ticker_id_head = ticker_current->next;
	}

	/* Remaining timeout between next timeout */
	timeout = ticker_current->ticks_to_expire;

	/* Link previous ticker with next of this ticker
	 * i.e. removing the ticker from list
	 */
	node[previous].next = ticker_current->next;

	/* If this is not the last ticker, increment the
	 * next ticker by this ticker timeout
	 */
	if (ticker_current->next != TICKER_NULL) {
		node[ticker_current->next].ticks_to_expire += timeout;
	}

	return (total + timeout);
}

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
/**
 * @brief Resolve ticker node collision
 *
 * @details Evaluates the provided ticker node against other queued nodes
 * and returns non-zero if the ticker node collides and should be skipped.
 * The following rules are checked:
 *   1) If the periodic latency is not yet exhausted, node is skipped
 *   2) If the node has highest possible priority, node is never skipped
 *   2) If the node will starve next node due to slot reservation
 *      overlap, node is skipped if:
 *      a) Next node has higher priority than current node
 *      b) Next node has more accumulated latency than the current node
 *      c) Next node is 'older' than current node and has same priority
 *      d) Next node has force flag set, and the current does not
 *
 * @param nodes         Pointer to ticker node array
 * @param ticker        Pointer to ticker to resolve
 *
 * @return 0 if no collision was detected. 1 if ticker node collides
 * with other ticker node of higher composite priority
 * @internal
 */
static u8_t ticker_resolve_collision(struct ticker_node *nodes,
				     struct ticker_node *ticker)
{
	if ((ticker->priority != TICKER_PRIORITY_CRITICAL) &&
	    (ticker->next != TICKER_NULL)) {
		s32_t lazy_current = ticker->lazy_current;

		/* Check if this ticker node will starve next node which has
		 * latency or higher priority
		 */
		if (lazy_current >= ticker->lazy_periodic) {
			lazy_current -= ticker->lazy_periodic;
		}
		u8_t  id_head = ticker->next;
		u32_t acc_ticks_to_expire = 0U;

		/* Age is time since last expiry */
		u32_t current_age = ticker->ticks_periodic +
				    (lazy_current * ticker->ticks_periodic);

		while (id_head != TICKER_NULL) {
			struct ticker_node *ticker_next = &nodes[id_head];

			/* Accumulate ticks_to_expire for each node */
			acc_ticks_to_expire += ticker_next->ticks_to_expire;
			if (acc_ticks_to_expire > ticker->ticks_slot) {
				break;
			}

			/* We only care about nodes with slot reservation */
			if (ticker_next->ticks_slot == 0U) {
				id_head = ticker_next->next;
				continue;
			}

			s32_t lazy_next = ticker_next->lazy_current;
			u8_t  lazy_next_periodic_skip =
				ticker_next->lazy_periodic > lazy_next;

			if (!lazy_next_periodic_skip) {
				lazy_next -= ticker_next->lazy_periodic;
			}

			/* Is the current and next node equal in priority? */
			u8_t equal_priority = ticker->priority ==
				ticker_next->priority;

			/* Age is time since last expiry */
			u32_t next_age = (ticker_next->ticks_periodic == 0U ?
					  0U :
					 (ticker_next->ticks_periodic -
					  ticker_next->ticks_to_expire)) +
					 (lazy_next *
					  ticker_next->ticks_periodic);

			/* Was the current node scheduled earlier? */
			u8_t current_is_older = current_age > next_age;
			/* Was next node scheduled earlier (legacy priority)? */
			u8_t next_is_older = next_age > current_age;

			/* Is force requested for next node (e.g. update) -
			 * more so than for current node?
			 */
			u8_t next_force = (ticker_next->force > ticker->force);

			/* Does next node have critical priority and should
			 * always be scheduled?
			 */
			u8_t next_is_critical = ticker_next->priority ==
				TICKER_PRIORITY_CRITICAL;

			/* Does next node have higher priority? */
			u8_t next_has_priority =
				(lazy_next - ticker_next->priority) >
				(lazy_current - ticker->priority);

			/* Check if next node is within this reservation slot
			 * and wins conflict resolution
			 */
			if (!lazy_next_periodic_skip &&
			    (next_force ||
			     next_is_critical ||
			    (next_has_priority && !current_is_older) ||
			    (equal_priority && next_is_older))) {
				/* This node must be skipped - check window */
				return 1U;
			}
			id_head = ticker_next->next;
		}
	}

	return 0U;
}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Ticker worker
 *
 * @details Runs as upper half of ticker operation, triggered by a compare
 * match from the underlaying counter HAL, via the ticker_trigger function.
 * Traverses ticker nodes to find tickers expired since last job
 * execution. Expired (requested) ticker nodes have their timeout callback
 * functions called. Finally, a ticker job is enqueued. Invoked from the
 * ticker worker mayfly context (TICKER_MAYFLY_CALL_ID_WORKER)
 *
 * @param param Pointer to ticker instance
 *
 */
void ticker_worker(void *param)
{
	struct ticker_instance *instance = param;
	struct ticker_node *node;
	u32_t ticks_elapsed;
	u32_t ticks_expired;
	u8_t ticker_id_head;

	/* Defer worker if job running */
	instance->worker_trigger = 1U;
	if (instance->job_guard) {
		return;
	}

	/* If no tickers queued (active), do nothing */
	if (instance->ticker_id_head == TICKER_NULL) {
		instance->worker_trigger = 0U;
		return;
	}

	/* Get ticks elapsed since last job execution */
	ticks_elapsed = ticker_ticks_diff_get(cntr_cnt_get(),
					      instance->ticks_current);

	/* Initialize actual elapsed ticks being consumed */
	ticks_expired = 0U;

	/* Auto variable containing the head of tickers expiring */
	ticker_id_head = instance->ticker_id_head;

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
	/* Check if the previous ticker node which had air-time, is still
	 * active and has this time slot reserved
	 */
	u8_t slot_reserved = 0;

	if (instance->ticker_id_slot_previous != TICKER_NULL) {
		if (instance->ticks_slot_previous > ticks_elapsed) {
			/* This node intersects reserved slot */
			slot_reserved = 1;
		}
	}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

	/* Expire all tickers within ticks_elapsed and collect ticks_expired */
	node = &instance->nodes[0];

	while (ticker_id_head != TICKER_NULL) {
		struct ticker_node *ticker;
		u32_t ticks_to_expire;
		u8_t must_expire_skip;

		ticker = &node[ticker_id_head];

		/* Stop if ticker did not expire */
		ticks_to_expire = ticker->ticks_to_expire;
		if (ticks_elapsed < ticks_to_expire) {
			break;
		}

		/* Decrement ticks_elapsed and collect expired ticks */
		ticks_elapsed -= ticks_to_expire;
		ticks_expired += ticks_to_expire;

		/* Move to next ticker node */
		ticker_id_head = ticker->next;
		must_expire_skip = 0U;

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
		/* Check if node has slot reservation and resolve any collision
		 * with other ticker nodes
		 */
		if (ticker->ticks_slot != 0U &&
		   (slot_reserved || ticker_resolve_collision(node, ticker))) {
#if defined(CONFIG_BT_TICKER_EXT)
			struct ticker_ext *ext_data = ticker->ext_data;

			if (ext_data &&
			    ext_data->ticks_slot_window != 0U &&
			    ext_data->reschedule_state ==
			    TICKER_RESCHEDULE_STATE_NONE &&
			   (ticker->lazy_periodic <= ticker->lazy_current)) {
				/* Mark node for re-scheduling in ticker_job */
				ext_data->reschedule_state =
					TICKER_RESCHEDULE_STATE_PENDING;
			} else if (ext_data) {
				/* Mark node as not re-scheduling */
				ext_data->reschedule_state =
					TICKER_RESCHEDULE_STATE_NONE;
			}
#endif /* CONFIG_BT_TICKER_EXT */
			ticker->lazy_current++;

			if ((ticker->must_expire == 0U) ||
			    (ticker->lazy_periodic >= ticker->lazy_current) ||
			    TICKER_RESCHEDULE_PENDING(ticker)) {
				/* Not a must-expire case, this is programmed
				 * latency or pending re-schedule. Skip this
				 * ticker node.
				 */
				continue;
			}
			/* Continue but perform shallow expiry */
			must_expire_skip = 1U;
		}

#if defined(CONFIG_BT_TICKER_EXT)
		if (ticker->ext_data) {
			ticker->ext_data->ticks_drift = 0U;
			/* Mark node as not re-scheduling */
			ticker->ext_data->reschedule_state =
				TICKER_RESCHEDULE_STATE_NONE;
		}
#endif /* CONFIG_BT_TICKER_EXT */
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */
		/* Skip if not scheduled to execute */
		if (((ticker->req - ticker->ack) & 0xff) != 1U) {
			continue;
		}

		/* Scheduled timeout is acknowledged to be complete */
		ticker->ack--;

		if (ticker->timeout_func) {
			u32_t ticks_at_expire;

			ticks_at_expire = (instance->ticks_current +
					   ticks_expired -
					   ticker->ticks_to_expire_minus) &
					   HAL_TICKER_CNTR_MASK;

			DEBUG_TICKER_TASK(1);
			/* Invoke the timeout callback */
			ticker->timeout_func(ticks_at_expire,
					     ticker->remainder_current,
					     must_expire_skip ?
					     TICKER_LAZY_MUST_EXPIRE :
					     ticker->lazy_current,
					     ticker->context);
			DEBUG_TICKER_TASK(0);

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
			if (must_expire_skip == 0U) {
				/* Reset latency to periodic offset */
				ticker->lazy_current = 0U;
				ticker->force = 0U;
				if (ticker->ticks_slot != 0U) {
					/* Any further nodes will be skipped */
					slot_reserved = 1U;
				}
			}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */
		}
	}

	/* Queue the elapsed ticks */
	if (instance->ticks_elapsed_first == instance->ticks_elapsed_last) {
		ticker_next_elapsed(&instance->ticks_elapsed_last);
	}
	instance->ticks_elapsed[instance->ticks_elapsed_last] = ticks_expired;

	instance->worker_trigger = 0U;

	/* Enqueue the ticker job with chain=1 (do not inline) */
	instance->sched_cb(TICKER_CALL_ID_WORKER, TICKER_CALL_ID_JOB, 1,
			   instance);
}

/**
 * @brief Prepare ticker node expiration
 *
 * @details Calculates the number of ticks until next expiration, taking
 * into consideration any negative drift correction.
 *
 * @param ticker         Pointer to ticker node
 * @param ticks_current  Current number of ticks (elapsed)
 * @param ticks_at_start Number of ticks at start (anchor)
 *
 * @internal
 */
static void ticks_to_expire_prep(struct ticker_node *ticker,
				 u32_t ticks_current, u32_t ticks_at_start)
{
	u32_t ticks_to_expire = ticker->ticks_to_expire;
	u32_t ticks_to_expire_minus = ticker->ticks_to_expire_minus;

	/* Calculate ticks to expire for this new node */
	if (!((ticks_at_start - ticks_current) & BIT(HAL_TICKER_CNTR_MSBIT))) {
		/* Most significant bit is 0 so ticks_at_start lies ahead of
		 * ticks_current: ticks_at_start >= ticks_current
		 */
		ticks_to_expire += ticker_ticks_diff_get(ticks_at_start,
							 ticks_current);
	} else {
		/* ticks_current > ticks_at_start
		 */
		u32_t delta_current_start;

		delta_current_start = ticker_ticks_diff_get(ticks_current,
							    ticks_at_start);
		if (ticks_to_expire > delta_current_start) {
			/* There's still time until expiration - subtract
			 * elapsed time
			 */
			ticks_to_expire -= delta_current_start;
		} else {
			/* Ticker node should have expired (we're late).
			 * Add 'lateness' to negative drift correction
			 * (ticks_to_expire_minus) and set ticks_to_expire
			 * to 0
			 */
			ticks_to_expire_minus +=
			    (delta_current_start - ticks_to_expire);
			ticks_to_expire = 0U;
		}
	}

	/* Handle negative drift correction */
	if (ticks_to_expire > ticks_to_expire_minus) {
		ticks_to_expire -= ticks_to_expire_minus;
		ticks_to_expire_minus = 0U;
	} else {
		ticks_to_expire_minus -= ticks_to_expire;
		ticks_to_expire = 0U;
	}

	/* Update ticker */
	ticker->ticks_to_expire = ticks_to_expire;
	ticker->ticks_to_expire_minus = ticks_to_expire_minus;
}

/**
 * @brief Increment remainder
 *
 * @details Calculates whether the remainder should increments expiration time
 * for above-microsecond precision counter HW. The remainder enables improved
 * ticker precision, but is disabled for for sub-microsecond precision
 * configurations.
 *
 * @param ticker Pointer to ticker node
 *
 * @return Returns 1 to indicate increment is due, otherwise 0
 * @internal
 */
static u8_t ticker_remainder_inc(struct ticker_node *ticker)
{
#ifdef HAL_TICKER_REMAINDER_RANGE
	ticker->remainder_current += ticker->remainder_periodic;
	if ((ticker->remainder_current < BIT(31)) &&
	    (ticker->remainder_current > (HAL_TICKER_REMAINDER_RANGE >> 1))) {
		ticker->remainder_current -= HAL_TICKER_REMAINDER_RANGE;
		return 1;
	}
	return 0;
#else
	return 0;
#endif
}

/**
 * @brief Decrement remainder
 *
 * @details Calculates whether the remainder should decrements expiration time
 * for above-microsecond precision counter HW. The remainder enables improved
 * ticker precision, but is disabled for for sub-microsecond precision
 * configurations.
 *
 * @param ticker Pointer to ticker node
 *
 * @return Returns 1 to indicate decrement is due, otherwise 0
 * @internal
 */
static u8_t ticker_remainder_dec(struct ticker_node *ticker)
{
#ifdef HAL_TICKER_REMAINDER_RANGE
	u8_t decrement = 0U;

	if ((ticker->remainder_current >= BIT(31)) ||
	    (ticker->remainder_current <= (HAL_TICKER_REMAINDER_RANGE >> 1))) {
		decrement++;
		ticker->remainder_current += HAL_TICKER_REMAINDER_RANGE;
	}
	ticker->remainder_current -= ticker->remainder_periodic;
	return decrement;
#else
	return 0;
#endif
}

/**
 * @brief Invoke user operation callback
 *
 * @param user_op Pointer to user operation struct
 * @param status  User operation status to pass to callback
 *
 * @internal
 */
static void ticker_job_op_cb(struct ticker_user_op *user_op, u32_t status)
{
	user_op->op = TICKER_USER_OP_TYPE_NONE;
	user_op->status = status;
	if (user_op->fp_op_func) {
		user_op->fp_op_func(user_op->status, user_op->op_context);
	}
}

/**
 * @brief Update and insert ticker node
 *
 * @details Update ticker node with parameters passed in user operation.
 * After update, the ticker is inserted in front as new head.
 *
 * @param ticker	Pointer to ticker node
 * @param user_op	Pointer to user operation
 * @param ticks_current	Current ticker instance ticks
 * @param ticks_elapsed	Expired ticks at time of call
 * @param insert_head	Pointer to current head (id). Contains id
 *			from user operation upon exit
 * @internal
 */
static inline void ticker_job_node_update(struct ticker_node *ticker,
					  struct ticker_user_op *user_op,
					  u32_t ticks_current,
					  u32_t ticks_elapsed,
					  u8_t *insert_head)
{
	u32_t ticks_to_expire = ticker->ticks_to_expire;
	u32_t ticks_now;

	ticks_now = cntr_cnt_get();
	ticks_elapsed += ticker_ticks_diff_get(ticks_now, ticks_current);
	if (ticks_to_expire > ticks_elapsed) {
		ticks_to_expire -= ticks_elapsed;
	} else {
		ticker->ticks_to_expire_minus += ticks_elapsed -
						 ticks_to_expire;
		ticks_to_expire = 0U;
	}

	/* Update ticks_to_expire from latency (lazy) input */
	if ((ticker->ticks_periodic != 0U) &&
	    (user_op->params.update.lazy != 0U)) {
		user_op->params.update.lazy--;
		while ((ticks_to_expire > ticker->ticks_periodic) &&
		       (ticker->lazy_current > user_op->params.update.lazy)) {
			ticks_to_expire -= ticker->ticks_periodic +
					   ticker_remainder_dec(ticker);
			ticker->lazy_current--;
		}

		while (ticker->lazy_current < user_op->params.update.lazy) {
			ticks_to_expire += ticker->ticks_periodic +
					   ticker_remainder_inc(ticker);
			ticker->lazy_current++;
		}
		ticker->lazy_periodic = user_op->params.update.lazy;
	}

	/* Update ticks_to_expire from drift input */
	ticker->ticks_to_expire = ticks_to_expire +
				  user_op->params.update.ticks_drift_plus;
	ticker->ticks_to_expire_minus +=
				user_op->params.update.ticks_drift_minus;

#if defined(CONFIG_BT_TICKER_EXT)
	/* TODO: An improvement on this could be to only consider the drift
	 * (ADV => randomization) if re-sceduling fails. We would still store
	 * the drift ticks here, but not actually update the node. That would
	 * allow the ticker to use the full window for re-scheduling.
	 */
	struct ticker_ext *ext_data = ticker->ext_data;

	if (ext_data && ext_data->ticks_slot_window != 0U) {
		ext_data->ticks_drift =
			user_op->params.update.ticks_drift_plus -
			user_op->params.update.ticks_drift_minus;
	}
#endif /* CONFIG_BT_TICKER_EXT */

	ticks_to_expire_prep(ticker, ticks_current, ticks_now);

	/* Update ticks_slot parameter from plus/minus input */
	ticker->ticks_slot += user_op->params.update.ticks_slot_plus;
	if (ticker->ticks_slot > user_op->params.update.ticks_slot_minus) {
		ticker->ticks_slot -= user_op->params.update.ticks_slot_minus;
	} else {
		ticker->ticks_slot = 0U;
	}

	/* Update force parameter */
	if (user_op->params.update.force != 0U) {
		ticker->force = user_op->params.update.force;
	}

	ticker->next = *insert_head;
	*insert_head = user_op->id;
}

/**
 * @brief Manage user update operation
 *
 * @details Called by ticker_job to execute an update request, or set node
 * as done if request is not update. Invokes user operation callback before
 * exit.
 *
 * @param instance	Pointer to ticker instance
 * @param ticker	Pointer to ticker node
 * @param user_op	Pointer to user operation
 * @param ticks_elapsed Expired ticks at time of call
 * @param insert_head	Pointer to current head (id). For update operation,
 *			contains operation id upon exit
 * @internal
 */
static inline void ticker_job_node_manage(struct ticker_instance *instance,
					  struct ticker_node *ticker,
					  struct ticker_user_op *user_op,
					  u32_t ticks_elapsed,
					  u8_t *insert_head)
{
	/* Remove ticker node from list */
	ticker->ticks_to_expire = ticker_dequeue(instance, user_op->id);

	/* Handle update of ticker by re-inserting it back. */
	if (user_op->op == TICKER_USER_OP_TYPE_UPDATE) {
		ticker_job_node_update(ticker, user_op, instance->ticks_current,
				       ticks_elapsed, insert_head);

		/* Set schedule status of node
		 * as updating.
		 */
		ticker->req++;
	} else {
		/* Reset schedule status of node */
		ticker->req = ticker->ack;

		if (instance->ticker_id_slot_previous == user_op->id) {
			u32_t ticks_now = cntr_cnt_get();
			u32_t ticks_used;

			instance->ticker_id_slot_previous = TICKER_NULL;
			ticks_used = ticks_elapsed +
				ticker_ticks_diff_get(ticks_now,
						      instance->ticks_current);
			instance->ticks_slot_previous =	MIN(ticker->ticks_slot,
							    ticks_used);
		}
	}

	/* op success, @todo update may fail during
	 * actual insert! need to design that yet.
	 */
	ticker_job_op_cb(user_op, TICKER_STATUS_SUCCESS);
}

/**
 * @brief Manage user operations list
 *
 * @details Called by ticker_job to execute requested user operations. A
 * number of operation may be queued since last ticker_job. Only update and
 * stop operations are handled. Start is handled implicitly by inserting
 * the ticker node in ticker_job_list_insert.
 *
 * @param instance	Pointer to ticker instance
 * @param ticks_elapsed Expired ticks at time of call
 * @param insert_head	Pointer to current head (id). For update operation,
 *			contains operation id upon exit
 * @return Returns 1 if operations is pending, 0 if all operations are done.
 * @internal
 */
static inline u8_t ticker_job_list_manage(struct ticker_instance *instance,
					  u32_t ticks_elapsed,
					  u8_t *insert_head)
{
	u8_t pending;
	struct ticker_node *node;
	struct ticker_user *users;
	u8_t count_user;

	pending = 0U;
	node = &instance->nodes[0];
	users = &instance->users[0];
	count_user = instance->count_user;
	/* Traverse users - highest id first */
	while (count_user--) {
		struct ticker_user *user;
		struct ticker_user_op *user_ops;

		user = &users[count_user];
		user_ops = &user->user_op[0];
		/* Traverse user operation queue - middle to last (with wrap).
		 * This operation updates user->middle to be the past the last
		 * processed user operation. This is used later by
		 * ticker_job_list_insert, for handling user->first to middle.
		 */
		while (user->middle != user->last) {
			struct ticker_user_op *user_op;
			struct ticker_node *ticker;
			u8_t state;
			u8_t prev;
			u8_t middle;

			user_op = &user_ops[user->middle];

			/* Increment index and handle wrapping */
			prev = user->middle;
			middle = user->middle + 1;
			if (middle == user->count_user_op) {
				middle = 0U;
			}
			user->middle = middle;

			ticker = &node[user_op->id];

			/* if op is start, then skip update and stop ops */
			if (user_op->op < TICKER_USER_OP_TYPE_UPDATE) {
				continue;
			}

			/* determine the ticker state */
			state = (ticker->req - ticker->ack) & 0xff;

			/* if not started or update not required,
			 * set status and continue.
			 */
			if ((user_op->op > TICKER_USER_OP_TYPE_STOP) ||
			    (state == 0U) ||
			    ((user_op->op == TICKER_USER_OP_TYPE_UPDATE) &&
			     (user_op->params.update.ticks_drift_plus == 0U) &&
			     (user_op->params.update.ticks_drift_minus == 0U) &&
			     (user_op->params.update.ticks_slot_plus == 0U) &&
			     (user_op->params.update.ticks_slot_minus == 0U) &&
			     (user_op->params.update.lazy == 0U) &&
			     (user_op->params.update.force == 0U))) {
				ticker_job_op_cb(user_op,
						 TICKER_STATUS_FAILURE);
				continue;
			}

			/* Delete node, if not expired */
			if (state == 1U) {
				ticker_job_node_manage(instance, ticker,
						       user_op, ticks_elapsed,
						       insert_head);
			} else {
				/* Update on expired node requested, defering
				 * update until bottom half finishes.
				 */
				/* sched job to run after worker bottom half.
				 */
				instance->sched_cb(TICKER_CALL_ID_JOB,
						   TICKER_CALL_ID_JOB, 1,
						   instance);

				/* Update the index upto which management is
				 * complete.
				 */
				user->middle = prev;

				pending = 1U;
				break;
			}
		}
	}

	return pending;
}

/**
 * @brief Handle ticker node expirations
 *
 * @details Called by ticker_job to schedule next expirations. Expired ticker
 * nodes are removed from the active list, and re-inserted if periodic.
 *
 * @param instance	 Pointer to ticker instance
 * @param ticks_previous Absolute ticks at ticker_job start
 * @param ticks_elapsed  Expired ticks at time of call
 * @param insert_head	 Pointer to current head (id). Updated if nodes are
 *			 re-inserted
 * @internal
 */
static inline void ticker_job_worker_bh(struct ticker_instance *instance,
					u32_t ticks_previous,
					u32_t ticks_elapsed,
					u8_t *insert_head)
{
	struct ticker_node *node;
	u32_t ticks_expired;

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
	u32_t ticks_latency;
	u32_t ticks_now;

	ticks_now = cntr_cnt_get();
	ticks_latency = ticker_ticks_diff_get(ticks_now, ticks_previous);
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

	node = &instance->nodes[0];
	ticks_expired = 0U;
	while (instance->ticker_id_head != TICKER_NULL) {
		u8_t is_must_expire_skip = 0U;
		struct ticker_node *ticker;
		u32_t ticks_to_expire;
		u8_t id_expired;

		/* auto variable for current ticker node */
		id_expired = instance->ticker_id_head;
		ticker = &node[id_expired];

		/* Do nothing if ticker did not expire */
		ticks_to_expire = ticker->ticks_to_expire;
		if (ticks_elapsed < ticks_to_expire) {
			ticker->ticks_to_expire -= ticks_elapsed;
			break;
		}

		/* decrement ticks_elapsed and collect expired ticks */
		ticks_elapsed -= ticks_to_expire;
		ticks_expired += ticks_to_expire;

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
		ticks_latency -= ticks_to_expire;

		is_must_expire_skip = (ticker->must_expire &&
				       (ticker->lazy_current != 0U));
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

		/* decrement ticks_slot_previous */
		if (instance->ticks_slot_previous > ticks_to_expire) {
			instance->ticks_slot_previous -= ticks_to_expire;
		} else {
			instance->ticker_id_slot_previous = TICKER_NULL;
			instance->ticks_slot_previous = 0U;
		}

		/* If a reschedule is set pending, we will need to keep
		 * the slot_previous information
		 */
		if ((ticker->ticks_slot != 0U) &&
		    (((ticker->req - ticker->ack) & 0xff) == 2U) &&
		    !is_must_expire_skip &&
		    !TICKER_RESCHEDULE_PENDING(ticker)) {
			instance->ticker_id_slot_previous = id_expired;
			instance->ticks_slot_previous = ticker->ticks_slot;
		}

		/* ticker expired, set ticks_to_expire zero */
		ticker->ticks_to_expire = 0U;

		/* remove the expired ticker from head */
		instance->ticker_id_head = ticker->next;

		/* Ticker will be restarted if periodic or to be re-scheduled */
		if ((ticker->ticks_periodic != 0U) ||
		    TICKER_RESCHEDULE_PENDING(ticker)) {
#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
			if (TICKER_RESCHEDULE_PENDING(ticker)) {
				/* Set the re-scheduled node to now. Will be
				 * collision resolved after all nodes are
				 * restarted
				 */
				ticker->ticks_to_expire = ticks_elapsed;

				/* Reset ticker state, so that its put
				 * back in requested state later down
				 * in the code.
				 */
				ticker->req = ticker->ack;
			} else {
				u16_t lazy_periodic;
				u32_t count;
				u16_t lazy;

				/* If not skipped, apply lazy_periodic */
				if (!ticker->lazy_current) {
					lazy_periodic = ticker->lazy_periodic;
				} else {
					lazy_periodic = 0U;

					/* Reset ticker state, so that its put
					 * back in requested state later down
					 * in the code.
					 */
					ticker->req = ticker->ack;
				}

				/* Reload ticks_to_expire with atleast one
				 * period.
				 */
				ticks_to_expire = 0U;
				count = 1 + lazy_periodic;
				while (count--) {
					ticks_to_expire +=
						ticker->ticks_periodic;
					ticks_to_expire +=
						ticker_remainder_inc(ticker);
				}

				/* Skip intervals that have elapsed w.r.t.
				 * current ticks.
				 */
				lazy = 0U;
				if (!ticker->must_expire) {
					while (ticks_to_expire <
					       ticks_latency) {
						ticks_to_expire +=
							ticker->ticks_periodic;
						ticks_to_expire +=
						  ticker_remainder_inc(ticker);
						lazy++;
					}
				}

				/* Use the calculated ticks to expire and
				 * laziness.
				 */
				ticker->ticks_to_expire = ticks_to_expire;
				ticker->lazy_current += (lazy_periodic + lazy);
			}

			ticks_to_expire_prep(ticker, instance->ticks_current,
					     (ticks_previous + ticks_expired));
#else /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */
			u32_t count;

			/* Prepare for next interval */
			ticks_to_expire = 0U;
			count = 1 + ticker->lazy_periodic;
			while (count--) {
				ticks_to_expire += ticker->ticks_periodic;
				ticks_to_expire += ticker_remainder_inc(ticker);
			}
			ticker->ticks_to_expire = ticks_to_expire;

			ticks_to_expire_prep(ticker, instance->ticks_current,
					     (ticks_previous + ticks_expired));

			/* Reset latency to periodic offset */
			ticker->lazy_current = ticker->lazy_periodic;
			ticker->force = 0U;
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */
			/* Add to insert list */
			ticker->next = *insert_head;
			*insert_head = id_expired;

			/* set schedule status of node as restarting. */
			ticker->req++;
		} else {
			/* reset schedule status of node */
			ticker->req = ticker->ack;
		}
	}
}

/**
 * @brief Prepare ticker node start
 *
 * @details Called by ticker_job to prepare ticker node start operation.
 *
 * @param ticker	Pointer to ticker node
 * @param user_op	Pointer to user operation
 * @param ticks_current Expired ticks at time of call
 *
 * @internal
 */
static inline void ticker_job_op_start(struct ticker_node *ticker,
				       struct ticker_user_op *user_op,
				       u32_t ticks_current)
{
	struct ticker_user_op_start *start = (void *)&user_op->params.start;

#if defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
	/* Must expire is not supported in compatibility mode */
	LL_ASSERT(start->lazy != TICKER_LAZY_MUST_EXPIRE);
#else
	ticker->must_expire = (start->lazy == TICKER_LAZY_MUST_EXPIRE) ? 1U :
			       0U;
#if defined(CONFIG_BT_TICKER_EXT)
	ticker->ext_data = start->ext_data;
#endif /* CONFIG_BT_TICKER_EXT */
#endif /* CONFIG_BT_TICKER_COMPATIBILITY_MODE */

	ticker->ticks_periodic = start->ticks_periodic;
	ticker->remainder_periodic = start->remainder_periodic;
	ticker->lazy_periodic = (start->lazy == TICKER_LAZY_MUST_EXPIRE) ? 0U :
				 start->lazy;
	ticker->ticks_slot = start->ticks_slot;
	ticker->timeout_func = start->fp_timeout_func;
	ticker->context = start->context;
	ticker->ticks_to_expire = start->ticks_first;
	ticker->ticks_to_expire_minus = 0U;
	ticks_to_expire_prep(ticker, ticks_current, start->ticks_at_start);
	ticker->remainder_current = 0U;
	ticker->lazy_current = 0U;
	ticker->force = 1U;
}

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
/**
 * @brief Insert new ticker node
 *
 * @details Called by ticker_job to insert a new ticker node. If node collides
 * with existing ticker nodes, either the new node is postponed, or colliding
 * node is un-scheduled. Decision is based on latency and the force-state of
 * individual nodes.
 *
 * @param instance    Pointer to ticker instance
 * @param id_insert   Id of ticker to insert
 * @param ticker      Pointer to ticker node to insert
 * @param insert_head Pointer to current head. Updated if colliding nodes
 *		      are un-scheduled
 * @internal
 */
static inline u32_t ticker_job_insert(struct ticker_instance *instance,
				      u8_t id_insert,
				      struct ticker_node *ticker,
				      u8_t *insert_head)
{
	ARG_UNUSED(insert_head);

	/* Prepare to insert */
	ticker->next = TICKER_NULL;

	/* Enqueue the ticker node */
	(void)ticker_enqueue(instance, id_insert);

	/* Inserted/Scheduled */
	ticker->req = ticker->ack + 1;

	return TICKER_STATUS_SUCCESS;
}

#if defined(CONFIG_BT_TICKER_EXT)
/**
 * @brief Re-schedule ticker nodes within slot_window
 *
 * @details This function is responsible for re-scheduling ticker nodes
 * which have been marked for re-scheduling in ticker_worker. These nodes
 * have a non-zero ticks_slot_window configuration, which indicates a
 * valid range in which to re-schedule the node.
 * The function iterates over nodes, and handles one re-schedule at a
 * time. After a re-schedule, nodes are once again iterated until no more
 * nodes are marked for re-scheduling.
 *
 * @param instance      Pointer to ticker instance
 * @param ticks_elapsed Number of ticks elapsed since last ticker job
 *
 * @internal
 */
static u8_t ticker_job_reschedule_in_window(struct ticker_instance *instance,
					    u32_t ticks_elapsed)
{
	struct ticker_node *nodes;
	struct ticker_node *ticker;
	struct ticker_node *node;
	struct ticker_ext  *ext_data;

	u32_t window_start_ticks;
	u32_t window_end_ticks;
	u32_t ticks_to_expire;
	u32_t ticks_start_offset;
	u32_t ticks_slot_window = 0;

	u8_t  ticker_id_head;
	u8_t  ticker_id_iter;
	u8_t  ticker_id_prev;

	u8_t  rescheduling = 1U;
	u8_t  rescheduled = 0U;

	nodes = &instance->nodes[0];

	/* Do until all pending re-schedules handled */
	while (rescheduling) {
		ticker_id_head = instance->ticker_id_head;
		rescheduling = 0U;

		ticks_start_offset = 0U;
		window_start_ticks = 0U;
		window_end_ticks   = 0U;
		ticks_to_expire    = 0U;

		/* Find first pending re-schedule */
		while (ticker_id_head != TICKER_NULL) {
			ticker = &nodes[ticker_id_head];
			ext_data = ticker->ext_data;
			if (ext_data && ext_data->reschedule_state ==
			    TICKER_RESCHEDULE_STATE_PENDING) {
				/* Pending reschedule found */
				break;
			}
			ticker_id_head = ticker->next;
		}
		if (ticker_id_head == TICKER_NULL) {
			/* Done */
			break;
		}

		/* Check for intersection with already active node */
		if (instance->ticker_id_slot_previous != TICKER_NULL &&
			instance->ticks_slot_previous > ticks_elapsed) {
			/* Active node intersects - window starts after end of
			 * active slot
			 */
			window_start_ticks = instance->ticks_slot_previous -
					     ticks_elapsed;
		}

		ticker_id_iter = nodes[ticker_id_head].next;
		ticker_id_prev = TICKER_NULL;

		/* If drift was applied to this node, this must be
		 * taken into consideration. Reduce the window with
		 * the amount of drift already applied.
		 *
		 * TODO: An improvement on this could be to only consider the
		 * drift (ADV => randomization) if re-sceduling fails. Then the
		 * ticker would have the best possible window to re-schedule in
		 * and not be restricted to ticks_slot_window - ticks_drift.
		 */
		if (ext_data->ticks_drift < ext_data->ticks_slot_window) {
			ticks_slot_window = ext_data->ticks_slot_window -
					    ext_data->ticks_drift;
		} else {
			/* Window has been exhausted - we can't reschedule */
			ticker_id_iter = TICKER_NULL;
		}

		/* Try to find available slot for re-scheduling */
		while (ticker_id_iter != TICKER_NULL &&
		       ticks_start_offset + ticker->ticks_slot <=
		       ticks_slot_window) {

			node = &nodes[ticker_id_iter];

			if (node->ext_data &&
			    node->ext_data->reschedule_state ==
			    TICKER_RESCHEDULE_STATE_PENDING) {
				/* Skip other pending re-schedule nodes */
				ticker_id_iter = node->next;
				continue;
			}

			/* Calculate end of window. Since window may be aligned
			 * with expiry of next node, we add a margin
			 */
			if (node->ticks_to_expire >
			    HAL_TICKER_RESCHEDULE_MARGIN) {
				window_end_ticks =
					MIN(ticks_slot_window,
					    ticks_start_offset +
					    node->ticks_to_expire -
					    HAL_TICKER_RESCHEDULE_MARGIN);
			} else {
				/* Next expiry is too close - try the next
				 * node
				 */
				window_end_ticks = 0;
			}

			/* Calculate new ticks_to_expire as end of window minus
			 * slot size => place at end of window.
			 */
			if (window_end_ticks > ticks_start_offset +
			    ticker->ticks_slot) {
				ticks_to_expire = window_end_ticks -
					ticker->ticks_slot;
			} else {
				/* No space in window - try the next node */
				ticks_to_expire = 0;
			}

			/* Decide if the re-scheduling ticker node fits in the
			 * slot found - break if it fits
			 */
			if (ticks_to_expire != 0U &&
			    ticks_to_expire >= window_start_ticks &&
			    ticks_to_expire <= window_end_ticks -
			    ticker->ticks_slot) {
				/* Re-scheduled node fits before this node */
				break;
			}

			/* We din't find a valid slot for re-scheduling - try
			 * the next node
			 */
			ticks_start_offset += node->ticks_to_expire;
			window_start_ticks  = node->ticks_to_expire +
					      node->ticks_slot;
			ticks_to_expire     = ticks_slot_window -
					      ticker->ticks_slot;

			ticker_id_prev = ticker_id_iter;
			ticker_id_iter = node->next;
		}

		ticker->ticks_to_expire = ticks_to_expire;
		ticker_id_iter = nodes[ticker_id_head].next;
		ticker_id_prev = TICKER_NULL;

		/* Place the ticker node sorted by expiration time and adjust
		 * delta times
		 */
		while (ticker_id_iter != TICKER_NULL) {
			node = &nodes[ticker_id_iter];
			if (ticker->ticks_to_expire > node->ticks_to_expire) {
				/* Node is after this - adjust delta */
				ticker->ticks_to_expire -=
					node->ticks_to_expire;
			} else {
				/* Node is before this one */
				node->ticks_to_expire -=
					ticker->ticks_to_expire;
				break;
			}
			ticker_id_prev = ticker_id_iter;
			ticker_id_iter = node->next;
		}

		if (ticker_id_prev != TICKER_NULL) {
			/* Node did not become the first - update head and
			 * insert node after 'previous'
			 */
			instance->ticker_id_head = nodes[ticker_id_head].next;

			/* Link inserted node */
			nodes[ticker_id_head].next = nodes[ticker_id_prev].next;
			nodes[ticker_id_prev].next = ticker_id_head;
		}

		/* Remove latency added in ticker_worker */
		ticker->lazy_current--;

		/* Prevent repeated re-scheduling */
		ext_data->reschedule_state =
			TICKER_RESCHEDULE_STATE_DONE;

		/* Check for other pending re-schedules and set exit flag */
		rescheduling = 1U;
		rescheduled  = 1U;
	}

	return rescheduled;
}
#endif /* CONFIG_BT_TICKER_EXT */
#else  /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Insert new ticker node
 *
 * @details Called by ticker_job to insert a new ticker node. If node collides
 * with existing ticker nodes, either the new node is postponed, or colliding
 * node is un-scheduled. Decision is based on latency and the force-state of
 * individual nodes.
 *
 * @param instance    Pointer to ticker instance
 * @param id_insert   Id of ticker to insert
 * @param ticker      Pointer to ticker node to insert
 * @param insert_head Pointer to current head. Updated if colliding nodes
 *		      are un-scheduled
 * @internal
 */
static inline u32_t ticker_job_insert(struct ticker_instance *instance,
				      u8_t id_insert,
				      struct ticker_node *ticker,
				      u8_t *insert_head)
{
	struct ticker_node *node = &instance->nodes[0];
	u8_t id_collide;
	u16_t skip;

	/* Prepare to insert */
	ticker->next = TICKER_NULL;

	/* No. of times ticker has skipped its interval */
	if (ticker->lazy_current > ticker->lazy_periodic) {
		skip = ticker->lazy_current -
		       ticker->lazy_periodic;
	} else {
		skip = 0U;
	}

	/* If insert collides, remove colliding or advance to next interval */
	while (id_insert !=
	       (id_collide = ticker_enqueue(instance, id_insert))) {
		/* Check for collision */
		if (id_collide != TICKER_NULL) {
			struct ticker_node *ticker_collide = &node[id_collide];
			u16_t skip_collide;

			/* No. of times colliding ticker has skipped its
			 * interval.
			 */
			if (ticker_collide->lazy_current >
			    ticker_collide->lazy_periodic) {
				skip_collide = ticker_collide->lazy_current -
					       ticker_collide->lazy_periodic;
			} else {
				skip_collide = 0U;
			}

			/* Check if colliding node should be un-scheduled */
			if (ticker_collide->ticks_periodic &&
			    skip_collide <= skip &&
			    ticker_collide->force < ticker->force) {
				/* Dequeue and get the reminder of ticks
				 * to expire.
				 */
				ticker_collide->ticks_to_expire =
					ticker_dequeue(instance, id_collide);
				/* Unschedule node */
				ticker_collide->req = ticker_collide->ack;

				/* Enqueue for re-insertion */
				ticker_collide->next = *insert_head;
				*insert_head = id_collide;

				continue;
			}
		}

		/* occupied, try next interval */
		if (ticker->ticks_periodic != 0U) {
			ticker->ticks_to_expire += ticker->ticks_periodic +
						   ticker_remainder_inc(ticker);
			ticker->lazy_current++;

			/* Remove any accumulated drift (possibly added due to
			 * ticker job execution latencies).
			 */
			if (ticker->ticks_to_expire >
			    ticker->ticks_to_expire_minus) {
				ticker->ticks_to_expire -=
					ticker->ticks_to_expire_minus;
				ticker->ticks_to_expire_minus = 0U;
			} else {
				ticker->ticks_to_expire_minus -=
					ticker->ticks_to_expire;
				ticker->ticks_to_expire = 0U;
			}
		} else {
			return TICKER_STATUS_FAILURE;
		}
	}

	/* Inserted/Scheduled */
	ticker->req = ticker->ack + 1;

	return TICKER_STATUS_SUCCESS;
}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Insert and start ticker nodes for all users
 *
 * @details Called by ticker_job to prepare, insert and start ticker nodes
 * for all users. Specifying insert_head to other than TICKER_NULL causes
 * that ticker node to be inserted first.
 *
 * @param instance    Pointer to ticker instance
 * @param insert_head Id of ticker node to insert, or TICKER_NULL if only
 *                    handle user operation inserts
 * @internal
 */
static inline void ticker_job_list_insert(struct ticker_instance *instance,
					  u8_t insert_head)
{
	struct ticker_node *node;
	struct ticker_user *users;
	u8_t count_user;

	node = &instance->nodes[0];
	users = &instance->users[0];
	count_user = instance->count_user;

	/* Iterate through all user ids */
	while (count_user--) {
		struct ticker_user_op *user_ops;
		struct ticker_user *user;
		u8_t user_ops_first;

		user = &users[count_user];
		user_ops = (void *)&user->user_op[0];
		user_ops_first = user->first;
		/* Traverse user operation queue - first to middle (wrap) */
		while ((insert_head != TICKER_NULL) ||
		       (user_ops_first != user->middle)) {
			struct ticker_user_op *user_op;
			struct ticker_node *ticker;
			u8_t id_insert;
			u32_t status;

			if (insert_head != TICKER_NULL) {
				/* Prepare insert of ticker node specified by
				 * insert_head
				 */
				id_insert = insert_head;
				ticker = &node[id_insert];
				insert_head = ticker->next;

				user_op = NULL;
			} else {
				/* Prepare insert of any ticker nodes requested
				 * via user operation TICKER_USER_OP_TYPE_START
				 */
				u8_t first;

				user_op = &user_ops[user_ops_first];
				first = user_ops_first + 1;
				if (first == user->count_user_op) {
					first = 0U;
				}
				user_ops_first = first;

				id_insert = user_op->id;
				ticker = &node[id_insert];
				if (user_op->op != TICKER_USER_OP_TYPE_START) {
					/* User operation is not start - skip
					 * to next operation
					 */
					continue;
				}

				if (((ticker->req -
				      ticker->ack) & 0xff) != 0U) {
					ticker_job_op_cb(user_op,
							 TICKER_STATUS_FAILURE);
					continue;
				}

				/* Prepare ticker for start */
				ticker_job_op_start(ticker, user_op,
						    instance->ticks_current);
			}

			/* Insert ticker node */
			status = ticker_job_insert(instance, id_insert, ticker,
						   &insert_head);

			if (user_op) {
				ticker_job_op_cb(user_op, status);
			}
		}
	}
}

/**
 * @brief Perform inquiry for specific user operation
 *
 * @param instance Pointer to ticker instance
 * @param uop	   Pointer to user operation
 *
 * @internal
 */
static inline void ticker_job_op_inquire(struct ticker_instance *instance,
					 struct ticker_user_op *uop)
{
	ticker_op_func fp_op_func;

	fp_op_func = NULL;
	switch (uop->op) {
	case TICKER_USER_OP_TYPE_SLOT_GET:
		ticker_by_next_slot_get(instance,
					uop->params.slot_get.ticker_id,
					uop->params.slot_get.ticks_current,
					uop->params.slot_get.ticks_to_expire);
		/* Fall-through */
	case TICKER_USER_OP_TYPE_IDLE_GET:
		uop->status = TICKER_STATUS_SUCCESS;
		fp_op_func = uop->fp_op_func;
		break;
#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
	case TICKER_USER_OP_TYPE_PRIORITY_SET:
		if (uop->id < instance->count_node) {
			struct ticker_node *node = instance->nodes;

			node[uop->id].priority =
				uop->params.priority_set.priority;
			uop->status = TICKER_STATUS_SUCCESS;
		} else {
			uop->status = TICKER_STATUS_FAILURE;
		}
		fp_op_func = uop->fp_op_func;
		break;
#endif
	default:
		/* do nothing for other ops */
		break;
	}

	if (fp_op_func) {
		fp_op_func(uop->status, uop->op_context);
	}
}

/**
 * @brief Check for pending inquiries for all users
 *
 * @details Run through all user operation lists, checking for pending
 * inquiries. Currently only two types of inquiries are supported:
 * TICKER_USER_OP_TYPE_SLOT_GET and TICKER_USER_OP_TYPE_IDLE_GET. The
 * function also supports user operation TICKER_USER_OP_TYPE_PRIORITY_SET.
 * This operation modifies the user->first index, indicating user operations
 * are complete.
 *
 * @param instance Pointer to ticker instance
 *
 * @internal
 */
static inline void ticker_job_list_inquire(struct ticker_instance *instance)
{
	struct ticker_user *users;
	u8_t count_user;

	users = &instance->users[0];
	count_user = instance->count_user;
	/* Traverse user operation queue - first to last (with wrap) */
	while (count_user--) {
		struct ticker_user_op *user_op;
		struct ticker_user *user;

		user = &users[count_user];
		user_op = &user->user_op[0];
		while (user->first != user->last) {
			u8_t first;

			ticker_job_op_inquire(instance, &user_op[user->first]);

			first = user->first + 1;
			if (first == user->count_user_op) {
				first = 0U;
			}
			user->first = first;
		}
	}
}

/**
 * @brief Update counter compare value (trigger)
 *
 * @details Updates trigger to the match next expiring ticker node. The
 * function takes into consideration that it may be preempted in the process,
 * and makes sure - by iteration - that compare value is set in the future
 * (with a margin).
 *
 * @param instance           Pointer to ticker instance
 * @param ticker_id_old_head Previous ticker_id_head
 *
 * @internal
 */
static inline void ticker_job_compare_update(struct ticker_instance *instance,
					     u8_t ticker_id_old_head)
{
	struct ticker_node *ticker;
	u32_t ticks_to_expire;
	u32_t ctr_post;
	u32_t ctr;
	u32_t cc;
	u32_t i;

	if (instance->ticker_id_head == TICKER_NULL) {
		if (cntr_stop() == 0) {
			instance->ticks_slot_previous = 0U;
			instance->ticks_current = cntr_cnt_get();
		}
		return;
	}

	/* Check if this is the first update. If so, start the counter */
	if (ticker_id_old_head == TICKER_NULL) {
		u32_t ticks_current;

		ticks_current = cntr_cnt_get();

		if (cntr_start() == 0) {
			instance->ticks_current = ticks_current;
		}
	}

	ticker = &instance->nodes[instance->ticker_id_head];
	ticks_to_expire = ticker->ticks_to_expire;

	/* Iterate few times, if required, to ensure that compare is
	 * correctly set to a future value. This is required in case
	 * the operation is pre-empted and current h/w counter runs
	 * ahead of compare value to be set.
	 */
	i = 10U;
	do {
		u32_t ticks_elapsed;

		LL_ASSERT(i);
		i--;

		ctr = cntr_cnt_get();
		cc = instance->ticks_current;
		ticks_elapsed = ticker_ticks_diff_get(ctr, cc) +
				HAL_TICKER_CNTR_CMP_OFFSET_MIN +
				HAL_TICKER_CNTR_SET_LATENCY;
		cc += MAX(ticks_elapsed, ticks_to_expire);
		cc &= HAL_TICKER_CNTR_MASK;
		instance->trigger_set_cb(cc);

		ctr_post = cntr_cnt_get();
	} while ((ticker_ticks_diff_get(ctr_post, ctr) +
		  HAL_TICKER_CNTR_CMP_OFFSET_MIN) >
		  ticker_ticks_diff_get(cc, ctr));
}

/**
 * @brief Ticker job
 *
 * @details Runs the bottom half of the ticker, after ticker nodes have elapsed
 * or user operations requested. The ticker_job is responsible for removing and
 * re-inserting ticker nodes, based on next elapsing and periodicity of the
 * nodes. The ticker_job is also responsible for processing user operations,
 * i.e. requests for start, update, stop etc.
 * Invoked from the ticker job mayfly context (TICKER_MAYFLY_CALL_ID_JOB).
 *
 * @param param Pointer to ticker instance
 *
 * @internal
 */
void ticker_job(void *param)
{
	struct ticker_instance *instance = param;
	u8_t ticker_id_old_head;
	u8_t insert_head;
	u32_t ticks_elapsed;
	u32_t ticks_previous;
	u8_t flag_elapsed;
	u8_t pending;
	u8_t flag_compare_update;

	DEBUG_TICKER_JOB(1);

	/* Defer worker, as job is now running */
	if (instance->worker_trigger) {
		DEBUG_TICKER_JOB(0);
		return;
	}
	instance->job_guard = 1U;

	/* Back up the previous known tick */
	ticks_previous = instance->ticks_current;

	/* Update current tick with the elapsed value from queue, and dequeue */
	if (instance->ticks_elapsed_first != instance->ticks_elapsed_last) {
		ticker_next_elapsed(&instance->ticks_elapsed_first);

		ticks_elapsed =
		    instance->ticks_elapsed[instance->ticks_elapsed_first];

		instance->ticks_current += ticks_elapsed;
		instance->ticks_current &= HAL_TICKER_CNTR_MASK;

		flag_elapsed = 1U;
	} else {
		/* No elapsed value in queue */
		flag_elapsed = 0U;
		ticks_elapsed = 0U;
	}

	/* Initialise internal re-insert list */
	insert_head = TICKER_NULL;

	/* Initialise flag used to update next compare value */
	flag_compare_update = 0U;

	/* Remember the old head, so as to decide if new compare needs to be
	 * set.
	 */
	ticker_id_old_head = instance->ticker_id_head;

	/* Manage user operations (updates and deletions) in ticker list */
	pending = ticker_job_list_manage(instance, ticks_elapsed, &insert_head);

	/* Detect change in head of the list */
	if (instance->ticker_id_head != ticker_id_old_head) {
		flag_compare_update = 1U;
	}

	/* Handle expired tickers */
	if (flag_elapsed) {
		ticker_job_worker_bh(instance, ticks_previous, ticks_elapsed,
				     &insert_head);

		/* Detect change in head of the list */
		if (instance->ticker_id_head != ticker_id_old_head) {
			flag_compare_update = 1U;
		}

		/* Handle insertions */
		ticker_job_list_insert(instance, insert_head);

#if defined(CONFIG_BT_TICKER_EXT)
		/* Re-schedule any pending nodes with slot_window */
		if (ticker_job_reschedule_in_window(instance, ticks_elapsed)) {
			flag_compare_update = 1U;
		}
#endif /* CONFIG_BT_TICKER_EXT */
	} else {
		/* Handle insertions */
		ticker_job_list_insert(instance, insert_head);
	}

	/* Detect change in head of the list */
	if (instance->ticker_id_head != ticker_id_old_head) {
		flag_compare_update = 1U;
	}

	/* Process any list inquiries */
	if (!pending) {
		/* Handle inquiries */
		ticker_job_list_inquire(instance);
	}

	/* Permit worker job to run */
	instance->job_guard = 0U;

	/* update compare if head changed */
	if (flag_compare_update) {
		ticker_job_compare_update(instance, ticker_id_old_head);
	}

	/* trigger worker if deferred */
	if (instance->worker_trigger) {
		instance->sched_cb(TICKER_CALL_ID_JOB, TICKER_CALL_ID_WORKER, 1,
				   instance);
	}

	DEBUG_TICKER_JOB(0);
}

/*****************************************************************************
 * Public Interface
 ****************************************************************************/

/**
 * @brief Initialize ticker instance
 *
 * @details Called by ticker instance client once to initialize the ticker.
 *
 * @param instance_index   Index of ticker instance
 * @param count_node	   Number of ticker nodes in node array
 * @param node		   Pointer to ticker node array
 * @param count_user	   Number of users in user array
 * @param user		   Pointer to user array of size count_user
 * @param count_op	   Number of user operations in user_op array
 * @param user_op	   Pointer to user operations array of size count_op
 * @param caller_id_get_cb Pointer to function for retrieving caller_id from
 *			   user id
 * @param sched_cb	   Pointer to function for scheduling ticker_worker
 *			   and ticker_job
 * @param trigger_set_cb   Pointer to function for setting the compare trigger
 *			   ticks value
 *
 * @return TICKER_STATUS_SUCCESS if initialization was successful, otherwise
 * TICKER_STATUS_FAILURE
 */
u32_t ticker_init(u8_t instance_index, u8_t count_node, void *node,
		  u8_t count_user, void *user, u8_t count_op, void *user_op,
		  ticker_caller_id_get_cb_t caller_id_get_cb,
		  ticker_sched_cb_t sched_cb,
		  ticker_trigger_set_cb_t trigger_set_cb)
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op_ = (void *)user_op;
	struct ticker_user *users;

	if (instance_index >= TICKER_INSTANCE_MAX) {
		return TICKER_STATUS_FAILURE;
	}

	instance->count_node = count_node;
	instance->nodes = node;

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
	while (count_node--) {
		instance->nodes[count_node].priority = 0;
	}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

	instance->count_user = count_user;
	instance->users = user;

	/** @todo check if enough ticker_user_op supplied */

	users = &instance->users[0];
	while (count_user--) {
		users[count_user].user_op = user_op_;
		user_op_ += users[count_user].count_user_op;
		count_op -= users[count_user].count_user_op;
	}

	if (count_op) {
		return TICKER_STATUS_FAILURE;
	}

	instance->caller_id_get_cb = caller_id_get_cb;
	instance->sched_cb = sched_cb;
	instance->trigger_set_cb = trigger_set_cb;

	instance->ticker_id_head = TICKER_NULL;
	instance->ticker_id_slot_previous = TICKER_NULL;
	instance->ticks_slot_previous = 0U;
	instance->ticks_current = 0U;
	instance->ticks_elapsed_first = 0U;
	instance->ticks_elapsed_last = 0U;

	return TICKER_STATUS_SUCCESS;
}

/**
 * @brief Check if ticker instance is initialized
 *
 * @param instance_index Index of ticker instance
 *
 * @return true if ticker instance is initialized, false otherwise
 */
bool ticker_is_initialized(u8_t instance_index)
{
	return !!(_instance[instance_index].count_node);
}

/**
 * @brief Trigger the ticker worker
 *
 * @details Schedules the ticker_worker upper half by invoking the
 * corresponding mayfly.
 *
 * @param instance_index Index of ticker instance
 */
void ticker_trigger(u8_t instance_index)
{
	struct ticker_instance *instance;

	DEBUG_TICKER_ISR(1);

	instance = &_instance[instance_index];
	if (instance->sched_cb) {
		instance->sched_cb(TICKER_CALL_ID_TRIGGER,
				   TICKER_CALL_ID_WORKER, 1, instance);
	}

	DEBUG_TICKER_ISR(0);
}

/**
 * @brief Start a ticker node
 *
 * @details Creates a new user operation of type TICKER_USER_OP_TYPE_START and
 * schedules the ticker_job.
 *
 * @param instance_index     Index of ticker instance
 * @param user_id	     Ticker user id. Used for indexing user operations
 *			     and mapping to mayfly caller id
 * @param ticker_id	     Id of ticker node
 * @param ticks_anchor	     Absolute tick count as anchor point for
 *			     ticks_first
 * @param ticks_first	     Initial number of ticks before first timeout
 * @param ticks_periodic     Number of ticks for a peridic ticker node. If 0,
 *			     ticker node is treated as one-shot
 * @param remainder_periodic Periodic ticks fraction
 * @param lazy		     Number of periods to skip (latency). A value of 1
 *			     causes skipping every other timeout
 * @param ticks_slot	     Slot reservation ticks for node (air-time)
 * @param ticks_slot_window  Window in which the slot reservation may be
 *			     re-scheduled to avoid collision. Set to 0 for
 *			     legacy behavior
 * @param fp_timeout_func    Function pointer of function to call at timeout
 * @param context	     Context passed in timeout call
 * @param fp_op_func	     Function pointer of user operation completion
 *			     function
 * @param op_context	     Context passed in operation completion call
 *
 * @return TICKER_STATUS_BUSY if start was successful but not yet completed.
 * TICKER_STATUS_FAILURE is returned if there are no more user operations
 * available, and TICKER_STATUS_SUCCESS is returned if ticker_job gets to
 * run before exiting ticker_start
 */
#if defined(CONFIG_BT_TICKER_EXT)
u32_t ticker_start(u8_t instance_index, u8_t user_id, u8_t ticker_id,
		   u32_t ticks_anchor, u32_t ticks_first, u32_t ticks_periodic,
		   u32_t remainder_periodic, u16_t lazy, u32_t ticks_slot,
		   ticker_timeout_func fp_timeout_func, void *context,
		   ticker_op_func fp_op_func, void *op_context)
{
	return ticker_start_ext(instance_index, user_id, ticker_id,
				ticks_anchor, ticks_first, ticks_periodic,
				remainder_periodic, lazy, ticks_slot,
				fp_timeout_func, context,
				fp_op_func, op_context,
				NULL);
}

u32_t ticker_start_ext(u8_t instance_index, u8_t user_id, u8_t ticker_id,
		       u32_t ticks_anchor, u32_t ticks_first,
		       u32_t ticks_periodic, u32_t remainder_periodic,
		       u16_t lazy, u32_t ticks_slot,
		       ticker_timeout_func fp_timeout_func, void *context,
		       ticker_op_func fp_op_func, void *op_context,
		       struct ticker_ext *ext_data)
#else
u32_t ticker_start(u8_t instance_index, u8_t user_id, u8_t ticker_id,
		   u32_t ticks_anchor, u32_t ticks_first, u32_t ticks_periodic,
		   u32_t remainder_periodic, u16_t lazy, u32_t ticks_slot,
		   ticker_timeout_func fp_timeout_func, void *context,
		   ticker_op_func fp_op_func, void *op_context)
#endif
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op;
	struct ticker_user *user;
	u8_t last;

	user = &instance->users[user_id];

	last = user->last + 1;
	if (last >= user->count_user_op) {
		last = 0U;
	}

	if (last == user->first) {
		return TICKER_STATUS_FAILURE;
	}

	user_op = &user->user_op[user->last];
	user_op->op = TICKER_USER_OP_TYPE_START;
	user_op->id = ticker_id;
	user_op->params.start.ticks_at_start = ticks_anchor;
	user_op->params.start.ticks_first = ticks_first;
	user_op->params.start.ticks_periodic = ticks_periodic;
	user_op->params.start.remainder_periodic = remainder_periodic;
	user_op->params.start.ticks_slot = ticks_slot;
	user_op->params.start.lazy = lazy;
#if defined(CONFIG_BT_TICKER_EXT)
	user_op->params.start.ext_data = ext_data;
#endif
	user_op->params.start.fp_timeout_func = fp_timeout_func;
	user_op->params.start.context = context;
	user_op->status = TICKER_STATUS_BUSY;
	user_op->fp_op_func = fp_op_func;
	user_op->op_context = op_context;

	user->last = last;

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);

	return user_op->status;
}

/**
 * @brief Update a ticker node
 *
 * @details Creates a new user operation of type TICKER_USER_OP_TYPE_UPDATE and
 * schedules the ticker_job.
 *
 * @param instance_index     Index of ticker instance
 * @param user_id	     Ticker user id. Used for indexing user operations
 *			     and mapping to mayfly caller id
 * @param ticker_id	     Id of ticker node
 * @param ticks_drift_plus   Number of ticks to add for drift compensation
 * @param ticks_drift_minus  Number of ticks to subtract for drift compensation
 * @param ticks_slot_plus    Number of ticks to add to slot reservation
 * @param ticks_slot_minus   Number of ticks to add subtract from slot
 *			     reservation
 * @param lazy		     Number of periods to skip (latency). A value of 0
 *			     means no action. 1 means no latency (normal). A
 *			     value >1 means latency = lazy - 1
 * @param force		     Force update to take effect immediately. With
 *			     force = 0, update is scheduled to take effect as
 *			     soon as possible
 * @param fp_op_func	     Function pointer of user operation completion
 *			     function
 * @param op_context	     Context passed in operation completion call
 *
 * @return TICKER_STATUS_BUSY if update was successful but not yet completed.
 * TICKER_STATUS_FAILURE is returned if there are no more user operations
 * available, and TICKER_STATUS_SUCCESS is returned if ticker_job gets to run
 * before exiting ticker_update
 */
u32_t ticker_update(u8_t instance_index, u8_t user_id, u8_t ticker_id,
		    u32_t ticks_drift_plus, u32_t ticks_drift_minus,
		    u32_t ticks_slot_plus, u32_t ticks_slot_minus, u16_t lazy,
		    u8_t force, ticker_op_func fp_op_func, void *op_context)
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op;
	struct ticker_user *user;
	u8_t last;

	user = &instance->users[user_id];

	last = user->last + 1;
	if (last >= user->count_user_op) {
		last = 0U;
	}

	if (last == user->first) {
		return TICKER_STATUS_FAILURE;
	}

	user_op = &user->user_op[user->last];
	user_op->op = TICKER_USER_OP_TYPE_UPDATE;
	user_op->id = ticker_id;
	user_op->params.update.ticks_drift_plus = ticks_drift_plus;
	user_op->params.update.ticks_drift_minus = ticks_drift_minus;
	user_op->params.update.ticks_slot_plus = ticks_slot_plus;
	user_op->params.update.ticks_slot_minus = ticks_slot_minus;
	user_op->params.update.lazy = lazy;
	user_op->params.update.force = force;
	user_op->status = TICKER_STATUS_BUSY;
	user_op->fp_op_func = fp_op_func;
	user_op->op_context = op_context;

	user->last = last;

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);

	return user_op->status;
}

/**
 * @brief Stop a ticker node
 *
 * @details Creates a new user operation of type TICKER_USER_OP_TYPE_STOP and
 * schedules the ticker_job.
 *
 * @param instance_index     Index of ticker instance
 * @param user_id	     Ticker user id. Used for indexing user operations
 *			     and mapping to mayfly caller id
 * @param fp_op_func	     Function pointer of user operation completion
 *			     function
 * @param op_context	     Context passed in operation completion call
 *
 * @return TICKER_STATUS_BUSY if stop was successful but not yet completed.
 * TICKER_STATUS_FAILURE is returned if there are no more user operations
 * available, and TICKER_STATUS_SUCCESS is returned if ticker_job gets to run
 * before exiting ticker_stop
 */
u32_t ticker_stop(u8_t instance_index, u8_t user_id, u8_t ticker_id,
		  ticker_op_func fp_op_func, void *op_context)
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op;
	struct ticker_user *user;
	u8_t last;

	user = &instance->users[user_id];

	last = user->last + 1;
	if (last >= user->count_user_op) {
		last = 0U;
	}

	if (last == user->first) {
		return TICKER_STATUS_FAILURE;
	}

	user_op = &user->user_op[user->last];
	user_op->op = TICKER_USER_OP_TYPE_STOP;
	user_op->id = ticker_id;
	user_op->status = TICKER_STATUS_BUSY;
	user_op->fp_op_func = fp_op_func;
	user_op->op_context = op_context;

	user->last = last;

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);

	return user_op->status;
}

/**
 * @brief Get next ticker node slot
 *
 * @details Gets the next ticker which has slot ticks specified and
 * return the ticker id and accumulated ticks until expiration. If no
 * ticker nodes have slot ticks, the next ticker node is returned.
 * If no head id is provided (TICKER_NULL) the first node is returned.
 *
 * @param instance_index     Index of ticker instance
 * @param user_id	     Ticker user id. Used for indexing user operations
 *			     and mapping to mayfly caller id
 * @param ticker_id	     Pointer to id of ticker node
 * @param ticks_current	     Pointer to current ticks count
 * @param ticks_to_expire    Pointer to ticks to expire
 * @param fp_op_func	     Function pointer of user operation completion
 *			     function
 * @param op_context	     Context passed in operation completion call
 *
 * @return TICKER_STATUS_BUSY if request was successful but not yet completed.
 * TICKER_STATUS_FAILURE is returned if there are no more user operations
 * available, and TICKER_STATUS_SUCCESS is returned if ticker_job gets to run
 * before exiting ticker_next_slot_get
 */
u32_t ticker_next_slot_get(u8_t instance_index, u8_t user_id, u8_t *ticker_id,
			   u32_t *ticks_current, u32_t *ticks_to_expire,
			   ticker_op_func fp_op_func, void *op_context)
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op;
	struct ticker_user *user;
	u8_t last;

	user = &instance->users[user_id];

	last = user->last + 1;
	if (last >= user->count_user_op) {
		last = 0U;
	}

	if (last == user->first) {
		return TICKER_STATUS_FAILURE;
	}

	user_op = &user->user_op[user->last];
	user_op->op = TICKER_USER_OP_TYPE_SLOT_GET;
	user_op->id = TICKER_NULL;
	user_op->params.slot_get.ticker_id = ticker_id;
	user_op->params.slot_get.ticks_current = ticks_current;
	user_op->params.slot_get.ticks_to_expire = ticks_to_expire;
	user_op->status = TICKER_STATUS_BUSY;
	user_op->fp_op_func = fp_op_func;
	user_op->op_context = op_context;

	user->last = last;

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);

	return user_op->status;
}

/**
 * @brief Get a callback at the end of ticker job execution
 *
 * @details Operation completion callback is called at the end of the
 * ticker_job execution. The user operation is immutable.
 *
 * @param instance_index     Index of ticker instance
 * @param user_id	     Ticker user id. Used for indexing user operations
 *			     and mapping to mayfly caller id
 * @param fp_op_func	     Function pointer of user operation completion
 *			     function
 * @param op_context	     Context passed in operation completion call
 *
 * @return TICKER_STATUS_BUSY if request was successful but not yet completed.
 * TICKER_STATUS_FAILURE is returned if there are no more user operations
 * available, and TICKER_STATUS_SUCCESS is returned if ticker_job gets to run
 * before exiting ticker_job_idle_get
 */
u32_t ticker_job_idle_get(u8_t instance_index, u8_t user_id,
			  ticker_op_func fp_op_func, void *op_context)
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op;
	struct ticker_user *user;
	u8_t last;

	user = &instance->users[user_id];

	last = user->last + 1;
	if (last >= user->count_user_op) {
		last = 0U;
	}

	if (last == user->first) {
		return TICKER_STATUS_FAILURE;
	}

	user_op = &user->user_op[user->last];
	user_op->op = TICKER_USER_OP_TYPE_IDLE_GET;
	user_op->id = TICKER_NULL;
	user_op->status = TICKER_STATUS_BUSY;
	user_op->fp_op_func = fp_op_func;
	user_op->op_context = op_context;

	user->last = last;

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);

	return user_op->status;
}

#if !defined(CONFIG_BT_TICKER_COMPATIBILITY_MODE)
/**
 * @brief Set ticker node priority
 *
 * @param instance_index     Index of ticker instance
 * @param user_id	     Ticker user id. Used for indexing user operations
 *			     and mapping to mayfly caller id
 * @param ticker_id	     Id of ticker node to set priority on
 * @param priority	     Priority to set. Range [-128..127], default is 0.
 *			     Lover value equals higher priority. Setting
 *			     priority to -128 (TICKER_PRIORITY_CRITICAL) makes
 *			     the node win all collision challenges. Only one
 *			     node can have this priority assigned.
 * @param fp_op_func	     Function pointer of user operation completion
 *			     function
 * @param op_context	     Context passed in operation completion call
 *
 * @return TICKER_STATUS_BUSY if request was successful but not yet completed.
 * TICKER_STATUS_FAILURE is returned if there are no more user operations
 * available, and TICKER_STATUS_SUCCESS is returned if ticker_job gets to run
 * before exiting ticker_priority_set
 */
u32_t ticker_priority_set(u8_t instance_index, u8_t user_id, u8_t ticker_id,
			  s8_t priority, ticker_op_func fp_op_func,
			  void *op_context)
{
	struct ticker_instance *instance = &_instance[instance_index];
	struct ticker_user_op *user_op;
	struct ticker_user *user;
	u8_t last;

	user = &instance->users[user_id];

	last = user->last + 1;
	if (last >= user->count_user_op) {
		last = 0U;
	}

	if (last == user->first) {
		return TICKER_STATUS_FAILURE;
	}

	user_op = &user->user_op[user->last];
	user_op->op = TICKER_USER_OP_TYPE_PRIORITY_SET;
	user_op->id = ticker_id;
	user_op->params.priority_set.priority = priority;
	user_op->status = TICKER_STATUS_BUSY;
	user_op->fp_op_func = fp_op_func;
	user_op->op_context = op_context;

	user->last = last;

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);

	return user_op->status;
}
#endif /* !CONFIG_BT_TICKER_COMPATIBILITY_MODE */

/**
 * @brief Schedule ticker job
 *
 * @param instance_index Index of ticker instance
 * @param user_id	 Ticker user id. Maps to mayfly caller id
 */
void ticker_job_sched(u8_t instance_index, u8_t user_id)
{
	struct ticker_instance *instance = &_instance[instance_index];

	instance->sched_cb(instance->caller_id_get_cb(user_id),
			   TICKER_CALL_ID_JOB, 0, instance);
}

/**
 * @brief Get current absolute tick count
 *
 * @return Absolute tick count
 */
u32_t ticker_ticks_now_get(void)
{
	return cntr_cnt_get();
}

/**
 * @brief Get diffence between two tick counts
 *
 * @details Subtract two counts and truncate to correct HW dependent counter
 * bit width
 *
 * @param ticks_now Highest tick count (now)
 * @param ticks_old Tick count to subtract from ticks_now
 */
u32_t ticker_ticks_diff_get(u32_t ticks_now, u32_t ticks_old)
{
	return ((ticks_now - ticks_old) & HAL_TICKER_CNTR_MASK);
}