📄 lib_usart.c
字号:
//* : <priority> = the USART interrupt source priority
//* : <service_irq> = the address of the interrupt handler
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE (u_int enable_usart_irq ( u_int usart_id ,
u_int mask_irq ,
TypeUSARTHandler handler_pt ))
#ifdef CORPS
//* Begin
{
u_int mask ;
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* If at least one Receive Bit set in the mask
if (( mask = ( mask_irq & MASK_IRQ_RX) ) != 0 )
{
//* Save the Receive Mask
USARTHandlerTable[usart_id].RxMask = mask ;
//* Save the Receive Handler
USARTHandlerTable[usart_id].RxHandler = handler_pt ;
}
//* EndIf
//* If at least one Transmit Bit set in the mask
if (( mask = ( mask_irq & MASK_IRQ_TX )) != 0 )
{
//* Save the Transmit Mask
USARTHandlerTable[usart_id].TxMask = mask ;
//* Save the Transmit Handler
USARTHandlerTable[usart_id].TxHandler = handler_pt ;
}
//* EndIf
//* If at least one Error Bit set in the mask
if (( mask = ( mask_irq & MASK_IRQ_ERROR )) != 0 )
{
//* Save the Error Mask
USARTHandlerTable[usart_id].ErrorMask = mask ;
//* Save the Error Handler
USARTHandlerTable[usart_id].ErrorHandler = handler_pt ;
}
//* EndIf
//* Enable the interrupt on the USART
ConstUsart[usart_id].UsartBase->US_IER = mask_irq ;
//* Return True
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : disable_usart_irq
//* Object : Disable one or many USART Interrupt.
//* Input Parameters : <usart_id> = the USART to disable
//* : <mask_irq> = the mask of the interrupt to disable
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE (u_int disable_usart_irq ( u_int usart_id, u_int mask_irq ))
#ifdef CORPS
//* Begin
{
u_int mask ;
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Disable the interrupt on the USART
ConstUsart[usart_id].UsartBase->US_IDR = mask_irq ;
//* If at least one Receive Bit set in the mask
if (( mask = ( mask_irq & MASK_IRQ_RX )) != 0 )
{
//* Clear the Receive Mask
USARTHandlerTable[usart_id].RxMask &= ~mask ;
//* Default the Receive Handler to <no_usart_handler>
USARTHandlerTable[usart_id].RxHandler = no_usart_handler ;
}
//* EndIf
//* If at least one Transmit Bit set in the mask
if (( mask = ( mask_irq & MASK_IRQ_TX )) != 0 )
{
//* Clear the Transmit Mask
USARTHandlerTable[usart_id].TxMask &= ~mask ;
//* Default the Transmit Handler to <no_usart_handler>
USARTHandlerTable[usart_id].TxHandler = no_usart_handler ;
}
//* EndIf
//* If at least one Error Bit set in the mask
if (( mask = ( mask_irq & MASK_IRQ_ERROR )) != 0 )
{
//* Clear the Error Mask
USARTHandlerTable[usart_id].ErrorMask &= ~mask ;
//* Default the Error Handler to <no_usart_handler>
USARTHandlerTable[usart_id].ErrorHandler = no_usart_handler ;
}
//* EndIf
//* Return True
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : usart_command
//* Object : Send a command to the USART.
//* Input Parameters : <usart_id> = the USART to disable
//* : <mask_command> = the command mask
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE (u_int usart_command ( u_int usart_id , u_int mask_command ))
#ifdef CORPS
//* Begin
{
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Disable receiver and transmitter and stop any activity immediately
ConstUsart[usart_id].UsartBase->US_CR = mask_command ;
//* Return true
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : send_byte
//* Object : Transmit a character.
//* Input Parameters : <usart_id> = the USART where send the byte
//* : <character> = the character to send
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE ( u_int send_byte ( u_int usart_id , u_int character ))
#ifdef CORPS
//* Begin
{
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Wait the transmitter Ready signal
while (( ConstUsart[usart_id].UsartBase->US_CSR & TXRDY) == 0 ) ;
//* Write the character in the Transmit Holding Register
ConstUsart[usart_id].UsartBase->US_THR = character ;
//* Return true
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : receive_byte
//* Object : Receive a character.
//* Input Parameters : <usart_id> = the USART where send the byte
//* : <pt_char> = pointer where store the character received
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE ( u_int receive_byte ( u_int usart_id , u_int *pt_char ))
#ifdef CORPS
//* Begin
{
u_int timeout ;
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Initialise the timeout value
timeout = POLLING_READ_TIMEOUT ;
//* Wait the transmitter Ready signal
while ((( ConstUsart[usart_id].UsartBase->US_CSR & RXRDY) == 0 ) &&
( timeout-- > 0 )) ;
//* If no character received
if (( ConstUsart[usart_id].UsartBase->US_CSR & RXRDY) == 0 )
{
//* Return False
return ( FALSE ) ;
}
//* Read the received character and store it
*pt_char = ConstUsart[usart_id].UsartBase->US_RHR ;
//* Return True
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : send_frame
//* Object : Transmit a complete.
//* Input Parameters : <usart_id> = the USART where send the byte
//* : <pt_buffer> = the buffer to send
//* : <size_buf> = the number of bytes to send
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE ( u_int send_frame ( u_int usart_id ,
char *pt_buffer ,
u_int size_buf ))
#ifdef CORPS
//* Begin
{
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Wait for previous transfer finished
while (( ConstUsart[usart_id].UsartBase->US_CSR & ENDTX ) == 0 ) ;
//* Store the address of the buffer
ConstUsart[usart_id].UsartBase->US_TPR = (u_int) pt_buffer ;
//* Store the number of bytes to transmit
ConstUsart[usart_id].UsartBase->US_TCR = size_buf ;
//* Return true
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : receive_frame
//* Object : Receive a complete frame.
//* Input Parameters : <usart_id> = the USART where receive the byte
//* : <pt_buffer> = the address of the receive buffer
//* : <max_size> = the maximum number of bytes to be
//* : received
//* : <timeout> = the inter-character time delay in number
//* : of byte
//* Output Parameters : True if <usart_id> is correct
//* Functions called : none
//*-----------------------------------------------------------------------------
_REFERENCE ( u_int receive_frame ( u_int usart_id ,
char *pt_buffer ,
u_int max_size ,
u_int timeout ))
#ifdef CORPS
//* Begin
{
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Store the timeout value
ConstUsart[usart_id].UsartBase->US_RTOR = (timeout * 10 / 4) ;
//* Restart the timeout logic
ConstUsart[usart_id].UsartBase->US_CR = STTTO ;
//* Store the address of the buffer
ConstUsart[usart_id].UsartBase->US_RPR = (u_int) pt_buffer ;
//* Store the number of bytes to transmit
ConstUsart[usart_id].UsartBase->US_RCR = max_size ;
//* Return true
return ( TRUE ) ;
}
//* End
#endif
//*P
//*-----------------------------------------------------------------------------
//* Function Name : get_rx_count
//* Object : Get remaining size of buffer in reception
//* Input Parameters : <usart_id> = the USART to disable
//* : <pt_size> = pointer where store the byte number
//* Output Parameters : True if <usart_id> is correct
//* Functions called : None
//*-----------------------------------------------------------------------------
_REFERENCE (u_int get_rx_count ( u_int usart_id , u_int *pt_size ))
#ifdef CORPS
//* Begin
{
//* If Usart index is too high, return False
if ( usart_id > NB_USART ) return ( FALSE ) ;
//* Get the remaining number of bytes
*pt_size = ConstUsart[usart_id].UsartBase->US_RCR ;
//* Return true
return ( TRUE ) ;
}
//* End
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -