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.

CCS: adc data lossing when transfering to sd card via spi, using msp432

Tool/software: Code Composer Studio

i used pingpong buffer in adc  sample,and via spi transfer to sd which used fatfs system.but adc data  loss when pingpong  buffer  flag  changes ,it  doesnot success  into  sd  card between two files,

the most important thing is when a file in sd close, a new file open and meanwhile pingpong buffer flag changes many times,but there is no data in file

#include <Hardware/SPI_Driver.h>

#include <Hardware/GPIO_Driver.h>

#include <Hardware/CS_Driver.h>

#include <Hardware/TIMERA_Driver.h>

#include <Hardware/adc14repeat.h>

#include <fatfs/ff.h>

#include <fatfs/diskio.h>

#include <Devices/MSPIO.h>

#include "driverlib.h"

#include <fatfs/mmc_MSP432P401r.h>





#define FILESIZE  512

#define EACHFILE  4

UINT index;

UINT rec_flag=1;

UINT arraycount = 0;

UINT temp;

FRESULT rc;

UINT g_flag=0;

UINT filecount;

UINT maxfil;



UINT buffer1[FILESIZE];



UINT buffer2[FILESIZE];



BYTE buffer3[FILESIZE*2];

BYTE buffer4[FILESIZE*2];



/* UART Configuration Parameter. These are the configuration parameters to

 * make the eUSCI A UART module to operate with a 9600 baud rate.*/

eUSCI_UART_Config UART0Config =

{

     EUSCI_A_UART_CLOCKSOURCE_SMCLK,

     11,

     10,

     214,

     EUSCI_A_UART_NO_PARITY,

     EUSCI_A_UART_LSB_FIRST,

     EUSCI_A_UART_ONE_STOP_BIT,

     EUSCI_A_UART_MODE,

     EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION

};



/* SPI Configuration Parameter. These are the configuration parameters to

 * make the eUSCI B SPI module to operate with a 500KHz clock.*/

eUSCI_SPI_MasterConfig SPI0MasterConfig =

{

     EUSCI_B_SPI_CLOCKSOURCE_SMCLK,

     3000000,

     500000,

     EUSCI_B_SPI_MSB_FIRST,

     EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,

     EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH,

     EUSCI_B_SPI_3PIN

};







void main(void)

{

    WDT_A_holdTimer();



        FIL  datafil;

        FATFS datafatfs;



        UINT bw;



        CHAR dataname[8];

        UINT exist_filecount=0;



        DIR dir;                                               /* Directory object */

        FILINFO fno;



    CS_Init();



    MAP_GPIO_setAsInputPin(GPIO_PORT_P1, GPIO_PIN5);

    MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN5);

    MAP_GPIO_interruptEdgeSelect(GPIO_PORT_P1, GPIO_PIN5, GPIO_LOW_TO_HIGH_TRANSITION);

    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN5);

    MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN5);

    MAP_Interrupt_enableInterrupt(INT_PORT1);

    MAP_Interrupt_enableMaster();



    /*Initialize all hardware required for the SD Card*/

        SPI_Init(EUSCI_B3_BASE, SPI0MasterConfig);

        UART_Init(EUSCI_A0_BASE, UART0Config);

        Setup_ADC14();

        GPIO_Init(GPIO_PORT_P1, GPIO_PIN0);

 



    do{

                   rc = f_mount(&datafatfs, "0", 1);

                         }

                       /*Check for errors. Trap MSP432 if there is an error*/

                       while(rc != FR_OK);



                       rc = f_opendir(&dir,"/");

                       while(rc != FR_OK);



                       for (;;)

                           {

                               rc = f_readdir(&dir, &fno);                        // Read a directory item

                               if (rc || !fno.fname[0])

                                   break;                                                   // Error or end of dir

                               sscanf(fno.fname,"%d",&exist_filecount);

                               if(exist_filecount>maxfil)

                                   maxfil = exist_filecount;

                            }



                       filecount = maxfil+1;



                       sprintf(dataname,"%d",filecount);





                       rc = f_open(&datafil, dataname, FA_WRITE | FA_CREATE_ALWAYS);

                       while(rc != FR_OK );





      while(1)

        {

                 if(index==FILESIZE)

                {

                    if(rec_flag==1)

                    {

                        index = 0;



                        // Write to file

                         rc = f_write(&datafil, buffer4,1024, &bw);



                        switch (rc)

                         {case 0:arraycount++;

                                 break;

                          default:break;}





                    }



                    else if(rec_flag==2)

                    {

                        index = 0;



                        // Write to file

                        rc = f_write(&datafil, buffer3, 1024, &bw);



                        switch (rc)

                        {case 0:arraycount++;

                                break;

                         default:break;}



                    }



                    if((arraycount%EACHFILE)==0)

                    {



                        rc = f_close(&datafil);



                        arraycount = 0;



                        filecount++;



                        sprintf(dataname,"%d",filecount);



                        rc = f_open(&datafil, dataname, FA_WRITE | FA_CREATE_ALWAYS);



                     }

                }

        }

