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) {
int scan = 0;
int dump = 0;
int i2cbus = 1;
int index;
int c;
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(I2CBUSNR)) {
LOG(LL_ERROR, ("Cannot open I2C bus %u", I2CBUSNR));
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,8 +28,7 @@
#include <unistd.h>
#include <errno.h>
struct mgos_i2c
{
struct mgos_i2c {
int fd;
uint16_t read_timeout_ms; // in msec
char * filename;
@ -41,7 +40,9 @@ static size_t i2c_read_timeout(struct mgos_i2c *i2c, void *data, size_t len) {
uint16_t tries;
size_t ret = -1;
if (!i2c) return -1;
if (!i2c) {
return -1;
}
ret = read(i2c->fd, data, len);
@ -56,7 +57,6 @@ static size_t i2c_read_timeout(struct mgos_i2c *i2c, void *data, size_t len) {
return len;
}
bool mgos_i2c_read(struct mgos_i2c *i2c, uint16_t addr, void *data, size_t len, bool stop) {
size_t ret;
@ -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;
@ -97,49 +97,51 @@ 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];
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];
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;
@ -154,6 +156,7 @@ bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_
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
@ -174,27 +177,26 @@ bool mgos_i2c_read_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size_
/* 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)
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;
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];
struct i2c_rdwr_ioctl_data packets;
@ -226,18 +228,16 @@ bool mgos_i2c_write_reg_n(struct mgos_i2c *i2c, uint16_t addr, uint8_t reg, size
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;

View File

@ -32,39 +32,45 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
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;
memset(ll_file, 0, sizeof(ll_file));
if (strlen(file) >= 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);
offset = 0;
memset(ll_func, 0, sizeof(ll_func));
if (strlen(func) >= 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);
printf("%-5s %-30s %-40s| ", ll_str, ll_file, ll_func);
return 1;
}
double mg_time(void) {
struct timespec ts;
double ret;
@ -77,37 +83,37 @@ double mg_time(void) {
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;
}