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