Merge pull request #200 from cyberman54/development

v1.6.62
This commit is contained in:
Verkehrsrot 2018-11-08 21:10:33 +01:00 committed by GitHub
commit a51d9f77ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
43 changed files with 548 additions and 435 deletions

View File

@ -14,7 +14,7 @@ Paxcounter is a proof-of-concept device for metering passenger flows in realtime
Intention of this project is to do this without intrusion in privacy: You don't need to track people owned devices, if you just want to count them. Therefore, Paxcounter does not persistenly store MAC adresses and does no kind of fingerprinting the scanned devices.
Metered counts are transferred to a server via a LoRaWAN network, and/or a local SPI cable interface.
Data is transferred to a server via a LoRaWAN network, and/or a wired SPI slave interface.
You can build this project battery powered and reach a full day uptime with a single 18650 Li-Ion cell.
@ -33,15 +33,16 @@ This can all be done with a single small and cheap ESP32 board for less than $20
LoLin32lite + [LoraNode32-Lite shield](https://github.com/hallard/LoLin32-Lite-Lora)
- Adafruit ESP32 Feather + LoRa Wing + OLED Wing, #IoT Octopus32 (Octopus + ESP32 Feather)
*SPI only*: (code yet to come)
*SPI only*:
- Pyom: WiPy
- WeMos: LoLin32, LoLin32 Lite, WeMos D32
- Generic ESP32
Depending on board hardware following features are supported:
- LED
- OLED Display
- RGB LED
- LED (power/status)
- OLED Display (detailed status)
- RGB LED (colorized status)
- Button
- Silicon unique ID
- Battery voltage monitoring

View File

@ -38,6 +38,14 @@ typedef struct {
uint8_t Message[PAYLOAD_BUFFER_SIZE];
} MessageBuffer_t;
typedef struct {
uint32_t latitude;
uint32_t longitude;
uint8_t satellites;
uint16_t hdop;
uint16_t altitude;
} gpsStatus_t;
// global variables
extern configData_t cfg; // current device configuration
extern char display_line6[], display_line7[]; // screen buffers
@ -52,24 +60,17 @@ extern std::array<uint64_t, 0xff> beacons;
extern TaskHandle_t irqHandlerTask, wifiSwitchTask;
#include "led.h"
#include "payload.h"
#ifdef HAS_GPS
#include "gpsread.h"
#endif
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
#include "led.h"
#endif
#include "payload.h"
#ifdef HAS_LORA
#include "lorawan.h"
#endif
#ifdef HAS_SPI
#include "spisend.h"
#endif
#ifdef HAS_DISPLAY
#include "display.h"
#endif
@ -90,8 +91,4 @@ extern TaskHandle_t irqHandlerTask, wifiSwitchTask;
#include "antenna.h"
#endif
void reset_counters(void);
void blink_LED(uint16_t set_color, uint16_t set_blinkduration);
uint64_t uptime();
#endif

View File

@ -8,14 +8,6 @@
#include <Wire.h>
#endif
typedef struct {
uint32_t latitude;
uint32_t longitude;
uint8_t satellites;
uint16_t hdop;
uint16_t altitude;
} gpsStatus_t;
extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
extern gpsStatus_t
gps_status; // Make struct for storing gps data globally available

View File

@ -37,8 +37,6 @@ extern TaskHandle_t ledLoopTask;
void rgb_set_color(uint16_t hue);
void blink_LED(uint16_t set_color, uint16_t set_blinkduration);
void ledLoop(void *parameter);
#if (HAS_LED != NOT_A_PIN)
void switch_LED(uint8_t state);
#endif
#endif

View File

@ -26,5 +26,10 @@ 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

@ -13,5 +13,7 @@
#include "ota.h"
#include "irqhandler.h"
#include "led.h"
#include "spislave.h"
#include "lorawan.h"
#endif

View File

@ -4,14 +4,15 @@
#ifdef USE_OTA
#include "globals.h"
#include "update.h"
#include "battery.h"
#include "update.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BintrayClient.h>
#include <string>
#include <algorithm>
void do_ota_update();
int do_ota_update();
void start_ota_update();
int version_compare(const String v1, const String v2);
void display(const uint8_t row, const std::string status,

View File

@ -38,12 +38,8 @@ public:
void addStatus(uint16_t voltage, uint64_t uptime, float cputemp, uint32_t mem,
uint8_t reset1, uint8_t reset2);
void addAlarm(int8_t rssi, uint8_t message);
#ifdef HAS_GPS
void addGPS(gpsStatus_t value);
#endif
#ifdef HAS_BUTTON
void addButton(uint8_t value);
#endif
#if PAYLOAD_ENCODER == 1 // format plain

View File

@ -2,6 +2,7 @@
#define _RCOMMAND_H
#include "senddata.h"
#include "cyclic.h"
#include "configmanager.h"
#include "lorawan.h"
#include "macsniff.h"

View File

@ -1,6 +1,10 @@
#ifndef _SENDDATA_H
#define _SENDDATA_H
#include "spislave.h"
#include "lorawan.h"
#include "cyclic.h"
void SendData(uint8_t port);
void sendPayload(void);
void checkSendQueues(void);

View File

@ -1,34 +0,0 @@
#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

36
include/spislave.h Normal file
View File

@ -0,0 +1,36 @@
/*
//////////////////////// ESP32-Paxcounter \\\\\\\\\\\\\\\\\\\\\\\\\\
Copyright 2018 Christian Ambach <christian.ambach@deutschebahn.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
NOTICE:
Parts of the source files in this repository are made available under different
licenses. Refer to LICENSE.txt file in repository for more details.
*/
#ifndef _SPISLAVE_H
#define _SPISLAVE_H
#include "globals.h"
esp_err_t spi_init();
void spi_enqueuedata(uint8_t messageType, MessageBuffer_t *message);
void spi_queuereset();
void spi_housekeeping();
#endif // _SPISLAVE_H

View File

@ -41,7 +41,7 @@ class UpdateClass {
Call this to check the space needed for the update
Will return false if there is not enough space
*/
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH);
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);
/*
Writes a buffer to the flash and increments the address
@ -174,6 +174,9 @@ class UpdateClass {
String _target_md5;
MD5Builder _md5;
int _ledPin;
uint8_t _ledOn;
};
extern UpdateClass Update;

View File

@ -29,7 +29,7 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng
[common]
; for release_version use max. 10 chars total, use any decimal format like "a.b.c"
release_version = 1.6.54
release_version = 1.6.62
; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running!
; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose
debug_level = 0
@ -38,7 +38,7 @@ upload_protocol = esptool
;upload_protocol = custom
extra_scripts = pre:build.py
keyfile = ota.conf
platform_espressif32 = espressif32@1.4.0
platform_espressif32 = espressif32@1.5.0
board_build.partitions = min_spiffs.csv
monitor_speed = 115200
lib_deps_all =
@ -58,6 +58,7 @@ build_flags =
-w
'-DARDUINO_LMIC_PROJECT_CONFIG_H=../../../src/lmic_config.h'
'-DCORE_DEBUG_LEVEL=${common.debug_level}'
'-DLOG_LOCAL_LEVEL=${common.debug_level}'
'-DBINTRAY_PACKAGE="${PIOENV}"'
'-DPROGVERSION="${common.release_version}"'

View File

@ -1,10 +1,9 @@
#ifdef HAS_BATTERY_PROBE
#include "globals.h"
// Local logging tag
static const char TAG[] = "main";
#ifdef HAS_BATTERY_PROBE
esp_adc_cal_characteristics_t *adc_characs =
(esp_adc_cal_characteristics_t *)calloc(
1, sizeof(esp_adc_cal_characteristics_t));
@ -12,8 +11,10 @@ esp_adc_cal_characteristics_t *adc_characs =
static const adc1_channel_t adc_channel = HAS_BATTERY_PROBE;
static const adc_atten_t atten = ADC_ATTEN_DB_11;
static const adc_unit_t unit = ADC_UNIT_1;
#endif
void calibrate_voltage(void) {
#ifdef HAS_BATTERY_PROBE
// configure ADC
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
ESP_ERROR_CHECK(adc1_config_channel_atten(adc_channel, atten));
@ -30,9 +31,11 @@ void calibrate_voltage(void) {
} else {
ESP_LOGI(TAG, "ADC characterization based on default reference voltage");
}
#endif
}
uint16_t read_voltage() {
#ifdef HAS_BATTERY_PROBE
// multisample ADC
uint32_t adc_reading = 0;
for (int i = 0; i < NO_OF_SAMPLES; i++) {
@ -47,11 +50,17 @@ uint16_t read_voltage() {
#endif
ESP_LOGD(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage);
return voltage;
#else
return 0;
#endif
}
bool batt_sufficient() {
#ifdef HAS_BATTERY_PROBE
uint16_t volts = read_voltage();
return (( volts < 1000 ) || (volts > OTA_MIN_BATT)); // no battery or battery sufficient
return ((volts < 1000) ||
(volts > OTA_MIN_BATT)); // no battery or battery sufficient
#else
return true;
#endif
}
#endif // HAS_BATTERY_PROBE

View File

@ -3,6 +3,8 @@
// Basic config
#include "cyclic.h"
#include "rcommand.h"
#include "spislave.h"
// Local logging tag
static const char TAG[] = "main";
@ -25,9 +27,9 @@ void doHousekeeping() {
#ifdef HAS_GPS
ESP_LOGD(TAG, "Gpsloop %d bytes left", uxTaskGetStackHighWaterMark(GpsTask));
#endif
#ifdef HAS_SPI
ESP_LOGD(TAG, "Spiloop %d bytes left", uxTaskGetStackHighWaterMark(SpiTask));
#endif
spi_housekeeping();
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
ESP_LOGD(TAG, "LEDloop %d bytes left", uxTaskGetStackHighWaterMark(ledLoopTask));
#endif

View File

@ -6,7 +6,6 @@
// Hardware related definitions for ebox ESP32-bit with external connected RFM95 LoRa
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1
#define HAS_LED (23) // blue LED on board

View File

@ -6,7 +6,6 @@
// Hardware related definitions for ebox ESP32-bit with external connected RFM95 LoRa
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1
#define HAS_LED (22) // Green LED on board

View File

@ -6,7 +6,6 @@
// Hardware related definitions for Pycom FiPy Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1272_radio 1
#define HAS_LED NOT_A_PIN // FiPy has no on board LED, so we use RGB LED

View File

@ -7,6 +7,11 @@
#define HAS_LORA 1 // comment out if device shall not send data via LoRa or has no LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
// pin definitions for SPI slave interface
#define SPI_MOSI GPIO_NUM_23
#define SPI_MISO GPIO_NUM_19
#define SPI_SCLK GPIO_NUM_18
#define SPI_CS GPIO_NUM_5
#define CFG_sx1276_radio 1 // select LoRa chip
//#define CFG_sx1272_radio 1 // select LoRa chip

View File

@ -6,7 +6,6 @@
// Hardware related definitions for Heltec LoRa-32 Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C // OLED-Display on board

View File

@ -6,7 +6,6 @@
// Hardware related definitions for Heltec V2 LoRa-32 Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C // OLED-Display on board

View File

@ -7,9 +7,7 @@
#define CFG_sx1272_radio 1 // dummy
#define HAS_LED 22 // on board LED on GPIO22
#define HAS_LED (5) // on board LED on GPIO5
#define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#endif

View File

@ -18,7 +18,6 @@
#define BUTTON_PULLUP 1 // Button need pullup instead of default pulldown
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // RFM95 module
// Pins for LORA chip SPI interface, reset line and interrupt lines

View File

@ -19,7 +19,6 @@
#define BUTTON_PULLUP 1 // Button need pullup instead of default pulldown
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // RFM95 module
// Pins for LORA chip SPI interface, reset line and interrupt lines

View File

@ -6,7 +6,6 @@
// Hardware related definitions for Pycom LoPy Board (NOT LoPy4)
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1272_radio 1
#define HAS_LED NOT_A_PIN // LoPy4 has no on board mono LED, we use on board RGB LED
#define HAS_RGB_LED (0) // WS2812B RGB LED on GPIO0
@ -26,9 +25,9 @@
#define WIFI_ANTENNA 0 // 0 = internal, 1 = external
// uncomment this only if your LoPy runs on a PYTRACK BOARD
//#define HAS_GPS 1
//#define GPS_I2C GPIO_NUM_25, GPIO_NUM_26 // SDA (P22), SCL (P21)
//#define GPS_ADDR 0x10
#define HAS_GPS 1
#define GPS_I2C GPIO_NUM_25, GPIO_NUM_26 // SDA (P22), SCL (P21)
#define GPS_ADDR 0x10
// uncomment this only if your LoPy runs on a EXPANSION BOARD
//#define HAS_LED (12) // use if LoPy is on Expansion Board, this has a user LED

View File

@ -6,7 +6,14 @@
// Hardware related definitions for Pycom LoPy4 Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
// pin definitions for local wired SPI slave interface
#define SPI_MOSI GPIO_NUM_22
#define SPI_MISO GPIO_NUM_33
#define SPI_SCLK GPIO_NUM_26
#define SPI_CS GPIO_NUM_36
#define CFG_sx1276_radio 1
//#define HAS_LED NOT_A_PIN // LoPy4 has no on board mono LED, we use on board RGB LED
#define HAS_RGB_LED (0) // WS2812B RGB LED on GPIO0 (P2)

View File

@ -17,7 +17,6 @@
//#define BUTTON_PULLUP 1 // Button need pullup instead of default pulldown
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // RFM95 module
// Pins for LORA chip SPI interface, reset line and interrupt lines

View File

@ -6,7 +6,6 @@
// Hardware related definitions for TTGO T-Beam board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
#define BOARD_HAS_PSRAM // use extra 4MB external RAM

View File

@ -6,7 +6,6 @@
// Hardware related definitions for TTGOv1 board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C // OLED-Display on board

View File

@ -6,7 +6,6 @@
// Hardware related definitions for TTGO V2 Board
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C

View File

@ -9,7 +9,6 @@
*/
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
#define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C

View File

@ -10,7 +10,6 @@
*/
#define HAS_LORA 1 // comment out if device shall not send data via LoRa
#define HAS_SPI 1 // comment out if device shall not send data via SPI
#define CFG_sx1276_radio 1 // HPD13A LoRa SoC
#define HAS_LED NOT_A_PIN // no usable LED on board
#define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature

View File

@ -87,9 +87,8 @@ void rgb_set_color(uint16_t hue) {}
#endif
#if (HAS_LED != NOT_A_PIN)
void switch_LED(uint8_t state) {
#if (HAS_LED != NOT_A_PIN)
if (state == LED_ON) {
// switch LED on
#ifdef LED_ACTIVE_LOW
@ -105,9 +104,8 @@ void switch_LED(uint8_t state) {
digitalWrite(HAS_LED, LOW);
#endif
}
}
#endif
}
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)

