user11895228
user11895228

Reputation: 1

SPI Communication Problem Between STM32F-Discovery and MAX31856

I was trying to communicate my STM discovery board with MAX31856 using SPI communication. According to datasheet, I should send address byte first, then data bytes following. I am currently able to write to registers but when i try to read the data i wrote to registers, I couldn't read data unless I only send read command.

For example:

http://puu.sh/E2gJD.png With this code, I am able to read default register values.

http://puu.sh/E2gLC.png With this code, I am able to write new values to CR0 and CR1 registers. The first 0x80 value is register CR0's address byte. The next values are new register values 0x80 and 0x37.

http://puu.sh/E2gNK.png I stopped the previous program and changed my code to this one. Then I started debugging. As you can see from this picture, the CR0 and CR1 register values are changed to what we wrote before.

Finally; http://puu.sh/E2gRw.png When I try to combine these two operations, writing and reading, I wasn't able to read what I wrote to registers. Need some help about solving this problem.

uint8_t spiTXData[17],spiRXData[17],spiTXDataR[17], spiRXDataR[17];

/* USER CODE END 0 */

  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_DAC_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */



//    HAL_Delay ( 1 ) ;
//
//      HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
//    spiTXDataR[0] = 0x00;
//    HAL_SPI_TransmitReceive(&hspi1, spiTXDataR, spiRXDataR, 3, 50);
//    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);

//    HAL_Delay ( 1 ) ;
    spiTXData[0] = 0x80;
    spiTXData[1] = 0x80;
    spiTXData[2] = 0x37;
    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
    HAL_SPI_TransmitReceive(&hspi1, spiTXData, spiRXData, 3, 50);
    HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
        spiTXDataR[0] = 0x00;
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
        HAL_SPI_TransmitReceive(&hspi1, spiTXDataR, spiRXDataR, 17, 50);
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

I need to be able to read the commands I write to CR0 and CR1 registers later. How can I do that? I hope you understand my problem and able to help me. Thanks.

Upvotes: -1

Views: 199

Answers (1)

EN20
EN20

Reputation: 53

As far as I know there is no CR0 and CR1 but I assume what you are locking for is CR1 and CR2. You can read these values by either setting a pointer to the address of the register and reding the dereferenced pointer value like so:

 uint32_t *ptr = REGISTER_ADDRESS;
 variable = *ptr;

now variable should contain the registers value assuming that the regsiter you are looking for is a 32 bit register.

Another way is to use the premade "defines" true studio provides to read those values for example:

SPI1->CR1

In my case it is SPI1 but it depends on the how many SPIs your mcu has and which one you are using. Usualy the Reference Manual and/or the Data Sheet that are provided by ST on their Website (specific one for each MCU or at least MCU family) are a pretty got help in figuring out the names of the registers and/or their addresses.

Upvotes: 0

Related Questions