uint16_t ioMasterSPI1(uint16_t u16_c) { checkRxErrorSPI1(); #if defined(_SRXMPT) && defined(_SPIBEN) //enhanced SPI module, need to handle possibility of enhanced SPI mode if (!SPI1CON2bits.SPIBEN) {//check enhanced buffer mode bit //legacy mode _SPI1IF = 0; //clear interrupt flag since we are about to write new value SPI1BUF = u16_c; while (!_SPI1IF) { //wait for operation to complete doHeartbeat(); } } else { //enhanced buffer mode SPI1BUF = u16_c; while (SPI1STATbits.SRXMPT) { //this flag is zero when RX buffer has data doHeartbeat(); } } #else //legacy mode _SPI1IF = 0; //clear interrupt flag since we are about to write new value SPI1BUF = u16_c; while (!_SPI1IF) { //wait for operation to complete doHeartbeat(); } #endif return(SPI1BUF); }
void Master::doHeartbeat(const boost::system::error_code& e) { // Cancelled? if (e) { return; } std::lock_guard<std::recursive_mutex> hLock(heartbeatMutex); if (!heartbeatRunning) return; // Do heartbeat { std::lock_guard<std::recursive_mutex> lock(listenersMutex); for (auto *l: listeners) l->heartbeat(*this); } // Setup next heartbeat heartbeatTimer.expires_from_now(boost::posix_time::seconds(3)); heartbeatTimer.async_wait([this] (const boost::system::error_code& e){ doHeartbeat(e); }); }
uint receiveVar(char* p_c) { // Receive the data by stepping the machine until it outputs // something do { // While there's no data, run the timeout counter RECEIVE_ERROR re; uint32_t u32_count = 0; while (!isCharReady()) { if (u32_count < RECEIVE_TIMEOUT) u32_count++; doHeartbeat(); } // Step the machine *p_c = inChar(); if (u32_count >= RECEIVE_TIMEOUT) notifyOfTimeout(); re = stepReceiveMachine(*p_c); if (re != ERR_NONE) { outString("Data receive error: "); outString(getReceiveErrorString()); outChar('\n'); } } while (!isReceiveMachineChar() && !isReceiveMachineData()); // Note that p_c already contains the received character, since it's // always the last thing received from inChar(). return getReceiveMachineIndex(); }
uint16 ioMasterSPI1(uint16 u16_c) { checkRxErrorSPI1(); _SPI1IF = 0; //clear interrupt flag since we are about to write new value SPI1BUF = u16_c; while (!_SPI1IF) { //wait for operation to complete doHeartbeat(); }; return(SPI1BUF); }
Master::Master(const std::shared_ptr<Library>& library, const MasterParameters& parameters): _library(library), _parameters(parameters), nodeAcceptor(library->ioService(), parseTcpEndpoint(parameters.nodeEndpoint)), clientAcceptor(library->ioService(), parseTcpEndpoint(parameters.clientEndpoint)), heartbeatTimer(library->ioService()), heartbeatRunning(true), _sslContext(ssl::context::tlsv1), disposed(false), nodeAcceptorRunning(true), clientAcceptorRunning(true) { // Setup SSL std::string pw = parameters.sslPassword; sslContext().set_password_callback([pw](std::size_t, ssl::context::password_purpose) { return pw; }); try { BOOST_LOG_SEV(log, LogLevel::Info) << format("Using %s as the certificate chain file.") % parameters.sslCertificateFile; sslContext().use_certificate_chain_file(parameters.sslCertificateFile); } catch (const std::exception& ex) { MSCThrow(InvalidDataException (str(format("Error occured while attempting to use '%s' as the " "certificate chain file: %s") % parameters.sslCertificateFile % ex.what()))); } try { BOOST_LOG_SEV(log, LogLevel::Info) << format("Using %s as the private key file.") % parameters.sslPrivateKeyFile; sslContext().use_private_key_file(parameters.sslPrivateKeyFile, boost::asio::ssl::context::pem); } catch (const std::exception& ex) { MSCThrow(InvalidDataException (str(format("Error occured while attempting to use '%s' as the " "private key file: %s") % parameters.sslPrivateKeyFile % ex.what()))); } BOOST_LOG_SEV(log, LogLevel::Debug) << "SSL is ready."; // Prepare to accept the first client and node BOOST_LOG_SEV(log, LogLevel::Debug) << "Preparing to accept clients and nodes."; waitingNodeConnection = std::make_shared<MasterNodeConnection>(*this); waitingClient = std::make_shared<MasterClient>(*this, 1, _parameters.allowVersionSpecification); acceptNodeConnectionAsync(true); acceptClientAsync(true); // Start heartbeat BOOST_LOG_SEV(log, LogLevel::Debug) << "Starting heartbeat."; doHeartbeat(boost::system::error_code()); BOOST_LOG_SEV(log, LogLevel::Info) << format("Merlion Master Server Core (%s) running.") % MSC_VERSION_STRING; }