2018-10-03 16:24:45 +02:00
|
|
|
#ifdef HAS_SPI
|
|
|
|
|
2018-10-27 13:51:59 +02:00
|
|
|
#include "spisend.h"
|
2018-10-03 16:24:45 +02:00
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = "main";
|
|
|
|
|
|
|
|
MessageBuffer_t SendBuffer;
|
|
|
|
|
|
|
|
QueueHandle_t SPISendQueue;
|
|
|
|
TaskHandle_t SpiTask;
|
|
|
|
|
|
|
|
// SPI feed Task
|
|
|
|
void spi_loop(void *pvParameters) {
|
|
|
|
|
2018-10-27 13:51:59 +02:00
|
|
|
uint8_t buf[32];
|
|
|
|
|
2018-10-03 16:24:45 +02:00
|
|
|
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
|
|
|
|
|
|
|
|
while (1) {
|
2018-10-27 13:51:59 +02:00
|
|
|
// check if data to send on SPI interface
|
2018-10-03 16:24:45 +02:00
|
|
|
if (xQueueReceive(SPISendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) {
|
2018-10-27 13:51:59 +02:00
|
|
|
hal_spi_write(SendBuffer.MessagePort, SendBuffer.Message,
|
|
|
|
SendBuffer.MessageSize);
|
2018-10-03 16:24:45 +02:00
|
|
|
ESP_LOGI(TAG, "%d bytes sent to SPI", SendBuffer.MessageSize);
|
|
|
|
}
|
|
|
|
|
2018-10-27 13:51:59 +02:00
|
|
|
// 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
|
2018-10-03 16:24:45 +02:00
|
|
|
|
|
|
|
vTaskDelete(NULL); // shoud never be reached
|
|
|
|
|
|
|
|
} // spi_loop()
|
|
|
|
|
2018-10-27 13:51:59 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:24:45 +02:00
|
|
|
#endif // HAS_SPI
|