2018-07-10 03:30:27 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the OpenMV project.
|
|
|
|
* Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
|
|
|
|
* This work is licensed under the MIT license, see the file LICENSE for details.
|
|
|
|
*
|
|
|
|
* SDRAM Driver.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
2018-07-17 17:13:49 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "pin_static_af.h"
|
2019-07-02 10:48:19 -04:00
|
|
|
#include "mpu.h"
|
2018-07-10 03:30:27 -04:00
|
|
|
#include "systick.h"
|
|
|
|
#include "sdram.h"
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
#define SDRAM_TIMEOUT ((uint32_t)0xFFFF)
|
2018-07-10 03:30:27 -04:00
|
|
|
#define SDRAM_MODEREG_BURST_LENGTH_1 ((uint16_t)0x0000)
|
|
|
|
#define SDRAM_MODEREG_BURST_LENGTH_2 ((uint16_t)0x0001)
|
|
|
|
#define SDRAM_MODEREG_BURST_LENGTH_4 ((uint16_t)0x0002)
|
|
|
|
#define SDRAM_MODEREG_BURST_LENGTH_8 ((uint16_t)0x0004)
|
|
|
|
#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL ((uint16_t)0x0000)
|
|
|
|
#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED ((uint16_t)0x0008)
|
|
|
|
#define SDRAM_MODEREG_CAS_LATENCY_2 ((uint16_t)0x0020)
|
|
|
|
#define SDRAM_MODEREG_CAS_LATENCY_3 ((uint16_t)0x0030)
|
|
|
|
#define SDRAM_MODEREG_OPERATING_MODE_STANDARD ((uint16_t)0x0000)
|
|
|
|
#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
|
|
|
|
#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE ((uint16_t)0x0200)
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
#if defined(MICROPY_HW_FMC_SDCKE0) && defined(MICROPY_HW_FMC_SDNE0)
|
|
|
|
#define FMC_SDRAM_BANK FMC_SDRAM_BANK1
|
|
|
|
#define FMC_SDRAM_CMD_TARGET_BANK FMC_SDRAM_CMD_TARGET_BANK1
|
|
|
|
#define SDRAM_START_ADDRESS 0xC0000000
|
|
|
|
#elif defined(MICROPY_HW_FMC_SDCKE1) && defined(MICROPY_HW_FMC_SDNE1)
|
|
|
|
#define FMC_SDRAM_BANK FMC_SDRAM_BANK2
|
|
|
|
#define FMC_SDRAM_CMD_TARGET_BANK FMC_SDRAM_CMD_TARGET_BANK2
|
|
|
|
#define SDRAM_START_ADDRESS 0xD0000000
|
|
|
|
#endif
|
|
|
|
|
2019-05-02 01:59:38 -04:00
|
|
|
// Provides the MPU_REGION_SIZE_X value when passed the size of region in bytes
|
|
|
|
// "m" must be a power of 2 between 32 and 4G (2**5 and 2**32) and this formula
|
|
|
|
// computes the log2 of "m", minus 1
|
|
|
|
#define MPU_REGION_SIZE(m) (((m) - 1) / (((m) - 1) % 255 + 1) / 255 % 255 * 8 + 7 - 86 / (((m) - 1) % 255 + 12) - 1)
|
|
|
|
|
|
|
|
#define SDRAM_MPU_REGION_SIZE (MPU_REGION_SIZE(MICROPY_HW_SDRAM_SIZE))
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
#ifdef FMC_SDRAM_BANK
|
|
|
|
|
2020-12-07 18:55:50 -05:00
|
|
|
static void sdram_init_seq(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *command);
|
2018-07-10 03:30:27 -04:00
|
|
|
extern void __fatal_error(const char *msg);
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
bool sdram_init(void) {
|
|
|
|
SDRAM_HandleTypeDef hsdram;
|
|
|
|
FMC_SDRAM_TimingTypeDef SDRAM_Timing;
|
|
|
|
FMC_SDRAM_CommandTypeDef command;
|
|
|
|
|
|
|
|
__HAL_RCC_FMC_CLK_ENABLE();
|
|
|
|
|
|
|
|
#if defined(MICROPY_HW_FMC_SDCKE0)
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDCKE0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDCKE0);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNE0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNE0);
|
2018-07-17 17:13:49 -04:00
|
|
|
|
|
|
|
#elif defined(MICROPY_HW_FMC_SDCKE1)
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDCKE1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDCKE1);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNE1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNE1);
|
2018-07-17 17:13:49 -04:00
|
|
|
#endif
|
|
|
|
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDCLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDCLK);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNCAS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNCAS);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNRAS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNRAS);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_SDNWE, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_SDNWE);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_BA0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_BA0);
|
2021-04-05 06:55:39 -04:00
|
|
|
#ifdef MICROPY_HW_FMC_BA1
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_BA1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_BA1);
|
2021-04-05 06:55:39 -04:00
|
|
|
#endif
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_NBL0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_NBL0);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_NBL1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_NBL1);
|
2018-09-19 21:29:37 -04:00
|
|
|
#ifdef MICROPY_HW_FMC_NBL2
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_NBL2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_NBL2);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_NBL3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_NBL3);
|
2018-09-19 21:29:37 -04:00
|
|
|
#endif
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A0);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A1);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A2);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A3);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A4, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A4);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A5, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A5);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A6, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A6);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A7, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A7);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A8, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A8);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A9, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A9);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A10, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A10);
|
2021-04-05 06:55:39 -04:00
|
|
|
#ifdef MICROPY_HW_FMC_A11
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A11);
|
2021-04-05 06:55:39 -04:00
|
|
|
#endif
|
2018-07-17 17:13:49 -04:00
|
|
|
#ifdef MICROPY_HW_FMC_A12
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_A12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_A12);
|
2018-07-17 17:13:49 -04:00
|
|
|
#endif
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D0);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D1);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D2);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D3);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D4, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D4);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D5, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D5);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D6, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D6);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D7, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D7);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D8, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D8);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D9, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D9);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D10, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D10);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D11);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D12);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D13, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D13);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D14, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D14);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D15, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D15);
|
2018-09-19 21:29:37 -04:00
|
|
|
#ifdef MICROPY_HW_FMC_D16
|
2019-02-18 19:11:41 -05:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D16, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D16);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D17, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D17);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D18, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D18);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D19, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D19);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D20, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D20);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D21, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D21);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D22, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D22);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D23, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D23);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D24, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D24);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D25, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D25);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D26, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D26);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D27, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D27);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D28, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D28);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D29, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D29);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D30, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D30);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_FMC_D31, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_FMC_D31);
|
2018-09-19 21:29:37 -04:00
|
|
|
#endif
|
2018-07-17 17:13:49 -04:00
|
|
|
|
2018-07-10 03:30:27 -04:00
|
|
|
/* SDRAM device configuration */
|
|
|
|
hsdram.Instance = FMC_SDRAM_DEVICE;
|
|
|
|
/* Timing configuration for 90 Mhz of SD clock frequency (180Mhz/2) */
|
|
|
|
/* TMRD: 2 Clock cycles */
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_Timing.LoadToActiveDelay = MICROPY_HW_SDRAM_TIMING_TMRD;
|
2018-07-10 03:30:27 -04:00
|
|
|
/* TXSR: min=70ns (6x11.90ns) */
|
2018-07-17 17:13:49 -04:00
|
|
|
SDRAM_Timing.ExitSelfRefreshDelay = MICROPY_HW_SDRAM_TIMING_TXSR;
|
|
|
|
/* TRAS */
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_Timing.SelfRefreshTime = MICROPY_HW_SDRAM_TIMING_TRAS;
|
2018-07-17 17:13:49 -04:00
|
|
|
/* TRC */
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_Timing.RowCycleDelay = MICROPY_HW_SDRAM_TIMING_TRC;
|
2018-07-17 17:13:49 -04:00
|
|
|
/* TWR */
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_Timing.WriteRecoveryTime = MICROPY_HW_SDRAM_TIMING_TWR;
|
2018-07-17 17:13:49 -04:00
|
|
|
/* TRP */
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_Timing.RPDelay = MICROPY_HW_SDRAM_TIMING_TRP;
|
2018-07-17 17:13:49 -04:00
|
|
|
/* TRCD */
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_Timing.RCDDelay = MICROPY_HW_SDRAM_TIMING_TRCD;
|
2018-07-17 17:13:49 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
#define _FMC_INIT(x, n) x##_##n
|
2018-07-17 17:13:49 -04:00
|
|
|
#define FMC_INIT(x, n) _FMC_INIT(x, n)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
hsdram.Init.SDBank = FMC_SDRAM_BANK;
|
|
|
|
hsdram.Init.ColumnBitsNumber = FMC_INIT(FMC_SDRAM_COLUMN_BITS_NUM, MICROPY_HW_SDRAM_COLUMN_BITS_NUM);
|
|
|
|
hsdram.Init.RowBitsNumber = FMC_INIT(FMC_SDRAM_ROW_BITS_NUM, MICROPY_HW_SDRAM_ROW_BITS_NUM);
|
|
|
|
hsdram.Init.MemoryDataWidth = FMC_INIT(FMC_SDRAM_MEM_BUS_WIDTH, MICROPY_HW_SDRAM_MEM_BUS_WIDTH);
|
2018-07-17 17:13:49 -04:00
|
|
|
hsdram.Init.InternalBankNumber = FMC_INIT(FMC_SDRAM_INTERN_BANKS_NUM, MICROPY_HW_SDRAM_INTERN_BANKS_NUM);
|
2020-02-26 23:36:53 -05:00
|
|
|
hsdram.Init.CASLatency = FMC_INIT(FMC_SDRAM_CAS_LATENCY, MICROPY_HW_SDRAM_CAS_LATENCY);
|
|
|
|
hsdram.Init.SDClockPeriod = FMC_INIT(FMC_SDRAM_CLOCK_PERIOD, MICROPY_HW_SDRAM_CLOCK_PERIOD);
|
|
|
|
hsdram.Init.ReadPipeDelay = FMC_INIT(FMC_SDRAM_RPIPE_DELAY, MICROPY_HW_SDRAM_RPIPE_DELAY);
|
|
|
|
hsdram.Init.ReadBurst = (MICROPY_HW_SDRAM_RBURST) ? FMC_SDRAM_RBURST_ENABLE : FMC_SDRAM_RBURST_DISABLE;
|
|
|
|
hsdram.Init.WriteProtection = (MICROPY_HW_SDRAM_WRITE_PROTECTION) ? FMC_SDRAM_WRITE_PROTECTION_ENABLE : FMC_SDRAM_WRITE_PROTECTION_DISABLE;
|
2018-07-10 03:30:27 -04:00
|
|
|
|
|
|
|
/* Initialize the SDRAM controller */
|
2020-02-26 23:36:53 -05:00
|
|
|
if (HAL_SDRAM_Init(&hsdram, &SDRAM_Timing) != HAL_OK) {
|
2018-07-10 03:30:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdram_init_seq(&hsdram, &command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
void *sdram_start(void) {
|
2020-02-26 23:36:53 -05:00
|
|
|
return (void *)SDRAM_START_ADDRESS;
|
2018-07-17 17:13:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *sdram_end(void) {
|
2020-02-26 23:36:53 -05:00
|
|
|
return (void *)(SDRAM_START_ADDRESS + MICROPY_HW_SDRAM_SIZE);
|
2018-07-17 17:13:49 -04:00
|
|
|
}
|
|
|
|
|
2018-07-10 03:30:27 -04:00
|
|
|
static void sdram_init_seq(SDRAM_HandleTypeDef
|
2020-02-26 23:36:53 -05:00
|
|
|
*hsdram, FMC_SDRAM_CommandTypeDef *command) {
|
2018-07-10 03:30:27 -04:00
|
|
|
/* Program the SDRAM external device */
|
2020-02-26 23:36:53 -05:00
|
|
|
__IO uint32_t tmpmrd = 0;
|
2018-07-10 03:30:27 -04:00
|
|
|
|
|
|
|
/* Step 3: Configure a clock configuration enable command */
|
2020-02-26 23:36:53 -05:00
|
|
|
command->CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
|
|
|
|
command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK;
|
|
|
|
command->AutoRefreshNumber = 1;
|
2018-07-10 03:30:27 -04:00
|
|
|
command->ModeRegisterDefinition = 0;
|
|
|
|
|
|
|
|
/* Send the command */
|
|
|
|
HAL_SDRAM_SendCommand(hsdram, command, 0x1000);
|
|
|
|
|
|
|
|
/* Step 4: Insert 100 ms delay */
|
|
|
|
HAL_Delay(100);
|
|
|
|
|
|
|
|
/* Step 5: Configure a PALL (precharge all) command */
|
2020-02-26 23:36:53 -05:00
|
|
|
command->CommandMode = FMC_SDRAM_CMD_PALL;
|
|
|
|
command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK;
|
|
|
|
command->AutoRefreshNumber = 1;
|
2018-07-10 03:30:27 -04:00
|
|
|
command->ModeRegisterDefinition = 0;
|
|
|
|
|
|
|
|
/* Send the command */
|
|
|
|
HAL_SDRAM_SendCommand(hsdram, command, 0x1000);
|
|
|
|
|
|
|
|
/* Step 6 : Configure a Auto-Refresh command */
|
2020-02-26 23:36:53 -05:00
|
|
|
command->CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
|
|
|
|
command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK;
|
|
|
|
command->AutoRefreshNumber = MICROPY_HW_SDRAM_AUTOREFRESH_NUM;
|
2018-07-10 03:30:27 -04:00
|
|
|
command->ModeRegisterDefinition = 0;
|
|
|
|
|
|
|
|
/* Send the command */
|
|
|
|
HAL_SDRAM_SendCommand(hsdram, command, 0x1000);
|
|
|
|
|
|
|
|
/* Step 7: Program the external memory mode register */
|
2018-08-05 21:12:43 -04:00
|
|
|
tmpmrd = (uint32_t)FMC_INIT(SDRAM_MODEREG_BURST_LENGTH, MICROPY_HW_SDRAM_BURST_LENGTH) |
|
2020-02-26 23:36:53 -05:00
|
|
|
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |
|
2018-07-17 17:13:49 -04:00
|
|
|
FMC_INIT(SDRAM_MODEREG_CAS_LATENCY, MICROPY_HW_SDRAM_CAS_LATENCY) |
|
2018-07-10 03:30:27 -04:00
|
|
|
SDRAM_MODEREG_OPERATING_MODE_STANDARD |
|
|
|
|
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
command->CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
|
|
|
|
command->CommandTarget = FMC_SDRAM_CMD_TARGET_BANK;
|
|
|
|
command->AutoRefreshNumber = 1;
|
2018-07-10 03:30:27 -04:00
|
|
|
command->ModeRegisterDefinition = tmpmrd;
|
|
|
|
|
|
|
|
/* Send the command */
|
|
|
|
HAL_SDRAM_SendCommand(hsdram, command, 0x1000);
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
/* Step 8: Set the refresh rate counter
|
|
|
|
RefreshRate = 64 ms / 8192 cyc = 7.8125 us/cyc
|
|
|
|
|
|
|
|
RefreshCycles = 7.8125 us * 90 MHz = 703
|
|
|
|
According to the formula on p.1665 of the reference manual,
|
|
|
|
we also need to subtract 20 from the value, so the target
|
|
|
|
refresh rate is 703 - 20 = 683.
|
|
|
|
*/
|
|
|
|
#define REFRESH_COUNT (MICROPY_HW_SDRAM_REFRESH_RATE * 90000 / 8192 - 20)
|
2018-07-10 03:30:27 -04:00
|
|
|
HAL_SDRAM_ProgramRefreshRate(hsdram, REFRESH_COUNT);
|
2018-07-16 20:32:36 -04:00
|
|
|
|
|
|
|
#if defined(STM32F7)
|
|
|
|
/* Enable MPU for the SDRAM Memory Region to allow non-aligned
|
|
|
|
accesses (hard-fault otherwise)
|
2019-05-02 01:59:38 -04:00
|
|
|
Initially disable all access for the entire SDRAM memory space,
|
|
|
|
then enable access/caching for the size used
|
|
|
|
*/
|
2019-10-20 20:43:22 -04:00
|
|
|
uint32_t irq_state = mpu_config_start();
|
2019-07-02 10:48:19 -04:00
|
|
|
mpu_config_region(MPU_REGION_SDRAM1, SDRAM_START_ADDRESS, MPU_CONFIG_DISABLE(0x00, MPU_REGION_SIZE_512MB));
|
|
|
|
mpu_config_region(MPU_REGION_SDRAM2, SDRAM_START_ADDRESS, MPU_CONFIG_SDRAM(SDRAM_MPU_REGION_SIZE));
|
2019-10-20 20:43:22 -04:00
|
|
|
mpu_config_end(irq_state);
|
2018-07-16 20:32:36 -04:00
|
|
|
#endif
|
2018-07-10 03:30:27 -04:00
|
|
|
}
|
|
|
|
|
2020-12-07 18:55:50 -05:00
|
|
|
void sdram_enter_low_power(void) {
|
|
|
|
// Enter self-refresh mode.
|
|
|
|
// In self-refresh mode the SDRAM retains data with external clocking.
|
|
|
|
FMC_SDRAM_DEVICE->SDCMR |= (FMC_SDRAM_CMD_SELFREFRESH_MODE | // Command Mode
|
|
|
|
FMC_SDRAM_CMD_TARGET_BANK | // Command Target
|
|
|
|
(0 << 5U) | // Auto Refresh Number -1
|
|
|
|
(0 << 9U)); // Mode Register Definition
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdram_leave_low_power(void) {
|
|
|
|
// Exit self-refresh mode.
|
|
|
|
// Self-refresh mode is exited when the device is accessed or the mode bits are
|
|
|
|
// set to Normal mode, so technically it's not necessary to call this functions.
|
|
|
|
FMC_SDRAM_DEVICE->SDCMR |= (FMC_SDRAM_CMD_NORMAL_MODE | // Command Mode
|
|
|
|
FMC_SDRAM_CMD_TARGET_BANK | // Command Target
|
|
|
|
(0 << 5U) | // Auto Refresh Number - 1
|
|
|
|
(0 << 9U)); // Mode Register Definition
|
|
|
|
}
|
|
|
|
|
2021-06-10 08:48:54 -04:00
|
|
|
#if __GNUC__ >= 11
|
|
|
|
// Prevent array bounds warnings when accessing SDRAM_START_ADDRESS as a memory pointer.
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
|
|
|
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
|
|
|
#endif
|
|
|
|
|
2020-01-30 19:31:23 -05:00
|
|
|
bool sdram_test(bool fast) {
|
2018-07-17 17:13:49 -04:00
|
|
|
uint8_t const pattern = 0xaa;
|
|
|
|
uint8_t const antipattern = 0x55;
|
2020-02-26 23:36:53 -05:00
|
|
|
uint8_t *const mem_base = (uint8_t *)sdram_start();
|
2018-07-10 03:30:27 -04:00
|
|
|
|
|
|
|
/* test data bus */
|
2018-07-17 17:13:49 -04:00
|
|
|
for (uint8_t i = 1; i; i <<= 1) {
|
2018-07-10 03:30:27 -04:00
|
|
|
*mem_base = i;
|
|
|
|
if (*mem_base != i) {
|
|
|
|
printf("data bus lines test failed! data (%d)\n", i);
|
2020-01-30 19:31:23 -05:00
|
|
|
return false;
|
2018-07-10 03:30:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test address bus */
|
|
|
|
/* Check individual address lines */
|
2018-07-17 17:13:49 -04:00
|
|
|
for (uint32_t i = 1; i < MICROPY_HW_SDRAM_SIZE; i <<= 1) {
|
2018-07-10 03:30:27 -04:00
|
|
|
mem_base[i] = pattern;
|
|
|
|
if (mem_base[i] != pattern) {
|
|
|
|
printf("address bus lines test failed! address (%p)\n", &mem_base[i]);
|
2020-01-30 19:31:23 -05:00
|
|
|
return false;
|
2018-07-10 03:30:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for aliasing (overlaping addresses) */
|
|
|
|
mem_base[0] = antipattern;
|
2018-07-17 17:13:49 -04:00
|
|
|
for (uint32_t i = 1; i < MICROPY_HW_SDRAM_SIZE; i <<= 1) {
|
2018-07-10 03:30:27 -04:00
|
|
|
if (mem_base[i] != pattern) {
|
|
|
|
printf("address bus overlap %p\n", &mem_base[i]);
|
2020-01-30 19:31:23 -05:00
|
|
|
return false;
|
2018-07-10 03:30:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test all ram cells */
|
2018-07-17 17:13:49 -04:00
|
|
|
if (!fast) {
|
|
|
|
for (uint32_t i = 0; i < MICROPY_HW_SDRAM_SIZE; ++i) {
|
|
|
|
mem_base[i] = pattern;
|
|
|
|
if (mem_base[i] != pattern) {
|
|
|
|
printf("address bus test failed! address (%p)\n", &mem_base[i]);
|
2020-01-30 19:31:23 -05:00
|
|
|
return false;
|
2018-07-17 17:13:49 -04:00
|
|
|
}
|
2018-07-10 03:30:27 -04:00
|
|
|
}
|
2018-07-17 17:13:49 -04:00
|
|
|
} else {
|
|
|
|
memset(mem_base, pattern, MICROPY_HW_SDRAM_SIZE);
|
2018-07-10 03:30:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-17 17:13:49 -04:00
|
|
|
|
2021-06-10 08:48:54 -04:00
|
|
|
#if __GNUC__ >= 11
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2018-07-17 17:13:49 -04:00
|
|
|
#endif // FMC_SDRAM_BANK
|