194 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			194 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #include "iot_config.h"
 | ||
|  | #include "os_types.h"
 | ||
|  | #include "plc_beacon.h"
 | ||
|  | #include "hw_reg_api.h"
 | ||
|  | #include "hw_phy_api.h"
 | ||
|  | #include "phy_rf_chn.h"
 | ||
|  | #include "iot_io.h"
 | ||
|  | 
 | ||
|  | /* phy rf band info structure */ | ||
|  | typedef struct { | ||
|  |     /* current band */ | ||
|  |     PHY_RF_BAND_T cur_band; | ||
|  |     /* the maximum channel number corresponding to each option in the
 | ||
|  |      * current band. | ||
|  |      */ | ||
|  |     uint8_t ch_max[RF_OPTION_MAX + 1]; | ||
|  |     /* the default option for the current band */ | ||
|  |     uint8_t default_opt; | ||
|  |     /* the default channel for the current band */ | ||
|  |     uint8_t default_ch; | ||
|  |     /* rf tx power min value */ | ||
|  |     int8_t rf_tx_pwr_min; | ||
|  |     /* rf tx power max value */ | ||
|  |     int8_t rf_tx_pwr_max; | ||
|  |     /* rf tx power default value */ | ||
|  |     int8_t rf_tx_pwr_def; | ||
|  |     /* rf tx full power value */ | ||
|  |     int8_t rf_tx_full_pwr; | ||
|  |     /* flasg to mark if initialized */ | ||
|  |     uint8_t inited : 1, | ||
|  | 
 | ||
|  |     /* reserved for further use */ | ||
|  |             rsvd   : 7; | ||
|  | } phy_rf_band_info_t; | ||
|  | 
 | ||
|  | static phy_rf_band_info_t g_phy_rf_band_info = { 0 }; | ||
|  | 
 | ||
