September 2019 Saturday 21 (STM32)

A .Flash

1. Built-in Flash programming access

(1) In the erase and write flash must be unlocked, locked after operation

    FLASH_Unlock (); // unlock

    FLASH_Lock (); // lock

Before (2) read / write Flash should clear the error identification

    FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR|

                    FLASH_FLAG_PGAERR|FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);

(3) erase and write operations

    FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)

    parameter:

        FLASH_Sector - which sector

        VoltageRange - voltage range

    Return FLASH_COMPLETE expressed erase success

    FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)

    parameter:

        Address - the address written

        Date - write data

    Indicates a write successful return FLASH_COMPLETE

(4) Read Operation

    Flash read operation is very simple, direct address to be read as a Flash memory addresses to access it

Exercise:

    Read the temperature via Bluetooth, the storage temperature and reading time to flash

Two .SPI

1. Concept and bus structures,

    SPI Serial Peripheral interface is an abbreviation, is the serial peripheral interface. Motorola is designed. SPI interface communication between the main clock used in eeprom flash rtc AD converter, a digital signal processor, etc., and CPU peripherals

    The SPI is a high-speed, full-duplex, synchronous serial bus (from a multi-master), data transmission requires four wires

        SDI: Serial Data Input

        SDO: Serial Data Output

        SCK: clock line

        CS: From the device is enabled

 

To distinguish the direction of transmission of the master and slave devices, we generally named in the following manner

    MISO (master input slave output): master input, slave output

    MOSI (master output slave input): master output, slave input

    SCLK: clock signal line, the main control device

    CS: chip select signal from the line, the main control device

2. Communication principle

 

 

 

 

    Transmission timing IO ports may be used an analog implementation may be implemented using the SPI Controller

3. The clock phase and polarity

(1) Clock Polarity

    The default level decision clock line

    CPOL = 1 default high

    Low default CPOL = 0

(2) Clock Phase

    Several edge decision reader

    CPHA = 1 sampled at the second edge

    CPHA = 0 is sampled at a first edge

 

 

 

4.stm32f407 of SPI controller

 

 

 

 

 

 Three .SPI Flash -------- W25Q128

1. Schematic

 

 

 

 

 2.w25q128 instruction manual

(1) a high order position and polarity

    MSB

    mode = 0 (the default level)

    mode = 1 (default high level)

    

    Rising input falling output

(2) Status register 1 and 2

 

 

 

(3) operation of the chip

 

 

   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.SPI Controller Programming

(1) Open GPIO clock and SPI

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

    ....

(2) configured to multiplex the GPIO function (chip select pin is configured as output)

    GPIO_Init (...);

(3) The IO port multiplexed and mapped to the SPI function

    GPIO_PinAFConfig (GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
    GPIO_PinAFConfig (GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);    

    GPIO_PinAFConfig (GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);

(4) SPI Initialization

    void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)

    parameter:

        SPIx - which SPI

        SPI_InitStruct - initialization structure

 struct typedef
{
  uint16_t SPI_Direction;! / * <full-duplex transmission SPI_Direction_2Lines_FullDuplex @ref SPI_data_direction * /

  uint16_t SPI_Mode;! / * <master / slave mode SPI_Mode_Master @ref SPI_mode * /

  uint16_t SPI_DataSize;! / * <data length SPI_DataSize_8b @ref SPI_data_size * /

  uint16_t SPI_CPOL;                /*!< 极性 SPI_CPOL_Low ? @ref SPI_Clock_Polarity */

  uint16_t SPI_CPHA;                /*!< 相位 SPI_CPHA_1Edge ?@ref SPI_Clock_Phase */

  uint16_t SPI_NSS;! / * <Chip Select * SPI_Slave_Select_management @ref selected SPI_NSS_Soft /
 
  uint16_t SPI_BaudRatePrescaler;! / * <prescaler SPI_BaudRatePrescaler_16 * /

  uint16_t SPI_FirstBit;! / * <high ​​position in order SPI_FirstBit_MSB * /

  uint16_t SPI_CRCPolynomial;! / * <CRC check is not used * /
} SPI_InitTypeDef;

(5) Enable SPI1

    SPI_Cmd(SPI1,ENABLE);


(6) transmitting and receiving data

    uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)

    void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)

(7) transmission status query

    FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)

    

Note: SPI transmit and receive functions for

u8 xxx(u8 data)

{

    // Wait transmit buffer is empty

    while(SPI_I2S_GetFlagStatus(...)...);

    //send data

    SPI_I2S_SendData(.....);

    // synchronize the received data reception waiting

    while(SPI_I2S_GetFlagStatus(...)...);

    //Receive data

    return SPI_I2S_ReceiveData(...);

}

Note: SPI and the communication device is selected from the sheet before the election

    Down before communication chip select

Communication is completed chip select pulled

Guess you like

Origin www.cnblogs.com/zjlbk/p/11573985.html