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