STM32 BlueNRG SPI

73 views Asked by At

I'm using B-L4S5I-IOT01A board (STM32L4S5VIT6) which from what I understand contains BlueNRG-MS on SPI3.

I configure BLE using following function:

HAL_StatusTypeDef ble_init()
{
    __HAL_RCC_GPIOC_CLK_ENABLE();
    GPIO_InitTypeDef gpio;
    gpio.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; // PC10 - SPI3_CLK, PC11 - SPI3_MISO, PC12 - SPI3_MOSI
    gpio.Mode = GPIO_MODE_AF_PP;
    gpio.Alternate = GPIO_AF6_SPI3;
    HAL_GPIO_Init(GPIOC, &gpio);

    __HAL_RCC_GPIOD_CLK_ENABLE();
    GPIO_InitTypeDef gpio_cs;
    gpio.Pin = GPIO_PIN_13; // PD13 - SPBTLE-RF-SPI3_CSN
    gpio.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(GPIOD, &gpio_cs);

    __HAL_RCC_SPI3_CLK_ENABLE();
    spi.Instance = SPI3;
    spi.Init.Mode = SPI_MODE_MASTER;
    spi.Init.Direction = SPI_DIRECTION_2LINES;
    spi.Init.NSS = SPI_NSS_SOFT;
    spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
    HAL_StatusTypeDef status = HAL_SPI_Init(&spi);
    __HAL_SPI_ENABLE(&spi);
    return status;
}

HAL_StatusTypeDef spi_send_recv(uint8_t *send_buf, uint8_t *recv_buf, size_t size)
{
    HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(&spi, send_buf, recv_buf, size, HAL_MAX_DELAY);
    return status;
}

My understanding is, when I use SPI I should set CS PIN to Low, transmit/receive data and set CS PIN to High again. Also, using BlueNRG-MS Bluetooth manual, it seems that I should receive proper status (0x02) on first received byte - in my case all bytes are 0. Are there any additional steps I should perform, to use SPI?

int main() 
{
    SystemCoreClock = 4000000;
    HAL_Init();

    ble_init();

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);

    while (1) {
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
      uint8_t tx[5] = {0x0a,0,0,0,0};
      uint8_t rx[5];
      spi_send_recv(tx, rx, 5);
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
    }
}
1

There are 1 answers

0
Witnes On BEST ANSWER

It seems resetting BLE module is required to acquire any valid response. On my module, BLE reset PIN is PA8, so

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

resolved my issue and I got proper status.