Skip to content

Commit

Permalink
Merge manuelbl:higher-level-spi from #158
Browse files Browse the repository at this point in the history
  • Loading branch information
terrillmoore committed Nov 26, 2018
2 parents 7b64c6a + 5f86cb1 commit 194c080
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
46 changes: 25 additions & 21 deletions src/hal/hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,33 +156,37 @@ static void hal_spi_init () {
SPI.begin();
}

void hal_pin_nss (u1_t val) {
if (!val) {
uint32_t spi_freq;
static void hal_spi_trx(u1_t cmd, u1_t* buf, int len, u1_t is_read) {
uint32_t spi_freq;
u1_t nss = plmic_pins->nss;

if ((spi_freq = plmic_pins->spi_freq) == 0)
spi_freq = LMIC_SPI_FREQ;
if ((spi_freq = plmic_pins->spi_freq) == 0)
spi_freq = LMIC_SPI_FREQ;

SPISettings settings(spi_freq, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(settings);
} else {
SPI.endTransaction();
SPISettings settings(spi_freq, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(settings);
digitalWrite(nss, 0);

SPI.transfer(cmd);

for (u1_t i = 0; i < len; i++) {
u1_t* p = buf + i;
u1_t data = is_read ? 0x00 : *p;
data = SPI.transfer(data);
if (is_read)
*p = data;
}

//Serial.println(val?">>":"<<");
digitalWrite(plmic_pins->nss, val);
digitalWrite(nss, 1);
SPI.endTransaction();
}

void hal_spi_write(u1_t cmd, const u1_t* buf, int len) {
hal_spi_trx(cmd, (u1_t*)buf, len, 0);
}

// perform SPI transaction with radio
u1_t hal_spi (u1_t out) {
u1_t res = SPI.transfer(out);
/*
Serial.print(">");
Serial.print(out, HEX);
Serial.print("<");
Serial.println(res, HEX);
*/
return res;
void hal_spi_read(u1_t cmd, u1_t* buf, int len) {
hal_spi_trx(cmd, buf, len, 1);
}

// -----------------------------------------------------------------------------
Expand Down
20 changes: 11 additions & 9 deletions src/lmic/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ void hal_init (void);
*/
void hal_init_ex (const void *pContext);

/*
* drive radio NSS pin (0=low, 1=high).
*/
void hal_pin_nss (u1_t val);

/*
* drive radio RX/TX pins (0=rx, 1=tx).
*/
Expand All @@ -64,11 +59,18 @@ void hal_pin_rxtx (u1_t val);
void hal_pin_rst (u1_t val);

/*
* perform 8-bit SPI transaction with radio.
* - write given byte 'outval'
* - read byte and return value
* Perform SPI write transaction with radio chip
* - write the command byte 'cmd'
* - write 'len' bytes out of 'buf'
*/
void hal_spi_write(u1_t cmd, const u1_t* buf, int len);

/*
* Perform SPI read transaction with radio chip
* - write the command byte 'cmd'
* - read 'len' bytes into 'buf'
*/
u1_t hal_spi (u1_t outval);
void hal_spi_read(u1_t cmd, u1_t* buf, int len);

/*
* disable all CPU interrupts.
Expand Down
27 changes: 6 additions & 21 deletions src/lmic/radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,36 +299,21 @@ static u1_t randbuf[16];


static void writeReg (u1_t addr, u1_t data ) {
hal_pin_nss(0);
hal_spi(addr | 0x80);
hal_spi(data);
hal_pin_nss(1);
hal_spi_write(addr | 0x80, &data, 1);
}

static u1_t readReg (u1_t addr) {
hal_pin_nss(0);
hal_spi(addr & 0x7F);
u1_t val = hal_spi(0x00);
hal_pin_nss(1);
return val;
u1_t buf[1];
hal_spi_read(addr & 0x7f, buf, 1);
return buf[0];
}

static void writeBuf (u1_t addr, xref2u1_t buf, u1_t len) {
hal_pin_nss(0);
hal_spi(addr | 0x80);
for (u1_t i=0; i<len; i++) {
hal_spi(buf[i]);
}
hal_pin_nss(1);
hal_spi_write(addr | 0x80, buf, len);
}

static void readBuf (u1_t addr, xref2u1_t buf, u1_t len) {
hal_pin_nss(0);
hal_spi(addr & 0x7F);
for (u1_t i=0; i<len; i++) {
buf[i] = hal_spi(0x00);
}
hal_pin_nss(1);
hal_spi_read(addr & 0x7f, buf, len);
}

static void requestTcxo(bit_t state) {
Expand Down

0 comments on commit 194c080

Please sign in to comment.