Beispiel #1
0
uint8_t read_msg(uint8_t *buf)
{
  uint16_t ret;
  uint8_t *ret_data = (uint8_t*)&ret;

  for(int i=0; i<3; i++)
    {
    top:
      ret = uart0_getc();
      if(!ret_data[0]) // If no error
        buf[i] = ret_data[1];
      else // Handling of errors, TODO
        {
          switch (ret)
            {
            case UART_NO_DATA:
              sleep_mode();
              goto top;
            default: /* All other error cases*/
              send_msg(NAK, ret);
              uart0_flush(); // Flushing the receiving buffer, since
                             // we’re unable to continue anyway.
              return ret_data[0];
            }
        }
    }

  return 0;
}
Beispiel #2
0
_ssize_t _read_r(
    struct _reent *r, 
    int file, 
    void *ptr, 
    size_t len)
{
	char c;
	int  i;
	unsigned char *p;
	
	p = (unsigned char*)ptr;
	
	for (i = 0; i < len; i++) {
		// c = uart0Getch();
		// c = uart0GetchW();
		while ( !iFdvUart_kbhit() ) ;
		c = (char) uart0_getc();
		if (c == 0x0D) {
			*p='\0';
			break;
		}
		*p++ = c;
		uart0_putc(c);
	}
	return len - i;
}
Beispiel #3
0
void uart0_gets(char *buf, int len)
{
    int i;
    for (i = 0; i < len - 1; i++)
    {
        buf[i] = uart0_getc();
        if (buf[i] == '\r')
        {
            break;
        }
        //abc‘back'
        //0123
        //'back'
        if (buf[i] == '\b' || buf[i] == 127)
        {
            i--;
            if (i < 0)
            {
                continue;
            }
            uart0_putc('\b');
            uart0_putc(' ');
            uart0_putc('\b');
            i--;
            continue;
        }
        uart0_putc(buf[i]);
    }
    buf[i] = 0;
}
Beispiel #4
0
int
main (void)
{
    uart0_init ();

    while (1)
        proto_accept (uart0_getc ());
}
int main(void)
{
    timeCount = 0;

    hardwareInit();
    uart0_init(BAUD_SETTING);
/* Initialise timer to divide by 1025, giving 32ms time tick */
    timer0Init(0,5);

    rfm12_init();
    wdtInit();
    sei();

    indx = 0;

    for(;;)
    {
        wdt_reset();

        uint8_t sendMessage = false;
        uint16_t character = uart0_getc();
/* Wait for a serial incoming message to be built. */
        if (character != UART_NO_DATA)
        {
            inBuf[indx++] = (uint8_t)(character & 0xFF);
/* Signal to transmit if a CR was received, or string too long. */ 
            sendMessage = ((indx > MAX_MESSAGE) || (character == 0x0D));
        }

/* Send a transmission if message is ready to send, or something was
received and time waited is too long. */ 
        if (sendMessage || (timeCount++ > TIMEOUT))
        {
/* Wait for the transmit buffer to be freed and message loaded. */
            if ((indx > 0) && (rfm12_tx(indx, 0, inBuf) != RFM12_TX_OCCUPIED))
                indx = 0;
            timeCount = 0;
        }
        rfm12_tick();

/* If an RF incoming message has been received, take it from the buffer one
character at a time and transmit via the serial port. */
        if (rfm12_rx_status() == STATUS_COMPLETE)
        {
            uint8_t *bufferContents = rfm12_rx_buffer();
            uint8_t messageLength = rfm12_rx_len();
            uint8_t i;
            for (i=0;i<messageLength;i++)
            {
	            uart0_putc(bufferContents[i]);
            }
/* Clear the "in use" status of the receive buffer to be available for
rfm12lib. */
            rfm12_rx_clear();
        }
    }
}
Beispiel #6
0
int
main (void)
{
    spi_init (SPI_MASTER, SPI_MODE_0, SPI_MSB_FIRST, SPI_FOSC_DIV128);
    uart0_init ();
    sei ();
    proto_send0 ('z');
    while (1)
	proto_accept (uart0_getc ());
}
Beispiel #7
0
static int uartstream_getchar(FILE *stream)
{
	uint16_t res;
	while(1){
		res = uart0_getc();
		if(res < 256){
			if('\r' == res)
				res = '\n';
			break;
		}
	}
	return (int) (res & 0xFF);
}
Beispiel #8
0
int
main (int argc, char **argv)
{
    avr_init (argc, argv);
    sei ();
    uart0_init ();
    proto_send0 ('z');
    while (1)
    {
        uint8_t c = uart0_getc ();
        proto_accept (c);
    }
}
Beispiel #9
0
BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
{
BaseType_t xReturn;

	/* Just call into the Altera support function, which has its own parameters,
	so the parameters passed in here are not used. */
	( void ) pxPort;
	( void ) xBlockTime;

	*pcRxedChar = uart0_getc();

	if( *pcRxedChar != -1 )
	{
		xReturn = pdPASS;
	}
	else
	{
		xReturn = pdFAIL;
	}

	return xReturn;
}
void check_wlan_cmd()
{
	static unsigned char cmdstate = WLANCMD_NONE;	// Status des aktuellen Befehls
	static unsigned char cindex = 0;		// Position in wlan_string (global)
	unsigned int c;						// zum Verarbeiten des aktuellen Zeichens
	unsigned char exit;					// für while-Schleifen-Ausstieg
	unsigned char uarterror;			// UART Empfangsfehler

	exit = 0;

	while(!exit)
	{
		// uart0_getc() returns in the lower byte the received character and in the higher byte (bitmask) the last receive error
		// UART_NO_DATA is returned when no data is available.

		uarterror = 0;
		//c = uart0_getc();	//

		#if defined( WLAN_UART_NR )	// WLAN_UART_NR = 1
			c = uart1_getc();
		#else // WLAN_UART_NR = 0
			c = uart0_getc();
		#endif	// WLAN_UART_NR




		if ( c & UART_NO_DATA )	//no data available from UART
		{
			exit = 1;
		}
		else	// data available
		{

			// new data available from UART - check for Frame or Overrun error

			if ( c & UART_FRAME_ERROR )
			{
				// Framing Error detected, i.e no stop bit detected
				uarterror = 1;
			}
			if ( c & UART_OVERRUN_ERROR )
			{
				// Overrun, a character already present in the UART UDR register was
				// not read by the interrupt handler before the next character arrived,
				// one or more received characters have been dropped
				uarterror = 2;
			}
			if ( c & UART_BUFFER_OVERFLOW )
			{
				// We are not reading the receive buffer fast enough,  one or more received character have been dropped
				uarterror = 3;
			}

			// empfangenes Zeichen verarbeiten

			if (!uarterror)	// falls kein Fehler aufgetreten ist
			{
				char d = (char)c;

				if (d == 60)	// > Befehl beginnt
				{

					cmdstate = WLANCMD_STARTED;	// ab nun Zeichen nach wlan_string übernehmen
					cindex = 0;
					memset(wlan_string, 0, UART_MAXSTRLEN+1);
				}
				else if (d == 62)	// > abschließendes Zeichen wurde empfangen
				{
					if (cmdstate == WLANCMD_STARTED) { befehl_auswerten(); }
					cmdstate = WLANCMD_NONE;
				}
				else
				{
					if (cmdstate == WLANCMD_STARTED)	// Zeichen in Befehl übernehmen
					{
						if (cindex < UART_MAXSTRLEN)
						{
							wlan_string[cindex] = d;
							cindex++;
						}
						else { cmdstate = WLANCMD_NONE; }	// Befehl ist zu lange - muss ignoriert werden!
					}
				}
			}
			else { cmdstate = WLANCMD_NONE; }	// bei uarterror bisherige Befehlsdaten verwerfen
		}


	} // end while loop


} //end check_wlan_cmd()
Beispiel #11
0
int
main (void)
{
    uint8_t i;
#ifndef HOST
    uint8_t read_old = 0;
    uint8_t old_ind = 0;
    const int total = 5000;
#endif
    timer_init ();
    for (i = 0; i < AC_ENCODER_EXT_NB; i++)
	encoder_init (i, &encoder[i]);
    encoder_corrector_init (&encoder_corrector_right);
    uart0_init ();
    proto_send0 ('z');
    sei ();
    while (1)
      {
	timer_wait ();
	if (count)
	  {
	    encoder_update ();
	    encoder_corrector_update (&encoder_corrector_right, &encoder[1]);
	  }
#ifndef HOST
	if (read && !--read_cpt)
	  {
	    uint8_t r0, r1, r2, r3;
	    r0 = encoder_ext_read (0);
	    r1 = encoder_ext_read (1);
	    r2 = encoder_ext_read (2);
	    r3 = encoder_ext_read (3);
	    if (read_mode == 0 || (read_mode == 1 && r3 != read_old)
		|| (read_mode == 2
		    && (r0 == 0 || r1 == 0 || r2 == 0 || r3 == 0)))
	      {
		proto_send4b ('r', r0, r1, r2, r3);
		read_old = r3;
	      }
	    read_cpt = read;
	  }
	if (ind && !--ind_cpt)
	  {
	    i = encoder_ext_read (3);
	    if (!ind_init && i != old_ind)
	      {
		uint8_t eip = old_ind + total;
		uint8_t eim = old_ind - total;
		proto_send7b ('i', old_ind, i, eip, eim, i - eip, i - eim,
			      i == eip || i == eim);
	      }
	    old_ind = i;
	    ind_init = 0;
	    ind_cpt = ind;
	  }
#endif
	if (count && !--count_cpt)
	  {
	    proto_send4w ('C', encoder[0].cur, encoder[1].cur,
			  encoder[2].cur, encoder[3].cur);
	    count_cpt = count;
	  }
	while (uart0_poll ())
	    proto_accept (uart0_getc ());
      }
}
/** Receive a single character
 *
 * Wait for a single character on the UART and return it.
 *
 * @return the character received.
 */
