siwicom/C/cc1100.c

196 lines
5.8 KiB
C

/*================================================================================================
cc1100.c SiWiCom copyright 2006 v1.0 (2006-05-11)
Name: Joakim Nilsson E-mail: mail@jopin.se
Name: Christoffer Martinsson E-mail: cm@cmtec.se
Description: Routines for cc1100. Uses usart.h as tranceiver
Hardware configuration is done by hardware.h witch must contain definitions for:
(X=letter i.e PORTX=PORTB, n=number i.e PXn=PB4)
GDO0 PXn (Example: #define GDO0 PC1)
GDO0_PORT PORTX (Example: #define GDO0_PORT PORTC)
GDO0_DDR DDRX (Example: #define GDO0_DDR DDRC)
GDO0_PIN PINX (Example: #define GDO0_PIN PINC)
GDO2 PXn (Example: #define GDO2 PD3)
GDO2_PORT PORTX (Example: #define GDO2_PORT PORTD)
GDO2_DDR DDRX (Example: #define GDO2_DDR DDRD)
GDO2_PIN PINX (Example: #define GDO2_PIN PIND)
CS_CC1100 SPI_CSX_NR (Example: #define CS_CC1100 SPI_CS0_NR)
================================================================================================*/
#include "hardware.h"
#include <util/delay.h>
#include <inttypes.h>
#include <avr/io.h>
#include "spi.h"
#include "cc1100.h"
/*================================================================================================
Functions
================================================================================================*/
/*================================================================================================
cc1100Select
Description: Select cc1100 as slave
Input: -
Output: -
------------------------------------------------------------------------------------------------*/
void cc1100Select(void)
{
spiChipSelect(CS_CC1100);
while(SPI_PIN & (1<<MISO));
}
/*===============================================================================================
cc1100DeSelect
Description: DeSelect cc1100
Input: -
Output: -
------------------------------------------------------------------------------------------------*/
void cc1100DeSelect(void)
{
spiChipDeSelect();
}
/*================================================================================================
cc1100WriteBurstReg
Description: Write to cc1100 registers in burst mode.
Input: startAddr (Address to start writing)
*regValueBuffer (data buffer contaning all data that should be written to cc1100)
bufferSize (size of de data buffer)
Output: -
------------------------------------------------------------------------------------------------*/
void cc1100WriteBurstReg(uint8_t startAddr, uint8_t *regValueBuffer, uint8_t bufferSize)
{
cc1100Select();
spiWrite(startAddr|(BURST<<6)|(WRITE<<7));
uint8_t i;
for(i = 0; i < bufferSize; i++)
{
spiWrite(regValueBuffer[i]);
}
cc1100DeSelect();
}
/*================================================================================================
cc1100ReadBurstReg
Description: read from cc1100 registers in burst mode.
Input: startAddr (Address to start reading)
*regValueBuffer (data buffer for storing all data read from the cc1100)
bufferSize (size of de data buffer)
Output: -
------------------------------------------------------------------------------------------------*/
void cc1100ReadBurstReg(uint8_t startAddr, uint8_t *regValueBuffer, uint8_t bufferSize)
{
cc1100Select();
spiWrite(startAddr|(BURST<<6)|(READ<<7));
uint8_t i;
for(i = 0; i < bufferSize; i++)
{
spiWrite(0);
regValueBuffer[i] = spiRead();
}
cc1100DeSelect();
}
/*================================================================================================
cc1100WriteReg
Description: Write to a cc1100 register.
Input: regAddr (register address)
regValue (new rigiter value)
Output: -
------------------------------------------------------------------------------------------------*/
void cc1100WriteReg(uint8_t regAddr, uint8_t regValue)
{
cc1100Select();
spiWrite(regAddr|(NOBURST<<6)|(WRITE<<7));
spiWrite(regValue);
cc1100DeSelect();
}
/*================================================================================================
cc1100ReadReg
Description: Read from a cc1100 register.
Input: regAddr (register address)
Output: regiter value
------------------------------------------------------------------------------------------------*/
uint8_t cc1100ReadReg(uint8_t regAddr)
{
uint8_t data;
cc1100Select();
spiWrite(regAddr|(NOBURST<<6)|(READ<<7));
spiWrite(0);
data = spiRead();
cc1100DeSelect();
return data;
}
/*===============================================================================================
cc1100WriteCommand
Description: Write a command to cc1100.
Input: cmdAddr (command register address, in the range 0x30 - 0x3D)
Output: -
------------------------------------------------------------------------------------------------*/
void cc1100WriteCommand(uint8_t cmdAddr)
{
cc1100Select();
spiWrite(cmdAddr); //Command strobe (R/W bit=0 (WRITE), Burst bit=0=command strobes)
cc1100DeSelect();
}
/*================================================================================================
cc1100ReadStatusReg
Description: Read status register from cc1100
Input: regAddr (register address)
Output: regiter value
------------------------------------------------------------------------------------------------*/
uint8_t cc1100ReadStatusReg(uint8_t regAddr)
{
uint8_t data;
cc1100Select();
spiWrite(regAddr|(BURST<<6)|(READ<<7));
spiWrite(0);
data = spiRead();
cc1100DeSelect();
return data;
}
/*=================================================================================================
END
=================================================================================================*/