191 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			191 lines
		
	
	
		
			6.8 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 CVG_ZC_H
 | ||
|  | #define CVG_ZC_H
 | ||
|  | 
 | ||
|  | /* common includes */ | ||
|  | #include "os_types.h"
 | ||
|  | #include "iot_errno.h"
 | ||
|  | #include "iot_config.h"
 | ||
|  | #include "cvg.h"
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | typedef enum { | ||
|  |     cvg_zc_power_freq_invalid = 0, | ||
|  |     cvg_zc_power_freq_50hz, | ||
|  |     cvg_zc_power_freq_60hz, | ||
|  |     cvg_zc_power_freq_max, | ||
|  | } cvg_vdev_power_freq_t; | ||
|  | 
 | ||
|  | /* zc ntb data */ | ||
|  | typedef struct cvg_zc_ntb_data { | ||
|  |     /* zero-cross ntb */ | ||
|  |     uint32_t ntb; | ||
|  |     /* power line period, unit is ntb */ | ||
|  |     float    period; | ||
|  |     /* valid flag */ | ||
|  |     uint8_t  valid; | ||
|  | } cvg_zc_ntb_data_t; | ||
|  | 
 | ||
|  | /* negative delay is used to calibrate the zero-crossing deviation
 | ||
|  |  * of different manufacturers, unit is 1 NTB. | ||
|  |  */ | ||
|  | #define CVG_ZC_NTB_NEGATIVE_DELAY   (12500)
 | ||
|  | 
 | ||
|  | #define CVG_ZC_INVALID_SIGNED_DATA  (INT32_MAX)
 | ||
|  | 
 | ||
|  | #if (PLC_SUPPORT_PHY_PHASE_IDENT)
 | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief cvg_zc_data_slr_process() - continuous ZC NTB data is processed | ||
|  |  * by simple linear regression algorithm. | ||
|  |  * @param ntb:         continuous NTB array | ||
|  |  * @param ntb_cnt:     array size | ||
|  |  * @param calc_ntb:    calculated initial ntb | ||
|  |  * @param calc_period: calculated period | ||
|  |  * @param allowed_diff: allowed ntb diffience between calculated and actual | ||
|  |  *                     values. if the limit is exceeded, the original data is | ||
|  |  *                     considered invalid. NULL means use default value. | ||
|  |  * @param allowed_diff_cnt: maximum number of exceeded the allowable value, if | ||
|  |  *                     the limit is exceeded, the original data is considered | ||
|  |  *                     invalid. NULL means use default value. | ||
|  |  * | ||
|  |  * return: | ||
|  |  *      0         -- for success case | ||
|  |  *      othersie  -- error code | ||
|  |  */ | ||
|  | uint32_t cvg_zc_data_slr_process(uint32_t ntb[], | ||
|  |     uint32_t ntb_cnt, uint32_t *calc_ntb, float *calc_period, | ||
|  |     uint32_t *allowed_diff, uint32_t *allowed_diff_cnt); | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief cvg_zc_ntb_phase_matching() - calculate the physical phase of sta, | ||
|  |  * based on the zc ntb difference between CCO and STA | ||
|  |  * @param compensate_flag:  flag to mark if need compensation zc NTB | ||
|  |  * @param allow_phase_diff: allowed phase ntb diffience between cco and sta, if | ||
|  |  *                          the phase difference is within this range, the | ||
|  |  *                          matching phase is considered. NULL means use default | ||
|  |  *                          value. | ||
|  |  * @param cco_ntb:          zc data of CCO | ||
|  |  * @param sta_ntb:          zc data of STA | ||
|  |  * @param opposite_phase:   flag to mark if L/N reversed in Single-phase power | ||
|  |  *                          meter or phase sequence reversed in Three-phase | ||
|  |  *                          power meter | ||
|  |  * @return:                 calculated physical phase | ||
|  |  */ | ||
|  | 
 | ||
