SPI code added (experimental)

This commit is contained in:
Klaus K Wilting 2018-10-27 13:51:59 +02:00
parent 41bec2714d
commit 16759c4dd5
2 changed files with 72 additions and 3 deletions

View File

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

View File

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