TX side code
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "inc/lm4f120h5qr.h"
#include "inc/hw_adc.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_sysexc.h"
#include "utils/uartstdio.h"
#include "inc/hw_ints.h"
#include "driverlib/debug.h"
#include "driverlib/pin_map.h"
#include "driverlib/adc.h"
#include "inc/hw_uart.h"
#include "driverlib/uart.h"
#include "driverlib/timer.h"
#include "driverlib/fpu.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ssi.h"
#include "inc/hw_types.h"
#include "driverlib/ssi.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "CC2520.h"
#include "definevariables.h"
#include "function_prototypes.h"
#ifdef DEBUG //Error Checking on APIs
void__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
int main(void)
{
//FPULazyStackingEnable();
//FPUEnable();
ADC_INIT(); //ADC initialization
InitConsole(); //UART initialization
MCU_SPI_INIT(); //SPI initialization
CC2520_ZIGBEE_INIT(); //CC2520 initialization
Before_Transmitt();
tx_buffer[0] = 's';
tx_buffer[1] = 's';
APP_PAYLOAD_LENGTH = 2;
while(1)
{
ADCIntClear(ADC0_BASE, 0); // Clear ADC Interrupt
ADCProcessorTrigger(ADC0_BASE, 0); // Trigger ADC Interrupt
while(!ADCIntStatus(ADC0_BASE, 0, false)) //Wait for interrupt Status flag to go off
{
}
ADCSequenceDataGet(ADC0_BASE, 0, ulADC0Value); //Grab the Entire FIFO
humidity_sensor_value = ulADC0Value[0]; //Sequencer Step 0: Samples Channel PE3-AIN0
humidity_sensor_value = calibrate_humidity(humidity_sensor_value);
LM_35_temp_value = ulADC0Value[1]; ////Sequencer Step 1: Samples Channel PE2-AIN1
LM_35_temp_value = calibrate_LM35_temp_value1(LM_35_temp_value);
LM35_temp_value2 = ulADC0Value[2];//Sequencer Step 2: Samples Channel PE1-AIN2
LM35_temp_value2 = calibrate_LM35_temp_value2(LM35_temp_value2);
sensor_value = ulADC0Value[3]; //Sequencer Step 3: Samples Channel PE0-AIN3
//UARTprintf("ID=LM_35_temp_value=%d.00humidity=%d.00light=00.00var4=00.00var5=00.00\n", LM_35_temp_value,humidity_sensor_value );
//UARTprintf("humidity_sensor_value = %d\n",humidity_sensor_value);
Delay_In_Usec(1000);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
issuestrobe(CC2520_INS_STXON);
Delay_In_Usec(100);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
Delay_In_Usec(100);
transmitter();
}
}
void ADC_INIT(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
//Configure System Clock to Run with 16Mhz crystal in Main Oscillator. Use PLL (400MHz). Divide by 5 (There is also, a default Divide by 2)
//Hence, Divide by 10. So, Clock Frequency = 400/10 = 40MHz.
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); //Enable ADC0 Peripheral
SysCtlADCSpeedSet(SYSCTL_ADCSPEED_250KSPS); //ADC Sample Rate set to 250 Kilo Samples Per Second
ADCHardwareOversampleConfigure(ADC0_BASE, 64); // Hardware averaging. ( 2, 4, 8 , 16, 32, 64 )
//64 Samples are averaged here i.e, each sample will be a result of 64 averaged samples. Therefore, every result is a result of 64 x 4 = 256 samples.
ADCSequenceDisable(ADC0_BASE, 1); //Before Configuring ADC Sequencer 1, it should be OFF
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
//ADC Configured so that Processor Triggers the sequence and we want to use highest priority. ADC Sequencer 0 is Used.
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0); //Sequencer Step 0: Samples Channel PE3 Humidity sensor
ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1); //Sequencer Step 1: Samples Channel PE2 LM-35 temperature sensor
ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH2); //Sequencer Step 2: Samples Channel PE1 BPW-34 sensor light intensity
ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END);
//Final Sequencer Step also Samples and enables Interrupt and we are telling the sequencer that this is the last step
//Configuring all eight steps in the ADC Sequence
ADCSequenceEnable(ADC0_BASE, 0); //Enable ADC Sequence
}
void MCU_SPI_INIT(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
//
// For this example SSI0 is used with PortA[5:2]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port A needs to be enabled so these pins can
// be used.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure the pin muxing for SSI0 functions on port A2, A3, A4, and A5.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);
//
// Configure the GPIO settings for the SSI pins. This function also gives
// control of these pins to the SSI hardware. Consult the data sheet to
// see which functions are allocated per pin.
// The pins are assigned as follows:
// PA5 - SSI0Tx
// PA4 - SSI0Rx
// PA3 - SSI0Fss
// PA2 - SSI0CLK
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
GPIO_PIN_2);
//
// Configure and enable the SSI port for TI master mode. Use SSI0, system
// clock supply, master mode, 1MHz SSI frequency, and 8-bit data.
//
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_TI,
SSI_MODE_MASTER, 3000000, 8);
//
// Enable the SSI0 module.
//
SSIEnable(SSI0_BASE);
}
//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
void InitConsole(void)
{
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Initialize the UART for console I/O.
UARTStdioInit(0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIODirModeSet(GPIO_PORTD_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,GPIO_DIR_MODE_HW);
}
void CC2520_ZIGBEE_INIT(void)
{
ResetCC2520(); //call reset function to reset the Rf module
ConfigureCC2520();
}
void ResetCC2520(void) //reset the CC2520 RF module
{
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,0); // VREG=0
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,0); // RESET=0
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,1); // VREG=1
Delay_In_Milisec(3); //delay 3ms
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,1); //RESET=1
Delay_In_Milisec(3); //delay 3ms
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_5,GPIO_RISING_EDGE);
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))); // Monitoring SO line
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
}
void ConfigureCC2520(void)
{
SELF_ADDR = 0;
DEST_ADDR = 0;
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,1); // VREG=1
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
issuestrobe(CC2520_INS_SXOSCON);
Delay_In_Usec(100);
Delay_In_Milisec(100); //delay 3ms
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
issuestrobe(CC2520_INS_SNOP);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
Delay_In_Milisec(1000); //delay 1000ms
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_TXPOWER, 0xF7); // Freq synthesizer control.
issuestrobe(CC2520_TXPOWER_0_DBM);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_FREQCTRL, 0x0b);
issuestrobe(frequency_2440);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_MDMCTRL0, 0x85); // Freq synthesizer control.
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_MDMCTRL1, 0x14); // Freq control word, high byte 0x5A 0x58
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x36);
SPIWrite(0xF8);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x4A);
SPIWrite(0x3F);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x4C);
SPIWrite(0x5A);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x4F);
SPIWrite(0x2B);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x53);
SPIWrite(0x11);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x56);
SPIWrite(0x10);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x57);
SPIWrite(0x0E);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x58);
SPIWrite(0x03);
Delay_In_Usec(100);
Write2CC2520(CC2520_CCACTRL0, 0xF8);
Write2CC2520(CC2520_FRMFILT0, 0x0C);
Write2CC2520(CC2520_RXCTRL, 0x3F); // Freq control word, mid byte. 0x1C 0xE3
Write2CC2520(CC2520_FSCTRL, 0x5A); // Freq control word, low byte. 0x71 0x8E
Write2CC2520(CC2520_TXCTRL, 0x0A);
Write2CC2520(CC2520_FSCAL1, 0x2B); // Modem configuration.0x0e//0x08
Write2CC2520(CC2520_AGCCTRL1, 0x11); // Modem configuration.0x2F
Write2CC2520(CC2520_ADCTEST0, 0x10); // Modem configuration.0x7b
Write2CC2520(CC2520_ADCTEST1, 0x0E); // Modem configuration.0x42
Write2CC2520(CC2520_ADCTEST2, 0x03); // Modem configuration.
Write2CC2520(CC2520_EXTCLOCK, 0x00);
Write2CC2520(CC2520_FRMCTRL0, 0x40);
Write2CC2520(CC2520_FRMCTRL1, 0x00);
Write2CC2520(CC2520_RAM_PANID, PAN_ID);
Write2CC2520(CC2520_RAM_SHORTADDR, SELF_ADDR);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
Delay_In_Usec(100);
//Write2CC2520(CC2520_GPIOCTRL0, 0x09); //GPIO FOR RX
//Write2CC2520(CC2520_GPIOCTRL0, 0x02); //GPIO FOR TX
}
void Write2CC2520(unsigned int address,unsigned int value)
{
address = CC2520_INS_REGWR | address;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))); // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
SPIWrite(value);
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void SPIWrite(unsigned char data)
{
unsigned char i;
for(i = 0 ; i<8 ; i++)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
if(data & 0x80 )
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,1); // SI=1
else
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0); // SI=0
while(!(HWREG(SSI0_BASE + SSI_O_SR) & SSI_SR_TNF))
{
}
// Write the data to the SSI.
HWREG(SSI0_BASE + SSI_O_DR) = data << 1;
//SSI_O_DR = data << 1;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,1); // SCLK=1
Delay_In_Usec(10);
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0); // SI=0
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
}
unsigned char SPIRead(void)
{
unsigned char i,byte=0;
for(i = 0;i<8 ;i++)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
if((GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
{
while(!(HWREG(SSI0_BASE + SSI_O_SR) & SSI_SR_RNE))
{
}
// Read data from SSI.
byte = (HWREG(SSI0_BASE + SSI_O_DR) << 1) | 0x01;
}
else
{
byte = HWREG(SSI0_BASE + SSI_O_DR) << 1;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,1); // SCLK=1
}
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
return(byte);
}
void Before_Transmitt(void)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
Write2CC2520(CC2520_GPIOCTRL0, 0x02); //GPIO FOR TX
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void Before_Receive(void)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
Write2CC2520(CC2520_GPIOCTRL0, 0x09); //GPIO FOR RX
issuestrobe(CC2520_INS_SRXON);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void issuestrobe(unsigned char address)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
}
void transmitter(void)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
send_packet(DEST_ADDR,&tx_buffer[0],APP_PAYLOAD_LENGTH);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(100);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(100);
issuestrobe(CC2520_INS_SRFOFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(100);
//while(!(GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_2))) ; // Monitoring GPIO0 line
Write2CC2520(CC2520_EXCFLAG0, 0x00);
Delay_In_Usec(10);
issuestrobe(CC2520_INS_SFLUSHTX);
}
void send_packet(unsigned int destAddr, unsigned char* pPayload, unsigned char length)
{
unsigned char mpduLength;
mpduLength = BuildMpdu(destAddr, pPayload, length);
halRfWriteTxBuf((unsigned char*)txMpdu, mpduLength);
}
void halRfWriteTxBuf(unsigned char* data, unsigned char length)
{
BurstWrite2CC2520(CC2520_INS_TXBUF,data,length);
}
unsigned char BuildMpdu(unsigned int destAddr, unsigned char* pPayload, unsigned char length)
{
unsigned char hdrLength = 0;
int n=0;
hdrLength = basicRfBuildHeader(txMpdu, destAddr, length);
for(n=0;n<length;n++)
{
txMpdu[hdrLength+n] = *(pPayload+n);
}
return hdrLength+n; // total mpdu length
}
unsigned char basicRfBuildHeader(unsigned char* buffer, unsigned int destAddr, unsigned char payloadLength)
{
basicRfPktHdr_t *pHdr;
pHdr= (basicRfPktHdr_t*)buffer;
// Populate packet header
pHdr->packetLength = payloadLength + BASIC_RF_PACKET_OVERHEAD_SIZE;
pHdr->frameControlField = BASIC_RF_FCF_NOACK;
pHdr->seqNumber= txState.txSeqNumber;
pHdr->panId= PAN_ID;
pHdr->destAddr= DEST_ADDR;
pHdr->srcAddr= SELF_ADDR;
return sizeof(basicRfPktHdr_t);
}
void BurstWrite2CC2520 (unsigned char address, unsigned char *value, unsigned char length)
{
unsigned char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
for(i=0;i<length;i++)
{
SPIWrite(*value);
value++;
}
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
}
void BurstreadCC2520 (unsigned char address,unsigned char length)
{
unsigned char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
for(i=0;i<length;i++)
{
recv_val[i] = SPIRead();
}
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
}
void receive(void)
{
Write2CC2520(CC2520_EXCFLAG1, 0x00);
rec_data1();
}
void halRfRecvFrame(uint8 length)
{
BurstreadCC2520(CC2520_INS_RXBUF,length);//(length, data);
}
void rec_data1(void) //this is for the TX wireless senssor node
{
BurstreadCC2520(CC2520_INS_RXBUF,1);
le = recv_val[0];
BurstreadCC2520(CC2520_INS_RXBUF,le);
Delay_In_Usec(10);
issuestrobe(CC2520_INS_SFLUSHRX);
}
void readCC2520(unsigned int address)
{
unsigned char add1, add2;
unsigned int address1;
add1 = address & 0x00FF;
address1 = address & 0xFF00;
address1 = address1 >> 8;
add2 = address1 & 0x00FF;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(CC2520_INS_MEMRD |add2);
SPIWrite(add1);
Delay_In_Usec(10);
recv_dat = SPIRead();
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(100);
}
void Delay_In_Usec(unsigned int del)
{
unsigned int i;
for(i = 0 ; i < del ; i++)
{
SysCtlDelay(SysCtlClockGet() / 3000);
}
}
void Delay_In_Milisec(unsigned int del)
{
unsigned int i,j;
for(i = 0 ; i < del ; i++)
{
for(j = 40000 ; j > 0 ; j--);
}
}
///////////////////////////////////////////////////////// The RX side code///////////////////////////////////////////////////////////////////////
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "inc/lm4f120h5qr.h"
#include "inc/hw_adc.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_sysexc.h"
#include "utils/uartstdio.h"
#include "inc/hw_ints.h"
#include "driverlib/debug.h"
#include "driverlib/pin_map.h"
#include "driverlib/adc.h"
#include "inc/hw_uart.h"
#include "driverlib/uart.h"
#include "driverlib/timer.h"
#include "driverlib/fpu.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ssi.h"
#include "inc/hw_types.h"
#include "driverlib/ssi.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "CC2520.h"
#include "definevariables.h"
#include "functions_prototypes.h"
#ifdef DEBUG //Error Checking on APIs
void__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
int main(void)
{
InitConsole(); //UART initialization
MCU_SPI_INIT(); //SPI initialization
CC2520_ZIGBEE_INIT(); //CC2520 initialization
Before_Receive(); //call in RX wireless sensor node
tx_buffer[0] = 's';
tx_buffer[1] = 'a';
APP_PAYLOAD_LENGTH = 2;
while(1)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
Delay_In_Usec(1000);
receive();
}
}
void MCU_SPI_INIT(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
//
// For this example SSI0 is used with PortA[5:2]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port A needs to be enabled so these pins can
// be used.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure the pin muxing for SSI0 functions on port A2, A3, A4, and A5.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);
//
// Configure the GPIO settings for the SSI pins. This function also gives
// control of these pins to the SSI hardware. Consult the data sheet to
// see which functions are allocated per pin.
// The pins are assigned as follows:
// PA5 - SSI0Tx
// PA4 - SSI0Rx
// PA3 - SSI0Fss
// PA2 - SSI0CLK
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
GPIO_PIN_2);
//
// Configure and enable the SSI port for TI master mode. Use SSI0, system
// clock supply, master mode, 1MHz SSI frequency, and 8-bit data.
//
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_TI,
SSI_MODE_MASTER, 1000000, 8);
//
// Enable the SSI0 module.
//
SSIEnable(SSI0_BASE);
}
//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
void InitConsole(void)
{
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Initialize the UART for console I/O.
UARTStdioInit(0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIODirModeSet(GPIO_PORTD_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,GPIO_DIR_MODE_HW);
}
void CC2520_ZIGBEE_INIT(void)
{
ResetCC2520(); //call reset function to reset the Rf module
ConfigureCC2520();
}
void ResetCC2520(void) //reset the CC2520 RF module
{
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,0); // VREG=0
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,0); // RESET=0
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,1); // VREG=1
Delay_In_Milisec(5); //delay 5ms
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0,1); //RESET=1
Delay_In_Milisec(5); //delay 5ms
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_4,GPIO_RISING_EDGE);
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))); // Monitoring SO line
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
}
void ConfigureCC2520(void)
{
SELF_ADDR = 0;
DEST_ADDR = 0;
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1,1); // VREG=1
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
issuestrobe(CC2520_INS_SXOSCON);
Delay_In_Usec(100);
Delay_In_Milisec(100); //delay 3ms
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
issuestrobe(CC2520_INS_SNOP);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
Delay_In_Milisec(1000); //delay 1000ms
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_TXPOWER, 0xF7); // Freq synthesizer control.
issuestrobe(CC2520_TXPOWER_0_DBM);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_FREQCTRL, 0x0b);
issuestrobe(frequency_2440);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_MDMCTRL0, 0x85); // Freq synthesizer control.
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
Write2CC2520(CC2520_MDMCTRL1, 0x14); // Freq control word, high byte 0x5A 0x58
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x36);
SPIWrite(0xF8);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x4A);
SPIWrite(0x3F);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x4C);
SPIWrite(0x5A);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x4F);
SPIWrite(0x2B);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x53);
SPIWrite(0x11);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x56);
SPIWrite(0x10);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x57);
SPIWrite(0x0E);
Delay_In_Usec(100);
issuestrobe(CC2520_INS_MEMWR);
SPIWrite(0x58);
SPIWrite(0x03);
Delay_In_Usec(100);
Write2CC2520(CC2520_CCACTRL0, 0xF8);
Write2CC2520(CC2520_FRMFILT0, 0x0C);
Write2CC2520(CC2520_RXCTRL, 0x3F); // Freq control word, mid byte. 0x1C 0xE3
Write2CC2520(CC2520_FSCTRL, 0x5A); // Freq control word, low byte. 0x71 0x8E
Write2CC2520(CC2520_TXCTRL, 0x0A);
Write2CC2520(CC2520_FSCAL1, 0x2B); // Modem configuration.0x0e//0x08
Write2CC2520(CC2520_AGCCTRL1, 0x11); // Modem configuration.0x2F
Write2CC2520(CC2520_ADCTEST0, 0x10); // Modem configuration.0x7b
Write2CC2520(CC2520_ADCTEST1, 0x0E); // Modem configuration.0x42
Write2CC2520(CC2520_ADCTEST2, 0x03); // Modem configuration.
Write2CC2520(CC2520_EXTCLOCK, 0x00);
Write2CC2520(CC2520_FRMCTRL0, 0x40);
Write2CC2520(CC2520_FRMCTRL1, 0x00);
Write2CC2520(CC2520_RAM_PANID, PAN_ID);
Write2CC2520(CC2520_RAM_SHORTADDR, SELF_ADDR);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); //CSn=1
Delay_In_Usec(100);
//Write2CC2520(CC2520_GPIOCTRL0, 0x09); //GPIO FOR RX
//Write2CC2520(CC2520_GPIOCTRL0, 0x02); //GPIO FOR TX
}
void Write2CC2520(unsigned int address,unsigned int value)
{
address = CC2520_INS_REGWR | address;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))); // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
SPIWrite(value);
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void SPIWrite(unsigned char data)
{
unsigned char i;
for(i = 0 ; i<8 ; i++)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
if(data & 0x80 )
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,1); // SI=1
else
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0); // SI=0
while(!(HWREG(SSI0_BASE + SSI_O_SR) & SSI_SR_TNF))
{
}
// Write the data to the SSI.
HWREG(SSI0_BASE + SSI_O_DR) = data << 1;
//SSI_O_DR = data << 1;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,1); // SCLK=1
Delay_In_Usec(10);
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4,0); // SI=0
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
}
unsigned char SPIRead(void)
{
unsigned char i,byte=0;
for(i = 0;i<8 ;i++)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
if((GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
{
while(!(HWREG(SSI0_BASE + SSI_O_SR) & SSI_SR_RNE))
{
}
// Read data from SSI.
byte = (HWREG(SSI0_BASE + SSI_O_DR) << 1) | 0x01;
}
else
{
byte = HWREG(SSI0_BASE + SSI_O_DR) << 1;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,1); // SCLK=1
}
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2,0); // SCLK=0
return(byte);
}
void Before_Transmitt(void)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
Write2CC2520(CC2520_GPIOCTRL0, 0x02); //GPIO FOR TX
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void Before_Receive(void)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
Write2CC2520(CC2520_GPIOCTRL0, 0x09); //GPIO FOR RX
issuestrobe(CC2520_INS_SRXON);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void issuestrobe(unsigned char address)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
}
void transmitter(void)
{
send_packet(DEST_ADDR,&tx_buffer[0],APP_PAYLOAD_LENGTH);
issuestrobe(CC2520_INS_STXON);
while(!(GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_5))) ; // Monitoring GPIO0 line
Write2CC2520(CC2520_EXCFLAG0, 0x00);
Delay_In_Usec(10);
issuestrobe(CC2520_INS_SRFOFF);
Delay_In_Usec(10);
issuestrobe(CC2520_INS_SFLUSHTX);
}
void send_packet(unsigned int destAddr, unsigned char* pPayload, unsigned char length)
{
unsigned char mpduLength;
mpduLength = BuildMpdu(destAddr, pPayload, length);
halRfWriteTxBuf((unsigned char*)txMpdu, mpduLength);
}
void halRfWriteTxBuf(unsigned char* data, unsigned char length)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
BurstWrite2CC2520(CC2520_INS_TXBUF,data,length);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
unsigned char BuildMpdu(unsigned int destAddr, unsigned char* pPayload, unsigned char length)
{
unsigned char hdrLength = 0;
int n=0;
hdrLength = basicRfBuildHeader(txMpdu, destAddr, length);
for(n=0;n<length;n++)
{
txMpdu[hdrLength+n] = *(pPayload+n);
}
return hdrLength+n; // total mpdu length
}
unsigned char basicRfBuildHeader(unsigned char* buffer, unsigned int destAddr, unsigned char payloadLength)
{
basicRfPktHdr_t *pHdr;
pHdr= (basicRfPktHdr_t*)buffer;
// Populate packet header
pHdr->packetLength = payloadLength + BASIC_RF_PACKET_OVERHEAD_SIZE;
pHdr->frameControlField = BASIC_RF_FCF_NOACK;
pHdr->seqNumber= txState.txSeqNumber;
pHdr->panId= PAN_ID;
pHdr->destAddr= DEST_ADDR;
pHdr->srcAddr= SELF_ADDR;
return sizeof(basicRfPktHdr_t);
}
void BurstWrite2CC2520 (unsigned char address, unsigned char *value, unsigned char length)
{
unsigned char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
for(i=0;i<length;i++)
{
SPIWrite(*value);
value++;
}
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
}
void BurstreadCC2520 (unsigned char address,unsigned char length)
{
unsigned char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(address);
Delay_In_Usec(10);
for(i=0;i<length;i++)
{
recv_val[i] = SPIRead();
}
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
}
void receive(void)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(100);
Write2CC2520(CC2520_EXCFLAG1, 0x00);
rec_data1();
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(100);
}
void halRfRecvFrame(uint8 length)
{
char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(10);
BurstreadCC2520(CC2520_INS_RXBUF,length);//(length, data);
for(i = 0 ;i < length ; i++)
{
UARTprintf("sachin= %s\n",recv_val[i]);
}
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(10);
}
void readCC2520(unsigned int address)
{
unsigned char add1, add2;
unsigned int address1;
add1 = address & 0x00FF;
address1 = address & 0xFF00;
address1 = address1 >> 8;
add2 = address1 & 0x00FF;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
while(!(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5))) // Monitoring SO line
SPIWrite(CC2520_INS_MEMRD |add2);
SPIWrite(add1);
Delay_In_Usec(10);
recv_dat = SPIRead();
UARTprintf("sachin= %s\n",recv_dat);
Delay_In_Usec(10);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(100);
}
void rec_data1(void) //this function call in RX wireless sensor node
{
char i;
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,0); // CSn=0
Delay_In_Usec(100);
BurstreadCC2520(CC2520_INS_RXBUF,1);
le = recv_val[0];
BurstreadCC2520(CC2520_INS_RXBUF,le);
for(i = 0 ;i < le ; i++)
{
UARTprintf("sachin= %s\n",recv_val[i]);
}
readCC2520(DEST_ADDR);
halRfRecvFrame(10);
issuestrobe(CC2520_INS_SFLUSHRX);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3,1); // CSn=1
Delay_In_Usec(100);
}
void Delay_In_Usec(unsigned int del)
{
unsigned int i;
for(i = 0 ; i < del ; i++)
{
SysCtlDelay(SysCtlClockGet() / 3000);
}
}
void Delay_In_Milisec(unsigned int del)
{
unsigned int i,j;
for(i = 0 ; i < del ; i++)
{
for(j = 40000 ; j > 0 ; j--);
}
}