250 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			250 lines
		
	
	
		
			9.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. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #ifndef IOT_BSRM_H
 | ||
|  | #define IOT_BSRM_H
 | ||
|  | 
 | ||
|  | /* os shim includes */ | ||
|  | #include "os_types_api.h"
 | ||
|  | #include "os_timer_api.h"
 | ||
|  | #include "iot_rtc_api.h"
 | ||
|  | #include "proto_645.h"
 | ||
|  | #include "iot_task_api.h"
 | ||
|  | #include "iot_uart_api.h"
 | ||
|  | #include "iot_io_api.h"
 | ||
|  | #include "iot_ipc_api.h"
 | ||
|  | #include "iot_plc_msg_api.h"
 | ||
|  | #include "iot_bsrm_rec.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #if IOT_BSRM_MODE
 | ||
|  | 
 | ||
|  | /* define branch signal recognition module message queue count and priorities,
 | ||
|  |  * the higer the priority, the lower the queue number. | ||
|  |  */ | ||
|  | #define IOT_BSRM_MSG_QUEUE_HP                       (0)
 | ||
|  | #define IOT_BSRM_MSG_QUEUE_LP                       (1)
 | ||
|  | #define IOT_BSRM_MSG_QUEUE_MAX_PRIO                 (2)
 | ||
|  | 
 | ||
|  | /* message type definition */ | ||
|  | /* messages from branch signal recognition module interface */ | ||
|  | #define IOT_BSRM_MSG_TYPE_UART                      (1)
 | ||
|  | /* messages from period timer */ | ||
|  | #define IOT_BSRM_MSG_TYPE_TIMER                     (2)
 | ||
|  | /* messages from bsrm internal */ | ||
|  | #define IOT_BSRM_MSG_TYPE_INTERNAL                  (3)
 | ||
|  | /* messages from sg app */ | ||
|  | #define IOT_BSRM_MSG_TYPE_SG_APP                    (4)
 | ||
|  | 
 | ||
|  | /* branch signal recognition module type message id definition */ | ||
|  | /* message delivered from branch signal recognition module */ | ||
|  | #define IOT_BSRM_MSG_ID_UART_DATA                   (1)
 | ||
|  | /* message delivered from period timer */ | ||
|  | #define IOT_BSRM_MSG_ID_TIMER_PERIOD                (1)
 | ||
|  | /* message check mr from timer on master mode */ | ||
|  | #define IOT_BSRM_MSG_ID_TIMER_CHECK_MR              (2)
 | ||
|  | /* SG_APP type message id definition */ | ||
|  | /* messages from SG APP */ | ||
|  | #define IOT_BSRM_MSG_ID_SG_APP                      (1)
 | ||
|  | 
 | ||
|  | /* handle branch signal recognition module data state */ | ||
|  | /* idle state */ | ||
|  | #define IOT_BSRM_MR_STATE_IDLE                      (0)
 | ||
|  | /* ready handle breaker data state */ | ||
|  | #define IOT_BSRM_MR_STATE_READY                     (1)
 | ||
|  | /* report measure data to breaker */ | ||
|  | #define IOT_BSRM_MR_GET_ADDR                        (2)
 | ||
|  | /* sync tm */ | ||
|  | #define IOT_BSRM_MR_STATE_SYNC_TIME                 (3)
 | ||
|  | #define IOT_BSRM_MR_STATE_EVT_NODIF                 (4)
 | ||
|  | #define IOT_BSRM_MR_STATE_FWD                       (5)
 | ||
|  | #define IOT_BSRM_MR_STATE_SYNC_CUR_RATIO_COEFF      (6)
 | ||
|  | #define IOT_BSRM_MR_STATE_PM_UPGRADE                (7)
 | ||
|  | 
 | ||
|  | /* sync cur ratio coeff status */ | ||
|  | #define IOT_BSRM_SYNC_CUR_COEFF_IDLE                (0)
 | ||
|  | #define IOT_BSRM_SYNC_CUR_COEFF_DOING               (1)
 | ||
|  | #define IOT_BSRM_SYNC_CUR_COEFF_DONE                (2)
 | ||
|  | /* sync cur ration coeff max count */ | ||
|  | #define IOT_BSRM_SYNC_CUR_COEFF_MAX_CNT             (5)
 | ||
|  | 
 | ||
|  | /* synchro time idle status */ | ||
|  | #define IOT_BSRM_SYNC_TIME_STATE_IDLE               (0)
 | ||
|  | /* synchro time ymd status */ | ||
|  | #define IOT_BSRM_SYNC_TIME_STATE_YMD                (1)
 | ||
