|
| 1 | +#include "application/pattern_generator_commands.h" |
| 2 | + |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "system/pattern_generator.h" |
| 6 | + |
| 7 | +enum { |
| 8 | + PG_DEFAULT_PIN_BASE = 16, |
| 9 | + PG_DEFAULT_PIN_COUNT = 1, |
| 10 | + PG_DEFAULT_RATE_HZ = 1000, |
| 11 | + PG_MAX_PIN_COUNT = 8, |
| 12 | + PG_MAX_PATTERN_WORDS = 16384, |
| 13 | + PG_MAX_RATE_HZ = 75000000, |
| 14 | +}; |
| 15 | + |
| 16 | +static PatternGenerator pg; |
| 17 | +static bool pg_initialized; |
| 18 | +static uint32_t pattern_buffer[PG_MAX_PATTERN_WORDS]; |
| 19 | + |
| 20 | +static struct { |
| 21 | + uint32_t pin_base; |
| 22 | + uint32_t pin_count; |
| 23 | + uint32_t rate_hz; |
| 24 | + uint32_t pattern_words; |
| 25 | + PatternGeneratorMode mode; |
| 26 | +} state = { |
| 27 | + .pin_base = PG_DEFAULT_PIN_BASE, |
| 28 | + .pin_count = PG_DEFAULT_PIN_COUNT, |
| 29 | + .rate_hz = PG_DEFAULT_RATE_HZ, |
| 30 | + .pattern_words = 0, |
| 31 | + .mode = PATTERN_GENERATOR_MODE_ONCE, |
| 32 | +}; |
| 33 | + |
| 34 | +static bool config_is_valid(void) |
| 35 | +{ |
| 36 | + return state.pin_count >= 1 && state.pin_count <= PG_MAX_PIN_COUNT && |
| 37 | + state.pin_base + state.pin_count <= 30 && state.rate_hz >= 1 && |
| 38 | + state.rate_hz <= PG_MAX_RATE_HZ; |
| 39 | +} |
| 40 | + |
| 41 | +static bool apply_config(void) |
| 42 | +{ |
| 43 | + if (!config_is_valid()) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + PatternGeneratorConfig config = { |
| 48 | + .pin_base = state.pin_base, |
| 49 | + .pin_count = state.pin_count, |
| 50 | + .rate_hz = state.rate_hz, |
| 51 | + }; |
| 52 | + |
| 53 | + if (pg_initialized) { |
| 54 | + return pattern_generator_configure(&pg, &config); |
| 55 | + } |
| 56 | + |
| 57 | + pg_initialized = pattern_generator_init(&pg, &config); |
| 58 | + return pg_initialized; |
| 59 | +} |
| 60 | + |
| 61 | +void pg_reset_state(void) |
| 62 | +{ |
| 63 | + pg_stop(); |
| 64 | + if (pg_initialized) { |
| 65 | + pattern_generator_deinit(&pg); |
| 66 | + } |
| 67 | + |
| 68 | + pg_initialized = false; |
| 69 | + state.pin_base = PG_DEFAULT_PIN_BASE; |
| 70 | + state.pin_count = PG_DEFAULT_PIN_COUNT; |
| 71 | + state.rate_hz = PG_DEFAULT_RATE_HZ; |
| 72 | + state.pattern_words = 0; |
| 73 | + state.mode = PATTERN_GENERATOR_MODE_ONCE; |
| 74 | + memset(pattern_buffer, 0, sizeof(pattern_buffer)); |
| 75 | +} |
| 76 | + |
| 77 | +void pg_task(void) |
| 78 | +{ |
| 79 | + if (pg_initialized) { |
| 80 | + pattern_generator_task(&pg); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +bool pg_set_pins(uint32_t pin_base, uint32_t pin_count) |
| 85 | +{ |
| 86 | + if (pin_count < 1 || pin_count > PG_MAX_PIN_COUNT || |
| 87 | + pin_base + pin_count > 30 || pg_is_running()) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + uint32_t old_pin_base = state.pin_base; |
| 92 | + uint32_t old_pin_count = state.pin_count; |
| 93 | + state.pin_base = pin_base; |
| 94 | + state.pin_count = pin_count; |
| 95 | + |
| 96 | + if (pg_initialized && !apply_config()) { |
| 97 | + state.pin_base = old_pin_base; |
| 98 | + state.pin_count = old_pin_count; |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | +} |
| 104 | + |
| 105 | +bool pg_set_rate(uint32_t rate_hz) |
| 106 | +{ |
| 107 | + if (rate_hz < 1 || rate_hz > PG_MAX_RATE_HZ || pg_is_running()) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + uint32_t old_rate_hz = state.rate_hz; |
| 112 | + state.rate_hz = rate_hz; |
| 113 | + |
| 114 | + if (pg_initialized && !apply_config()) { |
| 115 | + state.rate_hz = old_rate_hz; |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +bool pg_set_mode_once(void) |
| 123 | +{ |
| 124 | + if (pg_is_running()) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + state.mode = PATTERN_GENERATOR_MODE_ONCE; |
| 129 | + return true; |
| 130 | +} |
| 131 | + |
| 132 | +bool pg_set_mode_loop(void) |
| 133 | +{ |
| 134 | + if (pg_is_running()) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + state.mode = PATTERN_GENERATOR_MODE_LOOP; |
| 139 | + return true; |
| 140 | +} |
| 141 | + |
| 142 | +bool pg_upload_data(uint8_t const *data, size_t len) |
| 143 | +{ |
| 144 | + if (!data || len == 0 || (len % sizeof(uint32_t)) != 0 || pg_is_running()) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + size_t word_count = len / sizeof(uint32_t); |
| 149 | + if (word_count > PG_MAX_PATTERN_WORDS) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + memcpy(pattern_buffer, data, len); |
| 154 | + state.pattern_words = (uint32_t)word_count; |
| 155 | + return true; |
| 156 | +} |
| 157 | + |
| 158 | +bool pg_start(void) |
| 159 | +{ |
| 160 | + if (state.pattern_words == 0) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + if (!pg_initialized && !apply_config()) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + return pattern_generator_start( |
| 169 | + &pg, |
| 170 | + pattern_buffer, |
| 171 | + state.pattern_words, |
| 172 | + state.mode |
| 173 | + ); |
| 174 | +} |
| 175 | + |
| 176 | +void pg_stop(void) |
| 177 | +{ |
| 178 | + if (pg_initialized) { |
| 179 | + pattern_generator_stop(&pg); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +uint32_t pg_get_pin_base(void) { return state.pin_base; } |
| 184 | + |
| 185 | +uint32_t pg_get_pin_count(void) { return state.pin_count; } |
| 186 | + |
| 187 | +uint32_t pg_get_rate(void) { return state.rate_hz; } |
| 188 | + |
| 189 | +bool pg_get_mode_loop(void) { return state.mode == PATTERN_GENERATOR_MODE_LOOP; } |
| 190 | + |
| 191 | +uint32_t pg_get_pattern_words(void) { return state.pattern_words; } |
| 192 | + |
| 193 | +bool pg_is_running(void) |
| 194 | +{ |
| 195 | + return pg_initialized && pattern_generator_is_running(&pg); |
| 196 | +} |
0 commit comments