For robots and stuff
Diff: CC1101/CC1101-Threads.cpp
- Revision:
- 1:05a48c038381
- Parent:
- 0:c5afea7b9057
- Child:
- 2:c42a035d71ed
diff -r c5afea7b9057 -r 05a48c038381 CC1101/CC1101-Threads.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CC1101/CC1101-Threads.cpp Sun Dec 28 06:28:42 2014 +0000 @@ -0,0 +1,98 @@ +#include "Radio.h" + + +/* + +// SERIAL THREAD +void CC1101::get_serial_data_thread(void const *arg) +{ + CC1101 *instance = (CC1101*)arg; + osSignalWait(START_THREAD, osWaitForever); + Timer t; + + while(1) { + + if (instance->_pc.readable()) { + + if (t.read_us()) { + t.reset(); + } else { + t.start(); + } + + instance->_rx_int->disable_irq(); + instance->rxBuf.putc(instance->_pc.getc()); + instance->_rx_int->enable_irq(); + } + + if ( (instance->rxBuf.use() > 20) | (t.read_ms() > 50)) { // if more than 20 bytes received then tx the packet in RF + // set the signal for sending data + instance->_transmit_thread.signal_set(NEW_DATA); + t.stop(); + t.reset(); + } + + Thread::wait(50); + + } +} + +*/ + +// TRANSMITTING THREAD +void Radio::transmit_thread(void const *arg) +{ + Radio *instance = (Radio*)arg; + osSignalWait(START_THREAD, osWaitForever); + + while(1) { + osEvent evt = instance->_tx_data.get(); + if (evt.status == osEventMail) { + RTP_t *mail = (RTP_t*)evt.value.p; + + instance->put_pck(mail->payload, mail->size); // send the packet over air + } + } +} + + +// RECEIVING THREAD +void Radio::receive_thread(void const *arg) +{ + Radio *instance = (Radio*)arg; + osSignalWait(START_THREAD, osWaitForever); + + // receiving operations + while(1) { + osSignalWait(NEW_DATA, osWaitForever); + + // set the limit for max bytes to put in the buffer every time + uint8_t rxlength = BUFFER_SIZE; + + instance->_spi->frequency(8500000); + if (instance->get_pck(instance->buffer, &rxlength) ) { + + if (instance->buffer[0] == 0x02) { + instance->_need_ack = false; + } else { + + RTP_t *data = instance->_rx_data.alloc(); + data->payload = instance->buffer; + instance->_rx_data.put(data); + + // send the data over the serial connection +#if DEBUG_MODE > 0 + std::printf(" ==============\r\n"); + for (int i=0; i < rxlength; i++) { + std::printf(" | 0x%02X |\r\n", instance->buffer[i]); + } + std::printf(" ==============\r\n"); +#endif + + } + } else { + std::printf("Receiving packet failure\r\n"); + } + instance->_spi->frequency(5000000); + } +} \ No newline at end of file