然后将 aSRC_Const_Buffer 数组定义为常量,即可分配到 RO 区域,aDST_Buffer 定义为普通的全局变量或静态变量即可,因为内存区域从 0x20000000 开始,也就是 DTCM RAM。
好了,代码主体部分如下:
(完整的代码可以在 https://download.csdn.net/download/luckydarcy/10104739 下载)
#define SRAM1_ADDRESS_START (0x20020000UL)
static const uint32_t aSRC_Const_Buffer[BUFFER_SIZE] =
{
0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20,
0x21222324, 0x25262728, 0x292A2B2C, 0x2D2E2F30,
0x31323334, 0x35363738, 0x393A3B3C, 0x3D3E3F40,
0x41424344, 0x45464748, 0x494A4B4C, 0x4D4E4F50,
0x51525354, 0x55565758, 0x595A5B5C, 0x5D5E5F60,
0x61626364, 0x65666768, 0x696A6B6C, 0x6D6E6F70,
0x71727374, 0x75767778, 0x797A7B7C, 0x7D7E7F80
};
static uint32_t aDST_Buffer[BUFFER_SIZE];
int main(void)
{
uint32_t counter = 0;
uint32_t *pBuffer = (uint32_t*)SRAM1_ADDRESS_START;
if (HAL_Init() != HAL_OK)
{
Error_Handler();
}
/* Initialize LEDs */
BSP_LED_Init(LED1);
/* Configure the system clock to 216 MHz */
SystemClock_Config();
BSP_LCD_Config();
/* Set to 1 if an transfer error is detected */
transferErrorDetected = 0;
/* Fill 128 bytes with 0x55 pattern */
memset((uint8_t*)SRAM1_ADDRESS_START, 0x55, sizeof(aSRC_Const_Buffer));
/* TODO:Enable MPU and change SRAM region attribute
* set write-back policy on SRAM */
MPU_Config();
/* Enable Data cache */
SCB_EnableDCache();
/* Copy data from Flash to SRAM by CPU */
for (counter = 0; counter < (sizeof(aSRC_Const_Buffer)/4); counter++)
{
*pBuffer++ = aSRC_Const_Buffer[counter];
}
//* Configure and enable the DMA stream for Memory to Memory transfer */
DMA_Config();
/* Wait for DMA end-of-transfer */
while(TransferCompleteFlag == RESET)
{
}
/* Check data integrity*/
pBuffer = (uint32_t*)&aDST_Buffer;
for(counter = 0; counter <(sizeof(aSRC_Const_Buffer)/4); counter++)
{
if(aSRC_Const_Buffer[counter] != *pBuffer)
{
compareErrorDetected++;
}
pBuffer++;
}
if (compareErrorDetected != 0)
{
/* Toggle LED1 */
BSP_LED_Off(LED1);
compareErrorDetected = 0;
BSP_LCD_DisplayStringAtLine(10, (uint8_t *)" Data comparation failed! ");
}
else
{
/* Turn LED1 on */
BSP_LED_On(LED1);
BSP_LCD_DisplayStringAtLine(10, (uint8_t *)" Data comparation success! ");
}
while (1)
{
}
}
static void MPU_Config(void)
{
/* Disable MPU */
MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
/* Configure RAM region as Region N°0, 256kB of size and R/W region */
MPU->RNR = SRAM1_REGION_NUMBER;
MPU->RBAR = SRAM1_ADDRESS_START;
/* Write-Back policy */
MPU->RASR = SRAM1_SIZE | MPU_RASR_C_Msk | MPU_RASR_B_Msk | SRAM1_ACCESS_PERMISSION | 1<<MPU_RASR_TEX_Pos;
/* Enable MPU */
MPU->CTRL |= MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_ENABLE_Msk;
}
|