diff --git a/.vscode/settings.json b/.vscode/settings.json index 21b26d262448d16aeb78f3c75d341ac33ab024d6..90ba40d6528ec7344c1a88db73b171668fb8c090 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -39,6 +39,7 @@ "files.associations": { "xosc.h": "c", "gps.h": "c", - "pio_xor.h": "c" + "pio_xor.h": "c", + "dma.h": "c" } } diff --git a/GPS/GPS.c b/GPS/GPS.c index 2264dc8142d11112cd3f95fecd93e590c9df9045..e602baf70a7c594722f93a5cde39761e6e5d54ee 100644 --- a/GPS/GPS.c +++ b/GPS/GPS.c @@ -1,15 +1,19 @@ #include "GPS.h" -char * nmea_buffer_pointer; char nmea_buffer_A[128]; +char * nmea_buffer_pointer = nmea_buffer_A; + + char nmea_buffer_B[128]; uint8_t bufferA_selected = 1; int nmea_dma_channel; +int test_val = 0xabbacafe; + dma_channel_config c; PIO gps_pio; @@ -22,11 +26,16 @@ gps_t gps_data = {0}; gps_t gps = {0}; +int xor_dma_channel = 0; +dma_channel_config xor_dma_c; + volatile void __not_in_flash_func (pio_irq0_gps_handler)(void) { - + interrupt = 1; printf("GPS HANDLER: interrupt : %x\n", pio_interrupt_get(gps_pio, 0)); if (pio_interrupt_get(gps_pio, 0)) { + pio_sm_put(pio0,1, 0); + /* Stop DMA */ while(!pio_sm_is_rx_fifo_empty(gps_pio, 0)); dma_channel_abort(nmea_dma_channel); @@ -44,7 +53,15 @@ volatile void __not_in_flash_func (pio_irq0_gps_handler)(void) nmea_buffer_pointer = nmea_buffer_A; } /* Restart DMA */ - dma_channel_configure(nmea_dma_channel, &c, nmea_buffer_pointer, (io_rw_8*)&pio0_hw->rxf[0], 128, true); + dma_channel_configure( + xor_dma_channel, // Channel to be configured + &xor_dma_c, // The configuration we just created + (io_rw_8*)&pio0_hw->txf[1], // The initial write address + nmea_buffer_pointer, // The initial read address + 128, // Number of transfers; in this case each is 1 byte. + false // Start immediately. + ); + dma_channel_configure(nmea_dma_channel, &c, nmea_buffer_pointer+1, (io_rw_8*)&pio0_hw->rxf[0]+3, 128, true); /* Let the PIO read again */ pio_interrupt_clear(gps_pio, 0); /* Parse NMEA phrase */ @@ -262,18 +279,18 @@ 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, true, 8); - sm_config_set_out_shift(&c, true, true, 8); + sm_config_set_in_shift(&c, true, false, 32); + sm_config_set_out_shift(&c, true, true, 32); /* Associates the pins connected to "in" instruction */ sm_config_set_in_pins(&c, DATA_PIN); /* Write configuration to the State Machine */ pio_sm_init(gps_pio, gps_sm, offset, &c); /* Enable the state machine */ + pio_sm_put(gps_pio, gps_sm, '\n'<<24); pio_sm_set_enabled(gps_pio, gps_sm, 1); /* 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; } @@ -285,15 +302,34 @@ void init_nmea_dma(uint sm){ channel_config_set_transfer_data_size(&c, DMA_SIZE_8); channel_config_set_read_increment(&c, false); channel_config_set_write_increment(&c, true); - channel_config_set_dreq(&c, pio_get_dreq(gps_pio, 0, false)); + channel_config_set_dreq(&c, pio_get_dreq(gps_pio, sm, false)); dma_channel_configure( nmea_dma_channel, // Channel to be configured &c, // The configuration we just created - nmea_buffer_pointer, // The initial write address - (io_rw_8*)&pio0_hw->rxf[sm], // The initial read address + nmea_buffer_pointer+1, // The initial write address + (io_rw_8*)&pio0_hw->rxf[sm]+3, // The initial read address 128, // Number of transfers; in this case each is 1 byte. true // Start immediately. ); printf("------- GPS DMA Started\n"); + + printf("Configuring XOR DMA...\n"); + xor_dma_channel = dma_claim_unused_channel(true); + printf("------- Using DMA Channel %i\n", xor_dma_channel); + xor_dma_c = dma_channel_get_default_config(xor_dma_channel); + channel_config_set_transfer_data_size(&xor_dma_c, DMA_SIZE_8); + channel_config_set_read_increment(&xor_dma_c, true); + channel_config_set_write_increment(&xor_dma_c, false); + channel_config_set_dreq(&xor_dma_c, pio_get_dreq(gps_pio, sm, false)); + + dma_channel_configure( + xor_dma_channel, // Channel to be configured + &xor_dma_c, // The configuration we just created + (io_rw_8*)&pio0_hw->txf[1], // The initial write address + nmea_buffer_pointer, // The initial read address + 128, // Number of transfers; in this case each is 1 byte. + false // Start immediately. + ); + printf("------- XOR DMA Started\n"); } \ No newline at end of file diff --git a/GPS/GPS.h b/GPS/GPS.h index f8f0d3a920f59362d7b93230e6b636990647e793..5ac7b5be48509ab39acea9dbf7fc7ba86ae90efe 100644 --- a/GPS/GPS.h +++ b/GPS/GPS.h @@ -17,6 +17,10 @@ #define GPS_2D_FIX 1 #define GPS_3D_FIX 3 +extern char * nmea_buffer_pointer; + +extern volatile int interrupt; + typedef struct{ uint8_t sat; time_t time; diff --git a/GPS/pio.pio b/GPS/pio.pio index 6d5c246d830106a69fee65e5c14fa9f519cd6940..0b2f78b22d0894cb8c44b67cfb20807bda88fb53 100644 --- a/GPS/pio.pio +++ b/GPS/pio.pio @@ -4,15 +4,13 @@ .wrap_target skip: wait 0 PIN 0 [31]; Wait Start bit - ;set PINS 0b010 [31] ; Let the start bit pass before sampling - set Y, 7 [15] ; Sample in the middle of data bits + set Y, 7 [13] ; 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 + in PINS 1 [29] ; Read a single data bit, then wait for one period jmp Y-- read_bit ; Repeat 7 time - ;in NULL, 24 ; Fill the rest with zeroes - wait 1 PIN 0 ; Wait stop bit - mov Y, ISR + mov Y, ISR + push jmp X!=Y skip ; Repeat reception if not end of Packet - ;irq WAIT 0 ; Generate IRQ if end of Packet -.wrap \ No newline at end of file + irq WAIT 0 ; Generate IRQ if end of Packet +.wrap diff --git a/PA_PIO.c b/PA_PIO.c index 58a37b9bc0656ba026d3a025bcae1270e29d2c0b..3a69c67d7bf114a329655b2bfe2b8f0ef9c0132c 100644 --- a/PA_PIO.c +++ b/PA_PIO.c @@ -7,6 +7,15 @@ #include "PIO_XOR/pio_xor.h" #include "hardware/sync.h" +unsigned char reverseByte(unsigned char byte) { + unsigned char reversed = 0; + for (int i = 0; i < 8; i++) { + reversed = (reversed << 1) | (byte & 1); // Shift left and add LSB + byte >>= 1; // Shift byte to right + } + return reversed; +} + int main() { stdio_init_all(); @@ -15,11 +24,26 @@ int main() // comparator_init(pio0, 1000, 2); test_pio_xor(); char rx_gps = 0; + int read = 0; + char checksum = 0; while (true) { - 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); + read = pio_sm_get_blocking(pio0,1); + printf("res = %08x\n", read); + + sleep_us(100); + if(interrupt){ + //read = pio_sm_get_blocking(pio0,1); + char checksum = read ; + printf("calculated checksum = %x\n", reverseByte(checksum)); + //pio_sm_put(pio0,1, 0<<24); + //read = pio_sm_get_blocking(pio0,1); + interrupt = 0; + } + else { + pio_sm_put(pio0,1, read<<24); + } + + } } diff --git a/PIO_XOR/CMakeLists.txt b/PIO_XOR/CMakeLists.txt index 4d4b3c9772657fd832e7b515d2b8fa86c6c21af5..a40c3eaed3f86eeca5c9e6ffc0482b069c49e1a8 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 hardware_adc) +target_link_libraries(PIO_XOR pico_stdlib GPS 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 5ae8a630893fc8c16c976095bf87acff6aadd2cf..01b09101178c36d50be1b1049ee4030a7ebfd476 100644 --- a/PIO_XOR/pio_xor.c +++ b/PIO_XOR/pio_xor.c @@ -11,9 +11,10 @@ void test_pio_xor(void){ 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_out_shift(&c, false, false, 8); sm_config_set_in_shift(&c, false, false, 8); pio_sm_init(pio0, sm, offset, &c); /* Enable the state machine */ + pio_sm_put(pio0, sm, 0); pio_sm_set_enabled(pio0, sm, 1); } \ No newline at end of file diff --git a/PIO_XOR/pio_xor.h b/PIO_XOR/pio_xor.h index 5229f6c58ec13c9db4d25f52cc36200243f11856..cdd639e6c1eb82d94bb23c775b5f1c362290935b 100644 --- a/PIO_XOR/pio_xor.h +++ b/PIO_XOR/pio_xor.h @@ -6,6 +6,8 @@ #include "pico/stdlib.h" #include "hardware/pio.h" #include "hardware/adc.h" +#include "../GPS/GPS.h" +#include "hardware/dma.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 95ea777e6acd1996c2142dd851cc156e34974d04..69273ab53461b863cbc357a8eccb4974c570ea8b 100644 --- a/PIO_XOR/pio_xor.pio +++ b/PIO_XOR/pio_xor.pio @@ -19,7 +19,7 @@ jmp !Y is_one jmp Y-- twice ; decrement twice twice: - jmp !Y is_one ; If Y-2 <= 0 ; out = 1 + jmp !Y is_one ; If Y-2 >= 0 ; out = 1 jmp is_zero ; else out = 0 is_zero: diff --git a/test.py b/test.py new file mode 100644 index 0000000000000000000000000000000000000000..31dfa03647ee28f243c2275a076f615deb252d88 --- /dev/null +++ b/test.py @@ -0,0 +1,9 @@ +input_str = '$GLGSV,1,1,00,1*78*78$' + +# Compute bytewise XOR +xor_result = 0 +for char in input_str: + xor_result ^= ord(char) # XOR each character's ASCII value + +# Display the result +print(hex(xor_result)) \ No newline at end of file