Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:1d8d218c5cc5
- Child:
- 1:d95d135a4fb4
diff -r 000000000000 -r 1d8d218c5cc5 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Oct 05 16:11:31 2017 -0700
@@ -0,0 +1,239 @@
+#include "sx127x_lora.h"
+
+DigitalOut myled(LED1);
+
+SPI spi(D11, D12, D13); // mosi, miso, sclk
+// dio0, dio1, nss, spi, rst
+SX127x radio( D2, D3, D10, spi, A0); // sx1276 arduino shield
+
+SX127x_lora lora(radio);
+DigitalInOut rfsw(A4); // for SX1276 arduino shield
+
+InterruptIn user_button(USER_BUTTON);
+Timer t;
+Ticker ticker;
+AnalogIn a1(A1);
+DigitalOut pc6_out(PC_6);
+
+DigitalOut jumper_out(PC_10);
+InterruptIn jumper_in(PC_12);
+
+void rfsw_callback()
+{
+ if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER)
+ rfsw = 1;
+ else
+ rfsw = 0;
+}
+
+/**********************************************************************/
+#define CMD_OUT_PIN 0x01
+#define CMD_OUT_PIN_ACK (CMD_OUT_PIN | 0x80)
+
+uint8_t out_pin_state;
+
+
+static uint16_t crc16( uint8_t *buffer, uint16_t length )
+{
+ uint16_t i;
+ // The CRC calculation follows CCITT
+ const uint16_t polynom = 0x1021;
+ // CRC initial value
+ uint16_t crc = 0x0000;
+
+ if( buffer == NULL )
+ {
+ return 0;
+ }
+
+ for( i = 0; i < length; ++i )
+ {
+ uint16_t j;
+ crc ^= ( uint16_t ) buffer[i] << 8;
+ for( j = 0; j < 8; ++j )
+ {
+ crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
+ }
+ }
+
+ return crc;
+}
+
+void tx_ack()
+{
+ uint8_t n = 0;
+ uint16_t crc;
+
+ if (radio.rx_buf[0] == CMD_OUT_PIN) {
+ uint16_t ain = a1.read_u16();
+ radio.tx_buf[n++] = CMD_OUT_PIN_ACK;
+ /* TODO read analog pin and digital input pin */
+ radio.tx_buf[n++] = ain;
+ radio.tx_buf[n++] = ain >> 8;
+ } else
+ radio.tx_buf[n++] = 0xff;
+
+ crc = crc16(radio.tx_buf, n);
+ radio.tx_buf[n++] = crc;
+ radio.tx_buf[n++] = crc >> 8;
+
+ lora.RegPayloadLength = n;
+ radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
+
+ lora.start_tx(lora.RegPayloadLength); /* begin transmission */
+
+ printf("tx-ack-start\r\n");
+ while (lora.service() != SERVICE_TX_DONE) /* wait for transmission to complete */
+ ;
+
+ lora.start_rx(RF_OPMODE_RECEIVER);
+ printf("tx-ack-end\r\n");
+}
+
+void tx()
+{
+ int i;
+
+ for (i = 0; i < 3; i++) {
+ uint8_t n = 0;
+ uint16_t crc;
+ int rx_timeout_at;
+
+ radio.tx_buf[n++] = CMD_OUT_PIN;
+ radio.tx_buf[n++] = out_pin_state;
+ crc = crc16(radio.tx_buf, n);
+ radio.tx_buf[n++] = crc;
+ radio.tx_buf[n++] = crc >> 8;
+
+ lora.RegPayloadLength = n;
+ radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
+
+ lora.start_tx(lora.RegPayloadLength); /* begin transmission */
+
+ while (lora.service() != SERVICE_TX_DONE) /* wait for transmission to complete */
+ ;
+ rx_timeout_at = t.read_us() + 200000;
+
+ lora.start_rx(RF_OPMODE_RECEIVER);
+
+ while (t.read_us() < rx_timeout_at) {
+ if (lora.service() == SERVICE_READ_FIFO) {
+ uint16_t rx_crc;
+ rx_crc = radio.rx_buf[lora.RegRxNbBytes-2];
+ rx_crc |= radio.rx_buf[lora.RegRxNbBytes-1] << 8;
+ crc = crc16(radio.rx_buf, lora.RegRxNbBytes-2);
+ if (crc == rx_crc) {
+ printf("crcOk %u\r\n", i);
+ if (radio.rx_buf[0] == CMD_OUT_PIN_ACK) {
+ uint16_t ain = radio.rx_buf[0];
+ ain |= radio.rx_buf[1] << 8;
+ printf("ain %u\r\n", ain);
+ }
+ return;
+ } else
+ printf("crcFail %04x != %04x\r\n", crc, rx_crc);
+ }
+ }
+ printf("rx-timeout %u\r\n", i);
+ } // ..for()
+}
+
+volatile bool start_tx;
+
+void button_isr()
+{
+ if (!jumper_in.read())
+ start_tx = true;
+}
+
+void auto_tx()
+{
+ if (jumper_in.read())
+ start_tx = true;
+ else
+ ticker.detach();
+}
+
+int main()
+{
+ bool jin = false;
+ printf("\r\nreset\r\n");
+
+ jumper_out = 1;
+ jumper_in.mode(PullDown);
+
+ while (!user_button) {
+ printf("button-lo\r\n");
+ wait(0.01);
+ }
+ user_button.fall(&button_isr);
+ t.start();
+
+ radio.rf_switch = rfsw_callback;
+
+ radio.set_frf_MHz(917.3);
+ lora.enable();
+ lora.setBw_KHz(500);
+ lora.setSf(9);
+
+ /* RFO or PABOOST choice:
+ * SX1276 shield: RFO if using 900MHz, or PA_BOOST if using 433MHz
+ */
+ rfsw.input();
+ if (rfsw.read()) {
+ printf("LAS\r\n");
+ /* LAS HF=PA_BOOST LF=RFO */
+ if (radio.HF)
+ radio.RegPaConfig.bits.PaSelect = 1;
+ else
+ radio.RegPaConfig.bits.PaSelect = 0;
+ } else {
+ /* MAS shield board, only RFO TX */
+ radio.RegPaConfig.bits.PaSelect = 0;
+ printf("MAS\r\n");
+ }
+ rfsw.output();
+ radio.write_reg(REG_PACONFIG, radio.RegPaConfig.octet);
+
+ lora.start_rx(RF_OPMODE_RECEIVER);
+
+ for (;;) {
+
+ if (jumper_in.read()) {
+ if (!jin) {
+ ticker.attach(auto_tx, 0.5);
+ jin = true;
+ }
+ } else {
+ jin = false;
+ }
+
+ if (start_tx) {
+ out_pin_state ^= 1;
+ tx();
+ start_tx = false;
+ }
+ if (lora.service() == SERVICE_READ_FIFO) {
+ uint16_t crc, rx_crc;
+ int i;
+ for (i = 0; i < lora.RegRxNbBytes; i++) {
+ printf("%02x ", radio.rx_buf[i]);
+ }
+ printf("\r\n");
+ rx_crc = radio.rx_buf[lora.RegRxNbBytes-2];
+ rx_crc |= radio.rx_buf[lora.RegRxNbBytes-1] << 8;
+ crc = crc16(radio.rx_buf, lora.RegRxNbBytes-2);
+ if (crc == rx_crc) {
+ printf("crcOk\r\n");
+ if (radio.rx_buf[0] == CMD_OUT_PIN) {
+ pc6_out.write(radio.rx_buf[1]);
+ printf("out pin state: %u", radio.rx_buf[1]);
+ }
+ printf("\r\n");
+ tx_ack();
+ } else
+ printf("crc %04x != %04x\r\n", crc, rx_crc);
+ } // ..if something received
+ } // ..for (;;)
+}
+