200 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			200 lines
		
	
	
		
			6.1 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.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								****************************************************************************/
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifndef TOPO_ADC_ADP_H
							 | 
						||
| 
								 | 
							
								#define TOPO_ADC_ADP_H
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* os shim includes */
							 | 
						||
| 
								 | 
							
								#include "os_types.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "apb_dma.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								extern "C" {
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* define hw topo working mode */
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_MODE_NOR                        0
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_MODE_PA                         1
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_MODE_INVALID                    2
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* phase definition */
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_PHASE_ALL                       0
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_PHASE_A                         1
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_PHASE_B                         2
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_PHASE_C                         3
							 | 
						||
| 
								 | 
							
								#define IOT_HW_TOPO_PHASE_CNT                       3
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* define the maximum samples number for per power line period */
							 | 
						||
| 
								 | 
							
								#define IOT_PLC_HW_TOPO_SAMP_NUM_MAX_PER_PERIOD     384
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* defines the gap for automatic gain adjustment, uint is 1ms */
							 | 
						||
| 
								 | 
							
								#define IOT_PLC_HW_TOPO_AGC_ADJ_GAP                 50
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* adc adapter config descriptor */
							 | 
						||
| 
								 | 
							
								typedef struct _topo_adc_adp_cfg {
							 | 
						||
| 
								 | 
							
								    /* topo work mode, see IOT_HW_TOPO_MODE_XXX */
							 | 
						||
| 
								 | 
							
								    uint8_t mode;
							 | 
						||
| 
								 | 
							
								    /* is vcm gnd */
							 | 
						||
| 
								 | 
							
								    uint8_t is_vcm_gnd;
							 | 
						||
| 
								 | 
							
								    /* is agc enable */
							 | 
						||
| 
								 | 
							
								    uint8_t agc_en;
							 | 
						||
| 
								 | 
							
								} topo_adc_adp_cfg_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* agc descriptor */
							 | 
						||
| 
								 | 
							
								typedef struct _iot_plc_hw_topo_agc_ctrl {
							 | 
						||
| 
								 | 
							
								    /* sample data buffer */
							 | 
						||
| 
								 | 
							
								    int32_t     raw_data_buf[IOT_PLC_HW_TOPO_SAMP_NUM_MAX_PER_PERIOD];
							 | 
						||
| 
								 | 
							
								    /* the index where the sample data will be placed */
							 | 
						||
| 
								 | 
							
								    uint32_t    idx;
							 | 
						||
| 
								 | 
							
								    /* mean square value */
							 | 
						||
| 
								 | 
							
								    uint32_t    rms2;
							 | 
						||
| 
								 | 
							
								    /* peak value  */
							 | 
						||
| 
								 | 
							
								    uint32_t    peak;
							 | 
						||
| 
								 | 
							
								    /* the time stamp of the last gain adjustment, uint is 1s */
							 | 
						||
| 
								 | 
							
								    uint32_t    adjust_start_ts;
							 | 
						||
| 
								 | 
							
								    /* flak to mark if rms2 update */
							 | 
						||
| 
								 | 
							
								    uint32_t    rms2_update : 1,
							 | 
						||
| 
								 | 
							
								    /* flak to mark if over adc range */
							 | 
						||
| 
								 | 
							
								                over        : 1,
							 | 
						||
| 
								 | 
							
								    /* flasg to if gain is adjusted, in the observation */
							 | 
						||
| 
								 | 
							
								                adjusting   : 1,
							 | 
						||
| 
								 | 
							
								    /* reserved fields for further use */
							 | 
						||
| 
								 | 
							
								                rsvd        : 29;
							 | 
						||
| 
								 | 
							
								} iot_plc_hw_topo_agc_ctrl_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* adc info descriptor */
							 | 
						||
