spi_flash.c 6.1 KB
Newer Older
wuyangyong's avatar
wuyangyong 已提交
1 2
#include <stm32f10x.h>
#include "spi_flash.h"
wuyangyong's avatar
wuyangyong 已提交
3
#include "rtthread.h"
wuyangyong's avatar
wuyangyong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

extern unsigned char SPI_WriteByte(unsigned char data);

/********************** hardware *************************************/
/* SPI_FLASH_CS   PA4 */
/* SPI_FLASH_RST  PA3 */
#define FLASH_RST_0()    GPIO_ResetBits(GPIOA,GPIO_Pin_3)
#define FLASH_RST_1()    GPIO_SetBits(GPIOA,GPIO_Pin_3)

#define FLASH_CS_0()     GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define FLASH_CS_1()     GPIO_SetBits(GPIOA,GPIO_Pin_4)
/********************** hardware *************************************/

static void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);

wuyangyong's avatar
wuyangyong 已提交
28
    FLASH_RST_0(); // RESET
wuyangyong's avatar
wuyangyong 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    FLASH_CS_1();
    FLASH_RST_1();
}

static unsigned char SPI_HostReadByte(void)
{
    //return SPI_WriteByte(0x00);
    //Wait until the transmit buffer is empty
    //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
    while( (SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);
    // Send the byte
    SPI_I2S_SendData(SPI1, 0);

    //Wait until a data is received
    //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
    while( (SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);
    // Get the received data
    return SPI_I2S_ReceiveData(SPI1);

}

static void SPI_HostWriteByte(unsigned char wByte)
{
    SPI_WriteByte(wByte);
}

/*****************************************************************************/
/*Status Register Format:                                   */
/* ------------------------------------------------------------------------- */
/* | bit7   | bit6   | bit5   | bit4   | bit3   | bit2   | bit1   | bit0   | */
/* |--------|--------|--------|--------|--------|--------|--------|--------| */
/* |RDY/BUSY| COMP   |         device density            |   X    |   X    | */
/* ------------------------------------------------------------------------- */
/* 0:busy   |        |        AT45DB041:0111             | protect|page size */
/* 1:ready  |        |        AT45DB161:1011             |                   */
/* --------------------------------------------------------------------------*/
/*****************************************************************************/
static unsigned char AT45DB_StatusRegisterRead(void)
{
    unsigned char i;

    FLASH_CS_0();
    SPI_HostWriteByte(AT45DB_READ_STATE_REGISTER);
    i=SPI_HostReadByte();
    FLASH_CS_1();

    return i;
}

static void wait_busy(void)
{
    unsigned int    i=0;
wuyangyong's avatar
wuyangyong 已提交
81
    while (i++<3000)
wuyangyong's avatar
wuyangyong 已提交
82 83 84 85 86 87
    {
        if (AT45DB_StatusRegisterRead()&0x80)
        {
            break;
        }
    }
wuyangyong's avatar
wuyangyong 已提交
88 89 90 91
    if( !(i<3000) )
    {
        rt_kprintf("\r\nSPI_FLASH timeout!!!");
    }
wuyangyong's avatar
wuyangyong 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

static void read_page(unsigned int page,unsigned char * pHeader)
{
    unsigned int i=0;

    wait_busy();

    FLASH_CS_0();
    SPI_HostWriteByte(AT45DB_MM_PAGE_TO_B1_XFER);
    SPI_HostWriteByte((unsigned char)(page >> 6));
    SPI_HostWriteByte((unsigned char)(page << 2));
    SPI_HostWriteByte(0x00);
    FLASH_CS_1();

    wait_busy();

    FLASH_CS_0();
    SPI_HostWriteByte(AT45DB_BUFFER_1_READ);
    SPI_HostWriteByte(0x00);
    SPI_HostWriteByte(0x00);
    SPI_HostWriteByte(0x00);
    SPI_HostWriteByte(0x00);
    for (i=0; i<512; i++)
    {
        *pHeader++ = SPI_HostReadByte();
    }
    FLASH_CS_1();

}

static void write_page(unsigned int page,unsigned char * pHeader)
{
    unsigned int i;

    wait_busy();

    FLASH_CS_0();
    SPI_HostWriteByte(AT45DB_BUFFER_2_WRITE);
    SPI_HostWriteByte(0);
    SPI_HostWriteByte(0);
    SPI_HostWriteByte(0);
    for(i=0; i<512; i++)
    {
        SPI_HostWriteByte(*pHeader++);
    }
    FLASH_CS_1();

    wait_busy();

    FLASH_CS_0();
    SPI_HostWriteByte(AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE);
    SPI_HostWriteByte((unsigned char)(page>>6));
    SPI_HostWriteByte((unsigned char)(page<<2));
    SPI_HostWriteByte(0x00);
    FLASH_CS_1();
}


#include <rtthread.h>
/* SPI DEVICE */
static struct rt_device spi_flash_device;

/* RT-Thread Device Driver Interface */
static rt_err_t rt_spi_flash_init(rt_device_t dev)
{
    return RT_EOK;
}

static rt_err_t rt_spi_flash_open(rt_device_t dev, rt_uint16_t oflag)
{

    return RT_EOK;
}

static rt_err_t rt_spi_flash_close(rt_device_t dev)
{
    return RT_EOK;
}

static rt_err_t rt_spi_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
    return RT_EOK;
}

static rt_size_t rt_spi_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
    rt_uint8_t *ptr;
    rt_uint32_t index, nr;

    nr = size/512;
    ptr = (rt_uint8_t*)buffer;

    for (index = 0; index < nr; index ++)
    {
        /* only supply single block read: block size 512Byte */
        read_page((pos + index * 512)/512, &ptr[index * 512]);
    }

    return nr * 512;
}

static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
    rt_uint8_t *ptr;
    rt_uint32_t index, nr;

    nr = size / 512;
    ptr = (rt_uint8_t*)buffer;

    for (index = 0; index < nr; index ++)
    {
        /* only supply single block write: block size 512Byte */
        write_page((pos + index * 512)/512, &ptr[index * 512]);
    }

    return nr * 512;
}

void rt_hw_spi_flash_init(void)
{
    GPIO_Configuration();

    /* register spi_flash device */
    spi_flash_device.type    = RT_Device_Class_Block;
    spi_flash_device.init    = rt_spi_flash_init;
    spi_flash_device.open    = rt_spi_flash_open;
    spi_flash_device.close   = rt_spi_flash_close;
    spi_flash_device.read 	 = rt_spi_flash_read;
    spi_flash_device.write   = rt_spi_flash_write;
    spi_flash_device.control = rt_spi_flash_control;

    /* no private */
    spi_flash_device.private = RT_NULL;

    rt_device_register(&spi_flash_device, "spi0",
                       RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}