point-2-point demo
radio chip selection
Radio chip driver is not included, because these options are available.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.
TX trigger selection
Edit main.h
to define DIGITAL_TRIGGER
or ANALOG_TRIGGER
to chose whether transmit is initiated by digital pin (button/jumper) or analog pin(s) level change.
This project is intended to be used on two LoRa shields.
Each board sits in continuous RX mode, waiting for request packet.
If the received packet has good CRC, the packet is acknowledged along with read of ADC sample from the replying device.
The original request packet also contains instruction to set level of output pin.
Both sides of the link are running the same code, and each can initiate a transmission at any time.
No addressing is used, so only two nodes can operate on the radio channel.
Revision 5:e35b1b281466, committed 2018-08-01
- Comitter:
- Wayne Roberts
- Date:
- Wed Aug 01 15:04:11 2018 -0700
- Parent:
- 4:b5dd459ac390
- Commit message:
- move analog to separate source file
Changed in this revision
--- a/main.cpp Tue Jul 17 14:32:29 2018 -0700 +++ b/main.cpp Wed Aug 01 15:04:11 2018 -0700 @@ -1,7 +1,5 @@ +#include "main.h" #include "radio.h" -#include "uart_cmds.h" - -//#define ANALOG_TX_TRIGGER #if defined(SX127x_H) || defined(SX126x_H) #define BW_KHZ 500 @@ -23,24 +21,13 @@ DigitalOut myled(LED1); - Timer t; -Ticker ticker; volatile bool tx_done; -InterruptIn user_button(USER_BUTTON); DigitalOut pc6_out(PC_6); -#ifndef ANALOG_TX_TRIGGER - #define AIN_REST_THRESHOLD 96 // 12bit left justified - DigitalOut jumper_out(PC_10); - InterruptIn jumper_in(PC_12); - volatile bool start_tx; -#endif /* !ANALOG_TX_TRIGGER */ - #define RX_TIMEOUT_US 200000 /**********************************************************************/ -uint8_t out_pin_state; void txDoneCB() { @@ -173,23 +160,6 @@ } // ..for() } -#ifndef ANALOG_TX_TRIGGER -void button_isr() -{ - if (!jumper_in.read()) - start_tx = true; -} - -void auto_tx() -{ - if (jumper_in.read()) - start_tx = true; - else - ticker.detach(); -} -#endif /* !ANALOG_TX_TRIGGER */ - - const RadioEvents_t rev = { /* Dio0_top_half */ NULL, /* TxDone_topHalf */ NULL, @@ -204,23 +174,10 @@ int main() { -#ifdef ANALOG_TX_TRIGGER - uint16_t prev_ain; - int8_t ain_movement; - bool ain_sent; -#else - bool jin = false; + printf("\r\nreset\r\n"); - jumper_out = 1; - jumper_in.mode(PullDown); + trigger_init(); - while (!user_button) { - printf("button-lo\r\n"); - wait(0.01); - } - user_button.fall(&button_isr); -#endif /* !ANALOG_TX_TRIGGER */ - printf("\r\n2reset\r\n"); t.start(); Radio::Init(&rev); @@ -232,67 +189,9 @@ Radio::Rx(0); -#ifdef ANALOG_TX_TRIGGER - prev_ain = a1.read_u16(); - ain_movement = 0; - ain_sent = false; -#endif /* ANALOG_TX_TRIGGER */ for (;;) { -#ifdef ANALOG_TX_TRIGGER - uint16_t ain = a1.read_u16(); - uint16_t diff = abs(ain-prev_ain); - if (diff > AIN_REST_THRESHOLD) { - ain_sent = false; - if (ain_movement < 1) - ain_movement = 1; - else { - if (++ain_movement > 16) - ain_movement = 16; - } - } else { - /* steady state */ - if (ain_movement > 0) - ain_movement = 0; - else { - if (--ain_movement < -16) { - ain_movement = -16; - if (!ain_sent) { - uint8_t buf[4]; - printf("## %02x ##\r\n", ain >> 8); - buf[0] = CMD_PWM; - buf[1] = 120; // Hz - buf[2] = ain >> 8; // duty - radio_tx(buf, 3); - ain_sent = true; - } - } - } - } - //printf("%05u diff:%04u move:%d\r\n", ain, diff, ain_movement); - prev_ain = ain; - wait_us(5000); + trigger_mainloop(); -#else - - if (jumper_in.read()) { - if (!jin) { - ticker.attach(auto_tx, 0.5); - jin = true; - } - } else { - jin = false; - } - - if (start_tx) { - start_tx = false; - - uint8_t buf[2]; - out_pin_state ^= 1; - buf[0] = CMD_OUT_PIN; - buf[1] = out_pin_state; - radio_tx(buf, 2); - } -#endif /* !ANALOG_TX_TRIGGER */ if (rx.length > 0) { uint16_t crc, rx_crc; int i;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Wed Aug 01 15:04:11 2018 -0700 @@ -0,0 +1,31 @@ +#include "mbed.h" + +#define DIGITAL_TRIGGER +//#define ANALOG_TRIGGER + +void trigger_init(void); +void trigger_mainloop(void); + +/* from main.cpp: */ +void radio_tx(uint8_t*, uint8_t); + +/* from uart_cmds.cpp: */ +void uart_service(void); +bool parse_radio_rx(uint8_t*); + +#define ACK_BIT 0x80 + +#define CMD_OUT_PIN 0x01 +#define CMD_OUT_PIN_ACK (CMD_OUT_PIN | ACK_BIT) + +#define CMD_PWM_A 0x02 +#define CMD_PWM_A_ACK (CMD_PWM_A | ACK_BIT) + +#define CMD_PWM_B 0x03 +#define CMD_PWM_B_ACK (CMD_PWM_B | ACK_BIT) + +#define CMD_PWM_C 0x04 +#define CMD_PWM_C_ACK (CMD_PWM_C | ACK_BIT) + +#define CMD_PWM_D 0x05 +#define CMD_PWM_D_ACK (CMD_PWM_D | ACK_BIT)
--- a/mbed-os.lib Tue Jul 17 14:32:29 2018 -0700 +++ b/mbed-os.lib Wed Aug 01 15:04:11 2018 -0700 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#c29fe896a1b5157fa33aa0c777ba92767f6a61f3 +https://github.com/ARMmbed/mbed-os/#485bdeee150e2bc8ed75e27d936060fb63a7a7d1
--- a/sx12xx_hal.lib Tue Jul 17 14:32:29 2018 -0700 +++ b/sx12xx_hal.lib Wed Aug 01 15:04:11 2018 -0700 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/dudmuck/code/sx12xx_hal/#e79b0a55135f +https://os.mbed.com/users/dudmuck/code/sx12xx_hal/#122af639cf0b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trigger_analog.cpp Wed Aug 01 15:04:11 2018 -0700 @@ -0,0 +1,88 @@ +#include "main.h" +#ifdef ANALOG_TRIGGER + +#define NUM_ANALOG_IN 4 + +#if defined(TARGET_FF_MORPHO) && defined(TARGET_FAMILY_STM32) +PinName pin_names[NUM_ANALOG_IN] = { + PC_2, /* CN7-35 */ + PC_3, /* CN7-37 */ + PC_4, /* CN10-34 */ + PC_5 /* CN10-6 */ +}; +#endif + +const uint8_t rfCmds[NUM_ANALOG_IN] = { + CMD_PWM_A, + CMD_PWM_B, + CMD_PWM_C, + CMD_PWM_D +}; + +typedef struct { + AnalogIn* ain; + uint16_t prev; + int8_t movement; + bool sent; +} analog_t; + +analog_t _a_[NUM_ANALOG_IN]; + +void trigger_init() +{ + unsigned n; + for (n = 0; n < NUM_ANALOG_IN; n++) { + _a_[n].ain = new AnalogIn(pin_names[n]); + _a_[n].prev = _a_[n].ain->read_u16(); + _a_[n].movement = 0; + _a_[n].sent = false; + } +} + +#define AIN_REST_THRESHOLD 96 // 12bit left justified + +void analog_mainloop(analog_t* ana, uint8_t rfCmd) +{ + uint16_t ain = ana->ain->read_u16(); + uint16_t diff = abs(ain-ana->prev); + if (diff > AIN_REST_THRESHOLD) { + ana->sent = false; + if (ana->movement < 1) + ana->movement = 1; + else { + if (++ana->movement > 16) + ana->movement = 16; + } + } else { + /* steady state */ + if (ana->movement > 0) + ana->movement = 0; + else { + if (--ana->movement < -16) { + ana->movement = -16; + if (!ana->sent) { + uint8_t buf[4]; + printf("## %02x ##\r\n", ain >> 8); + buf[0] = rfCmd; + buf[1] = 120; // Hz + buf[2] = ain >> 8; // duty + radio_tx(buf, 3); + ana->sent = true; + } + } + } + } + //printf("%05u diff:%04u move:%d\r\n", ain, diff, ain_movement); + ana->prev = ain; +} + +void trigger_mainloop() +{ + unsigned n; + for (n = 0; n < NUM_ANALOG_IN; n++) { + analog_mainloop(&_a_[n], rfCmds[n]); + } + wait_us(5000); +} + +#endif /* ANALOG_TRIGGER */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trigger_digital.cpp Wed Aug 01 15:04:11 2018 -0700 @@ -0,0 +1,65 @@ +#include "main.h" +#ifdef DIGITAL_TRIGGER + +Ticker ticker; +InterruptIn user_button(USER_BUTTON); + +DigitalOut jumper_out(PC_10); +InterruptIn jumper_in(PC_12); +volatile bool start_tx; + +volatile bool jin; +uint8_t out_pin_state; + +void button_isr() +{ + if (!jumper_in.read()) + start_tx = true; +} + +void auto_tx() +{ + if (jumper_in.read()) + start_tx = true; + else + ticker.detach(); +} + +void trigger_init() +{ + jin = false; + + jumper_out = 1; + jumper_in.mode(PullDown); + + while (!user_button) { + printf("button-lo\r\n"); + wait(0.01); + } + user_button.fall(&button_isr); +} + +void trigger_mainloop() +{ + if (jumper_in.read()) { + if (!jin) { + ticker.attach(auto_tx, 0.5); + jin = true; + } + } else { + jin = false; + } + + if (start_tx) { + start_tx = false; + + uint8_t buf[2]; + out_pin_state ^= 1; + buf[0] = CMD_OUT_PIN; + buf[1] = out_pin_state; + radio_tx(buf, 2); + } +} + +#endif /* DIGITAL_TRIGGER */ +
--- a/uart_cmds.cpp Tue Jul 17 14:32:29 2018 -0700 +++ b/uart_cmds.cpp Wed Aug 01 15:04:11 2018 -0700 @@ -1,7 +1,10 @@ #include <mbed.h> -#include "uart_cmds.h" +#include "main.h" -PwmOut pwm(PB_11); +PwmOut pwmA(PB_11); /* CN10-18 */ +PwmOut pwmB(PA_15); /* CN7-17 */ +PwmOut pwmC(PB_1); /* CN10-24 */ +PwmOut pwmD(PC_6); /* CN10-4 */ RawSerial pc(USBTX, USBRX); char pcbuf[128]; @@ -12,27 +15,38 @@ /* return true for parsed */ bool parse_radio_rx(uint8_t* rx_buf) { - if (rx_buf[0] == CMD_PWM) { - pwm.period(1.0 / rx_buf[1]); // rx_buf[1] is Hz - pwm.write(rx_buf[2] / 255.0); - printf("%uHz, duty:%.2f\r\n", rx_buf[1], rx_buf[2]/255.0); - return true; + PwmOut* pwmPtr; + + switch (rx_buf[0]) { + case CMD_PWM_A: pwmPtr = &pwmA; break; + case CMD_PWM_B: pwmPtr = &pwmB; break; + case CMD_PWM_C: pwmPtr = &pwmC; break; + case CMD_PWM_D: pwmPtr = &pwmD; break; + default: return false; } - return false; + pwmPtr->period(1.0 / rx_buf[1]); // rx_buf[1] is Hz + pwmPtr->write(rx_buf[2] / 255.0); + printf("%uHz, duty:%.2f\r\n", rx_buf[1], rx_buf[2]/255.0); + return true; } void cmd_pwm(uint8_t idx) { uint8_t buf[4]; - unsigned p, d; + unsigned ch, p, d; - if (sscanf(pcbuf+idx, "%u %u", &p, &d) != 2) { + if (sscanf(pcbuf+idx, "%u %u %u", &ch, &p, &d) != 3) { printf("parse fail\r\n"); return; } - buf[0] = CMD_PWM; + switch (ch) { + case 0: buf[0] = CMD_PWM_A; break; + case 1: buf[0] = CMD_PWM_B; break; + case 2: buf[0] = CMD_PWM_C; break; + case 3: buf[0] = CMD_PWM_D; break; + } buf[1] = p; buf[2] = d; radio_tx(buf, 3);
--- a/uart_cmds.h Tue Jul 17 14:32:29 2018 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ - -#define ACK_BIT 0x80 - -#define CMD_OUT_PIN 0x01 -#define CMD_OUT_PIN_ACK (CMD_OUT_PIN | ACK_BIT) - -#define CMD_PWM 0x02 -#define CMD_PWM_ACK (CMD_PWM | ACK_BIT) - -void uart_service(void); -bool parse_radio_rx(uint8_t*); - -/* from main.cpp: */ -void radio_tx(uint8_t*, uint8_t);