spi_mmc_gpio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2011 by Thermotemp 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. * \brief Low Level Multimedia Card Support.
  36. *
  37. * MMC support routines for gpio usage
  38. *
  39. * \verbatim
  40. *
  41. * $Id$
  42. *
  43. * \endverbatim
  44. */
  45. #include <cfg/clock.h>
  46. #include <cfg/mmci.h>
  47. #include <dev/mmcard.h>
  48. #include <dev/spibus.h>
  49. #include <dev/gpio.h>
  50. #ifdef MMC_CD_PORT
  51. #if (MMC_CD_PORT==NUTGPIO_PORTA)
  52. #define MMC_CD_SIGNAL sig_GPIO1
  53. #elif (MMC_CD_PORT==NUTGPIO_PORTB)
  54. #define MMC_CD_SIGNAL sig_GPIO2
  55. #elif (MMC_CD_PORT==NUTGPIO_PORTC)
  56. #define MMC_CD_SIGNAL sig_GPIO3
  57. #endif
  58. #endif
  59. #if !defined(SPI_MMC_CLK)
  60. #define SPI_MMC_CLK 33000000
  61. #endif
  62. #if !defined(SPI_MMC_CS)
  63. #define SPI_MMC_CS 1
  64. #endif
  65. /*!
  66. * \addtogroup xgSPIBusMmcSupport
  67. */
  68. /*@{*/
  69. static int SpiMmcGpioReset(NUTDEVICE * dev);
  70. static void SpiMmcGpioLed(int mode);
  71. static int SpiMmcGpioPower(int mode);
  72. /*!
  73. * \brief Memory card support structure for MMC with GPIO support for controle lines.
  74. */
  75. static MEMCARDSUPP mcsSpiMmcGpio = {
  76. 0, /*!< \brief Status change flag (mcs_cf). */
  77. 0, /*!< \brief Socket status (mcs_sf). */
  78. SpiMmcGpioReset, /*!< \brief Status reset (mcs_reset). */
  79. SpiMmcGpioLed, /*!< \brief Set activity indicator (mcs_act). */
  80. SpiMmcGpioPower, /*!< \brief Power control (mcs_power). */
  81. };
  82. #if defined(MMC_CD_PORT) && defined(MMC_CD_PIN)
  83. /*!
  84. * \brief Card insertion and removal interrupt routine.
  85. *
  86. * \param arg Pointer to the memory card support structure.
  87. */
  88. static void SpiMmcGpioIrq(void *arg)
  89. {
  90. MEMCARDSUPP *mcs = (MEMCARDSUPP *) arg;
  91. if (GpioPinGet(MMC_CD_PORT, MMC_CD_PIN) == 0) {
  92. /* Card was inserted */
  93. /* Set the change flag. */
  94. mcs->mcs_cf = 1;
  95. /* Set the card detect flag. */
  96. mcs->mcs_sf = NUTMC_SF_CD;
  97. #if defined(MMC_WP_PORT) && defined(MMC_WP_PIN)
  98. if (GpioPinGet(MMC_WP_PORT, MMC_WP_PIN) == 0) {
  99. mcs->mcs_sf |= NUTMC_SF_WP;
  100. }
  101. #endif
  102. } else {
  103. /* Card was removed */
  104. /* Set the change flag. */
  105. mcs->mcs_cf = 1;
  106. /* Clear card detect and write protect flags. */
  107. mcs->mcs_sf = 0;
  108. }
  109. }
  110. #endif
  111. /*!
  112. * \brief Card activity indicator control.
  113. *
  114. * Not implemented.
  115. *
  116. * \return 0 on success, -1 if no card is detected.
  117. */
  118. static void SpiMmcGpioLed(int mode)
  119. {
  120. if (mode == NUTMC_IND_OFF) {
  121. // switch off LED
  122. } else {
  123. // switch on LED
  124. }
  125. }
  126. /*!
  127. * \brief Card 0 power control.
  128. *
  129. * Not implemented.
  130. *
  131. * \param mode If zero, the card will be put into power down mode.
  132. * Otherwise the card will be activated.
  133. *
  134. * \return Always -1.
  135. */
  136. static int SpiMmcGpioPower(int mode)
  137. {
  138. return -1;
  139. }
  140. /*!
  141. * \brief Card socket 0 reset.
  142. *
  143. * \return Always 0.
  144. */
  145. static int SpiMmcGpioReset(NUTDEVICE * dev)
  146. {
  147. NUTSPINODE * node;
  148. NUTSPIBUS * bus;
  149. int rc;
  150. node = (NUTSPINODE *) dev->dev_icb;
  151. bus = (NUTSPIBUS *) node->node_bus;
  152. /*
  153. * 80 bits of ones with deactivated chip select will put the card
  154. * in SPI mode.
  155. */
  156. node->node_mode |= SPI_MODE_CSHIGH;
  157. rc = (bus->bus_alloc) (node, 1000);
  158. if (rc == 0) {
  159. uint8_t txb[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  160. (bus->bus_transfer) (node, &txb, NULL, 10);
  161. }
  162. node->node_mode &= ~SPI_MODE_CSHIGH;
  163. (bus->bus_release) (node);
  164. /* Switch LED off. */
  165. SpiMmcGpioLed(NUTMC_IND_OFF);
  166. return 0;
  167. }
  168. /*!
  169. * \brief Initialize MMC driver for socket 0.
  170. *
  171. * This function is automatically executed during during device
  172. * registration via NutRegisterDevice().
  173. *
  174. * \param dev Identifies the device to initialize.
  175. */
  176. static int SpiMmcGpioInit(NUTDEVICE * dev)
  177. {
  178. int rc;
  179. SpiMmcGpioReset(dev);
  180. #if defined(MMC_WP_PORT) && defined(MMC_WP_PIN)
  181. /* Configure write protect detection pin as input with pullups enabled */
  182. GpioPinConfigSet(MMC_WP_PORT, MMC_WP_PIN, GPIO_CFG_PULLUP);
  183. #endif
  184. #if defined(MMC_CD_PORT) && defined(MMC_CD_PIN)
  185. /* Configure card detect pin as input with pullups enabled */
  186. GpioPinConfigSet(MMC_CD_PORT, MMC_CD_PIN, GPIO_CFG_PULLUP);
  187. /* Register card detection interrupt. */
  188. rc = GpioRegisterIrqHandler(&MMC_CD_SIGNAL, MMC_CD_PIN, SpiMmcGpioIrq, dev->dev_dcb);
  189. if (rc == 0) {
  190. GpioIrqEnable(&MMC_CD_SIGNAL, MMC_CD_PIN);
  191. /* Initialize the SPI interface. */
  192. rc = SpiMmcInit(dev);
  193. }
  194. /* call the IRQ handler routine to check if a card is available just right now */
  195. SpiMmcGpioIrq(dev->dev_dcb);
  196. #else
  197. /* No card detect pin is defined, so we assume that the card is always present */
  198. rc = SpiMmcInit(dev);
  199. /* Set the change flag. */
  200. ((MEMCARDSUPP *)dev->dev_dcb)->mcs_cf = 1;
  201. /* Set the card detect flag. */
  202. ((MEMCARDSUPP *)dev->dev_dcb)->mcs_sf = NUTMC_SF_CD;
  203. #if defined(MMC_WP_PORT) && defined(MMC_WP_PIN)
  204. if (GpioPinGet(MMC_WP_PORT, MMC_WP_PIN) == 0) {
  205. ((MEMCARDSUPP *)dev->dev_dcb)->mcs_sf |= NUTMC_SF_WP
  206. }
  207. #endif
  208. #endif
  209. return rc;
  210. }
  211. /*!
  212. * \brief NPL SPI node implementation structure for MMC.
  213. */
  214. NUTSPINODE nodeSpiMmcGpio = {
  215. NULL, /*!< \brief Pointer to the bus controller driver, node_bus. */
  216. NULL, /*!< \brief Pointer to device driver specific settings, node_stat. */
  217. SPI_MMC_CLK, /*!< \brief Initial clock rate, node_rate. */
  218. SPI_MODE_0, /*!< \brief Initial mode, node_mode. */
  219. 8, /*!< \brief Initial data bits, node_bits. */
  220. SPI_MMC_CS, /*!< \brief Chip select, node_cs. */
  221. &mcsSpiMmcGpio /*!< Driver control block used by the low level part, node_dcb. */
  222. };
  223. /*!
  224. * \brief Multimedia card device information structure.
  225. *
  226. * A pointer to this structure must be passed to NutRegisterDevice()
  227. * to bind this driver to the Nut/OS kernel. An application may then
  228. * call
  229. * /verbatim
  230. * _open("MMC0:", _O_RDWR | _O_BINARY);
  231. * /endverbatim
  232. * to mount the first active primary partition with any previously
  233. * registered file system driver (typically devPhat0).
  234. */
  235. NUTDEVICE devSpiMmcGpio = {
  236. 0, /*!< Pointer to next device, dev_next. */
  237. {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
  238. , /*!< Unique device name, dev_name. */
  239. 0, /*!< Type of device, dev_type. Obsolete. */
  240. 0, /*!< Base address, dev_base. Unused. */
  241. 0, /*!< First interrupt number, dev_irq. Unused. */
  242. &nodeSpiMmcGpio, /*!< Interface control block, dev_icb. */
  243. &mcsSpiMmcGpio, /*!< Driver control block used by the low level part, dev_dcb. */
  244. SpiMmcGpioInit, /*!< Driver initialization routine, dev_init. */
  245. SpiMmcIOCtl, /*!< Driver specific control function, dev_ioctl. */
  246. SpiMmcBlockRead, /*!< Read data from a file, dev_read. */
  247. SpiMmcBlockWrite, /*!< Write data to a file, dev_write. */
  248. #ifdef __HARVARD_ARCH__
  249. SpiMmcBlockWrite_P, /*!< Write data from program space to a file, dev_write_P. */
  250. #endif
  251. SpiMmcMount, /*!< Mount a file system, dev_open. */
  252. SpiMmcUnmount, /*!< Unmount a file system, dev_close. */
  253. NULL, /*!< Return file size, dev_size. */
  254. NULL, /*!< Select function, optional, not yet implemented */
  255. };
  256. /*@}*/