]> rtime.felk.cvut.cz Git - sysless.git/blob - board/arm/lpceurobot/app/minibee/main.c
1a2c4c6916af66d6308f4f96f3acfde002307584
[sysless.git] / board / arm / lpceurobot / app / minibee / main.c
1 //
2 // Author: Bc. Jiri Kubias <Jiri.kubias@gmail.com>, (C) 2008
3 //
4 // Copyright: (c) DCE FEE CTU - Department of Control Engeneering
5 // License: GNU GPL v.2
6 //
7
8
9 /**
10  * \mainpage Zigbee radio MC13192 or MC13202
11  * @author Bc. Jiri Kubias , DCE FEL CTU 2008
12  * \section Introduction
13  *      This code provides same sample how to use MC13xx2 radio in packet mode.
14  *      The basic libraries for testing are writen mostly platform independent. 
15  *      See the function description. In some function is noted that use some platform 
16  *      depended parts whis should be modified. Originaly this code was developed for 
17  *      MC13192 radio, but now we use MC13202 radio which is almost fully compatible.
18  * 
19  *      MC1319x.c - is mostly platform independent, see notes inside. This library 
20  *      contains basic function which contains funcion for send, recieve, set clko, 
21  *      read ID, set frequency and do energy detect.
22  * 
23  *      MC1319xdef.h - contains register definions and bit masks for MC13xxx radio
24  * 
25  *      spi.h - platform independent. Contains function prototypes and definition 
26  *      SPI_SPEED for setting SPI speed in Hz.
27  * 
28  *      spi_LPC.c - platform dependent. Contains inicializing function for SPI channel,
29  *      all other used pins and installs ISR.
30  * 
31  *      uart_minibee.h  - platform independent. Function prototypes for uart communication
32  *      
33  *      uart_minibee.c  - particulary platform dependent. Some part of this code must be ported 
34  *      for new platform.
35  *      
36  *      Porting to another platform:
37  *              For another platform you must implement your own SPI function, 
38  *              set pins and install ISR. It must support the prototypes functions 
39  *              which is defined in spi.h. You also must implement your
40  *              function for uart communication (see uart_minibee.c).
41  * 
42  *      The last tested platform is LpcEurobot which is ARM7 micproporcesor LPC2119. 
43  *      It also use eurobot ebb library for UART communication for ARM7. Older platform 
44  *      was AVR which is no longer supported due to no platform for testing. In this code 
45  *      is added spi_avr.c which is not fully ported.
46  * 
47  *      @note   Known issues:
48  *                      1) IRQ edge sensive - in this code are parts where is ISR at IRQ pins is disabled. 
49  *                      If this code didnt fully acknowledge IRQ in radio the renewed ISR will not recognise
50  *                      IRQ request. If this occurs the main MCU must be reseted. 
51  *                              Solutions:
52  *                                      a) comment  line 73 in spi_LPC.c - in commnent is  ***
53  *                                      b) move all code to ISR and never disable ISR on IRQ pin - recomended
54  */
55
56 /**
57  * @file   main.c
58  * @author Bc. Jiri Kubias , DCE FEL CTU 2008
59  * 
60  * @brief  Demo application demonstrating use of MC1319x library.
61  *      This file provides simply how-to use MC1319x library.
62  *      From main function is called init function of SPI and UART. 
63  *      After sucessfull inicialize it tries read chip ID and recognize
64  *      connected radio. Whan everythig is sucessfull it runs command 
65  *      processor which allows you to test radio functions.
66  * 
67  */
68
69
70
71
72
73
74 #include <lpc21xx.h>                            /* LPC21xx definitions */
75 #include <deb_led.h>
76 #include <types.h>
77 #include "MC1319x.h"
78 #include "spi.h"
79 #include "uart_minibee.h"
80
81
82
83 struct Message MsgSnd;  ///< Send buffer
84 struct Message MsgRcv;  ///< Recieve buffer
85
86
87
88
89 /**
90  *      Send data via radio
91  *      @param *msg     pointer to Message sructure
92  */
93 void sendData(struct Message *msg)
94 {
95         msg->done = 0;
96         msg->error = 0;
97         MC_SendPaket(msg);
98 }
99
100 /**
101  *      Reads data from Radio buffer
102  */
103 void recieveData(void)
104 {
105         MsgRcv.done = 0;
106         MsgRcv.error = 0;
107
108         MC_RecievePaket(&MsgRcv);
109 }
110 /**
111  *      At first it inicializes serial and SPI line + other pins + ISR (RXTXEN, IRQ....) 
112  *      then it inicialzes Radio and reads radio ID. Whan everything is sucessfull 
113  *      runs the serial command processor.
114  *      
115  */
116
117 int main (void)  {
118
119         volatile uint32_t test =0;      
120
121         // init serial line 
122         init_uart();
123
124         // init SPI line, ISR and oter pins
125         send_rs_str("SPI init.\n");
126         spi_Init(5);
127         
128         // reset and inicialize radio
129         send_rs_str("\nMC reset\n");
130         if (MC_Reset()==1) 
131         {
132                 send_rs_str("FAIL\n");
133                 while(1);
134         }       
135         
136         // Read and test radio ID
137         send_rs_str("Who am i:\n");
138         test = MC_WhoAmI();
139         if((test == 0x5000) | (test == 0x5400))
140         {
141                 send_rs_str("MC13192 Detected\n");
142         }
143         else if ((test == 0x6000) | (test == 0x6400))
144                 send_rs_str("MC13202 Detected\n");
145         else 
146         {
147                 send_rs_str("ID FAIL: ");
148                 send_rs_int(test);
149                 while(1);
150         }
151
152
153         MC_SetPa(0xFF);                 // nastavi vysilaci vykon
154         MC_SetChannel(ZB_CH802_21);     // nastavi kanal
155         
156         
157
158         int command = 0;
159         int myvar = 0;
160         int j = 0;
161         send_rs_str("Entering command processor loop\n");       
162         
163         while(1)
164         {
165                 send_rs_str("-----------------------------------\n");
166                 send_rs_str("1 Send message\n");
167                 send_rs_str("2 Recieve message\n");
168                 send_rs_str("3 Get IRQ pin\n");
169                 send_rs_str("4 enable ISR\n");
170                 send_rs_str("5 Read IRQ\n");
171                 send_rs_str("6 Set channel\n");
172                 send_rs_str("7 Energy detect\n");
173                 send_rs_str("8 Set clock output\n");
174                 send_rs_str("-----------------------------------\n");
175                                 
176                 command = uart_get_char();
177                 uart_send_char(command);
178                 uart_send_char('\n');
179                 
180                 switch(command){
181                         case '1':
182                                 send_rs_str("Sending message\n");
183                                 MsgSnd.len = 14;
184                                 MsgSnd.data[0] = 0x88;
185                                 MsgSnd.data[1] = 0x41;
186                                 MsgSnd.data[2] = 0x22;
187                                 MsgSnd.data[3] = 0x88;
188                                 MsgSnd.data[4] = 0xFF;
189                                 MsgSnd.data[5] = 0x00;
190                                 MsgSnd.data[6] = 0x01;
191                                 MsgSnd.data[7] = 0xFF;
192                                 MsgSnd.data[8] = 0x06;
193                                 MsgSnd.data[9] = 0x00;
194                                 MsgSnd.data[10] = 0x01;
195                                 MsgSnd.data[11] = 0x00;
196                                 MsgSnd.data[12] = 0x89;
197                                 MsgSnd.data[13] = 0x00;
198                                 sendData(&MsgSnd);      
199                                 break;
200                                 
201                         case '5':
202                                 disable_IRQ_pin();
203                                 send_rs_str("IRQ register is: ");
204                                 myvar = spi_Read(IRQ_STATUS);   // acknowledge power-on IRQ     
205                                 send_rs_int(myvar);
206                                 enable_IRQ_pin();
207                                 break;
208                                 
209                         case '3':
210                                 send_rs_str("IRQ pin is: ");
211                                 if((IO0PIN & (1<<IRQ)) == (1<<IRQ))
212                                         send_rs_str("true\n");  
213                                 else
214                                         send_rs_str("false\n");
215                                 break;
216                                 
217                         case '4': send_rs_str("Enable ISR: OK\n");
218                                 enable_IRQ_pin();
219                                 break;
220                                 
221                         case '2': send_rs_str("Recieving:\n");
222                                 recieveData();
223                                 myvar = 0;
224                                 while(myvar<10)
225                                 {
226                                         if((MsgRcv.done == 1))
227                                         {
228                                                 send_rs_str("Recieved Data:\n");
229                                                 send_rs_str("Msg.len: ");
230                                                 send_rs_int(MsgRcv.len);
231                                                 send_rs_str("\nMsg.data: \n");
232                                                 for (j = 0; j < MsgRcv.len; j++)
233                                                 {
234                                                         send_rs_int(MsgRcv.data[j]);
235                                                         send_rs_str("\n");                              
236                                                 }
237                                                 
238                                                 break;
239                                         }
240                                         else{
241                                                 ++myvar;
242                                                 dummy_wait();
243                                         }
244                                 }
245                                 
246                                 if(myvar == 10)
247                                         send_rs_str("Timed out\n");             
248                                 else
249                                         send_rs_str("Reading done!\n");         
250                                 break;
251                                 
252                         case '6':
253                                 send_rs_str("Select channel (11~26)\n");
254                                 j = uart_get_char();
255                                 uart_send_char(j);
256                                 
257                                 if(!((j == '1')|(j=='2'))){                     
258                                         send_rs_str("Bad value\n");
259                                         break;
260                                 }
261                                 
262                                 myvar = (j - '0')* 10;
263                                 
264                                 j = uart_get_char();
265                                 uart_send_char(j);
266                                 if(!((j >= '0')&(j<='9'))){
267                                         send_rs_str("Bad value\n");
268                                         break;
269                                 }
270                                 myvar += (j - '0');
271                                 myvar -= 10;
272                                 MC_SetChannel(myvar);
273                                 break;
274                                 
275                         case '7':
276                                 send_rs_str("Energy detect: ");
277                                 send_rs_int(MC_ED());
278                                 break;
279                                 
280                         case '8':
281                                 send_rs_str("Enable CLKO? (y/n) ");
282                                 j = uart_get_char();
283                                 uart_send_char(j);
284                                 if( j == 'n')
285                                 {
286                                         send_rs_str("CLKO - Switching OFF");    
287                                         MC_SetClko(CLKO_CTL_CLKO_16Km, CLKO_CTL_CLKO_OFFm);
288                                         break;
289                                 }
290                                 else if( j != 'y')
291                                 {
292                                         send_rs_str("\nInvalid command\n");     
293                                         break;
294                                 }
295                                 
296                                 
297                                 send_rs_str("Select clock\n");
298                                 send_rs_str("1 16.393kHz\n");
299                                 send_rs_str("2 32.786kHz\n");
300                                 send_rs_str("3 62.5kHz\n");
301                                 send_rs_str("4 1Mhz\n");
302                                 send_rs_str("5 2Mhz\n");
303                                 send_rs_str("6 4Mhz\n");
304                                 send_rs_str("7 8Mhz\n");
305                                 send_rs_str("8 16Mhz\n");
306                                         
307                                 j = uart_get_char();
308                                 uart_send_char(j);
309                                         
310                                 if(('0' < j) & (j < '9'))
311                                 {
312                                         switch(j){
313                                                 case '1':
314                                                         MC_SetClko(CLKO_CTL_CLKO_16Km, CLKO_CTL_CLKO_ONm);
315                                                         break;
316                                                 case '2':
317                                                         MC_SetClko(CLKO_CTL_CLKO_32Km, CLKO_CTL_CLKO_ONm);
318                                                         break;
319                                                 case '3':
320                                                         MC_SetClko(CLKO_CTL_CLKO_62Km, CLKO_CTL_CLKO_ONm);
321                                                         break;
322                                                 case '4':
323                                                         MC_SetClko(CLKO_CTL_CLKO_1Mm, CLKO_CTL_CLKO_ONm);
324                                                         break;
325                                                 case '5':
326                                                         MC_SetClko(CLKO_CTL_CLKO_2Mm, CLKO_CTL_CLKO_ONm);
327                                                         break;
328                                                 case '6':
329                                                         MC_SetClko(CLKO_CTL_CLKO_4Mm, CLKO_CTL_CLKO_ONm);
330                                                         break;
331                                                 case '7':
332                                                         MC_SetClko(CLKO_CTL_CLKO_8Mm, CLKO_CTL_CLKO_ONm);
333                                                         break;
334                                                 case '8':
335                                                         MC_SetClko(CLKO_CTL_CLKO_16Mm, CLKO_CTL_CLKO_ONm);
336                                                         break;
337                                                 default:
338                                                         break;
339                                         }
340                                 }
341                                 else
342                                 {
343                                         
344                                         send_rs_str("Invalid command\n");
345                                 }
346                                 break;
347                                 
348                                 
349                         default:
350                                 send_rs_str("Bad command!\n");
351                                 break;
352                                 
353                 }
354                 
355                 dummy_wait();
356                 
357                 // test if sessage is sent
358                 if ((MsgSnd.done == 1))
359                 {
360                         send_rs_str("Message sent!!!!!!!!!!!!!!\n");
361                         MsgSnd.done = 0;
362                         // add same handle code her
363                 }
364
365                 // test if some packet is recieved
366                 if ((MsgRcv.done == 1))
367                 {
368                         // add same handle code here
369                 }
370                 
371                 
372                 
373                 send_rs_str("\nHit key to continue ...");
374                 uart_get_char();
375                 send_rs_str("\n\n\n\n\n\n\n\n\n\n");
376         }
377
378 }
379
380
381