blob: fb52cfddd30bc4d30d8544d7de6ef9906d2b0545 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23/*
24 * COPYRIGHT(c) 2015 STMicroelectronics
25 *
26 * Redistribution and use in source and binary forms, with or without modification,
27 * are permitted provided that the following conditions are met:
28 * 1. Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright notice,
31 * this list of conditions and the following disclaimer in the documentation
32 * and/or other materials provided with the distribution.
33 * 3. Neither the name of STMicroelectronics nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 ******************************************************************************
49 */
50
51#include <err.h>
52#include <debug.h>
53#include <trace.h>
54#include <target.h>
55#include <compiler.h>
56#include <dev/gpio.h>
57#include <platform/stm32.h>
58
59#define SRAM_OK ((uint8_t)0x00)
60#define SRAM_ERROR ((uint8_t)0x01)
61
62/* #define SRAM_MEMORY_WIDTH FMC_NORSRAM_MEM_BUS_WIDTH_8*/
63#define SRAM_MEMORY_WIDTH FMC_NORSRAM_MEM_BUS_WIDTH_16
64
65#define SRAM_BURSTACCESS FMC_BURST_ACCESS_MODE_DISABLE
66//#define SRAM_BURSTACCESS FMC_BURST_ACCESS_MODE_ENABLE
67
68#define SRAM_WRITEBURST FMC_WRITE_BURST_DISABLE
69//#define SRAM_WRITEBURST FMC_WRITE_BURST_ENABLE
70
71#define CONTINUOUSCLOCK_FEATURE FMC_CONTINUOUS_CLOCK_SYNC_ONLY
72//#define CONTINUOUSCLOCK_FEATURE FMC_CONTINUOUS_CLOCK_SYNC_ASYNC
73
74/* DMA definitions for SRAM DMA transfer */
75#define __SRAM_DMAx_CLK_ENABLE __HAL_RCC_DMA2_CLK_ENABLE
76#define __SRAM_DMAx_CLK_DISABLE __HAL_RCC_DMA2_CLK_DISABLE
77#define SRAM_DMAx_CHANNEL DMA_CHANNEL_0
78#define SRAM_DMAx_STREAM DMA2_Stream4
79#define SRAM_DMAx_IRQn DMA2_Stream4_IRQn
80#define SRAM_DMAx_IRQHandler DMA2_Stream4_IRQHandler
81
82static SRAM_HandleTypeDef sramHandle;
83static FMC_NORSRAM_TimingTypeDef Timing;
84
85/**
86 * @brief Initializes SRAM MSP.
87 * @param hsram: SRAM handle
88 * @retval None
89 */
90static void BSP_SRAM_MspInit(SRAM_HandleTypeDef *hsram, void *Params)
91{
92 static DMA_HandleTypeDef dma_handle;
93 GPIO_InitTypeDef gpio_init_structure;
94
95 /* Enable FMC clock */
96 __HAL_RCC_FMC_CLK_ENABLE();
97
98 /* Enable chosen DMAx clock */
99 __SRAM_DMAx_CLK_ENABLE();
100
101 /* Enable GPIOs clock */
102 __HAL_RCC_GPIOD_CLK_ENABLE();
103 __HAL_RCC_GPIOE_CLK_ENABLE();
104 __HAL_RCC_GPIOF_CLK_ENABLE();
105 __HAL_RCC_GPIOG_CLK_ENABLE();
106
107 /* Common GPIO configuration */
108 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
109 gpio_init_structure.Pull = GPIO_PULLUP;
110 gpio_init_structure.Speed = GPIO_SPEED_HIGH;
111 gpio_init_structure.Alternate = GPIO_AF12_FMC;
112
113 /* GPIOD configuration */
114 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 |\
115 GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |\
116 GPIO_PIN_14 | GPIO_PIN_15;
117 HAL_GPIO_Init(GPIOD, &gpio_init_structure);
118
119 /* GPIOE configuration */
120 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3| GPIO_PIN_4 | GPIO_PIN_7 |\
121 GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 |\
122 GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
123 HAL_GPIO_Init(GPIOE, &gpio_init_structure);
124
125 /* GPIOF configuration */
126 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\
127 GPIO_PIN_5 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
128 HAL_GPIO_Init(GPIOF, &gpio_init_structure);
129
130 /* GPIOG configuration */
131 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\
132 GPIO_PIN_5 | GPIO_PIN_10;
133 HAL_GPIO_Init(GPIOG, &gpio_init_structure);
134
135 /* Configure common DMA parameters */
136 dma_handle.Init.Channel = SRAM_DMAx_CHANNEL;
137 dma_handle.Init.Direction = DMA_MEMORY_TO_MEMORY;
138 dma_handle.Init.PeriphInc = DMA_PINC_ENABLE;
139 dma_handle.Init.MemInc = DMA_MINC_ENABLE;
140 dma_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
141 dma_handle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
142 dma_handle.Init.Mode = DMA_NORMAL;
143 dma_handle.Init.Priority = DMA_PRIORITY_HIGH;
144 dma_handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
145 dma_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
146 dma_handle.Init.MemBurst = DMA_MBURST_SINGLE;
147 dma_handle.Init.PeriphBurst = DMA_PBURST_SINGLE;
148
149 dma_handle.Instance = SRAM_DMAx_STREAM;
150
151 /* Associate the DMA handle */
152 __HAL_LINKDMA(hsram, hdma, dma_handle);
153
154 /* Deinitialize the Stream for new transfer */
155 HAL_DMA_DeInit(&dma_handle);
156
157 /* Configure the DMA Stream */
158 HAL_DMA_Init(&dma_handle);
159
160 /* NVIC configuration for DMA transfer complete interrupt */
161 HAL_NVIC_SetPriority(SRAM_DMAx_IRQn, 5, 0);
162 HAL_NVIC_EnableIRQ(SRAM_DMAx_IRQn);
163}
164
165/**
166 * @brief Initializes the SRAM device.
167 * @retval SRAM status
168 */
169uint8_t BSP_SRAM_Init(void)
170{
171 static uint8_t sram_status = SRAM_ERROR;
172 /* SRAM device configuration */
173 sramHandle.Instance = FMC_NORSRAM_DEVICE;
174 sramHandle.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
175
176 /* SRAM device configuration */
177 /* Timing configuration derived from system clock (up to 216Mhz)
178 for 108Mhz as SRAM clock frequency */
179 Timing.AddressSetupTime = 2;
180 Timing.AddressHoldTime = 1;
181 Timing.DataSetupTime = 2;
182 Timing.BusTurnAroundDuration = 1;
183 Timing.CLKDivision = 2;
184 Timing.DataLatency = 2;
185 Timing.AccessMode = FMC_ACCESS_MODE_A;
186
187 sramHandle.Init.NSBank = FMC_NORSRAM_BANK3;
188 sramHandle.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
189 sramHandle.Init.MemoryType = FMC_MEMORY_TYPE_SRAM;
190 sramHandle.Init.MemoryDataWidth = SRAM_MEMORY_WIDTH;
191 sramHandle.Init.BurstAccessMode = SRAM_BURSTACCESS;
192 sramHandle.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
193 sramHandle.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
194 sramHandle.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE;
195 sramHandle.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
196 sramHandle.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
197 sramHandle.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
198 sramHandle.Init.WriteBurst = SRAM_WRITEBURST;
199 sramHandle.Init.ContinuousClock = CONTINUOUSCLOCK_FEATURE;
200
201 /* SRAM controller initialization */
202 BSP_SRAM_MspInit(&sramHandle, NULL); /* __weak function can be rewritten by the application */
203 if (HAL_SRAM_Init(&sramHandle, &Timing, &Timing) != HAL_OK) {
204 sram_status = SRAM_ERROR;
205 } else {
206 sram_status = SRAM_OK;
207 }
208 return sram_status;
209}
210
211
212
213