title: How to implement IAP (in application programming) for STM23F107 ARM microcontroller?
body: <p>I want to design a board with STM32F107 (or other STM32F microcontrollers) with the capability of <strong>IAP</strong> from <em>SD card</em>, <em>USB</em>, and a <em>serial flash</em> (like AT45DB161)</p>
  
  <ul>
  <li>In the following paragraph, I'll try to describe my solution. <strong><em>Please correct me if I am wrong.</em></strong></li>
  </ul>
  
  <p>As I know (<a href="http://www.st.com/web/en/resource/technical/document/datasheet/CD00220364.pdf" rel="nofollow">STM32F107 datasheet</a>, section 2.3.8), STM32F107 can boot from <strong>system memory</strong>, <strong>SRAM</strong>, or <strong>user flash</strong> based on BOOT pines settings (BOOT0, and BOOT1[PB7]).</p>
  
  <p>According to "<em>STM32 microcontroller system memory boot mode</em>" document (<a href="http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf" rel="nofollow">STM32 Application note AN2606</a> page 55, Fig. 15), the ROM bootloader of STM32 checks USB, USART, and CAN buses for signal availability, and goes on ... (system memory boot mode is activated when "BOOT1_BOOT0 = 01").</p>
  
  <p>Therefore when I want to program my STM32 using USB port (not a USB stick), I must set boot pins to "BOOT1_BOOT0 = 01" and connect a usb programmer to the board and ... (and this operation is a kind of ISP -in system programming- method, <strong><em>is'nt it?</em></strong>)</p>
  
  <p>However, to be able to program my STM32 from a USB stick or an SD card, I think STM32 must first boot from <em>user flash</em> (a custom bootloader program), check a flag (say, a byte in flash memory) to see if it should check the disk file system (USB stick or SD card) or serial flash for a new version of firmware.</p>
  
  <ul>
  <li><p>If <strong>YES</strong>, it will again check if the new firmware is going to upgrade <strong>the custom bootloader itself</strong> or <strong>the application</strong>: (1) if the target is the custom bootloader, it must first copy the bootloader to SRAM and then run it; or (2) if the application is the target, it must start the upgrading process.</p></li>
  <li><p>If <strong>NO</strong>, it must jump to the application.</p></li>
  </ul>
  
  <p><strong><em>I would appreciate any comments and suggestions.</em></strong></p>