• MPLAB XC16
  • Variable doesn't update in callback function
2020/11/10 01:13:04
lucciodelavegas
Hi,
 
can someone explain this problem? my variable "sixD_src" is not updated when used in a callback.
 
This works with a flag and variable "sixd_src" used in main
bool accFlag;

void EX_INT1_CallBack(void)
{
    accFlag = true;
}

int main(void)
{
     uint8_t sixD_src;
 
     accFlag = false;
    while (! accFlag) {}
 
    if (accFlag)
    {
        // Check acceleromter orientation
        LIS2DW12_ReadByteRegister(LIS2DW12_REG_SIXD_SRC, &sixD_src);
    }

    if (sixD_src != 0)
   {
       // Do job...
   }
}

 
 
and this doesn't with variable "sixD_src" used in callback
uint8_t sixD_src;

void EX_INT1_CallBack(void)
{
    // Check acceleromter orientation
     LIS2DW12_ReadByteRegister(LIS2DW12_REG_SIXD_SRC, &sixD_src);
}

int main(void)
{
    sixD_src = 0;
    while (sixD_src == 0) {}

    if (sixD_src != 0)
    {
        // Do job...
    }
}

 
Thank you for your help!
 
2020/11/10 01:17:05
al_bin
Hello, search for keyword "volatile" in C guide.
 
 
2020/11/10 01:30:41
ric
As above, try changing
uint8_t sixD_src;
to
volatile uint8_t sixD_src;
2020/11/10 01:44:27
NKurzman
Try:

volatile bool accFlag;
variables shared between main and Interrupts , or two different interrupts with different Priority need to be volatile

Also you need to be aware if the operation is atomic. (yours appears to be.)
And what ric said.
2020/11/10 02:02:02
al_bin
The "Priority" has nothing to do with it.
With equal priority volatile is also needed.
2020/11/10 02:07:14
lucciodelavegas
I already tried to put "volatile uint8_t sixD_src"
 
It doesn't work too and I get this warning:
 
main.c:65:5: warning: passing argument 2 of 'LIS2DW12_ReadByteRegister' discards qualifiers from pointer target type
LIS2DW12.h:139:6: note: expected 'uint8_t *' but argument is of type 'volatile uint8_t *'
2020/11/10 03:43:36
du00000001
Why not make the call parameter a pointer to volatile uint8_t type?
 
PEBKAC!
2020/11/10 05:46:04
lucciodelavegas
I can't make the call parameter a pointer to volatile uint8_t type because the function is used elsewhere with non volatile uint8_t.
If I write a value to the variable in the callback (sixD_src = 1;), the value is modified and I can get it in the main()
 
the function used in the callback "LIS2DW12_ReadByteRegister(LIS2DW12_REG_SIXD_SRC, &sixD_src)" will read I2C using interrupts. I'm using I2C interrupts in an interrupt callback, am I'm wrong? or I should define interrupt priority or I just miss something....
 
I use I2C from MCC, my function above derived from MMC example:
 
bool LIS2DW12_ReadByteRegister(uint8_t dataAddress, uint8_t *pData)
{
    I2C1_TRANSACTION_REQUEST_BLOCK readTRB[2];

    // we need to create the TRBs for a random read sequence to the EEPROM
    // Build TRB for sending address
    I2C1_MasterWriteTRBBuild(    &readTRB[0],
                                    &dataAddress,
                                    1,
                                    LIS2DW12_DEFAULT_ADDRESS);
    // Build TRB for receiving data
    I2C1_MasterReadTRBBuild(     &readTRB[1],
                                    pData,
                                    1,
                                    LIS2DW12_DEFAULT_ADDRESS);

    return I2C_SendTransaction(2, readTRB);
}  
 
2020/11/10 05:57:38
lucciodelavegas
I solved this using interrupt priority !!
 
level 1 for EXT_INT1
level 2 for I2C
 
Thanks all for your replies
2020/11/10 06:15:58
Mysil
Hi,
"doesn't work" or you get the warning, or both?
 
The volatile specifier is needed in main, because of the tight loop testing the value.
In function called from interrupt handler,
the variable vill be loaded at least once, and updated at least once, even if not specified to be volatile.
 
You may get away from the warning by casting the address argument: (uint8_t *)&sixD_src
    // Check acceleromter orientation
     LIS2DW12_ReadByteRegister(LIS2DW12_REG_SIXD_SRC, (uint8_t *)&sixD_src);



Or you may carry the correct declaration all the way thru, as suggested above.
 
    Mysil
12
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account