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.

suddenly going to infinite loop

Hello,

          I want to store the data in the sdcard in .csv format.i was successfully create a new file .In the first row i write the  heading successfully and then  i write  simple count number ,for the two row it was write work correctly but after that it is going to infinite loop.

What is the  problem am not able to understand.

  • Hello Darshan,

    Infinite loop of what? A code post with where the loop is happening would be useful.

    Regards

    Amit

  • #include <string.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/fpu.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/rom.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/systick.h"
    #include "grlib/grlib.h"
    #include "utils/cmdline.h"
    #include "utils/uartstdio.h"
    #include "fatfs/src/ff.h"
    #include "fatfs/src/diskio.h"
    #include "drivers/cfal96x64x16.h"
    #include "driverlib/timer.h"
    #include "driverlib/debug.h"
    #include "driverlib/adc.h"
    
    #define PATH_BUF_SIZE           80
    
    #define CMD_BUF_SIZE            64
    
    static FATFS FatFs;
    static FIL Fil;
    FRESULT fr;
    
    DWORD size;
    unsigned int bw,count,br,i;
    unsigned char Second,Hour,Min,Date,Month,Year;       //received data from the rtc module
    char second[],hour[],min[],date[],month[],year[];   //variable which are use forr the converting to string
    int len,count2;
    int count1=0;
    char result[];
    unsigned int counter=0;
    
    unsigned int indata16, k,indata; //16 bit variables
    unsigned int Hum, Temp;  //8 bit variables
    unsigned char Hum1[],Temp1[];  //8 bit variables
    unsigned int bufferHum[], bufferTemp[];
    unsigned int mydatac1;
    unsigned int datacount;
    
    #define CLK   GPIO_PIN_1
    #define RDY   GPIO_PIN_2
    #define DAT   GPIO_PIN_3
    
    unsigned long ulADC0Value[4];
    unsigned long ulADC0Value1[4];
    volatile unsigned long ulTempAvg;
    int ulTempValueC;
    int ulTempValueF;
    
    int bufferulTempValueC[];
    int bufferulTempValueF[];
    
    volatile unsigned long ulTempAvg1;
    char ulTempValueC1[];
    char ulTempValueF1[];
    
    void
    SysTickHandler(void)
    {
        //
        // Call the FatFs tick timer.
        //
        disk_timerproc();
    }
    
    //
    // The error routine that is called if the driver library encounters an error.
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, unsigned long ulLine)
    {
    }
    #endif
    
    // The program main function.  It performs initialization, then runs a command
    // processing loop to read commands from the console.
    
    char *strrev(char *str){
        char c, *front, *back;
    
        for(front=str,back=str+strlen(str)-1;front < back;front++,back--){
            c=*front;*front=*back;*back=c;
        }
        return str;
    }
    
    char *itoa(int v, char *buff, int radix_base){
        static char table[] = "0123456789abcdefghijklmnopqrstuvwxyz";
        char *p=buff;
        unsigned int n = (v < 0 && radix_base == 10)? -v : (unsigned int) v;
        while(n>=radix_base){
            *p++=table[n%radix_base];
            n/=radix_base;
        }
        *p++=table[n];
        if(v < 0 && radix_base == 10) *p++='-';
        *p='\0';
        return strrev(buff);
    }
    unsigned int temp_hum(void)
    {
    	signed int j=15;
    	//GPIOPinWrite(GPIO_PORTB_BASE,CLK, 2);
    	  //    		 SysCtlDelay(1000000);
    	      		//delay_ms(100); //delay for 100ms at power up
    	      		indata16 = 0x0000;
    	            SysCtlDelay(500);
    				if(GPIOPinRead(GPIO_PORTB_BASE, RDY)==0)
    				//SysCtlDelay(1000000);
    				{
    					for(j=15;j>0;j-=2)
    					{
    						k = 0;
    
    						SysCtlDelay(500);
    						if(GPIOPinRead(GPIO_PORTB_BASE, DAT)==8)
    						{
    							    //SysCtlDelay(1000000);
    								k = 1 << j;
    						}
    						indata16|=k;
    						GPIOPinWrite(GPIO_PORTB_BASE,CLK, 0);
    						SysCtlDelay(500);
    						while(GPIOPinRead(GPIO_PORTB_BASE, RDY)==0);
    						//SysCtlDelay(1000000);
    						k = 0;
    						SysCtlDelay(500);
    						if(GPIOPinRead(GPIO_PORTB_BASE, DAT)==8)
    						{
    							    //SysCtlDelay(1000000);
    								k = 1 << (j-1);
    						}
    						indata16|=k;
    						GPIOPinWrite(GPIO_PORTB_BASE,CLK, 2);
    						SysCtlDelay(500);
    						while(GPIOPinRead(GPIO_PORTB_BASE, RDY)==1);
    						//SysCtlDelay(1000000);
    
    					}
                     }
    return indata16;
    }
    static void mysdcard(unsigned int mydatac)
    {
    
    
    
    			//itoa(bufferHum[mydatac],Hum1,10);            //this function convert integer to string
    			//itoa(bufferTemp[mydatac],Temp1,10);            //this function convert integer to string
    
    					mydatac1=mydatac;
    			    	itoa(mydatac1,result,10);            //this function convert integer to string
    			    	itoa(bufferulTempValueC[mydatac1],ulTempValueC1,10);            //this function convert integer to string
    			    	itoa(bufferulTempValueF[mydatac1],ulTempValueF1,10);            //this function convert integer to string
    fr=f_open(&Fil, "DEMO1.csv", FA_WRITE | FA_OPEN_ALWAYS );
    			    										if (fr == FR_OK) 	// Create a file
    			    	          	        		        {
    			    	          	        		         		size = (&Fil)->fsize;
    			    	          	        		    	 		fr= f_lseek(&Fil,size);
    			     		       	        		    	 	    fr = f_write(&Fil,result,2, &bw);
    			     		       	        		    	 	    fr= f_write(&Fil,",",1,&bw);
    
    			     		       	        		    	 	    fr = f_write(&Fil,ulTempValueC1,2, &bw);
    			     		       	        		    	 		fr= f_write(&Fil,",",1,&bw);
    
    			     		       	        		    	 	    fr = f_write(&Fil,ulTempValueF1,2, &bw);
    			     		       	        		    	 	    //fr= f_write(&Fil,",",1,&bw);
    
    			     		       	        		    	 	    //fr = f_write(&Fil,Hum1,2, &bw);
    			     		       	        		    	 		//fr= f_write(&Fil,",",1,&bw);
    
    			     		       	        		    	 		//fr = f_write(&Fil,Temp1,2, &bw);
    			     		       	        		    	 	    fr= f_write(&Fil,"\n",1,&bw);
    			     		       	        		    	 	    //f_printf(&Fil, "%u", 1234);            /* "1234" */
    
    			    	          	        		    	 		//fr= f_write(&Fil,"Time\r\n",6,&bw);
    			    	       	        		    	 	 }
    			    	          	        		    	 	fr=f_close(&Fil);
    
    	}
    int
    main(void)
    {
        count=0;
    
       	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
       	SysCtlADCSpeedSet(SYSCTL_ADCSPEED_250KSPS);
       	ADCSequenceDisable(ADC0_BASE, 1);
    
       	ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
       	ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS);
       	ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS);
       	ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS);
       	ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END);
       	ADCSequenceEnable(ADC0_BASE, 1);
    
    
        // Enable lazy stacking for interrupt handlers.  This allows floating-point
        // instructions to be used within interrupt handlers, but at the expense of
        // extra stack usage.
        //
        ROM_FPULazyStackingEnable();
    
        // Set the system clock to run at 50MHz from the PLL.
        //
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                           SYSCTL_XTAL_16MHZ);
    
        // Enable the peripherals used by this example.
        //
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
        GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
    
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
        GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_2|GPIO_PIN_3);
        GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1);
    
        SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
        TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
    
        //
        // Configure SysTick for a 100Hz interrupt.  The FatFs driver wants a 10 ms tick.
        //
        /*ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
        ROM_SysTickEnable();
        ROM_SysTickIntEnable();*/
    
        /*for the user requirment
                * here it's generats interupt at every X second.
    
               	ulPeriod = SysCtlClockGet()*X;
               	TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod);
               */
    
    
      	                fr = f_mount(0, &FatFs);
              		    if(fr != FR_OK)
               	        {
               	         GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,GPIO_PIN_3);
            	        //
             	        // Delay for a bit
              	        //
              	        SysCtlDelay(50000);
           		        return(1);
              		    }
       	        		//fr = f_mkdir("sub1");
            		     //if (fr) die(fr);
    
      	        		if (f_open(&Fil, "DEMO1.csv", FA_WRITE | FA_OPEN_ALWAYS ) == FR_OK) 	// Create a file
           		        {
           		         		size = (&Fil)->fsize;
           		    	 		fr= f_lseek(&Fil,size);
          		    	 		fr= f_write(&Fil,"Time,internel_celsius,intenel_fahrenheit,ext_Hum,ext_Temp\r\n",56,&bw);
          		    	 		fr= f_write(&Fil,"\n",1,&bw);
           		    	 		//fr= f_write(&Fil,"Time\r\n",6,&bw);
           		    	  	  // f_printf(&Fil, "%u", count);            /* "1234" */
       		    	 	 }
          		    	 	fr=f_close(&Fil);
    
        //count1="0";
          		    	 	indata16 = 0x0000;
          		    	 	GPIOPinWrite(GPIO_PORTB_BASE,CLK, 2);
          		    	 	SysCtlDelay(1000000);
    
    
          		    	 	        TimerLoadSet(TIMER1_BASE, TIMER_A,SysCtlClockGet());
          		    	 	    	IntEnable(INT_TIMER1A);
          		    	 	    	TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
          		    	 	    	IntMasterEnable();
          		    	 	    	TimerEnable(TIMER1_BASE, TIMER_A);
          		    	 	    //
          		    	 	    // Enable Interrupts
          		    	 	    //
        	    //ROM_IntMasterEnable();
            while(1)
        	{
            	ADCIntClear(ADC0_BASE, 1);
            						ADCProcessorTrigger(ADC0_BASE, 1);
    
            						while(!ADCIntStatus(ADC0_BASE, 1, false))
            						{
            						}
    
    
            						ADCSequenceDataGet(ADC0_BASE, 1, ulADC0Value);
    					if(count>=5)
    					{
    								IntDisable(INT_TIMER1A);
    								TimerIntDisable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    								//IntMasterDisable();
    								TimerDisable(TIMER1_BASE, TIMER_A);
    								goto my_data;
    					}
    
    
        	}
    
            my_data:
            fr = f_mount(0, &FatFs);
           	    if(fr != FR_OK)
                  {
                   GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,GPIO_PIN_3);
               //
                // Delay for a bit
                 //
                 SysCtlDelay(50000);
                  return(1);
                  }
      	  	  for(datacount=1;datacount<=5;datacount++)
      	  	  {
    
      	  		  mysdcard(datacount);
      	  	  }
    
    
    }
    void Timer1IntHandler(void)
    {
    	counter=counter+1;
    	// Clear the timer interrupt
    	count=count+1;
    	TimerIntClear (TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    		if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2))
    	{
    		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
    	}
    	else
    	{
    		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
    	}
    
    		ulTempAvg = (ulADC0Value[0] + ulADC0Value[1] + ulADC0Value[2] + ulADC0Value[3] + 2)/4;
    		ulTempValueC = (1475 - ((2475 * ulTempAvg)) / 4096)/10;
    		ulTempValueF = ((ulTempValueC * 9) + 160) / 5;
    		bufferulTempValueC[count]=ulTempValueC;
    		bufferulTempValueF[count]=ulTempValueF;
    
    		/*indata=temp_hum();
    		Hum= indata >>8; //Separate Humidity reading into variable
    		bufferHum[count]=Hum;
    		Temp= indata & 0x00FF; //Separate Temperature reading into variable
    		bufferTemp[count]=Temp;*/
    
    
    
    }
    
    

    Here i post my code.

    In that after write the "Time,internel_celsius,intenel_fahrenheit,ext_Hum,ext_Temp" into the first row of the .csv file.

    timer is enable,then for the 5 second i store the internal temperature of the controller into the temporary arrey.

    now i remount the sdcard again,then in the for loop 2 times it is store the data correctly but for the datacount=2 in the mysdcrd function at this line "fr=f_open(&Fil, "DEMO1.csv", FA_WRITE | FA_OPEN_ALWAYS );"

    it is going to infinite loop.This loop is 

    static void
    FaultISR(void)
    {
    //
    // Enter an infinite loop.
    //
    while(1)
    {
    }
    }

    in the startup file.

  • Hello Darshan

    A Bus fault has occurred. The following application note will help you to diagnose why the Bus Fault has occurred

    http://www.ti.com/lit/an/spma043/spma043.pdf

    Regards

    Amit

  • I am not understand for 2 times it is work correctly ,but why not for the datacount=3.what is the problem when third time function call.

    -Darshan 

  • Hello Darshan,

    That is what needs to be debugged.

    Regards

    Amit

  • According to my debugger i found that value of 

    NVIC_FAULT_STAT is 0x00008200 ,but the value of 

    NVIC_FAULT_ADDR is 0x3034305C .which try to find in the memory map table but it is not in the any range of the memory table,which i see in the datasheet.

    Now which type of fault is generated ?

    Regards

    -Darshan

  • Hello Darshan

    This is a Data Fault which is because of an access from D-Bus of the Cortex M4 to an unknown address. This would generally arise if for a function you are using an un-initialized variable

    Regards

    Amit

  • 3858.loop.docx

    OK,But here I call this function 5 times.I send you the function which i call 5 times.In that it is execute whole the function 2 times for mydatac=1 and 2.But for the mydatac=3 at the line of opening the file it is generating the fault.

    Now If  I use un-initialized variable than  it should not work for the mydata1=1 and 2.

    Why fault is create for the 3rd time calling ?

    I send you the function and in that bold line represent the fault line.

    Regard

    -Darshan

  • Hello Darshan,

    Couple of points.

    1. Is the Fil a global variable?

    2. Is bufferulTempValueC and bufferulTempValueF array of the corrects size.

    Regards

    Amit

  • Yes,Fil is a global variable and size of bufferulTempValueC and bufferulTempValueF is 5 also because i store data into them for 5 second and it show me also five value.

     

    But here problem is  not related to bufferulTempValueC and bufferulTempValueF. because whenever i going to open the demo1.csv file. It will create a fault for the 3rd time call only.

     

    Regards

    Darshan

  • Hello Drashan,

    Firstly I do not have the setup that you may be having so reproducing the issue would not be possible. My suggestion would be to step debug the code when you run the file open (where you believe the 3rd time round it goes into fault) and see where exactly is the CPU execution going to cause a Hard Fault.

    Regards

    Amit