Skip to content

Commit 04aa43b

Browse files
committed
SDRAM: fix CM4 functionality
1 parent 870d15a commit 04aa43b

File tree

2 files changed

+35
-52
lines changed

2 files changed

+35
-52
lines changed

libraries/Portenta_SDRAM/src/SDRAM.cpp

+34-46
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@ extern "C" {
33
#include "ram_internal.h"
44
}
55

6+
static void MPU_Config() {
7+
MPU_Region_InitTypeDef MPU_InitStruct;
8+
9+
/* Disable the MPU */
10+
HAL_MPU_Disable();
11+
12+
// Initialize SDRAM Start as shareable
13+
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
14+
MPU_InitStruct.BaseAddress = SDRAM_START_ADDRESS;
15+
MPU_InitStruct.Size = ARM_MPU_REGION_SIZE_8MB;
16+
//MPU_InitStruct.SubRegionDisable = 0x00;
17+
MPU_InitStruct.Number = MPU_REGION_NUMBER5;
18+
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
19+
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
20+
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
21+
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
22+
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
23+
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
24+
25+
HAL_MPU_ConfigRegion(&MPU_InitStruct);
26+
27+
/* Enable the MPU */
28+
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
29+
}
30+
631
int SDRAMClass::begin(uint32_t start_address) {
732

8-
//printf("FMC_SDRAM_DEVICE->SDCMR: %x\n", FMC_SDRAM_DEVICE->SDCMR);
933
if (FMC_SDRAM_DEVICE->SDCMR == 0x00000000U) {
10-
//printf("initializing external ram\n");
1134
bool ret = sdram_init();
1235
if (ret == false) {
1336
return 0;
@@ -18,51 +41,16 @@ int SDRAMClass::begin(uint32_t start_address) {
1841
then enable access/caching for the size used
1942
*/
2043

21-
if (SDRAM_START_ADDRESS != 0xC0000000) {
22-
//printf("remap ram to 0x60000000\n");
44+
if (SDRAM_START_ADDRESS == 0x60000000) {
2345
HAL_SetFMCMemorySwappingConfig(FMC_SWAPBMAP_SDRAM_SRAM);
2446
}
2547

26-
#if 0
27-
28-
printf("setup mpu\n");
29-
#define MPU_SDRAM_EXEC_REGION_NUMBER MPU_REGION_SDRAM1
30-
#define MPU_SDRAM_REGION_TEX (0x4 << MPU_RASR_TEX_Pos) /* Cached memory */
31-
#define MPU_SDRAM_EXEC_REGION_SIZE (22 << MPU_RASR_SIZE_Pos) /* 2^(22+1) = 8Mo */
32-
#define MPU_SDRAM_ACCESS_PERMSSION (0x03UL << MPU_RASR_AP_Pos)
33-
#define MPU_SDRAM_REGION_CACHABLE (0x01UL << MPU_RASR_C_Pos)
34-
#define MPU_SDRAM_REGION_BUFFERABLE (0x01UL << MPU_RASR_B_Pos)
35-
36-
MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
37-
/* Configure SDARM region as first region */
38-
MPU->RNR = MPU_SDRAM_EXEC_REGION_NUMBER;
39-
/* Set MPU SDARM base address (0xD0000000) */
40-
MPU->RBAR = SDRAM_START_ADDRESS;
41-
/*
42-
- Execute region: RASR[size] = 22 -> 2^(22+1) -> size 8MB
43-
- Access permission: Full access: RASR[AP] = 0b011
44-
- Cached memory: RASR[TEX] = 0b0100
45-
- Disable the Execute Never option: to allow the code execution on SDRAM: RASR[XN] = 0
46-
- Enable the region MPU: RASR[EN] = 1
47-
*/
48-
MPU->RASR = (MPU_SDRAM_EXEC_REGION_SIZE | MPU_SDRAM_ACCESS_PERMSSION | MPU_SDRAM_REGION_TEX | \
49-
MPU_RASR_ENABLE_Msk | MPU_SDRAM_REGION_BUFFERABLE) & ~MPU_RASR_XN_Msk ;
50-
51-
/* Enable MPU and leave the predefined regions to default configuration */
52-
MPU->CTRL |= MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_ENABLE_Msk;
53-
#endif
54-
55-
#if 0
56-
mpu_config_start();
57-
mpu_config_region(MPU_REGION_SDRAM1, SDRAM_START_ADDRESS, MPU_CONFIG_DISABLE(0x00, MPU_REGION_SIZE_512MB));
58-
mpu_config_region(MPU_REGION_SDRAM2, SDRAM_START_ADDRESS, MPU_CONFIG_SDRAM(SDRAM_MPU_REGION_SIZE));
59-
mpu_config_end();
60-
#endif
61-
48+
#ifdef CORE_CM4
49+
MPU_Config();
50+
#endif
6251
}
6352

6453
if (start_address) {
65-
//printf("malloc_addblock: allocate %d bytes\n", SDRAM_END_ADDRESS - start_address);
6654
malloc_addblock((void*)start_address, SDRAM_END_ADDRESS - start_address);
6755
}
6856

@@ -77,7 +65,7 @@ void SDRAMClass::free(void* ptr) {
7765
ea_free(ptr);
7866
}
7967

80-
bool __attribute__((optimize("O0"))) SDRAMClass::test(bool fast) {
68+
bool __attribute__((optimize("O0"))) SDRAMClass::test(bool fast, Stream& _serial) {
8169
uint8_t const pattern = 0xaa;
8270
uint8_t const antipattern = 0x55;
8371
uint8_t *const mem_base = (uint8_t*)SDRAM_START_ADDRESS;
@@ -86,7 +74,7 @@ bool __attribute__((optimize("O0"))) SDRAMClass::test(bool fast) {
8674
for (uint8_t i = 1; i; i <<= 1) {
8775
*mem_base = i;
8876
if (*mem_base != i) {
89-
printf("data bus lines test failed! data (%d)\n", i);
77+
_serial.println("data bus lines test failed! data (" + String(i) + ")");
9078
__asm__ volatile ("BKPT");
9179
}
9280
}
@@ -96,7 +84,7 @@ bool __attribute__((optimize("O0"))) SDRAMClass::test(bool fast) {
9684
for (uint32_t i = 1; i < HW_SDRAM_SIZE; i <<= 1) {
9785
mem_base[i] = pattern;
9886
if (mem_base[i] != pattern) {
99-
printf("address bus lines test failed! address (%p)\n", &mem_base[i]);
87+
_serial.println("address bus lines test failed! address ("+ String((uint32_t)&mem_base[i], HEX) + ")");
10088
__asm__ volatile ("BKPT");
10189
}
10290
}
@@ -105,7 +93,7 @@ bool __attribute__((optimize("O0"))) SDRAMClass::test(bool fast) {
10593
mem_base[0] = antipattern;
10694
for (uint32_t i = 1; i < HW_SDRAM_SIZE; i <<= 1) {
10795
if (mem_base[i] != pattern) {
108-
printf("address bus overlap %p\n", &mem_base[i]);
96+
_serial.println("address bus overlap! address ("+ String((uint32_t)&mem_base[i], HEX) + ")");
10997
__asm__ volatile ("BKPT");
11098
}
11199
}
@@ -115,7 +103,7 @@ bool __attribute__((optimize("O0"))) SDRAMClass::test(bool fast) {
115103
for (uint32_t i = 0; i < HW_SDRAM_SIZE; ++i) {
116104
mem_base[i] = pattern;
117105
if (mem_base[i] != pattern) {
118-
printf("address bus test failed! address (%p)\n", &mem_base[i]);
106+
_serial.println("address bus test failed! address ("+ String((uint32_t)&mem_base[i], HEX) + ")");
119107
__asm__ volatile ("BKPT");
120108
}
121109
}

libraries/Portenta_SDRAM/src/SDRAM.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44

55
#include "Arduino.h"
66

7-
#if !defined(CORE_CM4)
87
#define SDRAM_END_ADDRESS (0x60800000)
98
#define SDRAM_START_ADDRESS (0x60000000)
10-
#else
11-
#define SDRAM_END_ADDRESS (0xC0800000)
12-
#define SDRAM_START_ADDRESS (0xC0000000)
13-
#endif
149

1510
class SDRAMClass {
1611
public:
1712
SDRAMClass() {}
1813
int begin(uint32_t start_address = SDRAM_START_ADDRESS);
1914
void* malloc(size_t size);
2015
void free(void* ptr);
21-
bool test(bool fast = false);
16+
bool test(bool fast = false, Stream& _serial = Serial);
2217
private:
2318
void mpu_config_start(void) {
2419
__disable_irq();

0 commit comments

Comments
 (0)