Skip to content

Commit 945dde1

Browse files
committed
Add FSMC for stm32f40X
1 parent b60639d commit 945dde1

File tree

11 files changed

+309
-0
lines changed

11 files changed

+309
-0
lines changed

arch/ARM/STM32/devices/stm32f40x/stm32-device.adb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,24 @@ package body STM32.Device is
727727
RCC_Periph.AHB2RSTR.DCMIRST := False;
728728
end Reset_DCMI;
729729

730+
-----------------------
731+
-- Enable_FSMC_Clock --
732+
-----------------------
733+
734+
procedure Enable_FSMC_Clock is
735+
begin
736+
STM32_SVD.RCC.RCC_Periph.AHB3ENR.FSMCEN := True;
737+
end Enable_FSMC_Clock;
738+
739+
------------------------
740+
-- Disable_FSMC_Clock --
741+
------------------------
742+
743+
procedure Disable_FSMC_Clock is
744+
begin
745+
STM32_SVD.RCC.RCC_Periph.AHB3ENR.FSMCEN := False;
746+
end Disable_FSMC_Clock;
747+
730748
------------------
731749
-- Enable_Clock --
732750
------------------

arch/ARM/STM32/devices/stm32f40x/stm32-device.ads

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ package STM32.Device is
435435

436436
procedure Reset (This : in out CRC_32);
437437

