[rtems commit] bsp/atsam: Move clock and SDRAM init to SRAM.

Sebastian Huber sebh at rtems.org
Fri Nov 17 13:22:33 UTC 2017


Module:    rtems
Branch:    master
Commit:    d00a7d1c22fd6e9965ef22c77aaf3be1b418e694
Changeset: http://git.rtems.org/rtems/commit/?id=d00a7d1c22fd6e9965ef22c77aaf3be1b418e694

Author:    Christian Mauderer <Christian.Mauderer at embedded-brains.de>
Date:      Fri Nov 17 09:21:59 2017 +0100

bsp/atsam: Move clock and SDRAM init to SRAM.

If necessary, the BSP can now have it's clock and SDRAM initialization
in the SRAM instead of the SDRAM. This allows to change the clock
frequency during the startup of an SDRAM application.

---

 c/src/lib/libbsp/arm/atsam/README                  |  7 ++-
 c/src/lib/libbsp/arm/atsam/configure.ac            |  6 ++
 .../libbsp/arm/atsam/include/atsam-clock-config.h  | 10 ++++
 .../libraries/libboard/include/board_memories.h    |  4 ++
 .../libboard/resources_v71/system_samv71.c         |  4 ++
 .../libchip/include/same70/system_same70.h         |  7 +++
 .../libchip/include/sams70/system_sams70.h         |  7 +++
 .../libchip/include/samv71/system_samv71.h         |  7 +++
 c/src/lib/libbsp/arm/atsam/startup/bspstarthooks.c | 65 +++++++++++++++-------
 9 files changed, 95 insertions(+), 22 deletions(-)

diff --git a/c/src/lib/libbsp/arm/atsam/README b/c/src/lib/libbsp/arm/atsam/README
index e21e7ee..c374e86 100644
--- a/c/src/lib/libbsp/arm/atsam/README
+++ b/c/src/lib/libbsp/arm/atsam/README
@@ -22,8 +22,11 @@ Use ATSAM_MCK=XYZ to set the MCK frequency that should be used. The default case
 evaluation kit. Some other configurations (e.g. 150MHz) would be too fast on
 that board.
 
