spi_flash.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <stm32f10x.h>
  2. //#include "spi_flash.h"
  3. #include <string.h>
  4. extern unsigned char SPI_WriteByte(unsigned char data);
  5. /********************** hardware *************************************/
  6. /* SPI_FLASH_CS PA4 */
  7. /* SPI_FLASH_RST PA3 */
  8. #define FLASH_RST_0() GPIO_ResetBits(GPIOA,GPIO_Pin_3)
  9. #define FLASH_RST_1() GPIO_SetBits(GPIOA,GPIO_Pin_3)
  10. #define FLASH_CS_0() GPIO_ResetBits(GPIOA,GPIO_Pin_4)
  11. #define FLASH_CS_1() GPIO_SetBits(GPIOA,GPIO_Pin_4)
  12. /********************** hardware *************************************/
  13. static void GPIO_Configuration(void)
  14. {
  15. GPIO_InitTypeDef GPIO_InitStructure;
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  17. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
  18. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  19. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  20. GPIO_Init(GPIOA,&GPIO_InitStructure);
  21. FLASH_RST_0(); // RESET
  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 | 0 | 1 | 1 | 1 | X | X | */
  38. /* ----------------------------------------------------------------------- */
  39. /* bit7 - 忙标记,0为忙1为不忙。 */
  40. /* 当Status Register的位0移出之后,接下来的时钟脉冲序列将使SPI器件继续*/
  41. /* 将最新的状态字节送出。 */
  42. /* bit6 - 标记最近一次Main Memory Page和Buffer的比较结果,0相同,1不同。 */
  43. /* bit5 */
  44. /* bit4 */
  45. /* bit3 */
  46. /* bit2 - 这4位用来标记器件密度,对于AT45DB041B,这4位应该是0111,一共能标记 */
  47. /* 16种不同密度的器件。 */
  48. /* bit1 */
  49. /* bit0 - 这2位暂时无效 */
  50. /******************************************************************************/
  51. static unsigned char AT45DB_StatusRegisterRead(void)
  52. {
  53. unsigned char i;
  54. FLASH_CS_0();
  55. SPI_HostWriteByte(0xd7);
  56. i=SPI_HostReadByte();
  57. FLASH_CS_1();
  58. return i;
  59. }
  60. static void wait_busy(void)
  61. {
  62. unsigned int i=0;
  63. while (i++<2000)
  64. {
  65. if (AT45DB_StatusRegisterRead()&0x80)
  66. {
  67. break;
  68. }
  69. }
  70. }
  71. static void read_page(unsigned int page,unsigned char * pHeader)
  72. {
  73. unsigned int i=0;
  74. wait_busy();
  75. FLASH_CS_0();
  76. SPI_HostWriteByte(0x53);
  77. SPI_HostWriteByte((unsigned char)(page >> 6));
  78. SPI_HostWriteByte((unsigned char)(page << 2));
  79. SPI_HostWriteByte(0x00);
  80. FLASH_CS_1();
  81. wait_busy();
  82. FLASH_CS_0();
  83. SPI_HostWriteByte(0xD4);
  84. SPI_HostWriteByte(0x00);
  85. SPI_HostWriteByte(0x00);
  86. SPI_HostWriteByte(0x00);
  87. SPI_HostWriteByte(0x00);
  88. for (i=0; i<512; i++)
  89. {
  90. *pHeader++ = SPI_HostReadByte();
  91. }
  92. FLASH_CS_1();
  93. }
  94. static void write_page(unsigned int page,unsigned char * pHeader)
  95. {
  96. unsigned int i;
  97. wait_busy();
  98. FLASH_CS_0();
  99. SPI_HostWriteByte(0x87);
  100. SPI_HostWriteByte(0);
  101. SPI_HostWriteByte(0);
  102. SPI_HostWriteByte(0);
  103. for(i=0; i<512; i++)
  104. {
  105. SPI_HostWriteByte(*pHeader++);
  106. }
  107. FLASH_CS_1();
  108. wait_busy();
  109. FLASH_CS_0();
  110. SPI_HostWriteByte(0x86);
  111. SPI_HostWriteByte((unsigned char)(page>>6));
  112. SPI_HostWriteByte((unsigned char)(page<<2));
  113. SPI_HostWriteByte(0x00);
  114. FLASH_CS_1();
  115. }
  116. #include <rtthread.h>
  117. /* SPI DEVICE */
  118. static struct rt_device spi_flash_device;
  119. /* RT-Thread Device Driver Interface */
  120. static rt_err_t rt_spi_flash_init(rt_device_t dev)
  121. {
  122. return RT_EOK;
  123. }
  124. static rt_err_t rt_spi_flash_open(rt_device_t dev, rt_uint16_t oflag)
  125. {
  126. return RT_EOK;
  127. }
  128. static rt_err_t rt_spi_flash_close(rt_device_t dev)
  129. {
  130. return RT_EOK;
  131. }
  132. static rt_err_t rt_spi_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  133. {
  134. return RT_EOK;
  135. }
  136. static rt_size_t rt_spi_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  137. {
  138. /* only supply single block read: block size 512Byte */
  139. read_page(pos/512,buffer);
  140. return RT_EOK;
  141. }
  142. static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  143. {
  144. /* only supply single block write: block size 512Byte */
  145. write_page(pos/512,(unsigned char*)buffer);
  146. return RT_EOK;
  147. }
  148. void rt_hw_spi_flash_init(void)
  149. {
  150. GPIO_Configuration();
  151. /* register spi_flash device */
  152. spi_flash_device.init = rt_spi_flash_init;
  153. spi_flash_device.open = rt_spi_flash_open;
  154. spi_flash_device.close = rt_spi_flash_close;
  155. spi_flash_device.read = rt_spi_flash_read;
  156. spi_flash_device.write = rt_spi_flash_write;
  157. spi_flash_device.control = rt_spi_flash_control;
  158. /* no private */
  159. spi_flash_device.private = RT_NULL;
  160. rt_device_register(&spi_flash_device, "spi0",
  161. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  162. }