From 1210df589e721722a331516379aa59e0e737a0a2 Mon Sep 17 00:00:00 2001 From: eddyem Date: Wed, 6 Dec 2017 21:46:16 +0300 Subject: [PATCH] Fix EEPROM in flash emulation; can't fix error in ADC16/ADC17 (temperature and Vdd calculation) --- STM32/steppers/adc.c | 49 ++++++++++++++++++------- STM32/steppers/adc.h | 2 +- STM32/steppers/flash.c | 70 ++++++++++++++++++++++-------------- STM32/steppers/flash.h | 2 +- STM32/steppers/proto.c | 10 ++---- STM32/steppers/steppers.bin | Bin 0 -> 4948 bytes STM32/steppers/usart.h | 1 + 7 files changed, 87 insertions(+), 47 deletions(-) create mode 100755 STM32/steppers/steppers.bin diff --git a/STM32/steppers/adc.c b/STM32/steppers/adc.c index f468ac7..954731d 100644 --- a/STM32/steppers/adc.c +++ b/STM32/steppers/adc.c @@ -23,6 +23,7 @@ #include "stm32f0.h" #include "flash.h" #include "adc.h" +#include "usart.h" /* * 0 - Steppers current @@ -39,9 +40,11 @@ uint16_t ADC_array[NUMBER_OF_ADC_CHANNELS]; void adc_setup(){ // AIN: PA0..3, PA13, PA14. ADC_IN16 - inner temperature. ADC_IN17 - VREFINT /* (1) Enable the peripheral clock of the ADC */ - /* (2) Set peripheral prescaler to /2 so PCLK = HCLK/2 = 24MHz */ + /* (2) Start HSI14 RC oscillator */ + /* (3) Wait HSI14 is ready */ RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; /* (1) */ - RCC->CFGR |= RCC_CFGR_PPRE_2; /* (2) */ + RCC->CR2 |= RCC_CR2_HSI14ON; /* (2) */ + while ((RCC->CR2 & RCC_CR2_HSI14RDY) == 0) /* (3) */ /* (1) Ensure that ADEN = 0 */ /* (2) Clear ADEN */ /* (3) Launch the calibration by setting ADCAL */ @@ -56,12 +59,12 @@ void adc_setup(){ do{ ADC1->CR |= ADC_CR_ADEN; /* (1) */ }while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* (2) */; - /* (1) Select PCLK/2 by writing 01 in CKMODE */ + /* (1) Select HSI14 by writing 00 in CKMODE (reset value) */ /* (2) Select the continuous mode */ /* (3) Select CHSEL0..3, 13,14, 16,17 */ /* (4) Select a sampling mode of 111 i.e. 239.5 ADC clk to be greater than 17.1us */ /* (5) Wake-up the VREFINT and Temperature sensor (only for VBAT, Temp sensor and VRefInt) */ - ADC1->CFGR2 |= ADC_CFGR2_CKMODE_0; /* (1) */ + // ADC1->CFGR2 &= ~ADC_CFGR2_CKMODE; /* (1) */ ADC1->CFGR1 |= ADC_CFGR1_CONT; /* (2)*/ ADC1->CHSELR = ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL2 | ADC_CHSELR_CHSEL3 | ADC_CHSELR_CHSEL13 | ADC_CHSELR_CHSEL14 | @@ -87,25 +90,47 @@ void adc_setup(){ } // return MCU temperature (degrees of celsius) -uint32_t getTemp(){ - uint32_t temperature = ADC_array[6]; - temperature = ((temperature * VDD_APPLI / VDD_CALIB) - (uint32_t) *TEMP30_CAL_ADDR ) ; - temperature *= (uint32_t)(110 - 30); - temperature /= (uint32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR); - temperature += 30; +int32_t getTemp(){ + int32_t temperature = (int32_t)ADC_array[6]; +write2trbuf("getTemp()\ncal30="); +put_uint(*TEMP30_CAL_ADDR); +write2trbuf(", cal110="); +put_uint(*TEMP110_CAL_ADDR); +write2trbuf(", t="); +put_int(temperature); +SENDBUF(); + temperature = ((int32_t) *TEMP30_CAL_ADDR - temperature); +put_int(temperature); +SENDBUF(); + temperature *= (int32_t)(1100 - 300); +put_int(temperature); +SENDBUF(); + temperature = temperature / (int32_t)(*TEMP30_CAL_ADDR - *TEMP110_CAL_ADDR); +put_int(temperature); +SENDBUF(); + temperature += 300; return(temperature); } //static uint32_t calval = 0; // return Vdd * 10 (V) uint32_t getVdd(){ +write2trbuf("getVdd(), val="); +put_uint(ADC_array[7]); +write2trbuf(", cal="); +put_uint(*VREFINT_CAL_ADDR); +SENDBUF(); /* if(!calval){ calval = ((uint32_t) *VREFINT_CAL_ADDR) * VDD_CALIB; calval /= VDD_APPLI; } */ - uint32_t vdd = ADC_array[7] * (uint32_t)33 * the_conf.v33numerator; // 3.3V + uint32_t vdd = ((uint32_t) *VREFINT_CAL_ADDR) * (uint32_t)33 * the_conf.v33numerator; // 3.3V +put_uint(vdd); +SENDBUF(); //vdd /= calval * the_conf.v33denominator; - vdd /= ((uint32_t) *VREFINT_CAL_ADDR) * the_conf.v33denominator; + vdd /= ADC_array[7] * the_conf.v33denominator; +put_uint(vdd); +SENDBUF(); return vdd; } diff --git a/STM32/steppers/adc.h b/STM32/steppers/adc.h index 073c36d..82ed527 100644 --- a/STM32/steppers/adc.h +++ b/STM32/steppers/adc.h @@ -38,7 +38,7 @@ typedef enum{ ESW_ERROR } ESW_status; -uint32_t getTemp(); +int32_t getTemp(); uint32_t getVdd(); uint32_t getVmot(); uint32_t getImot(); diff --git a/STM32/steppers/flash.c b/STM32/steppers/flash.c index 015b418..b020d17 100644 --- a/STM32/steppers/flash.c +++ b/STM32/steppers/flash.c @@ -28,9 +28,10 @@ // start of configuration data in flash (from 15kB, one kB size) #define FLASH_CONF_START_ADDR ((uint32_t)0x08003C00) +static const int maxnum = 1024 / sizeof(user_conf); user_conf the_conf = { - .good_data_pos = 0xffffffff + .userconf_sz = sizeof(user_conf) ,.devID = 0 ,.v12numerator = 1 ,.v12denominator = 1 @@ -41,66 +42,81 @@ user_conf the_conf = { ,.ESW_thres = 150 }; -static int maxnum = 0x800 / sizeof(user_conf); - static int erase_flash(); static int get_gooddata(){ user_conf *c = (user_conf*) FLASH_CONF_START_ADDR; - uint32_t datapos = c->good_data_pos; - if(datapos == 0xffffffff){ // virginity clear - return maxnum; - } // have data - move it to `the_conf` - if(maxnum > 32) maxnum = 32; int idx; - for(idx = 1; idx < maxnum; ++idx){ // find current settings index - first non-zero bit - if(datapos & 1<SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR; } FLASH->CR &= ~(FLASH_CR_PG); @@ -110,6 +126,8 @@ usart1_send_blocking(buf); static int erase_flash(){ int ret = 0; +/*write2trbuf("erase_flash()"); +SENDBUF();*/ /* (1) Wait till no operation is on going */ /* (2) Clear error & EOP bits */ /* (3) Check that the Flash is unlocked */ diff --git a/STM32/steppers/flash.h b/STM32/steppers/flash.h index 6c8840b..48bdb1d 100644 --- a/STM32/steppers/flash.h +++ b/STM32/steppers/flash.h @@ -26,7 +26,7 @@ #define __FLASH_H__ typedef struct{ - uint32_t good_data_pos; // position of data (index of mostly left zero) + uint16_t userconf_sz; // size of data uint16_t devID; // device address (id) uint16_t ESW_thres; // ADC threshold for end-switches/Hall sensors // calibration values for current/voltage sensors diff --git a/STM32/steppers/proto.c b/STM32/steppers/proto.c index 37f9cbb..7c0efed 100644 --- a/STM32/steppers/proto.c +++ b/STM32/steppers/proto.c @@ -27,8 +27,6 @@ #include "string.h" #include "usart.h" -#define SENDBUF() do{usart1_send_blocking(gettrbuf()); cleartrbuf();}while(0) - static const char *eodata = "DATAEND"; static const char *badcmd = "BADCMD"; static const char *allok = "ALL OK"; @@ -158,6 +156,7 @@ typedef struct{ } user_conf_descr; static const user_conf_descr descrarr[] = { + {"CONFSZ", &the_conf.userconf_sz}, {"DEVID", &the_conf.devID}, {"V12NUM", &the_conf.v12numerator}, {"V12DEN", &the_conf.v12denominator}, @@ -171,9 +170,6 @@ static const user_conf_descr descrarr[] = { static char *get_conf(){ const user_conf_descr *curdesc = descrarr; - write2trbuf("DATAPOS="); - put_uint(the_conf.good_data_pos); - SENDBUF(); do{ write2trbuf(curdesc->fieldname); put2trbuf('='); @@ -274,9 +270,9 @@ static char *setESWthres(char *str){ } static char *get_temper(){ - uint32_t t = getTemp(); + int32_t t = getTemp(); write2trbuf("TEMP="); - put_uint(t); + put_int(t); SENDBUF(); return NULL; } diff --git a/STM32/steppers/steppers.bin b/STM32/steppers/steppers.bin new file mode 100755 index 0000000000000000000000000000000000000000..a640856dc9e99f1b0d156d3a5203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jr5c~`$3L6N z_A_v!om7p~zY$tYZD`u?EnIE>?OVA`%(qD3a}oWevuQWRWFOv@;{ORUI0JIa@0+oK zksn=CmD&I~U<9UmyM=7uO6r^?@@DCX{E+nb@@YwxnW|4ECh%i?s^xMo^URKCoC&8r zX!N;eiZ%oHrkPZ|2ES~-zRwgmgLgC*{~aILs!~ zn6p^VAw|+38Yf*Xwa0aOP^WpF7Im6RleA5z&!KjdH|X*@J*d;XPK!ECJwSP#{%GnP zdW9#5enO`Qb(+^{QKzZrctWSo;ayN3ba|Z~)MY`Jwli6?`l@F zFsp#y+BRTL>FmhH@ozPNc?yBz{|#(I9e;{v{|SM9kEFf+E@A`X_Yu?IATA)m-$nNy#Lc$4#(k6*!p+7oppFK1uZTM84n%PY2P(HU?Qhg^xULCx z-B}~n*9QWDRem6#uj^VLXp6J}969`W+u=w1O3SQ0-H!zPx*ruR(v_9^qJn)~-$S_r d4?kR5R>3~e{Rr;pGKllh_K$%4!9O4k@ZV^NJ7@p^ literal 0 HcmV?d00001 diff --git a/STM32/steppers/usart.h b/STM32/steppers/usart.h index a26d3b8..a695020 100644 --- a/STM32/steppers/usart.h +++ b/STM32/steppers/usart.h @@ -47,6 +47,7 @@ void USART1_config(); int usart1_getline(char **line); TXstatus usart1_send(char *str); #define usart1_send_blocking(str) do{}while(LINE_BUSY == usart1_send(str)) +#define SENDBUF() do{usart1_send_blocking(gettrbuf()); cleartrbuf();}while(0) #define cleartrbuf() do{trbufidx = 0;}while(0) #define trbufisfull() (trbufidx)