lpc176x: Convert i2c code to use standard i2ccmds.c

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-11-14 10:23:56 -05:00
parent f973886dac
commit f3c4deb1ab
4 changed files with 38 additions and 13 deletions

View File

@ -10,8 +10,9 @@ WiperRegisters = [0x00, 0x01, 0x06, 0x07]
class mcp4451:
def __init__(self, config):
printer = config.get_printer()
# Read config parameters
self.mcu = mcu.get_printer_mcu(printer, config.get('mcu', 'mcu'))
self.i2c_addr = config.getint('i2c_address')
i2c_addr = config.getint('i2c_address')
scale = config.getfloat('scale', 1., above=0.)
wipers = [None]*4
for i in range(len(wipers)):
@ -19,14 +20,20 @@ class mcp4451:
minval=0., maxval=scale)
if val is not None:
wipers[i] = int(val * 255. / scale + .5)
# Setup i2c
self.oid = self.mcu.create_oid()
self.mcu.add_config_cmd(
"config_i2c oid=%d bus=0 rate=100000 addr=%d" % (
self.oid, i2c_addr))
# Configure registers
self.add_config_cmd(0x04, 0xff)
self.add_config_cmd(0x0a, 0xff)
for reg, val in zip(WiperRegisters, wipers):
if val is not None:
self.add_config_cmd(reg, val)
def add_config_cmd(self, reg, val):
self.mcu.add_config_cmd("i2c_send data=%02x%02x%02x" % (
self.i2c_addr, (reg << 4) | ((val >> 8) & 0x03), val), is_init=True)
self.mcu.add_config_cmd("i2c_write oid=%d data=%02x%02x" % (
self.oid, (reg << 4) | ((val >> 8) & 0x03), val), is_init=True)
def load_config_prefix(config):
return mcp4451(config)

View File

@ -7,6 +7,7 @@ config LPC_SELECT
default y
select HAVE_GPIO
select HAVE_GPIO_ADC
select HAVE_GPIO_I2C
select HAVE_GPIO_SPI
select HAVE_GPIO_BITBANGING

View File

@ -37,4 +37,13 @@ void spi_prepare(struct spi_config config);
void spi_transfer(struct spi_config config, uint8_t receive_data
, uint8_t len, uint8_t *data);
struct i2c_config {
uint8_t addr;
};
struct i2c_config i2c_setup(uint32_t bus, uint32_t rate, uint8_t addr);
void i2c_write(struct i2c_config config, uint8_t write_len, uint8_t *write);
void i2c_read(struct i2c_config config, uint8_t reg_len, uint8_t *reg
, uint8_t read_len, uint8_t *read);
#endif // gpio.h

View File

@ -7,6 +7,7 @@
#include "LPC17xx.h" // LPC_I2C1
#include "board/misc.h" // timer_is_before
#include "command.h" // DECL_COMMAND
#include "gpio.h" // i2c_setup
#include "internal.h" // gpio_peripheral
#include "sched.h" // sched_shutdown
@ -37,6 +38,15 @@ i2c_init(void)
LPC_I2C1->I2CONSET = IF_ENA;
}
struct i2c_config
i2c_setup(uint32_t bus, uint32_t rate, uint8_t addr)
{
if (bus)
shutdown("Unsupported i2c bus");
i2c_init();
return (struct i2c_config){ .addr=addr };
}
static void
i2c_wait(uint32_t bit, uint32_t timeout)
{
@ -78,23 +88,21 @@ i2c_stop(uint32_t timeout)
i2c_wait(IF_STOP, timeout);
}
static void
i2c_send(uint8_t *data, int data_len)
void
i2c_write(struct i2c_config config, uint8_t write_len, uint8_t *write)
{
i2c_init();
uint32_t timeout = timer_read_time() + timer_from_us(5000);
i2c_start(timeout);
while (data_len--)
i2c_send_byte(*data++, timeout);
i2c_send_byte(config.addr, timeout);
while (write_len--)
i2c_send_byte(*write++, timeout);
i2c_stop(timeout);
}
// This provides just enough functionality to program an MCP4451 chip
void
command_i2c_send(uint32_t *args)
i2c_read(struct i2c_config config, uint8_t reg_len, uint8_t *reg
, uint8_t read_len, uint8_t *read)
{
uint8_t data_len = args[0], *data = (void*)(size_t)args[1];
i2c_send(data, data_len);
shutdown("i2c_read not supported on lpc176x");
}
DECL_COMMAND(command_i2c_send, "i2c_send data=%*s");