1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

cpu/avr8_common: Split avr8_state

The avr8_state store state information used to determine scheduling
and uart irq. This move all uart irq states to avr8_state_uart
variable. It introduce the use of General Purpose IO Register 0
(GPIOR0) when available and now all uarts from xmega can be used.

This is a preparation for future scheduling and irq optimizations.

Signed-off-by: Gerson Fernando Budke <nandojve@gmail.com>
This commit is contained in:
Gerson Fernando Budke 2023-06-27 22:09:21 +02:00
parent 3ec62a291a
commit b7873015aa
5 changed files with 126 additions and 31 deletions

View File

@ -2,6 +2,7 @@
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
* 2017 Thomas Perrot <thomas.perrot@tupi.fr>
* 2023 Hugues Larrive
* 2023 Gerson Fernando Budke
*
* 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 Hinnerk van Bruinehsen <h.v.bruinehsen@fu-berlin.de>
* @author Thomas Perrot <thomas.perrot@tupi.fr>
* @author Hugues Larrive <hugues.larrive@pm.me>
* @author Gerson Fernando Budke <nandojve@gmail.com>
*
*
* Support static BAUD rate calculation using STDIO_UART_BAUDRATE.
@ -192,8 +194,9 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)
/* start of TX won't finish until no data in UDRn and transmit shift
register is empty */
unsigned long state = irq_disable();
avr8_state |= AVR8_STATE_FLAG_UART_TX(uart);
avr8_uart_tx_set_pending(uart);
irq_restore(state);
dev[uart]->DR = data[i];
}
}
@ -225,7 +228,7 @@ static inline void _tx_isr_handler(int num)
/* entire frame in the Transmit Shift Register has been shifted out and
there are no new data currently present in the transmit buffer */
avr8_state &= ~AVR8_STATE_FLAG_UART_TX(num);
avr8_uart_tx_clear_pending(num);
avr8_exit_isr();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Gerson Fernando Budke <nandojve@gmail.com>
* Copyright (C) 2021-2023 Gerson Fernando Budke <nandojve@gmail.com>
*
* 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
@ -310,7 +310,7 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)
/* start of TX won't finish until no data in DATAn and transmit shift
register is empty */
uint8_t irq_state = irq_disable();
avr8_state |= AVR8_STATE_FLAG_UART_TX(uart);
avr8_uart_tx_set_pending(uart);
irq_restore(irq_state);
dev(uart)->DATA = data[i];
@ -344,7 +344,7 @@ static inline void _tx_isr_handler(int num)
/* entire frame in the Transmit Shift Register has been shifted out and
there are no new data currently present in the transmit buffer */
avr8_state &= ~AVR8_STATE_FLAG_UART_TX(num);
avr8_uart_tx_clear_pending(num);
avr8_exit_isr();
}

View File

@ -2,7 +2,7 @@
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
* 2017 RWTH Aachen, Josua Arndt
* 2018 Matthew Blue
* 2021 Gerson Fernando Budke
* 2021-2023 Gerson Fernando Budke
*
* 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
@ -69,6 +69,9 @@
uint8_t mcusr_mirror __attribute__((section(".noinit")));
uint8_t soft_rst __attribute__((section(".noinit")));
uint8_t avr8_state = 0;
#if (AVR8_STATE_UART_USE_SRAM)
uint8_t avr8_state_uart_sram = 0;
#endif
void get_mcusr(void) __attribute__((naked, section(".init0"), used));
@ -118,6 +121,9 @@ void cpu_init(void)
| PMIC_LOLVLEN_bm;
#endif
/* Set global resources initial state */
avr8_state_uart = 0;
irq_enable();
}

View File