438+
----------
439+
-- FSMC --
440+
----------
441+
442+
procedure Enable_FSMC_Clock;
443+
procedure Disable_FSMC_Clock;
444+
438445
-----------------------------
439446
-- Reset and Clock Control --
440447
-----------------------------
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2023, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of STMicroelectronics nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
32+
with STM32_SVD.FSMC;
33+
34+
package body STM32.FSMC is
35+
36+
type BCR_BTR is record
37+
BCR : STM32_SVD.FSMC.BCR_Register;
38+
-- SRAM/NOR-Flash chip-select timing register
39+
BTR : STM32_SVD.FSMC.BTR_Register;
40+
-- SRAM/NOR-Flash chip-select control register
41+
end record
42+
with Volatile;
43+
44+
for BCR_BTR use record
45+
BCR at 16#0# range 0 .. 31;
46+
BTR at 16#4# range 0 .. 31;
47+
end record;
48+
49+
type BCR_BTR_Array is array (1 .. 4) of BCR_BTR;
50+
51+
---------------
52+
-- Configure --
53+
---------------
54+
55+
procedure Configure
56+
(Bank_1 : Bank_1_Configuration := (1 .. 4 => (Is_Set => False));
57+
Bank_2 : NAND_PC_Card_Configuration := (Is_Set => False);
58+
Bank_3 : NAND_PC_Card_Configuration := (Is_Set => False);
59+
Bank_4 : NAND_PC_Card_Configuration := (Is_Set => False))
60+
is
61+
pragma Unreferenced (Bank_2, Bank_3, Bank_4);
62+
BCR_BTR_List : BCR_BTR_Array
63+
with Import, Address => STM32_SVD.FSMC.FSMC_Periph.BCR1'Address;
64+
65+
BWTR_List : array (1 .. 4) of STM32_SVD.FSMC.BWTR_Register
66+
with Import, Address => STM32_SVD.FSMC.FSMC_Periph.BWTR1'Address;
67+
begin
68+
for X in Bank_1'Range loop
69+
declare
70+
pragma Warnings (Off, "is not referenced");
71+
-- GNAT GCC 12.2 gives a wrong warning here
72+
BCR : STM32_SVD.FSMC.BCR_Register renames BCR_BTR_List (X).BCR;
73+
BTR : STM32_SVD.FSMC.BTR_Register renames BCR_BTR_List (X).BTR;
74+
pragma Warnings (On, "is not referenced");
75+
BWTR : STM32_SVD.FSMC.BWTR_Register renames BWTR_List (X);
76+
begin
77+
if Bank_1 (X).Is_Set then
78+
declare
79+
Value : Asynchronous_Configuration renames Bank_1 (X).Value;
80+
begin
81+
BCR :=
82+
(MBKEN => True,
83+
MUXEN => False,
84+
MTYP => Memory_Type'Pos (Value.Memory_Type),
85+
MWID => Memory_Bus_Width'Pos (Value.Bus_Width),
86+
FACCEN => Value.Memory_Type = NOR_Flash,
87+
Reserved_7_7 => 1,
88+
BURSTEN => False,
89+
WAITPOL => Value.Wait_Signal = Positive,
90+
WRAPMOD => False,
91+
WAITCFG => False,
92+
WREN => Value.Write_Enable,
93+
WAITEN => False,
94+
EXTMOD => Value.Extended.Mode /= None,
95+
ASYNCWAIT => Value.Wait_Signal /= None,
96+
Reserved_16_18 => 0,
97+
CBURSTRW => False,
98+
Reserved_20_31 => 0);
99+
100+
BTR :=
101+
(ADDSET => HAL.UInt4 (Value.Address_Setup),
102+
ADDHLD =>
103+
(if Value.Extended.Mode = Mode_D
104+
then HAL.UInt4 (Value.Extended.Read_Address_Hold)
105+
else 0),
106+
DATAST => HAL.UInt8 (Value.Data_Setup),
107+
BUSTURN => HAL.UInt4 (Value.Bus_Turn),
108+
CLKDIV => 0,
109+
DATLAT => 0,
110+
ACCMOD =>
111+
(if Value.Extended.Mode = None then 0
112+
else Asynchronous_Extended_Mode'Pos
113+
(Value.Extended.Mode)),
114+
Reserved_30_31 => 3);
115+
116+
if Value.Extended.Mode /= None then
117+
BWTR :=
118+
(ADDSET =>
119+
HAL.UInt4 (Value.Extended.Write_Address_Setup),
120+
ADDHLD =>
121+
(if Value.Extended.Mode = Mode_D
122+
then HAL.UInt4 (Value.Extended.Write_Address_Hold)
123+
else 0),
124+
DATAST =>
125+
HAL.UInt8 (Value.Extended.Write_Data_Setup),
126+
Reserved_16_19 => 16#F#,
127+
CLKDIV => 0,
128+
DATLAT => 0,
129+
ACCMOD => 0,
130+
Reserved_30_31 => 0);
131+
end if;
132+
end;
133+
else
134+
BCR.MBKEN := False;
135+
end if;
136+
end;
137+
end loop;
138+
end Configure;
139+
140+
end STM32.FSMC;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2023, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of STMicroelectronics nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
32+
-- Flexible Static Memory Controller in STM32F40x/41x family
33+
34+
with System;
35+
36+
package STM32.FSMC is
37+
38+
subtype Clock_Cycle_Count is Natural;
39+
-- Number of AHB clock cycles (HCLK)
40+
41+
-- Bank 1 supports NOR Flash, PSRAM and SRAM.
42+
type Memory_Type is (SRAM, PSRAM, NOR_Flash);
43+
type Memory_Bus_Width is (Byte, Half_Word); -- 8 or 16 bit
44+
type Signal_Kind is (None, Positive, Negative);
45+
46+
type Asynchronous_Extended_Mode is (Mode_A, Mode_B, Mode_C, Mode_D, None);
47+
-- See extended mode definitions in STM32F40x/41x datasheet.
48+
-- None corresponds to Mode 1 or Mode 2 depending on the type of memory.
49+
-- Extended modes have a dedicated settings for write operations, while
50+
-- Mode 1, 2 have shared settings for read/write.
51+
52+
type Asynchronous_Extended_Configuration
53+
(Mode : Asynchronous_Extended_Mode := None) is
54+
record
55+
case Mode is
56+
when None =>
57+
null;
58+
59+
when others =>
60+
Write_Bus_Turn : Clock_Cycle_Count range 0 .. 15;
61+
Write_Data_Setup : Clock_Cycle_Count range 1 .. 256;
62+
Write_Address_Setup : Clock_Cycle_Count range 0 .. 15;
63+
64+
case Mode is
65+
when Mode_D =>
66+
Read_Address_Hold : Clock_Cycle_Count range 0 .. 15;
67+
Write_Address_Hold : Clock_Cycle_Count range 0 .. 15;
68+
69+
when others =>
70+
null;
71+
end case;
72+
end case;
73+
end record;
74+
75+
type Asynchronous_Configuration is record
76+
Wait_Signal : Signal_Kind := None;
77+
Write_Enable : Boolean := False;
78+
Bus_Width : Memory_Bus_Width := Byte;
79+
Memory_Type : FSMC.Memory_Type;
80+
Bus_Turn : Clock_Cycle_Count range 0 .. 15 := 15;
81+
Data_Setup : Clock_Cycle_Count range 1 .. 255 := 255;
82+
Address_Setup : Clock_Cycle_Count range 0 .. 15 := 15;
83+
84+
Extended : Asynchronous_Extended_Configuration := (Mode => None);
85+
-- Additional settings
86+
end record;
87+
-- Configuration of the subbank of Bank 1. Not every combination of memory
88+
-- type and extended mode is allowed. Corresponding predicate is:
89+
-- with Predicate =>
90+
-- (if Memory_Type = NOR_Flash then Extended.Mode /= Mode_A
91+
-- else Extended.Mode not in Mode_B | Mode_C);
92+
93+
type NOR_PSRAM_Configuration (Is_Set : Boolean := False) is record
94+
case Is_Set is
95+
when True =>
96+
Value : Asynchronous_Configuration;
97+
when False =>
98+
null;
99+
end case;
100+
end record;
101+
102+
subtype Subbank_Index is Integer range 1 .. 4;
103+
-- Bank 1 has 4 sub-banks
104+
105+
type Bank_1_Configuration is array (Subbank_Index) of NOR_PSRAM_Configuration;
106+
107+
type NAND_PC_Card_Configuration (Is_Set : Boolean := False) is record
108+
null; -- TBD
109+
end record;
110+
-- Bank 2, 3 supports NAND flash, Bank 4 - PC Card
111+
-- These are not yet implemented
112+
113+
procedure Configure
114+
(Bank_1 : Bank_1_Configuration := (1 .. 4 => (Is_Set => False));
115+
Bank_2 : NAND_PC_Card_Configuration := (Is_Set => False);
116+
Bank_3 : NAND_PC_Card_Configuration := (Is_Set => False);
117+
Bank_4 : NAND_PC_Card_Configuration := (Is_Set => False));
118+
-- Configure memory banks
119+
120+
function Bank_1_Start
121+
(Subbank : Subbank_Index := 1) return System.Address is
122+
(case Subbank is
123+
when 1 => System'To_Address (16#6000_0000#),
124+
when 2 => System'To_Address (16#6400_0000#),
125+
when 3 => System'To_Address (16#6800_0000#),
126+
when 4 => System'To_Address (16#6C00_0000#));
127+
128+
Bank_2_Start : constant System.Address :=
129+
System'To_Address (16#7000_0000#);
130+
131+
Bank_3_Start : constant System.Address :=
132+
System'To_Address (16#8000_0000#);
133+
134+
Bank_4_Start : constant System.Address :=
135+
System'To_Address (16#9000_0000#);
136+
137+
end STM32.FSMC;

boards/nucleo_f446ze/nucleo_f446ze_full.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ library project NUCLEO_F446ZE_Full is
105105
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma/", -- From MCU definition
106106
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma_interrupts/", -- From MCU definition
107107
Src_Dirs_Root & "/arch/ARM/STM32/drivers/crc_stm32f4/", -- From MCU definition
108+
Src_Dirs_Root & "/arch/ARM/STM32/drivers/fsmc/", -- From MCU definition
108109
Src_Dirs_Root & "/arch/ARM/STM32/drivers/i2c_stm32f4", -- From MCU definition
109110
Src_Dirs_Root & "/arch/ARM/STM32/drivers/power_control_stm32f4", -- From MCU definition
110111
Src_Dirs_Root & "/arch/ARM/STM32/drivers/uart_stm32f4/", -- From MCU definition

boards/nucleo_f446ze/nucleo_f446ze_sfp.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ library project NUCLEO_F446ZE_SFP is
105105
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma/", -- From MCU definition
106106
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma_interrupts/", -- From MCU definition
107107
Src_Dirs_Root & "/arch/ARM/STM32/drivers/crc_stm32f4/", -- From MCU definition
108+
Src_Dirs_Root & "/arch/ARM/STM32/drivers/fsmc/", -- From MCU definition
108109
Src_Dirs_Root & "/arch/ARM/STM32/drivers/i2c_stm32f4", -- From MCU definition
109110
Src_Dirs_Root & "/arch/ARM/STM32/drivers/power_control_stm32f4", -- From MCU definition
110111
Src_Dirs_Root & "/arch/ARM/STM32/drivers/uart_stm32f4/", -- From MCU definition

boards/stm32f407_discovery/stm32f407_discovery_full.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ library project STM32F407_Discovery_Full is
105105
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma/", -- From MCU definition
106106
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma_interrupts/", -- From MCU definition
107107
Src_Dirs_Root & "/arch/ARM/STM32/drivers/crc_stm32f4/", -- From MCU definition
108+
Src_Dirs_Root & "/arch/ARM/STM32/drivers/fsmc/", -- From MCU definition
108109
Src_Dirs_Root & "/arch/ARM/STM32/drivers/i2c_stm32f4", -- From MCU definition
109110
Src_Dirs_Root & "/arch/ARM/STM32/drivers/power_control_stm32f4", -- From MCU definition
110111
Src_Dirs_Root & "/arch/ARM/STM32/drivers/uart_stm32f4/", -- From MCU definition

boards/stm32f407_discovery/stm32f407_discovery_sfp.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ library project STM32F407_Discovery_SFP is
105105
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma/", -- From MCU definition
106106
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma_interrupts/", -- From MCU definition
107107
Src_Dirs_Root & "/arch/ARM/STM32/drivers/crc_stm32f4/", -- From MCU definition
108+
Src_Dirs_Root & "/arch/ARM/STM32/drivers/fsmc/", -- From MCU definition
108109
Src_Dirs_Root & "/arch/ARM/STM32/drivers/i2c_stm32f4", -- From MCU definition
109110
Src_Dirs_Root & "/arch/ARM/STM32/drivers/power_control_stm32f4", -- From MCU definition
110111
Src_Dirs_Root & "/arch/ARM/STM32/drivers/uart_stm32f4/", -- From MCU definition

boards/stm32f4xx_m/stm32f4xx_m_full.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ library project STM32F4XX_M_Full is
106106
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma/", -- From MCU definition
107107
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma_interrupts/", -- From MCU definition
108108
Src_Dirs_Root & "/arch/ARM/STM32/drivers/crc_stm32f4/", -- From MCU definition
109+
Src_Dirs_Root & "/arch/ARM/STM32/drivers/fsmc/", -- From MCU definition
109110
Src_Dirs_Root & "/arch/ARM/STM32/drivers/i2c_stm32f4", -- From MCU definition
110111
Src_Dirs_Root & "/arch/ARM/STM32/drivers/power_control_stm32f4", -- From MCU definition
111112
Src_Dirs_Root & "/arch/ARM/STM32/drivers/uart_stm32f4/", -- From MCU definition

boards/stm32f4xx_m/stm32f4xx_m_sfp.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ library project STM32F4XX_M_SFP is
106106
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma/", -- From MCU definition
107107
Src_Dirs_Root & "/arch/ARM/STM32/drivers/dma_interrupts/", -- From MCU definition
108108
Src_Dirs_Root & "/arch/ARM/STM32/drivers/crc_stm32f4/", -- From MCU definition
109+
Src_Dirs_Root & "/arch/ARM/STM32/drivers/fsmc/", -- From MCU definition
109110
Src_Dirs_Root & "/arch/ARM/STM32/drivers/i2c_stm32f4", -- From MCU definition
110111
Src_Dirs_Root & "/arch/ARM/STM32/drivers/power_control_stm32f4", -- From MCU definition
111112
Src_Dirs_Root & "/arch/ARM/STM32/drivers/uart_stm32f4/", -- From MCU definition

0 commit comments

Comments
 (0)