我必须通过UART读取firmware.bin文件,文件大小为50,000字节。我编写了代码,但在读取文件时显示了垃圾值。
我如何读取大小为50,000字节的firmware.bin文件??我使用ARM cortex控制器进行编程。
i能够读取大小小于150字节的.txt文件,但是当文件大小大于150字节时,其读取垃圾值。我的任务是读取50,000字节的二进制文件.bin
void read file()
{
UART1_TxString("AT+QFOPEN=\"RAM:FIRMWARE.BIN\",2\r");
//2:only read for opend file.
WaitForExpectedResponse("+QFOPEN:",1000); // timeout 1000ms
//in response +QFOPEN: 134072 filehandler
UART1_TxString("AT+QFREAD=134072\r");
connect = WaitForConnectResponse("CONNECT",60000); // timeout 60000ms
// in response CONNECT 50000 (i.e.filesize 50,000 byte)
while(connect)
{
int i=0,j=0;
char* param = strchr(UART1Buffer, 'T') + (strlen(size_buff)+2);
// UART1Buffer its UART-Buffer of size 160.
// size_buff store file size to be read in string (50000)
// size_int store file size to be read in integer(50000)
for(i=0;i<size_int;i++){
UART2_Printf(" %c ",*param);// print UART-BUFFER-DATA
}
}
}
void UART1_IRQHandler ( void )
{
uint8_t IIRValue, LSRValue;
uint8_t Dummy = Dummy;
IIRValue = LPC_UART1->IIR;
IIRValue >>= 1; /* skip pending bit in IIR */
IIRValue &= 0x07; /* check bit 1~3, interrupt identification */
if ( IIRValue == IIR_RLS ) /* Receive Line Status */
{
LSRValue = LPC_UART1->LSR;
if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
{
UART1Status = LSRValue;
Dummy = LPC_UART1->RBR; /* Dummy read on RX to clear
interrupt, then bail out */
return;
}
if ( LSRValue & LSR_RDR ) /* Receive Data Ready */
{
UART1Buffer[UART1Count] = LPC_UART1->RBR;
UART1Count++;
if ( UART1Count == BUFSIZE ) // BUFSIZE= 160
{
UART1Count = 0; /* buffer overflow */
}
}
}
else if ( IIRValue == IIR_RDA ) /* Receive Data Available */
{
UART1Buffer[UART1Count] = LPC_UART1->RBR;
UART1Count++;
if ( UART1Count == BUFSIZE )
{
UART1Count = 0; /* buffer overflow */
}
}
}
要缓冲在其他情况下(在这种情况下,在ISR和“正常”线程之间,将在异步上下文中异步读取的数据,您应该使用FIFO或环形缓冲区,以便ISR可以在继续写操作时继续写入该缓冲区普通线程正在读取和处理它。例如,给定:
#include <stdint.h>
#include <stdatomic.h>
#define BUFFLEN ((uint8_t)128u) // Must be a power of 2
#define BUFFMODMASK ((uint8_t)(BUFFLEN - 1)) // modulo BUFFLEN mask
typedef volatile struct
{
uint8_t write_idx ;
uint8_t read_idx ;
atomic_int count ;
uint8_t data[BUFFLEN] ;
} tRingBuf ;
void ringBufPut( tRingBuf* buffer, uint8_t ch )
{
if( buffer->count < BUFFLEN )
{
buffer->data[buffer->write_idx++] = ch ;
buffer->write_idx &= BUFFMODMASK ;
buffer->count++ ;
}
}
int ringBufGet( tRingBuf* buffer )
{
int ch = -1 ;
if( buffer->count > 0 )
{
ch = (int)buffer->data[buffer->read_idx++] ;
buffer->read_idx &= BUFFMODMASK ;
buffer->count-- ;
}
return ch ;
}
tRingBuf UART1RxBuffer = { 0, 0, 0, {0} } ;
然后,您的ISR将数据放入缓冲区:
ringBufPut( &UART1RxBuffer, LPC_UART1->RBR ) ;
然后您的主线程可以读取数据:
for( int i = 0; i < size_int; i++ )
{
int ch = ringBufGet( &UART1RxBuffer ) ;
if( ch > 0 )
{
uint8_t byte = (uint8_t)ch ;
// ... do something with the data here
// For example...
UART2_Printf( "%02X", (unsigned)byte ) ;
}
}
您可能会为所有UART以相同的方式(反转放置/获取)来实现UART Tx,您的ISR目前仅处理Rx。完全缓冲的I / O将导致更多确定性的时序。