added priority control for lora and spi sendqueue
This commit is contained in:
		
							parent
							
								
									cd08335634
								
							
						
					
					
						commit
						06a1775779
					
				| @ -92,13 +92,15 @@ typedef struct { | |||||||
|   float gas;             // raw gas sensor signal
 |   float gas;             // raw gas sensor signal
 | ||||||
| } bmeStatus_t; | } bmeStatus_t; | ||||||
| 
 | 
 | ||||||
|  | enum sendprio_t { prio_low, prio_normal, prio_high }; | ||||||
|  | 
 | ||||||
| // global variables
 | // global variables
 | ||||||
| extern configData_t cfg;                      // current device configuration
 | extern configData_t cfg;                      // current device configuration
 | ||||||
| extern char display_line6[], display_line7[]; // screen buffers
 | extern char display_line6[], display_line7[]; // screen buffers
 | ||||||
| extern uint8_t volatile channel;              // wifi channel rotation counter
 | extern uint8_t volatile channel;              // wifi channel rotation counter
 | ||||||
| extern uint16_t volatile macs_total, macs_wifi, macs_ble, | extern uint16_t volatile macs_total, macs_wifi, macs_ble, | ||||||
|     batt_voltage; // display values
 |     batt_voltage; // display values
 | ||||||
| extern hw_timer_t *channelSwitch, *sendCycle, *displaytimer; | extern hw_timer_t *sendCycle, *displaytimer; | ||||||
| extern SemaphoreHandle_t I2Caccess; | extern SemaphoreHandle_t I2Caccess; | ||||||
| 
 | 
 | ||||||
