⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart_hisr.c

📁 手机底层驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
		 */  
		
		case DLE:

			if (!uart->dle_detected)
			{

				/*
				 * No DLE previously detected => 
				 * Skip the current byte and set the flag.
				 */

				uart->dle_detected = 1;
				bytes_to_process--;
				uart->rx_out++;
			}
			else
			{ /* if (uart->dle_detected) */
				if (uart->inframe)
				{

					/*
					 * DLE previously detected AND currently inside of a frame =>
					 * Copy the current byte in the output buffer, reset the flag
					 * and increase the frame length.
					 */

					uart->dle_detected = 0;
					bytes_to_process--;
					uart->frame_length++;
					*(buffer++) = *(uart->rx_out++);
					bytes_written++;
				}
				else
				{ /* if (!uart->inframe) */

					/*
					 * DLE previously detected AND currently outside of a frame =>
					 * Skip the current byte.
					 */

					bytes_to_process--;
					uart->rx_out++;
				}
			}

			break; /* case DLE */

			/*
			 * Current byte is STX.
			 */  

		case STX:
			if ((!uart->dle_detected) && (uart->inframe))
			{
				/*
				 * No DLE previously detected AND currently inside of a frame.
				 */
				if (uart->frame_length)
				{
					/*
					 * Frame length is not zero (End of Frame) => 
					 * Skip the current byte and set the flags (EOF).
					 */
					uart->inframe = 0;
					*eof_detected = 1;
					uart->frame_length = 0;
					bytes_to_process--;
					uart->rx_out++;
				}
				else
				{ /* if (!uart->frame_length) */

					/*
					 * Frame length is zero (STX followed by another STX =
					 * Synchro lost but start of a new frame) =>
					 * Skip the current byte and keep the flag set.
					 */

					bytes_to_process--;
					uart->rx_out++;
				}
			}

			else if ((!uart->dle_detected) && (!uart->inframe))
			{

				/*
				 * No DLE previously detected AND currently outside of a
				 * frame (Start of Frame) =>
				 * Skip the current byte and set the flag.
				 */

				uart->inframe = 1;
				bytes_to_process--;
				uart->rx_out++;
			}

			else if ((uart->dle_detected) && (uart->inframe))
			{

				/*
				 * DLE previously detected AND currently inside of a frame =>
				 * Copy the current byte in the output buffer, reset the flag
				 * and increase the frame length.
				 */

				uart->dle_detected = 0;
				bytes_to_process--;
				uart->frame_length++;
				*(buffer++) = *(uart->rx_out++);
				bytes_written++;
			}

			else if ((uart->dle_detected) && (!uart->inframe))
			{

				/*
				 * DLE previously detected AND currently outside of a frame =>
				 * Skip the current byte and reset the flag.
				 */

				uart->dle_detected = 0;
				bytes_to_process--;
				uart->rx_out++;
			}

			break; /* case STX */

			/*
			 * Current byte is neither DLE nor STX.
			 */  

		default:

			if (uart->inframe)
			{

				/*
				 * Currently inside of a frame =>
				 * Copy the current byte in the output buffer and increase
				 * the frame length.
				 */

				bytes_to_process--;
				uart->frame_length++;
				*(buffer++) = *(uart->rx_out++);
				bytes_written++;
			}

			else
			{ /* if (!uart->inframe) */

				/*
				 * Currently outside of a frame =>
				 * Skip the current byte.
				 */

				bytes_to_process--;
				uart->rx_out++;
			}

			break; /* default */
		}

		if (uart->rx_out == &(uart->rx_buffer[0]) + BUFFER_SIZE + 1)
			uart->rx_out = &(uart->rx_buffer[0]);
	}

	return(bytes_written);
}

							
/*--------------------------------------------------------------
 Name: UA_ReadNChars 
 Description: Reads N characters from the RX buffer.
 Parameter: 
          uart_id      : UART id.
          buffer       : buffer address where the characters are copied.
          chars_to_read: number of characters to read.
 Return: The number of bytes read.         
//--------------------------------------------------------------*/
SYS_UWORD32 UA_ReadNChars (T_tr_UartId uart_id,
							char *buffer,
							SYS_UWORD32 chars_to_read)
{
	SYS_UWORD32 chars_in_rx_buffer;
	SYS_UWORD32 chars_to_copy;
	SYS_UWORD32 chars_written;
	char        *rx_in;
	t_uart      *uart;

	uart = &(uart_parameter[uart_id]);

	/*
	 * A copy of the rx_in pointer is used because it may be updated by
	 * the interrupt handler.
	 * Get the number of bytes available in the RX buffer.
	 */

	rx_in = uart->rx_in;

	if (uart->rx_out <= rx_in)
		chars_in_rx_buffer = (SYS_UWORD32) (rx_in - uart->rx_out);
	else
		chars_in_rx_buffer = (SYS_UWORD32) (rx_in - uart->rx_out + BUFFER_SIZE + 1);

	/*
	 * No more bytes than those received may be written in the output buffer.
	 */

	if (chars_in_rx_buffer >= chars_to_read)
		chars_to_copy = chars_to_read;
	else
		chars_to_copy = chars_in_rx_buffer;

	chars_written = chars_to_copy;

	/*
	 * Write the received bytes in the output buffer.
	 */

	while (chars_to_copy)
	{

		*(buffer++) = *(uart->rx_out++);
		chars_to_copy--;

		if (uart->rx_out == &(uart->rx_buffer[0]) + BUFFER_SIZE + 1)
			uart->rx_out = &(uart->rx_buffer[0]);
	}
	
	return(chars_written);
}



