SPI slave integration

This commit is contained in:
Klaus K Wilting 2018-11-03 20:29:02 +01:00
parent 85a180206a
commit 0cc98a2e24
5 changed files with 78 additions and 47 deletions

View File

@ -26,5 +26,9 @@ void os_getDevEui(u1_t *buf);
void showLoraKeys(void);
void switch_lora(uint8_t sf, uint8_t tx);
void lora_send(osjob_t *job);
void lora_enqueuedata(uint8_t messageType, MessageBuffer_t *message);
void lora_queuereset(void);
void lora_housekeeping(void);
esp_err_t lora_stack_init();
#endif

View File

@ -341,4 +341,70 @@ void lora_send(osjob_t *job) {
lora_send);
}
esp_err_t lora_stack_init() {
#ifndef HAS_LORA
return ESP_OK; // continue main program
#else
LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (LoraSendQueue == 0) {
ESP_LOGE(TAG, "Could not create LORA send queue. Aborting.");
return ESP_FAIL;
}
ESP_LOGI(TAG, "LORA send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
ESP_LOGI(TAG, "Starting LMIC...");
os_init(); // initialize lmic run-time environment on core 1
LMIC_reset(); // initialize lmic MAC
LMIC_setLinkCheckMode(0);
// This tells LMIC to make the receive windows bigger, in case your clock is
// faster or slower. This causes the transceiver to be earlier switched on,
// so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE
// in src/lmic_config.h if you are limited on battery.
LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100);
// Set the data rate to Spreading Factor 7. This is the fastest supported
// rate for 125 kHz channels, and it minimizes air time and battery power. Set
// the transmission power to 14 dBi (25 mW).
LMIC_setDrTxpow(DR_SF7, 14);
#if defined(CFG_US915) || defined(CFG_au921)
// in the US, with TTN, it saves join time if we start on subband 1 (channels
// 8-15). This will get overridden after the join by parameters from the
// network. If working with other networks or in other regions, this will need
// to be changed.
LMIC_selectSubBand(1);
#endif
LMIC_startJoining(); // start joining
return ESP_OK; // continue main program
#endif
}
#endif // HAS_LORA
void lora_enqueuedata(uint8_t messageType, MessageBuffer_t *message) {
// enqueue message in LORA send queue
#ifdef HAS_LORA
BaseType_t ret =
xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0);
if (ret == pdTRUE) {
ESP_LOGI(TAG, "%d bytes enqueued for LORA interface",
message->MessageSize);
} else {
ESP_LOGW(TAG, "LORA sendqueue is full");
}
#endif
}
void lora_queuereset(void) {
#ifdef HAS_LORA
xQueueReset(LoraSendQueue);
#endif
}
void lora_housekeeping(void) {
#ifdef HAS_LORA
// ESP_LOGD(TAG, "loraloop %d bytes left",
// uxTaskGetStackHighWaterMark(LoraTask));
#endif
}

View File

@ -56,7 +56,7 @@ uint8_t volatile channel = 0; // channel rotation counter
uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0,
batt_voltage = 0; // globals for display
hw_timer_t *channelSwitch, *sendCycle, *homeCycle, *displaytimer; // irq tasks
hw_timer_t *channelSwitch = NULL, *sendCycle = NULL, *homeCycle = NULL, *displaytimer = NULL; // irq tasks
TaskHandle_t irqHandlerTask, wifiSwitchTask;
std::set<uint16_t> macs; // container holding unique MAC adress hashes
@ -161,39 +161,8 @@ void setup() {
// initialize LoRa
#ifdef HAS_LORA
strcat_P(features, " LORA");
LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (LoraSendQueue == 0) {
ESP_LOGE(TAG, "Could not create LORA send queue. Aborting.");
exit(0);
} else
ESP_LOGI(TAG, "LORA send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
ESP_LOGI(TAG, "Starting LMIC...");
os_init(); // initialize lmic run-time environment on core 1
LMIC_reset(); // initialize lmic MAC
LMIC_setLinkCheckMode(0);
// This tells LMIC to make the receive windows bigger, in case your clock is
// faster or slower. This causes the transceiver to be earlier switched on,
// so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE
// in src/lmic_config.h if you are limited on battery.
LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100);
// Set the data rate to Spreading Factor 7. This is the fastest supported
// rate for 125 kHz channels, and it minimizes air time and battery power. Set
// the transmission power to 14 dBi (25 mW).
LMIC_setDrTxpow(DR_SF7, 14);
#if defined(CFG_US915) || defined(CFG_au921)
// in the US, with TTN, it saves join time if we start on subband 1 (channels
// 8-15). This will get overridden after the join by parameters from the
// network. If working with other networks or in other regions, this will need
// to be changed.
LMIC_selectSubBand(1);
#endif
LMIC_startJoining(); // start joining
#endif
assert(lora_stack_init() == ESP_OK);
// initialize SPI
#ifdef HAS_SPI

View File

@ -13,13 +13,8 @@ void SendData(uint8_t port) {
: (PAYLOAD_ENCODER == 4 ? LPP2PORT : LPP1PORT);
memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize());
// enqueue message in LoRa send queue
#ifdef HAS_LORA
if (xQueueSendToBack(LoraSendQueue, (void *)&SendBuffer, (TickType_t)0) ==
pdTRUE)
ESP_LOGI(TAG, "%d bytes enqueued to send on LoRa", payload.getSize());
#endif
// enqueue message in device's send queues
lora_enqueuedata(port, &SendBuffer);
spi_enqueuedata(port, &SendBuffer);
// clear counter if not in cumulative counter mode
@ -59,9 +54,6 @@ void sendPayload() {
} // sendpayload()
void flushQueues() {
#ifdef HAS_LORA
xQueueReset(LoraSendQueue);
#endif
lora_queuereset();
spi_queuereset();
}

View File

@ -131,7 +131,7 @@ void spi_enqueuedata(uint8_t messageType, MessageBuffer_t *message) {
BaseType_t ret =
xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0);
if (ret == pdTRUE) {
ESP_LOGI(TAG, "%d bytes enqueued for SPI consumption",
ESP_LOGI(TAG, "%d bytes enqueued for SPI interface",
message->MessageSize);
} else {
ESP_LOGW(TAG, "SPI sendqueue is full");