diff --git a/peripheral/usart_6089/templates/plib_usart.c.ftl b/peripheral/usart_6089/templates/plib_usart.c.ftl index a9c73a69ba..08806c2da9 100644 --- a/peripheral/usart_6089/templates/plib_usart.c.ftl +++ b/peripheral/usart_6089/templates/plib_usart.c.ftl @@ -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) { @@ -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; @@ -611,4 +632,46 @@ size_t ${USART_INSTANCE_NAME}_ReadCountGet( void ) } + +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; + + + /* 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; + +} + \ No newline at end of file diff --git a/peripheral/usart_6089/templates/plib_usart.h.ftl b/peripheral/usart_6089/templates/plib_usart.h.ftl index 2ec3a740fc..ef01762e68 100644 --- a/peripheral/usart_6089/templates/plib_usart.h.ftl +++ b/peripheral/usart_6089/templates/plib_usart.h.ftl @@ -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 ); @@ -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 ); + bool ${USART_INSTANCE_NAME}_TransmitComplete( void ); diff --git a/peripheral/usart_6089/templates/plib_usart_common.h b/peripheral/usart_6089/templates/plib_usart_common.h index 817c440515..fbf401622a 100644 --- a/peripheral/usart_6089/templates/plib_usart_common.h +++ b/peripheral/usart_6089/templates/plib_usart_common.h @@ -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;