This commit is contained in:
parent
baf6f8bb45
commit
aa32239740
11 changed files with 221 additions and 136 deletions
6
firmware/.idea/.gitignore
vendored
6
firmware/.idea/.gitignore
vendored
|
@ -1,6 +0,0 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -1,9 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="External" external.linked.project.id="firmware" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="CompDB" type="CPP_MODULE" version="4" />
|
||||
<module version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="Python" name="Python facet">
|
||||
<configuration sdkName="Python 3.9" />
|
||||
</facet>
|
||||
</component>
|
||||
</module>
|
117
firmware/bus.cpp
Normal file
117
firmware/bus.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
#include "bus.h"
|
||||
#include "esp/gpio.h"
|
||||
|
||||
#include <esp/spi.h>
|
||||
#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;
|
||||
}
|
30
firmware/bus.h
Normal file
30
firmware/bus.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#ifndef FIATLUX_BUS_H
|
||||
#define FIATLUX_BUS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
|
@ -3,6 +3,7 @@
|
|||
#include "web.h"
|
||||
#include "mqtt.h"
|
||||
#include "lux.h"
|
||||
#include "esplibs/libmain.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <FreeRTOS.h>
|
||||
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
111
firmware/lux.cpp
111
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 <string.h>
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
|
||||
#include <esp/spi.h>
|
||||
#include <ws2812_i2s/ws2812_i2s.h>
|
||||
#include <cstdio>
|
||||
|
||||
extern "C" {
|
||||
#include <sysparam.h>
|
||||
|
@ -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);
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
//
|
||||
// Created by jedi on 25.06.21.
|
||||
//
|
||||
|
||||
#ifndef FIRMWARE_LUX_H
|
||||
#define FIRMWARE_LUX_H
|
||||
|
||||
|
|
|
@ -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,6 +352,7 @@ extern "C" void wifi_task(void *pvParameters) {
|
|||
uint8_t status = 0;
|
||||
uint8_t retries = 30;
|
||||
|
||||
if(wifi_sta_enable) {
|
||||
while (1) {
|
||||
|
||||
retries = 30;
|
||||
|
@ -385,4 +386,9 @@ extern "C" void wifi_task(void *pvParameters) {
|
|||
sdk_wifi_station_disconnect();
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
} else {
|
||||
while(1) {
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,9 +20,9 @@ TARGET = stm32f030c8t_test
|
|||
# building variables
|
||||
######################################
|
||||
# debug build?
|
||||
DEBUG = 1
|
||||
DEBUG = 0
|
||||
# optimization
|
||||
OPT = -O3
|
||||
OPT = -O3 -flto
|
||||
|
||||
|
||||
#######################################
|
||||
|
|
|
@ -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--------------------------------------------------------*/
|
||||
|
|
Loading…
Reference in a new issue