Skip to main content
Visitor II
March 1, 2023
Question

Hello! I am getting constant ADC value through SPI when I add a debug message using UART on STM32F205 with CMSIS RTOS.

  • March 1, 2023
  • 2 replies
  • 1183 views

We have interfaced CS5532 using SPI which is initialized in continuous conversion mode. We read the ADC count in a task being called every 160 ms. When I add following statements in the task, the ADC count read from CS5532 becomes constant.

sprintf(Debug,",%ld,%f,%d\n\r",ADC_Sum,CurrentWt,StableFlag);

uart0Puts(Debug);

Removing above code solves the problem. I have also noticed that adding/removing similar debug messages in/from other parts of code also creates same problem. I commented similar statements from a function that is not getting called with current settings in EEPROM. This also caused same problem. The code works fine when no such debug message is used. I also checked the stack usage using ST Link debugger and found it to be within limits. Any kind insights on how to approach this problem would really be helpful. Thank you.

    This topic has been closed for replies.

    2 replies

    Graduate II
    March 1, 2023

    You need to post your uart0Puts function and any other functions it may call. Post what is in the task as well.

    TBhil.1Author
    Visitor II
    March 3, 2023

    The code is as follows:

    void Timer0_Procedure(void const *arg)// __task

    {

    Timer0_Th_Id = osThreadGetId();

    Timer_Sem = osSemaphoreCreate(osSemaphore(Timer_Sem), 0);

    while(1)

    {

    osSemaphoreWait(Timer_Sem, osWaitForever);

    // if(Timer0Event.status == osEventSignal)

    {

    AdcReadCounter++;

    if(AdcReadCounter == 2) //160ms -> 6.25 SPS

    {

    AdcReadCounter = 0;

    ADC_DATA = CS5532_Read_ADC(); // read ADC data

    // sprintf(Debug, "%ld\r\n", ADC_DATA);

    // uart0Puts(Debug);

    osSignalSet(tsk1,0x0002);//isr_evt_set (0x0002,tsk1); // event set for processing ADC data

    }

    c160_counter++;

    c160_ADC=1;

    Global_80ms=1;

    if(std_cal_stable_counter != 0xFFFF) //counter increment moved in ISR in order to solve std cal getting aborted after Lin 9 issue

    {

    if(StableFlag)

    std_cal_stable_counter++;

    else

    std_cal_stable_counter = 0;

    }

    if(c160_counter==2)   //if part added by RAHUL to sync with 160ms timing

    {

    LCM_Display_Flag = 1; // set LCM display flag

    autocal_timer++;

    AutoOff_timer++;

    c160_counter =0;

    iStability_Density_Counter++;   //added by rahul

    if(iStability_Density_Counter>=282)   // 282 to 141

    {

    cStability_time_den=1;

    iStability_Density_Counter=0;

    }  //end added by rahul

    }

    if (Cal_trigger_time || cal_temp_trigger || cal_trigger_after15min || cal_trigger_after5_20min)

    {

    cIntCal_counter++;

    }

    if (Start_15min_Counter == 1)

    {

    cIntCal_15min_counter++;

    if (cIntCal_15min_counter >= INTCAL_AFTER_SUCESSFUL_CAL)

    {

    cal_trigger_after15min = 1;

    LCM_DisplaySymbol(LCM_CODE_INCAL);

    cIntCal_15min_counter = 0;

    }

    }

    if(Autocal_Enable==1)

    {

    if (Start_Five_Twenty_Min_Counter==1)

    {

    Intcal_After_5min_20min++;

    }

    if (Intcal_After_5min_20min >= Intcal_5Min20Min_Band)

    {

    cal_trigger_after5_20min=1;

    }

    if ((Intcal_After_5min_20min>=18750)||(Cal_trigger_time)||(cal_temp_trigger)||(cal_trigger_after15min))// temp cahnged for testing  //18750 25 min

    {

    Intcal_5Min20Min_Band = 0;

    Start_Five_Twenty_Min_Counter=0;

    Intcal_After_5min_20min=0;

    // cal_trigger_after5_20min=0;

    Perform_5min_20min_Intcal=0;

    }

    }

    if(QMS_Page2r_Send)

    {

    QMS_Page2r();

    QMS_Page2r_Send = 0;

    }

    if(QMS_Unstable_ADC_Weight_Temp)

    {

    sprintf(Debug,"&A:%ld&W:%lf&T:%0.3lf&S:%d\r\n",ADC_Sum,Simple_Weighing_Weight,Current_Temp,StableFlag);

    uart0Puts(Debug);

    }

    if(QMS_MdNo_SrNo_Send)

    {

      QMS_MdNo_SrNo_Send = 0;

      QMS_MdNo_SrNo();

    }

    }

    }

    }

    This is a task in which ADC read function is called. The semaphore in this task is released from a timer ISR after every 80 ms. ADC is being read every 160 ms. ADC_Data_Process task is activated after this.

    void ADC_Data_Process(void)

    {

    //---------------------added by rahul------------------------------------------------

    ADC_RECORD *ptrAdcRecord ; //Poonam

    osMutexWait(ADC_Protect_id, osWaitForever);//os_mut_wait (ADC_Protect, 0xffff);

    //---------------------new vibration algo------------------------------------------------

     ptrAdcRecord = validate(ADC_DATA);  //its on in backgroud

      if(SWM_Dosing==TRUE)

    ptrAdcRecord->mSummedAdc = getSummedAdc(ptrAdcRecord->mRawAdc); //ADC value After Noise Elimination

    else

     ptrAdcRecord->mSummedAdc = getSummedAdc(ADC_DATA); //Noise Elimination Excluded ADC value

    getAverage(ptrAdcRecord, StableFlag);

    ADC_Sum = ptrAdcRecord->mAvgAdc;

    sprintf(Debug,",%ld,%f,%d\n\r",ADC_Sum,CurrentWt,StableFlag);

    uart0Puts(Debug);

    //------------------------------------------------------stability----------------------------------------------------------------------

    if(CheckStability(ADC_Sum))  //was 49, changed by rahul

    {

    StableFlag = 1;  // stable flag set

    cCreep_Counter++;

    if(cCreep_Counter >= NINE_SEC) //TEN_SEC =10000/80= 125 counts 112

    {   

    cEnter_Creep = 1;

    cCreep_Counter = NINE_SEC; //after 9 second stable mark it enters here

    }

    else

    {

    StableFlag=0;

    cCreep_Counter=0;

    cEnter_Creep=0;

    cSaved_Wt_Flag=0;

    cCreep_Done = 0;

    if( fabs(Creep_Weight) < (WeighingAccuracy_Local*2)) //creep weight is less than 0.0002

    {

    Creep_Weight = 0;  //make creep weight zero

    previous_weight_Accumulated_Creep = 0;

    now_Creep_Weight = 0;

    }

    else

      {

    previous_weight_Accumulated_Creep = 1;

    if(yes_in)

    {

    if( fabs(now_Creep_Weight) < (WeighingAccuracy_Local*2))

    {

    Creep_Weight = Creep_Weight - now_Creep_Weight;

    //now_Creep_Weight = 0;

    }

    else

    ;//previous_weight_Accumulated_Creep = 1;

    yes_in=0;

    now_Creep_Weight = 0;

    }

    }

    }

    Temp_Read_Count++;

    if(Temp_Read_Count >= 2)

    {

    osSignalSet(tsk2, 0x0004);//os_evt_set (0x0004,tsk2); // for temperature read event set after 2 ADC data

    Temp_Read_Count = 0;

    // Read_Temp = 1;

    osMutexRelease(ADC_Protect_id);//os_mut_release (ADC_Protect);   // mutex released

    void uart0Puts(const char *string)

    {

    char ch;

    const char *dummy_string;

    if(!Feature_Setting.printer_Set) // for Printer settings

    {

    return;

    }

    if(PrinterState == PRINTSTATE_OFF)

    return; 

    osSemaphoreWait(semaphore1, osWaitForever);//os_sem_wait (semaphore1, 0xffff); // wait for semaphore indefinite

    dummy_string = string;

    while ((ch = *dummy_string)!=0)

    {

    if (putchr(ch)<=NULL_CHARACTER) break; // check end of string

    if ((ch == '\n' || ch == '\r') && (cWinPrint_Enable == 1)) break; //anup windows

    dummy_string++;

    }

    osSemaphoreRelease(semaphore1);//os_sem_send (semaphore1); // release semaphore

    }

    unsigned char putchr (unsigned char ch) 

    {

    while(!(USART_GetFlagStatus(USART3, USART_FLAG_TXE)));

    USART_SendData(DEBUG_USART,ch);

    return 1;//(USART3->DR = (ch & (uint16_t)0x01FF));

    }