|  | uint8_t cvg_zc_ntb_phase_matching(uint8_t compensate_flag, | ||
|  |     uint32_t *allow_phase_diff, cvg_zc_ntb_data_t cco_ntb[], uint32_t sta_ntb, | ||
|  |     uint8_t *opposite_phase); | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief cvg_zc_kick_invalid_delta() - kick invalid zc delta data. | ||
|  |  * @param data:        continuous data array | ||
|  |  * @param cnt:         array size | ||
|  |  * @param allow_diff:  allowed delta difference threshold value | ||
|  |  * @return:            invalid number of data. | ||
|  |  */ | ||
|  | uint32_t cvg_zc_kick_invalid_delta(int32_t data[], uint32_t cnt, | ||
|  |     uint32_t allow_diff); | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief cvg_zc_delta_mean_calc() - calculate the mean of the array | ||
|  |  * @param data:        continuous data array | ||
|  |  * @param cnt:         array size | ||
|  |  * @param average:     calculated average | ||
|  |  * @param variance:    calculated variance | ||
|  |  */ | ||
|  | void cvg_zc_delta_mean_calc(int32_t data[], uint32_t cnt, int32_t *average, | ||
|  |     uint32_t *variance); | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief cvg_zc_kick_invalid_data_v2() - kick invalid zc data. | ||
|  |  * @param data:        continuous data array | ||
|  |  * @param cnt:         array size | ||
|  |  * @param min:         allowed minimum threshold value | ||
|  |  * @param min:         allowed maximum threshold value | ||
|  |  */ | ||
|  | void cvg_zc_kick_invalid_data_v2(int32_t data[], uint32_t cnt, int32_t min, | ||
|  |     int32_t max); | ||
|  | 
 | ||
|  | #else /* PLC_SUPPORT_PHY_PHASE_IDENT */
 | ||
|  | 
 | ||
|  | #define cvg_zc_data_slr_process(ntb, ntb_cnt, calc_ntb, calc_period, \
 | ||
|  |     allowed_diff, allowed_diff_cnt) (ERR_NOSUPP) | ||
|  | 
 | ||
|  | #define cvg_zc_ntb_phase_matching(compensate_flag, allow_phase_diff, cco_ntb, \
 | ||
|  |     sta_ntb, opposite_phase) (0) | ||
|  | 
 | ||
|  | #define cvg_zc_kick_invalid_delta(data, cnt, allow_diff) (0)
 | ||
|  | 
 | ||
|  | #define cvg_zc_delta_mean_calc(data, cnt, average, variance)
 | ||
|  | 
 | ||
|  | #define cvg_zc_kick_invalid_data_v2(data, cnt, min, max)
 | ||
|  | 
 | ||
|  | #endif /* PLC_SUPPORT_PHY_PHASE_IDENT */
 | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief cvg_zc_3p_meter_opposite_check() - if live wire and neutral wire | ||
|  |  * reversed in three phase power meter, the opposite phase has 30 degrees phase | ||
|  |  * position difference with other two phases, and the other two phase has 300 | ||
|  |  * degrees phase position difference. using the phase position difference, we | ||
|  |  * can distinguish the opposite connecting. | ||
|  |  * @param sta_ntb:          zc data of STA | ||
|  |  * @param opposite_phase:   the reversed phase | ||
|  |  * @return: | ||
|  |  *      0 - not reversed | ||
|  |  *      otherwise - the live wire and neutral wire reversed | ||
|  |  */ | ||
|  | uint8_t cvg_zc_3p_meter_opposite_check(cvg_zc_ntb_data_t sta_ntb[], uint8_t | ||
|  |     *opposite_phase); | ||
|  | 
 | ||
|  | /*
 | ||
|  | * @brief           start zero-cross ntb collection, and judge power frequency | ||
|  | * @param pdev_id:  which pdev to be collected | ||
|  | * @param vdev:     which vdev to be collected | ||
|  | * @param pkt:      pointer to an iot_pkt_t pointer that holds zc ntb raw data | ||
|  | * @param cnt:      number of consecutive zero-cross ntb to be collected. | ||
|  | * @param is_full_period: flag to mark if it is collecting full period. | ||
|  | * @param nid:      target network id, 0: means local network id | ||
|  | * @param ntb_ptr:  pointer to target historical zc ntb that needs to be | ||
|  | *                  collected, NULL: means collecting real-time zc ntb. | ||
|  | * | ||
|  | * @retval:         0 - for success case | ||
|  | *                  otherwise - error code | ||
|  | */ | ||
|  | uint32_t cvg_zc_ntb_collect(uint8_t pdev_id, cvg_vdev_t *vdev, iot_pkt_t **pkt, | ||
|  |     uint16_t cnt, uint8_t is_full_period, uint32_t nid, uint32_t *ntb_ptr); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief           get the ntb of one period | ||
|  |  * @return:         the value of ntb | ||
|  |  */ | ||
|  | uint32_t cvg_zc_get_period_ntb(); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief           get current ntb value | ||
|  |  * @return:         current ntb value | ||
|  |  */ | ||
|  | uint32_t cvg_zc_get_cur_ntb(); | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif /* CVG_ZC_H */
 |