Created
November 9, 2019 21:02
-
-
Save ekarlso/135fe2082ee27d65b52c7cc0d93a0f26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2018 Tavish Naruka <tavishnaruka@gmail.com> | |
* | |
* SPDX-License-Identifier: Apache-2.0 | |
*/ | |
#include <logging/log_ctrl.h> | |
#include <errno.h> | |
#include <zephyr.h> | |
#include <sys/printk.h> | |
#include <device.h> | |
#include <drivers/i2c.h> | |
#include <logging/log.h> | |
#ifdef ARDUINO_I2C_LABEL | |
#define I2C_DEV ARDUINO_I2C_LABEL | |
#else | |
#define I2C_DEV "I2C_0" | |
#endif | |
LOG_MODULE_REGISTER(main); | |
/** | |
* @file This app scans I2C bus for any devices present | |
*/ | |
#define PFC8574_MAX_ADDR 0x38 | |
#define PCF8574_MAX_PIN 8 | |
#define PFC8574_STATUS_PIN_ON 1 | |
#define PFC8574_STATUS_PIN_OFF 0 | |
#define COUNT(X) (sizeof((X)) / sizeof((X)[0])) | |
bool pfc8574_is_valid_pin(struct device *dev, int pin) | |
{ | |
if ((pin > PCF8574_MAX_PIN) || (pin < 0)) | |
{ | |
LOG_ERR("Pin %d is invalid", pin); | |
return false; | |
} | |
return true; | |
} | |
int pfc8674_write(struct device *dev, uint16_t *data) | |
{ | |
struct i2c_msg wrt[] = { | |
{.buf = data, | |
.len = sizeof(*data), | |
.flags = I2C_MSG_WRITE | I2C_MSG_STOP}}; | |
int result = i2c_transfer(dev, wrt, COUNT(wrt), PFC8574_MAX_ADDR); | |
LOG_DBG("Write result (%d)", result); | |
return result; | |
} | |
// int pfc8574_pinmode(struct device *dev, int pin, int mode) | |
// { | |
// if (!pfc8574_is_valid_pin(dev, pin)) | |
// { | |
// return -1; | |
// } | |
// LOG_DBG("%d is valid pin.", pin); | |
// if (pin == 0) | |
// { | |
// LOG_DBG("Applying for all pins since pin is 0."); | |
// for (int i = 1; i <= PCF8574_MAX_PIN; i++) | |
// { | |
// pfc8574_pinmode(dev, i, mode); | |
// } | |
// } | |
// else | |
// { | |
// LOG_DBG("Toggling pin %d/%d", pin, mode); | |
// // i2c_transfer(dev, ) | |
// union { | |
// uint8_t bytes[2]; | |
// uint16_t word; | |
// } tbw = { | |
// .bytes = {(1 << pin), (1 << pin)}}; | |
// // i2c_transfer(dev, msgs[0]) | |
// return pfc8674_write(dev, &tbw.word); | |
// } | |
// return 0; | |
// } | |
int pfc8574_pinmode(struct device *dev, int pin, int mode) | |
{ | |
LOG_DBG("Toggling pin %d -> %d", pin, mode); | |
// i2c_transfer(dev, ) | |
union { | |
uint8_t bytes[2]; | |
uint16_t word; | |
} tbw = { | |
.bytes = {(mode << pin), (mode << pin)}}; | |
// i2c_transfer(dev, msgs[0]) | |
return pfc8674_write(dev, &tbw.word); | |
return 0; | |
} | |
int pfc8574_disable_all(struct device *dev) | |
{ | |
LOG_DBG("Disable all pins"); | |
// i2c_transfer(dev, ) | |
union { | |
uint8_t bytes[2]; | |
uint16_t word; | |
} tbw = { | |
.bytes = {0, 0}}; | |
// i2c_transfer(dev, msgs[0]) | |
return pfc8674_write(dev, &tbw.word); | |
return 0; | |
} | |
int pfc8574_read(struct device *dev, int pin, bool *dst) | |
{ | |
uint8_t rd; | |
int result = i2c_read(dev, &rd, sizeof(rd), PFC8574_MAX_ADDR); | |
LOG_DBG("Read result is (%d)", result); | |
if (result != 0) | |
{ | |
return result; | |
} | |
if (dst) | |
{ | |
*dst = (rd & (1 << pin)) > 0; | |
} | |
// LOG_DBG("%x", buf[0]); | |
return 0; | |
} | |
void main(void) | |
{ | |
struct device *pfc8574_dev; | |
LOG_INF("Starting i2c scanner...\n"); | |
pfc8574_dev = device_get_binding(I2C_DEV); | |
if (!pfc8574_dev) | |
{ | |
LOG_ERR("I2C: Device driver not found.\n"); | |
return; | |
} | |
LOG_INF("Now running"); | |
for (u8_t i = 4; i <= 0x77; i++) | |
{ | |
struct i2c_msg msgs[1]; | |
u8_t dst; | |
/* Send the address to read from */ | |
msgs[0].buf = &dst; | |
msgs[0].len = 0U; | |
msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP; | |
if (i2c_transfer(pfc8574_dev, &msgs[0], 1, i) == 0) | |
{ | |
printk("0x%2x FOUND\n", i); | |
} | |
} | |
pfc8574_pinmode(pfc8574_dev, 1, 1); | |
bool state = 0; | |
int pin_state; | |
// while (true) | |
// { | |
// pfc8574_pinmode(pfc8574_dev, 0, (int)state); | |
// state = !state; | |
// LOG_INF("State %d", state); | |
// pfc8574_read(pfc8574_dev, 1, &pin_state); | |
// printk("%i\n", pin_state); | |
// LOG_INF("Sleepin 1 sec"); | |
// k_sleep(4 * 1000); | |
// } | |
pfc8574_disable_all(pfc8574_dev); | |
while (true) | |
{ | |
for (int i = 0; i < 8; i++) | |
{ | |
pfc8574_pinmode(pfc8574_dev, i, 1); | |
} | |
state = !state; | |
k_sleep(2 * 1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment