mirror of
https://github.com/eddyem/IR-controller.git
synced 2025-12-06 02:35:14 +03:00
fixed errors in flash write module
This commit is contained in:
parent
a54c3e3a66
commit
3c6450b9d4
@ -3,6 +3,8 @@ First PCB have been prodused, so I need "only" to solder elements & finish the c
|
||||
|
||||
Pinout of MCU is in file schematics/STM32_PINS
|
||||
|
||||
USE FUNCTION rat() in GNU/octave to calculate rational approximation to ADC conversion coefficients
|
||||
|
||||
|
||||
-- OLD --
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
|
||||
#include "flash.h"
|
||||
#include <libopencm3/stm32/flash.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* this is a default values of stored data
|
||||
@ -29,13 +30,29 @@
|
||||
*/
|
||||
#define FLASH_BLOCK_SIZE (2048)
|
||||
#define FLASH_WRONG_DATA_WRITTEN 0x80
|
||||
/*
|
||||
.bss._flash_buffer
|
||||
0x20001000 0x800 mk/flash.o
|
||||
0x20001000 _flash_buffer
|
||||
*/
|
||||
|
||||
const uint8_t _flash_buffer[FLASH_BLOCK_SIZE] __attribute__ ((aligned(FLASH_BLOCK_SIZE)));
|
||||
//const uint8_t _flash_buffer[FLASH_BLOCK_SIZE] __attribute__ ((aligned(FLASH_BLOCK_SIZE)));
|
||||
const flash_data Stored_Data __attribute__ ((aligned(FLASH_BLOCK_SIZE))) = {
|
||||
//.magick = FLASH_MAGICK,
|
||||
._ADC_multipliers = {100000,100000,100000,100000,100000,100000,100000,100000, // TRD
|
||||
26, // shutter
|
||||
2 // power
|
||||
},
|
||||
._ADC_divisors = {1,1,1,1,1,1,1,1, // TRD
|
||||
25, // shutter
|
||||
7 // power
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* these are default values
|
||||
* they can be changed in runtime to change data stored in flash
|
||||
*/
|
||||
*
|
||||
flash_data Default_stored_data = {
|
||||
.magick = FLASH_MAGICK,
|
||||
._ADC_multipliers = {100000,100000,100000,100000,100000,100000,100000,100000, // TRD
|
||||
@ -48,49 +65,28 @@ flash_data Default_stored_data = {
|
||||
}
|
||||
};
|
||||
|
||||
flash_data *Stored_Data = (flash_data*) _flash_buffer;
|
||||
*/
|
||||
//flash_data *Stored_Data = (flash_data*) _flash_buffer;
|
||||
|
||||
uint32_t flash_write_data(uint8_t *var, uint8_t *data, uint16_t datalen){
|
||||
uint32_t start_address = (uint32_t)var, page_address = (uint32_t)Stored_Data;
|
||||
uint32_t *dataptr = (uint32_t*)data;
|
||||
uint32_t flash_write_data(uint32_t *dataptr, uint16_t datalen){
|
||||
uint32_t start_address = (uint32_t)&Stored_Data;
|
||||
uint16_t i, rem;
|
||||
uint32_t ret = 0;
|
||||
// check start address - it should be inside
|
||||
if((start_address - page_address) >= FLASH_BLOCK_SIZE){
|
||||
// DBG("bad starting address\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
flash_unlock();
|
||||
DBG("erase\n");
|
||||
//Erasing page
|
||||
flash_erase_page(page_address);
|
||||
//DBG("erase flash ");
|
||||
/* if(FLASH_SR_EOP != (ret = flash_get_status_flags()))
|
||||
goto endoffunction;
|
||||
*/
|
||||
//DBG("OK\nwrite");
|
||||
flash_erase_page(start_address);
|
||||
rem = datalen % 4; // remainder
|
||||
datalen /= 4; // round to 4
|
||||
/*
|
||||
print_int(datalen, lastsendfun);
|
||||
DBG(" blocks of 4 bytes\n");
|
||||
*/
|
||||
// copy data by blocks of four
|
||||
for(i = 0; i < datalen; i++, dataptr++, start_address += 4){
|
||||
// write data word
|
||||
flash_program_word(start_address, *dataptr);
|
||||
/* if(FLASH_SR_EOP != (ret = flash_get_status_flags()))
|
||||
goto endoffunction;
|
||||
*/ //verify
|
||||
//verify
|
||||
if(*((uint32_t*)start_address) != *dataptr){
|
||||
ret = FLASH_WRONG_DATA_WRITTEN;
|
||||
goto endoffunction;
|
||||
}
|
||||
/*
|
||||
DBG("Written: ");
|
||||
print_int((int32_t)(uint32_t*)dataptr, lastsendfun);
|
||||
lastsendfun('\n');
|
||||
*/
|
||||
}
|
||||
// remainder
|
||||
if(rem){
|
||||
@ -100,8 +96,6 @@ lastsendfun('\n');
|
||||
if(rem == 3){ halfwords[1] = *((uint8_t*)dataptr+3); n = 2;}
|
||||
for(i = 0; i < n; i++, start_address += 2){
|
||||
flash_program_half_word(start_address, halfwords[i]);
|
||||
if(FLASH_SR_EOP != (ret = flash_get_status_flags()))
|
||||
goto endoffunction;
|
||||
//verify
|
||||
if(*((uint16_t*)start_address) != halfwords[i]){
|
||||
ret = FLASH_WRONG_DATA_WRITTEN;
|
||||
@ -109,25 +103,20 @@ lastsendfun('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
DBG("ok written\n");
|
||||
endoffunction:
|
||||
flash_lock();
|
||||
/*
|
||||
DBG("end, status: ");
|
||||
print_int(ret, lastsendfun);
|
||||
lastsendfun('\n');
|
||||
*/
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* checks magick in start of data block and fill block with default data
|
||||
* if flash is uninitialized
|
||||
*/
|
||||
uint32_t check_flash_data(){
|
||||
if(Stored_Data->magick == FLASH_MAGICK) return 0;
|
||||
DBG("copy data\n");
|
||||
return flash_write_data((uint8_t*)Stored_Data, (uint8_t*)&Default_stored_data, sizeof(flash_data));
|
||||
uint32_t flash_store_U32(uint32_t addr, uint32_t *data){
|
||||
flash_data Saved_Data;
|
||||
uint32_t sz, ptrdiff;
|
||||
sz = (uint32_t)&Stored_Data.last_addr - (uint32_t)&Stored_Data;
|
||||
ptrdiff = addr - (uint32_t)&Stored_Data;
|
||||
memcpy((void*)&Saved_Data, (void*)&Stored_Data, sz);
|
||||
memcpy((void*)((uint32_t)&Saved_Data + ptrdiff), (void*)data, 4);
|
||||
return flash_write_data((uint32_t*)&Saved_Data, sz);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -135,8 +124,8 @@ uint32_t check_flash_data(){
|
||||
*/
|
||||
void dump_flash_data(sendfun s){
|
||||
int i;
|
||||
P("magick: ", s);
|
||||
print_int(Stored_Data->magick, s);
|
||||
// P("magick: ", s);
|
||||
// print_int(Stored_Data.magick, s);
|
||||
P("\nADC multipliers: ", s);
|
||||
for(i = 0; i < ADC_CHANNELS_NUMBER; i++){
|
||||
if(i) P(", ", s);
|
||||
|
||||
@ -29,18 +29,20 @@
|
||||
#define FLASH_MAGICK ((uint32_t) 0xAA55A55A)
|
||||
|
||||
typedef struct{
|
||||
uint32_t magick; // magick value
|
||||
//uint32_t magick; // magick value
|
||||
// A-D value[x] = ADU * ADC_multipliers[x] / ADC_divisors[x]
|
||||
uint32_t _ADC_multipliers[ADC_CHANNELS_NUMBER];
|
||||
uint32_t _ADC_divisors[ADC_CHANNELS_NUMBER];
|
||||
char last_addr[0];
|
||||
char struct_end[0] __attribute__ ((aligned(2048)));
|
||||
} flash_data;
|
||||
|
||||
extern flash_data *Stored_Data;
|
||||
extern const flash_data Stored_Data;
|
||||
|
||||
#define ADC_multipliers Stored_Data->_ADC_multipliers
|
||||
#define ADC_divisors Stored_Data->_ADC_divisors
|
||||
#define ADC_multipliers Stored_Data._ADC_multipliers
|
||||
#define ADC_divisors Stored_Data._ADC_divisors
|
||||
|
||||
uint32_t check_flash_data();
|
||||
void dump_flash_data(sendfun s);
|
||||
uint32_t flash_store_U32(uint32_t addr, uint32_t *data);
|
||||
|
||||
#endif // __FLASH_H__
|
||||
|
||||
@ -234,6 +234,7 @@ int shutter_voltage(){
|
||||
* 3.3V == 4096 ADU, 10..12V comes to ADC in through resistor divider 4.7k:12k, so
|
||||
* U10(V/100) = Uadc(ADU) * 167/47 * 33/40960 * 100 = Uadc(ADU) * 5511 / 19251
|
||||
* ==> approximately this is equal to val*2/7
|
||||
* (real: approx 17/58)
|
||||
*/
|
||||
int power_voltage(){
|
||||
uint32_t val = ADC_value[POWER_SENSE_NUMBER]; // 9
|
||||
|
||||
Binary file not shown.
@ -31,7 +31,7 @@ usbd_device *usbd_dev;
|
||||
|
||||
uint8_t ADC_monitoring = 0; // ==1 to make continuous monitoring
|
||||
|
||||
uint32_t ad7794_on = 0, flash_status = 1;
|
||||
uint32_t ad7794_on = 0;
|
||||
uint32_t ad7794_values[TRD_NO];
|
||||
uint8_t doubleconv = 1; // ==0 to single conversion; 1 to double (with currents reversing)
|
||||
#define ADC_direct() setup_AD7794(EXTREFIN_1 | REF_DETECTION | UNIPOLAR_CODING, IEXC_DIRECT | IEXC_1MA)
|
||||
@ -175,7 +175,6 @@ int main(){
|
||||
|
||||
usb_connect(); // turn on USB
|
||||
shutter_init();
|
||||
flash_status = check_flash_data(); // init flash block if uninitialized
|
||||
while(1){
|
||||
usbd_poll(usbd_dev);
|
||||
if(usbdatalen){ // there's something in USB buffer
|
||||
|
||||
@ -55,7 +55,6 @@
|
||||
extern uint32_t ad7794_values[]; // array with ADC data
|
||||
extern uint8_t doubleconv; // single/double ADC conversion
|
||||
extern uint32_t ad7794_on; // ==1 after AD7794 initialisation
|
||||
extern uint32_t flash_status; // == 0 if flash OK, or == FLASH_SR/FLASH_SR2
|
||||
extern uint8_t ADC_monitoring; // ==1 to make continuous monitoring
|
||||
void AD7794_init();
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include "main.h"
|
||||
#include "uart.h"
|
||||
#include "hardware_ini.h"
|
||||
#include "flash.h"
|
||||
|
||||
// integer value given by user
|
||||
static volatile int32_t User_value = 0;
|
||||
@ -71,20 +72,53 @@ void set_shtr_delay(int32_t v, sendfun s){
|
||||
print_int(d, s);
|
||||
}
|
||||
|
||||
|
||||
int adc_channel = -1;
|
||||
int div_mul = 0; // 0 - multip., !0 - div.
|
||||
void ch_divmul(int32_t v, sendfun s){
|
||||
uint32_t val = (uint32_t) v;
|
||||
if(adc_channel == -1) return;
|
||||
if(div_mul){ // != 0 - divisors
|
||||
flash_store_U32((uint32_t)&ADC_divisors[adc_channel], &val);
|
||||
}else{ // == 0 - mul
|
||||
flash_store_U32((uint32_t)&ADC_multipliers[adc_channel], &val);
|
||||
}
|
||||
adc_channel = -1;
|
||||
P("stored\n", s);
|
||||
}
|
||||
/**
|
||||
* Change divisor
|
||||
* @param v - user value (sensor number)
|
||||
* @param s - active sendfunction
|
||||
*/
|
||||
void try_ch_divmul(int32_t v, sendfun s){
|
||||
if(v > ADC_CHANNELS_NUMBER || v < 0){
|
||||
P("wrong channel number\n", s);
|
||||
adc_channel = -1;
|
||||
return; // error
|
||||
}
|
||||
adc_channel = v;
|
||||
I = ch_divmul;
|
||||
read_int(NULL, 0); //start reading next int
|
||||
}
|
||||
|
||||
void parce_incoming_buf(char *buf, int len, sendfun s){
|
||||
uint8_t command;
|
||||
//uint32_t utmp;
|
||||
int i = 0, j, m;
|
||||
lastsendfun = s;
|
||||
if(Uval_ready == UVAL_START){ // we are in process of user's value reading
|
||||
i += read_int(buf, len);
|
||||
}
|
||||
if(Uval_ready == UVAL_ENTERED){
|
||||
P("confirm entered value (+/-): ", s);
|
||||
print_int(User_value, s); // printout readed integer value for error control
|
||||
Uval_ready = UVAL_PRINTED;
|
||||
}
|
||||
if(I && Uval_ready == UVAL_CHECKED){
|
||||
Uval_ready = UVAL_BAD; // clear Uval_ready
|
||||
I(User_value, s);
|
||||
return;
|
||||
}
|
||||
for(; i < len; i++){
|
||||
command = buf[i];
|
||||
@ -201,15 +235,14 @@ void parce_incoming_buf(char *buf, int len, sendfun s){
|
||||
dump_flash_data(s);
|
||||
break;
|
||||
case 'd': // change ADC_divisor
|
||||
;
|
||||
div_mul = 1; //divisors
|
||||
I = try_ch_divmul;
|
||||
READINT();
|
||||
break;
|
||||
case 'm': // change ADC_multiplier
|
||||
;
|
||||
break;
|
||||
case 'z': // temporary: refresh
|
||||
flash_status = check_flash_data();
|
||||
print_int(flash_status, s);
|
||||
s('\n');
|
||||
div_mul = 0; // multipliers
|
||||
I = try_ch_divmul;
|
||||
READINT();
|
||||
break;
|
||||
case '\n': // show newline as is
|
||||
break;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user