-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrfm300_demo.c
147 lines (127 loc) · 2.98 KB
/
rfm300_demo.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
#include "radio.h"
#include <rtconfig.h>
#define DBG_TAG "cmt_demo"
#define DBG_LVL DBG_ERROR
#include <rtdbg.h>
#if (PKG_RFM300_CSB_PIN < 0) || (PKG_RFM300_FCSB_PIN < 0)
#error Please specify a valid pin
#endif
#define RF_PACKET_SIZE 32 /* Define the payload size here */
static uint8_t rf_rx_buffer[RF_PACKET_SIZE]; /* RF Rx buffer */
static uint8_t rf_tx_buffer[RF_PACKET_SIZE]; /* RF Tx buffer */
static uint8_t enable_master_flg = RT_TRUE; /* Master/Slave selection */
static uint16_t rf_recv_count = 0;
static uint16_t rf_send_count = 0;
static uint16_t rf_err_count = 0;
/* Manages the master operation */
void OnMaster(void)
{
char str[32];
switch (RF_Process())
{
case RF_IDLE:
{
rf_send_count++;
rf_tx_buffer[0] = (uint8_t)rf_send_count;
rf_tx_buffer[1] = rf_send_count >> 8;
RF_StartTx(rf_tx_buffer, RF_PACKET_SIZE, 1000);
break;
}
case RF_TX_DONE:
{
LOG_D("send: %d", rf_send_count);
RF_StartRx(rf_rx_buffer, RF_PACKET_SIZE, 1000);
break;
}
case RF_RX_DONE:
{
rf_recv_count++;
LOG_D("recv: %d", rf_recv_count);
break;
}
case RF_RX_TIMEOUT:
{
LOG_D("recv: timeout");
break;
}
case RF_ERROR:
{
LOG_D("error: %d", ++rf_err_count);
break;
}
default:
break;
}
}
/* Manages the slave operation */
void OnSlave(void)
{
char str[32];
switch (RF_Process())
{
case RF_IDLE:
{
RF_StartRx(rf_rx_buffer, RF_PACKET_SIZE, 0xFFFFFFFF);
break;
}
case RF_RX_DONE:
{
rf_recv_count++;
LOG_D("recv: %d", rf_recv_count);
CMT2300A_DelayMs(10);
rf_send_count++;
rf_tx_buffer[0] = (uint8_t)rf_send_count;
rf_tx_buffer[1] = rf_send_count >> 8;
RF_StartTx(rf_tx_buffer, RF_PACKET_SIZE, 1000);
break;
}
case RF_TX_DONE:
{
LOG_D("send: %d", rf_send_count);
break;
}
case RF_ERROR:
{
LOG_D("error: %d", ++rf_err_count);
break;
}
default:
break;
}
}
static void rfm300_thread_entry(void *param)
{
while (1)
{
if (enable_master_flg == RT_TRUE)
{
OnMaster();
}
else
{
OnSlave();
}
}
}
static int rfm300_sample_init(void)
{
rt_thread_t tid;
RF_Init();
if (CMT2300A_IsExist() == RT_FALSE)
{
LOG_E("CMT2300A not found!");
return RT_EEMPTY;
}
else
{
LOG_I("CMT2300A ready");
}
tid = rt_thread_create("rfm300_demo", rfm300_thread_entry, RT_NULL,
1024, RT_THREAD_PRIORITY_MAX/2, 20);
if (tid)
{
rt_thread_startup(tid);
}
return 0;
}
INIT_APP_EXPORT(rfm300_sample_init);