Now transferring buffers with SPI instead of single-byte operations.

This commit is contained in:
Wolfgang Klenk 2017-04-19 08:21:21 +02:00
parent da68f8111d
commit 514e81c3c1
3 changed files with 53 additions and 36 deletions

View File

@ -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.

View File

@ -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<len; i++) {
hal_spi(buf[i]);
}
hal_spi_buffer(addr | 0x80, buf, len);
hal_pin_nss(1);
}
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_spi_buffer(addr & 0x7F, buf, len);
hal_pin_nss(1);
}

View File

@ -101,8 +101,7 @@ void hal_init () {
// Make sure that SPI communication with the radio module works
// by reading the "version" register 0x42 of the radio module.
hal_pin_nss(0);
hal_spi(0x42 & 0x7F);
u1_t val = hal_spi(0x00);
u1_t val = hal_spi_single(0x42 & 0x7F, 0x00);
hal_pin_nss(1);
if (0 == val) {
@ -144,7 +143,7 @@ void hal_pin_nss (u1_t val) {
// switch between radio RX/TX
void hal_pin_rxtx (u1_t val) {
#ifdef DEBUG_HAL
val > 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 () {
}