Guest User

spiflash_sw

a guest
Oct 15th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. diff --git a/software/main.c b/software/main.c
  2. index 005a883..09208b9 100644
  3. --- a/software/main.c
  4. +++ b/software/main.c
  5. @@ -7,6 +7,130 @@
  6.  #include <console.h>
  7.  #include <generated/csr.h>
  8.  
  9. +#define PAGE_PROGRAM_CMD   (0x02)
  10. +#define WRDI_CMD       (0x04)
  11. +#define RDSR_CMD       (0x05)
  12. +#define WREN_CMD       (0x06)
  13. +#define SE_CMD         (0x20)
  14. +
  15. +#define BITBANG_CLK        (1 << 2)
  16. +#define BITBANG_CS_N       (1 << 3)
  17. +#define BITBANG_DQ_INPUT   (1 << 4)
  18. +#define BITBANG_ENABLE     (1 << 5)
  19. +
  20. +#define SR_WIP         (1)
  21. +
  22. +static inline void _spiflash_bitbang_write(unsigned int val);
  23. +void flash_write_byte(unsigned char b);
  24. +void flash_write_addr(unsigned int addr);
  25. +void write_to_flash(unsigned int addr, char c);
  26. +void wait_for_device_ready(void);
  27. +
  28. +static inline void _spiflash_bitbang_write(unsigned int val)
  29. +{
  30. +   spiflash_bitbang_write(val | BITBANG_ENABLE);
  31. +}
  32. +
  33. +void flash_write_byte(unsigned char cmd)
  34. +{
  35. +   int i;
  36. +   _spiflash_bitbang_write(0); // ~CS_N ~CLK
  37. +
  38. +   for (i = 0 ; i < 8 ; i++, cmd <<= 1)
  39. +   {
  40. +      
  41. +       _spiflash_bitbang_write((cmd & 0x80) >> 7);
  42. +       _spiflash_bitbang_write(((cmd & 0x80) >> 7) | BITBANG_CLK);
  43. +   }
  44. +
  45. +   _spiflash_bitbang_write(0); // ~CS_N ~CLK
  46. +
  47. +}
  48. +
  49. +void flash_write_addr(unsigned int addr)
  50. +{
  51. +   int i;
  52. +   _spiflash_bitbang_write(0);
  53. +
  54. +   for (i = 0 ; i < 24 ; i++, addr <<= 1)
  55. +   {
  56. +       _spiflash_bitbang_write((addr & 0x800000) >> 23);
  57. +       _spiflash_bitbang_write(((addr & 0x800000) >> 23) | BITBANG_CLK);
  58. +   }
  59. +
  60. +   _spiflash_bitbang_write(0);
  61. +}
  62. +
  63. +void wait_for_device_ready(void)
  64. +{
  65. +   unsigned char sr;
  66. +   unsigned char i;
  67. +   do
  68. +   {
  69. +       sr = 0;
  70. +       flash_write_byte(RDSR_CMD);
  71. +       _spiflash_bitbang_write(BITBANG_DQ_INPUT);
  72. +       for (i = 0 ; i < 8 ; i++)
  73. +       {
  74. +           sr <<= 1;
  75. +           _spiflash_bitbang_write(BITBANG_CLK | BITBANG_DQ_INPUT);
  76. +           sr |= spiflash_miso_read();
  77. +           _spiflash_bitbang_write(0           | BITBANG_DQ_INPUT);
  78. +       }
  79. +       _spiflash_bitbang_write(0);
  80. +       _spiflash_bitbang_write(BITBANG_CS_N);
  81. +       printf("sr == %02x\n", sr);
  82. +   } while (sr & SR_WIP);
  83. +}
  84. +
  85. +void erase_flash(unsigned int addr)
  86. +{
  87. +   unsigned int sector_addr = addr & ~(0xfff);
  88. +   flash_write_byte(WREN_CMD);
  89. +   _spiflash_bitbang_write(BITBANG_CS_N);
  90. +  
  91. +   flash_write_byte(SE_CMD);
  92. +   flash_write_addr(sector_addr);
  93. +   _spiflash_bitbang_write(BITBANG_CS_N);
  94. +}
  95. +
  96. +void write_to_flash(unsigned int addr, char c)
  97. +{
  98. +   unsigned int i;
  99. +   flash_write_byte(WREN_CMD);
  100. +   _spiflash_bitbang_write(BITBANG_CS_N);
  101. +   flash_write_byte(PAGE_PROGRAM_CMD);
  102. +   flash_write_addr(addr);
  103. +   for (i = 0 ; i < 256 ; i++)
  104. +       flash_write_byte(c);
  105. +
  106. +   _spiflash_bitbang_write(BITBANG_CS_N);
  107. +   spiflash_bitbang_write(0);
  108. +}
  109. +
  110. +void erase_then_write_to_flash(unsigned int addr, char c)
  111. +{
  112. +   unsigned int sector_addr = addr & ~(0xfff);
  113. +
  114. +   flash_write_byte(WREN_CMD);
  115. +   _spiflash_bitbang_write(BITBANG_CS_N);
  116. +  
  117. +   flash_write_byte(SE_CMD);
  118. +   flash_write_addr(sector_addr);
  119. +   _spiflash_bitbang_write(BITBANG_CS_N);
  120. +
  121. +   wait_for_device_ready();
  122. +
  123. +   flash_write_byte(WREN_CMD);
  124. +   _spiflash_bitbang_write(BITBANG_CS_N);
  125. +   flash_write_byte(PAGE_PROGRAM_CMD);
  126. +   flash_write_addr(addr);
  127. +   flash_write_byte(c);
  128. +
  129. +   _spiflash_bitbang_write(BITBANG_CS_N);
  130. +   spiflash_bitbang_write(0);
  131. +}
  132. +
  133.  int main(void)
  134.  {
  135.     int c;
  136. @@ -19,6 +143,12 @@ int main(void)
  137.    
  138.     while(1) {
  139.         c = readchar();
  140. +       if (c == '0')
  141. +           erase_flash(0x75000);
  142. +       if (c == '1')
  143. +           write_to_flash(0x75000, '@');
  144. +       if (c == '2')
  145. +           erase_then_write_to_flash(0x75000, '%');
  146.         if((c >= '0') && (c <= '9')) {
  147.             printf("Set blink frequency #%c\n", c);
  148.             c -= '0';
Advertisement
Add Comment
Please, Sign In to add comment