2009-11-28 10:28:11 +01:00
|
|
|
/**
|
2009-11-29 11:03:08 +01:00
|
|
|
******************************************************************************
|
2010-07-16 21:53:35 +02:00
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_USART USART Functions
|
|
|
|
* @brief PIOS interface for USART port
|
|
|
|
* @{
|
2009-11-28 12:25:16 +01:00
|
|
|
*
|
2009-12-01 18:15:33 +01:00
|
|
|
* @file pios_usart.c
|
2010-01-31 18:56:54 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* Parts by Thorsten Klose (tk@midibox.org) (tk@midibox.org)
|
2010-07-16 21:53:35 +02:00
|
|
|
* @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent)
|
2009-11-29 11:03:08 +01:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2010-07-16 07:31:11 +02:00
|
|
|
*
|
2009-11-29 11:03:08 +01:00
|
|
|
*****************************************************************************/
|
2009-11-28 10:28:11 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2009-11-29 07:09:33 +01:00
|
|
|
|
2009-11-28 10:28:11 +01:00
|
|
|
/* Project Includes */
|
|
|
|
#include "pios.h"
|
|
|
|
|
2010-03-11 13:38:51 +01:00
|
|
|
#if defined(PIOS_INCLUDE_USART)
|
2010-02-28 07:47:49 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
#include <pios_usart_priv.h>
|
|
|
|
|
|
|
|
/* Provide a COM driver */
|
|
|
|
const struct pios_com_driver pios_usart_com_driver = {
|
|
|
|
.set_baud = PIOS_USART_ChangeBaud,
|
|
|
|
.tx_nb = PIOS_USART_TxBufferPutMoreNonBlocking,
|
|
|
|
.tx = PIOS_USART_TxBufferPutMore,
|
|
|
|
.rx = PIOS_USART_RxBufferGet,
|
|
|
|
.rx_avail = PIOS_USART_RxBufferUsed,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct pios_usart_dev * find_usart_dev_by_id (uint8_t usart)
|
|
|
|
{
|
|
|
|
if (usart >= pios_usart_num_devices) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-29 07:09:33 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
return &(pios_usart_devs[usart]);
|
|
|
|
}
|
2009-11-29 07:09:33 +01:00
|
|
|
|
2009-11-29 08:15:59 +01:00
|
|
|
/**
|
2010-05-11 15:36:13 +02:00
|
|
|
* Initialise the onboard USARTs
|
2009-11-29 08:15:59 +01:00
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
void PIOS_USART_Init(void)
|
2009-11-28 10:28:11 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < pios_usart_num_devices; i++) {
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(i);
|
|
|
|
PIOS_DEBUG_Assert(usart_dev);
|
|
|
|
|
|
|
|
/* Clear buffer counters */
|
|
|
|
usart_dev->rx.head = usart_dev->rx.tail = usart_dev->rx.size = 0;
|
|
|
|
usart_dev->tx.head = usart_dev->tx.tail = usart_dev->tx.size = 0;
|
|
|
|
|
|
|
|
/* Enable the USART Pins Software Remapping */
|
|
|
|
if (usart_dev->cfg->remap) {
|
|
|
|
GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the USART Rx and Tx pins */
|
|
|
|
GPIO_Init(usart_dev->cfg->rx.gpio, &usart_dev->cfg->rx.init);
|
|
|
|
GPIO_Init(usart_dev->cfg->tx.gpio, &usart_dev->cfg->tx.init);
|
|
|
|
|
|
|
|
/* Enable USART clock */
|
|
|
|
switch ((uint32_t)usart_dev->cfg->regs) {
|
|
|
|
case (uint32_t)USART1:
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)USART2:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)USART3:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable USART */
|
|
|
|
USART_Init(usart_dev->cfg->regs, &usart_dev->cfg->init);
|
|
|
|
|
|
|
|
/* Configure USART Interrupts */
|
|
|
|
NVIC_Init(&usart_dev->cfg->irq.init);
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE);
|
|
|
|
|
|
|
|
/* Enable USART */
|
|
|
|
USART_Cmd(usart_dev->cfg->regs, ENABLE);
|
|
|
|
}
|
2010-02-28 09:29:14 +01:00
|
|
|
}
|
2009-11-29 07:09:33 +01:00
|
|
|
|
2009-11-29 08:15:59 +01:00
|
|
|
/**
|
2010-02-28 09:29:14 +01:00
|
|
|
* Changes the baud rate of the USART peripheral without re-initialising.
|
2010-05-08 04:44:26 +02:00
|
|
|
* \param[in] usart USART name (GPS, TELEM, AUX)
|
|
|
|
* \param[in] baud Requested baud rate
|
2009-11-29 08:15:59 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
void PIOS_USART_ChangeBaud(uint8_t usart, uint32_t baud)
|
2009-11-28 10:28:11 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -2;
|
|
|
|
}
|
2010-05-12 03:56:00 +02:00
|
|
|
#endif
|
2010-06-13 06:24:26 +02:00
|
|
|
|
|
|
|
/* Start with a copy of the default configuration for the peripheral */
|
|
|
|
USART_InitStructure = usart_dev->cfg->init;
|
|
|
|
|
|
|
|
/* Adjust the baud rate */
|
|
|
|
USART_InitStructure.USART_BaudRate = baud;
|
|
|
|
|
|
|
|
/* Write back the new configuration */
|
|
|
|
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
|
2009-11-28 10:28:11 +01:00
|
|
|
}
|
2009-11-28 21:05:36 +01:00
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* Returns number of free bytes in receive buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return USART number of free bytes
|
|
|
|
* \return 1: USART available
|
|
|
|
* \return 0: USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_RxBufferFree(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sizeof(usart_dev->rx.buf) - usart_dev->rx.size);
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* Returns number of used bytes in receive buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return > 0: number of used bytes
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return 0 if USART not available
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_RxBufferUsed(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (usart_dev->rx.size);
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* Gets a byte from the receive buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return -2 if no new byte available
|
|
|
|
* \return >= 0: number of received bytes
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_RxBufferGet(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usart_dev->rx.size) {
|
|
|
|
/* Nothing new in the buffer */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get byte - this operation should be atomic! */
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
uint8_t b = usart_dev->rx.buf[usart_dev->rx.tail++];
|
|
|
|
if (usart_dev->rx.tail >= sizeof(usart_dev->rx.buf)) {
|
|
|
|
usart_dev->rx.tail = 0;
|
|
|
|
}
|
|
|
|
usart_dev->rx.size--;
|
|
|
|
PIOS_IRQ_Enable();
|
|
|
|
|
|
|
|
/* Return received byte */
|
|
|
|
return b;
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* Returns the next byte of the receive buffer without taking it
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return -2 if no new byte available
|
|
|
|
* \return >= 0: number of received bytes
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_RxBufferPeek(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usart_dev->rx.size) {
|
|
|
|
/* Nothing new in the buffer */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get byte - this operation should be atomic! */
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
uint8_t b = usart_dev->rx.buf[usart_dev->rx.tail];
|
|
|
|
PIOS_IRQ_Enable();
|
|
|
|
|
|
|
|
/* Return received byte */
|
|
|
|
return b;
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* puts a byte onto the receive buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \param[in] b byte which should be put into Rx buffer
|
|
|
|
* \return 0 if no error
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return -2 if buffer full (retry)
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_RxBufferPut(uint8_t usart, uint8_t b)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usart_dev->rx.size >= sizeof(usart_dev->rx.buf)) {
|
|
|
|
/* Buffer full (retry) */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy received byte into receive buffer */
|
|
|
|
/* This operation should be atomic! */
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
usart_dev->rx.buf[usart_dev->rx.head++] = b;
|
|
|
|
if (usart_dev->rx.head >= sizeof(usart_dev->rx.buf)) {
|
|
|
|
usart_dev->rx.head = 0;
|
|
|
|
}
|
|
|
|
usart_dev->rx.size++;
|
|
|
|
PIOS_IRQ_Enable();
|
|
|
|
|
|
|
|
/* No error */
|
|
|
|
return 0;
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* returns number of free bytes in transmit buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return number of free bytes
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return 0 if USART not available
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_TxBufferFree(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sizeof(usart_dev->tx.buf) - usart_dev->tx.size);
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* returns number of used bytes in transmit buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return number of used bytes
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return 0 if USART not available
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_TxBufferUsed(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (usart_dev->tx.size);
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* gets a byte from the transmit buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return -2 if no new byte available
|
|
|
|
* \return >= 0: transmitted byte
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_TxBufferGet(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usart_dev->tx.size) {
|
|
|
|
/* Nothing new in the buffer */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get byte - this operation should be atomic! */
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
uint8_t b = usart_dev->tx.buf[usart_dev->tx.tail++];
|
|
|
|
if (usart_dev->tx.tail >= sizeof(usart_dev->tx.buf)) {
|
|
|
|
usart_dev->tx.tail = 0;
|
|
|
|
}
|
|
|
|
usart_dev->tx.size--;
|
|
|
|
PIOS_IRQ_Enable();
|
|
|
|
|
|
|
|
/* Return received byte */
|
|
|
|
return b;
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* puts more than one byte onto the transmit buffer (used for atomic sends)
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \param[in] *buffer pointer to buffer to be sent
|
|
|
|
* \param[in] len number of bytes to be sent
|
|
|
|
* \return 0 if no error
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return -2 if buffer full or cannot get all requested bytes (retry)
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -3 if USART not supported by USARTTxBufferPut Routine
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-07-04 04:21:08 +02:00
|
|
|
int32_t PIOS_USART_TxBufferPutMoreNonBlocking(uint8_t usart, const uint8_t *buffer, uint16_t len)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
|
|
|
|
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
|
|
|
|
if (!usart_dev) {
|
|
|
|
/* Undefined USART port for this board (see pios_board.c) */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usart_dev->tx.size + len >= sizeof(usart_dev->tx.buf)) {
|
|
|
|
/* Buffer cannot accept all requested bytes (retry) */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy bytes to be transmitted into transmit buffer */
|
|
|
|
/* This operation should be atomic! */
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
|
|
|
|
uint16_t i;
|
|
|
|
for(i = 0; i < len; ++i) {
|
|
|
|
usart_dev->tx.buf[usart_dev->tx.head++] = *buffer++;
|
|
|
|
if (usart_dev->tx.head >= sizeof(usart_dev->tx.buf)) {
|
|
|
|
usart_dev->tx.head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usart_dev->tx.size++;
|
|
|
|
if (usart_dev->tx.size == 1) {
|
|
|
|
/* Buffer has just become non-empty, enable tx interrupt. */
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_IRQ_Enable();
|
|
|
|
|
|
|
|
/* No error */
|
|
|
|
return 0;
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* puts more than one byte onto the transmit buffer (used for atomic sends)<BR>
|
|
|
|
* (blocking function)
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \param[in] *buffer pointer to buffer to be sent
|
|
|
|
* \param[in] len number of bytes to be sent
|
|
|
|
* \return 0 if no error
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
|
|
|
* \return -3 if USART not supported by USARTTxBufferPut Routine
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-07-04 04:21:08 +02:00
|
|
|
int32_t PIOS_USART_TxBufferPutMore(uint8_t usart, const uint8_t *buffer, uint16_t len)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t rc;
|
2009-11-28 21:05:36 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
while((rc = PIOS_USART_TxBufferPutMoreNonBlocking(usart, buffer, len)) == -2);
|
2009-11-28 21:05:36 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
return rc;
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* puts a byte onto the transmit buffer
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \param[in] b byte which should be put into Tx buffer
|
|
|
|
* \return 0 if no error
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
2009-11-29 15:21:50 +01:00
|
|
|
* \return -2 if buffer full (retry)
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -3 if USART not supported by USARTTxBufferPut Routine
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_TxBufferPut_NonBlocking(uint8_t usart, uint8_t b)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
return PIOS_USART_TxBufferPutMoreNonBlocking(usart, &b, 1);
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2009-11-29 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* puts a byte onto the transmit buffer<BR>
|
|
|
|
* (blocking function)
|
2010-02-28 09:29:14 +01:00
|
|
|
* \param[in] USART USART name
|
2009-11-29 15:21:50 +01:00
|
|
|
* \param[in] b byte which should be put into Tx buffer
|
|
|
|
* \return 0 if no error
|
2009-12-01 18:15:33 +01:00
|
|
|
* \return -1 if USART not available
|
|
|
|
* \return -3 if USART not supported by USARTTxBufferPut Routine
|
2009-11-30 17:03:37 +01:00
|
|
|
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
2009-11-29 15:21:50 +01:00
|
|
|
*/
|
2010-06-13 06:24:26 +02:00
|
|
|
int32_t PIOS_USART_TxBufferPut(uint8_t usart, uint8_t b)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
return PIOS_USART_TxBufferPutMore(usart, &b, 1);
|
2009-11-28 21:05:36 +01:00
|
|
|
}
|
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
void PIOS_USART_IRQ_Handler(uint8_t usart)
|
2009-11-28 21:05:36 +01:00
|
|
|
{
|
2010-06-13 06:24:26 +02:00
|
|
|
struct pios_usart_dev * usart_dev;
|
2009-11-28 21:05:36 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
/* Get a handle for the device configuration */
|
|
|
|
usart_dev = find_usart_dev_by_id(usart);
|
|
|
|
PIOS_DEBUG_Assert(usart_dev);
|
2009-11-28 21:05:36 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
/* Check if RXNE flag is set */
|
|
|
|
if (usart_dev->cfg->regs->SR & USART_SR_RXNE) {
|
|
|
|
uint8_t b = usart_dev->cfg->regs->DR;
|
2009-11-28 21:05:36 +01:00
|
|
|
|
2010-06-13 06:24:26 +02:00
|
|
|
if (PIOS_USART_RxBufferPut(usart, b) < 0) {
|
|
|
|
/* Here we could add some error handling */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if TXE flag is set */
|
|
|
|
if (usart_dev->cfg->regs->SR & USART_SR_TXE) {
|
|
|
|
if (PIOS_USART_TxBufferUsed(usart) > 0) {
|
|
|
|
int32_t b = PIOS_USART_TxBufferGet(usart);
|
|
|
|
|
|
|
|
if(b < 0) {
|
|
|
|
/* Here we could add some error handling */
|
|
|
|
usart_dev->cfg->regs->DR = 0xff;
|
|
|
|
} else {
|
|
|
|
usart_dev->cfg->regs->DR = b & 0xff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Disable TXE interrupt (TXEIE=0) */
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, DISABLE);
|
|
|
|
}
|
|
|
|
}
|
2010-01-24 14:03:13 +01:00
|
|
|
}
|
2010-02-28 07:47:49 +01:00
|
|
|
|
|
|
|
#endif
|
2010-07-16 07:31:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|