-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst25dv.c
146 lines (113 loc) · 4.34 KB
/
st25dv.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
141
142
143
144
145
146
/*
* Written by RJRP - 28/10/2023
* ST25 Library for idf framework
* This program is distributed under the MIT License
*/
#include <memory.h>
#include <st25dv_registers.h>
#include "st25dv.h"
#include "driver/i2c.h"
esp_err_t st25dv_write_byte(uint8_t st25_address, uint16_t address, uint8_t byte) {
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
//St25 Write sequence :
i2c_master_write_byte(cmd, (st25_address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address >> 8, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address & 0xFF, ACK_CHECK_EN);
i2c_master_write_byte(cmd, byte, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t st25dv_read_byte(uint8_t st25_address, uint16_t address, uint8_t *byte) {
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
//St25 Read sequence :
i2c_master_write_byte(cmd, (st25_address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address >> 8, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address & 0xFF, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (st25_address << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
i2c_master_read_byte(cmd, byte, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t st25dv_write_bit(uint8_t st25_address, uint16_t address, uint8_t bit_mask, bool bit) {
uint8_t byte = 0;
esp_err_t ret;
ret = st25dv_read_byte(st25_address, address, &byte);
if (ret != ESP_OK) {
return ret;
}
if (bit) {
byte |= bit_mask;
} else {
byte &= ~bit_mask;
}
ret = st25dv_write_byte(st25_address, address, byte);
return ret;
}
esp_err_t st25dv_read_bit(uint8_t st25_address, uint16_t address, uint8_t bit_mask, bool *bit) {
uint8_t byte = 0;
esp_err_t ret;
ret = st25dv_read_byte(st25_address, address, &byte);
if (ret != ESP_OK) {
return ret;
}
*bit = (byte & bit_mask);
return ESP_OK;
}
esp_err_t st25dv_init_i2c(i2c_port_t port, i2c_config_t config) {
i2c_param_config(port, &config);
i2c_set_timeout(port, 0x0000001FU);
return i2c_driver_install(port, config.mode, 0, 0, 0);
}
esp_err_t st25dv_write(uint8_t st25_address, uint16_t address, uint8_t *data, size_t size) {
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
//St25 Write sequence :
i2c_master_write_byte(cmd, (st25_address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address >> 8, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address & 0xFF, ACK_CHECK_EN);
i2c_master_write(cmd, data, size, ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t st25dv_read(uint8_t st25_address, uint16_t address, uint8_t *data, size_t size) {
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
//St25 Read sequence :
i2c_master_write_byte(cmd, (st25_address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address >> 8, ACK_CHECK_EN);
i2c_master_write_byte(cmd, address & 0xFF, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (st25_address << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
if (size > 1) {
i2c_master_read(cmd, data, size - 1, 0);
}
i2c_master_read_byte(cmd, data + size - 1, 1);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t st25dv_open_session(uint8_t st25_address, uint64_t password){
uint8_t buffer[8 * 2 + 1] = {0};
uint8_t *buffer_ptr = buffer + 9;
memcpy(buffer, &password, sizeof(password));
buffer[8] = 0x09;
memcpy(buffer_ptr, &password, sizeof(password));
return st25dv_write(st25_address, REG_I2C_PASSWD_BASE,buffer, 17);
}
esp_err_t st25dv_is_session_opened(uint8_t st25_address, bool *bit){
return st25dv_read_bit(st25_address, REG_I2C_SSO_DYN, BIT_I2C_SSO_DYN_I2C_SSO, bit);
}