175 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			175 lines
		
	
	
		
			5.3 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 "iot_errno.h"
							 | 
						||
| 
								 | 
							
								#include "iot_utils.h"
							 | 
						||
| 
								 | 
							
								#include "iot_system.h"
							 | 
						||
| 
								 | 
							
								#include "iot_io.h"
							 | 
						||
| 
								 | 
							
								#include "plc_utils.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "mac_rf.h"
							 | 
						||
| 
								 | 
							
								#include "mac_vdev.h"
							 | 
						||
| 
								 | 
							
								#include "phy_bb.h"
							 | 
						||
| 
								 | 
							
								#include "plc_beacon.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#if HPLC_RF_CHANNEL_SUPPORT
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "phy_rf_chn.h"
							 | 
						||
| 
								 | 
							
								#include "phy_rf_init.h"
							 | 
						||
| 
								 | 
							
								#if HPLC_RF_DEV_SUPPORT
							 | 
						||
| 
								 | 
							
								#include "mac_rf_common_hw.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_sched_hw.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_pdev.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_sched.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_isr.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_tx_hw.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_rx_hw.h"
							 | 
						||
| 
								 | 
							
								#include "rf_hw_tonemap.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_rx_buf_ring.h"
							 | 
						||
| 
								 | 
							
								#include "mac_rf_txq_hw.h"
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								mac_rf_t *mac_rf_init()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    mac_rf_t *tmp = os_mem_malloc(PLC_MAC_RF_MID,sizeof(mac_rf_t));
							 | 
						||
| 
								 | 
							
								    IOT_ASSERT(tmp);
							 | 
						||
| 
								 | 
							
								#if HPLC_RF_DEV_SUPPORT
							 | 
						||
| 
								 | 
							
								    tmp->rf_channel = mac_rf_get_tobe_set_channel_id();
							 | 
						||
| 
								 | 
							
								    tmp->rf_option = mac_rf_get_option();
							 | 
						||
| 
								 | 
							
								#else
							 | 
						||
| 
								 | 
							
								    uint8_t option, channel;
							 | 
						||
| 
								 | 
							
								    phy_rf_get_default_cfg(&option, &channel);
							 | 
						||
| 
								 | 
							
								    tmp->rf_channel = channel;
							 | 
						||
| 
								 | 
							
								    tmp->rf_option = option;
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								    g_phy_cpu_share_ctxt.rf_channel = tmp->rf_channel;
							 | 
						||
| 
								 | 
							
								    g_phy_cpu_share_ctxt.rf_option = tmp->rf_option;
							 | 
						||
| 
								 | 
							
								    return tmp;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void mac_rf_set_option_channel(uint8_t rf_option, uint8_t rf_channel)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    (void)rf_option;
							 | 
						||
| 
								 | 
							
								    (void)rf_channel;
							 | 
						||
| 
								 | 
							
								#if HPLC_RF_DEV_SUPPORT
							 | 
						||
| 
								 | 
							
								    uint32_t proto = PHY_PROTO_TYPE_GET();
							 | 
						||
| 
								 | 
							
								    mac_rf_pdev_t *rf_pdev = get_rf_pdev_ptr(PLC_PDEV_ID, RF_PDEV_ID);
							 | 
						||
| 
								 | 
							
								    mac_rf_sched_enable_bp(NULL, 0);
							 | 
						||
| 
								 | 
							
								    /* rf disable rx ring */
							 | 
						||
| 
								 | 
							
								    mac_rf_rx_all_ring_disable(rf_pdev);
							 | 
						||
| 
								 | 
							
								    /* flush all tdma/csma queue */
							 | 
						||
| 
								 | 
							
								    mac_rf_tx_flush_all_queue(&rf_pdev->hwq_hdl);
							 | 
						||
| 
								 | 
							
								    /* plc cpu and bbcpu sync init, rf rxdc enable */
							 | 
						||
| 
								 | 
							
								    phy_rf_init(proto, rf_option, rf_channel, 1, 0, 0);
							 | 
						||
| 
								 | 
							
								    /* rf isr init */
							 | 
						||
| 
								 | 
							
								    mac_rf_isr_init(mac_rf_get_dsr_callback());
							 | 
						||
| 
								 | 
							
								    /* init rf pdev */
							 | 
						||
| 
								 | 
							
								    mac_rf_pdev_init(RF_PDEV_ID);
							 | 
						||
| 
								 | 
							
								    /* rf enable rx ring */
							 | 
						||
| 
								 | 
							
								    mac_rf_rx_all_ring_enable(rf_pdev);
							 | 
						||
| 
								 | 
							
								    /* start isr */
							 | 
						||
| 
								 | 
							
								    mac_rf_isr_start();
							 | 
						||
| 
								 | 
							
								    /* trigger cmdlist */
							 | 
						||
| 
								 | 
							
								    mac_rf_sched_trigger_bp(NULL);
							 | 
						||
