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.

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

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(g_flag==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();
   }
}

void PORT1_IRQHandler(void)
{
    uint32_t status;

    status = MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P1);

    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, status);

    if(status & GPIO_PIN5)
    {  g_flag=1; }
}


and  there is   adc  code

/*
 * -------------------------------------------
 *    MSP432 DriverLib - v3_21_00_05 
 * -------------------------------------------
 *
 * --COPYRIGHT--,BSD,BSD
 * Copyright (c) 2016, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --/COPYRIGHT--*/
/*******************************************************************************
 * MSP432 ADC14 - Single Channel Sample Repeat
 *
 * Description: This code example will demonstrate the basic functionality of
 * of the DriverLib ADC APIs with sampling a single channel repeatedly. Each
 * time the ADC conversion occurs, the result is stored into a local variable.
 * The sample timer is used to continuously grab a new value from the ADC
 * module using a manual iteration that is performed in the ADC ISR. A
 * normalized ADC value with respect to the 3.3v Avcc is also calculated using
 * the FPU.
 *
 *                MSP432P401
 *             ------------------
 *         /|\|                  |
 *          | |                  |
 *          --|RST         P5.4  |<--- A1 (Analog Input)
 *            |                  |
 *            |                  |
 *            |                  |
 *            |                  |
 *            |                  |
 *
 * Author: Timothy Logan
 ******************************************************************************/
/* 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);
}