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

📄 dfuusart.c

📁 at91rm9200 的rom程序
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : dfuUsart.c
//* Object              : Low level functions for Usart Dfu
//*
//* 1.0 23/03/01 	    : Creation
//*----------------------------------------------------------------------------

#include <stdio.h>
#include "appli\includes\main.h"
#include "appli\includes\StructDfu.h"
#include "periph/usart2/lib_usart2.h"

//#define	MAX_SIZE_PDC	(64*1024)
#define	MAX_SIZE_PDC	10
#define DFU_TIMEOUT		1000

#if DFU_ENABLE

//*----------------------------------------------------------------------------
//* Function Name       : Send()
//* Object              : Send a buffer with the PDC
//* Input Parameters    : SBuffer
//* Output Parameters   : none
//*----------------------------------------------------------------------------
void SendUsart(struct _Speriph  *pPeriph)
{
	char *pData;
	unsigned int SizeRead;
	Usart2Desc *usart_pt = (Usart2Desc *)pPeriph->pPeriphDesc;

	pData = pPeriph->pBuffer->Read(pPeriph->pBuffer, &SizeRead);

	//* at91 send frame 
    //* Wait end PDC Transmission)
	while((usart_pt->usart_base->US_TCR) !=0);		// Add timeout

	if (SizeRead > MAX_SIZE_PDC)
		SizeRead = MAX_SIZE_PDC;
	
	//* send frame
	at91_usart2_send_frame(usart_pt, pData, SizeRead, 0, 0);

	//* Wait end PDC Transmission)
	while((usart_pt->usart_base->US_TCR) !=0);		// Add timeout

	pPeriph->pBuffer->free(pPeriph->pBuffer, SizeRead, 1);	
}



//*----------------------------------------------------------------------------
//* Function Name       : Receive()
//* Object              : Receive a buffer with the PDC
//* Input Parameters    : SBuffer
//* Output Parameters   : none
//*----------------------------------------------------------------------------
unsigned int ReceiveUsart(struct _Speriph *pPeriph)
{
	char *pData;
	unsigned int SizeWrite;
	unsigned int tick;
	unsigned int timeout = DFU_TIMEOUT;
	Usart2Desc *usart_pt = (Usart2Desc *)pPeriph->pPeriphDesc;

	pData = pPeriph->pBuffer->Write(pPeriph->pBuffer, &SizeWrite);

    if ( pData != (void *) NULL )
    {
    	//* Store the timeout value
    	usart_pt->usart_base->US_RTOR = (timeout * 10 / 4) ;

    	//* Restart the timeout logic
    	usart_pt->usart_base->US_CR = US_STTTO ;

		// dummy red to flush the receive register
		tick = usart_pt->usart_base->US_RHR;

    	//* Store the address of the buffer
    	usart_pt->usart_base->US_RPR = (unsigned int)pData;

    	//* Store the number of bytes to receive
    	usart_pt->usart_base->US_RCR = SizeWrite ;

    	//* Enables the receiver PDC transfer requests
    	usart_pt->usart_base->US_PTCR = US_RXTEN ;
	}
	tick = GetTickCount();
	//* wait end of reception
   	while ( ((usart_pt->usart_base->US_CSR & US_ENDRX ) != US_ENDRX )&& (GetTickCount() < tick + DFU_TIMEOUT) );

	if ((usart_pt->usart_base->US_CSR & US_ENDRX ) != US_ENDRX ) // Timeout escape
	{		
		SizeWrite -= usart_pt->usart_base->US_RCR;
	}
	pPeriph->pBuffer->free(pPeriph->pBuffer, SizeWrite, 0);
	return SizeWrite;
}


//*----------------------------------------------------------------------------
//* Function Name       : GetStatus()
//* Object              : Return the status of the usart
//* Input Parameters    : 
//* Output Parameters   : none
//*----------------------------------------------------------------------------
unsigned int GetStatusUsart(struct _Speriph *pPeriph)
{
	return (at91_usart2_get_status ((Usart2Desc *)pPeriph->pPeriphDesc));
}


#endif

⌨️ 快捷键说明

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