spi_at45d0.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2008-2009 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file dev/spi_at45d0.c
  36. * \brief First AT45D DataFlash device.
  37. *
  38. * \verbatim
  39. * $Id: spi_at45d0.c 5472 2013-12-06 00:16:28Z olereinhardt $
  40. * \endverbatim
  41. */
  42. #include <cfg/memory.h>
  43. #include <dev/blockdev.h>
  44. #include <dev/spi_at45d.h>
  45. #ifndef SPI_RATE_AT45D0
  46. #define SPI_RATE_AT45D0 33000000
  47. #endif
  48. #ifndef SPI_MODE_AT45D0
  49. #ifdef SPI_CSHIGH_AT45D0
  50. #define SPI_MODE_AT45D0 (SPI_MODE_3 | SPI_MODE_CSHIGH)
  51. #else
  52. #define SPI_MODE_AT45D0 SPI_MODE_3
  53. #endif
  54. #elif defined(SPI_CSHIGH_AT45D0)
  55. /* This is a tricky problem. Originally we didn't provide mode settings
  56. ** in the Configurator, but used mode 3 only. After experiencing problems
  57. ** with mode switching on the EIR, we need to set mode 0 for that board,
  58. ** which spoils our chip select polarity setting. */
  59. #if SPI_MODE_AT45D0 == SPI_MODE_0
  60. #undef SPI_MODE_AT45D0
  61. #define SPI_MODE_AT45D0 (SPI_MODE_0 | SPI_MODE_CSHIGH)
  62. #elif SPI_MODE_AT45D0 == SPI_MODE_3
  63. #undef SPI_MODE_AT45D0
  64. #define SPI_MODE_AT45D0 (SPI_MODE_3 | SPI_MODE_CSHIGH)
  65. #endif
  66. #endif /* SPI_MODE_AT45D0 */
  67. #ifndef MOUNT_OFFSET_AT45D0
  68. #define MOUNT_OFFSET_AT45D0 0
  69. #endif
  70. #ifndef MOUNT_TOP_RESERVE_AT45D0
  71. #define MOUNT_TOP_RESERVE_AT45D0 0
  72. #endif
  73. /*!
  74. * \brief AT45D DataFlash SPI node implementation structure.
  75. */
  76. NUTSPINODE nodeSpiAt45d0 = {
  77. NULL, /*!< \brief Pointer to the bus controller driver, node_bus. */
  78. NULL, /*!< \brief Pointer to device driver specific settings, node_stat. */
  79. SPI_RATE_AT45D0, /*!< \brief Initial clock rate, node_rate. */
  80. SPI_MODE_AT45D0, /*!< \brief Initial mode, node_mode. */
  81. 8, /*!< \brief Initial data bits, node_bits. */
  82. 0 /*!< \brief Chip select, node_cs. */
  83. };
  84. /*!
  85. * \brief AT45D DataFlash block I/O implementation structure.
  86. */
  87. static NUTBLOCKIO blkIoAt45d0 = {
  88. NULL, /*!< \brief Device specific parameters, blkio_info. */
  89. 0, /*!< \brief Total number of pages, blkio_blk_cnt. */
  90. 0, /*!< \brief Number of bytes per page, blkio_blk_siz. */
  91. MOUNT_OFFSET_AT45D0, /*!< \brief Number of sectors reserved at bottom, blkio_vol_bot. */
  92. MOUNT_TOP_RESERVE_AT45D0, /*!< \brief Number of sectors reserved at top, blkio_vol_top. */
  93. SpiAt45dPageRead, /*!< \brief Read from node, blkio_read. */
  94. SpiAt45dPageWrite, /*!< \brief Write to node, blkio_write. */
  95. #ifdef __HARVARD_ARCH__
  96. SpiAt45dPageWrite_P, /*!< \brief Write program memory to node, blkio_write_P. */
  97. #endif
  98. SpiAt45dIOCtl /*!< \brief Control functions, blkio_ioctl. */
  99. };
  100. /*!
  101. * \brief AT45D DataFlash device implementation structure.
  102. */
  103. NUTDEVICE devSpiAt45d0 = {
  104. NULL, /*!< \brief Pointer to next device, dev_next. */
  105. {'A', 'T', '4', '5', 'D', '0', 0, 0, 0}, /*!< \brief Unique device name, dev_name. */
  106. IFTYP_BLKIO, /*!< \brief Type of device, dev_type. */
  107. 0, /*!< \brief Base address, dev_base (not used). */
  108. 0, /*!< \brief First interrupt number, dev_irq (not used). */
  109. &nodeSpiAt45d0, /*!< \brief Interface control block, dev_icb. */
  110. &blkIoAt45d0, /*!< \brief Driver control block, dev_dcb. */
  111. SpiAt45dInit, /*!< \brief Driver initialization routine, dev_init. */
  112. NutBlockDeviceIOCtl, /*!< \brief Driver specific control function, dev_ioctl. */
  113. NutBlockDeviceRead, /*!< \brief Read from device, dev_read. */
  114. NutBlockDeviceWrite, /*!< \brief Write to device, dev_write. */
  115. #ifdef __HARVARD_ARCH__
  116. NutBlockDeviceWrite_P, /*!< \brief Write data from program space to device, dev_write_P. */
  117. #endif
  118. NutBlockDeviceOpen, /*!< \brief Mount volume, dev_open. */
  119. NutBlockDeviceClose, /*!< \brief Unmount volume, dev_close. */
  120. NutBlockDeviceSize, /*!< \brief Request file size, dev_size. */
  121. NULL, /*!< \brief Select function, optional, not yet implemented */
  122. };