xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/controller/src/ble_ll_xcvr.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <stdint.h>
21 #include <stddef.h>
22 #include <assert.h>
23 #include <stddef.h>
24 #include "syscfg/syscfg.h"
25 #include "os/os_cputime.h"
26 #include "controller/ble_phy.h"
27 #include "controller/ble_ll.h"
28 #include "controller/ble_ll_xcvr.h"
29 
30 #ifdef BLE_XCVR_RFCLK
31 int
ble_ll_xcvr_rfclk_state(void)32 ble_ll_xcvr_rfclk_state(void)
33 {
34     uint32_t expiry;
35 
36     if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_ON) {
37         expiry = g_ble_ll_data.ll_rfclk_start_time;
38         if ((int32_t)(os_cputime_get32() - expiry) >
39                 g_ble_ll_data.ll_xtal_ticks) {
40             g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_SETTLED;
41         }
42     }
43     return g_ble_ll_data.ll_rfclk_state;
44 }
45 
46 void
ble_ll_xcvr_rfclk_enable(void)47 ble_ll_xcvr_rfclk_enable(void)
48 {
49     if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_OFF) {
50         g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_ON;
51         ble_phy_rfclk_enable();
52     }
53 }
54 
55 void
ble_ll_xcvr_rfclk_disable(void)56 ble_ll_xcvr_rfclk_disable(void)
57 {
58     if (g_ble_ll_data.ll_rfclk_state != BLE_RFCLK_STATE_OFF) {
59         ble_phy_rfclk_disable();
60         g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_OFF;
61     }
62 }
63 
64 void
ble_ll_xcvr_rfclk_stop(void)65 ble_ll_xcvr_rfclk_stop(void)
66 {
67     os_cputime_timer_stop(&g_ble_ll_data.ll_rfclk_timer);
68     ble_ll_xcvr_rfclk_disable();
69 }
70 
71 uint32_t
ble_ll_xcvr_rfclk_time_till_settled(void)72 ble_ll_xcvr_rfclk_time_till_settled(void)
73 {
74     int32_t dt;
75     uint32_t rc;
76 
77     rc = 0;
78     if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_ON) {
79         dt = (int32_t)(os_cputime_get32() - g_ble_ll_data.ll_rfclk_start_time);
80         BLE_LL_ASSERT(dt >= 0);
81         if (dt < g_ble_ll_data.ll_xtal_ticks) {
82             rc = g_ble_ll_data.ll_xtal_ticks - (uint32_t)dt;
83         }
84     }
85 
86     return rc;
87 }
88 
89 /**
90  * Called when the timer to turn on the RF CLOCK expires. This function checks
91  * the state of the clock. If the clock is off, the clock is turned on.
92  * Otherwise, we just exit.
93  *
94  * Context: Interrupt
95  *
96  * @param arg
97  */
98 void
ble_ll_xcvr_rfclk_timer_exp(void * arg)99 ble_ll_xcvr_rfclk_timer_exp(void *arg)
100 {
101     if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_OFF) {
102         ble_ll_xcvr_rfclk_start_now(os_cputime_get32());
103     }
104 }
105 
106 /**
107  * This API is used to turn on the rfclock without setting the cputime timer to
108  * start the clock at some later point.
109  *
110  * NOTE: presumes that the state of the rf clock was checked prior to calling.
111  *
112  * @param now
113  */
114 void
ble_ll_xcvr_rfclk_start_now(uint32_t now)115 ble_ll_xcvr_rfclk_start_now(uint32_t now)
116 {
117     ble_ll_xcvr_rfclk_enable();
118     g_ble_ll_data.ll_rfclk_start_time = now;
119 }
120 
121 /**
122  * Starts the timer that will turn the rf clock on. The 'cputime' is
123  * the time at which the clock needs to be settled.
124  *
125  * @param cputime   Time at which rfclock should be on and settled.
126  */
127 void
ble_ll_xcvr_rfclk_timer_start(uint32_t cputime)128 ble_ll_xcvr_rfclk_timer_start(uint32_t cputime)
129 {
130     /*
131      * If we are currently in an advertising event or a connection event,
132      * no need to start the cputime timer
133      */
134     if ((g_ble_ll_data.ll_state == BLE_LL_STATE_ADV) ||
135         (g_ble_ll_data.ll_state == BLE_LL_STATE_CONNECTION)) {
136         return;
137     }
138 
139     /* Account for the settling time */
140     cputime -= g_ble_ll_data.ll_xtal_ticks;
141 
142     /*
143      * If the timer is on the list, we need to see if its expiry is before
144      * 'cputime'. If the expiry is before, no need to do anything. If it
145      * is after, we need to stop the timer and start at new time.
146      */
147     if (g_ble_ll_data.ll_rfclk_timer.link.tqe_prev != NULL) {
148         if ((int32_t)(cputime - g_ble_ll_data.ll_rfclk_timer.expiry) >= 0) {
149             return;
150         }
151         os_cputime_timer_stop(&g_ble_ll_data.ll_rfclk_timer);
152     }
153     os_cputime_timer_start(&g_ble_ll_data.ll_rfclk_timer, cputime);
154 }
155 #endif
156