-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
304 lines (270 loc) · 11.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Project:capinfo-rf_route
// Filename: mcu_config.c
// Function: rf底层驱动
// Author: wzd wzd
// Date: 2013年8月15日10:21:25
#include "rf_route.h"
INT8U g_1s_counter=0,g_leng=0,g_count = 0,g_test_count=0;
// 有用的全局
Module_Sn g_module_id,g_gateway;
INT16U timer = 0;
INT8U g_wor_flag = 0x00,g_rx_flag = 0,g_rf_rx_flag = 0,g_rx_timeout = 0x00,g_enter_rx = 0x00;
INT8U g_search = 0x00,g_getroute=0x00; // 进行3次搜索路由
INT8U g_rid = 0x01,g_pre_rid = 0x00;
INT8U WorCarry[2] = {0xFF,0xFF};
INT8U TxBuf[64]; // 11字节, 如果需要更长的数据包,请正确设置
//INT8U RxBuf[64];
INT8U RfSentBuf[64];
INT8U RfRecBuf[64];
INT8U SensorData[20]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; // 传感器数据
Rf_Route rf_route_data;
INT16U g_pre_src;
INT8U g_module_rpl = 0x01;
INT8U g_route_size = 0x01;
//INT8U Test[20] = "Send Packet";
// 路由申请命令
// Orien
// AA 0B 01 81 10 01 51 01 01 00 01 10 01 FD
INT8U SearchData[14] = {0xAA,0x0B,0x01,0x81,0x10,0x01,0x51,0x01,0x01,0x00,0x01,0x10,0x01,0x00};
//***************更多功率参数设置可详细参考DATACC1100英文文档中第48-49页的参数表******************
//INT8U PaTabel[8] = {0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04}; //-30dBm 功率最小
//INT8U PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60}; //0dBm
INT8U PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};
const RF_SETTINGS rfSettings =
{
0x00,
0x08, // FSCTRL1 Frequency synthesizer control. IF(中频 intermediate frequence) 晶振频率*val/2^10 = 203.125khz
0x00, // FSCTRL0 Frequency synthesizer control. Fxtal/2^14 = 1.59k
0x10, // FREQ2 Frequency control word, high byte. Fcarrier = 晶振频率*val/2^16 = 433Mhz 定义了中心频率
0xA7, // FREQ1 Frequency control word, middle byte.
0x62, // FREQ0 Frequency control word, low byte.
// 空中波特率 100kbaud,载波频率433MHZ 非Manchester编码 4Byte同步字(30/32同步字质量模式),2-FSK
// MDMCFG4指定了信道带宽
// MDMCFG3指定了空中波特率
// MDMCFG2指定滤波器,无线信号调制格式,4Byte前导码,4Byte同步字(30/32同步字质量模式)
// 配置每个信道的带宽
0x5B, // MDMCFG4 Modem configuration. CHANBW_E = 01 CHANBW_M =01 DRATE_E = 11 BW_channel = Fxosc/(8*(4+CHANBW_M)*2^CHANBW_E)= 325khz
0xF8, // MDMCFG3 Modem configuration. DRATE_M = 248 Rdata = 100kBaud 空中波特率
0x03, // MDMCFG2 Modem configuration. DEM_DCFILT_OFF=0 MOD_FORMAT=000 MANCHESTER_EN=0(不使能)SYNC_MODE = 011(30/32sync word bits detected)
0x22, // MDMCFG1 Modem configuration. FEC_EN=0(disable) NUM_PREAMBLE=010(4byte前导码) NU=00 CHANSPC_E=10
0xF8, // MDMCFG0 Modem configuration. 不必关心
// 0信道
0x03, // CHANNR Channel number.
0x47, // DEVIATN Modem deviation setting (when FSK modulation is enabled).
// 少了两个配置,主要针对于wor模式的操作
// MCSM2 WOR模式需要寄存器 需进行配置 RX_TIME[2:0]
// MCSM1 RXOFF_MODE 接收数据包后,RF的状态
0xB6, // FREND1 Front end RX configuration.
0x10, // FREND0 Front end RX configuration.
// MCSM2
// MCSM1 RXOFF_MODE TXOFF_MODE 同时配置为接收完成或发送完成后 切换到IDLE模式
0x18, // MCSM0 Main Radio Control State Machine configuration.
0x1D, // FOCCFG Frequency Offset Compensation Configuration.
0x1C, // BSCFG Bit synchronization Configuration.
0xC7, // AGCCTRL2 AGC control.
0x00, // AGCCTRL1 AGC control.
0xB2, // AGCCTRL0 AGC control.
0xEA, // FSCAL3 Frequency synthesizer calibration.
0x2A, // FSCAL2 Frequency synthesizer calibration.
0x00, // FSCAL1 Frequency synthesizer calibration.
0x11, // FSCAL0 Frequency synthesizer calibration.
0x59, // FSTEST Frequency synthesizer calibration.
0x81, // TEST2 Various test settings.
0x35, // TEST1 Various test settings.
0x09, // TEST0 Various test settings.
0x0B, // IOCFG2 GDO2 output pin configuration.
0x06, // IOCFG0 GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.
// 通过查询GDO0来确认是否接收到数据 接收到数据包置位 数据包末尾结束置位
0x04, // PKTCTRL1 Packet automation control. APPEND_STATUS=1增加两个状态字节以及CRC_OK标记增加在数据包有效负载上 ADR_CHK=00无地址校验
0x05, // PKTCTRL0 Packet automation control. NU=0 WHITE_DATA=1关闭数据白化 PKT_FORMAT=00(use FIFOs for RX and TX) NU=0 CRC_EN=1 LENGTH_CONFIG=01可变长度包模式
0x00, // ADDR Device address.
0xFF // PKTLEN Packet length. 可变数据包模式下,可发送的最大最大字节 定义为最大
};
void main()
{
INT8U i=0;
INT8U search_temp = 0x06; // search_temp 可以增加到6 在search_temp小于3的时候 开始发送广播唤醒
CpuInit();
POWER_UP_RESET_CC1100();
halRfWriteRfSettings();
halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);
CC1101_Setwor();
G_IT_ON; // 开启单片机全局中断
Usart_printf(&g_module_id.Sn[0],1);
Usart_printf(&g_module_id.Sn[1],1);
Usart_printf(&g_gateway.Sn[0],1);
Usart_printf(&g_gateway.Sn[1],1);
Log_printf(" ");
// 上电设置网关
// 只有外部中断没有打开,现在进行设置网关字节 地址和网管不能为全0xFFFF
while( ( 0xFFFF == g_gateway.Sn_temp ) || ( 0xFFFF == g_module_id.Sn_temp ) )
{
LED_D4 = ~LED_D4;
delay(50000);
if( 0x55 == g_rx_flag )
{
g_rx_flag = 0x00;
// 将网关数据写入
IapProgramByte(GATEWAY_ADDRESS,TxBuf[1]);
IapProgramByte(GATEWAY_ADDRESS+1,TxBuf[2]);
//g_gateway.Sn_temp = IapReadByte(GATEWAY_ADDRESS);
// 将地址数据写入
IapProgramByte(MODEL_SN_ADDRESS,TxBuf[3]);
IapProgramByte(MODEL_SN_ADDRESS+1,TxBuf[4]);
//g_module_id.Sn_temp = IapReadByte(MODEL_SN_ADDRESS);
Log_printf("GATEWAY OK\n");
Log_printf("MODEL_SN OK\n");
LED_D4 = 0;
// 读取设置数据
IapReadModelSn(MODEL_SN_ADDRESS,&g_module_id);
IapReadModelSn(GATEWAY_ADDRESS,&g_gateway);
}
}
// g_module_rpl = IapReadByte(MODEL_RPL);
// 默认模块id的路由等级1 模块ID 最高位为0 表示路由模块
g_module_id.Sn[0] |= ( (g_module_rpl<<4) & 0x7F );
g_pre_src = g_module_id.Sn_temp;
Usart_printf(&g_module_id.Sn[0],1);
Usart_printf(&g_module_id.Sn[1],1);
Usart_printf(&g_gateway.Sn[0],1);
Usart_printf(&g_gateway.Sn[1],1);
Log_printf(" initialization ok ");
// 地址网关设置完成
LED_D2 = ~LED_D2;
// 读出搜索模式 首次上电为0xFF 则进行搜索
g_search = IapReadByte(SEARCH_MODE);
if( 0xFF == g_search )
{
SearchMode:
while( search_temp-- != 0 )
{
SearchData[2] = g_rid;
// 网关地址
SearchData[4] = g_gateway.Sn[0];
SearchData[5] = g_gateway.Sn[1];
// 源地址(模块ID)
SearchData[9] = g_module_id.Sn[0];
SearchData[10] = g_module_id.Sn[1];
// 目的地址(网关地址)
SearchData[11] = g_gateway.Sn[0];
SearchData[12] = g_gateway.Sn[1];
for( i=0;i<13;i++)
{
SearchData[13] += SearchData[i];
}
// 进行唤醒时,只需要把路由标识滤除即可 将路由标识高字节分出一位代表是模块还是基站
// 首先发送唤醒波,而后发送数据 进行路由搜索时,使用广播唤醒
if( search_temp < 3 )
CC1101_Wakeupcarry(WorCarry, 2,4);
halRfSendPacket(SearchData, 14);
g_rid++; // 发送完成后g_rid自增
//g_wor_flag = 0x55;
timer = 0;
Timer0_Init(10);
TIMER0_ON;
//g_search = 0x55;
g_enter_rx = 0x55;
// 存放校验和
SearchData[13] = 0x00;
goto EnterRx;
}
g_search = 0x00;
}
// else
// {
// g_search = 0x00;
// }
while (1)
{
//Log_printf("Enter wor\n");
if( 0x55 == g_wor_flag )
{
CC1101_Worwakeup();
// 将接收的数据存储到RxBuf数组中
EnterRx:
while(g_enter_rx)
halRfRxPacket(RfRecBuf);
// 此处进行rf数据处理
if( 0x55 == g_rf_rx_flag )
{
g_rf_rx_flag = 0x00;
RfRouteManage(&rf_route_data);
}
if( 0xFF == g_search ) // 若没有搜索到路径,则跳转回搜索路径
goto SearchMode;
}
halSpiStrobe(CCxxx0_SWORRST); // 复位到 事件1
halSpiStrobe(CCxxx0_SWOR); // 启动WOR
INT1_ON; // 开外部中断
PCON |= PD_ON; // 从掉电模式唤醒后,程序从这行开市
//Log_printf("Exit pd\n");
}
}
// while (1)
// {
// //Log_printf("first pd\n");
// INT1_ON; // 开外部中断
// PCON |= PD_ON; // 从掉电模式唤醒后,程序从这行开市
// if( 0x55 == g_wor_flag )
// {
// g_wor_flag = 0x00;
// CC1101_Worwakeup();
// while( 0x55 == g_rf_rx_flag )//&& 0x00 == g_rx_timeout
// {
// CC1101_EnterRx(RxBuf);
// }
// }
// halSpiStrobe(CCxxx0_SWORRST); //复位到 事件1
// halSpiStrobe(CCxxx0_SWOR); //启动WOR
// //Log_printf("Exit pd\n");
// }
//void main()
// {
// INT16U i;
// INT8U eerom;
// UART_init();
// LED_D4 = ~LED_D4;
// delay(50000);
// //P1 = 0xfe; //1111,1110 System Reset OK
// //Delay(10); //Delay
// IapEraseSector(IAP_ADDRESS); //Erase current sector
// for (i=0; i<512; i++) //Check whether all sector data is FF
// {
// if (IapReadByte(IAP_ADDRESS+i) != 0xff)
// goto Error; //If error, break
// }
// //P1 = 0xfc; //1111,1100 Erase successful
// //Delay(10); //Delay
//
// LED_D3 = ~LED_D3;
// delay(50000);
// for (i=0; i<512; i++) //Program 512 bytes data into data flash
//
// {
// IapProgramByte(IAP_ADDRESS+i, (INT8U)i);
// }
//
// //P1 = 0xf8; //1111,1000 Program successful
// //Delay(10); //Delay
// LED_D2 = ~LED_D2;
// delay(50000);
//
// for (i=0; i<512; i++) //Verify 512 bytes data
// {
// //eerom = IapReadByte(IAP_ADDRESS+i);
//
// if ( (eerom = IapReadByte(IAP_ADDRESS+i)) != (INT8U)i)
// goto Error; //If error, break
// Usart_printf(&eerom,1);
//
// }
// LED_D1 = ~LED_D1;
// delay(50000);
// //P1 = 0xf0; //1111,0000 Verify successful
// while (1);
//Error:
// //P1 &= 0x7f; //0xxx,xxxx IAP operation fail
// Log_printf("Error EEPROM");
// while (1);
//
// }