Loosing some bytes on USART transmission with STM32L1XX

110 views Asked by At

I'm facing a problem with the communication between my computer and my board stm32L152RE. I try to send a string of 1024 bytes and write it on my flash board. For that, i send packets of 32 bytes. The algorithm is simple :

  • I erase my memory with enought space to write my string of 1024
  • I receive 32 bytes in a string buffer
  • I write them on the flash

I do that until i receive 1024 bytes and the I stop.

The problem is , when i print what i have in memory step by step, I only receive 16 bytes and i don't know why

Here's my code, maybe it will help understand my program

/*
 * 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;
}
0

There are 0 answers