If you need urgent consulting help click here

STM32 CCM example

Overview

Show usage of the Core Coupled Memory (CCM) that is available on several STM32 devices. The very important difference with normal RAM is that CCM can not be used for DMA.

By prefixing a variable with __ccm_data_section, __ccm_bss_section, or __ccm_noinit_section those variables are placed in the CCM.

The __ccm_data_section prefix should be used for variables that are initialized. Like the normal data section the initial values take up space in the FLASH image.

The __ccm_bss_section prefix should be used for variables that should be initialized to 0. Like the normal bss section they do not take up FLASH space.

The __ccm_noinit_section prefix should be used for variables that don’t need to have a defined initial value (for example buffers that will receive data). Compared to bss or data the kernel does not need to initialize the noinit section making the startup slightly faster.

To add CCM support to a board, add the following line to the board’s DTS file chosen section:

zephyr,ccm = &ccm0;

For example the olimex STM32 E407 DTS file looks like this:

  1/*
  2 * Copyright (c) 2017, Erwin Rol <erwin@erwinrol.com>
  3 *
  4 * SPDX-License-Identifier: Apache-2.0
  5 */
  6
  7/dts-v1/;
  8#include <st/f4/stm32f407Xg.dtsi>
  9#include <st/f4/stm32f407z(e-g)tx-pinctrl.dtsi>
 10
 11/ {
 12	model = "Olimex STM32-E407 board";
 13	compatible = "olimex,stm32-e407";
 14
 15	chosen {
 16		zephyr,console = &usart1;
 17		zephyr,shell-uart = &usart1;
 18		zephyr,sram = &sram0;
 19		zephyr,flash = &flash0;
 20		zephyr,ccm = &ccm0;
 21	};
 22
 23	leds {
 24		compatible = "gpio-leds";
 25		green_led_1: led_1 {
 26			gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
 27			label = "LED1";
 28		};
 29	};
 30
 31	gpio_keys {
 32		compatible = "gpio-keys";
 33		user_button: button {
 34			label = "Key";
 35			gpios = <&gpioa 0 GPIO_ACTIVE_LOW>;
 36		};
 37	};
 38
 39	aliases {
 40		led0 = &green_led_1;
 41		sw0 = &user_button;
 42	};
 43};
 44
 45&clk_hse {
 46	clock-frequency = <DT_FREQ_M(12)>;
 47	status = "okay";
 48};
 49
 50&pll {
 51	div-m = <6>;
 52	mul-n = <168>;
 53	div-p = <2>;
 54	div-q = <7>;
 55	clocks = <&clk_hse>;
 56	status = "okay";
 57};
 58
 59&rcc {
 60	clocks = <&pll>;
 61	clock-frequency = <DT_FREQ_M(168)>;
 62	ahb-prescaler = <1>;
 63	apb1-prescaler = <4>;
 64	apb2-prescaler = <2>;
 65};
 66
 67&usart1 {
 68	pinctrl-0 = <&usart1_tx_pb6 &usart1_rx_pb7>;
 69	pinctrl-names = "default";
 70	current-speed = <115200>;
 71	status = "okay";
 72};
 73
 74&usart3 {
 75	pinctrl-0 = <&usart3_tx_pb10 &usart3_rx_pb11>;
 76	pinctrl-names = "default";
 77	current-speed = <115200>;
 78	status = "okay";
 79};
 80
 81&usart6 {
 82	pinctrl-0 = <&usart6_tx_pc6 &usart6_rx_pc7>;
 83	pinctrl-names = "default";
 84	current-speed = <115200>;
 85	status = "okay";
 86};
 87
 88&rtc {
 89	status = "okay";
 90};
 91
 92&rng {
 93	status = "okay";
 94};
 95
 96/* Only one interface should be enabled at a time: usbotg_fs or usbotg_hs */
 97usb_otg1: &usbotg_fs {
 98	pinctrl-0 = <&usb_otg_fs_dm_pa11 &usb_otg_fs_dp_pa12>;
 99	pinctrl-names = "default";
100	status = "disabled";
101};
102
103zephyr_udc0: &usbotg_hs {
104	pinctrl-0 = <&usb_otg_hs_dm_pb14 &usb_otg_hs_dp_pb15>;
105	pinctrl-names = "default";
106	status = "okay";
107};
108
109&mac {
110	status = "okay";
111	pinctrl-0 = <&eth_mdc_pc1
112		     &eth_rxd0_pc4
113		     &eth_rxd1_pc5
114		     &eth_ref_clk_pa1
115		     &eth_mdio_pa2
116		     &eth_col_pa3
117		     &eth_crs_dv_pa7
118		     &eth_tx_en_pg11
119		     &eth_txd0_pg13
120		     &eth_txd1_pg14>;
121	pinctrl-names = "default";
122};

