Page 1 of 1

how to set the GPNVM1 through programming with c++

Posted: Thu Mar 08, 2012 2:59 pm
by mininiu5216
hello
I want to program using sam-ba.dll by myself to achieve the functional like the SAM-BA that could upload file to AT91SAM3U4-EK。I know that the program can load from flash 0 throuth set GPNVM0、GPNVM1、GPNVM2,However,I don't know how to write the code with c++. So I need your help ,thank you!

Re: how to set the GPNVM1 through programming with c++

Posted: Fri Mar 09, 2012 3:29 am
by mininiu5216
mininiu5216 wrote:hello
I want to program using sam-ba.dll by myself to achieve the functional like the SAM-BA that could upload file to AT91SAM3U4-EK。I know that the program can load from flash 0 throuth set GPNVM0、GPNVM1、GPNVM2,However,I don't know how to write the code with c++. So I need your help ,thank you!

Re: how to set the GPNVM1 through programming with c++

Posted: Fri Mar 09, 2012 2:09 pm
by przemekbary
Hi!
I am not familiar with the SAM3 family, but I wrote a piece of code for SAM7 that worked like a bootloader. The firmware was downloaded via GSM modem. Then the processor reprogrammed itself.

In your case, you'd like to use USART to achieve similar? Right? If this is the case, I can send you *.h and *.cpp files to study. It might be a good starting point.

Best regards
Przemyslaw Baranski

Re: how to set the GPNVM1 through programming with c++

Posted: Sat Mar 10, 2012 2:06 pm
by mininiu5216
Hi
thank you for your reply.I want to program a little tool by myself which can upload the .bin to the flash through USB.Then the program can start and run from flash .
To realize this function,First,we should initialize flash through the applet and set GPNVM1 (my idea).Is there some experience for the question to you?
thanks!

Re: how to set the GPNVM1 through programming with c++

Posted: Sat Mar 10, 2012 2:47 pm
by przemekbary
Hi!
This is not easy but doable.
1) I'd suggest USART instead of USB. It's much easier. You can use FTDI to convert from USB<->UART

2) The program should be properly structured:
a) Reset
b) Basic initialization
c) Bootloader - check through USART if new software available. If so download it and reprogram "User program". If not jump to "User program"
d) User program

3) There are following things that should be considered:
a) during reprogramming the flash, the bootloader should be run from RAM. The FLASH memory cannot be reprogrammed and used at the same time
b) You might want to check the correctness of the *.bin by adding some control sum, e.g. 16-bit CRC
c) You make the whole updating procedure immune against unexpected events like power failure etc in which case the procedure should recover from it. After reflashing, the bootloader should check if the program was correctly flashed.

4) Herewith I attach basic files for FLASH operations - setting, clearing FLASH bits, programming FLASH pages etc. Note that the flashram.cpp should be run from RAM. These files were written for SAM7 uC. I guess, they can be easily adapted for your uC. At least, that's a good starting point.

5) You can use fromelf.exe program to convert from *.hex to *.bin.

6) Please, study the files and refer to the documentation. Should the need arise, you can ask here

Re: how to set the GPNVM1 through programming with c++

Posted: Thu Apr 19, 2012 2:08 am
by CptTitanic
You know those aren't CRC (Cyclic Redundancy Check) routines, but rather a VERY weak parity check, right?

This is what a CRC looks like using one of the standard 16-bit polynomials. This is a bitwise method, but could be done faster with 4-bit or 8-bit parallel methods using tables.

Code: Select all

uint16 CRC16(uint16 crc, int count, uint8 *buffer)
{
  int i;

  while(count--)
  {
    crc ^= ((uint16)*buffer++) << 8;
    i = 8;
    while(i--)
      if (crc & 0x8000)
        crc = (crc << 1) ^ 0x1021;
      else
        crc <<= 1;
  }
  return crc;
}

printf("CRC16 %04X\n", CRC16(0xFFFF,0x200,SectorBuffer));