t
Fork of mbed-dev by
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/twi_master.h@29:d3245924094c, 2015-11-26 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Nov 26 13:15:10 2015 +0000
- Revision:
- 29:d3245924094c
- Parent:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
Synchronized with git revision afe56019e06e2aa8eb37c925549569f186b2bba0
Full URL: https://github.com/mbedmicro/mbed/commit/afe56019e06e2aa8eb37c925549569f186b2bba0/
Fix #1393 - I2C assumes NRF_TWI1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved. |
bogdanm | 0:9b334a45a8ff | 2 | * |
bogdanm | 0:9b334a45a8ff | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
bogdanm | 0:9b334a45a8ff | 4 | * Terms and conditions of usage are described in detail in NORDIC |
bogdanm | 0:9b334a45a8ff | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
bogdanm | 0:9b334a45a8ff | 6 | * |
bogdanm | 0:9b334a45a8ff | 7 | * Licensees are granted free, non-transferable use of the information. NO |
bogdanm | 0:9b334a45a8ff | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
bogdanm | 0:9b334a45a8ff | 9 | * the file. |
bogdanm | 0:9b334a45a8ff | 10 | * |
bogdanm | 0:9b334a45a8ff | 11 | */ |
bogdanm | 0:9b334a45a8ff | 12 | |
bogdanm | 0:9b334a45a8ff | 13 | #ifndef TWI_MASTER_H |
bogdanm | 0:9b334a45a8ff | 14 | #define TWI_MASTER_H |
bogdanm | 0:9b334a45a8ff | 15 | |
bogdanm | 0:9b334a45a8ff | 16 | |
bogdanm | 0:9b334a45a8ff | 17 | #ifdef __cplusplus |
bogdanm | 0:9b334a45a8ff | 18 | extern "C" { |
bogdanm | 0:9b334a45a8ff | 19 | #endif |
bogdanm | 0:9b334a45a8ff | 20 | |
bogdanm | 0:9b334a45a8ff | 21 | /*lint ++flb "Enter library region" */ |
bogdanm | 0:9b334a45a8ff | 22 | |
bogdanm | 0:9b334a45a8ff | 23 | #include <stdbool.h> |
bogdanm | 0:9b334a45a8ff | 24 | #include <stdint.h> |
bogdanm | 0:9b334a45a8ff | 25 | |
mbed_official | 29:d3245924094c | 26 | #include "nrf51.h" |
mbed_official | 29:d3245924094c | 27 | |
bogdanm | 0:9b334a45a8ff | 28 | /** @file |
bogdanm | 0:9b334a45a8ff | 29 | * @brief Software controlled TWI Master driver. |
bogdanm | 0:9b334a45a8ff | 30 | * |
bogdanm | 0:9b334a45a8ff | 31 | * |
bogdanm | 0:9b334a45a8ff | 32 | * @defgroup lib_driver_twi_master Software controlled TWI Master driver |
bogdanm | 0:9b334a45a8ff | 33 | * @{ |
bogdanm | 0:9b334a45a8ff | 34 | * @ingroup nrf_drivers |
bogdanm | 0:9b334a45a8ff | 35 | * @brief Software controlled TWI Master driver. |
bogdanm | 0:9b334a45a8ff | 36 | * |
bogdanm | 0:9b334a45a8ff | 37 | * Supported features: |
bogdanm | 0:9b334a45a8ff | 38 | * - Repeated start |
bogdanm | 0:9b334a45a8ff | 39 | * - No multi-master |
bogdanm | 0:9b334a45a8ff | 40 | * - Only 7-bit addressing |
bogdanm | 0:9b334a45a8ff | 41 | * - Supports clock stretching (with optional SMBus style slave timeout) |
bogdanm | 0:9b334a45a8ff | 42 | * - Tries to handle slaves stuck in the middle of transfer |
bogdanm | 0:9b334a45a8ff | 43 | */ |
bogdanm | 0:9b334a45a8ff | 44 | |
bogdanm | 0:9b334a45a8ff | 45 | #define TWI_READ_BIT (0x01) //!< If this bit is set in the address field, transfer direction is from slave to master. |
bogdanm | 0:9b334a45a8ff | 46 | |
bogdanm | 0:9b334a45a8ff | 47 | #define TWI_ISSUE_STOP ((bool)true) //!< Parameter for @ref twi_master_transfer |
bogdanm | 0:9b334a45a8ff | 48 | #define TWI_DONT_ISSUE_STOP ((bool)false) //!< Parameter for @ref twi_master_transfer |
bogdanm | 0:9b334a45a8ff | 49 | |
bogdanm | 0:9b334a45a8ff | 50 | /* These macros are needed to see if the slave is stuck and we as master send dummy clock cycles to end its wait */ |
bogdanm | 0:9b334a45a8ff | 51 | /*lint -e717 -save "Suppress do {} while (0) for these macros" */ |
bogdanm | 0:9b334a45a8ff | 52 | /*lint ++flb "Enter library region" */ |
bogdanm | 0:9b334a45a8ff | 53 | #define TWI_SCL_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line high */ |
bogdanm | 0:9b334a45a8ff | 54 | #define TWI_SCL_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line low */ |
bogdanm | 0:9b334a45a8ff | 55 | #define TWI_SDA_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Pulls SDA line high */ |
bogdanm | 0:9b334a45a8ff | 56 | #define TWI_SDA_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Pulls SDA line low */ |
bogdanm | 0:9b334a45a8ff | 57 | #define TWI_SDA_INPUT() do { NRF_GPIO->DIRCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Configures SDA pin as input */ |
bogdanm | 0:9b334a45a8ff | 58 | #define TWI_SDA_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Configures SDA pin as output */ |
bogdanm | 0:9b334a45a8ff | 59 | #define TWI_SCL_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Configures SCL pin as output */ |
bogdanm | 0:9b334a45a8ff | 60 | /*lint -restore */ |
bogdanm | 0:9b334a45a8ff | 61 | |
bogdanm | 0:9b334a45a8ff | 62 | #define TWI_SDA_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_DATA_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SDA */ |
bogdanm | 0:9b334a45a8ff | 63 | #define TWI_SCL_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SCL */ |
bogdanm | 0:9b334a45a8ff | 64 | |
bogdanm | 0:9b334a45a8ff | 65 | #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. */ |
bogdanm | 0:9b334a45a8ff | 66 | |
bogdanm | 0:9b334a45a8ff | 67 | |
bogdanm | 0:9b334a45a8ff | 68 | /** |
bogdanm | 0:9b334a45a8ff | 69 | * @brief Function for initializing TWI bus IO pins and checks if the bus is operational. |
bogdanm | 0:9b334a45a8ff | 70 | * |
bogdanm | 0:9b334a45a8ff | 71 | * Both pins are configured as Standard-0, No-drive-1 (open drain). |
bogdanm | 0:9b334a45a8ff | 72 | * |
mbed_official | 29:d3245924094c | 73 | * @param twi The TWI interface to use - either NRF_TWI0 or NRF_TWI1 |
bogdanm | 0:9b334a45a8ff | 74 | * @return |
bogdanm | 0:9b334a45a8ff | 75 | * @retval true TWI bus is clear for transfers. |
bogdanm | 0:9b334a45a8ff | 76 | * @retval false TWI bus is stuck. |
bogdanm | 0:9b334a45a8ff | 77 | */ |
mbed_official | 29:d3245924094c | 78 | bool twi_master_init_and_clear(NRF_TWI_Type* twi); |
bogdanm | 0:9b334a45a8ff | 79 | |
bogdanm | 0:9b334a45a8ff | 80 | /** |
bogdanm | 0:9b334a45a8ff | 81 | * @brief Function for transferring data over TWI bus. |
bogdanm | 0:9b334a45a8ff | 82 | * |
bogdanm | 0:9b334a45a8ff | 83 | * If TWI master detects even one NACK from the slave or timeout occurs, STOP condition is issued |
bogdanm | 0:9b334a45a8ff | 84 | * and the function returns false. |
bogdanm | 0:9b334a45a8ff | 85 | * Bit 0 (@ref TWI_READ_BIT) in the address parameter controls transfer direction; |
bogdanm | 0:9b334a45a8ff | 86 | * - If 1, master reads data_length number of bytes from the slave |
bogdanm | 0:9b334a45a8ff | 87 | * - If 0, master writes data_length number of bytes to the slave. |
bogdanm | 0:9b334a45a8ff | 88 | * |
bogdanm | 0:9b334a45a8ff | 89 | * @note Make sure at least data_length number of bytes is allocated in data if TWI_READ_BIT is set. |
bogdanm | 0:9b334a45a8ff | 90 | * @note @ref TWI_ISSUE_STOP |
bogdanm | 0:9b334a45a8ff | 91 | * |
bogdanm | 0:9b334a45a8ff | 92 | * @param address Data transfer direction (LSB) / Slave address (7 MSBs). |
bogdanm | 0:9b334a45a8ff | 93 | * @param data Pointer to data. |
bogdanm | 0:9b334a45a8ff | 94 | * @param data_length Number of bytes to transfer. |
bogdanm | 0:9b334a45a8ff | 95 | * @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. |
mbed_official | 29:d3245924094c | 96 | * @param twi The TWI interface to use - either NRF_TWI0 or NRF_TWI1 |
bogdanm | 0:9b334a45a8ff | 97 | * @return |
bogdanm | 0:9b334a45a8ff | 98 | * @retval true Data transfer succeeded without errors. |
bogdanm | 0:9b334a45a8ff | 99 | * @retval false Data transfer failed. |
bogdanm | 0:9b334a45a8ff | 100 | */ |
mbed_official | 29:d3245924094c | 101 | bool twi_master_transfer(uint8_t address, uint8_t *data, uint8_t data_length, bool issue_stop_condition, NRF_TWI_Type* twi); |
bogdanm | 0:9b334a45a8ff | 102 | |
bogdanm | 0:9b334a45a8ff | 103 | /** |
bogdanm | 0:9b334a45a8ff | 104 | *@} |
bogdanm | 0:9b334a45a8ff | 105 | **/ |
bogdanm | 0:9b334a45a8ff | 106 | |
bogdanm | 0:9b334a45a8ff | 107 | #ifdef __cplusplus |
bogdanm | 0:9b334a45a8ff | 108 | } |
bogdanm | 0:9b334a45a8ff | 109 | #endif |
bogdanm | 0:9b334a45a8ff | 110 | |
bogdanm | 0:9b334a45a8ff | 111 | /*lint --flb "Leave library region" */ |
bogdanm | 0:9b334a45a8ff | 112 | #endif //TWI_MASTER_H |