| 
								 | 
							
								    mac_rf_sched_enable_bp(NULL, 1);
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void mac_rf_set_self_option_channel(uint8_t rf_option, uint8_t rf_channel)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    mac_vdev_t *vdev = get_vdev_ptr(PLC_PDEV_ID, PLC_DEFAULT_VDEV);
							 | 
						||
| 
								 | 
							
								    mac_rf_t *mac_rf = vdev->mac_rf;
							 | 
						||
| 
								 | 
							
								    IOT_ASSERT(mac_rf);
							 | 
						||
| 
								 | 
							
								    /* check option */
							 | 
						||
| 
								 | 
							
								    if (rf_option >= PHY_RF_OPTION_MAX ||
							 | 
						||
| 
								 | 
							
								        rf_option < PHY_RF_OPTION1_1M) {
							 | 
						||
| 
								 | 
							
								        iot_printf("%s set option err: set option:%d, cur option:%d\n",
							 | 
						||
| 
								 | 
							
								            __FUNCTION__, rf_option, mac_rf_get_self_option());
							 | 
						||
| 
								 | 
							
								        rf_option = mac_rf_get_self_option();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    /* check channel */
							 | 
						||
| 
								 | 
							
								    if (rf_channel >= phy_rf_get_channel_id_max(rf_option) ||
							 | 
						||
| 
								 | 
							
								        rf_channel < RF_CHANNEL_ID_1) {
							 | 
						||
| 
								 | 
							
								        iot_printf("%s set channel err, set channel:%d, cur channel:%d,"
							 | 
						||
| 
								 | 
							
								            " set option:%d, cur option:%d\n",
							 | 
						||
| 
								 | 
							
								            __FUNCTION__, rf_channel, mac_rf->rf_channel,
							 | 
						||
| 
								 | 
							
								            rf_option, mac_rf_get_self_option());
							 | 
						||
| 
								 | 
							
								        return;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    uint8_t cur_option = mac_rf_get_self_option();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    iot_printf("%s, cur option:%d, channel:%d, set option:%d, channel:%d\n",
							 | 
						||
| 
								 | 
							
								        __FUNCTION__, cur_option, mac_rf->rf_channel, rf_option, rf_channel);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (cur_option != rf_option) {
							 | 
						||
| 
								 | 
							
								        /* update option/channel to mac */
							 | 
						||
| 
								 | 
							
								        mac_rf_set_option_channel(rf_option, rf_channel);
							 | 
						||
| 
								 | 
							
								        mac_rf->rf_option = rf_option;
							 | 
						||
| 
								 | 
							
								        mac_rf->rf_channel = rf_channel;
							 | 
						||
| 
								 | 
							
								        g_phy_cpu_share_ctxt.rf_channel = rf_channel;
							 | 
						||
| 
								 | 
							
								        g_phy_cpu_share_ctxt.rf_option = rf_option;
							 | 
						||
| 
								 | 
							
								    } else if (rf_channel != mac_rf->rf_channel) {
							 | 
						||
| 
								 | 
							
								        mac_rf->rf_channel = rf_channel;
							 | 
						||
| 
								 | 
							
								        g_phy_cpu_share_ctxt.rf_channel = rf_channel;
							 | 
						||
| 
								 | 
							
								        g_phy_cpu_share_ctxt.rf_option = rf_option;
							 | 
						||
| 
								 | 
							
								        /* set channel to bbcpu */
							 | 
						||
| 
								 | 
							
								        phy_rf_set_channel(rf_channel);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t mac_rf_get_self_channel()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    mac_vdev_t *vdev = get_vdev_ptr(PLC_PDEV_ID, PLC_DEFAULT_VDEV);
							 | 
						||
| 
								 | 
							
								    mac_rf_t *mac_rf = vdev->mac_rf;
							 | 
						||
| 
								 | 
							
								    IOT_ASSERT(mac_rf);
							 | 
						||
| 
								 | 
							
								    return (uint8_t)mac_rf->rf_channel;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t mac_rf_get_self_option()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    mac_vdev_t *vdev = get_vdev_ptr(PLC_PDEV_ID, PLC_DEFAULT_VDEV);
							 | 
						||
| 
								 | 
							
								    mac_rf_t *mac_rf = vdev->mac_rf;
							 | 
						||
| 
								 | 
							
								    IOT_ASSERT(mac_rf);
							 | 
						||
| 
								 | 
							
								    return (uint8_t)mac_rf->rf_option;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#else /* HPLC_RF_CHANNEL_SUPPORT */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								mac_rf_t *mac_rf_init()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    return NULL;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void mac_rf_set_self_option_channel(uint8_t rf_option, uint8_t rf_channel)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    (void)rf_option;
							 | 
						||
| 
								 | 
							
								    (void)rf_channel;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t mac_rf_get_self_channel()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    return RF_CHANNEL_ID_INVALID;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t mac_rf_get_self_option()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    return RF_OPTION_INVALID;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* HPLC_RF_CHANNEL_SUPPORT */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 |