Merge avr-common

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
5 files changed, 113 insertions(+), 10 deletions(-)

M arch.h
M arch/atmega48p.h
A => arch/generic_gpio.h
A => arch/generic_mcu.h
M main.c
M arch.h +6 -1
@@ 1,5 1,5 @@ 
 /*
- * Copyright (c) 2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2022-2023 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * 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

          
M arch/atmega48p.h +5 -0
@@ 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
 

          
A => arch/generic_gpio.h +55 -0
@@ 0,0 1,55 @@ 
+/*
+ * Copyright (c) 2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * 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

          
A => arch/generic_mcu.h +46 -0
@@ 0,0 1,46 @@ 
+/*
+ * Copyright (c) 2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * 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

          
M main.c +1 -9
@@ 1,5 1,5 @@ 
 /*
- * Copyright (c) 2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2022-2023 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * 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 @@ static uint16_t eeprom_read16(uint16_t a
  * 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));
 
 /*