BOOL xMBPortSerialPoll( ) { BOOL bStatus = TRUE; DWORD dwBytesRead; DWORD dwBytesWritten; DWORD i; while( bRxEnabled ) { /* buffer wrap around. */ if( uiRxBufferPos >= BUF_SIZE ) uiRxBufferPos = 0; if( ReadFile( g_hSerial, &ucBuffer[uiRxBufferPos], BUF_SIZE - uiRxBufferPos, &dwBytesRead, NULL ) ) { if( dwBytesRead == 0 ) { /* timeout with no bytes. */ break; } else if( dwBytesRead > 0 ) { vMBPortLog( MB_LOG_DEBUG, _T( "SER-POLL" ), _T( "detected end of frame (t3.5 expired.)\r\n" ) ); for( i = 0; i < dwBytesRead; i++ ) { /* Call the modbus stack and let him fill the buffers. */ ( void )pxMBFrameCBByteReceived( ); } } } else { vMBPortLog( MB_LOG_ERROR, _T( "SER-POLL" ), _T( "I/O error on serial device: %s" ), Error2String( GetLastError ( ) ) ); bStatus = FALSE; } } if( bTxEnabled ) { while( bTxEnabled ) { ( void )pxMBFrameCBTransmitterEmpty( ); /* Call the modbus stack to let him fill the buffer. */ } dwBytesWritten = 0; if( !WriteFile ( g_hSerial, &ucBuffer[0], uiTxBufferPos, &dwBytesWritten, NULL ) || ( dwBytesWritten != uiTxBufferPos ) ) { vMBPortLog( MB_LOG_ERROR, _T( "SER-POLL" ), _T( "I/O error on serial device: %s" ), Error2String( GetLastError ( ) ) ); bStatus = FALSE; } } return bStatus; }
/* Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ static void prvvUARTTxReadyISR( void ) { //mb.c eMBInit函数中 //pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM //发送状态机 pxMBFrameCBTransmitterEmpty( ); }
void prvvMBSerialIRQHandler( void ) { portENTER_SWITCHING_ISR( ); static BOOL xTaskWokenReceive = FALSE; static BOOL xTaskWokenTransmit = FALSE; static USHORT usStatus; usStatus = UART_FlagStatus( MB_UART_DEV ); if( prvMBPortTXIsEnabled( ) && ( usStatus & UART_TxHalfEmpty ) ) { xTaskWokenReceive = pxMBFrameCBTransmitterEmpty( ); } if( prvMBPortRXIsEnabled( ) && ( usStatus & UART_RxBufFull ) ) { xTaskWokenReceive = pxMBFrameCBByteReceived( ); } /* End the interrupt in the EIC. */ EIC->IPR |= 1 << EIC_CurrentIRQChannelValue( ); portEXIT_SWITCHING_ISR( ( xTaskWokenReceive || xTaskWokenTransmit ) ? pdTRUE : pdFALSE ); }
static void interrupt prvvUARTTxReadyISR( void ) { pxMBFrameCBTransmitterEmpty( ); IRQ0 &= ~UART0_TXD_INT_PENDING; }
/* Find out what interrupted and get or send data as appropriate */ void MB_USART_ISR(void) { /* Check if we were called because of RXNE. */ if (((USART_CR1(MB_USART) & USART_CR1_RXNEIE) != 0) && ((USART_SR(MB_USART) & USART_SR_RXNE) != 0)) { pxMBFrameCBByteReceived(); } /* Check if we were called because of TXE. */ if (((USART_CR1(MB_USART) & USART_CR1_TXEIE) != 0) && ((USART_SR(MB_USART) & USART_SR_TXE) != 0)) { pxMBFrameCBTransmitterEmpty(); /* Check if we need to disable transmitter*/ if(!txen) { USART_SR (MB_USART) &= ~USART_SR_TC; /* Clear TC flag*/ USART_CR1(MB_USART) |= USART_CR1_TCIE; /* Enable transfer complite interrupt*/ } } /* Disable transmitter on transfer comlite*/ if (((USART_CR1(MB_USART) & USART_CR1_TCIE) != 0) && ((USART_SR(MB_USART) & USART_SR_TC) != 0)) { USART_CR1(MB_USART) &= ~USART_CR1_TCIE;/* Disble transfer complite interrupt*/ USART_SR (MB_USART) &= ~USART_SR_TC; /* Clear TC flag*/ /* Disable transmitter*/ gpio_clear(MB_USART_TXEN_PORT, MB_USART_TXEN_PIN); } }
bool xMBPortSerialPoll(void) { bool bStatus = true; uint16_t usBytesRead; int i; while (bRxEnabled) { if (prvbMBPortSerialRead(&ucBuffer[0], BUF_SIZE, &usBytesRead)) { if (usBytesRead == 0) { /* timeout with no bytes. */ break; } else if (usBytesRead > 0) { for (i = 0; i < usBytesRead; i++) { /* Call the modbus stack and let him fill the buffers. */ (void)pxMBFrameCBByteReceived(); } uiRxBufferPos = 0; } } else { vMBPortLog(MB_LOG_ERROR, "SER-POLL", "read failed on serial device: %d\n", errno); bStatus = false; } } if (bTxEnabled) { while (bTxEnabled) { (void)pxMBFrameCBTransmitterEmpty(); /* Call the modbus stack to let him fill the buffer. */ } if (!prvbMBPortSerialWrite(&ucBuffer[0], uiTxBufferPos)) { vMBPortLog(MB_LOG_ERROR, "SER-POLL", "write failed on serial device: %d\n", errno); bStatus = false; } } return bStatus; }
/* Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ interrupt void prvvUARTTxReadyISR( void ) { static unsigned int uiCnt = 0; #if (TEST == TEST_TX) if(uiCnt++ < 10) xMBPortSerialPutByte('a'); else vMBPortSerialEnable(false,false); #elif (TEST == NO_TEST) pxMBFrameCBTransmitterEmpty( ); #endif PieCtrlRegs.PIEACK.all|=0x100; // Issue PIE ack }
virtual void run() { while (1) { if (m_serial) { if (m_serial->readable()) { pxMBFrameCBByteReceived(); } if (m_serial->writeable()) { pxMBFrameCBTransmitterEmpty(); } } } }
void __attribute__((__interrupt__, no_auto_psv)) _U1TXInterrupt() /*void IntUart2Handler(void)*/ { // // __asm__ volatile("btg PORTB,#9"); // TX interrupt if (_U1TXIF) { _U1TXIF = 0;; pxMBFrameCBTransmitterEmpty( ); // } }
void vUSARTHandler( void ) { uint32_t uiCSR; uint32_t uiIMR; BOOL bTaskWoken = FALSE; vMBPortSetWithinException( TRUE ); uiCSR = xUSARTHWMappings[ucUsedPort].pUsart->US_CSR; uiIMR = xUSARTHWMappings[ucUsedPort].pUsart->US_IMR; uint32_t uiCSRMasked = uiCSR & uiIMR; if( uiCSRMasked & US_CSR_RXRDY ) { bTaskWoken = pxMBFrameCBByteReceived( ); } if( uiCSRMasked & US_CSR_TXRDY ) { bTaskWoken = pxMBFrameCBTransmitterEmpty( ); } if( uiCSRMasked & US_CSR_TXEMPTY ) { if( NULL != xUSARTHWMappings[ucUsedPort].USARTDEPin ) { PIO_Clear( xUSARTHWMappings[ucUsedPort].USARTDEPin ); } if( NULL != xUSARTHWMappings[ucUsedPort].USARTNotREPin ) { PIO_Clear( xUSARTHWMappings[ucUsedPort].USARTNotREPin ); } USART_DisableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IER_TXEMPTY ); } vMBPortSetWithinException( FALSE ); portEND_SWITCHING_ISR( bTaskWoken ? pdTRUE : pdFALSE ); }
/* * Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ void prvvUARTTxReadyISR(void) { pxMBFrameCBTransmitterEmpty(); }