From 59d08ea21ef2437ff0758c416b0f1fda5517230e Mon Sep 17 00:00:00 2001 From: atc1441 Date: Tue, 9 Jan 2024 22:48:18 +0100 Subject: [PATCH] Update of the ESP32-C6 SubGHz handling, working Beta --- .../Arduino_OpenEPaperLink_C6_AP.ino | 39 ++++++----- .../Arduino_OpenEPaperLink_C6_AP/radio.cpp | 51 ++++++++------ .../second_uart.cpp | 7 +- .../Arduino_OpenEPaperLink_C6_AP/subGhz.cpp | 70 ++++++++++--------- .../Arduino_OpenEPaperLink_C6_AP/subGhz.h | 1 - 5 files changed, 93 insertions(+), 75 deletions(-) diff --git a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/Arduino_OpenEPaperLink_C6_AP.ino b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/Arduino_OpenEPaperLink_C6_AP.ino index d29b685a..3ef836e8 100644 --- a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/Arduino_OpenEPaperLink_C6_AP.ino +++ b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/Arduino_OpenEPaperLink_C6_AP.ino @@ -6,6 +6,7 @@ #include "subGhz.h" #include "driver/gpio.h" #include "driver/uart.h" +#include "esp_log.h" #include "esp_err.h" #include "esp_event.h" #include "esp_ieee802154.h" @@ -25,6 +26,8 @@ #include #include +static const char *TAG = "MAIN"; + const uint8_t channelList[6] = { 11, 15, 20, 25, 26, 27 }; #define DATATYPE_NOUPDATE 0 @@ -36,7 +39,7 @@ const uint8_t channelList[6] = { 11, 15, 20, 25, 26, 27 }; struct pendingData pendingDataArr[MAX_PENDING_MACS]; // VERSION GOES HERE! -uint16_t version = 0x0019; +uint16_t version = 0x001A; #define RAW_PKT_PADDING 2 @@ -195,7 +198,7 @@ void processSerial(uint8_t lastchar) { static uint32_t blockStartTime = 0; if ((RXState != ZBS_RX_WAIT_HEADER) && ((millis() - lastSerial) > 1000)) { RXState = ZBS_RX_WAIT_HEADER; - Serial.printf("UART Timeout\r\n"); + ESP_LOGI(TAG, "UART Timeout"); } lastSerial = millis(); switch (RXState) { @@ -209,27 +212,27 @@ void processSerial(uint8_t lastchar) { if (isSame(cmdbuffer + 1, ">D>", 3)) { pr("ACK>"); blockStartTime = millis(); - Serial.printf("Starting BlkData, %lu ms after request\r\n", blockStartTime - nextBlockAttempt); + ESP_LOGI(TAG, "Starting BlkData, %lu ms after request", blockStartTime - nextBlockAttempt ); blockPosition = 0; RXState = ZBS_RX_WAIT_BLOCKDATA; } if (isSame(cmdbuffer, "SDA>", 4)) { - Serial.printf("SDA In\r\n"); + ESP_LOGI(TAG, "SDA In"); RXState = ZBS_RX_WAIT_SDA; bytesRemain = sizeof(struct pendingData); serialbufferp = serialbuffer; break; } if (isSame(cmdbuffer, "CXD>", 4)) { - Serial.printf("CXD In\r\n"); + ESP_LOGI(TAG, "CXD In"); RXState = ZBS_RX_WAIT_CANCEL; bytesRemain = sizeof(struct pendingData); serialbufferp = serialbuffer; break; } if (isSame(cmdbuffer, "SCP>", 4)) { - Serial.printf("SCP In\r\n"); + ESP_LOGI(TAG, "SCP In"); RXState = ZBS_RX_WAIT_SCP; bytesRemain = sizeof(struct espSetChannelPower); serialbufferp = serialbuffer; @@ -237,25 +240,25 @@ void processSerial(uint8_t lastchar) { } if (isSame(cmdbuffer, "NFO?", 4)) { pr("ACK>"); - Serial.printf("NFO? In\r\n"); + ESP_LOGI(TAG, "NFO? In"); espNotifyAPInfo(); RXState = ZBS_RX_WAIT_HEADER; } if (isSame(cmdbuffer, "RDY?", 4)) { pr("ACK>"); - Serial.printf("RDY? In\r\n"); + ESP_LOGI(TAG, "RDY? In"); RXState = ZBS_RX_WAIT_HEADER; } if (isSame(cmdbuffer, "RSET", 4)) { pr("ACK>"); - Serial.printf("RSET In\r\n"); + ESP_LOGI(TAG, "RSET In"); delay(100); // TODO RESET US HERE RXState = ZBS_RX_WAIT_HEADER; } if (isSame(cmdbuffer, "HSPD", 4)) { pr("ACK>"); - Serial.printf("HSPD In, switching to 2000000\r\n"); + ESP_LOGI(TAG, "HSPD In, switching to 2000000"); delay(100); uart_switch_speed(2000000); delay(100); @@ -267,7 +270,7 @@ void processSerial(uint8_t lastchar) { case ZBS_RX_WAIT_BLOCKDATA: blockbuffer[blockPosition++] = 0xAA ^ lastchar; if (blockPosition >= 4100) { - Serial.printf("Blockdata fully received in %lu ms, %lu ms after the request\r\n", millis() - blockStartTime, millis() - nextBlockAttempt); + ESP_LOGI(TAG, "Blockdata fully received in %lu ms, %lu ms after the request", millis() - blockStartTime, millis() - nextBlockAttempt); RXState = ZBS_RX_WAIT_HEADER; } break; @@ -329,7 +332,7 @@ SCPchannelFound: } curPower = scp->power; radioSetTxPower(scp->power); - Serial.printf("Set channel: %d power: %d\r\n", curChannel, curPower); + ESP_LOGI(TAG, "Set channel: %d power: %d", curChannel, curPower); } else { SCPfailed: pr("NOK>"); @@ -693,8 +696,7 @@ void sendPong(void *buf, bool isSubGHz) { extern uint8_t mSelfMac[8]; void setup() { - Serial.begin(115200); - +//Serial.begin(115200); init_led(); init_second_uart(); @@ -707,7 +709,7 @@ void setup() { pr("RES>"); pr("RDY>"); - Serial.printf("C6 ready!\r\n"); + ESP_LOGI(TAG, "C6 ready!"); housekeepingTimer = millis(); } @@ -726,6 +728,11 @@ void loop() { Serial.printf("\n"); } + if (isSubGhzRx) { + ESP_LOGD(TAG, "RXed packet %s len %u", isSubGhzRx ? "subGhz" : "2.4Ghz", ret); + ESP_LOGD(TAG, "First bytes: %02X %02X %02X %02X %02X", radiorxbuffer[0], radiorxbuffer[1], radiorxbuffer[2], radiorxbuffer[3], radiorxbuffer[4]); + } + led_flash(0); // received a packet, lets see what it is switch (getPacketType(radiorxbuffer)) { @@ -765,7 +772,7 @@ void loop() { processTagReturnData(radiorxbuffer, ret, isSubGhzRx); break; default: - Serial.printf("t=%02X\r\n", getPacketType(radiorxbuffer)); + ESP_LOGI(TAG, "t=%02X" , getPacketType(radiorxbuffer)); break; } } else if (blockStartTimer == 0) { diff --git a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/radio.cpp b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/radio.cpp index a72da29d..1c26ac73 100644 --- a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/radio.cpp +++ b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/radio.cpp @@ -1,6 +1,7 @@ #include "radio.h" #include "driver/gpio.h" #include "driver/uart.h" +#include "esp_log.h" #include "esp_err.h" #include "esp_event.h" #include "esp_ieee802154.h" @@ -23,6 +24,8 @@ #include #include +static const char *TAG = "RADIO"; + bool has_sub_ghz = false; uint8_t mSelfMac[8]; @@ -30,7 +33,7 @@ volatile uint8_t isInTransmit = 0; QueueHandle_t packet_buffer = NULL; void esp_ieee802154_receive_done(uint8_t *frame, esp_ieee802154_frame_info_t *frame_info) { - Serial.printf("RADIO info RX %d\r\n", frame[0]); + ESP_LOGI(TAG, "RX %d", frame[0]); BaseType_t xHigherPriorityTaskWoken; static uint8_t inner_rxPKT[130]; memcpy(inner_rxPKT, &frame[0], frame[0] + 1); @@ -40,12 +43,12 @@ void esp_ieee802154_receive_done(uint8_t *frame, esp_ieee802154_frame_info_t *fr void esp_ieee802154_transmit_failed(const uint8_t *frame, esp_ieee802154_tx_error_t error) { isInTransmit = 0; - Serial.printf("RADIO err TX Err: %d\r\n", error); + ESP_LOGE(TAG, "TX Err: %d", error); } void esp_ieee802154_transmit_done(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info) { isInTransmit = 0; - Serial.printf("RADIO info TX %d\r\n", frame[0]); + ESP_LOGI(TAG, "TX %d", frame[0]); } void radio_init(uint8_t ch) { @@ -81,26 +84,30 @@ void radio_init(uint8_t ch) { delay(100); led_flash(0); - Serial.printf("RADIO Receiver ready, panId=0x%04x, channel=%d, long=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, short=%04x\r\n", - esp_ieee802154_get_panid(), esp_ieee802154_get_channel(), - mSelfMac[0], mSelfMac[1], mSelfMac[2], mSelfMac[3], - mSelfMac[4], mSelfMac[5], mSelfMac[6], mSelfMac[7], - esp_ieee802154_get_short_address()); + ESP_LOGI(TAG, "Receiver ready, panId=0x%04x, channel=%d, long=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, short=%04x", + esp_ieee802154_get_panid(), esp_ieee802154_get_channel(), + mSelfMac[0], mSelfMac[1], mSelfMac[2], mSelfMac[3], + mSelfMac[4], mSelfMac[5], mSelfMac[6], mSelfMac[7], + esp_ieee802154_get_short_address()); - // Lets here take care of the SubGhz Init - if (!init_subGhz()) - Serial.printf("Sub-GHz radio init failed\r\n"); - else if (!tiRadioSetChannel(ch)) - Serial.printf("SubGHz radio channel fail\r\n"); - else - has_sub_ghz = true; + if (has_sub_ghz) {// If radio is already working why activate it again^^ + if (!tiRadioSetChannel(ch)) + ESP_LOGI(TAG, "SubGHz radio channel fail"); + } else { + // Lets here take care of the SubGhz Init + if (!init_subGhz()) + ESP_LOGI(TAG, "Sub-GHz radio init failed"); + else if (!tiRadioSetChannel(ch)) + ESP_LOGI(TAG, "SubGHz radio channel fail"); + else + has_sub_ghz = true; - Serial.printf("SubGhz %s\r\n", has_sub_ghz ? "Active" : "Not Found"); - if (has_sub_ghz) { - tiRadioRxFilterCfg(mSelfMac, SHORT_MAC_UNUSED, PROTO_PAN_ID, true); - tiRadioTxConfigure(mSelfMac, SHORT_MAC_UNUSED, PROTO_PAN_ID); - tiRadioRxEnable(true, false); + ESP_LOGI(TAG, "SubGhz %s", has_sub_ghz ? "Active" : "Not Found"); + if (has_sub_ghz) { + tiRadioRxFilterCfg(mSelfMac, SHORT_MAC_UNUSED, PROTO_PAN_ID, false); + tiRadioRxEnable(true, false); + } } } @@ -125,8 +132,6 @@ bool radioTx(uint8_t *packet, bool subGhz) { void radioSetChannel(uint8_t ch) { radio_init(ch); - if (has_sub_ghz) - tiRadioSetChannel(ch); } void radioSetTxPower(uint8_t power) {} @@ -145,7 +150,7 @@ int8_t commsRxUnencrypted(uint8_t *data, bool *subGhzRx) { int32_t ret_sub_rx_len = tiRadioRxDequeuePkt(inner_rxPKT_out, sizeof(inner_rxPKT_out), &rssi_sub_rx, &lqi_sub_rx); if (ret_sub_rx_len > 0) { - //Serial.printf("Got Sub Ghz Len %i data: %i %u\r\n", ret_sub_rx, rssi_sub_rx, lqi_sub_rx); + ESP_LOGD(TAG, "Got Sub Ghz Len %i data: %i %u", ret_sub_rx_len, rssi_sub_rx, lqi_sub_rx); memcpy(data, inner_rxPKT_out, ret_sub_rx_len); *subGhzRx = true; // This is SubGHz data return ret_sub_rx_len; diff --git a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/second_uart.cpp b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/second_uart.cpp index a284be85..5e8567d5 100644 --- a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/second_uart.cpp +++ b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/second_uart.cpp @@ -5,6 +5,7 @@ #include #include "driver/gpio.h" #include "driver/uart.h" +#include "esp_log.h" #include "esp_err.h" #include "esp_event.h" #include "esp_ieee802154.h" @@ -21,6 +22,8 @@ #include "soc/lp_uart_reg.h" #include "second_uart.h" +static const char *TAG = "SECOND_UART"; + #if defined(CONFIG_OEPL_HARDWARE_PROFILE_POE_AP) #define CONFIG_OEPL_HARDWARE_UART_TX 5 #define CONFIG_OEPL_HARDWARE_UART_RX 18 @@ -100,7 +103,7 @@ static void uart_event_task(void *pvParameters) { } break; default: - Serial.printf("Second UART uart event type: %d\r\n", event.type); + // ESP_LOGI(TAG, "uart event type: %d", event.type); break; } } @@ -122,4 +125,4 @@ void uart_printf(const char *format, ...) { if (len > 0) { uart_write_bytes(1, buffer, len); } -} \ No newline at end of file +} diff --git a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.cpp b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.cpp index 64f2516e..17ba1fca 100644 --- a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.cpp +++ b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.cpp @@ -5,6 +5,7 @@ #include "subGhz.h" #include "driver/gpio.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/task.h" @@ -17,8 +18,10 @@ #include #include +static const char *TAG = "subGHz"; + #define SUB_GHZ_CH_OFST 11 -#define SUB_GHZ_NUM_CHANNELS 25 +#define SUB_GHZ_NUM_CHANNELS 27 /* //we configure GDO_2 is for TX.has_fifo_space @@ -118,10 +121,10 @@ static volatile uint8_t mRxBufs[MAX_RX_PKTS][RADIO_MAX_PACKET_LEN + 1 /* length */ + 2 /* RSSI, LQI/STA */]; static volatile uint8_t mRxNextWrite, mRxNextRead, mRxNumFree, mRxNumGot; -static uint8_t mRxFilterLongMac[8], mTxLongMac[8]; -static uint32_t mRxFilterShortMac, mTxShortMac; +static uint8_t mRxFilterLongMac[8]; +static uint32_t mRxFilterShortMac; static bool mRxEnabled, mAutoAck, mPromisc; -static uint16_t mRxFilterPan, mTxPan; +static uint16_t mRxFilterPan; static volatile int16_t mLastAck; struct MacHeaderGenericAddr { @@ -144,13 +147,6 @@ struct MacHeaderLongAddr { } __attribute__((packed)); - -void tiRadioTxConfigure(const uint8_t *myLongMac, uint32_t myShortMac, uint16_t pan) { - memcpy(mTxLongMac, myLongMac, sizeof(mTxLongMac)); - mTxShortMac = myShortMac; - mTxPan = pan; -} - void tiRadioRxFilterCfg(const uint8_t *myMac, uint32_t myShortMac, uint16_t myPan, bool promisc) { mPromisc = promisc; mRxFilterShortMac = myShortMac; @@ -319,7 +315,7 @@ static void tiRadioPrvPacketRx(void) { if (!now && !--nWaitCycles) { tiRadioPrvDeselect(); - //Serial.printf(" !!! RX timeout !!! \r\n"); + ESP_LOGI(TAG, " !!! RX timeout !!! "); goto fail; } @@ -412,7 +408,7 @@ out: state = tiRadioPrvGetState(); if (!--maxWait) { - //Serial.printf("too long wait for rx state. state is %d\n", state); + //ESP_LOGI(TAG, "too long wait for rx state. state is %d", state); break; } @@ -490,15 +486,15 @@ static void tiRadioPrvIfInit(void) { mRxNumFree = MAX_RX_PKTS; } -static void tiRadioPrvIrqInit(void) { - attachInterrupt(sub_GD0, data_input_interrupt, RISING); -} - bool tiRadioSetChannel(uint_fast8_t channel) { channel -= SUB_GHZ_CH_OFST; - if (channel >= SUB_GHZ_NUM_CHANNELS) + if (channel >= SUB_GHZ_NUM_CHANNELS) { + ESP_LOGI(TAG, "CH not set: %i", channel); return false; + } + ESP_LOGI(TAG, "CH: %i", channel); + delayMicroseconds(1000); return tiRadioPrvRegWrite(REG_CHANNR, channel * 3); } @@ -507,13 +503,8 @@ bool tiRadioTxLL(const void *pkt) { uint32_t len = 1 + *data, now; bool ret = false; - if (0) { - Serial.printf("TX packet len %u :", len); - for (int t = 0; t < len; t++) { - Serial.printf(" %02x", data[1 + t]); - } - Serial.printf("\n"); - } + ESP_LOGD(TAG, "TX packet len %u", len); + ESP_LOGD(TAG, "First bytes: %02X %02X %02X %02X %02X", data[1], data[2], data[3], data[4], data[5]); if (tiRadioPrvStrobe(CMD_SIDLE) < 0) goto out; @@ -558,6 +549,8 @@ out: return ret; } + +bool interrupt_attached = false; bool init_subGhz(void) { uint8_t regsCfg[] = { /* 0x00 */ 0x02, 0x2e, 0x01, 0x07, 0xd3, 0x91, 0x7f, 0x04, @@ -570,40 +563,51 @@ bool init_subGhz(void) { uint8_t paTab[] = { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0 }; + if (interrupt_attached) + detachInterrupt(sub_GD0); + tiRadioPrvIfInit(); if (tiRadioPrvRegRead(REG_PARTNUM) != 0x00) { - Serial.printf("partnum is wrong\n"); + ESP_LOGE(TAG, "partnum is wrong"); return false; } + delayMicroseconds(300); + if (tiRadioPrvStrobe(CMD_SRES) < 0) { - Serial.printf("res reply\n"); + ESP_LOGE(TAG, "res reply"); return false; } delayMicroseconds(300); if (tiRadioPrvStrobe(CMD_SIDLE) != 0x0f) { - Serial.printf("idle reply\n"); + ESP_LOGE(TAG, "idle reply"); return false; } + delayMicroseconds(300); + if (!tiRadioPrvRegWriteLong(0, regsCfg, sizeof(regsCfg))) { - Serial.printf("config issue\n"); + ESP_LOGE(TAG, "config issue"); return false; } + delayMicroseconds(300); + if (!tiRadioPrvRegWriteLong(REG_PATABLE, paTab, sizeof(paTab))) { - Serial.printf("PAtable issue\n"); + ESP_LOGE(TAG, "PAtable issue"); return false; } - tiRadioPrvIrqInit(); + attachInterrupt(sub_GD0, data_input_interrupt, RISING); + interrupt_attached = true; - Serial.printf("Sub-GHz radio inited\n"); + ESP_LOGI(TAG, "Sub-GHz radio inited"); tiRadioRxEnable(true, false); - Serial.printf("Sub-GHz rx is on\n"); + delayMicroseconds(1000); + ESP_LOGI(TAG, "Sub-GHz rx is on"); return true; } diff --git a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.h b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.h index 1b08ad79..149c40f0 100644 --- a/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.h +++ b/ARM_Tag_FW/Arduino_OpenEPaperLink_C6_AP/subGhz.h @@ -7,7 +7,6 @@ #include #include -void tiRadioTxConfigure(const uint8_t *myLongMac, uint32_t myShortMac, uint16_t pan); void tiRadioRxFilterCfg(const uint8_t *myMac, uint32_t myShortMac, uint16_t myPan, bool promisc); static bool tiRadioPrvSelect(void); static void tiRadioPrvDeselect(void);