//          PCM_gotoLPM0();

}



void ADC14_IRQHandler(void)

{

    uint64_t status = MAP_ADC14_getEnabledInterruptStatus();

    MAP_ADC14_clearInterruptFlag(status);

    switch(ADC_INT0&status)

    {

        case 0: break;

        case 1:



              if(rec_flag==1)

              {

                  *(buffer1+index) = MAP_ADC14_getResult(ADC_MEM0);

                  *(buffer3+2*index) = buffer1[index]>>8;

                  *(buffer3+2*index+1) = buffer1[index];



                  index++;

                  if(index==FILESIZE)

                  {

                      rec_flag = 2;

                  }



                  if(index>FILESIZE)

                  {

                      index=0;


                  }



              }

              else if(rec_flag==2)

              {

                  *(buffer2+index) = MAP_ADC14_getResult(ADC_MEM0);

                  *(buffer4+2*index) = buffer2[index]>>8;

                  *(buffer4+2*index+1) = buffer2[index];

                  index++;

                  if(index==FILESIZE)

                  {

                      rec_flag = 1;

                  }

                  if(index>FILESIZE)

                  {

                      index=0;



                  }



              }

              MAP_ADC14_toggleConversionTrigger();

   }

}





and  there is   adc  code

/* DriverLib Includes */

#include "driverlib.h"



/* Standard Includes */

#include <stdint.h>



#include <stdbool.h>



void Setup_ADC14(void)

{

    /* Initializing ADC  */



    CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_4);

    ADC14_enableModule();

    ADC14_initModule(ADC_CLOCKSOURCE_MCLK,ADC_PREDIVIDER_4,ADC_DIVIDER_2,0);

    /* Configuring GPIOs (5.4 A1) */

    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN7|GPIO_PIN6|GPIO_PIN4,

    GPIO_TERTIARY_MODULE_FUNCTION);



    /* Configuring ADC Memory */

    MAP_ADC14_configureSingleSampleMode(ADC_MEM0, true);

    MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_EXTPOS_VREFNEG_EXTNEG,

    ADC_INPUT_A1, false);



    /* Configuring Sample Timer */

    ADC14_enableSampleTimer(ADC_MANUAL_ITERATION);

    /* Enabling/Toggling Conversion */

    MAP_ADC14_enableConversion();

    MAP_ADC14_toggleConversionTrigger();



    /* Enabling interrupts */

    MAP_ADC14_enableInterrupt(ADC_INT0);

    MAP_Interrupt_enableInterrupt(INT_ADC14);

    MAP_Interrupt_enableMaster();

    /*Set resolution*/

    ADC14_setResolution(ADC_14BIT);

}

  • Hello,
    My first guess is that the ADC irq is interrupting or corrupting the communication. Does it make more sense to utilize the DMA to capture a set of data without CPU intervention in the ping-pong mode. This would require you to both process and transmit while the other data set is being collected. Since you are triggering the next ADC trigger within the ISR maybe as an experiment you can disable the interrupts around the communications to see if the issue goes away.

    Examples of the ping-pong mode can be found here:
    dev.ti.com/.../

    www.ti.com/.../slaa821

    Regards,
    Chris
  • hi,Chirs,how should i do it in details? would you tell me specificly? thank you very much.
  • (1) In the adc setup you should probably move the start conversion API after the interrupts are enabled.  For example the API which sets the resolution to 14-bits will not be applied since the ADC14ENC=1.

    (2) Please let me know if the example is not understood.  Another option would be to stop the ADC while the file transfer is taking place.

    /*Set resolution*/
    
        ADC14_setResolution(ADC_14BIT);
    
        MAP_ADC14_enableConversion();    
    
        /* Enabling interrupts */
    
        MAP_ADC14_enableInterrupt(ADC_INT0);
    
        MAP_Interrupt_enableInterrupt(INT_ADC14);
    
        MAP_Interrupt_enableMaster();
    
        MAP_ADC14_toggleConversionTrigger();
    

**Attention** This is a public forum