flabbergast's ARM/STM32 examples using libopencm3
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.

lib-flash-from-ram.patch 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. diff --git a/include/libopencm3/stm32/l0/flash.h b/include/libopencm3/stm32/l0/flash.h
  2. index 3716300..c2179c3 100644
  3. --- a/include/libopencm3/stm32/l0/flash.h
  4. +++ b/include/libopencm3/stm32/l0/flash.h
  5. @@ -70,6 +70,14 @@
  6. #define FLASH_OPTR_RDPRT_LEVEL_1 (0x00)
  7. #define FLASH_OPTR_RDPRT_LEVEL_2 (0xcc)
  8. +/* --- RAM functions workaround -------------------------------------------- */
  9. +
  10. +#if defined(L0_FLASH_FNS_FROM_RAM) && L0_FLASH_FNS_FROM_RAM
  11. +#define __FROM_RAM __attribute__((__long_call__,section(".data")))
  12. +#else
  13. +#define __FROM_RAM
  14. +#endif
  15. +
  16. /* --- Function prototypes ------------------------------------------------- */
  17. BEGIN_DECLS
  18. diff --git a/lib/stm32/l0/Makefile b/lib/stm32/l0/Makefile
  19. index 8ed6779..a29a5ad 100644
  20. --- a/lib/stm32/l0/Makefile
  21. +++ b/lib/stm32/l0/Makefile
  22. @@ -20,6 +20,8 @@
  23. LIBNAME = libopencm3_stm32l0
  24. SRCLIBDIR ?= ../..
  25. +FLASH_FROM_RAM ?= 0
  26. +
  27. PREFIX ?= arm-none-eabi
  28. #PREFIX ?= arm-elf
  29. CC = $(PREFIX)-gcc
  30. @@ -30,7 +32,8 @@ TGT_CFLAGS = -Os \
  31. -Wundef -Wshadow \
  32. -I../../../include -fno-common \
  33. -mcpu=cortex-m0plus $(FP_FLAGS) -mthumb -Wstrict-prototypes \
  34. - -ffunction-sections -fdata-sections -MD -DSTM32L0
  35. + -ffunction-sections -fdata-sections -MD -DSTM32L0 \
  36. + -DL0_FLASH_FNS_FROM_RAM=$(FLASH_FROM_RAM)
  37. TGT_CFLAGS += $(DEBUG_FLAGS)
  38. ARFLAGS = rcs
  39. diff --git a/lib/stm32/l0/flash.c b/lib/stm32/l0/flash.c
  40. index ec7280a..9e2b34f 100644
  41. --- a/lib/stm32/l0/flash.c
  42. +++ b/lib/stm32/l0/flash.c
  43. @@ -93,13 +93,15 @@ Warning: this function MUST be executed from RAM.
  44. @param[in] data Pointer data to write
  45. */
  46. -void flash_program_half_page(uint32_t address, uint32_t *data, uint16_t page_size)
  47. +__FROM_RAM void flash_program_half_page(uint32_t address, uint32_t *data, uint16_t page_size)
  48. {
  49. uint16_t i;
  50. + while (FLASH_SR & FLASH_SR_BSY);
  51. +
  52. FLASH_PECR |= FLASH_PECR_PROG | FLASH_PECR_FPRG;
  53. - for (i = 0; i < ((page_size/2) * 4); i+=4, data++) {
  54. + for (i = 0; i < (page_size/2); i+=4, data++) {
  55. MMIO32(address + i) = *data;
  56. }
  57. @@ -151,7 +153,7 @@ memory. The information block is unaffected.
  58. Warning: this function MUST be executed from RAM.
  59. */
  60. -void flash_erase_all_pages(void)
  61. +__FROM_RAM void flash_erase_all_pages(void)
  62. {
  63. /* (1) Check if the read protection is not level 2 */
  64. @@ -173,7 +175,8 @@ void flash_erase_all_pages(void)
  65. /* Should check that the memory was erased here */
  66. /* (6) Lock the NVM by setting the PELOCK bit */
  67. - flash_lock_pecr();
  68. + while (FLASH_SR & FLASH_SR_BSY);
  69. + FLASH_PECR |= FLASH_PECR_PELOCK;
  70. /* Infinite loop */
  71. while (1);
  72. @@ -191,7 +194,7 @@ Warning: this function MUST be executed from RAM.
  73. @param[in] address Address of option byte from @ref flash_options.
  74. */
  75. -void flash_erase_option_byte(uint32_t address)
  76. +__FROM_RAM void flash_erase_option_byte(uint32_t address)
  77. {
  78. FLASH_PECR |= FLASH_PECR_ERASE;
  79. @@ -221,7 +224,7 @@ Warning: this function MUST be executed from RAM.
  80. @param[in] data value to write
  81. */
  82. -void flash_program_option_byte(uint32_t address, uint16_t data)
  83. +__FROM_RAM void flash_program_option_byte(uint32_t address, uint16_t data)
  84. {
  85. MMIO32(address) = (uint32_t)((~data << 16) | data);