May 2021 test

Dependencies:   sx128x sx12xx_hal

Committer:
grit
Date:
Mon Aug 19 01:30:26 2019 +0000
Revision:
4:a645b6a6fa1f
Parent:
3:f81d64ff0164
Child:
5:5ac152096add
initial;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grit 4:a645b6a6fa1f 1 #include "mbed.h"
Wayne Roberts 3:f81d64ff0164 2 #include "radio.h"
grit 4:a645b6a6fa1f 3
grit 4:a645b6a6fa1f 4 #include "mbed-trace/mbed_trace.h"
grit 4:a645b6a6fa1f 5 #define TRACE_GROUP "slav"
grit 4:a645b6a6fa1f 6
grit 4:a645b6a6fa1f 7 static Mutex serialOutMutex;
grit 4:a645b6a6fa1f 8 static void serial_out_mutex_wait()
grit 4:a645b6a6fa1f 9 {
grit 4:a645b6a6fa1f 10 serialOutMutex.lock();
grit 4:a645b6a6fa1f 11 }
grit 4:a645b6a6fa1f 12
grit 4:a645b6a6fa1f 13 static void serial_out_mutex_release()
grit 4:a645b6a6fa1f 14 {
grit 4:a645b6a6fa1f 15 osStatus s = serialOutMutex.unlock();
grit 4:a645b6a6fa1f 16 MBED_ASSERT(s == osOK);
grit 4:a645b6a6fa1f 17 }
dudmuck 0:b6ec8db2edbf 18
dudmuck 0:b6ec8db2edbf 19 DigitalOut myled(LED1);
dudmuck 2:bf201940a9db 20
Wayne Roberts 3:f81d64ff0164 21 #if defined(SX127x_H) || defined(SX126x_H)
Wayne Roberts 3:f81d64ff0164 22 #define BW_KHZ 500
Wayne Roberts 3:f81d64ff0164 23 #define SPREADING_FACTOR 11
Wayne Roberts 3:f81d64ff0164 24 #define CF_HZ 910800000
Wayne Roberts 3:f81d64ff0164 25 #elif defined(SX128x_H)
Wayne Roberts 3:f81d64ff0164 26 #define BW_KHZ 200
Wayne Roberts 3:f81d64ff0164 27 #define SPREADING_FACTOR 11
Wayne Roberts 3:f81d64ff0164 28 #define CF_HZ 2487000000
Wayne Roberts 3:f81d64ff0164 29 #endif
Wayne Roberts 3:f81d64ff0164 30
Wayne Roberts 3:f81d64ff0164 31 #ifdef TARGET_DISCO_L072CZ_LRWAN1
grit 4:a645b6a6fa1f 32 DigitalOut pin0(PB_5);
grit 4:a645b6a6fa1f 33 DigitalOut pin1(PB_6);
grit 4:a645b6a6fa1f 34 DigitalOut pin2(PB_8);
grit 4:a645b6a6fa1f 35 DigitalOut pin3(PB_9);
grit 4:a645b6a6fa1f 36 DigitalOut pin4(PB_12);
grit 4:a645b6a6fa1f 37 DigitalOut pin5(PB_13);
grit 4:a645b6a6fa1f 38 DigitalOut pin6(PB_14);
grit 4:a645b6a6fa1f 39 DigitalOut pin7(PB_15);
dudmuck 2:bf201940a9db 40 #else
grit 4:a645b6a6fa1f 41 DigitalOut pin4(PC_3);
grit 4:a645b6a6fa1f 42 DigitalOut pin5(PC_2);
grit 4:a645b6a6fa1f 43 DigitalOut pin6(PC_6);
grit 4:a645b6a6fa1f 44 DigitalOut pin7(PC_8);
dudmuck 2:bf201940a9db 45 #endif /* !TARGET_DISCO_L072CZ_LRWAN1 */
dudmuck 2:bf201940a9db 46
grit 4:a645b6a6fa1f 47 Timeout timeoutOutputDelay;
grit 4:a645b6a6fa1f 48 static uint8_t command = 0;
dudmuck 0:b6ec8db2edbf 49
dudmuck 1:6a3a48d657a9 50 #define PIN_ASSERT_us 500000
dudmuck 1:6a3a48d657a9 51
dudmuck 0:b6ec8db2edbf 52
Wayne Roberts 3:f81d64ff0164 53 /**********************************************************************/
Wayne Roberts 3:f81d64ff0164 54
dudmuck 0:b6ec8db2edbf 55 void alarm_pin_clr()
dudmuck 0:b6ec8db2edbf 56 {
grit 4:a645b6a6fa1f 57
dudmuck 0:b6ec8db2edbf 58 }
dudmuck 0:b6ec8db2edbf 59
grit 4:a645b6a6fa1f 60 static bool commandWrittenFlag = false;
grit 4:a645b6a6fa1f 61
grit 4:a645b6a6fa1f 62 void writeCommandToDigitalOuts(void)
dudmuck 0:b6ec8db2edbf 63 {
grit 4:a645b6a6fa1f 64 pin0 = command & 0b00000001;
grit 4:a645b6a6fa1f 65 pin1 = command & 0b00000010;
grit 4:a645b6a6fa1f 66 pin2 = command & 0b00000100;
grit 4:a645b6a6fa1f 67 pin3 = command & 0b00001000;
grit 4:a645b6a6fa1f 68 pin4 = command & 0b00010000;
grit 4:a645b6a6fa1f 69 pin5 = command & 0b00100000;
grit 4:a645b6a6fa1f 70 pin6 = command & 0b01000000;
grit 4:a645b6a6fa1f 71 pin7 = command & 0b10000000;
grit 4:a645b6a6fa1f 72 commandWrittenFlag = true;
dudmuck 0:b6ec8db2edbf 73 }
dudmuck 0:b6ec8db2edbf 74
dudmuck 0:b6ec8db2edbf 75 static uint16_t crc_ccitt( uint8_t *buffer, uint16_t length )
dudmuck 0:b6ec8db2edbf 76 {
grit 4:a645b6a6fa1f 77 tr_debug(__FUNCTION__);
grit 4:a645b6a6fa1f 78 const uint16_t polynom = 0x1021; // The CRC calculation follows CCITT
grit 4:a645b6a6fa1f 79 uint16_t crc = 0x0000; // CRC initial value
dudmuck 0:b6ec8db2edbf 80
dudmuck 0:b6ec8db2edbf 81 if( buffer == NULL )
dudmuck 0:b6ec8db2edbf 82 {
grit 4:a645b6a6fa1f 83 tr_debug("NULL Buffer");
dudmuck 0:b6ec8db2edbf 84 return 0;
dudmuck 0:b6ec8db2edbf 85 }
dudmuck 0:b6ec8db2edbf 86
grit 4:a645b6a6fa1f 87 for(uint16_t i = 0; i < length; ++i)
dudmuck 0:b6ec8db2edbf 88 {
dudmuck 0:b6ec8db2edbf 89 crc ^= ( uint16_t ) buffer[i] << 8;
dudmuck 0:b6ec8db2edbf 90 for( uint16_t j = 0; j < 8; ++j )
dudmuck 0:b6ec8db2edbf 91 {
dudmuck 0:b6ec8db2edbf 92 crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
dudmuck 0:b6ec8db2edbf 93 }
dudmuck 0:b6ec8db2edbf 94 }
dudmuck 0:b6ec8db2edbf 95 return crc;
dudmuck 0:b6ec8db2edbf 96 }
dudmuck 0:b6ec8db2edbf 97
dudmuck 0:b6ec8db2edbf 98 void get_alarm()
dudmuck 0:b6ec8db2edbf 99 {
grit 4:a645b6a6fa1f 100 tr_debug(__FUNCTION__);
grit 4:a645b6a6fa1f 101
Wayne Roberts 3:f81d64ff0164 102 uint16_t rx_crc, crc = crc_ccitt(Radio::radio.rx_buf, 5);
Wayne Roberts 3:f81d64ff0164 103 rx_crc = Radio::radio.rx_buf[5];
dudmuck 0:b6ec8db2edbf 104 rx_crc <<= 8;
Wayne Roberts 3:f81d64ff0164 105 rx_crc += Radio::radio.rx_buf[6];
grit 4:a645b6a6fa1f 106 //tr_debug("%u) crc rx:%04x, calc:%04x\r\n", lora.RegRxNbBytes, rx_crc, crc);
grit 4:a645b6a6fa1f 107
grit 4:a645b6a6fa1f 108 if (crc != rx_crc)
grit 4:a645b6a6fa1f 109 {
grit 4:a645b6a6fa1f 110 tr_debug("crc fail %04x, %04x\r\n", rx_crc, crc);
grit 4:a645b6a6fa1f 111 return;
grit 4:a645b6a6fa1f 112 }
grit 4:a645b6a6fa1f 113
grit 4:a645b6a6fa1f 114 CriticalSectionLock::enable();
grit 4:a645b6a6fa1f 115 command = Radio::radio.rx_buf[0];
grit 4:a645b6a6fa1f 116 CriticalSectionLock::disable();
grit 4:a645b6a6fa1f 117
grit 4:a645b6a6fa1f 118 unsigned delay;
grit 4:a645b6a6fa1f 119 delay = Radio::radio.rx_buf[1];
grit 4:a645b6a6fa1f 120 delay <<= 8;
grit 4:a645b6a6fa1f 121 delay += Radio::radio.rx_buf[2];
grit 4:a645b6a6fa1f 122 delay <<= 8;
grit 4:a645b6a6fa1f 123 delay += Radio::radio.rx_buf[3];
grit 4:a645b6a6fa1f 124 delay <<= 8;
grit 4:a645b6a6fa1f 125 delay += Radio::radio.rx_buf[4];
grit 4:a645b6a6fa1f 126
grit 4:a645b6a6fa1f 127 timeoutOutputDelay.attach_us(writeCommandToDigitalOuts, delay);
grit 4:a645b6a6fa1f 128 tr_debug("output delay:%u\r\n", delay);
dudmuck 0:b6ec8db2edbf 129 }
Wayne Roberts 3:f81d64ff0164 130
Wayne Roberts 3:f81d64ff0164 131 void txDoneCB()
Wayne Roberts 3:f81d64ff0164 132 {
Wayne Roberts 3:f81d64ff0164 133 }
Wayne Roberts 3:f81d64ff0164 134
Wayne Roberts 3:f81d64ff0164 135 void rxDoneCB(uint8_t size, float Rssi, float Snr)
Wayne Roberts 3:f81d64ff0164 136 {
Wayne Roberts 3:f81d64ff0164 137 get_alarm();
grit 4:a645b6a6fa1f 138 tr_debug("%.1fdBm snr:%.1fdB ", Rssi, Snr);
Wayne Roberts 3:f81d64ff0164 139 }
Wayne Roberts 3:f81d64ff0164 140
grit 4:a645b6a6fa1f 141 const RadioEvents_t rev =
grit 4:a645b6a6fa1f 142 {
Wayne Roberts 3:f81d64ff0164 143 /* Dio0_top_half */ NULL,
Wayne Roberts 3:f81d64ff0164 144 /* TxDone_topHalf */ NULL,
Wayne Roberts 3:f81d64ff0164 145 /* TxDone_botHalf */ txDoneCB,
Wayne Roberts 3:f81d64ff0164 146 /* TxTimeout */ NULL,
Wayne Roberts 3:f81d64ff0164 147 /* RxDone */ rxDoneCB,
Wayne Roberts 3:f81d64ff0164 148 /* RxTimeout */ NULL,
Wayne Roberts 3:f81d64ff0164 149 /* RxError */ NULL,
Wayne Roberts 3:f81d64ff0164 150 /* FhssChangeChannel */NULL,
Wayne Roberts 3:f81d64ff0164 151 /* CadDone */ NULL
Wayne Roberts 3:f81d64ff0164 152 };
Wayne Roberts 3:f81d64ff0164 153
dudmuck 0:b6ec8db2edbf 154
dudmuck 0:b6ec8db2edbf 155 int main()
dudmuck 0:b6ec8db2edbf 156 {
grit 4:a645b6a6fa1f 157 mbed_trace_mutex_wait_function_set( serial_out_mutex_wait );
grit 4:a645b6a6fa1f 158 mbed_trace_mutex_release_function_set( serial_out_mutex_release );
grit 4:a645b6a6fa1f 159 mbed_trace_init();
grit 4:a645b6a6fa1f 160 tr_debug(__FUNCTION__);
Wayne Roberts 3:f81d64ff0164 161
Wayne Roberts 3:f81d64ff0164 162 Radio::Init(&rev);
Wayne Roberts 3:f81d64ff0164 163 Radio::Standby();
Wayne Roberts 3:f81d64ff0164 164 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
Wayne Roberts 3:f81d64ff0164 165 Radio::LoRaPacketConfig(8, false, true, false); // preambleLen, fixLen, crcOn, invIQ
Wayne Roberts 3:f81d64ff0164 166 Radio::SetChannel(CF_HZ);
Wayne Roberts 3:f81d64ff0164 167 Radio::Rx(0);
dudmuck 0:b6ec8db2edbf 168
grit 4:a645b6a6fa1f 169 while(true)
grit 4:a645b6a6fa1f 170 {
Wayne Roberts 3:f81d64ff0164 171 Radio::service();
grit 4:a645b6a6fa1f 172 if(commandWrittenFlag)
grit 4:a645b6a6fa1f 173 {
grit 4:a645b6a6fa1f 174 tr_debug("Command Written: %d", command);
grit 4:a645b6a6fa1f 175 CriticalSectionLock::enable();
grit 4:a645b6a6fa1f 176 commandWrittenFlag = false;
grit 4:a645b6a6fa1f 177 CriticalSectionLock::disable();
grit 4:a645b6a6fa1f 178 }
dudmuck 0:b6ec8db2edbf 179 }
dudmuck 0:b6ec8db2edbf 180 }