/*******************************************************************************
 *
 *                           UA_EncapsulateNChars
 * 
 * Purpose  : Writes N characters in the TX FIFO in encapsulating them with 2
 *            STX bytes (one at the beginning and one at the end).
 *
 * Arguments: In : uart_id       : UART id.
 *                 buffer        : buffer address from which characters are
 *                                 written.
 *                 chars_to_write: number of chars to write.
 *            Out: none
 *
 * Returns  : Number of chars written.
 *
 * Warning: Parameters are not verified.
 *
 ******************************************************************************/

SYS_UWORD32 UA_EncapsulateNChars (T_tr_UartId uart_id,
								char *buffer,
								SYS_UWORD32 chars_to_write)
{
	SYS_UWORD32 chars_written;
	SYS_UWORD32 chars_in_tx_fifo;
	t_uart      *uart;

	chars_written = 0;
	uart = &(uart_parameter[uart_id]);
	
		chars_in_tx_fifo = 0;

		/*
		 * Check if the message has been already encapsulated.
		 */

		if (!uart->encapsulation_flag)
		{
			/*
			 * Write STX in the TX FIFO and set the flag.
			 */
			UA_WriteChar(uart_id,STX); 
			chars_in_tx_fifo++;
			uart->encapsulation_flag = 1;
		}

		/*
		 * Keep one char margin in the TX FIFO for the last STX.
		 */

		while ((chars_written < chars_to_write) &&
			   (chars_in_tx_fifo < (FIFO_SIZE-1)))
		{
			UA_WriteChar(uart_id,*(buffer++)); 
			chars_written++;
			chars_in_tx_fifo++;
		}

		/*
		 * Append STX byte at the end if the frame is complete.
		 */

		if (chars_written == chars_to_write)
		{

			/*
			 * Write STX in the TX FIFO and reset the flag.
			 */
			UA_WriteChar(uart_id,STX); 			
			uart->encapsulation_flag = 0;
		}

	return(chars_written);
}

/*--------------------------------------------------------------
 Name: bsp_Usart_rxCallback   
 Description: Copies data from BSP_USART buffer to RX buffer
 Parameter: 
           irq                :  if irq set to TRUE otherwise FALSE 
           reinstallPtr       :  Reinstall pointer for reinstall after each thresh-hold 
           cb                 :  Circular buffer for data manupulation.  
           numBytesAvailable  :  Number of byte availabe in circular buffer. 
           state              :  which state the UART is. 
           CommPort           :  which UART. 
 Return: None          
//--------------------------------------------------------------*/
static BspUsart_ResultCode 
bsp_Usart_rxCallback( Uint16                  irq,
                                    BspUsart_ReInstallMode *reinstallPtr, 
                                    BspUtil_CircBuf_Handle cb,                                    
                                    Uint16 	               numBytesAvailable, 
                                    BspUsart_State         state,
                                    BspUsart_Id            CommPort )
{
    Uint16 numRetrieved = 0;
	t_uart *uart;
	Uint8 uart_id;
    
	uart_id = CommPort-1;
	uart = &(uart_parameter[uart_id]);
  
	while( (numBytesAvailable > 0)  && (!RX_BUFFER_FULL (uart)) )
    {
        bspUtil_CircBuf_dequeueUint8( cb, (Uint8*)uart->rx_in++ );
		if (uart->rx_in == &(uart->rx_buffer[0]) + BUFFER_SIZE + 1)
			uart->rx_in = &(uart->rx_buffer[0]);        
        numBytesAvailable--;     
        numRetrieved ++;
    }

	if (uart->callback_function != NULL)
		(*(uart->callback_function)) ();
	
	*reinstallPtr = BSP_USART_REINSTALL_MODE_REINSTALL; 
	return numRetrieved;
}

/*--------------------------------------------------------------
 Name: UA_EnterSleep    
 Description: enable UART sleep mode.
 Parameter: 
          T_tr_UartId uart_id   
 Return: None          
//--------------------------------------------------------------*/
SYS_BOOL UA_EnterSleep (T_tr_UartId uart_id)
{
	uart_id += 1;
	bspUsart_enableSleepMode(uart_id,1);
	return TRUE;
}

/*--------------------------------------------------------------
 Name: //UA_WakeUp   
 Description: Wakes up UART after Deep Sleep.
 Parameter: 
          T_tr_UartId uart_id
 Return: None          
//--------------------------------------------------------------*/
void UA_WakeUp (T_tr_UartId uart_id)
{
	uart_id += 1;
	bspUsart_enableSleepMode(uart_id,0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -