// read a byte using the target monitor UINT8 ReadByte(tUartHandle serial_handle, UINT32 addr) { UINT8 send; UINT8 received; // send the address command send = ADDRESS; UartWrite(serial_handle, &send, 1); // transmit the address, big endian send = (UINT8)((addr>>24) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>16) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>8) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)(addr & 0xFF); UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != ADDRESS) { printf("Protocol error!\n"); return 1; } // send the read command send = BYTE_READ; UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response return received; }
// write many bytes using the target monitor int FlashByteMultiple(tUartHandle serial_handle, UINT32 addr, UINT32 size, UINT8* pBuffer) { UINT8 send, received; // send the address command send = ADDRESS; UartWrite(serial_handle, &send, 1); // transmit the address, big endian send = (UINT8)((addr>>24) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>16) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>8) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)(addr & 0xFF); UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != ADDRESS) { printf("Protocol error!\n"); return 1; } while (size) { if (size >= 16) { // we can use a "burst" command send = BYTE_FLASH16; UartWrite(serial_handle, &send, 1); // send the write command UartWrite(serial_handle, pBuffer, 16); // transmit the data UartRead(serial_handle, &received, 1); // response if (received != BYTE_FLASH16) { printf("Protocol error!\n"); return 1; } pBuffer += 16; size -= 16; } else { // use single byte command send = BYTE_FLASH; UartWrite(serial_handle, &send, 1); // send the write command UartWrite(serial_handle, pBuffer++, 1); // transmit the data UartRead(serial_handle, &received, 1); // response if (received != BYTE_FLASH) { printf("Protocol error!\n"); return 1; } size--; } } return 0; }
// transfer a byte for the monitor download, with or without acknowledge int DownloadByte(tUartHandle serial_handle, unsigned char byte, bool bAck) { unsigned char received; while (1) { UartWrite(serial_handle, &byte, 1); if (bAck) { UartRead(serial_handle, &received, 1); if (received == byte) { UartWrite(serial_handle, (UINT8*)"\x01", 1); // ack success break; // exit the loop } else { printf("Error transmitting monitor byte 0x%02X, got 0x%0X\n", byte, received); UartWrite(serial_handle, (UINT8*)"\x00", 1); // ack fail, try again } } else break; // no loop } return 1; }
// change baudrate using target monitor int SetTargetBaudrate(tUartHandle serial_handle, long lClock, long lBaudrate) { UINT8 send; UINT8 received; UINT8 brr; long lBRR; lBRR = lClock / lBaudrate; lBRR = ((lBRR + 16) / 32) - 1; // with rounding brr = (UINT8)lBRR; // send the command send = BAUDRATE; UartWrite(serial_handle, &send, 1); UartWrite(serial_handle, &brr, 1); // send the BRR value UartRead(serial_handle, &received, 1); // response ack if (received != BAUDRATE) { // bad situation, now we're unclear about the baudrate of the target printf("Protocol error!\n"); return 1; } SLEEP(100); // give it some time to settle // change our baudrate, too UartConfig(serial_handle, lBaudrate, eNOPARITY, eONESTOPBIT, 8); return 0; }
// write a 16bit halfword using the target monitor int WriteHalfword(tUartHandle serial_handle, UINT32 addr, UINT16 halfword) { UINT8 send; UINT8 received; // send the address command send = ADDRESS; UartWrite(serial_handle, &send, 1); // transmit the address, big endian send = (UINT8)((addr>>24) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>16) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>8) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)(addr & 0xFF); UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != ADDRESS) { printf("Protocol error!\n"); return 1; } // send the write command send = HALFWORD_WRITE; UartWrite(serial_handle, &send, 1); // transmit the data send = halfword >> 8; // highbyte UartWrite(serial_handle, &send, 1); send = halfword & 0xFF; // lowbyte UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != HALFWORD_WRITE) { printf("Protocol error!\n"); return 1; } return 0; }
/* ** 向串口读写数据. ** @buf: 发送与接收数据缓冲区 ** @bufSize: 缓冲区长度 */ U8 logic_sendAndRead(U8* buf, U16* bufSize, U32 timeout) { UartWrite(buf, *bufSize, timeout, gpu); *bufSize = UartRead(buf, 100, timeout, gpu); if (*bufSize == 0) {//如果超时后没有读到数据, 返回错误 return ERROR; } return NO_ERR; }
// write a byte using the target monitor int WriteByte(tUartHandle serial_handle, UINT32 addr, UINT8 byte) { UINT8 send; UINT8 received; // send the address command send = ADDRESS; UartWrite(serial_handle, &send, 1); // transmit the address, big endian send = (UINT8)((addr>>24) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>16) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>8) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)(addr & 0xFF); UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != ADDRESS) { printf("Protocol error, receiced 0x%02X!\n", received); return 1; } // send the write command send = BYTE_WRITE; UartWrite(serial_handle, &send, 1); // transmit the data UartWrite(serial_handle, &byte, 1); UartRead(serial_handle, &received, 1); // response if (received != BYTE_WRITE) { printf("Protocol error!\n"); return 1; } return 0; }
// call a subroutine using the target monitor int Execute(tUartHandle serial_handle, UINT32 addr, bool bReturns) { UINT8 send; UINT8 received; // send the address command send = ADDRESS; UartWrite(serial_handle, &send, 1); // transmit the address, big endian send = (UINT8)((addr>>24) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>16) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>8) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)(addr & 0xFF); UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != ADDRESS) { printf("Protocol error!\n"); return 1; } // send the execute command send = EXECUTE; UartWrite(serial_handle, &send, 1); if (bReturns) { // we expect the call to return control to minimon UartRead(serial_handle, &received, 1); // response if (received != EXECUTE) { printf("Protocol error!\n"); return 1; } } return 0; }
// read a 16bit halfword using the target monitor UINT16 ReadHalfword(tUartHandle serial_handle, UINT32 addr) { UINT8 send; UINT8 received; UINT16 halfword; // send the address command send = ADDRESS; UartWrite(serial_handle, &send, 1); // transmit the address, big endian send = (UINT8)((addr>>24) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>16) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)((addr>>8) & 0xFF); UartWrite(serial_handle, &send, 1); send = (UINT8)(addr & 0xFF); UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response if (received != ADDRESS) { printf("Protocol error!\n"); return 1; } // send the read command send = HALFWORD_READ; UartWrite(serial_handle, &send, 1); UartRead(serial_handle, &received, 1); // response halfword = received << 8; // highbyte UartRead(serial_handle, &received, 1); halfword |= received; // lowbyte return halfword; }
/*----------------------------------------------------------------------------* * NAME * ConfigureUart * * DESCRIPTION * This function configures the UART Baud rate. A TRUE value in the * parameter means that high baud rate is to be configured and a FALSE * means that low baud rate is to be configured. * * RETURNS * Nothing * *----------------------------------------------------------------------------*/ extern void ConfigureUart(bool b_high) { if(b_high) { /* Configure the UART for high baud rate */ UartConfig(HIGH_BAUD_RATE,0); /* Enable the UART back */ UartEnable(TRUE); /* Read from UART */ UartRead(1,0); /* Disable deep sleep, so characters dont go missing */ SleepModeChange(sleep_mode_never); /* Setup the variable to indicate the current baud rate is high */ g_is_current_baud_rate_high = TRUE; } else { /* Configure the UART for high baud rate */ UartConfig(LOW_BAUD_RATE,0); /* Enable the UART back */ UartEnable(TRUE); /* Read from UART */ UartRead(1,0); /* Enable deep sleep */ SleepModeChange(sleep_mode_deep); /* Setup the variable to indicate the current baud rate is low */ g_is_current_baud_rate_high = FALSE; } }
void UartProcessData(u8 port,u8* data,u8* cmd) { u8 ch; static u8 state=SOP_STATE; static u8 LEN_Token ; while (Uart_RxBufLen(port)) { UartRead (port, &ch); switch (state) { case SOP_STATE: if (ch == UART_SOF) state = LEN_STATE; printf("1"); break; case LEN_STATE: LEN_Token = ch; state = CMD_STATE; printf("2"); break; case CMD_STATE: *cmd = ch; if (LEN_Token) { state = SOP_STATE; } else { state = SOP_STATE; } printf("3"); break; case DATA_STATE: state = SOP_STATE; break; case FCS_STATE: state = SOP_STATE; break; default: break; } } }
// send a sting and check the echo int SendWithEcho(tUartHandle serial_handle, char* pszSend) { int i = 0; unsigned char received; while(pszSend[i] != '\0') { UartWrite(serial_handle, (unsigned char*)(pszSend + i), 1); // send char do { UartRead(serial_handle, &received, 1); // receive echo printf("%c", received); // debug } while (received != pszSend[i]); // should normally be equal i++; // next char } return 0; }
// wait for a fixed string to be received (no foolproof algorithm, // may overlook if the searched string contains repeatitions) int WaitForString(tUartHandle serial_handle, char* pszWait) { int i = 0; unsigned char received; while(pszWait[i] != '\0') { UartRead(serial_handle, &received, 1); printf("%c", received); // debug if (received == pszWait[i]) i++; // continue else i=0; // mismatch, start over } return 0; }
/* Echo back recevied data in upper case */ int UartTest1( IN const char *pszPort ) { int Retval; uhandle_t hUart; char Buff[256]; unsigned int BytesRead; unsigned int BytesWritten; DBG_MSG(DBG_TRACE, "%s\n", __FUNCTION__); Retval = UartCtor(&hUart); CHECK_RETVAL(Retval, ExitOnFailure); Retval = UartOpen(hUart, pszPort, UART_RATE_57600, UART_DATA_BITS_8, UART_PARITY_NONE, UART_STOP_1); CHECK_RETVAL(Retval, ExitOnFailure); for ( ; ; ) { Retval = UartRead(hUart, Buff, sizeof(Buff), &BytesRead, 1000); if (BytesRead) { UartTestToUpper(Buff, BytesRead); Retval = UartWrite(hUart, Buff, BytesRead, &BytesWritten, 1000); CHECK_RETVAL(Retval, ExitOnFailure); } } ExitOnFailure: UartDtor(hUart); return Retval; }
static void log_loop(tUartHandle hUart) { double last, curr; int count = 0; uint8_t msg[1000]; last = hires_time(); do { double waited; uint8_t receive; int got = 1; UartRead(hUart, &receive, &got); curr = hires_time(); waited = curr - last; last = curr; if (got == 0) continue; // should never happen if (waited > 0.01) { if (count) { dump(msg, count); printf(" (stale bytes)"); count = 0; } printf("\n"); // block separator, for nicer readability } msg[count] = receive; count++; if (count >= 4) // minimum packet length { int i; // CRC candidate uint16_t cand = msg[count-2] + msg[count-1] * 256; // little endian // search for size + CRC match, assume packet end if both matches for (i=0; i<count-2; i++) { int packetlen = msg[i+1] & 0x7F; // from msg candidate if (packetlen == count-i-4 && crc16(msg+i, count-2-i) == cand) { if (i>0) { // dump heading garbage dump(msg, i); printf(" Garbage (possible collision)\n"); } { // print timestamp char *timeline; #ifdef WIN32 struct _timeb timebuffer; _ftime( &timebuffer ); #else struct timeb timebuffer; ftime( &timebuffer ); #endif timeline = ctime( & ( timebuffer.time ) ); printf( "%.8s.%03hu ", &timeline[11], timebuffer.millitm); } dump (msg+i, count-i); // dump the packet decode_packet(msg+i, count-i); printf("\n"); //out_rcv_packet(&out, msg+i, count-i); // pass to emulation count = 0; } // if crc match } // for i } // if (count > 2) } while(1); }