char uartRecv() {
	return uart0_getc();
 }
Beispiel #13
0
void uart() {
#define SIZE_BUF 128
	
	char buf[SIZE_BUF];
	int nbuf = 0;
	for (int i = 0; i < SIZE_BUF; i++)
		buf[i] = 0;
	
//	init_frame();
//	load(); // 初回はデータクリアいるかも?
	int mode = 1;
	
	unsigned char data[8];
	for (int i = 0; i < 8; i++)
		data[i] = 0;
	
	int n = 0;
	int cnt = 0;
	int nframe = 0;
	for (int i = 0;; i++) {
		while (uart0_test()) {
			int c = uart0_getc();
//			println(buf);
			if (c == '\n') {
				buf[nbuf] = '\0';
				if (startsWith(buf, "MATLED SHOW ")) {
					decode(buf + (9 + 3), data);
					println("SHOW");
				} else if (startsWith(buf, "MATLED SET ")) {
					char* pbuf = buf + 11;
					int nf = parseInt(pbuf);
					if (nf >= 0 && nf <= N_FRAME) {
						int n = indexOf(pbuf, ' ');
						if (n >= 0) {
							pbuf += n + 1;
//							println(pbuf);
							decode(pbuf, fr->frame[nf]);
							decode(pbuf, data); // 停止時の画面にも表示
							n = indexOf(pbuf, ' ');
							int nw = 100;
							if (n >= 0) {
								pbuf += n + 1;
								nw = parseInt(pbuf);
							}
							fr->waitms[nf] = nw;
						}
					}
				} else if (startsWith(buf, "MATLED CLEAR")) {
					mode = 0;
					init_frame();
				} else if (startsWith(buf, "MATLED RUN")) {
					mode = 1;
					println("RUN");
				} else if (startsWith(buf, "MATLED STOP")) {
					mode = 0;
					println("STOP");
				} else if (startsWith(buf, "MATLED SAVE")) {
					save();
					println("SAVE");
				} else if (startsWith(buf, "MATLED LOAD")) {
					load();
					println("LOAD");
				}
				nbuf = 0;
				continue;
			} else if (c == '\r') {
			} else {
				if (nbuf < SIZE_BUF - 1)
					buf[nbuf++] = c;
			}
		}
		if (mode == 0) {
			setMatrix(data);
		} else {
			setMatrix(fr->frame[nframe]);
			
			cnt++;
			if (cnt >= fr->waitms[nframe]) {
				cnt = 0;
				int bknframe = nframe;
				for (;;) {
					nframe++;
					if (nframe == N_FRAME)
						nframe = 0;
					if (fr->waitms[nframe])
						break;
					if (bknframe == nframe) {
						mode = 0;
						break;
					}
				}
			}
		}
	}
}
Beispiel #14
0
/** Main (and infinite) loop. */
static void
main_loop (void)
{
    while (1)
      {
	/* Wait until next cycle. */
	timer_wait ();
	/* Update chrono. */
	chrono_update ();
	/* Is match over? */
	if (chrono_is_match_over ())
	  {
	    /* Power off doors. */
	    pwm_set (BOT_PWM_DOOR_FRONT_BOTTOM, 0);
	    pwm_set (BOT_PWM_DOOR_FRONT_TOP, 0);
	    pwm_set (BOT_PWM_DOOR_BACK_BOTTOM, 0);
	    pwm_set (BOT_PWM_DOOR_BACK_TOP, 0);
	    pwm_set (BOT_PWM_CLAMP, 0);
	    /* End it and block here indefinitely. */
	    chrono_end_match (42);
	    return;
	  }
	/* Handle commands from UART. */
	while (uart0_poll ())
	    proto_accept (uart0_getc ());
	/* Update IO modules. */
	pwm_update ();
	contact_update ();
	pawn_sensor_update ();
	if (usdist_update ())
	  {
	    position_t robot_pos;
	    asserv_get_position (&robot_pos);
	    main_obstacles_nb = radar_update (&robot_pos, main_obstacles_pos);
	    move_obstacles_update ();
	    simu_send_pos_report (main_obstacles_pos, main_obstacles_nb, 0);
	  }
	/* Update AI modules. */
	logistic_update ();
	path_decay ();
	/* Only manage events if slaves are synchronised. */
	if (twi_master_sync ())
	    main_event_to_fsm ();
	/* Send stats if requested. */
	if (main_stats_asserv_ && !--main_stats_asserv_cpt_)
	  {
	    /* Get current position */
	    position_t cur_pos;
	    asserv_get_position (&cur_pos);
	    /* Send stats */
	    proto_send3w ('A', cur_pos.v.x, cur_pos.v.y, cur_pos.a);
	    /* Reset stats counter */
	    main_stats_asserv_cpt_ = main_stats_asserv_;
	  }
	if (main_stats_contact_ && !--main_stats_contact_cpt_)
	  {
	    proto_send1d ('P', contact_all () | (uint32_t) mimot_get_input () << 24);
	    main_stats_contact_cpt_ = main_stats_contact_;
	  }
	if (main_stats_codebar_ && !--main_stats_codebar_cpt_)
	  {
	    proto_send2b ('B', codebar_get (DIRECTION_FORWARD),
			  codebar_get (DIRECTION_BACKWARD));
	    main_stats_codebar_cpt_ = main_stats_codebar_;
	  }
	if (main_stats_usdist_ && !--main_stats_usdist_cpt_)
	  {
	    proto_send4w ('U', usdist_mm[0], usdist_mm[1], usdist_mm[2],
			  usdist_mm[3]);
	    main_stats_usdist_cpt_ = main_stats_usdist_;
	  }
      }
}