|  | /* synchro time hms status */ | ||
|  | #define IOT_BSRM_SYNC_TIME_STATE_HMS                (2)
 | ||
|  | 
 | ||
|  | /* recv hw_tsfm info */ | ||
|  | #define IOT_BSRM_MSG_ID_INTERNAL_HW_TSFM            (1)
 | ||
|  | /* recv internal event msg */ | ||
|  | #define IOT_BSRM_MSG_ID_INTERNAL_EVENT_INTERRUPT    (2)
 | ||
|  | /* recv hw_tsfm record clear msg */ | ||
|  | #define IOT_BSRM_MSG_ID_INTERNAL_BR_REC_CLEAR       (3)
 | ||
|  | 
 | ||
|  | #define IOT_BSRM_CHECK_ZC_STATUS_IDLE               (0)
 | ||
|  | #define IOT_BSRM_CHECK_ZC_STATUS_GOING              (1)
 | ||
|  | #define IOT_BSRM_CHECK_ZC_STATUS_DONE               (2)
 | ||
|  | 
 | ||
|  | #define IOT_BSRM_CHECK_ZC_TIMEOUT                   (3)
 | ||
|  | 
 | ||
|  | #define IOT_BSRM_CHECK_EVENT_STATUS_IDLE            (0)
 | ||
|  | #define IOT_BSRM_CHECK_EVENT_STATUS_DOING           (1)
 | ||
|  | #define IOT_BSRM_CHECK_EVENT_STATUS_DONE            (2)
 | ||
|  | 
 | ||
|  | /* extend branch signal recognition module message */ | ||
|  | typedef struct _iot_bsrm_msg { | ||
|  |     /* iot task message */ | ||
|  |     iot_task_msg_t  task_msg; | ||
|  |     /* pointer to message data */ | ||
|  |     void            *data; | ||
|  |     /* another data field */ | ||
|  |     uint32_t        data2; | ||
|  | } iot_bsrm_msg_t; | ||
|  | 
 | ||
|  | /* function check status */ | ||
|  | typedef struct _iot_bsrm_check_zc_ctrl { | ||
|  |     /* zero cross test status, see IOT_BSRM_CHECK_ZC_STATUS_XXX */ | ||
|  |     uint8_t status; | ||
|  |     /* zero cross circuit bitmap:
 | ||
|  |      * bit0 - phase A, bit1 - phase B, bit2 - phase C, | ||
|  |      * bit3 - phase A ext, bit4 - phase B ext, bit5 - phase C ext | ||
|  |      */ | ||
|  |     uint8_t zc_bm; | ||
|  |     /* bitmap for zero cross circuit check is started, set to 1 when the gpio
 | ||
|  |      * interrupt is triggered: | ||
|  |      * bit0 - phase A, bit1 - phase B, bit2 - phase C, | ||
|  |      * bit3 - phase A ext, bit4 - phase B ext, bit5 - phase C ext | ||
|  |      */ | ||
|  |     uint8_t started_zc_bm; | ||
|  |     /* timestamp at start */ | ||
|  |     uint32_t start_ts; | ||
|  |     /* the gpio tick at the last trigger interrupt for each zc circuit */ | ||
|  |     uint32_t pre_tick[2 * IOT_PLC_PHASE_CNT]; | ||
|  |     /* total time during check for each zc circuit, unit is 1us */ | ||
|  |     uint32_t total_time[2 * IOT_PLC_PHASE_CNT]; | ||
|  |     /* gpio interrupt trigger count during check */ | ||
|  |     uint32_t trig_cnt[2 * IOT_PLC_PHASE_CNT]; | ||
|  |     /* period for each zero cross circuit */ | ||
|  |     uint32_t period[2 * IOT_PLC_PHASE_CNT]; | ||
|  | } iot_bsrm_check_zc_ctrl_t; | ||
|  | 
 | ||
|  | /* function check event status */ | ||
|  | typedef struct _iot_bsrm_check_event_ctrl { | ||
|  |     uint8_t status; | ||
|  |     /* check times, unit: 1/s */ | ||
|  |     uint8_t times; | ||
|  |     /* event interrupt count  */ | ||
|  |     uint8_t count; | ||
|  | } iot_bsrm_check_event_ctrl_t; | ||
|  | 
 | ||
|  | /* function check status */ | ||
|  | typedef struct _iot_bsrm_check_ctrl { | ||
|  |     iot_bsrm_check_zc_ctrl_t zc_ctrl; | ||
|  |     iot_bsrm_check_event_ctrl_t event; | ||
|  | } iot_bsrm_check_ctrl_t; | ||
|  | 
 | ||
