spi_flash.c 6.0 KB

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