Skip to content

Commit

Permalink
Add SD card examples for stm32f4xx_m.
Browse files Browse the repository at this point in the history
  • Loading branch information
reznikmm committed Dec 6, 2023
1 parent dfbc15d commit f79cd47
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/stm32f4xx_m/sdcard_fs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SD card filesystem example

Turns on the LED and waits for the SD card. Then, it mounts the first FAT
partition as '/card/', opens the `/file.txt` file and reads 512 bytes from
it. If everything is okay, the program blinks the green LED slowly
at a rate of 1 Hz. Otherwise, it blinks the LED faster at 5 Hz.
20 changes: 20 additions & 0 deletions examples/stm32f4xx_m/sdcard_fs/sdcard_fs.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
with "../../../boards/stm32f4xx_m/stm32f4xx_m_full.gpr";

project SDCard_FS is

for Runtime ("Ada") use STM32F4XX_M_Full'Runtime ("Ada");
for Target use "arm-eabi";
for Main use ("main.adb");
for Languages use ("Ada");
for Source_Dirs use ("src");
for Object_Dir use "obj/";
for Create_Missing_Dirs use "True";

package Compiler renames STM32F4XX_M_Full.Compiler;

package Ide is
for Program_Host use "localhost:4242";
for Communication_Protocol use "remote";
for Connection_Tool use "st-util";
end Ide;
end SDCard_FS;
97 changes: 97 additions & 0 deletions examples/stm32f4xx_m/sdcard_fs/src/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
------------------------------------------------------------------------------
-- --
-- Copyright (C) 2023, AdaCore --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
-- met: --
-- 1. Redistributions of source code must retain the above copyright --
-- notice, this list of conditions and the following disclaimer. --
-- 2. Redistributions in binary form must reproduce the above copyright --
-- notice, this list of conditions and the following disclaimer in --
-- the documentation and/or other materials provided with the --
-- distribution. --
-- 3. Neither the name of the copyright holder nor the names of its --
-- contributors may be used to endorse or promote products derived --
-- from this software without specific prior written permission. --
-- --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
-- --
------------------------------------------------------------------------------

-- Turns on the LED and waits for the SD card. Then, it mounts the first FAT
-- partition as '/card/', opens the `/file.txt` file and reads 512 bytes from
-- it. If everything is okay, the program blinks the green LED slowly
-- at a rate of 1 Hz. Otherwise, it blinks the LED faster at 5 Hz.

pragma Ada_2022;

with Ada.Real_Time;

with File_IO;
with HAL;
with SDCard;
with STM32.Board;

procedure Main is
use type Ada.Real_Time.Time;
use all type File_IO.Status_Code;

Next : Ada.Real_Time.Time := Ada.Real_Time.Clock;

SD_Card renames STM32.Board.SDCard_Device;
-- Typeless renaming works since Ada 2022

Success : Boolean;
begin
STM32.Board.Initialize_LEDs;
SD_Card.Initialize;
STM32.Board.Green_LED.Set;

-- Wait for card
while not SD_Card.Card_Present loop
Next := Next + Ada.Real_Time.Milliseconds (200);
delay until Next;
end loop;

