1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

com: fragment transmitted buffers to match underlying device

PIOS_COM_SendBufferNonBlocking() will now fragment its buffer
to match the max size of the underlying device.

This allows the buffer size of the underlying device to shrink
below the maximum message size, thus allowing us to use smaller
buffers on memory-constrained platforms.
This commit is contained in:
Stacey Sheldon 2011-12-26 17:20:29 -05:00
parent 407bec0c95
commit 80e0be3cec

View File

@ -272,7 +272,7 @@ int32_t PIOS_COM_ChangeBaud(uint32_t com_id, uint32_t baud)
* \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
* \return number of bytes transmitted on success
*/
int32_t PIOS_COM_SendBufferNonBlocking(uint32_t com_id, const uint8_t *buffer, uint16_t len)
{
@ -302,7 +302,7 @@ int32_t PIOS_COM_SendBufferNonBlocking(uint32_t com_id, const uint8_t *buffer, u
}
}
return (0);
return (bytes_into_fifo);
}
/**
@ -312,7 +312,7 @@ int32_t PIOS_COM_SendBufferNonBlocking(uint32_t com_id, const uint8_t *buffer, u
* \param[in] buffer character buffer
* \param[in] len buffer length
* \return -1 if port not available
* \return 0 on success
* \return number of bytes transmitted on success
*/
int32_t PIOS_COM_SendBuffer(uint32_t com_id, const uint8_t *buffer, uint16_t len)
{
@ -325,25 +325,46 @@ int32_t PIOS_COM_SendBuffer(uint32_t com_id, const uint8_t *buffer, uint16_t len
PIOS_Assert(com_dev->has_tx);
int32_t rc;
do {
rc = PIOS_COM_SendBufferNonBlocking(com_id, buffer, len);
uint32_t max_frag_len = fifoBuf_getSize(&com_dev->tx);
uint32_t bytes_to_send = len;
while (bytes_to_send) {
uint32_t frag_size;
#if defined(PIOS_INCLUDE_FREERTOS)
if (rc == -2) {
if (bytes_to_send > max_frag_len) {
frag_size = max_frag_len;
} else {
frag_size = bytes_to_send;
}
int32_t rc = PIOS_COM_SendBufferNonBlocking(com_id, buffer, frag_size);
if (rc >= 0) {
bytes_to_send -= rc;
buffer += rc;
} else {
switch (rc) {
case -1:
/* Device is invalid, this will never work */
return -1;
case -2:
/* Device is busy, wait for the underlying device to free some space and retry */
/* Make sure the transmitter is running while we wait */
if (com_dev->driver->tx_start) {
(com_dev->driver->tx_start)(com_dev->lower_id,
fifoBuf_getUsed(&com_dev->tx));
}
#if defined(PIOS_INCLUDE_FREERTOS)
if (xSemaphoreTake(com_dev->tx_sem, portMAX_DELAY) != pdTRUE) {
return -3;
}
}
#endif
} while (rc == -2);
continue;
default:
/* Unhandled return code */
return rc;
}
}
}
return len;
}
/**