我的计算机和我的主板stm32L152RE之间的通信问题。 我尝试发送一个1024字节的字符串并将其写在我的闪存板上。 为此,我发送32字节的数据包。 算法很简单:
我这样做直到我收到1024个字节并且我停止。
问题是,当我逐步打印内存中的内容时,我只收到16个字节,我不知道为什么
这是我的代码,也许它有助于理解我的程序
/*
* bootloader.c
*
* Created on: 9 juin 2015
* Author: tgloaguen
*/
#include "usart.h"
#include "stm32l1xx_flash.h"
#define WRITE_START_ADDR 0x08000000
#define WRITE_END_ADDR 0x08000FFF
#define FLASH_PAGE_SIZE ((uint16_t)0x100) //If a page is 256 bits
#define MY_BL_FUNCTIONS __attribute__((section(".bootsection")))
void BootLoader(void) MY_BL_FUNCTIONS;
FLASH_Status Flash_Write ( uint32_t StartAddress, uint8_t *p, uint32_t Size ) MY_BL_FUNCTIONS;
uint8_t Flash_Erase() MY_BL_FUNCTIONS;
void Receive_Data(char * buffer,int size)MY_BL_FUNCTIONS;
void Receive_Size(char * buffer, int *sizeData)MY_BL_FUNCTIONS;
void BootLoader(void) {
//clear all ITs
USART_ITConfig( USART1, USART_IT_RXNE, DISABLE );
//SendString("HELLO",USART2);
uint8_t status,i;
char buffer[33];
//en dur
uint16_t *adr = WRITE_START_ADDR;
uint16_t sizeBin = 1024,k = 0,k_hexa = 0x20;
//Receive size of BIN
// Receive_Size(buffer,&sizeBin);
Flash_Erase();
//if sizeBin ok
//receive frames
do
{
//receive 32 bytes
Receive_Data(buffer,32);
GPIO_ToggleBits(GPIOA, GPIO_Pin_5);
// //write a sector (1024k)
status = Flash_Write(adr , buffer, 0x20);
// on check si on ecrit bien au bon endroit
SendString("\r\n ", USART2);
// //increment cpt
k += 32;
adr = adr + k_hexa;
// //check CRC
// //TODO
//
} while (k < sizeBin);
SendString("nickel ", USART2);
}
void Receive_Size(char * buffer, int *sizeData) {
int i = 0;
do {
buffer[i] = Uart2ReadChar();
i++;
} while (buffer[i] != '\0');
*sizeData = (buffer[1] << 8) + buffer[0];
}
void Receive_Data(char * buffer,int size){
int i=0;
do{
buffer[i] = Uart2ReadChar();
i++;
}
while(i < size);
}
uint8_t Flash_Erase() {
uint32_t EraseCounter = 0x00, Address = 0x00;//Erase count, write address
uint32_t NbrOfPage = 0x00;//Recording to erase the pages
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;/*FLASH complete erasure marks*/
/*Unlock FLASH*/
FLASH_Unlock();
/*Calculate the number of FLASH pages need to erase */
NbrOfPage = (WRITE_END_ADDR - WRITE_START_ADDR) / FLASH_PAGE_SIZE;
/* Remove all hang flags */
FLASH_ClearFlag ( FLASH_FLAG_EOP |
FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR |
FLASH_FLAG_SIZERR |
FLASH_FLAG_OPTVERR );
/* Erase the FLASH page*/
for(EraseCounter = 0; (EraseCounter <NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage(WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
}
FLASH_Lock ( );
return (uint8_t)FLASHStatus;
}
FLASH_Status Flash_Write ( uint32_t StartAddress, uint8_t *p, uint32_t Size )
{
uint32_t Address;
__IO FLASH_Status status = FLASH_COMPLETE;
Address = StartAddress;
/* Unlock the FLASH Program memory */
FLASH_Unlock ();
/* Clear all pending flags */
FLASH_ClearFlag ( FLASH_FLAG_EOP |
FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR |
FLASH_FLAG_SIZERR |
FLASH_FLAG_OPTVERR );
while ( Address < StartAddress + Size )
{
status = FLASH_FastProgramWord ( Address, *(uint32_t *)p );
//debug
SendChar('|',USART2);
SendChar(*p,USART2);
Address = Address + 4;
p = p + 4;
if ( status != FLASH_COMPLETE ) return status;
}
/* Lock the FLASH Program memory */
FLASH_Lock ( );
return status;
}