Beispiel #1
0
void uart_driver_process(void)
{
    if (uart_recv[0].inited)
        uart_rx(0);
    if (uart_recv[1].inited)
        uart_rx(1);
    if (uart_recv[2].inited)
        uart_rx(2);
}
main()
{
unsigned char temp;
unsigned char a[10];
	unsigned char i=0;

	uart_init();
lcd_init();

	do
	{		
		temp=uart_rx();
		uart_tx(temp);
		a[i++]=temp;
		lcd_cmd(0x01);
		lcd_data(temp);
		lcd_cmd(0x85);
		lcd_int(temp);
		lcd_cmd(0x89);
		lcd_string("0x");
		lcd_hex(temp);
		delay_ms(1000);
	}while(temp!=13);
	a[--i]='\0';
	lcd_cmd(0xc0);
	lcd_string(a);
	delay_ms(5000);
}
Beispiel #3
0
// read and parse message from usb dongle over uart
void *read_message()
{
  fprintf(stderr,"reading thread started\n");
  unsigned char data[256]; // enough for BLE
  struct ble_header hdr;
  int r;

  while (state != state_finish) {

    r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, UART_TIMEOUT);
    if (!r) {
      //printf("ERROR: UART timeout. Error code:%d\n", r);
      continue; // return NULL; // timeout
    } else if (r < 0) {
      fprintf(stderr,"ERROR: Reading header failed. Error code:%d\n", r);
      pthread_exit(NULL);
    }

    if (hdr.lolen) {
      r = uart_rx(hdr.lolen, data, UART_TIMEOUT);
      if (r <= 0) {
        fprintf(stderr,"ERROR: Reading data failed. Error code:%d\n", r);
        pthread_exit(NULL);
      }
    }

    const struct ble_msg *msg = ble_get_msg_hdr(hdr);

#ifdef DEBUG
    print_raw_packet(&hdr, data);
#endif

    if (!msg) {
      fprintf(stderr,"ERROR: Unknown message received\n");
      pthread_exit(NULL);
    }
    msg->handler(data);

    //usleep(500);
  }

  pthread_exit(NULL);

  //return 0;
}
static void ICACHE_FLASH_ATTR uart_con_clear_rx_dma(void) {
	// In case of failures we just ignore the data completely...
	uart_con_buffer_recv_index = 0;
	uart_con_buffer_recv_expected_length = 3;

	// ... and clear out the complete rx dma buffer
	while(uart_get_rx_fifo_count(UART_CONNECTION) > 0) {
		uart_rx(UART_CONNECTION);
	}
}
Beispiel #5
0
int read_message(int timeout_ms)
{
  unsigned char data[256]; // enough for BLE
  struct ble_header hdr;
  int r;

  r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, UART_TIMEOUT);
  if (!r)
  {
    return -1; // timeout
  }
  else if (r < 0)
  {
    printf("ERROR: Reading header failed. Error code: %d\n", r);
    return 1;
  }

  if (hdr.lolen)
  {
    r = uart_rx(hdr.lolen, data, UART_TIMEOUT);
    if (r <= 0)
    {
      printf("ERROR: Reading data failed. Error code: %d\n", r);
      return 1;
    }
  }

  const struct ble_msg *msg = ble_get_msg_hdr(hdr);

#ifdef DEBUG
  print_raw_packet(&hdr, data);
#endif

  if (!msg)
  {
    printf("ERROR: Unknown message received\n");
    exit(1);
  }

  msg->handler(data);

  return 0;
}
Beispiel #6
0
/**
 * Receive BGAPI packet using UART interface
 *
 * @param timeout_ms Milliseconds to wait before timing out on the UART RX operation
 */
int read_api_packet(int timeout_ms)
{
  unsigned char data[256]; // enough for BLE
  struct ble_header hdr;
  int r;

  r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, timeout_ms);
  if (!r) {
    return -1; // timeout
  } else if (r < 0) {
    printf("ERROR: Reading header failed. Error code:%d\n", r);
    return 1;
  }

  if (hdr.lolen) {
    r = uart_rx(hdr.lolen, data, timeout_ms);
    if (r <= 0) {
      printf("ERROR: Reading data failed. Error code:%d\n", r);
      return 1;
    }
  }

  // use BGLib function to create correct ble_msg structure based on the header
  // (header contains command class/ID info, used to identify the right structure to apply)
  const struct ble_msg *msg = ble_get_msg_hdr(hdr);

#ifdef DEBUG
  // display incoming BGAPI packet
  print_raw_packet(&hdr, data);
