OPL-Receiver.C

kfjl

Member
View attachment 21805

Code:
/* RASPBERRY PI LoRa RECEIVER
*
*  Uses a push button to leave the endless listening loop so the program will continue and clean up.
*  Also uses current-limiting resistors to protect your Pi from you :)
*
*  AI Ra-01_V1.0 Semtech SX1278  NOT BREADBOARD FRIENDLY !!!
*
*  LoRa            Pi
*  3.3V             3.3V
*  GND            GND
*  SCK            SCLK
*  MOSI            MOSI
*  MISO            MISO
*  NSS            CE0
*  RESET        #18
*  DIO0    -----1kohm----->#24      IRQ pulled DOWN 1k current limiter
*  Pi3.3V--1kohm--button-> #17   STOP button pulled DOWN 1k current limiter
*/

const int reset = 18;
const int irq = 24;
const int button = 17;

#include <bcm2835.h>
#include <stdio.h>

void SXSetup(void)       
{
    char buf[2];
        
    bcm2835_gpio_write(reset,LOW);              // reset low  100us
    bcm2835_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] = 0x91; buf[1] = 0xBF;               // Setup for RX_Done interrupt
    bcm2835_spi_transfern(buf, sizeof(buf));
        
    buf[0] = 0x81; buf[1] = 0x8D;               // RXContinuous, LF, LoRa mode (listen)
    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 listen(void)
{
    char buf[2];
    
    while(!bcm2835_gpio_lev(button))                            // while button not pressed
    {
        if (bcm2835_gpio_lev(irq))
        {
            buf[0] = 0x10; buf[1] = 0x00;               // get fifo RX pointer
            bcm2835_spi_transfern(buf, sizeof(buf));
            
            buf[0] = 0x8D;                              // write rx pointer to spi pointer
            bcm2835_spi_transfern(buf, sizeof(buf));
            
            buf[0] = 0x00; buf[1] = 0X00;
            bcm2835_spi_transfern(buf, sizeof(buf));    // Read data
            
            clear_LoRa_interrupts();
            
            printf("Received %d \n",buf[1]);
        }
    }         
}
    
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("Waiting for data, press button to quit... \n");
        
    listen();
        
    bcm2835_spi_end();
    bcm2835_close();
    printf("================= Clean-up DONE ================ \n");
    return 0;
}


Avec un bouton-poussoir pour arrêter proprement, sans Ctrl-C, et des résistances limiteurs de courant pour protèger le Pi.

A suivre...
 
Top