mirror of
https://github.com/eddyem/IR-controller.git
synced 2025-12-06 10:45:15 +03:00
added flash support
This commit is contained in:
parent
37b7d5cb16
commit
a54c3e3a66
@ -1,7 +1,7 @@
|
||||
The work begins
|
||||
First PCB have been prodused, so I need "only" to solder elements & finish the code
|
||||
|
||||
Pinout of MCI is in file schematics/STM32_PINS
|
||||
Pinout of MCU is in file schematics/STM32_PINS
|
||||
|
||||
|
||||
-- OLD --
|
||||
|
||||
151
with_opencm3/flash.c
Normal file
151
with_opencm3/flash.c
Normal file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
* flash.c - functions to work with STM32 flash memory
|
||||
*
|
||||
* Copyright 2015 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "flash.h"
|
||||
#include <libopencm3/stm32/flash.h>
|
||||
|
||||
/*
|
||||
* this is a default values of stored data
|
||||
* they could be changed by appropriate command
|
||||
* align by 2k & make size 2k for using with high density devices
|
||||
*/
|
||||
#define FLASH_BLOCK_SIZE (2048)
|
||||
#define FLASH_WRONG_DATA_WRITTEN 0x80
|
||||
|
||||
const uint8_t _flash_buffer[FLASH_BLOCK_SIZE] __attribute__ ((aligned(FLASH_BLOCK_SIZE)));
|
||||
|
||||
/**
|
||||
* 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
|
||||
26, // shutter
|
||||
2 // power
|
||||
},
|
||||
._ADC_divisors = {1,1,1,1,1,1,1,1, // TRD
|
||||
25, // shutter
|
||||
7 // power
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
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();
|
||||
//Erasing page
|
||||
flash_erase_page(page_address);
|
||||
//DBG("erase flash ");
|
||||
/* if(FLASH_SR_EOP != (ret = flash_get_status_flags()))
|
||||
goto endoffunction;
|
||||
*/
|
||||
//DBG("OK\nwrite");
|
||||
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
|
||||
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){
|
||||
uint16_t halfwords[2] = {0,0}, n = 1;
|
||||
if(rem == 1) halfwords[0] = *((uint8_t*)dataptr);
|
||||
else halfwords[0] = *((uint16_t*)dataptr);
|
||||
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;
|
||||
goto endoffunction;
|
||||
}
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* printout all data stored in flash
|
||||
*/
|
||||
void dump_flash_data(sendfun s){
|
||||
int i;
|
||||
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);
|
||||
print_int(ADC_multipliers[i], s);
|
||||
}
|
||||
P("\nADC divisors: ", s);
|
||||
for(i = 0; i < ADC_CHANNELS_NUMBER; i++){
|
||||
if(i) P(", ", s);
|
||||
print_int(ADC_divisors[i], s);
|
||||
}
|
||||
s('\n');
|
||||
}
|
||||
46
with_opencm3/flash.h
Normal file
46
with_opencm3/flash.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* flash.h
|
||||
*
|
||||
* Copyright 2015 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef __FLASH_H__
|
||||
#define __FLASH_H__
|
||||
|
||||
#include "main.h"
|
||||
#include "user_proto.h"
|
||||
|
||||
#define FLASH_MAGICK ((uint32_t) 0xAA55A55A)
|
||||
|
||||
typedef struct{
|
||||
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];
|
||||
} flash_data;
|
||||
|
||||
extern flash_data *Stored_Data;
|
||||
|
||||
#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);
|
||||
|
||||
#endif // __FLASH_H__
|
||||
@ -28,8 +28,8 @@
|
||||
#include "main.h"
|
||||
#include "hardware_ini.h"
|
||||
#include "onewire.h"
|
||||
#include "flash.h"
|
||||
|
||||
#define ADC_CHANNELS_NUMBER 10
|
||||
/*
|
||||
* Due to inconvenient pins position on STM32F103VxT6 I had to make this strange location:
|
||||
* my channel # -> ADC1/2 channel #
|
||||
@ -223,9 +223,10 @@ void ADC_calibrate_and_start(){
|
||||
* ==> approximately this is equal to val*26/25 or val + val/25
|
||||
*/
|
||||
int shutter_voltage(){
|
||||
int val = SHUTTER_SENSE_VALUE;
|
||||
val += val/25;
|
||||
return val;
|
||||
uint32_t val = ADC_value[SHUTTER_SENSE_NUMBER]; // 8
|
||||
val *= ADC_multipliers[SHUTTER_SENSE_NUMBER];
|
||||
val /= ADC_divisors[SHUTTER_SENSE_NUMBER];
|
||||
return (int)val;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -235,9 +236,10 @@ int shutter_voltage(){
|
||||
* ==> approximately this is equal to val*2/7
|
||||
*/
|
||||
int power_voltage(){
|
||||
int val = POWER_SENSE_VALUE * 2;
|
||||
val /= 7;
|
||||
return val;
|
||||
uint32_t val = ADC_value[POWER_SENSE_NUMBER]; // 9
|
||||
val *= ADC_multipliers[POWER_SENSE_NUMBER];
|
||||
val /= ADC_divisors[POWER_SENSE_NUMBER];
|
||||
return (int)val;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -248,8 +250,8 @@ int power_voltage(){
|
||||
*/
|
||||
int TRD_value(uint8_t num){
|
||||
uint32_t v = ADC_value[num];
|
||||
uint32_t r = 100000 * v;
|
||||
r /= (uint32_t)(4096 - v);
|
||||
uint32_t r = v * ADC_multipliers[num];
|
||||
r /= (uint32_t)(4096 - v) * ADC_divisors[num];
|
||||
return (int) r;
|
||||
}
|
||||
|
||||
|
||||
@ -128,8 +128,8 @@ void ADC_calibrate_and_start();
|
||||
#define SHUTTER_OP_DELAY (200)
|
||||
|
||||
// ADC_value[8] is U36, ADC_value[9] is U10
|
||||
#define SHUTTER_SENSE_VALUE (ADC_value[8])
|
||||
#define POWER_SENSE_VALUE (ADC_value[9])
|
||||
#define SHUTTER_SENSE_NUMBER (8)
|
||||
#define POWER_SENSE_NUMBER (9)
|
||||
int shutter_voltage();
|
||||
int power_voltage();
|
||||
int TRD_value(uint8_t num);
|
||||
|
||||
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;
|
||||
uint32_t ad7794_on = 0, flash_status = 1;
|
||||
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,6 +175,7 @@ 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
|
||||
@ -200,8 +201,7 @@ int main(){
|
||||
|
||||
if(Timer - Old_timer > 999){ // one-second cycle
|
||||
Old_timer += 1000;
|
||||
if(Shutter_State == SHUTTER_NOTREADY)
|
||||
shutter_init();
|
||||
// if(Shutter_State == SHUTTER_NOTREADY) shutter_init();
|
||||
//OW_fill_ID(0);
|
||||
//gpio_toggle(GPIOC, GPIO12); // toggle LED
|
||||
//gpio_toggle(GPIO_BANK_SPI2_MOSI, GPIO_SPI2_MOSI);
|
||||
|
||||
@ -37,7 +37,10 @@
|
||||
#include <libopencm3/stm32/dma.h>
|
||||
#include <libopencm3/stm32/spi.h>
|
||||
|
||||
#define ADC_CHANNELS_NUMBER (10)
|
||||
|
||||
#include "sync.h" // mutexes
|
||||
#include "flash.h"
|
||||
#include "user_proto.h"
|
||||
#include "AD7794.h"
|
||||
#include "onewire.h"
|
||||
@ -52,6 +55,7 @@
|
||||
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();
|
||||
|
||||
|
||||
@ -197,9 +197,19 @@ void parce_incoming_buf(char *buf, int len, sendfun s){
|
||||
case 'o': // open shutter
|
||||
try_to_open_shutter();
|
||||
break;
|
||||
case 'z': // temporary: change delay
|
||||
I = set_shtr_delay;
|
||||
READINT();
|
||||
case 'F': // dump flash data
|
||||
dump_flash_data(s);
|
||||
break;
|
||||
case 'd': // change ADC_divisor
|
||||
;
|
||||
break;
|
||||
case 'm': // change ADC_multiplier
|
||||
;
|
||||
break;
|
||||
case 'z': // temporary: refresh
|
||||
flash_status = check_flash_data();
|
||||
print_int(flash_status, s);
|
||||
s('\n');
|
||||
break;
|
||||
case '\n': // show newline as is
|
||||
break;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user