Files
Embedded-Hacking/drivers/0x0b_spi/spi.c
T
Kevin Thomas 3604b3c921 refactor: enforce max 8 code lines, add docstrings, fix warnings across all Rust and C SDK projects
Rust (all 15 projects):
- Refactored overlength functions: format_counter, format_u8, format_f32_1,
  format_u32_minimal, gpio_drive, read_sensor, poll_sensor, format_round_trip,
  format_u32, prepare_write_buf, write_min_digits, write_temp, UartDriver::init,
  init_spi, angle_to_pulse_us, compute_servo_level
- Added 200+ docstrings to test functions, mock structs, impl blocks
- Fixed pub static comments (//) to doc comments (///) in all main.rs files
- Fixed helper function ordering (helpers above callers)
- Fixed Fn(u32) -> FnMut(u32) bound in button poll_button
- Moved OneShot trait import from main.rs to board.rs in adc project
- Added unsafe {} blocks in flash unsafe fn bodies (Rust 2024 edition)
- Removed unused hal::Clock imports from pwm/servo main.rs
- All 15 projects build with zero errors and zero warnings

C Pico SDK (all 15 projects):
- Added docstrings to all public functions, macros, and static variables
- All 15 projects rebuilt with zero errors

Cleanup:
- Removed build/ and target/ directories from git tracking
- Added target/ to .gitignore
- Deleted temporary fix_rust_docs.py script
2026-04-06 08:33:17 -04:00

116 lines
3.9 KiB
C

/**
* @file spi.c
* @brief Implementation of SPI bus driver (master mode)
* @author Kevin Thomas
* @date 2025
*
* MIT License
*
* Copyright (c) 2025 Kevin Thomas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "spi.h"
#include "pico/stdlib.h"
#include "hardware/spi.h"
/**
* @brief Map an SPI port number to its hardware instance pointer
*
* @param port SPI port number (0 for spi0, 1 for spi1)
* @return spi_inst_t* Pointer to the corresponding SPI hardware instance
*/
static spi_inst_t *_get_spi_inst(uint8_t port) {
return port == 0 ? spi0 : spi1;
}
/**
* @brief Assign GPIO alternate functions for the SPI data and clock pins
*
* @param mosi GPIO pin for MOSI
* @param miso GPIO pin for MISO
* @param sck GPIO pin for SCK
*/
static void _setup_spi_pins(uint32_t mosi, uint32_t miso, uint32_t sck) {
gpio_set_function(mosi, GPIO_FUNC_SPI);
gpio_set_function(miso, GPIO_FUNC_SPI);
gpio_set_function(sck, GPIO_FUNC_SPI);
}
/**
* @brief Initialize an SPI peripheral in master mode
*
* Configures the SPI instance at the requested baud rate, assigns GPIO
* alternate functions for MOSI, MISO, and SCK, and configures the chip
* select pin as a GPIO output deasserted (high) by default.
*
* @param port SPI port number (0 for spi0, 1 for spi1)
* @param mosi GPIO pin number for MOSI (controller TX)
* @param miso GPIO pin number for MISO (controller RX)
* @param sck GPIO pin number for SCK (clock)
* @param cs GPIO pin number for chip select (active-low)
* @param baud_hz Desired SPI clock frequency in Hz (e.g. 1000000 for 1 MHz)
*/
void spi_driver_init(uint8_t port, uint32_t mosi, uint32_t miso,
uint32_t sck, uint32_t cs, uint32_t baud_hz) {
spi_inst_t *spi = _get_spi_inst(port);
spi_init(spi, baud_hz);
_setup_spi_pins(mosi, miso, sck);
gpio_init(cs);
gpio_set_dir(cs, GPIO_OUT);
gpio_put(cs, 1);
}
/**
* @brief Assert the chip-select line (drive CS low)
*
* @param cs GPIO pin number of the chip-select output
*/
void spi_driver_cs_select(uint32_t cs) {
gpio_put(cs, 0);
}
/**
* @brief Deassert the chip-select line (drive CS high)
*
* @param cs GPIO pin number of the chip-select output
*/
void spi_driver_cs_deselect(uint32_t cs) {
gpio_put(cs, 1);
}
/**
* @brief Perform a full-duplex SPI transfer
*
* Sends len bytes from tx while simultaneously receiving len bytes
* into rx. Both buffers must hold at least len bytes. The caller is
* responsible for asserting and deasserting CS around this call.
*
* @param port SPI port number (0 for spi0, 1 for spi1)
* @param tx Pointer to the transmit buffer (must be len bytes)
* @param rx Pointer to the receive buffer (must be len bytes)
* @param len Number of bytes to transfer
*/
void spi_driver_transfer(uint8_t port, const uint8_t *tx, uint8_t *rx,
uint32_t len) {
spi_inst_t *spi = _get_spi_inst(port);
spi_write_read_blocking(spi, tx, rx, (size_t)len);
}