Skip to content

Commit

Permalink
[USART] add Rx timeout handling to USART interrupt handler and functi…
Browse files Browse the repository at this point in the history
…ons for receiver timeout handling
  • Loading branch information
Alexander Pertsch committed Jul 6, 2022
1 parent c198703 commit f522e1e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
63 changes: 63 additions & 0 deletions peripheral/usart_6089/templates/plib_usart.c.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ void ${USART_INSTANCE_NAME}_InterruptHandler( void )
}
}
<#else>
/* Receiver timeout */
if (${USART_INSTANCE_NAME}_REGS->US_CSR & US_CSR_USART_TIMEOUT_Msk)
{
if(${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutCallback != NULL)
{
${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutCallback(${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutContext);
}
else
{
/* clear and disable timer to avoid re-raising the timout, reset should be controlled by rxTimeoutCallback */
${USART_INSTANCE_NAME}_ClearReadTimeout();
}
}

/* Receiver status */
if (${USART_INSTANCE_NAME}_REGS->US_CSR & US_CSR_USART_RXRDY_Msk)
{
Expand Down Expand Up @@ -567,6 +581,13 @@ void ${USART_INSTANCE_NAME}_ReadCallbackRegister( USART_CALLBACK callback, uintp
${USART_INSTANCE_NAME?lower_case}Obj.rxContext = context;
}

void ${USART_INSTANCE_NAME}_ReadTimeoutCallbackRegister( USART_CALLBACK callback, uintptr_t context )
{
${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutCallback = callback;

${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutContext = context;
}

bool ${USART_INSTANCE_NAME}_WriteIsBusy( void )
{
return ${USART_INSTANCE_NAME?lower_case}Obj.txBusyStatus;
Expand Down Expand Up @@ -611,4 +632,46 @@ size_t ${USART_INSTANCE_NAME}_ReadCountGet( void )
</#if>
}


void ${USART_INSTANCE_NAME}_StartReadTimeoutAfterNextChar(uint32_t nbitperiods)
{
/* update timer value, this also starts the countdown */
${USART_INSTANCE_NAME}_StartReadTimeoutNow(nbitperiods);

/* immediately cancel running countdown (hopefully we're fast enough), and wait for next char */
${USART_INSTANCE_NAME}_REGS->US_CR = US_CR_USART_STTTO_Msk;
}

void ${USART_INSTANCE_NAME}_StartReadTimeoutNow(uint32_t nbitperiods)
{
<#if USART_INTERRUPT_MODE_ENABLE == true>
/* enable USART Rx timeout interrupt */
${USART_INSTANCE_NAME}_REGS->US_IER = US_IER_USART_TIMEOUT_Msk;
</#if>

/* update timer value, immediately starts timeout */
${USART_INSTANCE_NAME}_REGS->US_RTOR = US_RTOR_TO(nbitperiods);
}

void ${USART_INSTANCE_NAME}_RestartReadTimeoutAfterNextChar()
{
${USART_INSTANCE_NAME}_REGS->US_CR = US_CR_USART_STTTO_Msk;
}

void ${USART_INSTANCE_NAME}_RestartReadTimeoutNow()
{
${USART_INSTANCE_NAME}_REGS->US_CR = US_CR_USART_RETTO_Msk;
}

void ${USART_INSTANCE_NAME}_ClearReadTimeout( void )
{
/* reset timer value, stops running timer */
${USART_INSTANCE_NAME}_REGS->US_RTOR = 0;

<#if USART_INTERRUPT_MODE_ENABLE == true>
/* disable USART Rx timeout interrupt */
${USART_INSTANCE_NAME}_REGS->US_IDR = US_IDR_USART_TIMEOUT_Msk;
</#if>
}

</#if>
12 changes: 12 additions & 0 deletions peripheral/usart_6089/templates/plib_usart.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ bool ${USART_INSTANCE_NAME}_Write( void *buffer, const size_t size );

bool ${USART_INSTANCE_NAME}_Read( void *buffer, const size_t size );

void ${USART_INSTANCE_NAME}_StartReadTimeoutNow(uint32_t nbitperiods);

void ${USART_INSTANCE_NAME}_StartReadTimeoutAfterNextChar(uint32_t nbitperiods);

void ${USART_INSTANCE_NAME}_RestartReadTimeoutNow( void );

void ${USART_INSTANCE_NAME}_RestartReadTimeoutAfterNextChar( void );

void ${USART_INSTANCE_NAME}_ClearReadTimeout( void );

<#if USART_INTERRUPT_MODE_ENABLE == false>
int ${USART_INSTANCE_NAME}_ReadByte( void );

Expand All @@ -97,6 +107,8 @@ void ${USART_INSTANCE_NAME}_WriteCallbackRegister( USART_CALLBACK callback, uint

void ${USART_INSTANCE_NAME}_ReadCallbackRegister( USART_CALLBACK callback, uintptr_t context );

void ${USART_INSTANCE_NAME}_ReadTimeoutCallbackRegister( USART_CALLBACK callback, uintptr_t context );

</#if>

bool ${USART_INSTANCE_NAME}_TransmitComplete( void );
Expand Down
2 changes: 2 additions & 0 deletions peripheral/usart_6089/templates/plib_usart_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ typedef struct
volatile size_t rxProcessedSize;
USART_CALLBACK rxCallback;
uintptr_t rxContext;
USART_CALLBACK rxTimeoutCallback;
uintptr_t rxTimeoutContext;
bool rxBusyStatus;

volatile USART_ERROR errorStatus;
Expand Down

0 comments on commit f522e1e

Please sign in to comment.