@ -2,7 +2,7 @@
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
* 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
* 2018 RWTH Aachen, Josua Arndt <jarndt@ias.rwth-aachen.de>
* 2021 Gerson Fernando Budke <nandojve@gmail.com>
* 2021-2023 Gerson Fernando Budke <nandojve@gmail.com>
*
* 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
@ -41,6 +41,7 @@
#include "cpu_clock.h"
#include "sched.h"
#include "thread.h"
#include "states_internal.h"
#ifdef __cplusplus
extern "C"
@ -62,14 +63,6 @@ extern "C"
* @{
*/
#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 */
/** @} */
/**
@ -83,20 +76,13 @@ extern "C"
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 7 6 5 4 3 2 1 0
* +---+---+---+---+---+---+---+---+
* |IRQ|TX6|TX5|TX4|TX3|TX2|TX1|TX0|
* |IRQ| RESERVED |
* +---+---+---+---+---+---+---+---+
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* | 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 |
*/
extern uint8_t avr8_state;
@ -139,6 +125,33 @@ static inline void avr8_enter_isr(void)
avr8_state |= AVR8_STATE_FLAG_ISR;
}
/**
* @brief Compute UART TX channel
*
* @param uart The UART number
*/
#define AVR8_STATE_FLAG_UART_TX(uart) (0x01U << uart)
/**
* @brief Set UART TX channel as pending
*
* @param uart The UART number
*/
static inline void avr8_uart_tx_set_pending(unsigned uart)
{
avr8_state_uart |= AVR8_STATE_FLAG_UART_TX(uart);
}
/**
* @brief Clear UART TX channel pending state
*
* @param uart The UART number
*/
static inline void avr8_uart_tx_clear_pending(unsigned uart)
{
avr8_state_uart &= ~AVR8_STATE_FLAG_UART_TX(uart);
}
/**
* @brief Check if TX on any present UART device is still pending
*
@ -147,14 +160,7 @@ static inline void avr8_enter_isr(void)
*/
static inline int avr8_is_uart_tx_pending(void)
{
uint8_t state = avr8_get_state();
return (state & (AVR8_STATE_FLAG_UART0_TX
| AVR8_STATE_FLAG_UART1_TX
| AVR8_STATE_FLAG_UART2_TX
| AVR8_STATE_FLAG_UART3_TX
| AVR8_STATE_FLAG_UART4_TX
| AVR8_STATE_FLAG_UART5_TX
| AVR8_STATE_FLAG_UART6_TX));
return avr8_state_uart;
}
/**

View File

@ -0,0 +1,80 @@
/*
* Copyright (C) 2023 Gerson Fernando Budke
*
* 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_avr8_common
* @{
*
* @file
* @brief States internal interface
*
* @author Gerson Fernando Budke <nandojve@gmail.com>
*
*/
#ifndef STATES_INTERNAL_H
#define STATES_INTERNAL_H
#include <avr/io.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name Internal flag which defines if uart state is stored on SRAM
* @{
*/
#ifdef GPIOR0
#define AVR8_STATE_UART_USE_SRAM 0 /**< UART state using GPIOR registers. */
#else
#define AVR8_STATE_UART_USE_SRAM 1 /**< UART state using SRAM. */
#endif
/** @} */
/**
* @name UART TX pending state
* @{
*
* @note The content must be changed using the pair
* @ref avr8_uart_tx_set_pending and @ref avr8_uart_tx_clear_pending
* methods and the state is stored on @ref avr8_state_uart.
*
* Contents:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 7 6 5 4 3 2 1 0
* +---+---+---+---+---+---+---+---+
* |TX7|TX6|TX5|TX4|TX3|TX2|TX1|TX0|
* +---+---+---+---+---+---+---+---+
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* | Label | Description |
* |:-------|:--------------------------------------------------------------|
* | TX7 | This bit is set when on UART7 TX is pending |
* | 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 |
*/
#if (AVR8_STATE_UART_USE_SRAM)
extern uint8_t avr8_state_uart_sram; /**< UART state variable. */
#define avr8_state_uart avr8_state_uart_sram /**< Definition for SRAM. */
#else
#define avr8_state_uart GPIOR0 /**< Definition for GPIOR0. */
#endif
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* STATES_INTERNAL_H */
/** @} */