From 514e81c3c1a750cb633ce73c253835bb785da04e Mon Sep 17 00:00:00 2001 From: Wolfgang Klenk Date: Wed, 19 Apr 2017 08:21:21 +0200 Subject: [PATCH] Now transferring buffers with SPI instead of single-byte operations. --- lmic/hal.h | 16 ++++++++++++- lmic/radio.c | 16 ++++--------- lora_gps_hat/hal.c | 57 +++++++++++++++++++++++++++------------------- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/lmic/hal.h b/lmic/hal.h index 12cca92..6c3183f 100644 --- a/lmic/hal.h +++ b/lmic/hal.h @@ -53,7 +53,21 @@ void hal_pin_rst (u1_t val); * - write given byte 'outval' * - read byte and return value */ -u1_t hal_spi (u1_t outval); +// u1_t hal_spi (u1_t outval); + +/* + * 2017-04-19 Wolfgang Klenk + * Perform Read/Write operations with 8-bit address and 8-bit data + */ +u1_t hal_spi_single(u1_t address, u1_t outval); + +/* + * 2017-04-19 Wolfgang Klenk + * Perform Read/Write operations with 8-bit address and buffer + */ +void hal_spi_buffer(u1_t address, u1_t *buffer, int len); + + /* * disable all CPU interrupts. diff --git a/lmic/radio.c b/lmic/radio.c index 1ed0cf1..b551bf4 100644 --- a/lmic/radio.c +++ b/lmic/radio.c @@ -278,34 +278,26 @@ 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_spi_single(addr | 0x80, data); hal_pin_nss(1); } static u1_t readReg (u1_t addr) { hal_pin_nss(0); - hal_spi(addr & 0x7F); - u1_t val = hal_spi(0x00); + u1_t val = hal_spi_single(addr & 0x7F, 0x00); hal_pin_nss(1); return val; } 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 0 ? fprintf(stdout, "%09d HAL: Sending ...\n", osticks2ms(hal_ticks()), val) : fprintf(stdout, "%09d HAL: Receiving ...\n", osticks2ms(hal_ticks()), val); + val > 0 ? fprintf(stdout, "%09d HAL: Sending ...\n", osticks2ms(hal_ticks())) : fprintf(stdout, "%09d HAL: Receiving ...\n", osticks2ms(hal_ticks())); #endif // Nothing to do. There is no such pin in the Lora/GPS HAT module. @@ -167,13 +166,6 @@ void hal_pin_rst (u1_t val) { // perform 8-bit SPI transaction. // write given byte outval to radio, read byte from radio and return value. u1_t hal_spi (u1_t out) { - static u1_t isAddress = 0x01; - static u1_t address = 0x00; - u1_t value = out; - - if (isAddress) { - address = out; - } u1_t rc = wiringPiSPIDataRW(0, &out, 1); if (rc < 0) { @@ -181,22 +173,41 @@ u1_t hal_spi (u1_t out) { hal_failed(); } -/* - if (!isAddress && (address != 0x2c)) { - if (address & 0x80) { - fprintf(stdout, "%09d HAL: SPI write to address 0x%02x value 0x%02x\n", osticks2ms(hal_ticks()), address & 0x7F, value); - fprintf(stdout, " writeReg(0x%02x, 0x%02x);\n", address & 0x7F, value); - } else { - fprintf(stdout, "%09d HAL: SPI read from address 0x%02x value 0x%02x\n", osticks2ms(hal_ticks()), address, out); - } - } -*/ - - isAddress = !isAddress; - return out; } +// SPI transfer with address and one single byte. +u1_t hal_spi_single (u1_t address, u1_t out) { + + u1_t buffer[2]; + buffer[0] = address; + buffer[1] = out; + + u1_t rc = wiringPiSPIDataRW(0, buffer, 2); + if (rc < 0) { + fprintf(stderr, "HAL: Cannot send data on SPI: %s\n", strerror(errno)); + hal_failed(); + } + + return buffer[1]; +} + +// SPI transfer with address and byte buffer. +void hal_spi_buffer (u1_t address, u1_t *buffer, int len) { + + u1_t buf[len + 1]; + buf[0] = address; + memcpy(&buf[1], buffer, len); + + u1_t rc = wiringPiSPIDataRW(0, buf, len + 1); + if (rc < 0) { + fprintf(stderr, "HAL: Cannot send data on SPI: %s\n", strerror(errno)); + hal_failed(); + } + + memcpy(buffer, &buf[1], len); +} + void hal_disableIRQs () { }