YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
soft_i2c_master.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// This is a I2C master driver implemented in SW.
15// The I2C protocol is simulated by 2 GPIO line.
16
17#ifndef _SOFT_I2C_MASTER_H_
18#define _SOFT_I2C_MASTER_H_
19
20#include <i2c_interface.h>
21#include <gpio_interface.h>
22
24{
25public:
26
27 soft_i2c_master(gpio_interface & sda, gpio_interface & scl, void (*delay)(uint32_t us),
28 bool pullup = false);
29 virtual ~soft_i2c_master();
30
31 int16_t i2cRead(uint16_t addr, uint8_t *rxbuf, uint16_t len,
32 bool sendStop = true) override;
33
34 int16_t i2cWrite(uint16_t addr, uint8_t *txbuf, uint16_t len,
35 bool sendStop = true) override;
36
37 void setSpeed(uint32_t);
38
39 void init();
40
41 // No copy, no assignment
42 soft_i2c_master (const soft_i2c_master &) = delete;
43 soft_i2c_master & operator = (const soft_i2c_master &) = delete;
44
45private:
46
47 uint8_t read_byte(bool nack);
48 bool write_byte(uint8_t byte);
49 bool read_bit();
50 bool write_bit(bool bit);
51 bool send_start();
52 void send_stop();
53 bool check_bus_idle();
54
55 bool _init;
56
57 gpio_interface & _sda;
58 gpio_interface & _scl;
59 void (*_delay)(uint32_t us);
60 bool _pullup;
61 uint32_t _us;
62};
63
64#endif // _SOFT_I2C_MASTER_H_