2009-11-30 17:03:37 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file pios_com.c
|
2010-01-31 18:56:54 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2010-03-10 10:12:31 +01:00
|
|
|
* Parts by Thorsten Klose (tk@midibox.org)
|
2009-11-30 17:03:37 +01:00
|
|
|
* @brief COM layer functions
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
* @defgroup PIOS_COM COM layer functions
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Project Includes */
|
|
|
|
#include "pios.h"
|
|
|
|
|
2010-03-11 13:38:51 +01:00
|
|
|
#if defined(PIOS_INCLUDE_COM)
|
2009-11-30 17:03:37 +01:00
|
|
|
|
|
|
|
|
2010-03-06 04:13:14 +01:00
|
|
|
/* Global Variables */
|
2009-11-30 17:03:37 +01:00
|
|
|
|
|
|
|
/* Local Variables */
|
|
|
|
static int32_t (*receive_callback_func)(COMPortTypeDef port, char c);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-03-06 04:13:14 +01:00
|
|
|
* Initialises COM layer
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] mode currently only mode 0 supported
|
|
|
|
* \return < 0 if initialisation failed
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_Init(void)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
|
|
|
/* Disable callback by default */
|
|
|
|
receive_callback_func = NULL;
|
|
|
|
|
|
|
|
/* If any COM assignment: */
|
2010-03-13 07:44:15 +01:00
|
|
|
#if defined(PIOS_INCLUDE_USART)
|
2009-12-08 03:13:21 +01:00
|
|
|
PIOS_USART_Init();
|
2010-03-13 07:44:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_USB_HID)
|
2010-02-10 04:31:51 +01:00
|
|
|
PIOS_USB_HID_Init(0);
|
2010-03-13 07:44:15 +01:00
|
|
|
#endif
|
2009-11-30 17:03:37 +01:00
|
|
|
|
2010-01-23 18:31:14 +01:00
|
|
|
return ret;
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a package over given port
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] buffer character buffer
|
|
|
|
* \param[in] len buffer length
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return -2 if non-blocking mode activated: buffer is full
|
|
|
|
* caller should retry until buffer is free again
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendBufferNonBlocking(COMPortTypeDef port, uint8_t *buffer, uint16_t len)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
|
|
|
/* Branch depending on selected port */
|
|
|
|
switch(port) {
|
2010-02-12 11:11:17 +01:00
|
|
|
case COM_DEBUG_USART:
|
2010-02-28 09:29:14 +01:00
|
|
|
return PIOS_USART_TxBufferPutMoreNonBlocking(PIOS_COM_DEBUG_PORT, buffer, len);
|
2010-03-04 08:07:27 +01:00
|
|
|
case COM_USART1:
|
|
|
|
return PIOS_USART_TxBufferPutMoreNonBlocking(USART_1, buffer, len);
|
|
|
|
case COM_USART2:
|
|
|
|
return PIOS_USART_TxBufferPutMoreNonBlocking(USART_2, buffer, len);
|
|
|
|
case COM_USART3:
|
|
|
|
return PIOS_USART_TxBufferPutMoreNonBlocking(USART_3, buffer, len);
|
2010-02-10 04:31:51 +01:00
|
|
|
case COM_USB_HID:
|
|
|
|
return PIOS_USB_HID_TxBufferPutMoreNonBlocking(buffer, len);
|
2009-11-30 17:03:37 +01:00
|
|
|
default:
|
|
|
|
/* Invalid port */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a package over given port
|
|
|
|
* (blocking function)
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] buffer character buffer
|
|
|
|
* \param[in] len buffer length
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendBuffer(COMPortTypeDef port, uint8_t *buffer, uint16_t len)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
|
|
|
/* Branch depending on selected port */
|
|
|
|
switch(port) {
|
2010-02-12 11:11:17 +01:00
|
|
|
case COM_DEBUG_USART:
|
2010-02-28 09:29:14 +01:00
|
|
|
return PIOS_USART_TxBufferPutMore(PIOS_COM_DEBUG_PORT, buffer, len);
|
2010-03-04 08:07:27 +01:00
|
|
|
case COM_USART1:
|
|
|
|
return PIOS_USART_TxBufferPutMore(USART_1, buffer, len);
|
|
|
|
case COM_USART2:
|
|
|
|
return PIOS_USART_TxBufferPutMore(USART_2, buffer, len);
|
|
|
|
case COM_USART3:
|
|
|
|
return PIOS_USART_TxBufferPutMore(USART_3, buffer, len);
|
2010-02-10 04:31:51 +01:00
|
|
|
case COM_USB_HID:
|
|
|
|
return PIOS_USB_HID_TxBufferPutMore(buffer, len);
|
2009-11-30 17:03:37 +01:00
|
|
|
default:
|
|
|
|
/* Invalid port */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a single character over given port
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] c character
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return -2 buffer is full
|
|
|
|
* caller should retry until buffer is free again
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendCharNonBlocking(COMPortTypeDef port, char c)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
2009-12-08 03:13:21 +01:00
|
|
|
return PIOS_COM_SendBufferNonBlocking(port, (uint8_t *)&c, 1);
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a single character over given port
|
|
|
|
* (blocking function)
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] c character
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendChar(COMPortTypeDef port, char c)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
2009-12-08 03:13:21 +01:00
|
|
|
return PIOS_COM_SendBuffer(port, (uint8_t *)&c, 1);
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a string over given port
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] str zero-terminated string
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return -2 buffer is full
|
|
|
|
* caller should retry until buffer is free again
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendStringNonBlocking(COMPortTypeDef port, char *str)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
2009-12-08 03:13:21 +01:00
|
|
|
return PIOS_COM_SendBufferNonBlocking(port, (uint8_t *)str, (uint16_t)strlen(str));
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a string over given port
|
|
|
|
* (blocking function)
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] str zero-terminated string
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendString(COMPortTypeDef port, char *str)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
2009-12-08 03:13:21 +01:00
|
|
|
return PIOS_COM_SendBuffer(port, (uint8_t *)str, strlen(str));
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a formatted string (-> printf) over given port
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] *format zero-terminated format string - 128 characters supported maximum!
|
|
|
|
* \param[in] ... optional arguments,
|
|
|
|
* 128 characters supported maximum!
|
|
|
|
* \return -2 if non-blocking mode activated: buffer is full
|
|
|
|
* caller should retry until buffer is free again
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendFormattedStringNonBlocking(COMPortTypeDef port, char *format, ...)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
|
|
|
uint8_t buffer[128]; // TODO: tmp!!! Provide a streamed COM method later!
|
2009-11-30 18:01:21 +01:00
|
|
|
|
2009-11-30 17:03:37 +01:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vsprintf((char *)buffer, format, args);
|
2009-12-08 03:13:21 +01:00
|
|
|
return PIOS_COM_SendBufferNonBlocking(port, buffer, (uint16_t)strlen((char *)buffer));
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a formatted string (-> printf) over given port
|
|
|
|
* (blocking function)
|
2010-03-06 04:13:14 +01:00
|
|
|
* \param[in] port COM port
|
2009-11-30 17:03:37 +01:00
|
|
|
* \param[in] *format zero-terminated format string - 128 characters supported maximum!
|
|
|
|
* \param[in] ... optional arguments,
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2009-12-08 03:13:21 +01:00
|
|
|
int32_t PIOS_COM_SendFormattedString(COMPortTypeDef port, char *format, ...)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
|
|
|
uint8_t buffer[128]; // TODO: tmp!!! Provide a streamed COM method later!
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vsprintf((char *)buffer, format, args);
|
2009-12-08 03:13:21 +01:00
|
|
|
return PIOS_COM_SendBuffer(port, buffer, (uint16_t)strlen((char *)buffer));
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-06 04:13:14 +01:00
|
|
|
* Transfer bytes from port buffers into another buffer
|
|
|
|
* \param[in] port COM port
|
|
|
|
* \returns Byte from buffer
|
2009-11-30 17:03:37 +01:00
|
|
|
*/
|
2010-03-06 04:13:14 +01:00
|
|
|
uint8_t PIOS_COM_ReceiveBuffer(COMPortTypeDef port)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
2010-03-06 04:13:14 +01:00
|
|
|
switch(port) {
|
2010-03-13 16:37:00 +01:00
|
|
|
case COM_DEBUG_USART:
|
|
|
|
return PIOS_USART_RxBufferGet(PIOS_COM_DEBUG_PORT);
|
2010-03-06 04:13:14 +01:00
|
|
|
case COM_USART1:
|
|
|
|
return PIOS_USART_RxBufferGet(USART_1);
|
|
|
|
case COM_USART2:
|
|
|
|
return PIOS_USART_RxBufferGet(USART_2);
|
|
|
|
case COM_USART3:
|
|
|
|
return PIOS_USART_RxBufferGet(USART_3);
|
|
|
|
case COM_USB_HID:
|
|
|
|
return PIOS_USB_HID_RxBufferGet();
|
|
|
|
/* To suppress warnings */
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-06 04:13:14 +01:00
|
|
|
* Get the number of bytes waiting in the buffer
|
|
|
|
* \param[in] port COM port
|
|
|
|
* \return Number of bytes used in buffer
|
2009-11-30 17:03:37 +01:00
|
|
|
*/
|
2010-03-06 04:13:14 +01:00
|
|
|
int32_t PIOS_COM_ReceiveBufferUsed(COMPortTypeDef port)
|
2009-11-30 17:03:37 +01:00
|
|
|
{
|
2010-03-06 04:13:14 +01:00
|
|
|
switch(port) {
|
2010-03-13 16:37:00 +01:00
|
|
|
case COM_DEBUG_USART:
|
|
|
|
return PIOS_USART_RxBufferUsed(PIOS_COM_DEBUG_PORT);
|
2010-03-06 04:13:14 +01:00
|
|
|
case COM_USART1:
|
|
|
|
return PIOS_USART_RxBufferUsed(USART_1);
|
|
|
|
case COM_USART2:
|
|
|
|
return PIOS_USART_RxBufferUsed(USART_2);
|
|
|
|
case COM_USART3:
|
|
|
|
return PIOS_USART_RxBufferUsed(USART_3);
|
|
|
|
case COM_USB_HID:
|
|
|
|
return PIOS_USB_HID_DATA_LENGTH;
|
|
|
|
/* To suppress warnings */
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-30 17:03:37 +01:00
|
|
|
}
|
2010-02-28 07:47:49 +01:00
|
|
|
|
|
|
|
#endif
|