00001
00028 #include <string.h>
00029 #include "compiler.h"
00030 #include "preprocessor.h"
00031 #include "board.h"
00032 #include "power_clocks_lib.h"
00033 #include "gpio.h"
00034 #include "spi.h"
00035 #include "conf_at45dbx.h"
00036 #include "at45dbx.h"
00037 #include "clocks.h"
00038
00039
00040 void flash_init(void)
00041 {
00042 static const gpio_map_t AT45DBX_SPI_GPIO_MAP = {
00043 { AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION },
00044 { AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION },
00045 { AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION },
00046 #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \
00047 { AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION },
00048 MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~)
00049 #undef AT45DBX_ENABLE_NPCS_PIN
00050 };
00051
00052 spi_options_t spiOptions = {
00053 .reg = AT45DBX_SPI_FIRST_NPCS,
00054 .baudrate = AT45DBX_SPI_MASTER_SPEED,
00055 .bits = AT45DBX_SPI_BITS,
00056 .spck_delay = 0,
00057 .trans_delay = 0,
00058 .stay_act = 1,
00059 .spi_mode = 0,
00060 .modfdis = 1
00061 };
00062
00063 gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00064 sizeof(AT45DBX_SPI_GPIO_MAP) /
00065 sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00066
00067 spi_initMaster(AT45DBX_SPI, &spiOptions);
00068
00069 spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00070 spi_enable(AT45DBX_SPI);
00071 at45dbx_init(spiOptions, FPBA_HZ);
00072 }
00073
00074 void flash_write(U32 addr, const U8* buf, U32 len)
00075 {
00076 U32 sector = addr / AT45DBX_SECTOR_SIZE;
00077 U32 i;
00078 Assert(addr % AT45DBX_SECTOR_SIZE == 0);
00079
00080 at45dbx_write_open(sector);
00081 for (i = 0; i < len; i++)
00082 at45dbx_write_byte(buf[i]);
00083 at45dbx_write_close();
00084 }
00085
00086 void flash_read(U32 addr, U8* buf, U32 len)
00087 {
00088 U32 sector = addr / AT45DBX_SECTOR_SIZE;
00089 U32 i;
00090 Assert(addr % AT45DBX_SECTOR_SIZE == 0);
00091
00092 at45dbx_read_open(sector);
00093 for (i = 0; i < len; i++)
00094 buf[i] = at45dbx_read_byte();
00095 at45dbx_read_close();
00096 }