*ram usage optimization

+TX FIFO increased up to 100 slots
*allow driver to wait us if we have no more room in TX FIFO buffer ( useful for huge data stream what comes without delay )
This commit is contained in:
moonglow 2021-06-11 12:52:18 +03:00
parent 449dc73fa3
commit 43533b5b4a
7 changed files with 62 additions and 34 deletions

View File

@ -1227,8 +1227,8 @@ HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderType
(state == HAL_CAN_STATE_LISTENING))
{
/* Check that all the Tx mailboxes are not full */
if (((tsr & CAN_TSR_TME0) != 0U) ||
((tsr & CAN_TSR_TME1) != 0U) /*||
if (((tsr & CAN_TSR_TME0) != 0U) /* ||
((tsr & CAN_TSR_TME1) != 0U) ||
((tsr & CAN_TSR_TME2) != 0U)*/ )
{
/* Select an empty transmit mailbox */

View File

@ -195,7 +195,10 @@ hex: $(HEX_TARGET)
#######################################
clean:
-rm -fR $(BUILD_DIR)*
clean_obj:
-rm -f $(BUILD_DIR)*/*.o $(BUILD_DIR)*/*.d $(BUILD_DIR)*/*.lst
#######################################
# dependencies
#######################################

View File

@ -56,7 +56,7 @@ ENTRY(Reset_Handler)
_estack = 0x20001800; /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x000; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
_Min_Stack_Size = 0x200; /* required amount of stack */
/* Specify the memory areas */
MEMORY

View File

@ -5,7 +5,7 @@
#include "pcan_can.h"
#include "pcan_timestamp.h"
#define CAN_TX_FIFO_SIZE (32)
#define CAN_TX_FIFO_SIZE (100)
static CAN_HandleTypeDef g_hcan = { .Instance = CAN };
#define INTERNAL_CAN_IT_FLAGS ( CAN_IT_TX_MAILBOX_EMPTY |\
CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_RX_FIFO1_MSG_PENDING |\
@ -62,7 +62,7 @@ void pcan_can_init(void)
g_hcan.Init.AutoWakeUp = DISABLE;
g_hcan.Init.AutoRetransmission = ENABLE;
g_hcan.Init.ReceiveFifoLocked = DISABLE;
g_hcan.Init.TransmitFifoPriority = DISABLE;
g_hcan.Init.TransmitFifoPriority = ENABLE;
if( HAL_CAN_Init( &g_hcan ) != HAL_OK )
{
@ -173,7 +173,10 @@ static void pcan_can_flush_tx( void )
if( pcan_try_send_message( p_msg ) < 0 )
return;
/* update fifo index */
can_dev.tx_tail = (can_dev.tx_tail+1)&(CAN_TX_FIFO_SIZE-1);
uint32_t tail = can_dev.tx_tail+1;
if( tail == CAN_TX_FIFO_SIZE )
tail = 0;
can_dev.tx_tail = tail;
}
int pcan_can_send_message( const can_message_t *p_msg )
@ -181,16 +184,18 @@ int pcan_can_send_message( const can_message_t *p_msg )
if( !p_msg )
return 0;
uint32_t tx_head_next = (can_dev.tx_head+1)&(CAN_TX_FIFO_SIZE-1);
uint32_t head = can_dev.tx_head+1;
if( head == CAN_TX_FIFO_SIZE )
head = 0;
/* overflow ? just skip it */
if( tx_head_next == can_dev.tx_tail )
if( head == can_dev.tx_tail )
{
++can_dev.tx_ovfs;
return -1;
}
can_dev.tx_fifo[can_dev.tx_head] = *p_msg;
can_dev.tx_head = tx_head_next;
can_dev.tx_head = head;
return 0;
}
@ -218,9 +223,11 @@ void pcan_can_set_bus_active( uint16_t mode )
if( mode )
{
HAL_CAN_Start( &g_hcan );
HAL_CAN_AbortTxRequest( &g_hcan, CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2 );
}
else
{
HAL_CAN_AbortTxRequest( &g_hcan, CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2 );
HAL_CAN_Stop( &g_hcan );
}
}

View File

