Skip to content

Commit 5c08854

Browse files
author
Xie,Qi
committed
support lakemont I2C controller master mode
1 parent a86f8ea commit 5c08854

File tree

13 files changed

+817
-582
lines changed

13 files changed

+817
-582
lines changed

cores/arduino/i2c.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@
2929
extern "C" {
3030
#endif
3131

32-
#define I2C_OK 0
32+
#define I2C_OK 0
3333
#define I2C_TIMEOUT -10
34-
#define I2C_ERROR -20
34+
#define I2C_ERROR -20
3535
#define I2C_ERROR_ADDRESS_NOACK (-2)
36-
#define I2C_ERROR_DATA_NOACK (-3)
37-
#define I2C_ERROR_OTHER (-4)
36+
#define I2C_ERROR_DATA_NOACK (-3)
37+
#define I2C_ERROR_OTHER (-4)
3838

39-
#define I2C_ABRT_7B_ADDR_NOACK (1 << 0)
40-
#define I2C_ABRT_TXDATA_NOACK (1 << 3)
39+
#define I2C_ABRT_7B_ADDR_NOACK (1 << 0)
40+
#define I2C_ABRT_TXDATA_NOACK (1 << 3)
4141

4242
int i2c_openadapter(void);
4343
int i2c_openadapter_speed(int);
4444
void i2c_setslave(uint8_t addr);
4545
int i2c_writebytes(uint8_t *bytes, uint8_t length, bool no_stop);
4646
int i2c_readbytes(uint8_t *buf, int length, bool no_stop);
4747

48-
int soc_i2c_openadapter(uint8_t address);
49-
void soc_i2c_setslave(uint8_t addr);
48+
int soc_i2c_openadapter(uint32_t address,int i2c_speed,int i2c_addr_mode);
49+
void soc_i2c_master_set_slave_address(uint32_t addr);
5050
int soc_i2c_master_witebytes(uint8_t *bytes, uint8_t length, bool no_stop);
5151
int soc_i2c_master_readbytes(uint8_t *buf, int length, bool no_stop);
5252
void soc_i2c_slave_set_rx_user_callback(void (*onReceiveCallback)(int));

cores/arduino/soc_i2c.c

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,53 @@ static volatile uint8_t soc_i2c_master_rx_complete;
3131
static volatile uint8_t soc_i2c_err_detect;
3232
static volatile uint32_t soc_i2c_err_source;
3333

34-
static volatile uint8_t soc_i2c_slave = 0;
34+
static volatile uint32_t soc_i2c_slave_address = 0;
3535

36-
static void soc_i2c_master_rx_callback(uint32_t dev_id) {
36+
static void soc_i2c_master_rx_callback(uint32_t dev_id)
37+
{
3738
soc_i2c_master_rx_complete = 1;
3839
}
3940

40-
static void soc_i2c_master_tx_callback(uint32_t dev_id) {
41+
static void soc_i2c_master_tx_callback(uint32_t dev_id)
42+
{
4143
soc_i2c_master_tx_complete = 1;
4244
}
4345

44-
static void soc_i2c_err_callback(uint32_t dev_id) {
46+
static void soc_i2c_err_callback(uint32_t dev_id)
47+
{
4548
soc_i2c_err_detect = 1;
4649
soc_i2c_err_source = dev_id;
4750
}
4851

4952
static void (*soc_i2c_slave_rx_user_callback)(int) = NULL;
5053
static void (*soc_i2c_slave_tx_user_callback)(void) = NULL;
5154

52-
static void soc_i2c_slave_rx_callback(uint32_t bytes) {
55+
static void soc_i2c_slave_rx_callback(uint32_t bytes)
56+
{
5357
if (soc_i2c_slave_rx_user_callback) {
5458
soc_i2c_slave_rx_user_callback((int)bytes);
5559
}
5660
}
5761

58-
static void soc_i2c_slave_tx_callback(uint32_t bytes) {
62+
static void soc_i2c_slave_tx_callback(uint32_t bytes)
63+
{
5964
if (soc_i2c_slave_tx_user_callback) {
6065
soc_i2c_slave_tx_user_callback();
6166
}
6267
}
6368

64-
void soc_i2c_slave_set_rx_user_callback(void (*onReceiveCallback)(int)) {
69+
void soc_i2c_slave_set_rx_user_callback(void (*onReceiveCallback)(int))
70+
{
6571
soc_i2c_slave_rx_user_callback = onReceiveCallback;
6672
}
6773

68-
void soc_i2c_slave_set_tx_user_callback(void (*onRequestCallback)(void)) {
74+
void soc_i2c_slave_set_tx_user_callback(void (*onRequestCallback)(void))
75+
{
6976
soc_i2c_slave_tx_user_callback = onRequestCallback;
7077
}
7178

72-
static int soc_i2c_master_wait_rx_or_err() {
79+
static int soc_i2c_master_wait_rx_or_err()
80+
{
7381
uint64_t timeout = TIMEOUT_MS * 200;
7482
while (timeout--) {
7583
if (soc_i2c_err_detect) {
@@ -89,7 +97,8 @@ static int soc_i2c_master_wait_rx_or_err() {
8997
return I2C_TIMEOUT;
9098
}
9199

92-
static int soc_i2c_master_wait_tx_or_err() {
100+
static int soc_i2c_master_wait_tx_or_err()
101+
{
93102
uint64_t timeout = TIMEOUT_MS * 200;
94103
while (timeout--) {
95104
if (soc_i2c_err_detect) {
@@ -110,11 +119,12 @@ static int soc_i2c_master_wait_tx_or_err() {
110119
}
111120

112121
static int soc_i2c_wait_dev_ready(SOC_I2C_CONTROLLER controller_id,
113-
bool no_stop) {
122+
bool no_stop)
123+
{
114124
uint64_t timeout = TIMEOUT_MS * 200;
115125
int ret = 0;
116126
while (timeout--) {
117-
ret = soc_i2c_status(controller_id);
127+
ret = soc_i2c_status(controller_id, no_stop);
118128
if (ret == I2C_OK) {
119129
return I2C_OK;
120130
}
@@ -125,8 +135,9 @@ static int soc_i2c_wait_dev_ready(SOC_I2C_CONTROLLER controller_id,
125135
return I2C_TIMEOUT - ret;
126136
}
127137

128-
int soc_i2c_openadapter(uint8_t address) {
129-
int ret;
138+
int soc_i2c_openadapter(uint32_t address, int i2c_speed, int i2c_addr_mode)
139+
{
140+
int ret = 0;
130141

131142
// use I2C0
132143
SET_PIN_MODE(20, I2C_MUX_MODE);
@@ -138,8 +149,8 @@ int soc_i2c_openadapter(uint8_t address) {
138149
i2c_cfg_data_t i2c_cfg;
139150
memset(&i2c_cfg, 0, sizeof(i2c_cfg_data_t));
140151

141-
i2c_cfg.speed = I2C_FAST;
142-
i2c_cfg.addressing_mode = I2C_7_Bit;
152+
i2c_cfg.speed = i2c_speed;
153+
i2c_cfg.addressing_mode = i2c_addr_mode;
143154
if (address) {
144155
i2c_cfg.mode_type = I2C_SLAVE;
145156
i2c_cfg.cb_tx = soc_i2c_slave_tx_callback;
@@ -159,35 +170,37 @@ int soc_i2c_openadapter(uint8_t address) {
159170

160171
soc_i2c_set_config(SOC_I2C_0, &i2c_cfg);
161172
soc_i2c_clock_enable(SOC_I2C_0);
173+
162174
ret = soc_i2c_wait_dev_ready(SOC_I2C_0, false);
163-
if (i2c_cfg.mode_type == I2C_SLAVE)
164-
{
165-
soc_i2c_slave_enable(SOC_I2C_0);
166-
}
167175

168176
return ret;
169177
}
170178

171-
void soc_i2c_setslave(uint8_t addr) {
172-
soc_i2c_slave = addr;
179+
void soc_i2c_master_set_slave_address(uint32_t addr)
180+
{
181+
soc_i2c_slave_address = addr;
173182
return;
174183
}
175184

176-
void soc_i2c_slave_set_rx_user_buffer(uint8_t *buffer, uint8_t length) {
185+
void soc_i2c_slave_set_rx_user_buffer(uint8_t *buffer, uint8_t length)
186+
{
177187
soc_i2c_slave_enable_rx(SOC_I2C_0, buffer, length);
178188
}
179189

180-
void soc_i2c_slave_set_tx_user_buffer(uint8_t *buffer, uint8_t length) {
190+
void soc_i2c_slave_set_tx_user_buffer(uint8_t *buffer, uint8_t length)
191+
{
181192
soc_i2c_slave_enable_tx(SOC_I2C_0, buffer, length);
182193
}
183194

184-
int soc_i2c_master_witebytes(uint8_t *bytes, uint8_t length, bool no_stop) {
195+
int soc_i2c_master_witebytes(uint8_t *bytes, uint8_t length, bool no_stop)
196+
{
185197
int ret;
186198

187199
soc_i2c_master_tx_complete = 0;
188200
soc_i2c_err_detect = 0;
189201
soc_i2c_err_source = 0;
190-
soc_i2c_transfer(SOC_I2C_0, bytes, length, 0, 0, soc_i2c_slave, no_stop);
202+
soc_i2c_master_transfer(SOC_I2C_0, bytes, length, 0, 0,
203+
soc_i2c_slave_address, no_stop);
191204
ret = soc_i2c_master_wait_tx_or_err();
192205
if (ret)
193206
return ret;
@@ -197,13 +210,15 @@ int soc_i2c_master_witebytes(uint8_t *bytes, uint8_t length, bool no_stop) {
197210
return length;
198211
}
199212

200-
int soc_i2c_master_readbytes(uint8_t *buf, int length, bool no_stop) {
213+
int soc_i2c_master_readbytes(uint8_t *buf, int length, bool no_stop)
214+
{
201215
int ret;
202216

203217
soc_i2c_master_rx_complete = 0;
204218
soc_i2c_err_detect = 0;
205219
soc_i2c_err_source = 0;
206-
soc_i2c_transfer(SOC_I2C_0, buf, length, 0, 0, soc_i2c_slave, no_stop);
220+
soc_i2c_master_transfer(SOC_I2C_0, 0, 0, buf, length, soc_i2c_slave_address,
221+
no_stop);
207222
ret = soc_i2c_master_wait_rx_or_err();
208223
if (ret)
209224
return ret;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Wire2 Master Wiret/Reader test Sketch
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire2 library
5+
// Reads data from an I2C/TWI slave device
6+
// Writes data to an I2C/TWI slave device
7+
// Refer to the "Wire2 Slave Sender/Receiver" example for use with this
8+
9+
// Created 29 March 2006
10+
11+
// This example code is in the public domain.
12+
13+
/**
14+
* the sent data(8 bits): the higher 4 bits are equal to count and count will increase by 1 in every loop
15+
* when master is as writer, data are got from buffer_sender
16+
* when master is as reader, the received data are stored in buffer_receiver
17+
* data checking is to verify whether the buffer_sender is equal to buffer_receiver
18+
**/
19+
#include <Wire2.h>
20+
21+
#define BUFFER_SIZE 32
22+
23+
static int count = 0; // recode the higher 4 bits of data
24+
static uint8_t buffer_sender[BUFFER_SIZE]; // data source for master writer
25+
static uint8_t buffer_receiver[BUFFER_SIZE]; // data distination for master reader
26+
27+
void setup()
28+
{
29+
Wire2.begin(); // join i2c bus (address optional for master)
30+
Serial.begin(115200); // start serial for output
31+
while (Serial)
32+
;
33+
}
34+
35+
void loop()
36+
{
37+
int ret = 0;
38+
int bytesWritten = 0;
39+
40+
count++;
41+
42+
Wire2.beginTransmission(8); // transmit to device #8
43+
44+
for (int i = 0; i < BUFFER_SIZE; i++)
45+
{
46+
buffer_sender[i] = i;
47+
}
48+
bytesWritten = Wire2.write(buffer_sender, BUFFER_SIZE);
49+
50+
ret = Wire2.endTransmission(); // stop transmitting
51+
if (ret != 0)
52+
{
53+
Serial.print("master write failed with error ");
54+
Serial.println(ret);
55+
}
56+
else
57+
{
58+
Serial.print("master sucessfull write ");
59+
Serial.print(bytesWritten);
60+
Serial.println(" bytes");
61+
}
62+
63+
ret = Wire2.requestFrom(8, bytesWritten, true); // request BUFFER_SIZE bytes from slave device #8
64+
if (ret == 0)
65+
{
66+
Serial.println("master read failed");
67+
}
68+
else
69+
{
70+
Serial.print("master sucessfull read ");
71+
Serial.print(ret);
72+
Serial.println(" bytes");
73+
}
74+
75+
int k = 0;
76+
while (Wire2.available()) // slave may send less than requested
77+
{
78+
buffer_receiver[k] = Wire2.read();
79+
k++;
80+
}
81+
82+
// check data: the received data should be equal to the sent data
83+
for(int i = 0; i < bytesWritten; i++)
84+
{
85+
if(buffer_sender[i] == buffer_receiver[i])
86+
{
87+
/*Serial.println("OK");*/
88+
}
89+
else
90+
{
91+
Serial.print(buffer_sender[i],HEX);
92+
Serial.print(" != ");
93+
Serial.println(buffer_receiver[i],HEX);
94+
}
95+
}
96+
97+
Serial.print("+++++");
98+
Serial.print(count);
99+
Serial.println("+++++");
100+
delay(1000);
101+
102+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Wire Master Reader
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire library
5+
// Reads data from an I2C/TWI slave device
6+
// Refer to the "Wire Slave Sender" example for use with this
7+
8+
// Created 29 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire2.h>
14+
15+
void setup()
16+
{
17+
Serial.begin(115200); // start serial for output
18+
while(Serial);
19+
20+
Wire2.begin(); // join i2c bus (address optional for master)
21+
}
22+
23+
void loop()
24+
{
25+
26+
int ret = Wire2.requestFrom(8, 20, 1); // request 20 bytes from slave device #8
27+
if (ret == 0)
28+
{
29+
Serial.println("read from slave device failed");
30+
}
31+
32+
while (Wire2.available()) // slave may send less than requested
33+
{
34+
char c = Wire2.read(); // receive a byte as character
35+
Serial.println(c, HEX);
36+
}
37+
delay(1000);
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Wire Master Writer
2+
// by Nicholas Zambetti <http://www.zambetti.com>
3+
4+
// Demonstrates use of the Wire library
5+
// Writes data to an I2C/TWI slave device
6+
// Refer to the "Wire Slave Receiver" example for use with this
7+
8+
// Created 29 March 2006
9+
10+
// This example code is in the public domain.
11+
12+
13+
#include <Wire2.h>
14+
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
while(Serial);
19+
20+
Wire2.begin(); // join i2c bus (address optional for master)
21+
}
22+
23+
byte x = 1;
24+
byte rdata;
25+
void loop()
26+
{
27+
Wire2.beginTransmission(8); // transmit to device #8
28+
Wire2.write(x); // sends one byte
29+
int result = Wire2.endTransmission(); // stop transmitting
30+
if (result == 0)
31+
{
32+
Serial.print("x = ");
33+
Serial.println(x);
34+
}
35+
else
36+
{
37+
Serial.print("transmit failed with error code ");
38+
Serial.println(result);
39+
}
40+
x++;
41+
delay(500);
42+
}

0 commit comments

Comments
 (0)