Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

sdk/mbed/WiconnectSerial.cpp

Committer:
dan_ackme
Date:
2014-08-11
Revision:
0:ea85c4bb5e1f
Child:
1:6ec9998427ad

File content as of revision 0:ea85c4bb5e1f:

/*
 * Copyright 2014, ACKme Networks
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks;
 * the contents of this file may not be disclosed to third parties, copied
 * or duplicated in any form, in whole or in part, without the prior
 * written permission of ACKme Networks.
 */

#include <assert.h>

#include "Wiconnect.h"
#include "internal/common.h"

#ifndef WICONNECT_SERIAL_RX_BUFFER
#error WICONNECT_SERIAL_RX_BUFFER NOT defined NOT currently supported
#endif




typedef struct
{
    uint16_t size;
    uint8_t *start, *end;
    volatile uint8_t *head;
    volatile uint8_t *tail;
    volatile uint16_t count;
} SerialRxBuffer;


/*************************************************************************************************/
WiconnectSerial::WiconnectSerial(const SerialConfig  &config, Wiconnect *wiconnect) : RawSerial(config.tx, config.rx)
{
    ct_assert(sizeof(ringBuffer) >= sizeof(SerialRxBuffer));

    baud(config.baud);

    SerialRxBuffer *rxBuffer = (SerialRxBuffer*)ringBuffer;
    memset(rxBuffer, 0, sizeof(SerialRxBuffer));
    bufferAlloc = false;
    if(config.serialRxBufferSize > 0)
    {
        if(config.serialRxBuffer != NULL)
        {
            rxBuffer->head = (uint8_t*)config.serialRxBuffer;
        }
#ifdef WICONNECT_ENABLE_MALLOC
        else
        {
            assert(wiconnect->_malloc != NULL);
            rxBuffer->start = (uint8_t*)wiconnect->_malloc(config.serialRxBufferSize);
            bufferAlloc = true;
        }
#endif
        rxBuffer->head = rxBuffer->tail = (volatile uint8_t*)rxBuffer->start;
        rxBuffer->end = (uint8_t*)rxBuffer->head + config.serialRxBufferSize;
        rxBuffer->size = config.serialRxBufferSize;
        attach(this, &WiconnectSerial::rxIrqHandler, SerialBase::RxIrq);
    }
}

/*************************************************************************************************/
WiconnectSerial::~WiconnectSerial()
{
#ifdef WICONNECT_ENABLE_MALLOC
    if(bufferAlloc)
    {
        SerialRxBuffer *rxBuffer = (SerialRxBuffer*)ringBuffer;
        Wiconnect::getInstance()->_free(rxBuffer->start);
    }
#endif
}

/*************************************************************************************************/
void WiconnectSerial::flush(void)
{
    while (readable())
    {
        int c = getc();
    }

    SerialRxBuffer *rxBuffer = (SerialRxBuffer*)ringBuffer;
    rxBuffer->tail = rxBuffer->head = rxBuffer->start;
    rxBuffer->count = 0;
}

/*************************************************************************************************/
int WiconnectSerial::write(const void *data, int bytesToWrite, int timeoutMs)
{
    uint8_t *ptr = (uint8_t*)data;
    int remaining = bytesToWrite;

    while(remaining > 0)
    {
        if(!writeable())
        {
            timeoutTimer.reset();
            while(!writeable())
            {
                if(timeoutMs == 0)
                {
                    if(timeoutTimer.readUs() >= 500)
                    {
                        goto exit;
                    }
                }
                else
                {
                    if(timeoutTimer.readUs() >= timeoutMs*1000)
                    {
                        goto exit;
                    }
                }
            }
        }

        putc(*ptr);
        ++ptr;
        --remaining;
    }

    exit:
    return bytesToWrite - remaining;
}

/*************************************************************************************************/
int WiconnectSerial::read(void *data, int bytesToRead, int timeoutMs)
{
    SerialRxBuffer *rxBuffer = (SerialRxBuffer*)ringBuffer;
    uint8_t *ptr = (uint8_t*)data;
    int remaining = bytesToRead;

    while(remaining > 0)
    {
        if(rxBuffer->count == 0)
        {
            if(timeoutMs == 0)
            {
                break;
            }
            else
            {
                timeoutTimer.reset();
                while(rxBuffer->count == 0)
                {
                    if(timeoutTimer.readUs() >= timeoutMs*1000)
                    {
                        goto exit;
                    }
                }
            }
        }

        *ptr++ = *rxBuffer->tail++;
        --remaining;
        --rxBuffer->count;
        if(rxBuffer->tail >= rxBuffer->end)
            rxBuffer->tail = rxBuffer->start;
    }

exit:
    return bytesToRead - remaining;
}

/*************************************************************************************************/
void WiconnectSerial::rxIrqHandler(void)
{
    SerialRxBuffer *rxBuffer = (SerialRxBuffer*)ringBuffer;

    while (readable() && rxBuffer->count < rxBuffer->size)
    {
        *rxBuffer->head++ = (uint8_t)getc();
        ++rxBuffer->count;
        if(rxBuffer->head >= rxBuffer->end)
            rxBuffer->head = rxBuffer->start;
    }
}