Skip to content

Commit f522e1e

Browse files
author
Alexander Pertsch
committed
[USART] add Rx timeout handling to USART interrupt handler and functions for receiver timeout handling
1 parent c198703 commit f522e1e

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

peripheral/usart_6089/templates/plib_usart.c.ftl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ void ${USART_INSTANCE_NAME}_InterruptHandler( void )
210210
}
211211
}
212212
<#else>
213+
/* Receiver timeout */
214+
if (${USART_INSTANCE_NAME}_REGS->US_CSR & US_CSR_USART_TIMEOUT_Msk)
215+
{
216+
if(${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutCallback != NULL)
217+
{
218+
${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutCallback(${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutContext);
219+
}
220+
else
221+
{
222+
/* clear and disable timer to avoid re-raising the timout, reset should be controlled by rxTimeoutCallback */
223+
${USART_INSTANCE_NAME}_ClearReadTimeout();
224+
}
225+
}
226+
213227
/* Receiver status */
214228
if (${USART_INSTANCE_NAME}_REGS->US_CSR & US_CSR_USART_RXRDY_Msk)
215229
{
@@ -567,6 +581,13 @@ void ${USART_INSTANCE_NAME}_ReadCallbackRegister( USART_CALLBACK callback, uintp
567581
${USART_INSTANCE_NAME?lower_case}Obj.rxContext = context;
568582
}
569583

584+
void ${USART_INSTANCE_NAME}_ReadTimeoutCallbackRegister( USART_CALLBACK callback, uintptr_t context )
585+
{
586+
${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutCallback = callback;
587+
588+
${USART_INSTANCE_NAME?lower_case}Obj.rxTimeoutContext = context;
589+
}
590+
570591
bool ${USART_INSTANCE_NAME}_WriteIsBusy( void )
571592
{
572593
return ${USART_INSTANCE_NAME?lower_case}Obj.txBusyStatus;
@@ -611,4 +632,46 @@ size_t ${USART_INSTANCE_NAME}_ReadCountGet( void )
611632
</#if>
612633
}
613634

635+
636+
void ${USART_INSTANCE_NAME}_StartReadTimeoutAfterNextChar(uint32_t nbitperiods)
637+
{
638+
/* update timer value, this also starts the countdown */
639+
${USART_INSTANCE_NAME}_StartReadTimeoutNow(nbitperiods);
640+
641+
/* immediately cancel running countdown (hopefully we're fast enough), and wait for next char */
642+
${USART_INSTANCE_NAME}_REGS->US_CR = US_CR_USART_STTTO_Msk;
643+
}
644+
645+
void ${USART_INSTANCE_NAME}_StartReadTimeoutNow(uint32_t nbitperiods)
646+
{
647+
<#if USART_INTERRUPT_MODE_ENABLE == true>
648+
/* enable USART Rx timeout interrupt */
649+
${USART_INSTANCE_NAME}_REGS->US_IER = US_IER_USART_TIMEOUT_Msk;
650+
</#if>
651+
652+
/* update timer value, immediately starts timeout */
653+
${USART_INSTANCE_NAME}_REGS->US_RTOR = US_RTOR_TO(nbitperiods);
654+
}
655+
656+
void ${USART_INSTANCE_NAME}_RestartReadTimeoutAfterNextChar()
657+
{
658+
${USART_INSTANCE_NAME}_REGS->US_CR = US_CR_USART_STTTO_Msk;
659+
}
660+
661+
void ${USART_INSTANCE_NAME}_RestartReadTimeoutNow()
662+
{
663+
${USART_INSTANCE_NAME}_REGS->US_CR = US_CR_USART_RETTO_Msk;
664+
}
665+
666+
void ${USART_INSTANCE_NAME}_ClearReadTimeout( void )
667+
{
668+
/* reset timer value, stops running timer */
669+
${USART_INSTANCE_NAME}_REGS->US_RTOR = 0;
670+
671+
<#if USART_INTERRUPT_MODE_ENABLE == true>
672+
/* disable USART Rx timeout interrupt */
673+
${USART_INSTANCE_NAME}_REGS->US_IDR = US_IDR_USART_TIMEOUT_Msk;
674+
</#if>
675+
}
676+
614677
</#if>

peripheral/usart_6089/templates/plib_usart.h.ftl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ bool ${USART_INSTANCE_NAME}_Write( void *buffer, const size_t size );
7272

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

75+
void ${USART_INSTANCE_NAME}_StartReadTimeoutNow(uint32_t nbitperiods);
76+
77+
void ${USART_INSTANCE_NAME}_StartReadTimeoutAfterNextChar(uint32_t nbitperiods);
78+
79+
void ${USART_INSTANCE_NAME}_RestartReadTimeoutNow( void );
80+
81+
void ${USART_INSTANCE_NAME}_RestartReadTimeoutAfterNextChar( void );
82+
83+
void ${USART_INSTANCE_NAME}_ClearReadTimeout( void );
84+
7585
<#if USART_INTERRUPT_MODE_ENABLE == false>
7686
int ${USART_INSTANCE_NAME}_ReadByte( void );
7787

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

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

110+
void ${USART_INSTANCE_NAME}_ReadTimeoutCallbackRegister( USART_CALLBACK callback, uintptr_t context );
111+
100112
</#if>
101113

102114
bool ${USART_INSTANCE_NAME}_TransmitComplete( void );

peripheral/usart_6089/templates/plib_usart_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ typedef struct
138138
volatile size_t rxProcessedSize;
139139
USART_CALLBACK rxCallback;
140140
uintptr_t rxContext;
141+
USART_CALLBACK rxTimeoutCallback;
142+
uintptr_t rxTimeoutContext;
141143
bool rxBusyStatus;
142144

143145
volatile USART_ERROR errorStatus;

0 commit comments

Comments
 (0)