diff --git a/src/hal/hal.cpp b/src/hal/hal.cpp index fba4ce10..15c7dec3 100644 --- a/src/hal/hal.cpp +++ b/src/hal/hal.cpp @@ -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); } // ----------------------------------------------------------------------------- diff --git a/src/lmic/hal.h b/src/lmic/hal.h index 51866d9a..a4256b1c 100644 --- a/src/lmic/hal.h +++ b/src/lmic/hal.h @@ -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). */ @@ -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. diff --git a/src/lmic/radio.c b/src/lmic/radio.c index 60ec6336..0b5c28c4 100644 --- a/src/lmic/radio.c +++ b/src/lmic/radio.c @@ -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