AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
I2cMaster.h
Go to the documentation of this file.
1 /*
2  I2cMaster.h - An I2C master library
3  For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4  This is part of the AVRTools library.
5  Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
68 #ifndef I2cMaster_h
69 #define I2cMaster_h
70 
71 
72 #ifdef I2cSlave_h
73 #error "You cannot use both I2cMaster and I2cSlave in the same application"
74 #endif
75 
76 
77 #include <stdint.h>
78 #include <stdlib.h>
79 
80 #include <util/atomic.h>
81 
82 
83 #if defined( DEBUG_I2cMasterBuffer ) || defined( DEBUG_I2cMasterDiary )
84 #include "USART0.h"
85 #endif
86 
87 
88 
89 
90 
91 #ifndef I2C_MASTER_MAX_TX_MSG_LEN
92 #define I2C_MASTER_MAX_TX_MSG_LEN 24
93 #endif
94 
95 #ifndef I2C_MASTER_MAX_TX_MSG_NBR
96 #define I2C_MASTER_MAX_TX_MSG_NBR 3
97 #endif
98 
99 #if I2C_MASTER_MAX_TX_MSG_LEN > 255
100 #error "I2C_MASTER_MAX_TX_MSG_LEN exceeds size of a uint8_t"
101 #endif
102 
103 #if I2C_MASTER_MAX_TX_MSG_NBR > 255
104 #error "I2C_MASTER_MAX_TX_MSG_NBR exceeds size of a uint8_t"
105 #endif
106 
107 
108 
109 
110 
142 namespace I2cMaster
143 {
144 
151  {
154  };
155 
156 
161  {
163  kI2cError = 0x01,
164  kI2cNotStarted = 0x02,
166  };
167 
168 
173  {
180  };
181 
182 
189  {
192  };
193 
194 
195 
196 
197 
207  void start( uint8_t speed = kI2cBusFast );
208 
209 
215  void stop();
216 
217 
226  void pullups( uint8_t set = kPullupsOn );
227 
228 
235  bool busy();
236 
237 
238 
239 
240  // Asynchronous functions
241 
242 
259  uint8_t writeAsync( uint8_t address, uint8_t registerAddress, volatile uint8_t* status );
260 
261 
279  uint8_t writeAsync( uint8_t address, uint8_t registerAddress, uint8_t data, volatile uint8_t* status );
280 
281 
300  uint8_t writeAsync( uint8_t address, uint8_t registerAddress, const char* data, volatile uint8_t* status );
301 
302 
322  uint8_t writeAsync( uint8_t address, uint8_t registerAddress, uint8_t* data, uint8_t numberBytes,
323  volatile uint8_t* status );
324 
325 
326 
347  uint8_t readAsync( uint8_t address, uint8_t numberBytes, volatile uint8_t* destination,
348  volatile uint8_t* bytesRead, volatile uint8_t* status );
349 
350 
374  uint8_t readAsync( uint8_t address, uint8_t registerAddress, uint8_t numberBytes,
375  volatile uint8_t* destination, volatile uint8_t* bytesRead,
376  volatile uint8_t* status );
377 
378 
379 
380  // Synchronous
381 
382 
394  int writeSync( uint8_t address, uint8_t registerAddress );
395 
396 
409  int writeSync( uint8_t address, uint8_t registerAddress, uint8_t data );
410 
411 
426  int writeSync( uint8_t address, uint8_t registerAddress, const char* data );
427 
428 
445  int writeSync( uint8_t address, uint8_t registerAddress, uint8_t* data, uint8_t numberBytes );
446 
447 
448 
462  int readSync( uint8_t address, uint8_t numberBytes, uint8_t* destination );
463 
464 
481  int readSync( uint8_t address, uint8_t registerAddress, uint8_t numberBytes, uint8_t* destination );
482 
483 
484 #if defined( DEBUG_I2cMasterBuffer ) || defined( DEBUG_I2cMasterDiary )
485  void setDebugSout( Serial0* s );
486 #endif
487 
488 #ifdef DEBUG_I2cMasterBuffer
489  void dumpBufferContents();
490 #endif
491 
492 #ifdef DEBUG_I2cMasterDiary
493  void clearDebugI2cDiary();
494  void dumpDebugI2cDiary();
495 #endif
496 
497 
498 };
499 
500 
501 #endif
The message is too long for the transmit buffer.
Definition: I2cMaster.h:176
Disable the built-in TWI hardware pullups.
Definition: I2cMaster.h:190
This file provides functions that offer high-level interfaces to USART0 hardware, which is available ...
void pullups(uint8_t set=kPullupsOn)
Sets the state of the internal pullups that are part of the TWI hardware.
Definition: I2cMaster.cpp:555
int readSync(uint8_t address, uint8_t numberBytes, uint8_t *destination)
Request to read data from a device and receive that data synchronously. This function blocks until th...
Definition: I2cMaster.cpp:812
I2cBusSpeed
This enum lists I2C bus speed configurations.
Definition: I2cMaster.h:150
I2C communications on this message still in progress.
Definition: I2cMaster.h:165
I2C communications not started on this message.
Definition: I2cMaster.h:164
The transmit buffer is full (try again later)
Definition: I2cMaster.h:175
The pointer to the status variable is null (need to provide a valid pointer)
Definition: I2cMaster.h:177
Performing a write+read, but no buffer provided to store the "read" data.
Definition: I2cMaster.h:179
void stop()
Terminates the I2C communications using the TWI hardware, and disables the TWI interrupts.
Definition: I2cMaster.cpp:548
I2C slow (standard) mode: 100 KHz.
Definition: I2cMaster.h:152
Enable the built-in TWI hardware pullups.
Definition: I2cMaster.h:191
I2cSendErrorCodes
This enum lists I2C errors codes that may occur when you try to write a message.
Definition: I2cMaster.h:172
I2C communications had an error on this message.
Definition: I2cMaster.h:163
uint8_t readAsync(uint8_t address, uint8_t numberBytes, volatile uint8_t *destination, volatile uint8_t *bytesRead, volatile uint8_t *status)
Request to read data from a device and receive that data asynchronously. This function queues the mes...
Definition: I2cMaster.cpp:700
No data provided to send.
Definition: I2cMaster.h:178
Provides a high-end interface to serial communications using USART0.
Definition: USART0.h:295
I2cPullups
This enum lists the options for controlling the built-in pullups in the TWI hardware.
Definition: I2cMaster.h:188
I2C communications completed on this message with no error.
Definition: I2cMaster.h:162
void start(uint8_t speed=kI2cBusFast)
Configures the TWI hardware for I2C communications in Master mode. You must call this function before...
Definition: I2cMaster.cpp:508
bool busy()
Reports whether the TWI hardware is busy communicating (either transmitting or receiving).
Definition: I2cMaster.cpp:571
I2C fast mode: 400 KHz.
Definition: I2cMaster.h:153
uint8_t writeAsync(uint8_t address, uint8_t registerAddress, volatile uint8_t *status)
Transmit a single register address (a one-byte message) asynchronously. This function queues the mess...
Definition: I2cMaster.cpp:582
I2cStatusCodes
This enum lists I2C status codes reported by the various transmit functions.
Definition: I2cMaster.h:160
This namespace bundles the I2C-protocol-based interface to the TWI hardware. It provides logical cohe...
Definition: I2cMaster.h:142
No error.
Definition: I2cMaster.h:174
int writeSync(uint8_t address, uint8_t registerAddress)
Transmit a single register address (a one-byte message) synchronously. This function blocks until the...
Definition: I2cMaster.cpp:759