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

Rename OC i2c files to prevent confusion with micropython files

parent e37fc457
No related branches found
No related tags found
No related merge requests found
......@@ -11,10 +11,15 @@ 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
${CMAKE_CURRENT_LIST_DIR}/oc_i2c_master.h
${CMAKE_CURRENT_LIST_DIR}/oc_i2c_master.c
${CMAKE_CURRENT_LIST_DIR}/oc_i2c_slave.h
${CMAKE_CURRENT_LIST_DIR}/oc_i2c_slave.c
)
target_link_libraries(usermod_opencube_lib INTERFACE
usermod_opencube_pindefs
pico_i2c_slave
)
# Add the current directory as an include directory.
......
#include "hardware/i2c.h"
#include "PCF8575.h"
#include <opencube_hw.h>
#include "oc_i2c_master.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);
}
\ 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 <pico/stdlib.h>
#include <opencube_hw.h>
#include "oc_i2c_slave.h"
#include "PCF8575.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
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