spi_flash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stm32f10x.h>
  2. #include "spi_flash.h"
  3. extern unsigned char SPI_WriteByte(unsigned char data);
  4. /********************** hardware *************************************/
  5. /* SPI_FLASH_CS PA4 */
  6. /* SPI_FLASH_RST PA3 */
  7. #define FLASH_RST_0() GPIO_ResetBits(GPIOA,GPIO_Pin_3)
  8. #define FLASH_RST_1() GPIO_SetBits(GPIOA,GPIO_Pin_3)
  9. #define FLASH_CS_0() GPIO_ResetBits(GPIOA,GPIO_Pin_4)
  10. #define FLASH_CS_1() GPIO_SetBits(GPIOA,GPIO_Pin_4)
  11. /********************** hardware *************************************/
  12. static void GPIO_Configuration(void)
  13. {
  14. GPIO_InitTypeDef GPIO_InitStructure;
  15. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  16. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19. GPIO_Init(GPIOA,&GPIO_InitStructure);
  20. FLASH_RST_0(); // RESET
  21. FLASH_CS_1();
  22. FLASH_RST_1();
  23. }
  24. static unsigned char SPI_HostReadByte(void)
  25. {
  26. return SPI_WriteByte(0x00);
  27. }
  28. static void SPI_HostWriteByte(unsigned char wByte)
  29. {
  30. SPI_WriteByte(wByte);
  31. }
  32. /*****************************************************************************/
  33. /*Status Register Format: */
  34. /* ------------------------------------------------------------------------- */
  35. /* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
  36. /* |--------|--------|--------|--------|--------|--------|--------|--------| */
  37. /* |RDY/BUSY| COMP | device density | X | X | */
  38. /* ------------------------------------------------------------------------- */
  39. /* 0:busy | | AT45DB041:0111 | protect|page size */
  40. /* 1:ready | | AT45DB161:1011 | */
  41. /* --------------------------------------------------------------------------*/
  42. /*****************************************************************************/
  43. static unsigned char AT45DB_StatusRegisterRead(void)
  44. {
  45. unsigned char i;
  46. FLASH_CS_0();
  47. SPI_HostWriteByte(AT45DB_READ_STATE_REGISTER);
  48. i=SPI_HostReadByte();
  49. FLASH_CS_1();
  50. return i;
  51. }
  52. static void wait_busy(void)
  53. {
  54. unsigned int i=0;
  55. while (i++<2000)
  56. {
  57. if (AT45DB_StatusRegisterRead()&0x80)
  58. {
  59. break;
  60. }
  61. }
  62. }
  63. static void read_page(unsigned int page,unsigned char * pHeader)
  64. {
  65. unsigned int i=0;
  66. wait_busy();
  67. FLASH_CS_0();
  68. SPI_HostWriteByte(AT45DB_MM_PAGE_TO_B1_XFER);
  69. SPI_HostWriteByte((unsigned char)(page >> 6));
  70. SPI_HostWriteByte((unsigned char)(page << 2));
  71. SPI_HostWriteByte(0x00);
  72. FLASH_CS_1();
  73. wait_busy();
  74. FLASH_CS_0();
  75. SPI_HostWriteByte(AT45DB_BUFFER_1_READ);
  76. SPI_HostWriteByte(0x00);
  77. SPI_HostWriteByte(0x00);
  78. SPI_HostWriteByte(0x00);
  79. SPI_HostWriteByte(0x00);
  80. for (i=0; i<512; i++)
  81. {
  82. *pHeader++ = SPI_HostReadByte();
  83. }
  84. FLASH_CS_1();
  85. }
  86. static void write_page(unsigned int page,unsigned char * pHeader)
  87. {
  88. unsigned int i;
  89. wait_busy();
  90. FLASH_CS_0();
  91. SPI_HostWriteByte(AT45DB_BUFFER_2_WRITE);
  92. SPI_HostWriteByte(0);
  93. SPI_HostWriteByte(0);
  94. SPI_HostWriteByte(0);
  95. for(i=0; i<512; i++)
  96. {
  97. SPI_HostWriteByte(*pHeader++);
  98. }
  99. FLASH_CS_1();
  100. wait_busy();
  101. FLASH_CS_0();
  102. SPI_HostWriteByte(AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE);
  103. SPI_HostWriteByte((unsigned char)(page>>6));
  104. SPI_HostWriteByte((unsigned char)(page<<2));
  105. SPI_HostWriteByte(0x00);
  106. FLASH_CS_1();
  107. }
  108. #include <rtthread.h>
  109. /* SPI DEVICE */
  110. static struct rt_device spi_flash_device;
  111. /* RT-Thread Device Driver Interface */
  112. static rt_err_t rt_spi_flash_init(rt_device_t dev)
  113. {
  114. return RT_EOK;
  115. }
  116. static rt_err_t rt_spi_flash_open(rt_device_t dev, rt_uint16_t oflag)
  117. {
  118. return RT_EOK;
  119. }
  120. static rt_err_t rt_spi_flash_close(rt_device_t dev)
  121. {
  122. return RT_EOK;
  123. }
  124. static rt_err_t rt_spi_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  125. {
  126. return RT_EOK;
  127. }
  128. static rt_size_t rt_spi_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  129. {
  130. rt_uint8_t *ptr;
  131. rt_uint32_t index, nr;
  132. nr = size/512;
  133. ptr = (rt_uint8_t*)buffer;
  134. for (index = 0; index < nr; index ++)
  135. {
  136. /* only supply single block read: block size 512Byte */
  137. read_page((pos + index * 512)/512, &ptr[index * 512]);
  138. }
  139. return nr * 512;
  140. }
  141. static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  142. {
  143. rt_uint8_t *ptr;
  144. rt_uint32_t index, nr;
  145. nr = size / 512;
  146. ptr = (rt_uint8_t*)buffer;
  147. for (index = 0; index < nr; index ++)
  148. {
  149. /* only supply single block write: block size 512Byte */
  150. write_page((pos + index * 512)/512, &ptr[index * 512]);
  151. }
  152. return nr * 512;
  153. }
  154. void rt_hw_spi_flash_init(void)
  155. {
  156. GPIO_Configuration();
  157. /* register spi_flash device */
  158. spi_flash_device.type = RT_Device_Class_Block;
  159. spi_flash_device.init = rt_spi_flash_init;
  160. spi_flash_device.open = rt_spi_flash_open;
  161. spi_flash_device.close = rt_spi_flash_close;
  162. spi_flash_device.read = rt_spi_flash_read;
  163. spi_flash_device.write = rt_spi_flash_write;
  164. spi_flash_device.control = rt_spi_flash_control;
  165. /* no private */
  166. spi_flash_device.private = RT_NULL;
  167. rt_device_register(&spi_flash_device, "spi0",
  168. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  169. }