sylveos

Toy Operating System
Log | Files | Refs

commit 5489ba5ae05dc12e6c7056b73c35d8e3050a2d93
parent 93f89d83bb17f5f5dbdb2f184f4c60d0861c88fd
Author: Sylvia Ivory <git@sivory.net>
Date:   Sun,  8 Mar 2026 20:58:10 -0700

Add SD card module

Diffstat:
Api/devices/sd.zig | 37+++++++++++++++++++++++++++++++++++++
Mpi/root.zig | 1+
2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/pi/devices/sd.zig b/pi/devices/sd.zig @@ -0,0 +1,37 @@ +// Temporarily use external C code until I feel like writing my own +const std = @import("std"); +const pi_sd = @cImport({ + @cInclude("pi-sd.h"); +}); + +const Self = @This(); + +alloc: std.mem.Allocator, + +pub fn init(alloc: std.mem.Allocator) Self { + return .{ .alloc = alloc }; +} + +pub fn read_sector(self: *const Self, sector: usize) !u4096 { + _ = self; + + var buffer: [512]u8 = .{0} ** 512; + if (pi_sd.pi_sd_read(&buffer, sector, 1) == -1) { + return error.InvalidRead; + } + + return std.mem.readInt(u4096, buffer[0..512], .little); +} + +pub fn read_sectors(self: *const Self, sector: usize, count: usize) ![]u4096 { + var buffer = try self.alloc.alloc(u8, count * 512); + if (pi_sd.pi_sd_read(&buffer, sector, 1) == -1) { + return error.InvalidRead; + } + + return @ptrCast(@alignCast(buffer)); +} + +pub fn free_sectors(self: *const Self, sectors: []u4096) void { + self.alloc.free(sectors); +} diff --git a/pi/root.zig b/pi/root.zig @@ -23,6 +23,7 @@ pub const devices = struct { pub const sw_uart = @import("./devices/sw-uart.zig"); pub const timer = @import("./devices/timer.zig"); pub const mailbox = @import("./devices/mailbox.zig"); + pub const sd = @import("./devices/sd.zig"); }; pub export const STACK_ADDRESS: usize = 0x8000000;