1 /*
2  * Copyright (c) 2017 Erwin Rol <erwin@erwinrol.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <stdio.h>
10 #include <string.h>
11 
12 #include <zephyr/linker/linker-defs.h>
13 
14 #define CCM_DATA_VAR_8_VAL	0x12
15 #define CCM_DATA_VAR_16_VAL	0x3456
16 #define CCM_DATA_VAR_32_VAL	0x789ABCDE
17 
18 #define CCM_DATA_ARRAY_SIZE	5
19 #define CCM_BSS_ARRAY_SIZE	7
20 #define CCM_NOINIT_ARRAY_SIZE	11
21 
22 #define CCM_DATA_ARRAY_VAL(i)	(((i)+1)*0x11)
23 
24 uint8_t __ccm_data_section ccm_data_var_8 = CCM_DATA_VAR_8_VAL;
25 uint16_t __ccm_data_section ccm_data_var_16 = CCM_DATA_VAR_16_VAL;
26 uint32_t __ccm_data_section ccm_data_var_32 = CCM_DATA_VAR_32_VAL;
27 
28 uint8_t __ccm_data_section ccm_data_array[CCM_DATA_ARRAY_SIZE] = {
29 	CCM_DATA_ARRAY_VAL(0),
30 	CCM_DATA_ARRAY_VAL(1),
31 	CCM_DATA_ARRAY_VAL(2),
32 	CCM_DATA_ARRAY_VAL(3),
33 	CCM_DATA_ARRAY_VAL(4),
34 };
35 uint8_t __ccm_bss_section ccm_bss_array[CCM_BSS_ARRAY_SIZE];
36 uint8_t __ccm_noinit_section ccm_noinit_array[CCM_NOINIT_ARRAY_SIZE];
37 
print_array(const void * array,uint32_t size)38 void print_array(const void *array, uint32_t size)
39 {
40 	int i;
41 
42 	for (i = 0; i < size; i++) {
43 		printf("0x%02x ", ((const char *)array)[i]);
44 	}
45 }
46 
check_initial_var_values(void)47 void check_initial_var_values(void)
48 {
49 	int i;
50 	int check_failed = 0;
51 
52 	printf("\nChecking initial variable values: ... ");
53 
54 	if (ccm_data_var_8 != CCM_DATA_VAR_8_VAL) {
55 		check_failed = 1;
56 		printf("\nccm_data_var_8 incorrect: 0x%02x should be 0x%02x",
57 			ccm_data_var_8, CCM_DATA_VAR_8_VAL);
58 	}
59 
60 	if (ccm_data_var_16 != CCM_DATA_VAR_16_VAL) {
61 		check_failed = 1;
62 		printf("\nccm_data_var_16 incorrect: 0x%04x should be 0x%04x",
63 			ccm_data_var_16, CCM_DATA_VAR_16_VAL);
64 	}
65 
66 	if (ccm_data_var_32 != CCM_DATA_VAR_32_VAL) {
67 		check_failed = 1;
68 		printf("\nccm_data_var_32 incorrect: 0x%08x should be 0x%08x",
69 			ccm_data_var_32, CCM_DATA_VAR_32_VAL);
70 	}
71 
72 	for (i = 0; i < CCM_DATA_ARRAY_SIZE; i++) {
73 		if (ccm_data_array[i] != CCM_DATA_ARRAY_VAL(i)) {
74 			check_failed = 1;
75 			printf("\nccm_data_array[%d] incorrect: "
76 				"0x%02x should be 0x%02x",
77 				i, ccm_data_array[i], CCM_DATA_ARRAY_VAL(i));
78 		}
79 	}
80 
81 	for (i = 0; i < CCM_BSS_ARRAY_SIZE; i++) {
82 		if (ccm_bss_array[i] != 0x00) {
83 			check_failed = 1;
84 			printf("\nccm_bss_array[%d] incorrect: "
85 				"0x%02x should be 0x00",
86 				i, ccm_bss_array[i]);
87 		}
88 	}
89 
90 	if (!check_failed) {
91 		printf("PASSED\n");
92 	}
93 }
94 
print_var_values(void)95 void print_var_values(void)
96 {
97 	printf("ccm_data_var_8  addr: %p value: 0x%02x\n",
98 		&ccm_data_var_8, ccm_data_var_8);
99 	printf("ccm_data_var_16 addr: %p value: 0x%04x\n",
100 		&ccm_data_var_16, ccm_data_var_16);
101 	printf("ccm_data_var_32 addr: %p value: 0x%08x\n",
102 		&ccm_data_var_32, ccm_data_var_32);
103 	printf("ccm_data_array  addr: %p size: %d value:\n\t",
104 		ccm_data_array, (int)sizeof(ccm_data_array));
105 	print_array(ccm_data_array, sizeof(ccm_data_array));
106 	printf("\nccm_bss_array addr: %p size: %d value:\n\t",
107 		ccm_bss_array, (int)sizeof(ccm_bss_array));
108 	print_array(ccm_bss_array, sizeof(ccm_bss_array));
109 	printf("\nccm_noinit_array addr: %p size: %d value:\n\t",
110 		ccm_noinit_array, (int)sizeof(ccm_noinit_array));
111 	print_array(ccm_noinit_array, sizeof(ccm_noinit_array));
112 	printf("\n");
113 }
114 
main(void)115 int main(void)
116 {
117 	printf("\nCCM (Core Coupled Memory) usage example\n\n");
118 
119 	printf("The total used CCM area   : [%p, %p)\n",
120 		&__ccm_start, &__ccm_end);
121 	printf("Zero initialized BSS area : [%p, %p)\n",
122 		&__ccm_bss_start, &__ccm_bss_end);
123 	printf("Uninitialized NOINIT area : [%p, %p)\n",
124 		&__ccm_noinit_start, &__ccm_noinit_end);
125 	printf("Initialised DATA area     : [%p, %p)\n",
126 		&__ccm_data_start, &__ccm_data_end);
127 	printf("Start of DATA in FLASH    : %p\n",
128 		&__ccm_data_rom_start);
129 
130 	check_initial_var_values();
131 
132 	printf("\nInitial variable values:\n");
133 	print_var_values();
134 
135 	ccm_data_var_8  = ~CCM_DATA_VAR_8_VAL;
136 	ccm_data_var_16 = ~CCM_DATA_VAR_16_VAL;
137 	ccm_data_var_32 = ~CCM_DATA_VAR_32_VAL;
138 
139 	(void)memset(ccm_data_array, 0xAA, sizeof(ccm_data_array));
140 	(void)memset(ccm_bss_array, 0xBB, sizeof(ccm_bss_array));
141 	(void)memset(ccm_noinit_array, 0xCC, sizeof(ccm_noinit_array));
142 
143 	printf("\nVariable values after writing:\n");
144 	print_var_values();
145 
146 	printf("\nExample end\n");
147 	return 0;
148 }
149