Fonctionnel
This commit is contained in:
parent
80593efd46
commit
693446dfe0
22
main.c
22
main.c
@ -11,14 +11,16 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static const uint I2C_SLAVE_ADDRESS = 0x17;
|
static const uint I2C_SLAVE_ADDRESS = 0x09;
|
||||||
static const uint I2C_BAUDRATE = 100000; // 100 kHz
|
static const uint I2C_BAUDRATE = 100000; // 100 kHz
|
||||||
|
|
||||||
|
uint reception;
|
||||||
|
|
||||||
#ifdef i2c_default
|
#ifdef i2c_default
|
||||||
// For this example, we run both the master and slave from the same board.
|
// For this example, we run both the master and slave from the same board.
|
||||||
// You'll need to wire pin GP4 to GP6 (SDA), and pin GP5 to GP7 (SCL).
|
// You'll need to wire pin GP4 to GP6 (SDA), and pin GP5 to GP7 (SCL).
|
||||||
static const uint I2C_SLAVE_SDA_PIN = 19; // 4
|
static const uint I2C_SLAVE_SDA_PIN = 17; // 17
|
||||||
static const uint I2C_SLAVE_SCL_PIN = 18; // 5
|
static const uint I2C_SLAVE_SCL_PIN = 16; // 16
|
||||||
static const uint I2C_MASTER_SDA_PIN = 6;
|
static const uint I2C_MASTER_SDA_PIN = 6;
|
||||||
static const uint I2C_MASTER_SCL_PIN = 7;
|
static const uint I2C_MASTER_SCL_PIN = 7;
|
||||||
|
|
||||||
@ -55,6 +57,7 @@ static void i2c_slave_handler(i2c_inst_t *i2c, i2c_slave_event_t event) {
|
|||||||
break;
|
break;
|
||||||
case I2C_SLAVE_FINISH: // master has signalled Stop / Restart
|
case I2C_SLAVE_FINISH: // master has signalled Stop / Restart
|
||||||
context.mem_address_written = false;
|
context.mem_address_written = false;
|
||||||
|
reception++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -70,9 +73,9 @@ static void setup_slave() {
|
|||||||
gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_I2C);
|
gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_I2C);
|
||||||
gpio_pull_up(I2C_SLAVE_SCL_PIN);
|
gpio_pull_up(I2C_SLAVE_SCL_PIN);
|
||||||
|
|
||||||
i2c_init(i2c1, I2C_BAUDRATE);
|
i2c_init(i2c0, I2C_BAUDRATE);
|
||||||
// configure I2C1 for slave mode
|
// configure I2C1 for slave mode
|
||||||
i2c_slave_init(i2c1, I2C_SLAVE_ADDRESS, &i2c_slave_handler);
|
i2c_slave_init(i2c0, I2C_SLAVE_ADDRESS, &i2c_slave_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -90,8 +93,13 @@ int main() {
|
|||||||
setup_slave();
|
setup_slave();
|
||||||
//run_master();
|
//run_master();
|
||||||
while(1){
|
while(1){
|
||||||
puts("\nI2C slave example\n");
|
if(reception != 0){
|
||||||
sleep_ms(1000);
|
puts("reception != 0\n");
|
||||||
|
reception = 0;
|
||||||
|
printf(">a:%d\n", context.mem[0]);
|
||||||
|
}
|
||||||
|
puts("I2C slave example\n");
|
||||||
|
sleep_ms(10);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user