Skip to content

Commit

Permalink
firmware: drivers: sx127x: Implementation of the register access func…
Browse files Browse the repository at this point in the history
…tions, and general fixes #60
  • Loading branch information
mgm8 committed Dec 20, 2021
1 parent 014f237 commit 76c6e2a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
61 changes: 57 additions & 4 deletions firmware/drivers/sx127x/sx127x.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,22 +619,75 @@ int sx127x_set_rx_interrupt(void)

int sx127x_read_reg(uint8_t adr, uint8_t *val)
{
return -1;
int err = -1;

uint8_t wbuf[2] = {0};
uint8_t rbuf[2] = {0};

if (sx127x_spi_transfer(wbuf, rbuf, 2) == 0)
{
*val = rbuf[1];

err = 0;
}

return err;
}

int sx127x_write_reg(uint8_t adr, uint8_t val)
{
return -1;
uint8_t wbuf[2] = {0};

wbuf[0] = adr | SX127X_SPI_WNR;
wbuf[1] = val;

return sx127x_spi_write(wbuf, 2);
}

int sx127x_burst_read(uint8_t adr, uint8_t *ptr, uint8_t len)
{
return -1;
int err = -1;

if (len <= 1)
{
err = sx127x_read_reg(adr, ptr);
}
else
{
uint8_t wbuf[32] = {0};

err = sx127x_spi_write(adr, 1);

uint8_t i = 0;
for(i = 0; i < len; i++)
{
err = sx127x_spi_transfer(wbuf, ptr, len);
}
}

return err;
}

int sx127x_burst_write(uint8_t adr, uint8_t *ptr, uint8_t len)
{
return -1;
int err = -1;

if (len <= 1)
{
err = sx127x_write_reg(adr, ptr[0]);
}
else
{
uint8_t wbuf[32] = {0};

wbuf[0] = adr | SX127X_SPI_WNR;

memcpy(&wbuf[1], ptr, len);

err = sx127x_spi_write(wbuf, 1U + len)
}

return err;
}

/** \} End of sx127x group */
20 changes: 12 additions & 8 deletions firmware/drivers/sx127x/sx127x.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define SX127X_H_

#include <stdint.h>
#include <stdbool.h>

#define SX127X_MODULE_NAME "SX127x"

Expand Down Expand Up @@ -216,6 +217,9 @@
#define SX127X_PA_SELECT_RFO 0x00U
#define SX127X_PA_SELECT_PA_BOOST 0x80U

/* The first bit in SPI address byte is a wnr bit, 1 for write access , 0 for read access */
#define SX127X_SPI_WNR 0x80U

/**
* \brief Operation modes.
*/
Expand Down Expand Up @@ -544,7 +548,7 @@ static int sx127x_burst_write(uint8_t adr, uint8_t *ptr, uint8_t len);
*
* \return The status/error code.
*/
static int sx127x_spi_init(void);
int sx127x_spi_init(void);

/**
* \brief SPI transfer routine (write and read at the same time).
Expand All @@ -557,7 +561,7 @@ static int sx127x_spi_init(void);
*
* \return The status/error code.
*/
static int sx127x_spi_transfer(uint8_t *wd, uint8_t *rd, uint16_t len);
int sx127x_spi_transfer(uint8_t *wd, uint8_t *rd, uint16_t len);

/**
* \brief Writes a byte over the SPI interface.
Expand All @@ -566,7 +570,7 @@ static int sx127x_spi_transfer(uint8_t *wd, uint8_t *rd, uint16_t len);
*
* \return The status/error code.
*/
static int sx127x_spi_write_byte(uint8_t byte);
int sx127x_spi_write_byte(uint8_t byte);

/**
* \brief Write an array of bytes over the SPI interface.
Expand All @@ -577,7 +581,7 @@ static int sx127x_spi_write_byte(uint8_t byte);
*
* \return The status/error code.
*/
static int sx127x_spi_write(uint8_t *data, uint16_t len);
int sx127x_spi_write(uint8_t *data, uint16_t len);

/**
* \brief Reads N bytes from the SPI interface.
Expand All @@ -588,14 +592,14 @@ static int sx127x_spi_write(uint8_t *data, uint16_t len);
*
* \return The status/error code.
*/
static int sx127x_spi_read(uint8_t *data, uint16_t len);
int sx127x_spi_read(uint8_t *data, uint16_t len);

/**
* \brief GPIO pins initialization.
*
* \return The status/error code.
*/
static int sx127x_gpio_init(void);
int sx127x_gpio_init(void);

/**
* \brief Write the state of the RESET pin.
Expand All @@ -604,7 +608,7 @@ static int sx127x_gpio_init(void);
*
* \return The status/error code.
*/
static int sx127X_gpio_write_reset(bool state);
int sx127X_gpio_write_reset(bool state);

/**
* \brief Reads the state of the nIRQ pin.
Expand All @@ -616,7 +620,7 @@ static int sx127X_gpio_write_reset(bool state);
* .
* \endparblock
*/
static int sx127x_gpio_read_nirq(void);
int sx127x_gpio_read_nirq(void);

/**
* \brief Milliseconds delay.
Expand Down

0 comments on commit 76c6e2a

Please sign in to comment.