diff --git a/F1-nolib/chronometer/Readme.md b/F1-nolib/chronometer/Readme.md index 3c64c24..545d295 100644 --- a/F1-nolib/chronometer/Readme.md +++ b/F1-nolib/chronometer/Readme.md @@ -16,4 +16,5 @@ Chronometer for downhill competitions - PA14 - TRIG1 - button1/laser/etc (EXTI) - PA15 - USB pullup -- PB0,1,2 - free for other functions +- PB0 - ADC channel 8 +- PB1,2 - free for other functions diff --git a/F1-nolib/chronometer/adc.c b/F1-nolib/chronometer/adc.c index 9887722..eadbd4f 100644 --- a/F1-nolib/chronometer/adc.c +++ b/F1-nolib/chronometer/adc.c @@ -17,6 +17,8 @@ */ #include "adc.h" +#include "flash.h" +#include "hardware.h" /** * @brief ADC_array - array for ADC channels with median filtering: @@ -68,3 +70,19 @@ uint32_t getVdd(){ vdd /= getADCval(2); return vdd; } + +void chkADCtrigger(){ + static uint8_t triggered = 0; + savetrigtime(); + uint16_t val = getADCval(0); + if(triggered){ // check untriggered action + if(val < the_conf.ADC_min || val > the_conf.ADC_max){ + triggered = 0; + } + }else{ // check if thigger shot + if(val > the_conf.ADC_min && val < the_conf.ADC_max){ + triggered = 1; + fillshotms(4); + } + } +} diff --git a/F1-nolib/chronometer/adc.h b/F1-nolib/chronometer/adc.h index de20e01..1cab310 100644 --- a/F1-nolib/chronometer/adc.h +++ b/F1-nolib/chronometer/adc.h @@ -25,5 +25,5 @@ extern uint16_t ADC_array[]; int32_t getMCUtemp(); uint32_t getVdd(); uint16_t getADCval(int nch); - +void chkADCtrigger(); #endif // ADC_H diff --git a/F1-nolib/chronometer/chrono.bin b/F1-nolib/chronometer/chrono.bin index c5f312d..7809584 100755 Binary files a/F1-nolib/chronometer/chrono.bin and b/F1-nolib/chronometer/chrono.bin differ diff --git a/F1-nolib/chronometer/flash.c b/F1-nolib/chronometer/flash.c index e55fffe..580c3c3 100644 --- a/F1-nolib/chronometer/flash.c +++ b/F1-nolib/chronometer/flash.c @@ -58,6 +58,8 @@ typedef struct{ ,.trig_pullups = 0xff \ ,.trigstate = 0 \ ,.trigpause = {400, 400, 400} \ + ,.ADC_min = 1024 \ + ,.ADC_max = 3072 \ } __attribute__((section(".myvars"))) static const flash_storage Flash_Storage = { diff --git a/F1-nolib/chronometer/flash.h b/F1-nolib/chronometer/flash.h index 14d5293..cbbb452 100644 --- a/F1-nolib/chronometer/flash.h +++ b/F1-nolib/chronometer/flash.h @@ -38,6 +38,8 @@ typedef struct __attribute__((packed)){ uint8_t trig_pullups; // trigger pullups: each bit ==0 to set OFF, ==1 to set ON pullup with given number uint8_t trigstate; // level in `triggered` state int32_t trigpause[TRIGGERS_AMOUNT]; // pause (ms) for false shots + int16_t ADC_min; // min&max values of ADC (shot when ADval > ADC_min && < ADC_max) + int16_t ADC_max; // !!! BOTH ARE SIGNED! so you can include 0 & 4096 } user_conf; extern user_conf the_conf; diff --git a/F1-nolib/chronometer/hardware.c b/F1-nolib/chronometer/hardware.c index 49e5a9a..7aceba0 100644 --- a/F1-nolib/chronometer/hardware.c +++ b/F1-nolib/chronometer/hardware.c @@ -29,12 +29,13 @@ #include // memcpy +uint8_t LEDSon = 1; // LEDS are working // ports of triggers -GPIO_TypeDef *trigport[TRIGGERS_AMOUNT] = {GPIOA, GPIOA, GPIOA}; +static GPIO_TypeDef *trigport[DIGTRIG_AMOUNT] = {GPIOA, GPIOA, GPIOA}; // pins of triggers: PA13, PA14, PA4 -uint16_t trigpin[TRIGGERS_AMOUNT] = {1<<13, 1<<14, 1<<4}; +static uint16_t trigpin[DIGTRIG_AMOUNT] = {1<<13, 1<<14, 1<<4}; // value of pin in `triggered` state -uint8_t trigstate[TRIGGERS_AMOUNT]; +static uint8_t trigstate[DIGTRIG_AMOUNT]; // time of triggers shot trigtime shottime[TRIGGERS_AMOUNT]; // Tms value when they shot @@ -64,7 +65,7 @@ static inline void gpio_setup(){ EXTI->IMR = EXTI_IMR_MR1; EXTI->RTSR = EXTI_RTSR_TR1; // rising trigger // PA4/PA13/PA14 - buttons - for(int i = 0; i < TRIGGERS_AMOUNT; ++i){ + for(int i = 0; i < DIGTRIG_AMOUNT; ++i){ uint16_t pin = trigpin[i]; // fill trigstate array uint8_t trgs = (the_conf.trigstate & (1< TRIGGERS_AMOUNT) return; if(shotms[i] - Tms > (uint32_t)the_conf.trigpause[i]){ shotms[i] = Tms; @@ -146,14 +147,14 @@ static void fillshotms(int i){ } } -void exti4_isr(){ // PA4 - button2 +void exti4_isr(){ // PA4 - trigger[2] savetrigtime(); fillshotms(2); DBG("exti4"); EXTI->PR = EXTI_PR_PR4; } -void exti15_10_isr(){ // PA13 - button0, PA14 - button1 +void exti15_10_isr(){ // PA13 - trigger[0], PA14 - trigger[1] savetrigtime(); if(EXTI->PR & EXTI_PR_PR13){ fillshotms(0); diff --git a/F1-nolib/chronometer/hardware.h b/F1-nolib/chronometer/hardware.h index d392e39..869f327 100644 --- a/F1-nolib/chronometer/hardware.h +++ b/F1-nolib/chronometer/hardware.h @@ -38,12 +38,14 @@ #define PPS_pin (1<<1) // PPS and triggers state -// amount of triggers, should be less than 9 -#define TRIGGERS_AMOUNT (3) -extern GPIO_TypeDef *trigport[TRIGGERS_AMOUNT]; -extern uint16_t trigpin[TRIGGERS_AMOUNT]; -extern uint8_t trigstate[TRIGGERS_AMOUNT]; +// amount of triggers, should be less than 9; 5 - 0..2 - switches, 3 - LIDAR, 4 - ADC +#define TRIGGERS_AMOUNT (5) +// amount of digital triggers (on interrupts) +#define DIGTRIG_AMOUNT (3) + uint8_t gettrig(uint8_t N); +void fillshotms(int i); +void savetrigtime(); #define GET_PPS() ((GPIOA->IDR & (1<<1)) ? 1 : 0) // USB pullup - PA15 @@ -52,12 +54,12 @@ uint8_t gettrig(uint8_t N); #define USBPU_ON() pin_clear(USBPU_port, USBPU_pin) #define USBPU_OFF() pin_set(USBPU_port, USBPU_pin) -#define LED_blink() pin_toggle(LED0_port, LED0_pin) -#define LED_on() pin_clear(LED0_port, LED0_pin) -#define LED_off() pin_set(LED0_port, LED0_pin) -#define LED1_blink() pin_toggle(LED1_port, LED1_pin) -#define LED1_on() pin_clear(LED1_port, LED1_pin) -#define LED1_off() pin_set(LED1_port, LED1_pin) +#define LED_blink() do{if(LEDSon)pin_toggle(LED0_port, LED0_pin);}while(0) +#define LED_on() do{if(LEDSon)pin_clear(LED0_port, LED0_pin);}while(0) +#define LED_off() do{if(LEDSon)pin_set(LED0_port, LED0_pin);}while(0) +#define LED1_blink() do{if(LEDSon)pin_toggle(LED1_port, LED1_pin);}while(0) +#define LED1_on() do{if(LEDSon)pin_clear(LED1_port, LED1_pin);}while(0) +#define LED1_off() do{if(LEDSon)pin_set(LED1_port, LED1_pin);}while(0) // GPS USART == USART2, LIDAR USART == USART3 #define GPS_USART (2) @@ -68,6 +70,8 @@ typedef struct{ curtime Time; } trigtime; +// turn on/off LEDs: +extern uint8_t LEDSon; // time of triggers shot extern trigtime shottime[TRIGGERS_AMOUNT]; // if trigger[N] shots, the bit N will be 1 diff --git a/F1-nolib/chronometer/lidar.c b/F1-nolib/chronometer/lidar.c index dd2ee14..dc363c6 100644 --- a/F1-nolib/chronometer/lidar.c +++ b/F1-nolib/chronometer/lidar.c @@ -25,7 +25,7 @@ uint16_t last_lidar_stren = 0; uint16_t lidar_triggered_dist = 0; void parse_lidar_data(char *txt){ - static int triggered = 0; + static uint8_t triggered = 0; last_lidar_dist = txt[2] | (txt[3] << 8); last_lidar_stren = txt[4] | (txt[5] << 8); if(last_lidar_stren < LIDAR_LOWER_STREN) return; // weak signal @@ -46,8 +46,10 @@ void parse_lidar_data(char *txt){ } }else{ if(last_lidar_dist > the_conf.dist_min && last_lidar_dist < the_conf.dist_max){ + savetrigtime(); triggered = 1; lidar_triggered_dist = last_lidar_dist; + fillshotms(3); #ifdef EBUG SEND("Triggered! distance="); printu(1, last_lidar_dist); diff --git a/F1-nolib/chronometer/main.c b/F1-nolib/chronometer/main.c index f479409..1c4fef0 100644 --- a/F1-nolib/chronometer/main.c +++ b/F1-nolib/chronometer/main.c @@ -19,7 +19,7 @@ * MA 02110-1301, USA. */ -//#include "adc.h" +#include "adc.h" #include "GPS.h" #include "flash.h" #include "hardware.h" @@ -174,6 +174,24 @@ static char *get_USB(){ return NULL; } +/* +void linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){ // get/set line coding + DBG("linecoding_handler"); +}*/ + +void clstate_handler(uint16_t __attribute__((unused)) val){ // lesser bits of val: DTR|RTS + USB_send("Chronometer version " VERSION ".\n"); +#ifdef EBUG + if(val & 1) DBG("RTS set"); + if(val & 2) DBG("DTR set"); +#endif +} + +/* +void break_handler(){ // client disconnected + DBG("break_handler"); +}*/ + int main(void){ uint32_t lastT = 0; sysreset(); @@ -185,7 +203,10 @@ int main(void){ get_userconf(); // !!! hw_setup() should be the first in setup stage hw_setup(); + USB_setup(); + USBPU_ON(); usarts_setup(); +#ifdef EBUG SEND("Chronometer version " VERSION ".\n"); if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured SEND("WDGRESET=1\n"); @@ -193,11 +214,10 @@ int main(void){ if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured SEND("SOFTRESET=1\n"); } +#endif RCC->CSR |= RCC_CSR_RMVF; // remove reset flags - - USB_setup(); iwdg_setup(); - USBPU_ON(); + while (1){ IWDG->KR = IWDG_REFRESH; // refresh watchdog @@ -265,6 +285,7 @@ int main(void){ parse_lidar_data(txt); } } + chkADCtrigger(); } return 0; } diff --git a/F1-nolib/chronometer/str.c b/F1-nolib/chronometer/str.c index 7d89339..1e511ba 100644 --- a/F1-nolib/chronometer/str.c +++ b/F1-nolib/chronometer/str.c @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include "adc.h" #include "flash.h" #include "str.h" #include "time.h" @@ -57,18 +58,30 @@ char *getchr(const char *str, char symbol){ return NULL; } +#define sendu(x) do{USB_send(u2str(x));}while(0) + +static void sendi(int32_t I){ + if(I < 0){ + USB_send("-"); + I = -I; + } + USB_send(u2str((uint32_t)I)); +} + /** * @brief showuserconf - show configuration over USB */ static void showuserconf(){ - USB_send("\nCONFIG:\nDISTMIN="); USB_send(u2str(the_conf.dist_min)); - USB_send("\nDISTMAX="); USB_send(u2str(the_conf.dist_max)); - USB_send("\nPULLUPS="); USB_send(u2str(the_conf.trig_pullups)); - USB_send("\nTRIGLVL="); USB_send(u2str(the_conf.trigstate)); + USB_send("\nCONFIG:\nDISTMIN="); sendu(the_conf.dist_min); + USB_send("\nDISTMAX="); sendu(the_conf.dist_max); + USB_send("\nADCMIN="); sendi(the_conf.ADC_min); + USB_send("\nADCMAX="); sendi(the_conf.ADC_max); + USB_send("\nPULLUPS="); sendu(the_conf.trig_pullups); + USB_send("\nTRIGLVL="); sendu(the_conf.trigstate); USB_send("\nTRIGPAUSE={"); for(int i = 0; i < TRIGGERS_AMOUNT; ++i){ if(i) USB_send(", "); - USB_send(u2str(the_conf.trigpause[i])); + sendu(the_conf.trigpause[i]); } USB_send("}"); USB_send("\nENDCONFIG\n"); @@ -89,9 +102,14 @@ int parse_USBCMD(char *cmd){ IWDG->KR = IWDG_REFRESH; if(*cmd == '?'){ // help USB_send("Commands:\n" + CMD_ADCMAX " - max ADC value treshold for trigger\n" + CMD_ADCMIN " - min -//- (triggered when ADval>min & TRIGGERS_AMOUNT - 1) goto bad_number; show_trigger_shot((uint8_t)1< 1) goto bad_number; + USB_send("LEDS="); + if(Nt){ + LEDSon = 1; + USB_send("ON\n"); + }else{ + LED_off(); + LED1_off(); + LEDSon = 0; + USB_send("OFF\n"); + } + }else if(CMP(cmd, CMD_ADCMAX) == 0){ // set low limit + GETNUM(CMD_ADCMAX); + if(N < -4096 || N > 4096) goto bad_number; + if(the_conf.ADC_max != (int16_t)N){ + conf_modified = 1; + the_conf.ADC_max = (int16_t) N; + succeed = 1; + } + }else if(CMP(cmd, CMD_ADCMIN) == 0){ // set low limit + GETNUM(CMD_ADCMIN); + if(N < -4096 || N > 4096) goto bad_number; + if(the_conf.ADC_min != (int16_t)N){ + conf_modified = 1; + the_conf.ADC_min = (int16_t) N; + succeed = 1; + } }else return 1; IWDG->KR = IWDG_REFRESH; if(succeed) USB_send("Success!\n"); @@ -198,7 +267,7 @@ void show_trigger_shot(uint8_t tshot){ else continue; if(trigger_shot & X) trigger_shot &= ~X; USB_send("TRIG"); - USB_send(u2str(i)); + sendu(i); USB_send("="); USB_send(get_time(&shottime[i].Time, shottime[i].millis)); USB_send("\n"); diff --git a/F1-nolib/chronometer/str.h b/F1-nolib/chronometer/str.h index 7f06f47..5cc21ae 100644 --- a/F1-nolib/chronometer/str.h +++ b/F1-nolib/chronometer/str.h @@ -26,10 +26,8 @@ // lower and upper limits to capture #define CMD_DISTMIN "distmin" #define CMD_DISTMAX "distmax" -#define CMD_ADC1MIN "adc1min" -#define CMD_ADC2MIN "adc2min" -#define CMD_ADC1MAX "adc1max" -#define CMD_ADC2MAX "adc2max" +#define CMD_ADCMIN "adcmin" +#define CMD_ADCMAX "adcmax" #define CMD_PRINTTIME "time" #define CMD_STORECONF "store" #define CMD_GPSSTR "gpsstring" @@ -38,6 +36,10 @@ #define CMD_TRIGLVL "triglevel" #define CMD_TRGPAUSE "trigpause" #define CMD_TRGTIME "trigtime" +#define CMD_GETVDD "vdd" +#define CMD_GETMCUTEMP "mcutemp" +#define CMD_GETADCVAL "adcval" +#define CMD_LEDS "leds" extern uint8_t showGPSstr;