From aa322397408c4cd04b4da498666ed77d53a82c38 Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 12 Feb 2023 06:53:41 +0100 Subject: [PATCH] a liitle more stable --- firmware/.idea/.gitignore | 6 -- firmware/.idea/firmware.iml | 9 -- firmware/bus.cpp | 117 +++++++++++++++++++++++++ firmware/bus.h | 30 +++++++ firmware/fiatlux.c | 5 ++ firmware/log.cpp | 2 +- firmware/lux.cpp | 111 ++++++----------------- firmware/lux.h | 4 - firmware/wifi.cpp | 68 +++++++------- peripherals/cc48x6/firmware/Makefile | 4 +- peripherals/cc48x6/firmware/Src/main.c | 1 - 11 files changed, 221 insertions(+), 136 deletions(-) delete mode 100644 firmware/.idea/.gitignore delete mode 100644 firmware/.idea/firmware.iml create mode 100644 firmware/bus.cpp create mode 100644 firmware/bus.h diff --git a/firmware/.idea/.gitignore b/firmware/.idea/.gitignore deleted file mode 100644 index 8bf4d45..0000000 --- a/firmware/.idea/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/firmware/.idea/firmware.iml b/firmware/.idea/firmware.iml deleted file mode 100644 index 190a5aa..0000000 --- a/firmware/.idea/firmware.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/firmware/bus.cpp b/firmware/bus.cpp new file mode 100644 index 0000000..1b7866f --- /dev/null +++ b/firmware/bus.cpp @@ -0,0 +1,117 @@ +#include "bus.h" +#include "esp/gpio.h" + +#include +#include "log.h" + +uint32_t spi_transfer_32_duplex(uint8_t bus, uint32_t val) { + uint32_t out = val; + uint32_t in; + spi_transfer(bus, &out, &in, 1, SPI_32BIT); + return in; +} + +uint16_t spi_transfer_16_duplex(uint8_t bus, uint32_t val) { + uint16_t out = val; + uint16_t in; + spi_transfer(bus, &out, &in, 1, SPI_16BIT); + return in; +} + +void reset_bus() { + gpio_write(4, false); + for (volatile int k = 0; k < 128; ++k); + gpio_write(4, true); +} + +void bus_init() { + gpio_write(4, false); + for (volatile int k = 0; k < 512 * 2; ++k); + gpio_write(4, true); + gpio_enable(4, GPIO_OUT_OPEN_DRAIN); + gpio_write(4, true); + for (volatile int k = 0; k < 512 * 4; ++k); + + spi_init(1, SPI_MODE0, SPI_FREQ_DIV_2M, true, SPI_BIG_ENDIAN, false); +} + +uint16_t bus_transfer_msg(uint8_t addr, uint16_t dat) { + union { + struct { + unsigned dat: 12; + unsigned addr: 4; + } __attribute__((packed)); + uint16_t _; + } frame = {._=0}; + + frame.addr = addr; + frame.dat = dat; + spi_transfer_16_duplex(1, frame._); + for (volatile int k = 0; k < 512 * 2; ++k); + uint16_t reply = spi_transfer_16_duplex(1, 0x0000); + for (volatile int k = 0; k < 512 * 2; ++k); + return reply; +} + +bool bus_transfer_msg_arr(uint8_t addr, uint16_t* dat, uint8_t len) { + union { + struct { + unsigned dat: 12; + unsigned addr: 4; + } __attribute__((packed)); + uint16_t _; + } frame = {._=0}; + + uint16_t last_frame = 0x0000; + uint16_t reply = 0x0000; + uint8_t error_count = 0; + + for (int i = 0; i < len; ++i) { + + frame.addr = addr + i; + frame.dat = dat[i]; + + reply = spi_transfer_16_duplex(1, frame._); + + if(reply != last_frame && i != 0) + error_count++; + + if(0){ + syslog(" > "); + syslog_i32(frame._); + syslog(" < "); + syslog_i32(reply); + syslog(" =? "); + syslog_i32(last_frame); + syslog("\n"); + } + + last_frame = frame._; + + for (volatile int k = 0; k < 512 * 2; ++k); + } + + reply = spi_transfer_16_duplex(1, 0x0000); + if(reply != last_frame) + error_count++; + + if(0){ + syslog(" > "); + syslog_i32(frame._); + syslog(" < "); + syslog_i32(reply); + syslog(" =? "); + syslog_i32(last_frame); + syslog("\n"); + } + + return error_count == 0; +} + +uint16_t bus_get_version() { + return bus_transfer_msg(15, 0); +} + +bool bus_ping() { + return bus_get_version() == 0x002a; +} \ No newline at end of file diff --git a/firmware/bus.h b/firmware/bus.h new file mode 100644 index 0000000..c2ea53d --- /dev/null +++ b/firmware/bus.h @@ -0,0 +1,30 @@ +#ifndef FIATLUX_BUS_H +#define FIATLUX_BUS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +uint32_t spi_transfer_32_duplex(uint8_t bus, uint32_t val); + +uint16_t spi_transfer_16_duplex(uint8_t bus, uint32_t val); + +void reset_bus(); + +void bus_init(); + +uint16_t bus_transfer_msg(uint8_t addr, uint16_t dat); + +bool bus_transfer_msg_arr(uint8_t addr, uint16_t dat[], uint8_t len); + +uint16_t bus_get_version(); + +bool bus_ping(); + +#ifdef __cplusplus +} +#endif + +#endif //FIATLUX_BUS_H diff --git a/firmware/fiatlux.c b/firmware/fiatlux.c index 93433d9..dc7e9f1 100644 --- a/firmware/fiatlux.c +++ b/firmware/fiatlux.c @@ -3,6 +3,7 @@ #include "web.h" #include "mqtt.h" #include "lux.h" +#include "esplibs/libmain.h" #include #include @@ -21,6 +22,10 @@ void user_init(void) { system_init_config(); + sdk_system_overclock(); + + printf("CPU freq: %d\n", sdk_system_get_cpu_freq()); + wifi_available_semaphore = xSemaphoreCreateBinary(); //xTaskCreate(mqtt_task, "mqtt_task", 1024, NULL, 1, NULL); diff --git a/firmware/log.cpp b/firmware/log.cpp index e3b9a00..f3adb4e 100644 --- a/firmware/log.cpp +++ b/firmware/log.cpp @@ -30,7 +30,7 @@ extern "C" void syslog_i32(const uint32_t val) { } extern "C" void syslog(const char *msg) { - //printf("syslog> %s", msg); + printf("%s", msg); while (char c = *msg++) { syslog_buf[head++ % syslog_buffer_size] = c; } diff --git a/firmware/lux.cpp b/firmware/lux.cpp index db17968..45bfc24 100644 --- a/firmware/lux.cpp +++ b/firmware/lux.cpp @@ -4,13 +4,16 @@ #include "lux.h" #include "log.h" +#include "bus.h" +#include "esp/spi.h" +#include "espressif/esp_system.h" #include #include #include -#include #include +#include extern "C" { #include @@ -42,6 +45,7 @@ volatile uint16_t light_value; volatile uint16_t peripheral_version; + namespace fiatlux { struct hal_error_t { @@ -225,24 +229,14 @@ extern "C" void signal_led(bool state) { fiatlux::signal::write_data(state); } -uint32_t spi_transfer_32_duplex(uint8_t bus, uint32_t val) { - uint32_t out = val; - uint32_t in; - spi_transfer(bus, &out, &in, 1, SPI_32BIT); - return in; -}; -uint16_t spi_transfer_16_duplex(uint8_t bus, uint32_t val) { - uint16_t out = val; - uint16_t in; - spi_transfer(bus, &out, &in, 1, SPI_16BIT); - return in; -}; +extern "C" bool set_cc48x6(uint16_t a[6]) { + return bus_transfer_msg_arr(1, a, 6); +} -void reset_bus() { - gpio_write(4, false); - for (volatile int k = 0; k < 128; ++k); - gpio_write(4, true); +extern "C" bool set_cc48(uint16_t a) { + uint16_t d[6] = {a, a, a, a, a, a}; + return set_cc48x6(d); } @@ -254,12 +248,7 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { fiatlux::signal::setup(); fiatlux::relais::setup(); - gpio_write(4, false); - for (volatile int k = 0; k < 512 * 2; ++k); - gpio_write(4, true); - gpio_enable(4, GPIO_OUT_OPEN_DRAIN); - gpio_write(4, true); - for (volatile int k = 0; k < 512 * 4; ++k); + bus_init(); int32_t lux_ws2812_number = 240; auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_number); @@ -285,7 +274,7 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { //fiatlux::spi_dimmer::setup(); //fiatlux::apa10x::setup(); - spi_init(1, SPI_MODE0, SPI_FREQ_DIV_2M, true, SPI_BIG_ENDIAN, false); + /* CC48 API: @@ -294,22 +283,8 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { - [2*n+1] = 8bit PWM value */ - union { - struct { - unsigned dat: 12; - unsigned addr: 4; - } __attribute__((packed)); - uint16_t _; - } frame = {._=0}; - { - frame.addr = 15; - frame.dat = 0; - spi_transfer_16_duplex(1, frame._); - for (volatile int k = 0; k < 512 * 2; ++k); - uint16_t reply = spi_transfer_16_duplex(1, 0x0000); - peripheral_version = reply; - for (volatile int k = 0; k < 512 * 2; ++k); + peripheral_version = bus_get_version(); syslog("peripheral_version: "); syslog_i32(peripheral_version); syslog("\n"); @@ -318,6 +293,7 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { light_value = 0xFFF; uint16_t last_light_value = 0xFFFF; + while (true) { fiatlux::signal::write_data(false); @@ -328,56 +304,24 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { syslog_i32(light_value); syslog("\n"); - uint16_t last_frame = 0x0000; - uint16_t reply = 0x0000; - uint8_t error_count = 0; + bool ok = set_cc48(light_value); - for (int i = 0; i < 6; ++i) { - - frame.addr = i + 1; - frame.dat = light_value; - reply = spi_transfer_16_duplex(1, frame._); - - if(reply != last_frame) - error_count++; - - { - syslog(" > "); - syslog_i32(frame._); - syslog(" < "); - syslog_i32(reply); - syslog(" -- "); - syslog_i32(last_frame); - syslog("\n"); - } - - last_frame = frame._; - - for (volatile int k = 0; k < 512 * 2; ++k); - } - - reply = spi_transfer_16_duplex(1, 0x0000); - if(reply != last_frame) - error_count++; - - { - syslog(" > "); - syslog_i32(frame._); - syslog(" < "); - syslog_i32(reply); - syslog(" -- "); - syslog_i32(last_frame); - syslog("\n"); - } - - if(!error_count) { + if(ok) { last_light_value = light_value; + vTaskDelay(1000 / portTICK_PERIOD_MS); } else { syslog("reset_bus\n"); reset_bus(); + vTaskDelay(1000 / portTICK_PERIOD_MS); } - vTaskDelay(5000 / portTICK_PERIOD_MS); + } else { + bool ping = bus_ping(); + if(!ping) { + syslog("ping: reset_bus\n"); + reset_bus(); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } } for (int i = 0; i < 120; i++) @@ -388,7 +332,10 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { ws2812_i2s_update(pixels, PIXEL_RGBW); + //uint32_t lt = sdk_system_get_time(); vTaskDelay(200 / portTICK_PERIOD_MS); + //uint32_t dt = sdk_system_get_time() - lt; + //printf("delay: %d, expect: %d\n", dt, configTICK_RATE_HZ * 200); /*//fiatlux::write_channel((uint8_t *) &leds[0], lux_apa10xx_number, 4, fiatlux::hal_module_t::APA10X); vTaskDelay(200 / portTICK_PERIOD_MS); diff --git a/firmware/lux.h b/firmware/lux.h index bd0d535..bdafb40 100644 --- a/firmware/lux.h +++ b/firmware/lux.h @@ -1,7 +1,3 @@ -// -// Created by jedi on 25.06.21. -// - #ifndef FIRMWARE_LUX_H #define FIRMWARE_LUX_H diff --git a/firmware/wifi.cpp b/firmware/wifi.cpp index b2d9d79..9039c17 100644 --- a/firmware/wifi.cpp +++ b/firmware/wifi.cpp @@ -146,9 +146,6 @@ extern "C" void wifi_task(void *pvParameters) { sysparam_get_int8("wifi_ap_enable", &wifi_ap_enable); wifi_ap_enable = 0; - if(!wifi_sta_enable) - wifi_ap_enable = 1; - int8_t wifi_sta_disabled_restarts = 0; sysparam_get_int8("wifi_sta_disabled_restarts", &wifi_sta_disabled_restarts); if(wifi_sta_disabled_restarts > 0) { @@ -176,6 +173,9 @@ extern "C" void wifi_task(void *pvParameters) { wifi_sta_enable = 0; } + if(!wifi_sta_enable) + wifi_ap_enable = 1; + if(wifi_ap_enable) { /* Default AP ssid and password. */ if(!wifi_ap_ssid && wificfg_default_ssid) { @@ -352,37 +352,43 @@ extern "C" void wifi_task(void *pvParameters) { uint8_t status = 0; uint8_t retries = 30; - while (1) { + if(wifi_sta_enable) { + while (1) { - retries = 30; - while ((status != STATION_GOT_IP) && (retries)) { - status = sdk_wifi_station_get_connect_status(); - printf("%s: status = %d [%d retries left]\n\r", __func__, status, retries); - if(status == STATION_WRONG_PASSWORD) { - printf("WiFi: wrong password\n\r"); - break; - } else if(status == STATION_NO_AP_FOUND) { - printf("WiFi: AP not found\n\r"); - break; - } else if(status == STATION_CONNECT_FAIL) { - printf("WiFi: connection failed\r\n"); - break; + retries = 30; + while ((status != STATION_GOT_IP) && (retries)) { + status = sdk_wifi_station_get_connect_status(); + printf("%s: status = %d [%d retries left]\n\r", __func__, status, retries); + if(status == STATION_WRONG_PASSWORD) { + printf("WiFi: wrong password\n\r"); + break; + } else if(status == STATION_NO_AP_FOUND) { + printf("WiFi: AP not found\n\r"); + break; + } else if(status == STATION_CONNECT_FAIL) { + printf("WiFi: connection failed\r\n"); + break; + } + vTaskDelay(1000 / portTICK_PERIOD_MS); + --retries; + } + if(status == STATION_GOT_IP) { + printf("WiFi: Connected\n\r"); + xSemaphoreGive(wifi_alive); + taskYIELD(); } - vTaskDelay(1000 / portTICK_PERIOD_MS); - --retries; - } - if(status == STATION_GOT_IP) { - printf("WiFi: Connected\n\r"); - xSemaphoreGive(wifi_alive); - taskYIELD(); - } - while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) { - xSemaphoreGive(wifi_alive); - taskYIELD(); + while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) { + xSemaphoreGive(wifi_alive); + taskYIELD(); + } + printf("WiFi: disconnected\n\r"); + sdk_wifi_station_disconnect(); + vTaskDelay(10000 / portTICK_PERIOD_MS); + } + } else { + while(1) { + vTaskDelay(10000 / portTICK_PERIOD_MS); } - printf("WiFi: disconnected\n\r"); - sdk_wifi_station_disconnect(); - vTaskDelay(10000 / portTICK_PERIOD_MS); } } \ No newline at end of file diff --git a/peripherals/cc48x6/firmware/Makefile b/peripherals/cc48x6/firmware/Makefile index 216f2fa..d73a2aa 100644 --- a/peripherals/cc48x6/firmware/Makefile +++ b/peripherals/cc48x6/firmware/Makefile @@ -20,9 +20,9 @@ TARGET = stm32f030c8t_test # building variables ###################################### # debug build? -DEBUG = 1 +DEBUG = 0 # optimization -OPT = -O3 +OPT = -O3 -flto ####################################### diff --git a/peripherals/cc48x6/firmware/Src/main.c b/peripherals/cc48x6/firmware/Src/main.c index 1fdef79..798b93f 100644 --- a/peripherals/cc48x6/firmware/Src/main.c +++ b/peripherals/cc48x6/firmware/Src/main.c @@ -161,7 +161,6 @@ void setDAC(uint8_t channel, uint16_t val) { */ int main(void) { /* USER CODE BEGIN 1 */ - /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/