# HG changeset patch # User Josef 'Jeff' Sipek # Date 1672871399 18000 # Wed Jan 04 17:29:59 2023 -0500 # Node ID 37baf263bc306d994f92d9a69e204766b6dde26b # Parent 5743090c30aa658286dfea4950194bf2eeeab1b2 # Parent 6bc701fb517f214de16312e7ba718e4b64f9566c Merge avr-common Signed-off-by: Josef 'Jeff' Sipek diff --git a/arch.h b/arch.h --- a/arch.h +++ b/arch.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Josef 'Jeff' Sipek + * Copyright (c) 2022-2023 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 @@ -38,7 +38,12 @@ #ifndef _ASM #define STATIC_ASSERT(c) _Static_assert(c, #c) +#define ARRAY_LEN(x) (sizeof(x) / sizeof(x[0])) + #define mmregs ((volatile struct mmregs_layout * volatile) (void *) 0x0) #endif +#include "arch/generic_mcu.h" +#include "arch/generic_gpio.h" + #endif diff --git a/arch/atmega48p.h b/arch/atmega48p.h --- a/arch/atmega48p.h +++ b/arch/atmega48p.h @@ -23,6 +23,11 @@ #ifndef AVR_COMMON_ARCH_ATMEGA48P_H #define AVR_COMMON_ARCH_ATMEGA48P_H +/* the built-in oscillator runs at 8MHz */ +#ifndef MCU_RAW_FREQ +#define MCU_RAW_FREQ 8000000 +#endif + #define RAM_START_ADDR 0x100 #define RAM_END_ADDR 0x2ff 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 diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Josef 'Jeff' Sipek + * Copyright (c) 2022-2023 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 @@ -128,14 +128,6 @@ * GPIO */ -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)); -} - #define set_ptt(on) __set_pin(PORTC, 1, (on)); /*