2014-07-15 12:08:52 +02:00
|
|
|
/*
|
2015-09-14 00:27:27 +02:00
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
|
2018-03-05 19:42:41 +01:00
|
|
|
* 2018 RWTH Aachen, Josua Arndt <jarndt@ias.rwth-aachen.de>
|
2014-07-15 12:08:52 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-12-06 01:12:31 +01:00
|
|
|
* @ingroup cpu_atmega_common
|
2014-07-15 12:08:52 +02:00
|
|
|
* @brief Common implementations and headers for ATmega family based micro-controllers
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Basic definitions for the ATmega common module
|
|
|
|
*
|
|
|
|
* When ever you want to do something hardware related, that is accessing MCUs registers directly,
|
|
|
|
* just include this file. It will then make sure that the MCU specific headers are included.
|
|
|
|
*
|
|
|
|
* @author Stefan Pfeiffer <stefan.pfeiffer@fu-berlin.de>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
* @author Hinnerk van Bruinehsen <h.v.bruinehsen@fu-berlin.de>
|
2015-09-14 00:27:27 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2018-03-05 19:42:41 +01:00
|
|
|
* @author Josua Arndt <jarndt@ias.rwth-aachen.de>
|
|
|
|
*
|
2014-07-15 12:08:52 +02:00
|
|
|
*/
|
|
|
|
|
2017-05-23 18:19:52 +02:00
|
|
|
#ifndef CPU_H
|
|
|
|
#define CPU_H
|
2014-07-15 12:08:52 +02:00
|
|
|
|
2015-09-04 16:51:08 +02:00
|
|
|
#include <stdio.h>
|
2015-09-14 00:27:27 +02:00
|
|
|
#include <stdint.h>
|
2014-07-15 12:08:52 +02:00
|
|
|
|
2015-09-14 00:27:27 +02:00
|
|
|
#include <avr/interrupt.h>
|
2015-09-04 16:51:08 +02:00
|
|
|
#include "cpu_conf.h"
|
|
|
|
|
2014-07-15 12:08:52 +02:00
|
|
|
/**
|
|
|
|
* For downwards compatibility with old RIOT code.
|
|
|
|
* TODO: remove once core was adjusted
|
|
|
|
*/
|
|
|
|
#include "irq.h"
|
2014-10-13 10:53:20 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-09-14 00:27:27 +02:00
|
|
|
/**
|
|
|
|
* @brief global in-ISR state variable
|
|
|
|
*/
|
|
|
|
extern volatile uint8_t __in_isr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Flag entering of an ISR
|
|
|
|
*/
|
|
|
|
static inline void __enter_isr(void)
|
|
|
|
{
|
|
|
|
__in_isr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Flag exiting of an ISR
|
|
|
|
*/
|
|
|
|
static inline void __exit_isr(void)
|
|
|
|
{
|
|
|
|
__in_isr = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-15 12:08:52 +02:00
|
|
|
/**
|
|
|
|
* @brief Initialization of the CPU
|
|
|
|
*/
|
|
|
|
void cpu_init(void);
|
|
|
|
|
2015-09-04 16:51:08 +02:00
|
|
|
/**
|
|
|
|
* @brief Print the last instruction's address
|
|
|
|
*/
|
2018-03-20 23:32:08 +01:00
|
|
|
__attribute__((always_inline)) static inline void cpu_print_last_instruction(void)
|
2015-09-04 16:51:08 +02:00
|
|
|
{
|
2018-03-05 19:42:41 +01:00
|
|
|
uint8_t hi;
|
|
|
|
uint8_t lo;
|
|
|
|
uint16_t ptr;
|
|
|
|
|
2018-03-20 23:32:08 +01:00
|
|
|
__asm__ volatile( "in __tmp_reg__, __SP_H__ \n\t"
|
|
|
|
"mov %0, __tmp_reg__ \n\t"
|
|
|
|
: "=g"(hi) );
|
|
|
|
|
|
|
|
__asm__ volatile( "in __tmp_reg__, __SP_L__ \n\t"
|
|
|
|
"mov %0, __tmp_reg__ \n\t"
|
|
|
|
: "=g"(lo) );
|
2018-03-05 19:42:41 +01:00
|
|
|
ptr = hi<<8 | lo;
|
|
|
|
printf("Stack Pointer: 0x%04x\n", ptr);
|
2015-09-04 16:51:08 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 06:27:02 +02:00
|
|
|
/**
|
|
|
|
* @brief ATmega system clock prescaler settings
|
|
|
|
*
|
|
|
|
* Some CPUs may not support the highest prescaler settings
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV1 = 0,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV2 = 1,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV4 = 2,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV8 = 3,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV16 = 4,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV32 = 5,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV64 = 6,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV128 = 7,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV256 = 8,
|
|
|
|
CPU_ATMEGA_CLK_SCALE_DIV512 = 9,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes system clock prescaler
|
|
|
|
*/
|
|
|
|
static inline void atmega_set_prescaler(uint8_t clk_scale)
|
|
|
|
{
|
|
|
|
/* Enable clock change */
|
|
|
|
/* Must be assignment to set all other bits to zero, see datasheet */
|
|
|
|
CLKPR = (1 << CLKPCE);
|
|
|
|
|
|
|
|
/* Write clock within 4 cycles */
|
|
|
|
CLKPR = clk_scale;
|
|
|
|
}
|
|
|
|
|
2018-03-26 21:46:12 +02:00
|
|
|
/**
|
|
|
|
* @brief Initializes avrlibc stdio
|
|
|
|
*/
|
|
|
|
void atmega_stdio_init(void);
|
|
|
|
|
2018-04-08 17:10:58 +02:00
|
|
|
/**
|
|
|
|
* @brief Exit ISR mode and yield with a return from interrupt. Use at the
|
|
|
|
* end of ISRs in place of thread_yield_higher. If thread_yield is needed, use
|
|
|
|
* thread_yield followed by thread_yield_isr instead of thread_yield alone.
|
|
|
|
*/
|
|
|
|
void thread_yield_isr(void);
|
|
|
|
|
2014-10-13 10:53:20 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-15 12:08:52 +02:00
|
|
|
|
2017-05-23 18:19:52 +02:00
|
|
|
#endif /* CPU_H */
|
2014-07-15 12:08:52 +02:00
|
|
|
/** @} */
|