2 // Author: Bc. Jiri Kubias <Jiri.kubias@gmail.com>, (C) 2008
4 // Copyright: (c) DCE FEE CTU - Department of Control Engeneering
5 // License: GNU GPL v.2
11 * @author Bc. Jiri Kubias , DCE FEL CTU 2008
13 * @brief Library for MC13xx2 freescale radio
21 #include "MC1319xdef.h"
25 #include <lpc21xx.h> /* LPC21xx definitions */
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
);
39 * Interupt handling. Here is reading IRQ register and deremine what generates IRQ
40 * @note This function is platform depended
54 EXTINT
|= EXTINT_EINT3_m
; // Acknowledge interupt source
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
66 dummy
= (spi_Read(RX_STATUS
) & 0x7F);
70 MC_RecievePaket(rcvBuf
);
81 if((mask
& IRQ_STATUS_TX_SENT_IRQm
) == IRQ_STATUS_TX_SENT_IRQm
) // Data send done
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
105 VICVectAddr
= 0; // acknowledge ISR in VIC
111 * @note Should be removed in future
115 unsigned int wait
= 5000000;
122 * Woodoo function inicialize radio. I hove no idea what it is doing, but its VERY inportatnt.
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
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);
167 EXTINT
|= EXTINT_EINT3_m
; // acknowledge ISR
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.
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);
208 * Sends the packed stored in Message structure
209 * @param *msg ponter to Message structure
211 uint8_t MC_SendPaket(struct Message
*msg
)
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
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
)
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
258 * Reads packet stored in radio buffer
259 * @param *msg pointer to Message structure
261 uint8_t MC_RecievePaket(struct Message
*msg
)
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
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;
301 case 1: int_div
= LO1_INT_DIV_CH1
;
302 lo1_num
= LO1_NUM_CH1
;
305 case 2: int_div
= LO1_INT_DIV_CH2
;
306 lo1_num
= LO1_NUM_CH2
;
309 case 3: int_div
= LO1_INT_DIV_CH3
;
310 lo1_num
= LO1_NUM_CH3
;
313 case 4: int_div
= LO1_INT_DIV_CH4
;
314 lo1_num
= LO1_NUM_CH4
;
317 case 5: int_div
= LO1_INT_DIV_CH5
;
318 lo1_num
= LO1_NUM_CH5
;
321 case 6: int_div
= LO1_INT_DIV_CH6
;
322 lo1_num
= LO1_NUM_CH6
;
325 case 7: int_div
= LO1_INT_DIV_CH7
;
326 lo1_num
= LO1_NUM_CH7
;
329 case 8: int_div
= LO1_INT_DIV_CH8
;
330 lo1_num
= LO1_NUM_CH8
;
333 case 9: int_div
= LO1_INT_DIV_CH9
;
334 lo1_num
= LO1_NUM_CH9
;
337 case 10: int_div
= LO1_INT_DIV_CH10
;
338 lo1_num
= LO1_NUM_CH10
;
341 case 11: int_div
= LO1_INT_DIV_CH11
;
342 lo1_num
= LO1_NUM_CH11
;
345 case 12: int_div
= LO1_INT_DIV_CH12
;
346 lo1_num
= LO1_NUM_CH12
;
349 case 13: int_div
= LO1_INT_DIV_CH13
;
350 lo1_num
= LO1_NUM_CH13
;
353 case 14: int_div
= LO1_INT_DIV_CH14
;
354 lo1_num
= LO1_NUM_CH14
;
357 case 15: int_div
= LO1_INT_DIV_CH15
;
358 lo1_num
= LO1_NUM_CH15
;
361 case 16: int_div
= LO1_INT_DIV_CH16
;
362 lo1_num
= LO1_NUM_CH16
;
368 spi_Write(LO1_NUM
,lo1_num
);
369 spi_ReadModifiWrite(LO1_INT_DIV
,int_div
,0xFF00);
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
));
387 spi_ReadModifiWrite(CONTROL_C
,CONTROL_C_CLKO_ENm
,0xFFFF);
391 spi_ReadModifiWrite(CONTROL_C
,0,~(CONTROL_C_CLKO_ENm
));
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);