spi_flash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <stm32f10x.h>
  2. #include "spi_flash.h"
  3. #include "rtthread.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_CS_1();
  23. FLASH_RST_1();
  24. }
  25. static unsigned char SPI_HostReadByte(void)
  26. {
  27. //return SPI_WriteByte(0x00);
  28. //Wait until the transmit buffer is empty
  29. //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  30. while( (SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);
  31. // Send the byte
  32. SPI_I2S_SendData(SPI1, 0);
  33. //Wait until a data is received
  34. //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  35. while( (SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);
  36. // Get the received data
  37. return SPI_I2S_ReceiveData(SPI1);
  38. }
  39. static void SPI_HostWriteByte(unsigned char wByte)
  40. {
  41. SPI_WriteByte(wByte);
  42. }
  43. /*****************************************************************************/
  44. /*Status Register Format: */
  45. /* ------------------------------------------------------------------------- */
  46. /* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
  47. /* |--------|--------|--------|--------|--------|--------|--------|--------| */
  48. /* |RDY/BUSY| COMP | device density | X | X | */
  49. /* ------------------------------------------------------------------------- */
  50. /* 0:busy | | AT45DB041:0111 | protect|page size */
  51. /* 1:ready | | AT45DB161:1011 | */
  52. /* --------------------------------------------------------------------------*/
  53. /*****************************************************************************/
  54. static unsigned char AT45DB_StatusRegisterRead(void)
  55. {
  56. unsigned char i;
  57. FLASH_CS_0();
  58. SPI_HostWriteByte(AT45DB_READ_STATE_REGISTER);
  59. i=SPI_HostReadByte();
  60. FLASH_CS_1();
  61. return i;
  62. }
  63. static void wait_busy(void)
  64. {
  65. unsigned int i=0;
  66. while (i++<3000)
  67. {
  68. if (AT45DB_StatusRegisterRead()&0x80)
  69. {
  70. break;
  71. }
  72. }
  73. if( !(i<3000) )
  74. {
  75. rt_kprintf("\r\nSPI_FLASH timeout!!!");
  76. }
  77. }
  78. static void read_page(unsigned int page,unsigned char * pHeader)
  79. {
  80. unsigned int i=0;
  81. wait_busy();
  82. FLASH_CS_0();
  83. SPI_HostWriteByte(AT45DB_MM_PAGE_TO_B1_XFER);
  84. SPI_HostWriteByte((unsigned char)(page >> 6));
  85. SPI_HostWriteByte((unsigned char)(page << 2));
  86. SPI_HostWriteByte(0x00);
  87. FLASH_CS_1();
  88. wait_busy();
  89. FLASH_CS_0();
  90. SPI_HostWriteByte(AT45DB_BUFFER_1_READ);
  91. SPI_HostWriteByte(0x00);
  92. SPI_HostWriteByte(0x00);
  93. SPI_HostWriteByte(0x00);
  94. SPI_HostWriteByte(0x00);
  95. for (i=0; i<512; i++)
  96. {
  97. *pHeader++ = SPI_HostReadByte();
  98. }
  99. FLASH_CS_1();
  100. }
  101. static void write_page(unsigned int page,unsigned char * pHeader)
  102. {
  103. unsigned int i;
  104. wait_busy();
  105. FLASH_CS_0();
  106. SPI_HostWriteByte(AT45DB_BUFFER_2_WRITE);
  107. SPI_HostWriteByte(0);
  108. SPI_HostWriteByte(0);
  109. SPI_HostWriteByte(0);
  110. for(i=0; i<512; i++)
  111. {
  112. SPI_HostWriteByte(*pHeader++);
  113. }
  114. FLASH_CS_1();
  115. wait_busy();
  116. FLASH_CS_0();
  117. SPI_HostWriteByte(AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE);
  118. SPI_HostWriteByte((unsigned char)(page>>6));
  119. SPI_HostWriteByte((unsigned char)(page<<2));
  120. SPI_HostWriteByte(0x00);
  121. FLASH_CS_1();
  122. }
  123. #include <rtthread.h>
  124. /* SPI DEVICE */
  125. static struct rt_device spi_flash_device;
  126. /* RT-Thread Device Driver Interface */
  127. static rt_err_t rt_spi_flash_init(rt_device_t dev)
  128. {
  129. return RT_EOK;
  130. }
  131. static rt_err_t rt_spi_flash_open(rt_device_t dev, rt_uint16_t oflag)
  132. {
  133. return RT_EOK;
  134. }
  135. static rt_err_t rt_spi_flash_close(rt_device_t dev)
  136. {
  137. return RT_EOK;
  138. }
  139. static rt_err_t rt_spi_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  140. {
  141. return RT_EOK;
  142. }
  143. static rt_size_t rt_spi_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  144. {
  145. rt_uint8_t *ptr;
  146. rt_uint32_t index, nr;
  147. nr = size/512;
  148. ptr = (rt_uint8_t*)buffer;
  149. for (index = 0; index < nr; index ++)
  150. {
  151. /* only supply single block read: block size 512Byte */
  152. read_page((pos + index * 512)/512, &ptr[index * 512]);
  153. }
  154. return nr * 512;
  155. }
  156. static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  157. {
  158. rt_uint8_t *ptr;
  159. rt_uint32_t index, nr;
  160. nr = size / 512;
  161. ptr = (rt_uint8_t*)buffer;
  162. for (index = 0; index < nr; index ++)
  163. {
  164. /* only supply single block write: block size 512Byte */
  165. write_page((pos + index * 512)/512, &ptr[index * 512]);
  166. }
  167. return nr * 512;
  168. }
  169. void rt_hw_spi_flash_init(void)
  170. {
  171. GPIO_Configuration();
  172. /* register spi_flash device */
  173. spi_flash_device.type = RT_Device_Class_Block;
  174. spi_flash_device.init = rt_spi_flash_init;
  175. spi_flash_device.open = rt_spi_flash_open;
  176. spi_flash_device.close = rt_spi_flash_close;
  177. spi_flash_device.read = rt_spi_flash_read;
  178. spi_flash_device.write = rt_spi_flash_write;
  179. spi_flash_device.control = rt_spi_flash_control;
  180. /* no private */
  181. spi_flash_device.private = RT_NULL;
  182. rt_device_register(&spi_flash_device, "spi0",
  183. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  184. }