Simple Metal Detectors

Read and write external memory MX25L128 via SPI bus for STM32F103

I am currently working on a battery-powered wireless terminal , which uses STM32F103 and MXIC’s MX25L128 memory. The underlying driver is automatically generated using STM32CubeMX, and the test is OK. Now I will share my experience in the configuration and debugging process with you.

MX25L128 is connected to STM32F103 microcontroller via SPI3 bus, and CS chip select is selected by GPIO software simulation. The connection method is as follows:

Read and write external memory MX25L128 via SPI bus for STM32F103

The process of generating HAL driver by STM32CubeMX

First, create a new project and select STM32F103VETx in MCU Selector . Then configure the used pins in the Pinout tab. Only the related pins of SYS, RCC, SPI3 and USART1 are modified in the test code.

Configure SPI3 peripherals. The SPI3 bus supports full-duplex and half-duplex master/slave modes. Here we select the full-duplex master mode and do not enable the hardware NSS signal. STM32CubeMX automatically configures PB3 to SPI3_SCK, PB4 to SPI3_MISO, and PB5 to SPI3_MOSI. Then we configure PD7 to GPIO output mode as the chip select pin of the SPI3 bus.

Then in the SPI3 configuration, we select the frame format as Motorola , the data length as 8 bits, and the first bit MSB first. Set the SPI bus clock division factor to 4, so the baud rate is 9.0 Mbit/s. Next, we set the phase and polarity of the clock signal. MX25L128 supports mode 1 and mode 3 operation. We choose mode 3, that is, CPOL is selected as High and CPHA is selected as 2 Edge . Keep other options as default.

Next, in the GPIO pin configuration, we set the user label of PD7 to SPI3_CS and the output level to High, so that the SPI memory is not selected by default.

Finally, we select HSE in RCC to use an external 8M crystal oscillator, and select Serial wire debugging mode in Debug in SYS. USART1 selects asynchronous communication mode and uses the default configuration. This peripheral is used to implement the printf print output function. In the clock configuration, set the main clock HCLK to 72MHz. The complete pin configuration is as follows:

Click the menu Project->Generate code… , the software pops up the project configuration dialog box, we set the project name to SPI_Flash Test, select the folder to save it. The tool chain uses MDK-ARM V5. Click OK to generate the project code.

Project file configuration

Open the generated project, we can see that the spi initialization function is located in the stm32 f1xx_hal_msp.c file, and the related SPI operation function is located in the stm32f1xx_hal_spi file. We have written the underlying driver for MX25L128, you can download and use it directly: BSP.

Copy the attachment to the Driver folder in the project directory. Add a group in the project directory, rename it to Drivers/BSP/ZQ1102 , and add mx25l128.c to the group.

In the project’s target options, add ../Drivers/BSP/ZQ1102 to the project’s Include Path .

The next step is to modify the main.c file and add the required header files between /* USER CODE BEGIN Includes */ and /* USER CODE END Includes */.

  1. /* USER CODE BEGIN Includes */
  2. #include “mx25l128.h”
  3. /* USER CODE END Includes */

Copy code

Add the test function of MX25 memory chip between /* USER CODE BEGIN 2 */ and /* USER CODE END 2 */.

  1.   /* USER CODE BEGIN 2 */
  2. BSP_MX25Lx_Test();
  3.   /* USER CODE END 2 */

Copy code

Add the following code between /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ to implement the printf function.

  1. /* USER CODE BEGIN 4 */
  2. #ifdef __GNUC__
  3.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  4.      set to ‘Yes’) calls __io_putchar() */
  5.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  6. #else
  7.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  8. #endif /* __GNUC__ */
  9. /**
  10.   * @brief Retargets the C library printf function to the USART.
  11.   * @param None
  12.   * @retval None
  13.   */
  14. PUTCHAR_PROTOTYPE
  15. {
  16.   /* Place your implementation of fputc here */
  17.   /* eg write a character to the EVAL_COM1 and Loop until the end of transmission */
  18.   HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  19.         ;
  20.   return ch;
  21. }
  22. /* USER CODE END 4 */

Copy code

After the modification is completed, recompile and download it to the circuit board for running. Open the serial port software on the computer and you can see the test information.

The whole process is as described above. I took some detours during the debugging process and always failed to debug. The ARM core has built-in ITM function, and the printf function can be implemented through the emulator. The multiplexing function of PB3 is the TRACESWO pin, so it conflicts with the SPI3 bus. SPI3 and ITM cannot be implemented at the same time. If Trace Enable is enabled in Development- >Setting->Trace in the keil software , whether the hardware is connected as ITM or not, it will cause SPI3 bus communication abnormality.

Exit mobile version