uStepper S-lite
i2cMaster.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: i2cMaster.h *
3 * Version: 1.1.0 *
4 * Date: June 14, 2020 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * i2cMaster class *
9 * *
10 * This file contains the implementation of the class methods, used to communicate over *
11 * the I2C bus. *
12 * *
13 *********************************************************************************************
14 * (C) 2020 *
15 * *
16 * uStepper ApS *
17 * www.ustepper.com *
18 * administration@ustepper.com *
19 * *
20 * The code contained in this file is released under the following open source license: *
21 * *
22 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
23 * *
24 * The code in this file is provided without warranty of any kind - use at own risk! *
25 * neither uStepper ApS nor the author, can be held responsible for any damage *
26 * caused by the use of the code contained in this file ! *
27 * *
28 ********************************************************************************************/
35 #ifndef _I2CMASTER_H_
36 #define _I2CMASTER_H_
37 
38 #include <inttypes.h>
39 #include <avr/io.h>
40 #include <stdlib.h>
41 
43 #define I2CFREE 0
44 
46 #define READ 1
47 
49 #define WRITE 0
50 
52 #define START 0x08
53 
55 #define REPSTART 0x10
56 
58 #define TXADDRACK 0x18
59 
61 #define TXDATAACK 0x28
62 
64 #define RXADDRACK 0x40
65 
67 #define ACK 1
68 
70 #define NACK 0
71 
93 class i2cMaster
94 {
95  private:
97  uint8_t status;
98 
99 
108  bool cmd(uint8_t cmd);
109  volatile uint8_t twsr;
110  volatile uint8_t twbr;
111  volatile uint8_t twdr;
112  volatile uint8_t twcr;
113 
114  public:
115 
116  void* operator new(size_t size);
117 
127  i2cMaster(bool channel);
128 
138  i2cMaster(void);
139 
157  bool readByte(bool ack, uint8_t *data);
158 
180  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
181 
199  bool start(uint8_t addr, bool RW);
200 
218  bool restart(uint8_t addr, bool RW);
219 
230  bool writeByte(uint8_t data);
231 
252  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
253 
264  bool stop(void);
265 
274  uint8_t getStatus(void);
275 
283  void begin(void);
284 
292  void begin(bool channel);
293 };
294 
295 #endif
i2cMaster::cmd
bool cmd(uint8_t cmd)
Sends commands over the I2C bus.
Definition: i2cMaster.cpp:39
i2cMaster::i2cMaster
i2cMaster(void)
Constructor.
Definition: i2cMaster.cpp:274
i2cMaster::read
bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
sets up I2C connection to device, reads a number of data bytes and closes the connection
Definition: i2cMaster.cpp:61
i2cMaster::begin
void begin(void)
Setup TWI (I2C) interface.
Definition: i2cMaster.cpp:221
i2cMaster::getStatus
uint8_t getStatus(void)
Get current I2C status.
Definition: i2cMaster.cpp:216
i2cMaster::stop
bool stop(void)
Closes the I2C connection.
Definition: i2cMaster.cpp:192
i2cMaster::write
bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
sets up I2C connection to device, writes a number of data bytes and closes the connection
Definition: i2cMaster.cpp:103
i2cMaster::restart
bool restart(uint8_t addr, bool RW)
Restarts connection between arduino and I2C device.
Definition: i2cMaster.cpp:178
i2cMaster
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: i2cMaster.h:94
i2cMaster::writeByte
bool writeByte(uint8_t data)
Writes a byte to a device on the I2C bus.
Definition: i2cMaster.cpp:183
i2cMaster::status
uint8_t status
Definition: i2cMaster.h:97
i2cMaster::readByte
bool readByte(bool ack, uint8_t *data)
Reads a byte from the I2C bus.
Definition: i2cMaster.cpp:128
i2cMaster::start
bool start(uint8_t addr, bool RW)
sets up connection between arduino and I2C device.
Definition: i2cMaster.cpp:152