twi_master.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef TWI_MASTER_H
  41. #define TWI_MASTER_H
  42. /*lint ++flb "Enter library region" */
  43. #include <stdbool.h>
  44. #include <stdint.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /** @file
  49. * @brief Software controlled TWI Master driver.
  50. *
  51. *
  52. * @defgroup lib_driver_twi_master Software controlled TWI Master driver
  53. * @{
  54. * @ingroup nrf_twi
  55. * @brief Software controlled TWI Master driver (deprecated).
  56. *
  57. * @warning This module is deprecated.
  58. *
  59. * Supported features:
  60. * - Repeated start
  61. * - No multi-master
  62. * - Only 7-bit addressing
  63. * - Supports clock stretching (with optional SMBus style slave timeout)
  64. * - Tries to handle slaves stuck in the middle of transfer
  65. */
  66. #define TWI_READ_BIT (0x01) //!< If this bit is set in the address field, transfer direction is from slave to master.
  67. #define TWI_ISSUE_STOP ((bool)true) //!< Parameter for @ref twi_master_transfer
  68. #define TWI_DONT_ISSUE_STOP ((bool)false) //!< Parameter for @ref twi_master_transfer
  69. /* These macros are needed to see if the slave is stuck and we as master send dummy clock cycles to end its wait */
  70. /*lint -e717 -save "Suppress do {} while (0) for these macros" */
  71. /*lint ++flb "Enter library region" */
  72. #define TWI_SCL_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while (0) /*!< Pulls SCL line high */
  73. #define TWI_SCL_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while (0) /*!< Pulls SCL line low */
  74. #define TWI_SDA_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Pulls SDA line high */
  75. #define TWI_SDA_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Pulls SDA line low */
  76. #define TWI_SDA_INPUT() do { NRF_GPIO->DIRCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Configures SDA pin as input */
  77. #define TWI_SDA_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while (0) /*!< Configures SDA pin as output */
  78. #define TWI_SCL_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while (0) /*!< Configures SCL pin as output */
  79. /*lint -restore */
  80. #define TWI_SDA_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_DATA_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SDA */
  81. #define TWI_SCL_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SCL */
  82. #define TWI_DELAY() nrf_delay_us(4) /*!< 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. */
  83. /**
  84. * @brief Function for initializing TWI bus IO pins and checks if the bus is operational.
  85. *
  86. * Both pins are configured as Standard-0, No-drive-1 (open drain).
  87. *
  88. * @return
  89. * @retval true TWI bus is clear for transfers.
  90. * @retval false TWI bus is stuck.
  91. */
  92. bool twi_master_init(void);
  93. /**
  94. * @brief Function for transferring data over TWI bus.
  95. *
  96. * If TWI master detects even one NACK from the slave or timeout occurs, STOP condition is issued
  97. * and the function returns false.
  98. * Bit 0 (@ref TWI_READ_BIT) in the address parameter controls transfer direction;
  99. * - If 1, master reads data_length number of bytes from the slave
  100. * - If 0, master writes data_length number of bytes to the slave.
  101. *
  102. * @note Make sure at least data_length number of bytes is allocated in data if TWI_READ_BIT is set.
  103. * @note @ref TWI_ISSUE_STOP
  104. *
  105. * @param address Data transfer direction (LSB) / Slave address (7 MSBs).
  106. * @param data Pointer to data.
  107. * @param data_length Number of bytes to transfer.
  108. * @param issue_stop_condition If @ref TWI_ISSUE_STOP, STOP condition is issued before exiting function. If @ref TWI_DONT_ISSUE_STOP, STOP condition is not issued before exiting function. If transfer failed for any reason, STOP condition will be issued in any case.
  109. * @return
  110. * @retval true Data transfer succeeded without errors.
  111. * @retval false Data transfer failed.
  112. */
  113. bool twi_master_transfer(uint8_t address, uint8_t *data, uint8_t data_length, bool issue_stop_condition);
  114. /**
  115. *@}
  116. **/
  117. /*lint --flb "Leave library region" */
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif //TWI_MASTER_H