Implemented I2C scanner app (#97)
This commit is contained in:
committed by
GitHub
parent
6094b9c3f2
commit
3f62ec2efa
@@ -157,28 +157,36 @@ bool isStarted(i2c_port_t port) {
|
||||
return started;
|
||||
}
|
||||
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize) {
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
lock(port);
|
||||
esp_err_t result = i2c_master_read_from_device(port, address, data, dataSize, dataArray[port].configuration.timeout);
|
||||
esp_err_t result = i2c_master_read_from_device(port, address, data, dataSize, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize) {
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
lock(port);
|
||||
esp_err_t result = i2c_master_write_to_device(port, address, data, dataSize, dataArray[port].configuration.timeout);
|
||||
esp_err_t result = i2c_master_write_to_device(port, address, data, dataSize, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize) {
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) {
|
||||
lock(port);
|
||||
esp_err_t result = i2c_master_write_read_device(port, address, writeData, writeDataSize, readData, readDataSize, dataArray[port].configuration.timeout);
|
||||
esp_err_t result = i2c_master_write_read_device(port, address, writeData, writeDataSize, readData, readDataSize, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
TtStatus lock(i2c_port_t port, uint32_t timeout) {
|
||||
bool masterCheckAddressForDevice(i2c_port_t port, uint8_t address, TickType_t timeout) {
|
||||
lock(port);
|
||||
uint8_t message[2] = { 0, 0 };
|
||||
esp_err_t result = i2c_master_write_to_device(port, address, message, 2, timeout);
|
||||
unlock(port);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
TtStatus lock(i2c_port_t port, TickType_t timeout) {
|
||||
return dataArray[port].mutex.acquire(timeout);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,12 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef ESP_TARGET
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#else
|
||||
#include "FreeRTOS.h"
|
||||
#endif
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
typedef enum {
|
||||
@@ -24,8 +30,6 @@ typedef struct {
|
||||
bool canReinit;
|
||||
/** Whether configuration can be changed. */
|
||||
bool hasMutableConfiguration;
|
||||
/** Read/write timeout (not related to mutex locking mechanism) */
|
||||
unsigned long timeout;
|
||||
/** Configuration that must be valid when initAtBoot is set to true. */
|
||||
i2c_config_t config;
|
||||
} Configuration;
|
||||
@@ -35,10 +39,11 @@ bool init(const std::vector<i2c::Configuration>& configurations);
|
||||
bool start(i2c_port_t port);
|
||||
bool stop(i2c_port_t port);
|
||||
bool isStarted(i2c_port_t port);
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize);
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize);
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize);
|
||||
TtStatus lock(i2c_port_t port, uint32_t timeout = UINT_MAX);
|
||||
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout);
|
||||
bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout);
|
||||
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout);
|
||||
bool masterCheckAddressForDevice(i2c_port_t port, uint8_t address, TickType_t timeout);
|
||||
TtStatus lock(i2c_port_t port, TickType_t timeout = UINT_MAX);
|
||||
TtStatus unlock(i2c_port_t port);
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
/**
|
||||
* This code is based on i2c_manager from https://github.com/ropg/i2c_manager/blob/master/i2c_manager/i2c_manager.c (original has MIT license)
|
||||
*/
|
||||
#include <Kernel.h>
|
||||
#include "I2c.h"
|
||||
#include "Log.h"
|
||||
#include "Mutex.h"
|
||||
@@ -81,15 +82,15 @@ bool isStarted(i2c_port_t port) {
|
||||
return started;
|
||||
}
|
||||
|
||||
bool read(i2c_port_t port, uint16_t address, uint32_t reg, uint8_t* buffer, uint16_t size) {
|
||||
bool read(i2c_port_t port, uint16_t address, uint32_t reg, uint8_t* buffer, uint16_t size, TickType_t timeout) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool write(i2c_port_t port, uint16_t address, uint32_t reg, const uint8_t* buffer, uint16_t size) {
|
||||
bool write(i2c_port_t port, uint16_t address, uint32_t reg, const uint8_t* buffer, uint16_t size, TickType_t timeout) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TtStatus lock(i2c_port_t port, uint32_t timeout) {
|
||||
TtStatus lock(i2c_port_t port, TickType_t timeout) {
|
||||
return dataArray[port].mutex.acquire(timeout);
|
||||
}
|
||||
|
||||
@@ -97,6 +98,10 @@ TtStatus unlock(i2c_port_t port) {
|
||||
return dataArray[port].mutex.release();
|
||||
}
|
||||
|
||||
bool masterCheckAddressForDevice(i2c_port_t port, uint8_t address, TickType_t timeout) {
|
||||
return (rand()) % 25 == 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user