1 /** 2 * \file 3 * 4 * \brief I2C Master Driver for SAMB 5 * 6 * Copyright (c) 2015-2016 Atmel Corporation. All rights reserved. 7 * 8 * \asf_license_start 9 * 10 * \page License 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions are met: 14 * 15 * 1. Redistributions of source code must retain the above copyright notice, 16 * this list of conditions and the following disclaimer. 17 * 18 * 2. Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * 22 * 3. The name of Atmel may not be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * 4. This software may only be redistributed and used in connection with an 26 * Atmel microcontroller product. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 * 40 * \asf_license_stop 41 * 42 */ 43 /* 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> 45 */ 46 47 #ifndef I2C_MASTER_H_INCLUDED 48 #define I2C_MASTER_H_INCLUDED 49 50 #include <compiler.h> 51 #include "i2c_common.h" 52 53 #ifdef __cplusplus 54 extern "C" { 55 #endif 56 57 /** 58 * \addtogroup asfdoc_samb_i2c_group 59 * 60 * @{ 61 */ 62 63 64 /** 65 * \brief I<SUP>2</SUP>C master packet for read/write 66 * 67 * Structure to be used when transferring I<SUP>2</SUP>C master packets. 68 */ 69 struct i2c_master_packet { 70 /** Address to slave device */ 71 uint16_t address; 72 /** Length of data array */ 73 uint16_t data_length; 74 /** Data array containing all data to be transferred */ 75 uint8_t *data; 76 }; 77 78 /** \brief Interrupt flags 79 * 80 * Flags used when reading or setting interrupt flags. 81 */ 82 enum i2c_master_interrupt_flag { 83 /** Interrupt flag used for write */ 84 I2C_MASTER_INTERRUPT_WRITE = 0, 85 /** Interrupt flag used for read */ 86 I2C_MASTER_INTERRUPT_READ = 1, 87 }; 88 89 /** 90 * \brief I<SUP>2</SUP>C frequencies 91 * 92 * Values for I<SUP>2</SUP>C speeds supported by the module. 93 * 94 */ 95 enum i2c_master_baud_rate { 96 /** Baud rate at 100KHz (Standard-mode) */ 97 I2C_MASTER_BAUD_RATE_100KHZ = 100, 98 /** Baud rate at 400KHz (Fast-mode) */ 99 I2C_MASTER_BAUD_RATE_400KHZ = 400, 100 }; 101 102 103 #if I2C_MASTER_CALLBACK_MODE == true 104 /** 105 * \brief Callback types 106 * 107 * The available callback types for the I<SUP>2</SUP>C master module. 108 */ 109 enum i2c_master_callback { 110 /** Callback for packet write complete */ 111 I2C_MASTER_CALLBACK_WRITE_COMPLETE = 0, 112 /** Callback for packet read complete */ 113 I2C_MASTER_CALLBACK_READ_COMPLETE = 1, 114 # if !defined(__DOXYGEN__) 115 /** Total number of callbacks */ 116 _I2C_MASTER_CALLBACK_N = 2, 117 # endif 118 }; 119 120 # if !defined(__DOXYGEN__) 121 /** Prototype for software module. */ 122 struct i2c_master_module; 123 124 /** Prototype for I2C Callback function */ 125 typedef void (*i2c_master_callback_t)( 126 struct i2c_master_module *const module); 127 # endif 128 #endif 129 130 /** 131 * \brief I<SUP>2</SUP>C driver software device instance structure. 132 * 133 * I<SUP>2</SUP>C driver software instance structure, used to 134 * retain software state information of an associated hardware module instance. 135 * 136 * \note The fields of this structure should not be altered by the user 137 * application; they are reserved for module-internal use only. 138 */ 139 struct i2c_master_module { 140 #if !defined(__DOXYGEN__) 141 /** Hardware instance initialized for the struct */ 142 I2c *hw; 143 /** Module lock */ 144 volatile bool locked; 145 /** If true, stop condition will be sent after a read/write */ 146 bool no_stop; 147 # if I2C_MASTER_CALLBACK_MODE == true 148 /** Pointers to callback functions */ 149 volatile i2c_master_callback_t callbacks[_I2C_MASTER_CALLBACK_N]; 150 /** Mask for registered callbacks */ 151 volatile uint8_t registered_callback; 152 /** Mask for enabled callbacks */ 153 volatile uint8_t enabled_callback; 154 /** The total number of bytes to transfer */ 155 volatile uint16_t buffer_length; 156 /** 157 * Counter used for bytes left to send in write and to count number of 158 * obtained bytes in read 159 */ 160 volatile uint16_t buffer_remaining; 161 /** Data buffer for packet write and read */ 162 volatile uint8_t *buffer; 163 /** Save direction of async request. 1 = read, 0 = write */ 164 volatile enum i2c_transfer_direction transfer_direction; 165 /** Status for status read back in error callback */ 166 volatile enum status_code status; 167 # endif 168 #endif 169 }; 170 171 /** 172 * \brief Configuration structure for the I<SUP>2</SUP>C Master device 173 * 174 * This is the configuration structure for the I<SUP>2</SUP>C Master device. It 175 * is used as an argument for \ref i2c_master_init to provide the desired 176 * configurations for the module. The structure should be initialized using the 177 * \ref i2c_master_get_config_defaults . 178 */ 179 struct i2c_master_config { 180 /** CLOCK INPUT to use as clock source */ 181 enum i2c_clock_input clock_source; 182 /** Divide ratio used to generate the sck clock */ 183 uint16_t clock_divider; 184 /** PAD0 (SDA) pin number */ 185 uint32_t pin_number_pad0; 186 /** PAD0 (SDA) pinmux selection */ 187 uint32_t pinmux_sel_pad0; 188 /** PAD1 (SCL) pin numer */ 189 uint32_t pin_number_pad1; 190 /** PAD1 (SCL) pinmux selection */ 191 uint32_t pinmux_sel_pad1; 192 }; 193 194 void i2c_master_get_config_defaults( 195 struct i2c_master_config *const config); 196 197 enum status_code i2c_master_init( 198 struct i2c_master_module *const module, 199 I2c *const hw, 200 const struct i2c_master_config *const config); 201 202 void i2c_master_reset(struct i2c_master_module *const module); 203 204 enum status_code i2c_master_write_address( 205 struct i2c_master_module *const module, 206 uint8_t address, 207 uint8_t command); 208 209 enum status_code i2c_master_read_packet_wait( 210 struct i2c_master_module *const module, 211 struct i2c_master_packet *const packet); 212 213 enum status_code i2c_master_read_packet_wait_no_stop( 214 struct i2c_master_module *const module, 215 struct i2c_master_packet *const packet); 216 217 enum status_code i2c_master_write_packet_wait( 218 struct i2c_master_module *const module, 219 struct i2c_master_packet *const packet); 220 221 enum status_code i2c_master_write_packet_wait_no_stop( 222 struct i2c_master_module *const module, 223 struct i2c_master_packet *const packet); 224 225 void i2c_master_send_stop(struct i2c_master_module *const module); 226 227 void i2c_master_send_start(struct i2c_master_module *const module); 228 229 enum status_code i2c_master_read_byte( 230 struct i2c_master_module *const module, 231 uint8_t *byte); 232 233 enum status_code i2c_master_write_byte( 234 struct i2c_master_module *const module, 235 uint8_t byte); 236 237 /** @} */ 238 239 #ifdef __cplusplus 240 } 241 #endif 242 243 #endif /* I2C_MASTER_H_INCLUDED */ 244 245