View File

@ -1,11 +1,11 @@
#ifdef HAS_LORA
// Basic Config
#include "lorawan.h"
// Local logging Tag
static const char TAG[] = "lora";
#ifdef HAS_LORA
osjob_t sendjob;
QueueHandle_t LoraSendQueue;
@ -329,9 +329,13 @@ void lora_send(osjob_t *job) {
} else {
if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) {
// SendBuffer gets struct MessageBuffer with next payload from queue
LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message,
SendBuffer.MessageSize, (cfg.countermode & 0x02));
ESP_LOGI(TAG, "%d bytes sent to LoRa", SendBuffer.MessageSize);
if (!LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message,
SendBuffer.MessageSize, (cfg.countermode & 0x02))) {
ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize);
} else {
ESP_LOGE(TAG, "could not send %d byte(s) to LoRa",
SendBuffer.MessageSize);
}
// sprintf(display_line7, "PACKET QUEUED");
}
}
@ -342,3 +346,71 @@ void lora_send(osjob_t *job) {
}
#endif // HAS_LORA
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
if (!LMIC_startJoining()) { // start joining
ESP_LOGI(TAG, "Already joined");
}
return ESP_OK; // continue main program
#endif
}
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

@ -55,7 +55,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
@ -160,51 +160,14 @@ 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
strcat_P(features, " SPI");
SPISendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (SPISendQueue == 0) {
ESP_LOGE(TAG, "Could not create SPI send queue. Aborting.");
exit(0);
} else
ESP_LOGI(TAG, "SPI send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
#endif
assert(spi_init() == ESP_OK);
#ifdef VENDORFILTER
strcat_P(features, " OUIFLT");
@ -312,17 +275,6 @@ void setup() {
1); // CPU core
#endif
#ifdef HAS_SPI
ESP_LOGI(TAG, "Starting SPIloop...");
xTaskCreatePinnedToCore(spi_loop, // task function
"spiloop", // name of task
2048, // stack size of task
(void *)1, // parameter of the task
2, // priority of the task
&SpiTask, // task handle
0); // CPU core
#endif
// start state machine
ESP_LOGI(TAG, "Starting IRQ Handler...");
xTaskCreatePinnedToCore(irqHandler, // task function

View File

@ -26,9 +26,6 @@ const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE);
// Connection port (HTTPS)
const int port = 443;
// Connection timeout
const uint32_t RESPONSE_TIMEOUT_MS = 5000;
// Variables to validate firmware content
int volatile contentLength = 0;
bool volatile isValidContentType = false;
@ -43,24 +40,13 @@ inline String getHeaderValue(String header, String headerName) {
void start_ota_update() {
/*
// check battery status if we can before doing ota
#ifdef HAS_BATTERY_PROBE
// check battery status if we can before doing ota
if (!batt_sufficient()) {
ESP_LOGW(TAG, "Battery voltage %dmV too low for OTA", batt_voltage);
ESP_LOGE(TAG, "Battery voltage %dmV too low for OTA", batt_voltage);
return;
}
#endif
*/
// turn on LED
#if (HAS_LED != NOT_A_PIN)
#ifdef LED_ACTIVE_LOW
digitalWrite(HAS_LED, LOW);
#else
digitalWrite(HAS_LED, HIGH);
#endif
#endif
switch_LED(LED_ON);
#ifdef HAS_DISPLAY
u8x8.begin();
@ -82,86 +68,94 @@ void start_ota_update() {
ESP_LOGI(TAG, "Starting Wifi OTA update");
display(1, "**", WIFI_SSID);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
int i = WIFI_MAX_TRY;
int i = WIFI_MAX_TRY, j = OTA_MAX_TRY;
int ret = 1; // 0 = finished, 1 = retry, -1 = abort
ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID);
while (i--) {
ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID);
if (WiFi.status() == WL_CONNECTED)
break;
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
if (i >= 0) {
if (WiFi.status() == WL_CONNECTED) {
// we now have wifi connection and try to do an OTA over wifi update
ESP_LOGI(TAG, "Connected to %s", WIFI_SSID);
display(1, "OK", "WiFi connected");
do_ota_update(); // gets and flashes new firmware
} else {
ESP_LOGI(TAG, "Could not connect to %s, rebooting.", WIFI_SSID);
display(1, " E", "no WiFi connect");
// do a number of tries to update firmware limited by OTA_MAX_TRY
while ((j--) && (ret > 0)) {
ESP_LOGI(TAG, "Starting OTA update, attempt %u of %u", OTA_MAX_TRY - j,
OTA_MAX_TRY);
ret = do_ota_update();
}
goto end;
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
WiFi.reconnect();
}
// wifi did not connect
ESP_LOGI(TAG, "Could not connect to %s", WIFI_SSID);
display(1, " E", "no WiFi connect");
vTaskDelay(5000 / portTICK_PERIOD_MS);
end:
switch_LED(LED_OFF);
ESP_LOGI(TAG, "Rebooting to %s firmware", (ret == 0) ? "new" : "current");
display(5, "**", ""); // mark line rebooting
// turn off LED
#if (HAS_LED != NOT_A_PIN)
#ifdef LED_ACTIVE_LOW
digitalWrite(HAS_LED, HIGH);
#else
digitalWrite(HAS_LED, LOW);
#endif
#endif
vTaskDelay(5000 / portTICK_PERIOD_MS);
ESP.restart();
} // start_ota_update
void do_ota_update() {
// Reads data vom wifi client and flashes it to ota partition
// returns: 0 = finished, 1 = retry, -1 = abort
int do_ota_update() {
char buf[17];
bool redirect = true;
size_t written = 0;
// Fetch the latest firmware version
ESP_LOGI(TAG, "Checking latest firmware version on server...");
ESP_LOGI(TAG, "Checking latest firmware version on server");
display(2, "**", "checking version");
const String latest = bintray.getLatestVersion();
if (latest.length() == 0) {
ESP_LOGI(
TAG,
"Could not load info about the latest firmware. Rebooting to runmode.");
ESP_LOGI(TAG, "Could not fetch info on latest firmware");
display(2, " E", "file not found");
return;
return -1;
} else if (version_compare(latest, cfg.version) <= 0) {
ESP_LOGI(TAG, "Current firmware is up to date. Rebooting to runmode.");
ESP_LOGI(TAG, "Current firmware is up to date");
display(2, "NO", "no update found");
return;
return -1;
}
ESP_LOGI(TAG, "New firmware version v%s available. Downloading...",
latest.c_str());
ESP_LOGI(TAG, "New firmware version v%s available", latest.c_str());
display(2, "OK", latest.c_str());
display(3, "**", "");
String firmwarePath = bintray.getBinaryPath(latest);
if (!firmwarePath.endsWith(".bin")) {
ESP_LOGI(TAG, "Unsupported binary format, OTA update cancelled.");
ESP_LOGI(TAG, "Unsupported binary format");
display(3, " E", "file type error");
return;
return -1;
}
String currentHost = bintray.getStorageHost();
String prevHost = currentHost;
WiFiClientSecure client;
client.setCACert(bintray.getCertificate(currentHost));
// client.setTimeout(RESPONSE_TIMEOUT_MS);
// --> causing error [E][WiFiClient.cpp:236] setSocketOption(): 1006 : 9
// so we unfortunately need patched update.cpp which sets the stream timeout
if (!client.connect(currentHost.c_str(), port)) {
ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str());
display(3, " E", "connection lost");
return;
goto abort;
}
bool redirect = true;
while (redirect) {
if (currentHost != prevHost) {
client.stop();
@ -170,7 +164,7 @@ void do_ota_update() {
ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s",
currentHost.c_str());
display(3, " E", "server error");
return;
goto abort;
}
}
@ -183,11 +177,10 @@ void do_ota_update() {
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > RESPONSE_TIMEOUT_MS) {
ESP_LOGI(TAG, "Client Timeout.");
if ((millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
ESP_LOGI(TAG, "Client timeout");
display(3, " E", "client timeout");
client.stop();
return;
goto abort;
}
}
@ -207,13 +200,12 @@ void do_ota_update() {
"firmware flashing");
redirect = false;
} else if (line.indexOf("302") > 0) {
ESP_LOGI(TAG, "Got 302 status code from server. Redirecting to the "
ESP_LOGI(TAG, "Got 302 status code from server. Redirecting to "
"new address");
redirect = true;
} else {
ESP_LOGI(TAG, "Could not get a valid firmware url.");
// Unexptected HTTP response. Retry or skip update?
redirect = false;
ESP_LOGI(TAG, "Could not get firmware download URL");
goto retry;
}
}
@ -242,76 +234,71 @@ void do_ota_update() {
isValidContentType = true;
}
}
}
}
} // while (client.available())
} // while (redirect)
display(3, "OK", ""); // line download
// check whether we have everything for OTA update
if (contentLength && isValidContentType) {
if (!(contentLength && isValidContentType)) {
ESP_LOGI(TAG, "Invalid OTA server response");
display(4, " E", "response error");
goto retry;
}
size_t written = 0;
#ifdef HAS_LED
#ifndef LED_ACTIVE_LOW
if (!Update.begin(contentLength, U_FLASH, HAS_LED, HIGH)) {
#else
if (!Update.begin(contentLength, U_FLASH, HAS_LED, LOW)) {
#endif
#else
if (!Update.begin(contentLength)) {
#endif
ESP_LOGI(TAG, "Not enough space to start OTA update");
display(4, " E", "disk full");
goto abort;
}
if (Update.begin(contentLength)) {
#ifdef HAS_DISPLAY
// register callback function for showing progress while streaming data
Update.onProgress(&show_progress);
#endif
int i = FLASH_MAX_TRY;
while ((i--) && (written != contentLength)) {
ESP_LOGI(TAG,
"Starting OTA update, attempt %u of %u. This will take some "
"time to complete...",
FLASH_MAX_TRY - i, FLASH_MAX_TRY);
display(4, "**", "writing...");
written = Update.writeStream(client);
written = Update.writeStream(client); // this is a blocking call
if (written == contentLength) {
ESP_LOGI(TAG, "Written %u bytes successfully", written);
snprintf(buf, 17, "%ukB Done!", (uint16_t)(written / 1024));
display(4, "OK", buf);
break;
} else {
ESP_LOGI(TAG,
"Written only %u of %u bytes, OTA update attempt cancelled.",
ESP_LOGI(TAG, "Written only %u of %u bytes, OTA update attempt cancelled",
written, contentLength);
}
}
if (Update.end()) {
if (Update.isFinished()) {
ESP_LOGI(
TAG,
"OTA update completed. Rebooting to runmode with new version.");
client.stop();
return;
goto finished;
} else {
ESP_LOGI(TAG, "Something went wrong! OTA update hasn't been finished "
"properly.");
}
} else {
ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError());
snprintf(buf, 17, "Error #: %d", Update.getError());
ESP_LOGI(TAG, "An error occurred. Error#: %d", Update.getError());
snprintf(buf, 17, "Error#: %d", Update.getError());
display(4, " E", buf);
goto retry;
}
} else {
ESP_LOGI(TAG, "There isn't enough space to start OTA update");
display(4, " E", "disk full");
client.flush();
}
} else {
ESP_LOGI(TAG,
"There was no valid content in the response from the OTA server!");
display(4, " E", "response error");
client.flush();
}
ESP_LOGI(TAG,
"OTA update failed. Rebooting to runmode with current version.");
finished:
client.stop();
ESP_LOGI(TAG, "OTA update finished");
return 0;
abort:
client.stop();
ESP_LOGI(TAG, "OTA update failed");
return -1;
retry:
return 1;
} // do_ota_update
void display(const uint8_t row, const std::string status,
@ -329,45 +316,29 @@ void display(const uint8_t row, const std::string status,
#ifdef HAS_DISPLAY
// callback function to show download progress while streaming data
void show_progress (unsigned long current, unsigned long size) {
void show_progress(unsigned long current, unsigned long size) {
char buf[17];
snprintf(buf, 17, "%-9lu (%3lu%%)", current, current * 100 / size);
display(4, "**", buf);
}
#endif
// helper function to compare two versions. Returns 1 if v2 is
// helper function to convert strings into lower case
bool comp(char s1, char s2) { return tolower(s1) < tolower(s2); }
// helper function to lexicographically compare two versions. Returns 1 if v2 is
// smaller, -1 if v1 is smaller, 0 if equal
int version_compare(const String v1, const String v2) {
// vnum stores each numeric part of version
int vnum1 = 0, vnum2 = 0;
// loop until both string are processed
for (int i = 0, j = 0; (i < v1.length() || j < v2.length());) {
// storing numeric part of version 1 in vnum1
while (i < v1.length() && v1[i] != '.') {
vnum1 = vnum1 * 10 + (v1[i] - '0');
i++;
}
// storing numeric part of version 2 in vnum2
while (j < v2.length() && v2[j] != '.') {
vnum2 = vnum2 * 10 + (v2[j] - '0');
j++;
}
if (vnum1 > vnum2)
return 1;
if (vnum2 > vnum1)
return -1;
// if equal, reset variables and go for next numeric
// part
vnum1 = vnum2 = 0;
i++;
j++;
}
if (v1 == v2)
return 0;
const char *a1 = v1.c_str(), *a2 = v2.c_str();
if (lexicographical_compare(a1, a1 + strlen(a1), a2, a2 + strlen(a2), comp))
return -1;
else
return 1;
}
#endif // USE_OTA

View File

@ -67,9 +67,10 @@
// OTA settings
#define USE_OTA 1 // Comment out to disable OTA update
#define WIFI_MAX_TRY 20 // maximum number of wifi connect attempts for OTA update [default = 20]
#define FLASH_MAX_TRY 3 // maximum number of attempts for writing update binary to flash [default = 3]
#define OTA_MIN_BATT 3700 // minimum battery level vor OTA [millivolt]
#define WIFI_MAX_TRY 5 // maximum number of wifi connect attempts for OTA update [default = 20]
#define OTA_MAX_TRY 5 // maximum number of attempts for OTA download and write to flash [default = 3]
#define OTA_MIN_BATT 3600 // minimum battery level for OTA [millivolt]
#define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds]
// LMIC settings
// moved to src/lmic_config.h

View File

@ -75,8 +75,8 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp,
buffer[cursor++] = (byte)(reset2);
}
#ifdef HAS_GPS
void PayloadConvert::addGPS(gpsStatus_t value) {
#ifdef HAS_GPS
buffer[cursor++] = (byte)((value.latitude & 0xFF000000) >> 24);
buffer[cursor++] = (byte)((value.latitude & 0x00FF0000) >> 16);
buffer[cursor++] = (byte)((value.latitude & 0x0000FF00) >> 8);
@ -90,14 +90,17 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
buffer[cursor++] = lowByte(value.hdop);
buffer[cursor++] = highByte(value.altitude);
buffer[cursor++] = lowByte(value.altitude);
#endif
}
#endif
void PayloadConvert::addButton(uint8_t value) {
#ifdef HAS_BUTTON
void PayloadConvert::addButton(uint8_t value) { buffer[cursor++] = value; }
buffer[cursor++] = value;
#endif
}
/* ---------------- packed format with LoRa serialization Encoder ---------- */
/* ---------------- packed format with LoRa serialization Encoder ----------
*/
// derived from
// https://github.com/thesolarnomad/lora-serialization/blob/master/src/LoraEncoder.cpp
@ -138,18 +141,20 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp,
writeUint8(reset2);
}
#ifdef HAS_GPS
void PayloadConvert::addGPS(gpsStatus_t value) {
#ifdef HAS_GPS
writeLatLng(value.latitude, value.longitude);
writeUint8(value.satellites);
writeUint16(value.hdop);
writeUint16(value.altitude);
#endif
}
#endif
void PayloadConvert::addButton(uint8_t value) {
#ifdef HAS_BUTTON
void PayloadConvert::addButton(uint8_t value) { writeUint8(value); }
writeUint8(value);
#endif
}
void PayloadConvert::intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) {
for (uint8_t x = 0; x < byteSize; x++) {
@ -162,7 +167,7 @@ void PayloadConvert::writeUptime(uint64_t uptime) {
intToBytes(cursor, uptime, 8);
}
void PayloadConvert::writeVersion(char * version) {
void PayloadConvert::writeVersion(char *version) {
memcpy(buffer + cursor, version, 10);
cursor += 10;
}
@ -273,8 +278,8 @@ void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float celsius,
buffer[cursor++] = lowByte(temp);
}
#ifdef HAS_GPS
void PayloadConvert::addGPS(gpsStatus_t value) {
#ifdef HAS_GPS
int32_t lat = value.latitude / 100;
int32_t lon = value.longitude / 100;
int32_t alt = value.altitude * 100;
@ -291,18 +296,18 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
buffer[cursor++] = (byte)((alt & 0xFF0000) >> 16);
buffer[cursor++] = (byte)((alt & 0x00FF00) >> 8);
buffer[cursor++] = (byte)((alt & 0x0000FF));
}
#endif
}
#ifdef HAS_BUTTON
void PayloadConvert::addButton(uint8_t value) {
#ifdef HAS_BUTTON
#if (PAYLOAD_ENCODER == 3)
buffer[cursor++] = LPP_BUTTON_CHANNEL;
#endif
buffer[cursor++] = LPP_DIGITAL_INPUT;
buffer[cursor++] = value;
}
#endif
}
#else
#error "No valid payload converter defined"

View File

@ -1,5 +1,5 @@
// Basic Config
#include "globals.h"
#include "senddata.h"
// put data to send in RTos Queues used for transmit over channels Lora and SPI
void SendData(uint8_t port) {
@ -12,19 +12,9 @@ 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 SPI send queue
#ifdef HAS_SPI
if (xQueueSendToBack(SPISendQueue, (void *)&SendBuffer, (TickType_t)0) ==
pdTRUE)
ESP_LOGI(TAG, "%d bytes enqueued to send on SPI", 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
if ((port == COUNTERPORT) && (cfg.countermode != 1)) {
@ -63,10 +53,6 @@ void sendPayload() {
} // sendpayload()
void flushQueues() {
#ifdef HAS_LORA
xQueueReset(LoraSendQueue);
#endif
#ifdef HAS_SPI
xQueueReset(SPISendQueue);
#endif
lora_queuereset();
spi_queuereset();
}

View File

@ -1,72 +0,0 @@
#ifdef HAS_SPI
#include "spisend.h"
// 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) {
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);
}
// 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(uint8_t port, uint8_t *buf, int len, uint8_t is_read) {
SPISettings settings(1E6, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(settings);
digitalWrite(SS, 0);
SPI.transfer(port);
for (uint8_t i = 0; i < len; i++) {
uint8_t *p = buf + i;
uint8_t data = is_read ? 0x00 : *p;
data = SPI.transfer(data);
if (is_read)
*p = data;
}
digitalWrite(SS, 1);
SPI.endTransaction();
}
void hal_spi_write(uint8_t port, const uint8_t *buf, int len) {
hal_spi_trx(port, (uint8_t *)buf, len, 0);
}
void hal_spi_read(uint8_t port, uint8_t *buf, int len) {
hal_spi_trx(port, buf, len, 1);
}
#endif // HAS_SPI

169
src/spislave.cpp Normal file
View File

@ -0,0 +1,169 @@
/*
//////////////////////// ESP32-Paxcounter \\\\\\\\\\\\\\\\\\\\\\\\\\
Copyright 2018 Christian Ambach <christian.ambach@deutschebahn.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
NOTICE:
Parts of the source files in this repository are made available under different
licenses. Refer to LICENSE.txt file in repository for more details.
*/
#include "spislave.h"
#include <driver/spi_slave.h>
#include <sys/param.h>
#include <rom/crc.h>
static const char TAG[] = __FILE__;
#define HEADER_SIZE 4
// SPI transaction size needs to be at least 8 bytes and dividable by 4, see
// https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/spi_slave.html
#define BUFFER_SIZE \
(MAX(8, HEADER_SIZE + PAYLOAD_BUFFER_SIZE) + \
(4 - MAX(8, HEADER_SIZE + PAYLOAD_BUFFER_SIZE) % 4))
DMA_ATTR uint8_t txbuf[BUFFER_SIZE];
DMA_ATTR uint8_t rxbuf[BUFFER_SIZE];
QueueHandle_t SPISendQueue;
TaskHandle_t spiTask;
void spi_slave_task(void *param) {
while (1) {
MessageBuffer_t msg;
size_t transaction_size;
// clear rx + tx buffers
memset(txbuf, 0, sizeof(txbuf));
memset(rxbuf, 0, sizeof(rxbuf));
// wait until data to send arrivey
if (xQueueReceive(SPISendQueue, &msg, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!");
continue;
}
// fill tx buffer with data to send from queue and calculate crc16 cheksum
uint8_t *messageType = txbuf + 2;
*messageType = msg.MessagePort;
uint8_t *messageSize = txbuf + 3;
*messageSize = msg.MessageSize;
memcpy(txbuf + HEADER_SIZE, &msg.Message, msg.MessageSize);
// calculate crc16 checksum, not used yet
// uint16_t *crc = (uint16_t *)txbuf;
//*crc = crc16_be(0, messageType, msg.MessageSize + HEADER_SIZE - 2);
// set length for spi slave driver
transaction_size = HEADER_SIZE + msg.MessageSize;
transaction_size += (4 - transaction_size % 4);
// prepare spi transaction
spi_slave_transaction_t spi_transaction = {0};
spi_transaction.length = transaction_size * 8;
spi_transaction.tx_buffer = txbuf;
spi_transaction.rx_buffer = rxbuf;
// 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_LOG_BUFFER_HEXDUMP(TAG, txbuf, transaction_size, ESP_LOG_DEBUG);
esp_err_t ret =
spi_slave_transmit(HSPI_HOST, &spi_transaction, portMAX_DELAY);
ESP_LOG_BUFFER_HEXDUMP(TAG, rxbuf, transaction_size, ESP_LOG_DEBUG);
ESP_LOGI(TAG, "Transaction finished with size %zu bits",
spi_transaction.trans_len);
// check if command was received, then call interpreter with command payload
if ((spi_transaction.trans_len) && ((rxbuf[2]) == RCMDPORT)) {
rcommand(rxbuf + HEADER_SIZE, spi_transaction.trans_len - HEADER_SIZE);
};
}
}
esp_err_t spi_init() {
#ifndef HAS_SPI
return ESP_OK;
#else
SPISendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (SPISendQueue == 0) {
ESP_LOGE(TAG, "Could not create SPI send queue. Aborting.");
return ESP_FAIL;
}
ESP_LOGI(TAG, "SPI send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
spi_bus_config_t spi_bus_cfg = {.mosi_io_num = SPI_MOSI,
.miso_io_num = SPI_MISO,
.sclk_io_num = SPI_SCLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
.flags = 0};
spi_slave_interface_config_t spi_slv_cfg = {.spics_io_num = SPI_CS,
.flags = 0,
.queue_size = 1,
.mode = 0,
.post_setup_cb = NULL,
.post_trans_cb = NULL};
// Enable pull-ups on SPI lines so we don't detect rogue pulses when no master
// is connected
gpio_set_pull_mode(SPI_MOSI, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(SPI_SCLK, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(SPI_CS, GPIO_PULLUP_ONLY);
esp_err_t ret =
spi_slave_initialize(HSPI_HOST, &spi_bus_cfg, &spi_slv_cfg, 1);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Starting SPIloop...");
xTaskCreate(spi_slave_task, "spiloop", 4096, (void *)NULL, 2, &spiTask);
} else {
ESP_LOGE(TAG, "SPI interface initialization failed");
}
return ret;
#endif
}
void spi_enqueuedata(uint8_t messageType, MessageBuffer_t *message) {
// enqueue message in SPI send queue
#ifdef HAS_SPI
BaseType_t ret =
xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0);
if (ret == pdTRUE) {
ESP_LOGI(TAG, "%d byte(s) enqueued for SPI interface", message->MessageSize);
} else {
ESP_LOGW(TAG, "SPI sendqueue is full");
}
#endif
}
void spi_queuereset(void) {
#ifdef HAS_SPI
xQueueReset(SPISendQueue);
#endif
}
void spi_housekeeping(void) {
#ifdef HAS_SPI
ESP_LOGD(TAG, "spiloop %d bytes left", uxTaskGetStackHighWaterMark(spiTask));
#endif
}

View File

@ -1,11 +1,9 @@
/*
this file copied from esp32-arduino library and patched, see PR
https://github.com/espressif/arduino-esp32/pull/1886
https://github.com/espressif/arduino-esp32/pull/1979
*/
#include "update.h"
#include "Update.h"
#include "Arduino.h"
#include "esp_spi_flash.h"
#include "esp_ota_ops.h"
@ -95,6 +93,10 @@ void UpdateClass::_reset() {
_progress = 0;
_size = 0;
_command = U_FLASH;
if(_ledPin != -1) {
digitalWrite(_ledPin, !_ledOn); // off
}
}
bool UpdateClass::canRollBack(){
@ -113,12 +115,15 @@ bool UpdateClass::rollBack(){
return _partitionIsBootable(partition) && !esp_ota_set_boot_partition(partition);
}
bool UpdateClass::begin(size_t size, int command) {
bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
if(_size > 0){
log_w("already running");
return false;
}
_ledPin = ledPin;
_ledOn = !!ledOn; // 0(LOW) or 1(HIGH)
_reset();
_error = 0;
@ -310,7 +315,7 @@ size_t UpdateClass::write(uint8_t *data, size_t len) {
}
size_t UpdateClass::writeStream(Stream &data) {
data.setTimeout(20000);
data.setTimeout(RESPONSE_TIMEOUT_MS);
size_t written = 0;
size_t toRead = 0;
if(hasError() || !isRunning())
@ -323,16 +328,32 @@ size_t UpdateClass::writeStream(Stream &data) {
if (_progress_callback) {
_progress_callback(0, _size);
}
if(_ledPin != -1) {
pinMode(_ledPin, OUTPUT);
}
while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
if(_ledPin != -1) {
digitalWrite(_ledPin, _ledOn); // Switch LED on
}
size_t bytesToRead = SPI_FLASH_SEC_SIZE - _bufferLen;
if(bytesToRead > remaining()) {
bytesToRead = remaining();
}
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) { //Timeout
delay(100);
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) { //Timeout
_abort(UPDATE_ERROR_STREAM);
return written;
}
}
if(_ledPin != -1) {
digitalWrite(_ledPin, !_ledOn); // Switch LED off
}
_bufferLen += toRead;
if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer())
return written;