stm32spistm32f0swd

stm32 and external flash (w25q) connection problem


I want to read/write from external flash (Winbond W25Q16BV) with STM32 micro (stm32F030F4). but running process halt on 'HAL_SPI_Init()' function.

I checked the debug process, and found HAL_SPI_STATE_BUSY. but i don't know why?

I am using STM32CubeMX to generate main project and Keil IDE to write and debug.

SPI_HandleTypeDef hspi1;


void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);


uint8_t spiData[2];

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_FATFS_Init();


  SPI_HandleTypeDef my_hspi;
  HAL_SPI_Init(&my_hspi);

  HAL_FLASH_Unlock();


  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET); // CS to HIGH
  HAL_Delay(10);


  //Read data
  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET); // CS to low

  spiData[0]=0x05;

  //transmit register address
  HAL_SPI_Transmit(&my_hspi,spiData,1,10);

 //read
  HAL_SPI_Receive(&my_hspi,&spiData[1],1,10);

  ...

Here is our schematic: enter image description here

Unfortunately, I did not find a good example/instruction of how to use external SPI libraries. Any help in this problem is highly appreciated.


Solution

  • You have not set any parameters for the my_hspi struct so your HAL driver doesn't know what he has to do.

    Look at the definition of the struct. There are a lot of comments what the different struct elements are used for. For initialization the my_hspi.init part will be most interesting. Also you have to the the my_hspi.Instance to the desired SPI Channel.

    You can generate an example configuration using the free STM32 Cube Mx Software.