| extern std::set<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs; | extern std::set<uint16_t, std::less<uint16_t>, Mallocator<uint16_t>> macs; | ||||||
| @ -150,4 +152,8 @@ extern Timezone myTZ; | |||||||
| #include "if482.h" | #include "if482.h" | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | #ifdef HAS_DCF77 | ||||||
|  | #include "dcf77.h" | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
| @ -33,7 +33,7 @@ void os_getDevEui(u1_t *buf); | |||||||
| void showLoraKeys(void); | void showLoraKeys(void); | ||||||
| void switch_lora(uint8_t sf, uint8_t tx); | void switch_lora(uint8_t sf, uint8_t tx); | ||||||
| void lora_send(osjob_t *job); | void lora_send(osjob_t *job); | ||||||
| void lora_enqueuedata(MessageBuffer_t *message); | void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio); | ||||||
| void lora_queuereset(void); | void lora_queuereset(void); | ||||||
| void lora_housekeeping(void); | void lora_housekeeping(void); | ||||||
| void user_request_network_time_callback(void *pVoidUserUTCTime, | void user_request_network_time_callback(void *pVoidUserUTCTime, | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ | |||||||
| #include "lorawan.h" | #include "lorawan.h" | ||||||
| #include "cyclic.h" | #include "cyclic.h" | ||||||
| 
 | 
 | ||||||
| void SendPayload(uint8_t port); | void SendPayload(uint8_t port, sendprio_t prio); | ||||||
| void sendCounter(void); | void sendCounter(void); | ||||||
| void checkSendQueues(void); | void checkSendQueues(void); | ||||||
| void flushQueues(); | void flushQueues(); | ||||||
|  | |||||||
| @ -28,7 +28,7 @@ licenses. Refer to LICENSE.txt file in repository for more details. | |||||||
| 
 | 
 | ||||||
| esp_err_t spi_init(); | esp_err_t spi_init(); | ||||||
| 
 | 
 | ||||||
| void spi_enqueuedata(MessageBuffer_t *message); | void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio); | ||||||
| void spi_queuereset(); | void spi_queuereset(); | ||||||
| 
 | 
 | ||||||
| void spi_housekeeping(); | void spi_housekeeping(); | ||||||
|  | |||||||
| @ -10,6 +10,6 @@ void readButton() { | |||||||
|     ESP_LOGI(TAG, "Button pressed"); |     ESP_LOGI(TAG, "Button pressed"); | ||||||
|     payload.reset(); |     payload.reset(); | ||||||
|     payload.addButton(0x01); |     payload.addButton(0x01); | ||||||
|     SendPayload(BUTTONPORT); |     SendPayload(BUTTONPORT, prio_normal); | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| @ -85,7 +85,7 @@ void doHousekeeping() { | |||||||
|              "Memory full, counter cleared (heap low water mark = %d Bytes / " |              "Memory full, counter cleared (heap low water mark = %d Bytes / " | ||||||
|              "free heap = %d bytes)", |              "free heap = %d bytes)", | ||||||
|              ESP.getMinFreeHeap(), ESP.getFreeHeap()); |              ESP.getMinFreeHeap(), ESP.getFreeHeap()); | ||||||
|     SendPayload(COUNTERPORT); // send data before clearing counters
 |     SendPayload(COUNTERPORT, prio_high); // send data before clearing counters
 | ||||||
|     reset_counters();         // clear macs container and reset all counters
 |     reset_counters();         // clear macs container and reset all counters
 | ||||||
|     get_salt();               // get new salt for salting hashes
 |     get_salt();               // get new salt for salting hashes
 | ||||||
| 
 | 
 | ||||||
| @ -97,7 +97,7 @@ void doHousekeeping() { | |||||||
| #ifdef BOARD_HAS_PSRAM | #ifdef BOARD_HAS_PSRAM | ||||||
|   if (ESP.getMinFreePsram() <= MEM_LOW) { |   if (ESP.getMinFreePsram() <= MEM_LOW) { | ||||||
|     ESP_LOGI(TAG, "PSRAM full, counter cleared"); |     ESP_LOGI(TAG, "PSRAM full, counter cleared"); | ||||||
|     SendPayload(COUNTERPORT); // send data before clearing counters
 |     SendPayload(COUNTERPORT, prio_high); // send data before clearing counters
 | ||||||
|     reset_counters();         // clear macs container and reset all counters
 |     reset_counters();         // clear macs container and reset all counters
 | ||||||
|     get_salt();               // get new salt for salting hashes
 |     get_salt();               // get new salt for salting hashes
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -358,6 +358,7 @@ esp_err_t lora_stack_init() { | |||||||
| #ifndef HAS_LORA | #ifndef HAS_LORA | ||||||
|   return ESP_OK; // continue main program
 |   return ESP_OK; // continue main program
 | ||||||
| #else | #else | ||||||
|  |   assert(SEND_QUEUE_SIZE); | ||||||
|   LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); |   LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); | ||||||
|   if (LoraSendQueue == 0) { |   if (LoraSendQueue == 0) { | ||||||
|     ESP_LOGE(TAG, "Could not create LORA send queue. Aborting."); |     ESP_LOGE(TAG, "Could not create LORA send queue. Aborting."); | ||||||
| @ -397,11 +398,20 @@ esp_err_t lora_stack_init() { | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void lora_enqueuedata(MessageBuffer_t *message) { | void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { | ||||||
|   // enqueue message in LORA send queue
 |   // enqueue message in LORA send queue
 | ||||||
| #ifdef HAS_LORA | #ifdef HAS_LORA | ||||||
|   BaseType_t ret = |   BaseType_t ret; | ||||||
|       xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); |   switch (prio) { | ||||||
|  |   case prio_high: | ||||||
|  |   ret = xQueueSendToFront(LoraSendQueue, (void *)message, (TickType_t)0); | ||||||
|  |     break; | ||||||
|  |   case prio_low: | ||||||
|  |   case prio_normal: | ||||||
|  |   default: | ||||||
|  |     ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); | ||||||
|  |     break; | ||||||
|  |   } | ||||||
|   if (ret == pdTRUE) { |   if (ret == pdTRUE) { | ||||||
|     ESP_LOGI(TAG, "%d bytes enqueued for LORA interface", message->MessageSize); |     ESP_LOGI(TAG, "%d bytes enqueued for LORA interface", message->MessageSize); | ||||||
|   } else { |   } else { | ||||||
|  | |||||||
| @ -107,7 +107,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { | |||||||
| #endif | #endif | ||||||
|           payload.reset(); |           payload.reset(); | ||||||
|           payload.addAlarm(rssi, beaconID); |           payload.addAlarm(rssi, beaconID); | ||||||
|           SendPayload(BEACONPORT); |           SendPayload(BEACONPORT, prio_high); | ||||||
|         } |         } | ||||||
|       }; |       }; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -47,7 +47,7 @@ | |||||||
| #define PAYLOAD_BUFFER_SIZE             51      // maximum size of payload block per transmit | #define PAYLOAD_BUFFER_SIZE             51      // maximum size of payload block per transmit | ||||||
| #define LORASFDEFAULT                   9       // 7 ... 12 SF, according to LoRaWAN specs | #define LORASFDEFAULT                   9       // 7 ... 12 SF, according to LoRaWAN specs | ||||||
| #define MAXLORARETRY                    500     // maximum count of TX retries if LoRa busy | #define MAXLORARETRY                    500     // maximum count of TX retries if LoRa busy | ||||||
| #define SEND_QUEUE_SIZE                 10      // maximum number of messages in payload send queue | #define SEND_QUEUE_SIZE                 10       // maximum number of messages in payload send queue [1 = no queue] | ||||||
| 
 | 
 | ||||||
| // Ports on which the device sends and listenes on LoRaWAN and SPI | // Ports on which the device sends and listenes on LoRaWAN and SPI | ||||||
| #define COUNTERPORT                     1       // Port on which device sends counts | #define COUNTERPORT                     1       // Port on which device sends counts | ||||||
|  | |||||||
| @ -233,7 +233,7 @@ void get_config(uint8_t val[]) { | |||||||
|   ESP_LOGI(TAG, "Remote command: get device configuration"); |   ESP_LOGI(TAG, "Remote command: get device configuration"); | ||||||
|   payload.reset(); |   payload.reset(); | ||||||
|   payload.addConfig(cfg); |   payload.addConfig(cfg); | ||||||
|   SendPayload(CONFIGPORT); |   SendPayload(CONFIGPORT, prio_high); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| void get_status(uint8_t val[]) { | void get_status(uint8_t val[]) { | ||||||
| @ -247,7 +247,7 @@ void get_status(uint8_t val[]) { | |||||||
|   payload.addStatus(voltage, uptime() / 1000, temperatureRead(), |   payload.addStatus(voltage, uptime() / 1000, temperatureRead(), | ||||||
|                     getFreeRAM(), rtc_get_reset_reason(0), |                     getFreeRAM(), rtc_get_reset_reason(0), | ||||||
|                     rtc_get_reset_reason(1)); |                     rtc_get_reset_reason(1)); | ||||||
|   SendPayload(STATUSPORT); |   SendPayload(STATUSPORT, prio_high); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| void get_gps(uint8_t val[]) { | void get_gps(uint8_t val[]) { | ||||||
| @ -256,7 +256,7 @@ void get_gps(uint8_t val[]) { | |||||||
|   gps_read(); |   gps_read(); | ||||||
|   payload.reset(); |   payload.reset(); | ||||||
|   payload.addGPS(gps_status); |   payload.addGPS(gps_status); | ||||||
|   SendPayload(GPSPORT); |   SendPayload(GPSPORT, prio_high); | ||||||
| #else | #else | ||||||
|   ESP_LOGW(TAG, "GPS function not supported"); |   ESP_LOGW(TAG, "GPS function not supported"); | ||||||
| #endif | #endif | ||||||
| @ -267,7 +267,7 @@ void get_bme(uint8_t val[]) { | |||||||
| #ifdef HAS_BME | #ifdef HAS_BME | ||||||
|   payload.reset(); |   payload.reset(); | ||||||
|   payload.addBME(bme_status); |   payload.addBME(bme_status); | ||||||
|   SendPayload(BMEPORT); |   SendPayload(BMEPORT, prio_high); | ||||||
| #else | #else | ||||||
|   ESP_LOGW(TAG, "BME680 sensor not supported"); |   ESP_LOGW(TAG, "BME680 sensor not supported"); | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| #include "senddata.h" | #include "senddata.h" | ||||||
| 
 | 
 | ||||||
| // put data to send in RTos Queues used for transmit over channels Lora and SPI
 | // put data to send in RTos Queues used for transmit over channels Lora and SPI
 | ||||||
| void SendPayload(uint8_t port) { | void SendPayload(uint8_t port, sendprio_t prio) { | ||||||
| 
 | 
 | ||||||
|   MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[]
 |   MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[]
 | ||||||
| 
 | 
 | ||||||
| @ -24,8 +24,8 @@ void SendPayload(uint8_t port) { | |||||||
|   memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize()); |   memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize()); | ||||||
| 
 | 
 | ||||||
|   // enqueue message in device's send queues
 |   // enqueue message in device's send queues
 | ||||||
|   lora_enqueuedata(&SendBuffer); |   lora_enqueuedata(&SendBuffer, prio); | ||||||
|   spi_enqueuedata(&SendBuffer); |   spi_enqueuedata(&SendBuffer, prio); | ||||||
| 
 | 
 | ||||||
| } // SendPayload
 | } // SendPayload
 | ||||||
| 
 | 
 | ||||||
| @ -55,7 +55,7 @@ void sendCounter() { | |||||||
|       } |       } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|       SendPayload(COUNTERPORT); |       SendPayload(COUNTERPORT, prio_normal); | ||||||
|       // clear counter if not in cumulative counter mode
 |       // clear counter if not in cumulative counter mode
 | ||||||
|       if (cfg.countermode != 1) { |       if (cfg.countermode != 1) { | ||||||
|         reset_counters(); // clear macs container and reset all counters
 |         reset_counters(); // clear macs container and reset all counters
 | ||||||
| @ -68,7 +68,7 @@ void sendCounter() { | |||||||
|     case MEMS_DATA: |     case MEMS_DATA: | ||||||
|       payload.reset(); |       payload.reset(); | ||||||
|       payload.addBME(bme_status); |       payload.addBME(bme_status); | ||||||
|       SendPayload(BMEPORT); |       SendPayload(BMEPORT, prio_normal); | ||||||
|       break; |       break; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| @ -79,7 +79,7 @@ void sendCounter() { | |||||||
|         gps_read(); |         gps_read(); | ||||||
|         payload.reset(); |         payload.reset(); | ||||||
|         payload.addGPS(gps_status); |         payload.addGPS(gps_status); | ||||||
|         SendPayload(GPSPORT); |         SendPayload(GPSPORT, prio_high); | ||||||
|       } else |       } else | ||||||
|         ESP_LOGD(TAG, "No valid GPS position"); |         ESP_LOGD(TAG, "No valid GPS position"); | ||||||
|       break; |       break; | ||||||
| @ -89,17 +89,17 @@ void sendCounter() { | |||||||
|     case SENSOR1_DATA: |     case SENSOR1_DATA: | ||||||
|       payload.reset(); |       payload.reset(); | ||||||
|       payload.addSensor(sensor_read(1)); |       payload.addSensor(sensor_read(1)); | ||||||
|       SendPayload(SENSOR1PORT); |       SendPayload(SENSOR1PORT, prio_normal); | ||||||
|       break; |       break; | ||||||
|     case SENSOR2_DATA: |     case SENSOR2_DATA: | ||||||
|       payload.reset(); |       payload.reset(); | ||||||
|       payload.addSensor(sensor_read(2)); |       payload.addSensor(sensor_read(2)); | ||||||
|       SendPayload(SENSOR2PORT); |       SendPayload(SENSOR2PORT, prio_normal); | ||||||
|       break; |       break; | ||||||
|     case SENSOR3_DATA: |     case SENSOR3_DATA: | ||||||
|       payload.reset(); |       payload.reset(); | ||||||
|       payload.addSensor(sensor_read(3)); |       payload.addSensor(sensor_read(3)); | ||||||
|       SendPayload(SENSOR3PORT); |       SendPayload(SENSOR3PORT, prio_normal); | ||||||
|       break; |       break; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| @ -107,7 +107,7 @@ void sendCounter() { | |||||||
|     case BATT_DATA: |     case BATT_DATA: | ||||||
|       payload.reset(); |       payload.reset(); | ||||||
|       payload.addVoltage(read_voltage()); |       payload.addVoltage(read_voltage()); | ||||||
|       SendPayload(BATTPORT); |       SendPayload(BATTPORT, prio_normal); | ||||||
|       break; |       break; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -66,7 +66,8 @@ void spi_slave_task(void *param) { | |||||||
|     uint8_t *messageSize = txbuf + 3; |     uint8_t *messageSize = txbuf + 3; | ||||||
|     *messageSize = msg.MessageSize; |     *messageSize = msg.MessageSize; | ||||||
|     memcpy(txbuf + HEADER_SIZE, &msg.Message, msg.MessageSize); |     memcpy(txbuf + HEADER_SIZE, &msg.Message, msg.MessageSize); | ||||||
|     // calculate crc16 checksum over txbuf and insert checksum at pos 0+1 of txbuf
 |     // calculate crc16 checksum over txbuf and insert checksum at pos 0+1 of
 | ||||||
|  |     // txbuf
 | ||||||
|     uint16_t *crc = (uint16_t *)txbuf; |     uint16_t *crc = (uint16_t *)txbuf; | ||||||
|     *crc = crc16_be(0, messageType, msg.MessageSize + HEADER_SIZE - 2); |     *crc = crc16_be(0, messageType, msg.MessageSize + HEADER_SIZE - 2); | ||||||
| 
 | 
 | ||||||
| @ -87,7 +88,8 @@ void spi_slave_task(void *param) { | |||||||
|     // wait until spi master clocks out the data, and read results in rx buffer
 |     // wait until spi master clocks out the data, and read results in rx buffer
 | ||||||
|     ESP_LOGI(TAG, "Prepared SPI transaction for %zu byte(s)", transaction_size); |     ESP_LOGI(TAG, "Prepared SPI transaction for %zu byte(s)", transaction_size); | ||||||
|     ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, transaction_size, ESP_LOG_DEBUG); |     ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, transaction_size, ESP_LOG_DEBUG); | ||||||
|     ESP_ERROR_CHECK_WITHOUT_ABORT(spi_slave_transmit(HSPI_HOST, &spi_transaction, portMAX_DELAY)); |     ESP_ERROR_CHECK_WITHOUT_ABORT( | ||||||
|  |         spi_slave_transmit(HSPI_HOST, &spi_transaction, portMAX_DELAY)); | ||||||
|     ESP_LOG_BUFFER_HEXDUMP(TAG, rxbuf, transaction_size, ESP_LOG_DEBUG); |     ESP_LOG_BUFFER_HEXDUMP(TAG, rxbuf, transaction_size, ESP_LOG_DEBUG); | ||||||
|     ESP_LOGI(TAG, "Transaction finished with size %zu bits", |     ESP_LOGI(TAG, "Transaction finished with size %zu bits", | ||||||
|              spi_transaction.trans_len); |              spi_transaction.trans_len); | ||||||
| @ -103,7 +105,7 @@ esp_err_t spi_init() { | |||||||
| #ifndef HAS_SPI | #ifndef HAS_SPI | ||||||
|   return ESP_OK; |   return ESP_OK; | ||||||
| #else | #else | ||||||
| 
 |   assert(SEND_QUEUE_SIZE); | ||||||
|   SPISendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); |   SPISendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); | ||||||
|   if (SPISendQueue == 0) { |   if (SPISendQueue == 0) { | ||||||
|     ESP_LOGE(TAG, "Could not create SPI send queue. Aborting."); |     ESP_LOGE(TAG, "Could not create SPI send queue. Aborting."); | ||||||
| @ -148,11 +150,20 @@ esp_err_t spi_init() { | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void spi_enqueuedata(MessageBuffer_t *message) { | void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { | ||||||
|   // enqueue message in SPI send queue
 |   // enqueue message in SPI send queue
 | ||||||
| #ifdef HAS_SPI | #ifdef HAS_SPI | ||||||
|   BaseType_t ret = |   BaseType_t ret; | ||||||
|       xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0); |   switch (prio) { | ||||||
|  |   case prio_high: | ||||||
|  |     ret = xQueueSendToFront(SPISendQueue, (void *)message, (TickType_t)0); | ||||||
|  |     break; | ||||||
|  |   case prio_low: | ||||||
|  |   case prio_normal: | ||||||
|  |   default: | ||||||
|  |     ret = xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0); | ||||||
|  |     break; | ||||||
|  |   } | ||||||
|   if (ret == pdTRUE) { |   if (ret == pdTRUE) { | ||||||
|     ESP_LOGI(TAG, "%d byte(s) enqueued for SPI interface", |     ESP_LOGI(TAG, "%d byte(s) enqueued for SPI interface", | ||||||
|              message->MessageSize); |              message->MessageSize); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user