Ten post chciałbym poświęcić na opisanie sposobu generacji oraz wykonania projektu, w oparciu o biblioteki hala oraz środowisko CubeMx w celu przygotowania projektu obsługującego pamieć SDRAM ISSI IS42S16400.
Cube Mx:
Poniżej przedstawię sposób wykonania projektu dla pamięci SDRAM w programie CubeMx.
Podłączenie pamięci na płytce SDRAM jest następujące:
- FMC SDCKE1 - PB5;
- FMC SDNE1 - PB6;
- FMC SDNWE - PC0;
- FMC D2 - PD0;
- FMC D3 - PD1;
- FMC D13 - PD8;
- FMC D14 - PD9;
- FMC D15 - PD10;
- FMC D0 - PD14;
- FMC D1 - PD15;
- FMC NBL0 - PE0;
- FMC NBL1 - PE1;
- FMC D4 - PE7;
- FMC D5 - PE8;
- FMC D6 - PE9;
- FMC D7 - PE10;
- FMC D8 - PE11;
- FMC D9 - PE12;
- FMC D10 - PE13;
- FMC D11 - PE14;
- FMC D12 - PE15;
- FMC A0 - PF0;
- FMC A1 - PF1;
- FMC A2 - PF2;
- FMC A3 - PF3;
- FMC A4 - PF4;
- FMC A5 - PF5;
- FMC SDNRAS - PF11;
- FMC A6 - PF12;
- FMC A7 - PF13;
- FMC A8 - PF14;
- FMC A9 - PF15;
- FMC A10 - PG0;
- FMC A11 - PG1;
- FMC BA0 - PG4;
- FMC BA1 - PG5;
- FMC SDCLK - PG8;
- FMC SDNCAS - PG15;
Konfiguracja FMC w pinout wygląda następująco:
Konfiguracja zegara wygląda następująco:
Ustawienie FMC w głównej konfiguracji prezentuje się następująco:
Programowanie:
Funkcja uruchamiająca zegary, wygląda następująco:
- static void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* The voltage scaling allows optimizing the power consumption when the device is
- clocked below the maximum system frequency, to update the voltage scaling value
- regarding system frequency refer to product datasheet. */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 360;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- /* Activate the Over-Drive mode */
- HAL_PWREx_EnableOverDrive();
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
- }
Procedura inicjalizacyjna przygotowana w programie Cube Mx wygląda następująco:
- uint8_t SDRAM_Init(void)
- {
- static uint8_t initialized = 0;
- /* SDRAM handler declaration */
- SDRAM_HandleTypeDef hsdram;
- FMC_SDRAM_TimingTypeDef SDRAM_Timing;
- FMC_SDRAM_CommandTypeDef command;
- /* If it is already initialized */
- if (initialized) { return 1; }
- /* Set instance */
- hsdram.Instance = FMC_SDRAM_DEVICE;
- /* Init pins of FMC controler */
- SDRAM_InitPins();
- /* Enable FMC clock */
- __HAL_RCC_FMC_CLK_ENABLE();
- /* Timing configuration for 90 Mhz of SD clock frequency (180Mhz/2) */
- /* TMRD: 2 Clock cycles */
- SDRAM_Timing.LoadToActiveDelay = 2; /* 1 clock cycle = 1 / 90MHz = 11.1ns */
- SDRAM_Timing.ExitSelfRefreshDelay = 7; /* TXSR: min=70ns (6x11.10ns) */
- SDRAM_Timing.SelfRefreshTime = 4; /* TRAS: min=42ns (4x11.10ns) max=120k (ns) */
- SDRAM_Timing.RowCycleDelay = 7; /* TRC: min=70 (6x11.10ns) */
- SDRAM_Timing.WriteRecoveryTime = 2; /* TWR: 2 Cycles */
- SDRAM_Timing.RPDelay = 2; /* TRP: 20ns => 2x11.10ns */
- SDRAM_Timing.RCDDelay = 2; /* TRCD: 20ns => 2x11.10ns */
- /* FMC SDRAM control configuration */
- hsdram.Init.SDBank = FMC_SDRAM_BANK2;
- hsdram.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
- hsdram.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
- hsdram.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
- hsdram.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
- hsdram.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
- hsdram.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
- hsdram.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
- hsdram.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
- hsdram.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
- /* Initialize the SDRAM controller */
- if(HAL_SDRAM_Init(&hsdram, &SDRAM_Timing) != HAL_OK)
- {
- /* Initialization Error */
- }
- /* SDRAM Init sequence */
- /* Configure a clock configuration enable command */
- command.CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
- command.CommandTarget = SDRAM_COMMAND_TARGET_BANK;
- command.AutoRefreshNumber = 1;
- command.ModeRegisterDefinition = 0;
- /* Send command */
- HAL_SDRAM_SendCommand(&hsdram, &command, SDRAM_TIMEOUT);
- /* Little delay */
- HAL_Delay(130);
- /* Configure a PALL (precharge all) command */
- command.CommandMode = FMC_SDRAM_CMD_PALL;
- command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = 1;
- command.ModeRegisterDefinition = 0;
- /* Send the command */
- HAL_SDRAM_SendCommand(&hsdram, &command, SDRAM_TIMEOUT);
- /* Configure a Auto-Refresh command */
- command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
- command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = 4;
- command.ModeRegisterDefinition = 0;
- /* Send the command */
- HAL_SDRAM_SendCommand(&hsdram, &command, SDRAM_TIMEOUT);
- /* Configure a load Mode register command */
- command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
- command.CommandTarget = SDRAM_COMMAND_TARGET_BANK;
- command.AutoRefreshNumber = 1;
- command.ModeRegisterDefinition = (uint32_t)SDRAM_REG_VALUE;
- /* Wait until the SDRAM controller is ready */
- HAL_SDRAM_SendCommand(&hsdram, &command, SDRAM_TIMEOUT);
- /* Set the refresh rate counter */
- HAL_SDRAM_ProgramRefreshRate(&hsdram, SDRAM_REFRESH_COUNT);
- HAL_Delay(100);
- /* Check if there is nor Error in initialization */
- SDRAM_Write_8_Bit(0x20, 0xA5);
- /* Read and check */
- if (SDRAM_Read_8_Bit(0x20) == 0xA5) {
- initialized = 1;
- return 1;
- }
- /* Not initialized ERROR */
- initialized = 0;
- /* ERROR */
- return 0;
- }
W przypadku wykorzystywania funkcji wygenerowanej przez program, która wygląda następująco:
- /* FMC initialization function */
- static void MX_FMC_Init(void)
- {
- FMC_SDRAM_TimingTypeDef SdramTiming;
- /** Perform the SDRAM1 memory initialization sequence
- */
- hsdram1.Instance = FMC_SDRAM_DEVICE;
- /* hsdram1.Init */
- hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
- hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
- hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_13;
- hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
- hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_2;
- hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
- hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
- hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
- hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
- hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
- /* SdramTiming */
- SdramTiming.LoadToActiveDelay = 2;
- SdramTiming.ExitSelfRefreshDelay = 7;
- SdramTiming.SelfRefreshTime = 4;
- SdramTiming.RowCycleDelay = 7;
- SdramTiming.WriteRecoveryTime = 3;
- SdramTiming.RPDelay = 2;
- SdramTiming.RCDDelay = 2;
- if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
To aby uruchomić poprawnie SDRAM należy jeszcze przesłać odpowiednie komendy do układu, wywołuje się funkcjami umieszczonymi w bibliotece:
- //=================================================================================
- uint8_t SDRAM_Set_Operation(void)
- {
- FMC_SDRAM_CommandTypeDef command;
- /* Configure a clock configuration enable command */
- command.CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
- command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = 1;
- command.ModeRegisterDefinition = 0;
- /* Send command */
- HAL_SDRAM_SendCommand(&hsdram1, &command, SDRAM_TIMEOUT);
- /* Little delay */
- HAL_Delay(130);
- /* Configure a PALL (precharge all) command */
- command.CommandMode = FMC_SDRAM_CMD_PALL;
- command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = 1;
- command.ModeRegisterDefinition = 0;
- /* Send the command */
- HAL_SDRAM_SendCommand(&hsdram1, &command, SDRAM_TIMEOUT);
- /* Configure a Auto-Refresh command */
- command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
- command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = 4;
- command.ModeRegisterDefinition = 0;
- /* Send the command */
- HAL_SDRAM_SendCommand(&hsdram1, &command, SDRAM_TIMEOUT);
- /* Configure a load Mode register command */
- command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
- command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
- command.AutoRefreshNumber = 1;
- command.ModeRegisterDefinition = (uint32_t)SDRAM_REG_VALUE;
- /* Wait until the SDRAM controller is ready */
- HAL_SDRAM_SendCommand(&hsdram1, &command, SDRAM_TIMEOUT);
- /* Set the refresh rate counter */
- HAL_SDRAM_ProgramRefreshRate(&hsdram1, SDRAM_REFRESH_COUNT);
- HAL_Delay(100);
- if(Check_If_SDRAM_Works_OK() == 1)
- {
- return 1;
- }
- /* ERROR */
- return 0;
- }
- //=================================================================================
- static uint8_t Check_If_SDRAM_Works_OK(void)
- {
- static uint8_t check = 0;
- SDRAM_Write_8_Bit(0x20, 0xA5);
- if (SDRAM_Read_8_Bit(0x20) == 0xA5) {
- check = 1;
- return check;
- }
- /* Not initialized ERROR */
- check = 0;
- return check;
- }
Włączenie podłączonych wyprowadzeń wykonuje się za pomocą następującej instrukcji:
- /* Private functions */
- static void SDRAM_InitPins(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* Enable clocks */
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOE_CLK_ENABLE();
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOG_CLK_ENABLE();
- /* GPIO B */
- GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* GPIO C */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /* GPIO D */
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 |
- GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- /* GPIO E */
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7 |
- GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |
- GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |
- GPIO_PIN_14 | GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
- HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
- /* GPIO F */
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |
- GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_11 |
- GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
- HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
- /* GPIO G */
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 |
- GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
- HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
- }
Włączenie pinów odbywa się albo poprzez CubeMx gdzie wybiera się odpowiednie wyprowadzenia wraz z wszystkimi ustawieniami. W przypadku korzystania z funkcji z biblioteki, procedura inicjalizująca odwołuje się do funkcji statycznej, która ustawia wyprowadzenia dla pamięci.
Teraz funkcje pozwalające na wpisywanie podanych danych do programu. Funkcje zapisu i odczytu danych 8 bitowych:
- //=================================================================================
- /*
- * @brief Write 8 bit data value into SDRAM
- * @param addressOffset from start value
- * dataToSave data that will be save in SDRAM
- * @ret none
- */
- inline void SDRAM_Write_8_Bit(uint32_t addressOffset, uint8_t dataToSave){
- (*(__IO uint8_t *) (SDRAM_START_ADR + (addressOffset)) = (dataToSave));
- }
- //=================================================================================
- /*
- * @brief read 8 bit data value from SDRAM
- * @param addressOffset from start value
- * @ret value that has been read from Sdram
- */
- inline uint8_t SDRAM_Read_8_Bit(uint32_t addressOffset){
- return (*(__IO uint8_t *) (SDRAM_START_ADR + (addressOffset)));
- }
Funkcje zapisu i odczytu danych 16 bitowych:
- //=================================================================================
- /*
- * @brief Write 16 bit data value into SDRAM
- * @param addressOffset from start value
- * dataToSave data that will be save in SDRAM
- * @ret none
- */
- inline void SDRAM_Write_16_Bit(uint32_t addressOffset, uint16_t dataToSave) {
- (*(__IO uint16_t *) (SDRAM_START_ADR + (addressOffset)) = (dataToSave));
- }
- //=================================================================================
- /*
- * @brief Read 16 bit data value into SDRAM
- * @param addressOffset from start value
- * @ret none
- */
- inline uint16_t SDRAM_Read_16_Bit(uint32_t address){
- return (*(__IO uint16_t *) (SDRAM_START_ADR + (address)));
- }
Funkcje zapisu i odczytu danych 32 bitowych:
- //=================================================================================
- /*
- * @brief Write 32 bit data value into SDRAM
- * @param addressOffset from start value
- * dataToSave data that will be save in SDRAM
- * @ret none
- */
- inline void SDRAM_Write_32_Bit(uint32_t addressOffset, uint32_t dataToSave){
- (*(__IO uint32_t *) (SDRAM_START_ADR + (addressOffset)) = (dataToSave));
- }
- //=================================================================================
- /*
- * @brief Reads 32 bit data value into SDRAM
- * @param addressOffset from start value
- * dataToSave data that will be save in SDRAM
- * @ret none
- */
- inline uint32_t SDRAM_Read_32_Bit(uint32_t address){
- return (*(__IO uint32_t *) (SDRAM_START_ADR + (address)));
- }
Funkcje zapisu i odczytu danych float:
- //=================================================================================
- /*
- * @brief Write float bit data value into SDRAM
- * @param addressOffset from start value
- * dataToSave data that will be save in SDRAM
- * @ret none
- */
- inline void SDRAM_Write_Float(uint32_t addressOffset, uint32_t dataToSave){
- (*(__IO float *) (SDRAM_START_ADR + (addressOffset)) = (dataToSave));
- }
- //=================================================================================
- /*
- * @brief Reads float bit data value into SDRAM
- * @param addressOffset from start value
- * dataToSave data that will be save in SDRAM
- * @ret none
- */
- inline float SDRAM_Read_Float(uint32_t addressOffset){
- return (*(__IO float *) (SDRAM_START_ADR + (addressOffset)));
- }
Pętla główna programu wygląda następująco:
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_FMC_Init(void);
- static void MX_USART1_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- uint32_t WriteBuffer[32];
- uint32_t ReadBuffer[32];
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint8_t i = 0;
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_FMC_Init();
- SDRAM_Set_Operation();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- /* When using functions from library */
- //SDRAM_Init();
- /* Fill write data */
- for (i = 0; i < 255; i++) {
- WriteBuffer[i] = 0xA5;
- /* Write to SDRAM */
- SDRAM_Write_32_Bit(16 * i, WriteBuffer[i]);
- }
- /* Read data from SDRAM */
- for (i = 0; i < 255; i++) {
- ReadBuffer[i] = SDRAM_Read_32_Bit(16 * i);
- }
- /* Memory compare */
- if (memcmp((uint8_t *)ReadBuffer, (uint8_t *)WriteBuffer, sizeof(ReadBuffer)) == 0) {
- /* SDRAM is OK */
- HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, GPIO_PIN_SET);
- } else {
- /* SDRAM failed */
- HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
Pliki z projektem można znaleźć na dysku Google pod tym linkiem.