mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-03-22 01:31:21 +03:00
SteppersCAN: add CAN & start working on steppers
This commit is contained in:
@@ -20,143 +20,200 @@
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
#include "stm32f0.h"
|
||||
|
||||
/**
|
||||
ATTENTION!!
|
||||
This things works only if you will add next section:
|
||||
|
||||
.myvars :
|
||||
{
|
||||
. = ALIGN(1024);
|
||||
__varsstart = ABSOLUTE(.);
|
||||
KEEP(*(.myvars));
|
||||
} > rom
|
||||
|
||||
after section .data
|
||||
*/
|
||||
#include <stm32f0.h>
|
||||
#include "adc.h"
|
||||
#include "flash.h"
|
||||
#include "proto.h" // printout
|
||||
#include <string.h> // memcpy
|
||||
|
||||
#include "flash.h"
|
||||
#include "usart.h"
|
||||
// max amount of Config records stored (will be recalculate in flashstorage_init()
|
||||
static uint32_t maxCnum = FLASH_BLOCK_SIZE / sizeof(user_conf);
|
||||
|
||||
// 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);
|
||||
#define USERCONF_INITIALIZER { \
|
||||
.userconf_sz = sizeof(user_conf) \
|
||||
,.defflags = 0 \
|
||||
,.CANspeed = 100 \
|
||||
}
|
||||
|
||||
user_conf the_conf = {
|
||||
.userconf_sz = sizeof(user_conf)
|
||||
,.devID = 0
|
||||
,.v12numerator = 1
|
||||
,.v12denominator = 1
|
||||
,.i12numerator = 1
|
||||
,.i12denominator = 1
|
||||
,.v33denominator = 1
|
||||
,.v33numerator = 1
|
||||
,.ESW_thres = 150
|
||||
};
|
||||
static int erase_flash(const void*, const void*);
|
||||
static int write2flash(const void*, const void*, uint32_t);
|
||||
// don't write `static` here, or get error:
|
||||
// 'memcpy' forming offset 8 is out of the bounds [0, 4] of object '__varsstart' with type 'uint32_t'
|
||||
const user_conf *Flash_Data = (const user_conf *)(&__varsstart);
|
||||
|
||||
static int erase_flash();
|
||||
user_conf the_conf = USERCONF_INITIALIZER;
|
||||
|
||||
static int get_gooddata(){
|
||||
user_conf *c = (user_conf*) FLASH_CONF_START_ADDR;
|
||||
// have data - move it to `the_conf`
|
||||
int idx;
|
||||
//write2trbuf("get_gooddata()\n");
|
||||
for(idx = 0; idx < maxnum; ++idx){ // find current settings index - first good
|
||||
uint16_t sz = c[idx].userconf_sz;
|
||||
/*write2trbuf("idx=");
|
||||
put_int((int32_t) idx);
|
||||
write2trbuf(", sz=");
|
||||
put_uint((uint32_t) sz);
|
||||
write2trbuf(", devID=");
|
||||
put_uint((uint32_t) c[idx].devID);
|
||||
write2trbuf(", ESW_thres=");
|
||||
put_uint((uint32_t) c[idx].ESW_thres);
|
||||
SENDBUF();*/
|
||||
if(sz != sizeof(user_conf)){
|
||||
if(sz == 0xffff) break; // first clear
|
||||
else{
|
||||
return -2; // flash corrupt, need to erase
|
||||
static int currentconfidx = -1; // index of current configuration
|
||||
|
||||
/**
|
||||
* @brief binarySearch - binary search in flash for last non-empty cell
|
||||
* any struct searched should have its sizeof() @ the first field!!!
|
||||
* @param l - left index
|
||||
* @param r - right index (should be @1 less than last index!)
|
||||
* @param start - starting address
|
||||
* @param stor_size - size of structure to search
|
||||
* @return index of non-empty cell or -1
|
||||
*/
|
||||
static int binarySearch(int r, const uint8_t *start, int stor_size){
|
||||
int l = 0;
|
||||
while(r >= l){
|
||||
int mid = l + (r - l) / 2;
|
||||
const uint8_t *s = start + mid * stor_size;
|
||||
if(*((const uint16_t*)s) == stor_size){
|
||||
if(*((const uint16_t*)(s + stor_size)) == 0xffff){
|
||||
return mid;
|
||||
}else{ // element is to the right
|
||||
l = mid + 1;
|
||||
}
|
||||
}else{ // element is to the left
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return idx-1; // -1 if there's no data at all & flash is clear; maxnum-1 if flash is full
|
||||
return -1; // not found
|
||||
}
|
||||
|
||||
void get_userconf(){
|
||||
user_conf *c = (user_conf*) FLASH_CONF_START_ADDR;
|
||||
int idx = get_gooddata();
|
||||
if(idx < 0) return; // no data stored
|
||||
memcpy(&the_conf, &c[idx], sizeof(user_conf));
|
||||
/**
|
||||
* @brief flashstorage_init - initialization of user conf storage
|
||||
* run in once @ start
|
||||
*/
|
||||
void flashstorage_init(){
|
||||
if(FLASH_SIZE > 0 && FLASH_SIZE < 20000){
|
||||
uint32_t flsz = FLASH_SIZE * 1024; // size in bytes
|
||||
flsz -= (uint32_t)(&__varsstart) - FLASH_BASE;
|
||||
maxCnum = flsz / sizeof(user_conf);
|
||||
//SEND("flsz="); printu(flsz);
|
||||
//SEND("\nmaxCnum="); printu(maxCnum); newline(); sendbuf();
|
||||
}
|
||||
// -1 if there's no data at all & flash is clear; maxnum-1 if flash is full
|
||||
currentconfidx = binarySearch((int)maxCnum-2, (const uint8_t*)Flash_Data, sizeof(user_conf));
|
||||
if(currentconfidx > -1){
|
||||
memcpy(&the_conf, &Flash_Data[currentconfidx], sizeof(user_conf));
|
||||
}
|
||||
}
|
||||
|
||||
// store new configuration
|
||||
// @return 0 if all OK
|
||||
int store_userconf(){
|
||||
// maxnum - 3 means that there always should be at least one empty record after last data
|
||||
// for binarySearch() checking that there's nothing more after it!
|
||||
if(currentconfidx > (int)maxCnum - 3){ // there's no more place
|
||||
currentconfidx = 0;
|
||||
if(erase_flash(Flash_Data, (&__varsstart))) return 1;
|
||||
}else ++currentconfidx; // take next data position (0 - within first run after firmware flashing)
|
||||
return write2flash((const void*)&Flash_Data[currentconfidx], &the_conf, sizeof(the_conf));
|
||||
}
|
||||
|
||||
static int write2flash(const void *start, const void *wrdata, uint32_t stor_size){
|
||||
int ret = 0;
|
||||
user_conf *c = (user_conf*) FLASH_CONF_START_ADDR;
|
||||
int idx = get_gooddata();
|
||||
if(idx == -2 || idx == maxnum - 1){ // data corruption or there's no more place
|
||||
idx = 0;
|
||||
if(erase_flash()) return 1;
|
||||
}else ++idx; // take next data position
|
||||
/*write2trbuf("store_userconf()\nidx=");
|
||||
put_int((int32_t) idx);
|
||||
SENDBUF();*/
|
||||
if (FLASH->CR & FLASH_CR_LOCK){ // unloch flash
|
||||
FLASH->KEYR = FLASH_FKEY1;
|
||||
FLASH->KEYR = FLASH_FKEY2;
|
||||
FLASH->KEYR = FLASH_KEY1;
|
||||
FLASH->KEYR = FLASH_KEY2;
|
||||
}
|
||||
while (FLASH->SR & FLASH_SR_BSY);
|
||||
if(FLASH->SR & FLASH_SR_WRPERR) return 1; // write protection
|
||||
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR; // clear all flags
|
||||
if(FLASH->SR & FLASH_SR_WRPRTERR){
|
||||
MSG("Can't remove write protection\n");
|
||||
return 1; // write protection
|
||||
}
|
||||
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; // clear all flags
|
||||
FLASH->CR |= FLASH_CR_PG;
|
||||
uint16_t *data = (uint16_t*) &the_conf;
|
||||
uint16_t *address = (uint16_t*) &c[idx];
|
||||
uint32_t i, count = sizeof(user_conf) / 2;
|
||||
const uint16_t *data = (const uint16_t*) wrdata;
|
||||
volatile uint16_t *address = (volatile uint16_t*) start;
|
||||
uint32_t i, count = (stor_size + 1) / 2;
|
||||
for (i = 0; i < count; ++i){
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
*(volatile uint16_t*)(address + i) = data[i];
|
||||
while (FLASH->SR & FLASH_SR_BSY);
|
||||
if(FLASH->SR & FLASH_SR_PGERR) ret = 1; // program error - meet not 0xffff
|
||||
else while (!(FLASH->SR & FLASH_SR_EOP));
|
||||
/*write2trbuf("write byte ");
|
||||
put_int((int32_t) i);
|
||||
write2trbuf(", write value=");
|
||||
put_uint(data[i]);
|
||||
write2trbuf(", read value=");
|
||||
put_uint(address[i]);
|
||||
SENDBUF();
|
||||
if(ret){
|
||||
write2trbuf("PGERR");
|
||||
SENDBUF();*/
|
||||
}
|
||||
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR;
|
||||
if(FLASH->SR & FLASH_SR_PGERR){
|
||||
ret = 1; // program error - meet not 0xffff
|
||||
MSG("FLASH_SR_PGERR\n");
|
||||
break;
|
||||
}else while (!(FLASH->SR & FLASH_SR_EOP));
|
||||
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR;
|
||||
}
|
||||
FLASH->CR |= FLASH_CR_LOCK; // lock it back
|
||||
FLASH->CR &= ~(FLASH_CR_PG);
|
||||
MSG("Flash stored\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int erase_flash(){
|
||||
/**
|
||||
* @brief erase_flash - erase N pages of flash memory
|
||||
* @param start - first address
|
||||
* @param end - last address (or NULL if need to erase all flash remaining)
|
||||
* @return 0 if succeed
|
||||
*/
|
||||
static int erase_flash(const void *start, const void *end){
|
||||
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 */
|
||||
/* (4) Perform unlock sequence */
|
||||
while ((FLASH->SR & FLASH_SR_BSY) != 0){} /* (1) */
|
||||
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR; /* (2) */
|
||||
/* if (FLASH->SR & FLASH_SR_EOP){
|
||||
FLASH->SR |= FLASH_SR_EOP;
|
||||
}*/
|
||||
if ((FLASH->CR & FLASH_CR_LOCK) != 0){ /* (3) */
|
||||
FLASH->KEYR = FLASH_FKEY1; /* (4) */
|
||||
FLASH->KEYR = FLASH_FKEY2;
|
||||
uint32_t nblocks = 1, flsz = 0;
|
||||
if(!end){ // erase all remaining
|
||||
if(FLASH_SIZE > 0 && FLASH_SIZE < 20000){
|
||||
flsz = FLASH_SIZE * 1024; // size in bytes
|
||||
flsz -= (uint32_t)start - FLASH_BASE;
|
||||
}
|
||||
}else{ // erase a part
|
||||
flsz = (uint32_t)end - (uint32_t)start;
|
||||
}
|
||||
/* (1) Set the PER bit in the FLASH_CR register to enable page erasing */
|
||||
/* (2) Program the FLASH_AR register to select a page to erase */
|
||||
/* (3) Set the STRT bit in the FLASH_CR register to start the erasing */
|
||||
/* (4) Wait until the EOP flag in the FLASH_SR register set */
|
||||
/* (5) Clear EOP flag by software by writing EOP at 1 */
|
||||
/* (6) Reset the PER Bit to disable the page erase */
|
||||
FLASH->CR |= FLASH_CR_PER; /* (1) */
|
||||
FLASH->AR = FLASH_CONF_START_ADDR; /* (2) */
|
||||
FLASH->CR |= FLASH_CR_STRT; /* (3) */
|
||||
while(!(FLASH->SR & FLASH_SR_EOP));
|
||||
FLASH->SR |= FLASH_SR_EOP; /* (5)*/
|
||||
if(FLASH->SR & FLASH_SR_WRPERR){ /* Check Write protection error */
|
||||
ret = 1;
|
||||
FLASH->SR |= FLASH_SR_WRPERR; /* Clear the flag by software by writing it at 1*/
|
||||
nblocks = flsz / FLASH_BLOCK_SIZE;
|
||||
if(nblocks == 0 || nblocks >= FLASH_SIZE) return 1;
|
||||
for(uint32_t i = 0; i < nblocks; ++i){
|
||||
#ifdef EBUG
|
||||
SEND("Try to erase page #"); printu(i); newline(); sendbuf();
|
||||
#endif
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
/* (1) Wait till no operation is on going */
|
||||
/* (2) Clear error & EOP bits */
|
||||
/* (3) Check that the Flash is unlocked */
|
||||
/* (4) Perform unlock sequence */
|
||||
while ((FLASH->SR & FLASH_SR_BSY) != 0){} /* (1) */
|
||||
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; /* (2) */
|
||||
/* if (FLASH->SR & FLASH_SR_EOP){
|
||||
FLASH->SR |= FLASH_SR_EOP;
|
||||
}*/
|
||||
if ((FLASH->CR & FLASH_CR_LOCK) != 0){ /* (3) */
|
||||
FLASH->KEYR = FLASH_KEY1; /* (4) */
|
||||
FLASH->KEYR = FLASH_KEY2;
|
||||
}
|
||||
/* (1) Set the PER bit in the FLASH_CR register to enable page erasing */
|
||||
/* (2) Program the FLASH_AR register to select a page to erase */
|
||||
/* (3) Set the STRT bit in the FLASH_CR register to start the erasing */
|
||||
/* (4) Wait until the EOP flag in the FLASH_SR register set */
|
||||
/* (5) Clear EOP flag by software by writing EOP at 1 */
|
||||
/* (6) Reset the PER Bit to disable the page erase */
|
||||
FLASH->CR |= FLASH_CR_PER; /* (1) */
|
||||
FLASH->AR = (uint32_t)Flash_Data + i*FLASH_BLOCK_SIZE; /* (2) */
|
||||
FLASH->CR |= FLASH_CR_STRT; /* (3) */
|
||||
while(!(FLASH->SR & FLASH_SR_EOP));
|
||||
FLASH->SR |= FLASH_SR_EOP; /* (5)*/
|
||||
if(FLASH->SR & FLASH_SR_WRPRTERR){ /* Check Write protection error */
|
||||
ret = 1;
|
||||
MSG("Write protection error!\n");
|
||||
FLASH->SR |= FLASH_SR_WRPRTERR; /* Clear the flag by software by writing it at 1*/
|
||||
break;
|
||||
}
|
||||
FLASH->CR &= ~FLASH_CR_PER; /* (6) */
|
||||
}
|
||||
FLASH->CR &= ~FLASH_CR_PER; /* (6) */
|
||||
return ret;
|
||||
}
|
||||
|
||||
void dump_userconf(){
|
||||
SEND("userconf_addr="); printuhex((uint32_t)Flash_Data);
|
||||
SEND("\nuserconf_sz="); printu(the_conf.userconf_sz);
|
||||
SEND("\nflags="); printuhex(the_conf.defflags);
|
||||
SEND("\nCANspeed="); printu(the_conf.CANspeed);
|
||||
newline();
|
||||
sendbuf();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* flash.h
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||
@@ -25,22 +24,27 @@
|
||||
#ifndef __FLASH_H__
|
||||
#define __FLASH_H__
|
||||
|
||||
typedef struct{
|
||||
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
|
||||
uint16_t v12numerator; // 12V to motors
|
||||
uint16_t v12denominator;
|
||||
uint16_t i12numerator; // motors' current
|
||||
uint16_t i12denominator;
|
||||
uint16_t v33numerator; // 3.3V (vref)
|
||||
uint16_t v33denominator;
|
||||
#include "hardware.h"
|
||||
|
||||
#define FLASH_BLOCK_SIZE (1024)
|
||||
#define FLASH_SIZE_REG ((uint32_t)0x1FFFF7CC)
|
||||
#define FLASH_SIZE *((uint16_t*)FLASH_SIZE_REG)
|
||||
|
||||
/*
|
||||
* struct to save user configurations
|
||||
*/
|
||||
typedef struct __attribute__((packed, aligned(4))){
|
||||
uint16_t userconf_sz; // "magick number"
|
||||
uint8_t defflags; // default flags
|
||||
uint16_t CANspeed; // default CAN speed
|
||||
} user_conf;
|
||||
|
||||
extern user_conf the_conf;
|
||||
extern user_conf the_conf; // global user config (read from FLASH to RAM)
|
||||
// data from ld-file: start address of storage
|
||||
extern const uint32_t __varsstart;
|
||||
|
||||
void get_userconf();
|
||||
void flashstorage_init();
|
||||
int store_userconf();
|
||||
void dump_userconf();
|
||||
|
||||
#endif // __FLASH_H__
|
||||
|
||||
@@ -4,7 +4,6 @@ void printuhex(uint32_t val){
|
||||
uint8_t *ptr = (uint8_t*)&val + 3;
|
||||
int8_t i, j;
|
||||
for(i = 0; i < 4; ++i, --ptr){
|
||||
if(*ptr == 0 && i != 3) continue; // omit leading zeros
|
||||
for(j = 1; j > -1; --j){
|
||||
uint8_t half = (*ptr >> (4*j)) & 0x0f;
|
||||
if(half < 10) bufputchar(half + '0');
|
||||
|
||||
Reference in New Issue
Block a user