diff --git a/CMakeLists.txt b/CMakeLists.txt index a7dddec..d73e89d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,14 +18,21 @@ add_compile_options(-Wall -Wno-maybe-uninitialized ) -add_executable(hello_multicore +add_executable( + hello_multicore multicore.c - automat.c) + automat.c + ) + +pico_generate_pio_header(hello_multicore ${CMAKE_CURRENT_LIST_DIR}/pwm.pio) # Add pico_multicore which is required for multicore functionality -target_link_libraries(hello_multicore +target_link_libraries( + hello_multicore pico_stdlib - pico_multicore) + hardware_pio + pico_multicore + ) # enable usb output, disable uart output pico_enable_stdio_usb(hello_multicore 1) diff --git a/CV.h b/CV.h new file mode 100644 index 0000000..5451cf4 --- /dev/null +++ b/CV.h @@ -0,0 +1,8 @@ +#define CV_1 5 //Adresse - Die Basisadresse enthält die Lokadresse. Ist CV#1 gleich 0, verlässt der Decoder den Digitalbetrieb und wechselt in den Alternativ-Betrieb (Analog-Betrieb). Normalerweise wird beim Schreiben der CV1 im Decoder automatisch CV19 (Mehrfachtraktionsadresse) und in CV29 das Bit 5 (Verwendung der erweiterten Adresse; Bit 5 bei Zählung ab 0) gelöscht. +#define CV_2 0 //U_min - Definiert die Fahrspannung, die bei niedrigster Fahrstufe am Motor anliegt. +#define CV_3 5 //Beschleunigungsrate - Definiert die Länge der Wartezeit, die beim Beschleunigen jeweils vor dem Hochschalten zur nächst höheren Fahrstufe vergeht. +#define CV_4 5 //Bremsrate - Definiert die Länge der Wartezeit, die beim Bremsen jeweils vor dem Herunterschalten zur nächst niedrigen Fahrstufe vergeht. +#define CV_5 255 //U_max - Definiert die Fahrspannung, die bei höchster Fahrstufe am Motor anliegt. +#define CV_6 127 //U_mid - Definiert die Fahrspannung, die bei mittlerer Fahrstufe am Motor anliegt. +#define CV_8 1 //Versionsnummer - Gibt die Versionsnummer der Decoder-Software an. +#define CV_9 5 //PWM-Periode - Definiert die Frequenz der Pulsweitenmodulation. \ No newline at end of file diff --git a/automat.c b/automat.c index 0965e20..e091e0f 100644 --- a/automat.c +++ b/automat.c @@ -2,11 +2,15 @@ // Created by r3d on 8/22/21. // -int -fsm_main(const char *s) -{ - const char *p; +#include +#include +#include +#include "pico/stdlib.h" + +int +fsm_main(const bool *arr, int index, const int len) +{ enum { S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15, S16, S17, S18, S19, @@ -32,10 +36,11 @@ fsm_main(const char *s) S210, S211, S212, S213, S214, S215, S216, S217, S218, S219, S220, S221, S222, S223, S224, S225, S226, S227, S228 } state; - + bool cancel_for = false; state = S0; - - for (p = s; *p != '\0'; p--) { + for (;!cancel_for;index++) { + char c = arr[index % len] ? '1' : '0'; + char* p = &c; switch (state) { case S0: /* start */ switch ((unsigned char) *p) { @@ -1118,7 +1123,8 @@ fsm_main(const char *s) break; case S138: /* e.g. "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S139: /* e.g. "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1184,7 +1190,8 @@ fsm_main(const char *s) break; case S147: /* e.g. "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S148: /* e.g. "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1250,7 +1257,8 @@ fsm_main(const char *s) break; case S156: /* e.g. "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S157: /* e.g. "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1316,7 +1324,8 @@ fsm_main(const char *s) break; case S165: /* e.g. "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S166: /* e.g. "100000000000000000000000000000000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1382,7 +1391,8 @@ fsm_main(const char *s) break; case S174: /* e.g. "10000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S175: /* e.g. "100000000000000000000000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1448,7 +1458,8 @@ fsm_main(const char *s) break; case S183: /* e.g. "10000000000000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S184: /* e.g. "100000000000000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1514,7 +1525,8 @@ fsm_main(const char *s) break; case S192: /* e.g. "10000000000000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S193: /* e.g. "100000000000000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1580,7 +1592,8 @@ fsm_main(const char *s) break; case S201: /* e.g. "10000000000000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S202: /* e.g. "100000000000000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1646,7 +1659,8 @@ fsm_main(const char *s) break; case S210: /* e.g. "10000000000000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S211: /* e.g. "100000000000000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1712,7 +1726,8 @@ fsm_main(const char *s) break; case S219: /* e.g. "10000000000000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; case S220: /* e.g. "100000000000000000000000000011" */ switch ((unsigned char) *p) { @@ -1778,7 +1793,8 @@ fsm_main(const char *s) break; case S228: /* e.g. "10000000000000000000000000001111111111" */ - return -1; /* leaf */ + cancel_for = true; + break; default: ; /* unreached */ @@ -1787,18 +1803,17 @@ fsm_main(const char *s) /* end states */ switch (state) { - case S138: return 0x400; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S147: return 0x200; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S156: return 0x100; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S165: return 0x80; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S174: return 0x40; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S183: return 0x20; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S192: return 0x10; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S201: return 0x8; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S210: return 0x4; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S219: return 0x2; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" */ - case S228: return 0x1; /* "1[01]{8}0[01]{8}0[01]{8}01{10}" */ + case S138: return 13; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 13-BYTE*/ + case S147: return 12; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 12-BYTE*/ + case S156: return 11; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 11-BYTE*/ + case S165: return 10; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 10-BYTE*/ + case S174: return 9; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 9-BYTE*/ + case S183: return 8; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 8-BYTE*/ + case S192: return 7; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 7-BYTE*/ + case S201: return 6; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 6-BYTE*/ + case S210: return 5; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 5-BYTE*/ + case S219: return 4; /* "1[01]{8}0[01]{8}0[01]{8}0[01]{8}01{10}" 4-BYTE*/ + case S228: return 3; /* "1[01]{8}0[01]{8}0[01]{8}01{10}" 3-BYTE*/ default: return -1; /* unexpected EOT */ } -} - +} \ No newline at end of file diff --git a/automat.h b/automat.h index 27e3324..f8f26c8 100644 --- a/automat.h +++ b/automat.h @@ -4,5 +4,6 @@ #ifndef PICO_EXAMPLES_AUTOMAT_H #define PICO_EXAMPLES_AUTOMAT_H -int fsm_main(const char *s); +int fsm_main(const bool *arr, int index, const int len); #endif //PICO_EXAMPLES_AUTOMAT_H + diff --git a/multicore.c b/multicore.c index 53738a7..62e4a82 100644 --- a/multicore.c +++ b/multicore.c @@ -10,7 +10,13 @@ #include "pico/stdlib.h" #include "pico/multicore.h" #include "hardware/gpio.h" +#include "hardware/pio.h" +#include "pwm.pio.h" #include "automat.h" +#include "CV.h" + + + bool readBit() { @@ -18,122 +24,210 @@ bool readBit() return !gpio_get(17); } -void printer(char buf[100]){ - while (*buf) { - multicore_fifo_push_blocking(*buf); - buf++; - } -} -uint32_t errorcount = 0; - -#define FLAG_VALUE 123 - -void pretty_print(char* s){ - char addresse[9]; - s+=11; - for (int i = 0; i < 8 ; i++) { - addresse[i] = *(s+i); - } - addresse[8] =0; - s+=9; - - char befehl[9]; - for (int i = 0; i < 8 ; i++) { - befehl[i] = *(s+i); - } - befehl[8] =0; - s+=9; - char pruef[9]; - for (int i = 0; i < 8 ; i++) { - pruef[i] = *(s+i); - } - s+=8; - pruef[8] =0; - static int32_t counter = 1; - printf("%d address: %s befehl: %s pruef: %s %c\n", counter++, addresse, befehl, pruef, *s); +#define SIZE 128 +bool bitarray[SIZE] = {false}; +int index_neu = 0; +int index_alt; +void writeBit(bool bit){ + index_alt = (index_neu+SIZE-1)%SIZE; + bitarray[index_alt] = bit; } -void core1_entry() { - multicore_fifo_push_blocking(FLAG_VALUE); +// void printer(char buf[100]){ +// while (*buf) { +// multicore_fifo_push_blocking(*buf); +// buf++; +// } +// } +// uint32_t errorcount = 0; - int index = 0; - char buf[100] = {0}; - while (1) { - buf[index] = multicore_fifo_pop_blocking(); - if (buf[index] == '\n' || index == 99) { - pretty_print(buf); - for(int i =0 ; i < 100; i++) { - buf[i] = 0; - } - index = -1; - } - index++; - } -} +// #define FLAG_VALUE 123 -const int SIZE = 10 + 1 + 3 * 9 + 1 + 1; +// void pretty_print(char* s){ +// char addresse[9]; +// s+=11; +// for (int i = 0; i < 8 ; i++) { +// addresse[i] = *(s+i); +// } +// addresse[8] =0; +// s+=9; -#define LEFTESTBIT 0x8000000000000000 -char* genCharArray(uint64_t last_read_bits, uint64_t mask); +// char befehl[9]; +// for (int i = 0; i < 8 ; i++) { +// befehl[i] = *(s+i); +// } +// befehl[8] =0; +// s+=9; +// char pruef[9]; +// for (int i = 0; i < 8 ; i++) { +// pruef[i] = *(s+i); +// } +// s+=8; +// pruef[8] =0; +// static int32_t counter = 1; +// printf("%d address: %s befehl: %s pruef: %s %c\n", counter++, addresse, befehl, pruef, *s); +// } + +// void core1_entry() { +// multicore_fifo_push_blocking(FLAG_VALUE); + +// int index = 0; +// char buf[100] = {0}; +// while (1) { +// buf[index] = multicore_fifo_pop_blocking(); +// if (buf[index] == '\n' || index == 99) { +// pretty_print(buf); +// for(int i =0 ; i < 100; i++) { +// buf[i] = 0; +// } +// index = -1; +// } +// index++; +// } +// } + +// const int SIZE = 10 + 1 + 3 * 9 + 1 + 1; + +// #define LEFTESTBIT 0x8000000000000000 +// char* genCharArray(uint64_t last_read_bits, uint64_t mask); void gpio_callback_rise(uint gpio, uint32_t events) { - static uint64_t last_read_Bits; - static uint64_t mask = LEFTESTBIT; - bool last_read_bit = readBit(); - - if (last_read_bit) { - last_read_Bits = last_read_Bits | mask; - } else { - last_read_Bits = last_read_Bits & ~mask; - } - if ( mask == 1) { - mask = LEFTESTBIT; - } else { - mask >>= 1; - } - char * s = genCharArray(last_read_Bits, mask); - if (fsm_main(s) == 1) { - s[SIZE-2 ] ='\n'; - s[SIZE-1 ] =0; - printer(s); - } - free(s); -} - -char* genCharArray(uint64_t last_read_bits, uint64_t mask) { - char *s = malloc(SIZE); - - for ( int i = SIZE-3; i >= 0 ; i-- ) { - if ( mask == LEFTESTBIT ){ - mask = 1; - } else { - mask <<= 1; + writeBit(readBit()); + bool * s = bitarray; + int FSM_RESULT = fsm_main(s, index_neu, SIZE ); + if( -1 != FSM_RESULT){ + /*printf("Index: %d\n", index_neu); + for (int i = 0; i < SIZE; i++) + { + printf("Bit %d: %d \n",i,bitarray[i]); + }*/ + for (int i = 0; i < FSM_RESULT; i++) + { + printf("\nletzter-%d BYTE: ",i); + for (int j = 0; j < 8; j++) + { + printf("%d",bitarray[(index_neu-j+8+(9*i))%SIZE]); + } } - s[i] = last_read_bits & mask ? '1': '0'; + printf("\nPaketgroesse: %d\n",FSM_RESULT); + printf("\n"); } - s[SIZE-2 ] =0; - return s; + index_neu = index_alt; } + // static uint64_t last_read_Bits; + // static uint64_t mask = LEFTESTBIT; + // bool last_read_bit = readBit(); -int main() { + // if (last_read_bit) { + // last_read_Bits = last_read_Bits | mask; + // } else { + // last_read_Bits = last_read_Bits & ~mask; + // } + // if ( mask == 1) { + // mask = LEFTESTBIT; + // } else { + // mask >>= 1; + // } + // char * s = genCharArray(last_read_Bits, mask); + // if (fsm_main(bitarray) == 1) { + // s[SIZE-2 ] ='\n'; + // s[SIZE-1 ] =0; + // printer(s); + // } + // free(s); + + +// char* genCharArray(uint64_t last_read_bits, uint64_t mask) { +// char *s = malloc(SIZE); +// for ( int i = SIZE-3; i >= 0 ; i-- ) { +// if ( mask == LEFTESTBIT ){ +// mask = 1; +// } else { +// mask <<= 1; +// } +// s[i] = last_read_bits & mask ? '1': '0'; +// } +// s[SIZE-2 ] =0; +// return s; +// } + + int main() { stdio_init_all(); - multicore_launch_core1(core1_entry); + // multicore_launch_core1(core1_entry); // Wait for it to start up - uint32_t g = multicore_fifo_pop_blocking(); + // uint32_t g = multicore_fifo_pop_blocking(); - if (g != FLAG_VALUE) - printf("Hmm, that's not right on core 0!\n"); - else { - printf("It's all gone well on core 0!"); - } + // if (g != FLAG_VALUE) + // printf("Hmm, that's not right on core 0!\n"); + // else { + // printf("It's all gone well on core 0!"); + // } gpio_init(17); gpio_set_dir(17, GPIO_IN); gpio_set_irq_enabled_with_callback(17, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise); - while(1) { - ; +/* + //preamble + for (int i = 0; i < 14; i++) writeBit(1); + + //trenn bit + writeBit(0); + + //byte 0 + for (int i = 0; i < 8; i++) writeBit(1); + + //trenn bit + writeBit(0); + + //byte 1 + for (int i = 0; i < 8; i++) writeBit(0); + + //trenn bit + writeBit(0); + + //byte 2 + for (int i = 0; i < 8; i++) writeBit(1); + + //trenn bit + writeBit(0); + + //byte 3 + for (int i = 0; i < 8; i++) writeBit(1); + + //trenn bit + writeBit(0); + + //byte 4 + for (int i = 0; i < 8; i++) writeBit(1); + + //trenn bit + writeBit(0); + + //byte 5 + for (int i = 0; i < 8; i++) writeBit(1); + + //end bit + writeBit(1); +*/ + while(1); + + /// \end::setup_multicore[] + // todo get free sm + // PIO pio = pio0; + // int sm = 0; + // uint offset = pio_add_program(pio, &pwm_program); + // printf("Loading program at %d\n", offset); + + // pwm_program_init(pio, sm, offset, 10); + // pio_pwm_set_period(pio, sm, (1u<<16)-1000); + + // int level = 0; + // while (true) { + // printf("Level = %d\n", level); + // pio_pwm_set_level(pio, sm, level * level); + // level = (level + 1) % 256; + // sleep_ms(100); } - /// \end::setup_multicore[] -} + diff --git a/pwm.pio b/pwm.pio new file mode 100644 index 0000000..d0f2bcb --- /dev/null +++ b/pwm.pio @@ -0,0 +1,31 @@ +; +; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. +; +; SPDX-License-Identifier: BSD-3-Clause +; + +; Side-set pin 0 is used for PWM output + +.program pwm +.side_set 1 opt + + pull noblock side 0 ; Pull from FIFO to OSR if available, else copy X to OSR. + mov x, osr ; Copy most-recently-pulled value back to scratch X + mov y, isr ; ISR contains PWM period. Y used as counter. +countloop: + jmp x!=y noset ; Set pin high if X == Y, keep the two paths length matched + jmp skip side 1 +noset: + nop ; Single dummy cycle to keep the two paths the same length +skip: + jmp y-- countloop ; Loop until Y hits 0, then pull a fresh PWM value from FIFO + +% c-sdk { +static inline void pwm_program_init(PIO pio, uint sm, uint offset, uint pin) { + pio_gpio_init(pio, pin); + pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); + pio_sm_config c = pwm_program_get_default_config(offset); + sm_config_set_sideset_pins(&c, pin); + pio_sm_init(pio, sm, offset, &c); +} +%}