2017-11-17 15:10:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-2017, Yanzi Networks.
|
2017-11-28 19:03:47 +00:00
|
|
|
* Copyright (c) 2017, University of Bristol - http://www.bristol.ac.uk/
|
2017-11-17 15:10:45 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2018-03-02 18:06:19 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \addtogroup spi-hal
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
|
|
|
* Implementation of the platform-independent aspects of the SPI HAL
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-03-02 20:37:28 +00:00
|
|
|
#include <dev/spi.h>
|
2018-03-02 18:07:42 +00:00
|
|
|
#include "contiki.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2017-11-17 15:10:45 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_acquire(spi_device_t *dev)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
2018-04-03 11:58:47 +00:00
|
|
|
/* lock and open the bus */
|
|
|
|
return spi_arch_lock_and_open(dev);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_release(spi_device_t *dev)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
2018-04-03 11:58:47 +00:00
|
|
|
/* close and unlock the bus */
|
|
|
|
return spi_arch_close_and_unlock(dev);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_select(spi_device_t *dev)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2017-11-28 19:03:47 +00:00
|
|
|
return spi_arch_select(dev);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_deselect(spi_device_t *dev)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2017-11-28 19:03:47 +00:00
|
|
|
return spi_arch_deselect(dev);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
bool
|
|
|
|
spi_has_bus(spi_device_t *dev)
|
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-28 19:03:47 +00:00
|
|
|
return false;
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_has_lock(dev);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_write_byte(spi_device_t *dev, uint8_t data)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_transfer(dev, &data, 1, 0, 0, 0);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_write(spi_device_t *dev, const uint8_t *data, int size)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_transfer(dev, data, size, 0, 0, 0);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_read_byte(spi_device_t *dev, uint8_t *buf)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_transfer(dev, NULL, 0, buf, 1, 0);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_read(spi_device_t *dev, uint8_t *buf, int size)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_transfer(dev, NULL, 0, buf, size, 0);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_read_skip(spi_device_t *dev, int size)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_transfer(dev, NULL, 0, NULL, 0, size);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_transfer(spi_device_t *dev,
|
|
|
|
const uint8_t *wdata, int wsize,
|
|
|
|
uint8_t *rbuf, int rsize, int ignore)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wdata == NULL && wsize > 0) {
|
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rbuf == NULL && rsize > 0) {
|
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
return spi_arch_transfer(dev, wdata, wsize, rbuf, rsize, ignore);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_read_register(spi_device_t *dev, uint8_t reg, uint8_t *data, int size)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t status;
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the register first (will read a status) */
|
2017-11-28 19:03:47 +00:00
|
|
|
status = spi_write_byte(dev, reg);
|
2017-11-17 15:10:45 +00:00
|
|
|
if(status != SPI_DEV_STATUS_OK) {
|
|
|
|
return status;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
2017-11-17 15:10:45 +00:00
|
|
|
/* then read the value (will read the value) */
|
2017-11-28 19:03:47 +00:00
|
|
|
status = spi_read(dev, data, size);
|
2017-11-17 15:10:45 +00:00
|
|
|
if(status != SPI_DEV_STATUS_OK) {
|
|
|
|
return status;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
2017-11-17 15:10:45 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-28 19:03:47 +00:00
|
|
|
spi_status_t
|
|
|
|
spi_strobe(spi_device_t *dev, uint8_t strobe, uint8_t *result)
|
2017-11-17 15:10:45 +00:00
|
|
|
{
|
2018-03-02 19:36:22 +00:00
|
|
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-28 19:03:47 +00:00
|
|
|
if(!spi_arch_has_lock(dev)) {
|
2017-11-17 15:10:45 +00:00
|
|
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
|
|
}
|
2017-11-28 19:03:47 +00:00
|
|
|
|
|
|
|
return spi_arch_transfer(dev, &strobe, 1, result, 1, 0);
|
2017-11-17 15:10:45 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-03-02 18:06:19 +00:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|