#endif

  if (!msg) {
    printf("ERROR: Unknown message received\n");
    exit(1);
  }

  // call the appropriate handler function with any payload data
  // (this is what triggers the ble_evt_* and ble_rsp_* functions)
  msg -> handler(data);

  return 0;
}
Beispiel #7
0
Datei: uart.c Projekt: rargo/app
void uart_test3(void)
{
	int ret;
	char buf[1024];
	int i;
	volatile int j;

	for(i = 0; i<sizeof(buf); i++)
		buf[i] = i & 0xff;

	/* uart3 tx test */
	ret = uart_open(3,115200,8,'n',1);
	dprintf("open uart 3 ret:%d\r\n", ret);

	for(;;) {
		uart_tx_str(3, "AT\r");
		while(!uart_tx_finish(3))
			;

		while(!(ret = uart_rx(3, buf, sizeof(buf))))
			;

		if(ret < 0) {
			RERR();
			continue;
		}

		j = 1000;
		while(j--)
			;
		ret += uart_rx(3, buf + ret, sizeof(buf));
		if(ret > 0) {
			buf[ret] = 0;
			dprintf("ret:%d buf:%s\r\n",ret, buf);
		} else {
			dprintf("uart rx 3 ret:%d\r\n",ret);
		}
	}

	uart_close(3);
}
Beispiel #8
0
// Funktion zum senden der Menuepunkte ueber die serielle Schnittstelle
void 	menu_puts		(void *arg, char *name) { // Menu/Sende Funktion
	uart_put_string(arg, D_Stepper);	// Uebergebenen String an Stepper senden
	// Befehl auf Display ausgeben
	lcd_clrscr();
	lcd_puts("Sent: ");
	lcd_puts(arg);
	lcd_puts("\n");
	ms_spin(100);
	//if ((UCSR1A & (1 << RXC1)))
	uart_rx(D_Stepper);	// Antwort des Stepper empfangen
	ms_spin(1000);		// Antwort noch eine weile Anzeigen
}
Beispiel #9
0
void * thread_func(void *arg)
{
    printf("UART loop start\n");

    uart_data_lock_init();

    while (!g_stop)
    {
        uart_rx();
        uart_tx();

        gw_sleep(100);
    }

    uart_data_lock_destroy();

    printf("UART loop finish\n");

    pthread_exit((void*)0);

    return NULL;
}
Beispiel #10
0
Datei: uart.c Projekt: rargo/app
void uart_test(void)
{
	char buf[1024];
	char buf2[1024];
	int i;
	int len;
	int ret;

	/* uart1 tx <---> uart3 rx
	 * uart1 rx <---> uart3 tx
	 */
	ret = uart_open(1,115200,8,'n',1);
	dprintf("uart2 open ret:%d\r\n",ret);
	ret = uart_open(3,115200,8,'n',1);
	dprintf("uart3 open ret:%d\r\n",ret);

	while(1) {
		for(i = 0; i<sizeof(buf); i++)
			buf[i] = i & 0xff;
		uart_tx(1, buf, sizeof(buf));

		
		while(!uart_tx_buffer_empty(1)) {
			RDIAG(LEGACY_DEBUG,"wait uart 2 tx finish");
		}

		while(1) {
			len = uart_rx(3, buf2, sizeof(buf2));
			dprintf("len:%d\r\n",len);
			if(len > 0) {
				for(i = 0; i<len; i++)
					dprintf("%2x",buf2[i]);
			} else {
				dprintf("\r\n");
				break;
			}
		}
	}
}
Beispiel #11
0
Datei: uart.c Projekt: rargo/app
void uart_test2(void)
{
	int ret;
	char buf[1024];
	int len;
	int total_len = 0;
	volatile int j;

	/* uart0 tx <---> uart 3 rx 
	 * to test baudrate is right
	 */
	dprintf("capture from uart0 start");

	for(j =0 ; j<2000; j++)
		;

	uart_close(3);
	ret = uart_open(3,115200,8,'n',1);

	/* send to uart 3 */
	dprintf("abcdefghijklmnopq1234325454545\r\n");

	for(j =0 ; j<2000; j++)
		;

	memset(buf,0,sizeof(buf));
	while((len = uart_rx(3, buf + total_len, 1024 - total_len))) {
		total_len += len;
	}

	uart_close(3);

	dprintf("%s",buf);
	dprintf("capture stop");
	for(;;)
		;
}
Beispiel #12
0
/**
 * The main function.
 */
