flash r/w

This commit is contained in:
Conor Patrick 2018-10-20 21:02:59 -04:00
parent 2bb4a9fe16
commit 2108f0f286
4 changed files with 132 additions and 10 deletions

View File

@ -2,7 +2,7 @@ CC=arm-none-eabi-gcc
CP=arm-none-eabi-objcopy
SZ=arm-none-eabi-size
SRC=src/main.c src/init.c src/redirect.c src/startup_stm32l432xx.s src/system_stm32l4xx.c $(wildcard lib/*c)
SRC=src/main.c src/init.c src/redirect.c src/flash.c src/startup_stm32l432xx.s src/system_stm32l4xx.c $(wildcard lib/*c)
OBJ=$(SRC:.c=.o)
INC=-Isrc/ -Isrc/cmsis -Ilib/
LDSCRIPT=stm32l432xx.ld

View File

@ -0,0 +1,93 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "stm32l4xx.h"
#include "app.h"
#include "flash.h"
static void flash_unlock()
{
if (FLASH->CR & FLASH_CR_LOCK)
{
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
}
void flash_erase_page(uint8_t page)
{
__disable_irq();
// Wait if flash is busy
while (FLASH->SR & (1<<16))
;
FLASH->SR = FLASH->SR;
// enable flash erase and select page
FLASH->CR &= ~((0xff<<3) | 7);
FLASH->CR |= (page<<3) | (1<<1);
// Go!
FLASH->CR |= (1<<16);
while (FLASH->SR & (1<<16))
;
if(FLASH->SR & (1<<1))
{
printf("erase NOT successful %x\r\n", FLASH->SR);
}
FLASH->CR &= ~(0x7);
__enable_irq();
}
void flash_write_dword(uint32_t addr, uint64_t data)
{
__disable_irq();
while (FLASH->SR & (1<<16))
;
FLASH->SR = FLASH->SR;
// Select program action
FLASH->CR |= (1<<0);
*(volatile uint32_t*)addr = data;
*(volatile uint32_t*)(addr+4) = data>>32;
while (FLASH->SR & (1<<16))
;
if(FLASH->SR & (1<<1))
{
printf("program NOT successful %x\r\n", FLASH->SR);
}
FLASH->SR = (1<<0);
FLASH->CR &= ~(1<<0);
__enable_irq();
}
void flash_write(uint32_t addr, uint8_t * data, size_t sz)
{
int i,j;
uint8_t buf[8];
// dword align
addr &= ~(0x7);
for(i = 0; i < sz; i+=8)
{
memmove(buf, data + i, (sz - i) > 8 ? 8 : sz - i);
if (sz - i < 8)
{
memset(buf + sz - i, 0xff, 8 - (sz - i));
}
flash_write_dword(addr, *(uint64_t*)buf);
addr += 8;
}
}
void flash_lock()
{
FLASH->CR |= (1<<31);
}

View File

@ -0,0 +1,15 @@
#ifndef _FLASH_H_H
#define _FLASH_H_H
void flash_erase_page(uint8_t page);
void flash_write_dword(uint32_t addr, uint64_t data);
void flash_write(uint32_t addr, uint8_t * data, size_t sz);
#define FLASH_PAGE_SIZE 2048
#define flash_addr(page) (0x08000000 + ((page)*FLASH_PAGE_SIZE))
#define FLASH_PAGE_START 0
#define FLASH_PAGE_END 127
#endif

View File

@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "stm32l4xx.h"
#include "stm32l4xx_ll_gpio.h"
#include "stm32l4xx_ll_rcc.h"
@ -13,6 +14,7 @@
#include "stm32l4xx_ll_tim.h"
#include "app.h"
#include "flash.h"
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
@ -69,7 +71,7 @@ void test_colors()
while(1)
{
printf("%d: %d\r\n", j++, millis());
printf("%d: %lu\r\n", j++, millis());
printf("white pulse\r\n");
time = millis();
@ -126,13 +128,10 @@ void test_colors()
update();
rgb((i<<8) | (i<<0));
}
}
}
uint32_t __65_seconds = 0;
void TIM6_DAC_IRQHandler()
{
@ -143,9 +142,11 @@ void TIM6_DAC_IRQHandler()
int main(void)
{
uint8_t str[] = "YouCompleteMe: a code-completion engine for Vim";
uint8_t buf[500];
uint32_t i = 0;
int inc = 1;
hw_init();
printf("hello solo\r\n");
/*LL_GPIO_SetPinMode(LED_PORT, LED_PIN_R, LL_GPIO_MODE_OUTPUT);*/
@ -155,15 +156,28 @@ int main(void)
/*LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_R);*/
/*LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_G);*/
/*LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_B);*/
test_colors();
// Test flash
flash_erase_page(60);
flash_write(flash_addr(60), str, sizeof(str));
memmove(buf,(uint8_t*)flash_addr(60),sizeof(str));
printf("flash: \"%s\"\r\n", buf);
// test timer
uint32_t t1 = millis();
delay(100);
uint32_t t2 = millis();
printf("100 ms delay (%lu)\r\n",t2-t1);
// Test PWM + weighting of RGB
test_colors();
while (1)
{
rgb(i | (i << 8) | (i << 16));
delay(100);
printf("%d: %d\r\n", i++, millis());
delay(1000);
printf("%lu: %lu\r\n", i+=50, millis());
}
}