• TI Thinks Resolved

ADS1256: Ads1256

Prodigy 140 points

Replies: 18

Views: 295

Part Number: ADS1256

I've the same kind of problem. I use stm32f302r8 microcontroller (nucleo-f302r8) and adc1256.  I don't receive any data through my spi communication. I've shared my code. Kindly help me through this.

  • include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ uint8_t receive[2]; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ SPI_HandleTypeDef hspi3; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_SPI3_Init(void); static void MX_USART2_UART_Init(void); /* USER CODE BEGIN PFP */ void adc_write(uint8_t address, uint8_t value) { uint8_t data[3]; data[0] = 0x50|address; data[1] = 0x00; data[2] = value; HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi3, data,3, 50); //DWT_Delay(10); HAL_Delay(1); HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET); } void adc_write_command(uint8_t command) { HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi3,&command,1,50); HAL_Delay(1); HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET); } /* float freq(float volta) { int value= 0, tim_val[100]; float frequency; for(int i=0;i0) { //start timer while(!(volta0)); //stop timer } //tim_val[i] = timercount_value; value += tim_val[i]; } value = value/100; value = value*2; frequency = 1/value; return frequency; } */ int adc_read() { uint8_t datacon; uint32_t convert = 0x00; HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET); adc_write_command(0x01); //read for(int i=0;i<
  • In reply to Gokul Balasubramanian:

    Hi Gokul,

    Welcome to the TI E2E Forums!

    Are you able to read and write from any of the ADS1256 device registers or is the main issue your facing have to do with programming the SPI peripheral on the STM MCU?
    Unfortunately, I am not familiar with that MCU and won't be able to help with code that is specifically related to the microcontroller; However, if you have the SPI peripheral working and sending commands to the ADS1256, I certainly can comment on the interfacing code for the communication to the ADC.

    Have you tried probing the SPI signals on an oscilloscope or logic analyzer to verify if any of the HAL_GPIO_WritePin() or HAL_SPI_Transmit() commands are working as expected?
    Feel free to share any of those screenshots with me and I'll see if I can help your debug the issue with the SPI communication.

    FYI: The code you shared appears to have been truncated...I did not see the end of the adc_read() function. Feel free to share the rest of the code and I'll look it over to see if I can spot any issues.

    Best regards,
    Chris Hall
    Applications Engineer | Precision ADCs


    Check out these helpful resources...
    TI Precision Data Converters | TI Precision Labs - ADCs | Analog Engineer's Calculator | Data Converters Learning Center | Selection Guide

     

  • In reply to Christopher Hall:

    Dear Hall,

        My spi peripheral is working properly and I only have problems in sending and receiving the bytes on the adc. I just dont know where the problem is. I.ve shared the code and kindly verify

    /* USER CODE BEGIN Header */
    /**
      ******************************************************************************
      * @file           : main.c
      * @brief          : Main program body
      ******************************************************************************
      * @attention
      *
      * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
      * All rights reserved.</center></h2>
      *
      * This software component is licensed by ST under BSD 3-Clause license,
      * the "License"; You may not use this file except in compliance with the
      * License. You may obtain a copy of the License at:
      *                        opensource.org/licenses/BSD-3-Clause
      *
      ******************************************************************************
      */
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    uint8_t receive[2];
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    SPI_HandleTypeDef hspi3;
    
    UART_HandleTypeDef huart2;
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_SPI3_Init(void);
    static void MX_USART2_UART_Init(void);
    /* USER CODE BEGIN PFP */
    
    void adc_write(uint8_t address, uint8_t value)
    {
                    uint8_t data[3];
                    data[0] = 0x50|address;
                    data[1] = 0x00;
                    data[2] = value;
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
                    HAL_SPI_Transmit(&hspi3, data,3, 50);
                    //DWT_Delay(10);
                    HAL_Delay(1);
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
    }
    
    void adc_write_command(uint8_t command)
    {
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
                    HAL_SPI_Transmit(&hspi3,&command,1,50);
                    HAL_Delay(1);
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
    
    }
    
    /*
    float freq(float volta)
    {
                    int value= 0, tim_val[100];
                    float frequency;
    
                    for(int i=0;i<100;i++)
                    {
                                    if(volta<0.1 && volta>0)
                                    {
                                                    //start timer
                                                    while(!(volta<0.1 && volta>0));
                                                    //stop timer
    
                                    }
                                                    //tim_val[i] = timercount_value;
                                    value += tim_val[i];
                    }
                    value = value/100;
                    value = value*2;
                    frequency = 1/value;
                    return frequency;
    }
    */
    int adc_read()
    {
                    uint8_t datacon;
                    uint32_t convert = 0x00;
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
                    adc_write_command(0x01); //read
    
                    for(int i=0;i<3;i++)
                    {
                                    HAL_SPI_Receive(&hspi3,&datacon,1,50);
                                    convert |= datacon;
                                    convert<<=8;
                    }
    
                    //DWT_Delay(10);
                    HAL_Delay(1);
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
                    return convert;
    }
    
    float volt(uint32_t data2)
    {
                    float v;
        v =(float) data2*0.000000596046; // 8388607 = +5.000v reference voltage
                    return v;
    }
    
    
    void adc_read_regis(uint8_t address1)
    {
                                    uint8_t data1[3];
                                    data1[0] = 0x10|address1;
                                    data1[1] = 0x00;
                                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
                                    HAL_SPI_Transmit(&hspi3, data1, 2, 50);
                                    HAL_Delay(5);
                                    HAL_SPI_Receive(&hspi3,&data1[2],1,50);
                                    //DWT_Delay(10);
                                    HAL_Delay(1);
                                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
    }
    
    void adc_reset()
    {
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
                    HAL_GPIO_WritePin(GPIOC, RESET_Pin, GPIO_PIN_RESET);
                    HAL_Delay(1);
                    HAL_GPIO_WritePin(GPIOC, RESET_Pin, GPIO_PIN_SET);
                    HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
    }
    
    void adc_init()
    {
                    adc_write(0x00, 0x03); //status
                    adc_write(0x02, 0x20); //adcon
                    adc_write(0x03, 0x82); //drate 100sps
                    adc_read_regis(0x03); //verification
                    adc_write_command(0xF0); //selfcab of adc
                    adc_write(0x01, 0x0F); //mux for channel 0 positive
                    adc_write_command(0xFC); //sync
                    adc_write_command(0x00); //wake up
                    HAL_Delay(500);
    
    }
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    int main(void)
    {
      /* USER CODE BEGIN 1 */
    
                    uint32_t adc_data_ch0, adc_data_ch1;
                    float voltage_ch0, voltage_ch1;
    
      /* USER CODE END 1 */
      
    
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* USER CODE BEGIN Init */
    
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_SPI3_Init();
      MX_USART2_UART_Init();
      /* USER CODE BEGIN 2 */
       MX_GPIO_Init();
       HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
       adc_reset();
       HAL_Delay(500);
       MX_SPI3_Init();
       HAL_Delay(500);
       adc_write_command(0xFE); //reset to power up values
       adc_write_command(0xFC); //sync
       adc_write_command(0x00); //wake up
       //DWT_Delay(10);
       HAL_Delay(1);
       adc_init();
       HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET); //confirm configuration
       MX_USART2_UART_Init();
       HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
                                    //while(HAL_GPIO_ReadPin(DRDY_GPIO_Port,DRDY_Pin));
                                    adc_write(0x01, 0x1F); //mux for ch1 positive channel
                                    adc_write_command(0xFC); //sync
                                    adc_write_command(0x00); //wake up
                                    adc_data_ch0 = adc_read();
                                    voltage_ch0  = volt(adc_data_ch0);
    
                        //while(HAL_GPIO_ReadPin(DRDY_GPIO_Port,DRDY_Pin));
                        adc_write(0x01, 0x0F);  //mux for ch0 positive channel
                        adc_write_command(0xFC); //sync
                        adc_write_command(0x00); //wake up
                        adc_data_ch1 = adc_read();
                        voltage_ch1  = volt(adc_data_ch1);
                        //frequency_in_ch1 = freq(voltage_in_ch1);
    
    
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
    
                      HAL_Delay(100);
    
    
                        HAL_Delay(100);
    
                        HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
      }
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
    
      /* USER CODE END 3 */
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      RCC_OscInitTypeDef RCC_OscInitStruct = {0};
      RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
      RCC_OscInitStruct.HSIState = RCC_HSI_ON;
      RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
      RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
      RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
      if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    
     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
      {
        Error_Handler();
      }
    }
    
    /**
      * @brief SPI3 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_SPI3_Init(void)
    {
    
      /* USER CODE BEGIN SPI3_Init 0 */
    
      /* USER CODE END SPI3_Init 0 */
    
      /* USER CODE BEGIN SPI3_Init 1 */
    
      /* USER CODE END SPI3_Init 1 */
      /* SPI3 parameter configuration*/
      hspi3.Instance = SPI3;
      hspi3.Init.Mode = SPI_MODE_MASTER;
      hspi3.Init.Direction = SPI_DIRECTION_2LINES;
      hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
      hspi3.Init.NSS = SPI_NSS_SOFT;
      hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
      hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi3.Init.CRCPolynomial = 7;
      hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
      hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
      if (HAL_SPI_Init(&hspi3) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN SPI3_Init 2 */
    
      /* USER CODE END SPI3_Init 2 */
    
    }
    
    /**
      * @brief USART2 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_USART2_UART_Init(void)
    {
    
      /* USER CODE BEGIN USART2_Init 0 */
    
      /* USER CODE END USART2_Init 0 */
    
      /* USER CODE BEGIN USART2_Init 1 */
    
      /* USER CODE END USART2_Init 1 */
      huart2.Instance = USART2;
      huart2.Init.BaudRate = 38400;
      huart2.Init.WordLength = UART_WORDLENGTH_8B;
      huart2.Init.StopBits = UART_STOPBITS_1;
      huart2.Init.Parity = UART_PARITY_NONE;
      huart2.Init.Mode = UART_MODE_TX_RX;
      huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
      huart2.Init.OverSampling = UART_OVERSAMPLING_16;
      huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
      huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
      if (HAL_UART_Init(&huart2) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN USART2_Init 2 */
    
      /* USER CODE END USART2_Init 2 */
    
    }
    
    /**
      * @brief GPIO Initialization Function
      * @param None
      * @retval None
      */
    static void MX_GPIO_Init(void)
    {
      GPIO_InitTypeDef GPIO_InitStruct = {0};
    
      /* GPIO Ports Clock Enable */
      __HAL_RCC_GPIOC_CLK_ENABLE();
      __HAL_RCC_GPIOF_CLK_ENABLE();
      __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOB_CLK_ENABLE();
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOC, RESET_Pin|CHIP_SELECT_Pin, GPIO_PIN_SET);
    
      /*Configure GPIO pin : B1_Pin */
      GPIO_InitStruct.Pin = B1_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : LD2_Pin */
      GPIO_InitStruct.Pin = LD2_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : DRDY_Pin */
      GPIO_InitStruct.Pin = DRDY_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(DRDY_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : RESET_Pin CHIP_SELECT_Pin */
      GPIO_InitStruct.Pin = RESET_Pin|CHIP_SELECT_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
    }
    
    /* USER CODE BEGIN 4 */
    
    /* USER CODE END 4 */
    
    /**
      * @brief  This function is executed in case of error occurrence.
      * @retval None
      */
    void Error_Handler(void)
    {
      /* USER CODE BEGIN Error_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
    
      /* USER CODE END Error_Handler_Debug */
    }
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *         where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(char *file, uint32_t line)
    { 
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
         tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
      /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */
    

     

  • In reply to Gokul Balasubramanian:

    Hi Gokul,

    See my comments in the code snippet below:

    int adc_read()
    {
    	uint8_t datacon;
    	uint32_t convert = 0x00;	// TODO: Consider making this a signed 32-bit value, since the ADC data is signed
    	
    	HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
    	
    	// TODO: Insert a short delay here (between /CS low and first SCLK),
    	// unless HAL_GPIO_WritePin() already provide sufficent delay to meet the ADS1256's "t3" timing requirement.
    	
    	adc_write_command(0x01);	// TODO: Don't set /CS high after sending this byte!
    	
    	// TODO: Insert a short delay here to meet the "t6" timing requirement
    	
    	// NOTE: Some MCUs use FIFOs to store the MISO data from the SPI peripheral. Make sure that this FIFO has been cleared
    	// before calling HAL_SPI_Receive(), or else the DONT CARE return byte from the previous "0x01" command may be mis-interpreted as data. 
    	
    	for(int i=0;i<3;i++)
    	{
    		// TODO: Send a "0x00 byte" to clock out the ADC data byte, unless HAL_SPI_Receive() already does this...
    		HAL_SPI_Receive(&hspi3,&datacon,1,50); 
    		convert |= datacon;
    		convert<<=8;
    	}
    
    	// TODO: Consider appending sign-extension byte to "convert" (based on most-significant bit of ADC data), see the following URL for an example:
    	// https://e2e.ti.com/blogs_/archives/b/precisionhub/archive/2016/04/01/it-s-in-the-math-how-to-convert-adc-code-to-a-voltage-part-1
    
    	HAL_Delay(1);
    	HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
    	
    	return convert;
    }

    I hope that helps!

    Best regards,
    Chris Hall
    Applications Engineer | Precision ADCs


    Check out these helpful resources...
    TI Precision Data Converters | TI Precision Labs - ADCs | Analog Engineer's Calculator | Data Converters Learning Center | Selection Guide

     

  • In reply to Christopher Hall:

    Dear hall,

     @christopher hall rest of code looks fine? I mean I got to change the convert data type to signed datatype and introduce a short delay after sending rdata command. Will sending a 1ms delay work?. And I'll also work on the fifo miso output. Is it ok to reset the chip select at the initial code and phase and never set till the end. Other parts of adc_init() looks ok? Kindly point out all other errors possible because I'm not sure whether data is getting written on the status or adcon or drate registers. When I try to read back the data I get a big fat zero. You can see my read reg code. Kindly help me with the same .

  • In reply to Gokul Balasubramanian:

    Hi Gokul,

    Assuming your using the nominal 7.68 MHz clock for the ADS1256, then the "t6" delay only needs to be about 6 us. The datasheet says "t3" can be a minimum of 0 ns, but I would recommend making it at least 50 ns.

    If you make these delays too long then it will take you longer to clock out the ADC data, and this could present a problem when trying to run the ADC at the faster data rates.

    /CS is used to selected which device on the SPI bus you want to talk to, so if you only have one device on the SPI bus, then technically you can leave it low. However, I would strongly advise against using /CS in this way...

    /CS prevents the slave device from responding to SCLKs while it is high. Therefore, to protect the MCU and ADC from getting out of sync (due to noise or glitches on the SCLK signal), only set /CS low to issue a command (or read data) and then set it back high when your done. Each time you set /CS low, the ADC's SPI peripheral is reset to look for the beginning of the next command and this helps keep the SPI communication in alignment between the master and slave.

    I looked through additional parts of your code and had the following comments

    • In your "adc_reset()" function, it is not necessary to toggle /CS unless you are sending the "RESET" SPI command. When using the /RESET pin, you can toggle the /RESET pin independently without setting /CS low.

    • After sending the "0xF0" command in your "adc_init()" function to perform self-calibration, you need to wait for this operation to complete. Add the required delay after this command, per Table 19 in the datasheet. Alternatively, you could probably poll for /DRDY to go low instead of having to adjust the delay time for every data rate.

    • In you main() function, after selecting an input channel and starting a new conversion, you need to wait for /DRDY to go low before reading the ADC data. Reading data immediately will return an invalid result. It is okay to poll /DRDY if this is the only operation you need you MCU to perform, but typically it is a better practice to use a falling edge GPIO triggered interrupt to tell the MCU when data is available (so that ti can perform other operations while waiting for the ADC to finish converting.) 

    • I would also double check your line of code which configures the SPI clock polarity:
      hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;

      The ADS1256 communicates using SPI Mode 1, meaning that MISO will change on the rising edge of SCLK, but should be sampled on the falling edge of SCLK.

    Best regards,
    Chris Hall
    Applications Engineer | Precision ADCs


    Check out these helpful resources...
    TI Precision Data Converters | TI Precision Labs - ADCs | Analog Engineer's Calculator | Data Converters Learning Center | Selection Guide

     

  • In reply to Christopher Hall:

    Dear Hall,

    Christopher Hall (1528318) Hi! All your points were really helpful and I did implement in my program. Unfortunately still I don't receive the conversion data. I'll share you two codes one with arduino and another with stm32f302r8. The code works perfectly in arduino i.e displaying two conversion data from two channels(ain0 and ain1 both positive channel) in the serial monitor. But when I try to do the same with stm32f302r8 it doesn't work. I've initialized my spi in spi mode 1 (i.e) CPHA 2 edge and COPL LOW. I've uploaded the oscilloscope image of clk, miso and mosi signal in both arduino for your reference. in case of stm32f302r I dont see any signal in my miso pin. Can you please help me in debugging my problem since I'm fairly new to the controller world. Kindly comment on the waveforms in both arduino and stm32f302r8 board because I really doubt with drdy signal. Thanks for the replies earlier! Sorry for the long post.waveform.rar

    Arduino code

    /* ADS1256
        CLK  - pin 13
        DIN  - pin 11 (MOSI)
        DOUT - pin 12 (MISO)
        CS   - pin 10
        DRDY - pin 9
        RESET- pin 8 (or tie HIGH?)
        DVDD - 3V3
        DGND - GND
    */
    
    #define cs 10 // chip select
    #define rdy 9 // data ready, input
    #define rst 8 // reset pin
    #define pwd 7 // power down
    #define flag 0 //
    
    #define SPISPEED 1000000
                  // 1250000
    #define rdata 0x01
    #include <SPI.h>
     //byte data = 0b00000000;
    
     void mux( byte data)
    { 
      SPI.transfer(0x50 | 1); // MUX register
      SPI.transfer(0x00);   // 2nd command byte, write one register only
      SPI.transfer(data);   // write the databyte to the register
      delayMicroseconds(10);
    }
    
    void sync()
    {
      //SYNC command 1111 1100
      SPI.transfer(0xFC);
      delayMicroseconds(10);
      
      //WAKEUP 0000 0000
      SPI.transfer(0x00);
      delayMicroseconds(10);
    }
    
    void calculation()
    {
       int32_t adc_val =0;
       float V = 0;
      adc_val = SPI.transfer(0);
      adc_val <<= 8; //shift to left
      adc_val |= SPI.transfer(0);
      adc_val <<= 8;
      adc_val |= SPI.transfer(0);
      V = adc_val*0.000000596046; // 8388607 = +5.000v /
      
      delayMicroseconds(10);
      Serial.println(adc_val, HEX);
      Serial.print("+");
      Serial.println(V);
    }
    void setup()
    { 
      Serial.begin(115200);
      int8_t data;
      
      pinMode(cs, OUTPUT);
      digitalWrite(cs, LOW); // tied low is also OK.
      pinMode(rdy, INPUT);
      pinMode(rst, OUTPUT);
      digitalWrite(rst, LOW);
      delay(1); // LOW at least 4 clock cycles of onboard clock. 100 microsecons is enough
      digitalWrite(rst, HIGH); // now reset to deafult values
      
      //delay(500);
      SPI.begin(); //start the spi-bus
      //delay(500);
    
      //init
      
      digitalWrite(pwd, LOW);
      //while (digitalRead(rdy)) {}  // wait for ready_line to go low
      SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
      delayMicroseconds(10);
    
      //Reset to Power-Up Values (FEh)
      SPI.transfer(0xFE);
      delayMicroseconds(10);
      
      byte status_reg = 0 ;  // address (datasheet p. 30)
      //PGA SETTING
      //1 ±5V
      //2 ±2.5V
      //4 ±1.25V
      //8 ±0.625V
      //16 ±312.5mV
      //32 ±156.25mV
      //64 ±78.125mV
      //byte status_data = 0x01; //status: Most Significant Bit First, Auto-Calibration Disabled, Analog Input Buffer Disabled
      byte status_data = 0b00000110;
      SPI.transfer(0x50 | status_reg);
      SPI.transfer(0x00);   // 2nd command byte, write one register only
      SPI.transfer(status_data);   // write the databyte to the register
      delayMicroseconds(10);
    
      Serial.println("status done");
    
      SPI.transfer(0x01 | status_reg);
     SPI.transfer(0x00);   // 2nd command byte, write one register only
      delayMicroseconds(25);
      data = SPI.transfer(0xFF);
      Serial.println(data, HEX);
      delayMicroseconds(10);
      
     
      byte adcon_reg = 2; //A/D Control Register (Address 02h)
      byte adcon_data = 0x20; // 0 01 00 000 => Clock Out Frequency = fCLKIN, Sensor Detect OFF, gain 1
      SPI.transfer(0x50 | adcon_reg);
      SPI.transfer(0x00);   // 2nd command byte, write one register only
      SPI.transfer(adcon_data);   // write the databyte to the register
      delayMicroseconds(10);
    
      Serial.println("adcon done");
    
      // Set sampling rate
      byte drate_reg = 3;
      byte drate_data = 0b11110000; // 100SPS
      SPI.transfer(0x50 | drate_reg);
      SPI.transfer(0x00);
      SPI.transfer(drate_data);
      delayMicroseconds(10);
    
      Serial.println("drate done");
    
      SPI.transfer(0xF0);
      delay(500);
      
    // digitalWrite(cs, HIGH);
      
      Serial.println("configured, starting");
      mux(0b00001111);//DIFF 1
      sync();
      delay(500);
    }
    
    
    
    
    void loop()
    {
      Serial.println();
      //Serial.println("adc");
     
      
     
    
      // digitalWrite(cs, LOW);
      SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
      delayMicroseconds(10);
      
      
    
      while (digitalRead(rdy)== 1) {} ;
    
       mux(0b00011111);//DIFF 1
      sync();
     Serial.println("pot");
      SPI.transfer(rdata); // Read Data 0000  0001 (01h)
      delayMicroseconds(10);
      
        
      
      calculation();
       
      
      
      delayMicroseconds(10);
      
      // digitalWrite(cs, HIGH);
      //SPI.endTransaction();
      
    
    /*
      //The ADS1255/6 output 24 bits of data in Binary Two's
      //Complement format. The LSB has a weight of
      //2VREF/(PGA(223 − 1)). A positive full-scale input produces
      //an output code of 7FFFFFh and the negative full-scale
      //input produces an output code of 800000h.
      if(adc_val > 0x7fffff){ //if MSB == 1
        adc_val = (16777215ul - adc_val) + 1; //do 2's complement
        Serial.print("-");
      }
      else
      {
        Serial.print("+");
      }
      */
      //mV /= 1000;
      
     
      delay(100);
      Serial.println();
    
      while (digitalRead(rdy)== 1) {} ;
    
      mux(0b00001111);
      sync();
     
      //SYNC command 1111 1100
      SPI.transfer(0xFC);
      delayMicroseconds(10);
      
      //WAKEUP 0000 0000
      SPI.transfer(0x00);
      delayMicroseconds(10);
      
     Serial.println("ldr");
      SPI.transfer(rdata); // Read Data 0000  0001 (01h)
      delayMicroseconds(10);
       
      
      
      // digitalWrite(cs, HIGH);
      //SPI.endTransaction();
     
      calculation();
    }

    Stm32f302r8 code

    #include "main.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    UART_HandleTypeDef hlpuart1;
    UART_HandleTypeDef huart3;
    
    SPI_HandleTypeDef hspi1;
    
    TIM_HandleTypeDef htim6;
    
    PCD_HandleTypeDef hpcd_USB_OTG_FS;
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_LPUART1_UART_Init(void);
    static void MX_USART3_UART_Init(void);
    static void MX_TIM6_Init(void);
    static void MX_USB_OTG_FS_PCD_Init(void);
    static void MX_SPI1_Init(void);
    /* USER CODE BEGIN PFP */
    
    void delay() //create delay of 26us
    {
    		__HAL_TIM_ENABLE(&htim6);
    		while(__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE)==0);
    		__HAL_TIM_DISABLE(&htim6);
    		__HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE);
    
    }
    
    void adc_write(uint8_t address, uint8_t value)
    {
    	uint8_t data[3];
    	data[0] = 0x50|address;
    	data[1] = 0x00;
    	data[2] = value;
    	delay();
    	HAL_SPI_Transmit(&hspi1, data,3, 10);
    	delay();
    
    }
    
    void adc_write_command(uint8_t command)
    {
    	delay();
       	HAL_SPI_Transmit(&hspi1,&command,1,10);
       	delay();
    
    }
    
    int adc_read()
    {
    	uint8_t datacon;
    	int32_t convert = 0x00;
    	delay();
    	adc_write_command(0x01); //read
    	delay();
    	for(int i=0;i<3;i++)
    	{
    		HAL_SPI_TransmitReceive(&hspi1,0xFF,&datacon,1,10);
    		convert |= datacon;
    		convert<<=8;
    	}
    
    	return convert;
    }
    
    void adc_reset()
    {
    	HAL_GPIO_WritePin(GPIOB, RESET_Pin, GPIO_PIN_RESET);
    	HAL_Delay(1);
    	HAL_GPIO_WritePin(GPIOB, RESET_Pin, GPIO_PIN_SET);
    }
    
    void adc_init()
    {
    
        delay();
    	adc_write_command(0xFE);
        delay();
    	adc_write_command(0x0F);
    	delay();
    	adc_write(0x00, 0x02); //status
    	delay();
    	adc_write(0x02, 0x20); //adcon
    	delay();
    	adc_write(0x03, 0x82); //drate 100sps
    	delay();
    	adc_write_command(0xF0); //selfcab of adc
    	delay();
    	HAL_Delay(100);  //100ms delay
    
    
    
    }
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    int main(void)
    {
      /* USER CODE BEGIN 1 */
    	int32_t adc_data_ch0, adc_data_ch1;
    
      /* USER CODE END 1 */
      
    
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* USER CODE BEGIN Init */
    
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_LPUART1_UART_Init();
      MX_USART3_UART_Init();
      MX_TIM6_Init();
      MX_USB_OTG_FS_PCD_Init();
      MX_SPI1_Init();
      /* USER CODE BEGIN 2 */
      HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin, GPIO_PIN_RESET);
      adc_reset();
      MX_SPI1_Init();
      HAL_GPIO_WritePin(GPIOB, PDWN_Pin, GPIO_PIN_RESET);
      adc_init();
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
    
    	  HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin, GPIO_PIN_RESET);
    	  delay();
    	  adc_write(0x01, 0x0F); //mux for channel 0 positive
    	  	delay();
    	  	adc_write_command(0xFC); //sync
    	  	delay();
    	  	adc_write_command(0x00); //wake up
    	  	delay();
    	  	while(HAL_GPIO_ReadPin(DRDY_GPIO_Port,DRDY_Pin));
    	  	adc_data_ch1 = adc_read();
    	  	HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin, GPIO_PIN_SET);
    
    	  	HAL_Delay(100);
    
    	  	 HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin, GPIO_PIN_RESET);
    	  	delay();
    	  	adc_write(0x01, 0x1F); //mux for channel 0 positive
    	  	delay();
    	  	adc_write_command(0xFC); //sync
    	  	delay();
    	  	adc_write_command(0x00); //wake up
    	  	delay();
    	  	while(HAL_GPIO_ReadPin(DRDY_GPIO_Port,DRDY_Pin));
      	  	adc_data_ch0 = adc_read();
    	  	HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin, GPIO_PIN_SET);
    
    	  	HAL_Delay(100);
    
    	  HAL_GPIO_WritePin(GPIOB, LD2_Pin,1);
    	  delay();
    	  HAL_GPIO_WritePin(GPIOB, LD2_Pin,0);
    	  	  delay();
    
    
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
    
      }
      /* USER CODE END 3 */
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      RCC_OscInitTypeDef RCC_OscInitStruct = {0};
      RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
      RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
    
      /** Configure the main internal regulator output voltage 
      */
      if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
      {
        Error_Handler();
      }
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
      RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
      RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
      RCC_OscInitStruct.PLL.PLLM = 1;
      RCC_OscInitStruct.PLL.PLLN = 8;
      RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
      RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
      RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
      if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    
      if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
      {
        Error_Handler();
      }
      PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_LPUART1
                                  |RCC_PERIPHCLK_USB;
      PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
      PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
      PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
      PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSE;
      PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
      PeriphClkInit.PLLSAI1.PLLSAI1N = 12;
      PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV2;
      PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
      PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
      PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
      if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
      {
        Error_Handler();
      }
    }
    
    /**
      * @brief LPUART1 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_LPUART1_UART_Init(void)
    {
    
      /* USER CODE BEGIN LPUART1_Init 0 */
    
      /* USER CODE END LPUART1_Init 0 */
    
      /* USER CODE BEGIN LPUART1_Init 1 */
    
      /* USER CODE END LPUART1_Init 1 */
      hlpuart1.Instance = LPUART1;
      hlpuart1.Init.BaudRate = 209700;
      hlpuart1.Init.WordLength = UART_WORDLENGTH_7B;
      hlpuart1.Init.StopBits = UART_STOPBITS_1;
      hlpuart1.Init.Parity = UART_PARITY_NONE;
      hlpuart1.Init.Mode = UART_MODE_TX_RX;
      hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
      hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
      hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
      hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
      hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;
      if (HAL_UART_Init(&hlpuart1) != HAL_OK)
      {
        Error_Handler();
      }
      if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
      {
        Error_Handler();
      }
      if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN LPUART1_Init 2 */
    
      /* USER CODE END LPUART1_Init 2 */
    
    }
    
    /**
      * @brief USART3 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_USART3_UART_Init(void)
    {
    
      /* USER CODE BEGIN USART3_Init 0 */
    
      /* USER CODE END USART3_Init 0 */
    
      /* USER CODE BEGIN USART3_Init 1 */
    
      /* USER CODE END USART3_Init 1 */
      huart3.Instance = USART3;
      huart3.Init.BaudRate = 115200;
      huart3.Init.WordLength = UART_WORDLENGTH_8B;
      huart3.Init.StopBits = UART_STOPBITS_1;
      huart3.Init.Parity = UART_PARITY_NONE;
      huart3.Init.Mode = UART_MODE_TX_RX;
      huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
      huart3.Init.OverSampling = UART_OVERSAMPLING_16;
      huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
      huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
      huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
      if (HAL_UART_Init(&huart3) != HAL_OK)
      {
        Error_Handler();
      }
      if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
      {
        Error_Handler();
      }
      if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
      {
        Error_Handler();
      }
      if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN USART3_Init 2 */
    
      /* USER CODE END USART3_Init 2 */
    
    }
    
    /**
      * @brief SPI1 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_SPI1_Init(void)
    {
    
      /* USER CODE BEGIN SPI1_Init 0 */
    
      /* USER CODE END SPI1_Init 0 */
    
      /* USER CODE BEGIN SPI1_Init 1 */
    
      /* USER CODE END SPI1_Init 1 */
      /* SPI1 parameter configuration*/
      hspi1.Instance = SPI1;
      hspi1.Init.Mode = SPI_MODE_MASTER;
      hspi1.Init.Direction = SPI_DIRECTION_2LINES;
      hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
      hspi1.Init.NSS = SPI_NSS_SOFT;
      hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
      hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi1.Init.CRCPolynomial = 7;
      hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
      hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
      if (HAL_SPI_Init(&hspi1) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN SPI1_Init 2 */
    
      /* USER CODE END SPI1_Init 2 */
    
    }
    
    /**
      * @brief TIM6 Initialization Function
      * @param None
      * @retval None
      */
    static void MX_TIM6_Init(void)
    {
    
      /* USER CODE BEGIN TIM6_Init 0 */
    
      /* USER CODE END TIM6_Init 0 */
    
      TIM_MasterConfigTypeDef sMasterConfig = {0};
    
      /* USER CODE BEGIN TIM6_Init 1 */
    
      /* USER CODE END TIM6_Init 1 */
      htim6.Instance = TIM6;
      htim6.Init.Prescaler = 0;
      htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
      htim6.Init.Period = 31;
      htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
      if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
      {
        Error_Handler();
      }
      sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
      sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
      if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN TIM6_Init 2 */
    
      /* USER CODE END TIM6_Init 2 */
    
    }
    
    /**
      * @brief USB_OTG_FS Initialization Function
      * @param None
      * @retval None
      */
    static void MX_USB_OTG_FS_PCD_Init(void)
    {
    
      /* USER CODE BEGIN USB_OTG_FS_Init 0 */
    
      /* USER CODE END USB_OTG_FS_Init 0 */
    
      /* USER CODE BEGIN USB_OTG_FS_Init 1 */
    
      /* USER CODE END USB_OTG_FS_Init 1 */
      hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
      hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
      hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
      hpcd_USB_OTG_FS.Init.Sof_enable = ENABLE;
      hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
      hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
      hpcd_USB_OTG_FS.Init.battery_charging_enable = ENABLE;
      hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
      hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;
      if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN USB_OTG_FS_Init 2 */
    
      /* USER CODE END USB_OTG_FS_Init 2 */
    
    }
    
    /**
      * @brief GPIO Initialization Function
      * @param None
      * @retval None
      */
    static void MX_GPIO_Init(void)
    {
      GPIO_InitTypeDef GPIO_InitStruct = {0};
    
      /* GPIO Ports Clock Enable */
      __HAL_RCC_GPIOC_CLK_ENABLE();
      __HAL_RCC_GPIOH_CLK_ENABLE();
      __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOB_CLK_ENABLE();
      __HAL_RCC_GPIOE_CLK_ENABLE();
      __HAL_RCC_GPIOD_CLK_ENABLE();
      __HAL_RCC_GPIOG_CLK_ENABLE();
      HAL_PWREx_EnableVddIO2();
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOB, CHIP_SELECT_Pin|RESET_Pin|PDWN_Pin, GPIO_PIN_SET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);
    
      /*Configure GPIO pin : B1_Pin */
      GPIO_InitStruct.Pin = B1_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : DRDY_Pin */
      GPIO_InitStruct.Pin = DRDY_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
      GPIO_InitStruct.Pull = GPIO_PULLUP;
      HAL_GPIO_Init(DRDY_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : CHIP_SELECT_Pin RESET_Pin PDWN_Pin */
      GPIO_InitStruct.Pin = CHIP_SELECT_Pin|RESET_Pin|PDWN_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_PULLUP;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
      /*Configure GPIO pins : LD3_Pin LD2_Pin */
      GPIO_InitStruct.Pin = LD3_Pin|LD2_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
      /*Configure GPIO pin : USB_PowerSwitchOn_Pin */
      GPIO_InitStruct.Pin = USB_PowerSwitchOn_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pin : USB_OverCurrent_Pin */
      GPIO_InitStruct.Pin = USB_OverCurrent_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      HAL_GPIO_Init(USB_OverCurrent_GPIO_Port, &GPIO_InitStruct);
    
    }
    
    /* USER CODE BEGIN 4 */
    
    /* USER CODE END 4 */
    
    /**
      * @brief  This function is executed in case of error occurrence.
      * @retval None
      */
    void Error_Handler(void)
    {
      /* USER CODE BEGIN Error_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
    
      /* USER CODE END Error_Handler_Debug */
    }
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *         where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(char *file, uint32_t line)
    { 
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
         tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
      /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */

  • In reply to Gokul Balasubramanian:

    Hi Gokul,

    I would recommend probing the /DRDY signal on the ADS1256 first to ensure that it is active. You should see a periodic waveform, with the time between /DRDY falling edge corresponding to the data rate. If you don't then you'll need to check that the ADC is properly powered, the GPIO configured correctly, and that the device is clocked.

    How are you connecting the ADS1256 to the STM MCU? Do you have a common ground between these devices?
    On one of your waveforms (with the Arduino) it looked like DOUT did not have a stable ground reference voltage.

    NOTE: When probing the SPI signals, make sure to probe them near the ADC, and ground the oscilloscope to the ADC's ground potential so that you can see what the ADC sees.

    Best regards,
    Chris Hall
    Applications Engineer | Precision ADCs


    Check out these helpful resources...
    TI Precision Data Converters | TI Precision Labs - ADCs | Analog Engineer's Calculator | Data Converters Learning Center | Selection Guide

     

  • In reply to Christopher Hall:

    Dear Hall,

        I checked all the things you mentioned and I still unable to get the converted data from adc to the stm32f302r8 board. I kindly request you to go through both codes and try to point the errors in my stm32f302r8 code. I've shared the code in the previous comment. I think I'm missing out some thing in my code. Please help me in finding out the missing part in my code. fyi the arduino code which i shared in the previous comment works perfectly. My spi clock geerates 1Mhz clock with clock polarity low and clock phase 2 edged. Kindly help me with the same.

  • In reply to Gokul Balasubramanian:

    Hi Gokul,

    Would it be possible for you to capture some more oscilloscope screenshots of the SPI communication? (Ideally showing the /CS, DIN, DOUT, SCLK signals).

    I looked through you code once and didn't see any obvious errors. If you're still running into issues then I think look at the SPI signals will be the easiest way to troubleshot.

    Best regards,
    Chris Hall
    Applications Engineer | Precision ADCs


    Check out these helpful resources...
    TI Precision Data Converters | TI Precision Labs - ADCs | Analog Engineer's Calculator | Data Converters Learning Center | Selection Guide