mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-25 16:57:56 +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
113 lines
3.8 KiB
C
113 lines
3.8 KiB
C
/**
|
|
* @file i2c.c
|
|
* @brief Implementation of the I2C bus driver
|
|
* @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 "i2c.h"
|
|
#include <stdio.h>
|
|
#include "pico/stdlib.h"
|
|
#include "hardware/i2c.h"
|
|
#include "hardware/gpio.h"
|
|
|
|
/**
|
|
* @brief Map an I2C port number to its hardware instance pointer
|
|
*
|
|
* @param port I2C port number (0 for i2c0, 1 for i2c1)
|
|
* @return i2c_inst_t* Pointer to the corresponding I2C hardware instance
|
|
*/
|
|
static i2c_inst_t *_get_i2c_inst(uint8_t port) {
|
|
return port == 0 ? i2c0 : i2c1;
|
|
}
|
|
|
|
/**
|
|
* @brief Initialize an I2C peripheral at the requested baud rate
|
|
*
|
|
* @param port I2C port number (0 for i2c0, 1 for i2c1)
|
|
* @param sda_pin GPIO pin number for SDA
|
|
* @param scl_pin GPIO pin number for SCL
|
|
* @param baud_hz Bus clock frequency in Hz
|
|
*/
|
|
void i2c_driver_init(uint8_t port, uint32_t sda_pin, uint32_t scl_pin,
|
|
uint32_t baud_hz) {
|
|
i2c_inst_t *inst = _get_i2c_inst(port);
|
|
i2c_init(inst, baud_hz);
|
|
gpio_set_function(sda_pin, GPIO_FUNC_I2C);
|
|
gpio_set_function(scl_pin, GPIO_FUNC_I2C);
|
|
gpio_pull_up(sda_pin);
|
|
gpio_pull_up(scl_pin);
|
|
}
|
|
|
|
/**
|
|
* @brief Probe a 7-bit I2C address and return whether a device responds
|
|
*
|
|
* @param port I2C port number (0 for i2c0, 1 for i2c1)
|
|
* @param addr 7-bit I2C address to probe
|
|
* @return bool true if a device acknowledged, false otherwise
|
|
*/
|
|
bool i2c_driver_probe(uint8_t port, uint8_t addr) {
|
|
i2c_inst_t *inst = _get_i2c_inst(port);
|
|
uint8_t dummy;
|
|
return i2c_read_blocking(inst, addr, &dummy, 1, false) >= 0;
|
|
}
|
|
|
|
/**
|
|
* @brief Print the I2C scan table header over UART
|
|
*/
|
|
static void _print_scan_header(void) {
|
|
printf("\r\nI2C bus scan:\r\n");
|
|
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\r\n");
|
|
}
|
|
|
|
/**
|
|
* @brief Print one cell of the scan table for a given address
|
|
*
|
|
* Prints the row label when the address is at a 16-byte boundary, then
|
|
* prints the address if a device responds, dashes if not, or blank if
|
|
* the address is in the reserved range. Ends the row at every 16th address.
|
|
*
|
|
* @param port I2C port number (0 for i2c0, 1 for i2c1)
|
|
* @param addr 7-bit I2C address being probed
|
|
*/
|
|
static void _print_scan_entry(uint8_t port, uint8_t addr) {
|
|
if (addr % 16 == 0) printf("%02X: ", addr);
|
|
if (addr < 0x08 || addr > 0x77) printf(" ");
|
|
else if (i2c_driver_probe(port, addr)) printf("%02X ", addr);
|
|
else printf("-- ");
|
|
if (addr % 16 == 15) printf("\r\n");
|
|
}
|
|
|
|
/**
|
|
* @brief Scan all valid 7-bit addresses and print a formatted table
|
|
*
|
|
* @param port I2C port number (0 for i2c0, 1 for i2c1)
|
|
*/
|
|
void i2c_driver_scan(uint8_t port) {
|
|
_print_scan_header();
|
|
for (uint8_t addr = 0; addr < 128; addr++)
|
|
_print_scan_entry(port, addr);
|
|
}
|