| 
								 | 
							
								typedef struct {
							 | 
						||
| 
								 | 
							
								    /* sampling frequency, uint is HZ */
							 | 
						||
| 
								 | 
							
								    uint32_t   samp_freq;
							 | 
						||
| 
								 | 
							
								    /* current gain value of ADC */
							 | 
						||
| 
								 | 
							
								    float      gain[IOT_HW_TOPO_PHASE_CNT];
							 | 
						||
| 
								 | 
							
								    /* scale factor for energy normalization */
							 | 
						||
| 
								 | 
							
								    float      scale;
							 | 
						||
| 
								 | 
							
								    /* current sampled data phase, see IOT_PLC_PHASE_XXX */
							 | 
						||
| 
								 | 
							
								    uint8_t    cur_smap_phase;
							 | 
						||
| 
								 | 
							
								    /* simultaneously sampled phase num */
							 | 
						||
| 
								 | 
							
								    uint8_t    phase_num;
							 | 
						||
| 
								 | 
							
								} topo_adc_info_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_parse_dma_data parse dma data
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param buf           dma data buffer
							 | 
						||
| 
								 | 
							
								 * @param index         index in the rebuffer to be parsed
							 | 
						||
| 
								 | 
							
								 * @param phy_phase     return plc phase, see IOT_PLC_PHASE_XXX
							 | 
						||
| 
								 | 
							
								 * @param data_val      return adc value
							 | 
						||
| 
								 | 
							
								 * @retuval: 1 - parse ok, 0 - parse error.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t topo_adc_adp_parse_dma_data(uint8_t *buf, uint32_t index,
							 | 
						||
| 
								 | 
							
								    uint32_t *phy_phase, int32_t *data_val);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_drv_get_sadc_info get adc information
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param info          return information, see topo_adc_info_t
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return none
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void topo_drv_get_sadc_info(topo_adc_info_t *info);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_get_fsb get the maximum value of adc data
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return the maximum value of adc data
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t topo_adc_adp_get_fsb(void);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_agc_handle agc handle
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param phy_phase     plc phase, see IOT_PLC_PHASE_XXX
							 | 
						||
| 
								 | 
							
								 * @param agc_ctl       information used for agc control
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return 0:           success
							 | 
						||
| 
								 | 
							
								 * @return NON-0:       error
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void topo_adc_adp_agc_handle(uint8_t phy_phase,
							 | 
						||
| 
								 | 
							
								    iot_plc_hw_topo_agc_ctrl_t *agc_ctl);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_samp_phase_cfg change sample phase
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param phy_phase     plc phase, see IOT_PLC_PHASE_XXX
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return none
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void topo_adc_adp_samp_phase_cfg(uint8_t phy_phase);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_samp_start start sample
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param dma_desc      dma descriptor
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return none
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void topo_adc_adp_samp_start(desc_t *dma_desc);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_samp_stop stop sample
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return none
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								void topo_adc_adp_samp_stop(void);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_init initialize topo adc
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param madp_cfgode   adapter config, see topo_adc_adp_cfg_t
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return 0:           success
							 | 
						||
| 
								 | 
							
								 * @return NON-0:       error
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t topo_adc_adp_init(topo_adc_adp_cfg_t *adp_cfg);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_dma_init initialize topo dma
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param dev_src_addr  dma source address
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return 0:           success
							 | 
						||
| 
								 | 
							
								 * @return NON-0:       error
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t topo_adc_adp_dma_init(uint32_t *dev_src_addr);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief topo_adc_adp_dump dump adc data
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @param phy_phase     plc phase, see IOT_PLC_PHASE_XXX
							 | 
						||
| 
								 | 
							
								 * @param gain1_idx     gain 1 index
							 | 
						||
| 
								 | 
							
								 * @param gain2_idx     gain 2 index
							 | 
						||
| 
								 | 
							
								 * @param discard_num   discard num for each sample
							 | 
						||
| 
								 | 
							
								 * @param data_num      data num for each sample
							 | 
						||
| 
								 | 
							
								 * @param samp_num      number of samples to be sampled
							 | 
						||
| 
								 | 
							
								 * @param data_buf      data out buffer, it is malloc in this function
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @return 0:           success
							 | 
						||
| 
								 | 
							
								 * @return NON-0:       error
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t topo_adc_adp_dump(uint8_t phy_phase, uint8_t gain1_idx,
							 | 
						||
| 
								 | 
							
								    uint8_t gain2_idx, uint8_t shift_num, uint32_t discard_num,
							 | 
						||
| 
								 | 
							
								    uint32_t data_num, uint32_t samp_num, uint32_t **data_buf);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* TOPO_ADC_ADP_H */
							 |