Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

42 lines
827 B

  1. #include <board/simpflash.h>
  2. #include <stm32f1xx.h>
  3. #include <stm32f1xx_hal_flash.h>
  4. #include <strobe_rng_init.h> /* roundup */
  5. void
  6. doflash(const void *dst, void *src, int bytes)
  7. {
  8. FLASH_EraseInitTypeDef erase;
  9. const uint32_t *dstp;
  10. uint32_t *srcp;
  11. size_t i;
  12. uint32_t pageerr;
  13. uint32_t primask;
  14. dstp = dst;
  15. srcp = src;
  16. primask = __get_PRIMASK();
  17. __disable_irq();
  18. HAL_FLASH_Unlock();
  19. erase = (FLASH_EraseInitTypeDef){
  20. .TypeErase = FLASH_TYPEERASE_PAGES,
  21. .Banks = FLASH_BANK_1,
  22. .PageAddress = (intptr_t)&dstp[0],
  23. .NbPages = roundup(bytes, FLASH_PAGE_SIZE) / FLASH_PAGE_SIZE,
  24. };
  25. HAL_FLASHEx_Erase(&erase, &pageerr);
  26. for (i = 0; i < roundup(bytes, sizeof *srcp) / 4; i++) {
  27. HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (intptr_t)&dstp[i], srcp[i]);
  28. }
  29. HAL_FLASH_Lock();
  30. __set_PRIMASK(primask);
  31. }