1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 
19 /********************************************************************************************************
20  * @file	rf.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #ifndef     RF_H
28 #define     RF_H
29 
30 #include "gpio.h"
31 #include "sys.h"
32 
33 /**********************************************************************************************************************
34  *                                         RF  global macro                                                           *
35  *********************************************************************************************************************/
36 /**
37  *  @brief This define serve to calculate the DMA length of packet.
38  */
39 #define 	rf_tx_packet_dma_len(rf_data_len)			(((rf_data_len)+3)/4)|(((rf_data_len) % 4)<<22)
40 
41 /***********************************************************FOR BLE******************************************************/
42 /**
43  *  @brief Those setting of offset according to ble packet format, so this setting for ble only.
44  */
45 #define 	RF_BLE_DMA_RFRX_LEN_HW_INFO					0
46 #define 	RF_BLE_DMA_RFRX_OFFSET_HEADER				4
47 #define 	RF_BLE_DMA_RFRX_OFFSET_RFLEN				5
48 #define 	RF_BLE_DMA_RFRX_OFFSET_DATA					6
49 
50 /**
51  *  @brief According to the packet format find the information of packet through offset.
52  */
53 #define 	rf_ble_dma_rx_offset_crc24(p)				(p[RF_BLE_DMA_RFRX_OFFSET_RFLEN]+6)  //data len:3
54 #define 	rf_ble_dma_rx_offset_time_stamp(p)			(p[RF_BLE_DMA_RFRX_OFFSET_RFLEN]+9)  //data len:4
55 #define 	rf_ble_dma_rx_offset_freq_offset(p)			(p[RF_BLE_DMA_RFRX_OFFSET_RFLEN]+13) //data len:2
56 #define 	rf_ble_dma_rx_offset_rssi(p)				(p[RF_BLE_DMA_RFRX_OFFSET_RFLEN]+15) //data len:1, signed
57 #define		rf_ble_packet_length_ok(p)					(p[5] <= reg_rf_rxtmaxlen)    			//dma_len must 4 byte aligned
58 #define		rf_ble_packet_crc_ok(p)						((p[(p[5]+5 + 11)] & 0x01) == 0x0)
59 
60 /**
61  * @brief		This define for ble debug the effect of rx_dly.
62  * 				when this function turn on the time of rx_dly will shorten 6.3us,
63  */
64 #define 	RF_RX_SHORT_MODE_EN			1//In order to debug whether the problem is caused by rx_dly.
65 
66 /******************************************************FOR ESB************************************************************/
67 
68 /**
69  *  @brief Those setting of offset according to private esb packet format, so this setting for ble only.
70  */
71 #define 	RF_PRI_ESB_DMA_RFRX_OFFSET_RFLEN				4
72 
73 /**
74  *  @brief According to the packet format find the information of packet through offset.
75  */
76 
77 #define 	rf_pri_esb_dma_rx_offset_crc(p)					(p[RF_PRI_ESB_DMA_RFRX_OFFSET_RFLEN]+5)  //data len:2
78 #define 	rf_pri_esb_dma_rx_offset_time_stamp(p)			(p[RF_PRI_ESB_DMA_RFRX_OFFSET_RFLEN]+7)  //data len:4
79 #define 	rf_pri_esb_dma_rx_offset_freq_offset(p)			(p[RF_PRI_ESB_DMA_RFRX_OFFSET_RFLEN]+11) //data len:2
80 #define 	rf_pri_esb_dma_rx_offset_rssi(p)				(p[RF_PRI_ESB_DMA_RFRX_OFFSET_RFLEN]+13) //data len:1, signed
81 #define     rf_pri_esb_packet_crc_ok(p)            		((p[((p[4] & 0x3f) + 11+3)] & 0x01) == 0x00)
82 
83 
84 /******************************************************FOR ZIGBEE************************************************************/
85 
86 /**
87  *  @brief Those setting of offset according to zigbee packet format, so this setting for zigbee only.
88  */
89 
90 #define 	RF_ZIGBEE_DMA_RFRX_OFFSET_RFLEN				4
91 
92 /**
93  *  @brief According to the packet format find the information of packet through offset.
94  */
95 
96 
97 #define 	rf_zigbee_dma_rx_offset_crc(p)					(p[RF_ZIGBEE_DMA_RFRX_OFFSET_RFLEN]+3)  //data len:2
98 #define 	rf_zigbee_dma_rx_offset_time_stamp(p)			(p[RF_ZIGBEE_DMA_RFRX_OFFSET_RFLEN]+5)  //data len:4
99 #define 	rf_zigbee_dma_rx_offset_freq_offset(p)			(p[RF_ZIGBEE_DMA_RFRX_OFFSET_RFLEN]+9) //data len:2
100 #define 	rf_zigbee_dma_rx_offset_rssi(p)				(p[RF_ZIGBEE_DMA_RFRX_OFFSET_RFLEN]+11) //data len:1, signed
101 #define     rf_zigbee_packet_crc_ok(p)       			((p[(p[4]+9+3)] & 0x51) == 0x0)
102 #define		rf_zigbee_get_payload_len(p)				(p[4])
103 #define		rf_zigbee_packet_length_ok(p)				(1)
104 /**
105  *  @brief According to different packet format find the crc check digit.
106  */
107 #define     rf_pri_sb_packet_crc_ok(p)              	((p[(reg_rf_sblen & 0x3f)+4+9] & 0x01) == 0x00)
108 #define     rf_hybee_packet_crc_ok(p)       			((p[(p[4]+9+3)] & 0x51) == 0x0)
109 
110 #define     rf_ant_packet_crc_ok(p)              	((p[(reg_rf_sblen & 0x3f)+4+9] & 0x01) == 0x00)
111 
112 /**********************************************************************************************************************
113  *                                       RF global data type                                                          *
114  *********************************************************************************************************************/
115 
116 /**
117  *  @brief  select status of rf.
118  */
119 typedef enum {
120     RF_MODE_TX = 0,		/**<  Tx mode */
121     RF_MODE_RX = 1,		/**<  Rx mode */
122     RF_MODE_AUTO=2,		/**<  Auto mode */
123 	RF_MODE_OFF =3		/**<  TXRX OFF mode */
124 } rf_status_e;
125 
126 /**
127  *  @brief  select RX_CYC2LNA and TX_CYC2PA pin;
128  */
129 
130 typedef enum {
131 	RF_RFFE_RX_PB1 = GPIO_PB1,	/**<  pb1 as rffe rx pin */
132     RF_RFFE_RX_PD6 = GPIO_PD6,	/**<  pd6 as rffe rx pin */
133     RF_RFFE_RX_PE4 = GPIO_PE4	/**<  pe4 as rffe rx pin */
134 } rf_lna_rx_pin_e;
135 
136 
137 typedef enum {
138 	RF_RFFE_TX_PB0 = GPIO_PB0,	/**<  pb0 as rffe tx pin */
139 	RF_RFFE_TX_PB6 = GPIO_PB6,	/**<  pb6 as rffe tx pin */
140     RF_RFFE_TX_PD7 = GPIO_PD7,	/**<  pd7 as rffe tx pin */
141     RF_RFFE_TX_PE5 = GPIO_PE5	/**<  pe5 as rffe tx pin */
142 } rf_pa_tx_pin_e;
143 
144 /**
145  *  @brief  Define power list of RF.
146  */
147 typedef enum {
148 	 /*VBAT*/
149 	 RF_POWER_P9p11dBm = 63,  /**<  9.1 dbm */
150 	 RF_POWER_P8p57dBm  = 45, /**<  8.6 dbm */
151 	 RF_POWER_P8p05dBm  = 35, /**<  8.1 dbm */
152 	 RF_POWER_P7p45dBm  = 27, /**<  7.5 dbm */
153 	 RF_POWER_P6p98dBm  = 23, /**<  7.0 dbm */
154 	 RF_POWER_P5p68dBm  = 18, /**<  6.0 dbm */
155 	 /*VANT*/
156 	 RF_POWER_P4p35dBm  = BIT(7) | 63,   /**<   4.4 dbm */
157 	 RF_POWER_P3p83dBm  = BIT(7) | 50,   /**<   3.8 dbm */
158 	 RF_POWER_P3p25dBm  = BIT(7) | 41,   /**<   3.3 dbm */
159 	 RF_POWER_P2p79dBm  = BIT(7) | 36,   /**<   2.8 dbm */
160 	 RF_POWER_P2p32dBm  = BIT(7) | 32,   /**<   2.3 dbm */
161 	 RF_POWER_P1p72dBm  = BIT(7) | 26,   /**<   1.7 dbm */
162 	 RF_POWER_P0p80dBm  = BIT(7) | 22,   /**<   0.8 dbm */
163 	 RF_POWER_P0p01dBm  = BIT(7) | 20,   /**<   0.0 dbm */
164 	 RF_POWER_N0p53dBm  = BIT(7) | 18,   /**<  -0.5 dbm */
165 	 RF_POWER_N1p37dBm  = BIT(7) | 16,   /**<  -1.4 dbm */
166 	 RF_POWER_N2p01dBm  = BIT(7) | 14,   /**<  -2.0 dbm */
167 	 RF_POWER_N3p37dBm  = BIT(7) | 12,   /**<  -3.4 dbm */
168 	 RF_POWER_N4p77dBm  = BIT(7) | 10,   /**<  -4.8 dbm */
169 	 RF_POWER_N6p54dBm = BIT(7) | 8,     /**<  -6.5 dbm */
170 	 RF_POWER_N8p78dBm = BIT(7) | 6,     /**<  -8.8 dbm */
171 	 RF_POWER_N12p06dBm = BIT(7) | 4,    /**<  -12.1 dbm */
172 	 RF_POWER_N17p83dBm = BIT(7) | 2,    /**<  -17.8 dbm */
173 	 RF_POWER_N23p54dBm = BIT(7) | 1,    /**<  -23.5 dbm */
174 
175 	 RF_POWER_N30dBm    = 0xff,          /**<  -30 dbm */
176 	 RF_POWER_N50dBm    = BIT(7) | 0,    /**<  -50 dbm */
177 
178 } rf_power_level_e;
179 
180 /**
181  *  @brief  Define power index list of RF.
182  */
183 typedef enum {
184 	 /*VBAT*/
185 	 RF_POWER_INDEX_P9p11dBm,	/**< power index of 9.1 dbm */
186 	 RF_POWER_INDEX_P8p57dBm,	/**< power index of 8.6 dbm */
187 	 RF_POWER_INDEX_P8p05dBm,	/**< power index of 8.1 dbm */
188 	 RF_POWER_INDEX_P7p45dBm,	/**< power index of 7.5 dbm */
189 	 RF_POWER_INDEX_P6p98dBm,	/**< power index of 7.0 dbm */
190 	 RF_POWER_INDEX_P5p68dBm,	/**< power index of 6.0 dbm */
191 	 /*VANT*/
192 	 RF_POWER_INDEX_P4p35dBm,	/**< power index of 4.4 dbm */
193 	 RF_POWER_INDEX_P3p83dBm,	/**< power index of 3.8 dbm */
194 	 RF_POWER_INDEX_P3p25dBm,	/**< power index of 3.3 dbm */
195 	 RF_POWER_INDEX_P2p79dBm,	/**< power index of 2.8 dbm */
196 	 RF_POWER_INDEX_P2p32dBm,	/**< power index of 2.3 dbm */
197 	 RF_POWER_INDEX_P1p72dBm,	/**< power index of 1.7 dbm */
198 	 RF_POWER_INDEX_P0p80dBm,	/**< power index of 0.8 dbm */
199 	 RF_POWER_INDEX_P0p01dBm,	/**< power index of 0.0 dbm */
200 	 RF_POWER_INDEX_N0p53dBm,	/**< power index of -0.5 dbm */
201 	 RF_POWER_INDEX_N1p37dBm,	/**< power index of -1.4 dbm */
202 	 RF_POWER_INDEX_N2p01dBm,	/**< power index of -2.0 dbm */
203 	 RF_POWER_INDEX_N3p37dBm,	/**< power index of -3.4 dbm */
204 	 RF_POWER_INDEX_N4p77dBm,	/**< power index of -4.8 dbm */
205 	 RF_POWER_INDEX_N6p54dBm,	/**< power index of -6.5 dbm */
206 	 RF_POWER_INDEX_N8p78dBm,	/**< power index of -8.8 dbm */
207 	 RF_POWER_INDEX_N12p06dBm,	/**< power index of -12.1 dbm */
208 	 RF_POWER_INDEX_N17p83dBm,	/**< power index of -17.8 dbm */
209 	 RF_POWER_INDEX_N23p54dBm,	/**< power index of -23.5 dbm */
210 } rf_power_level_index_e;
211 
212 
213 
214 /**
215  *  @brief  Define RF mode.
216  */
217 typedef enum {
218 	RF_MODE_BLE_2M 		   =    BIT(0),		/**< ble 2m mode */
219 	RF_MODE_BLE_1M 		   = 	BIT(1),		/**< ble 1M mode */
220     RF_MODE_BLE_1M_NO_PN   =    BIT(2),		/**< ble 1M close pn mode */
221 	RF_MODE_ZIGBEE_250K    =    BIT(3),		/**< zigbee 250K mode */
222     RF_MODE_LR_S2_500K     =    BIT(4),		/**< ble 500K mode */
223     RF_MODE_LR_S8_125K     =    BIT(5),		/**< ble 125K mode */
224     RF_MODE_PRIVATE_250K   =    BIT(6),		/**< private 250K mode */
225     RF_MODE_PRIVATE_500K   =    BIT(7),		/**< private 500K mode */
226     RF_MODE_PRIVATE_1M     =    BIT(8),		/**< private 1M mode */
227     RF_MODE_PRIVATE_2M     =    BIT(9),		/**< private 2M mode */
228     RF_MODE_ANT     	   =    BIT(10),	/**< ant mode */
229     RF_MODE_BLE_2M_NO_PN   =    BIT(11),	/**< ble 2M close pn mode */
230     RF_MODE_HYBEE_1M   	   =    BIT(12),	/**< hybee 1M mode */
231     RF_MODE_HYBEE_2M   	   =    BIT(13),	/**< hybee 2M mode */
232     RF_MODE_HYBEE_500K     =    BIT(14),	/**< hybee 500K mode */
233 } rf_mode_e;
234 
235 
236 
237 /**
238  *  @brief  Define RF channel.
239  */
240 typedef enum {
241 	 RF_CHANNEL_0   =    BIT(0),	/**< RF channel 0 */
242 	 RF_CHANNEL_1   =    BIT(1),	/**< RF channel 1 */
243 	 RF_CHANNEL_2   =    BIT(2),	/**< RF channel 2 */
244 	 RF_CHANNEL_3   =    BIT(3),	/**< RF channel 3 */
245 	 RF_CHANNEL_4   =    BIT(4),	/**< RF channel 4 */
246 	 RF_CHANNEL_5   =    BIT(5),	/**< RF channel 5 */
247 	 RF_CHANNEL_NONE =   0x00,		/**< none RF channel*/
248 	 RF_CHANNEL_ALL =    0x0f,		/**< all RF channel */
249 } rf_channel_e;
250 
251 /**********************************************************************************************************************
252  *                                         RF global constants                                                        *
253  *********************************************************************************************************************/
254 extern const rf_power_level_e rf_power_Level_list[30];
255 
256 
257 /**********************************************************************************************************************
258  *                                         RF function declaration                                                    *
259  *********************************************************************************************************************/
260 
261 
262 /**
263  * @brief   	This function serves to judge the statue of  RF receive.
264  * @return  	-#0:idle
265  * 				-#1:rx_busy
266  */
rf_receiving_flag(void)267 static inline unsigned char rf_receiving_flag(void)
268 {
269 	//if the value of [2:0] of the reg_0x140840 isn't 0 , it means that the RF is in the receiving packet phase.(confirmed by junwen).
270 	return ((read_reg8(0x140840)&0x07) > 1);
271 }
272 
273 /**
274  * @brief	This function serve to judge whether it is in a certain state.
275  * @param[in]	status	- Option of rf state machine status.
276  * @return		-#0:Not in parameter setting state
277  * 				-#1:In parameter setting state
278  */
rf_get_state_machine_status(state_machine_status_e status)279 static inline unsigned short rf_get_state_machine_status(state_machine_status_e status)
280 {
281 	return	status == read_reg8(0x140a24);
282 }
283 /**
284  * @brief	  	This function serves to set the which irq enable.
285  * @param[in]	mask 	- Options that need to be enabled.
286  * @return	 	Yes: 1, NO: 0.
287  */
rf_set_irq_mask(rf_irq_e mask)288 static inline void rf_set_irq_mask(rf_irq_e mask)
289 {
290 	BM_SET(reg_rf_irq_mask,mask);
291 }
292 
293 
294 /**
295  * @brief	  	This function serves to clear the TX/RX irq mask.
296  * @param[in]   mask 	- RX/TX irq value.
297  * @return	 	none.
298  */
rf_clr_irq_mask(rf_irq_e mask)299 static inline void rf_clr_irq_mask(rf_irq_e mask)
300 {
301 	BM_CLR(reg_rf_irq_mask,mask);
302 }
303 
304 
305 /**
306  *	@brief	  	This function serves to judge whether it is in a certain state.
307  *	@param[in]	mask 	- RX/TX irq status.
308  *	@return	 	Yes: 1, NO: 0.
309  */
rf_get_irq_status(rf_irq_e status)310 static inline unsigned short rf_get_irq_status(rf_irq_e status)
311 {
312 	return ((unsigned short )BM_IS_SET(reg_rf_irq_status,status));
313 }
314 
315 
316 /**
317  *@brief	This function serves to clear the Tx/Rx finish flag bit.
318  *			After all packet data are sent, corresponding Tx finish flag bit
319  *			will be set as 1.By reading this flag bit, it can check whether
320  *			packet transmission is finished. After the check, it is needed to
321  *			manually clear this flag bit so as to avoid misjudgment.
322  *@return	none.
323  */
rf_clr_irq_status(rf_irq_e status)324 static inline void rf_clr_irq_status(rf_irq_e status)
325 {
326 	  reg_rf_irq_status = status;
327 }
328 
329 
330 /**
331  * @brief   	This function serves to settle adjust for RF Tx.This function for adjust the differ time
332  * 				when rx_dly enable.
333  * @param[in]   txstl_us   - adjust TX settle time.
334  * @return  	none.
335  */
rf_tx_settle_us(unsigned short txstl_us)336 static inline void 	rf_tx_settle_us(unsigned short txstl_us)
337 {
338 	REG_ADDR16(0x80140a04) = txstl_us;
339 }
340 
341 
342 /**
343  * @brief   	This function serves to set RF access code.
344  * @param[in]   acc   - the value of access code.
345  * @return  	none.
346  */
rf_access_code_comm(unsigned int acc)347 static inline void rf_access_code_comm (unsigned int acc)
348 {
349 	reg_rf_access_code = acc;
350 	//The following two lines of code are for trigger access code in S2,S8 mode.It has no effect on other modes.
351 	write_reg8(0x140c25,read_reg8(0x140c25)|0x01);
352 }
353 
354 
355 /**
356  * @brief		this function is to enable/disable each access_code channel for
357  *				RF Rx terminal.
358  * @param[in]	pipe  	- Bit0~bit5 correspond to channel 0~5, respectively.
359  *					  	- #0:Disable.
360  *					  	- #1:Enable.
361  *						  If "enable" is set as 0x3f (i.e. 00111111),
362  *						  all access_code channels (0~5) are enabled.
363  * @return	 	none
364  */
rf_rx_acc_code_pipe_en(rf_channel_e pipe)365 static inline void rf_rx_acc_code_pipe_en(rf_channel_e pipe)
366 {
367     write_reg8(0x140c4d, (read_reg8(0x140c4d)&0xc0) | pipe); //rx_access_code_chn_en
368 }
369 
370 
371 /**
372  * @brief		this function is to select access_code channel for RF tx terminal.
373  * @param[in]	pipe  	- Bit0~bit2 the value correspond to channel 0~5, respectively.
374  *						  if value > 5 enable channel 5.And only 1 channel can be selected every time.
375  *						- #0:Disable.
376  *						- #1:Enable.
377  *						  If "enable" is set as 0x7 (i.e. 0111),
378  *						  the access_code channel (5) is enabled.
379  * @return	 	none
380  */
rf_tx_acc_code_pipe_en(rf_channel_e pipe)381 static inline void rf_tx_acc_code_pipe_en(rf_channel_e pipe)
382 {
383     write_reg8(0x140a15, (read_reg8(0x140a15)&0xf8) | pipe); //Tx_Channel_man[2:0]
384 }
385 
386 
387 /**
388  * @brief 	  This function serves to reset RF Tx/Rx mode.
389  * @return 	  none.
390  */
rf_set_tx_rx_off(void)391 static inline void rf_set_tx_rx_off(void)
392 {
393 	write_reg8 (0x80140a16, 0x29);
394 	write_reg8 (0x80140828, 0x80);	// rx disable
395 	write_reg8 (0x80140a02, 0x45);	// reset tx/rx state machine
396 }
397 
398 
399 /**
400  * @brief    This function serves to turn off RF auto mode.
401  * @return   none.
402  */
rf_set_tx_rx_off_auto_mode(void)403 static inline void rf_set_tx_rx_off_auto_mode(void)
404 {
405 	write_reg8 (0x80140a00, 0x80);
406 }
407 
408 
409 /**
410  * @brief    This function serves to set CRC advantage.
411  * @return   none.
412  */
rf_set_ble_crc_adv(void)413 static inline void rf_set_ble_crc_adv (void)
414 {
415 	write_reg32 (0x80140824, 0x555555);
416 }
417 
418 
419 /**
420  * @brief  	  	This function serves to set CRC value for RF.
421  * @param[in]  	crc  - CRC value.
422  * @return 		none.
423  */
rf_set_ble_crc_value(unsigned int crc)424 static inline void rf_set_ble_crc_value (unsigned int crc)
425 {
426 	write_reg32 (0x80140824, crc);
427 }
428 
429 
430 /**
431  * @brief  	   This function serves to set the max length of rx packet.Use byte_len to limit what DMA
432  * 			   moves out will not exceed the buffer size we define.And old chip do this through dma size.
433  * @param[in]  byte_len  - The longest of rx packet.
434  * @return     none.
435  */
rf_set_rx_maxlen(unsigned int byte_len)436 static inline void rf_set_rx_maxlen(unsigned int byte_len)
437 {
438 	reg_rf_rxtmaxlen = byte_len;
439 }
440 
441 
442 /**
443  * @brief		This function serve to rx dma fifo size.
444  * @param[in]	fifo_byte_size - the size of each fifo.
445  * @return		none
446  */
rf_set_rx_dma_fifo_size(unsigned short fifo_byte_size)447 static inline void rf_set_rx_dma_fifo_size(unsigned short fifo_byte_size)
448 {
449 	reg_rf_bb_rx_size = fifo_byte_size>>4;
450 }
451 /**
452  * @brief		This function serve to set rx dma wptr.
453  * @param[in]	wptr	-rx_wptr_real=rx_wptr & mask:After receiving 4 packets,the address returns to original address.mask value must in (0x01,0x03,0x07,0x0f).
454  * @return 		none
455  */
rf_set_rx_dma_fifo_num(unsigned char fifo_num)456 static inline void rf_set_rx_dma_fifo_num(unsigned char fifo_num)
457 {
458 	reg_rf_rx_wptr_mask = fifo_num; //rx_wptr_real=rx_wptr & mask:After receiving 4 packets,the address returns to original address.mask value must in (0x01,0x03,0x07,0x0f).
459 }
460 
461 
462 /**
463  * @brief	  	This function serves to DMA rxFIFO address
464  *	            The function apply to the configuration of one rxFiFO when receiving packets,
465  *	            In this case,the rxFiFo address can be changed every time a packet is received
466  *	            Before setting, call the function "rf_set_rx_dma" to clear DMA fifo mask value(set 0)
467  * @param[in]	rx_addr   - The address store receive packet.
468  * @return	 	none
469  */
rf_set_rx_buffer(unsigned char * rx_addr)470 static inline void rf_set_rx_buffer(unsigned char *rx_addr)
471 {
472 	rx_addr += 4;
473 	dma_set_dst_address(DMA1,convert_ram_addr_cpu2bus(rx_addr));
474 }
475 /**
476  * @brief		This function serve to set the number of tx dma fifo.
477  * @param[in]	fifo_dep - the number of dma fifo is 2 to the power of fifo_dep.
478  * @return		none
479  */
rf_set_tx_dma_fifo_num(unsigned char fifo_num)480 static inline void rf_set_tx_dma_fifo_num(unsigned char fifo_num)
481 {
482 	reg_rf_bb_tx_chn_dep = fifo_num;//tx_chn_dep = 2^2 =4 (have 4 fifo)
483 }
484 
485 /**
486  * @brief		This function serve to set the number of tx dma fifo.
487  * @param[in]	fifo_byte_size - the size of each dma fifo.
488  * @return		none
489  */
rf_set_tx_dma_fifo_size(unsigned short fifo_byte_size)490 static inline void rf_set_tx_dma_fifo_size(unsigned short fifo_byte_size)
491 {
492 	reg_rf_bb_tx_size	= fifo_byte_size>>4;//tx_idx_addr = {tx_chn_adr*bb_tx_size,4'b0}// in this setting the max data in one dma buffer is 0x20<<4.And the The product of fifo_dep and bytesize cannot exceed 0xfff.
493 }
494 /**
495  * @brief   This function serves to set RF tx settle time.
496  * @tx_stl_us  tx settle time.The max value of this param is 0xfff;
497  * @return  none.
498  */
rf_set_tx_settle_time(unsigned short tx_stl_us)499 static inline void rf_set_tx_settle_time(unsigned short tx_stl_us )
500 {
501 	tx_stl_us &= 0x0fff;
502 	write_reg8(0x140a04, (read_reg8(0x140a04)& 0xf000) |tx_stl_us);//txxstl 112us
503 }
504 /**
505  * @brief   This function serves to set RF tx settle time and rx settle time.
506  * @rx_stl_us  rx settle time.The max value of this param is 0xfff;
507  * @return  none.
508  */
rf_set_rx_settle_time(unsigned short rx_stl_us)509 static inline void rf_set_rx_settle_time( unsigned short rx_stl_us )
510 {
511 	 rx_stl_us &= 0x0fff;
512 	 write_reg8(0x140a0c, (read_reg8(0x140a0c)& 0xf000) |rx_stl_us);//rxstl 85us
513 }
514 
515 /**
516  * @brief	This function serve to get ptx wptr.
517  * @param[in]	pipe_id	-	The number of tx fifo.0<= pipe_id <=5.
518  * @return		The write pointer of the tx.
519  */
rf_get_tx_wptr(unsigned char pipe_id)520 static inline unsigned char rf_get_tx_wptr(unsigned char pipe_id)
521 {
522 	return reg_rf_dma_tx_wptr(pipe_id);
523 }
524 
525 /**
526  * @brief	This function serve to update the wptr of tx terminal.
527  * @param[in]	pipe_id	-	The number of pipe which need to update wptr.
528  * @param[in]	wptr	-	The pointer of write in tx terminal.
529  * @return		none
530  */
rf_set_tx_wptr(unsigned char pipe_id,unsigned char wptr)531 static inline void rf_set_tx_wptr(unsigned char pipe_id,unsigned char wptr)
532 {
533 	reg_rf_dma_tx_wptr(pipe_id) = wptr;
534 }
535 
536 
537 /**
538  * @brief	This function serve to clear the writer pointer of tx terminal.
539  * @param[in]	pipe_id	-	The number of tx DMA.0<= pipe_id <=5.
540  * @return	none.
541  */
rf_clr_tx_wptr(unsigned char pipe_id)542 static inline void rf_clr_tx_wptr(unsigned char pipe_id)
543 {
544 	reg_rf_dma_tx_wptr(pipe_id) = 0;
545 }
546 
547 /**
548  * @brief	This function serve to get ptx rptr.
549  * @param[in]	pipe_id	-The number of tx pipe.0<= pipe_id <=5.
550  * @return		The read pointer of the tx.
551  */
rf_get_tx_rptr(unsigned char pipe_id)552 static inline unsigned char rf_get_tx_rptr(unsigned char pipe_id)
553 {
554 	return reg_rf_dma_tx_rptr(pipe_id);
555 }
556 
557 
558 /**
559  * @brief	This function serve to clear read pointer of tx terminal.
560  * @param[in]	pipe_id	-	The number of tx DMA.0<= pipe_id <=5.
561  * @return	none.
562  */
rf_clr_tx_rptr(unsigned char pipe_id)563 static inline void rf_clr_tx_rptr(unsigned char pipe_id)
564 {
565 	reg_rf_dma_tx_rptr(pipe_id) = 0x80;
566 }
567 
568 /**
569  * @brief 	This function serve to get the pointer of read in rx terminal.
570  * @return	wptr	-	The pointer of rx_rptr.
571  */
rf_get_rx_rptr(void)572 static inline unsigned char rf_get_rx_rptr(void)
573 {
574 	return reg_rf_dma_rx_rptr;
575 }
576 
577 /**
578  * @brief	This function serve to clear read pointer of rx terminal.
579  * @return	none.
580  */
rf_clr_rx_rptr(void)581 static inline void rf_clr_rx_rptr(void)
582 {
583 	write_reg8(0x1004f5, 0x80); //clear rptr
584 }
585 
586 
587 /**
588  * @brief 	This function serve to get the pointer of write in rx terminal.
589  * @return	wptr	-	The pointer of rx_wptr.
590  */
rf_get_rx_wptr(void)591 static inline unsigned char rf_get_rx_wptr(void)
592 {
593 	return reg_rf_dma_rx_wptr;
594 }
595 
596 
597 /**
598  * @brief	This function serve to get ptx initial pid value.
599  * @return	The  value of ptx pid before update.
600  */
rf_get_ptx_pid(void)601 static inline unsigned char rf_get_ptx_pid(void)
602 {
603 	return ((reg_rf_ll_ctrl_1 & 0xc0)>>6);
604 }
605 
606 
607 /**
608  * @brief	This function serve to set the new ptx pid value.
609  * @param[in]	pipe_id	-The number of pipe.0<= pipe_id <=5.
610  * @return	none
611  */
rf_set_ptx_pid(unsigned char pipe_pid)612 static inline void rf_set_ptx_pid(unsigned char pipe_pid)
613 {
614 	reg_rf_ll_ctrl_1 |= (pipe_pid << 6);
615 }
616 
617 
618 /**
619  * @brief      This function serves to initiate information of RF.
620  * @return	   none.
621  */
622 void rf_mode_init(void);
623 
624 
625 /**
626  * @brief     This function serves to set ble_1M  mode of RF.
627  * @return	  none.
628  */
629 void rf_set_ble_1M_mode(void);
630 
631 
632 /**
633  * @brief     This function serves to  set ble_1M_NO_PN  mode of RF.
634  * @return	  none.
635  */
636 void rf_set_ble_1M_NO_PN_mode(void);
637 
638 
639 /**
640  * @brief     This function serves to set ble_2M  mode of RF.
641  * @return	  none.
642  */
643 void rf_set_ble_2M_mode(void);
644 
645 
646 /**
647  * @brief     This function serves to set ble_2M_NO_PN  mode of RF.
648  * @return	  none.
649  */
650 void rf_set_ble_2M_NO_PN_mode(void);
651 
652 
653 /**
654  * @brief     This function serves to set ble_500K  mode of RF.
655  * @return	  none.
656  */
657 void rf_set_ble_500K_mode(void);
658 
659 /**
660  * @brief     This function serves to set ble_1M  mode of RF.
661  * @return	  none.
662  */
663 void rf_set_ble_1M_mode(void);
664 
665 /**
666  * @brief     This function serves to  set zigbee_125K  mode of RF.
667  * @return	  none.
668  */
669 void rf_set_ble_125K_mode(void);
670 
671 
672 /**
673  * @brief     This function serves to set zigbee_250K  mode of RF.
674  * @return	  none.
675  */
676 void rf_set_zigbee_250K_mode(void);
677 
678 
679 /**
680  * @brief     This function serves to set pri_250K  mode of RF.
681  * @return	  none.
682  */
683 void rf_set_pri_250K_mode(void);
684 
685 
686 /**
687  * @brief     This function serves to  set pri_500K  mode of RF.
688  * @return	  none.
689  */
690 void rf_set_pri_500K_mode(void);
691 
692 
693 /**
694  * @brief     This function serves to set pri_1M  mode of RF.
695  * @return	  none.
696  */
697 void rf_set_pri_1M_mode(void);
698 
699 
700 /**
701  * @brief     This function serves to set pri_2M  mode of RF.
702  * @return	  none.
703  */
704 void rf_set_pri_2M_mode(void);
705 
706 
707 /**
708  * @brief     This function serves to set hybee_500K  mode of RF.
709  * @return	   none.
710  */
711 void rf_set_hybee_500K_mode(void);
712 
713 
714 /**
715  * @brief     This function serves to set hybee_2M  mode of RF.
716  * @return	  none.
717  */
718 void rf_set_hybee_2M_mode(void);
719 
720 
721 /**
722  * @brief     This function serves to set hybee_1M  mode of RF.
723  * @return	   none.
724  */
725 void rf_set_hybee_1M_mode(void);
726 
727 
728 
729 
730 
731 /**
732  * @brief     This function serves to set ant  mode of RF.
733  * @return	   none.
734  */
735 void rf_set_ant_mode(void);
736 
737 /**
738  * @brief     This function serves to set RF tx DMA setting.
739  * @param[in] none
740  * @return	  none.
741  */
742 void rf_set_tx_dma_config(void);
743 /**
744  * @brief     This function serves to set RF tx DMA setting.
745  * @param[in] fifo_depth  		- tx chn deep.
746  * @param[in] fifo_byte_size    - tx_idx_addr = {tx_chn_adr*bb_tx_size,4'b0}.
747  * @return	  none.
748  */
749 void rf_set_tx_dma(unsigned char fifo_depth,unsigned short fifo_byte_size);
750 
751 
752 /**
753  * @brief     This function serves to rx dma setting.
754  * @param[in] buff 		 	  - The buffer that store received packet.
755  * @param[in] wptr_mask  	  - DMA fifo mask value (0~fif0_num-1).
756  * @param[in] fifo_byte_size  - The length of one dma fifo.
757  * @return	  none.
758  */
759 void rf_set_rx_dma(unsigned char *buff,unsigned char wptr_mask,unsigned short fifo_byte_size);
760 
761 /**
762  * @brief		This function serve to rx dma config
763  * @param[in]	none
764  * @return		none
765  */
766 void rf_set_rx_dma_config(void);
767 
768 /**
769  * @brief     This function serves to trigger srx on.
770  * @param[in] tick  - Trigger rx receive packet after tick delay.
771  * @return	  none.
772  */
773 void rf_start_srx(unsigned int tick);
774 
775 
776 /**
777  * @brief	  	This function serves to get rssi.
778  * @return	 	rssi value.
779  */
780 signed char rf_get_rssi(void);
781 
782 
783 /**
784  * @brief	  	This function serves to set pin for RFFE of RF.
785  * @param[in]   tx_pin   - select pin as rffe to send.
786  * @param[in]   rx_pin   - select pin as rffe to receive.
787  * @return	 	none.
788  */
789 void rf_set_rffe_pin(rf_pa_tx_pin_e tx_pin, rf_lna_rx_pin_e rx_pin);
790 
791 
792 
793 /**
794  * @brief  	 	This function serves to set RF Tx mode.
795  * @return  	none.
796  */
797 void rf_set_txmode(void);
798 
799 
800 /**
801  * @brief	  	This function serves to set RF Tx packet address to DMA src_addr.
802  * @param[in]	addr   - The packet address which to send.
803  * @return	 	none.
804  */
805 _attribute_ram_code_sec_ void rf_tx_pkt(void* addr);
806 
807 
808 /**
809  * @brief	  	This function serves to judge RF Tx/Rx state.
810  * @param[in]	rf_status   - Tx/Rx status.
811  * @param[in]	rf_channel  - This param serve to set frequency channel(2400+rf_channel) .
812  * @return	 	Whether the setting is successful(-1:failed;else success).
813  */
814 int rf_set_trx_state(rf_status_e rf_status, signed char rf_channel);
815 
816 
817 /**
818  * @brief   	This function serves to set rf channel for all mode.The actual channel set by this function is 2400+chn.
819  * @param[in]   chn   - That you want to set the channel as 2400+chn.
820  * @return  	none.
821  */
822 void rf_set_chn(signed char chn);
823 
824 
825 /**
826  * @brief   	This function serves to set pri sb mode enable.
827  * @return  	none.
828  */
829 void rf_private_sb_en(void);
830 
831 
832 /**
833  * @brief   	This function serves to set pri sb mode payload length.
834  * @param[in]   pay_len  - In private sb mode packet payload length.
835  * @return  	none.
836  */
837 void rf_set_private_sb_len(int pay_len);
838 
839 
840 /**
841  * @brief   	This function serves to disable pn of ble mode.
842  * @return  	none.
843  */
844 void rf_pn_disable(void);
845 
846 
847 /**
848  * @brief   	This function serves to get the right fifo packet.
849  * @param[in]   fifo_num   - The number of fifo set in dma.
850  * @param[in]   fifo_dep   - deepth of each fifo set in dma.
851  * @param[in]   addr       - address of rx packet.
852  * @return  	the next rx_packet address.
853  */
854 unsigned char* rf_get_rx_packet_addr(int fifo_num,int fifo_dep,void* addr);
855 
856 
857 /**
858  * @brief   	This function serves to set RF power level.
859  * @param[in]   level 	 - The power level to set.
860  * @return 		none.
861  */
862 void rf_set_power_level (rf_power_level_e level);
863 
864 
865 /**
866  * @brief   	This function serves to set RF power through select the level index.
867  * @param[in]   idx 	 - The index of power level which you want to set.
868  * @return  	none.
869  */
870 void rf_set_power_level_index(rf_power_level_index_e idx);
871 
872 
873 /**
874  * @brief	  	This function serves to close internal cap.
875  * @return	 	none.
876  */
877 void rf_turn_off_internal_cap(void);
878 
879 
880 /**
881  * @brief	  	This function serves to update the value of internal cap.
882  * @param[in]  	value   - The value of internal cap which you want to set.
883  * @return	 	none.
884  */
885 void rf_update_internal_cap(unsigned char value);
886 
887 
888 /**
889  * @brief	  	This function serves to get RF status.
890  * @return	 	RF Rx/Tx status.
891  */
892 rf_status_e rf_get_trx_state(void);
893 
894 
895 /**
896  * @brief	This function serve to change the length of preamble.
897  * @param[in]	len		-The value of preamble length.Set the register bit<0>~bit<4>.
898  * @return		none
899  */
900 void rf_set_preamble_len(unsigned char len);
901 
902 /**
903  * @brief	This function serve to set the private ack enable,mainly used in prx/ptx.
904  * @param[in]	rf_mode		-	Must be one of the private mode.
905  * @return		none
906  */
907 void rf_set_pri_tx_ack_en(rf_mode_e rf_mode);
908 
909 
910 /**
911  * @brief	This function serve to set the length of access code.
912  * @param[in]	byte_len	-	The value of access code length.
913  * @return		none
914  */
915 void rf_set_access_code_len(unsigned char byte_len);
916 
917 /**
918  * @brief 	This function serve to set access code.This function will first get the length of access code from register 0x140805
919  * 			and then set access code in addr.
920  * @param[in]	pipe_id	-The number of pipe.0<= pipe_id <=5.
921  * @param[in]	acc	-The value access code
922  * @note		For compatibility with previous versions the access code should be bit transformed by bit_swap();
923  */
924 void rf_set_pipe_access_code (unsigned int pipe_id, unsigned char *addr);
925 
926 /**
927  * @brief   This function serves to set RF rx timeout.
928  * @param[in]	timeout_us	-	rx_timeout after timeout_us us,The maximum of this param is 0xfff.
929  * @return  none.
930  */
931 void rf_set_rx_timeout(unsigned short timeout_us);
932 
933 
934 /**
935  * @brief	This function serve to initial the ptx seeting.
936  * @return	none.
937  */
938 void rf_ptx_config(void);
939 
940 /**
941  * @brief	This function serve to initial the prx seeting.
942  * @return	none.
943  */
944 void rf_prx_config(void);
945 
946 /**
947  * @brief   This function serves to set RF ptx trigger.
948  * @param[in]	addr	-	The address of tx_packet.
949  * @param[in]	tick	-	Trigger ptx after (tick-current tick),If the difference is less than 0, trigger immediately.
950  * @return  none.
951  */
952 void rf_start_ptx  (void* addr,  unsigned int tick);
953 
954 /**
955  * @brief   This function serves to set RF prx trigger.
956  * @param[in]	tick	-	Trigger prx after (tick-current tick),If the difference is less than 0, trigger immediately.
957  * @return  none.
958  */
959 void rf_start_prx(unsigned int tick);
960 
961 
962 /**
963  * @brief	This function to set retransmit and retransmit delay.
964  * @param[in] 	retry_times	- Number of retransmit, 0: retransmit OFF
965  * @param[in] 	retry_delay	- Retransmit delay time.
966  * @return		none.
967  */
968 void rf_set_ptx_retry(unsigned char retry_times, unsigned short retry_delay);
969 
970 
971 /**
972  * @brief   This function serves to judge whether the FIFO is empty.
973  * @param pipe_id specify the pipe.
974  * @return TX FIFO empty bit.
975  * 			-#0 TX FIFO NOT empty.
976  * 			-#1 TX FIFO empty.
977  */
978 unsigned char rf_is_rx_fifo_empty(unsigned char pipe_id);
979 
980 
981 /**
982  * @brief     	This function serves to RF trigger stx
983  * @param[in] 	addr  	- DMA tx buffer.
984  * @param[in] 	tick  	- Send after tick delay.
985  * @return	   	none.
986  */
987 _attribute_ram_code_sec_noinline_ void rf_start_stx(void* addr, unsigned int tick);
988 
989 
990 /**
991  * @brief     	This function serves to RF trigger stx2rx
992  * @param[in] 	addr  	- DMA tx buffer.
993  * @param[in] 	tick  	- Send after tick delay.
994  * @return	    none.
995  */
996 _attribute_ram_code_sec_noinline_ void rf_start_stx2rx  (void* addr, unsigned int tick);
997 
998 
999 /**
1000  * @brief   	This function serves to set RF baseband channel.This function is suitable for ble open PN mode.
1001  * @param[in]   chn_num  - Bluetooth channel set according to Bluetooth protocol standard.
1002  * @return  	none.
1003  */
1004 _attribute_ram_code_sec_noinline_ void rf_set_ble_chn(signed char chn_num);
1005 
1006 
1007 
1008 /**
1009  * @brief   	This function serves to set RF Rx manual on.
1010  * @return  	none.
1011  */
1012 _attribute_ram_code_sec_noinline_ void rf_set_rxmode(void);
1013 
1014 
1015 /**
1016  * @brief	  	This function serves to start Rx of auto mode. In this mode,
1017  *				RF module stays in Rx status until a packet is received or it fails to receive packet when timeout expires.
1018  *				Timeout duration is set by the parameter "tick".
1019  *				The address to store received data is set by the function "addr".
1020  * @param[in]	addr   - The address to store received data.
1021  * @param[in]	tick   - It indicates timeout duration in Rx status.Max value: 0xffffff (16777215)
1022  * @return	 	none
1023  */
1024 _attribute_ram_code_sec_noinline_ void rf_start_brx  (void* addr, unsigned int tick);
1025 
1026 
1027 /**
1028  * @brief	  	This function serves to start tx of auto mode. In this mode,
1029  *				RF module stays in tx status until a packet is sent or it fails to sent packet when timeout expires.
1030  *				Timeout duration is set by the parameter "tick".
1031  *				The address to store send data is set by the function "addr".
1032  * @param[in]	addr   - The address to store send data.
1033  * @param[in]	tick   - It indicates timeout duration in Rx status.Max value: 0xffffff (16777215)
1034  * @return	 	none
1035  */
1036 _attribute_ram_code_sec_noinline_ void rf_start_btx (void* addr, unsigned int tick);
1037 
1038 /**
1039  * @brief   	This function serves to set RF power through select the level index.
1040  * @param[in]   idx 	 - The index of power level which you want to set.
1041  * @return  	none.
1042  */
1043 void rf_set_power_level_index(rf_power_level_index_e idx);
1044 
1045 /**
1046  * @brief		This function do radio baseband reset
1047  * @param 		none
1048  * @return		none
1049  */
1050 void rf_baseband_reset(void);
1051 
1052 #endif
1053