forked from espressif/esp-iot-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
at24c02.c
141 lines (129 loc) · 4.72 KB
/
at24c02.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "driver/i2c.h"
#include "iot_i2c_bus.h"
#include "iot_at24c02.h"
typedef struct {
i2c_bus_handle_t bus;
uint16_t dev_addr;
} at24c02_dev_t;
at24c02_handle_t iot_at24c02_create(i2c_bus_handle_t bus, uint16_t dev_addr)
{
at24c02_dev_t* dev = (at24c02_dev_t*) calloc(1, sizeof(at24c02_dev_t));
dev->bus = bus;
dev->dev_addr = dev_addr;
return (at24c02_handle_t) dev;
}
esp_err_t iot_at24c02_delete(at24c02_handle_t dev, bool del_bus)
{
at24c02_dev_t* device = (at24c02_dev_t*) dev;
if (del_bus) {
iot_i2c_bus_delete(device->bus);
device->bus = NULL;
}
free(device);
return ESP_OK;
}
esp_err_t iot_at24c02_write_byte(at24c02_handle_t dev, uint8_t addr,
uint8_t data)
{
//start-device_addr-word_addr-data-stop
esp_err_t ret;
at24c02_dev_t* device = (at24c02_dev_t*) dev;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (device->dev_addr << 1) | WRITE_BIT,
ACK_CHECK_EN);
i2c_master_write_byte(cmd, addr, ACK_CHECK_EN);
i2c_master_write_byte(cmd, data, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = iot_i2c_bus_cmd_begin(device->bus, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t iot_at24c02_write(at24c02_handle_t dev, uint8_t start_addr,
uint8_t write_num, uint8_t *data_buf)
{
uint32_t i = 0;
i2c_cmd_handle_t cmd;
esp_err_t ret = ESP_FAIL;
uint32_t writeNum = write_num;
at24c02_dev_t* device = (at24c02_dev_t*) dev;
if (data_buf != NULL) {
for (uint32_t j = 0; j < write_num; j += 8) {
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (device->dev_addr << 1) | WRITE_BIT,
ACK_CHECK_EN);
i2c_master_write_byte(cmd, start_addr, ACK_CHECK_EN);
for (i = j; i < ((writeNum >= 8) ? 8 : writeNum); i++) {
i2c_master_write_byte(cmd, data_buf[i], ACK_CHECK_EN);
vTaskDelay(100 / portTICK_RATE_MS);
}
i2c_master_stop(cmd);
ret = iot_i2c_bus_cmd_begin(device->bus, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
writeNum -= 8; //write num count
if (ret == ESP_FAIL) {
return ret;
}
}
}
return ret;
}
esp_err_t iot_at24c02_read_byte(at24c02_handle_t dev, uint8_t addr,
uint8_t *data)
{
//start-device_addr-word_addr-start-device_addr-data-stop; no_ack of end data
esp_err_t ret;
at24c02_dev_t* device = (at24c02_dev_t*) dev;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (device->dev_addr << 1) | WRITE_BIT,
ACK_CHECK_EN);
i2c_master_write_byte(cmd, addr, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (device->dev_addr << 1) | READ_BIT,
ACK_CHECK_EN);
i2c_master_read_byte(cmd, data, NACK_VAL);
i2c_master_stop(cmd);
ret = iot_i2c_bus_cmd_begin(device->bus, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t iot_at24c02_read(at24c02_handle_t dev, uint8_t start_addr,
uint8_t read_num, uint8_t *data_buf)
{
uint32_t i = 0;
esp_err_t ret = ESP_FAIL;
if (data_buf != NULL) {
at24c02_dev_t* device = (at24c02_dev_t*) dev;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (device->dev_addr << 1) | WRITE_BIT,
ACK_CHECK_EN);
i2c_master_write_byte(cmd, start_addr, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (device->dev_addr << 1) | READ_BIT,
ACK_CHECK_EN);
for (i = 0; i < read_num - 1; i++) {
i2c_master_read_byte(cmd, &data_buf[i], ACK_VAL);
}
i2c_master_read_byte(cmd, &data_buf[i], NACK_VAL);
i2c_master_stop(cmd);
ret = iot_i2c_bus_cmd_begin(device->bus, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
}
return ret;
}