Add flags and usage() to main. Uncrustify

This commit is contained in:
Pim van Pelt
2019-01-03 21:28:32 +01:00
parent cb48b78265
commit cafecbb897
3 changed files with 167 additions and 107 deletions

View File

@@ -28,35 +28,35 @@
#include <unistd.h>
#include <errno.h>
struct mgos_i2c
{
int fd;
struct mgos_i2c {
int fd;
uint16_t read_timeout_ms; // in msec
char *filename;
char * filename;
};
static struct mgos_i2c *s_global_i2c_bus = NULL;
static size_t i2c_read_timeout(struct mgos_i2c *i2c, void *data, size_t len) {
uint16_t tries;
size_t ret=-1;
size_t ret = -1;
if (!i2c) return -1;
if (!i2c) {
return -1;
}
ret = read(i2c->fd, data, len);
for(tries=i2c->read_timeout_ms; tries && ret!=len && len>0; tries--) {
for (tries = i2c->read_timeout_ms; tries && ret != len && len > 0; tries--) {
usleep(1000);
ret = read(i2c->fd, data, len);
}
if (ret!=len) {
if (ret != len) {
// LOG(LL_ERROR, ("Read timeout on I2C"));
return -1;
}
return len;
}
bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len, bool stop) {
size_t ret;
@@ -65,7 +65,7 @@ bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len,
return false;
}
if (ioctl(i2c->fd,I2C_SLAVE,addr) < 0) {
if (ioctl(i2c->fd, I2C_SLAVE, addr) < 0) {
LOG(LL_ERROR, ("Cannot select slave 0x%04x on I2C bus", addr));
return false;
}
@@ -75,10 +75,10 @@ bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len,
return false;
}
return true;
(void)stop;
}
bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_t len, bool stop) {
size_t ret;
@@ -87,7 +87,7 @@ bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_
return false;
}
if (ioctl(i2c->fd,I2C_SLAVE,addr) < 0) {
if (ioctl(i2c->fd, I2C_SLAVE, addr) < 0) {
LOG(LL_ERROR, ("Cannot select slave 0x%04x on I2C bus", addr));
return false;
}
@@ -97,69 +97,72 @@ bool mgos_i2c_write(struct mgos_i2c *i2c, uint16_t addr, const void *data, size_
return false;
}
return true;
(void)stop;
}
void mgos_i2c_stop(struct mgos_i2c *i2c) {
return;
(void)i2c;
}
int mgos_i2c_get_freq(struct mgos_i2c *i2c) {
return MGOS_I2C_FREQ_100KHZ;
(void)i2c;
}
bool mgos_i2c_set_freq(struct mgos_i2c *i2c, int freq) {
if (freq==MGOS_I2C_FREQ_100KHZ) return true;
if (freq == MGOS_I2C_FREQ_100KHZ) {
return true;
}
return false;
(void)i2c;
}
int mgos_i2c_read_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) {
uint8_t value;
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 1, &value))
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 1, &value)) {
return -1;
}
return value;
}
int mgos_i2c_read_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg) {
uint16_t value;
uint8_t data[2];
uint8_t data[2];
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 2, data))
if (!mgos_i2c_read_reg_n(i2c, addr, reg, 2, data)) {
return -1;
value=(data[0]<<8)+data[1];
}
value = (data[0] << 8) + data[1];
return value;
}
bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_t n, uint8_t *buf) {
uint8_t outbuf;
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
struct i2c_msg messages[2];
if (!i2c) {
LOG(LL_ERROR, ("No I2C bus"));
return -1;
}
if (ioctl(i2c->fd,I2C_SLAVE,addr) < 0) {
if (ioctl(i2c->fd, I2C_SLAVE, addr) < 0) {
LOG(LL_ERROR, ("Cannot select slave 0x%04x on I2C bus: %s", addr, strerror(errno)));
return -1;
}
/*
* In order to read a register, we first do a "dummy write" by writing
* 0 bytes to the register we want to read from. This is similar to
* the packet in set_i2c_register, except it's 1 byte rather than 2.
*/
outbuf = reg;
outbuf = reg;
messages[0].addr = addr;
messages[0].flags = 0;
messages[0].len = sizeof(outbuf);
@@ -167,85 +170,82 @@ bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_
/* The data will get returned in this structure */
messages[1].addr = addr;
messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
messages[1].flags = I2C_M_RD /* | I2C_M_NOSTART*/;
messages[1].len = n;
messages[1].buf = buf;
/* Send the request to the kernel and get the result back */
packets.msgs = messages;
packets.nmsgs = 2;
if(ioctl(i2c->fd, I2C_RDWR, &packets) < 0)
packets.msgs = messages;
packets.nmsgs = 2;
if (ioctl(i2c->fd, I2C_RDWR, &packets) < 0) {
return false;
}
return true;
}
bool mgos_i2c_write_reg_w(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint16_t value) {
uint8_t data[2];
data[0]=value>>8;
data[1]=value&0xFF;
data[0] = value >> 8;
data[1] = value & 0xFF;
return mgos_i2c_write_reg_n(i2c, addr, reg, 2, data);
}
bool mgos_i2c_write_reg_b(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, uint8_t value) {
return mgos_i2c_write_reg_n(i2c, addr, reg, 1, &value);
}
bool mgos_i2c_write_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_t n, const uint8_t *buf) {
unsigned char outbuf[n+1];
unsigned char outbuf[n + 1];
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[1];
struct i2c_msg messages[1];
if (!i2c) {
LOG(LL_ERROR, ("No I2C bus"));
return false;
}
if (ioctl(i2c->fd,I2C_SLAVE,addr) < 0) {
if (ioctl(i2c->fd, I2C_SLAVE, addr) < 0) {
LOG(LL_ERROR, ("Cannot select slave 0x%04x on I2C bus: %s", addr, strerror(errno)));
return false;
}
messages[0].addr=addr;
messages[0].flags=0;
messages[0].len=sizeof(outbuf);
messages[0].buf=outbuf;
messages[0].addr = addr;
messages[0].flags = 0;
messages[0].len = sizeof(outbuf);
messages[0].buf = outbuf;
outbuf[0]=reg;
memcpy(outbuf+1, buf, n);
outbuf[0] = reg;
memcpy(outbuf + 1, buf, n);
packets.msgs = messages;
packets.nmsgs = 1;
if(ioctl(i2c->fd, I2C_RDWR, &packets) < 0) {
if (ioctl(i2c->fd, I2C_RDWR, &packets) < 0) {
return false;
}
return true;
}
void mgos_i2c_close(struct mgos_i2c *i2c) {
return;
(void)i2c;
}
struct mgos_i2c *mgos_i2c_get_global(void) {
return s_global_i2c_bus;
}
// User provided function to interface with Linux I2C driver
bool mgos_i2c_open(int busnr) {
int fd;
char filename[20];
int fd;
char filename[20];
struct mgos_i2c *i2c;
sprintf(filename,"/dev/i2c-%d",busnr);
if ((fd = open(filename,O_RDWR)) < 0) {
sprintf(filename, "/dev/i2c-%d", busnr);
if ((fd = open(filename, O_RDWR)) < 0) {
LOG(LL_ERROR, ("Could not open %s", filename));
return false;
}
@@ -254,10 +254,10 @@ bool mgos_i2c_open(int busnr) {
LOG(LL_ERROR, ("Could not allocate mgos_i2c handle for %s", filename));
return false;
}
i2c->fd=fd;
i2c->filename=strdup(filename);
i2c->read_timeout_ms=1000;
s_global_i2c_bus=i2c;
i2c->fd = fd;
i2c->filename = strdup(filename);
i2c->read_timeout_ms = 1000;
s_global_i2c_bus = i2c;
LOG(LL_INFO, ("Opened I2C bus on %s", filename));
return true;