1 /*
2  * Copyright (c) 2017 Oticon A/S
3  * Copyright (c) 2023 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * This file includes the radio timing related logic
8  * That is, how long the different delays and ramp ups are
9  */
10 
11 #include "bs_types.h"
12 #include "bs_tracing.h"
13 #include "NHW_common_types.h"
14 #include "NHW_config.h"
15 #include "NHW_peri_types.h"
16 #include "NHW_RADIO.h"
17 #include "NHW_RADIO_utils.h"
18 
19 extern NRF_RADIO_Type NRF_RADIO_regs;
20 
21 static struct {
22   /*Ramp up times*/
23   bs_time_t TX_RU_time[5][2][2];
24   /* The versions are [1,2Mbps,CodedS=2,CodedS=8, 15.4] [Normal, Fast] [No_TIFS, HW_TIFS] */
25   /* where HW TIFS only applies for Normal rampup */
26   bs_time_t RX_RU_time[5][2][2];
27 
28   /*Digital processing delay:*/
29   bs_time_t TX_chain_delay;    //Time between the START task and the bits start coming out of the antenna
30   bs_time_t RX_chain_delay[5]; //Time between the bit ends in the antenna, and the corresponding event is generated (e.g. ADDRESS)
31                                /*Indexed as 1Mbps, 2Mbps, CodedS=2, CodedS=8, 15.4*/
32 
33   /*Ramp down times*/
34   bs_time_t TX_RD_time[5];
35   bs_time_t RX_RD_time;
36 } radio_timings;
37 
38 
nrfra_timings_init(void)39 void nrfra_timings_init(void) {
40   //All timings for a 52833
41   /* The versions are [1, 2Mbps,CodedS=2,CodedS=8, 15.4] [Normal, Fast] [No_TIFS, HW_TIFS] */
42   /* where HW TIFS only applies for Normal rampup */
43   /* BLE 1 Mbps */
44   radio_timings.TX_RU_time[0][1][0] =  41; // 41000
45   radio_timings.TX_RU_time[0][0][1] = 141; //141000
46   radio_timings.TX_RU_time[0][0][0] = 130; //130000
47   /* BLE 2 Mbps */
48   radio_timings.TX_RU_time[1][1][0] =  40; // 40000
49   radio_timings.TX_RU_time[1][0][1] = 140; //140000
50   radio_timings.TX_RU_time[1][0][0] = 129; //128900
51   /* Coded S=2 */
52   radio_timings.TX_RU_time[2][1][0] =  42; // 42000
53   radio_timings.TX_RU_time[2][0][1] = 132; //132000
54   radio_timings.TX_RU_time[2][0][0] = 132; //132000
55   /* Coded S=8 */
56   radio_timings.TX_RU_time[3][1][0] =  42; // 42000
57   radio_timings.TX_RU_time[3][0][1] = 122; //122000
58   radio_timings.TX_RU_time[3][0][0] = 132; //132000
59   /* 15.4 */
60   radio_timings.TX_RU_time[4][1][0] =  40; // 40000
61   radio_timings.TX_RU_time[4][0][1] = 130; //130000 - Is this correct? or should it be 169us?
62   radio_timings.TX_RU_time[4][0][0] = 129; //128900 ?? just copied from Ble 1Mbps
63 
64   /* BLE 1 Mbps */
65   radio_timings.RX_RU_time[0][1][0] =  40; // 40000
66   radio_timings.RX_RU_time[0][0][1] = 140; //140000
67   radio_timings.RX_RU_time[0][0][0] = 129; //129000
68   /* BLE 2 Mbps */
69   radio_timings.RX_RU_time[1][1][0] =  40; // 40000
70   radio_timings.RX_RU_time[1][0][1] = 140; //140000
71   radio_timings.RX_RU_time[1][0][0] = 129; //129000
72   /* Coded S=2 */ //The radio always ramps up with S=8
73   radio_timings.RX_RU_time[2][1][0] =  40; // 40000
74   radio_timings.RX_RU_time[2][0][1] = 120; //120000
75   radio_timings.RX_RU_time[2][0][0] = 130; //130000
76   /* Coded S=8 */
77   radio_timings.RX_RU_time[3][1][0] =  40; // 40000
78   radio_timings.RX_RU_time[3][0][1] = 120; //120000
79   radio_timings.RX_RU_time[3][0][0] = 130; //130000
80   /* 15.4 */
81   radio_timings.RX_RU_time[4][1][0] =  40; // 40000
82   radio_timings.RX_RU_time[4][0][1] = 130; //140000 - Is this correct? or should it be 169us?
83   radio_timings.RX_RU_time[4][0][0] = 129; //129000 ?? just copied from Ble 1Mbps
84 
85   radio_timings.TX_chain_delay    = 1; //~1us /*both 1, 2Mbps and 15.4, for BLE coded phy it is ~2us*/
86   radio_timings.RX_chain_delay[0] = 9; //9.4  /* 1Mbps */
87   radio_timings.RX_chain_delay[1] = 5; //5.45 /* 2Mbps */
88   radio_timings.RX_chain_delay[2] = 30; ///* BLE coded, S=2 */ /* For simplicity S=2 & S=8 are given the same chain delay */
89   radio_timings.RX_chain_delay[3] = 30; ///* BLE coded, S=8 */
90   radio_timings.RX_chain_delay[4] = 22; //22us /* 15.4 */
91 
92   //Note: TXEND is produced significantly earlier in 15.4 than the end of the bit in the air (~17.3us),
93   //      while for 1/2M BLE it is ~1us, and for coded w S8 it is ~6us.
94   //Note: TXPHYEND comes *after* the bit has finished in air.
95 
96   radio_timings.TX_RD_time[0] = 6;
97   radio_timings.TX_RD_time[1] = 6; //According to the spec this should be 4us for the 52833. To avoid a behavior change we leave it as 6 by now
98   radio_timings.TX_RD_time[2] = 10;
99   radio_timings.TX_RD_time[3] = 10;
100   radio_timings.TX_RD_time[4] = 21;
101   radio_timings.RX_RD_time = 0; //In reality it seems modulation dependent at ~0, ~0 & ~0.5 us
102 }
103 
104 /**
105  * Return the Rx chain delay given the configured MODE
106  */
nhwra_timings_get_Rx_chain_delay(void)107 bs_time_t nhwra_timings_get_Rx_chain_delay(void) {
108   int mod_idx = 0;
109   if (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ble_2Mbit) {
110     mod_idx = 1;
111   } else if ((NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ble_LR500Kbit)
112        || (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ble_LR125Kbit)) {
113     /* To differentiate we'd need to check if we are in the FEC1 or FEC2 and the CI value */
114     mod_idx = 2;
115   } else if (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ieee802154_250Kbit) {
116     mod_idx = 4;
117   }
118   return radio_timings.RX_chain_delay[mod_idx];
119 }
120 
get_modidx(void)121 static int get_modidx(void) {
122   int mod_idx = 0;
123   if (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ble_2Mbit) {
124     mod_idx = 1;
125   } else if (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ble_LR500Kbit) {
126     mod_idx = 2;
127   } else if (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ble_LR125Kbit) {
128     mod_idx = 3;
129   } else if (NRF_RADIO_regs.MODE == RADIO_MODE_MODE_Ieee802154_250Kbit) {
130     mod_idx = 4;
131   }
132   return mod_idx;
133 }
134 /**
135  * Return the rampup time given the configured MODE & MODECNF0
136  * * TxNotRx should be set to 1 for Tx and 0 for Rx
137  * * from_hw_TIFS should be set to 1 if the RADIO is automatically
138  *                switching during its auto IFS mechanism
139  * returns the requested rampup time
140  */
nhwra_timings_get_rampup_time(bool TxNotRx,bool from_hw_TIFS)141 bs_time_t nhwra_timings_get_rampup_time(bool TxNotRx, bool from_hw_TIFS) {
142   int fast  = 0;
143   int mod_idx = get_modidx();
144   int HWTIFS= 0;
145 
146 #if NHW_RADIO_IS_54
147   if ( NRF_RADIO_regs.TIMING & 1 ){ /* TIMMING.RU */
148 #else
149   if ( NRF_RADIO_regs.MODECNF0 & 1 ){ /* MODECNF0.RU */
150 #endif
151     fast = 1;
152   } else {
153     HWTIFS = from_hw_TIFS | nhwra_is_HW_TIFS_enabled();
154   }
155 
156   if (TxNotRx) {
157     return radio_timings.TX_RU_time[mod_idx][fast][HWTIFS];
158   } else {
159     return radio_timings.RX_RU_time[mod_idx][fast][HWTIFS];
160   }
161 }
162 
163 bs_time_t nhwra_timings_get_RX_rampdown_time(void){
164   return radio_timings.RX_RD_time;
165 }
166 
167 bs_time_t nhwra_timings_get_TX_rampdown_time(void){
168   int mod_idx = get_modidx();
169   return radio_timings.TX_RD_time[mod_idx];
170 }
171 
172 bs_time_t nhwra_timings_get_TX_chain_delay(void){
173   return radio_timings.TX_chain_delay;
174 }
175 
176