Main function
| 49 | |
| 50 | // Main function |
| 51 | int main() |
| 52 | { |
| 53 | // needed for printf |
| 54 | stdio_init_all(); |
| 55 | |
| 56 | // Set up the state machine to receive RC SBUS data |
| 57 | PIO pio = pio0; |
| 58 | uint sm = 0; |
| 59 | uint offset = pio_add_program(pio, &sbus_program); |
| 60 | pio_sm_config c = sbus_program_get_default_config(offset); |
| 61 | |
| 62 | // configure the pin to receive the SBUS data |
| 63 | pio_sm_set_consecutive_pindirs(pio, sm, PIO_RX_PIN, 1, false); |
| 64 | pio_gpio_init(pio, PIO_RX_PIN); |
| 65 | gpio_pull_down(PIO_RX_PIN); |
| 66 | sm_config_set_in_pins(&c, PIO_RX_PIN); // for WAIT, IN |
| 67 | sm_config_set_jmp_pin(&c, PIO_RX_PIN); // for JMP |
| 68 | // Shift to right, autopull disabled |
| 69 | sm_config_set_in_shift(&c, true, false, 32); |
| 70 | // Shift to left, autopull disabled |
| 71 | sm_config_set_out_shift(&c, false, false, 32); |
| 72 | // Deeper FIFO as we're not doing any TX |
| 73 | sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX); |
| 74 | // SM transmits 1 bit per 8 execution cycles. |
| 75 | float div = (float)clock_get_hz(clk_sys) / (8 * SERIAL_BAUD); |
| 76 | sm_config_set_clkdiv(&c, div); |
| 77 | // init and enable the sm |
| 78 | pio_sm_init(pio, sm, offset, &c); |
| 79 | pio_sm_set_enabled(pio, sm, true); |
| 80 | |
| 81 | uint8_t index = 0; |
| 82 | uint8_t data[MAX_DATA_ITEMS]; |
| 83 | |
| 84 | // continuously get the SBUS data from the pio and decode it |
| 85 | while (true) |
| 86 | { |
| 87 | // Note: |
| 88 | // Although there is sufficient time for receiving and decoding because the clkdiv |
| 89 | // and join (see above), too much printing will cause data loss |
| 90 | while (pio_sm_is_rx_fifo_empty(pio, sm)) |
| 91 | tight_loop_contents(); |
| 92 | uint8_t data_item = pio_sm_get(pio, sm) >> 24; |
| 93 | // search for 0x0f: the start marker |
| 94 | if (data_item == 0x0f) |
| 95 | index = 0; |
| 96 | else if (index < MAX_DATA_ITEMS) |
| 97 | { |
| 98 | // test if the first end marker is read |
| 99 | if (data_item == 0) |
| 100 | { |
| 101 | // read the second end marker |
| 102 | while (pio_sm_is_rx_fifo_empty(pio, sm)) |
| 103 | tight_loop_contents(); |
| 104 | data_item = pio_sm_get(pio, sm) >> 24; |
| 105 | // the second end marker should also be 0 |
| 106 | if (data_item != 0) |
| 107 | printf("Error, second end marker not found\n"); |
| 108 | else |