Exemplo n.º 1
0
int main( void ) {
  unsigned char inc;
  unsigned int rx_time=0, tx_time=0;

  mcu_init();
  sys_time_init();
  led_init();
  VCOM_allow_linecoding(1);

#ifdef USE_USB_SERIAL
  VCOM_init();
#endif

  mcu_int_enable();

  LED_ON(3);

#ifdef USE_UART0
  while(1) {
    if (T0TC > (rx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(1);
    if (T0TC > (tx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(2);
    if (uart_char_available(&uart0) && VCOM_check_free_space(1)) {
      LED_ON(1);
      rx_time = T0TC;
      inc = uart_getch(&uart0);
      VCOM_putchar(inc);
    }
    if (VCOM_check_available() && uart_check_free_space(&uart0, 1)) {
      LED_ON(2);
      tx_time = T0TC;
      inc = VCOM_getchar();
      uart_transmit(&uart0, inc);
    }
  }
#else
  while(1) {
    if (T0TC > (rx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(1);
    if (T0TC > (tx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(2);
    if (uart_char_available(&uart1) && VCOM_check_free_space(1)) {
      LED_ON(1);
      rx_time = T0TC;
      inc = uart_getch(&uart1);
      VCOM_putchar(inc);
    }
    if (VCOM_check_available() && uart_check_free_space(&uart1, 1)) {
      LED_ON(2);
      tx_time = T0TC;
      inc = VCOM_getchar();
      uart_transmit(&uart1, inc);
    }
  }
#endif

  return 0;
}
Exemplo n.º 2
0
// Decoding event function
// Reading from UART
void sbus_decode_event(void) {
  uint8_t rbyte;
  if (uart_char_available(&SBUS_UART_DEV)) {
    do {
      rbyte = uart_getch(&SBUS_UART_DEV);
      switch (sbus.status) {
        case SBUS_STATUS_UNINIT:
          // Wait for the start byte
          if (rbyte == SBUS_START_BYTE) {
            sbus.status++;
            sbus.idx = 0;
          }
          break;
        case SBUS_STATUS_GOT_START:
          // Store buffer
          sbus.buffer[sbus.idx] = rbyte;
          sbus.idx++;
          if (sbus.idx == SBUS_BUF_LENGTH) {
            // Decode if last byte is the correct end byte
            if (rbyte == SBUS_END_BYTE) {
              decode_sbus_buffer(sbus.buffer, sbus.pulses, &sbus.frame_available);
            }
            sbus.status = SBUS_STATUS_UNINIT;
          }
          break;
        default:
          break;
      }
    } while (uart_char_available(&SBUS_UART_DEV));
  }
}
Exemplo n.º 3
0
// Decoding event function
// Reading from UART
void sbus_common_decode_event(struct Sbus *sbus_p, struct uart_periph *dev)
{
  uint8_t rbyte;
  if (uart_char_available(dev)) {
    do {
      rbyte = uart_getch(dev);
      switch (sbus_p->status) {
        case SBUS_STATUS_UNINIT:
          // Wait for the start byte
          if (rbyte == SBUS_START_BYTE) {
            sbus_p->status++;
            sbus_p->idx = 0;
          }
          break;
        case SBUS_STATUS_GOT_START:
          // Store buffer
          sbus_p->buffer[sbus_p->idx] = rbyte;
          sbus_p->idx++;
          if (sbus_p->idx == SBUS_BUF_LENGTH) {
            // Decode if last byte is the correct end byte
            if (rbyte == SBUS_END_BYTE) {
              decode_sbus_buffer(sbus_p->buffer, sbus_p->pulses, &sbus_p->frame_available, sbus_p->ppm);
            }
            sbus_p->status = SBUS_STATUS_UNINIT;
          }
          break;
        default:
          break;
      }
    } while (uart_char_available(dev));
  }
}
Exemplo n.º 4
0
void airspeed_uADC_event(void)
{
  while (uart_char_available(&(uADC_DEV))) {
    uint8_t ch = uart_getch(&(uADC_DEV));
    airspeed_uadc_parse(ch);
  }
}
Exemplo n.º 5
0
static inline void main_periodic( void ) {
  char ch;

  uart_transmit(&uart1, 'a');
  uart_transmit(&uart2, 'b');
  uart_transmit(&uart3, 'c');
  uart_transmit(&uart5, 'd');

  LED_OFF(1);
  LED_OFF(2);

  if (uart_char_available(&uart1)) {
    ch = uart_getch(&uart1);
    if (ch == 'a') {
      LED_ON(1);
    } else {
      LED_ON(2);
    }
  }

  if (uart_char_available(&uart2)) {
    ch =  uart_getch(&uart2);
    if (ch == 'b') {
      LED_ON(1);
    } else {
      LED_ON(2);
    }
  }

  if (uart_char_available(&uart3)) {
    ch =  uart_getch(&uart3);
    if (ch == 'c') {
      LED_ON(1);
    } else {
      LED_ON(2);
    }
  }

  if (uart_char_available(&uart5)) {
    ch =  uart_getch(&uart5);
    if (ch == 'd') {
      LED_ON(1);
    } else {
      LED_ON(2);
    }
  }
}
Exemplo n.º 6
0
void esc32_event(void) {
  while (uart_char_available(esc32_priv.dev)) {
    parse_esc32(&esc32_priv, uart_getch(esc32_priv.dev));
    if (esc32_priv.msg_available) {
      esc32_parse_msg(&esc32_priv, &esc32);
    }
  }
}
Exemplo n.º 7
0
static inline void main_periodic(void)
{
  static uint8_t expected_i = 0;
  static float last_ts = 0;
  static float recv_ts = 0;

  if (uart_char_available(&TEST_UART)) {
    uint8_t c =  uart_getch(&TEST_UART);
    RunOnceEvery(1, printf("%f, received: '%d'\n", get_sys_time_float(), c);)
    if (c != expected_i) {
Exemplo n.º 8
0
static inline void main_periodic(void)
{
  const char *foo = "FooBarBaz";
  static int i = 0;

  if (i == strlen(foo)) {
    i = 0;
  }

  uart_transmit(&TEST_UART, foo[i]);
  printf("%f, transmit: '%c'\n", get_sys_time_float(), foo[i]);

  if (uart_char_available(&TEST_UART)) {
    char c =  uart_getch(&TEST_UART);
    printf("%f, received: '%c'\n", get_sys_time_float(), c);
  }

  i++;
}
Exemplo n.º 9
0
void decode_packet() {
    unsigned short cs, ccs, newbits;
    if (buf[1] == 1 && buf[2] == 'r') {
        //  reset from board
        first = true;
        PORTD |= LED_ALL;
        return;
    }
    if (buf[1] != 16) {
        goto bad_packet;
    }
    memcpy(&cs, &buf[16], 2);
    ccs = calc_cksum((unsigned short *)buf, 8);
    if (cs != ccs) {
        goto bad_packet;
    }
    memcpy(targets, &buf[2], 12);
    memcpy(&newbits, &buf[14], 2);
    if (newbits != iobits) {
        iobits = newbits;
        PORTD |= (iobits & IOBIT_MASK) | LED_RED;
        PORTD &= (iobits | ~IOBIT_MASK);
    }
    if (first) {
        first = false;
        memcpy(counts, targets, 12);
    }
    for (unsigned char i = 0; i != 6; ++i) {
        short diff = (short)(targets[i] - counts[i]);
        if (diff < -10000) {
            diff = 10000;
        }
        else if (diff > 10000) {
            diff = 10000;
        }
        else if (diff < 0) {
            diff = -diff;
        }
        if (diff < 2) {
            diff = 2;
        }
        steprates[i] = 25000 / diff;
    }
    //  no longer blue, no longer lost packet
    PORTD &= ~LED_BLUE;
    blue_timeout = 0;
    uart_send_all(3, ack_packet);
    return;

bad_packet:
    //  bad packets make me feel blue
    PORTD |= LED_BLUE;
    wdt_reset();
    unsigned short max = 0;
    uart_send_all(3, nak_packet);
    while (!uart_available()) {
        udelay(10);
        if (max++ == 20) {
            //  no byte is there
            return;
        }
    }
    //  deliberately de-sync, because I might be treating 
    //  data as sync bytes
    uart_getch();
}
Exemplo n.º 10
0
void main_loop(void *) {
    toggle_debug();
    if (uart_available()) {
        PORTD |= LED_YELLOW;
        unsigned char ch = (unsigned char)uart_getch();
        if (recv_ptr == 0) {
            if (ch != 0xed) {
                //  not a sync byte
            }
            else {
                buf[0] = ch;
                recv_ptr = 1;
            }
        }
        else if (recv_ptr == 1) {
            if (ch > sizeof(buf) - 2) {
                //  not a proper packet
                recv_ptr = 0;
                recv_end = 0;
            }
            else {
                buf[1] = ch;
                recv_end = 2 + ch;
                recv_ptr = 2;
            }
        }
        else {
            buf[recv_ptr] = ch;
            ++recv_ptr;
            if (recv_ptr == recv_end) {
                decode_packet();
                recv_ptr = 0;
                recv_end = 0;
            }
        }
        PORTD &= ~LED_YELLOW;
    }
    unsigned short now = uread_timer();
    unsigned short delta = now - prev;
    prev = now;
    //  if this loop takes 10 milliseconds, we're already in trouble...
    if (delta > 10000) {
        delta = 10000;
    }
    //uart_force_out(((unsigned char *)&delta)[0]);
    //uart_force_out(((unsigned char *)&delta)[1]);
    unsigned char mask = 1;
    bool change = false;
    for (unsigned char i = 0; i != 6; ++i) {
        if (targets[i] != counts[i]) {
            stepphases[i] += delta;
            if ((steps & mask) || (stepphases[i] >= steprates[i])) {
                change = true;
                if (!(steps & mask)) {
                    stepphases[i] -= steprates[i];
                }
                //  avoid too much accumulation of phase -- this 
                //  means a limit on slew rate
                if (stepphases[i] > 40000) {
                    stepphases[i] = 40000;
                }
                steps = steps ^ mask;
                if ((short)(targets[i] - counts[i]) > 0) {
                    directions |= mask;
                    if (!(steps & mask)) {
                        counts[i]++;
                    }
                }
                else {
                    directions &= ~mask;
                    if (!(steps & mask)) {
                        counts[i]--;
                    }
                }
            }
        }
        mask = mask << 1;
    }
    DIR_PORT = directions;
    if (change) {
        PORTD |= LED_RED;
    }
    else {
        PORTD &= ~LED_RED;
    }
    blue_timeout += delta;
    if (blue_timeout > 2000000) {
        PORTD |= LED_BLUE;
        first = true;
    }
    after(0, main_loop, 0);
    STEP_PORT = steps;
}
Exemplo n.º 11
0
int main(int argc, char const *argv[]) {

    unsigned flit;
    unsigned flit_type;
    unsigned payload;
    unsigned packet_counter = 1;

    /* Test UART */
    setup_uart(CPU_SPEED, UART_BAUDRATE);
    uart_puts("UART TEST: If you can read this, then UART output works!\n");

    #if (UART_IN_TEST == 1)

    uart_puts("Please press letter 'b' on the UART terminal:\n");
    char uart_in = uart_getch();

    if (uart_in == 'b')
    {
        uart_puts("UART INPUT TEST PASSED!\n\n");
    }

    else
    {
        uart_puts("UART INPUT TEST FAILED!\n");
        uart_puts("Received following letter: {ASCII:HEX}\n");
        uart_putchar(uart_in);
        uart_putchar(':');
        uart_print_hex(uart_in);
        uart_puts("\n\n");
    }

    #endif

    #if (GPIO_TEST == 1)

    /* Test GPIO */
    unsigned gpio_in = memory_read(GPIOA_IN);
    memory_write(GPIO0_SET, gpio_in);

    #endif

    uart_puts("\n\nBeginning communication test\n\n");


    ni_write(build_header(DST_ADDR, 3));
    ni_write(0b1111111111111111111111111111);
    ni_write(0);

    while (packet_counter <= SEND_PACKET_COUNT)
    {
        if ((ni_read_flags() & NI_READ_MASK) == 0)
        {
            flit = ni_read();
            flit_type = get_flit_type(flit);

            if (flit_type == FLIT_TYPE_HEADER)
            {
                uart_puts("Sending packet number ");
                uart_print_num(packet_counter, 10, 0);
                uart_putchar('\n');
                ni_write(build_header(DST_ADDR, 3));
                packet_counter++;
            }
            else
            {
                payload = get_flit_payload(flit);
                ni_write(payload);
            }
        }
    }

    /* Run CPU test */
    test_plasma_funcitons();

    return 0;
}