mraa mock: Add SPI 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
9030ae2eeb
commit
bdbbfd03dd
@@ -90,6 +90,7 @@ set (mraa_LIB_MOCK_SRCS_NOAUTO
|
||||
${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_i2c.c
|
||||
${PROJECT_SOURCE_DIR}/src/mock/mock_board_spi.c
|
||||
)
|
||||
|
||||
if (JSONPLAT)
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "mock/mock_board_gpio.h"
|
||||
#include "mock/mock_board_aio.h"
|
||||
#include "mock/mock_board_i2c.h"
|
||||
#include "mock/mock_board_spi.h"
|
||||
|
||||
#define PLATFORM_NAME "MRAA mock platform"
|
||||
|
||||
@@ -55,7 +56,14 @@ mraa_mock_board()
|
||||
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 = 1;
|
||||
b->def_spi_bus = 0;
|
||||
b->spi_bus[0].bus_id = 0;
|
||||
b->spi_bus[0].slave_s = 0;
|
||||
b->spi_bus[0].cs = 4;
|
||||
b->spi_bus[0].mosi = 5;
|
||||
b->spi_bus[0].miso = 6;
|
||||
b->spi_bus[0].sclk = 7;
|
||||
|
||||
b->pwm_default_period = 0;
|
||||
b->pwm_max_period = 0;
|
||||
@@ -99,6 +107,16 @@ mraa_mock_board()
|
||||
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;
|
||||
b->adv_func->spi_init_raw_replace = &mraa_mock_spi_init_raw_replace;
|
||||
b->adv_func->spi_stop_replace = &mraa_mock_spi_stop_replace;
|
||||
b->adv_func->spi_bit_per_word_replace = &mraa_mock_spi_bit_per_word_replace;
|
||||
b->adv_func->spi_lsbmode_replace = &mraa_mock_spi_lsbmode_replace;
|
||||
b->adv_func->spi_mode_replace = &mraa_mock_spi_mode_replace;
|
||||
b->adv_func->spi_frequency_replace = &mraa_mock_spi_frequency_replace;
|
||||
b->adv_func->spi_write_replace = &mraa_mock_spi_write_replace;
|
||||
b->adv_func->spi_write_word_replace = &mraa_mock_spi_write_word_replace;
|
||||
b->adv_func->spi_transfer_buf_replace = &mraa_mock_spi_transfer_buf_replace;
|
||||
b->adv_func->spi_transfer_buf_word_replace = &mraa_mock_spi_transfer_buf_word_replace;
|
||||
|
||||
// Pin definitions
|
||||
int pos = 0;
|
||||
@@ -127,6 +145,30 @@ mraa_mock_board()
|
||||
b->pins[pos].i2c.pinmap = 0;
|
||||
pos++;
|
||||
|
||||
strncpy(b->pins[pos].name, "SPI0CS", 8);
|
||||
b->pins[pos].capabilities = (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 };
|
||||
b->pins[pos].spi.mux_total = 0;
|
||||
b->pins[pos].spi.pinmap = 0;
|
||||
pos++;
|
||||
|
||||
strncpy(b->pins[pos].name, "SPI0MOSI", 8);
|
||||
b->pins[pos].capabilities = (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 };
|
||||
b->pins[pos].spi.mux_total = 0;
|
||||
b->pins[pos].spi.pinmap = 0;
|
||||
pos++;
|
||||
|
||||
strncpy(b->pins[pos].name, "SPI0MISO", 8);
|
||||
b->pins[pos].capabilities = (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 };
|
||||
b->pins[pos].spi.mux_total = 0;
|
||||
b->pins[pos].spi.pinmap = 0;
|
||||
pos++;
|
||||
|
||||
strncpy(b->pins[pos].name, "SPI0SCLK", 8);
|
||||
b->pins[pos].capabilities = (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 };
|
||||
b->pins[pos].spi.mux_total = 0;
|
||||
b->pins[pos].spi.pinmap = 0;
|
||||
pos++;
|
||||
|
||||
return b;
|
||||
|
||||
error:
|
||||
|
||||
177
src/mock/mock_board_spi.c
Normal file
177
src/mock/mock_board_spi.c
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#if defined(MSYS)
|
||||
#define __USE_LINUX_IOCTL_DEFS
|
||||
#endif
|
||||
#include <sys/ioctl.h>
|
||||
#if defined(MSYS)
|
||||
// There's no spidev.h on MSYS, so we need to provide our own,
|
||||
// and only *after* including ioctl.h as that one contains prerequisites.
|
||||
#include "linux/spi_kernel_headers.h"
|
||||
#else
|
||||
#include <linux/spi/spidev.h>
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
#include "mock/mock_board_spi.h"
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_init_raw_replace(mraa_spi_context dev, unsigned int bus, unsigned int cs)
|
||||
{
|
||||
dev->clock = MOCK_SPI_DEFAULT_FREQ;
|
||||
|
||||
if ((mraa_spi_mode(dev, MOCK_SPI_DEFAULT_MODE) != MRAA_SUCCESS) ||
|
||||
(mraa_spi_lsbmode(dev, MOCK_SPI_DEFAULT_LSBMODE) != MRAA_SUCCESS) ||
|
||||
(mraa_spi_bit_per_word(dev, MOCK_SPI_DEFAULT_BIT_PER_WORD) != MRAA_SUCCESS)) {
|
||||
return MRAA_ERROR_INVALID_RESOURCE;
|
||||
}
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_stop_replace(mraa_spi_context dev)
|
||||
{
|
||||
free(dev);
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_bit_per_word_replace(mraa_spi_context dev, unsigned int bits)
|
||||
{
|
||||
if (bits == 0) {
|
||||
syslog(LOG_ERR, "spi: bit_per_word: Cannot set to zero");
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
dev->bpw = bits;
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_lsbmode_replace(mraa_spi_context dev, mraa_boolean_t lsb)
|
||||
{
|
||||
dev->lsb = lsb;
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_mode_replace(mraa_spi_context dev, mraa_spi_mode_t mode)
|
||||
{
|
||||
uint8_t spi_mode = 0;
|
||||
|
||||
switch (mode) {
|
||||
case MRAA_SPI_MODE0:
|
||||
spi_mode = SPI_MODE_0;
|
||||
break;
|
||||
case MRAA_SPI_MODE1:
|
||||
spi_mode = SPI_MODE_1;
|
||||
break;
|
||||
case MRAA_SPI_MODE2:
|
||||
spi_mode = SPI_MODE_2;
|
||||
break;
|
||||
case MRAA_SPI_MODE3:
|
||||
spi_mode = SPI_MODE_3;
|
||||
break;
|
||||
default:
|
||||
syslog(LOG_ERR, "spi: mode: Invalid SPI mode selected", mode);
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
dev->mode = spi_mode;
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_frequency_replace(mraa_spi_context dev, int hz)
|
||||
{
|
||||
if (hz <= 0) {
|
||||
syslog(LOG_ERR, "spi: frequency: Cannot set to zero or negative");
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
dev->clock = hz;
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
int
|
||||
mraa_mock_spi_write_replace(mraa_spi_context dev, uint8_t data)
|
||||
{
|
||||
return (int) (data ^ MOCK_SPI_REPLY_DATA_MODIFIER_BYTE);
|
||||
}
|
||||
|
||||
int
|
||||
mraa_mock_spi_write_word_replace(mraa_spi_context dev, uint16_t data)
|
||||
{
|
||||
return (int) (data ^ MOCK_SPI_REPLY_DATA_MODIFIER_WORD);
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_transfer_buf_replace(mraa_spi_context dev, uint8_t* data, uint8_t* rxbuf, int length)
|
||||
{
|
||||
if (data == NULL) {
|
||||
syslog(LOG_ERR, "spi: transfer_buf: Incoming data is null, cannot proceed");
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (length <= 0) {
|
||||
syslog(LOG_ERR, "spi: transfer_buf: Length given is equal to or less than zero, cannot proceed");
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (rxbuf != NULL) {
|
||||
for (int i = 0; i < length; ++i) {
|
||||
rxbuf[i] = data[i] ^ MOCK_SPI_REPLY_DATA_MODIFIER_BYTE;
|
||||
}
|
||||
}
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_mock_spi_transfer_buf_word_replace(mraa_spi_context dev, uint16_t* data, uint16_t* rxbuf, int length)
|
||||
{
|
||||
if (data == NULL) {
|
||||
syslog(LOG_ERR, "spi: transfer_buf_word: Incoming data is null, cannot proceed");
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (length <= 0) {
|
||||
syslog(LOG_ERR, "spi: transfer_buf_word: Length given is equal to or less than zero, cannot proceed");
|
||||
return MRAA_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (rxbuf != NULL) {
|
||||
// length is given in bytes, but arrays are comprised of words
|
||||
for (int i = 0; i < (length / 2); ++i) {
|
||||
rxbuf[i] = data[i] ^ MOCK_SPI_REPLY_DATA_MODIFIER_WORD;
|
||||
}
|
||||
}
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user