Skip to content
Snippets Groups Projects
Commit e37fc457 authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Add initial files for i2c master and slave

parent fa02ecc0
No related branches found
No related tags found
No related merge requests found
Pipeline #124121 canceled
......@@ -187,6 +187,7 @@ class Sensors:
if sensor_type is not None:
if sensor_type == Sensor.NXT_ULTRASONIC:
if isinstance(self.ultra_nxt, EmptyObject):
self.buttons.set_pin(I2C_STRONG_PULL_RPIN_BIT, 1)
self.ultra_nxt = nxt.UltrasonicSensor(periodic = True, use_nonblocking=False)
elif sensor_type == Sensor.GYRO_ACC:
if isinstance(self.gyro_acc, EmptyObject):
......
#include "hardware/i2c.h"
#include "PCF8575.h"
#include <opencube_hw.h>
void opencube_i2c_master_init() {
PCF8575_init();
PCF8575_write_pin(I2C_STRONG_PULL_RPIN, false); // activate external pull-ups
gpio_init(UTZ_I2C_SDA_PIN);
gpio_set_function(UTZ_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_init(UTZ_I2C_SCK_PIN);
gpio_set_function(UTZ_I2C_SCK_PIN, GPIO_FUNC_I2C);
i2c_init(UTZ_I2C_BUS, MULTICUBE_I2C_BAUD_RATE);
}
// I2C reserves some addresses for special purposes. We exclude these from the scan.
// These are any addresses of the form 000 0xxx or 111 1xxx
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
\ No newline at end of file
/**
* Library for I2C master multicube communication
*/
#ifndef OPENCUBE_I2C_MASTER_H
#define OPENCUBE_I2C_MASTER_H
#endif //OPENCUBE_I2C_MASTER_H
\ No newline at end of file
#include <hardware/i2c.h>
#include "pico/i2c_slave.h"
#include <opencube_hw.h>
// The slave implements a 256 byte memory. To write a series of bytes, the master first
// writes the memory address, followed by the data. The address is automatically incremented
// for each byte transferred, looping back to 0 upon reaching the end. Reading is done
// sequentially from the current memory address.
static struct
{
uint8_t mem[256];
uint8_t mem_address;
bool mem_address_written;
} context;
static void opencube_i2c_slave_handler(i2c_inst_t *i2c, i2c_slave_event_t event);
// Our handler is called from the I2C ISR, so it must complete quickly. Blocking calls /
// printing to stdio may interfere with interrupt handling.
static void opencube_i2c_slave_handler(i2c_inst_t *i2c, i2c_slave_event_t event) {
switch (event) {
case I2C_SLAVE_RECEIVE: // master has written some data
if (!context.mem_address_written) {
// writes always start with the memory address
context.mem_address = i2c_read_byte_raw(i2c);
context.mem_address_written = true;
} else {
// save into memory
context.mem[context.mem_address] = i2c_read_byte_raw(i2c);
context.mem_address++;
}
break;
case I2C_SLAVE_REQUEST: // master is requesting data
// load from memory
i2c_write_byte_raw(i2c, context.mem[context.mem_address]);
context.mem_address++;
break;
case I2C_SLAVE_FINISH: // master has signalled Stop / Restart
context.mem_address_written = false;
break;
default:
break;
}
}
void opencube_i2c_slave_init(uint8_t address) {
PCF8575_init();
PCF8575_write_pin(I2C_STRONG_PULL_RPIN, true)
gpio_init(UTZ_I2C_SDA_PIN);
gpio_set_function(UTZ_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_init(UTZ_I2C_SCK_PIN);
gpio_set_function(UTZ_I2C_SCK_PIN, GPIO_FUNC_I2C);
i2c_init(UTZ_I2C_BUS, MULTICUBE_I2C_BAUD_RATE);
// configure for slave mode
i2c_slave_init(i2c0, address, &opencube_i2c_slave_handler);
}
// I2C reserves some addresses for special purposes. We exclude these from the scan.
// These are any addresses of the form 000 0xxx or 111 1xxx
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
\ No newline at end of file
/**
* Library for I2C slave multicube communication
*/
#ifndef OPENCUBE_I2C_SLAVE_H
#define OPENCUBE_I2C_SLAVE_H
#endif //OPENCUBE_I2C_SLAVE_H
\ No newline at end of file
......@@ -11,6 +11,10 @@ target_sources(usermod_opencube_lib INTERFACE
${CMAKE_CURRENT_LIST_DIR}/PCF8575.c
${CMAKE_CURRENT_LIST_DIR}/ICM42688.h
${CMAKE_CURRENT_LIST_DIR}/ICM42688.c
${CMAKE_CURRENT_LIST_DIR}/i2c_master.h
${CMAKE_CURRENT_LIST_DIR}/i2c_master.c
${CMAKE_CURRENT_LIST_DIR}/i2c_slave.h
${CMAKE_CURRENT_LIST_DIR}/i2c_slave.c
)
# Add the current directory as an include directory.
......
......@@ -18,6 +18,13 @@
#define ICM42688_I2C_ADDR 0x68
#define ADS1119_I2C_ADDR 0x40 // 1000000 (A0+A1=GND)
// ==== I2C bus for external sensors ====
#define UTZ_I2C_SDA_PIN = 16
#define UTZ_I2C_SCK_PIN = 17
#define UTZ_I2C_BUS i2c0
#define UTZ_I2C_BAUD_RATE 30000
#define MULTICUBE_I2C_BAUD_RATE 100000
// ==== Misc pins ====
#define TURN_OFF_PIN 26
#define VBATT_ADC_PIN 27
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment