Code:
/* RASPBERRY PI One-Page LoRa TRANSMITTER
*
* AI Ra-01_V1.0 Semtech SX1278
*
* LoRa Pi
* 3.3V 3.3V Red
* GND GND Black
* SCK SCLK Yellow
* MOSI MOSI Green
* MISO MISO Orange
* NSS CE0 White
* RESET #18 Brown
* DIO0 ------1k------- #24 Grey IRQ pulled down + 1k current limiter
* Pi3.3V--button--1k--> #17 STOP button pulled DOWN + 1k current limiter
*/
#include <bcm2835.h>
#include <stdio.h>
const int reset = 18;
const int irq = 24;
const int button = 17;
void SXSetup(void)
{
char buf[2];
bcm2835_gpio_write(reset,LOW); // reset low 100us
delayMicroseconds(100);
bcm2835_gpio_write(reset,HIGH);
delay(10); // time to reset, minimum 5ms
buf[0] = 0x81; buf[1] = 0x00; // sleep mode
bcm2835_spi_transfern(buf, sizeof(buf));
buf[0] = 0x81; buf[1] = 0x88; // LoRa, low frequency mode
bcm2835_spi_transfern(buf, sizeof(buf));
buf[0] = 0x89; buf[1] = 0xF3; // PA_BOOST
bcm2835_spi_transfern(buf, sizeof(buf));
buf[0] = 0x91; buf[1] = 0xF7; // Setup for TX_Done interrupt
bcm2835_spi_transfern(buf, sizeof(buf));
}
void clear_LoRa_interrupts(void)
{
char buf[2];
buf[0] = 0x92; buf[1] = 0xFF;
bcm2835_spi_transfern(buf, sizeof(buf));
}
void send(void)
{
char buf[2];
char data = 0;
while(!bcm2835_gpio_lev(button)) // while button not pressed
{
buf[0] = 0x81; buf[1] = 0x09; // standby to set up packet
bcm2835_spi_transfern(buf, sizeof(buf));
buf[0] = 0x8D; buf[1] = 0x80; // Transmitter base address
bcm2835_spi_transfern(buf, sizeof(buf)); // Set SPI fifo pointer
buf[0] = 0x80; buf[1] = data;
bcm2835_spi_transfern(buf, sizeof(buf)); // Write packet to fifo
buf[0] = 0x81; buf[1] = 0x8B;
bcm2835_spi_transfern(buf, sizeof(buf)); // TX on, LF mode (send)
delay(100);
buf[0] = 0x12; buf[1] = 0x00;
bcm2835_spi_transfern(buf, sizeof(buf)); // Get RegIrqFlags
if (buf[1]) // If RegIrqFlags != 0 then TXDone
{
printf("Sent %d \n",data); // so byte has been sent
clear_LoRa_interrupts();
}
buf[0] = 0x81; buf[1] = 0x88;
bcm2835_spi_transfern(buf, sizeof(buf)); // TX and RX to sleep, clear fifo
delay(900); // Wait a ~sec before sending next byte
if (data<10){
data++;
}
else
{
data = 0;
}
}
}
int main(int argc, char **argv)
{
if (!bcm2835_init())
{
printf("bcm2835_init failed. Root?\n");
return 1;
}
if (!bcm2835_spi_begin())
{
printf("bcm2835_spi_begin failed. Root?\n");
return 1;
}
bcm2835_gpio_fsel(reset,BCM2835_GPIO_FSEL_OUTP); // Reset pin is output
bcm2835_gpio_write(reset,HIGH); // Reset active low
bcm2835_gpio_fsel(irq,BCM2835_GPIO_FSEL_INPT); // IRQ pin is input (from DIO0)
bcm2835_gpio_set_pud(irq,BCM2835_GPIO_PUD_DOWN); // Pulled DOWN
bcm2835_gpio_fsel(button,BCM2835_GPIO_FSEL_INPT); // Button is input
bcm2835_gpio_set_pud(button,BCM2835_GPIO_PUD_DOWN); // Pulled DOWN
SXSetup();
clear_LoRa_interrupts();
printf("Ready to send data. Press button to quit... \n");
send();
bcm2835_spi_end();
bcm2835_close();
printf("================= Clean-up DONE ================ \n");
return 0;
}
*****************************************************************************************************************************************************************************