-
Notifications
You must be signed in to change notification settings - Fork 3
/
i2c_util.c
119 lines (106 loc) · 2.37 KB
/
i2c_util.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
#include <asf.h>
#include "i2c_util.h"
#ifndef TWI
#define TWI TWI0
#endif
static volatile uint8_t I2C_LOCK = 0;
static volatile uint8_t I2C_SETUP = 0;
void i2c_setup(void)
{
gpio_configure_pin(PIN_I2C_SDA, PIN_I2C_SDA_FLAGS);
gpio_configure_pin(PIN_I2C_SCL, PIN_I2C_SCL_FLAGS);
I2C_LOCK = 0;
if (I2C_SETUP) return;
// limited to 400kHz
twi_master_options_t opt = {
.speed = 100E3,
.chip = 0x00
};
twi_master_setup(TWI, &opt);
I2C_SETUP = 1;
}
void i2c_reset(void)
{
twi_master_options_t opt = {
.speed = 100E3,
.chip = 0x00
};
twi_reset(TWI);
twi_master_setup(TWI, &opt);
I2C_LOCK = 0;
// I2C_SETUP = 0;
}
int i2c_write(uint8_t chip_addr, uint8_t reg_addr, void *data, uint8_t len)
{
if (I2C_LOCK) {
return -10;
}
I2C_LOCK = 1;
twi_package_t packet_write = {
.addr = {reg_addr}, // TWI slave memory address data
.addr_length = 1, // TWI slave memory address data size
.chip = chip_addr, // TWI slave bus address
.buffer = data, // transfer data source buffer
.length = len // transfer data size (bytes)
};
int rtn = 0;
if (rtn = twi_master_write(TWI, &packet_write), rtn == TWI_SUCCESS){
I2C_LOCK = 0;
return 0;
} else {
// reset just in case
i2c_reset();
I2C_LOCK = 0;
return rtn;
}
}
int i2c_read(uint8_t chip_addr, uint8_t reg_addr, void *data, uint8_t len)
{
if (I2C_LOCK) {
return -10;
}
I2C_LOCK = 1;
twi_package_t packet_read = {
.addr = {reg_addr}, // TWI slave memory address data
.addr_length = 1, // TWI slave memory address data size
.chip = chip_addr, // TWI slave bus address
.buffer = data, // transfer data destination buffer
.length = len, // transfer data size (bytes)
};
int rtn = 0;
if(rtn = twi_master_read(TWI, &packet_read), rtn == TWI_SUCCESS){
I2C_LOCK = 0;
return 0;
} else {
//reset just in case
i2c_reset();
I2C_LOCK = 0;
return rtn;
}
}
int i2c_is_locked(void)
{
return I2C_LOCK;
}
int raw_i2c_read(twi_package_t *packet)
{
uint32_t rtn = 0;
if (I2C_LOCK) {
return -10;
}
I2C_LOCK = 1;
rtn = twi_master_read(TWI0, packet);
I2C_LOCK = 0;
return rtn;
}
int raw_i2c_send(twi_package_t *packet)
{
uint32_t rtn = 0;
if (I2C_LOCK) {
return -10;
}
I2C_LOCK = 1;
rtn = twi_master_write(TWI0, packet);
I2C_LOCK = 0;
return rtn;
}