Skip to main content
togrady
Associate III
March 6, 2008
Question

SPI driver for Atmel DataFlash

  • March 6, 2008
  • 12 replies
  • 2182 views
Posted on March 06, 2008 at 20:18

SPI driver for Atmel DataFlash

    This topic has been closed for replies.

    12 replies

    lanchon
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:25

    and here's another forum bug:

    type &lt;PRE&gt; and hit preview, it gets converted to <PRE>... ugly!

    togrady
    togradyAuthor
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:25

    #define DUMMY_BYTE 0xff

    static unsigned char tx8(unsigned char data)

    {

    while ((SPI1->SR & SPI_FLAG_TXE) == (u16)RESET); // Loop while DR register in not emplty

    SPI1->DR = data;

    while ((SPI1->SR & SPI_FLAG_RXNE) == (u16)RESET); // Wait to receive a byte

    return SPI1->DR;

    }

    void spi_tx_8 (unsigned char _data8)

    {

    tx8(_data8);

    }

    unsigned char spi_rx_8 (void)

    {

    return tx8(DUMMY_BYTE);

    }

    unsigned long spi_rx_32 (void)

    {

    unsigned long r;

    int i;

    for (i = 0,r = 0; i

    {

    r |= spi_rx_8();

    if (i

    r<<=8;

    }

    return r;

    }

    void spi_init (void)

    {

    SPI_InitTypeDef SPI_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    /* Enable SPI1 and GPIOA clocks */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);

    /* Configure SPI1 pins: SCK(PA5), MISO(PA6) and MOSI(PA7) */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Configure PA.3 as Output push-pull, used as Flash Chip select */

    /********* using pc.6 (LED1)

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    */

    /* Deselect the FLASH: Chip Select high */

    DATAFLASH_CS_HI();

    /* SPI1 configuration */

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

    // SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;

    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

    SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Init(SPI1, &SPI_InitStructure);

    /* Enable SPI1 */

    SPI_Cmd(SPI1, ENABLE);

    }