siwicom/C/rfProtocol.c

213 lines
7.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*================================================================================================
rfProtocol.c SiWiCom copyright 2006 v1.0 (2006-06-16)
Name: Christoffer Martinsson E-mail: cm@cmtec.se
Name: Joakim Nilsson E-mail: mail@jopin.se
Description: Routines for rfProtocol. Uses cc1100.h as tranceiver.
Overhead and CRC is added in cc1100 hardware.
BytePos: 0 1 2 3 5
------------ ------------ --------- ------ ------
Outgoing packet: | targetAddr | deviceAddr | command | data | ... | data |
------------ ------------ --------- ------ ------
BytePos: 0 1 2 3 5 6 7
------------ ------------ --------- ------ ------ ------ ---------
Incoming packet: | targetAddr | deviceAddr | command | data | ... | data | RSSI | LQI/CRC |
------------ ------------ --------- ------ ------ ------ ---------
--------------------------------------------------------------------------------------------
Name: nrOfBytes: Value: Master: Slave:
--------------------------------------------------------------------------------------------
targetAddr 1 Target address R/W R/W
deviceAddr 1 Device address R/W R/W
Command 1 0x11 DATA R W
0xFA ACK + UPDATE W R
0xFE ACK R/W R/W
Data 3 Value for the specified command above. R/W R/W
DATA: Byte0 data[0]
Byte1 data[1]
Byte2 data[2]
ACK + UPDATE: Byte0 New master address (0x00 = no change)
Byte1 New device address (0x00 = no change)
Byte2 New wakeup time (0x00 = no change)
ACK: Byte0 0x55
Byte1 0x55
Byte2 0x55
================================================================================================*/
#include <hardware.h>
#include <inttypes.h>
#include <avr/io.h>
#include <rfProtocol.h>
#include <cc1100.h>
static uint8_t deviceAddr = 0x00;
static uint8_t masterAddr = 0x00;
/*================================================================================================
Functions
================================================================================================*/
/*================================================================================================
rfProtocolSendPacket
Description: Write a data packet to the cc1100 TX-FIFO
Input: *dataBuffer (Pointer to data array)
bufferSize (number of element in the dataBuffer array)
Return: -
------------------------------------------------------------------------------------------------*/
static void rfProtocolSendPacket(uint8_t *dataBuffer, uint8_t bufferSize)
{
cc1100WriteBurstReg(TXFIFO, dataBuffer, bufferSize); // Copy data to cc1100 FIFO
cc1100WriteCommand(STX); // Send packet
uint16_t sendTimeOut = 0;
// wait for cc1100 to enter TX_END mode
while((!(cc1100ReadStatusReg(MARCSTATE) == 0x01)) && (sendTimeOut++ < 1000));
}
/*================================================================================================
rfProtocolSendData
Description: Write a data packet to the cc1100 (command = DATA).
Input: *data (3byte data array)
Return: -
------------------------------------------------------------------------------------------------*/
void rfProtocolSendData(uint8_t *data)
{
uint8_t tempBuffer[] = {masterAddr, deviceAddr, RF_COMMAND_DATA,
data[0], data[1], data[2]};
rfProtocolSendPacket(tempBuffer, sizeof(tempBuffer));
}
/*================================================================================================
rfProtocolSendACK
Description: Write a data packet to the cc1100 (command = ACK).
Input: targetAddr (specific address for the slave device)
Return: -
------------------------------------------------------------------------------------------------*/
void rfProtocolSendACK(uint8_t targetAddr)
{
uint8_t tempBuffer[] = {targetAddr, deviceAddr, RF_COMMAND_ACK,
RF_PADDING, RF_PADDING, RF_PADDING};
rfProtocolSendPacket(tempBuffer, sizeof(tempBuffer));
}
/*================================================================================================
rfProtocolSendACKAndUpdate
Description: Write a data packet to the cc1100 (command = ACK_AND_UPDATE).
Input: targetAddr (specific address for the slave device)
*data (3byte data array)
Return: -
------------------------------------------------------------------------------------------------*/
void rfProtocolSendACKAndUpdate(uint8_t targetAddr, uint8_t *data)
{
uint8_t tempBuffer[] = {targetAddr, deviceAddr, RF_COMMAND_ACK_AND_UPDATE,
data[0], data[1], data[2]};
rfProtocolSendPacket(tempBuffer, sizeof(tempBuffer));
}
/*================================================================================================
rfProtocolReadPacket
Description: Add one byte to the input buffer and validate packet when buffer is full.
Input: *dataBuffer (Pointer to array[5] for storing data)
Return: type of packet. 0 = Packer ERROR
------------------------------------------------------------------------------------------------*/
uint8_t rfProtocolReadPacket(uint8_t *dataBuffer)
{
uint8_t tempBuffer[8];
uint8_t i;
//Check if bytes in FIFO
if((cc1100ReadStatusReg(RXBYTES) & 0x7F) >= sizeof(tempBuffer))
{
cc1100ReadBurstReg(RXFIFO, tempBuffer, sizeof(tempBuffer));
for(i = 0; i < 6; i++)
{
dataBuffer[i] = tempBuffer[i+1];
}
// Check if packet is addressed for this device
if((tempBuffer[0] == deviceAddr) || (tempBuffer[0] == 0x00))
{
return tempBuffer[2]; // return
}
}
return 0;
}
/*================================================================================================
rfProtocolSetDeviceAddr
Description: Set new device address.
Input: newDeviceAddr (New device address)
Return: -
------------------------------------------------------------------------------------------------*/
void rfProtocolSetDeviceAddr(uint8_t newDeviceAddr)
{
deviceAddr = newDeviceAddr;
}
/*================================================================================================
rfProtocolGetDeviceAddr
Description: Get current device address
Input: -
Return: deviceAddr
------------------------------------------------------------------------------------------------*/
uint8_t rfProtocolGetDeviceAddr(void)
{
return deviceAddr;
}
/*================================================================================================
rfProtocolSetMasterAddr
Description: Set new master device address.
Input: newMasterAddr (New device address)
Return: -
------------------------------------------------------------------------------------------------*/
void rfProtocolSetMasterAddr(uint8_t newMasterAddr)
{
masterAddr = newMasterAddr;
}
/*================================================================================================
rfProtocolGetMasterAddr
Description: Get current master device address
Input: -
Return: masterAddr
------------------------------------------------------------------------------------------------*/
uint8_t rfProtocolGetMasterAddr(void)
{
return masterAddr;
}
/*================================================================================================
End
================================================================================================*/