sdio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright (c) 2009 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdint.h>
  41. #include "nrf.h"
  42. #include "nrf_delay.h"
  43. #include "sdio.h"
  44. #include "nrf_gpio.h"
  45. #include "sdio_config.h"
  46. /*lint ++flb "Enter library region" */
  47. /*lint -e717 -save "Suppress do {} while (0) for these macros" */
  48. #define SDIO_CLOCK_HIGH() do { NRF_GPIO->OUTSET = (1UL << SDIO_CONFIG_CLOCK_PIN_NUMBER); } while (0) /*!< Pulls SCL line high */
  49. #define SDIO_CLOCK_LOW() do { NRF_GPIO->OUTCLR = (1UL << SDIO_CONFIG_CLOCK_PIN_NUMBER); } while (0) /*!< Pulls SCL line low */
  50. #define SDIO_DATA_HIGH() do { NRF_GPIO->OUTSET = (1UL << SDIO_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Pulls SDA line high */
  51. #define SDIO_DATA_LOW() do { NRF_GPIO->OUTCLR = (1UL << SDIO_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Pulls SDA line low */
  52. #define SDIO_DATA_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << SDIO_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Configures SDA pin as output */
  53. #define SDIO_CLOCK_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << SDIO_CONFIG_CLOCK_PIN_NUMBER); } while (0) /*!< Configures SCL pin as output */
  54. /*lint -restore */
  55. /*lint -emacro(845,SDIO_DATA_INPUT) // A zero has been given as right argument to operator '|'" */
  56. #define SDIO_DATA_INPUT() do { \
  57. nrf_gpio_cfg_input(25, NRF_GPIO_PIN_NOPULL); \
  58. } while (0)
  59. #define SDIO_DATA_READ() ((NRF_GPIO->IN >> SDIO_CONFIG_DATA_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SDA */
  60. #define SDIO_CLOCK_READ() ((NRF_GPIO->IN >> SDIO_CONFIG_CLOCK_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SCL */
  61. #define SDIO_DELAY() nrf_delay_us(10) /*!< Time to wait when pin states are changed. For fast-mode the delay can be zero and for standard-mode 4 us delay is sufficient. */
  62. void sdio_init(void)
  63. {
  64. SDIO_CLOCK_HIGH();
  65. SDIO_DATA_HIGH();
  66. SDIO_CLOCK_OUTPUT();
  67. SDIO_DATA_INPUT();
  68. // If slave is stuck in the middle of transfer, clock out bits until the slave ACKs the transfer
  69. for (uint_fast8_t i = 16; i--;)
  70. {
  71. SDIO_DELAY();
  72. SDIO_CLOCK_LOW();
  73. SDIO_DELAY();
  74. SDIO_CLOCK_HIGH();
  75. SDIO_DELAY();
  76. if (SDIO_DATA_READ())
  77. {
  78. break;
  79. }
  80. }
  81. for (uint_fast8_t i = 5; i--;)
  82. {
  83. SDIO_DELAY();
  84. SDIO_CLOCK_LOW();
  85. SDIO_DELAY();
  86. SDIO_CLOCK_HIGH();
  87. }
  88. SDIO_DATA_OUTPUT();
  89. SDIO_DATA_HIGH();
  90. SDIO_DELAY();
  91. }
  92. uint8_t sdio_read_byte(uint8_t address)
  93. {
  94. uint8_t data_byte = 0;
  95. SDIO_DATA_OUTPUT();
  96. for (uint_fast8_t i = 8; i--;)
  97. {
  98. SDIO_DELAY();
  99. SDIO_CLOCK_LOW();
  100. if (address & (1U << i))
  101. {
  102. SDIO_DATA_HIGH();
  103. }
  104. else
  105. {
  106. SDIO_DATA_LOW();
  107. }
  108. SDIO_DELAY();
  109. SDIO_CLOCK_HIGH();
  110. }
  111. nrf_delay_us(20);
  112. SDIO_DATA_INPUT();
  113. for (uint_fast8_t i = 8; i--;)
  114. {
  115. SDIO_CLOCK_LOW();
  116. SDIO_DELAY();
  117. SDIO_CLOCK_HIGH();
  118. SDIO_DELAY();
  119. data_byte |= (uint8_t)(SDIO_DATA_READ() << i);
  120. }
  121. SDIO_DATA_HIGH();
  122. SDIO_DATA_OUTPUT();
  123. SDIO_DELAY();
  124. return data_byte;
  125. }
  126. void sdio_read_burst(uint8_t * target_buffer, uint8_t target_buffer_size)
  127. {
  128. uint_fast8_t address = 0x63;
  129. SDIO_DATA_OUTPUT();
  130. for (uint_fast8_t bit_index=8; bit_index--;)
  131. {
  132. SDIO_CLOCK_LOW();
  133. if (address & (1U << bit_index))
  134. {
  135. SDIO_DATA_HIGH();
  136. }
  137. else
  138. {
  139. SDIO_DATA_LOW();
  140. }
  141. SDIO_CLOCK_HIGH();
  142. }
  143. SDIO_DATA_INPUT();
  144. for (uint_fast8_t target_buffer_index = 0; target_buffer_index < target_buffer_size; target_buffer_index++)
  145. {
  146. target_buffer[target_buffer_index] = 0;
  147. for (uint_fast8_t bit_index = 8; bit_index--;)
  148. {
  149. SDIO_CLOCK_LOW();
  150. SDIO_CLOCK_HIGH();
  151. target_buffer[target_buffer_index] |= (uint8_t)(SDIO_DATA_READ() << bit_index);
  152. }
  153. }
  154. }
  155. void sdio_write_byte(uint8_t address, uint8_t data_byte)
  156. {
  157. // Add write indication bit
  158. address |= 0x80;
  159. SDIO_DATA_OUTPUT();
  160. for (uint_fast8_t i = 8; i--;)
  161. {
  162. SDIO_DELAY();
  163. SDIO_CLOCK_LOW();
  164. if (address & (1U << i))
  165. {
  166. SDIO_DATA_HIGH();
  167. }
  168. else
  169. {
  170. SDIO_DATA_LOW();
  171. }
  172. SDIO_DELAY();
  173. SDIO_CLOCK_HIGH();
  174. }
  175. SDIO_DELAY();
  176. for (uint_fast8_t i = 8; i--;)
  177. {
  178. SDIO_CLOCK_LOW();
  179. if (data_byte & (1U << i))
  180. {
  181. SDIO_DATA_HIGH();
  182. }
  183. else
  184. {
  185. SDIO_DATA_LOW();
  186. }
  187. SDIO_DELAY();
  188. SDIO_CLOCK_HIGH();
  189. SDIO_DELAY();
  190. }
  191. SDIO_DATA_HIGH();
  192. SDIO_DELAY();
  193. }
  194. /*lint --flb "Leave library region" */