Stm32 Spi-flash w25q64

Source: Internet
Author: User

The W25Q64BV array is organized to 32,768 programmable pages of 256-bytes each. Up to bytes can is programmed at a time. Pages can erased in groups of (sector erase), groups of (32KB block erase), groups of (64KB block erase) or The entire chip (chip erase)

BUSY is a read only bit in the status register (S0) which is set to a 1 if the device is executing a Page program, Sector Erase, Block Erase, Chip Erase or Write Status Register instruction

w25q64

SPI Configuration Steps

1. Enable SPI clock
2. Enable Gpio port Clock
3. Initialize GPIO, configure pin mode
4. Initialize the SPI
5. Enable SPI
6.SPI Read and write data
7. View SPI Transfer status

Example



typedef struct {uint16_t spi_direction; 
    uint16_t Spi_mode; uint16_t spi_datasize; Data frame format uint16_t Spi_cpol; Clock polarity uint16_t Spi_cpha; Clock phase uint16_t Spi_nss; Software from the device management uint16_t Spi_baudrateprescaler; Baud rate control uint16_t spi_firstbit; Frame format uint16_t spi_crcpolynomial;

CRC polynomial Register}spi_inittypedef; #define En25x_writeenable 0x06 #define EN25X_WRITEDISABLE 0x04 #define EN25X_READSTATUSREG 0x0          5 #define EN25X_WRITESTATUSREG 0x01 #define EN25X_READDATA 0x03 #define En25x_fastreaddata            0x0B #define EN25X_FASTREADDUAL 0x3B #define EN25X_PAGEPROGRAM 0x02 #define En25x_blockerase             0xd8 #define EN25X_SECTORERASE 0x20 #define EN25X_CHIPERASE 0xC7 #define En25x_powerdown              0XB9 #define En25x_continue_read 0xFF #define En25x_releasepowerdown 0xAB #define En25x_deviceid    0xAB #define En25x_manufactdeviceid  0x90 #define En25x_jedecdeviceid 0x9F void Spi_init () {gpio_inittypedef GPIO = {gpio_pin_1 3 | gpio_pin_14 |  

    Gpio_pin_15, Gpio_speed_50mhz, gpio_mode_af_pp}; Spi_inittypedef SPI = {Spi_direction_2lines_fullduplex, spi_mode_master,//0x0104 Spi_datasi ze_8b, Spi_cpol_high, Spi_cpha_2edge, Spi_nss_soft, spi_baudrateprescaler_256, SPI

    _FIRSTBIT_MSB, 7}; Rcc_apb1periphclockcmd (Rcc_apb1periph_spi2, ENABLE); Enable SPI clock Rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOB, enable); Enable GPIO port Clock Gpio_init (GPIOB, &gpio); Initialize GPIO, configure pin mode Spi_init (SPI2, &SPI); Initialize SPI spi_cmd (SPI2, ENABLE);
    Enable SPI} void Spi2_setspeed (U8 rate) {SPI2->CR1 &= 0xffc7;
    SPI2->CR1 |= rate;
Spi_cmd (SPI2, ENABLE); } void En25q64_init () {gpio_inittypedef Gpiob = {Gpio_pin_12, Gpio_speed_50mhz, Gpio_ MODE_OUT_PP};  

    Gpio_inittypedef Gpiog13 = {gpio_pin_13, Gpio_speed_50mhz, gpio_mode_out_pp};

    Gpio_inittypedef Gpiog14 = {gpio_pin_13, Gpio_speed_50mhz, gpio_mode_out_pp};  

    Gpio_inittypedef gpiof = {gpio_pin_9, Gpio_speed_50mhz, gpio_mode_out_pp}; Rcc_apb2periphclockcmd (Rcc_apb2periph_gpiob | Rcc_apb2periph_gpiog |

    RCC_APB2PERIPH_GPIOF, ENABLE);
    Gpio_init (Gpiob, &GPIOB); Spi2_nss = 1;
    Ethernet module selection (pull high, prevent interference) gpio_init (Gpiog, &GPIOG13); Flash_cs = 1;
    Flash tablets selected Gpio_init (Gpiog, &GPIOG14); Sd_cs = 1;
    SD Card Module selected Gpio_init (Gpiof, &GPIOF); Tub_4 = 1;
NRF24L01 Module Selection spi_init ();

    } U8 Spi_read_write (U16 D)//spi Read and write data {while (Spi_i2s_getflagstatus (SPI2, spi_i2s_flag_txe)! = SET);

    Spi_i2s_senddata (SPI2, D);

    while (Spi_i2s_getflagstatus (SPI2, spi_i2s_flag_rxne)! = SET); Return Spi_i2s_receivedata (SPI2);

    } U16 en25q64_read_id () {U16 R;

    Flash_cs = 0;
    Spi_read_write (En25x_manufactdeviceid); Spi_read_write (0);
    Dummy spi_read_write (0);

    Spi_read_write (0);
    R = Spi_read_write (en25x_continue_read) << 8;

    R = Spi_read_write (En25x_continue_read);

    Flash_cs = 1;
return R;

    } U8 En25q64_read_status () {U8 R;

    Flash_cs = 0;
    Spi_read_write (En25x_readstatusreg);

    R = Spi_read_write (En25x_continue_read);

    Flash_cs = 1;
return R;

    } void En25q64_read (U8 *buf, u32 addr, U16 num) {u16 i = 0;

    Flash_cs = 0; 
    Spi_read_write (En25x_readdata); Spi_read_write ((U8) (addr) >> 16);
    24bit address Spi_read_write ((U8) (addr) >> 8);

    Spi_read_write ((U8) addr);
    for (i = 0; i < num; i++) {Buf[i] = Spi_read_write (en25x_continue_read);
} Flash_cs = 1;

    } void En25q64_write_enable () {flash_cs = 0;

    Spi_read_write (en25x_writeenable);
Flash_cs = 1; } void En25q64_wRite_disable () {flash_cs = 0;

    Spi_read_write (en25x_writeenable);
Flash_cs = 1;

    } void En25q64_write_status (U8 s) {flash_cs = 0;
    Spi_read_write (en25x_writedisable);

    Spi_read_write (s);
Flash_cs = 1; } void En25q64_is_busy () {while ((En25q64_read_status () & 0x01) = = 0x01);//busy} void En25q64_write_page (U8 *b

    UF, U32 addr, U16 num)//page Write {u16 i = 0;

    En25q64_write_enable ();

    Flash_cs = 0;  Spi_read_write (En25x_pageprogram);
    Page program Spi_read_write (addr >> 16);
    Spi_read_write (addr >> 8);

    Spi_read_write (addr);
    for (i = 0; i < num; i++) {spi_read_write (buf[i]);

    } Flash_cs = 1;
