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

@ -4,12 +4,19 @@
#include <math.h>
#include <sys/ioctl.h>
#define I2CBUSNR 7
static bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr);
static void i2c_scanner(struct mgos_i2c *i2c, bool dumpregs);
static int usage(void);
bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr);
void i2c_scanner(struct mgos_i2c *i2c, bool dumpregs);
static int usage(void) {
printf("Flags: [-i <i2cnusnr>] [-s] [-d]\n");
printf(" -i: Select the I2C bus number, ie '7' for /dev/i2c-7\n");
printf(" -s: Scan the I2C bus upon startup\n");
printf(" -d: Dump byte-registers on each found I2C device\n");
return -1;
}
void i2c_scanner(struct mgos_i2c *i2c, bool dumpregs) {
static void i2c_scanner(struct mgos_i2c *i2c, bool dumpregs) {
int i;
if (!i2c) {
@ -17,17 +24,19 @@ void i2c_scanner(struct mgos_i2c *i2c, bool dumpregs) {
return;
}
for (i = 0x3; i < 0x77; i++) {
for (i = 0x3; i < 0x78; i++) {
bool ret;
ret = mgos_i2c_read(i2c, i, NULL, 0, true);
if (ret) {
LOG(LL_INFO, ("I2C Address 0x%02x %s", i, ret ? "true" : "false"));
if (dumpregs) i2c_dumpregs(i2c, i);
LOG(LL_INFO, ("Found device at I2C address 0x%02x", i));
if (dumpregs) {
i2c_dumpregs(i2c, i);
}
}
}
}
bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
static bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
uint16_t reg;
int value;
@ -47,24 +56,69 @@ bool i2c_dumpregs(struct mgos_i2c *i2c, uint8_t i2caddr) {
}
int main(int argc, char **argv, char **environ) {
struct mgos_i2c * i2c = NULL;
int scan = 0;
int dump = 0;
int i2cbus = 1;
int index;
int c;
if (!mgos_i2c_open(I2CBUSNR)) {
LOG(LL_ERROR, ("Cannot open I2C bus %u", I2CBUSNR));
opterr = 0;
while ((c = getopt(argc, argv, "i:sdh")) != -1) {
switch (c) {
case 's':
scan = 1;
break;
case 'd':
dump = 1;
break;
case 'i':
i2cbus = atoi(optarg);
break;
case '?':
if (optopt == 'i') {
LOG(LL_ERROR, ("Option -%c requires an argument", optopt));
} else if (isprint(optopt)) {
LOG(LL_ERROR, ("Unknown option `-%c'", optopt));
} else{
LOG(LL_ERROR, ("Unknown option character `\\x%x'", optopt));
}
return usage();
default:
return usage();
}
}
for (index = optind; index < argc; index++) {
LOG(LL_WARN, ("Non-option argument %s, skipping", argv[index]));
}
struct mgos_i2c *i2c = NULL;
if (!mgos_i2c_open(i2cbus)) {
LOG(LL_ERROR, ("Cannot open I2C bus %u", i2cbus));
return -1;
}
if (!(i2c = mgos_i2c_get_global())) {
LOG(LL_ERROR, ("Cannot open I2C bus"));
return -2;
}
i2c_scanner(i2c, true);
if (scan) {
i2c_scanner(i2c, dump);
}
tests_create();
tests_run();
tests_destroy();
return 0;
(void)argc;
(void)argv;
(void)environ;

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;

View File

@ -23,91 +23,97 @@
int _mgos_timers = 0;
int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
char ll_str[6];
char ll_file[31];
char ll_func[41];
size_t offset=0;
char ll_str[6];
char ll_file[31];
char ll_func[41];
size_t offset = 0;
switch(l) {
case LL_ERROR:
strncpy(ll_str, "ERROR", sizeof(ll_str));
break;
case LL_WARN:
strncpy(ll_str, "WARN", sizeof(ll_str));
break;
case LL_INFO:
strncpy(ll_str, "INFO", sizeof(ll_str));
break;
case LL_DEBUG:
strncpy(ll_str, "DEBUG", sizeof(ll_str));
break;
case LL_VERBOSE_DEBUG:
strncpy(ll_str, "VERB", sizeof(ll_str));
break;
default: // LL_NONE
return 0;
switch (l) {
case LL_ERROR:
strncpy(ll_str, "ERROR", sizeof(ll_str));
break;
case LL_WARN:
strncpy(ll_str, "WARN", sizeof(ll_str));
break;
case LL_INFO:
strncpy(ll_str, "INFO", sizeof(ll_str));
break;
case LL_DEBUG:
strncpy(ll_str, "DEBUG", sizeof(ll_str));
break;
case LL_VERBOSE_DEBUG:
strncpy(ll_str, "VERB", sizeof(ll_str));
break;
default: // LL_NONE
return 0;
}
offset=0;
offset = 0;
memset(ll_file, 0, sizeof(ll_file));
if (strlen(file) >= sizeof(ll_file))
offset=strlen(file)-sizeof(ll_file)+1;
strncpy(ll_file, file+offset, sizeof(ll_file)-1);
if (strlen(file) >= sizeof(ll_file)) {
offset = strlen(file) - sizeof(ll_file) + 1;
}
strncpy(ll_file, file + offset, sizeof(ll_file) - 1);
offset=0;
offset = 0;
memset(ll_func, 0, sizeof(ll_func));
if (strlen(func) >= sizeof(ll_func))
offset=strlen(func)-sizeof(ll_func)+1;
strncpy(ll_func, func+offset, sizeof(ll_func)-1);
if (strlen(func) >= sizeof(ll_func)) {
offset = strlen(func) - sizeof(ll_func) + 1;
}
strncpy(ll_func, func + offset, sizeof(ll_func) - 1);
printf ("%-5s %-30s %-40s| ", ll_str, ll_file, ll_func);
printf("%-5s %-30s %-40s| ", ll_str, ll_file, ll_func);
return 1;
}
double mg_time(void) {
struct timespec ts;
double ret;
double ret;
clock_gettime(CLOCK_REALTIME_COARSE, &ts);
ret = (double) ts.tv_sec;
ret += ((double)ts.tv_nsec)/1000000000;
ret = (double)ts.tv_sec;
ret += ((double)ts.tv_nsec) / 1000000000;
return ret;
}
void mgos_usleep(uint32_t usecs) {
usleep(usecs);
}
bool mgos_gpio_set_int_handler(int pin, enum mgos_gpio_int_mode mode, mgos_gpio_int_handler_f cb, void *arg) {
LOG(LL_INFO, ("Not implemented."));
return true;
(void)pin;
(void)mode;
(void)cb;
(void)arg;
}
bool mgos_gpio_enable_int(int pin) {
LOG(LL_INFO, ("Not implemented."));
return true;
(void)pin;
}
bool mgos_gpio_disable_int(int pin) {
LOG(LL_INFO, ("Not implemented."));
return true;
(void)pin;
}
void mgos_gpio_clear_int(int pin) {
LOG(LL_INFO, ("Not implemented."));
return;
(void)pin;
}