Codes that take into consideration temporary anomalies

This post describes a code that take into consideration temporary anomalies.

Embedded products may be subjected to harsh operating environments and have long durability periods. Therefore, temporary abnormalities that do not result in failures may occur due to the occurrence of noise and other disturbance factors, hardware element degradation, and other factors. There are products that include codes that take such cases into account.

Example1 : Overflow check for addition and underflow check for subtraction

unsigned char func_add(unsigned char x1, unsigned char x2)
{
    unsigned short y = (unsigned short)x1 + x2;
    if( y > 0x00ff )
    {
        y = 0x00ff;
    }
    return((unsigned char)y);
}

unsigned char func_sub(unsigned char x1, unsigned char x2)
{
    signed short y = (signed short)x1 - x2;
    if( y < 0x0000 )
    {
        y = 0x0000;
    }
    return((unsigned char)y);
}


void func(void)
{
    unsigned char i=X=0;

    for( i = 0; i < 10 ; i++ )
    {
           X = func_add(X, i);  // X=45
    }

    for( i = 0; i < 10 ; i++ )
    {
           X = func_sub(X, i);  // X=0
    }
}

Example 2: AD Conversion Bit Larger AD conversion value

In this case, there are two possible anomalies: consult with the MCU manufacturer to determine which of the two measures is best.

Abnormality of ADC conversion unit

unsigned short func(void)
{
    unsigned short tmp=0;
    tmp = 0x03ff & adc_read(ch0);
    return(tmp);
}

HAL processing anomaly

#define mac_lmt_up(val,lmt)  (((val)>(lmt))?(lmt):(val) )

unsigned short func(void)
{
    unsigned short tmp=0;
    tmp = mac_lmt_up(adc_read(ch0), 0x3ff);
    return(tmp);
}
OSS-ECAL English
error: Content is protected !!