Adjusted status LED definitions
[LPC2xxx_and_RobotSpejbl.git] / app / minibee / MC1319x.c
blob068749270aff93cb2bd1ceecbea30b919a1e776e
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 //
9 /**
10 * @file MC1319x.c
11 * @author Bc. Jiri Kubias , DCE FEL CTU 2008
13 * @brief Library for MC13xx2 freescale radio
19 #include "spi.h"
20 #include "MC1319x.h"
21 #include "MC1319xdef.h"
24 #ifdef LPC
25 #include <lpc21xx.h> /* LPC21xx definitions */
26 #include <types.h>
27 #include <deb_led.h>
28 #endif
30 struct Message *rcvBuf; ///< Pointer to recieve Message buffer
31 struct Message *sndBuf; ///< Pointer to send Message buffer
34 uint8_t MC_RecievePaket(struct Message *msg);
38 /**
39 * Interupt handling. Here is reading IRQ register and deremine what generates IRQ
40 * @note This function is platform depended
43 #ifdef ATmega88
44 ISR(INT0_vect)
45 #endif
46 #ifdef LPC
47 void ext_isr(void)
48 #endif
51 uint16_t dummy =0;
53 #ifdef LPC
54 EXTINT |= EXTINT_EINT3_m ; // Acknowledge interupt source
55 #endif
57 deb_led_on(LEDR);
58 // send_rs_str("ISR\n"); // printout that ISR occurs
60 uint16_t mask = spi_Read(IRQ_STATUS);
62 if((mask & IRQ_STATUS_RX_RCVD_IRQm) == IRQ_STATUS_RX_RCVD_IRQm) // Recieved data
64 CLR(RXTXEN); // clear
66 dummy = (spi_Read(RX_STATUS) & 0x7F);
68 if(!(mask & 1))
70 MC_RecievePaket(rcvBuf);
71 goto ISR_END;
74 rcvBuf->done = 1;
75 rcvBuf->len = dummy;
77 spi_Read_Buf(rcvBuf);
81 if((mask & IRQ_STATUS_TX_SENT_IRQm) == IRQ_STATUS_TX_SENT_IRQm) // Data send done
83 CLR(RXTXEN); // clear
84 sndBuf->done = 1;
88 // if((mask & IRQ_STATUS_PLL_LOCK_IRQm) == IRQ_STATUS_PLL_LOCK_IRQm); //todo , lost PLL LOCK
89 // if((mask & IRQ_STATUS_RAM_ADR_ERRm) == IRQ_STATUS_RAM_ADR_ERRm) ; //todo, RAM buffer overrun
90 // if((mask & IRQ_STATUS_ARB_BUSY_ERRm) == IRQ_STATUS_ARB_BUSY_ERRm) ; //todo
91 // if((mask & IRQ_STATUS_SRTM_DATA_ERRm) == IRQ_STATUS_SRTM_DATA_ERRm); //todo Streaming data requested/aviable
92 // if((mask & IRQ_STATUS_ATTN_IRQm) == IRQ_STATUS_ATTN_IRQm); //todo ATTN asserted
93 // if((mask & IRQ_STATUS_DOZE_IRQm) == IRQ_STATUS_DOZE_IRQm); // todo timer event - return to idle from doze
94 // if((mask & IRQ_STATUS_CCA_IRQm) == IRQ_STATUS_CCA_IRQm); //todo CCA/ED done
95 // if((mask & IRQ_STATUS_TMR1_IRQm) == IRQ_STATUS_TMR1_IRQm);//todo timer 1 match
96 // if((mask & IRQ_STATUS_TMR2_IRQm) == IRQ_STATUS_TMR2_IRQm);//todo timer 2 match
97 // if((mask & IRQ_STATUS_TMR3_IRQm) == IRQ_STATUS_TMR3_IRQm);//todo timer 3 match
98 // if((mask & IRQ_STATUS_TMR4_IRQm) == IRQ_STATUS_TMR4_IRQm);//todo timer 4 match
99 // if((mask & IRQ_STATUS_CCAm) == IRQ_STATUS_CCAm); // output of the comparation of CCA
100 // if((mask & IRQ_STATUS_CRC_VALIDm) == IRQ_STATUS_CRC_VALIDm); // recieved correct CRC
102 ISR_END:
104 #ifdef LPC
105 VICVectAddr = 0; // acknowledge ISR in VIC
106 #endif
107 deb_led_off(LEDR);
110 * Delay function.
111 * @note Should be removed in future
113 void dummy_wait()
115 unsigned int wait = 5000000;
116 while(--wait);
122 * Woodoo function inicialize radio. I hove no idea what it is doing, but its VERY inportatnt.
124 void MC_Woodoo(void)
126 spi_ReadModifiWrite(0x08, 0x0001, 0xFFFF);
127 spi_ReadModifiWrite(0x08, 0x0010, 0xFFFF);
128 spi_ReadModifiWrite(0x11, 0x0000, 0xFCFF);
129 spi_ReadModifiWrite(0x06, 0x4000, 0xFFFF);
134 * Reset the radio to it default state and inicialize it
136 uint8_t MC_Reset(void) // generate reset on MC13192x
140 disable_IRQ_pin();
142 // reset MC radio
143 SET(ATTN);
144 CLR(RXTXEN);
145 CLR(RST);
146 dummy_wait();
147 SET(RST);
150 // wait for IRQ
151 while( (IO0PIN & (1<<IRQ)) == (1<<IRQ)); // waiting for incomming interrupt
153 MC_Woodoo(); // radio init woodoo
155 if(spi_Read(RST_IND) & RST_IND_RESET_INDm) // test reset indicator
157 return 1; // return 1 if test fails
160 spi_Read(IRQ_STATUS); // acknowledge power-on IRQ
162 spi_ReadModifiWrite( GPIO_DATA_OUT, (1<<7) | (1<<9) ,0xFFFF); // eneble IRQ pullup and set moderate drive strenght
163 spi_ReadModifiWrite( CONTROL_B, (1<<12)|(1<<14),0xFFFF);
164 spi_ReadModifiWrite( CONTROL_A, CONTROL_A_RX_RCVDm|CONTROL_A_TX_SENTm,0xFFFF);
166 #ifdef LPC
167 EXTINT |= EXTINT_EINT3_m ; // acknowledge ISR
168 #endif
170 enable_IRQ_pin();
171 return 0;
175 * Scan selected radio channel and returns measured energy (lower is better)
176 * @note This function is not using ISR, but in future it may do.
178 uint8_t MC_ED(void)
180 uint16_t mask;
181 uint8_t val;
183 disable_IRQ_pin();
185 CLR(RXTXEN); //switch off radio output
186 spi_ReadModifiWrite(CONTROL_A,CONTROL_A_CCA_EDm|CONTROL_A_CCA_XCVR_CCAEDm | CONTROL_A_CCAm, \
187 ~(CONTROL_A_CCA_TYPE0m | CONTROL_A_CCA_XCVR_SEG1m ));
188 // selects CCA as ED type and sets transerciver to CCA/ED mode and alows to generate CCA interrupt
191 SET(RXTXEN); // starts the ED detection
193 while( (IO0PIN & (1<<IRQ)) == (1<<IRQ)); // waiting for incomming interrupt
195 CLR(RXTXEN); //switch off radio output
197 mask = spi_Read(IRQ_STATUS); // acknowledge interrupt
198 if((mask & IRQ_STATUS_CCA_IRQm) != IRQ_STATUS_CCA_IRQm) return 1;
200 val = (uint8_t)(spi_Read(RX_STATUS) >> 8);
202 enable_IRQ_pin();
204 return val;
208 * Sends the packed stored in Message structure
209 * @param *msg ponter to Message structure
211 uint8_t MC_SendPaket(struct Message *msg)
214 sndBuf = msg;
215 msg->done = 0;
217 CLR(RXTXEN); //switch off radio output
219 spi_ReadModifiWrite(CONTROL_A, CONTROL_A_TX_SENTm | CONTROL_A_RX_RCVDm \
220 ,~(CONTROL_A_TX_STRMm|CONTROL_A_RX_STRMm|CONTROL_A_TMR_TRIG_ENm|CONTROL_A_CCA_XCVR_CLRm) );
221 // selects CCA as ED type and sets transerciver to CCA/ED mode and alows to generate CCA interrupt
223 spi_Write(TX_PKT_CTL, TX_PKT_CTL_PKT_LENGHT(msg->len)); // write the number data bytes in message + 2
224 spi_Write_Buf (msg); // recursive write data to MC radio
226 spi_ReadModifiWrite(CONTROL_A, CONTROL_A_CCA_XCVR_PMTXm, 0xFFFF); // Sets TX mode
227 SET(RXTXEN); // starts transmit
229 return 0;
233 * Resend the packed stored in radio transmit buffer
234 * @param *msg pointer to Message structure
235 * @note Not sure if it is working
237 uint8_t MC_ReSendPaket(struct Message *msg)
239 sndBuf = msg;
240 msg->done = 0;
242 CLR(RXTXEN); //switch off radio output
244 spi_ReadModifiWrite(CONTROL_A, CONTROL_A_TX_SENTm | CONTROL_A_RX_RCVDm \
245 ,~(CONTROL_A_TX_STRMm|CONTROL_A_RX_STRMm|CONTROL_A_TMR_TRIG_ENm|CONTROL_A_CCA_XCVR_CLRm) );
246 // selects CCA as ED type and sets transerciver to CCA/ED mode and alows to generate CCA interrupt
247 spi_Write(TX_PKT_CTL, TX_PKT_CTL_PKT_LENGHT(msg->len)); // recursive write data to MC radio
249 spi_ReadModifiWrite(CONTROL_A, CONTROL_A_CCA_XCVR_PMTXm, 0xFFFF); // Sets TX mode
251 SET(RXTXEN); // starts transmit
253 return 0;
258 * Reads packet stored in radio buffer
259 * @param *msg pointer to Message structure
261 uint8_t MC_RecievePaket(struct Message *msg)
263 uint16_t dummy;
265 rcvBuf = msg;
266 msg->done = 0;
268 CLR(RXTXEN); //switch off radio output
270 spi_ReadModifiWrite(CONTROL_A, CONTROL_A_TX_SENTm | CONTROL_A_RX_RCVDm | CONTROL_A_CCA_XCVR_PMRXm \
271 ,~(CONTROL_A_TX_STRMm|CONTROL_A_RX_STRMm|CONTROL_A_TMR_TRIG_ENm|CONTROL_A_CCA_XCVR_CLRm) );
272 // selects CCA as ED type and sets transerciver to CCA/ED mode and alows to generate CCA interrupt
274 dummy = (spi_Read(RX_STATUS) & 0x7F);
275 SET(RXTXEN); // starts recieving
277 return 0;
282 * Returns radio identification
284 uint16_t MC_WhoAmI(void)
286 return spi_Read(CHIP_ID);
290 * Sets radio frequency channel
291 * @param channel specifies frequency output (1~16)
292 * @note See definions from MC1319xdef.h
294 uint8_t MC_SetChannel(uint8_t channel)
296 uint16_t int_div = 0;
297 uint16_t lo1_num = 0;
299 switch(channel)
301 case 1: int_div = LO1_INT_DIV_CH1;
302 lo1_num = LO1_NUM_CH1;
303 break;
305 case 2: int_div = LO1_INT_DIV_CH2;
306 lo1_num = LO1_NUM_CH2;
307 break;
309 case 3: int_div = LO1_INT_DIV_CH3;
310 lo1_num = LO1_NUM_CH3;
311 break;
313 case 4: int_div = LO1_INT_DIV_CH4;
314 lo1_num = LO1_NUM_CH4;
315 break;
317 case 5: int_div = LO1_INT_DIV_CH5;
318 lo1_num = LO1_NUM_CH5;
319 break;
321 case 6: int_div = LO1_INT_DIV_CH6;
322 lo1_num = LO1_NUM_CH6;
323 break;
325 case 7: int_div = LO1_INT_DIV_CH7;
326 lo1_num = LO1_NUM_CH7;
327 break;
329 case 8: int_div = LO1_INT_DIV_CH8;
330 lo1_num = LO1_NUM_CH8;
331 break;
333 case 9: int_div = LO1_INT_DIV_CH9;
334 lo1_num = LO1_NUM_CH9;
335 break;
337 case 10: int_div = LO1_INT_DIV_CH10;
338 lo1_num = LO1_NUM_CH10;
339 break;
341 case 11: int_div = LO1_INT_DIV_CH11;
342 lo1_num = LO1_NUM_CH11;
343 break;
345 case 12: int_div = LO1_INT_DIV_CH12;
346 lo1_num = LO1_NUM_CH12;
347 break;
349 case 13: int_div = LO1_INT_DIV_CH13;
350 lo1_num = LO1_NUM_CH13;
351 break;
353 case 14: int_div = LO1_INT_DIV_CH14;
354 lo1_num = LO1_NUM_CH14;
355 break;
357 case 15: int_div = LO1_INT_DIV_CH15;
358 lo1_num = LO1_NUM_CH15;
359 break;
361 case 16: int_div = LO1_INT_DIV_CH16;
362 lo1_num = LO1_NUM_CH16;
363 break;
365 default: return 0;
368 spi_Write(LO1_NUM,lo1_num);
369 spi_ReadModifiWrite(LO1_INT_DIV,int_div,0xFF00);
371 return 1;
375 * Sets CLKO pin to specified clock output
376 * @param tick specifies frequency output
377 * @param enable enable or disable output
378 * @note Use definions from MC1319xdef.h
380 uint8_t MC_SetClko(uint16_t tick, uint8_t enable)
383 spi_ReadModifiWrite(CLKO_CTL,tick,~(CLKO_CTL_CLKO_CLRm));
385 if (enable == 1)
387 spi_ReadModifiWrite(CONTROL_C,CONTROL_C_CLKO_ENm,0xFFFF);
389 else
391 spi_ReadModifiWrite(CONTROL_C,0,~(CONTROL_C_CLKO_ENm));
394 return 0;
398 * Im not remeber what is this doing.
400 uint8_t MC_SetPa(uint8_t val)
402 spi_ReadModifiWrite(PA_LVL,(uint16_t)val,0xFFFF);
403 return 0;