-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
156 lines (121 loc) · 3.39 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/i2c.h"
#include "pico/i2c_slave.h"
#include "i2c_peripheral/i2c_peripheral.h"
void blink_setup() {
gpio_init(22);
gpio_set_dir(22, GPIO_OUT);
}
void blink_loop() {
static bool pin_status = true;
gpio_put(22, pin_status);
pin_status ^= true;
}
void button_setup() {
gpio_init(21);
gpio_set_dir(21, GPIO_IN);
}
#define I2C_MASTER_SDA 16
#define I2C_MASTER_SCL 17
/**
* Sets up I2C controller 0 as master
* on pins GPIO16 and GPIO17 (SDA, SCL)
*/
void i2c_master_setup() {
gpio_init(I2C_MASTER_SDA);
gpio_init(I2C_MASTER_SCL);
gpio_set_function(I2C_MASTER_SDA, GPIO_FUNC_I2C);
gpio_set_function(I2C_MASTER_SCL, GPIO_FUNC_I2C);
gpio_pull_up(I2C_MASTER_SDA);
gpio_pull_up(I2C_MASTER_SCL);
i2c_init(i2c0, 100000);
}
#define I2C_SLAVE_SDA 14
#define I2C_SLAVE_SCL 15
#define SLAVE_ADDR 0xa0
static uint8_t i2c_slave_mempool[256];
static int mempool_index = 0;
static bool data_available = false;
static bool data_requested = false;
static char *slave_response = "Hello world\n";
#define RESPONSE_LEN 12
static int slave_resp_index = 0;
/**
* Process an I2C slave interrupt
*/
static void i2c_slave_request_handler(i2c_inst_t *i2c, i2c_slave_event_t evt) {
switch(evt) {
case I2C_SLAVE_RECEIVE:
if(mempool_index < 256) {
i2c_slave_mempool[mempool_index++] = i2c_read_byte_raw(i2c);
data_available = true;
}
break;
case I2C_SLAVE_REQUEST:
i2c_write_byte_raw(i2c, slave_response[slave_resp_index++]);
if(slave_resp_index == RESPONSE_LEN)
slave_resp_index = 0;
data_requested = true;
break;
case I2C_SLAVE_FINISH:
break;
default:
break;
}
}
/**
* Sets up I2C controller 1 as slave
* on pins GPIO14 and GPIO15 (SDA, SCL)
*/
void i2c_slave_setup() {
gpio_init(I2C_SLAVE_SDA);
gpio_init(I2C_SLAVE_SCL);
gpio_set_function(I2C_SLAVE_SDA, GPIO_FUNC_I2C);
gpio_set_function(I2C_SLAVE_SCL, GPIO_FUNC_I2C);
gpio_pull_up(I2C_SLAVE_SDA);
gpio_pull_up(I2C_SLAVE_SCL);
i2c_init(i2c1, 100000);
i2c_slave_init(i2c1, SLAVE_ADDR, &i2c_slave_request_handler);
}
int main() {
stdio_init_all();
i2c_master_setup();
i2c_peripheral_config_t conf = {
.sda_pin = I2C_SLAVE_SDA,
.scl_pin = I2C_SLAVE_SCL,
.speed = 100000,
.use_internal_pullup = true,
.controller = i2c1,
.address = SLAVE_ADDR
};
i2c_peripheral_setup(&conf);
int master_readbytes = 0;
char read_string[100];
int mode = 0;
int i = 0;
while(true) {
i2c_peripheral_loop();
if(mode == 1) {
master_readbytes = i2c_read_blocking(i2c0, SLAVE_ADDR, read_string, 1, false);
if(master_readbytes > 0) {
read_string[master_readbytes] = 0;
printf("%x", read_string[0]);
} else {
printf("Read return code: %d\n", master_readbytes);
}
mode = 0;
} else {
i2c_write_blocking(i2c0, SLAVE_ADDR, (uint8_t *)&i, 1, false);
i++;
if(i == 256)
i=0;
mode = 1;
}
sleep_ms(100);
}
return 0;
}