This commit is contained in:
Edward Emelianov
2026-04-11 00:26:47 +03:00
parent 9ef62c0d4d
commit 56701d6ef6
8 changed files with 220 additions and 17 deletions

Binary file not shown.

View File

@@ -43,6 +43,14 @@ static int as3935_write(uint8_t reg, uint8_t data){
return TRUE;
}
int as3935_get_displco(uint8_t *n){
t_tun_disp t;
if(!as3935_read(TUN_DISP, &t.u8)) return FALSE; // we need to save old `tun_cap` value
*n = t.DISP_TRCO + 2 * t.DISP_SRCO + 3 * t.DISP_LCO;
return TRUE;
}
// display on IRQ: nothing (0), TRCO (1), SRCO (2) or LCO (3)
int as3935_displco(uint8_t n){
if(n > 3) return FALSE;
@@ -74,6 +82,14 @@ int as3935_tuncap(uint8_t n){
return as3935_write(TUN_DISP, t.u8);
}
int as3935_get_tuncap(uint8_t *n){
t_tun_disp t;
if(!as3935_read(TUN_DISP, &t.u8)) return FALSE;
*n = t.TUN_CAP;
return TRUE;
}
#if 0
// set gain
int as3935_gain(uint8_t n){
if(n > 0x1f) return FALSE;
@@ -82,22 +98,34 @@ int as3935_gain(uint8_t n){
g.AFE_GB = n;
return as3935_write(AFE_GAIN, g.u8);
}
#endif
#include "usb_dev.h"
#include "strfunc.h"
// starting calibration
int as3935_calib_rco(){
t_tun_disp t;
USB_sendstr("1\n");
if(!as3935_read(TUN_DISP, &t.u8)) return FALSE;
USB_sendstr("2\n");
if(!as3935_write(CALIB_RCO, DIRECT_COMMAND)) return FALSE;
USB_sendstr("3\n");
t.DISP_LCO = t.DISP_TRCO = 0;
t.DISP_SRCO = 1;
if(!as3935_write(TUN_DISP, t.u8)) return FALSE;
USB_sendstr("4\n");
uint32_t Tstart = Tms;
while(Tms - Tstart < 5) IWDG->KR = IWDG_REFRESH; // sleep for 5ms
while(Tms - Tstart < 3) IWDG->KR = IWDG_REFRESH; // sleep for ~2ms
t.DISP_SRCO = 0;
USB_sendstr("5\n");
if(!as3935_write(TUN_DISP, t.u8)) return FALSE;
while(Tms - Tstart < 300) IWDG->KR = IWDG_REFRESH; // sleep for ~2ms
t_calib srco, trco;
USB_sendstr("6\n");
if(!as3935_read(CALIB_TRCO, &trco.u8)) return FALSE;
USB_sendstr(uhex2str(trco.u8)); newline();
if(!as3935_read(CALIB_SRCO, &srco.u8)) return FALSE;
USB_sendstr(uhex2str(srco.u8)); newline();
if(!srco.CALIB_DONE || !trco.CALIB_DONE) return FALSE;
return TRUE;
}
@@ -112,13 +140,20 @@ int as3935_wakeup(){
}
// set amplifier gain
int as3935_set_gain(uint8_t g){
int as3935_gain(uint8_t g){
if(g > 0x1f) return FALSE;
t_afe_gain a = {0};
a.AFE_GB = g;
return as3935_write(AFE_GAIN, a.u8);
}
int as3935_get_gain(uint8_t *n){
t_afe_gain g;
if(!as3935_read(AFE_GAIN, &g.u8)) return FALSE;
*n = g.AFE_GB;
return TRUE;
}
// watchdog threshold
int as3935_wdthres(uint8_t t){
if(t > 0x0f) return FALSE;
@@ -128,6 +163,13 @@ int as3935_wdthres(uint8_t t){
return as3935_write(THRESHOLD, thres.u8);
}
int as3935_get_wdthres(uint8_t *n){
t_threshold thres;
if(!as3935_read(THRESHOLD, &thres.u8)) return FALSE;
*n = thres.WDTH;
return TRUE;
}
// noice floor level
int as3935_nflev(uint8_t l){
if(l > 7) return FALSE;
@@ -137,6 +179,13 @@ int as3935_nflev(uint8_t l){
return as3935_write(THRESHOLD, thres.u8);
}
int as3935_get_nflev(uint8_t *n){
t_threshold thres;
if(!as3935_read(THRESHOLD, &thres.u8)) return FALSE;
*n = thres.NF_LEV;
return TRUE;
}
// spike rejection
int as3935_srej(uint8_t s){
if(s > 0xf) return FALSE;
@@ -146,6 +195,13 @@ int as3935_srej(uint8_t s){
return as3935_write(LIGHTNING_REG, lr.u8);
}
int as3935_get_srej(uint8_t *n){
t_lightning_reg lr;
if(!as3935_read(LIGHTNING_REG, &lr.u8)) return FALSE;
*n = lr.SREJ;
return TRUE;
}
// minimal lighting number
int as3935_minnumlig(uint8_t n){
if(n > 3) return FALSE;
@@ -155,6 +211,13 @@ int as3935_minnumlig(uint8_t n){
return as3935_write(LIGHTNING_REG, lr.u8);
}
int as3935_get_minnumlig(uint8_t *n){
t_lightning_reg lr;
if(!as3935_read(LIGHTNING_REG, &lr.u8)) return FALSE;
*n = lr.MIN_NUM_LIG;
return TRUE;
}
// clear amount of lightnings for last 15 min
int as3935_clearstat(){
t_lightning_reg lr;
@@ -173,7 +236,7 @@ int as3935_intcode(uint8_t *code){
}
// should interrupt react on disturbers?
int as3935_mask_disturber(uint8_t m){
int as3935_maskdist(uint8_t m){
if(m > 1) return FALSE;
t_int_mask_ant i;
if(!as3935_read(INT_MASK_ANT, &i.u8)) return FALSE;
@@ -181,6 +244,13 @@ int as3935_mask_disturber(uint8_t m){
return as3935_write(INT_MASK_ANT, i.u8);
}
int as3935_get_maskdist(uint8_t *n){
t_int_mask_ant i;
if(!as3935_read(INT_MASK_ANT, &i.u8)) return FALSE;
*n = i.MASK_DIST;
return TRUE;
}
// set Fdiv of antenna LCO
int as3935_lco_fdiv(uint8_t d){
if(d > 3) return FALSE;
@@ -190,6 +260,13 @@ int as3935_lco_fdiv(uint8_t d){
return as3935_write(INT_MASK_ANT, i.u8);
}
int as3935_get_lco_fdiv(uint8_t *n){
t_int_mask_ant i;
if(!as3935_read(INT_MASK_ANT, &i.u8)) return FALSE;
*n = i.LCO_FDIV;
return TRUE;
}
// calculate last lightning energy
int as3935_energy(uint32_t *E){
if(!E) return FALSE;

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 19.0.0, 2026-04-08T23:46:21. -->
<!-- Written by QtCreator 19.0.0, 2026-04-11T00:25:21. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@@ -121,23 +121,29 @@ typedef union{
// distance out of range
#define DIST_OUT_OF_RANGE (0x3f)
int as3935_open(const char *path, uint8_t id);
int as3935_getter(uint8_t reg, uint8_t *data);
int as3935_setter(uint8_t reg, uint8_t data);
int as3935_displco(uint8_t n);
int as3935_get_displco(uint8_t *n);
int as3935_tuncap(uint8_t n);
int as3935_get_tuncap(uint8_t *n);
int as3935_gain(uint8_t n);
int as3935_get_gain(uint8_t *n);
int as3935_wakeup();
int as3935_calib_rco();
int as3935_set_gain(uint8_t g);
//int as3935_set_gain(uint8_t g);
int as3935_wdthres(uint8_t t);
int as3935_get_wdthres(uint8_t *t);
int as3935_nflev(uint8_t l);
int as3935_get_nflev(uint8_t *l);
int as3935_srej(uint8_t s);
int as3935_get_srej(uint8_t *s);
int as3935_minnumlig(uint8_t n);
int as3935_get_minnumlig(uint8_t *n);
int as3935_clearstat();
int as3935_intcode(uint8_t *code);
int as3935_mask_disturber(uint8_t m);
int as3935_maskdist(uint8_t m);
int as3935_get_maskdist(uint8_t *m);
int as3935_lco_fdiv(uint8_t d);
int as3935_get_lco_fdiv(uint8_t *d);
int as3935_energy(uint32_t *E);
int as3935_distance(uint8_t *d);
int as3935_resetdef();

View File

@@ -22,6 +22,7 @@ extern "C"{
#include <stm32f1.h>
#include "adc.h"
#include "as3935.h"
#include "commproto.h"
#include "hardware.h"
#include "spi.h"
@@ -43,12 +44,27 @@ static uint8_t curbuf[MAXSTRLEN];
// list of all commands and handlers
#define COMMAND_TABLE \
COMMAND(clearstat, "clear amount of lightnings for last 15 min") \
COMMAND(displco, "display on LCO: 0- nothing, 1- TRCO, 2- SRCO, 3- LCO") \
COMMAND(distance, "distance to lightning, km") \
COMMAND(energy, "energy of last lightning") \
COMMAND(gain, "change sensor's gain (0..1f)") \
COMMAND(help, "show this help") \
COMMAND(intcode, "last interrupt code") \
COMMAND(lco_fdiv, "set Fdiv of antenna LCO") \
COMMAND(maskdist, "mask (1) or unmask (0) disturber") \
COMMAND(mcutemp, "get MCU temperature (degC*10)") \
COMMAND(mcureset, "reset MCU") \
COMMAND(SPI, "transfer SPI data: SPI=data (hex)") \
COMMAND(minnumlig, "ninimal lightnings number (0..2)") \
COMMAND(nflev, "noice floor level (0..7)") \
COMMAND(resetdef, "reset sensor to defaults") \
COMMAND(SPI, "transfer SPI data: SPIx=data (hex)") \
COMMAND(srej, "spike rejection (0..15)") \
COMMAND(time, "show current time (ms)") \
COMMAND(vdd, "get approx Vdd value (V*100)")
COMMAND(tuncap, "set 'tune capasitors' to given value") \
COMMAND(vdd, "get approx Vdd value (V*100)") \
COMMAND(wakeup, "wake-up given sensor and make its calibration") \
COMMAND(wdthres, "watchdog threshold (0..15)" )
typedef struct {
const char *name;
@@ -106,7 +122,6 @@ static char *splitargs(char *args, int32_t *parno){
return next;
}
#if 0
/**
* @brief argsvals - split `args` into `parno` and setter's value
* @param args - rest of string after command
@@ -125,7 +140,6 @@ static bool argsvals(char *args, int32_t *parno, int32_t *parval){
}
return false;
}
#endif
static errcodes_t cmd_time(const char *cmd, char _U_ *args){
CMDEQ();
@@ -177,6 +191,91 @@ static errcodes_t cmd_help(const char _U_ *cmd, char _U_ *args){
return ERR_AMOUNT;
}
static errcodes_t senscmd8(int32_t CHno, int (*cmd)(uint8_t), int32_t arg){
if(CHno < 0 || CHno >= SENSORS_AMOUNT) return ERR_BADPAR;
uint8_t par = static_cast<uint8_t>(arg);
CS(CHno);
int ans = cmd(par);
CS_OFF();
if(ans) return ERR_OK;
return ERR_CANTRUN;
}
static errcodes_t getta(const char *cmd, int32_t CHno, int (*fn)(uint8_t*)){
if(CHno < 0 || CHno >= SENSORS_AMOUNT) return ERR_BADPAR;
uint8_t par;
CS(CHno);
int ans = fn(&par);
CS_OFF();
if(!ans) return ERR_CANTRUN;
CMDEQP(CHno); SEND(u2str(par)); SEND("\n");
return ERR_AMOUNT;
}
static errcodes_t senscmd(int32_t CHno, int (*cmd)()){
if(CHno < 0 || CHno >= SENSORS_AMOUNT) return ERR_BADPAR;
CS(CHno);
int ans = cmd();
CS_OFF();
if(ans) return ERR_OK;
return ERR_CANTRUN;
}
static errcodes_t cmd_displco(const char *cmd, char *args){
int32_t CHno, val;
if(!argsvals(args, &CHno, &val)) return getta(cmd, CHno, as3935_get_displco);
errcodes_t ret = senscmd8(CHno, as3935_displco, val);
if(ret == ERR_OK) DISPLCO[CHno] = val;
return ret;
}
#define AS3935_FN8(nm) \
static errcodes_t cmd_ ## nm(const char* cmd, char* args){ \
int32_t CHno = -1, val; \
if(!argsvals(args, &CHno, &val)) return getta(cmd, CHno, as3935_get_ ## nm); \
return senscmd8(CHno, as3935_ ## nm, val);}
#define AS3935_FN(nm) \
static errcodes_t cmd_ ## nm(const char*, char* args){ \
int32_t CHno = -1; \
splitargs(args, &CHno); \
return senscmd(CHno, as3935_ ## nm);}
AS3935_FN8(tuncap)
AS3935_FN8(gain)
AS3935_FN8(wdthres)
AS3935_FN8(nflev)
AS3935_FN8(srej)
AS3935_FN8(minnumlig)
AS3935_FN8(maskdist)
AS3935_FN8(lco_fdiv)
AS3935_FN(wakeup)
AS3935_FN(clearstat)
AS3935_FN(resetdef)
#define AS3935_GU8(nm) \
static errcodes_t cmd_ ## nm(const char* cmd, char* args){ \
int32_t CHno = -1; uint8_t par;\
splitargs(args, &CHno); \
if(CHno < 0 || CHno >= SENSORS_AMOUNT) return ERR_BADPAR; \
CS(CHno); int ans = as3935_ ## nm(&par); CS_OFF(); \
if(!ans) return ERR_CANTRUN; \
CMDEQP(CHno); SEND(u2str(par)); SEND("\n"); \
return ERR_AMOUNT; }
AS3935_GU8(intcode)
AS3935_GU8(distance)
static errcodes_t cmd_energy(const char* cmd, char* args){
int32_t CHno = -1; uint32_t par;
splitargs(args, &CHno);
if(CHno < 0 || CHno >= SENSORS_AMOUNT) return ERR_BADPAR;
CS(CHno); int ans = as3935_energy(&par); CS_OFF();
if(!ans) return ERR_CANTRUN;
CMDEQP(CHno); SEND(u2str(par)); SEND("\n");
return ERR_AMOUNT;
}
/**
* @brief parse_hex_data - data parsing in case of `hex + text` input format
* @param input - input string
@@ -224,7 +323,7 @@ static errcodes_t cmd_SPI(const char _U_ *cmd, char *args){
int32_t CHno;
char *setter = splitargs(args, &CHno);
if(!setter) return ERR_BADVAL;
if(CHno < 0 || CHno > 1) return ERR_BADPAR;
if(CHno < 0 || CHno >= SENSORS_AMOUNT) return ERR_BADPAR;
int len = parse_hex_data(setter, curbuf, MAXSTRLEN);
if(len <= 0) return ERR_BADVAL;
CS(CHno);

View File

@@ -20,6 +20,9 @@
#include <stm32f1.h>
// amount of connected sensors
#define SENSORS_AMOUNT 2
// LED0 - PC13 (bluepill), blinking each second
#define LED0_port GPIOC
#define LED0_pin (1<<13)
@@ -41,5 +44,14 @@
// INT pins: PA0/PA1
#define CHK_INT(x) ((GPIOA->IDR & (1<<x)) ? 1 : 0)
enum{
DISPLCO_NOTHING,
DISPLCO_TRCO,
DISPLCO_SRCO,
DISPLCO_LCO
};
extern uint8_t DISPLCO[SENSORS_AMOUNT];
void hw_setup();
void iwdg_setup();

View File

@@ -23,6 +23,8 @@
#include "strfunc.h"
#include "usb_dev.h"
uint8_t DISPLCO[SENSORS_AMOUNT] = {0};
volatile uint32_t Tms = 0;
static char inbuff[RBINSZ];
@@ -45,6 +47,12 @@ int main(){
#endif
//usart_setup();
USBPU_ON();
// wake-up all sensors and run auto-calibration
for(int ch = 0; ch < SENSORS_AMOUNT; ++ch){
CS(ch);
as3935_wakeup();
CS_OFF();
}
while(1){
IWDG->KR = IWDG_REFRESH; // refresh watchdog
if(Tms - lastT > 499){
@@ -57,7 +65,8 @@ int main(){
const char *ans = parse_cmd(USB_sendstr, inbuff);
if(ans) USB_sendstr(ans);
}
for(int ch = 0; ch < 2; ++ch){
for(int ch = 0; ch < SENSORS_AMOUNT; ++ch){
if(DISPLCO[ch] == DISPLCO_NOTHING) continue; // don't check IRQ in if it used as clock output
if(CHK_INT(ch)){
if(oldint[ch] == 0){
oldint[ch] = 1;

View File

@@ -1,2 +1,2 @@
#define BUILD_NUMBER "14"
#define BUILD_DATE "2026-04-08"
#define BUILD_NUMBER "25"
#define BUILD_DATE "2026-04-11"