Skip to content

Commit 2e50b68

Browse files
committed
Allow SD card detection pin to be shared with DAT3 pin.
In this configuration SDCard_Controller detects SD card only betwee Initialize call and Card_Present=True. After Card_Present because True the controller reconfigures DAT3 to SDIO mode.
1 parent 31e36cf commit 2e50b68

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

boards/stm32_common/sdcard/sdcard.adb

+60-8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ package body SDCard is
4747
(Interrupt_ID => SD_Interrupt,
4848
Priority => System.Interrupt_Priority'Last);
4949

50+
-----------------------
51+
-- Has_SD_Detect_Pin --
52+
-----------------------
53+
54+
function Has_SD_Detect_Pin return Boolean is
55+
begin
56+
return (for all Pin of SD_Pins => Pin.Pin /= SD_Detect_Pin.Pin);
57+
end Has_SD_Detect_Pin;
58+
5059
----------------
5160
-- Initialize --
5261
----------------
@@ -78,12 +87,19 @@ package body SDCard is
7887
Resistors => Pull_Up));
7988

8089
-- GPIO configuration for the SD-Detect pin
81-
Configure_IO
82-
(SD_Detect_Pin,
83-
(Mode => Mode_In,
84-
-- Output_Type => Open_Drain,
85-
-- Speed => Speed_High,
86-
Resistors => Pull_Up));
90+
if Has_SD_Detect_Pin then
91+
Configure_IO
92+
(SD_Detect_Pin,
93+
(Mode => Mode_In,
94+
Resistors => Pull_Up));
95+
else
96+
Configure_IO
97+
(SD_Detect_Pin,
98+
(Mode => Mode_In,
99+
Resistors => Pull_Down));
100+
101+
This.Card_Detected := False;
102+
end if;
87103

88104
-- Enable the SDIO clock
89105
STM32.Device.Enable_Clock (This.Device.all);
@@ -140,8 +156,30 @@ package body SDCard is
140156
function Card_Present
141157
(This : in out SDCard_Controller) return Boolean
142158
is
159+
function Card_Detect return Boolean;
160+
-- Check SD_Detect_Pin depending on the presence of a dedicated pin
161+
162+
-----------------
163+
-- Card_Detect --
164+
-----------------
165+
166+
function Card_Detect return Boolean is
167+
begin
168+
if Has_SD_Detect_Pin then
169+
-- We have a dedicated pin.
170+
return not STM32.GPIO.Set (SD_Detect_Pin);
171+
elsif This.Card_Detected then
172+
-- We have no dedicated pin.
173+
-- In normal operation mode no detection is possible.
174+
return True;
175+
else
176+
-- We have no dedicated pin. Check DAT3 pin
177+
return STM32.GPIO.Set (SD_Detect_Pin);
178+
end if;
179+
end Card_Detect;
180+
143181
begin
144-
if STM32.GPIO.Set (SD_Detect_Pin) then
182+
if not Card_Detect then
145183
-- No card
146184
This.Device.Clear_Card_Information;
147185
This.Card_Detected := False;
@@ -152,7 +190,21 @@ package body SDCard is
152190
delay until Clock + Milliseconds (50);
153191
end if;
154192

155-
This.Card_Detected := not STM32.GPIO.Set (SD_Detect_Pin);
193+
if Has_SD_Detect_Pin then
194+
This.Card_Detected := Card_Detect;
195+
196+
elsif not This.Card_Detected and Card_Detect then
197+
-- Reconfigure the SD_Detect_Pin pin to SDIO function
198+
Configure_IO
199+
(SD_Detect_Pin,
200+
(Mode => Mode_AF,
201+
AF => SD_Pins_AF,
202+
AF_Output_Type => Push_Pull,
203+
AF_Speed => Speed_High,
204+
Resistors => Pull_Up));
205+
206+
This.Card_Detected := True;
207+
end if;
156208
end if;
157209

158210
return This.Card_Detected;

boards/stm32_common/sdcard/sdcard.ads

+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ package SDCard is
5353
function Card_Present
5454
(This : in out SDCard_Controller) return Boolean;
5555
-- Whether a SD-Card is present in the sdcard reader
56+
--
57+
-- If there is no separate pin on the device for card detection, then a pin
58+
-- shared with the data transmission line is used. In this case, card
59+
-- detection cannot work simultaneously with other subprograms, and caution
60+
-- is required when using it. In this configuration, the card detection
61+
-- mode operates immediately after the Initialize function is called until
62+
-- the Card_Present function returns True. From that moment on, the
63+
-- controller switches to normal operation mode, and the Card_Present
64+
-- function will keep returning True until the next Initialize function
65+
-- call.
66+
67+
function Has_SD_Detect_Pin return Boolean;
68+
-- Whether the dedicated SD card detection pin is present on the device
5669

5770
function Get_Card_Information
5871
(This : in out SDCard_Controller)

0 commit comments

Comments
 (0)