json: Improve i2c and spi configuration.
Bus number no longer has to be the same as its id and all busses are disabled until a configuration entry is found. Signed-off-by: Henry Bruce <henry.bruce@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
61d20c7266
commit
9de294b389
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"platform": [
|
||||
{
|
||||
"id":300,
|
||||
@@ -59,14 +59,14 @@
|
||||
{ "index": 27, "rawpin": 360 }
|
||||
],
|
||||
"I2C": [
|
||||
{ "sclpin":13, "sdapin":15, "rawpin": 0, "index": 0, "default":true }
|
||||
{ "sclpin":13, "sdapin":15, "bus": 9, "index": 0, "default":true }
|
||||
],
|
||||
"PWM": [
|
||||
{ "index":22, "rawpin":0, "chipID":0},
|
||||
{ "index":24, "rawpin":0, "chipID":1 }
|
||||
],
|
||||
"SPI": [
|
||||
{ "index":0, "chipID": 0, "clock": 11, "miso": 7, "mosi": 9, "chipselect": 5, "default": true}
|
||||
{ "index":0, "bus": 0, "slaveselect": 0, "clock": 11, "miso": 7, "mosi": 9, "chipselect": 5, "default": true}
|
||||
],
|
||||
"UART": [
|
||||
{ "index":0, "chipID":0, "rawpin":0, "path":"/dev/ttyS0", "default":true}
|
||||
|
||||
@@ -35,6 +35,12 @@
|
||||
#define HAVE_PTHREAD_CANCEL
|
||||
#endif
|
||||
|
||||
// Max count for various busses
|
||||
#define MAX_I2C_BUS_COUNT 12
|
||||
#define MAX_SPI_BUS_COUNT 12
|
||||
#define MAX_UART_COUNT 6
|
||||
|
||||
|
||||
// general status failures for internal functions
|
||||
#define MRAA_PLATFORM_NO_INIT -3
|
||||
#define MRAA_IO_SETUP_FAILURE -2
|
||||
@@ -66,9 +72,11 @@
|
||||
#define MISO_KEY "miso"
|
||||
#define MOSI_KEY "mosi"
|
||||
#define CS_KEY "chipselect"
|
||||
#define SS_KEY "slaveselect"
|
||||
#define PIN_KEY "pin"
|
||||
#define IO_KEY "layout"
|
||||
#define PLATFORM_KEY "platform"
|
||||
#define BUS_KEY "bus"
|
||||
|
||||
// IO keys
|
||||
#define GPIO_KEY "GPIO"
|
||||
@@ -346,16 +354,16 @@ typedef struct _board_t {
|
||||
int gpio_count; /**< GPIO Count */
|
||||
int aio_count; /**< Analog side Count */
|
||||
int i2c_bus_count; /**< Usable i2c Count */
|
||||
mraa_i2c_bus_t i2c_bus[12]; /**< Array of i2c */
|
||||
mraa_i2c_bus_t i2c_bus[MAX_I2C_BUS_COUNT]; /**< Array of i2c */
|
||||
unsigned int def_i2c_bus; /**< Position in array of default i2c bus */
|
||||
int spi_bus_count; /**< Usable spi Count */
|
||||
mraa_spi_bus_t spi_bus[12]; /**< Array of spi */
|
||||
mraa_spi_bus_t spi_bus[MAX_SPI_BUS_COUNT]; /**< Array of spi */
|
||||
unsigned int def_spi_bus; /**< Position in array of defult spi bus */
|
||||
unsigned int adc_raw; /**< ADC raw bit value */
|
||||
unsigned int adc_supported; /**< ADC supported bit value */
|
||||
unsigned int def_uart_dev; /**< Position in array of defult uart */
|
||||
int uart_dev_count; /**< Usable spi Count */
|
||||
mraa_uart_dev_t uart_dev[6]; /**< Array of UARTs */
|
||||
mraa_uart_dev_t uart_dev[MAX_UART_COUNT]; /**< Array of UARTs */
|
||||
mraa_boolean_t no_bus_mux; /**< i2c/spi/adc/pwm/uart bus muxing setup not required */
|
||||
int pwm_default_period; /**< The default PWM period is US */
|
||||
int pwm_max_period; /**< Maximum period in us */
|
||||
|
||||
@@ -167,7 +167,7 @@ mraa_i2c_init(int bus)
|
||||
}
|
||||
if (!board->no_bus_mux) {
|
||||
int pos = board->i2c_bus[bus].sda;
|
||||
if (board->pins[pos].i2c.mux_total > 0) {
|
||||
if (pos >=0 && board->pins[pos].i2c.mux_total > 0) {
|
||||
if (mraa_setup_mux_mapped(board->pins[pos].i2c) != MRAA_SUCCESS) {
|
||||
syslog(LOG_ERR, "i2c%i_init: Failed to set-up i2c sda multiplexer", bus);
|
||||
return NULL;
|
||||
@@ -175,7 +175,7 @@ mraa_i2c_init(int bus)
|
||||
}
|
||||
|
||||
pos = board->i2c_bus[bus].scl;
|
||||
if (board->pins[pos].i2c.mux_total > 0) {
|
||||
if (pos >=0 && board->pins[pos].i2c.mux_total > 0) {
|
||||
if (mraa_setup_mux_mapped(board->pins[pos].i2c) != MRAA_SUCCESS) {
|
||||
syslog(LOG_ERR, "i2c%i_init: Failed to set-up i2c scl multiplexer", bus);
|
||||
return NULL;
|
||||
|
||||
@@ -138,22 +138,27 @@ mraa_init_json_platform_platform(json_object* jobj_platform, mraa_board_t* board
|
||||
|
||||
// Check to see if they've provided a UART count
|
||||
ret = mraa_init_json_platform_get_index(jobj_platform, PLATFORM_KEY, UART_COUNT_KEY, index,
|
||||
&(board->uart_dev_count), 6);
|
||||
&(board->uart_dev_count), MAX_UART_COUNT);
|
||||
if (ret != MRAA_SUCCESS && ret != MRAA_ERROR_NO_DATA_AVAILABLE) {
|
||||
return ret;
|
||||
}
|
||||
// Check to see if they've provided a I2C count
|
||||
ret = mraa_init_json_platform_get_index(jobj_platform, PLATFORM_KEY, I2C_COUNT_KEY, index,
|
||||
&(board->i2c_bus_count), 12);
|
||||
&(board->i2c_bus_count), MAX_I2C_BUS_COUNT);
|
||||
if (ret != MRAA_SUCCESS && ret != MRAA_ERROR_NO_DATA_AVAILABLE) {
|
||||
return ret;
|
||||
}
|
||||
for (int i = 0; i < board->i2c_bus_count; ++i)
|
||||
board->i2c_bus[i].bus_id = -1;
|
||||
|
||||
// Check to see if they've provided a SPI count
|
||||
ret = mraa_init_json_platform_get_index(jobj_platform, PLATFORM_KEY, SPI_COUNT_KEY, index,
|
||||
&(board->spi_bus_count), 12);
|
||||
&(board->spi_bus_count), MAX_SPI_BUS_COUNT);
|
||||
if (ret != MRAA_SUCCESS && ret != MRAA_ERROR_NO_DATA_AVAILABLE) {
|
||||
return ret;
|
||||
}
|
||||
for (int i = 0; i < board->spi_bus_count; ++i)
|
||||
board->spi_bus[i].bus_id = -1;
|
||||
|
||||
// Set the PWM default numbers
|
||||
board->pwm_default_period = -1;
|
||||
@@ -278,23 +283,22 @@ mraa_init_json_platform_i2c(json_object* jobj_i2c, mraa_board_t* board, int inde
|
||||
{
|
||||
int pos = 0;
|
||||
int pin = 0;
|
||||
int sysfs_pin = 0;
|
||||
int bus = 0;
|
||||
// int sysfs_pin = 0;
|
||||
mraa_result_t ret = MRAA_SUCCESS;
|
||||
json_object* jobj_temp = NULL;
|
||||
|
||||
// disable bus if we error out
|
||||
board->i2c_bus[pos].bus_id = -1;
|
||||
// Default to no mux pins defined
|
||||
board->pins[pin].i2c.mux_total = 0;
|
||||
|
||||
// Get the I2C bus array index
|
||||
ret = mraa_init_json_platform_get_index(jobj_i2c, I2C_KEY, INDEX_KEY, index, &pos, board->i2c_bus_count - 1);
|
||||
if (ret != MRAA_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
// Get the sysfs pin
|
||||
ret = mraa_init_json_platform_get_pin(jobj_i2c, I2C_KEY, RAW_PIN_KEY, index, &sysfs_pin);
|
||||
if (ret != MRAA_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
// Get the bus number (e.g. 2 for /dev/i2c-2). If it doesn't exist, default to bus = pos
|
||||
bus = pos;
|
||||
ret = mraa_init_json_platform_get_pin(jobj_i2c, I2C_KEY, BUS_KEY, index, &bus);
|
||||
// Setup the sda pin
|
||||
ret = mraa_init_json_platform_get_index(jobj_i2c, I2C_KEY, SDAPIN_KEY, index, &pin,
|
||||
board->phy_pin_count - 1);
|
||||
@@ -302,7 +306,6 @@ mraa_init_json_platform_i2c(json_object* jobj_i2c, mraa_board_t* board, int inde
|
||||
board->i2c_bus[pos].sda = -1;
|
||||
} else if (ret == MRAA_SUCCESS) {
|
||||
board->pins[pin].capabilities.i2c = 1;
|
||||
board->pins[pin].i2c.pinmap = sysfs_pin;
|
||||
board->i2c_bus[pos].sda = pin;
|
||||
} else {
|
||||
return ret;
|
||||
@@ -314,13 +317,12 @@ mraa_init_json_platform_i2c(json_object* jobj_i2c, mraa_board_t* board, int inde
|
||||
board->i2c_bus[pos].scl = -1;
|
||||
} else if (ret == MRAA_SUCCESS) {
|
||||
board->pins[pin].capabilities.i2c = 1;
|
||||
board->pins[pin].i2c.pinmap = sysfs_pin;
|
||||
board->i2c_bus[pos].scl = pin;
|
||||
} else {
|
||||
return ret;
|
||||
}
|
||||
|
||||
board->i2c_bus[pos].bus_id = pos;
|
||||
board->i2c_bus[pos].bus_id = bus;
|
||||
|
||||
// check to see if this i2c is the default one
|
||||
if (json_object_object_get_ex(jobj_i2c, DEFAULT_KEY, &jobj_temp)) {
|
||||
@@ -369,8 +371,9 @@ mraa_result_t
|
||||
mraa_init_json_platform_spi(json_object* jobj_spi, mraa_board_t* board, int index)
|
||||
{
|
||||
int pos = 0;
|
||||
int bus = 0;
|
||||
int ss = 0;
|
||||
int pin = 0;
|
||||
int parent_id = 0;
|
||||
json_object* jobj_temp = NULL;
|
||||
mraa_result_t ret = MRAA_SUCCESS;
|
||||
|
||||
@@ -380,19 +383,27 @@ mraa_init_json_platform_spi(json_object* jobj_spi, mraa_board_t* board, int inde
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Get the parent id
|
||||
ret = mraa_init_json_platform_get_pin(jobj_spi, SPI_KEY, CHIP_ID_KEY, index, &parent_id);
|
||||
// Get the bus
|
||||
ret = mraa_init_json_platform_get_pin(jobj_spi, SPI_KEY, BUS_KEY, index, &bus);
|
||||
if (ret != MRAA_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Get slave select
|
||||
ret = mraa_init_json_platform_get_pin(jobj_spi, SPI_KEY, SS_KEY, index, &ss);
|
||||
if (ret != MRAA_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
board->spi_bus[pos].bus_id = bus;
|
||||
board->spi_bus[pos].slave_s = ss;
|
||||
|
||||
// Setup the clock pin
|
||||
ret = mraa_init_json_platform_get_index(jobj_spi, SPI_KEY, CLOCK_KEY, index, &pin, board->phy_pin_count - 1);
|
||||
if (ret == MRAA_ERROR_NO_DATA_AVAILABLE) {
|
||||
board->spi_bus[pos].sclk = -1;
|
||||
} else if (ret == MRAA_SUCCESS) {
|
||||
board->pins[pin].capabilities.spi = 1;
|
||||
board->pins[pin].spi.parent_id = parent_id;
|
||||
board->spi_bus[pos].sclk = pin;
|
||||
} else {
|
||||
return ret;
|
||||
@@ -403,7 +414,6 @@ mraa_init_json_platform_spi(json_object* jobj_spi, mraa_board_t* board, int inde
|
||||
board->spi_bus[pos].miso = -1;
|
||||
} else if (ret == MRAA_SUCCESS) {
|
||||
board->pins[pin].capabilities.spi = 1;
|
||||
board->pins[pin].spi.parent_id = parent_id;
|
||||
board->spi_bus[pos].miso = pin;
|
||||
} else {
|
||||
return ret;
|
||||
@@ -414,7 +424,6 @@ mraa_init_json_platform_spi(json_object* jobj_spi, mraa_board_t* board, int inde
|
||||
board->spi_bus[pos].mosi = -1;
|
||||
} else if (ret == MRAA_SUCCESS) {
|
||||
board->pins[pin].capabilities.spi = 1;
|
||||
board->pins[pin].spi.parent_id = parent_id;
|
||||
board->spi_bus[pos].mosi = pin;
|
||||
} else {
|
||||
return ret;
|
||||
@@ -425,7 +434,6 @@ mraa_init_json_platform_spi(json_object* jobj_spi, mraa_board_t* board, int inde
|
||||
board->spi_bus[pos].cs = -1;
|
||||
} else if (ret == MRAA_SUCCESS) {
|
||||
board->pins[pin].capabilities.spi = 1;
|
||||
board->pins[pin].spi.parent_id = parent_id;
|
||||
board->spi_bus[pos].cs = pin;
|
||||
} else {
|
||||
return ret;
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "spi.h"
|
||||
#include "mraa_internal.h"
|
||||
@@ -88,7 +89,7 @@ mraa_spi_init(int bus)
|
||||
|
||||
if (!plat->no_bus_mux) {
|
||||
int pos = plat->spi_bus[bus].sclk;
|
||||
if (plat->pins[pos].spi.mux_total > 0) {
|
||||
if (pos >= 0 && plat->pins[pos].spi.mux_total > 0) {
|
||||
if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS) {
|
||||
syslog(LOG_ERR, "spi: failed to set-up spi sclk multiplexer");
|
||||
return NULL;
|
||||
@@ -96,7 +97,7 @@ mraa_spi_init(int bus)
|
||||
}
|
||||
|
||||
pos = plat->spi_bus[bus].mosi;
|
||||
if (plat->pins[pos].spi.mux_total > 0) {
|
||||
if (pos >= 0 && plat->pins[pos].spi.mux_total > 0) {
|
||||
if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS) {
|
||||
syslog(LOG_ERR, "spi: failed to set-up spi mosi multiplexer");
|
||||
return NULL;
|
||||
@@ -104,7 +105,7 @@ mraa_spi_init(int bus)
|
||||
}
|
||||
|
||||
pos = plat->spi_bus[bus].miso;
|
||||
if (plat->pins[pos].spi.mux_total > 0) {
|
||||
if (pos >= 0 && plat->pins[pos].spi.mux_total > 0) {
|
||||
if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS) {
|
||||
syslog(LOG_ERR, "spi: failed to set-up spi miso multiplexer");
|
||||
return NULL;
|
||||
@@ -112,7 +113,7 @@ mraa_spi_init(int bus)
|
||||
}
|
||||
|
||||
pos = plat->spi_bus[bus].cs;
|
||||
if (plat->pins[pos].spi.mux_total > 0) {
|
||||
if (pos >= 0 && plat->pins[pos].spi.mux_total > 0) {
|
||||
if (mraa_setup_mux_mapped(plat->pins[pos].spi) != MRAA_SUCCESS) {
|
||||
syslog(LOG_ERR, "spi: failed to set-up spi cs multiplexer");
|
||||
return NULL;
|
||||
@@ -158,7 +159,7 @@ mraa_spi_init_raw(unsigned int bus, unsigned int cs)
|
||||
|
||||
dev->devfd = open(path, O_RDWR);
|
||||
if (dev->devfd < 0) {
|
||||
syslog(LOG_ERR, "spi: Failed opening SPI Device. bus:%s", path);
|
||||
syslog(LOG_ERR, "spi: Failed opening SPI Device. bus:%s. Error %d %s", path, errno, strerror(errno));
|
||||
status = MRAA_ERROR_INVALID_RESOURCE;
|
||||
goto init_raw_cleanup;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user