SPI MISO value 255

1.1k views Asked by At

i am trying to move my program to hal and my SPI is receiving only 255. What did i forget to set?

Function for reading:

uint8_t addressMsb[1] = { READ_ADDRESS | RTD_MSB_ADDRESS };
uint8_t addressLsb[1] = { READ_ADDRESS | RTD_LSB_ADDRESS };

uint8_t Temp_Msb[1];
uint8_t Temp_Lsb[1];
...
PT_CS1_LOW;
HAL_SPI_Transmit(&hspi1, (uint8_t*) addressMsb, 1, 10);
HAL_SPI_Receive(&hspi1, (uint8_t*) Temp_Msb, 1, 10);
PT_CS1_HIGH;
for(int i = 0; i<10;i++);
PT_CS1_LOW;
HAL_SPI_Transmit(&hspi1, (uint8_t*) addressLsb, 1, 10);
HAL_SPI_Receive(&hspi1, (uint8_t*) Temp_Lsb, 1, 10);
PT_CS1_HIGH;
...

Settings:

void MX_SPI1_Init(void)
{

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_HIGH;
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;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
   Error_Handler();
}

}
...
__HAL_RCC_SPI1_CLK_ENABLE();

/**SPI1 GPIO Configuration    
PA5     ------> SPI1_SCK
PA6     ------> SPI1_MISO
PA7     ------> SPI1_MOSI 
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

I know that it isnt a HW fail because a selfmade function on this board works. (frequency and settings are the same)

enter image description here

the old function:

if (!((SPI1)->CR1 & SPI_CR1_SPE)){
while(((SPI1)->SR & (SPI_SR_TXE | SPI_SR_RXNE)) == 0 || (SPI1)->SR & SPI_SR_BSY))SPI_WAIT(SPIx);

SPI1->DR = data;

while(((SPI1)->SR & (SPI_SR_TXE | SPI_SR_RXNE)) == 0 || ((SPI1)->SR & SPI_SR_BSY))SPI_WAIT(SPIx);

return SPI1->DR;
}
1

There are 1 answers

0
Michal D On BEST ANSWER

Sry guys my mistake, if someone needs a working example for MAX31865 you can take the code up there... I powered down my computer and my HW for 5 minutes and now is everthink working even with HAL. It is suspicious that it did work with the old program and with HAL dont and after restart are both working... but now i know the HAL code is from principle is not wrong.

READ_ADDRESS = 0x00
RTD_MSB_ADDRESS = 0x01
RTD_LSB_ADDRESS = 0x02