mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-12-28 10:45:22 +00:00
STM32 has a single-byte mailbox for UART I/O. When the core clock
runs at 16Mhz we can service interrupts fast enough to handle 115200
baud input, but when we drop to 1MHz we drop characters. Using DMA to
receive input solves this problem.
The STM32 DMA engine can only generate interrupts when the transfer is
half-done / all-done, so we need to poll the DMA receive-head-pointer
to see if individual characters have been received. Do this in the
tick task (every 250ms). When a character is received, poll more
quickly for a bit (5 times before the next tick) so the input console
is more responsive to typing.
BUG=chrome-os-partner:20485
BRANCH=none
TEST=Console is responsive to debug commands. For example, help -> prints help
apshutdown -> shuts down AP
arrow keys -> move cursor and scroll through command history
Ctrl+Q, help, wait a second, Ctrl+S -> help output printed after Ctrl+S
Then in chip/stm32/config_chip.h, comment out #define CONFIG_UART_RX_DMA
and rebuild/reflash the EC. When the AP is up, the console works normally
but after 'apshutdown', the EC drops to 1MHz core clock, and the arrow
keys don't work. (This step confirms that adding DMA support did not
change the behavior of systems where CONFIG_UART_RX_DMA is not defined.)
Change-Id: I199448354824bd747c7b290ea7fd5ccf354c11bb
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/169406
Reviewed-by: Simon Glass <sjg@chromium.org>
218 lines
4.6 KiB
C
218 lines
4.6 KiB
C
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
/* uart.h - UART module for Chrome EC */
|
|
|
|
#ifndef __CROS_EC_UART_H
|
|
#define __CROS_EC_UART_H
|
|
|
|
#include <stdarg.h> /* For va_list */
|
|
#include "common.h"
|
|
|
|
/**
|
|
* Initialize the UART module.
|
|
*/
|
|
void uart_init(void);
|
|
|
|
/**
|
|
* Return non-zero if UART init has completed.
|
|
*/
|
|
int uart_init_done(void);
|
|
|
|
/*
|
|
* Output functions
|
|
*
|
|
* Output is buffered. If the buffer overflows, subsequent output is
|
|
* discarded.
|
|
*
|
|
* Modules should use the output functions in console.h in preference to these
|
|
* routines, so that output can be filtered on a module-by-module basis.
|
|
*/
|
|
|
|
/**
|
|
* Put a single character to the UART, like putchar().
|
|
*
|
|
* @param c Character to put
|
|
* @return EC_SUCCESS, or non-zero if output was truncated.
|
|
*/
|
|
int uart_putc(int c);
|
|
|
|
/**
|
|
* Put a null-terminated string to the UART, like fputs().
|
|
*
|
|
* @return EC_SUCCESS, or non-zero if output was truncated.
|
|
*/
|
|
int uart_puts(const char *outstr);
|
|
|
|
/**
|
|
* Print formatted output to the UART, like printf().
|
|
*
|
|
* See printf.h for valid formatting codes.
|
|
*
|
|
* @return EC_SUCCESS, or non-zero if output was truncated.
|
|
*/
|
|
int uart_printf(const char *format, ...);
|
|
|
|
/**
|
|
* Print formatted output to the UART, like vprintf().
|
|
*
|
|
* See printf.h for valid formatting codes.
|
|
*
|
|
* @return EC_SUCCESS, or non-zero if output was truncated.
|
|
*/
|
|
int uart_vprintf(const char *format, va_list args);
|
|
|
|
/**
|
|
* Flush output. Blocks until UART has transmitted all output.
|
|
*/
|
|
void uart_flush_output(void);
|
|
|
|
/*
|
|
* Input functions
|
|
*
|
|
* Input is buffered. If the buffer overflows, the oldest input in
|
|
* the buffer is discarded to make room for the new input.
|
|
*
|
|
* Input lines may be terminated by CR ('\r'), LF ('\n'), or CRLF; all
|
|
* are translated to newline.
|
|
*/
|
|
|
|
/**
|
|
* Read a single character of input, similar to fgetc().
|
|
*
|
|
* @return the character, or -1 if no input waiting.
|
|
*/
|
|
int uart_getc(void);
|
|
|
|
/*
|
|
* Hardware UART driver functions
|
|
*/
|
|
|
|
/**
|
|
* Flush the transmit FIFO.
|
|
*/
|
|
void uart_tx_flush(void);
|
|
|
|
/**
|
|
* Return non-zero if there is room to transmit a character immediately.
|
|
*/
|
|
int uart_tx_ready(void);
|
|
|
|
/**
|
|
* Return non-zero if UART is ready to start a DMA transfer.
|
|
*/
|
|
int uart_tx_dma_ready(void);
|
|
|
|
/**
|
|
* Start a UART transmit DMA transfer
|
|
*
|
|
* @param src Pointer to data to send
|
|
* @param len Length of transfer in bytes
|
|
*/
|
|
void uart_tx_dma_start(const char *src, int len);
|
|
|
|
/**
|
|
* Return non-zero if the UART has a character available to read.
|
|
*/
|
|
int uart_rx_available(void);
|
|
|
|
/**
|
|
* Start a UART receive DMA transfer.
|
|
*
|
|
* DMA will be configured in circular buffer mode, so received characters
|
|
* will be stored into the buffer continuously.
|
|
*
|
|
* @param dest Pointer to destination buffer
|
|
* @param len Length of buffer in bytes
|
|
*/
|
|
void uart_rx_dma_start(char *dest, int len);
|
|
|
|
/**
|
|
* Return the head of the receive DMA transfer buffer
|
|
*
|
|
* This is the next offset in the buffer which will receive a character, and
|
|
* will be from 0..(len-1) where len is the buffer length passed to
|
|
* uart_rx_dma_start().
|
|
*/
|
|
int uart_rx_dma_head(void);
|
|
|
|
/**
|
|
* Send a character to the UART data register.
|
|
*
|
|
* If the transmit FIFO is full, blocks until there is space.
|
|
*
|
|
* @param c Character to send.
|
|
*/
|
|
void uart_write_char(char c);
|
|
|
|
/**
|
|
* Read one char from the UART data register.
|
|
*
|
|
* @return The character read.
|
|
*/
|
|
int uart_read_char(void);
|
|
|
|
/**
|
|
* Disable all UART related IRQs.
|
|
*
|
|
* Used to avoid concurrent accesses on UART management variables.
|
|
*/
|
|
void uart_disable_interrupt(void);
|
|
|
|
/**
|
|
* Re-enable UART IRQs.
|
|
*/
|
|
void uart_enable_interrupt(void);
|
|
|
|
/**
|
|
* Re-enable the UART transmit interrupt.
|
|
*
|
|
* This also forces triggering a UART interrupt, if the transmit interrupt was
|
|
* disabled.
|
|
*/
|
|
void uart_tx_start(void);
|
|
|
|
/**
|
|
* Disable the UART transmit interrupt.
|
|
*/
|
|
void uart_tx_stop(void);
|
|
|
|
/**
|
|
* Helper for processing UART input.
|
|
*
|
|
* Reads the input FIFO until empty. Intended to be called from the driver
|
|
* interrupt handler.
|
|
*/
|
|
void uart_process_input(void);
|
|
|
|
/**
|
|
* Helper for processing UART output.
|
|
*
|
|
* Fills the output FIFO until the transmit buffer is empty or the FIFO full.
|
|
* Intended to be called from the driver interrupt handler.
|
|
*/
|
|
void uart_process_output(void);
|
|
|
|
/*
|
|
* COMx functions
|
|
*/
|
|
|
|
/**
|
|
* Enable COMx interrupts
|
|
*/
|
|
void uart_comx_enable(void);
|
|
|
|
/**
|
|
* Return non-zero if ok to put a character via uart_comx_putc().
|
|
*/
|
|
int uart_comx_putc_ok(void);
|
|
|
|
/**
|
|
* Write a character to the COMx UART interface.
|
|
*/
|
|
void uart_comx_putc(int c);
|
|
|
|
#endif /* __CROS_EC_UART_H */
|