mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-07-18 01:47:30 +02:00
style: enforce coding standard — headers, @brief on #define, Doxygen on statics, remove _ prefix, max 8 lines per function
This commit is contained in:
@@ -1,23 +1,70 @@
|
||||
/**
|
||||
* @file 0x0008_unitialized-variables-d.c
|
||||
* @brief Blink LED using pico_default_asm_volatile coprocessor instructions
|
||||
* @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.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Blinks an LED on GPIO16 using pico_default_asm_volatile to emit mcrr
|
||||
* coprocessor instructions for GPIO output enable and data, plus inline
|
||||
* assembly for pad configuration (hw_write_masked, IO_BANK0 FUNCSEL,
|
||||
* hw_clear_bits equivalents).
|
||||
*
|
||||
* Wiring:
|
||||
* GPIO16 -> LED anode (with current-limiting resistor to GND)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#define LED_PIN 16
|
||||
/** @brief GPIO pin number for the LED */
|
||||
#define LED_PIN 16
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// gpio_init(LED_PIN);
|
||||
/// gpio_set_dir(LED_PIN, GPIO_IN);
|
||||
//// gpioc_bit_oe_put(LED_PIN, GPIO_OUT);
|
||||
/**
|
||||
* @brief Enable output and set initial coprocessor OE/OUT via mcrr
|
||||
*
|
||||
* @details Issues two mcrr p0 instructions: one to enable output direction,
|
||||
* one to set the initial output state.
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void asm_set_oe_and_out(void) {
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c4" : : "r" (LED_PIN), "r" (GPIO_OUT));
|
||||
/// gpio_put(LED_PIN, 0);
|
||||
//// gpioc_bit_out_put(LED_PIN, 0);
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c4" : : "r" (LED_PIN), "r" (GPIO_OUT));
|
||||
/// gpio_set_function(LED_PIN, GPIO_FUNC_SIO);
|
||||
//// hw_write_masked(&pads_bank0_hw->io[LED_PIN],
|
||||
//// PADS_BANK0_GPIO0_IE_BITS,
|
||||
//// PADS_BANK0_GPIO0_IE_BITS | PADS_BANK0_GPIO0_OD_BITS
|
||||
//// );
|
||||
//// hw_xor_bits(addr, (*addr ^ values) & write_mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure PADS_BANK0 IE/OD bits via inline assembly hw_xor_bits
|
||||
*
|
||||
* @details Performs a read-modify-write on the pad register to set IE
|
||||
* and clear OD, replicating hw_write_masked behavior.
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void asm_configure_pad(void) {
|
||||
pico_default_asm_volatile (
|
||||
"ldr r2, [%0]\n" // load current pad register
|
||||
"eor r2, r2, %1\n" // xor with IE bit
|
||||
@@ -30,8 +77,17 @@ int main(void)
|
||||
"r" (PADS_BANK0_GPIO0_IE_BITS | PADS_BANK0_GPIO0_OD_BITS)
|
||||
: "r2", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
/// io_bank0_hw->io[LED_PIN].ctrl = GPIO_FUNC_SIO << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB;
|
||||
/**
|
||||
* @brief Set IO_BANK0 FUNCSEL to SIO via inline assembly store
|
||||
*
|
||||
* @details Writes the SIO function select value directly to the
|
||||
* IO_BANK0 GPIO control register.
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void asm_set_funcsel(void) {
|
||||
pico_default_asm_volatile (
|
||||
"str %1, [%0]\n"
|
||||
:
|
||||
@@ -39,32 +95,50 @@ int main(void)
|
||||
"r" (GPIO_FUNC_SIO << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB)
|
||||
: "memory"
|
||||
);
|
||||
/// hw_clear_bits(&pads_bank0_hw->io[LED_PIN], PADS_BANK0_GPIO0_ISO_BITS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear ISO bits in PADS_BANK0 via inline assembly
|
||||
*
|
||||
* @details Performs a read-modify-write to clear the pad isolation
|
||||
* bit, un-isolating the pad for normal operation.
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void asm_clear_iso(void) {
|
||||
pico_default_asm_volatile (
|
||||
"ldr r2, [%0]\n" // load current register value
|
||||
"bic r2, r2, %1\n" // clear the ISO bits (bit clear)
|
||||
"str r2, [%0]\n" // write back
|
||||
"str r2, [%0]\n" // write back
|
||||
:
|
||||
: "r" (&pads_bank0_hw->io[LED_PIN]),
|
||||
"r" (PADS_BANK0_GPIO0_ISO_BITS)
|
||||
: "r2", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
// gpio_set_dir(LED_PIN, GPIO_OUT);
|
||||
/// gpioc_bit_oe_put(LED_PIN, GPIO_OUT);
|
||||
/**
|
||||
* @brief Blink LED using coprocessor mcrr output instructions
|
||||
*
|
||||
* @details Toggles the LED on and off with 500ms delays using
|
||||
* pico_default_asm_volatile mcrr instructions.
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void asm_blink_cycle(void) {
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c0" : : "r" (LED_PIN), "r" (1));
|
||||
sleep_us(500 * 1000ull);
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c0" : : "r" (LED_PIN), "r" (0));
|
||||
sleep_us(500 * 1000ull);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
asm_set_oe_and_out();
|
||||
asm_configure_pad();
|
||||
asm_set_funcsel();
|
||||
asm_clear_iso();
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c4" : : "r" (LED_PIN), "r" (GPIO_OUT));
|
||||
|
||||
while (true) {
|
||||
// gpio_put(LED_PIN, 1);
|
||||
/// gpioc_bit_out_put(LED_PIN, 1);
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c0" : : "r" (LED_PIN), "r" (1));
|
||||
/// sleep_ms(500);
|
||||
sleep_us(500 * 1000ull);
|
||||
|
||||
// gpio_put(LED_PIN, 0);
|
||||
/// gpioc_bit_out_put(LED_PIN, 0);
|
||||
pico_default_asm_volatile ("mcrr p0, #4, %0, %1, c0" : : "r" (LED_PIN), "r" (0));
|
||||
/// sleep_ms(500);
|
||||
sleep_us(500 * 1000ull);
|
||||
asm_blink_cycle();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user