📄 zigbee demo.c
字号:
/********************************************************************************************
File: main.c
Author: Freescale
Application Note: This simple application forms a star wireless network with ACKs.
MCF5213 takes as Gateway, 13192SARD or GB60 EVB takes as devices.
Power MCF5213EVB, connect the UART0 on the EVB with PC.Download the code to boards.
Set the Hyperterms for 38400 No flow control.
Power on board, hyperTerminal will display welcome information
Switch functions:
SW1:Change channels. Default channel=1
SW2:Change Power level
****************************************************************************/
#include "zigbee demo.h"
#include "drivers.h"
//for g726
#include <stdio.h>
#include <math.h>
//for g721
#include "mcf5xxx.h"
#include "common.h"
#include "mcf5213_gpio.h"
#include "g72x.h"
//for new cpt
#include "pub_def.h"
#include "drivers.h"
#include "MC13192_regs.h"
#include "simple_mac.h"
#include "simple_phy.h"
#include "common.h"
//max number of bytes to xmit
//#define UIF_MAX_LINE 10
//#define UIF_MAX_LINE 60
#define UIF_MAX_OUT 60
#define UIF_MAX_IN 60
#define MAXCHANNEL 0x03
//application status defines
#define INITIAL_STATE 0
#define IDLE_STATE 1
#define WAITING_FOR_ACK 2
#define RECEIVER_ALWAYS_ON 3
#define TRANSMIT_ACK 4
#define TRANSMIT_DATA 5
#define TRANSMIT_CMD 6
//add by michael
#define SEND_CALL 7
#define SEND_ACCEPT 8
#define REC_CALL 9
#define REC_ACCEPT 10
#define AUDIO_DATA 11
#define WAIT_FOR_CALL_ACK 12
#define WAIT_FOR_ACCEPT_ACK 13
#define WAIT_ACCEPT 14
#define SEND_CALL_ACK 15
#define SEND_ACCEPT_ACK 16
#define CANCEL 17
#define REC_ACCEPT_ACK 18
#define TIMEOUT_PERIOD 0x4000
#define NetID 0x78
#define GatewayID 0x00
//Define command
#define CMD_JOIN 1
#define CMD_LEAVE 2
#define CMD_STATUS 3
#define CMD_FDATA 4
#define CMD_DATA 5
#define CMD_BST 6
//add by michael chen
#define CMD_CALL 7
#define CMD_CALL_ACK 8
#define CMD_ACCEPT 9
#define CMD_ACCEPT_ACK 10
#define CMD_AUDIO_DATA 11
// Local variables
uint8 gu8RTxMode;
uint8 channel = 1;
uint8 power_level = MAX_POWER;
uint8 rx_data_buffer[UIF_MAX_IN+5];
uint8 tx_data_buffer[UIF_MAX_OUT+5];
vuint8 app_status;
//add by michael chen
static vuint8 cmd_para_buf[5];//
uint8 NodeID=1;
//chang by michael chen
//uint8 NodeID=2;
tTxPacket tx_pkt;
tRxPacket rx_pkt;
//add by michael chen
static vuint8 zigbee_flag = 0;//a flag for transmit channel have been set up
extern vint32 button_value;//the value of button by pressed
extern vint32 button_flag;//the times of button by pressed
extern vuint8 button_value_current;//the value of button by pressed currently
//define by michael chen
//src_nodeid is different from NodeID
static vuint8 src_nodeid = 1;
static vuint8 des_nodeid = 2;
/*********************************************************************************/
//一个生产者和消费者问题,形成一个循环缓冲区,生产者是AD in,消费者是zigbee output
static vuint8 out_data_buf1[UIF_MAX_OUT]=
{
0
};//buf1 for zigbee transmit audio out
static vuint8 out_data_buf2[UIF_MAX_OUT]=
{
0
};//buf2 for zigbee transmit audio out
static vuint8 out_buf_flag = 1; //using which out data buf [1] or [2] ;changed in zigbee_main()
static vuint8 out_buf_count = 0; //the position of out buf field ;changed in zigbee_main() and ISR
static vuint8 out_buf_full_flag = 0;//0-没有缓冲区满,1:缓冲区1满,2:缓冲区2满
/***********************************************************************************/
//另一个生产者和消费者问题,形成一个循环缓冲区,生产者是zigbee input,消费者是PWM output
static vuint8 in_data_buf1[UIF_MAX_IN]=
{
0
};//pingpang buf1 usage for zigbee receive audio in
static vuint8 in_data_buf2[UIF_MAX_IN]=
{
0
};//pingpang buf2 usage for zigbee receive audio in
static vuint8 in_buf_flag = 1; //using which out data buf [1] or [2] ;changed in interrupt ISR
static vuint8 in_buf_count = 0; //the position of out buf field ;changed in interrupt ISR
static vuint8 in_buf_full_flag = 0;//0-没有缓冲区满,1:缓冲区1满,2:缓冲区2满
/**********************************************************************************/
//16bit-》encode-》2bit-》put 4 encode‘data into one byte,first fill the lower bites of one byte
static vuint32 tt;
static vuint8 encode_bite_n = 0;//16bit-》encode-》2bit-》put 4 encode‘data into one byte,first fill the lower bites of one byte
//static vuint8 decode_bite_n = 3;//for g726
static vuint8 decode_bite_n = 1;//for g721
static vuint8 bite_temp = 0;
static vuint32 decode_temp = 0;
static vuint8 trans_flag = 0;//a flag for zigbee can transmit data without conflict 1--can tran ..0 --can not trans
static vuint8 host_client = 0;//0:client ,receive call demo 1:host,send call demo
/*********************************************************************/
//for G726
/*
static struct g726_state_s zigbee_st;
static struct g726_state_s *zigbee_p = NULL;
static struct g726_state_s zigbee_st2;
static struct g726_state_s *zigbee_p2 = NULL;
*/
//for new 721
//static vuint32 encoder_data = 0;
//static vuint32 decoder_data = 0;
static vint32 encoder_data = 0;
static vint32 decoder_data = 0;
static vuint8 zigbee_enter_726 = 0;
//for g721
struct g72x_state zigbee_st32;
struct g72x_state *zigbee_p32 = NULL;
struct g72x_state zigbee_st322;
struct g72x_state *zigbee_p322 = NULL;
//#define ENCODE_BIT_MASK 0x03//for G726 2 bit mask
#define BIT_MASK 0x0F//for G721 4 bit mask
//#define ENCODE_N_NUM 4//for G726 encode_n_bit or decond_n-bit max num =4
#define BIT_N_NUM 2//for G721 encode_n_bit or decond_n-bit max num =2
//#define ENCODE_BIT 1//for 16bit encode into 2 bit
#define ENCODE_BIT 2 //for 16bit encode into 4 bit
/**********************************************************************/
//for new cpt
static volatile int zigbee_cptout;
volatile int index_cpt = 0;
static char start_decode = 0;
#define zigbee_debug 1
//#undef zigbee_debug
/******************************************************************
*pakeage format
*buf[0] --- NetID
*buf[1] --- GatewayID
*buf[2] --- source nodeid
*buf[3] --- destination nodeid
*buf[4] --- command id
*buf[5]....buf[...]---- data
*
*tx_pkt.u8DataLength the length of transmit package
*rx_pkt->u8DataLength the length of receive package
********************************************************************/
void make_trans_package(unsigned char src_id,unsigned char des_id,unsigned char cmd,volatile unsigned char *data,unsigned char data_buf_length)
{
uint8 i = 0;
tx_data_buffer[0]=NetID; // device NetID
tx_data_buffer[1]=GatewayID; // src ID
tx_data_buffer[2]=src_id; // current Device ID
tx_data_buffer[3]=des_id; // destination ID
tx_data_buffer[4]=cmd; // command
for(i=0; i<data_buf_length; i++)//fill the data into the package
{
tx_data_buffer[5+i] = data[i];
}
tx_pkt.pu8Data = (uint8*)tx_data_buffer;
tx_pkt.u8DataLength = 5+data_buf_length;
return ;
}
/**********************************************************************************************
*90% copy from Adc_Pwm_Module.c
**********************************************************************************************/
/****************************************************
Fuction: initialize the Pwm1 Control Register
NOTE: There are at least two Errors in the pwm.h
One is "#define MCF_PWM_PWMDTY1 (*(vuint8 *)(&__IPSBAR[0x1B0019]))"
the right Address is (*(vuint8 *)(&__IPSBAR[0x1B0015]))
Two is "#define MCF_PWM_PWMCNT1 (*(vuint8 *)(&__IPSBAR[0x1B000D]))"
the right Address is (*(vuint8 *)(&__IPSBAR[0x1B001D]))
*****************************************************/
///*
void Zigbee_ADC_InitPwmRegister()
{
//deal with PWM1 and PWM0
MCF_GPIO_DDRTD = MCF_GPIO_DDRTD_DDRTD0; // Set the direction of PTD0 output
MCF_GPIO_PTDPAR |= MCF_GPIO_PTDPAR_PTDPAR0;// Select the IO function as pwm1
MCF_PWM_PWME &= ~MCF_PWM_PWME_PWME1; // Disable the Pwm Chanl 1
MCF_PWM_PWMPOL |= MCF_PWM_PWMPOL_PPOL1; // Begin low level
MCF_PWM_PWMPRCLK &= MCF_PWM_PWMPRCLK_PCKA(0x0); // Clock A prescale is 1
MCF_PWM_PWMCAE &= ~MCF_PWM_PWMCAE_CAE1; //left-Align
MCF_PWM_PWMCLK &= ~MCF_PWM_PWMCLK_PCLK1; // Select SA as Pwm Clock
MCF_PWM_PWMCTL = MCF_PWM_PWMCTL_CON01; // concentrate Pwm1 and Pwm2
MCF_PWM_PWMSCLA = MCF_PWM_PWMSCLA_SCALEA(0x01); // Now Clock SA Frequency is f_sys/2/10
(*(vuint8 *)(&__IPSBAR[0x1B0015])) = 0xff; // Make the Clock_SA frequency is f_SA/255
(*(vuint8 *)(&__IPSBAR[0x1B0014])) = 0x03; // Make the Clock_SA frequency is f_SA/255
(*(vuint8 *)(&__IPSBAR[0x1B001D])) = 0x0; // write Any Value
(*(vuint8 *)(&__IPSBAR[0x1B001C])) = 0x08; // write Any Value
MCF_PWM_PWMCNT1 = MCF_PWM_PWMCNT_COUNT(0xff); // write Any Value to clear the counter
MCF_PWM_PWMCNT0 = MCF_PWM_PWMCNT_COUNT(0x0f); // write Any Value to clear the counter
MCF_PWM_PWME |= MCF_PWM_PWME_PWME1; // Eable the Pwm Chanl 1
}
/*********************************************************
Fuction: Initialize PIT1 Interrupt Register
************************************************************/
void Zigbee_ADC_InitPIT1Register()
{
MCF_INTC_ICR56 = 0x09; // Int level 1, priority 0
MCF_PIT1_PCSR &= ~MCF_PIT_PCSR_PIE; //Disable PIT1 Int
MCF_INTC_IMRH &= 0xFEFFFFFF; // Enable PIT1 Int Mask
MCF_PIT1_PMR = 5000;//8KHZ
//MCF_PIT1_PMR = 7000;//5.7KHZ
//MCF_PIT1_PMR = 0x2710;//4KHZ
//MCF_PIT1_PMR = 20000;//2KHZ
//MCF_PIT1_PMR = 0x3E8/2;//80KHZ
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PRE(0x0); // Set the PIT1 Interrupt Period
//Writing PMRn immediately replaces value in PIT counter when counter reaches 0x00
MCF_PIT1_PCSR |= MCF_PIT_PCSR_OVW;
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIF; // Clear the Interrupt Flag
MCF_PIT1_PCSR |= MCF_PIT_PCSR_RLD; // Counter reloaded from PMRn on count of 0x0000
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIE; // Enable the PIT1 Interrupt
MCF_PIT1_PCSR |= MCF_PIT_PCSR_EN; // Enable the PIT1 Clock
}
/***************************************************
Funciton: The PIT1 Interrupt Function,
Everytime trap in the Interrupt, Read the VoiceData in the flash Rom,
and Write to PWM1 Duty_Register to change the Duty_to_Cycle Ratio of PWM1 Output
NOTE: KeyWord "__interrupt__" Make Compiler to know there is a Interrupt Funtion.
****************************************************/
__interrupt__ void Zigbee_ADC_PIT_1Interrupt(void)
{
/**************************************************************************
The following Assemble language is the translation of the C Language;
OSIntNesting ++;
if (OSIntNesting == 1)
{
OSTCBCur = sp;
}
*/
int AD_Data=0;
int temp726 = 0;
vuint32 decode_tmp = 0;
MCF_PIT1_PCSR &= ~MCF_PIT_PCSR_PIE; // Disable Interrupt
MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIF; // Clear the Interrupt Flag
asm(
MOVEQ.L #0,D0
MOVE.B (OSIntNesting),D0
ADDQ.L #1,D0
MOVE.B D0,(OSIntNesting)
CMPI.L #1, d0
BNE _SkipSave
MOVE.L (OSTCBCur), A1
MOVE.L A7,(A1)
);
/***********************************************************************/
_SkipSave:
switch(app_status)
{
case AUDIO_DATA:
/* Initialize the CRTL1 register to all 0's */
MCF_ADC_CTRL1 = 0;
/* Setting divisor in CTRL2 register */
MCF_ADC_CTRL2 = MCF_ADC_CTRL2_DIV(3);
/* Setting PortAN as ADC functionality */
MCF_GPIO_PANPAR |= MCF_GPIO_PANPAR_PANPAR0
| MCF_GPIO_PANPAR_PANPAR1;
// | MCF_GPIO_PANPAR_PANPAR2
// | MCF_GPIO_PANPAR_PANPAR3
// | MCF_GPIO_PANPAR_PANPAR4
// | MCF_GPIO_PANPAR_PANPAR5
// | MCF_GPIO_PANPAR_PANPAR6
// | MCF_GPIO_PANPAR_PANPAR7;
/* Setting Power Register appropriately */
MCF_ADC_POWER = MCF_ADC_POWER_PUDELAY(4);
/* Turn on ADC clock */
MCF_PMM_PPMRH = MCF_PMM_PPMRH &~ MCF_PMM_PPMRH_CDADC;
/* Stop conversions after doing first conversion */
MCF_ADC_ADSDIS = MCF_ADC_ADSDIS_DS1;
/* Clear stop bit */
MCF_ADC_CTRL1 &= ~MCF_ADC_CTRL1_STOP0;
/* Set start bit */
MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_START0;
/* Waiting for Ready bits to be set */
while (!(MCF_ADC_ADSTAT & MCF_ADC_ADSTAT_RDY0))
{};
/* Set stop bit */
MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_STOP0;
/* Shutting off ADC clock */
MCF_PMM_PPMRH != MCF_PMM_PPMRH_CDADC;
AD_Data = MCF_ADC_ADRSLT0;
AD_Data = AD_Data & 0x0000FFFF;
//for new 721
/*
asm
{
move.l AD_Data,d4
ext.l d4
asr.l #3,d4
move.l d4,AD_Data
}
temp726 = AD_Data - 1933;
*/
//for nemest 726_32
asm
{
move.l AD_Data,d4
ext.l d4
asr.l #3,d4
move.l d4,AD_Data
}
// temp726 = 2200;
temp726 = AD_Data - 1933;
//方案三
//encode
//for debug
if(zigbee_enter_726 == 0)
{
zigbee_enter_726 = 1;
zigbee_p32= &zigbee_st32;
zigbee_p322 = &zigbee_st322;
g72x_init_state(zigbee_p32);
g72x_init_state(zigbee_p322);
}
//for newest 726_32
/*
asm
{
move.l temp726,d4
ext.l d4
asl.l #4,d4
move.l d4,temp726
}
*/
encoder_data = g726_32encoder(temp726,zigbee_p32);
encoder_data = encoder_data & BIT_MASK;
bite_temp = bite_temp | (vuint8)(encoder_data << (encode_bite_n<<ENCODE_BIT) );
//for debug to test the
// bite_temp = 0xaa;
encode_bite_n++;
switch( out_buf_flag)
{
case 1:
if(encode_bite_n == BIT_N_NUM)//a byte complete,and put this byte into buf
{
out_data_buf1[out_buf_count] = bite_temp;
out_buf_count++;
encode_bite_n = 0;
bite_temp = 0;
}
if(out_buf_count == UIF_MAX_OUT)
{
out_buf_flag = 2;
out_buf_count = 0;
encode_bite_n = 0;
out_buf_full_flag = 1;//set full flag
}
break;
case 2 :
if(encode_bite_n == BIT_N_NUM)//a byte complete,and put this byte into buf
{
out_data_buf2[out_buf_count] = bite_temp;
out_buf_count++;
encode_bite_n = 0;
bite_temp = 0;
}
if(out_buf_count == UIF_MAX_OUT)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -