/* * CSwim.cpp * * Created on: 30 Jan 2018 * Author: user */ #include "CSwim.h" // ------------------------------------------- // ------------------------------------------- CSwim::ErrorCode CSwim::checkNumBytes(uint32_t p_uNumBytes) const { return (p_uNumBytes > numBytesInTransmitBlock()) ? STM8_too_Many_bytes : OK; } // ------------------------------------------- // ------------------------------------------- CSwim::ErrorCode CSwim::stm8SwimConfig(uint32_t p_uAddr, uint8_t *p_pyData, uint32_t p_uNumBytes) { CSwim::ErrorCode err = checkNumBytes(p_uNumBytes); if (err == OK) { if (p_uNumBytes == numBytesInTransmitBlock()) { if (!transmitBlock(p_pyData, p_uNumBytes, p_uAddr))// send as a block { #ifdef DEBUG fprintf(stderr, "CSwim::stm8SwimConfig - failed to transmit a block of %u bytes to %x\n", p_uNumBytes, p_uAddr); #endif err = SwimTransmitByte; } } else if (p_uNumBytes < numBytesInTransmitBlock()) { // TODO // clear SWIM state - CSR_SWIM_CONTROL::RESET_STATE // clear SWIM ctrl errors - CSR_SWIM_CONTROL::CLEAR_ERRORS // set low speed - CSR_SWIM_CONTROL::HIGH_SPEED doResetState(); doClearErrors(); for (uint32_t currByte = 0, currAddrs = p_uAddr; currByte < p_uNumBytes; ++currByte, ++currAddrs) { if (!transmitByte(p_pyData[currByte ], currAddrs)) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8SwimConfig - failed to transmit byte number %u of %u bytes\n", currByte, currAddrs); #endif err = SwimTransmitByte; break; } } } #ifdef DEBUG else { fprintf(stderr, "CSwim::stm8SwimConfig - Write size of %u cannot be greater than (%u)\n", p_uNumBytes, numBytesInTransmitBlock()); } #endif } return err; } // ------------------------------------------- // ------------------------------------------- CSwim::ErrorCode CSwim::stm8SwimRead(uint32_t p_uAddr, uint8_t *p_pyData, uint32_t p_uNumBytes) { CSwim::ErrorCode err = OK; if (err == OK) { // TODO // clear SWIM state - CSR_SWIM_CONTROL::RESET_STATE // clear SWIM ctrl errors - CSR_SWIM_CONTROL::CLEAR_ERRORS // set low speed - CSR_SWIM_CONTROL::HIGH_SPEED doResetState(); doClearErrors(); for (uint32_t currByte = 0, currAddrs = p_uAddr; currByte < p_uNumBytes; ++currByte, ++currAddrs) { if (!readByte(p_pyData[currByte], currAddrs)) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8SwimRead - failed to read byte number %u of %u bytes\n", currByte, p_uNumBytes); #endif err = SwimReceiveByte; break; } } } return err; } // ------------------------------------------- // ------------------------------------------- CSwim::ErrorCode CSwim::openSwimInterface() { std::vector vxSTM8; // Enable SWIM interface for access to slave's registers vxSTM8.clear(); uint8_t val = swimCtrlRegInitVal(); vxSTM8.push_back(val); CSwim::ErrorCode err = stm8SwimConfig(swimCtrlRegAddr(), vxSTM8.data(), vxSTM8.size()); return err; }; // ------------------------------------------- // ------------------------------------------- CSwim::ErrorCode CSwim::openStm8ForProgramming() { std::vector vxSTM8; std::array axRevbytes; CSwim::ErrorCode err = OK; // Open the SWIM channel before we can access internal registers err = openSwimInterface(); if (err != OK) { err = SwimTransmitByte; return err; } vxSTM8.clear(); // Stall the processor uint8_t val = stm8Csr2StallCpu(); vxSTM8.push_back(val); err = stm8SwimConfig(swimCsr2(), vxSTM8.data(), vxSTM8.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::openStm8ForProgramming - error disabling CPU in SWIM_CSR2\n"); #endif err = SwimTransmitByte; return err; } // Unlock programming FLASH by writing the two key values to the PUKR register in succession vxSTM8[0] = stm8FlashKey1(); // First key value err = stm8SwimConfig(flashPukrAddr(), vxSTM8.data(), vxSTM8.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::openStm8ForProgramming - error writing key1 to FLASH_PUKR\n"); #endif err = SwimTransmitByte; return err; } vxSTM8[0] = stm8FlashKey2(); // Second key value err = stm8SwimConfig(flashPukrAddr(), vxSTM8.data(), vxSTM8.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::openStm8ForProgramming - error writing key2 to FLASH_PUKR\n"); #endif err = SwimTransmitByte; return err; } // Read back flash status err = stm8SwimRead(flashIapsrAddr(), axRevbytes.data(), axRevbytes.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::openStm8ForProgramming - error reading FLASH_IAPSR\n"); #endif err = SwimReceiveByte; return err; } // The PUL bit of the IAPSR register should be set to indicate that the programming flash is now unlocked if ((axRevbytes[0] & pulBitMask()) == 0) { #ifdef DEBUG fprintf(stderr, "CSwim::openStm8ForProgramming - error reading 0 at PUL bit (program flash unlocked) from FLASH_IAPSR\n"); #endif err = STM8_flash_enable_fail; return err; } return err; }; // ------------------------------------------- // ------------------------------------------- CSwim::ErrorCode CSwim::stm8ProgramFlash(const char* imageFile_str, bool verify) { std::vector vxSTM8; std::array axRevbytes; CSwim::ErrorCode err = OK; // Fixed size buffer, matching max size of flash char wrBufArray[flashSize()]; // // Open file for reading // std::ifstream rdStream (imageFile_str, std::ifstream::in | std::ifstream::binary); if (!rdStream.is_open()) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8ProgramFlash - error opening file %s\n", imageFile_str); #endif err = SwimFileOpen; } if (err == OK) { // // Check the files size isn't greater than flash size // // Get length of file: rdStream.seekg (0, rdStream.end); uint32_t length = rdStream.tellg(); rdStream.seekg (0, rdStream.beg); if (length >flashSize()) { err = STM8_image_too_big; rdStream.close(); } // Read file into buffer rdStream.read(wrBufArray, length); // // Program the buffer contents to flash // // Calculate the whole number of programming blocks required, rounded up uint32_t numBlks = (length + numBytesInTransmitBlock() - 1)/numBytesInTransmitBlock(); #ifdef DEBUG fprintf(stderr, "***Program the STM8 device.\n"); #endif // Prep the device for programming err = openStm8ForProgramming(); if (err != OK) { return err; } // Loop for the required number of blocks for (uint32_t faddr = flashAddr(), idx = 0; faddr < (flashAddr() + (numBlks * numBytesInTransmitBlock())); faddr += numBytesInTransmitBlock(), idx += numBytesInTransmitBlock()) { // Empty the vector buffer for filling with program data vxSTM8.clear(); // Fill in a block's worth of data from the program data in the buffer for (uint8_t y = 0; y < numBytesInTransmitBlock(); ++y) { vxSTM8.push_back(wrBufArray[y+idx]); } // Program the block to flash err = stm8SwimConfig(faddr, vxSTM8.data(), vxSTM8.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8ProgramFlash - failed to write block to flash at address 0x%04x\n", faddr); #endif err = STM8_block_write_failed; break; } } // // If requested, verify the flash contents // if (err == OK && verify) { #ifdef DEBUG fprintf(stderr, "***Verifying the STM8 device.\n"); #endif // Loop through the flash... for (uint32_t faddr = flashAddr(), idx = 0; faddr < (flashAddr() + (numBlks * numBytesInTransmitBlock())); faddr ++, idx++) { // Read a byte from flash err = stm8SwimRead(faddr, axRevbytes.data(), axRevbytes.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8ProgramFlash - failed to read byte from flash at address 0x%04x\n", faddr); #endif err = SwimReceiveByte; break; } if (err == OK) { // Check whether the flash byte matches that in the buffer if (wrBufArray[idx] != axRevbytes[0]) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8ProgramFlash - error reading FLASH block at addr 0x%04x. Expected 0x%02x, got 0x%02x\n", faddr, wrBufArray[idx], axRevbytes[0]); #endif err = STM8_flash_verify_fail; } } } } if (err == OK) { // Re-enable the CPU (TBD: in a 'close()' method? What about re-locking flash again?) uint8_t val = 0; vxSTM8.clear(); vxSTM8.push_back(val); err = stm8SwimConfig(swimCsr2(), vxSTM8.data(), vxSTM8.size() ); if (err != OK) { #ifdef DEBUG fprintf(stderr, "CSwim::stm8ProgramFlash - error re-enabling STM8 CPU\n"); #endif err = SwimTransmitByte; return err; } } } return err; }; // ------------------------------------------- // ------------------------------------------- bool CSwim::transmitBlock(uint8_t* p_pxyVal, uint8_t p_yNumBytes /* ==128 */, uint32_t p_STM8addr /* lower 24bits only */) { bool bRval = !p_status->GetTxError(); // check Iin error bool bFlshErr = false; if (bRval) { // Program the FLASH_CR2 register for block programming transmitByte(0x01, flashCr2Addr()); //txRetryTimeoutCtrl(100); //sendEntrySeqCtrl(); while (p_status->GetTxbusy() && !p_status->GetTxError()) // wait until we can send { usleep(10); } if (!p_status->GetTxError()) { doClearErrors(); // Program the address p_txBlock->SetTxBlock(p_STM8addr << CSR_SWIM_TX_BLOCK_WADDR); // Wait for command to complete while (p_status->GetTxbusy() && !p_status->GetTxError()) { usleep(1); } if (!p_status->GetTxError()) { for (uint8_t yByte = 0; yByte < p_yNumBytes; ++yByte) { doClearErrors(); // Initially we'll just retry until a byte is sent, but we may need to r p_pushByte->SetPushByte(p_pxyVal[yByte]); while (p_status->GetTxbusy() && !p_status->GetTxError()) { usleep(1); } //CDebug::log(CDebug::log_ERR, "blockRemaining %d.\n", *TxBlockByteRemainingPtr()); if (p_status->GetTxError()) { #ifdef DEBUG fprintf(stderr, "***CSR_SWIM_STATUS 0x%02x\n", p_status->GetStatus()); #endif break; } } // If no error, wait for EOP event in FLASH_IAPSR if (!p_status->GetTxError()) { uint8_t yFlashRegVal; bool bStatus; uint32_t uTimeoutCount = 10000; // TODO do { usleep(1); --uTimeoutCount; bStatus = readByte(yFlashRegVal, flashIapsrAddr()); } while(bStatus && ((yFlashRegVal & eopBitMask()) == 0) && (uTimeoutCount != 0)); // check we didn't timeout if (uTimeoutCount == 0) { #ifdef DEBUG fprintf(stderr, "***FLASH_IAPSR EOP event timed out at addr 0x%04x\n", p_STM8addr); #endif bFlshErr = true; } // check that the write was okay else if (yFlashRegVal & wrPgDisBitMask()) { #ifdef DEBUG fprintf(stderr, "***FLASH_IAPSR register's WR_PG_DIS bit set, indicating a failure at addr 0x%04x\n", p_STM8addr); #endif bFlshErr = true; } } } } bRval = !p_status->GetTxError() && !p_status->GetTxNakError() && !bFlshErr; } return bRval; } // ------------------------------------------- // ------------------------------------------- bool CSwim::readByte(uint8_t &p_yVal, uint32_t p_STM8addr /* lower 24bits only */) { //fprintf(stderr, "CSwim::readByte: addr =%08x\n", p_STM8addr); bool bRval = (!p_status->GetRxbusy() && !p_status->GetRxError()); if (bRval) { p_control->SetClrRx(1); doResetState(); // instigate the read p_rxByte->SetRxByte(p_STM8addr << 8); // wait for a result while ((p_status->GetTxbusy() || p_status->GetRxbusy()) && !p_status->GetTxError() && !p_status->GetRxError()) { usleep(1); } if (!p_status->GetRxError() && !p_status->GetTxError()) { bRval = (bool)p_status->GetRxReady(); p_yVal = uint8_t( p_rxByte->GetByte() ); //fprintf(stderr, "CSwim::readByte: status = %d, rxByte = 0x%02x\n", bRval, p_yVal); } else { bRval = false; //fprintf(stderr, "CSwim::readByte: rxErr = %d txErr = %d\n", p_status->GetRxError(), p_status->GetTxError()); } } return bRval; } // ------------------------------------------- // ------------------------------------------- bool CSwim::transmitByte(uint8_t p_yVal, uint32_t p_STM8addr /* lower 24bits only */) { bool bRval = !p_status->GetTxError(); // check Iin error if (bRval) { doResetState(); while (p_status->GetTxbusy() ) // wait until we can send the reset { usleep(1); } //txRetryTimeoutCtrl(100); doEntrySeq(); while (p_status->GetTxbusy() && !p_status->GetTxError()) // wait until we can send { usleep(1); } if (!p_status->GetTxError()) { doClearErrors(); sendTxByte(p_yVal,p_STM8addr); while (p_status->GetTxbusy() && !p_status->GetTxError() && !p_status->GetTxNakError()) { usleep(1); } } bool bNak = !p_status->GetTxNakError(), bErr = !p_status->GetTxError(); bRval = bErr && bNak; } return bRval; }