cant read a register with SPI
Hey There,
Ive got a Semtech SX1261 Board (LoRa- tranceiver) and iam trying to read its registers with my STM32L432KC (with SPI)
Ive got working arduino code to read all its registers, but its giving me different results when i try the same thing with my STM controller..
I tried to reduce the problem to the bare minimum ( read a single 1 byte register) buts its giving me wrong results... I read the datasheet and configured my SPI Interface in the .ioc file (CPOL, CPHA, max freq and the used pins)
I don't know what else I could try now .. so iam asking you guys.
Any help/tips would be super nice!
Firstoff the arduino code :
void loop()
{ uint16_t add = 0x8E1; // The Register i want to read
uint8_t addr_l, addr_h;
addr_h = add >> 8;
addr_l = add & 0x00FF;
Serial.print("MSB: ");
Serial.print(addr_h);
Serial.print(" LSB: ");
Serial.print(addr_l);
Serial.print(" addr: ");
Serial.println(add);
checkBusy();
digitalWrite(NSS, LOW);
SPI.transfer(RADIO_READ_REGISTER); //0x1D
SPI.transfer(addr_h); //MSB
SPI.transfer(addr_l); //LSB
SPI.transfer(0xFF);
uint8_t data = SPI.transfer(0xFF);
digitalWrite(NSS, HIGH);
checkBusy();
Serial.println(data);
delay(5000);
}
Output :
MSB: 8 LSB: 225 addr: 2273 (decimal)
57 (decimal)now my STM Code :
while(1) {
uint16_t add = 0x8E1; // address i want to read ; result should be 57 in DEC
uint8_t addr_l, addr_h;
addr_h = add >> 8;
addr_l = add & 0x00FF;
checkBusy();
HAL_GPIO_WritePin(SX1261_NSS_GPIO_Port, SX1261_NSS_Pin, GPIO_PIN_RESET);
uint8_t rrg = RADIO_READ_REGISTER; //0x1D
uint8_t term = 0xFF;
HAL_SPI_Transmit(&hspi1, &rrg,1, HAL_MAX_DELAY);
HAL_SPI_Transmit(&hspi1, &addr_h,1, HAL_MAX_DELAY);
HAL_SPI_Transmit(&hspi1, &addr_l,1, HAL_MAX_DELAY);
HAL_SPI_Transmit(&hspi1, &term,1, HAL_MAX_DELAY);
uint8_t spirxbuff=0;//[4] ={0};
//HAL_SPI_Receive(&hspi1, &spirxbuff, 1, HAL_MAX_DELAY);
HAL_SPI_TransmitReceive(&hspi1, &term, &spirxbuff, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(SX1261_NSS_GPIO_Port, SX1261_NSS_Pin, GPIO_PIN_SET);
checkBusy();
char s1[100] ;
sprintf(s1,"MSB: %i LSB: %i addr = %i ; %x \r\n",addr_h, addr_l, add,spirxbuff);
tx_com((uint8_t*)s1, strlen(s1));
HAL_Delay(3000);
}the output is :
MSB: 8 LSB: 225 addr = 2273 ; 8 the problem is that the value of the read register is wrong (8 instead of 57)
this is my SPI_Init funcion:
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_4BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
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 */
__HAL_SPI_ENABLE(&hspi1);
/* USER CODE END SPI1_Init 2 */
}thank you very much!
