diff --git a/platformio.ini b/platformio.ini index ccdc695b..23291c06 100644 --- a/platformio.ini +++ b/platformio.ini @@ -6,7 +6,7 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -env_default = generic +;env_default = generic ;env_default = ebox ;env_default = eboxtube ;env_default = heltec @@ -17,7 +17,7 @@ env_default = generic ;env_default = ttgov21new ;env_default = ttgobeam ;env_default = lopy -;env_default = lopy4 +env_default = lopy4 ;env_default = fipy ;env_default = lolin32litelora ;env_default = lolin32lora diff --git a/src/hal/lopy.h b/src/hal/lopy.h index 1f061d39..feaf9024 100644 --- a/src/hal/lopy.h +++ b/src/hal/lopy.h @@ -25,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 diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 9974663d..b9c49623 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -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 bytes sent to LoRa", SendBuffer.MessageSize); + } else { + ESP_LOGE(TAG, "coult not send %d bytes to LoRa", + SendBuffer.MessageSize); + } // sprintf(display_line7, "PACKET QUEUED"); } } @@ -341,6 +345,8 @@ void lora_send(osjob_t *job) { lora_send); } +#endif // HAS_LORA + esp_err_t lora_stack_init() { #ifndef HAS_LORA return ESP_OK; // continue main program @@ -375,21 +381,21 @@ esp_err_t lora_stack_init() { LMIC_selectSubBand(1); #endif - LMIC_startJoining(); // start joining - return ESP_OK; // continue main program + if (!LMIC_startJoining()) { // start joining + ESP_LOGI(TAG, "Already joined"); + } + + 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); + ESP_LOGI(TAG, "%d bytes enqueued for LORA interface", message->MessageSize); } else { ESP_LOGW(TAG, "LORA sendqueue is full"); } diff --git a/src/spislave.cpp b/src/spislave.cpp index c6268d81..7657a924 100644 --- a/src/spislave.cpp +++ b/src/spislave.cpp @@ -48,31 +48,37 @@ void spi_slave_task(void *param) { 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); - uint16_t *crc = (uint16_t *)txbuf; - *crc = crc16_be(0, messageType, msg.MessageSize + HEADER_SIZE - 2); - + // 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 bytes", transaction_size); ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, transaction_size, ESP_LOG_DEBUG); esp_err_t ret = @@ -80,6 +86,11 @@ void spi_slave_task(void *param) { 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); + }; } } @@ -111,6 +122,8 @@ esp_err_t spi_init() { .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); @@ -131,8 +144,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 interface", - message->MessageSize); + ESP_LOGI(TAG, "%d bytes enqueued for SPI interface", message->MessageSize); } else { ESP_LOGW(TAG, "SPI sendqueue is full"); }