int main(void) {

    /* Initialization */
    init_qfly();
    log_s("receiver initialization ... ok\n");

    /* Our loop */
    while (1) {

        /* Wait 100ms */
        _delay_ms(100);

#ifdef UART_AVAILABLE
        if (uart_rx_ready()) {
            uart_tx("Echo: ");
            uart_tx(uart_rx());
            uart_tx("\n");
        }
#endif /* UART_AVAILABLE */
    }

    /* Finally. (Never ever) */
    return 0;
}
Beispiel #13
0
int main(void)
{
    // Set up Timer2 (normal mode), freq ~55 Hz
    TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20);   // Fcpu/1024
    TIMSK2 = (1 << TOIE2);      // Enable timer overflow interrupt

    DDRB |= (1 << PB5);         // LED
    led_off();

    uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(BAUD, F_CPU));

    /*
     * now enable interrupts...
     */
    sei();

    msdelay(10);


    byte min = 0;
    byte packet[4];
    byte i = 0;

    while (1) {

        unsigned int data = uart_rx();

        // flush packet if data is invalid
        if (data & 0xff00) {
            if (i >= min) {
                txhead(JTAG430X2, NOK, 0);
                min = 1;
            }
            i = 0;
            continue;
        }

        packet[i++] = data;

        if (i < 4) {
            continue;           // keep looking for further bytes
        }

        byte app = packet[0];
        byte verb = packet[1];
        int len = packet[2] + 256 * packet[3];

        byte ok = 1;

        // read the data
        for (i = 0; i < len; i++) {
            data = uart_rx();

            if (data & 0xff00) {
                ok = 0;
                break;
            }
            if (i < CMDDATALEN) {
                cmddata[i] = data & 0xff;
            } else {
                ok = 0;         // just ignore the extra bytes
            }
        }

        if (ok && app == JTAG430X2) {
            // start processing
            jtag430x2_app.handle(app, verb, len);
        } else {
            txhead(JTAG430X2, NOK, 0);
        }

        i = 0;
    }

}
Beispiel #14
0
int gdb_getc (void)
	{
	return uart_rx(UART_DEV);
	}
static void ICACHE_FLASH_ATTR uart_con_loop(os_event_t *events) {
	com_poll();

	// If the uart buffer is empty, we try to get data from tfp ringbuffer
	if(uart_con_buffer_send_size == 0) {
		tfp_poll();
	}

	if(uart_con_buffer_recv_wait_for_tfp) {
		if(tfp_send(&uart_con_buffer_recv[UART_CON_INDEX_TFP_START], uart_con_buffer_recv_expected_length-3)) {
			uart_con_buffer_recv_index = 0;
			uart_con_buffer_recv_expected_length = 3;
			uart_con_buffer_recv_wait_for_tfp = false;
		}

		system_os_post(uart_con_prio, 0, 0);
		return;
	}

	while(uart_get_rx_fifo_count(UART_CONNECTION) > 0) {
		uart_con_buffer_recv[uart_con_buffer_recv_index] = uart_rx(UART_CONNECTION);

		if(uart_con_buffer_recv_index == UART_CON_INDEX_LENGTH) {
			// TODO: Sanity-check length
			uart_con_buffer_recv_expected_length = uart_con_buffer_recv[UART_CON_INDEX_LENGTH];

			if(uart_con_buffer_recv_expected_length > UART_CON_BUFFER_SIZE) {
				logw("Length is malformed: %d > %d\n", uart_con_buffer_recv_expected_length, UART_CON_BUFFER_SIZE);
				uart_con_clear_rx_dma();
			}
		}

		uart_con_buffer_recv_index++;
		if(uart_con_buffer_recv_index >= uart_con_buffer_recv_expected_length) {
			uint8_t checksum_calculated = 0;
			for(uint8_t i = 0; i < uart_con_buffer_recv_index-1; i++) {
				PEARSON(checksum_calculated, uart_con_buffer_recv[i]);
			}

			const uint8_t checksum_uart = uart_con_buffer_recv[uart_con_buffer_recv_index-1];
			if(checksum_calculated != checksum_uart) {
				logw("Checksum error: %d (calc) != %d (uart)\n", checksum_calculated, checksum_uart);
				logw("uart recv data: %d %d [", uart_con_buffer_recv_index, uart_con_buffer_recv_expected_length);
				for(uint8_t i = 0; i < uart_con_buffer_recv_expected_length; i++) {
					logwohw("%d ", uart_con_buffer_recv[i]);
				}
				logwohw("]\n");

				uart_con_clear_rx_dma();
				break;
			}

			if(uart_con_buffer_recv_expected_length != 3) {
				const uint8_t seq = uart_con_buffer_recv[UART_CON_INDEX_SEQUENCE] & 0x0F;
				if(uart_con_last_sequence_number_seen != seq) {
					if(!tfp_send(&uart_con_buffer_recv[UART_CON_INDEX_TFP_START], uart_con_buffer_recv_expected_length-3)) {
						uart_con_buffer_recv_wait_for_tfp = true;
					}
					uart_con_last_sequence_number_seen = seq;
				}

				ack_last_message();
			}

			const uint8_t seq_seen_by_master = uart_con_buffer_recv[UART_CON_INDEX_SEQUENCE] >> 4;
			if(wait_for_ack) {
				if(seq_seen_by_master == uart_con_sequence_number) {
					os_timer_disarm(&timeout_timer);
					wait_for_ack = false;
					uart_con_sequence_number++;
					if(uart_con_sequence_number >= 0x10) {
						uart_con_sequence_number = 1;
					}
					uart_con_buffer_send_size = 0;
				}
			}

			if(!uart_con_buffer_recv_wait_for_tfp) {
				uart_con_buffer_recv_index = 0;
				uart_con_buffer_recv_expected_length = 3;
			} else {
				// If we have to wait for tfp, we need to break out of the while loop.
				break;
			}
		}
	}