# HG changeset patch # User Josef 'Jeff' Sipek # Date 1671407363 18000 # Sun Dec 18 18:49:23 2022 -0500 # Node ID dba62b57af5582c6e8673ac9bf5db2fae6d7ace1 # Parent 6e671caa7dfe93b180954539f1cc285ffd027c58 arch: move most of generic code into arch/generic_*.h Signed-off-by: Josef 'Jeff' Sipek diff --git a/arch.h b/arch.h --- a/arch.h +++ b/arch.h @@ -39,48 +39,9 @@ #define STATIC_ASSERT(c) _Static_assert(c, #c) #define mmregs ((volatile struct mmregs_layout * volatile) (void *) 0x0) - -/* - * MCU clock - */ -static inline void mcu_set_clock_prescalar(uint16_t scale) -{ - asm volatile( - "st Z, %1\n" - "st Z, %2\n" - : /* out */ - : /* in */ - "z" (&mmregs->clkpr), - "r" (REG_CLKPR_CLKPCE), - "r" (mcu_clock_prescalar_to_clkps(scale)) - ); -} +#endif -/* - * GPIO pin setting helper - */ -#define PORTB (offsetof(struct mmregs_layout, portb) - 0x20) -#define PORTC (offsetof(struct mmregs_layout, portc) - 0x20) -#define PORTD (offsetof(struct mmregs_layout, portd) - 0x20) - -static inline void __set_pin(uint8_t port, uint8_t pin, bool set) -{ - if (set) - asm volatile("sbi %0, %1" :: "I" (port), "I" (pin)); - else - asm volatile("cbi %0, %1" :: "I" (port), "I" (pin)); -} - -/* - * Macros to construct PORT and DDR register values - */ -#define PIN_INPUT_PORT_Z(bit) 0 -#define PIN_INPUT_PORT_H(bit) (1 << (bit)) -#define PIN_INPUT_DDR(bit) 0 -#define PIN_OUTPUT_PORT_H(bit) (1 << (bit)) -#define PIN_OUTPUT_PORT_L(bit) 0 -#define PIN_OUTPUT_DDR(bit) (1 << (bit)) +#include "arch/generic_mcu.h" +#include "arch/generic_gpio.h" #endif - -#endif diff --git a/arch/generic_gpio.h b/arch/generic_gpio.h new file mode 100644 --- /dev/null +++ b/arch/generic_gpio.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Josef 'Jeff' Sipek + * + * 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. + */ + +#ifndef AVR_COMMON_ARCH_GENERIC_GPIO_H +#define AVR_COMMON_ARCH_GENERIC_GPIO_H + +#ifndef _ASM + +/* + * GPIO pin setting helper + */ +#define PORTB (offsetof(struct mmregs_layout, portb) - 0x20) +#define PORTC (offsetof(struct mmregs_layout, portc) - 0x20) +#define PORTD (offsetof(struct mmregs_layout, portd) - 0x20) + +static inline void __set_pin(uint8_t port, uint8_t pin, bool set) +{ + if (set) + asm volatile("sbi %0, %1" :: "I" (port), "I" (pin)); + else + asm volatile("cbi %0, %1" :: "I" (port), "I" (pin)); +} + +/* + * Macros to construct PORT and DDR register values + */ +#define PIN_INPUT_PORT_Z(bit) 0 +#define PIN_INPUT_PORT_H(bit) (1 << (bit)) +#define PIN_INPUT_DDR(bit) 0 +#define PIN_OUTPUT_PORT_H(bit) (1 << (bit)) +#define PIN_OUTPUT_PORT_L(bit) 0 +#define PIN_OUTPUT_DDR(bit) (1 << (bit)) + +#endif + +#endif diff --git a/arch/generic_mcu.h b/arch/generic_mcu.h new file mode 100644 --- /dev/null +++ b/arch/generic_mcu.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Josef 'Jeff' Sipek + * + * 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. + */ + +#ifndef AVR_COMMON_ARCH_GENERIC_MCU_H +#define AVR_COMMON_ARCH_GENERIC_MCU_H + +#ifndef _ASM + +/* + * MCU clock + */ +static inline void mcu_set_clock_prescalar(uint16_t scale) +{ + asm volatile( + "st Z, %1\n" + "st Z, %2\n" + : /* out */ + : /* in */ + "z" (&mmregs->clkpr), + "r" (REG_CLKPR_CLKPCE), + "r" (mcu_clock_prescalar_to_clkps(scale)) + ); +} + +#endif + +#endif