📄 echomaster.c
字号:
/* Interface an Atmel AVR MCU with Freescales MC1319x ZigBee chip */
/*
* Author: johan@anteeo.se
*
* Copyright (C) 2005 by Anteeo Systems HB, Sweden
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* Created: Fri May 13 22:46:58 2005, johan@anteeo.se
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <usart.h>
#include <mc1319x.h>
#include <ledfeedback.h>
// This assumes the default prescaler (250kHz) in the MC13192 is used
// Timeout in 0.5s
#define RCV_DEFAULT_TIMEOUT (0xFFFFFF/67)*0.5
unsigned long g_sendtime=0;
unsigned char g_packetSeqNo=0;
unsigned char g_iChannel=0;
// Clear Channel Assessment callback
void CCAdoneCallback(char bBusy, char iPower)
{
char szBuf[64];
sprintf(szBuf, "CCA finnished state: %d, pwr: %d\r\n", bBusy, iPower);
USART_TransmitStr(szBuf);
if(g_iChannel<16)
{
g_iChannel++;
MC13192_setChannel(g_iChannel);
MC13192_doCCA();
}
else
{
g_iChannel=1;
MC13192_setChannel(g_iChannel);
}
}
void packetSentCallback(void)
{
// Do not log to USART here because we need to be quick about starting to
// wait for our answer, otherwise we might miss it!
// Wait for reply tgm
MC13192_receivePacket(RCV_DEFAULT_TIMEOUT);
//USART_Transmit( "s",1);
}
void packetReceivedCallback(void)
{
// If we are not the master, bounce back packet!
if( packetSize==0)
{
// Received corrupt tgm!
USART_TransmitStr( "Got corrupt tgm\r\n");
return;
}
else if(packetSize==0xff )
{
// Receive Timeout!
USART_TransmitStr( "Timeout\r\n");
return;
}
else
{
unsigned long currtime=MC13192_getCurrentTime();
char szBuf[32];
sprintf(szBuf, "Got answer %u (%u ms)!\r\n", packetBuf[0], MC13192_timeToMs(currtime-g_sendtime) );
USART_TransmitStr(szBuf);
g_packetSeqNo++;
}
}
void rcvAbortedCallback(void)
{
USART_TransmitStr("RCV aborted, sending\r\n");
MC13192_sendPacket(10);
}
void USART_ReceivedByte(unsigned char data)
{
char szBuf[64];
switch(data)
{
// Do CCA
case 'c':
if(DSRC_Status==DSRC_INIT_OK)
{
USART_TransmitStr("Doing CCA\r\n");
MC13192_doCCA();
}
break;
// Log timer
case 'a':
sprintf(szBuf, "MC Time: %lu - %lX\r\n",MC13192_getCurrentTime(),MC13192_getCurrentTime());
USART_TransmitStr( szBuf);
break;
// Send packed
case 's':
if(DSRC_Status==DSRC_INIT_OK)
USART_TransmitStr( "Sending packet\r\n");
packetBuf[0]=g_packetSeqNo;
g_sendtime=MC13192_getCurrentTime();
MC13192_sendPacket(10);
break;
default:
USART_TransmitStr( "Unknown CMD\r\n");
break;
}
}
int main (void)
{
// Setup registers to control the LED
LED_Init();
// Setup registers to interface the AVR with MC1319x
MC1319x_MCU_Init();
// Init MC13192
MC13192Init();
// Flash led to show we are alive
blinkLED(LED_GREEN);
// Init USART stuff
USART_Init(MYUBRR);
USART_EnableIRQ();
USART_TransmitStr( "MC13192 DSRC Module\r\n");
if(DSRC_Status!=DSRC_INIT_OK)
{
USART_TransmitStr( "MC13192 Init failed!!!\r\n");
}
else
{
USART_TransmitStr( "MC13192 Init OK!\r\n");
}
MC1319x_MCUenableInterrupts();
USART_TransmitStr( "Starting as echo master\r\n");
while (1)
{
// Drive the LED loop
tickLEDs();
}
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -