Пример #1
0
/*! \fn checkForStateEvent
 *
 *  \param [ref] [data]
 *  \param [ref] [eventList]
 *
 *  This function checks for events in interval=[oldTime, timeValue]
 *  If a zero crossing function cause a sign change, root finding
 *  process will start
 */
int checkForStateEvent(DATA* data, LIST *eventList)
{
  TRACE_PUSH
  long i=0;

  debugStreamPrint(LOG_EVENTS, 1, "check state-event zerocrossing at time %g",  data->localData[0]->timeValue);

  for(i=0; i<data->modelData.nZeroCrossings; i++)
  {
    int *eq_indexes;
    const char *exp_str = data->callback->zeroCrossingDescription(i,&eq_indexes);
    debugStreamPrintWithEquationIndexes(LOG_EVENTS, 1, eq_indexes, "%s", exp_str);

    if(sign(data->simulationInfo.zeroCrossings[i]) != sign(data->simulationInfo.zeroCrossingsPre[i]))
    {
      debugStreamPrint(LOG_EVENTS, 0, "changed:   %s", (data->simulationInfo.zeroCrossingsPre[i] > 0) ? "TRUE -> FALSE" : "FALSE -> TRUE");
      listPushFront(eventList, &(data->simulationInfo.zeroCrossingIndex[i]));
    }
    else
    {
      debugStreamPrint(LOG_EVENTS, 0, "unchanged: %s", (data->simulationInfo.zeroCrossingsPre[i] > 0) ? "TRUE -- TRUE" : "FALSE -- FALSE");
    }

    if (DEBUG_STREAM(LOG_EVENTS))
      messageClose(LOG_EVENTS);
  }
  if (DEBUG_STREAM(LOG_EVENTS))
    messageClose(LOG_EVENTS);

  if(listLen(eventList) > 0)
  {
    TRACE_POP
    return 1;
  }
/****************************************************************************
NAME
    streamManagerOpenNotify

DESCRIPTION
    Handle new stream open notification.
    
*/
void streamManagerOpenNotify (void)
{
    DEBUG_STREAM(("streamManagerOpenNotify AppState:(%x)\n", the_app->app_state));
    switch ( the_app->app_state )
    {
        case AppStateConnecting:
        {
            /* Ignore in these states.  The fact that a media channel has been opened */
            /* for streaming will be detected when all profiles connect.              */
            break;
        }
        case AppStateIdle:
        {
            if ((the_app->active_encoder == EncoderAv) && (the_app->audio_streaming_state == StreamingInactive))
            {    /* No USB audio, mark status as pending closure after specified timeout */
                a2dpStreamSetAudioStreamingState(StreamingPending);
            }
            setAppState(AppStateStreaming);
            kickCommAction(the_app->comm_action);
            break;
        }
        default:
        {
            DEBUG_STREAM((" - IGNORED\n"));
            break;
        }
    }
}
/****************************************************************************
NAME
    streamManagerOpenComplete

DESCRIPTION
    Handle new stream open completion.
    
*/
void streamManagerOpenComplete (bool status)
{
    DEBUG_STREAM(("streamManagerOpenComplete AppState:(%x)\n", the_app->app_state));
    switch ( the_app->app_state )
    {
        case AppStateIdle:
        case AppStateStreaming:
        {
            if (status)
            {
                if ((the_app->active_encoder == EncoderAv) && (the_app->audio_streaming_state == StreamingInactive))
                {    /* No USB audio, mark status as pending closure after specified timeout */
                    a2dpStreamSetAudioStreamingState(StreamingPending);
                }
                setAppState(AppStateStreaming);
                kickCommAction(the_app->comm_action);
            }
            else
            {
                setAppState(AppStateIdle);
                eventHandleCancelCommAction();
            }
            break;
        }
        default:
        {
            DEBUG_STREAM((" - IGNORED\n"));
            break;
        }
    }
}
Пример #4
0
inline std::string getAudioPath()
{
    std::string audio = AUDIO_FILES_PATH;
    audio += "/audiofiles/";
    DEBUG_STREAM(audio);
    return audio;
}
/****************************************************************************
NAME
    streamManagerCloseComplete

DESCRIPTION
    Handle new stream close completion.
    
*/
void streamManagerCloseComplete (void)
{
    DEBUG_STREAM(("streamManagerCloseComplete AppState:(%x)\n", the_app->app_state));
    switch ( the_app->app_state )
    {
        case AppStateStreaming:   
        {
            setAppState(AppStateIdle);
            if (the_app->comm_action != CommActionStream)
                kickCommAction(the_app->comm_action);
            break;
        }
        default:
        {
            DEBUG_STREAM((" - IGNORED\n"));
            break;
        }
    }
}
Пример #6
0
// MARK: - ownership
bool
xpcc::stm32::I2cMaster1::start(xpcc::i2c::Delegate *delegate)
{
	if (!::delegate && delegate && delegate->attaching())
	{
		DEBUG_STREAM("\n###\n");
		::delegate = delegate;
		callStarting();
		
		return true;
	}
	return false;
}
Пример #7
0
bool
xpcc::stm32::I2cMaster1::startSync(xpcc::i2c::Delegate *delegate)
{
	if (!::delegate && delegate && delegate->attaching())
	{
		DEBUG_STREAM("\n###\n");
		::delegate = delegate;
		callStarting();
		
		// memory barrier, yes you really need it
		__asm__ volatile ("dmb" ::: "memory");
		
		while(::delegate != 0)
			;
		
		return true;
	}
Пример #8
0
void
xpcc::stm32::I2cMaster1::initialize(uint16_t ccrPrescaler)
{
	reset();
	// Enable clock
	RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
	
	I2C1->CR1 = I2C_CR1_SWRST; 		// reset module
	I2C1->CR1 = 0;
	
	NVIC_EnableIRQ(I2C1_ER_IRQn);
	NVIC_EnableIRQ(I2C1_EV_IRQn);
	
	I2C1->CR2 = STM32_APB1_FREQUENCY/1000000;
	I2C1->CCR = ccrPrescaler;
	DEBUG_STREAM("ccrPrescaler=" << ccrPrescaler);
	I2C1->TRISE = 0x09;
	
	I2C1->CR1 |= I2C_CR1_PE; // Enable peripheral
}
Пример #9
0
// ----------------------------------------------------------------------------
extern "C" void
I2C1_ER_IRQHandler(void)
{
	DEBUG_STREAM("ERROR!");
	uint16_t sr1 = I2C1->SR1;
	
	if (sr1 & I2C_SR1_BERR)
	{
		DEBUG_STREAM("BUS ERROR");
		I2C1->CR1 |= I2C_CR1_STOP;
		errorState = xpcc::stm32::I2cMaster1::BUS_ERROR;
	}
	else if (sr1 & I2C_SR1_AF)
	{	// acknowledge fail
		I2C1->CR1 |= I2C_CR1_STOP;
		DEBUG_STREAM("ACK FAIL");
		 // may also be ADDRESS_NACK
		errorState = xpcc::stm32::I2cMaster1::DATA_NACK;
	}
	else if (sr1 & I2C_SR1_ARLO)
	{	// arbitration lost
		DEBUG_STREAM("ARBITRATION LOST");
		errorState = xpcc::stm32::I2cMaster1::ARBITRATION_LOST;
	}
	else
	{
		DEBUG_STREAM("UNKNOWN");
		errorState = xpcc::stm32::I2cMaster1::UNKNOWN_ERROR;
	}
	
	if (delegate)
	{
		delegate->stopped(xpcc::i2c::Delegate::ERROR_CONDITION);
		delegate = 0;
	}
	
	// Overrun error is not handled here separately
	
	// Clear flags and interrupts
	I2C1->CR1 &= ~I2C_CR1_POS;
	I2C1->SR1 = 0;
	I2C1->SR2 = 0;
	writeBytesLeft = 0;
	readBytesLeft = 0;
	checkNextOperation = CHECK_NEXT_OPERATION_NO;
	
	DEBUG_STREAM("disable interrupts");
	I2C1->CR2 &= ~(I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN | I2C_CR2_ITERREN);
}
Пример #10
0
ULONG ClipStreamHandler
(
    struct Hook 	* hook,
    struct IFFHandle	* iff,
    struct IFFStreamCmd * cmd
)
{
      #define CLIPSCANBUFSIZE 10000000 //500
    ULONG error = 0;

    /* Buffer neede for reading rest of clip in IFFCMD_CLEANUP. Eats some stack */
//	UBYTE  buf[CLIPSCANBUFSIZE];

    struct IOClipReq *req;

    req = &( ((struct ClipboardHandle*)iff->iff_Stream)->cbh_Req);

    DEBUG_STREAM(bug("ClipStream: iff %p cmd %d buf %p bytes %d\n",
			 iff, cmd->sc_Command, cmd->sc_Buf, cmd->sc_NBytes));

    switch (cmd->sc_Command)
    {
	case IFFCMD_READ:

	    DEBUG_BUFSTREAMHANDLER(dprintf("ClipStream: IFFCMD_READ...\n"));

	    req->io_Command = CMD_READ;
	    req->io_Data    = cmd->sc_Buf;
	    req->io_Length  =  cmd->sc_NBytes;

	    error = (DoIO((struct IORequest*)req));

	    break;

	case IFFCMD_WRITE:

	    DEBUG_BUFSTREAMHANDLER(dprintf("ClipStream: IFFCMD_WRITE...\n"));

	    req->io_Command = CMD_WRITE;
	    req->io_Data    = cmd->sc_Buf;
	    req->io_Length  =  cmd->sc_NBytes;

	    error = (DoIO((struct IORequest*)req));

	    break;

	case IFFCMD_SEEK:

	    DEBUG_BUFSTREAMHANDLER(dprintf("ClipStream: IFFCMD_SEEK...\n"));

	    req->io_Offset += cmd->sc_NBytes;

	    if (req->io_Offset < 0)
		error = TRUE;

	    break;

	case IFFCMD_INIT:

	    DEBUG_BUFSTREAMHANDLER(dprintf("ClipStream: IFFCMD_INIT...\n"));

	    /* Start reading and writing at offset 0 */
	    req->io_ClipID = 0;
	    req->io_Offset = 0;
	    break;

	case IFFCMD_CLEANUP:

	    DEBUG_BUFSTREAMHANDLER(dprintf("ClipStream: IFFCMD_CLEANUP...\n"));

	    if ((iff->iff_Flags & IFFF_RWBITS) == IFFF_READ)
	    {
		/* Read past end of clip if we are in read mode */
		req->io_Command = CMD_READ;
		req->io_Data    = NULL;
		req->io_Length  = CLIPSCANBUFSIZE;

		/* Read until there is not more left */
		do
                {
                    DoIO((struct IORequest*)req);
		}
		while (req->io_Actual != 0);

	    }

	    if ((iff->iff_Flags & IFFF_RWBITS) == IFFF_WRITE)
	    {
	        req->io_Command = CMD_UPDATE;
		DoIO((struct IORequest*)req);
	    }
	    break;

    }

    DEBUG_STREAM(bug("ClipStream: error %ld\n", error));

    return (error);
}
Пример #11
0
// ----------------------------------------------------------------------------
extern "C" void
I2C1_EV_IRQHandler(void)
{
	DEBUG_STREAM("\n--- interrupt ---");
	
	uint16_t sr1 = I2C1->SR1;
	
	if (sr1 & I2C_SR1_SB)
	{
		// EV5: SB=1, cleared by reading SR1 register followed by writing DR register with Address.
		DEBUG_STREAM("startbit set");
		
		xpcc::i2c::Delegate::Starting s = delegate->started();
		uint8_t address;
		
		switch (s.next)
		{
			case xpcc::i2c::Delegate::READ_OP:
				address = (s.address & 0xfe) | xpcc::i2c::READ;
				initializeRead(delegate->reading());
				if (readBytesLeft <= 2)
				{
					DEBUG_STREAM("NACK");
					I2C1->CR1 &= ~I2C_CR1_ACK;
				}
				else
				{
					DEBUG_STREAM("ACK");
					I2C1->CR1 |= I2C_CR1_ACK;
				}
				if (readBytesLeft == 2)
				{
					DEBUG_STREAM("POS");
					I2C1->CR1 |= I2C_CR1_POS;
				}
				DEBUG_STREAM("read op: reading=" << readBytesLeft);
				break;
				
			case xpcc::i2c::Delegate::WRITE_OP:
				address = (s.address & 0xfe) | xpcc::i2c::WRITE;
				initializeWrite(delegate->writing());
				DEBUG_STREAM("write op: writing=" << writeBytesLeft);
				break;
				
			case xpcc::i2c::Delegate::RESTART_OP:
				address = (s.address & 0xfe) | xpcc::i2c::WRITE;
				initializeRestartAfterAddress();
				DEBUG_STREAM("restart op");
				break;
				
			default:
			case xpcc::i2c::Delegate::STOP_OP:
				address = (s.address & 0xfe) | xpcc::i2c::WRITE;
				initializeStopAfterAddress();
				DEBUG_STREAM("stop op");
				break;
		}
		
		I2C1->DR = address;
	}
	
	
	
	else if (sr1 & I2C_SR1_ADDR)
	{
		// EV6: ADDR=1, cleared by reading SR1 register followed by reading SR2.
		DEBUG_STREAM("address sent");
		DEBUG_STREAM("writeBytesLeft=" << writeBytesLeft);
		DEBUG_STREAM("readBytesLeft=" << readBytesLeft);
		
		if (writeBytesLeft > 0 || readBytesLeft > 3)
		{
			DEBUG_STREAM("enable buffers");
			I2C1->CR2 |= I2C_CR2_ITBUFEN;
		}
		if (!readBytesLeft && !writeBytesLeft)
		{
			checkNextOperation = CHECK_NEXT_OPERATION_YES;
		}
		
		
		DEBUG_STREAM("clearing ADDR");
		uint16_t sr2 = I2C1->SR2;
		(void) sr2;
		
		
		if (readBytesLeft == 1)
		{
			DEBUG_STREAM("STOP");
			I2C1->CR1 |= I2C_CR1_STOP;
			
#if WAIT_FOR_STOP_LOW
			DEBUG_STREAM("waiting for stop");
			while (I2C1->CR1 & I2C_CR1_STOP)
				;
#endif
			
			uint16_t dr = I2C1->DR;
			*readPointer++ = dr & 0xff;
			readBytesLeft = 0;
			checkNextOperation = CHECK_NEXT_OPERATION_YES_NO_STOP_BIT;
		}
	}
	
	
	
	else if (sr1 & I2C_SR1_TXE)
	{
		// EV8_1: TxE=1, shift register empty, data register empty, write Data1 in DR
		// EV8: TxE=1, shift register not empty, data register empty, cleared by writing DR
		if (writeBytesLeft > 0)
		{
			DEBUG_STREAM("tx more bytes");
			I2C1->DR = *writePointer++; // write data
			writeBytesLeft--;
			
			DEBUG_STREAM("TXE: writeBytesLeft=" << writeBytesLeft);
			
			checkNextOperation = CHECK_NEXT_OPERATION_NO_WAIT_FOR_BTF;
		}
		// no else!
		if (writeBytesLeft == 0)
		{
			// disable TxE, and wait for EV8_2
			DEBUG_STREAM("last byte transmitted, wait for btf");
			I2C1->CR2 &= ~I2C_CR2_ITBUFEN;
		}
	}
	
	
	
	else if (sr1 & I2C_SR1_RXNE)
	{
		if (readBytesLeft > 3)
		{
			// EV7: RxNE=1, cleared by reading DR register
			uint16_t dr = I2C1->DR;
			*readPointer++ = dr & 0xff;
			readBytesLeft--;
			
			DEBUG_STREAM("RXNE: readBytesLeft=" << readBytesLeft);
		}
		
		if (readBytesLeft <= 3)
		{
			// disable RxNE, and wait for BTF
			DEBUG_STREAM("fourth last byte received, wait for btf");
			I2C1->CR2 &= ~I2C_CR2_ITBUFEN;
		}
	}
	
	
	
	if (sr1 & I2C_SR1_BTF)
	{
		// EV8_2
		DEBUG_STREAM("BTF");
		
		if (readBytesLeft == 2)
		{
			// EV7_1: RxNE=1, cleared by reading DR register, programming STOP=1
			DEBUG_STREAM("STOP");
			I2C1->CR1 |= I2C_CR1_STOP;
			
			DEBUG_STREAM("reading data1");
			uint16_t dr = I2C1->DR;
			*readPointer++ = dr & 0xff;
			
#if WAIT_FOR_STOP_LOW
			DEBUG_STREAM("waiting for stop");
			while (I2C1->CR1 & I2C_CR1_STOP)
				;
#endif
			
			DEBUG_STREAM("reading data2");
			dr = I2C1->DR;
			*readPointer++ = dr & 0xff;
			
			readBytesLeft = 0;
			checkNextOperation = CHECK_NEXT_OPERATION_YES_NO_STOP_BIT;
		}
		
		if (readBytesLeft == 3)
		{
			// EV7_1: RxNE=1, cleared by reading DR register, programming ACK=0
			I2C1->CR1 &= ~I2C_CR1_ACK;
			DEBUG_STREAM("NACK");
			
			uint16_t dr = I2C1->DR;
			*readPointer++ = dr & 0xff;
			readBytesLeft--;
			
			DEBUG_STREAM("BTF: readBytesLeft=2");
		}
		
		if (checkNextOperation == CHECK_NEXT_OPERATION_NO_WAIT_FOR_BTF
			&& writeBytesLeft == 0)
		{
			// EV8_2: TxE=1, BTF = 1, Program Stop request.
			// TxE and BTF are cleared by hardware by the Stop condition
			DEBUG_STREAM("BTF, write=0");
			checkNextOperation = CHECK_NEXT_OPERATION_YES;
		}
	}
	
	
	
	if (checkNextOperation > CHECK_NEXT_OPERATION_NO_WAIT_FOR_BTF)
	{
		switch (nextOperation)
		{
			case xpcc::i2c::Delegate::WRITE_OP:
				if (checkNextOperation != CHECK_NEXT_OPERATION_YES_NO_STOP_BIT)
				{
					initializeWrite(delegate->writing());
					// reenable TXE
					I2C1->CR2 |= I2C_CR2_ITBUFEN;
					DEBUG_STREAM("write op");
				}
				break;
				
			case xpcc::i2c::Delegate::RESTART_OP:
				callStarting();
				DEBUG_STREAM("restart op");
				break;
				
			default:
				if (checkNextOperation != CHECK_NEXT_OPERATION_YES_NO_STOP_BIT)
				{
					I2C1->CR1 |= I2C_CR1_STOP;
					DEBUG_STREAM("STOP");
				}
				
				DEBUG_STREAM("disable interrupts");
				I2C1->CR2 &= ~(I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN | I2C_CR2_ITERREN);
				if (delegate) delegate->stopped(xpcc::i2c::Delegate::NORMAL_STOP);
				delegate = 0;
				DEBUG_STREAM("write finished");
				break;
		}
		checkNextOperation = CHECK_NEXT_OPERATION_NO;
	}
}