From 3f0e1862a8b44ba58e89939610f1e90087ef20e7 Mon Sep 17 00:00:00 2001 From: Laurent Navet Date: Fri, 2 Sep 2016 22:26:19 +0200 Subject: [PATCH] cpu/atmega328p: add atmega328p mcu support add atmega328p support with: uart, timer, spi and gpio --- cpu/atmega328p/Makefile | 6 +++ cpu/atmega328p/Makefile.include | 11 +++++ cpu/atmega328p/cpu.c | 29 ++++++++++++ cpu/atmega328p/doc.txt | 10 ++++ cpu/atmega328p/include/cpu_conf.h | 51 ++++++++++++++++++++ cpu/atmega328p/include/periph_cpu.h | 49 ++++++++++++++++++++ cpu/atmega328p/lpm_arch.c | 54 ++++++++++++++++++++++ cpu/atmega328p/reboot_arch.c | 35 ++++++++++++++ cpu/atmega328p/startup.c | 72 +++++++++++++++++++++++++++++ 9 files changed, 317 insertions(+) create mode 100644 cpu/atmega328p/Makefile create mode 100644 cpu/atmega328p/Makefile.include create mode 100644 cpu/atmega328p/cpu.c create mode 100644 cpu/atmega328p/doc.txt create mode 100644 cpu/atmega328p/include/cpu_conf.h create mode 100644 cpu/atmega328p/include/periph_cpu.h create mode 100644 cpu/atmega328p/lpm_arch.c create mode 100644 cpu/atmega328p/reboot_arch.c create mode 100644 cpu/atmega328p/startup.c diff --git a/cpu/atmega328p/Makefile b/cpu/atmega328p/Makefile new file mode 100644 index 0000000000..81f110b68d --- /dev/null +++ b/cpu/atmega328p/Makefile @@ -0,0 +1,6 @@ +# define the module that is build +MODULE = cpu +# add a list of subdirectories, that should also be build +DIRS = $(ATMEGA_COMMON) + +include $(RIOTBASE)/Makefile.base diff --git a/cpu/atmega328p/Makefile.include b/cpu/atmega328p/Makefile.include new file mode 100644 index 0000000000..a952b7caef --- /dev/null +++ b/cpu/atmega328p/Makefile.include @@ -0,0 +1,11 @@ +# this CPU implementation is using the new core/CPU interface +export CFLAGS += -DCOREIF_NG=1 + +# tell the build system that the CPU depends on the atmega common files +USEMODULE += atmega_common + +# define path to atmega common module, which is needed for this CPU +export ATMEGA_COMMON = $(RIOTCPU)/atmega_common/ + +# CPU depends on the atmega common module, so include it +include $(ATMEGA_COMMON)Makefile.include diff --git a/cpu/atmega328p/cpu.c b/cpu/atmega328p/cpu.c new file mode 100644 index 0000000000..3de168ab4f --- /dev/null +++ b/cpu/atmega328p/cpu.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_atmega328p + * @{ + * + * @file + * @brief Implementation of the CPU initialization + * + * @author Hinnerk van Bruinehsen + * @} + */ + +#include "cpu.h" + +/** + * @brief Initialize the CPU, set IRQ priorities + */ +void cpu_init(void) +{ + /* Right now we need to do nothing here */ + ; +} diff --git a/cpu/atmega328p/doc.txt b/cpu/atmega328p/doc.txt new file mode 100644 index 0000000000..87c16cdbe4 --- /dev/null +++ b/cpu/atmega328p/doc.txt @@ -0,0 +1,10 @@ +/** + * @defgroup cpu_atmega328p Atmel ATmega328p + * @ingroup cpu + * @brief Implementation of Atmel's ATmega328p MCU + */ + +/** + * @defgroup cpu_atmega328p_definitions Atmel ATmega328p Definitions + * @ingroup cpu_atmega328p + */ diff --git a/cpu/atmega328p/include/cpu_conf.h b/cpu/atmega328p/include/cpu_conf.h new file mode 100644 index 0000000000..fd6b7a3374 --- /dev/null +++ b/cpu/atmega328p/include/cpu_conf.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_atmega328p + * @{ + * + * @file + * @brief Implementation specific CPU configuration options + * + * @author Hauke Petersen + * @author Hinnerk van Bruinehsen + */ + +#ifndef CPU_CONF_H +#define CPU_CONF_H + +#include "atmega_regs_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Kernel configuration + * + * Since printf seems to get memory allocated by the linker/avr-libc the stack + * size tested sucessfully even with pretty small stacks.k + * @{ + */ +#define THREAD_EXTRA_STACKSIZE_PRINTF (128) + +#ifndef THREAD_STACKSIZE_DEFAULT +#define THREAD_STACKSIZE_DEFAULT (256) +#endif + +#define THREAD_STACKSIZE_IDLE (128) +#define ISR_STACKSIZE (0) +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* CPU_CONF_H */ +/** @} */ diff --git a/cpu/atmega328p/include/periph_cpu.h b/cpu/atmega328p/include/periph_cpu.h new file mode 100644 index 0000000000..e7f67a2af9 --- /dev/null +++ b/cpu/atmega328p/include/periph_cpu.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2015 HAW Hamburg + * 2016 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_atmega328p + * @{ + * + * @file + * @brief CPU specific definitions for internal peripheral handling + * + * @author René Herthel + * @author Hauke Petersen + */ + +#ifndef PERIPH_CPU_H_ +#define PERIPH_CPU_H_ + +#include "periph_cpu_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define a CPU specific GPIO pin generator macro + */ +#define GPIO_PIN(x, y) ((x << 4) | y) + +/** + * @brief Available ports on the ATmega328p family + */ +enum { + PORT_B = 1, /**< port B */ + PORT_C = 2, /**< port C */ + PORT_D = 3 /**< port D */ +}; + +#ifdef __cplusplus +} +#endif + +#endif /* PERIPH_CPU_H_ */ +/** @} */ diff --git a/cpu/atmega328p/lpm_arch.c b/cpu/atmega328p/lpm_arch.c new file mode 100644 index 0000000000..8c8ed7b990 --- /dev/null +++ b/cpu/atmega328p/lpm_arch.c @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_atmega328p + * @{ + * + * @file + * @brief Implementation of the kernels power management interface + * + * @author Hinnerk van Bruinehsen + * + * @} + */ + +#include "arch/lpm_arch.h" + +void lpm_arch_init(void) +{ + /* TODO */ +} + +enum lpm_mode lpm_arch_set(enum lpm_mode target) +{ + (void) target; + /* TODO */ + return 0; +} + +enum lpm_mode lpm_arch_get(void) +{ + /* TODO */ + return 0; +} + +void lpm_arch_awake(void) +{ + /* TODO */ +} + +void lpm_arch_begin_awake(void) +{ + /* TODO */ +} + +void lpm_arch_end_awake(void) +{ + /* TODO */ +} diff --git a/cpu/atmega328p/reboot_arch.c b/cpu/atmega328p/reboot_arch.c new file mode 100644 index 0000000000..802e592632 --- /dev/null +++ b/cpu/atmega328p/reboot_arch.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 Kaspar Schleiser + * 2014 Freie Universität Berlin, Hinnerk van Bruinehsen + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_atmega328p + * @{ + * + * @file + * @brief Implementation of the kernels reboot interface + * + * @author Hinnerk van Bruinehsen + * @author Kaspar Schleiser + * + * @} + */ + +#include + +#include "cpu.h" + +void reboot(void) +{ + /* + * Since the AVR doesn't support a real software reset, we set the Watchdog + * Timer on a 250ms timeout. Consider this a kludge. + */ + wdt_enable(WDTO_250MS); + while(1); +} diff --git a/cpu/atmega328p/startup.c b/cpu/atmega328p/startup.c new file mode 100644 index 0000000000..4601dcc430 --- /dev/null +++ b/cpu/atmega328p/startup.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_atmega328p + * @{ + * + * @file + * @brief Startup code and interrupt vector definition + * + * @author Hinnerk van Bruinehsen + * + * @} + */ + +#include +#include +#include + +/* For Catchall-Loop */ +#include "board.h" + + +/** + * @brief functions for initializing the board, std-lib and kernel + */ +extern void board_init(void); +extern void kernel_init(void); +extern void __libc_init_array(void); + +/** + * @brief This pair of functions hook circumvent the call to main + * + * avr-libc normally uses the .init9 section for a call to main. This call + * seems to be not replaceable without hacking inside the library. We + * circumvent the call to main by using section .init7 to call the function + * reset_handler which therefore is the real entry point and section .init8 + * which should never be reached but just in case jumps to exit. + * This way there should be no way to call main directly. + */ +void init7_ovr(void) __attribute__((naked)) __attribute__((section(".init7"))); +void init8_ovr(void) __attribute__((naked)) __attribute__((section(".init8"))); + + +void init7_ovr(void) +{ + __asm__("call reset_handler"); +} + +void init8_ovr(void) +{ + __asm__("jmp exit"); +} +/** + * @brief This function is the entry point after a system reset + * + * After a system reset, the following steps are necessary and carried out: + * 1. initialize the board (sync clock, setup std-IO) + * 2. initialize and start RIOTs kernel + */ +void reset_handler(void) +{ + /* initialize the board and startup the kernel */ + board_init(); + /* startup the kernel */ + kernel_init(); +}