Skip to content

Commit 41a2232

Browse files
committed
Refactor, multiple modifications and added license file
1 parent d439e94 commit 41a2232

File tree

11 files changed

+214
-73
lines changed

11 files changed

+214
-73
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2017 Natanael Josue Rabello. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to
7+
deal in the Software without restriction, including without limitation the
8+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
sell copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
IN THE SOFTWARE.

README.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
11
# I2Cbus
22

3-
I2C interface library for working with ESP-32. Documentation is on its way..
3+
I2C interface library for working with **ESP32 _esp-idf_**, and supports master mode only.
4+
5+
The intention of this library is to read and write to I2C slave devices with ease, by providing quick and specific functions aimed at 8-bit registers. It is based on I2Cdev by Jeff Rowberg.
6+
7+
You can clone it right into your project components directory or in your specific library path.
8+
9+
`git clone https://github.com/natanaeljr/I2Cbus-esp32.git I2Cbus`
10+
11+
## Usage
12+
13+
The library provides two ready-to-use objects: `I2Cbus0` which corresponds to ESP32 I2C port 0, and `I2Cbus1` which corresponds to ESP32 I2C port 1. However you can create your own object as you will.
14+
15+
### Example:
16+
17+
```C++
18+
// default objects
19+
I2Cbus0.begin(GPIO_NUM_16, GPIO_NUM_17);
20+
I2Cbus1.begin(GPIO_NUM_21, GPIO_NUM_22);
21+
// OR
22+
// create an object which manages port 0
23+
I2Cbus_t myI2C(I2C_PORT_0);
24+
// configure and initialize
25+
myI2C.begin(GPIO_NUM_21, GPIO_NUM_22, 400000); // 400KHz
26+
27+
// call methods
28+
myI2C.setTimeout(100); // default 1000ms
29+
myI2C.scanner();
30+
myI2C.close();
31+
```
32+
33+
### List of methods:
34+
35+
```C++
36+
// SETUP
37+
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num, uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
38+
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num, gpio_pullup_t sda_pullup_en, gpio_pullup_t scl_pullup_en, uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
39+
esp_err_t close();
40+
void setTimeout(uint32_t ms);
41+
42+
// WRITING
43+
esp_err_t writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data, int32_t timeout = -1);
44+
esp_err_t writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data, int32_t timeout = -1);
45+
esp_err_t writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data, int32_t timeout = -1);
46+
esp_err_t writeBytes(uint8_t devAddr, uint8_t regAddr, size_t length, const uint8_t *data, int32_t timeout = -1);
47+
48+
// READING
49+
esp_err_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, int32_t timeout = -1);
50+
esp_err_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, int32_t timeout = -1);
51+
esp_err_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, int32_t timeout = -1);
52+
esp_err_t readBytes(uint8_t devAddr, uint8_t regAddr, size_t length, uint8_t *data, int32_t timeout = -1);
53+
54+
// TOOLS
55+
esp_err_t testConnection(uint8_t devAddr, int32_t timeout = -1);
56+
void scanner();
57+
```
58+
59+
---
60+
61+
Copyright (c) 2017 Natanael Rabello

component.mk

-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@ COMPONENT_SRCDIRS := src .
66

77
I2CBUS_COMPONENT_NAME := $(COMPONENT_NAME)
88
export I2CBUS_COMPONENT_NAME
9-
10-

examples/Makefile example/Makefile

File renamed without changes.

example/main/component.mk

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#
2+
# Main component makefile.
3+
#

example/main/main.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* =========================================================================
2+
The MIT License (MIT)
3+
4+
Copyright 2017 Natanael Josue Rabello. All rights reserved.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to
8+
deal in the Software without restriction, including without limitation the
9+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10+
sell copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22+
IN THE SOFTWARE.
23+
========================================================================= */
24+
25+
#include <stdint.h>
26+
#include "freertos/FreeRTOS.h"
27+
#include "freertos/task.h"
28+
#include "driver/gpio.h"
29+
#include "driver/i2c.h"
30+
#include "esp_log.h"
31+
32+
#include "I2Cbus.hpp"
33+
34+
#ifdef __cplusplus
35+
extern "C" {
36+
void app_main(void);
37+
}
38+
#endif
39+
40+
/**
41+
* I2Cbus0 and I2Cbus1 are the default objects
42+
* */
43+
44+
void app_main() {
45+
printf(LOG_BOLD("97") "\n[APP_MAIN]" LOG_RESET_COLOR "\n");
46+
47+
I2Cbus0.begin(GPIO_NUM_21, GPIO_NUM_22, 400000U);
48+
I2Cbus0.setTimeout(10);
49+
I2Cbus0.scanner();
50+
51+
I2Cbus0.writeBit(0x69, 0x6B, 6, false);
52+
53+
uint8_t buffer[6];
54+
while(true) {
55+
I2Cbus0.readBytes(0x68, 0x6F, 6, buffer);
56+
vTaskDelay(1000 / portTICK_PERIOD_MS);
57+
}
58+
59+
vTaskDelay(portMAX_DELAY);
60+
}

examples/sdkconfig.defaults example/sdkconfig.defaults

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# I2Cbus
33
#
44
CONFIG_I2CBUS_LOG_ERRORS=y
5-
CONFIG_I2CBUS_LOG_READWRITES=y
5+
CONFIG_I2CBUS_LOG_READWRITES=
66
CONFIG_I2CBUS_LOG_RW_LEVEL_INFO=
77
CONFIG_I2CBUS_LOG_RW_LEVEL_DEBUG=
8-
CONFIG_I2CBUS_LOG_RW_LEVEL_VERBOSE=y
8+
CONFIG_I2CBUS_LOG_RW_LEVEL_VERBOSE=

