dataflash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software GmbH
  3. * Copyright (C) 2009 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Id: dataflash.c 4115 2012-04-12 21:06:13Z olereinhardt $
  37. *
  38. * WARNING! Do not use any part of Basemon for your own applications. WARNING!
  39. *
  40. * This is not a typical application sample. It overrides parts of Nut/OS to
  41. * keep it running on broken hardware.
  42. */
  43. #include <stdint.h>
  44. #include "dataflash.h"
  45. #if defined (__AVR__)
  46. /*!
  47. * \brief Exchange SPI byte.
  48. *
  49. * Inlined?
  50. */
  51. static uint8_t SpiByte(uint8_t c)
  52. {
  53. outb(SPDR, c);
  54. while ((inb(SPSR) & 0x80) == 0);
  55. return inb(SPDR);
  56. }
  57. /*!
  58. * \brief Init SPI interface.
  59. *
  60. * Should be more general (Dataflash, VS1001 etc.)
  61. * Dataflash requires SPI mode 3 (enable flash when SCK is high)
  62. *
  63. * - PB1(SCK) -> Mem-SCK
  64. * - PB2(MOSI) -> Mem-SI
  65. * - PB3(MISO) <- Mem-SO
  66. * - PB4 -> Mem-CS (through inverter)
  67. *
  68. */
  69. static void SpiInit(void)
  70. {
  71. /*
  72. * Init SS pin. When configured as input, we will lose master
  73. * mode, if this pin goes low. Thus we switch on the pull-up.
  74. */
  75. if(bit_is_clear(DDRB, 0)) {
  76. sbi(PORTB, 0);
  77. }
  78. /* Set SCK output. */
  79. sbi(DDRB, 1);
  80. /* Set MOSI output. */
  81. sbi(DDRB, 2);
  82. /* Enable MISO pullup. */
  83. sbi(PORTB, 3);
  84. #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega2561__)
  85. /* Set double speed. */
  86. outb(SPSR, _BV(SPI2X));
  87. #endif
  88. /* Enable SPI master mode 3, fosc/2. */
  89. outb(SPCR, _BV(SPE) | _BV(MSTR) | _BV(CPHA) | _BV(CPOL));
  90. /* Clean-up the status. */
  91. outb(SPSR, inb(SPSR));
  92. inb(SPDR);
  93. }
  94. /*!
  95. * \brief Read memory chip status.
  96. */
  97. static uint8_t SpiMemStatus(uint8_t cs)
  98. {
  99. uint8_t rc;
  100. if (cs == SPIMEM_CS_BIT) {
  101. sbi(SPIMEM_CS_PORT, cs);
  102. } else {
  103. cbi(SPIMEM_CS_PORT, cs);
  104. }
  105. SpiByte(0x57);
  106. rc = SpiByte(0);
  107. if (cs == SPIMEM_CS_BIT) {
  108. cbi(SPIMEM_CS_PORT, cs);
  109. } else {
  110. sbi(SPIMEM_CS_PORT, cs);
  111. }
  112. return rc;
  113. }
  114. static int SpiMemInit(uint8_t cs, uint16_t *pages, uint16_t *pagesize)
  115. {
  116. uint8_t fs;
  117. /* Init SPI memory chip select. */
  118. if (cs == SPIMEM_CS_BIT) {
  119. cbi(SPIMEM_CS_PORT, cs);
  120. } else {
  121. sbi(SPIMEM_CS_PORT, cs);
  122. }
  123. sbi(SPIMEM_CS_DDR, cs);
  124. /* Initialize the SPI interface. */
  125. SpiInit();
  126. /* Read the status register for a rudimentary check. */
  127. fs = SpiMemStatus(cs);
  128. if(fs & 0x80) {
  129. fs = (fs >> 2) & 0x0F;
  130. *pagesize = 264;
  131. if(fs == 3) {
  132. *pages = 512;
  133. return 0;
  134. }
  135. else if(fs == 5) {
  136. *pages = 1024;
  137. return 0;
  138. }
  139. else if(fs == 7) {
  140. *pages = 2048;
  141. return 0;
  142. }
  143. else if(fs == 13) {
  144. *pagesize = 528;
  145. *pages = 8192;
  146. return 0;
  147. }
  148. }
  149. return -1;
  150. }
  151. #endif /* __AVR__ */
  152. /*
  153. * \return Number of bytes.
  154. */
  155. long SpiMemTest(void)
  156. {
  157. uint16_t pages = 0;
  158. uint16_t pagesize = 0;
  159. #if defined (__AVR__)
  160. if (SpiMemInit(SPIMEM_CS_BIT, &pages, &pagesize)) {
  161. SpiMemInit(SPIMEM_CS_BIT_ALT, &pages, &pagesize);
  162. }
  163. /* Disable SPI */
  164. outb(SPCR, 0);
  165. #endif /* __AVR__ */
  166. return (long)pages * (long)pagesize;
  167. }