some bugs fixed, but found new (e.g. ADC always give 4095)

This commit is contained in:
Edward Emelianov 2023-02-21 23:01:43 +03:00
parent ab5f7bc87b
commit 1a8345d4f0
14 changed files with 161 additions and 70 deletions

View File

@ -17,6 +17,9 @@
*/
#include "adc.h"
#ifdef EBUG
#include "proto.h"
#endif
/**
* @brief ADCx_array - arrays for ADC channels with median filtering:
@ -25,8 +28,8 @@
* 4 - internal Tsens - ADC1_IN16
* 5 - Vref - ADC1_IN18
* ADC2:
* 6 - AIN4 (ADC2_IN1)
* 7 - AIN5 (ADC2_IN10)
* 6 - AIN4 - vdrive/10 (ADC2_IN1)
* 7 - AIN5 - 5v/2 (ADC2_IN10)
*/
static uint16_t ADC_array[NUMBER_OF_ADC_CHANNELS*9];
@ -76,7 +79,7 @@ void adc_setup(){
ADC1->SMPR2 = ADC_SMPR2_SMP16 | ADC_SMPR2_SMP18;
// 4 conversions in group: 1->2->3->4->16->18
ADC1->SQR1 = (1<<6) | (2<<12) | (3<<18) | (4<<24) | (NUMBER_OF_ADC1_CHANNELS-1);
ADC1->SQR2 = (16<<6) | (18<<12);
ADC1->SQR2 = (16<<0) | (18<<6);
ADC2->SMPR1 = ADC_SMPR1_SMP1;
ADC2->SMPR2 = ADC_SMPR2_SMP10;
ADC2->SQR1 = (1<<6) | (10<<12) | (NUMBER_OF_ADC2_CHANNELS-1);
@ -121,9 +124,12 @@ uint16_t getADCval(int nch){
PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
PIX_SORT(p[4], p[2]) ;
return p[4];
#undef PIX_SORT
#undef PIX_SWAP
#ifdef EBUG
DBG("val: "); printu(p[4]); newline();
#endif
return p[4];
}
// get voltage @input nch (V)
@ -131,6 +137,9 @@ float getADCvoltage(int nch){
float v = getADCval(nch);
v *= getVdd();
v /= 4096.f; // 12bit ADC
#ifdef EBUG
DBG("v="); printf(v); newline();
#endif
return v;
}
@ -142,6 +151,9 @@ float getMCUtemp(){
temperature *= (110.f - 30.f);
temperature /= (float)(*TEMP30_CAL_ADDR - *TEMP110_CAL_ADDR);
temperature += 30.f;
#ifdef EBUG
DBG("t="); printf(temperature); newline();
#endif
return(temperature);
}
@ -149,5 +161,8 @@ float getMCUtemp(){
float getVdd(){
float vdd = ((float) *VREFINT_CAL_ADDR) * 3.3f; // 3.3V
vdd /= getADCval(ADC_VREF);
#ifdef EBUG
DBG("vdd="); printf(vdd); newline();
#endif
return vdd;
}

View File

@ -19,8 +19,12 @@
#pragma once
#include <stm32f3.h>
#define NUMBER_OF_ADC1_CHANNELS (6)
#define NUMBER_OF_ADC2_CHANNELS (2)
// ADC1 channels
#define NUMBER_OF_ADC1_CHANNELS (6)
// ext ADC channels (ADC_AINx)
#define NUMBER_OF_EXT_ADC_CHANNELS (4)
// ADC2 channels
#define NUMBER_OF_ADC2_CHANNELS (2)
// total number of channels - for array
#define NUMBER_OF_ADC_CHANNELS ((NUMBER_OF_ADC1_CHANNELS+NUMBER_OF_ADC2_CHANNELS))
@ -33,8 +37,8 @@
#define ADC_AIN3 (3)
#define ADC_TS (4)
#define ADC_VREF (5)
#define ADC_AIN4 (6)
#define ADC_AIN5 (7)
#define ADC_VDRIVE (6)
#define ADC_VFIVE (7)
// starting index of ADC2
#define ADC2START (9*NUMBER_OF_ADC1_CHANNELS)

View File

@ -22,6 +22,9 @@
#include "hardware.h"
#include "strfunc.h"
#include "usb.h"
#ifdef EBUG
#include "proto.h"
#endif
// PD1 - Tx, PD0 - Rx !!!
@ -53,7 +56,11 @@ CAN_status CAN_get_status(){
static int CAN_messagebuf_push(CAN_message *msg){
//MSG("Try to push\n");
#ifdef EBUG
USB_sendstr("push\n");
USB_sendstr("push: ");
for(int i = 0; i < msg->length; ++i){
printuhex(msg->data[i]); USB_putbyte(' ');
}
newline();
#endif
if(first_free_idx == first_nonfree_idx){
#ifdef EBUG
@ -80,6 +87,13 @@ CAN_message *CAN_messagebuf_pop(){
first_nonfree_idx = -1;
first_free_idx = 0;
}
#ifdef EBUG
USB_sendstr("pop: ");
for(int i = 0; i < msg->length; ++i){
printuhex(msg->data[i]); USB_putbyte(' ');
}
newline();
#endif
return msg;
}
@ -304,8 +318,7 @@ uint32_t CAN_speed(){
return oldspeed;
}
static void formerr(CAN_message *msg, errcodes err){
if(msg->length < 4) msg->length = 4;
TRUE_INLINE void formerr(CAN_message *msg, errcodes err){
msg->data[3] = (uint8_t)err;
}
@ -317,15 +330,16 @@ static void formerr(CAN_message *msg, errcodes err){
* [CMD][PAR][errcode][VALUE]
* CMD - uint16_t, PAR - uint8_t, errcode - one of CAN_errcodes, VALUE - int32_t
* `errcode` of incoming message doesn't matter
* all answers have 8 bytes length; incoming data may have variable length
*/
TRUE_INLINE void parseCANcommand(CAN_message *msg){
int N = 1000;
// we don't check msg here as it cannot be NULL
#ifdef EBUG
DBG("Get data");
DBG("Get data: ");
for(int i = 0; i < msg->length; ++i){
USB_sendstr(uhex2str(msg->data[i])); USB_putbyte(' ');
}
for(int i = msg->length-1; i < 8; ++i) msg->data[i] = 0;
newline();
#endif
if(msg->length == 0) goto sendmessage; // PING
@ -356,10 +370,10 @@ TRUE_INLINE void parseCANcommand(CAN_message *msg){
errcodes ec = cancmdlist[Index](par, val);
if(ec != ERR_OK){
formerr(msg, ec);
}else{
msg->length = 8;
}
sendmessage:
msg->length = 8;
int N = 1000;
while(CAN_BUSY == CAN_send(msg->data, msg->length, the_conf.CANID))
if(--N == 0) break;
}
@ -369,7 +383,8 @@ static void can_process_fifo(uint8_t fifo_num){
CAN_FIFOMailBox_TypeDef *box = &CAN->sFIFOMailBox[fifo_num];
volatile uint32_t *RFxR = (fifo_num) ? &CAN->RF1R : &CAN->RF0R;
#ifdef EBUG
USB_sendstr(u2str(*RFxR & CAN_RF0R_FMP0)); USB_sendstr(" messages in FIFO\n");
USB_sendstr(u2str(*RFxR & CAN_RF0R_FMP0)); USB_sendstr(" messages in FIFO #");
USB_sendstr(u2str(fifo_num)); newline();
#endif
// read all
while(*RFxR & CAN_RF0R_FMP0){ // amount of messages pending

View File

@ -42,8 +42,12 @@ static errcodes cu_nosuchfn(uint8_t _U_ par, int32_t _U_ *val){
errcodes cu_abspos(uint8_t _U_ par, int32_t _U_ *val){
uint8_t n; CHECKN(n, par);
stopmotor(n);
return ERR_OK;
errcodes ret = ERR_OK;
if(ISSETTER(par)){
ret = setmotpos(n, *val);
}
getpos(n, val);
return ret;
}
errcodes cu_accel(uint8_t _U_ par, int32_t _U_ *val){
@ -56,10 +60,13 @@ errcodes cu_accel(uint8_t _U_ par, int32_t _U_ *val){
return ERR_OK;
}
static const uint8_t extADCchnl[NUMBER_OF_EXT_ADC_CHANNELS] = {ADC_AIN0, ADC_AIN1, ADC_AIN2, ADC_AIN3};
// V*10
errcodes cu_adc(uint8_t par, int32_t *val){
uint8_t n = PARBASE(par);
if(n > NUMBER_OF_ADC_CHANNELS-1) return ERR_BADPAR;
*val = getADCval(n);
if(n > NUMBER_OF_EXT_ADC_CHANNELS - 1) return ERR_BADPAR;
float v = getADCvoltage(extADCchnl[n])*10.f;
*val = (int32_t)v;
return ERR_OK;
}
@ -109,12 +116,8 @@ errcodes cu_eswreact(uint8_t _U_ par, int32_t _U_ *val){
errcodes cu_goto(uint8_t _U_ par, int32_t _U_ *val){
uint8_t n; CHECKN(n, par);
errcodes ret = ERR_OK;
if(ISSETTER(par)){
ret = setmotpos(n, *val);
}
getpos(n, val);
return ret;
if(ISSETTER(par)) return motor_absmove(n, *val);
return getpos(n, val);
}
errcodes cu_gotoz(uint8_t _U_ par, int32_t _U_ *val){
@ -328,6 +331,20 @@ errcodes cu_usartstatus(uint8_t _U_ par, int32_t _U_ *val){
return ERR_BADCMD;
}
// V*10
errcodes cu_vdrive(uint8_t par, int32_t _U_ *val){
NOPARCHK(par);
float v = getADCvoltage(ADC_VDRIVE)*100.f;
*val = (int32_t)v;
return ERR_OK;
}
errcodes cu_vfive(uint8_t par, int32_t *val){
NOPARCHK(par);
float v = getADCvoltage(ADC_VFIVE)*20.f;
*val = (int32_t)v;
return ERR_OK;
}
const fpointer cancmdlist[CCMD_AMOUNT] = {
// different commands
@ -368,6 +385,13 @@ const fpointer cancmdlist[CCMD_AMOUNT] = {
[CCMD_ENCPOS] = cu_nosuchfn,
[CCMD_SETPOS] = cu_abspos,
[CCMD_GOTOZERO] = cu_gotoz,
[CCMD_MOTMUL] = cu_motmul,
[CCMD_DIAGN] = cu_diagn,
[CCMD_ERASEFLASH] = cu_eraseflash,
[CCMD_UDATA] = cu_udata,
[CCMD_USARTSTATUS] = cu_usartstatus,
[CCMD_VDRIVE] = cu_vdrive,
[CCMD_VFIVE] = cu_vfive
// Leave all commands upper for back-compatability with 3steppers
};
@ -399,5 +423,12 @@ const char* cancmds[CCMD_AMOUNT] = {
[CCMD_REINITMOTORS] = "motreinit",
[CCMD_MOTORSTATE] = "state",
[CCMD_SETPOS] = "abspos",
[CCMD_GOTOZERO] = "gotoz"
[CCMD_GOTOZERO] = "gotoz",
[CCMD_MOTMUL] = "motmul",
[CCMD_DIAGN] = "diagn",
[CCMD_ERASEFLASH] = "eraseflash",
[CCMD_UDATA] = "udata",
[CCMD_USARTSTATUS] = "usartstatus",
[CCMD_VDRIVE] = "vdrive",
[CCMD_VFIVE] = "vfive"
};

View File

@ -86,6 +86,13 @@ enum{
,CCMD_MOTORSTATE // motor state
,CCMD_ENCPOS // position of encoder (independing on settings)
,CCMD_SETPOS // set motor position
,CCMD_MOTMUL
,CCMD_DIAGN
,CCMD_ERASEFLASH
,CCMD_UDATA
,CCMD_USARTSTATUS
,CCMD_VDRIVE
,CCMD_VFIVE
// should be the last:
,CCMD_AMOUNT // amount of common commands
};
@ -129,3 +136,5 @@ errcodes cu_stop(uint8_t par, int32_t *val);
errcodes cu_tmcbus(uint8_t par, int32_t *val);
errcodes cu_udata(uint8_t par, int32_t *val);
errcodes cu_usartstatus(uint8_t par, int32_t *val);
errcodes cu_vdrive(uint8_t par, int32_t *val);
errcodes cu_vfive(uint8_t par, int32_t *val);

View File

@ -114,6 +114,10 @@ int fn_udata(_U_ uint32_t hash, _U_ char *args) WAL; // "udata" (2736127636)
int fn_usartstatus(_U_ uint32_t hash, _U_ char *args) WAL; // "usartstatus" (4007098968)
int fn_vdrive(_U_ uint32_t hash, _U_ char *args) WAL; // "vdrive" (2172773525)
int fn_vfive(_U_ uint32_t hash, _U_ char *args) WAL; // "vfive" (3017477285)
static uint32_t hashf(const char *str){
uint32_t hash = 5381;
uint32_t c;
@ -291,6 +295,12 @@ int parsecmd(const char *str){
case CMD_USARTSTATUS:
return fn_usartstatus(h, args);
break;
case CMD_VDRIVE:
return fn_vdrive(h, args);
break;
case CMD_VFIVE:
return fn_vfive(h, args);
break;
default: break;
}
return RET_CMDNOTFOUND;

View File

@ -65,3 +65,5 @@ int parsecmd(const char *cmdwargs);
#define CMD_TMCBUS (1906135955)
#define CMD_UDATA (2736127636)
#define CMD_USARTSTATUS (4007098968)
#define CMD_VDRIVE (2172773525)
#define CMD_VFIVE (3017477285)

View File

@ -1,12 +1,12 @@
"absposN* - GS absolute position (in steps, setter just changes current value)\n"
"accelN* - GS accel/decel (steps/s^2)\n"
"adcN* - G ADC value (N=0..3)\n"
"button[N]* - G all or given (N=0..6) buttons' state\n"
"absposN - GS absolute position (in steps, setter just changes current value)\n"
"accelN - GS accel/decel (steps/s^2)\n"
"adcN - G ADC value (N=0..3)\n"
"button[N] - G all or given (N=0..6) buttons' state\n"
"canerrcodes - G print last CAN errcodes\n"
"canfilter - GS can filters, format: bank# FIFO# mode(M/I) num0 [num1 [num2 [num3]]]\n"
"canflood - send/clear flood message: ID byte0 ... byteN\n"
"canfloodT - GS flood period (>=0ms)\n"
"canid* - GS CAN ID of device\n"
"canid - GS CAN ID of device\n"
"canignore - GS ignore list (max 10 IDs), negative to delete\n"
"canincrflood - send incremental flood message (ID is last for 'flood', stop by 'flood')\n"
"canpause - pause IN packets displaying\n"
@ -15,42 +15,40 @@
"cansend - send data over CAN: send ID byte0 .. byteN (N<8)\n"
"canspeed - GS CAN speed (reinit if setter)\n"
"canstat - G CAN status\n"
"delignlist* - delete ignore list\n"
"diagn[N]* - G DIAG state of motor N (or all)\n"
"dumperr* - dump error codes\n"
"dumpcmd* - dump command codes\n"
"dumpconf* - dump current configuration\n"
"emstop[N]* - emergency stop motor N or all\n"
"encposN* - GS encoder's position\n"
"encrevN* - GS max encoder's pulses per revolution\n"
"encstepmaxN* - GS maximal encoder ticks per step\n"
"encstepminN* - GS minimal encoder ticks per step\n"
"eraseflash* - erase flash data storage\n"
"esw[N]* - G end-switches state\n"
"eswreactN* - GS end-switches reaction (0 - ignore, 1 - stop@any, 2 - stop@zero)\n"
"gotoN* - GS move motor to given absolute position\n"
"gotozN* - find zero position & refresh counters\n"
"dumperr - dump error codes\n"
"dumpcmd - dump command codes\n"
"dumpconf - dump current configuration\n"
"dumpmotflags - dump motor flags' bits\n"
"emstop[N] - emergency stop motor N or all\n"
"eraseflash - erase flash data storage\n"
"esw[N] - G end-switches state\n"
"eswreactN - GS end-switches reaction (0 - ignore, 1 - stop@any, 2 - stop@zero)\n"
"gotoN - GS move motor to given absolute position\n"
"gotozN - find zero position & refresh counters\n"
"gpioconfN* - GS GPIO configuration (0 - PUin, 1 - PPout, 2 - ODout), N=0..2\n"
"gpioN* - GS GPIO values, N=0..2\n"
"maxspeedN* - GS max speed (steps per sec)\n"
"maxstepsN* - GS max steps (from zero ESW)\n"
"mcut* - G MCU T\n"
"mcuvdd* - G MCU Vdd\n"
"microstepsN* - GS microsteps settings (2^0..2^9)\n"
"minspeedN* - min speed (steps per sec)\n"
"motflagsN* - motorN flags\n"
"maxspeedN - GS max speed (steps per sec)\n"
"maxstepsN - GS max steps (from zero ESW)\n"
"mcut - G MCU T\n"
"mcuvdd - G MCU Vdd\n"
"microstepsN - GS microsteps settings (2^0..2^9)\n"
"minspeedN - min speed (steps per sec)\n"
"motflagsN - motorN flags\n"
"motmul* - GS external multiplexer status (<0 - disable, 0..7 - enable and set address)\n"
"motreinit* - re-init motors after configuration changed\n"
"ping* - echo given command back\n"
"relposN* - GS relative move (get remaining)\n"
"relslowN* - GS like 'relpos' but with slowest speed\n"
"motreinit - re-init motors after configuration changed\n"
"ping - echo given command back\n"
"relposN - GS relative move (get remaining)\n"
"relslowN - GS like 'relpos' but with slowest speed\n"
"reset - software reset\n"
"saveconf* - save current configuration\n"
"saveconf - save current configuration\n"
"screen* - GS screen enable (1) or disable (0)\n"
"speedlimit* - G limiting speed for current microsteps setting\n"
"stateN* - G motor state (0-relax, 1-accel, 2-move, 3-mvslow, 4-decel, 5-stall, 6-err)\n"
"stopN* - stop motor with deceleration\n"
"speedlimit - G limiting speed for current microsteps setting\n"
"stateN - G motor state (0-relax, 1-accel, 2-move, 3-mvslow, 4-decel, 5-stall, 6-err)\n"
"stopN - stop motor with deceleration\n"
"time - G time from start (ms)\n"
"tmcbus* - GS TMC control bus (0 - USART, 1 - SPI)\n"
"udata* - GS data by usart in slave mode (text strings, '\\n'-terminated)\n"
"usartstatus* - GS status of USART1 (0 - off, 1 - master, 2 - slave)\n"
"vdrive - G approx voltage on Vdrive\n"
"vfive - G approx voltage on 5V bus\n"

View File

@ -15,16 +15,12 @@ canresume
cansend
canspeed
canstat
delignlist
diagn
dumperr
dumpcmd
dumpconf
dumpmotflags
emstop
encpos
encrev
encstepmax
encstepmin
eraseflash
esw
eswreact
@ -54,3 +50,5 @@ time
tmcbus
udata
usartstatus
vdrive
vfive

View File

@ -547,7 +547,7 @@ static int canusb_function(uint32_t hash, char *args){
}
USB_sendstr("KEY"); USB_putbyte('0'+PARBASE(par));
USB_putbyte('='); USB_sendstr(kstate);
USB_sendstr("KEYTIME="); USB_sendstr(u2str(val));
USB_sendstr("\nKEYTIME="); USB_sendstr(u2str(val));
newline();
return RET_GOOD;
break;
@ -635,6 +635,12 @@ static int canusb_function(uint32_t hash, char *args){
case CMD_USARTSTATUS:
e = cu_usartstatus(par, &val);
break;
case CMD_VDRIVE:
e = cu_vdrive(par, &val);
break;
case CMD_VFIVE:
e = cu_vfive(par, &val);
break;
default:
e = ERR_BADCMD;
break;
@ -690,6 +696,8 @@ int fn_stop(uint32_t _U_ hash, char _U_ *args) AL; //* "stop" (17184971)
int fn_tmcbus(uint32_t _U_ hash, char _U_ *args) AL; //* "tmcbus" (1906135955)
int fn_udata(uint32_t _U_ hash, char _U_ *args) AL; //* "udata" (2736127636)
int fn_usartstatus(uint32_t _U_ hash, char _U_ *args) AL; //* "usartstatus" (4007098968)
int fn_vdrive(uint32_t _U_ hash, char _U_ *args) AL; // "vdrive" (2172773525)
int fn_vfive(uint32_t _U_ hash, char _U_ *args) AL; // "vfive" (3017477285)
/**

View File

@ -26,6 +26,7 @@
#define printu(x) do{USB_sendstr(u2str(x));}while(0)
#define printi(x) do{USB_sendstr(i2str(x));}while(0)
#define printuhex(x) do{USB_sendstr(uhex2str(x));}while(0)
#define printf(x) do{USB_sendstr(float2str(x, 2));}while(0)
extern uint8_t ShowMsgs; // show CAN messages flag

View File

@ -110,7 +110,7 @@ errcodes setmotpos(uint8_t i, int32_t position){
if(state[i] != STP_RELAX) return ERR_CANTRUN;
if(position > (int32_t)the_conf.maxsteps[i] || position < -(int32_t)the_conf.maxsteps[i])
return ERR_BADVAL; // too big position or zero
if(position == stppos[i]) return ERR_OK;
stppos[i] = position;
return ERR_OK;
}

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "54"
#define BUILD_DATE "2023-02-20"
#define BUILD_NUMBER "56"
#define BUILD_DATE "2023-02-21"