Skip to content

Instantly share code, notes, and snippets.

@andresv
Created July 31, 2012 12:21
Show Gist options
  • Save andresv/3216672 to your computer and use it in GitHub Desktop.
Save andresv/3216672 to your computer and use it in GitHub Desktop.
ccbee uart test application
/*
* Copyright (c) 2012 Andres Vahter.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - 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.
*
* - Neither the name of the copyright holders 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 HOLDERS 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.
*/
#ifndef BRIDGESTATION_H
#define BRIDGESTATION_H
enum {
AM_TRANSPORT = 0x37, // transport packet am id
WAIT_UART_PACKET_END_TIME = 10, // specifies how many ms are waited for the next byte until packet is sent
RADIO_OUT_QUEUE_SIZE = 10,
UART_OUT_QUEUE_SIZE = 10,
};
typedef nx_struct {
nx_uint8_t data[TOSH_DATA_LENGTH];
} transport_msg_t;
#endif
/*
* Copyright (c) 2012 Andres Vahter.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - 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.
*
* - Neither the name of the copyright holders 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 HOLDERS 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.
*/
#include "BridgeStation.h"
configuration BridgeStationAppC {}
implementation {
components BridgeStationC as App, LedsC, MainC;
App.Leds -> LedsC;
App.Boot -> MainC.Boot;
components new AMSenderC(AM_TRANSPORT);
components new AMReceiverC(AM_TRANSPORT);
components ActiveMessageC;
App.RadioReceive -> AMReceiverC;
App.RadioSend -> AMSenderC;
App.RadioControl -> ActiveMessageC;
App.RadioPacket -> AMSenderC;
components PlatformSerialC;
App.UartControl -> PlatformSerialC;
App.UartStream -> PlatformSerialC;
components new TimerMilliC() as UartRxTimer;
App.UartRxTimer -> UartRxTimer;
components new TimerMilliC() as DebugTimer;
App.DebugTimer -> DebugTimer;
components new PoolC(message_t, RADIO_OUT_QUEUE_SIZE) as RadioOutPoolC;
App.RadioOutPool -> RadioOutPoolC;
components new QueueC(message_t*, RADIO_OUT_QUEUE_SIZE) as RadioOutQueueC;
App.RadioOutQueue -> RadioOutQueueC;
components new PoolC(message_t, UART_OUT_QUEUE_SIZE) as UartOutPoolC;
App.UartOutPool -> UartOutPoolC;
components new QueueC(message_t*, UART_OUT_QUEUE_SIZE) as UartOutQueueC;
App.UartOutQueue -> UartOutQueueC;
}
/*
* Copyright (c) 2012 Andres Vahter.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - 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.
*
* - Neither the name of the copyright holders 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 HOLDERS 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.
*/
#include "BridgeStation.h"
module BridgeStationC {
uses {
interface Leds;
interface Boot;
interface Timer<TMilli> as UartRxTimer;
interface Timer<TMilli> as DebugTimer;
interface SplitControl as RadioControl;
interface Receive as RadioReceive;
interface AMSend as RadioSend;
interface Packet as RadioPacket;
interface StdControl as UartControl;
interface UartStream;
interface Pool<message_t> as RadioOutPool;
interface Queue<message_t*> as RadioOutQueue;
interface Pool<message_t> as UartOutPool;
interface Queue<message_t*> as UartOutQueue;
}
}
implementation {
message_t* uart_rx_message = NULL;
message_t* uart_tx_message = NULL;
uint8_t bytes_received = 0;
uint8_t rx_byte;
bool radio_busy = FALSE;
bool uart_busy = FALSE;
task void ByteReceivedTask();
task void SendToRadioTask();
task void SendToUartTask();
event void Boot.booted() {
uart_rx_message = call RadioOutPool.get();
call UartControl.start();
//call RadioControl.start();
call DebugTimer.startPeriodic(128);
}
event void DebugTimer.fired() {
call Leds.led1Toggle();
if (call UartStream.send((uint8_t*)"ABC", 3) == SUCCESS) {
uart_busy = TRUE;
}
}
//----------------------------------------------------------------------
// Peripheral start/stop events
//----------------------------------------------------------------------
event void RadioControl.startDone(error_t error) {
if (error == SUCCESS) {
call UartStream.enableReceiveInterrupt();
}
else {
call RadioControl.start();
}
}
event void RadioControl.stopDone(error_t error) {}
//----------------------------------------------------------------------
// Radio handling
//----------------------------------------------------------------------
event message_t* RadioReceive.receive(message_t* msg, void* payload, uint8_t len) {
call Leds.led0Toggle();
if (!call UartOutPool.empty() && call UartOutQueue.size() < call UartOutQueue.maxSize()) {
message_t* tmp = call UartOutPool.get();
call UartOutQueue.enqueue(msg);
post SendToUartTask();
return tmp;
}
return msg;
}
void SendToRadio() {
if (!call RadioOutPool.empty() && call RadioOutQueue.size() < call RadioOutQueue.maxSize()) {
call RadioOutQueue.enqueue(uart_rx_message);
uart_rx_message = call RadioOutPool.get();
}
post SendToRadioTask();
}
task void SendToRadioTask() {
if (!call RadioOutQueue.empty() && !radio_busy) {
message_t* msg = call RadioOutQueue.dequeue();
uint8_t len = call RadioPacket.payloadLength(msg);
if (call RadioSend.send(0xFFFF, msg, len) == SUCCESS) {
radio_busy = TRUE;
call Leds.led2Toggle();
}
else {
call RadioOutPool.put(msg);
post SendToRadioTask();
}
}
}
event void RadioSend.sendDone(message_t* msg, error_t error) {
call RadioOutPool.put(msg);
radio_busy = FALSE;
post SendToRadioTask();
}
//----------------------------------------------------------------------
// UART handling
//----------------------------------------------------------------------
task void SendToUartTask() {
if (!call UartOutQueue.empty() && !uart_busy) {
uint8_t len;
uart_tx_message = call UartOutQueue.dequeue();
len = call RadioPacket.payloadLength(uart_tx_message);
if (call UartStream.send((uint8_t*)uart_tx_message->data, len) == SUCCESS) {
uart_busy = TRUE;
}
else {
call UartOutPool.put(uart_tx_message);
post SendToUartTask();
}
}
}
async event void UartStream.sendDone(uint8_t* buf, uint16_t len, error_t error) {
//call UartOutPool.put(uart_tx_message);
uart_busy = FALSE;
//post SendToUartTask();
}
async event void UartStream.receivedByte(uint8_t byte) {
rx_byte = byte;
post ByteReceivedTask();
}
task void ByteReceivedTask() {
uint8_t* payload_p = (uint8_t*)call RadioPacket.getPayload(uart_rx_message, TOSH_DATA_LENGTH);
// fill in message_t payload with bytes received from Uart
payload_p[bytes_received] = rx_byte;
bytes_received++;
call RadioPacket.setPayloadLength(uart_rx_message, bytes_received);
if (bytes_received < TOSH_DATA_LENGTH) {
// wait a little, there might be more data coming from Uart
call UartRxTimer.startOneShot(WAIT_UART_PACKET_END_TIME);
}
else {
// payload is full, try to send data to radio
bytes_received = 0;
SendToRadio();
}
}
event void UartRxTimer.fired() {
bytes_received = 0;
SendToRadio();
}
async event void UartStream.receiveDone(uint8_t* buf, uint16_t len, error_t error) {
}
}
COMPONENT=BridgeStationAppC
CFLAGS += -DTOSH_DATA_LENGTH=40
CFLAGS += -DTOS_DEFAULT_BAUDRATE=38400 # tos/chips/msp430/x5xxx/usci/msp430usci.h
CFLAGS += -DSMARTRF_SETTING_PATABLE0=0xC0
include $(MAKERULES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment