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