/**************************************************************************** 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. ****************************************************************************/ /* os shim includes */ #include "iot_config.h" #include "os_types.h" #include "hw_reg_api.h" #include "iot_mem.h" /* plc public api includes */ #include "plc_fr.h" #include "plc_const.h" #include "iot_bitops.h" /* adc api includes */ #include "sadc.h" #include "sadc_hw.h" #include "iot_adc_api.h" #include "pmu_hw.h" #include "phy_temperature.h" #if PLC_PHY_SUPPORT_TEMPERATURE static phy_temperature_ctxt_t phy_tempt_ctxt; void phy_temperature_adc_init(void) { #if HW_PLATFORM >= HW_PLATFORM_FPGA /* pmu reset */ pmu_soft_reset(SADC_CH1_SOFT_RST_OFFSET); /* adc init */ sadc_init(ANA_SADC0, NULL); /* phase mode init */ sadc_phase_mode_set(ANA_SADC0, 0); /* phase 0 select mapping mux 0 */ sadc_phase_sel_scl_mux(SADC_PHASE0, ADC_CHANNEL0); /* set gain to 1/2 */ sadc_adc_gain_set(ADC_CHANNEL0, 0); /* mtr sel scl from meter vtem */ sadc_mtr_sel_scl_mux(SADC_PHASE0,SADC_TSW_MTR_SEL_MUX_MODE_VTMP); /* start sadc */ sadc_start_en(ANA_SADC0, 1); /* set channel sample time */ sadc_discard_num_set(ANA_SADC0, SADC_PHASE0, 6); /* select refer voltage as gnd */ sadc_vcm_sel_mux_set(ANA_SADC0, SADC_PHASE0, ANA_SADC_VCM_SEL_GND); #endif //#if HW_PLATFORM >= HW_PLATFORM_FPGA } void phy_temperature_adc_scan(void) { #if HW_PLATFORM >= HW_PLATFORM_FPGA uint32_t val; if (sadc_check_data_vld(ANA_SADC0, 0)) { val = sadc_data_get(ANA_SADC0, 0); sadc_data_vld_clr(ANA_SADC0,0); val &= 0xffffff; /* recover gain to 1 */ val <<= 1; /* check valid raw data */ if (val < RAW_DATA_VALID_MIN) { val = RAW_DATA_VALID_MIN; } else if (val > RAW_DATA_VALID_MAX) { val = RAW_DATA_VALID_MAX; } /* count for valid average data */ if (phy_tempt_ctxt.cnt < TEMPT_DATA_SKIP) { phy_tempt_ctxt.cnt++; } /* calc average raw data */ phy_tempt_ctxt.tempt_raw_data = phy_tempt_ctxt.tempt_raw_data - (phy_tempt_ctxt.tempt_raw_data >> NEW_RAW_WEIGHT) + (val >> NEW_RAW_WEIGHT); } #endif //#if HW_PLATFORM >= HW_PLATFORM_FPGA } float phy_temperature_val_get(void) { /* make sure return value is valid */ if (phy_tempt_ctxt.cnt < TEMPT_DATA_SKIP) { return 0; } /* convert the adc value to real temperature */ phy_tempt_ctxt.cur_tempt_val = (float)RAW_TO_TEMP(phy_tempt_ctxt.tempt_raw_data); if (phy_tempt_ctxt.cur_tempt_val < TEMP_VAL_MIN) { phy_tempt_ctxt.cur_tempt_val = TEMP_VAL_MIN; } if (phy_tempt_ctxt.cur_tempt_val > TEMP_VAL_MAX) { phy_tempt_ctxt.cur_tempt_val = TEMP_VAL_MAX; } return phy_tempt_ctxt.cur_tempt_val; } #endif //#if PLC_PHY_SUPPORT_TEMPERATURE > 0