Building and Running

west build -b None samples/boards/olimex_stm32_e407/ccm
west flash

The first time the example is run after power on, the output will look like this:

***** BOOTING ZEPHYR OS v1.10.99 - BUILD: Jan 14 2018 09:32:46 *****

CCM (Core Coupled Memory) usage example

The total used CCM area   : [0x10000000, 0x10000021)
Zero initialized BSS area : [0x10000000, 0x10000007)
Uninitialized NOINIT area : [0x10000008, 0x10000013)
Initialised DATA area     : [0x10000014, 0x10000021)
Start of DATA in FLASH    : 0x08003940

Checking initial variable values: ... PASSED

Initial variable values:
ccm_data_var_8  addr: 0x10000014 value: 0x12
ccm_data_var_16 addr: 0x10000016 value: 0x3456
ccm_data_var_32 addr: 0x10000018 value: 0x789abcde
ccm_data_array  addr: 0x1000001c size: 5 value:
        0x11 0x22 0x33 0x44 0x55
ccm_bss_array addr: 0x10000000 size: 7 value:
        0x00 0x00 0x00 0x00 0x00 0x00 0x00
ccm_noinit_array addr: 0x10000008 size: 11 value:
        0xa9 0x99 0xba 0x90 0xe1 0x2a 0xba 0x93 0x4c 0xfe 0x4b

Variable values after writing:
ccm_data_var_8  addr: 0x10000014 value: 0xed
ccm_data_var_16 addr: 0x10000016 value: 0xcba9
ccm_data_var_32 addr: 0x10000018 value: 0x87654321
ccm_data_array  addr: 0x1000001c size: 5 value:
        0xaa 0xaa 0xaa 0xaa 0xaa
ccm_bss_array addr: 0x10000000 size: 7 value:
        0xbb 0xbb 0xbb 0xbb 0xbb 0xbb 0xbb
ccm_noinit_array addr: 0x10000008 size: 11 value:
        0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc

Example end

First, each CCM section is listed with its address and size. Next, some usage examples are shown. Note that the noinit section holds variables with uninitialized data. After writing to the variables, they all should hold the values shown above.

When the board is reset (without power-cycling), the output looks like this:

***** BOOTING ZEPHYR OS v1.10.99 - BUILD: Jan 14 2018 09:32:46 *****

CCM (Core Coupled Memory) usage example

The total used CCM area   : [0x10000000, 0x10000021)
Zero initialized BSS area : [0x10000000, 0x10000007)
Uninitialized NOINIT area : [0x10000008, 0x10000013)
Initialised DATA area     : [0x10000014, 0x10000021)
Start of DATA in FLASH    : 0x08003940

Checking initial variable values: ... PASSED

Initial variable values:
ccm_data_var_8  addr: 0x10000014 value: 0x12
ccm_data_var_16 addr: 0x10000016 value: 0x3456
ccm_data_var_32 addr: 0x10000018 value: 0x789abcde
ccm_data_array  addr: 0x1000001c size: 5 value:
        0x11 0x22 0x33 0x44 0x55
ccm_bss_array addr: 0x10000000 size: 7 value:
        0x00 0x00 0x00 0x00 0x00 0x00 0x00
ccm_noinit_array addr: 0x10000008 size: 11 value:
        0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc

Variable values after writing:
ccm_data_var_8  addr: 0x10000014 value: 0xed
ccm_data_var_16 addr: 0x10000016 value: 0xcba9
ccm_data_var_32 addr: 0x10000018 value: 0x87654321
ccm_data_array  addr: 0x1000001c size: 5 value:
        0xaa 0xaa 0xaa 0xaa 0xaa
ccm_bss_array addr: 0x10000000 size: 7 value:
        0xbb 0xbb 0xbb 0xbb 0xbb 0xbb 0xbb
ccm_noinit_array addr: 0x10000008 size: 11 value:
        0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc 0xcc

Example end

The difference with the first run is that the ccm_noinit section still has the values from the last write. It is important to notice that this is not guaranteed, it still should be considered uninitialized leftover data.