Exemplo n.º 1
0
void uart_process_data(u8 cmd)
{
    if(uartState == UART_IDLE)
    {
        if(cmd >= NTAG_SCAN_CMD && cmd <= NTAG_VERSION_CMD)
        {
            timeout_start(4);
            uartCmd = cmd;
            uartState = UART_CNT;
        }
        else if(cmd == NTAG_CMD_ERROR || cmd == NTAG_CNT_ERROR || cmd == NTAG_CHK_ERROR)
        {
            uartState = UART_RESEND;
        }
        else
        {
            uartState = UART_CMD_TIMEOUT; // command error
            timeout_start(3);
        }
    }
    else if(uartState == UART_CNT)
    {
        uartByteCnt = cmd;
        if(uartCmd == NTAG_SCAN_CMD)
        {
            if(uartByteCnt != 0)
            {
                uartState = UART_CNT_TIMEOUT;
//                timeout_start(2);
            }
            else
            {
                uartState = UART_CHECK;
            }
        }
        else if(uartCmd == NTAG_SELECT_CMD)
        {
            if(uartByteCnt != 9)
            {
                uartState = UART_CNT_TIMEOUT;
//                timeout_start(2);
            }
            else
            {
                uartState = UART_DATA;
//                timeout_start(2);
                uartByteInc = 0;
            }
        }
        else if(uartCmd == NTAG_READ_CMD)
        {
            if(uartByteCnt != 11)
            {
                uartState = UART_CNT_TIMEOUT;
//                timeout_start(2);
            }
            else
            {
                uartState = UART_DATA;
//                timeout_start(2);
                uartByteInc = 0;
            }
        }
        else if(uartCmd == NTAG_WRITE_CMD)
        {
            if(uartByteCnt < 14 && uartByteCnt > 42)
            {
                uartState = UART_CNT_TIMEOUT;
//                timeout_start(2);
            }
            else
            {
                uartState = UART_DATA;
//                timeout_start(3);
                uartByteInc = 0;
            }
        }
        else if(uartCmd == NTAG_VERSION_CMD)
        {
            if(uartByteCnt != 0)
            {
                uartState = UART_CNT_TIMEOUT;
//                timeout_start(2);
            }
            else
            {
                uartState = UART_CHECK;
            }
        }
    }
    else if(uartState == UART_DATA)
    {
        if(uartByteCnt)
        {
            uartData[uartByteInc] = cmd;
            uartByteInc++;
            if(uartByteCnt == uartByteInc)  // time-out to be added for this condition being not met
            {
                uartState = UART_CHECK;
                uartByteInc = 0;
            }
        }
    }
    else if(uartState == UART_CHECK)
    {
        uartCheckSum = cmd;
        uartState = UART_CAL;  // null state to temperory reject incoming data
        timeout_stop(); // all data received
    }
}
Exemplo n.º 2
0
/*!
********************************************************************************
@brief	Timeout neu starten

startet einen Timer mit einem neuen Timeout. Der Alte Wert wird geloescht.

@usage
\code
static	TIMEOUT_HDL	_to_hdl;

static	void _timeout_handler (void *arg)
{
	// Do something
}


extern	void	my_fkt (void)
{
	TIMEOUT_RET	ret;
	
	ret = timeout_restart( &_to_hdl,	500);
}
\endcode

@warning 	-
@bug 		-
@see		-

@return	-
********************************************************************************
*/
extern	TIMEOUT_RET	timeout_start (
	TIMEOUT_HDL	*hdl,		//!< \c m: Referenz auf das Timerhandle
	int		timeout_ms)	//!< \c i: Zeit in [ms] nach der Timeout kommen soll
{
	if (!(_mstate & _MSTATE_INIT))
	{
		_RETURN(TIMEOUT_RET_E_NOT_INIT);
	}
	
	timeout_stop(hdl);

	
	hdl->next = NULL;
	hdl->time_ms = timeout_ms;
	hdl->time_ms_startval = timeout_ms;

	_DBGF(VOID, NOTICE, "set timer %s to %d", hdl->name, timeout_ms);

	if (_entry.next == NULL)
	{
		// Erstes Element, Timer neu starten
		_get_timediff_last_ms(NULL); 
		_entry.next = hdl;
	}
	else
	{
		if (_entry.next->time_ms > hdl->time_ms)
		{
			// Hdl am Anfang einfuegen, vorher zeit korrigieren
			_entry.next->time_ms -= hdl->time_ms;
			hdl->next = _entry.next;
			_entry.next = hdl;
			_DBGF(VOID, NOTICE, "set to first timer");
		}
		else
		{
			// Hdl in der Kette einfuegen
			TIMEOUT_HDL	*my;
			
			for (my = _entry.next; my != NULL; my = my->next)
			{
				hdl->time_ms -= my->time_ms;
				if ((my->next == NULL) ||
				    (my->next->time_ms > hdl->time_ms))
				{
					// Davor einfuegen
					if (my->next != NULL)	
					{	// Kommt noch einer hinten drann -> Vorher abziehen
						my->next->time_ms -= hdl->time_ms;
					}

					//Einfuegen
					hdl->next = my->next; // auch NULL
					my->next = hdl;
					
					break; // Ende
				}
			}
		}
	}
	_timer_dump();
	
	_RETURN(TIMEOUT_RET_OK);
}