2022-09-21 06:03 AM
In a custom board using BlueNRG, sometimes, specially while using external debug interface, we fall in a condition in which BlueNRG get "broken". Investigating more in deep, we discovered that sending the write "header" packet (0x0A, 0x00, 0x00, 0x00, 0x00) over SPI, we always get (0x02, 0x00, 0x00, 0x00, 0x00) answer, so, the device seems to be in ready state but there is no space on transmit buffer. Below there is the code we used for test. Our BlueNRG function ever exit with "1" value (fail). At the moment we can solve the situation only by phisically replacing the BlueNRG chip, but it's very strange that we have a consistent SPI answer (with 0x02 byte correctly placed in first byte) but always 0 bytes on tx buffer. Can unibodi help us to understand? Thank you.
// SPI test
#define HEADER_SIZE 5
#define TIMEOUT_DURATION 15
static void comp_bluenrg_cs(bool enable)
{
HAL_GPIO_WritePin(nBLE_CS_GPIO_Port, nBLE_CS_Pin, enable ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
static int BlueNRG_SPI_Ready (void)
{
SPI_HandleTypeDef *hspi = &hspi1;
// Vedi ST - UM1865 - The BlueNRG-MS Application Command Interface (ACI).pdf
// 5.2 SPI communication protocol
uint8_t header_master[HEADER_SIZE] = {
0x0a, // Write operation
0x00, // Dummy
0x00, // Dummy
0x00, // Dummy
0x00 // Dummy
};
uint8_t header_slave[HEADER_SIZE] = {
0xaa,
0x00,
0x00,
0x00,
0x00
};
bluenrg_irq_disable ();
/* CS reset */
comp_bluenrg_cs(true);
/* Exchange header */
HAL_StatusTypeDef ret = HAL_SPI_TransmitReceive (hspi, header_master, header_slave, HEADER_SIZE, TIMEOUT_DURATION);
comp_bluenrg_cs(false);
bluenrg_irq_enable ();
if(ret != HAL_OK) {
return 1;
}
if (header_slave[0] != 0x02) {
// Device is not READY (sleeping).
return 2;
}
if (header_slave[1] == 0) {
// There isn't space in write buffer
return 3;
}
/* SPI is not ready */
return 0;
}
static int BlueNRG_SPI_Test ()
{
for(uint16_t ii = 0; ii < 100; ii++) {
int ret = BlueNRG_SPI_Ready ();
if(ret == 0) {
return 0;
}
if(HAL_GPIO_ReadPin(BLE_IRQ_GPIO_Port, BLE_IRQ_Pin) == GPIO_PIN_SET) {
// Thera are data to be read
HAL_Delay(2);
}
HAL_Delay(1);
}
// fail
return 1;
}