From b63121c5888d6ae70483564f31587b31d99f11af Mon Sep 17 00:00:00 2001 From: Thomas Perrot Date: Mon, 3 Jul 2017 19:23:28 +0200 Subject: [PATCH] board: add support for arduino-leonardo --- boards/arduino-leonardo/Makefile | 5 ++ boards/arduino-leonardo/Makefile.features | 3 + boards/arduino-leonardo/Makefile.include | 17 +++++ boards/arduino-leonardo/board.c | 42 +++++++++++ boards/arduino-leonardo/doc.txt | 62 ++++++++++++++++ boards/arduino-leonardo/include/board.h | 35 +++++++++ boards/arduino-leonardo/led_init.c | 34 +++++++++ .../arduino-atmega/include/arduino_board.h | 10 ++- .../arduino-atmega/include/arduino_pinmap.h | 47 ++++++++++++ .../arduino-atmega/include/board_common.h | 29 +++++++- boards/common/atmega/board.c | 4 + .../include/periph_conf_atmega_common.h | 27 +++++-- cpu/atmega32u4/Makefile.include | 4 - cpu/atmega32u4/startup.c | 73 ------------------- .../vagrant/udev_rules/99-atmega32u4.rules | 1 + examples/default/Makefile | 1 - makefiles/tools/avrdude.inc.mk | 2 +- sys/pipe/pipe_dynamic.c | 2 +- 18 files changed, 306 insertions(+), 92 deletions(-) create mode 100644 boards/arduino-leonardo/Makefile create mode 100644 boards/arduino-leonardo/Makefile.features create mode 100644 boards/arduino-leonardo/Makefile.include create mode 100644 boards/arduino-leonardo/board.c create mode 100644 boards/arduino-leonardo/doc.txt create mode 100644 boards/arduino-leonardo/include/board.h create mode 100644 boards/arduino-leonardo/led_init.c delete mode 100644 cpu/atmega32u4/startup.c create mode 100644 dist/tools/vagrant/udev_rules/99-atmega32u4.rules diff --git a/boards/arduino-leonardo/Makefile b/boards/arduino-leonardo/Makefile new file mode 100644 index 0000000000..8c6f44eb53 --- /dev/null +++ b/boards/arduino-leonardo/Makefile @@ -0,0 +1,5 @@ +MODULE = board + +DIRS = $(RIOTBOARD)/common/arduino-atmega + +include $(RIOTBASE)/Makefile.base diff --git a/boards/arduino-leonardo/Makefile.features b/boards/arduino-leonardo/Makefile.features new file mode 100644 index 0000000000..00b2d135b8 --- /dev/null +++ b/boards/arduino-leonardo/Makefile.features @@ -0,0 +1,3 @@ +include $(RIOTBOARD)/common/arduino-atmega/Makefile.features + +include $(RIOTCPU)/atmega32u4/Makefile.features diff --git a/boards/arduino-leonardo/Makefile.include b/boards/arduino-leonardo/Makefile.include new file mode 100644 index 0000000000..96cfb4faaa --- /dev/null +++ b/boards/arduino-leonardo/Makefile.include @@ -0,0 +1,17 @@ +# define the cpu used by the arduino uno board +export CPU = atmega32u4 + +# export needed for flash rule +PORT_LINUX ?= /dev/ttyUSB0 +AVRDUDE_PORT ?= /dev/ttyACM0 +PORT_DARWIN ?= $(firstword $(sort $(wildcard /dev/tty.usbmodem*))) +BAUD ?= 9600 + +# PROGRAMMER defaults to avr109 which is the internal flasher via USB. Can be +# overridden for debugging (which requires changes that require to use an ISP) +PROGRAMMER ?= avr109 + +BOOTLOADER_SIZE ?= 4K +ROM_RESERVED ?= $(BOOTLOADER_SIZE) + +include $(RIOTBOARD)/common/arduino-atmega/Makefile.include diff --git a/boards/arduino-leonardo/board.c b/boards/arduino-leonardo/board.c new file mode 100644 index 0000000000..32403ecaf2 --- /dev/null +++ b/boards/arduino-leonardo/board.c @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2017 Thomas Perrot + * + * 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 boards_arduino-leonardo + * @{ + * + * @file + * @brief Board specific initialization for Arduino Leonardo + * + * @author Thomas Perrot + * + * @} + */ + +#include "board.h" +#include "cpu.h" +#include "irq.h" +#include "periph/gpio.h" + +#ifndef CPU_ATMEGA_CLK_SCALE_INIT +#define CPU_ATMEGA_CLK_SCALE_INIT CPU_ATMEGA_CLK_SCALE_DIV1 +#endif + +void led_init(void); + +void board_init(void) +{ + /* disable usb interrupt */ + PRR1 |= 1< + * + * 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. + */ + +/** + * @defgroup boards_arduino-leonardo Arduino Leonardo + * @ingroup boards + * @brief Support for the Arduino Leonardo board + * @{ + * + * @file + * @brief Board specific definitions for the Arduino Leonardo board + * + * @author Thomas Perrot + */ + +#ifndef BOARD_H +#define BOARD_H + +#include "board_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* BOARD_H */ +/** @} */ diff --git a/boards/arduino-leonardo/led_init.c b/boards/arduino-leonardo/led_init.c new file mode 100644 index 0000000000..07c764b4d6 --- /dev/null +++ b/boards/arduino-leonardo/led_init.c @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017 Thomas Perrot + * + * 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 boards_arduino-leonardo + * @{ + * + * @file + * @brief Board specific led initialization for Arduino Leonardo + * + * @author Thomas Perrot + * + * @} + */ + +#include "board.h" +#include "cpu.h" +#include "periph/gpio.h" + +void led_init(void) +{ + /* initialize the on-board LEDs */ + gpio_init(LED0_PIN, GPIO_OUT); + LED0_OFF; + gpio_init(LED1_PIN, GPIO_OUT); + LED2_OFF; + gpio_init(LED2_PIN, GPIO_OUT); + LED2_OFF; +} diff --git a/boards/common/arduino-atmega/include/arduino_board.h b/boards/common/arduino-atmega/include/arduino_board.h index a9397676fe..25b3113250 100644 --- a/boards/common/arduino-atmega/include/arduino_board.h +++ b/boards/common/arduino-atmega/include/arduino_board.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2016 Freie Universität Berlin + * 2017 Thomas Perrot * * 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 @@ -15,6 +16,7 @@ * * @author Hauke Petersen * @author Laurent Navet + * @author Thomas Perrot */ #ifndef ARDUINO_BOARD_H @@ -55,18 +57,24 @@ static const gpio_t arduino_pinmap[] = { ARDUINO_PIN_17, ARDUINO_PIN_18, ARDUINO_PIN_19, -#ifdef CPU_ATMEGA2560 +#if defined(CPU_ATMEGA2560) || defined(CPU_ATMEGA32U4) ARDUINO_PIN_20, ARDUINO_PIN_21, ARDUINO_PIN_22, ARDUINO_PIN_23, +#endif +#ifdef CPU_ATMEGA2560 ARDUINO_PIN_24, ARDUINO_PIN_25, ARDUINO_PIN_26, ARDUINO_PIN_27, ARDUINO_PIN_28, ARDUINO_PIN_29, +#endif +#if defined(CPU_ATMEGA2560) || defined(CPU_ATMEGA32U4) ARDUINO_PIN_30, +#endif +#ifdef CPU_ATMEGA2560 ARDUINO_PIN_31, ARDUINO_PIN_32, ARDUINO_PIN_33, diff --git a/boards/common/arduino-atmega/include/arduino_pinmap.h b/boards/common/arduino-atmega/include/arduino_pinmap.h index 00b7bf0ab7..b68ac6c5cb 100644 --- a/boards/common/arduino-atmega/include/arduino_pinmap.h +++ b/boards/common/arduino-atmega/include/arduino_pinmap.h @@ -1,6 +1,7 @@ /* * Copyright (C) 2015 Freie Universität Berlin * 2016 Laurent Navet + * 2017 Thomas Perrot * * 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 @@ -20,6 +21,7 @@ * @author Hauke Petersen * @author Daniel Nordahl * @author Laurent Navet + * @author Thomas Perrot */ #ifndef ARDUINO_PINMAP_H @@ -71,6 +73,51 @@ extern "C" { #define ARDUINO_PIN_A5 ARDUINO_PIN_19 #endif +#ifdef CPU_ATMEGA32U4 +/* Digital pins */ +#define ARDUINO_PIN_0 GPIO_PIN(PORT_D, 2) +#define ARDUINO_PIN_1 GPIO_PIN(PORT_D, 3) +#define ARDUINO_PIN_2 GPIO_PIN(PORT_D, 1) +#define ARDUINO_PIN_3 GPIO_PIN(PORT_D, 0) +#define ARDUINO_PIN_5 GPIO_PIN(PORT_C, 6) +#define ARDUINO_PIN_7 GPIO_PIN(PORT_E, 6) +#define ARDUINO_PIN_11 GPIO_PIN(PORT_B, 7) +#define ARDUINO_PIN_13 GPIO_PIN(PORT_C, 7) +#define ARDUINO_PIN_14 GPIO_PIN(PORT_B, 3) +#define ARDUINO_PIN_15 GPIO_PIN(PORT_B, 1) +#define ARDUINO_PIN_16 GPIO_PIN(PORT_B, 2) +#define ARDUINO_PIN_17 GPIO_PIN(PORT_B, 0) +#define ARDUINO_PIN_30 GPIO_PIN(PORT_D, 5) + +/* Analog pins */ +#define ARDUINO_PIN_4 GPIO_PIN(PORT_D, 4) +#define ARDUINO_PIN_6 GPIO_PIN(PORT_D, 7) +#define ARDUINO_PIN_8 GPIO_PIN(PORT_B, 4) +#define ARDUINO_PIN_9 GPIO_PIN(PORT_B, 5) +#define ARDUINO_PIN_10 GPIO_PIN(PORT_B, 6) +#define ARDUINO_PIN_12 GPIO_PIN(PORT_D, 6) +#define ARDUINO_PIN_18 GPIO_PIN(PORT_F, 7) +#define ARDUINO_PIN_19 GPIO_PIN(PORT_F, 6) +#define ARDUINO_PIN_20 GPIO_PIN(PORT_F, 5) +#define ARDUINO_PIN_21 GPIO_PIN(PORT_F, 4) +#define ARDUINO_PIN_22 GPIO_PIN(PORT_F, 3) +#define ARDUINO_PIN_23 GPIO_PIN(PORT_F, 2) + +/* Analog input */ +#define ARDUINO_PIN_A0 ARDUINO_PIN_18 +#define ARDUINO_PIN_A1 ARDUINO_PIN_19 +#define ARDUINO_PIN_A2 ARDUINO_PIN_20 +#define ARDUINO_PIN_A3 ARDUINO_PIN_21 +#define ARDUINO_PIN_A4 ARDUINO_PIN_22 +#define ARDUINO_PIN_A5 ARDUINO_PIN_23 +#define ARDUINO_PIN_A6 ARDUINO_PIN_4 +#define ARDUINO_PIN_A7 ARDUINO_PIN_6 +#define ARDUINO_PIN_A8 ARDUINO_PIN_8 +#define ARDUINO_PIN_A9 ARDUINO_PIN_9 +#define ARDUINO_PIN_A10 ARDUINO_PIN_10 +#define ARDUINO_PIN_A11 ARDUINO_PIN_12 +#endif + #ifdef CPU_ATMEGA2560 #define ARDUINO_PIN_0 GPIO_PIN(PORT_E, 0) #define ARDUINO_PIN_1 GPIO_PIN(PORT_E, 1) diff --git a/boards/common/arduino-atmega/include/board_common.h b/boards/common/arduino-atmega/include/board_common.h index d299e36df4..b9b2c87f00 100644 --- a/boards/common/arduino-atmega/include/board_common.h +++ b/boards/common/arduino-atmega/include/board_common.h @@ -1,6 +1,7 @@ /* * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen * 2016 Laurent Navet + * 2017 Thomas Perrot * * 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 @@ -18,6 +19,7 @@ * * @author Hinnerk van Bruinehsen * @author Laurent Navet + * @author Thomas Perrot */ #ifndef BOARD_COMMON_H @@ -49,14 +51,35 @@ extern "C" { #define LED0_MASK (1 << DDB5) #endif +#ifdef CPU_ATMEGA32U4 +#define LED0_PIN GPIO_PIN(2, 7) /**< BUILTIN LED */ +#define LED0_MASK (1 << DDC7) +#define LED1_PIN GPIO_PIN(1, 0) /**< RX LED */ +#define LED1_MASK (1 << DDB0) +#define LED2_PIN GPIO_PIN(3, 5) /**< TX LED */ +#define LED2_MASK (1 << DDD5) +#endif + #ifdef CPU_ATMEGA2560 #define LED0_PIN GPIO_PIN(1, 7) #define LED0_MASK (1 << DDB7) #endif -#define LED0_ON (PORTB |= LED0_MASK) -#define LED0_OFF (PORTB &= ~LED0_MASK) -#define LED0_TOGGLE (PORTB ^= LED0_MASK) +#ifdef CPU_ATMEGA32U4 +#define LED0_ON (PORTC |= LED0_MASK) /**< BUILTIN LED */ +#define LED0_OFF (PORTC &= ~LED0_MASK) +#define LED0_TOGGLE (PORTC ^= LED0_MASK) +#define LED1_OFF (PORTB |= LED1_MASK) /**< RX LED */ +#define LED1_ON (PORTB &= ~LED1_MASK) +#define LED1_TOGGLE (PORTB ^= LED1_MASK) +#define LED2_OFF (PORTD |= LED2_MASK) /**< TX LED */ +#define LED2_ON (PORTD &= ~LED2_MASK) +#define LED2_TOGGLE (PORTD ^= LED2_MASK) +#else +#define LED0_ON (PORTD |= LED0_MASK) +#define LED0_OFF (PORTD &= ~LED0_MASK) +#define LED0_TOGGLE (PORTD ^= LED0_MASK) +#endif /** @} */ /** diff --git a/boards/common/atmega/board.c b/boards/common/atmega/board.c index a85199b828..9e0fe6ebd0 100644 --- a/boards/common/atmega/board.c +++ b/boards/common/atmega/board.c @@ -31,6 +31,10 @@ void led_init(void); void board_init(void) { +#ifdef CPU_ATMEGA32U4 + /* disable usb interrupt on Atmega32U4 */ + PRR1 |= 1< - * - * 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_atmega32u4 - * @{ - * - * @file - * @brief Startup code and interrupt vector definition - * - * @author Thomas Perrot - * - * @} - */ - -#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__((section(".init7"))); -void init8_ovr(void) __attribute__((section(".init8"))); - - -__attribute__((used,naked)) void init7_ovr(void) -{ - __asm__("call reset_handler"); -} - -__attribute__((used,naked)) 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 - */ -__attribute__((used)) void reset_handler(void) -{ - /* initialize the board and startup the kernel */ - board_init(); - /* startup the kernel */ - kernel_init(); -} diff --git a/dist/tools/vagrant/udev_rules/99-atmega32u4.rules b/dist/tools/vagrant/udev_rules/99-atmega32u4.rules new file mode 100644 index 0000000000..0d04b32535 --- /dev/null +++ b/dist/tools/vagrant/udev_rules/99-atmega32u4.rules @@ -0,0 +1 @@ +SUBSYSTEM=="usb", ATTR{idVendor}=="2341", ATTR{idProduct}=="0036", MODE="0666", OWNER="vagrant" diff --git a/examples/default/Makefile b/examples/default/Makefile index 3af51cf2ce..3ba5afeb84 100644 --- a/examples/default/Makefile +++ b/examples/default/Makefile @@ -31,7 +31,6 @@ QUIET ?= 1 # Modules to include: USEMODULE += shell -USEMODULE += shell_commands USEMODULE += ps # include and auto-initialize all available sensors USEMODULE += saul_default diff --git a/makefiles/tools/avrdude.inc.mk b/makefiles/tools/avrdude.inc.mk index d3abc500d7..94d4b50959 100644 --- a/makefiles/tools/avrdude.inc.mk +++ b/makefiles/tools/avrdude.inc.mk @@ -9,7 +9,7 @@ DEBUGGER = $(DIST_PATH)/debug.sh $(DEBUGSERVER_FLAGS) $(DIST_PATH) $(DEBUGSERVER PROGRAMMER_FLAGS = -p $(subst atmega,m,$(CPU)) # Set flasher port only for programmers that require it -ifneq (,$(filter $(PROGRAMMER),arduino buspirate stk500v1 stk500v2 wiring)) +ifneq (,$(filter $(PROGRAMMER),arduino avr109 buspirate stk500v1 stk500v2 wiring)) # make the flasher port configurable (e.g. with atmelice the port is usb) # defaults to terminal's serial port if not configured AVRDUDE_PORT ?= $(PORT) diff --git a/sys/pipe/pipe_dynamic.c b/sys/pipe/pipe_dynamic.c index 410b41a3cc..b6ecd2982d 100644 --- a/sys/pipe/pipe_dynamic.c +++ b/sys/pipe/pipe_dynamic.c @@ -25,7 +25,7 @@ * @} */ -#if defined(MCU_ATMEGA2560) || defined(MCU_ATMEGA1281) || defined(MCU_ATMEGA328P) || defined(MCU_ATMEGA32U4) +#if defined(MCU_ATMEGA2560) || defined(MCU_ATMEGA1281) || defined(MCU_ATMEGA328P) #include #else #include