cpu.h File Reference

Common implementations and headers for AVR-8 family based micro-controllers. More...

Detailed Description

Common implementations and headers for AVR-8 family based micro-controllers.

Basic definitions for the AVR-8 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 stefa.nosp@m.n.pf.nosp@m.eiffe.nosp@m.r@fu.nosp@m.-berl.nosp@m.in.d.nosp@m.e
Hauke Petersen hauke.nosp@m..pet.nosp@m.ersen.nosp@m.@fu-.nosp@m.berli.nosp@m.n.de
Hinnerk van Bruinehsen h.v.b.nosp@m.ruin.nosp@m.ehsen.nosp@m.@fu-.nosp@m.berli.nosp@m.n.de
Kaspar Schleiser kaspa.nosp@m.r@sc.nosp@m.hleis.nosp@m.er.d.nosp@m.e
Josua Arndt jarnd.nosp@m.t@ia.nosp@m.s.rwt.nosp@m.h-aa.nosp@m.chen..nosp@m.de
Gerson Fernando Budke nando.nosp@m.jve@.nosp@m.gmail.nosp@m..com

Definition in file cpu.h.

#include <stdio.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include "cpu_conf.h"
#include "cpu_clock.h"
#include "sched.h"
#include "thread.h"
+ Include dependency graph for cpu.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static uint8_t avr8_get_state (void)
 Atomically read the state (avr8_state) More...
 
static void avr8_enter_isr (void)
 Run this code on entering interrupt routines.
 
static int avr8_is_uart_tx_pending (void)
 Check if TX on any present UART device is still pending. More...
 
void avr8_exit_isr (void)
 Run this code on exiting interrupt routines.
 
void cpu_init (void)
 Initialization of the CPU.
 
void avr8_clk_init (void)
 Initialization of the CPU clock.
 
static void cpu_print_last_instruction (void)
 Print the last instruction's address.
 
void avr8_stdio_init (void)
 Initializes avrlibc stdio.
 
void avr8_reset_cause (void)
 Print reset cause.
 

Variables

uint8_t avr8_state
 Global variable containing the current state of the MCU. More...
 

Flags for the current state of the ATmega MCU

#define AVR8_STATE_FLAG_ISR   (0x80U)
 In ISR.
 
#define AVR8_STATE_FLAG_UART0_TX   (0x01U)
 TX pending for UART 0.
 
#define AVR8_STATE_FLAG_UART1_TX   (0x02U)
 TX pending for UART 1.
 
#define AVR8_STATE_FLAG_UART2_TX   (0x04U)
 TX pending for UART 2.
 
#define AVR8_STATE_FLAG_UART3_TX   (0x08U)
 TX pending for UART 3.
 
#define AVR8_STATE_FLAG_UART4_TX   (0x10U)
 TX pending for UART 4.
 
#define AVR8_STATE_FLAG_UART5_TX   (0x20U)
 TX pending for UART 5.
 
#define AVR8_STATE_FLAG_UART6_TX   (0x40U)
 TX pending for UART 6.
 
#define AVR8_STATE_FLAG_UART_TX(x)   (0x01U << x)
 TX pending for UART x.
 

Function Documentation

◆ avr8_get_state()

static uint8_t avr8_get_state ( void  )
inlinestatic

Atomically read the state (avr8_state)

This function guarantees that the read is not optimized out, not reordered and done atomically. This does not mean that by the time return value is processed that it still reflects the value currently stored in avr8_state.

Using ASM rather than C11 atomics has less overhead, as not every access to the state has to be performed atomically: Those done from ISR will not be interrupted (no support for nested interrupts) and barriers at the begin and end of the ISRs make sure the access takes place before IRQ context is left.

Definition at line 115 of file cpu.h.

◆ avr8_is_uart_tx_pending()

static int avr8_is_uart_tx_pending ( void  )
inlinestatic

Check if TX on any present UART device is still pending.

Return values
!=0At least on UART device is still sending data out
0No UART is currently sending data

Definition at line 147 of file cpu.h.

Variable Documentation

◆ avr8_state

uint8_t avr8_state

Global variable containing the current state of the MCU.

Note
This variable is updated from IRQ context; access to it should be wrapped into irq_disable and irq_restore or avr8_get_state should be used.

Contents:

7 6 5 4 3 2 1 0
+---+---+---+---+---+---+---+---+
|IRQ|TX6|TX5|TX4|TX3|TX2|TX1|TX0|
+---+---+---+---+---+---+---+---+
Label Description
IRQ This bit is set when in IRQ context
TX6 This bit is set when on UART6 TX is pending
TX5 This bit is set when on UART5 TX is pending
TX4 This bit is set when on UART4 TX is pending
TX3 This bit is set when on UART3 TX is pending
TX2 This bit is set when on UART2 TX is pending
TX1 This bit is set when on UART1 TX is pending
TX0 This bit is set when on UART0 TX is pending