|  | /* br record fragment response control structure */ | ||
|  | typedef  struct { | ||
|  |     /* flag to mark if fragment response is startup */ | ||
|  |     uint8_t         start_up; | ||
|  |     /* oi of fragment response */ | ||
|  |     uint16_t         oi; | ||
|  |     /* current sequence numbere */ | ||
|  |     uint16_t        seq; | ||
|  |     /* fragment response window countdown, uint is 1s */ | ||
|  |     uint16_t        life_cd; | ||
|  |    /* last response recode start tm */ | ||
|  |     iot_time_tm_t   start_tm; | ||
|  |     /* last response recode end tm */ | ||
|  |     iot_time_tm_t   end_tm; | ||
|  | }  iot_bsrm_br_rec_frag_ctrl_t; | ||
|  | 
 | ||
|  | /* extend branch signal recognition module global descriptor */ | ||
|  | typedef struct _iot_bsrm_global { | ||
|  |     /* extend branch signal recognition module task configuration */ | ||
|  |     iot_task_config_t           task_cfg; | ||
|  |     /* extend branch signal recognition module task handle */ | ||
|  |     iot_task_h                  task_h; | ||
|  |     /* periodic timer */ | ||
|  |     timer_id_t                  periodic_timer; | ||
|  |     /* handle of registered IPC */ | ||
|  |     iot_ipc_h                   ipc_h; | ||
|  |     /* timer for check addr and tm */ | ||
|  |     timer_id_t                  mr_timer; | ||
|  |     /* branch signal recognition module uart handle*/ | ||
|  |     iot_uart_h                  handle; | ||
|  |     /* address, little endian */ | ||
|  |     uint8_t                     addr[IOT_MAC_ADDR_LEN]; | ||
|  |     /* correct time */ | ||
|  |     iot_time_tm_t               correct_tm; | ||
|  |     /* buffer is used to cache protocol data */ | ||
|  |     uint8_t                     proto_buff[PROTO_645_MAX_PKT_LEN]; | ||
|  |     /* read addr pend flag */ | ||
|  |     uint8_t                     mr_addr_pend  : 1, | ||
|  |     /* addr is valid */ | ||
|  |                                 addr_valid    : 1, | ||
|  |     /* sync tm status */ | ||
|  |                                 sync_tm_state : 2, | ||
|  |     /* sync time done */ | ||
|  |                                 sync_tm_done  : 1, | ||
|  |     /* reserved for further use */ | ||
|  |                                 rsvd          : 3; | ||
|  |     /* mr time interval, unit: 1s */ | ||
|  |     uint32_t                    mr_tm_interval; | ||
|  |     /* iot_pkt to be forwarded */ | ||
|  |     iot_pkt_t                   *fwd_pkt; | ||
|  |     /* read meter data status on master mode */ | ||
|  |     uint8_t                     mr_status; | ||
|  |     /* request tm and address count on master mode */ | ||
|  |     uint8_t                     req_cnt; | ||
|  |     /* event data count */ | ||
|  |     uint8_t                     data_cnt; | ||
|  |     /* store event entries */ | ||
|  |     uint8_t                     *evt_buf; | ||
|  |     /* event trigger record time*/ | ||
|  |     uint32_t                    evt_trig_cd; | ||
|  |     /* retry current ratio coefficient sync count */ | ||
|  |     uint8_t                     coeff_sync_cnt; | ||
|  |     /* flag to mark if the event will be notified */ | ||
|  |     uint8_t                     evt_nodify_pend      : 1, | ||
|  |     /* event notification try count */ | ||
|  |                                 evt_nodify_try_cnt   : 4, | ||
|  |     /* current ratio coefficient sync status see: IOT_BSRM_SYNC_CUR_COEFF_XXX */ | ||
|  |                                 cur_coeff_sync_sts   : 2, | ||
|  |     /* reserved for further use */ | ||
|  |                                 rsvd1                : 1; | ||
|  |     /* bsrm session mode */ | ||
|  |     uint8_t                     session_mode; | ||
|  |     /* test status */ | ||
|  |     iot_bsrm_check_ctrl_t       check_crtl; | ||
|  |     /* br result record buffer */ | ||
|  |     iot_bsrm_branch_info_entry_t br_rec_buf; | ||
|  |     /* br result record countdown */ | ||
|  |     uint8_t                      br_rec_cd; | ||
|  |     /* br record fragment response control structure */ | ||
|  |     iot_bsrm_br_rec_frag_ctrl_t  br_rec_frag_ctrl; | ||
|  | } iot_bsrm_global_t; | ||
|  | 
 | ||
|  | #endif /* IOT_BSRM_MODE */
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif /* IOT_BSRM_H */
 |