Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher level spi #158

Merged
merged 3 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions src/hal/hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,33 +141,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 @@ -43,11 +43,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 @@ -59,11 +54,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 @@ -290,36 +290,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 opmode (u1_t mode) {
Expand Down