mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-07-16 00:47:26 +02:00
e54c756423
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
132 lines
3.6 KiB
C
132 lines
3.6 KiB
C
/**
|
|
******************************************************************************
|
|
* @file main.c
|
|
* @author Kevin Thomas
|
|
* @brief SPI loopback demonstration.
|
|
*
|
|
* Performs a full-duplex SPI0 transfer in master mode with
|
|
* MOSI wired to MISO for loopback verification. Prints TX
|
|
* and RX data over UART every second.
|
|
*
|
|
* Wiring (loopback test):
|
|
* GPIO0 -> UART TX (USB-to-UART adapter RX)
|
|
* GPIO1 -> UART RX (USB-to-UART adapter TX)
|
|
* GPIO19 (MOSI) -> GPIO16 (MISO)
|
|
* GPIO18 (SCK) -> logic analyzer (optional)
|
|
* GPIO17 (CS) -> active-low slave (optional)
|
|
*
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2026 Kevin Thomas.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
#include "rp2350_spi.h"
|
|
#include "rp2350_uart.h"
|
|
#include "rp2350_delay.h"
|
|
#include "rp2350_xosc.h"
|
|
|
|
/**
|
|
* @brief Hex digit lookup table for byte-to-hex conversion.
|
|
*/
|
|
static const char _hex_lut[16] = "0123456789ABCDEF";
|
|
|
|
/**
|
|
* @brief Print a byte as a two-digit hex string over UART.
|
|
* @param value byte to print
|
|
* @retval None
|
|
*/
|
|
static void _print_hex(uint8_t value)
|
|
{
|
|
char buf[3];
|
|
buf[0] = _hex_lut[value >> 4];
|
|
buf[1] = _hex_lut[value & 0x0FU];
|
|
buf[2] = '\0';
|
|
uart_puts(buf);
|
|
}
|
|
|
|
/**
|
|
* @brief Print a buffer as hex bytes separated by spaces over UART.
|
|
* @param label text label to print before the data
|
|
* @param buf byte buffer to print
|
|
* @param len number of bytes in buffer
|
|
* @retval None
|
|
*/
|
|
static void _print_buffer(const char *label, const uint8_t *buf, uint32_t len)
|
|
{
|
|
uart_puts(label);
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
_print_hex(buf[i]);
|
|
if (i + 1 < len)
|
|
uart_putchar(' ');
|
|
}
|
|
uart_puts("\r\n");
|
|
}
|
|
|
|
/**
|
|
* @brief Perform one SPI loopback transfer and print TX/RX over UART.
|
|
* @param tx_buf transmit buffer
|
|
* @param rx_buf receive buffer (cleared after printing)
|
|
* @param len number of bytes to transfer
|
|
* @retval None
|
|
*/
|
|
static void _loopback_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t len)
|
|
{
|
|
spi_cs_select();
|
|
spi_transfer(tx_buf, rx_buf, len);
|
|
spi_cs_deselect();
|
|
_print_buffer("TX: ", tx_buf, len);
|
|
_print_buffer("RX: ", rx_buf, len);
|
|
uart_puts("\r\n");
|
|
}
|
|
|
|
/**
|
|
* @brief Clear a byte buffer to zero.
|
|
* @param buf pointer to buffer
|
|
* @param len number of bytes to clear
|
|
* @retval None
|
|
*/
|
|
static void _clear_buffer(uint8_t *buf, uint32_t len)
|
|
{
|
|
for (uint32_t i = 0; i < len; i++)
|
|
buf[i] = 0;
|
|
}
|
|
|
|
/**
|
|
* @brief Initialize clocks, SPI peripheral, and announce over UART.
|
|
* @retval None
|
|
*/
|
|
static void _spi_setup(void)
|
|
{
|
|
xosc_set_clk_ref();
|
|
spi_release_reset();
|
|
spi_init();
|
|
uart_puts("SPI loopback initialized (MOSI->MISO on GPIO19->GPIO16)\r\n");
|
|
}
|
|
|
|
/**
|
|
* @brief Application entry point for the SPI loopback demo.
|
|
* @retval int does not return
|
|
*/
|
|
int main(void)
|
|
{
|
|
/** @brief Transmit test pattern for SPI loopback. */
|
|
static const uint8_t tx[] = {0xDE, 0xAD, 0xBE, 0xEF};
|
|
/** @brief Buffer length for SPI loopback transfer. */
|
|
static const uint32_t len = sizeof(tx);
|
|
uint8_t rx[sizeof(tx)] = {0};
|
|
_spi_setup();
|
|
while (1) {
|
|
_loopback_transfer(tx, rx, len);
|
|
_clear_buffer(rx, len);
|
|
delay_ms(1000);
|
|
}
|
|
}
|