From cec52af586fd05250da07d97ec219a000cfa8baa Mon Sep 17 00:00:00 2001 From: Giovanni Mangano <giovanni@MacBook-Pro-di-Giovanni.local> Date: Fri, 7 Mar 2025 14:32:22 +0100 Subject: [PATCH] CRC OK with gps, bitreversed --- .vscode/settings.json | 7 +++++- GPS/GPS.c | 6 ++--- GPS/pio.pio | 10 +++----- PA_PIO.c | 10 +++++--- PIO_XOR/CMakeLists.txt | 2 +- PIO_XOR/pio_xor.c | 31 ++++++----------------- PIO_XOR/pio_xor.h | 1 + PIO_XOR/pio_xor.pio | 56 ++++++++++++++++++++++++++++++++++++------ PIO_XOR/test.pio | 32 ++++++++++++++++++++++++ 9 files changed, 110 insertions(+), 45 deletions(-) create mode 100644 PIO_XOR/test.pio diff --git a/.vscode/settings.json b/.vscode/settings.json index 992389d..21b26d2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,5 +35,10 @@ "raspberry-pi-pico.cmakeAutoConfigure": true, "raspberry-pi-pico.useCmakeTools": false, "raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v3.29.9/bin/cmake", - "raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja" + "raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja", + "files.associations": { + "xosc.h": "c", + "gps.h": "c", + "pio_xor.h": "c" + } } diff --git a/GPS/GPS.c b/GPS/GPS.c index 803e25e..2264dc8 100644 --- a/GPS/GPS.c +++ b/GPS/GPS.c @@ -262,8 +262,8 @@ int gps_init( PIO pio, uint32_t baudrate){ irq_add_shared_handler(PIO0_IRQ_0, pio_irq0_gps_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); irq_set_enabled(PIO0_IRQ_0, true); /* Shift registers configuration */ - sm_config_set_in_shift(&c, true, false, 8); - sm_config_set_out_shift(&c, true, false, 8); + sm_config_set_in_shift(&c, true, true, 8); + sm_config_set_out_shift(&c, true, true, 8); /* Associates the pins connected to "in" instruction */ sm_config_set_in_pins(&c, DATA_PIN); /* Write configuration to the State Machine */ @@ -273,7 +273,7 @@ int gps_init( PIO pio, uint32_t baudrate){ /* Write the separator (that will trigger an interrupt) */ pio_sm_put(gps_pio, gps_sm, '\n'); printf("------- Initialized GPS SM\n"); - init_nmea_dma(gps_sm); + //init_nmea_dma(gps_sm); return gps_sm; } diff --git a/GPS/pio.pio b/GPS/pio.pio index 092bf0b..6d5c246 100644 --- a/GPS/pio.pio +++ b/GPS/pio.pio @@ -1,20 +1,18 @@ .program pio - pull BLOCK ; Get initial argument: Delimiter char out X, 32 ; Copy this char in X scratch register .wrap_target skip: wait 0 PIN 0 [31]; Wait Start bit ;set PINS 0b010 [31] ; Let the start bit pass before sampling - set Y, 6 [15] ; Sample in the middle of data bits + set Y, 7 [15] ; Sample in the middle of data bits read_bit: + irq clear 4 in PINS 1 [31] ; Read a single data bit, then wait for one period jmp Y-- read_bit ; Repeat 7 time - in PINS 1 ; Read the last bit - in NULL, 24 ; Fill the rest with zeroes + ;in NULL, 24 ; Fill the rest with zeroes wait 1 PIN 0 ; Wait stop bit mov Y, ISR - push ; Send the received Byte to the FIFO jmp X!=Y skip ; Repeat reception if not end of Packet - irq WAIT (0) ; Generate IRQ if end of Packet + ;irq WAIT 0 ; Generate IRQ if end of Packet .wrap \ No newline at end of file diff --git a/PA_PIO.c b/PA_PIO.c index 4cf4cbe..58a37b9 100644 --- a/PA_PIO.c +++ b/PA_PIO.c @@ -5,17 +5,21 @@ #include "comparator/comparator.h" #include "hardware/pio.h" #include "PIO_XOR/pio_xor.h" - +#include "hardware/sync.h" int main() { stdio_init_all(); sleep_ms(2000); - // gps_init(pio0, 9600); + int gps_sm = gps_init(pio0, 9600); // comparator_init(pio0, 1000, 2); test_pio_xor(); + char rx_gps = 0; while (true) { - tight_loop_contents(); + pio_sm_put_blocking(pio0, 1, rx_gps); + printf("for input 0b00: %x\n", pio_sm_get_blocking(pio0,1)); + rx_gps = pio_sm_get_blocking(pio0, gps_sm)>>24; + printf("received %c\n", rx_gps); } } diff --git a/PIO_XOR/CMakeLists.txt b/PIO_XOR/CMakeLists.txt index 9ee859f..4d4b3c9 100644 --- a/PIO_XOR/CMakeLists.txt +++ b/PIO_XOR/CMakeLists.txt @@ -9,6 +9,6 @@ target_include_directories(PIO_XOR PUBLIC "${PROJECT_SOURCE_DIR}/build/" ) -target_link_libraries(PIO_XOR pico_stdlib hardware_pio hardware_dma hardware_gpio hardware_timer) +target_link_libraries(PIO_XOR pico_stdlib hardware_pio hardware_dma hardware_gpio hardware_timer hardware_adc) pico_generate_pio_header(PIO_XOR ${PROJECT_SOURCE_DIR}/PIO_XOR/pio_xor.pio) diff --git a/PIO_XOR/pio_xor.c b/PIO_XOR/pio_xor.c index eeffbeb..5ae8a63 100644 --- a/PIO_XOR/pio_xor.c +++ b/PIO_XOR/pio_xor.c @@ -1,34 +1,19 @@ #include "pio_xor.h" void test_pio_xor(void){ - sleep_ms(1000); uint sm = pio_claim_unused_sm(pio0, true); printf("sm = %i\n",sm); - uint offset = pio_add_program(pio0, &pio_xor_program); - printf("------- Offset = %x\n", offset); + int offset = pio_add_program(pio0, &pio_xor_program); + printf("------- Offset = %i\n", offset); pio_sm_config c = pio_xor_program_get_default_config(offset); - - sm_config_set_clkdiv_int_frac8(&c, 1, 0); + pio_gpio_init(pio0, 13); + pio_sm_set_consecutive_pindirs(pio0, sm, 13, 1, false); + sm_config_set_in_pins(&c, 13); + sm_config_set_clkdiv_int_frac8(&c, 1, 0); + sm_config_set_out_shift(&c, true, false, 8); + sm_config_set_in_shift(&c, false, false, 8); pio_sm_init(pio0, sm, offset, &c); /* Enable the state machine */ pio_sm_set_enabled(pio0, sm, 1); - /* Write the separator (that will trigger an interrupt) */ - - pio_sm_put(pio0, sm, 0b1110010011100100); - - printf("for input 0b11: %x\n", pio_sm_get_blocking(pio0,0)); - //pio_sm_put(pio0, sm, 0b10); - printf("for input 0b10: %x\n", pio_sm_get_blocking(pio0,0)); - //pio_sm_put(pio0, sm, 0b01); - printf("for input 0b01: %x\n", pio_sm_get_blocking(pio0,0)); - //pio_sm_put(pio0, sm, 0b00); - printf("for input 0b00: %x\n", pio_sm_get_blocking(pio0,0)); - printf("for input 0b11: %x\n", pio_sm_get_blocking(pio0,0)); - //pio_sm_put(pio0, sm, 0b10); - printf("for input 0b10: %x\n", pio_sm_get_blocking(pio0,0)); - //pio_sm_put(pio0, sm, 0b01); - printf("for input 0b01: %x\n", pio_sm_get_blocking(pio0,0)); - //pio_sm_put(pio0, sm, 0b00); - printf("for input 0b00: %x\n", pio_sm_get_blocking(pio0,0)); } \ No newline at end of file diff --git a/PIO_XOR/pio_xor.h b/PIO_XOR/pio_xor.h index dfe9cd5..5229f6c 100644 --- a/PIO_XOR/pio_xor.h +++ b/PIO_XOR/pio_xor.h @@ -5,6 +5,7 @@ #include <string.h> #include "pico/stdlib.h" #include "hardware/pio.h" +#include "hardware/adc.h" #include "pio_xor.pio.h" void test_pio_xor(void); diff --git a/PIO_XOR/pio_xor.pio b/PIO_XOR/pio_xor.pio index 139dc90..95ea777 100644 --- a/PIO_XOR/pio_xor.pio +++ b/PIO_XOR/pio_xor.pio @@ -1,12 +1,18 @@ .program pio_xor - pull BLOCK + ;pull BLOCK .wrap_target + pull IfEmpty + repeat: mov X, ISR ; 1: Copy ISR in X mov ISR, NULL ; Set ISR to all zeros - out ISR, 2 ; 2: ISR[0] = last byte bit, ISR[1] = new byte bit - mov Y, ISR ; 3: Copy ISR in Y - ; 4: Computation of XOR + irq wait 4 ; Wait pin sampling from uart + in PINS, 1 + out Y, 1 ; 2: ISR[0] = last byte bit, ISR[1] = new byte bit + in Y, 1 + mov Y, ISR ; 3: Copy ISR in Y + + ; 4: Computation of XOR jmp !Y is_zero ; if Y = 00; out = 0 jmp Y-- once ; decrement once once: @@ -26,7 +32,41 @@ send: mov ISR, X ; 6: Copy X (Last iteration's ISR) into ISR in Y, 1 ; 7: Push the new byte into ISR - mov X, ISR ; - push - mov ISR, X -.wrap \ No newline at end of file + jmp !OSRE repeat + push +.wrap + +; .program pio_xor +; .wrap_target +; pull IFEMPTY ; Changes OSR only when a full byte has been treated +; mov X, ISR ; 1: Copy ISR in X +; mov ISR, NULL ; Set ISR to all zeros +; wait 1 IRQ 5 +; out PINS, 1 ; 2: ISR[1] = new wbyte bit +; out ISR, 1 ; --- ISR[0] = last byte bit, +; mov Y, ISR ; 3: Copy ISR in Y +; ; 4: Computation of XOR + +; jmp !Y is_zero ; if Y = 00; out = 0 +; jmp Y-- once ; decrement once +; once: +; jmp !Y is_one +; jmp Y-- twice ; decrement twice +; twice: +; jmp !Y is_one ; If Y-2 <= 0 ; out = 1 +; jmp is_zero ; else out = 0 + +; is_zero: +; set Y, 0 +; jmp send +; is_one: +; set Y, 1 +; jmp send + +; send: +; mov ISR, X ; 6: Copy X (Last iteration's ISR) into ISR +; in Y, 1 ; 7: Push the new byte into ISR +; mov X, ISR ; +; push IFFULL; should setup autopush instead +; mov ISR, X +; .wrap \ No newline at end of file diff --git a/PIO_XOR/test.pio b/PIO_XOR/test.pio new file mode 100644 index 0000000..42353c5 --- /dev/null +++ b/PIO_XOR/test.pio @@ -0,0 +1,32 @@ +.program pio_xor + pull BLOCK +.wrap_target + mov X, ISR ; 1: Copy ISR in X + mov ISR, NULL ; Set ISR to all zeros + out ISR, 2 ; 2: ISR[0] = last byte bit, ISR[1] = new byte bit + mov Y, ISR ; 3: Copy ISR in Y + ; 4: Computation of XOR + + jmp !Y is_zero ; if Y = 00; out = 0 + jmp Y-- once ; decrement once + once: + jmp !Y is_one + jmp Y-- twice ; decrement twice + twice: + jmp !Y is_one ; If Y-2 <= 0 ; out = 1 + jmp is_zero ; else out = 0 + + is_zero: + set Y, 0 + jmp send + is_one: + set Y, 1 + jmp send + + send: + mov ISR, X ; 6: Copy X (Last iteration's ISR) into ISR + in Y, 1 ; 7: Push the new byte into ISR + mov X, ISR ; + push + mov ISR, X +.wrap -- GitLab