简体   繁体   English

从 flash memory STM32F1 读取特定地址的内容

[英]Read content of a specific address from flash memory STM32F1

I need to read the content of a specific address from the FLASH memory of a STM32F1 microcontroller.我需要从 STM32F1 微控制器的 FLASH memory 中读取特定地址的内容。 Each address contains 32 bits, so I would like to know if I can do the following:每个地址包含 32 位,所以我想知道是否可以执行以下操作:

uint32_t addr = 0X0801F000;
read_value = (*((uint32*)(addr)));

or should I do something like this:或者我应该做这样的事情:

uint32_t addr = 0X0801F000;
read_value = *(unsigned char *)addr;  // this in a loop since char is 8 bits?

It can be written it like this (but see below for a beeter idea):可以这样写(但请参阅下面的更详细的想法):

uint32_t *addr = 0X0801F000;
uint32_t read_value = *addr;

If you cast addr as an unsigned char * like you do in your second example, then, when you dereference the unsigned char pointer, you get an unsigned char:如果您将addrunsigned char *就像您在第二个示例中所做的那样,那么,当您取消引用 unsigned char 指针时,您会得到一个 unsigned char:

uint32_t* addr = 0X0801F000;
unsigned char read_value = *(unsigned char *)addr;

So that's not what you want because then you only read one character.所以这不是你想要的,因为你只会读到一个字符。 Then, there is one other thing you should remember, you need volatile if you want the compiler to read the memory address every single time you dereference the pointer.然后,您应该记住另一件事,如果您希望编译器每次取消引用指针时都读取 memory 地址,则需要volatile Otherwise, the compile could skip that if it already knows that the value is.否则,如果它已经知道该值是,则编译可以跳过该值。 Then you would have to write it like this:然后你必须这样写:

volatile uint32_t *addr = 0X0801F000;
uint32_t read_value = *addr;

Or if you put it all on one line (like you did in your comment):或者,如果您将所有内容放在一行中(就像您在评论中所做的那样):

uint32_t read_value = (*((volatile uint32_t*)(0x0801F000)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM