mock: added I2C functionality
Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
26718a67a2
commit
346f447c4d
11
docs/mock.md
11
docs/mock.md
@@ -12,8 +12,13 @@ Board configuration
|
|||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
This feature is yet in the experimental mode and not all functionality is available.
|
This feature is yet in the experimental mode and not all functionality is available.
|
||||||
Right now we simulate a single generic board with GPIO (without ISR) and
|
Right now we simulate a single generic board with:
|
||||||
an ADC with 10 (std)/12 (max) bit resolution, which returns random values on read.
|
* GPIO (without ISR)
|
||||||
|
* ADC with 10 (std)/12 (max) bit resolution, which returns random values on read
|
||||||
|
* Single I2C bus with one device at address 0x33 and 10 bytes of register space,
|
||||||
|
which can be read or written in bytes or words (big-endian). Technically those
|
||||||
|
registers are just an array of `uint8_t`, so you can treat them as 10 single-byte
|
||||||
|
registers or 5 single-word ones or a mix thereof.
|
||||||
|
|
||||||
We plan to develop it further and all [contributions](../CONTRIBUTING.md) are more than welcome.
|
We plan to develop it further and all [contributions](../CONTRIBUTING.md) are more than welcome.
|
||||||
|
|
||||||
@@ -23,6 +28,8 @@ See the table below for pin layout and features
|
|||||||
|-------------|----------|---------------------------------------|
|
|-------------|----------|---------------------------------------|
|
||||||
| 0 | GPIO0 | GPIO pin, no muxing, no ISR |
|
| 0 | GPIO0 | GPIO pin, no muxing, no ISR |
|
||||||
| 1 | ADC0 | AIO pin, returns random value on read |
|
| 1 | ADC0 | AIO pin, returns random value on read |
|
||||||
|
| 2 | I2C0SDA | SDA pin for I2C0 bus |
|
||||||
|
| 3 | I2C0SCL | SCL pin for I2C0 bus |
|
||||||
|
|
||||||
Building
|
Building
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ extern "C" {
|
|||||||
|
|
||||||
#include "mraa_internal.h"
|
#include "mraa_internal.h"
|
||||||
|
|
||||||
#define MRAA_MOCK_PINCOUNT 2
|
#define MRAA_MOCK_PINCOUNT 4
|
||||||
|
|
||||||
mraa_board_t*
|
mraa_board_t*
|
||||||
mraa_mock_board();
|
mraa_mock_board();
|
||||||
|
|||||||
81
include/mock/mock_board_i2c.h
Normal file
81
include/mock/mock_board_i2c.h
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
* Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "mraa_internal.h"
|
||||||
|
|
||||||
|
// Mock I2C device address
|
||||||
|
#define MOCK_I2C_DEV_ADDR 0x33
|
||||||
|
// Mock I2C device data registers block length in bytes. Our code assumes it's >= 1.
|
||||||
|
#define MOCK_I2C_DEV_DATA_LEN 10
|
||||||
|
// Initial value for each byte in the mock I2C device data registers
|
||||||
|
#define MOCK_I2C_DEV_DATA_INIT_BYTE 0xAB
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_init_bus_replace(mraa_i2c_context dev);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_stop_replace(mraa_i2c_context dev);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_set_frequency_replace(mraa_i2c_context dev, mraa_i2c_mode_t mode);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_address_replace(mraa_i2c_context dev, uint8_t addr);
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_replace(mraa_i2c_context dev, uint8_t* data, int length);
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_byte_replace(mraa_i2c_context dev);
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_byte_data_replace(mraa_i2c_context dev, uint8_t command);
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_bytes_data_replace(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length);
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_word_data_replace(mraa_i2c_context dev, uint8_t command);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_replace(mraa_i2c_context dev, const uint8_t* data, int length);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_byte_replace(mraa_i2c_context dev, const uint8_t data);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_byte_data_replace(mraa_i2c_context dev, const uint8_t data, const uint8_t command);
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_word_data_replace(mraa_i2c_context dev, const uint16_t data, const uint8_t command);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -89,6 +89,11 @@ struct _i2c {
|
|||||||
unsigned long funcs; /**< /dev/i2c-* device capabilities as per https://www.kernel.org/doc/Documentation/i2c/functionality */
|
unsigned long funcs; /**< /dev/i2c-* device capabilities as per https://www.kernel.org/doc/Documentation/i2c/functionality */
|
||||||
void *handle; /**< generic handle for non-standard drivers that don't use file descriptors */
|
void *handle; /**< generic handle for non-standard drivers that don't use file descriptors */
|
||||||
mraa_adv_func_t* advance_func; /**< override function table */
|
mraa_adv_func_t* advance_func; /**< override function table */
|
||||||
|
#if defined(MOCKPLAT)
|
||||||
|
uint8_t mock_dev_addr; /**< address of the mock I2C device */
|
||||||
|
uint8_t mock_dev_data_len; /**< mock device data register block length in bytes */
|
||||||
|
uint8_t* mock_dev_data; /**< mock device data register block contents */
|
||||||
|
#endif
|
||||||
/*@}*/
|
/*@}*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ set (mraa_LIB_MOCK_SRCS_NOAUTO
|
|||||||
${PROJECT_SOURCE_DIR}/src/mock/mock_board.c
|
${PROJECT_SOURCE_DIR}/src/mock/mock_board.c
|
||||||
${PROJECT_SOURCE_DIR}/src/mock/mock_board_gpio.c
|
${PROJECT_SOURCE_DIR}/src/mock/mock_board_gpio.c
|
||||||
${PROJECT_SOURCE_DIR}/src/mock/mock_board_aio.c
|
${PROJECT_SOURCE_DIR}/src/mock/mock_board_aio.c
|
||||||
|
${PROJECT_SOURCE_DIR}/src/mock/mock_board_i2c.c
|
||||||
)
|
)
|
||||||
|
|
||||||
set (mraa_LIBS ${CMAKE_THREAD_LIBS_INIT})
|
set (mraa_LIBS ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
#include "mock/mock_board.h"
|
#include "mock/mock_board.h"
|
||||||
#include "mock/mock_board_gpio.h"
|
#include "mock/mock_board_gpio.h"
|
||||||
#include "mock/mock_board_aio.h"
|
#include "mock/mock_board_aio.h"
|
||||||
|
#include "mock/mock_board_i2c.h"
|
||||||
|
|
||||||
#define PLATFORM_NAME "MRAA mock platform"
|
#define PLATFORM_NAME "MRAA mock platform"
|
||||||
|
|
||||||
@@ -47,7 +48,13 @@ mraa_mock_board()
|
|||||||
b->aio_count = 1;
|
b->aio_count = 1;
|
||||||
b->adc_raw = 12;
|
b->adc_raw = 12;
|
||||||
b->adc_supported = 10;
|
b->adc_supported = 10;
|
||||||
b->i2c_bus_count = 0;
|
|
||||||
|
b->i2c_bus_count = 1;
|
||||||
|
b->i2c_bus[0].bus_id = 0;
|
||||||
|
b->i2c_bus[0].sda = 2;
|
||||||
|
b->i2c_bus[0].scl = 3;
|
||||||
|
b->def_i2c_bus = b->i2c_bus[0].bus_id;
|
||||||
|
|
||||||
b->spi_bus_count = 0;
|
b->spi_bus_count = 0;
|
||||||
|
|
||||||
b->pwm_default_period = 0;
|
b->pwm_default_period = 0;
|
||||||
@@ -79,6 +86,19 @@ mraa_mock_board()
|
|||||||
b->adv_func->aio_init_internal_replace = &mraa_mock_aio_init_internal_replace;
|
b->adv_func->aio_init_internal_replace = &mraa_mock_aio_init_internal_replace;
|
||||||
b->adv_func->aio_close_replace = &mraa_mock_aio_close_replace;
|
b->adv_func->aio_close_replace = &mraa_mock_aio_close_replace;
|
||||||
b->adv_func->aio_read_replace = &mraa_mock_aio_read_replace;
|
b->adv_func->aio_read_replace = &mraa_mock_aio_read_replace;
|
||||||
|
b->adv_func->i2c_init_bus_replace = &mraa_mock_i2c_init_bus_replace;
|
||||||
|
b->adv_func->i2c_stop_replace = &mraa_mock_i2c_stop_replace;
|
||||||
|
b->adv_func->i2c_set_frequency_replace = &mraa_mock_i2c_set_frequency_replace;
|
||||||
|
b->adv_func->i2c_address_replace = &mraa_mock_i2c_address_replace;
|
||||||
|
b->adv_func->i2c_read_replace = &mraa_mock_i2c_read_replace;
|
||||||
|
b->adv_func->i2c_read_byte_replace = &mraa_mock_i2c_read_byte_replace;
|
||||||
|
b->adv_func->i2c_read_byte_data_replace = &mraa_mock_i2c_read_byte_data_replace;
|
||||||
|
b->adv_func->i2c_read_bytes_data_replace = &mraa_mock_i2c_read_bytes_data_replace;
|
||||||
|
b->adv_func->i2c_read_word_data_replace = &mraa_mock_i2c_read_word_data_replace;
|
||||||
|
b->adv_func->i2c_write_replace = &mraa_mock_i2c_write_replace;
|
||||||
|
b->adv_func->i2c_write_byte_replace = &mraa_mock_i2c_write_byte_replace;
|
||||||
|
b->adv_func->i2c_write_byte_data_replace = &mraa_mock_i2c_write_byte_data_replace;
|
||||||
|
b->adv_func->i2c_write_word_data_replace = &mraa_mock_i2c_write_word_data_replace;
|
||||||
|
|
||||||
// Pin definitions
|
// Pin definitions
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
@@ -95,6 +115,18 @@ mraa_mock_board()
|
|||||||
b->pins[pos].aio.mux_total = 0;
|
b->pins[pos].aio.mux_total = 0;
|
||||||
pos++;
|
pos++;
|
||||||
|
|
||||||
|
strncpy(b->pins[pos].name, "I2C0SDA", 8);
|
||||||
|
b->pins[pos].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
|
||||||
|
b->pins[pos].i2c.mux_total = 0;
|
||||||
|
b->pins[pos].i2c.pinmap = 0;
|
||||||
|
pos++;
|
||||||
|
|
||||||
|
strncpy(b->pins[pos].name, "I2C0SCL", 8);
|
||||||
|
b->pins[pos].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
|
||||||
|
b->pins[pos].i2c.mux_total = 0;
|
||||||
|
b->pins[pos].i2c.pinmap = 0;
|
||||||
|
pos++;
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
|
|||||||
246
src/mock/mock_board_i2c.c
Normal file
246
src/mock/mock_board_i2c.c
Normal file
@@ -0,0 +1,246 @@
|
|||||||
|
/*
|
||||||
|
* Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
* Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include "mock/mock_board.h"
|
||||||
|
#include "mock/mock_board_gpio.h"
|
||||||
|
#include "mock/mock_board_aio.h"
|
||||||
|
#include "mock/mock_board_i2c.h"
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_init_bus_replace(mraa_i2c_context dev)
|
||||||
|
{
|
||||||
|
dev->mock_dev_addr = MOCK_I2C_DEV_ADDR;
|
||||||
|
dev->mock_dev_data_len = MOCK_I2C_DEV_DATA_LEN;
|
||||||
|
|
||||||
|
dev->mock_dev_data = (uint8_t*) calloc(dev->mock_dev_data_len, sizeof(uint8_t));
|
||||||
|
if (dev->mock_dev_data == NULL) {
|
||||||
|
syslog(LOG_CRIT, "i2c%i: init: Failed to allocate memory for mock device context", dev->busnum);
|
||||||
|
return MRAA_ERROR_NO_RESOURCES;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set initial values for mock device "data registers"
|
||||||
|
memset(dev->mock_dev_data, MOCK_I2C_DEV_DATA_INIT_BYTE, dev->mock_dev_data_len);
|
||||||
|
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_stop_replace(mraa_i2c_context dev)
|
||||||
|
{
|
||||||
|
free(dev->mock_dev_data);
|
||||||
|
free(dev);
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_set_frequency_replace(mraa_i2c_context dev, mraa_i2c_mode_t mode)
|
||||||
|
{
|
||||||
|
switch (mode) {
|
||||||
|
case MRAA_I2C_STD:
|
||||||
|
case MRAA_I2C_FAST:
|
||||||
|
case MRAA_I2C_HIGH:
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
syslog(LOG_ERR, "i2c%i: set_frequency: Invalid I2C frequency selected", dev->busnum);
|
||||||
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_address_replace(mraa_i2c_context dev, uint8_t addr)
|
||||||
|
{
|
||||||
|
const uint8_t MAX_I2C_ADDR = 0x7F;
|
||||||
|
|
||||||
|
if (addr > MAX_I2C_ADDR) {
|
||||||
|
syslog(LOG_ERR, "i2c%i: address: Slave address 0x%X is bigger than max supported (0x%X)",
|
||||||
|
dev->busnum, addr, MAX_I2C_ADDR);
|
||||||
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_replace(mraa_i2c_context dev, uint8_t* data, int length)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
// Account for possible mismatch between length and our "register" range
|
||||||
|
int copy_len = (length <= dev->mock_dev_data_len) ? length : dev->mock_dev_data_len;
|
||||||
|
memcpy(data, dev->mock_dev_data, copy_len);
|
||||||
|
return copy_len;
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_byte_replace(mraa_i2c_context dev)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
return dev->mock_dev_data[0];
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_byte_data_replace(mraa_i2c_context dev, uint8_t command)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
if (command < dev->mock_dev_data_len) {
|
||||||
|
return dev->mock_dev_data[command];
|
||||||
|
} else {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"i2c%i: read_byte_data: Command/register number is too big, max is 0x%X",
|
||||||
|
dev->busnum, dev->mock_dev_data_len - 1);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_bytes_data_replace(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
if (command >= dev->mock_dev_data_len) {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"i2c%i: read_bytes_data: Command/register number is too big, max is 0x%X",
|
||||||
|
dev->busnum, dev->mock_dev_data_len - 1);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length <= 0) {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"i2c%i: read_bytes_data: Length to read is invalid (%d), cannot proceed",
|
||||||
|
dev->busnum, length);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
// We read requested length, but only up to mock device data length
|
||||||
|
for (i = command; (i < (command + length)) && (i < dev->mock_dev_data_len); ++i) {
|
||||||
|
data[i - command] = dev->mock_dev_data[i];
|
||||||
|
}
|
||||||
|
return (i - command);
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_i2c_read_word_data_replace(mraa_i2c_context dev, uint8_t command)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
if ((command + 1) < dev->mock_dev_data_len) {
|
||||||
|
// Let's say the device is big-endian
|
||||||
|
int result = (dev->mock_dev_data[command] << 8) + dev->mock_dev_data[command + 1];
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"i2c%i: read_word_data: Command/register number is too big, max is 0x%X",
|
||||||
|
dev->busnum, dev->mock_dev_data_len - 2);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_replace(mraa_i2c_context dev, const uint8_t* data, int length)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
int copy_len = (length <= dev->mock_dev_data_len) ? length : dev->mock_dev_data_len;
|
||||||
|
memcpy(dev->mock_dev_data, data, copy_len);
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_byte_replace(mraa_i2c_context dev, const uint8_t data)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
dev->mock_dev_data[0] = data;
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_byte_data_replace(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
if (command < dev->mock_dev_data_len) {
|
||||||
|
dev->mock_dev_data[command] = data;
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
} else {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"i2c%i: write_byte_data: Command/register number is too big, max is 0x%X",
|
||||||
|
dev->busnum, dev->mock_dev_data_len - 1);
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_i2c_write_word_data_replace(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
|
||||||
|
{
|
||||||
|
if (dev->addr == dev->mock_dev_addr) {
|
||||||
|
if ((command + 1) < dev->mock_dev_data_len) {
|
||||||
|
// Let's say the device is big-endian
|
||||||
|
dev->mock_dev_data[command] = (data & 0xFF00) >> 8;
|
||||||
|
dev->mock_dev_data[command + 1] = data & 0x00FF;
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
} else {
|
||||||
|
syslog(LOG_ERR,
|
||||||
|
"i2c%i: write_word_data: Command/register number is too big, max is 0x%X",
|
||||||
|
dev->busnum, dev->mock_dev_data_len - 2);
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Not our mock device
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,18 @@ add_test (NAME py_gpio_mode COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT
|
|||||||
|
|
||||||
add_test (NAME py_aio COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/aio_checks.py)
|
add_test (NAME py_aio COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/aio_checks.py)
|
||||||
|
|
||||||
|
add_test (NAME py_i2c_freq COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_freq.py)
|
||||||
|
add_test (NAME py_i2c_addr COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_addr.py)
|
||||||
|
add_test (NAME py_i2c_read COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read.py)
|
||||||
|
add_test (NAME py_i2c_write COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write.py)
|
||||||
|
add_test (NAME py_i2c_read_byte COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_byte.py)
|
||||||
|
add_test (NAME py_i2c_write_byte COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write_byte.py)
|
||||||
|
add_test (NAME py_i2c_read_byte_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_byte_data.py)
|
||||||
|
add_test (NAME py_i2c_write_byte_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write_byte_data.py)
|
||||||
|
add_test (NAME py_i2c_read_bytes_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_bytes_data.py)
|
||||||
|
add_test (NAME py_i2c_read_word_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_read_word_data.py)
|
||||||
|
add_test (NAME py_i2c_write_word_data COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/i2c_checks_write_word_data.py)
|
||||||
|
|
||||||
set_tests_properties(py_general
|
set_tests_properties(py_general
|
||||||
py_platform
|
py_platform
|
||||||
py_gpio_basic
|
py_gpio_basic
|
||||||
@@ -19,4 +31,15 @@ set_tests_properties(py_general
|
|||||||
py_gpio_isr
|
py_gpio_isr
|
||||||
py_gpio_mode
|
py_gpio_mode
|
||||||
py_aio
|
py_aio
|
||||||
|
py_i2c_freq
|
||||||
|
py_i2c_addr
|
||||||
|
py_i2c_read
|
||||||
|
py_i2c_write
|
||||||
|
py_i2c_read_byte
|
||||||
|
py_i2c_write_byte
|
||||||
|
py_i2c_read_byte_data
|
||||||
|
py_i2c_write_byte_data
|
||||||
|
py_i2c_read_bytes_data
|
||||||
|
py_i2c_read_word_data
|
||||||
|
py_i2c_write_word_data
|
||||||
PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}")
|
PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}")
|
||||||
|
|||||||
52
tests/mock/i2c_checks_addr.py
Normal file
52
tests/mock/i2c_checks_addr.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksAddr(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_address(self):
|
||||||
|
self.assertEqual(self.i2c.address(0x10),
|
||||||
|
m.SUCCESS,
|
||||||
|
"Setting address to 0x10 did not return success")
|
||||||
|
|
||||||
|
def test_i2c_address_invalid_bigger_than_max(self):
|
||||||
|
# For standard 7-bit addressing 0x7F is max address
|
||||||
|
self.assertEqual(self.i2c.address(0xFF),
|
||||||
|
m.ERROR_INVALID_PARAMETER,
|
||||||
|
"Setting address to 0xFF did not return INVALID_PARAMETER")
|
||||||
|
|
||||||
|
def test_i2c_address_invalid_smaller_than_min(self):
|
||||||
|
self.assertRaises(OverflowError, self.i2c.address, -100)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
63
tests/mock/i2c_checks_freq.py
Normal file
63
tests/mock/i2c_checks_freq.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksFreq(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_frequency_STD(self):
|
||||||
|
self.assertEqual(self.i2c.frequency(m.I2C_STD),
|
||||||
|
m.SUCCESS,
|
||||||
|
"Setting frequency to I2C_STD did not return success")
|
||||||
|
|
||||||
|
def test_i2c_frequency_FAST(self):
|
||||||
|
self.assertEqual(self.i2c.frequency(m.I2C_FAST),
|
||||||
|
m.SUCCESS,
|
||||||
|
"Setting frequency to I2C_FAST did not return success")
|
||||||
|
|
||||||
|
def test_i2c_frequency_HIGH(self):
|
||||||
|
self.assertEqual(self.i2c.frequency(m.I2C_HIGH),
|
||||||
|
m.SUCCESS,
|
||||||
|
"Setting frequency to I2C_HIGH did not return success")
|
||||||
|
|
||||||
|
def test_i2c_frequency_invalid_bigger_than_max(self):
|
||||||
|
self.assertEqual(self.i2c.frequency(100),
|
||||||
|
m.ERROR_INVALID_PARAMETER,
|
||||||
|
"Setting frequency to 100 did not return INVALID_PARAMETER")
|
||||||
|
|
||||||
|
def test_i2c_frequency_invalid_smaller_than_min(self):
|
||||||
|
self.assertEqual(self.i2c.frequency(-100),
|
||||||
|
m.ERROR_INVALID_PARAMETER,
|
||||||
|
"Setting frequency to -100 did not return INVALID_PARAMETER")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
58
tests/mock/i2c_checks_read.py
Normal file
58
tests/mock/i2c_checks_read.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksRead(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_read_full_reg_range(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
expected_res = bytearray([MRAA_MOCK_I2C_DATA_INIT_BYTE for i in range(MRAA_MOCK_I2C_DATA_LEN)])
|
||||||
|
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN)
|
||||||
|
self.assertEqual(res, expected_res, "I2C read() of full register range returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_part_reg_range(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
expected_res = bytearray([MRAA_MOCK_I2C_DATA_INIT_BYTE for i in range(MRAA_MOCK_I2C_DATA_LEN - 1)])
|
||||||
|
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN - 1)
|
||||||
|
self.assertEqual(res, expected_res, "I2C read() of partial register range returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
self.assertRaises(IOError, self.i2c.read, MRAA_MOCK_I2C_DATA_LEN)
|
||||||
|
|
||||||
|
def test_i2c_read_invalid_len_bigger_than_max(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
self.assertRaises(IOError, self.i2c.read, MRAA_MOCK_I2C_DATA_LEN + 1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
48
tests/mock/i2c_checks_read_byte.py
Normal file
48
tests/mock/i2c_checks_read_byte.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksReadByte(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_read_byte(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
expected_res = MRAA_MOCK_I2C_DATA_INIT_BYTE
|
||||||
|
res = self.i2c.readByte()
|
||||||
|
self.assertEqual(res, expected_res, "I2C readByte() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_byte_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
self.assertRaises(ValueError, self.i2c.readByte)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
52
tests/mock/i2c_checks_read_byte_data.py
Normal file
52
tests/mock/i2c_checks_read_byte_data.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksReadByteData(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_read_byte_data(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
expected_res = MRAA_MOCK_I2C_DATA_INIT_BYTE
|
||||||
|
res = self.i2c.readReg(MRAA_MOCK_I2C_DATA_LEN - 1)
|
||||||
|
self.assertEqual(res, expected_res, "I2C readReg() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_byte_data_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
self.assertRaises(ValueError, self.i2c.readReg, MRAA_MOCK_I2C_DATA_LEN - 1)
|
||||||
|
|
||||||
|
def test_i2c_read_byte_data_invalid_reg(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
self.assertRaises(ValueError, self.i2c.readReg, MRAA_MOCK_I2C_DATA_LEN)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
81
tests/mock/i2c_checks_read_bytes_data.py
Normal file
81
tests/mock/i2c_checks_read_bytes_data.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksReadBytesData(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_read_bytes_data(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
# Generate unique data bytes
|
||||||
|
data_to_write = bytearray([0xEE+i for i in range(MRAA_MOCK_I2C_DATA_LEN)])
|
||||||
|
self.i2c.write(data_to_write)
|
||||||
|
# We expect to read the last two bytes
|
||||||
|
expected_res = bytearray(data_to_write[-2:])
|
||||||
|
test_reg_addr = MRAA_MOCK_I2C_DATA_LEN - 2
|
||||||
|
test_read_len = 2
|
||||||
|
self.assertEqual(self.i2c.readBytesReg(test_reg_addr, test_read_len),
|
||||||
|
expected_res,
|
||||||
|
"I2C readBytesReg() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_bytes_data_length_bigger_than_max(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
# Generate unique data bytes
|
||||||
|
data_to_write = bytearray([0xEE+i for i in range(MRAA_MOCK_I2C_DATA_LEN)])
|
||||||
|
self.i2c.write(data_to_write)
|
||||||
|
# We expect to read the last two bytes
|
||||||
|
expected_res = bytearray(data_to_write[-2:])
|
||||||
|
test_reg_addr = MRAA_MOCK_I2C_DATA_LEN - 2
|
||||||
|
# Set the read length bigger than our data length
|
||||||
|
test_read_len = MRAA_MOCK_I2C_DATA_LEN + 2
|
||||||
|
self.assertEqual(self.i2c.readBytesReg(test_reg_addr, test_read_len),
|
||||||
|
expected_res,
|
||||||
|
"I2C readBytesReg() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_bytes_data_zero_length(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
self.assertRaises(IOError, self.i2c.readBytesReg, 0x0, 0)
|
||||||
|
|
||||||
|
def test_i2c_read_bytes_data_negative_length(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
self.assertRaises(ValueError, self.i2c.readBytesReg, 0x0, -1)
|
||||||
|
|
||||||
|
def test_i2c_read_bytes_data_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
self.assertRaises(IOError, self.i2c.readBytesReg, 0x0, MRAA_MOCK_I2C_DATA_LEN - 1)
|
||||||
|
|
||||||
|
def test_i2c_read_bytes_data_invalid_reg(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
self.assertRaises(IOError, self.i2c.readBytesReg, MRAA_MOCK_I2C_DATA_LEN, MRAA_MOCK_I2C_DATA_LEN - 1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
56
tests/mock/i2c_checks_read_word_data.py
Normal file
56
tests/mock/i2c_checks_read_word_data.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksReadWordData(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_read_word_data(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
high_byte = 0xAA
|
||||||
|
low_byte = 0xBB
|
||||||
|
expected_res = (high_byte << 8) + low_byte
|
||||||
|
self.i2c.writeReg(MRAA_MOCK_I2C_DATA_LEN - 2, high_byte)
|
||||||
|
self.i2c.writeReg(MRAA_MOCK_I2C_DATA_LEN - 1, low_byte)
|
||||||
|
res = self.i2c.readWordReg(MRAA_MOCK_I2C_DATA_LEN - 2)
|
||||||
|
self.assertEqual(res, expected_res, "I2C readWordReg() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_read_word_data_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
self.assertRaises(ValueError, self.i2c.readWordReg, MRAA_MOCK_I2C_DATA_LEN - 2)
|
||||||
|
|
||||||
|
def test_i2c_read_word_data_invalid_reg(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
self.assertRaises(ValueError, self.i2c.readReg, MRAA_MOCK_I2C_DATA_LEN)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
29
tests/mock/i2c_checks_shared.py
Normal file
29
tests/mock/i2c_checks_shared.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
MRAA_I2C_BUS_NUM = 0
|
||||||
|
# These are defined in mock_board.c
|
||||||
|
MRAA_MOCK_I2C_ADDR = 0x33
|
||||||
|
MRAA_MOCK_I2C_DATA_LEN = 10
|
||||||
|
MRAA_MOCK_I2C_DATA_INIT_BYTE = 0xAB
|
||||||
78
tests/mock/i2c_checks_write.py
Normal file
78
tests/mock/i2c_checks_write.py
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksWrite(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_write_full_reg_range(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN)])
|
||||||
|
self.assertEqual(self.i2c.write(data_to_write),
|
||||||
|
m.SUCCESS,
|
||||||
|
"I2C write() of full register range did not return success")
|
||||||
|
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN)
|
||||||
|
self.assertEqual(res,
|
||||||
|
data_to_write,
|
||||||
|
"I2C read() after write() of full register range returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_write_part_reg_range(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN - 1)])
|
||||||
|
self.assertEqual(self.i2c.write(data_to_write),
|
||||||
|
m.SUCCESS,
|
||||||
|
"I2C write() of partial register range did not return success")
|
||||||
|
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN - 1)
|
||||||
|
self.assertEqual(res,
|
||||||
|
data_to_write,
|
||||||
|
"I2C read() after write() of partial register range returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_write_len_bigger_than_max(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN + 1)])
|
||||||
|
self.assertEqual(self.i2c.write(data_to_write),
|
||||||
|
m.SUCCESS,
|
||||||
|
"I2C write() with length bigger than max did not return success")
|
||||||
|
res = self.i2c.read(MRAA_MOCK_I2C_DATA_LEN)
|
||||||
|
self.assertEqual(res,
|
||||||
|
data_to_write[:-1],
|
||||||
|
"I2C read() after write() with length bigger than max returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_write_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
data_to_write = bytearray([0xEE for i in range(MRAA_MOCK_I2C_DATA_LEN)])
|
||||||
|
self.assertEqual(self.i2c.write(data_to_write),
|
||||||
|
m.ERROR_UNSPECIFIED,
|
||||||
|
"I2C write() to invalid address did not return error")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
54
tests/mock/i2c_checks_write_byte.py
Normal file
54
tests/mock/i2c_checks_write_byte.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksWriteByte(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_write_byte(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
test_byte = 0xEE
|
||||||
|
self.assertEqual(self.i2c.writeByte(test_byte),
|
||||||
|
m.SUCCESS,
|
||||||
|
"I2C writeByte() did not return success")
|
||||||
|
self.assertEqual(self.i2c.readByte(),
|
||||||
|
test_byte,
|
||||||
|
"I2C readByte() after writeByte() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_write_byte_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
self.assertEqual(self.i2c.writeByte(0xEE),
|
||||||
|
m.ERROR_UNSPECIFIED,
|
||||||
|
"I2C writeByte() to invalid address did not return error")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
65
tests/mock/i2c_checks_write_byte_data.py
Normal file
65
tests/mock/i2c_checks_write_byte_data.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksWriteByteData(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_write_byte_data(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
test_byte = 0xEE
|
||||||
|
reg = MRAA_MOCK_I2C_DATA_LEN - 1
|
||||||
|
self.assertEqual(self.i2c.writeReg(reg, test_byte),
|
||||||
|
m.SUCCESS,
|
||||||
|
"I2C writeReg() did not return success")
|
||||||
|
self.assertEqual(self.i2c.readReg(reg),
|
||||||
|
test_byte,
|
||||||
|
"I2C readReg() after writeReg() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_write_byte_data_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
test_byte = 0xEE
|
||||||
|
reg = MRAA_MOCK_I2C_DATA_LEN - 1
|
||||||
|
self.assertEqual(self.i2c.writeReg(reg, test_byte),
|
||||||
|
m.ERROR_UNSPECIFIED,
|
||||||
|
"I2C writeReg() to invalid address did not return error")
|
||||||
|
|
||||||
|
def test_i2c_write_byte_data_invalid_reg(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
test_byte = 0xEE
|
||||||
|
reg = MRAA_MOCK_I2C_DATA_LEN
|
||||||
|
self.assertEqual(self.i2c.writeReg(reg, test_byte),
|
||||||
|
m.ERROR_UNSPECIFIED,
|
||||||
|
"I2C writeReg() with invalid register did not return error")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
70
tests/mock/i2c_checks_write_word_data.py
Normal file
70
tests/mock/i2c_checks_write_word_data.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
from i2c_checks_shared import *
|
||||||
|
|
||||||
|
class I2cChecksWriteWordData(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.i2c = m.I2c(MRAA_I2C_BUS_NUM)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.i2c
|
||||||
|
|
||||||
|
def test_i2c_write_word_data(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
high_byte = 0xAA
|
||||||
|
low_byte = 0xBB
|
||||||
|
test_word = (high_byte << 8) + low_byte
|
||||||
|
reg = MRAA_MOCK_I2C_DATA_LEN - 2
|
||||||
|
self.assertEqual(self.i2c.writeWordReg(reg, test_word),
|
||||||
|
m.SUCCESS,
|
||||||
|
"I2C writeWordReg() did not return success")
|
||||||
|
self.assertEqual(self.i2c.readReg(reg),
|
||||||
|
high_byte,
|
||||||
|
"I2C readReg() of higher byte after writeWordReg() returned unexpected data")
|
||||||
|
self.assertEqual(self.i2c.readReg(reg + 1),
|
||||||
|
low_byte,
|
||||||
|
"I2C readReg() of lower byte after writeWordReg() returned unexpected data")
|
||||||
|
|
||||||
|
def test_i2c_write_word_data_invalid_addr(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR - 1)
|
||||||
|
test_word = 0xAABB
|
||||||
|
reg = MRAA_MOCK_I2C_DATA_LEN - 2
|
||||||
|
self.assertEqual(self.i2c.writeWordReg(reg, test_word),
|
||||||
|
m.ERROR_UNSPECIFIED,
|
||||||
|
"I2C writeWordReg() to invalid address did not return error")
|
||||||
|
|
||||||
|
def test_i2c_write_word_data_invalid_reg(self):
|
||||||
|
self.i2c.address(MRAA_MOCK_I2C_ADDR)
|
||||||
|
test_word = 0xAABB
|
||||||
|
reg = MRAA_MOCK_I2C_DATA_LEN
|
||||||
|
self.assertEqual(self.i2c.writeWordReg(reg, test_word),
|
||||||
|
m.ERROR_UNSPECIFIED,
|
||||||
|
"I2C writeWordReg() with invalid register did not return error")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
import mraa as m
|
import mraa as m
|
||||||
import unittest as u
|
import unittest as u
|
||||||
|
|
||||||
PLATFORM_PINCOUNT = 2
|
PLATFORM_PINCOUNT = 4
|
||||||
PLATFORM_STD_ADC_RES_BITS = 10
|
PLATFORM_STD_ADC_RES_BITS = 10
|
||||||
PLATFORM_MAX_ADC_RES_BITS = 12
|
PLATFORM_MAX_ADC_RES_BITS = 12
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user