Files
kunlun/plc/halmac/inc/mac_ppm_scan.h
2024-09-28 14:24:04 +08:00

418 lines
12 KiB
C
Executable File

/****************************************************************************
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.
****************************************************************************/
#ifndef MAC_PPM_SCAN_H
#define MAC_PPM_SCAN_H
#include "os_types.h"
#include "os_timer_api.h"
#include "iot_config.h"
#include "plc_scan_tbl.h"
#include "mpdu_header.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MAC_PPM_RECORD_NODE_COUNT 16
/* unit ntb */
#define MAC_PPM_RECORD_REFRESH_INTERVAL 1500000000
/* unit ms */
#define MAC_PPM_RECORD_CHECK_INTERVAL 30000
#define MAC_PPM_RECORD_BITMAP_GROUP iot_ceil(MAC_PPM_RECORD_NODE_COUNT, 8)
typedef struct _mac_ntb_record_ctxt {
uint64_t last_saved_ntb;
/* if joined in the network, last pco's bcn ntb time stamp */
uint32_t last_bcn_remote_tx_ntb;
uint32_t last_bcn_local_ts;
uint32_t last_bcn_local_ntb;
} mac_ntb_record_ctxt_t;
typedef struct _mac_ppm_node {
/* record ntb record ctxt */
mac_ntb_record_ctxt_t record_ctxt;
/* record ppm error, accuracy rate is 1/(1 << PLC_NTB_PPM_SHIFT) ppm */
int16_t ppm;
/* source tei indicate this struct variate is used for recording which
* device's ppm err information
*/
uint16_t src_tei;
} mac_ppm_node_t;
typedef struct _mac_ppm_record {
/* used for record each device's ppm err information */
mac_ppm_node_t node[MAC_PPM_RECORD_NODE_COUNT];
/* used for node */
uint8_t bitmap[MAC_PPM_RECORD_BITMAP_GROUP];
/* if is timer start,1--start,0--stop */
uint8_t is_start;
/* last saved timestamp, unit ms */
uint32_t last_saved_ms;
} mac_ppm_record_t;
/* this is per vdev, for ppm sync */
typedef struct _mac_ppm_ctxt {
mac_ntb_record_ctxt_t ntb_record;
/* flag */
uint32_t is_sync_bb_ppm_to_ntb :1,
is_inited :1,
resv0 :30;
/* mac ppm */
int16_t ntb_ppm;
/* bb ppm */
int16_t bb_ppm;
/* ntb ppm update count */
uint32_t ntb_ppm_update_cnt;
#if PPM_NTB_CAL
/* mac ntb adjust timestamp */
uint32_t adj_ntb_ts;
/* mac ntb adjust decimal calculate accumulation */
int32_t adj_ntb_calc_accum;
#endif
#if MAC_PPM_MULTI_NODE_ENABLE
/* record ppm information */
mac_ppm_record_t record_info;
#endif
} mac_ppm_ctxt_t;
/* this is per pdev, for band scan */
typedef struct _mac_scan_ctxt {
/* which index is on */
uint8_t scan_idx;
/* means band selected by upper layer */
uint8_t band_selected : 1,
/* first scan band done or not */
is_first_scan_done : 1,
/* 1 - scan start, 0 - scan stop */
is_started : 1,
/* indicate current band rx packet or not */
is_rx_packet : 1,
/* reserved */
resv : 4;
/* reserved */
uint8_t rsvd1;
/* scan info table count */
uint8_t scan_count;
/* scan band start timestamp, unit: 1ms */
uint32_t start_ts;
/* scan band info */
plc_scan_band_t scan_info[PLC_SCAN_BAND_MAX_CNT];
} mac_scan_ctxt_t;
/**
*@brief mac_scan_get_support_band_tbl
*
* get mac/phy support band table infomation
*
*@param proto [protocol type id]
*@param size [band_info count size]
*@param band_info [get band info buffer address]
*@return [get support band count]
*/
uint32_t mac_scan_get_support_band_tbl(uint32_t proto, uint32_t size,
plc_scan_band_t *band_info);
/**
*@brief mac_scan_set_band_info
*
* set mac scan band table infomation
*
*@param proto [protocol type id]
*@param size [band_info count size]
*@param band_info [set band info buffer address]
*@return [error code, see ERR_XXX]
*/
uint32_t mac_scan_set_band_info(uint32_t proto, uint32_t size,
plc_scan_band_t *band_info);
/**
*@brief mac_scan_get_band_info
*
* get current mac scan band table infomation
*
*@param proto [protocol type id]
*@param size [band_info count size]
*@param band_info [get band info buffer address]
*@return [get scan band count]
*/
uint32_t mac_scan_get_band_info(uint32_t proto, uint32_t size,
plc_scan_band_t *band_info);
/**
*@brief mac_nid_ppm_reset_msg_handler mac handle timer msg,
*@return NULL
*/
void mac_nid_ppm_reset_msg_handler();
/**
*@brief mac_scan_set_ntb_ppm
*
* set ntb's ppm to remote peer, it would be updated every ms
*
*@param ppm_ctxt [phy scan context]
*@param ppm [value of ppm, positive means need go faster]
*@param rate_mode [rate mode of base band]
*@exception [none.]
*@return [none.]
*/
void mac_scan_set_ntb_ppm(mac_ppm_ctxt_t *ppm_ctxt, int16_t ppm,
uint32_t rate_mode);
/**
*@brief mac_scan_get_ntb_ppm
*
* get ntb's ppm to remote peer
*
*@param ppm_ctxt [phy scan context]
*@exception [none.]
*@return [none.]
*/
int16_t mac_scan_get_ntb_ppm(mac_ppm_ctxt_t *ppm_ctxt);
/**
*@brief mac_scan_set_bb_ppm
*
* set bb's ppm to remote peer, it would be updated immediately
*
*@param ppm_ctxt [phy scan context]
*@param ppm [value of ppm, positive means need go faster]
*@param rate_mode [rate mode of base band]
*@param update_flag [update tx/rx ppm flag]
*@exception [none.]
*@return [none.]
*/
void mac_scan_set_bb_ppm(mac_ppm_ctxt_t *ppm_ctxt, \
int16_t ppm, uint32_t rate_mode, uint8_t update_flag);
/**
*@brief mac_scan_get_bb_ppm
*
* get bb's ppm to remote peer
*
*@param ppm_ctxt [phy scan context]
*@exception [none.]
*@return [none.]
*/
int16_t mac_scan_get_bb_ppm(mac_ppm_ctxt_t *ppm_ctxt);
/**
*@brief @brief mac_ntb_sync_ppm_from_isr
* systic callback for ppm sync
*@return [none.]
*/
void mac_ntb_sync_ppm_from_isr();
/**
*@brief mac_ntb_ppm_sync
*
* calculate ntb's ppm to remote peer, it would be updated immediately
* and sync to base band's ppm also, if configured
*
*@param vdev [vdev context]
*@param rate_mode [rate mode of base band]
*@param remote_ts [remote peer's ntb]
*@param local_ts [local time stamp of cur pkt rx]
*@param local_ntb [local ntb time stamp of cur pkt rx]
*@param ppm_hw [value of ppm_hw, reported from hw desc]
*@param ntb_ppm [returned ntb_ppm calculated result]
*@param bcn_period_ms [beacon period ms]
*@param is_vaild_tei [is vaild tei]
*@exception [none.]
*@return [none.]
*/
uint32_t mac_ntb_ppm_sync(mac_ppm_ctxt_t *ppm_ctxt, uint32_t rate_mode,
uint32_t remote_ts, uint32_t local_ts, uint32_t local_ntb,
int16_t ppm_hw, int16_t *ntb_ppm, uint32_t bcn_period_ms,
bool_t is_vaild_tei);
/**
*@brief mac_scan_start
*
* start mac scan process, search for band, ppm of CCo
*
*@param scan [phy scan context]
*@exception [none.]
*@return [none.]
*/
uint32_t mac_scan_start(mac_scan_ctxt_t *scan);
/**
*@brief mac_scan_stop
*
* stop mac scan process, usually found a candidate
*
*@param scan [phy scan context]
*@exception [none.]
*@return [none.]
*/
uint32_t mac_scan_stop(mac_scan_ctxt_t *scan);
/**
* @brief mac_scan_set_band_selected
* @param scan [phy scan context]
* @param is_sel [1: selected done; 0: not selected, need scan]
* @return [ERR_OK or else fail]
*/
uint32_t mac_scan_set_band_selected(mac_scan_ctxt_t *scan, uint32_t is_sel);
/**
* @brief mac_scan_set_band_selected
* @param scan [phy scan context]
* @return [1: selected done; 0: not selected, need scan]
*/
uint32_t mac_scan_get_band_selected(mac_scan_ctxt_t *scan);
/**
* @brief mac_scan_update_rx_status
* @param scan [mac scan context address]
* @param fc_msg [fc msg context address]
* @param band_id [rx packet band id]
* @return [none]
*/
void mac_scan_update_rx_status(mac_scan_ctxt_t *scan, rx_fc_msg_t *fc_msg,
uint8_t band_id);
/**
*@brief mac_scan_init
*
* init the scan context in pdev
*
*@param pdev_in [phy dev pointer]
*@param ppm_init [ppm init value, positive means go faster]
*@param is_reinit [is re-init, not the first time init]
*@exception [none.]
*@return [none.]
*/
void mac_scan_init(void *pdev_in, uint8_t is_reinit);
/*
* @brief mac_ppm_init
* init the ppm context,
* this is per vdev
*/
uint32_t mac_ppm_init(mac_ppm_ctxt_t *ppm, int16_t ppm_init,
uint32_t rate_mode);
void mac_ppm_reset_counter(mac_ntb_record_ctxt_t *record);
/**
*@brief mac_ppm_cal
*
* calculate ntb's ppm to remote peer, it would be updated immediately
* and sync to base band's ppm also, if configured
*
*@param ntb_ctxt [ntb context]
*@param remote_ts [remote peer's ntb]
*@param local_ts [local time stamp of cur pkt rx]
*@param local_ntb [local ntb of cur pkt rx]
*@param bcn_period_ms [current beacon period, unit: 1ms]
*@exception [none.]
*@return [ppm]
*/
int16_t mac_ppm_cal(mac_ntb_record_ctxt_t *ntb_ctxt, uint32_t remote_ts,
uint32_t local_ts, uint32_t local_ntb, uint32_t bcn_period_ms);
#if MAC_PPM_MULTI_NODE_ENABLE
/**
*@brief mac_ppm_record_refresh_handle
*
* refresh the ppm record space
*
*@param arg [arguments,point to ppm_ctxt]
*@exception [none.]
*@return [none.]
*/
void mac_ppm_record_refresh_handle(mac_ppm_ctxt_t *arg);
/**
*@brief mac_ppm_record_timer_start
*
* start ppm record timer
*
*@param ppm_ctxt [point to the mac_ppm_ctxt_t variable witch will
record the ppm data]
*@exception [none.]
*@return [none.]
*/
void mac_ppm_record_timer_start(mac_ppm_ctxt_t *ppm_ctxt);
/**
*@brief mac_ppm_record_timer_stop
*
* stop ppm record timer
*
*@param ppm_ctxt [point to the mac_ppm_ctxt_t variable witch will
record the ppm data]
*@exception [none.]
*@return [none.]
*/
void mac_ppm_record_timer_stop(mac_ppm_ctxt_t *ppm_ctxt);
/**
*@brief mac_ppm_record_handle
*
* caculate and record each pco's ppm
*
*@param vdev_input [point to vedv]
*@param nid [net id]
*@param rx_fc_msg [reecive frame control message]
*@param local_ts [local timestamp, unit ntb]
*@param ntb_ts [ntb timestamp, unit ntb]
*@param proto [see PLC_PROTO_TYPE_XXX]
*@exception [none.]
*@return [none.]
*/
void mac_ppm_record_handle(void *vdev_input, uint32_t nid,
rx_fc_msg_t *rx_fc_msg, uint32_t local_ts, uint32_t ntb_ts, uint32_t proto);
#else/* MAC_PPM_MULTI_NODE_ENABLE */
#define mac_ppm_record_timer_start(ppm_ctxt) NULL; \
do { \
(void)ppm_ctxt; \
} while(0)
#define mac_ppm_record_timer_stop(ppm_ctxt) NULL; \
do { \
(void)ppm_ctxt; \
} while(0)
#define mac_ppm_record_refresh_handle(arg) \
do { \
(void)arg; \
} while(0)
#define mac_ppm_record_handle(vdev_input, nid, rx_fc_msg, local_ts, ntb_ts, \
proto) \
do { \
(void)vdev_input; (void)nid; (void)rx_fc_msg; (void)local_ts; \
(void)ntb_ts; (void)proto; \
} while(0)
#endif/* MAC_PPM_MULTI_NODE_ENABLE */
#ifdef __cplusplus
}
#endif
#endif // !MAC_PPM_SCAN_H