Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 1 | /** |
| 2 | * @file |
| 3 | * |
| 4 | * @brief Public APIs for the I2C drivers. |
| 5 | */ |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 6 | |
| 7 | /* |
| 8 | * Copyright (c) 2015 Intel Corporation |
| 9 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | * you may not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 13 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 15 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 21 | */ |
| 22 | #ifndef __DRIVERS_I2C_H |
| 23 | #define __DRIVERS_I2C_H |
| 24 | |
Anas Nashif | 75482aa | 2015-10-26 06:18:44 -0400 | [diff] [blame] | 25 | /** |
| 26 | * @brief I2C Interface |
| 27 | * @defgroup i2c_interface I2C Interface |
| 28 | * @ingroup io_interfaces |
| 29 | * @{ |
| 30 | */ |
| 31 | |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | #include <stdint.h> |
| 37 | #include <device.h> |
| 38 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 39 | /* |
| 40 | * The following #defines are used to configure the I2C controller. |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 41 | */ |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 42 | |
| 43 | /** I2C Standard Speed */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 44 | #define I2C_SPEED_STANDARD (0x1) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 45 | |
| 46 | /** I2C Fast Speed */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 47 | #define I2C_SPEED_FAST (0x2) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 48 | |
| 49 | /** I2C Fast Plus Speed */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 50 | #define I2C_SPEED_FAST_PLUS (0x3) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 51 | |
| 52 | /** I2C High Speed */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 53 | #define I2C_SPEED_HIGH (0x4) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 54 | |
| 55 | /** I2C Ultra Fast Speed */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 56 | #define I2C_SPEED_ULTRA (0x5) |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 57 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 58 | /** For internal use. */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 59 | #define I2C_SPEED_MASK (0x7 << 1) /* 3 bits */ |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 60 | |
| 61 | /** Use 10-bit addressing. */ |
| 62 | #define I2C_ADDR_10_BITS (1 << 0) |
| 63 | |
| 64 | /** Controller to act as Master. */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 65 | #define I2C_MODE_MASTER (1 << 4) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 66 | |
| 67 | /** Controller to act as Slave. */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 68 | #define I2C_MODE_SLAVE_READ (1 << 5) |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 69 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * I2C_MSG_* are I2C Message flags. |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 73 | */ |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 74 | |
| 75 | /** Write message to I2C bus. */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 76 | #define I2C_MSG_WRITE (0 << 0) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 77 | |
| 78 | /** Read message from I2C bus. */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 79 | #define I2C_MSG_READ (1 << 0) |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 80 | |
| 81 | /** For internal use. */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 82 | #define I2C_MSG_RW_MASK (1 << 0) |
| 83 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 84 | /** Send STOP after this message. */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 85 | #define I2C_MSG_STOP (1 << 1) |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 86 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 87 | /** RESTART I2C transaction for this message. */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 88 | #define I2C_MSG_RESTART (1 << 2) |
| 89 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 90 | /** |
| 91 | * @brief One I2C Message. |
| 92 | * |
| 93 | * This defines one I2C message to transact on the I2C bus. |
| 94 | */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 95 | struct i2c_msg { |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 96 | /** Data buffer in bytes */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 97 | uint8_t *buf; |
| 98 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 99 | /** Length of buffer in bytes */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 100 | uint32_t len; |
| 101 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 102 | /** Flags for this message */ |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 103 | uint8_t flags; |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 104 | |
Dan Kalowsky | 4743684 | 2016-01-28 12:53:34 -0800 | [diff] [blame] | 105 | uint8_t stride[3]; |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 106 | }; |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 107 | |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 108 | union dev_config { |
| 109 | uint32_t raw; |
Inaky Perez-Gonzalez | ecc4c76 | 2016-06-16 14:52:44 -0700 | [diff] [blame] | 110 | struct __bits { |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 111 | uint32_t use_10_bit_addr : 1; |
| 112 | uint32_t speed : 3; |
| 113 | uint32_t is_master_device : 1; |
| 114 | uint32_t is_slave_read : 1; |
| 115 | uint32_t reserved : 26; |
| 116 | } bits; |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 119 | /** |
| 120 | * @cond INTERNAL_HIDDEN |
| 121 | * |
| 122 | * These are for internal use only, so skip these in |
| 123 | * public documentation. |
| 124 | */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 125 | typedef int (*i2c_api_configure_t)(struct device *dev, |
| 126 | uint32_t dev_config); |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 127 | typedef int (*i2c_api_full_io_t)(struct device *dev, |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 128 | struct i2c_msg *msgs, |
| 129 | uint8_t num_msgs, |
| 130 | uint16_t addr); |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 131 | |
| 132 | struct i2c_driver_api { |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 133 | i2c_api_configure_t configure; |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 134 | i2c_api_full_io_t transfer; |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 135 | }; |
Daniel Leung | f6646e2 | 2016-02-17 11:31:11 -0800 | [diff] [blame] | 136 | /** |
| 137 | * @endcond |
| 138 | */ |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 139 | |
Anas Nashif | 20764a2 | 2015-07-01 16:47:13 -0400 | [diff] [blame] | 140 | /** |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 141 | * @brief Configure operation of a host controller. |
Tomasz Bursztyka | ce31c52 | 2015-12-08 12:16:41 +0100 | [diff] [blame] | 142 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 143 | * @param dev Pointer to the device structure for the driver instance. |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 144 | * @param dev_config Bit-packed 32-bit value to the device runtime configuration |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 145 | * for the I2C controller. |
Dan Kalowsky | e728cad | 2015-10-01 14:16:47 -0700 | [diff] [blame] | 146 | * |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 147 | * @retval 0 If successful. |
Andre Guedes | 4851ee7 | 2016-03-10 11:47:06 -0300 | [diff] [blame] | 148 | * @retval Negative errno code if failure. |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 149 | */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 150 | static inline int i2c_configure(struct device *dev, uint32_t dev_config) |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 151 | { |
Marcus Shawcroft | e7a2445 | 2016-10-22 10:02:48 +0100 | [diff] [blame] | 152 | const struct i2c_driver_api *api = dev->driver_api; |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 153 | |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 154 | return api->configure(dev, dev_config); |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Anas Nashif | 20764a2 | 2015-07-01 16:47:13 -0400 | [diff] [blame] | 157 | /** |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 158 | * @brief Write a set amount of data to an I2C device. |
Tomasz Bursztyka | d3d1484 | 2015-10-01 14:16:47 -0700 | [diff] [blame] | 159 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 160 | * This routine writes a set amount of data synchronously. |
Tomasz Bursztyka | ce31c52 | 2015-12-08 12:16:41 +0100 | [diff] [blame] | 161 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 162 | * @param dev Pointer to the device structure for the driver instance. |
| 163 | * @param buf Memory pool from which the data is transferred. |
| 164 | * @param len Size of the memory pool available for reading. |
| 165 | * @param addr Address to the target I2C device for writing. |
Dan Kalowsky | e728cad | 2015-10-01 14:16:47 -0700 | [diff] [blame] | 166 | * |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 167 | * @retval 0 If successful. |
Andre Guedes | 4851ee7 | 2016-03-10 11:47:06 -0300 | [diff] [blame] | 168 | * @retval Negative errno code if failure. |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 169 | */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 170 | static inline int i2c_write(struct device *dev, uint8_t *buf, |
Tomasz Bursztyka | ce31c52 | 2015-12-08 12:16:41 +0100 | [diff] [blame] | 171 | uint32_t len, uint16_t addr) |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 172 | { |
Marcus Shawcroft | e7a2445 | 2016-10-22 10:02:48 +0100 | [diff] [blame] | 173 | const struct i2c_driver_api *api = dev->driver_api; |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 174 | struct i2c_msg msg; |
| 175 | |
| 176 | msg.buf = buf; |
| 177 | msg.len = len; |
Andre Guedes | e0d353b | 2016-01-13 20:35:10 -0200 | [diff] [blame] | 178 | msg.flags = I2C_MSG_WRITE | I2C_MSG_STOP; |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 179 | |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 180 | return api->transfer(dev, &msg, 1, addr); |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Anas Nashif | 20764a2 | 2015-07-01 16:47:13 -0400 | [diff] [blame] | 183 | /** |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 184 | * @brief Read a set amount of data from an I2C device. |
Tomasz Bursztyka | ce31c52 | 2015-12-08 12:16:41 +0100 | [diff] [blame] | 185 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 186 | * This routine reads a set amount of data synchronously. |
Tomasz Bursztyka | ce31c52 | 2015-12-08 12:16:41 +0100 | [diff] [blame] | 187 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 188 | * @param dev Pointer to the device structure for the driver instance. |
| 189 | * @param buf Memory pool that stores the retrieved data. |
| 190 | * @param len Size of the memory pool available for writing. |
| 191 | * @param addr Address of the I2C device being read. |
Dan Kalowsky | e728cad | 2015-10-01 14:16:47 -0700 | [diff] [blame] | 192 | * |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 193 | * @retval 0 If successful. |
Andre Guedes | 4851ee7 | 2016-03-10 11:47:06 -0300 | [diff] [blame] | 194 | * @retval Negative errno code if failure. |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 195 | */ |
Dan Kalowsky | 9c21567 | 2015-08-24 11:30:06 -0700 | [diff] [blame] | 196 | static inline int i2c_read(struct device *dev, uint8_t *buf, |
Tomasz Bursztyka | ce31c52 | 2015-12-08 12:16:41 +0100 | [diff] [blame] | 197 | uint32_t len, uint16_t addr) |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 198 | { |
Marcus Shawcroft | e7a2445 | 2016-10-22 10:02:48 +0100 | [diff] [blame] | 199 | const struct i2c_driver_api *api = dev->driver_api; |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 200 | struct i2c_msg msg; |
| 201 | |
| 202 | msg.buf = buf; |
| 203 | msg.len = len; |
Andre Guedes | e0d353b | 2016-01-13 20:35:10 -0200 | [diff] [blame] | 204 | msg.flags = I2C_MSG_READ | I2C_MSG_STOP; |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 205 | |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 206 | return api->transfer(dev, &msg, 1, addr); |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Anas Nashif | 20764a2 | 2015-07-01 16:47:13 -0400 | [diff] [blame] | 209 | /** |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 210 | * @brief Perform data transfer to another I2C device. |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 211 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 212 | * This routine provides a generic interface to perform data transfer |
| 213 | * to another I2C device synchronously. Use i2c_read()/i2c_write() |
| 214 | * for simple read or write. |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 215 | * |
Rodrigo Caballero | 5903f23 | 2016-02-06 15:19:43 -0600 | [diff] [blame] | 216 | * @param dev Pointer to the device structure for the driver instance. |
| 217 | * @param msgs Array of messages to transfer. |
| 218 | * @param num_msgs Number of messages to transfer. |
| 219 | * @param addr Address of the I2C target device. |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 220 | * |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 221 | * @retval 0 If successful. |
Andre Guedes | 4851ee7 | 2016-03-10 11:47:06 -0300 | [diff] [blame] | 222 | * @retval Negative errno code if failure. |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 223 | */ |
| 224 | static inline int i2c_transfer(struct device *dev, |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 225 | struct i2c_msg *msgs, uint8_t num_msgs, |
| 226 | uint16_t addr) |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 227 | { |
Marcus Shawcroft | e7a2445 | 2016-10-22 10:02:48 +0100 | [diff] [blame] | 228 | const struct i2c_driver_api *api = dev->driver_api; |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 229 | |
Daniel Leung | dff5f6a | 2016-01-11 15:35:39 -0800 | [diff] [blame] | 230 | return api->transfer(dev, msgs, num_msgs, addr); |
Daniel Leung | ac0beb7 | 2015-10-07 15:08:17 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 234 | * @brief Read multiple bytes from an internal address of an I2C device. |
| 235 | * |
| 236 | * This routine reads multiple bytes from an internal address of an |
| 237 | * I2C device synchronously. |
| 238 | * |
| 239 | * @param dev Pointer to the device structure for the driver instance. |
| 240 | * @param dev_addr Address of the I2C device for reading. |
| 241 | * @param start_addr Internal address from which the data is being read. |
| 242 | * @param buf Memory pool that stores the retrieved data. |
| 243 | * @param num_bytes Number of bytes being read. |
| 244 | * |
| 245 | * @retval 0 If successful. |
| 246 | * @retval Negative errno code if failure. |
| 247 | */ |
| 248 | static inline int i2c_burst_read(struct device *dev, uint16_t dev_addr, |
| 249 | uint8_t start_addr, uint8_t *buf, |
| 250 | uint8_t num_bytes) |
| 251 | { |
Marcus Shawcroft | e7a2445 | 2016-10-22 10:02:48 +0100 | [diff] [blame] | 252 | const struct i2c_driver_api *api = dev->driver_api; |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 253 | struct i2c_msg msg[2]; |
| 254 | |
| 255 | msg[0].buf = &start_addr; |
| 256 | msg[0].len = 1; |
Maureen Helm | 92de81c | 2016-04-29 16:17:03 -0500 | [diff] [blame] | 257 | msg[0].flags = I2C_MSG_WRITE; |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 258 | |
| 259 | msg[1].buf = buf; |
| 260 | msg[1].len = num_bytes; |
Maureen Helm | 92de81c | 2016-04-29 16:17:03 -0500 | [diff] [blame] | 261 | msg[1].flags = I2C_MSG_RESTART | I2C_MSG_READ | I2C_MSG_STOP; |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 262 | |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 263 | return api->transfer(dev, msg, 2, dev_addr); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @brief Write multiple bytes to an internal address of an I2C device. |
| 268 | * |
| 269 | * This routine writes multiple bytes to an internal address of an |
| 270 | * I2C device synchronously. |
| 271 | * |
| 272 | * @param dev Pointer to the device structure for the driver instance. |
| 273 | * @param dev_addr Address of the I2C device for writing. |
| 274 | * @param start_addr Internal address to which the data is being written. |
| 275 | * @param buf Memory pool from which the data is transferred. |
| 276 | * @param num_bytes Number of bytes being written. |
| 277 | * |
| 278 | * @retval 0 If successful. |
| 279 | * @retval Negative errno code if failure. |
| 280 | */ |
| 281 | static inline int i2c_burst_write(struct device *dev, uint16_t dev_addr, |
| 282 | uint8_t start_addr, uint8_t *buf, |
| 283 | uint8_t num_bytes) |
| 284 | { |
Marcus Shawcroft | e7a2445 | 2016-10-22 10:02:48 +0100 | [diff] [blame] | 285 | const struct i2c_driver_api *api = dev->driver_api; |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 286 | struct i2c_msg msg[2]; |
| 287 | |
| 288 | msg[0].buf = &start_addr; |
| 289 | msg[0].len = 1; |
| 290 | msg[0].flags = I2C_MSG_WRITE; |
| 291 | |
| 292 | msg[1].buf = buf; |
| 293 | msg[1].len = num_bytes; |
| 294 | msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP; |
| 295 | |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 296 | return api->transfer(dev, msg, 2, dev_addr); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @brief Read internal register of an I2C device. |
| 301 | * |
| 302 | * This routine reads the value of an 8-bit internal register of an I2C |
| 303 | * device synchronously. |
| 304 | * |
| 305 | * @param dev Pointer to the device structure for the driver instance. |
| 306 | * @param dev_addr Address of the I2C device for reading. |
| 307 | * @param reg_addr Address of the internal register being read. |
| 308 | * @param value Memory pool that stores the retrieved register value. |
| 309 | * |
| 310 | * @retval 0 If successful. |
| 311 | * @retval Negative errno code if failure. |
| 312 | */ |
| 313 | static inline int i2c_reg_read_byte(struct device *dev, uint16_t dev_addr, |
| 314 | uint8_t reg_addr, uint8_t *value) |
| 315 | { |
| 316 | return i2c_burst_read(dev, dev_addr, reg_addr, value, 1); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @brief Write internal register of an I2C device. |
| 321 | * |
| 322 | * This routine writes a value to an 8-bit internal register of an I2C |
| 323 | * device synchronously. |
| 324 | * |
| 325 | * @param dev Pointer to the device structure for the driver instance. |
| 326 | * @param dev_addr Address of the I2C device for writing. |
| 327 | * @param reg_addr Address of the internal register being written. |
| 328 | * @param value Value to be written to internal register. |
| 329 | * |
| 330 | * @retval 0 If successful. |
| 331 | * @retval Negative errno code if failure. |
| 332 | */ |
| 333 | static inline int i2c_reg_write_byte(struct device *dev, uint16_t dev_addr, |
| 334 | uint8_t reg_addr, uint8_t value) |
| 335 | { |
| 336 | uint8_t tx_buf[2] = {reg_addr, value}; |
| 337 | |
| 338 | return i2c_write(dev, tx_buf, 2, dev_addr); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @brief Update internal register of an I2C device. |
| 343 | * |
| 344 | * This routine updates the value of a set of bits from an 8-bit internal |
| 345 | * register of an I2C device synchronously. |
| 346 | * |
| 347 | * @param dev Pointer to the device structure for the driver instance. |
| 348 | * @param dev_addr Address of the I2C device for updating. |
| 349 | * @param reg_addr Address of the internal register being updated. |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 350 | * @param mask Bitmask for updating internal register. |
Bogdan Davidoaia | 9496ec6 | 2016-04-25 18:58:41 +0300 | [diff] [blame] | 351 | * @param value Value for updating internal register. |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 352 | * |
| 353 | * @retval 0 If successful. |
| 354 | * @retval Negative errno code if failure. |
| 355 | */ |
| 356 | static inline int i2c_reg_update_byte(struct device *dev, uint8_t dev_addr, |
Bogdan Davidoaia | 9496ec6 | 2016-04-25 18:58:41 +0300 | [diff] [blame] | 357 | uint8_t reg_addr, uint8_t mask, |
| 358 | uint8_t value) |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 359 | { |
| 360 | uint8_t old_value, new_value; |
| 361 | int rc; |
| 362 | |
Bogdan Davidoaia | 6f6e65b | 2016-04-25 16:33:06 +0300 | [diff] [blame] | 363 | rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value); |
Bogdan Davidoaia | ef26bf7 | 2016-04-21 11:45:36 +0300 | [diff] [blame] | 364 | if (rc != 0) { |
| 365 | return rc; |
| 366 | } |
| 367 | |
| 368 | new_value = (old_value & ~mask) | (value & mask); |
| 369 | if (new_value == old_value) { |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value); |
| 374 | } |
| 375 | |
Vlad Dogaru | 8fed55f | 2016-04-21 17:22:27 +0300 | [diff] [blame] | 376 | struct i2c_client_config { |
| 377 | char *i2c_master; |
| 378 | uint16_t i2c_addr; |
| 379 | }; |
| 380 | |
| 381 | #define I2C_DECLARE_CLIENT_CONFIG struct i2c_client_config i2c_client |
| 382 | |
| 383 | #define I2C_CLIENT(_master, _addr) \ |
| 384 | .i2c_client = { \ |
| 385 | .i2c_master = (_master), \ |
| 386 | .i2c_addr = (_addr), \ |
| 387 | } |
| 388 | |
| 389 | #define I2C_GET_MASTER(_conf) ((_conf)->i2c_client.i2c_master) |
| 390 | #define I2C_GET_ADDR(_conf) ((_conf)->i2c_client.i2c_addr) |
| 391 | |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 392 | #ifdef __cplusplus |
| 393 | } |
| 394 | #endif |
| 395 | |
Anas Nashif | 75482aa | 2015-10-26 06:18:44 -0400 | [diff] [blame] | 396 | /** |
| 397 | * @} |
| 398 | */ |
| 399 | |
| 400 | |
Dan Kalowsky | 8c85f01 | 2015-06-30 09:44:34 -0700 | [diff] [blame] | 401 | #endif /* __DRIVERS_I2C_H */ |