This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC1352R: Reset source 6 - goes away only after reprogramming of device

Part Number: CC1352R

We are using CC1352R, and sensor controller.

The sensor controller has two tasks , only one is active (this is configured in production)

Funny thing that once everything works OK for the first task, the second task has the following problem:

- power up the device - reset source = 1

- reset the device once (HW reset with reset pin) - power up with reset source 1

- device boots up , reaches the sensor controller tick activation and then goes into  repetative resets with reset source = 6

- the reperative resets does nto go away , even if I power off the device and apply power again it goes directly to the repetative reset

The system reset (reset source = 6 ) is most probably related to the following line of code (where I enable the sensor controller tick)

scifStartRtcTicksNow(0x00010000 ) ;

When I comment out this line , there are no repetative resets , but sensor controller does not work (not ticks)

  • Hi Yaron, 

    Assigning an expert to comment. 

    Thanks, 
    Elin 

  • Hi Yaron,

    Could you share the code you are running in the second task and how the SCE interacts with the ARM software? Could you also share the SDK Version you are using and if you are basing your work of any of our examples?

  • Hi M-W
    - link to the code drive.google.com/.../view

    - SCE interaction is via shared memory (Automatically generated by SCS) , the ARM reads results from the SCE. There are no ARM to SCE commands

    - SDK version cc13x2_26x2_sdk_3_40_00_02

    -

  • Hi Yaron,

    I can unfortunately not access the code as the link is not public (and I cannot login to google with my TI account). If you could make it public that would help, if you do not want to share a public link to it, feel free to send me a DM withe link in private. 

  • Here is the code as a copy paste from the scp file:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="Maintanance task" version="2.6.0.132">
    <desc><![CDATA[Capacitor management task for O-sensor]]></desc>
    <pattr name="Apply default power mode">1</pattr>
    <pattr name="Board">None</pattr>
    <pattr name="Chip name">CC1352R1F3</pattr>
    <pattr name="Chip package">QFN48 7x7 RGZ</pattr>
    <pattr name="Chip revision">E</pattr>
    <pattr name="Clock in active mode">1</pattr>
    <pattr name="Code prefix"></pattr>
    <pattr name="Default power mode">0</pattr>
    <pattr name="Operating system">TI-RTOS</pattr>
    <pattr name="Output directory">.</pattr>
    <pattr name="RTC tick interval (16.16 format)">2000</pattr>
    <pattr name="Run-time logging UART RX pin"></pattr>
    <pattr name="Run-time logging UART TX pin"></pattr>
    <task name="capacitor charger">
    <desc><![CDATA[Idle state: if capacitor voltage is higher than 3.8V then stay idle
    ELSE switch to active state
    Active state: measure capacitor voltage and set DAC voltage to set input current according to temperature
    if capacitor voltage is above 3.9V switch to idle state]]></desc>
    <tattr name="Avg_exp" desc="16" type="expr" content="const" scope="task" min="0" max="65535">4</tattr>
    <tattr name="Avg_num" desc="number of measurements for Vfb" type="expr" content="const" scope="task" min="0" max="65535">16</tattr>
    <tattr name="DACInitFactor" desc="-0.0675 / 16bit signed" type="expr" content="const" scope="task" min="-32768" max="32767">-2211</tattr>
    <tattr name="DCDCRfeedback" desc="Rfeedback multiplied by 10 - 237K" type="expr" content="const" scope="task" min="0" max="65535">2370</tattr>
    <tattr name="DCDCRreff" desc="rreff multiplied by 10 - 90.9K" type="expr" content="const" scope="task" min="0" max="65535">909</tattr>
    <tattr name="Exit_illigal_state" desc="Exit because illigal state (not idle not active)" type="expr" content="const" scope="task" min="0" max="65535">0x0001</tattr>
    <tattr name="Exit_normal" desc="Normal termination" type="expr" content="const" scope="task" min="0" max="65535">0x00</tattr>
    <tattr name="State_Active" desc="State enumerator for active" type="expr" content="const" scope="task" min="0" max="65535">0001</tattr>
    <tattr name="State_Idle" desc="state enumartor for Idle" type="expr" content="const" scope="task" min="0" max="65535">0</tattr>
    <tattr name="TaskCapacitorCharger" desc="cap charger task is running" type="expr" content="const" scope="task" min="0" max="65535">0</tattr>
    <tattr name="Vbat_avg_num" desc="number of ADC measurments on Vbat to average" type="expr" content="const" scope="task" min="0" max="65535">16</tattr>
    <tattr name="cfg.VfbCapacitor3500mV" desc="ADC compenstaed value for 3.5V measurement (scaled by FB voltage dvivider)" type="expr" content="struct" scope="task" min="0" max="65535">627</tattr>
    <tattr name="cfg.VfbCapacitor3800mV" desc="Compensated ADC value for 3.8V scaled by FB voltage divider" type="expr" content="struct" scope="task" min="0" max="65535">680</tattr>
    <tattr name="cfg.VfbCapacitor3950mV" desc="ADC compenstaed value for 4V measurement (scaled by FB voltage dvivider)" type="expr" content="struct" scope="task" min="0" max="65535">690</tattr>
    <tattr name="cfg.adcVcap3500mV" desc="3500mV" type="expr" content="struct" scope="task" min="0" max="65535">3500</tattr>
    <tattr name="cfg.adcVcap3800mV" desc="3800mV" type="expr" content="struct" scope="task" min="0" max="65535">3800</tattr>
    <tattr name="cfg.adcVcap3950mV" desc="3950mV" type="expr" content="struct" scope="task" min="0" max="65535">3950</tattr>
    <tattr name="input.SuperFastCharge" desc="charge with 200mA - for certification tests only" type="expr" content="struct" scope="task" min="0" max="65535">1</tattr>
    <tattr name="input.Transmit" desc="set to 1 by mqtt thread to initiate the cap for cellular modem activation" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.ADCVfbbeforeActive" desc="Vfb measurements with VDDs ref before switching to active" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.DACCorrection" desc="Correction value to DAC" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.DACValue" desc="DAC value to control the cap charger" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.ErrorCode" desc="Exit to termination error code" type="expr" content="struct" scope="task" min="0" max="65535">Exit_normal</tattr>
    <tattr name="output.Ierr" desc="Error value from current setpoint to measured current at DC-DC input" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.Iin" desc="holds the calculated input current " type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.IinMAC" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.IinSetpoint" desc="Target input current consumption" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.InternalTemp" desc="Internal temp from BATMON" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.OneShot" desc="marks the first execution" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.Ready" desc="Signals the mqtt thread that the capcitor is ready from modem activation" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.Rfeedback" type="expr" content="struct" scope="task" min="0" max="65535">2370</tattr>
    <tattr name="output.Rreff" type="expr" content="struct" scope="task" min="0" max="65535">909</tattr>
    <tattr name="output.RunningTask" type="expr" content="struct" scope="task" min="0" max="65535">TaskCapacitorCharger</tattr>
    <tattr name="output.SCVersion" desc="version of the SC FW to be verified by the main processor" type="expr" content="struct" scope="task" min="0" max="65535">10</tattr>
    <tattr name="output.activeescape" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcBatteryVoltage" desc="battery voltage mV" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcTemp" desc="temp variable to store ADC measurements" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcValue" desc="storage of adc measurements" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVbat" desc="battery voltage with DC-DC load , internal reference" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVbatLoad" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVbatNoLoad" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVcap" desc="Cap voltage through Vfb " type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVin" desc="Vbat with DC-DC off" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.correctiontemp" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.loopcountactivestate" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="scale_factor_1" desc="32FS signed = 1" type="expr" content="const" scope="task" min="0" max="65535">2048</tattr>
    <tattr name="scale_factor_VbatSense" desc="32FS signed = 1.0498" type="expr" content="const" scope="task" min="0" max="65535">2150</tattr>
    <tattr name="scale_factor_Vfb" desc="final scale factor , used in cap voltage estimate from Vfb" type="expr" content="const" scope="task" min="0" max="65535">scale_factor_Vfb_90K_237K</tattr>
    <tattr name="scale_factor_Vfb_54K_237K" desc="32 FS , 16bit unsigned = 5.5817 correction factor" type="expr" content="const" scope="task" min="0" max="65535">11431</tattr>
    <tattr name="scale_factor_Vfb_90K_237K" desc="32 FS , 16bit unsigned = 3.787 correction factor" type="expr" content="const" scope="task" min="0" max="65535">7756</tattr>
    <tattr name="setpoint_fast_charge" desc="in Ma" type="expr" content="const" scope="task" min="0" max="65535">100</tattr>
    <tattr name="setpoint_minus_10" desc="in mA" type="expr" content="const" scope="task" min="0" max="65535">1</tattr>
    <tattr name="setpoint_roomtemp" desc="in mA" type="expr" content="const" scope="task" min="0" max="65535">10</tattr>
    <tattr name="setpoint_superfast_charge" desc="in mA" type="expr" content="const" scope="task" min="0" max="65535">200</tattr>
    <tattr name="setpoint_zero" desc="in mA" type="expr" content="const" scope="task" min="0" max="65535">5</tattr>
    <tattr name="state.ChargerState" desc="Charger state variable- idle or active" type="expr" content="struct" scope="task" min="0" max="65535">State_Idle</tattr>
    <tattr name="state.DACmode" desc="0 for DAC disable , 1 for DAC enabled" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="state.InitialCharge" desc="Oneshot flag to do the first charge at high current" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <resource_ref name="ADC" enabled="1"/>
    <resource_ref name="AON Domain Functionality" enabled="1"/>
    <resource_ref name="Accumulator-Based Math" enabled="1"/>
    <resource_ref name="Analog Open-Drain Pins" enabled="0"/>
    <resource_ref name="Analog Open-Source Pins" enabled="0"/>
    <resource_ref name="Analog Pins" enabled="1">
    <io_usage name="Vfb_Chg_Control" label="Vfb meas and control">
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO28</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    </io_usage>
    <io_usage name="Vbat_sense" label="voltage after 1 ohm resistor">
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO26</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="COMPA" enabled="1"/>
    <resource_ref name="COMPA Event Trigger" enabled="0"/>
    <resource_ref name="COMPB" enabled="0"/>
    <resource_ref name="COMPB Event Trigger" enabled="0"/>
    <resource_ref name="Delay Insertion" enabled="1"/>
    <resource_ref name="Differential Output Pins" enabled="0"/>
    <resource_ref name="Digital Input Pins" enabled="0"/>
    <resource_ref name="Digital Open-Drain Pins" enabled="0"/>
    <resource_ref name="Digital Open-Source Pins" enabled="0"/>
    <resource_ref name="Digital Output Pins" enabled="1">
    <io_usage name="POWER_EN" label="enable the DCDC converter">
    <uattr name="Configuration on uninitialization">-1</uattr>
    <uattr name="Output drive strength">0</uattr>
    <uattr name="Output value on initialization">0</uattr>
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO11</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Dynamic Power Control" enabled="1"/>
    <resource_ref name="GPIO Event Trigger" enabled="0"/>
    <resource_ref name="I2C Master" enabled="0">
    <rattr name="SCL frequency">0</rattr>
    <rattr name="SCL stretch timeout">1</rattr>
    <io_usage>
    <uattr name="SCL pin/0000"></uattr>
    <uattr name="SDA pin/0000"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="ISRC" enabled="0"/>
    <resource_ref name="LCD Controller" enabled="0">
    <rattr name="Waveform state count">8</rattr>
    <io_usage>
    <uattr name="Output drive strength">0</uattr>
    <uattr name="Pin count">20</uattr>
    <uattr name="Pin/0000"></uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Math and Logic" enabled="1"/>
    <resource_ref name="Multi-Buffered Output Data Exchange" enabled="0">
    <rattr name="Buffer count">2</rattr>
    <rattr name="Indicate overflow at buffer check">1</rattr>
    <rattr name="Indicate overflow at buffer switch">0</rattr>
    <rattr name="Prevent overflow at buffer switch">1</rattr>
    </resource_ref>
    <resource_ref name="Peripheral Sharing" enabled="0"/>
    <resource_ref name="Pulse Counter" enabled="0"/>
    <resource_ref name="RTC Multi-Event Capture" enabled="0"/>
    <resource_ref name="RTC-Based Execution Scheduling" enabled="1"/>
    <resource_ref name="Reference DAC" enabled="1"/>
    <resource_ref name="Run-Time Logging" enabled="0"/>
    <resource_ref name="SPI Chip Select" enabled="0"/>
    <resource_ref name="SPI Data Transfer" enabled="0">
    <rattr name="Bit rate">0</rattr>
    <rattr name="MISO configuration when inactive">-1</rattr>
    <rattr name="MOSI configuration on initialization">0</rattr>
    <rattr name="MOSI configuration on uninitialization">0</rattr>
    <rattr name="SCLK configuration on initialization">0</rattr>
    <rattr name="SCLK configuration on uninitialization">0</rattr>
    <io_usage>
    <uattr name="MISO pin/0000"></uattr>
    <uattr name="MOSI pin/0000"></uattr>
    <uattr name="SCLK pin/0000"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Signal Observation" enabled="0"/>
    <resource_ref name="System CPU Alert" enabled="1"/>
    <resource_ref name="System Event Trigger" enabled="0"/>
    <resource_ref name="TDC" enabled="0"/>
    <resource_ref name="Timer 0" enabled="0"/>
    <resource_ref name="Timer 0 Event Trigger" enabled="0"/>
    <resource_ref name="Timer 1" enabled="0"/>
    <resource_ref name="Timer 1 Event Trigger" enabled="0"/>
    <resource_ref name="Timer 2" enabled="0"/>
    <resource_ref name="Timer 2 Event Trigger" enabled="0"/>
    <resource_ref name="UART Emulator" enabled="0">
    <rattr name="RX buffer size">64</rattr>
    <rattr name="Required idle period before enabling RX">11</rattr>
    <rattr name="TX buffer size">64</rattr>
    <io_usage>
    <uattr name="RX pin/0000"></uattr>
    <uattr name="TX pin/0000"></uattr>
    </io_usage>
    </resource_ref>
    <sccode name="event0" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="event1" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="event2" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="event3" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="execute" init_power_mode="0"><![CDATA[macro sample_and_acc(avg_shift , result_shift, factor) {
    output.adcTemp = 0;
    accReset();
    for (U16 n = 0; n < (1 << avg_shift ); n++) {
    adcGenManualTrigger();
    adcReadFifo(output.adcTemp);
    accMac16s16u(output.adcTemp, factor);
    }
    accGet16(result_shift; output.adcValue );
    }


    adcEnableSync(ADC_REF_FIXED,ADC_SAMPLE_TIME_170_US, ADC_TRIGGER_MANUAL);
    // measure battery voltage for telemetry
    adcSelectIntInput(ADC_INPUT_VDDS);
    sample_and_acc(2,13,scale_factor_1);
    output.adcBatteryVoltage = output.adcValue;
    adcSelectGpioInput(AUXIO_A_VBAT_SENSE);

    // -------------------- state Idle -----------------------------------

    if (state.ChargerState == State_Idle) {
    // DAC disable
    refdacStopOutput();
    refdacDisable();
    //ADC enable
    // adcEnableSync(ADC_REF_FIXED, ADC_SAMPLE_TIME_1P37_MS, ADC_TRIGGER_MANUAL);

    // Configure FB as analog input
    adcSelectGpioInput(AUXIO_A_VFB_CHG_CONTROL);
    // Disable DC-DC: EN = 0
    gpioClearOutput(AUXIO_O_POWER_EN);
    fwDelayUs(20000);
    sample_and_acc(Avg_exp , ((15-Avg_exp)+4), scale_factor_Vfb);
    // Measure voltage on FB pin
    // Average 16 time
    // output.adcValue = 0;
    // output.adcTemp = 0;
    // for (U16 n = 0; n < Avg_num; n++) {
    // adcGenManualTrigger();
    // adcReadFifo(output.adcTemp);
    // output.adcValue += output.adcTemp;
    // }
    output.adcVcap = output.adcValue;
    // shut down ADC
    adcDisable();
    // do the trasmit input logic
    if (output.adcVcap < cfg.adcVcap3500mV) {
    output.Ready = 0;
    } else {
    output.Ready = 1 ;
    }
    // Compare to 3.8V
    // If voltage is more than 3.8V stay in Idle state
    if (output.adcVcap > cfg.adcVcap3800mV) {
    state.ChargerState = State_Idle;
    } else {
    // switch to active state
    state.ChargerState = State_Active ;
    output.loopcountactivestate = 0;
    // for 54.9K and 237K - dac_value = 280-0.0675Vin[mV]
    accReset();
    accAdd16u(280);
    accShiftLeft(15);
    accMac16u16s(output.adcVcap,DACInitFactor);
    accGet16( 15; output.DACValue);
    if (output.DACValue < 50 ) {
    output.DACValue = 50;
    }
    if (output.DACValue > 255) {
    output.DACValue = 255;
    }
    // output.DACValue = 128;
    // output.DACValue = 290;
    // output.DACValue -= output.adcVcap >> 2;
    // output.DACValue -= output.adcVcap >> 4;
    // output.DACValue -= output.adcVcap >> 6;
    compaSelectGpioInput(AUXIO_A_VFB_CHG_CONTROL);
    // load new value to DAC
    refdacStartOutputOnCompaIn(output.DACValue);
    // Enable DAC
    refdacEnable(REFDAC_PWRMODE_ANY,REFDAC_REF_DCOUPL_VOUT_LOW);
    refdacWaitForStableOutput() ;

    }
    } else {
    // ----------------- state Active ------------------------------------
    output.loopcountactivestate = 2;

    // while ( output.loopcountactivestate >= 0) {

    output.loopcountactivestate -= 1;
    // Enable DC-DC
    gpioSetOutput(AUXIO_O_POWER_EN);
    // Measure temperature
    sysGetBatmonTemp( output.InternalTemp);
    // convert from 1/4 degrees to degrees
    output.InternalTemp >>= 2 ;

    // Set Iin_setpoint according to:
    // series 1ohm resistor
    // internal ref with scaling gives 1LSB = 1.05mV --> 1.05mA
    if (input.SuperFastCharge==1) {
    output.IinSetpoint = setpoint_superfast_charge;
    } else if (state.InitialCharge == 0) {
    output.IinSetpoint = setpoint_fast_charge;
    } else if ( output.InternalTemp < (-10)) {
    output.IinSetpoint = setpoint_minus_10;
    } else if ( output.InternalTemp < 0) {
    output.IinSetpoint = setpoint_zero;
    } else {
    output.IinSetpoint = setpoint_roomtemp;
    }

    // measure Vbat x 4
    adcSelectGpioInput(AUXIO_A_VBAT_SENSE);
    fwDelayUs(10000);
    sample_and_acc(Avg_exp , (((15-Avg_exp)+4)-2), scale_factor_VbatSense);
    output.adcVbat = output.adcValue ;

    // measure input voltage x 4
    adcSelectIntInput(ADC_INPUT_VDDS);
    sample_and_acc(Avg_exp,(((15-Avg_exp)+4)-2),scale_factor_VbatSense);
    output.adcVbatLoad = output.adcValue;

    // Disable DC-DC , Disable DAC
    gpioClearOutput(AUXIO_O_POWER_EN);
    refdacStopOutput();
    refdacDisable();
    fwDelayUs(20000);

    // Measure Vinx 4
    //adcSelectGpioInput(AUXIO_A_VBAT_SENSE);
    //fwDelayUs(10000);
    //sample_and_acc(Avg_exp , (((15-Avg_exp)+4)-2), scale_factor_VbatSense);
    //output.adcVin = output.adcValue ;

    // measure input voltage x 4
    //adcSelectIntInput(ADC_INPUT_VDDS);
    //sample_and_acc(Avg_exp,(((15-Avg_exp)+4)-2),scale_factor_VbatSense);
    //output.adcVbatNoLoad = output.adcValue;

    // Calculate Iinx4 = ( Vbatx4 – Vinx4 ) / 1ohm
    //output.Iin = (output.adcVin - output.adcVbat);
    //output.Iin -= output.adcVbatNoLoad;
    //output.Iin += output.adcVbatLoad;
    output.Iin = output.adcVbatLoad - output.adcVbat;

    if (output.Iin < 0) {
    output.Iin = 0;
    }
    // calculate Ierr/Isetpoint
    accReset();
    // Iin is in the range of 0 to 1024mA
    // this means 1024 x 4 = 0 to 4096
    // saturate:
    if ( output.Iin > 4095) {
    output.Iin = 4095;
    }
    // scale so fullscale of 4096 wil be full 15bit
    output.IinMAC = output.Iin << 4;
    // multiply by 1/(setpoint current x4) - to be on the same scale of 1LSB = 1/4mA
    accMac16s16u(output.IinMAC, ((65536 / setpoint_superfast_charge)*4));
    accGet16(17;output.correctiontemp);

    // aritificail boost for Iin
    //output.Iin >>= 1;
    // Calculate Ierr = Iset_point - Iin
    // Scale Iinsetpoint by 4 to match Iin units
    output.Ierr = output.IinSetpoint << 2;
    output.Ierr -= output.Iin;
    output.Ierr = (output.Ierr >> 2);
    // Measure Vfb (Vcap)
    adcSelectGpioInput(AUXIO_A_VFB_CHG_CONTROL);
    sample_and_acc(Avg_exp , ((15-Avg_exp)+4), scale_factor_Vfb);
    output.adcVcap = output.adcValue;
    // select another input not to interfere with DAC
    adcSelectGpioInput(AUXIO_A_VBAT_SENSE);
    // If Vcap > 3.8V then reset initial charge
    if ( output.adcVcap > cfg.adcVcap3800mV ) {
    state.InitialCharge = 1;
    }
    if (output.adcVcap < cfg.adcVcap3800mV) {
    output.Ready = 0;
    } else {
    output.Ready = 1 ;
    }
    // If Vcap < 3.95v then adjust DAC
    // else next state is Idle
    if (output.adcVcap < cfg.adcVcap3950mV) {

    if (output.IinSetpoint == setpoint_superfast_charge) {
    output.DACCorrection = (output.Ierr + (1 << 5) )>> 6;
    } else if (output.IinSetpoint == setpoint_fast_charge) {
    output.DACCorrection = (output.Ierr + ( 1 <<5 ))>> 6;
    } else if ( output.IinSetpoint == setpoint_minus_10 ) {
    output.DACCorrection = output.Ierr >> 5;
    } else if (output.IinSetpoint == setpoint_zero) {
    output.DACCorrection = output.Ierr >> 5 ;
    } else {
    output.DACCorrection = ( output.Ierr + ( 1 << 3 ) ) >> 4 ;
    }
    // clip DAC correction
    if ( output.DACCorrection > 4 ) {
    output.DACCorrection = 4 ;
    }
    if (output.DACCorrection < (-8) ) {
    output.DACCorrection = (-8);
    }
    // clip DACcorrection at -1
    // if ( output.DACCorrection > 1 ) {
    // output.DACCorrection = 1 ;
    //}

    output.DACValue -= output.DACCorrection;
    // clip DAC value to prevent wraparound
    if ( output.DACValue < 0 ) {
    output.DACValue = 0 ;
    } else if ( output.DACValue > 0xFF ) {
    output.DACValue = 0xFF;
    }
    compaEnable(COMPA_PWRMODE_ANY) ;
    // select COMPA input as DC-DC vontrol
    compaSelectGpioInput(AUXIO_A_VFB_CHG_CONTROL);
    // load new value to DAC
    refdacStartOutputOnCompaIn(output.DACValue);
    // Enable DAC
    refdacEnable(REFDAC_PWRMODE_ANY,REFDAC_REF_DCOUPL_VOUT_LOW);

    refdacWaitForStableOutput() ;
    gpioSetOutput(AUXIO_O_POWER_EN);
    } else {
    state.ChargerState = State_Idle;
    state.InitialCharge = 1;
    }
    adcDisable();
    for ( U16 n = 0; n <80; n++ ) {
    fwDelayUs(10000);
    }

    // }

    }
    fwScheduleTask(1);]]></sccode>
    <sccode name="initialize" init_power_mode="0"><![CDATA[//U16 state.ChargerState ;
    state.ChargerState = State_Idle;
    state.InitialCharge = 0;

    output.SCVersion = 43;
    output.Ready = 0;
    output.RunningTask = TaskCapacitorCharger;
    output.Rreff = DCDCRreff;
    output.Rfeedback = DCDCRfeedback;
    // Schedule the first execution
    fwScheduleTask(1);]]></sccode>
    <sccode name="terminate" init_power_mode="0"><![CDATA[gpioClearOutput(AUXIO_O_POWER_EN);
    refdacStopOutput();
    refdacDisable();]]></sccode>
    <event_trigger active_count="1">0,1,2,3</event_trigger>
    <tt_iter>run_execute,wait_10ms,wait_10ms,wait_10ms,wait_10ms</tt_iter>
    <tt_struct>output.DACCorrection,output.DACValue,output.Ierr,output.Iin,output.adcBatteryVoltage,output.adcVcap,state.ChargerState,state.InitialCharge</tt_struct>
    <rtl_struct></rtl_struct>
    <rtl_task_sel en="1" struct_log_list="output"/>
    </task>
    <task name="Lipo charger">
    <desc><![CDATA[Charger for LiPo battery - it is the app responsibility to activate only one of the two
    1) Cap charger
    2) Lipo charger

    for Lipo
    vbat below 3V - trickle charge at 0.1C
    from 3V to 4.2V constant current charge 0.2C
    stop charging at Vbat 4.0V
    recharge at 3.8V

    charge cycle
    - measure charger plug state
    - measure charger voltage
    - measure input voltage to the DC-DC
    - check LDO flag
    - if no input voltage to DC-DC or voltage is not 4V or LDO flag is down - dont do anything
    - measure charge current
    - disconnect charger
    - check battery voltage
    - if battery voltage below 2V --> do not charge
    - if battery voltage above 4.0V --> do not charge
    - decide on charge current target according to battery voltage
    - if charge current below target , increase DC-DC voltage by 1 DAC unit (decrease DAV voltage by 1 LSB)
    - if charge current above target, decrease DC-DC voltage by 1 DAC unit (increase DAC voltage by 1 LSB)
    - re-activate DC-DC (unless battery voltage below 2V or above 4V)

    - Signal ready on battery voltage > 3.6V]]></desc>
    <tattr name="ArrraySize" type="expr" content="const" scope="task" min="0" max="65535">64</tattr>
    <tattr name="Avg_exp" desc="16" type="expr" content="const" scope="task" min="0" max="65535">4</tattr>
    <tattr name="Avg_num" desc="number of measurements for Vfb" type="expr" content="const" scope="task" min="0" max="65535">16</tattr>
    <tattr name="DAC_Init_Value" desc="init value for low V DC-DC " type="expr" content="const" scope="task" min="0" max="65535">60</tattr>
    <tattr name="DCDCRfeedback" desc="Rfeedback multiplied by 10 - 237K" type="expr" content="const" scope="task" min="0" max="65535">2370</tattr>
    <tattr name="DCDCRreff" desc="rreff multiplied by 10 - 90.9K" type="expr" content="const" scope="task" min="0" max="65535">909</tattr>
    <tattr name="Exit_illigal_state" desc="Exit because illigal state (not idle not active)" type="expr" content="const" scope="task" min="0" max="65535">0x0001</tattr>
    <tattr name="Exit_normal" desc="Normal termination" type="expr" content="const" scope="task" min="0" max="65535">0x00</tattr>
    <tattr name="PreAccThr" desc="threshold for pre accumulator of the DAC" type="expr" content="const" scope="task" min="-32768" max="32767">2</tattr>
    <tattr name="ScaleFactor10K_100K" desc="32FS, 16bit unsigned = 11.53 convert to mV (uncorrected)" type="expr" content="const" scope="task" min="0" max="65535">23650</tattr>
    <tattr name="State_CC_High" desc="State: higher current charge" type="expr" content="const" scope="task" min="0" max="65535">2</tattr>
    <tattr name="State_CC_Low" desc="State: lower current charge" type="expr" content="const" scope="task" min="0" max="65535">1</tattr>
    <tattr name="State_HighBat" desc="Lipo voltage is above 4200mV" type="expr" content="const" scope="task" min="0" max="65535">3</tattr>
    <tattr name="State_Low_Bat" desc="batery voltage is low" type="expr" content="const" scope="task" min="0" max="65535">0</tattr>
    <tattr name="State_NoBat_LowBat" desc="State: no bat or low bat or bat voltage too high state" type="expr" content="const" scope="task" min="0" max="65535">0</tattr>
    <tattr name="TaskLiPoCharger" type="expr" content="const" scope="task" min="0" max="65535">1</tattr>
    <tattr name="Vbat_avg_num" desc="number of ADC measurments on Vbat to average" type="expr" content="const" scope="task" min="0" max="65535">16</tattr>
    <tattr name="cfg.Isetpoint" desc="chrage current setpoint" type="expr" content="struct" scope="task" min="0" max="65535">100</tattr>
    <tattr name="cfg.LDO3800mV" desc="LDO low limit 100K 10K div" type="expr" content="struct" scope="task" min="0" max="65535">329</tattr>
    <tattr name="cfg.LDO4200mV" desc="LDO high limit 100K 10K div" type="expr" content="struct" scope="task" min="0" max="65535">363</tattr>
    <tattr name="cfg.VLipo3000mV" desc="lower threshold for battery charge" type="expr" content="struct" scope="task" min="0" max="65535">3000</tattr>
    <tattr name="cfg.VLipo3600mV" desc="used for ready signal" type="expr" content="struct" scope="task" min="0" max="65535">3600</tattr>
    <tattr name="cfg.VLipo4200mV" desc="4200mV" type="expr" content="struct" scope="task" min="0" max="65535">4200</tattr>
    <tattr name="cfg.Vin4500mV" desc="4500mV input 100K 10K div" type="expr" content="struct" scope="task" min="0" max="65535">4500</tattr>
    <tattr name="cfg.Vin5500mV" desc="5500mV input 100K 10K div" type="expr" content="struct" scope="task" min="0" max="65535">5500</tattr>
    <tattr name="output.DACPreAcc" desc="pre accumulator to lower loop gain" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.DACValue" desc="DAC value to control the cap charger" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.ExPowerPlugStatus" desc="0 - no plug , 1 plug" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.Icharge" desc="Chrage current in mA (not corrected)" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.Ierr" desc="charge current offset from target" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.InternalTemp" desc="Internal temp from BATMON" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="output.LDOFlag" desc="0 - fault 1-OK" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.Ready" desc="Signals the mqtt thread that the capcitor is ready from modem activation" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.Rfeedback" type="expr" content="struct" scope="task" min="0" max="65535">2370</tattr>
    <tattr name="output.Rreff" type="expr" content="struct" scope="task" min="0" max="65535">909</tattr>
    <tattr name="output.RunningTask" type="expr" content="struct" scope="task" min="0" max="65535">TaskLiPoCharger</tattr>
    <tattr name="output.SCVersion" desc="version of the SC FW to be verified by the main processor" type="expr" content="struct" scope="task" min="0" max="65535">10</tattr>
    <tattr name="output.adcBG95Vdd" desc="voltage of BG95 after 100K 10K div" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcBatteryVoltage" desc="battery voltage mV" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcExPowerSense" desc="adc of charger voltage 100K 10K div" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcTemp" desc="temp variable to store ADC measurements" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcValue" desc="storage of adc measurements" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVbat" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVcharger" desc="charger voltage after the diode in mV (not corrected)" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVin" desc="Vbat with DC-DC off" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVlipo" desc="Lipo voltagr in mV (not corrected)" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.adcVlipo1" type="expr" content="struct" scope="task" min="0" max="65535">0</tattr>
    <tattr name="output.voltageDrop" desc="first sample minus last sample" type="expr" content="struct" scope="task" min="-32768" max="32767">0</tattr>
    <tattr name="scale_factor_1" desc="32FS signed = 1" type="expr" content="const" scope="task" min="0" max="65535">2048</tattr>
    <tattr name="scale_factor_VbatSense" desc="32FS signed = 1.0498" type="expr" content="const" scope="task" min="0" max="65535">2150</tattr>
    <tattr name="state.ChargerState" desc="Charger state variable- idle or active" type="expr" content="struct" scope="task" min="0" max="65535">State_CC_Low</tattr>
    <resource_ref name="ADC" enabled="1"/>
    <resource_ref name="AON Domain Functionality" enabled="1"/>
    <resource_ref name="Accumulator-Based Math" enabled="1"/>
    <resource_ref name="Analog Open-Drain Pins" enabled="0"/>
    <resource_ref name="Analog Open-Source Pins" enabled="0"/>
    <resource_ref name="Analog Pins" enabled="1">
    <io_usage name="Vfb_Chg_Control" label="Vfb meas and control">
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO28</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    </io_usage>
    <io_usage name="EX_Power_sense" label="charger voltage 100K 10K div">
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO30</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    </io_usage>
    <io_usage name="Vbat_sense" label="voltage after 1 ohm resistor">
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO26</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    </io_usage>
    <io_usage name="Vdd_BG95" label="BG95 and LiPo voltage">
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO27</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="COMPA" enabled="1"/>
    <resource_ref name="COMPA Event Trigger" enabled="0"/>
    <resource_ref name="COMPB" enabled="0"/>
    <resource_ref name="COMPB Event Trigger" enabled="0"/>
    <resource_ref name="Delay Insertion" enabled="1"/>
    <resource_ref name="Differential Output Pins" enabled="0"/>
    <resource_ref name="Digital Input Pins" enabled="1">
    <io_usage name="LDO_Flag" label="Signal LDO good low fault">
    <uattr name="Configuration on initialization">1</uattr>
    <uattr name="Configuration on uninitialization">-1</uattr>
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO9</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    <io_usage name="EX_Power_Plug" label="shorted to ground when no plug">
    <uattr name="Configuration on initialization">1</uattr>
    <uattr name="Configuration on uninitialization">-1</uattr>
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO8</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Digital Open-Drain Pins" enabled="1">
    <io_usage name="DCDCVoltagBoost" label="resistor to ground for higher DCDC volt">
    <uattr name="Configuration on uninitialization">-1</uattr>
    <uattr name="Output drive strength">0</uattr>
    <uattr name="Output value on initialization">1</uattr>
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO29</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Digital Open-Source Pins" enabled="0"/>
    <resource_ref name="Digital Output Pins" enabled="1">
    <io_usage name="POWER_EN" label="enable the DCDC converter">
    <uattr name="Configuration on uninitialization">-1</uattr>
    <uattr name="Output drive strength">0</uattr>
    <uattr name="Output value on initialization">0</uattr>
    <uattr name="Pin count">0</uattr>
    <uattr name="Pin/0000">DIO11</uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Dynamic Power Control" enabled="1"/>
    <resource_ref name="GPIO Event Trigger" enabled="0"/>
    <resource_ref name="I2C Master" enabled="0">
    <rattr name="SCL frequency">0</rattr>
    <rattr name="SCL stretch timeout">1</rattr>
    <io_usage>
    <uattr name="SCL pin/0000"></uattr>
    <uattr name="SDA pin/0000"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="ISRC" enabled="0"/>
    <resource_ref name="LCD Controller" enabled="0">
    <rattr name="Waveform state count">8</rattr>
    <io_usage>
    <uattr name="Output drive strength">0</uattr>
    <uattr name="Pin count">20</uattr>
    <uattr name="Pin/0000"></uattr>
    <uattr name="Pin/0001"></uattr>
    <uattr name="Pin/0002"></uattr>
    <uattr name="Pin/0003"></uattr>
    <uattr name="Pin/0004"></uattr>
    <uattr name="Pin/0005"></uattr>
    <uattr name="Pin/0006"></uattr>
    <uattr name="Pin/0007"></uattr>
    <uattr name="Pin/0008"></uattr>
    <uattr name="Pin/0009"></uattr>
    <uattr name="Pin/0010"></uattr>
    <uattr name="Pin/0011"></uattr>
    <uattr name="Pin/0012"></uattr>
    <uattr name="Pin/0013"></uattr>
    <uattr name="Pin/0014"></uattr>
    <uattr name="Pin/0015"></uattr>
    <uattr name="Pin/0016"></uattr>
    <uattr name="Pin/0017"></uattr>
    <uattr name="Pin/0018"></uattr>
    <uattr name="Pin/0019"></uattr>
    <uattr name="Pin/0020"></uattr>
    <uattr name="Pin/0021"></uattr>
    <uattr name="Pin/0022"></uattr>
    <uattr name="Pin/0023"></uattr>
    <uattr name="Pin/0024"></uattr>
    <uattr name="Pin/0025"></uattr>
    <uattr name="Pin/0026"></uattr>
    <uattr name="Pin/0027"></uattr>
    <uattr name="Pin/0028"></uattr>
    <uattr name="Pin/0029"></uattr>
    <uattr name="Pin/0030"></uattr>
    <uattr name="Pin/0031"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Math and Logic" enabled="1"/>
    <resource_ref name="Multi-Buffered Output Data Exchange" enabled="0">
    <rattr name="Buffer count">2</rattr>
    <rattr name="Indicate overflow at buffer check">1</rattr>
    <rattr name="Indicate overflow at buffer switch">0</rattr>
    <rattr name="Prevent overflow at buffer switch">1</rattr>
    </resource_ref>
    <resource_ref name="Peripheral Sharing" enabled="0"/>
    <resource_ref name="Pulse Counter" enabled="0"/>
    <resource_ref name="RTC Multi-Event Capture" enabled="0"/>
    <resource_ref name="RTC-Based Execution Scheduling" enabled="1"/>
    <resource_ref name="Reference DAC" enabled="1"/>
    <resource_ref name="Run-Time Logging" enabled="0"/>
    <resource_ref name="SPI Chip Select" enabled="0"/>
    <resource_ref name="SPI Data Transfer" enabled="0">
    <rattr name="Bit rate">0</rattr>
    <rattr name="MISO configuration when inactive">-1</rattr>
    <rattr name="MOSI configuration on initialization">0</rattr>
    <rattr name="MOSI configuration on uninitialization">0</rattr>
    <rattr name="SCLK configuration on initialization">0</rattr>
    <rattr name="SCLK configuration on uninitialization">0</rattr>
    <io_usage>
    <uattr name="MISO pin/0000"></uattr>
    <uattr name="MOSI pin/0000"></uattr>
    <uattr name="SCLK pin/0000"></uattr>
    </io_usage>
    </resource_ref>
    <resource_ref name="Signal Observation" enabled="0"/>
    <resource_ref name="System CPU Alert" enabled="1"/>
    <resource_ref name="System Event Trigger" enabled="0"/>
    <resource_ref name="TDC" enabled="0"/>
    <resource_ref name="Timer 0" enabled="0"/>
    <resource_ref name="Timer 0 Event Trigger" enabled="0"/>
    <resource_ref name="Timer 1" enabled="0"/>
    <resource_ref name="Timer 1 Event Trigger" enabled="0"/>
    <resource_ref name="Timer 2" enabled="0"/>
    <resource_ref name="Timer 2 Event Trigger" enabled="0"/>
    <resource_ref name="UART Emulator" enabled="0">
    <rattr name="RX buffer size">64</rattr>
    <rattr name="Required idle period before enabling RX">11</rattr>
    <rattr name="TX buffer size">64</rattr>
    <io_usage>
    <uattr name="RX pin/0000"></uattr>
    <uattr name="TX pin/0000"></uattr>
    </io_usage>
    </resource_ref>
    <sccode name="event0" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="event1" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="event2" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="event3" init_power_mode="0"><![CDATA[]]></sccode>
    <sccode name="execute" init_power_mode="2"><![CDATA[macro sample_and_acc(avg_shift , result_shift, factor) {
    output.adcTemp = 0;
    accReset();
    for (U16 n = 0; n < (1 << avg_shift ); n++) {
    adcGenManualTrigger();
    adcReadFifo(output.adcTemp);
    accMac16s16u(output.adcTemp, factor);
    }
    accGet16(result_shift; output.adcValue );
    }

    //charge cycle
    // - measure charger plug state
    //- measure charger voltage
    //- measure input voltage to the DC-DC
    //- check LDO flag
    //- if no input voltage to DC-DC or voltage is not 4V or LDO flag is down - do nothing
    //- measure charge current
    // measure charger output voltage
    //- disconnect charger
    //- check battery voltage
    //- if battery voltage below 2V --> do not charge
    //- if battery voltage above 4.0V --> do not charge
    //- decide on charge current target according to battery voltage
    //- if charge current below target , increase DC-DC voltage by 1 DAC unit (decrease DAV voltage by 1 LSB)
    //- if charge current above target, decrease DC-DC voltage by 1 DAC unit (increase DAC voltage by 1 LSB)
    //- re-activate DC-DC (unless battery voltage below 2V or above 4V)
    //
    //- Signal ready on battery voltage > 3.6V

    // make sure DC-DC is diabled to let teh 22ucap discharge
    gpioClearOutput(AUXIO_O_POWER_EN);


    // set DC DC voltage boost mode , by shorting R18(220K) to ground
    gpioClearOutput(AUXIO_XD_DCDCVOLTAG_BOOST);

    // measure internal temp
    sysGetBatmonTemp( output.InternalTemp);
    // convert from 1/4 degrees to degrees
    output.InternalTemp >>= 2 ;

    // check charger plug state
    gpioEnableInputBuf(AUXIO_I_EX_POWER_PLUG);
    gpioGetInputValue(AUXIO_I_EX_POWER_PLUG;output.ExPowerPlugStatus);
    gpioDisableInputBuf(AUXIO_I_EX_POWER_PLUG);

    // measure charger voltage
    // ADC enable , with the right S&H time
    adcEnableSync(ADC_REF_FIXED, ADC_SAMPLE_TIME_170_US, ADC_TRIGGER_MANUAL);

    // measure battery voltage for telemetry
    adcSelectIntInput(ADC_INPUT_VDDS);
    sample_and_acc(2,13,scale_factor_1);
    output.adcBatteryVoltage = output.adcValue;


    // Configure EX_power_Sense as analog input
    adcSelectGpioInput(AUXIO_A_EX_POWER_SENSE);
    //for ( U16 n=0 ; n < 10; n++){
    //fwDelayUs(10000);
    //}
    sample_and_acc(Avg_exp , ((15-Avg_exp)+5), ScaleFactor10K_100K);
    output.adcExPowerSense = output.adcValue<<1;

    adcSelectGpioInput(AUXIO_A_VBAT_SENSE);
    fwDelayUs(10000);
    sample_and_acc(Avg_exp , ((15-Avg_exp)+5), scale_factor_VbatSense);
    output.adcVbat = output.adcValue<<1;

    //// check LDO power good signal
    //// check charger plug state
    gpioEnableInputBuf(AUXIO_I_EX_POWER_PLUG);
    gpioGetInputValue(AUXIO_I_LDO_FLAG;output.LDOFlag);
    gpioDisableInputBuf(AUXIO_I_EX_POWER_PLUG);

    // measure battery voltage
    adcSelectGpioInput(AUXIO_A_VDD_BG95);
    fwDelayUs(10000);
    sample_and_acc(Avg_exp , ((15-Avg_exp)+4), ScaleFactor10K_100K);
    output.adcVlipo = output.adcValue;

    //// check power plug
    ifnot (output.ExPowerPlugStatus == 1) {
    gpioClearOutput(AUXIO_O_POWER_EN);
    output.DACValue = DAC_Init_Value;
    } else ifnot (output.LDOFlag == 1) { // check LDO flag
    gpioClearOutput(AUXIO_O_POWER_EN);
    output.DACValue = DAC_Init_Value;
    } else ifnot (output.adcExPowerSense > cfg.Vin4500mV) { // check charger voltage low limit
    gpioClearOutput(AUXIO_O_POWER_EN);
    output.DACValue = DAC_Init_Value;
    } else ifnot (output.adcExPowerSense < cfg.Vin5500mV) { // check charger voltage high limit
    gpioClearOutput(AUXIO_O_POWER_EN);
    output.DACValue = DAC_Init_Value;
    } else {
    // measure charge current by measuing charger output voltage (after the diode) while active and then when disabled - divide by 1 ohm (2 ohm in the dev setup)

    // give some time for the 22u cap to discharge
    // for ( U16 n=0 ; n < 10; n++){
    // fwDelayUs(10000);
    // }

    // first activation of DAC
    if (state.ChargerState < State_CC_Low) {
    compaEnable(COMPA_PWRMODE_ANY) ;
    // select COMPA input as DC-DC vontrol
    compaSelectGpioInput(AUXIO_A_VFB_CHG_CONTROL);
    // load new value to DAC
    refdacStartOutputOnCompaIn(output.DACValue);
    // Enable DAC
    refdacEnable(REFDAC_PWRMODE_ANY,REFDAC_REF_DCOUPL_VOUT_LOW);
    // refdacWaitForStableOutput() ;
    }


    // ----------------- fix DAC value for debug of input inrush --------
    // turn on DAC
    // Enable DAC
    // redacbufenable();
    compaEnable(COMPA_PWRMODE_ANY) ;
    // select COMPA input as DC-DC vontrol
    compaSelectGpioInput(AUXIO_A_VFB_CHG_CONTROL);
    // load new value to DAC
    refdacStartOutputOnCompaIn(DAC_Init_Value);
    // Enable DAC
    refdacEnable(REFDAC_PWRMODE_ANY,REFDAC_REF_DCOUPL_VOUT_LOW);
    // refdacWaitForStableOutput() ;
    // load new value to DAC
    fwDelayUs(10000);

    // adcSelectGpioInput(AUXIO_A_VDD_BG95);
    // fwDelayUs(10000);
    // sample_and_acc(Avg_exp , ((15-Avg_exp)+4), ScaleFactor10K_100K);
    // output.adcVlipo1 = output.adcValue;

    // Turn on DC-DC
    gpioSetOutput(AUXIO_O_POWER_EN);
    state.ChargerState = State_CC_Low;
    fwDelayUs(10000);
    refdacChangeOutputValue(output.DACValue);
    // refdacWaitForStableOutput() ;

    // stabilization time
    for ( U16 n=0 ; n < 10; n++){
    fwDelayUs(10000);
    }

    adcSelectGpioInput(AUXIO_A_VDD_BG95);

    sample_and_acc(Avg_exp , ((15-Avg_exp)+4), ScaleFactor10K_100K);
    output.adcVcharger = output.adcValue;

    output.Icharge = (output.adcVcharger - output.adcVlipo);

    // no need to divide for the final baord with 1 ohm
    // divide by 2 ohm to get charge current in mA
    //output.Icharge >>= 1;
    if ( output.Icharge < 0) {
    output.Icharge = 0;
    }
    output.Ierr = output.Icharge - cfg.Isetpoint;
    // reconfig pin as analog output
    // gpioCfgMode(AUXIO_A_VFB_CHG_CONTROL,GPIO_MODE_INPUT);


    // check for condition where there is no need to charge
    if (output.adcVlipo > cfg.VLipo3000mV) {
    if (output.adcVlipo < cfg.VLipo4200mV ) {
    // target charge current is set to 100mA (~0.1C)
    if ( output.Ierr > 0 ) {
    output.DACPreAcc +=1;
    if ( output.DACPreAcc > PreAccThr) {
    output.DACPreAcc = 0;
    output.DACValue += 1;
    }
    } else if (output.adcVcharger < cfg.VLipo4200mV) {
    output.DACPreAcc -= 1;
    if ( output.DACPreAcc < (-PreAccThr) ) {
    output.DACPreAcc = 0;
    output.DACValue -= 1;
    }
    }

    }else{
    state.ChargerState = State_HighBat;
    output.DACValue = DAC_Init_Value;
    gpioClearOutput(AUXIO_O_POWER_EN);
    }
    } else {
    state.ChargerState = State_NoBat_LowBat;
    output.DACValue = DAC_Init_Value;
    gpioClearOutput(AUXIO_O_POWER_EN);
    }
    }


    if (output.adcVlipo > cfg.VLipo3000mV) {
    if (output.adcVlipo < cfg.VLipo4200mV ) {
    state.ChargerState = State_CC_Low;
    } else {
    state.ChargerState = State_HighBat;
    }
    } else {
    state.ChargerState = State_NoBat_LowBat;
    }


    ifnot (state.ChargerState > State_NoBat_LowBat ) {
    output.Ready = 0;
    } else if (output.adcVlipo > cfg.VLipo3600mV) {
    output.Ready = 1;
    } else {
    output.Ready = 0;
    }
    adcDisable();
    // keep sensor controller running to prevent low power mode and DAC disable
    for ( U16 n = 0; n <10; n++ ) {
    fwDelayUs(10000);
    }
    output.RunningTask = TaskLiPoCharger;
    fwScheduleTask(1);]]></sccode>
    <sccode name="initialize" init_power_mode="2"><![CDATA[state.ChargerState = State_NoBat_LowBat;

    output.DACValue = DAC_Init_Value ;

    output.SCVersion = 43;
    output.Ready = 0;

    output.DACPreAcc = 0;
    output.DACValue = DAC_Init_Value;
    output.Rreff = DCDCRreff;
    output.Rfeedback = DCDCRfeedback;

    // Schedule the first execution
    fwScheduleTask(1);]]></sccode>
    <sccode name="terminate" init_power_mode="2"><![CDATA[gpioClearOutput(AUXIO_O_POWER_EN);
    refdacStopOutput();
    refdacDisable();]]></sccode>
    <event_trigger active_count="1">0,1,2,3</event_trigger>
    <tt_iter>run_execute,wait_1s</tt_iter>
    <tt_struct>output.DACValue,output.Icharge,output.Ierr,output.LDOFlag,output.adcExPowerSense,output.adcVbat,output.adcVcharger,output.adcVlipo,state.ChargerState</tt_struct>
    <rtl_struct></rtl_struct>
    <rtl_task_sel en="1" struct_log_list="output"/>
    </task>
    </project>

  • Hi, 

    It would be helpful to get the ARM side of the project as well to test this. I did not see any immediate issue other than that you have not enabled "peripheral sharing" which might or might not be an issue (depending on what you do on the ARM side).

  • Hi Yaron,

    I also noticed you are setting the "lipo" task to run as "low-power" mode. This would not be supported as you expect to use the ADC. Please have a look at what peripherals you can use in either mode in the help section: Project Panel -> Power and Clock Settings Panel.

  • Hi M-W

    Thanks !!
    Apparently the task was waiting for the ADC which resulted a reset.