@ -50,7 +50,7 @@ pcan_device =
#define BLOCK_SIZE (64)
#define HEADER_SIZE (2)
#define PCAN_MAX_RECORD_SIZE (BLOCK_SIZE*16)
#define PCAN_MAX_RECORD_SIZE (BLOCK_SIZE*14)
typedef struct
{
/* raw data array */
@ -317,6 +317,7 @@ static void pcan_can_error( uint8_t err, uint8_t rx_err, uint8_t tx_err )
}
}
#define WAIT_FOR_TXSLOTS 1
static uint8_t pcan_decode_data_frame( uint8_t *ptr, uint16_t size, uint8_t flags )
{
can_message_t msg = { 0 };
@ -391,12 +392,31 @@ static uint8_t pcan_decode_data_frame( uint8_t *ptr, uint16_t size, uint8_t flag
pcan_led_set_mode( LED_CH0_TX, LED_MODE_BLINK_FAST, 237 );
#if WAIT_FOR_TXSLOTS
const uint16_t ts_poll = pcan_timestamp_ticks();
/* need more time to send data... ? */
while( pcan_can_send_message( &msg ) < 0 )
{
/* USB will get NACK and we will not miss other data */
pcan_can_poll();
uint16_t ts_diff = pcan_timestamp_ticks() - ts_poll;
/* we can't tramsit couse bus off or timeout ? */
if( ( pcan_device.can.err & PCAN_USB_ERROR_BUS_OFF ) ||
( ts_diff >= PCAN_TICKS_FROM_US( 1000000u ) ) )
{
/* tx buffer overflow, drop all data */
pcan_device.can.err |= PCAN_USB_ERROR_TXFULL;
return size;
}
}
#else
if( pcan_can_send_message( &msg ) < 0 )
{
/* tx queue overflow ? */
/* tx buffer overflow, drop all data */
pcan_device.can.err |= PCAN_USB_ERROR_TXFULL;
return size;
}
#endif
/* return back to PC */
if( srr_flag )
{

View File

@ -321,19 +321,18 @@ static uint8_t *device_get_device_qualifier( uint16_t *length )
static uint8_t *device_get_user_string( USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *length )
{
__ALIGN_BEGIN static uint8_t USBD_StrDesc[64] __ALIGN_END;
UNUSED( pdev );
switch( index )
if( index == 10 )
{
case 10:
USBD_GetString((uint8_t *)"PEAK-System Technik GmbH", USBD_StrDesc, length);
break;
default:
return 0;
__ALIGN_BEGIN static const uint16_t vendor_descriptor[1+24] __ALIGN_END =
{
0x0332,
'P','E','A','K','-','S','y','s','t','e','m',' ','T','e','c','h','n','i','k',' ','G','m','b','H'
};
*length = sizeof( vendor_descriptor );
return (uint8_t*)vendor_descriptor;
}
return USBD_StrDesc;
return 0;
}
USBD_ClassTypeDef usbd_pcan =

View File

@ -44,9 +44,6 @@ __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
HIBYTE(USBD_LANGID_STRING)
};
/* internal string descriptor */
__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
@ -65,20 +62,22 @@ uint8_t * USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
uint8_t * USBD_FS_HugeStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED( speed );
memset( USBD_StrDesc, 0x00, 254 );
*length = 252+2;
USBD_StrDesc[0] = ( 252 + 2 );
USBD_StrDesc[1] = USB_DESC_TYPE_STRING;
return USBD_StrDesc;
__ALIGN_BEGIN static const uint16_t huge_descriptor[1+126] __ALIGN_END = { 0x03FE };
*length = sizeof( huge_descriptor );
return (uint8_t*)huge_descriptor;
}
uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
USBD_GetString((uint8_t *)"PCAN-USB", USBD_StrDesc, length);
return USBD_StrDesc;
__ALIGN_BEGIN static const uint16_t cfg_descriptor[1+8] __ALIGN_END =
{
0x0312,
'P','C','A','N','-','U','S','B'
};
*length = sizeof( cfg_descriptor );
return (uint8_t*)cfg_descriptor;
}
USBD_DescriptorsTypeDef FS_Desc =