examples/main/component.mk

-8
This file was deleted.

examples/main/main.cpp

-35
This file was deleted.

include/I2Cbus.hpp

+45-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1+
/* =========================================================================
2+
I2Cbus library is placed under the MIT License
3+
Copyright 2017 Natanael Josue Rabello. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to
7+
deal in the Software without restriction, including without limitation the
8+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
sell copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
IN THE SOFTWARE.
22+
========================================================================= */
23+
124
#ifndef _I2CBUS_HPP_
225
#define _I2CBUS_HPP_
326

27+
#include <stdint.h>
428
#include "driver/i2c.h"
529
#include "driver/gpio.h"
630
#include "esp_err.h"
@@ -25,7 +49,9 @@ extern I2Cbus_t I2Cbus0; /*!< port: I2C_NUM_0 */
2549
extern I2Cbus_t I2Cbus1; /*!< port: I2C_NUM_1 */
2650

2751

28-
52+
/* ^^^^^^
53+
* I2CBUS
54+
* ^^^^^^ */
2955
class I2Cbus_t {
3056
private:
3157
i2c_port_t port; /*!< I2C port: I2C_NUM_0 or I2C_NUM_1 */
@@ -47,8 +73,10 @@ class I2Cbus_t {
4773
* - ESP_ERR_INVALID_ARG Parameter error
4874
* - ESP_FAIL Driver install error
4975
*/
50-
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num, uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
51-
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num,
76+
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num,
77+
uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
78+
79+
esp_err_t begin(gpio_num_t sda_io_num, gpio_num_t scl_io_num,
5280
gpio_pullup_t sda_pullup_en, gpio_pullup_t scl_pullup_en,
5381
uint32_t clk_speed = I2CBUS_CLOCKSPEED_DEFAULT);
5482

@@ -67,12 +95,15 @@ class I2Cbus_t {
6795
* *** WRITING interface ***
6896
* @brief I2C commands for writing to a 8-bit slave device register.
6997
* All of them returns standard esp_err_t codes. So it can be used
70-
* within ESP_ERROR_CHECK();
98+
* with ESP_ERROR_CHECK();
7199
* @param devAddr [I2C slave device register]
72-
* @param regAddr [Register address to write to]
73-
* @param bit [Number of the bit position to write (bit 7~0)]
100+
* @param regAddr [Register address to write to]
101+
* @param bitNum [Bit position number to write to (bit 7~0)]
102+
* @param bitStart [Start bit number when writing a bit-sequence (MSB)]
74103
* @param data [Value(s) to be write to the register]
75104
* @param length [Number of bytes to write (should be within the data buffer size)]
105+
* [writeBits() -> Number of bits after bitStart (including)]
106+
* @param timeout [Custom timeout for the particular call]
76107
* @return - ESP_OK Success
77108
* - ESP_ERR_INVALID_ARG Parameter error
78109
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
@@ -88,12 +119,14 @@ class I2Cbus_t {
88119
* *** READING interface ***
89120
* @breif I2C commands for reading a 8-bit slave device register.
90121
* All of them returns standard esp_err_t codes.So it can be used
91-
* within ESP_ERROR_CHECK();
122+
* with ESP_ERROR_CHECK();
92123
* @param devAddr [I2C slave device register]
93-
* @param regAddr [Register address to read from]
94-
* @param bit [Number of the bit position to write (bit 7~0)]
95-
* @param data [Buffer to store the read data(s)]
124+
* @param regAddr [Register address to read from]
125+
* @param bitNum [Bit position number to write to (bit 7~0)]
126+
* @param bitStart [Start bit number when writing a bit-sequence (MSB)]
127+
* @param data [Buffer to store the read value(s)]
96128
* @param length [Number of bytes to read (should be within the data buffer size)]
129+
* @param timeout [Custom timeout for the particular call]
97130
* @return - ESP_OK Success
98131
* - ESP_ERR_INVALID_ARG Parameter error
99132
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
@@ -107,7 +140,8 @@ class I2Cbus_t {
107140

108141
/**
109142
* @brief Quick check to see if a slave device responds.
110-
* @param devAddr [I2C slave device register]
143+
* @param devAddr [I2C slave device register]
144+
* @param timeout [Custom timeout for the particular call]
111145
* @return - ESP_OK Success
112146
* - ESP_ERR_INVALID_ARG Parameter error
113147
* - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
@@ -124,8 +158,4 @@ class I2Cbus_t {
124158

125159

126160

127-
128-
129-
130-
131161
#endif /* end of include guard: _I2CBUS_H_ */

src/I2Cbus.cpp

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1+
/* =========================================================================
2+
I2Cbus library is placed under the MIT License
3+
Copyright 2017 Natanael Josue Rabello. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to
7+
deal in the Software without restriction, including without limitation the
8+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
sell copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
IN THE SOFTWARE.
22+
========================================================================= */
23+
124
#include "I2Cbus.hpp"
25+
#include <stdint.h>
226
#include "driver/i2c.h"
327
#include "driver/gpio.h"
428
#include "esp_err.h"
@@ -205,13 +229,3 @@ void I2Cbus_t::scanner() {
205229

206230

207231

208-
209-
210-
211-
212-
213-
214-
215-
216-
217-

0 commit comments

Comments
 (0)