bool I2C::SlaveSendData(uint8_t* data, uint8_t data_size) { if (!WaitStatus(SLAVE_TRA_EV1, true)) { return false; } for (uint8_t i = 0; i < data_size; ++i) { if (!WaitStatus(SLAVE_TRA_EV3, true)) { return false; } SendByte (data[i]); } if (!WaitStatus(BTF, true)) { return false; } if (!WaitStatus(SLAVE_TRA_EV3_2, true)) { return false; } ClearAF(); return true; }
bool I2C::SlaveReceiveData(uint8_t* data, uint8_t data_size) { if (!WaitStatus(SLAVE_RECV_EV1, true)) { return false; } for (uint8_t i = 0; i < data_size; ++i) { if (!WaitStatus(SLAVE_RECV_EV2, true)) { return false; } data[i] = ReceiveByte(); } if (!WaitStatus(SLAVE_RECV_EV4, true)) { return false; } ClearSTOPF(); return true; }
bool I2C::_MasterWrite (uint8_t* data, uint32_t data_size, uint8_t slave_addr, uint8_t* preg_addr) { if (!WaitStatus(I2C::BUSY, false)) { //ClearBUSY(); _stuckBUSY= true; return false; } SendStart(true); if (!WaitStatus(I2C::MASTER_EV5, true)) return false; Send7bitAddress(slave_addr, true); if (!WaitStatus(I2C::MASTER_EV6_TRA, true)) //MSL | BUSY | ADDR | TXE | TRA return false; //////////////////////////////////////////////////////////////////////////// if (preg_addr) { SendByte(*preg_addr); if (!WaitStatus(I2C::MASTER_EV8_2, true)) //TRA, BUSY, MSL, TXE and BTF return false; } //////////////////////////////////////////////////////////////////////////// if (data_size == 1) { SendByte(*data); if (!WaitStatus(I2C::MASTER_EV8, true)) //TRA, BUSY, MSL, TXE and BTF return false; SendStop(true); return true; } else { return SendDMA (data, data_size); } }
bool I2C::_MasterRead (uint8_t* data, uint32_t data_size, uint8_t slave_addr, uint8_t* reg_addr) { if (!WaitStatus(I2C::BUSY, false)) { //ClearBUSY(); _stuckBUSY = true; return false; } if (reg_addr) { SendStart(true); if (!WaitStatus(I2C::MASTER_EV5, true)) //BSY MSL SB return false; Send7bitAddress(slave_addr, true); if (!WaitStatus(I2C::MASTER_EV6_TRA, true)) //MSL | BUSY | ADDR | TXE | TRA return false; SendByte(*reg_addr); if (!WaitStatus(I2C::MASTER_EV8_2, true)) //TRA, BUSY, MSL, TXE and BTF return false; } SendStart(true); if (!WaitStatus(I2C::MASTER_EV5, true)) //BSY MSL SB return false; Send7bitAddress(slave_addr, false); if (!WaitStatus(I2C::MASTER_EV6_RECV, true)) //MSL | BUSY | ADDR return false; if (data_size == 1) //if (false) { EnableACK(false); SendStop(true); // if (!WaitStatus(I2C::MASTER_EV7, true)) //MSL | BUSY | ADDR if (!WaitStatus(I2C::RXNE, true)) //MSL | BUSY | ADDR return false; *data = ReceiveByte(); EnableACK(true); return true; } else { return ReceiveDMA(data, data_size); } }
bool I2C::SendDMA (void* data_ptr, uint32_t data_size) { EnableDMA(true); DMA1* dma1 = DMA1::GetInstance(); if (!dma1->IsClockEnabled()) { dma1->EnablePeripheralClock(true); } DMA_Channel* dmaCh = dma1->GetChannel(_dmaTxChannel); dmaCh->SetPeripheralAddress ((uint32_t)_pI2C_DR); dmaCh->SetMemoryAddress((uint32_t)data_ptr); dmaCh->SetDirection_MemoryToPeripheral(); dmaCh->SetMemorySize_8bits(); dmaCh->SetPeripheralSize_8bits(); dmaCh->SetNumberOfData(data_size); dmaCh->SetMemoryIncrementMode(); dmaCh->SetPriorityHigh(); dmaCh->EnableChannel(true); bool rc = true; while (!dma1->IsTransferComplete(dmaCh)) { if (dma1->IsTransferError(dmaCh)) { rc = false; ++_errorDMATX; ++_errorCount; dma1->ClearTransferError(dmaCh); break; } } if (!WaitStatus(I2C::MASTER_EV8_2, true)) //TRA, BUSY, MSL, TXE and BTF rc = false; SendStop(true); dmaCh->EnableChannel(false); dma1->ClearTransferComplete(dmaCh); EnableDMA(false); return rc; }
//***************************************************************************** // Function : GetFile // Description : Get a file from host. // Input : aIdx // index of file to download // Return : 0/-1; // 0 => Success // // -1 => Error // Note : N/A // Globals Changed : N/A //***************************************************************************** static int GetFile(int aIdx) { T_APP_INFO app_list; int retry, j; int len, rx_len; BYTE auxname[30+1]; BYTE linea[30+1]; DWORD getfile_mark, rx_mark, which; int handle; TBuffers.gRxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos TBuffers.gTxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos memset(TBuffers.gRxBuf,0x00,COMM_BUFF_SIZE); memset(TBuffers.gTxBuf,0x00,COMM_BUFF_SIZE); for (retry=0; retry<NUM_OF_RETRY; retry++) { FlushAll (); which = MW_FTP_DATA_CHNL; IOCtlMW(gFtpHandle, IO_FTP_DISCONNECT, &which); FlushAll (); handle = os_file_open(KTempFile, K_O_RDWR|K_O_TRUNC|K_O_CREAT); if (handle < 0) { //ShowMsgWait("Temp File Open Fail.", TRUE); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return -1; } //SprintfMW ((char *)gTxBuf, "\x12\x1b\x20""Dnld %s..\x5", gFileList[aIdx]); //PrintfMW(gTxBuf); // clear_area_screen(4,2,7,20,FALSE,FALSE); sprintf ((char *)TBuffers.gTxBuf, "Descarga %.8s", gFileList[aIdx]); memset(auxname,0x00,sizeof(auxname)); memcpy(auxname,gFileList[aIdx],sizeof(auxname)); memset(linea, 0x00, sizeof(linea)); // printf("\f\x1C VERSIONES \x1D"); for( j = 0; j < 50; j++ ) { if(os_app_search_id(j) == TRUE) { os_app_info(j , &app_list); memset(linea,0x00,sizeof(linea)); sprintf(linea, "%s-%04X.tms", &app_list.s_app_name[2], app_list.w_checksum); //printf("\f %s \n %s\n %d",linea,gFileList[aIdx], gNumOfFiles);APM_WaitKey(3000,0); if(memcmp(linea,auxname,30)==0){ os_file_close(handle); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return 1; } } } //ShowMsgWait(TBuffers.gTxBuf, FALSE); // centerS(4,2,21,TBuffers.gTxBuf); SprintfMW ((char *)TBuffers.gTxBuf, "GET I /%s%s", STR_TEST_DIR_NAME, gFileList[aIdx]); if (IOCtlMW(gFtpHandle, IO_FTP_CMD_SEND, TBuffers.gTxBuf) == FALSE) { //ShowMsgWait("GET:Command Fail.", TRUE); break; } if (WaitStatus(MW_FTP_STAT_DATA_CONNED, TIMEOUT_STATUS) < 0) { graphicWindow("ERROR FTP"); // centerS(3,1,21,"Falla al"); // centerS(4,1,21,"Obtener los"); // centerS(5,1,21,"datos de FTP"); //ShowMsgWait("GET:Fail.", TRUE); break; } gRxSpeed = 0; rx_len = 0; getfile_mark = FreeRunMark(); while (1) { DWORD timeout=0; int ret; ret = GetCmdResp(); if (ret >= 400) { PrintfMW("\x1B\xE0\x1CGET:%d\x05\x1D", ret); break; } rx_mark = FreeRunMark(); do { len = ReadMW(gFtpHandle, TBuffers.gRxBuf, COMM_BUFF_SIZE); if (len > 0) { os_file_write(handle, TBuffers.gRxBuf, len); // save to file break; } if (WaitStatus(MW_FTP_STAT_DATA_CONNED, 1) < 0) { timeout = TIMEOUT_READ_BLOCK; break; } //SleepMW(); } while ((timeout = FreeRunElapsed(rx_mark)) < TIMEOUT_READ_BLOCK && GetCharMW() != MWKEY_CANCL); if (GetCharMW() == MWKEY_CANCL) break; if (timeout >= TIMEOUT_READ_BLOCK) // End ? break; rx_len += len; printf("\fRx: %3d.%d K.bytes",rx_len/1000,(rx_len%1000)/100); //PrintfMW("\x1B\x80""RXed %d bytes\x05\n\x05\n\x05", rx_len); } os_file_close(handle); if (rx_len == 0) { graphicWindow("ERROR FTP"); // clear_area_screen(2,2,7,20,FALSE,FALSE); // centerS(4,1,21,"RX Timeout."); ; os_file_close(handle); continue; } os_file_delete(gFileList[aIdx]); os_file_rename(gFileList[aIdx], KTempFile); which = MW_FTP_DATA_CHNL; IOCtlMW(gFtpHandle, IO_FTP_DISCONNECT, &which); // Close Data channel gRxSpeed = (rx_len/(FreeRunElapsed(getfile_mark)/1000)); //PrintfMW("\x1B\xA0Rx Spd:%d Bps\n", gRxSpeed); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return 0; } os_file_close(handle); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return -1; }
//***************************************************************************** // Function : GetListFile // Description : Get list file from host. // Input : N/A // Return : 0/-1; // 0 => Success // // -1 => Error // Note : N/A // Globals Changed : N/A //***************************************************************************** static int GetListFile(void) { int retry; int len, rx_len; int i, state, line_idx; DWORD getfile_mark, rx_mark, which; TBuffers.gRxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos TBuffers.gTxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos memset(TBuffers.gRxBuf,0x00,COMM_BUFF_SIZE); memset(TBuffers.gTxBuf,0x00,COMM_BUFF_SIZE); for (retry=0; retry<NUM_OF_RETRY; retry++) { FlushAll (); which = MW_FTP_DATA_CHNL; IOCtlMW(gFtpHandle, IO_FTP_DISCONNECT, &which); FlushAll (); graphicWindow("DESCARGANDO INFO"); printf("\n\nArchivo %s",STR_LIST_FILE_NAME); //SprintfMW ((char *)gTxBuf, "\x12\x1b\x20""Dnld %s..\x5", STR_LIST_FILE_NAME); //PrintfMW(gTxBuf); SprintfMW ((char *)TBuffers.gTxBuf, "GET I /%s%s", STR_TEST_DIR_NAME, STR_LIST_FILE_NAME); if (IOCtlMW(gFtpHandle, IO_FTP_CMD_SEND, TBuffers.gTxBuf) == FALSE) { // ShowMsgWait("GET:Command Fail.", TRUE); break; } if (WaitStatus(MW_FTP_STAT_DATA_CONNED, TIMEOUT_STATUS) < 0) { // centerS(5,1,21,"Fallo de datos"); DispLineMW("Fallo de datos",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); // getch(0); WaitKey(300,1); break; } gNumOfFiles = 0; gRxSpeed = 0; rx_len = 0; getfile_mark = FreeRunMark(); while (1) { DWORD timeout=0; int ret; ret = GetCmdResp(); if (ret >= 400) { PrintfMW("\x1B\xE0\x1CGET:%d\x05\x1D", ret); break; } state = S_IDLE; line_idx = 0; rx_mark = FreeRunMark(); do { len = ReadMW(gFtpHandle, TBuffers.gRxBuf, COMM_BUFF_SIZE); if (len) { for (i=0; i<len; i++) { // Process data if ((TBuffers.gRxBuf[i] == 0x0d) || (TBuffers.gRxBuf[i] == 0x0a)) { if (line_idx) { gLineBuf[line_idx] = 0; line_idx = 0; TrimLineBuf(); state = ProcGetListFileState(state); } } else if (line_idx < SIZE_LINE) { gLineBuf[line_idx] = TBuffers.gRxBuf[i]; line_idx++; } } break; } if (WaitStatus(MW_FTP_STAT_DATA_CONNED, 1) < 0) { timeout = TIMEOUT_READ_BLOCK; break; } if (GetCharMW() == MWKEY_CANCL) break; //SleepMW(); } while ((timeout = FreeRunElapsed(rx_mark)) < TIMEOUT_READ_BLOCK); if (timeout >= TIMEOUT_READ_BLOCK) // End ? break; rx_len += len; printf("\fRx: %3d.%d K.bytes",rx_len/1000,(rx_len%1000)/100); //dumpMemory(TBuffers.gRxBuf,rx_len,1); //printf("\f%s",TBuffers.gRxBuf);getch(0); //PrintfMW("\x1B\x80""RXed %d bytes\x05\n\x05", rx_len); if (GetCharMW() == MWKEY_CANCL) break; } if (rx_len == 0) { graphicWindow("ERROR FTP"); //clear_area_screen(2,2,7,20,FALSE); // centerS(4,1,21,"RX Timeout."); WaitKey(300,1); continue; } which = MW_FTP_DATA_CHNL; IOCtlMW(gFtpHandle, IO_FTP_DISCONNECT, &which); // Close Data channel gRxSpeed = (rx_len/(FreeRunElapsed(getfile_mark)/1000)); //PrintfMW("\x1B\xA0Rx Spd:%d Bps\n", gRxSpeed); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gRxBuf); return 0; } FreeMW(TBuffers.gTxBuf); FreeMW(TBuffers.gTxBuf); return -1; }
//***************************************************************************** // Function : FtpOpen // Description : Open & Login // Input : aCfg; // TCP port info. // aLoginName; // Login name // aLoginPassword; // password. // Return : 0/-1; // 0 => Success // // -1 => Error // Note : N/A // Globals Changed : N/A //***************************************************************************** static int FtpOpen(struct MW_TCP_PORT *aCfg, char *aLoginName, char *aLoginPassword) { int i; // Close all CloseAll(); TBuffers.gRxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos TBuffers.gTxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos memset(TBuffers.gRxBuf,0x00,COMM_BUFF_SIZE); memset(TBuffers.gTxBuf,0x00,COMM_BUFF_SIZE); // Open Port graphicWindow("CONEXION FTP"); DispLineMW("Conectando...",MW_LINE3,MW_CLRDISP|MW_CENTER|MW_BIGFONT); FlushAll (); if (IOCtlMW(gFtpHandle, IO_FTP_CONNECT, aCfg) == FALSE) { graphicWindow("ERROR"); //centerS(3,1,21,"Fallo al intentar"); DispLineMW("Fallo al intentar",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("Abrir!!",MW_LINE5,MW_CLRDISP|MW_CENTER|MW_SMFONT); // centerS(4,1,21,"Abrir!!"); Delay10ms(300); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return -1; } i = WaitCmdResp(TIMEOUT_RESP); if (i < 0) { graphicWindow("ERROR"); // centerS(3,1,21,"El tiempo de espera"); // centerS(4,1,21,"se agoto al"); // centerS(5,1,21,"intentat abrir!!"); DispLineMW("El tiempo de espera",MW_LINE3,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("se agoto al",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("intentat abrir!!",MW_LINE5,MW_CLRDISP|MW_CENTER|MW_SMFONT); // getch(300); Delay10ms(300); FreeMW(TBuffers.gTxBuf); FreeMW(TBuffers.gRxBuf); return -1; } if (i >= 400) { PrintfMW("\x1B\xE0""Open:%d\x05\x1D", i); FreeMW(TBuffers.gRxBuf); return -1; } if (WaitStatus(MW_FTP_STAT_CONNECTED, TIMEOUT_STATUS)<0) { graphicWindow("ERROR"); // centerS(3,1,21,"Fallo al intentar"); // centerS(4,1,21,"Abrir!!"); // getch(300); DispLineMW("Fallo al intentar",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("Abrir 2!!",MW_LINE5,MW_CLRDISP|MW_CENTER|MW_SMFONT); Delay10ms(300); FreeMW(TBuffers.gRxBuf);FreeMW(TBuffers.gTxBuf); return -1; } // Login DispLineMW("Negociando...",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); //centerS(4,1,21,"Negociando..."); FlushAll(); SprintfMW ((char *)TBuffers.gTxBuf, "LOGIN %s %s", aLoginName, aLoginPassword); if (IOCtlMW(gFtpHandle, IO_FTP_CMD_SEND, TBuffers.gTxBuf) == FALSE) { graphicWindow("ERROR"); DispLineMW("Fallo al intentar",MW_LINE3,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("Login",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); // centerS(3,1,21,"Fallo al intentar"); // centerS(4,1,21,"login!!"); // getch(300); Delay10ms(300); FreeMW(TBuffers.gRxBuf);FreeMW(TBuffers.gTxBuf); return -1; } i = WaitCmdResp(TIMEOUT_RESP); if (i < 0) { graphicWindow("ERROR"); // centerS(3,1,21,"El tiempo de espera"); // centerS(4,1,21,"se agoto al"); // centerS(5,1,21,"intentat login!!"); DispLineMW("El tiempo de espera",MW_LINE3,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("se agoto al",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("intentat login!!",MW_LINE5,MW_CLRDISP|MW_CENTER|MW_SMFONT); // getch(300); Delay10ms(300); FreeMW(TBuffers.gRxBuf);FreeMW(TBuffers.gTxBuf); return -1; } if (i >= 400) { graphicWindow("ERROR"); // centerS(3,1,21,"Fallo al intentar"); // centerS(4,1,21,"login!!"); // getch(300); DispLineMW("Fallo al intentar",MW_LINE3,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("login!!",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); Delay10ms(300); FreeMW(TBuffers.gRxBuf);FreeMW(TBuffers.gTxBuf); return -1; } if (WaitStatus(MW_FTP_STAT_OPENED, TIMEOUT_STATUS) < 0) { graphicWindow("ERROR"); // centerS(3,1,21,"Fallo al intentar"); // centerS(4,1,21,"login!!"); // getch(300); DispLineMW("Fallo al intentar",MW_LINE3,MW_CLRDISP|MW_CENTER|MW_SMFONT); DispLineMW("login!!",MW_LINE4,MW_CLRDISP|MW_CENTER|MW_SMFONT); Delay10ms(300); FreeMW(TBuffers.gRxBuf);FreeMW(TBuffers.gTxBuf); return -1; } return 0; }