-Your application can also overwrite the clock settings. To overwrite the clock
-settings, define the following structures in your application:
+Your application can also overwrite the clock settings.  If you have a
+bootloader with one setting in your internal flash and an application with
+another setting in your external SDRAM, you should also use the
+ATSAM_CHANGE_CLOCK_FROM_SRAM=1 option. To overwrite the clock settings, define
+the following structures in your application:
 
 --------
 const struct atsam_clock_config atsam_clock_config = {
diff --git a/c/src/lib/libbsp/arm/atsam/configure.ac b/c/src/lib/libbsp/arm/atsam/configure.ac
index a9f080d..8a4a2f2 100644
--- a/c/src/lib/libbsp/arm/atsam/configure.ac
+++ b/c/src/lib/libbsp/arm/atsam/configure.ac
@@ -68,6 +68,12 @@ RTEMS_BSPOPTS_HELP([ATSAM_MCK],
 [Frequency of the MCK in Hz. Set to 0 to force application defined speed.
 See startup/pmc-config.c for available clock configurations.])
 
+RTEMS_BSPOPTS_SET([ATSAM_CHANGE_CLOCK_FROM_SRAM],[*],[0])
+RTEMS_BSPOPTS_HELP([ATSAM_CHANGE_CLOCK_FROM_SRAM],
+[Move the functions that set up the clock into the SRAM.
+This allows to change the clock frequency even if the application is started from SDRAM.
+Requires a TCM_SIZE > 0.])
+
 RTEMS_BSPOPTS_SET([ATSAM_CONSOLE_BAUD],[*],[115200])
 RTEMS_BSPOPTS_HELP([ATSAM_CONSOLE_BAUD],[initial baud for console devices (default 115200)])
 
diff --git a/c/src/lib/libbsp/arm/atsam/include/atsam-clock-config.h b/c/src/lib/libbsp/arm/atsam/include/atsam-clock-config.h
index acf0d0b..3969868 100644
--- a/c/src/lib/libbsp/arm/atsam/include/atsam-clock-config.h
+++ b/c/src/lib/libbsp/arm/atsam/include/atsam-clock-config.h
@@ -23,6 +23,14 @@
 extern "C" {
 #endif /* __cplusplus */
 
+#if ATSAM_CHANGE_CLOCK_FROM_SRAM != 0
+/* Note: fast_text is the simplest section to put this code into. Other
+ * possibilities would include creating a new section. */
+#define ATSAM_START_SRAM_SECTION BSP_FAST_TEXT_SECTION
+#else
+#define ATSAM_START_SRAM_SECTION
+#endif
+
 struct atsam_clock_config {
   /* Initialization value for the PMC_PLLAR. */
   uint32_t pllar_init;
@@ -32,6 +40,7 @@ struct atsam_clock_config {
   uint32_t mck_freq;
 };
 
+ATSAM_START_SRAM_SECTION
 extern const struct atsam_clock_config atsam_clock_config;
 
 #define BOARD_MCK (atsam_clock_config.mck_freq)
@@ -43,6 +52,7 @@ struct BOARD_Sdram_Config {
   uint32_t sdramc_cfr1;
 };
 
+ATSAM_START_SRAM_SECTION
 extern const struct BOARD_Sdram_Config BOARD_Sdram_Config;
 
 #ifdef __cplusplus
diff --git a/c/src/lib/libbsp/arm/atsam/libraries/libboard/include/board_memories.h b/c/src/lib/libbsp/arm/atsam/libraries/libboard/include/board_memories.h
index 30767cd..72f8da5 100644
--- a/c/src/lib/libbsp/arm/atsam/libraries/libboard/include/board_memories.h
+++ b/c/src/lib/libbsp/arm/atsam/libraries/libboard/include/board_memories.h
@@ -41,7 +41,11 @@
  *        Exported functions
  *----------------------------------------------------------------------------*/
 
+#ifndef __rtems__
 extern void BOARD_ConfigureSdram(void);
+#else /* __rtems__ */
+extern void BOARD_ConfigureSdram(void) ATSAM_START_SRAM_SECTION;
+#endif /* __rtems__ */
 extern uint32_t BOARD_SdramValidation(uint32_t baseAddr, uint32_t size);
 
 #endif /* #ifndef BOARD_MEMORIES_H */
diff --git a/c/src/lib/libbsp/arm/atsam/libraries/libboard/resources_v71/system_samv71.c b/c/src/lib/libbsp/arm/atsam/libraries/libboard/resources_v71/system_samv71.c
index 72a4ec0..8d7eb8e 100644
--- a/c/src/lib/libbsp/arm/atsam/libraries/libboard/resources_v71/system_samv71.c
+++ b/c/src/lib/libbsp/arm/atsam/libraries/libboard/resources_v71/system_samv71.c
@@ -78,7 +78,11 @@ extern "C" {
 	 * \brief Setup the microcontroller system.
 	 * Initialize the System and update the SystemFrequency variable.
 	 */
+#ifndef __rtems__
 	void SystemInit(void)
+#else /* __rtems__ */
+	void ATSAM_START_SRAM_SECTION SystemInit(void)
+#endif /* __rtems__ */
 {
 	uint32_t read_MOR;
 	/* Set FWS according to SYS_BOARD_MCKR configuration */
diff --git a/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/same70/system_same70.h b/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/same70/system_same70.h
index f4c4d8e..fd04284 100644
--- a/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/same70/system_same70.h
+++ b/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/same70/system_same70.h
@@ -29,6 +29,9 @@
 
 #ifndef SYSTEM_SAME70_H_INCLUDED
 #define SYSTEM_SAME70_H_INCLUDED
+#ifdef __rtems__
+#include <bsp/atsam-clock-config.h>
+#endif /* __rtems__ */
 
 /* @cond 0 */
 /**INDENT-OFF**/
@@ -46,7 +49,11 @@ extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
  * @brief Setup the microcontroller system.
  * Initialize the System and update the SystemCoreClock variable.
  */
+#ifndef __rtems__
 void SystemInit(void);
+#else /* __rtems__ */
+void ATSAM_START_SRAM_SECTION SystemInit(void);
+#endif /* __rtems__ */
 
 /**
  * @brief Updates the SystemCoreClock with current core Clock
diff --git a/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/sams70/system_sams70.h b/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/sams70/system_sams70.h
index 9ecad30..3e74f5c 100644
--- a/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/sams70/system_sams70.h
+++ b/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/sams70/system_sams70.h
@@ -29,6 +29,9 @@
 
 #ifndef SYSTEM_SAMS70_H_INCLUDED
 #define SYSTEM_SAMS70_H_INCLUDED
+#ifdef __rtems__
+#include <bsp/atsam-clock-config.h>
+#endif /* __rtems__ */
 
 /* @cond 0 */
 /**INDENT-OFF**/
@@ -46,7 +49,11 @@ extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
  * @brief Setup the microcontroller system.
  * Initialize the System and update the SystemCoreClock variable.
  */
+#ifndef __rtems__
 void SystemInit(void);
+#else /* __rtems__ */
+void ATSAM_START_SRAM_SECTION SystemInit(void);
+#endif /* __rtems__ */
 
 /**
  * @brief Updates the SystemCoreClock with current core Clock
diff --git a/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/samv71/system_samv71.h b/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/samv71/system_samv71.h
index 39b17e8..f38282d 100644
--- a/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/samv71/system_samv71.h
+++ b/c/src/lib/libbsp/arm/atsam/libraries/libchip/include/samv71/system_samv71.h
@@ -29,6 +29,9 @@
 
 #ifndef SYSTEM_SAMV71_H_INCLUDED
 #define SYSTEM_SAMV71_H_INCLUDED
+#ifdef __rtems__
+#include <bsp/atsam-clock-config.h>
+#endif /* __rtems__ */
 
 /* @cond 0 */
 /**INDENT-OFF**/
@@ -46,7 +49,11 @@ extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
  * @brief Setup the microcontroller system.
  * Initialize the System and update the SystemCoreClock variable.
  */
+#ifndef __rtems__
 void SystemInit(void);
+#else /* __rtems__ */
+void ATSAM_START_SRAM_SECTION SystemInit(void);
+#endif /* __rtems__ */
 
 /**
  * @brief Updates the SystemCoreClock with current core Clock
diff --git a/c/src/lib/libbsp/arm/atsam/startup/bspstarthooks.c b/c/src/lib/libbsp/arm/atsam/startup/bspstarthooks.c
index 8a42fd8..697ec88 100644
--- a/c/src/lib/libbsp/arm/atsam/startup/bspstarthooks.c
+++ b/c/src/lib/libbsp/arm/atsam/startup/bspstarthooks.c
@@ -70,32 +70,28 @@ static BSP_START_TEXT_SECTION bool tcm_setup_and_check_if_do_efc_config(
   }
 }
 
-void BSP_START_TEXT_SECTION bsp_start_hook_0(void)
+static bool ATSAM_START_SRAM_SECTION sdram_settings_unchanged(void)
 {
-  uintptr_t tcm_size;
-  uint32_t itcmcr_sz;
+  return (
+    (SDRAMC->SDRAMC_CR == BOARD_Sdram_Config.sdramc_cr) &&
+    (SDRAMC->SDRAMC_TR == BOARD_Sdram_Config.sdramc_tr) &&
+    (SDRAMC->SDRAMC_MDR == BOARD_Sdram_Config.sdramc_mdr) &&
+    (SDRAMC->SDRAMC_CFR1 == BOARD_Sdram_Config.sdramc_cfr1)
+  );
+}
 
-  system_init_flash(BOARD_MCK);
+static void ATSAM_START_SRAM_SECTION setup_CPU_and_SDRAM(void)
+{
   SystemInit();
-
-  PIO_Configure(&atsam_pin_config[0], atsam_pin_config_count);
-  MATRIX->CCFG_SYSIO = atsam_matrix_ccfg_sysio;
-
-  if (!PMC_IsPeriphEnabled(ID_SDRAMC)) {
+  if (!PMC_IsPeriphEnabled(ID_SDRAMC) || !sdram_settings_unchanged()) {
     BOARD_ConfigureSdram();
   }
+}
 
-  if ((SCB->CCR & SCB_CCR_IC_Msk) == 0) {
-    SCB_EnableICache();
-  }
-
-  if ((SCB->CCR & SCB_CCR_DC_Msk) == 0) {
-    SCB_EnableDCache();
-  }
-
-  _SetupMemoryRegion();
-
-  /* Configure tightly coupled memory interfaces */
+static void configure_tcm(void)
+{
+  uintptr_t tcm_size;
+  uint32_t itcmcr_sz;
 
   tcm_size = (uintptr_t) atsam_memory_itcm_size;
   itcmcr_sz = (SCB->ITCMCR & SCB_ITCMCR_SZ_Msk) >> SCB_ITCMCR_SZ_Pos;
@@ -121,6 +117,35 @@ void BSP_START_TEXT_SECTION bsp_start_hook_0(void)
   }
 }
 
+void BSP_START_TEXT_SECTION bsp_start_hook_0(void)
+{
+  system_init_flash(BOARD_MCK);
+
+  PIO_Configure(&atsam_pin_config[0], atsam_pin_config_count);
+  MATRIX->CCFG_SYSIO = atsam_matrix_ccfg_sysio;
+
+  configure_tcm();
+#if ATSAM_CHANGE_CLOCK_FROM_SRAM != 0
+  /* Early copy of .fast_text section for CPU and SDRAM setup. */
+  bsp_start_memcpy_libc(
+    bsp_section_fast_text_begin,
+    bsp_section_fast_text_load_begin,
+    (size_t) bsp_section_fast_text_size
+  );
+#endif
+  setup_CPU_and_SDRAM();
+
+  if ((SCB->CCR & SCB_CCR_IC_Msk) == 0) {
+    SCB_EnableICache();
+  }
+
+  if ((SCB->CCR & SCB_CCR_DC_Msk) == 0) {
+    SCB_EnableDCache();
+  }
+
+  _SetupMemoryRegion();
+}
+
 void BSP_START_TEXT_SECTION bsp_start_hook_1(void)
 {
   bsp_start_copy_sections_compact();




More information about the vc mailing list