2020-01-06 09:12:45 +08:00
/*
* Copyright ( c ) 2006 - 2020 , RT - Thread Development Team
*
* SPDX - License - Identifier : Apache - 2.0
*
* Change Logs :
* Date Author Notes
* 2020 - 01 - 05 linyiyang first version
*/
# include <rtthread.h>
# include <rtdevice.h>
# include <board.h>
2020-01-08 22:04:49 +08:00
# ifdef BSP_USING_EXT_SRAM
2020-01-06 09:12:45 +08:00
# include <sram_port.h>
# define DRV_DEBUG
2020-01-08 22:04:49 +08:00
# define LOG_TAG "drv.ext_sram"
2020-01-06 09:12:45 +08:00
# include <drv_log.h>
static SRAM_HandleTypeDef hsram1 ;
# ifdef RT_USING_MEMHEAP_AS_HEAP
static struct rt_memheap system_heap ;
# endif
2020-01-08 22:04:49 +08:00
static int external_sram_init ( void )
2020-01-06 09:12:45 +08:00
{
int result = RT_EOK ;
FSMC_NORSRAM_TimingTypeDef Timing = { 0 } ;
/** Perform the SRAM1 memory initialization sequence
*/
hsram1 . Instance = FSMC_NORSRAM_DEVICE ;
hsram1 . Extended = FSMC_NORSRAM_EXTENDED_DEVICE ;
/* hsram1.Init */
hsram1 . Init . NSBank = FSMC_NORSRAM_BANK3 ;
hsram1 . Init . DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE ;
hsram1 . Init . MemoryType = FSMC_MEMORY_TYPE_SRAM ;
2020-01-08 22:04:49 +08:00
# if EXTERNAL_SRAM_DATA_WIDTH == 8
2020-01-06 09:12:45 +08:00
hsram1 . Init . MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_8 ;
2020-01-08 22:04:49 +08:00
# elif EXTERNAL_SRAM_DATA_WIDTH == 16
2020-01-06 09:12:45 +08:00
hsram1 . Init . MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16 ;
# else
hsram1 . Init . MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_32 ;
# endif
hsram1 . Init . BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE ;
hsram1 . Init . WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW ;
hsram1 . Init . WrapMode = FSMC_WRAP_MODE_DISABLE ;
hsram1 . Init . WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS ;
hsram1 . Init . WriteOperation = FSMC_WRITE_OPERATION_ENABLE ;
hsram1 . Init . WaitSignal = FSMC_WAIT_SIGNAL_DISABLE ;
hsram1 . Init . ExtendedMode = FSMC_EXTENDED_MODE_DISABLE ;
hsram1 . Init . AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE ;
hsram1 . Init . WriteBurst = FSMC_WRITE_BURST_DISABLE ;
/* Timing */
Timing . AddressSetupTime = 0 ;
Timing . AddressHoldTime = 15 ;
Timing . DataSetupTime = 3 ;
Timing . BusTurnAroundDuration = 0 ;
Timing . CLKDivision = 16 ;
Timing . DataLatency = 17 ;
Timing . AccessMode = FSMC_ACCESS_MODE_A ;
/* ExtTiming */
/* Initialize the SRAM controller */
if ( HAL_SRAM_Init ( & hsram1 , & Timing , NULL ) ! = HAL_OK )
{
2020-01-08 22:04:49 +08:00
LOG_E ( " External SRAM init failed! " ) ;
2020-01-06 09:12:45 +08:00
result = - RT_ERROR ;
}
else
{
2020-01-08 22:04:49 +08:00
LOG_D ( " External sram init success, mapped at 0x%X, size is %d bytes, data width is %d " , EXTERNAL_SRAM_BANK_ADDR , EXTERNAL_SRAM_SIZE , EXTERNAL_SRAM_DATA_WIDTH ) ;
2020-01-06 09:12:45 +08:00
# ifdef RT_USING_MEMHEAP_AS_HEAP
/* If RT_USING_MEMHEAP_AS_HEAP is enabled, SRAM is initialized to the heap */
2020-01-08 22:04:49 +08:00
rt_memheap_init ( & system_heap , " ext_sram " , ( void * ) EXTERNAL_SRAM_BANK_ADDR , EXTERNAL_SRAM_SIZE ) ;
2020-01-06 09:12:45 +08:00
# endif
}
/** Disconnect NADV
*/
__HAL_AFIO_FSMCNADV_DISCONNECTED ( ) ;
return result ;
}
2020-01-08 22:04:49 +08:00
INIT_BOARD_EXPORT ( external_sram_init ) ;
2020-01-06 09:12:45 +08:00
# ifdef DRV_DEBUG
# ifdef FINSH_USING_MSH
2020-01-08 22:04:49 +08:00
int external_sram_test ( void )
2020-01-06 09:12:45 +08:00
{
int i = 0 ;
uint32_t start_time = 0 , time_cast = 0 ;
2020-01-08 22:04:49 +08:00
# if EXTERNAL_SRAM_DATA_WIDTH == 8
2020-01-06 09:12:45 +08:00
char data_width = 1 ;
uint8_t data = 0 ;
2020-01-08 22:04:49 +08:00
uint8_t * ptr = ( uint8_t * ) EXTERNAL_SRAM_BANK_ADDR ;
# elif EXTERNAL_SRAM_DATA_WIDTH == 16
2020-01-06 09:12:45 +08:00
char data_width = 2 ;
uint16_t data = 0 ;
2020-01-08 22:04:49 +08:00
uint16_t * ptr = ( uint16_t * ) EXTERNAL_SRAM_BANK_ADDR ;
2020-01-06 09:12:45 +08:00
# else
char data_width = 4 ;
uint32_t data = 0 ;
2020-01-08 22:04:49 +08:00
uint32_t * ptr = ( uint32_t * ) EXTERNAL_SRAM_BANK_ADDR ;
2020-01-06 09:12:45 +08:00
# endif
/* write data */
2020-01-08 22:04:49 +08:00
LOG_D ( " Writing the %ld bytes data, waiting.... " , EXTERNAL_SRAM_SIZE ) ;
2020-01-06 09:12:45 +08:00
start_time = rt_tick_get ( ) ;
2020-01-08 22:04:49 +08:00
for ( i = 0 ; i < EXTERNAL_SRAM_SIZE / data_width ; i + + )
2020-01-06 09:12:45 +08:00
{
2020-01-08 22:04:49 +08:00
# if EXTERNAL_SRAM_DATA_WIDTH == 8
2020-01-06 09:12:45 +08:00
( ( __IO uint8_t * ) ptr ) [ i ] = ( uint8_t ) 0x55 ;
2020-01-08 22:04:49 +08:00
# elif EXTERNAL_SRAM_DATA_WIDTH == 16
2020-01-06 09:12:45 +08:00
( ( __IO uint16_t * ) ptr ) [ i ] = ( uint16_t ) 0x5555 ;
# else
( ( __IO uint32_t * ) ptr ) [ i ] = ( uint32_t ) 0x55555555 ;
# endif
}
time_cast = rt_tick_get ( ) - start_time ;
LOG_D ( " Write data success, total time: %d.%03dS. " , time_cast / RT_TICK_PER_SECOND ,
time_cast % RT_TICK_PER_SECOND / ( ( RT_TICK_PER_SECOND * 1 + 999 ) / 1000 ) ) ;
/* read data */
LOG_D ( " start Reading and verifying data, waiting.... " ) ;
2020-01-08 22:04:49 +08:00
for ( i = 0 ; i < EXTERNAL_SRAM_SIZE / data_width ; i + + )
2020-01-06 09:12:45 +08:00
{
2020-01-08 22:04:49 +08:00
# if EXTERNAL_SRAM_DATA_WIDTH == 8
2020-01-06 09:12:45 +08:00
data = ( ( __IO uint8_t * ) ptr ) [ i ] ;
if ( data ! = 0x55 )
{
2020-01-08 22:04:49 +08:00
LOG_E ( " External SRAM test failed! " ) ;
2020-01-06 09:12:45 +08:00
break ;
}
2020-01-08 22:04:49 +08:00
# elif EXTERNAL_SRAM_DATA_WIDTH == 16
2020-01-06 09:12:45 +08:00
data = ( ( __IO uint16_t * ) ptr ) [ i ] ;
if ( data ! = 0x5555 )
{
2020-01-08 22:04:49 +08:00
LOG_E ( " External SRAM test failed! " ) ;
2020-01-06 09:12:45 +08:00
break ;
}
# else
data = ( ( __IO uint32_t * ) ptr ) [ i ] ;
if ( data ! = 0x55555555 )
{
2020-01-08 22:04:49 +08:00
LOG_E ( " External SRAM test failed! " ) ;
2020-01-06 09:12:45 +08:00
break ;
}
# endif
}
2020-01-08 22:04:49 +08:00
if ( i > = EXTERNAL_SRAM_SIZE / data_width )
2020-01-06 09:12:45 +08:00
{
2020-01-08 22:04:49 +08:00
LOG_D ( " External SRAM test success! " ) ;
2020-01-06 09:12:45 +08:00
}
return RT_EOK ;
}
2020-01-08 22:04:49 +08:00
MSH_CMD_EXPORT ( external_sram_test , sram test ) ;
2020-01-06 09:12:45 +08:00
# endif /* FINSH_USING_MSH */
# endif /* DRV_DEBUG */
2020-01-08 22:04:49 +08:00
# endif /* BSP_USING_EXT_SRAM */