|  | void phy_rf_band_info_init(PHY_RF_BAND_T band_sel) | ||
|  | { | ||
|  |     if (band_sel == PHY_RF_BAND_OVERSEAS) { | ||
|  |         g_phy_rf_band_info.ch_max[PHY_RF_OPTION1_1M - 1] = | ||
|  |             PHY_RF_BAND_OVERSEAS_OPT1_CH_ID_MAX; | ||
|  |         g_phy_rf_band_info.ch_max[PHY_RF_OPTION2_500K - 1] = | ||
|  |             PHY_RF_BAND_OVERSEAS_OPT2_CH_ID_MAX; | ||
|  |         g_phy_rf_band_info.ch_max[PHY_RF_OPTION3_200K - 1] = | ||
|  |             PHY_RF_BAND_OVERSEAS_OPT3_CH_ID_MAX; | ||
|  |         g_phy_rf_band_info.default_ch = 51; | ||
|  |         g_phy_rf_band_info.default_opt = RF_OPTION_2; | ||
|  |         g_phy_rf_band_info.rf_tx_pwr_min = RF_TX_PWR_MIN_DBM_OVERSEAS; | ||
|  |         g_phy_rf_band_info.rf_tx_pwr_max = RF_TX_PWR_MAX_DBM_OVERSEAS; | ||
|  |         g_phy_rf_band_info.rf_tx_pwr_def = RF_TX_PWR_DEF_DBM_OVERSEAS; | ||
|  |         g_phy_rf_band_info.rf_tx_full_pwr = RF_TX_FULL_PWR_DBM_OVERSEAS; | ||
|  |     } else if (band_sel == PHY_RF_BAND_NSG) { | ||
|  |         g_phy_rf_band_info.ch_max[PHY_RF_OPTION1_1M - 1] = | ||
|  |             PHY_RF_BAND_NSG_OPT1_CH_ID_MAX; | ||
|  |         g_phy_rf_band_info.ch_max[PHY_RF_OPTION2_500K - 1] = | ||
|  |             PHY_RF_BAND_NSG_OPT2_CH_ID_MAX; | ||
|  |         g_phy_rf_band_info.ch_max[PHY_RF_OPTION3_200K - 1] = | ||
|  |             PHY_RF_BAND_NSG_OPT3_CH_ID_MAX; | ||
|  |         g_phy_rf_band_info.default_ch = RF_CHANNEL_ID_16; | ||
|  |         g_phy_rf_band_info.default_opt = RF_OPTION_2; | ||
|  |         g_phy_rf_band_info.rf_tx_pwr_min = RF_TX_PWR_MIN_DBM_NSG; | ||
|  |         g_phy_rf_band_info.rf_tx_pwr_max = RF_TX_PWR_MAX_DBM_NSG; | ||
|  |         g_phy_rf_band_info.rf_tx_pwr_def = RF_TX_PWR_DEF_DBM_NSG; | ||
|  |         g_phy_rf_band_info.rf_tx_full_pwr = RF_TX_FULL_PWR_DBM_NSG; | ||
|  |     } else { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     g_phy_rf_band_info.cur_band = band_sel; | ||
|  |     g_phy_rf_band_info.inited = 1; | ||
|  |     iot_printf("%s band_sel = %lu\n", __FUNCTION__, | ||
|  |         g_phy_rf_band_info.cur_band); | ||
|  | } | ||
|  | 
 | ||
|  | PHY_RF_BAND_T phy_rf_get_band_sel() | ||
|  | { | ||
|  |     if (g_phy_rf_band_info.inited == 0) { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     return g_phy_rf_band_info.cur_band; | ||
|  | } | ||
|  | 
 | ||
|  | void phy_rf_get_power(int8_t *min, int8_t *max, int8_t *def, int8_t *full) | ||
|  | { | ||
|  |     if (g_phy_rf_band_info.inited == 0) { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     if (min != NULL) { | ||
|  |         *min = g_phy_rf_band_info.rf_tx_pwr_min; | ||
|  |     } | ||
|  |     if (max != NULL) { | ||
|  |         *max = g_phy_rf_band_info.rf_tx_pwr_max; | ||
|  |     } | ||
|  |     if (def != NULL) { | ||
|  |         *def = g_phy_rf_band_info.rf_tx_pwr_def; | ||
|  |     } | ||
|  |     if (full != NULL) { | ||
|  |         *full = g_phy_rf_band_info.rf_tx_full_pwr; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void phy_rf_get_default_cfg(uint8_t *opt, uint8_t *ch) | ||
|  | { | ||
|  |     if (g_phy_rf_band_info.inited == 0) { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     *opt = g_phy_rf_band_info.default_opt; | ||
|  |     *ch = g_phy_rf_band_info.default_ch; | ||
|  | } | ||
|  | 
 | ||
|  | uint32_t phy_rf_get_channel_id_max(uint32_t option) | ||
|  | { | ||
|  |     if (g_phy_rf_band_info.inited == 0) { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     return g_phy_rf_band_info.ch_max[option - 1]; | ||
|  | } | ||
|  | 
 | ||
|  | uint32_t phy_rf_get_channel_freq_by_id(uint32_t option, uint32_t channel_id) | ||
|  | { | ||
|  |     uint32_t freq = 0; | ||
|  |     uint32_t spacing = 0; | ||
|  |     if (g_phy_rf_band_info.inited == 0) { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     if (g_phy_rf_band_info.cur_band == PHY_RF_BAND_OVERSEAS) { | ||
|  |         if (PHY_RF_OPTION1_1M == option) { | ||
|  |             spacing = OPTION1_CHANNEL_SPACING; | ||
|  |             if (channel_id > 12) { | ||
|  |                 channel_id -= 12; | ||
|  |                 freq = PHY_RF_BAND_OVERSEAS_OPT1_CH13_CF; | ||
|  |             } else { | ||
|  |                 freq = PHY_RF_BAND_OVERSEAS_OPT1_CH1_CF; | ||
|  |             } | ||
|  |         } else if (PHY_RF_OPTION2_500K == option) { | ||
|  |             spacing = OPTION2_CHANNEL_SPACING; | ||
|  |             if (channel_id > 25) { | ||
|  |                 channel_id -= 25; | ||
|  |                 freq = PHY_RF_BAND_OVERSEAS_OPT2_CH26_CF; | ||
|  |             } else { | ||
|  |                 freq = PHY_RF_BAND_OVERSEAS_OPT2_CH1_CF; | ||
|  |             } | ||
|  |         } else if (PHY_RF_OPTION3_200K == option) { | ||
|  |             spacing = OPTION3_CHANNEL_SPACING; | ||
|  |             if (channel_id > 64) { | ||
|  |                 channel_id -= 64; | ||
|  |                 freq = PHY_RF_BAND_OVERSEAS_OPT3_CH65_CF; | ||
|  |             } else { | ||
|  |                 freq = PHY_RF_BAND_OVERSEAS_OPT3_CH1_CF; | ||
|  |             } | ||
|  |         } else { | ||
|  |             IOT_ASSERT(0); | ||
|  |         } | ||
|  |     } else if (g_phy_rf_band_info.cur_band  == PHY_RF_BAND_NSG) { | ||
|  |         if (PHY_RF_OPTION1_1M == option) { | ||
|  |             spacing = OPTION1_CHANNEL_SPACING; | ||
|  |             freq = PHY_RF_BAND_NSG_OPT1_CH1_CF; | ||
|  |         } else if (PHY_RF_OPTION2_500K == option) { | ||
|  |             spacing = OPTION2_CHANNEL_SPACING; | ||
|  |             freq = PHY_RF_BAND_NSG_OPT2_CH1_CF; | ||
|  |         } else if (PHY_RF_OPTION3_200K == option) { | ||
|  |             spacing = OPTION3_CHANNEL_SPACING; | ||
|  |             freq = PHY_RF_BAND_NSG_OPT3_CH1_CF; | ||
|  |         } else { | ||
|  |             IOT_ASSERT(0); | ||
|  |         } | ||
|  |     } else { | ||
|  |         IOT_ASSERT(0); | ||
|  |     } | ||
|  |     return (freq + (channel_id - 1) * spacing); | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t phy_rf_check_rf_version(uint8_t ver) | ||
|  | { | ||
|  |     if (ver == RF_VER_1 || ver == RF_VER_2 || ver == RF_VER_3) | ||
|  |         return 1; | ||
|  |     else | ||
|  |         return 0; | ||
|  | } |