Program Listing for File spic-wiced.cpp

Return to documentation for file (src/framework/wiced-43xxx/pal/spic-wiced.cpp)

#include "spic-wiced.hpp"

#if (TLE5012_FRAMEWORK == TLE5012_FRMWK_WICED)
#include "wiced_rtos.h"
#include <wiced.h>
#include <platform.h>

using namespace tle5012;

SPICWiced::SPICWiced(wiced_gpio_t csPin)
{
    this->spi.port = WICED_SPI_0;
    this->spi.chip_select = csPin;
    this->spi.speed = 1000000;
    this->spi.mode = (SPI_CLOCK_RISING_EDGE | SPI_CLOCK_IDLE_LOW | SPI_NO_DMA | SPI_MSB_FIRST | SPI_CS_ACTIVE_LOW);
    this->spi.bits = 16U;
    this->csPin = csPin;
}

SPICWiced::SPICWiced(wiced_spi_t port, wiced_gpio_t csPin, wiced_gpio_t misoPin , wiced_gpio_t mosiPin , wiced_gpio_t sckPin )
{
    this->spi.port = port;
    this->spi.chip_select = csPin;
    this->spi.speed = 1000000;
    this->spi.mode = (SPI_CLOCK_RISING_EDGE | SPI_CLOCK_IDLE_LOW | SPI_NO_DMA | SPI_MSB_FIRST | SPI_CS_ACTIVE_LOW);
    this->spi.bits = 16U;
    this->csPin   = csPin;
    this->misoPin = misoPin;
    this->mosiPin = mosiPin;
    this->sckPin  = sckPin;
}

SPICWiced::~SPICWiced()
{
    deinit();
}
SPICWiced::Error_t SPICWiced::init()
{
    this->segment.tx_buffer = sendBuffer;
    this->segment.rx_buffer = receiveBuffer;
    this->segment.length = 2;

    wiced_spi_init( &this->spi );
    wiced_gpio_init(this->csPin, OUTPUT_PUSH_PULL);
    return OK;
}

SPICWiced::Error_t SPICWiced::deinit()
{
    wiced_spi_deinit( &this->spi );
    return OK;
}

SPICWiced::Error_t SPICWiced::transfer16(uint16_t send, uint16_t &received)
{
    sendBuffer[0] = (uint8_t)((send >> 8) & 0xFF);
    sendBuffer[1] = (uint8_t)(send & 0xFF);

    wiced_spi_transfer( &this->spi, &this->segment, 1);
    received = (uint16_t)(((uint16_t)receiveBuffer[0] << 8) | (receiveBuffer[1]));

    return OK;
}

SPICWiced::Error_t SPICWiced::triggerUpdate()
{
    wiced_gpio_output_low(this->sckPin);
    wiced_gpio_output_high(this->mosiPin);
    wiced_gpio_output_low(this->csPin);
    // grace period for register snapshot
    wiced_rtos_delay_microseconds( 5 );
    wiced_gpio_output_high(this->csPin);
    return OK;
}

SPICWiced::Error_t SPICWiced::sendReceive(uint16_t* sent_data, uint16_t size_of_sent_data, uint16_t* received_data, uint16_t size_of_received_data)
{
    uint32_t data_index = 0;
    //send via TX

    wiced_gpio_init(this->misoPin, INPUT_HIGH_IMPEDANCE);
    wiced_gpio_init(this->mosiPin, OUTPUT_PUSH_PULL);
    wiced_gpio_output_low(this->csPin);
    for(data_index = 0; data_index < size_of_sent_data; data_index++)
    {
        transfer16(sent_data[data_index],received_data[0]);
    }

    // For timing reasons we use only one gpio change
    // wiced_gpio_init(this->misoPin, OUTPUT_PUSH_PULL);
    wiced_gpio_init(this->mosiPin, INPUT_HIGH_IMPEDANCE);
    wiced_rtos_delay_microseconds( 5 );

    for(data_index = 0; data_index < size_of_received_data; data_index++)
    {
        transfer16(0x0000,received_data[data_index]);
    }

    wiced_gpio_output_high(this->csPin);
    return OK;
}

#endif