Recovered previous unsorted operations
+ written basic flash driver + included flash docuementation in repo * added flash tests in main
This commit is contained in:
parent
de28e1e8c5
commit
c94ae6f313
Binary file not shown.
77
src/drivers/flash.c
Normal file
77
src/drivers/flash.c
Normal file
@ -0,0 +1,77 @@
|
||||
//driver header
|
||||
#include "flash.h"
|
||||
|
||||
//target header
|
||||
#include "../target/stm32f103xb.h"
|
||||
|
||||
//custom header
|
||||
#include "../config.h"
|
||||
|
||||
int unlock(void) {
|
||||
FLASH->KEYR = FLASH_KEY1;
|
||||
FLASH->KEYR = FLASH_KEY2;
|
||||
|
||||
if(FLASH->CR & (1<<7)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lock(void) {
|
||||
FLASH->CR |= 1<<7;
|
||||
}
|
||||
|
||||
int flash_erase(uint16_t* page_addr) {
|
||||
if(unlock()) return -1;
|
||||
|
||||
// check that flash is not busy
|
||||
if(FLASH->SR & 1<<0) {
|
||||
lock();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// select the page to erase
|
||||
FLASH->CR |= 1<<1;
|
||||
FLASH->AR = (uint32_t)page_addr;
|
||||
|
||||
// start erase
|
||||
FLASH->CR |= 1<<6;
|
||||
|
||||
// wait for the operation to finish
|
||||
while(FLASH->SR & 1<<0);
|
||||
FLASH->CR &= ~(1<<1);
|
||||
lock();
|
||||
|
||||
// check for errors
|
||||
if(!(FLASH->SR & 1<<5)) return -1;
|
||||
FLASH->SR |= 1<<5;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int flash_write(uint16_t* addr, uint16_t* data, size_t data_len) {
|
||||
if(unlock()) return -1;
|
||||
|
||||
// check that flash is not busy
|
||||
if(FLASH->SR & 1<<0) return -1;
|
||||
|
||||
// write the flash
|
||||
for(size_t i=0; i<data_len; ++i) {
|
||||
FLASH->CR |= 1<<0;
|
||||
addr[i] = data[i];
|
||||
while(FLASH->SR & 1<<0);
|
||||
}
|
||||
|
||||
// finish write operation
|
||||
FLASH->CR &= ~(1<<0);
|
||||
lock();
|
||||
|
||||
// check for errors
|
||||
if(!(FLASH->SR & 1<<5)) return -1;
|
||||
FLASH->SR |= 1<<5;
|
||||
|
||||
for(size_t i=0; i<data_len; ++i) {
|
||||
if(addr[i] != data[i]) return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
15
src/drivers/flash.h
Normal file
15
src/drivers/flash.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef FLASH_H
|
||||
#define FLASH_H
|
||||
|
||||
//std headers
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int flash_erase(uint16_t* page_addr);
|
||||
|
||||
int flash_write(uint16_t* addr, uint16_t* data, size_t data_len);
|
||||
|
||||
#endif
|
||||
|
||||
23
src/main.c
23
src/main.c
@ -5,6 +5,7 @@
|
||||
// driver includes
|
||||
#include "drivers/rcc.h"
|
||||
#include "drivers/io.h"
|
||||
#include "drivers/flash.h"
|
||||
|
||||
Clock_t sysclks;
|
||||
#include "drivers/timer.h"
|
||||
@ -26,6 +27,14 @@ static void timeout_cb(void) {
|
||||
/* main function */
|
||||
int main(void) {
|
||||
|
||||
uint16_t* data_addr = (uint16_t*)0x8007C00;
|
||||
uint16_t data[8] = { 0x1234, 0x2345, 0x3456, 0x4567,
|
||||
0x6789, 0x789A, 0x89AB, 0x9ABC };
|
||||
|
||||
if(flash_erase(data_addr)) return -1;
|
||||
if(flash_write(data_addr, data, 4)) return -1;
|
||||
if(flash_erase(data_addr)) return -1;
|
||||
|
||||
// configure clocks (necessary before using timers)
|
||||
rcc_config_clock(CLOCK_CONFIG_PERFORMANCE, &sysclks);
|
||||
|
||||
@ -35,15 +44,15 @@ int main(void) {
|
||||
io_write(GPIOC, 1, PIN_13);
|
||||
|
||||
// start timed interruption
|
||||
timer_tick_init(TIM2, 1000, timeout_cb);
|
||||
timer_tick_init(TIM2, 2000, timeout_cb);
|
||||
timer_start(TIM2);
|
||||
|
||||
uint32_t test = (uint32_t)(&end);
|
||||
test++;
|
||||
int* tab = (int*)malloc(10*sizeof(int));
|
||||
for(int i=0; i<10; ++i) {
|
||||
tab[i] = i;
|
||||
}
|
||||
// uint32_t test = (uint32_t)(&end);
|
||||
// test++;
|
||||
// int* tab = (int*)malloc(10*sizeof(int));
|
||||
// for(int i=0; i<10; ++i) {
|
||||
// tab[i] = i;
|
||||
// }
|
||||
|
||||
// main loop
|
||||
for(;;);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user