Post Go back to editing

error in the library AdcLib.c

Category: Hardware
Product Number: ADuCM362

if the AdcRng function is called with the parameter iCode=0, the iCode can never be set to 1 because the mask is probably wrong i1 = pPort->CON & 0xfffcfff;

uint32_t AdcRng(ADI_ADC_TypeDef *pPort, uint32_t iRef, uint32_t iGain, uint32_t iCode)
{
uint32_t i1 = 0;

i1 = pPort->MDE & 0xff07;
pPort->MDE = i1 | ((iGain & 0xf8));
i1 = pPort->CON & 0xfffcfff;
if(iCode) {
i1 |= 1 << 18;
}

pPort->CON = i1 | ((iRef & 0x3000));
return 1;
}

- ADCxCON.18
- 0 or ADCCON_ADCCODE_INT for bipolar (C type int) result.
- 1 or ADCCON_ADCCODE_UINT to truncate negative values to 0.


bit 18 already remains at 0 regardless of the iCode parameter

Rastislav Kotesovsky

Parents
  • Hi Rastislav,

    I'm not sure I understand why you say mask  i1 = pPort->CON & 0xfffcfff; is wrong, that mask is only for read purposes into the variable i1.

    Bit 18 of variable i1 is set in the following step depending on the value of iCode:

    if(iCode) {
    i1 |= 1 << 18;
    }

    This is then written into the ADCCON register by the instruction

    pPort->CON = i1|((iRef&0x3000));

  • Hi Rgrau,

    I wrote it the other way around, if I call the AdcRng function with the iCode=1 parameter, the ADCxCON.18 bit is correctly set to 1, in the next call with iCode=0, the ADCxCON.18 bit is no longer set to 0 because there is nothing to set it. The mask 0xfffcfff leaves bit 18 in i1 at 1, so bit 18 cannot be changed to 0 regardless of the iCode parameter.

    If I change the mask to 0xffbcfff, the function works because bit 18 is reset after i1 = pPort->CON & 0xffbcfff; and the following is written because iCode=0 and

    if(iCode) {
    i1 |= 1 << 18;
    }

    is skipped.
    However, I don't know what the purpose of the mask 0xfffcfff is, so I would not like to manually modify this function so that it works correctly.

  • Thanks! I see now what you mean. There is a "set" instruction for bit 18, but not a "clear" instruction, thanks for bringing this up to our attention.

    Can you try changing the following code in the driver

    if(iCode) {
    i1 |= 1 << 18;
    }

    For the following?

    if(iCode)
    {
    i1 |= 1 << 18;
    }
    else
    {
    i1 &= ~(1 << 18);
    }

    Regards,

    Raquel.

  • Thank you for the solution, but I would still be interested in why the mask 0xfffcfff is used at all, which does not match e.g. with the state of the register pADI_ADCx_CON, what the author of the library meant. All library files use a lot of magic numbers without comments. Are there no other libraries for ADUCM36x?

    Rastislav Kotesovsky

  • Hi,

    That mask is for bits 13:12 (ADCREF), which then are written with the desired new configuration passed through the variable iRef in

    pPort->CON = i1 | ((iRef & 0x3000));

    This is the only driver library for the ADuCM362

Reply Children
No Data