From 16759c4dd55abecc6f51539ba00dffcffc1e29d0 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 27 Oct 2018 13:51:59 +0200 Subject: [PATCH] SPI code added (experimental) --- include/spisend.h | 25 ++++++++++++++++++++++++ src/spisend.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/include/spisend.h b/include/spisend.h index 750a3c20..70cc4437 100644 --- a/include/spisend.h +++ b/include/spisend.h @@ -1,9 +1,34 @@ #ifndef _SPISEND_H #define _SPISEND_H +#include "globals.h" +#include "spi.h" + extern TaskHandle_t SpiTask; extern QueueHandle_t SPISendQueue; +/* + * Process data in SPI send queue + */ void spi_loop(void *pvParameters); +/* +* initialize local SPI wire interface +*/ +void hal_spi_init(); + +/* + * Perform SPI write transaction on local SPI wire interface + * - write the command byte 'cmd' + * - write 'len' bytes out of 'buf' + */ +void hal_spi_write(uint8_t cmd, const uint8_t* buf, int len); + +/* + * Perform SPI read transaction on local SPI wire interface + * - read the command byte 'cmd' + * - read 'len' bytes into 'buf' + */ +void hal_spi_read(uint8_t cmd, uint8_t* buf, int len); + #endif \ No newline at end of file diff --git a/src/spisend.cpp b/src/spisend.cpp index 57da9287..c778a887 100644 --- a/src/spisend.cpp +++ b/src/spisend.cpp @@ -1,6 +1,6 @@ #ifdef HAS_SPI -#include "globals.h" +#include "spisend.h" // Local logging tag static const char TAG[] = "main"; @@ -13,18 +13,62 @@ TaskHandle_t SpiTask; // SPI feed Task void spi_loop(void *pvParameters) { + uint8_t buf[32]; + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check while (1) { + // check if data to send on SPI interface if (xQueueReceive(SPISendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) { + hal_spi_write(SendBuffer.MessagePort, SendBuffer.Message, + SendBuffer.MessageSize); ESP_LOGI(TAG, "%d bytes sent to SPI", SendBuffer.MessageSize); } - vTaskDelay(2 / portTICK_PERIOD_MS); // yield to CPU - } // end of infinite loop + // check if command is received on SPI command port, then call interpreter + hal_spi_read(RCMDPORT, buf, 32); + if (buf[0]) + rcommand(buf, sizeof(buf)); + + vTaskDelay(2 / portTICK_PERIOD_MS); // yield to CPU + } // end of infinite loop vTaskDelete(NULL); // shoud never be reached } // spi_loop() +// SPI hardware abstraction layer + +void hal_spi_init() { SPI.begin(SCK, MISO, MOSI, SS); } + +void hal_spi_trx(u1_t cmd, u1_t *buf, int len, u1_t is_read) { + + u1_t nss = SS; + SPISettings settings(1E6, 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; + } + + 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); +} + +void hal_spi_read(u1_t cmd, u1_t *buf, int len) { + hal_spi_trx(cmd, buf, len, 1); +} + #endif // HAS_SPI \ No newline at end of file