Success := File_IO.Mount_Drive ("card", SD_Card'Unchecked_Access) = OK;
-- Mounting the SD card as '/card/'

declare
use type File_IO.File_Size;

FD : File_IO.File_Descriptor;
Data : HAL.UInt8_Array (1 .. 512);
Last : File_IO.File_Size;
begin
if Success then
Success := File_IO.Open
(FD, "/card/file.txt", File_IO.Read_Only) = OK;
end if;

if Success then
Last := File_IO.Read (FD, Data'Address, Data'Length);

Success := Last = Data'Length;
end if;

loop
STM32.Board.Toggle (STM32.Board.Green_LED);

Next := Next + Ada.Real_Time.Milliseconds
(if Success then 500 else 100);

delay until Next;
end loop;
end;
end Main;
6 changes: 6 additions & 0 deletions examples/stm32f4xx_m/sdcard_raw_io/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SD card read example

Turns on the LED and waits for the SD card. Then, it reads the first 512
bytes from the SD card and checks for the 0x55 0xAA pattern at the end of
the block. If everything is okay, the program blinks the green LED slowly
at a rate of 1 Hz. Otherwise, it blinks the LED faster at 5 Hz.
20 changes: 20 additions & 0 deletions examples/stm32f4xx_m/sdcard_raw_io/sdcard_raw_io.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
with "../../../boards/stm32f4xx_m/stm32f4xx_m_full.gpr";

project SDCard_Raw_IO is

for Runtime ("Ada") use STM32F4XX_M_Full'Runtime ("Ada");
for Target use "arm-eabi";
for Main use ("main.adb");
for Languages use ("Ada");
for Source_Dirs use ("src");
for Object_Dir use "obj/";
for Create_Missing_Dirs use "True";

package Compiler renames STM32F4XX_M_Full.Compiler;

package Ide is
for Program_Host use "localhost:4242";
for Communication_Protocol use "remote";
for Connection_Tool use "st-util";
end Ide;
end SDCard_Raw_IO;
82 changes: 82 additions & 0 deletions examples/stm32f4xx_m/sdcard_raw_io/src/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
------------------------------------------------------------------------------
-- --
-- Copyright (C) 2023, AdaCore --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
-- met: --
-- 1. Redistributions of source code must retain the above copyright --
-- notice, this list of conditions and the following disclaimer. --
-- 2. Redistributions in binary form must reproduce the above copyright --
-- notice, this list of conditions and the following disclaimer in --
-- the documentation and/or other materials provided with the --
-- distribution. --
-- 3. Neither the name of the copyright holder nor the names of its --
-- contributors may be used to endorse or promote products derived --
-- from this software without specific prior written permission. --
-- --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
-- --
------------------------------------------------------------------------------

-- Turns on the LED and waits for the SD card. Then, it reads the first 512
-- bytes from the SD card and checks for the 0x55 0xAA pattern at the end of
-- the block. If everything is okay, the program blinks the green LED slowly
-- at a rate of 1 Hz. Otherwise, it blinks the LED faster at 5 Hz.

pragma Ada_2022;

with Ada.Real_Time;

with HAL;
with STM32.Board;
with SDCard;

procedure Main is
use type Ada.Real_Time.Time;
Next : Ada.Real_Time.Time := Ada.Real_Time.Clock;

SD_Card renames STM32.Board.SDCard_Device;
-- Typeless renaming works since Ada 2022
begin
STM32.Board.Initialize_LEDs;
SD_Card.Initialize;
STM32.Board.Green_LED.Set;

-- Wait for card
while not SD_Card.Card_Present loop
Next := Next + Ada.Real_Time.Milliseconds (200);
delay until Next;
end loop;

declare
use type HAL.UInt8_Array;

Data : HAL.UInt8_Array (1 .. 512);
Ok : Boolean := SD_Card.Read (Block_Number => 0, Data => Data);
begin
if Ok then
-- Check for 0x55 0xAA pattern at the end of MBR (Master Boot Record)
Ok := Data (511 .. 512) = [16#55#, 16#AA#];
end if;

loop
STM32.Board.Toggle (STM32.Board.Green_LED);

Next := Next + Ada.Real_Time.Milliseconds
(if Ok then 500 else 100);

delay until Next;
end loop;
end;
end Main;
2 changes: 2 additions & 0 deletions scripts/build_all_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def gprbuild(project_file, debug=False):
"/boards/stm32f4xx_m/stm32f4xx_m_sfp.gpr",
"/examples/stm32f4xx_m/blinky/blinky.gpr",
"/examples/stm32f4xx_m/flash/flash.gpr",
"/examples/stm32f4xx_m/sdcard_fs/sdcard_fs.gpr",
"/examples/stm32f4xx_m/sdcard_raw_io/sdcard_raw_io.gpr",

# Feather STM32F405
"/examples/feather_stm32f405/blinky/blinky.gpr",
Expand Down

0 comments on commit f79cd47

Please sign in to comment.