#define SDRAM_BASE_ADDR 0xa0000000
#define SDRAM_SIZE_BYTES 2000000 //(8 * 1024 * 1024)
#define SDRAM_EXAMPLE_DATALEN (SDRAM_SIZE_BYTES / 4)
#define SDRAM_TEST_PATTERN (2)
pti_return_t SDRAM_DataBusCheck(volatile uint32_t* address) {
uint32_t data = 0;
/* Write the walking 1's data test. */
for(data = 1; data != 0; data <<= 1) {
*address = data;
/* Read the data out of the address and check. */
if(*address != data) {
return pti_error;
}
}
return pti_success;
}
pti_return_t SDRAM_AddressBusCheck(volatile uint32_t* address, uint32_t bytes) {
uint32_t pattern = 0x55555555;
uint32_t size = bytes / 4;
uint32_t offset;
uint32_t checkOffset;
/* write the pattern to the power-of-two address. */
for(offset = 1; offset < size; offset <<= 1) {
address[offset] = pattern;
}
address[0] = ~pattern;
/* Read and check. */
uint32_t *ptr = NULL;
for(offset = 1; offset < size; offset <<= 1) {
ptr = &address[offset];
if(*ptr != pattern) {
return pti_error;
}
}
if(address[0] != ~pattern) {
return pti_error;
}
/* Change the data to the revert one address each time
* and check there is no effect to other address. */
for(offset = 1; offset < size; offset <<= 1) {
address[offset] = ~pattern;
for(checkOffset = 1; checkOffset < size; checkOffset <<= 1) {
if((checkOffset != offset) && (address[checkOffset] != pattern)) {
return pti_error;
}
}
address[offset] = pattern;
}
return pti_success;
}
static void test(void) {
volatile uint32_t index;
uint32_t* sdram = (uint32_t*)SDRAM_BASE_ADDR; /* SDRAM start address. */
/* Data/address bus check. */
if(SDRAM_DataBusCheck(sdram) != pti_success) {
LOG_DEBUG(MOD_ALL_BASE, "\r\n SDRAM data bus check is failure.\r\n");
}
if(SDRAM_AddressBusCheck(sdram, SDRAM_SIZE_BYTES) != pti_success) {
LOG_DEBUG(MOD_ALL_BASE, "\r\n SDRAM address bus check is failure.\r\n");
}
LOG_DEBUG(MOD_ALL_BASE, "\r\n Start EMC SDRAM access example.\r\n");
LOG_DEBUG_PF(MOD_ALL_BASE, "\r\n SDRAM Write Start, Start Address 0x%x, Data Length %d !\r\n", sdram,
SDRAM_EXAMPLE_DATALEN);
/* Prepare data and write to SDRAM. */
for(index = 0; index < SDRAM_EXAMPLE_DATALEN; index++) {
*(uint32_t*)(sdram + index) = index;
}
LOG_DEBUG(MOD_ALL_BASE, "\r\n SDRAM Write finished!\r\n");
LOG_DEBUG_PF(MOD_ALL_BASE, "\r\n SDRAM Read/Check Start, Start Address 0x%x, Data Length %d !\r\n", sdram,
SDRAM_EXAMPLE_DATALEN);
/* Read data from the SDRAM. */
uint32_t* ptr = NULL;
uint32_t value = 0;
for(index = 0; index < SDRAM_EXAMPLE_DATALEN; index++) {
ptr = sdram + index;
value = *ptr;
if(value != index) {
LOG_DEBUG(MOD_ALL_BASE, "\r\n SDRAM Write Data and Read Data Check Error!\r\n");
break;
}
}
LOG_DEBUG(MOD_ALL_BASE, "\r\n SDRAM Write Data and Read Data Succeed.\r\n");
}