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_HRM1017/TARGET_NORDIC/TARGET_MCU_NRF51822/twi_master.h@136:ef9c61f8c49f, 2017-02-14 (annotated)
- Committer:
- Kojto
- Date:
- Tue Feb 14 11:24:20 2017 +0000
- Revision:
- 136:ef9c61f8c49f
- Parent:
- 111:4336505e4b1c
Release 136 of the mbed library
Ports for Upcoming Targets
Fixes and Changes
3432: Target STM USBHOST support https://github.com/ARMmbed/mbed-os/pull/3432
3181: NUCLEO_F207ZG extending PeripheralPins.c: all available alternate functions can be used now https://github.com/ARMmbed/mbed-os/pull/3181
3626: NUCLEO_F412ZG : Add USB Device +Host https://github.com/ARMmbed/mbed-os/pull/3626
3628: Fix warnings https://github.com/ARMmbed/mbed-os/pull/3628
3629: STM32: L0 LL layer https://github.com/ARMmbed/mbed-os/pull/3629
3632: IDE Export support for platform VK_RZ_A1H https://github.com/ARMmbed/mbed-os/pull/3632
3642: Missing IRQ pin fix for platform VK_RZ_A1H https://github.com/ARMmbed/mbed-os/pull/3642
3664: Fix ncs36510 sleep definitions https://github.com/ARMmbed/mbed-os/pull/3664
3655: [STM32F4] Modify folder structure https://github.com/ARMmbed/mbed-os/pull/3655
3657: [STM32L4] Modify folder structure https://github.com/ARMmbed/mbed-os/pull/3657
3658: [STM32F3] Modify folder structure https://github.com/ARMmbed/mbed-os/pull/3658
3685: STM32: I2C: reset state machine https://github.com/ARMmbed/mbed-os/pull/3685
3692: uVisor: Standardize available legacy heap and stack https://github.com/ARMmbed/mbed-os/pull/3692
3621: Fix for #2884, LPC824: export to LPCXpresso, target running with wron https://github.com/ARMmbed/mbed-os/pull/3621
3649: [STM32F7] Modify folder structure https://github.com/ARMmbed/mbed-os/pull/3649
3695: Enforce device_name is valid in targets.json https://github.com/ARMmbed/mbed-os/pull/3695
3723: NCS36510: spi_format function bug fix https://github.com/ARMmbed/mbed-os/pull/3723
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 104:b9ad9a133dc7 | 1 | /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved. |
Kojto | 104:b9ad9a133dc7 | 2 | * |
Kojto | 104:b9ad9a133dc7 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
Kojto | 104:b9ad9a133dc7 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
Kojto | 104:b9ad9a133dc7 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
Kojto | 104:b9ad9a133dc7 | 6 | * |
Kojto | 104:b9ad9a133dc7 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
Kojto | 104:b9ad9a133dc7 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
Kojto | 104:b9ad9a133dc7 | 9 | * the file. |
Kojto | 104:b9ad9a133dc7 | 10 | * |
Kojto | 104:b9ad9a133dc7 | 11 | */ |
Kojto | 104:b9ad9a133dc7 | 12 | |
Kojto | 104:b9ad9a133dc7 | 13 | #ifndef TWI_MASTER_H |
Kojto | 104:b9ad9a133dc7 | 14 | #define TWI_MASTER_H |
Kojto | 104:b9ad9a133dc7 | 15 | |
Kojto | 104:b9ad9a133dc7 | 16 | |
Kojto | 104:b9ad9a133dc7 | 17 | #ifdef __cplusplus |
Kojto | 104:b9ad9a133dc7 | 18 | extern "C" { |
Kojto | 104:b9ad9a133dc7 | 19 | #endif |
Kojto | 104:b9ad9a133dc7 | 20 | |
Kojto | 104:b9ad9a133dc7 | 21 | /*lint ++flb "Enter library region" */ |
Kojto | 104:b9ad9a133dc7 | 22 | |
Kojto | 104:b9ad9a133dc7 | 23 | #include <stdbool.h> |
Kojto | 104:b9ad9a133dc7 | 24 | #include <stdint.h> |
Kojto | 104:b9ad9a133dc7 | 25 | |
Kojto | 111:4336505e4b1c | 26 | #include "nrf51.h" |
Kojto | 111:4336505e4b1c | 27 | |
Kojto | 104:b9ad9a133dc7 | 28 | /** @file |
Kojto | 104:b9ad9a133dc7 | 29 | * @brief Software controlled TWI Master driver. |
Kojto | 104:b9ad9a133dc7 | 30 | * |
Kojto | 104:b9ad9a133dc7 | 31 | * |
Kojto | 104:b9ad9a133dc7 | 32 | * @defgroup lib_driver_twi_master Software controlled TWI Master driver |
Kojto | 104:b9ad9a133dc7 | 33 | * @{ |
Kojto | 104:b9ad9a133dc7 | 34 | * @ingroup nrf_drivers |
Kojto | 104:b9ad9a133dc7 | 35 | * @brief Software controlled TWI Master driver. |
Kojto | 104:b9ad9a133dc7 | 36 | * |
Kojto | 104:b9ad9a133dc7 | 37 | * Supported features: |
Kojto | 104:b9ad9a133dc7 | 38 | * - Repeated start |
Kojto | 104:b9ad9a133dc7 | 39 | * - No multi-master |
Kojto | 104:b9ad9a133dc7 | 40 | * - Only 7-bit addressing |
Kojto | 104:b9ad9a133dc7 | 41 | * - Supports clock stretching (with optional SMBus style slave timeout) |
Kojto | 104:b9ad9a133dc7 | 42 | * - Tries to handle slaves stuck in the middle of transfer |
Kojto | 104:b9ad9a133dc7 | 43 | */ |
Kojto | 104:b9ad9a133dc7 | 44 | |
Kojto | 104:b9ad9a133dc7 | 45 | #define TWI_READ_BIT (0x01) //!< If this bit is set in the address field, transfer direction is from slave to master. |
Kojto | 104:b9ad9a133dc7 | 46 | |
Kojto | 104:b9ad9a133dc7 | 47 | #define TWI_ISSUE_STOP ((bool)true) //!< Parameter for @ref twi_master_transfer |
Kojto | 104:b9ad9a133dc7 | 48 | #define TWI_DONT_ISSUE_STOP ((bool)false) //!< Parameter for @ref twi_master_transfer |
Kojto | 104:b9ad9a133dc7 | 49 | |
Kojto | 104:b9ad9a133dc7 | 50 | /* These macros are needed to see if the slave is stuck and we as master send dummy clock cycles to end its wait */ |
Kojto | 104:b9ad9a133dc7 | 51 | /*lint -e717 -save "Suppress do {} while (0) for these macros" */ |
Kojto | 104:b9ad9a133dc7 | 52 | /*lint ++flb "Enter library region" */ |
Kojto | 104:b9ad9a133dc7 | 53 | #define TWI_SCL_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line high */ |
Kojto | 104:b9ad9a133dc7 | 54 | #define TWI_SCL_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line low */ |
Kojto | 104:b9ad9a133dc7 | 55 | #define TWI_SDA_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Pulls SDA line high */ |
Kojto | 104:b9ad9a133dc7 | 56 | #define TWI_SDA_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Pulls SDA line low */ |
Kojto | 104:b9ad9a133dc7 | 57 | #define TWI_SDA_INPUT() do { NRF_GPIO->DIRCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Configures SDA pin as input */ |
Kojto | 104:b9ad9a133dc7 | 58 | #define TWI_SDA_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Configures SDA pin as output */ |
Kojto | 104:b9ad9a133dc7 | 59 | #define TWI_SCL_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Configures SCL pin as output */ |
Kojto | 104:b9ad9a133dc7 | 60 | /*lint -restore */ |
Kojto | 104:b9ad9a133dc7 | 61 | |
Kojto | 104:b9ad9a133dc7 | 62 | #define TWI_SDA_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_DATA_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SDA */ |
Kojto | 104:b9ad9a133dc7 | 63 | #define TWI_SCL_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SCL */ |
Kojto | 104:b9ad9a133dc7 | 64 | |
Kojto | 104:b9ad9a133dc7 | 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. */ |
Kojto | 104:b9ad9a133dc7 | 66 | |
Kojto | 104:b9ad9a133dc7 | 67 | |
Kojto | 104:b9ad9a133dc7 | 68 | /** |
Kojto | 104:b9ad9a133dc7 | 69 | * @brief Function for initializing TWI bus IO pins and checks if the bus is operational. |
Kojto | 104:b9ad9a133dc7 | 70 | * |
Kojto | 104:b9ad9a133dc7 | 71 | * Both pins are configured as Standard-0, No-drive-1 (open drain). |
Kojto | 104:b9ad9a133dc7 | 72 | * |
Kojto | 111:4336505e4b1c | 73 | * @param twi The TWI interface to use - either NRF_TWI0 or NRF_TWI1 |
Kojto | 104:b9ad9a133dc7 | 74 | * @return |
Kojto | 104:b9ad9a133dc7 | 75 | * @retval true TWI bus is clear for transfers. |
Kojto | 104:b9ad9a133dc7 | 76 | * @retval false TWI bus is stuck. |
Kojto | 104:b9ad9a133dc7 | 77 | */ |
Kojto | 111:4336505e4b1c | 78 | bool twi_master_init_and_clear(NRF_TWI_Type* twi); |
Kojto | 104:b9ad9a133dc7 | 79 | |
Kojto | 104:b9ad9a133dc7 | 80 | /** |
Kojto | 104:b9ad9a133dc7 | 81 | * @brief Function for transferring data over TWI bus. |
Kojto | 104:b9ad9a133dc7 | 82 | * |
Kojto | 104:b9ad9a133dc7 | 83 | * If TWI master detects even one NACK from the slave or timeout occurs, STOP condition is issued |
Kojto | 104:b9ad9a133dc7 | 84 | * and the function returns false. |
Kojto | 104:b9ad9a133dc7 | 85 | * Bit 0 (@ref TWI_READ_BIT) in the address parameter controls transfer direction; |
Kojto | 104:b9ad9a133dc7 | 86 | * - If 1, master reads data_length number of bytes from the slave |
Kojto | 104:b9ad9a133dc7 | 87 | * - If 0, master writes data_length number of bytes to the slave. |
Kojto | 104:b9ad9a133dc7 | 88 | * |
Kojto | 104:b9ad9a133dc7 | 89 | * @note Make sure at least data_length number of bytes is allocated in data if TWI_READ_BIT is set. |
Kojto | 104:b9ad9a133dc7 | 90 | * @note @ref TWI_ISSUE_STOP |
Kojto | 104:b9ad9a133dc7 | 91 | * |
Kojto | 104:b9ad9a133dc7 | 92 | * @param address Data transfer direction (LSB) / Slave address (7 MSBs). |
Kojto | 104:b9ad9a133dc7 | 93 | * @param data Pointer to data. |
Kojto | 104:b9ad9a133dc7 | 94 | * @param data_length Number of bytes to transfer. |
Kojto | 104:b9ad9a133dc7 | 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. |
Kojto | 111:4336505e4b1c | 96 | * @param twi The TWI interface to use - either NRF_TWI0 or NRF_TWI1 |
Kojto | 104:b9ad9a133dc7 | 97 | * @return |
Kojto | 104:b9ad9a133dc7 | 98 | * @retval true Data transfer succeeded without errors. |
Kojto | 104:b9ad9a133dc7 | 99 | * @retval false Data transfer failed. |
Kojto | 104:b9ad9a133dc7 | 100 | */ |
Kojto | 111:4336505e4b1c | 101 | bool twi_master_transfer(uint8_t address, uint8_t *data, uint8_t data_length, bool issue_stop_condition, NRF_TWI_Type* twi); |
Kojto | 104:b9ad9a133dc7 | 102 | |
Kojto | 104:b9ad9a133dc7 | 103 | /** |
Kojto | 104:b9ad9a133dc7 | 104 | *@} |
Kojto | 104:b9ad9a133dc7 | 105 | **/ |
Kojto | 104:b9ad9a133dc7 | 106 | |
Kojto | 104:b9ad9a133dc7 | 107 | #ifdef __cplusplus |
Kojto | 104:b9ad9a133dc7 | 108 | } |
Kojto | 104:b9ad9a133dc7 | 109 | #endif |
Kojto | 104:b9ad9a133dc7 | 110 | |
Kojto | 104:b9ad9a133dc7 | 111 | /*lint --flb "Leave library region" */ |
Kojto | 104:b9ad9a133dc7 | 112 | #endif //TWI_MASTER_H |