May 2021 test
Dependencies: sx128x sx12xx_hal
Diff: main.cpp
- Revision:
- 4:a645b6a6fa1f
- Parent:
- 3:f81d64ff0164
- Child:
- 5:5ac152096add
--- a/main.cpp Wed Jul 18 18:50:19 2018 -0700 +++ b/main.cpp Mon Aug 19 01:30:26 2019 +0000 @@ -1,4 +1,20 @@ +#include "mbed.h" #include "radio.h" + +#include "mbed-trace/mbed_trace.h" +#define TRACE_GROUP "slav" + +static Mutex serialOutMutex; +static void serial_out_mutex_wait() +{ + serialOutMutex.lock(); +} + +static void serial_out_mutex_release() +{ + osStatus s = serialOutMutex.unlock(); + MBED_ASSERT(s == osOK); +} DigitalOut myled(LED1); @@ -13,53 +29,62 @@ #endif #ifdef TARGET_DISCO_L072CZ_LRWAN1 - DigitalOut pinA(PB_12); - DigitalOut pinB(PB_13); - DigitalOut pinC(PB_14); - DigitalOut pinD(PB_15); + DigitalOut pin0(PB_5); + DigitalOut pin1(PB_6); + DigitalOut pin2(PB_8); + DigitalOut pin3(PB_9); + DigitalOut pin4(PB_12); + DigitalOut pin5(PB_13); + DigitalOut pin6(PB_14); + DigitalOut pin7(PB_15); #else - DigitalOut pinA(PC_3); - DigitalOut pinB(PC_2); - DigitalOut pinC(PC_6); - DigitalOut pinD(PC_8); + DigitalOut pin4(PC_3); + DigitalOut pin5(PC_2); + DigitalOut pin6(PC_6); + DigitalOut pin7(PC_8); #endif /* !TARGET_DISCO_L072CZ_LRWAN1 */ -DigitalOut* pin; -Timeout to; +Timeout timeoutOutputDelay; +static uint8_t command = 0; #define PIN_ASSERT_us 500000 -#define CMD_PINA 0x02 -#define CMD_PINB 0x03 -#define CMD_PINC 0x06 -#define CMD_PIND 0x08 /**********************************************************************/ void alarm_pin_clr() { - pin->write(0); + } -void alarm_pin_set() +static bool commandWrittenFlag = false; + +void writeCommandToDigitalOuts(void) { - pin->write(1); - to.attach_us(&alarm_pin_clr, PIN_ASSERT_us); + pin0 = command & 0b00000001; + pin1 = command & 0b00000010; + pin2 = command & 0b00000100; + pin3 = command & 0b00001000; + pin4 = command & 0b00010000; + pin5 = command & 0b00100000; + pin6 = command & 0b01000000; + pin7 = command & 0b10000000; + commandWrittenFlag = true; } static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length ) { - // The CRC calculation follows CCITT - const uint16_t polynom = 0x1021; - // CRC initial value - uint16_t crc = 0x0000; + tr_debug(__FUNCTION__); + const uint16_t polynom = 0x1021; // The CRC calculation follows CCITT + uint16_t crc = 0x0000; // CRC initial value if( buffer == NULL ) { + tr_debug("NULL Buffer"); return 0; } - for( uint16_t i = 0; i < length; ++i ) + for(uint16_t i = 0; i < length; ++i) { crc ^= ( uint16_t ) buffer[i] << 8; for( uint16_t j = 0; j < 8; ++j ) @@ -67,40 +92,40 @@ crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 ); } } - return crc; } void get_alarm() { + tr_debug(__FUNCTION__); + uint16_t rx_crc, crc = crc_ccitt(Radio::radio.rx_buf, 5); rx_crc = Radio::radio.rx_buf[5]; rx_crc <<= 8; rx_crc += Radio::radio.rx_buf[6]; - //printf("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc); - if (crc == rx_crc) { - uint8_t c = Radio::radio.rx_buf[0]; - if (c == CMD_PINA || c == CMD_PINB || c == CMD_PINC || c == CMD_PIND) { - unsigned delay; - delay = Radio::radio.rx_buf[1]; - delay <<= 8; - delay += Radio::radio.rx_buf[2]; - delay <<= 8; - delay += Radio::radio.rx_buf[3]; - delay <<= 8; - delay += Radio::radio.rx_buf[4]; - switch (c) { - case CMD_PINA: pin = &pinA; break; - case CMD_PINB: pin = &pinB; break; - case CMD_PINC: pin = &pinC; break; - case CMD_PIND: pin = &pinD; break; - } - to.attach_us(&alarm_pin_set, delay); - printf("delay:%u\r\n", delay); - } else - printf("cmd? %02x\r\n", Radio::radio.rx_buf[0]); - } else - printf("crc fail %04x, %04x\r\n", rx_crc, crc); + //tr_debug("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc); + + if (crc != rx_crc) + { + tr_debug("crc fail %04x, %04x\r\n", rx_crc, crc); + return; + } + + CriticalSectionLock::enable(); + command = Radio::radio.rx_buf[0]; + CriticalSectionLock::disable(); + + unsigned delay; + delay = Radio::radio.rx_buf[1]; + delay <<= 8; + delay += Radio::radio.rx_buf[2]; + delay <<= 8; + delay += Radio::radio.rx_buf[3]; + delay <<= 8; + delay += Radio::radio.rx_buf[4]; + + timeoutOutputDelay.attach_us(writeCommandToDigitalOuts, delay); + tr_debug("output delay:%u\r\n", delay); } void txDoneCB() @@ -110,10 +135,11 @@ void rxDoneCB(uint8_t size, float Rssi, float Snr) { get_alarm(); - printf("%.1fdBm snr:%.1fdB ", Rssi, Snr); + tr_debug("%.1fdBm snr:%.1fdB ", Rssi, Snr); } -const RadioEvents_t rev = { +const RadioEvents_t rev = +{ /* Dio0_top_half */ NULL, /* TxDone_topHalf */ NULL, /* TxDone_botHalf */ txDoneCB, @@ -128,19 +154,27 @@ int main() { - printf("\r\nreset-rx\r\n"); - + mbed_trace_mutex_wait_function_set( serial_out_mutex_wait ); + mbed_trace_mutex_release_function_set( serial_out_mutex_release ); + mbed_trace_init(); + tr_debug(__FUNCTION__); Radio::Init(&rev); - Radio::Standby(); Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ Radio::SetChannel(CF_HZ); - Radio::Rx(0); - for (;;) { + while(true) + { Radio::service(); + if(commandWrittenFlag) + { + tr_debug("Command Written: %d", command); + CriticalSectionLock::enable(); + commandWrittenFlag = false; + CriticalSectionLock::disable(); + } } }