En25q64_is_busy ();
    } void En25q64_write_nocheck (U8 *buf, u32 addr, U16 num) {U8 *b = buf;
    U32 a = addr;

    U16 n = num; n = 256-(num%);

        Write a full page while (1) {en25q64_write_page (b, A, n);
        if (n = = num) {break;} B + = n;
        A + = n;

        num-= n;
        if (num > +) {n = 256;
        } else {n = num;
    }}} void En25q64_sector_erase (U32 a) {en25q64_write_enable ();

    En25q64_is_busy ();

    Flash_cs = 0;  
    Spi_read_write (en25x_sectorerase);    
    Spi_read_write ((U8) a >> 16);     
    Spi_read_write ((U8) a >> 8);      

    Spi_read_write (a);

    Flash_cs = 1;
En25q64_is_busy ();
} U8 en25qxx_buf[4096];
    void En25q64_write (U8 *buf, u32 addr, U16 num) {u16 i;
    U32 POS;
    U16 Offset;
    U16 N;

    U8 *b = buf; pos = addr/4096;
    Sector position offset = addr% 4096; n = 4096-offset;
    Write full sector if (num <= n) {n = num; } while (1) {En25q64_read (EN25QXX_BUF, POS * 4096, 4096);//read out the entire sector en25q64_sector_erase (POS);
        /Erase entire sector for (i = 0; i < n; i++) {en25qxx_buf[i + offset] = b[i];

   }     En25q64_write_nocheck (EN25QXX_BUF, POS * 4096, 4096);
        Writes the entire sector if (n = = num) {break;
        } pos++;
        num-= n;
        offset = 0; 
        B + = n;
        if (num > 4096) {n = 4096;
        } else {n = num;
    }}} void En25q64_chip_erase () {en25q64_write_enable ();

    En25q64_is_busy ();

    Flash_cs = 0;    

    Spi_read_write (en25x_chiperase);

    Flash_cs = 1;
En25q64_is_busy ();

    } void En25q64_power_down () {flash_cs = 0;    

    Spi_read_write (En25x_powerdown);

    Flash_cs = 1;
Delay_us (3);

    } void En25q64_release_power_down () {flash_cs = 0; 

    Spi_read_write (En25x_releasepowerdown);

    Flash_cs = 1;
Delay_us (3); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.