From 610915f45738bb0949defb270d61302c2ebd7f04 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 12 Jan 2024 12:37:16 +0100 Subject: [PATCH 01/13] #271 det_poly - start_val test Testing the effect of start_val on the speed of execution --- bsp/nrf/lh2_4.c | 104 +++++++++++++++++- .../01bsp_lighthouse_4/01bsp_lighthouse_4.c | 20 +++- 2 files changed, 117 insertions(+), 7 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index b99c02204..441c061d5 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -279,6 +279,7 @@ uint64_t _poly_check(uint32_t poly, uint32_t bits, uint8_t numbits); * @return polynomial, indicating which polynomial was found, or FF for error (polynomial not found). */ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val); +uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val); /** * @brief counts the number of 1s in a 64-bit @@ -431,6 +432,7 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { uint32_t temp_timestamp; uint64_t temp_bits_sweep; uint8_t temp_selected_polynomial; + uint8_t temp_selected_polynomial_no_test; int8_t temp_bit_offset; int8_t sweep; @@ -446,7 +448,18 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { // convert the SPI reading to bits via zero-crossing counter demodulation and differential/biphasic manchester decoding temp_bits_sweep = _demodulate_light(temp_spi_bits); // figure out which polynomial each one of the two samples come from. - temp_selected_polynomial = _determine_polynomial(temp_bits_sweep, &temp_bit_offset); + temp_selected_polynomial_no_test = _determine_polynomial(temp_bits_sweep, &temp_bit_offset); + + NRF_P1->OUTCLR = 1 << 7; + NRF_P1->OUTCLR = 1 << 11; + NRF_P0->OUTCLR = 1 << 28; + NRF_P0->OUTSET = 1 << 29; + temp_selected_polynomial = _determine_polynomial_test(temp_bits_sweep, &temp_bit_offset); + NRF_P0->OUTCLR = 1 << 29; + + if (temp_selected_polynomial != temp_selected_polynomial_no_test){ + NRF_P1->OUTSET = 1 << 11; + } // If there was an error with the polynomial, leave without updating anything if (temp_selected_polynomial == LH2_4_POLYNOMIAL_ERROR_INDICATOR){ @@ -487,6 +500,7 @@ void db_lh2_4_process_location(db_lh2_4_t *lh2) { lh2->raw_data[sweep][basestation].selected_polynomial, lh2->raw_data[sweep][basestation].bits_sweep >> (47 - lh2->raw_data[sweep][basestation].bit_offset)) - lh2->raw_data[sweep][basestation].bit_offset; + // Mark the data point as processed lh2->data_ready[sweep][basestation] = DB_LH2_4_PROCESSED_DATA_AVAILABLE; } @@ -906,7 +920,10 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { // check which polynomial the bit sequence is part of // TODO: make function a void and modify memory directly // TODO: rename chipsH1 to something relevant... like bits? - int32_t bits_N_for_comp = 47; + + *start_val = 0; // TODO: remove this? possible that I modify start value during the demodulation process + + int32_t bits_N_for_comp = 47 - *start_val; uint32_t bit_buffer1 = (uint32_t)(((0xFFFF800000000000) & chipsH1) >> 47); uint64_t bits_from_poly[LH2_4_BASESTATION_COUNT*2] = { 0 }; uint64_t weights[LH2_4_BASESTATION_COUNT*2] = { 0xFFFFFFFFFFFFFFFF }; @@ -916,8 +933,6 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { uint64_t bits_to_compare = 0; int32_t threshold = POLYNOMIAL_BIT_ERROR_INITIAL_THRESHOLD; - *start_val = 0; // TODO: remove this? possible that I modify start value during the demodulation process - // try polynomial vs. first buffer bits // this search takes 17-bit sequences and runs them forwards through the polynomial LFSRs. // if the remaining detected bits fit well with the chosen 17-bit sequence and a given polynomial, it is treated as "correct" @@ -927,6 +942,7 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { // run polynomial search on the first capture while (1) { + // TODO: do this math stuff in multiple operations to: (a) make it readable (b) ensure order-of-execution bit_buffer1 = (uint32_t)(((0xFFFF800000000000 >> (*start_val)) & chipsH1) >> (64 - 17 - (*start_val))); bits_to_compare = (chipsH1 & (0xFFFFFFFFFFFFFFFF << (64 - 17 - (*start_val) - bits_N_for_comp))); @@ -937,15 +953,89 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { for (uint8_t i = 0; i 8) { + *start_val = 0; + bits_N_for_comp = bits_N_for_comp + 1; + if (threshold > 1) { + threshold = threshold - 1; + } else if (threshold == 1) { // keep threshold at ones, but you're probably screwed with an unlucky bit error + threshold = 1; + } + } else { + *start_val = *start_val + 1; + bits_N_for_comp = bits_N_for_comp - 1; + } + + } + return selected_poly; +} + +uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { + // check which polynomial the bit sequence is part of + // TODO: make function a void and modify memory directly + // TODO: rename chipsH1 to something relevant... like bits? + + *start_val = 8; // TODO: remove this? possible that I modify start value during the demodulation process + + int32_t bits_N_for_comp = 47 - *start_val; + uint32_t bit_buffer1 = (uint32_t)(((0xFFFF800000000000) & chipsH1) >> 47); + uint64_t bits_from_poly[LH2_4_BASESTATION_COUNT*2] = { 0 }; + uint64_t weights[LH2_4_BASESTATION_COUNT*2] = { 0xFFFFFFFFFFFFFFFF }; + uint8_t selected_poly = LH2_4_POLYNOMIAL_ERROR_INDICATOR; // initialize to error condition + uint8_t min_weight_idx = LH2_4_POLYNOMIAL_ERROR_INDICATOR; + uint64_t min_weight = LH2_4_POLYNOMIAL_ERROR_INDICATOR; + uint64_t bits_to_compare = 0; + int32_t threshold = POLYNOMIAL_BIT_ERROR_INITIAL_THRESHOLD; + + uint32_t test_iteration = 0; + // try polynomial vs. first buffer bits + // this search takes 17-bit sequences and runs them forwards through the polynomial LFSRs. + // if the remaining detected bits fit well with the chosen 17-bit sequence and a given polynomial, it is treated as "correct" + // in case of bit errors at the beginning of the capture, the 17-bit sequence is shifted (to a max of 8 bits) + // in case of bit errors at the end of the capture, the ending bits are removed (to a max of + // removing bits reduces the threshold correspondingly, as incorrect packet detection will cause a significant delay in location estimate + + // run polynomial search on the first capture + while (1) { + if(test_iteration >=1){ + NRF_P0->OUTSET = 1 << 28; + } + // TODO: do this math stuff in multiple operations to: (a) make it readable (b) ensure order-of-execution + bit_buffer1 = (uint32_t)(((0xFFFF800000000000 >> (*start_val)) & chipsH1) >> (64 - 17 - (*start_val))); + bits_to_compare = (chipsH1 & (0xFFFFFFFFFFFFFFFF << (64 - 17 - (*start_val) - bits_N_for_comp))); + // reset the minimum polynomial match found + min_weight_idx = LH2_4_POLYNOMIAL_ERROR_INDICATOR; + min_weight = LH2_4_POLYNOMIAL_ERROR_INDICATOR; + // Check against all the known polynomials + for (uint8_t i = 0; iOUTSET = 1 << 7; selected_poly = LH2_4_POLYNOMIAL_ERROR_INDICATOR; // mark the poly as "wrong" break; } @@ -966,6 +1056,8 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { *start_val = *start_val + 1; bits_N_for_comp = bits_N_for_comp - 1; } + + test_iteration++; } return selected_poly; } diff --git a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c index d8abbba2f..2af559f3a 100644 --- a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c +++ b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c @@ -28,6 +28,18 @@ static db_lh2_4_t _lh2; //=========================== main ============================================= +/* +P0.30 -> TS4231 Data pin +P1.04 -> SPI SCLK +P0.29 -> High -> at the start of _determine_polynomial() + Low - at the end of _determine_polynomial() +P1.07 -> Goes briefly HIGH if there _determine_polynomial() failed to determine the polynomial +P0.28 -> Goes high if _determine_polynomial() took more than 1 iteration to find the polynomial +P1.11 -> Goes high if the original function and the function with the changes being tested gives differet results + +*/ + + /** * @brief The program starts executing here. */ @@ -38,13 +50,17 @@ int main(void) { // Initialize the LH2 db_lh2_4_init(&_lh2, &db_lh2_d, &db_lh2_e); db_lh2_4_start(&_lh2); + NRF_P0->DIRSET = 1 << 29; + NRF_P0->DIRSET = 1 << 28; + NRF_P1->DIRSET = 1 << 07; + NRF_P1->DIRSET = 1 << 11; + while (1) { // wait until something happens e.g. an SPI interrupt __WFE(); db_lh2_4_process_raw_data(&_lh2); - if (DB2_LH2_4_FULL_COMPUTATION) { // the location function has to be running all the time db_lh2_4_process_location(&_lh2); @@ -58,3 +74,5 @@ int main(void) { // one last instruction, doesn't do anything, it's just to have a place to put a breakpoint. __NOP(); } +// NRF_P0->OUTSET = 1 << 29; +// NRF_P0->OUTCLR = 1 << 29; \ No newline at end of file From 2e7bba9e516bbb70c7919b8551c53b3de13bf019 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 12 Jan 2024 17:13:47 +0100 Subject: [PATCH 02/13] #271 det_poly: limit iterations test --- bsp/nrf/lh2_4.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 441c061d5..a86d9cb6b 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -932,7 +932,7 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { uint64_t min_weight = LH2_4_POLYNOMIAL_ERROR_INDICATOR; uint64_t bits_to_compare = 0; int32_t threshold = POLYNOMIAL_BIT_ERROR_INITIAL_THRESHOLD; - + uint32_t test_iteration = 0; // try polynomial vs. first buffer bits // this search takes 17-bit sequences and runs them forwards through the polynomial LFSRs. // if the remaining detected bits fit well with the chosen 17-bit sequence and a given polynomial, it is treated as "correct" @@ -942,6 +942,9 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { // run polynomial search on the first capture while (1) { + if(test_iteration ==9){ + NRF_P0->OUTSET = 1 << 28; + } // TODO: do this math stuff in multiple operations to: (a) make it readable (b) ensure order-of-execution bit_buffer1 = (uint32_t)(((0xFFFF800000000000 >> (*start_val)) & chipsH1) >> (64 - 17 - (*start_val))); @@ -981,7 +984,7 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { *start_val = *start_val + 1; bits_N_for_comp = bits_N_for_comp - 1; } - +test_iteration++; } return selected_poly; } @@ -1004,6 +1007,7 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { int32_t threshold = POLYNOMIAL_BIT_ERROR_INITIAL_THRESHOLD; uint32_t test_iteration = 0; + // try polynomial vs. first buffer bits // this search takes 17-bit sequences and runs them forwards through the polynomial LFSRs. // if the remaining detected bits fit well with the chosen 17-bit sequence and a given polynomial, it is treated as "correct" @@ -1017,6 +1021,7 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { if(test_iteration >=1){ NRF_P0->OUTSET = 1 << 28; } + // TODO: do this math stuff in multiple operations to: (a) make it readable (b) ensure order-of-execution bit_buffer1 = (uint32_t)(((0xFFFF800000000000 >> (*start_val)) & chipsH1) >> (64 - 17 - (*start_val))); bits_to_compare = (chipsH1 & (0xFFFFFFFFFFFFFFFF << (64 - 17 - (*start_val) - bits_N_for_comp))); @@ -1033,31 +1038,32 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { min_weight = weights[i]; } } - // too few bits to reliably compare, give up - if (bits_N_for_comp < 10) { - NRF_P1->OUTSET = 1 << 7; - selected_poly = LH2_4_POLYNOMIAL_ERROR_INDICATOR; // mark the poly as "wrong" - break; - } + // If you found a sufficiently good value, then return which polinomial generated it if (min_weight <= (uint64_t)threshold) { selected_poly = min_weight_idx; break; // match failed, try again removing bits from the end } else if (*start_val > 8) { - *start_val = 0; - bits_N_for_comp = bits_N_for_comp + 1; - if (threshold > 1) { + *start_val = 8; + bits_N_for_comp = bits_N_for_comp - 9; + if (threshold > 2) { threshold = threshold - 1; - } else if (threshold == 1) { // keep threshold at ones, but you're probably screwed with an unlucky bit error - threshold = 1; + } else if (threshold == 2) { // keep threshold at ones, but you're probably screwed with an unlucky bit error + threshold = 2; } } else { *start_val = *start_val + 1; - bits_N_for_comp = bits_N_for_comp - 1; + bits_N_for_comp -= 1; } test_iteration++; + // too few bits to reliably compare, give up + if (bits_N_for_comp < 19) { + NRF_P1->OUTSET = 1 << 7; + selected_poly = LH2_4_POLYNOMIAL_ERROR_INDICATOR; // mark the poly as "wrong" + break; + } } return selected_poly; } From 324fa14ddceca6ae07339575ddcbaf6dff657c25 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Mon, 15 Jan 2024 17:32:42 +0100 Subject: [PATCH 03/13] #271 LH2: setup code to benchmark the _reverse_count_p function --- bsp/nrf/lh2_4.c | 137 ++++++++++++++---- .../01bsp_lighthouse_4/01bsp_lighthouse_4.c | 8 +- 2 files changed, 116 insertions(+), 29 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index a86d9cb6b..9ee01ba44 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -299,6 +299,7 @@ uint64_t _hamming_weight(uint64_t bits_in); * @return count: location of the sequence */ uint32_t _reverse_count_p(uint8_t index, uint32_t bits); +uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits); /** * @brief Set a gpio as an INPUT with no pull-up or pull-down @@ -432,7 +433,7 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { uint32_t temp_timestamp; uint64_t temp_bits_sweep; uint8_t temp_selected_polynomial; - uint8_t temp_selected_polynomial_no_test; + //uint8_t temp_selected_polynomial_no_test; int8_t temp_bit_offset; int8_t sweep; @@ -448,18 +449,9 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { // convert the SPI reading to bits via zero-crossing counter demodulation and differential/biphasic manchester decoding temp_bits_sweep = _demodulate_light(temp_spi_bits); // figure out which polynomial each one of the two samples come from. - temp_selected_polynomial_no_test = _determine_polynomial(temp_bits_sweep, &temp_bit_offset); + //temp_selected_polynomial_no_test = _determine_polynomial(temp_bits_sweep, &temp_bit_offset); - NRF_P1->OUTCLR = 1 << 7; - NRF_P1->OUTCLR = 1 << 11; - NRF_P0->OUTCLR = 1 << 28; - NRF_P0->OUTSET = 1 << 29; temp_selected_polynomial = _determine_polynomial_test(temp_bits_sweep, &temp_bit_offset); - NRF_P0->OUTCLR = 1 << 29; - - if (temp_selected_polynomial != temp_selected_polynomial_no_test){ - NRF_P1->OUTSET = 1 << 11; - } // If there was an error with the polynomial, leave without updating anything if (temp_selected_polynomial == LH2_4_POLYNOMIAL_ERROR_INDICATOR){ @@ -486,6 +478,8 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { void db_lh2_4_process_location(db_lh2_4_t *lh2) { + uint32_t lfsr_loc_temp, lsfr_loc_temp_test; + // compute LFSR locations and detect invalid packets for (uint8_t basestation = 0; basestation < LH2_4_BASESTATION_COUNT; basestation++) { for (uint8_t sweep = 0; sweep < 2; sweep++){ @@ -496,19 +490,33 @@ void db_lh2_4_process_location(db_lh2_4_t *lh2) { // Copy the selected polynomial lh2->locations[sweep][basestation].selected_polynomial = lh2->raw_data[sweep][basestation].selected_polynomial; // Copmute and save the lsfr location. - lh2->locations[sweep][basestation].lfsr_location = _reverse_count_p( + lfsr_loc_temp = _reverse_count_p( lh2->raw_data[sweep][basestation].selected_polynomial, lh2->raw_data[sweep][basestation].bits_sweep >> (47 - lh2->raw_data[sweep][basestation].bit_offset)) - - lh2->raw_data[sweep][basestation].bit_offset; + lh2->raw_data[sweep][basestation].bit_offset; + + lh2->locations[sweep][basestation].lfsr_location = lfsr_loc_temp; + + NRF_P1->OUTCLR = 1 << 11; + NRF_P0->OUTSET = 1 << 29; + lsfr_loc_temp_test = _reverse_count_p_test( + lh2->raw_data[sweep][basestation].selected_polynomial, + lh2->raw_data[sweep][basestation].bits_sweep >> (47 - lh2->raw_data[sweep][basestation].bit_offset)) - + lh2->raw_data[sweep][basestation].bit_offset; + NRF_P0->OUTCLR = 1 << 29; + lh2->locations[sweep][basestation].lfsr_location = lsfr_loc_temp_test; + + + if (lfsr_loc_temp != lsfr_loc_temp_test){ + NRF_P1->OUTSET = 1 << 11; + } + // Mark the data point as processed lh2->data_ready[sweep][basestation] = DB_LH2_4_PROCESSED_DATA_AVAILABLE; } - } } - - } //=========================== private ========================================== @@ -1005,8 +1013,6 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { uint64_t min_weight = LH2_4_POLYNOMIAL_ERROR_INDICATOR; uint64_t bits_to_compare = 0; int32_t threshold = POLYNOMIAL_BIT_ERROR_INITIAL_THRESHOLD; - - uint32_t test_iteration = 0; // try polynomial vs. first buffer bits // this search takes 17-bit sequences and runs them forwards through the polynomial LFSRs. @@ -1018,10 +1024,6 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { // run polynomial search on the first capture while (1) { - if(test_iteration >=1){ - NRF_P0->OUTSET = 1 << 28; - } - // TODO: do this math stuff in multiple operations to: (a) make it readable (b) ensure order-of-execution bit_buffer1 = (uint32_t)(((0xFFFF800000000000 >> (*start_val)) & chipsH1) >> (64 - 17 - (*start_val))); bits_to_compare = (chipsH1 & (0xFFFFFFFFFFFFFFFF << (64 - 17 - (*start_val) - bits_N_for_comp))); @@ -1057,10 +1059,8 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { bits_N_for_comp -= 1; } - test_iteration++; // too few bits to reliably compare, give up - if (bits_N_for_comp < 19) { - NRF_P1->OUTSET = 1 << 7; + if (bits_N_for_comp < 19) { selected_poly = LH2_4_POLYNOMIAL_ERROR_INDICATOR; // mark the poly as "wrong" break; } @@ -1163,6 +1163,93 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { return count; } +uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { + uint32_t count = 0; + uint32_t buffer = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked + uint8_t ii = 0; // loop variable for cumulative sum + uint32_t result = 0; + uint32_t b17 = 0; + uint32_t masked_buff = 0; + while (buffer != _end_buffers[index][0]) // do until buffer reaches one of the saved states + { + NRF_P1->OUTSET = 1 << 07; + b17 = buffer & 0x00000001; // save the "newest" bit of the buffer + buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time + masked_buff = (buffer) & (_polynomials[index]); // mask the buffer w/ the selected polynomial + for (ii = 0; ii < 17; ii++) { + result = result ^ (((masked_buff) >> ii) & (0x00000001)); // cumulative sum of buffer&poly + } + result = result ^ b17; + buffer = buffer | (result << 16); // update buffer w/ result + result = 0; // reset result + count++; + NRF_P1->OUTCLR = 1 << 07; + NRF_P0->OUTSET = 1 << 28; + if ((buffer ^ _end_buffers[index][1]) == 0x00000000) { + count = count + 8192 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][2]) == 0x00000000) { + count = count + 16384 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][3]) == 0x00000000) { + count = count + 24576 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][4]) == 0x00000000) { + count = count + 32768 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][5]) == 0x00000000) { + count = count + 40960 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][6]) == 0x00000000) { + count = count + 49152 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][7]) == 0x00000000) { + count = count + 57344 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][8]) == 0x00000000) { + count = count + 65536 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][9]) == 0x00000000) { + count = count + 73728 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][10]) == 0x00000000) { + count = count + 81920 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][11]) == 0x00000000) { + count = count + 90112 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][12]) == 0x00000000) { + count = count + 98304 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][13]) == 0x00000000) { + count = count + 106496 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][14]) == 0x00000000) { + count = count + 114688 - 1; + buffer = _end_buffers[index][0]; + } + if ((buffer ^ _end_buffers[index][15]) == 0x00000000) { + count = count + 122880 - 1; + buffer = _end_buffers[index][0]; + } + NRF_P0->OUTCLR = 1 << 28; + } + return count; +} + void _lh2_4_pin_set_input(const gpio_t *gpio) { // Configure Data pin as INPUT, with no pullup or pull down. nrf_port[gpio->port]->PIN_CNF[gpio->pin] = (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | diff --git a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c index 2af559f3a..b1ed30e42 100644 --- a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c +++ b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c @@ -31,10 +31,10 @@ static db_lh2_4_t _lh2; /* P0.30 -> TS4231 Data pin P1.04 -> SPI SCLK -P0.29 -> High -> at the start of _determine_polynomial() - Low - at the end of _determine_polynomial() -P1.07 -> Goes briefly HIGH if there _determine_polynomial() failed to determine the polynomial -P0.28 -> Goes high if _determine_polynomial() took more than 1 iteration to find the polynomial +P0.29 -> High -> at the start of _reverse_p_count() + Low - at the end of _reverse_p_count() +P1.07 -> HIGH during the LSFR update in _reverse_p_count() +P0.28 -> HIGH during the checkpoint check in _reverse_p_count(). P1.11 -> Goes high if the original function and the function with the changes being tested gives differet results */ From 511d69c05d398076690604f42754bdded7291c49 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Mon, 15 Jan 2024 19:11:02 +0100 Subject: [PATCH 04/13] LH2 #271 benchmark test LSFR computation in _reverse_count_p() --- bsp/nrf/lh2_4.c | 14 ++++++++++---- projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 9ee01ba44..261cc24f3 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -1170,21 +1170,27 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { uint32_t result = 0; uint32_t b17 = 0; uint32_t masked_buff = 0; + while (buffer != _end_buffers[index][0]) // do until buffer reaches one of the saved states { - NRF_P1->OUTSET = 1 << 07; + b17 = buffer & 0x00000001; // save the "newest" bit of the buffer buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time masked_buff = (buffer) & (_polynomials[index]); // mask the buffer w/ the selected polynomial + NRF_P1->OUTSET = 1 << 07; for (ii = 0; ii < 17; ii++) { - result = result ^ (((masked_buff) >> ii) & (0x00000001)); // cumulative sum of buffer&poly + result = result ^ (((masked_buff) >> ii) & (0x00000001)); // cumulative sum of buffer&poly } result = result ^ b17; buffer = buffer | (result << 16); // update buffer w/ result - result = 0; // reset result - count++; + result = 0; // reset result + NRF_P1->OUTCLR = 1 << 07; NRF_P0->OUTSET = 1 << 28; + buffer = buffer | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); + NRF_P0->OUTCLR = 1 << 28; + count++; + if ((buffer ^ _end_buffers[index][1]) == 0x00000000) { count = count + 8192 - 1; buffer = _end_buffers[index][0]; diff --git a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c index b1ed30e42..86a6f54c9 100644 --- a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c +++ b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c @@ -66,6 +66,7 @@ int main(void) { db_lh2_4_process_location(&_lh2); __NOP(); } + //db_lh2_4_start(&_lh2); From fb7a2aa5f437f83631180b767e5d785d9c5282ee Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Tue, 16 Jan 2024 14:44:59 +0100 Subject: [PATCH 05/13] #271 LH2: Make local copies of _end_buffers to speed up _reverse_count_p --- bsp/nrf/lh2_4.c | 100 ++++++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 261cc24f3..74a258162 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -1166,92 +1166,100 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { uint32_t count = 0; uint32_t buffer = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked - uint8_t ii = 0; // loop variable for cumulative sum - uint32_t result = 0; uint32_t b17 = 0; uint32_t masked_buff = 0; + //uint32_t result_test = 0; - while (buffer != _end_buffers[index][0]) // do until buffer reaches one of the saved states + NRF_P1->OUTSET = 1 << 07; + // Copy const variables (Flash) into local variables (RAM) to speed up execution. + uint32_t _end_buffers_local[16]; + uint32_t polynomials_local = _polynomials[index]; + + for (size_t i = 0; i < 16; i++) + { + _end_buffers_local[i] = _end_buffers[index][i]; + } + NRF_P1->OUTCLR = 1 << 07; + + + while (buffer != _end_buffers_local[0]) // do until buffer reaches one of the saved states { + b17 = buffer & 0x00000001; // save the "newest" bit of the buffer buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time - masked_buff = (buffer) & (_polynomials[index]); // mask the buffer w/ the selected polynomial - NRF_P1->OUTSET = 1 << 07; - for (ii = 0; ii < 17; ii++) { - result = result ^ (((masked_buff) >> ii) & (0x00000001)); // cumulative sum of buffer&poly - } - result = result ^ b17; - buffer = buffer | (result << 16); // update buffer w/ result - result = 0; // reset result + masked_buff = (buffer) & (polynomials_local); // mask the buffer w/ the selected polynomial + buffer = buffer | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); // This weird line propagates the LSFR one bit into the past + count++; + - NRF_P1->OUTCLR = 1 << 07; + + + NRF_P0->OUTSET = 1 << 28; - buffer = buffer | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); - NRF_P0->OUTCLR = 1 << 28; - count++; - - if ((buffer ^ _end_buffers[index][1]) == 0x00000000) { + if (buffer == _end_buffers_local[1]) { count = count + 8192 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][2]) == 0x00000000) { + if (buffer == _end_buffers_local[2]) { count = count + 16384 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][3]) == 0x00000000) { + if (buffer == _end_buffers_local[3]) { count = count + 24576 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][4]) == 0x00000000) { + if (buffer == _end_buffers_local[4]) { count = count + 32768 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][5]) == 0x00000000) { + if (buffer == _end_buffers_local[5]) { count = count + 40960 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][6]) == 0x00000000) { + if (buffer == _end_buffers_local[6]) { count = count + 49152 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][7]) == 0x00000000) { + if (buffer == _end_buffers_local[7]) { count = count + 57344 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][8]) == 0x00000000) { + if (buffer == _end_buffers_local[8]) { count = count + 65536 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][9]) == 0x00000000) { + if (buffer == _end_buffers_local[9]) { count = count + 73728 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][10]) == 0x00000000) { + if (buffer == _end_buffers_local[10]) { count = count + 81920 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][11]) == 0x00000000) { + if (buffer == _end_buffers_local[11]) { count = count + 90112 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][12]) == 0x00000000) { + if (buffer == _end_buffers_local[12]) { count = count + 98304 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][13]) == 0x00000000) { + if (buffer == _end_buffers_local[13]) { count = count + 106496 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][14]) == 0x00000000) { + if (buffer == _end_buffers_local[14]) { count = count + 114688 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } - if ((buffer ^ _end_buffers[index][15]) == 0x00000000) { + if (buffer == _end_buffers_local[15]) { count = count + 122880 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_local[0]; } NRF_P0->OUTCLR = 1 << 28; + + } return count; } From 4e8d846e7dcf49b7109aa7a3fbb4cefddc5ca259 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Wed, 17 Jan 2024 14:45:29 +0100 Subject: [PATCH 06/13] #271 LH2: lsfr checkpoint check with hash table --- bsp/nrf/lh2_4.c | 357 ++++++++++++++++++++++-------------------------- 1 file changed, 164 insertions(+), 193 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 74a258162..5134bbb79 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -35,6 +35,10 @@ #define PPI_SPI_START_CHAN 2 #define PPI_SPI_STOP_CHAN 3 #define PPI_SPI_GROUP 0 +#define HASH_TABLE_BITS 11 ///< How many bits will be used for the hashtable for the _end_buffers +#define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS) ///< How big will the hashtable for the _end_buffers +#define HASH_TABLE_MASK ((1 << HASH_TABLE_BITS) - 1) ///< Mask selecting the HAS_TABLE_BITS least significant bits +#define NUM_LSFR_COUNT_CHECKPOINTS 16 ///< How many lsfr checkpoints are per polynomial #if defined(NRF5340_XXAA) && defined(NRF_APPLICATION) #define NRF_SPIM NRF_SPIM4_S @@ -67,7 +71,7 @@ typedef struct { //=========================== variables ======================================== -static const uint32_t _polynomials[8] = { +static const uint32_t _polynomials[LH2_4_BASESTATION_COUNT*2] = { 0x0001D258, 0x00017E04, 0x0001FF6B, @@ -78,161 +82,163 @@ static const uint32_t _polynomials[8] = { 0x00018A55, }; -static const uint32_t _end_buffers[8][16] = { +static const uint32_t _end_buffers[LH2_4_BASESTATION_COUNT*2][NUM_LSFR_COUNT_CHECKPOINTS] = { { // p0 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b10101010110011101, // 1/16 way through - 0b10001010101011010, // 2/16 way through - 0b11001100100000010, // 3/16 way through - 0b01100101100011111, // 4/16 way through - 0b10010001101011110, // 5/16 way through - 0b10100011001011111, // 6/16 way through - 0b11110001010110001, // 7/16 way through - 0b10111000110011011, // 8/16 way through - 0b10100110100011110, // 9/16 way through - 0b11001101100010000, // 10/16 way through - 0b01000101110011111, // 11/16 way through - 0b11100101011110101, // 12/16 way through - 0b01001001110110111, // 13/16 way through - 0b11011100110011101, // 14/16 way through - 0b10000110101101011, // 15/16 way through + 0b10101010110011101, // 1/16 way + 0b10001010101011010, // 2/16 way + 0b11001100100000010, // 3/16 way + 0b01100101100011111, // 4/16 way + 0b10010001101011110, // 5/16 way + 0b10100011001011111, // 6/16 way + 0b11110001010110001, // 7/16 way + 0b10111000110011011, // 8/16 way + 0b10100110100011110, // 9/16 way + 0b11001101100010000, // 10/16 way + 0b01000101110011111, // 11/16 way + 0b11100101011110101, // 12/16 way + 0b01001001110110111, // 13/16 way + 0b11011100110011101, // 14/16 way + 0b10000110101101011, // 15/16 way }, { // p1 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11010000110111110, // 1/16 way through - 0b10110111100111100, // 2/16 way through - 0b11000010101101111, // 3/16 way through - 0b00101110001101110, // 4/16 way through - 0b01000011000110100, // 5/16 way through - 0b00010001010011110, // 6/16 way through - 0b10100101111010001, // 7/16 way through - 0b10011000000100001, // 8/16 way through - 0b01110011011010110, // 9/16 way through - 0b00100011101000011, // 10/16 way through - 0b10111011010000101, // 11/16 way through - 0b00110010100110110, // 12/16 way through - 0b01000111111100110, // 13/16 way through - 0b10001101000111011, // 14/16 way through - 0b00111100110011100, // 15/16 way through + 0b11010000110111110, // 1/16 way + 0b10110111100111100, // 2/16 way + 0b11000010101101111, // 3/16 way + 0b00101110001101110, // 4/16 way + 0b01000011000110100, // 5/16 way + 0b00010001010011110, // 6/16 way + 0b10100101111010001, // 7/16 way + 0b10011000000100001, // 8/16 way + 0b01110011011010110, // 9/16 way + 0b00100011101000011, // 10/16 way + 0b10111011010000101, // 11/16 way + 0b00110010100110110, // 12/16 way + 0b01000111111100110, // 13/16 way + 0b10001101000111011, // 14/16 way + 0b00111100110011100, // 15/16 way }, { // p2 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b00011011011000100, // 1/16 way through - 0b01011101010010110, // 2/16 way through - 0b11001011001101010, // 3/16 way through - 0b01110001111011010, // 4/16 way through - 0b10110110011111010, // 5/16 way through - 0b10110001110000001, // 6/16 way through - 0b10001001011101001, // 7/16 way through - 0b00000010011101011, // 8/16 way through - 0b01100010101111011, // 9/16 way through - 0b00111000001101111, // 10/16 way through - 0b10101011100111000, // 11/16 way through - 0b01111110101111111, // 12/16 way through - 0b01000011110101010, // 13/16 way through - 0b01001011100000011, // 14/16 way through - 0b00010110111101110, // 15/16 way through + 0b00011011011000100, // 1/16 way + 0b01011101010010110, // 2/16 way + 0b11001011001101010, // 3/16 way + 0b01110001111011010, // 4/16 way + 0b10110110011111010, // 5/16 way + 0b10110001110000001, // 6/16 way + 0b10001001011101001, // 7/16 way + 0b00000010011101011, // 8/16 way + 0b01100010101111011, // 9/16 way + 0b00111000001101111, // 10/16 way + 0b10101011100111000, // 11/16 way + 0b01111110101111111, // 12/16 way + 0b01000011110101010, // 13/16 way + 0b01001011100000011, // 14/16 way + 0b00010110111101110, // 15/16 way }, { // p3 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11011011110010110, // 1/16 way through - 0b11000100000001101, // 2/16 way through - 0b11100011000010110, // 3/16 way through - 0b00011111010001100, // 4/16 way through - 0b11000001011110011, // 5/16 way through - 0b10011101110001010, // 6/16 way through - 0b00001011001111000, // 7/16 way through - 0b00111100010000101, // 8/16 way through - 0b01001111001010100, // 9/16 way through - 0b01011010010110011, // 10/16 way through - 0b11111101010001100, // 11/16 way through - 0b00110101011011111, // 12/16 way through - 0b01110110010101011, // 13/16 way through - 0b00010000110100010, // 14/16 way through - 0b00010111110101110, // 15/16 way through + 0b11011011110010110, // 1/16 way + 0b11000100000001101, // 2/16 way + 0b11100011000010110, // 3/16 way + 0b00011111010001100, // 4/16 way + 0b11000001011110011, // 5/16 way + 0b10011101110001010, // 6/16 way + 0b00001011001111000, // 7/16 way + 0b00111100010000101, // 8/16 way + 0b01001111001010100, // 9/16 way + 0b01011010010110011, // 10/16 way + 0b11111101010001100, // 11/16 way + 0b00110101011011111, // 12/16 way + 0b01110110010101011, // 13/16 way + 0b00010000110100010, // 14/16 way + 0b00010111110101110, // 15/16 way }, { // p4 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11000011111110101, // 1/16 way through - 0b01111110000100101, // 2/16 way through - 0b01011100011111100, // 3/16 way through - 0b11001000111100000, // 4/16 way through - 0b00010001010010110, // 5/16 way through - 0b00100010100111011, // 6/16 way through - 0b10111101100101000, // 7/16 way through - 0b01000101010111010, // 8/16 way through - 0b10111101100011011, // 9/16 way through - 0b11110100111101000, // 10/16 way through - 0b11100010000111100, // 11/16 way through - 0b11010010101101010, // 12/16 way through - 0b01101100110010010, // 13/16 way through - 0b11111000100010000, // 14/16 way through - 0b00111011101100011, // 15/16 way through + 0b11000011111110101, // 1/16 way + 0b01111110000100101, // 2/16 way + 0b01011100011111100, // 3/16 way + 0b11001000111100000, // 4/16 way + 0b00010001010010110, // 5/16 way + 0b00100010100111011, // 6/16 way + 0b10111101100101000, // 7/16 way + 0b01000101010111010, // 8/16 way + 0b10111101100011011, // 9/16 way + 0b11110100111101000, // 10/16 way + 0b11100010000111100, // 11/16 way + 0b11010010101101010, // 12/16 way + 0b01101100110010010, // 13/16 way + 0b11111000100010000, // 14/16 way + 0b00111011101100011, // 15/16 way }, { // p5 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11001000010011110, // 1/16 way through - 0b10011010101100010, // 2/16 way through - 0b01100111010001011, // 3/16 way through - 0b11011011100110100, // 4/16 way through - 0b01001000001001100, // 5/16 way through - 0b11000001001111010, // 6/16 way through - 0b11001000010010000, // 7/16 way through - 0b10001110110110000, // 8/16 way through - 0b11111001001100101, // 9/16 way through - 0b01100001010010111, // 10/16 way through - 0b01000110101100010, // 11/16 way through - 0b11010011000101000, // 12/16 way through - 0b01011001111000111, // 13/16 way through - 0b10011010110011010, // 14/16 way through - 0b00001001000001110, // 15/16 way through + 0b11001000010011110, // 1/16 way + 0b10011010101100010, // 2/16 way + 0b01100111010001011, // 3/16 way + 0b11011011100110100, // 4/16 way + 0b01001000001001100, // 5/16 way + 0b11000001001111010, // 6/16 way + 0b11001000010010000, // 7/16 way + 0b10001110110110000, // 8/16 way + 0b11111001001100101, // 9/16 way + 0b01100001010010111, // 10/16 way + 0b01000110101100010, // 11/16 way + 0b11010011000101000, // 12/16 way + 0b01011001111000111, // 13/16 way + 0b10011010110011010, // 14/16 way + 0b00001001000001110, // 15/16 way }, { // p6 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b01110110101110110, // 1/16 way through - 0b00111111000100011, // 2/16 way through - 0b10011010110110011, // 3/16 way through - 0b11110001001110000, // 4/16 way through - 0b10001101001000111, // 5/16 way through - 0b01001000110110000, // 6/16 way through - 0b01000011100101101, // 7/16 way through - 0b00100110001001001, // 8/16 way through - 0b11001011101000100, // 9/16 way through - 0b11001011011100111, // 10/16 way through - 0b11001101100101000, // 11/16 way through - 0b00100011001101100, // 12/16 way through - 0b01011101001100000, // 13/16 way through - 0b11111001011001111, // 14/16 way through - 0b01001101000100110, // 15/16 way through + 0b01110110101110110, // 1/16 way + 0b00111111000100011, // 2/16 way + 0b10011010110110011, // 3/16 way + 0b11110001001110000, // 4/16 way + 0b10001101001000111, // 5/16 way + 0b01001000110110000, // 6/16 way + 0b01000011100101101, // 7/16 way + 0b00100110001001001, // 8/16 way + 0b11001011101000100, // 9/16 way + 0b11001011011100111, // 10/16 way + 0b11001101100101000, // 11/16 way + 0b00100011001101100, // 12/16 way + 0b01011101001100000, // 13/16 way + 0b11111001011001111, // 14/16 way + 0b01001101000100110, // 15/16 way }, { // p7 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b01000010111001111, // 1/16 way through - 0b00011111000011111, // 2/16 way through - 0b00100101010100111, // 3/16 way through - 0b10001110011000110, // 4/16 way through - 0b01000011011111110, // 5/16 way through - 0b01001001000001101, // 6/16 way through - 0b01110011110100100, // 7/16 way through - 0b10010000110101010, // 8/16 way through - 0b01010001101010011, // 9/16 way through - 0b00011011000110101, // 10/16 way through - 0b11001100101110100, // 11/16 way through - 0b10011000101101111, // 12/16 way through - 0b10100001000111100, // 13/16 way through - 0b11110100001010000, // 14/16 way through - 0b01010010010011110, // 15/16 way through + 0b01000010111001111, // 1/16 way + 0b00011111000011111, // 2/16 way + 0b00100101010100111, // 3/16 way + 0b10001110011000110, // 4/16 way + 0b01000011011111110, // 5/16 way + 0b01001001000001101, // 6/16 way + 0b01110011110100100, // 7/16 way + 0b10010000110101010, // 8/16 way + 0b01010001101010011, // 9/16 way + 0b00011011000110101, // 10/16 way + 0b11001100101110100, // 11/16 way + 0b10011000101101111, // 12/16 way + 0b10100001000111100, // 13/16 way + 0b11110100001010000, // 14/16 way + 0b01010010010011110, // 15/16 way }, }; +static uint8_t _end_buffers_hashtable[HASH_TABLE_SIZE] = {0}; + ///! NOTE: SPIM needs an SCK pin to be defined, P1.6 is used because it's not an available pin in the BCM module static const gpio_t _lh2_4_spi_fake_sck_gpio = { .port = 1, @@ -353,6 +359,13 @@ void _add_to_spi_ring_buffer(lh2_4_ring_buffer_t *cb, uint8_t data[SPI_BUFFER_SI */ bool _get_from_spi_ring_buffer(lh2_4_ring_buffer_t *cb, uint8_t data[SPI_BUFFER_SIZE], uint32_t *timestamp); +/** + * @brief retreive the oldest element from the ring buffer for spi captures + * + * @param[in] cb pointer to ring buffer structure + */ +void _fill_hash_table(uint8_t * hash_table); + //=========================== public =========================================== void db_lh2_4_init(db_lh2_4_t *lh2, const gpio_t *gpio_d, const gpio_t *gpio_e) { @@ -389,6 +402,10 @@ void db_lh2_4_init(db_lh2_4_t *lh2, const gpio_t *gpio_d, const gpio_t *gpio_e) } memset(_lh2_4_vars.data.buffer[0], 0, LH2_4_BUFFER_SIZE); + + //Initialize the hash table for the lsfr checkpoints + _fill_hash_table(_end_buffers_hashtable); + // initialize GPIOTEs _gpiote_setup(gpio_e); @@ -1168,98 +1185,40 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { uint32_t buffer = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked uint32_t b17 = 0; uint32_t masked_buff = 0; + uint8_t hash_index = 0; //uint32_t result_test = 0; - NRF_P1->OUTSET = 1 << 07; + // Copy const variables (Flash) into local variables (RAM) to speed up execution. - uint32_t _end_buffers_local[16]; + uint32_t _end_buffers_local[NUM_LSFR_COUNT_CHECKPOINTS]; uint32_t polynomials_local = _polynomials[index]; - for (size_t i = 0; i < 16; i++) + for (size_t i = 0; i < NUM_LSFR_COUNT_CHECKPOINTS; i++) { _end_buffers_local[i] = _end_buffers[index][i]; } - NRF_P1->OUTCLR = 1 << 07; + while (buffer != _end_buffers_local[0]) // do until buffer reaches one of the saved states { - - + + NRF_P1->OUTSET = 1 << 07; b17 = buffer & 0x00000001; // save the "newest" bit of the buffer buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time masked_buff = (buffer) & (polynomials_local); // mask the buffer w/ the selected polynomial buffer = buffer | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); // This weird line propagates the LSFR one bit into the past - count++; - - - - + count++; + NRF_P1->OUTCLR = 1 << 07; NRF_P0->OUTSET = 1 << 28; - if (buffer == _end_buffers_local[1]) { - count = count + 8192 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[2]) { - count = count + 16384 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[3]) { - count = count + 24576 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[4]) { - count = count + 32768 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[5]) { - count = count + 40960 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[6]) { - count = count + 49152 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[7]) { - count = count + 57344 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[8]) { - count = count + 65536 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[9]) { - count = count + 73728 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[10]) { - count = count + 81920 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[11]) { - count = count + 90112 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[12]) { - count = count + 98304 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[13]) { - count = count + 106496 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[14]) { - count = count + 114688 - 1; - buffer = _end_buffers_local[0]; - } - if (buffer == _end_buffers_local[15]) { - count = count + 122880 - 1; + hash_index = _end_buffers_hashtable[buffer & HASH_TABLE_MASK]; + if (buffer == _end_buffers_local[hash_index]){ + count = count + 8192*hash_index - 1; buffer = _end_buffers_local[0]; } + NRF_P0->OUTCLR = 1 << 28; - - } return count; } @@ -1391,7 +1350,19 @@ bool _get_from_spi_ring_buffer(lh2_4_ring_buffer_t *cb, uint8_t data[SPI_BUFFER_ return true; } +void _fill_hash_table(uint8_t * hash_table){ + // Iterate over all the checkpoints and save the HASH_TABLE_BITS 11 bits as a a index for the hashtable + for (size_t poly = 0; poly < LH2_4_BASESTATION_COUNT*2; poly++) + { + for (size_t checkpoint = 1; checkpoint < NUM_LSFR_COUNT_CHECKPOINTS; checkpoint++) + { + hash_table[_end_buffers[poly][checkpoint] & HASH_TABLE_MASK] = checkpoint; + } + + } + +} //=========================== interrupts ======================================= From 21c3522926e2a785939803af4dc410f3e223c9fa Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Wed, 17 Jan 2024 19:58:58 +0100 Subject: [PATCH 07/13] #271 LH2: reverse_count_p() counts up AND down --- bsp/nrf/lh2_4.c | 56 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 5134bbb79..dd8a09e82 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -1116,6 +1116,7 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { buffer = buffer | (result << 16); // update buffer w/ result result = 0; // reset result count++; + if ((buffer ^ _end_buffers[index][1]) == 0x00000000) { count = count + 8192 - 1; buffer = _end_buffers[index][0]; @@ -1176,51 +1177,68 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { count = count + 122880 - 1; buffer = _end_buffers[index][0]; } + } return count; } uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { - uint32_t count = 0; - uint32_t buffer = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked + uint32_t count_down = 0; + uint32_t count_up = 0; + uint32_t buffer_down = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked + uint32_t buffer_up = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked uint32_t b17 = 0; + uint32_t b1 = 0; uint32_t masked_buff = 0; - uint8_t hash_index = 0; - //uint32_t result_test = 0; - + uint8_t hash_index_down = 0; + uint8_t hash_index_up = 0; // Copy const variables (Flash) into local variables (RAM) to speed up execution. uint32_t _end_buffers_local[NUM_LSFR_COUNT_CHECKPOINTS]; uint32_t polynomials_local = _polynomials[index]; - for (size_t i = 0; i < NUM_LSFR_COUNT_CHECKPOINTS; i++) { _end_buffers_local[i] = _end_buffers[index][i]; } - - while (buffer != _end_buffers_local[0]) // do until buffer reaches one of the saved states + while (buffer_up != _end_buffers_local[0]) // do until buffer reaches one of the saved states { - NRF_P1->OUTSET = 1 << 07; - b17 = buffer & 0x00000001; // save the "newest" bit of the buffer - buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time - masked_buff = (buffer) & (polynomials_local); // mask the buffer w/ the selected polynomial - buffer = buffer | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); // This weird line propagates the LSFR one bit into the past - count++; + // LSFR backward update + b17 = buffer_down & 0x00000001; // save the "newest" bit of the buffer + buffer_down = (buffer_down & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time + masked_buff = (buffer_down) & (polynomials_local); // mask the buffer w/ the selected polynomial + buffer_down = buffer_down | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); // This weird line propagates the LSFR one bit into the past + count_down++; + + // LSFR forward update + b1 = __builtin_popcount(buffer_up & polynomials_local) & 0x01; // mask the buffer w/ the selected polynomial + buffer_up = ((buffer_up << 1) | b1) & (0x0001FFFF); + count_up++; NRF_P1->OUTCLR = 1 << 07; NRF_P0->OUTSET = 1 << 28; - hash_index = _end_buffers_hashtable[buffer & HASH_TABLE_MASK]; - if (buffer == _end_buffers_local[hash_index]){ - count = count + 8192*hash_index - 1; - buffer = _end_buffers_local[0]; + // Check point check backward + hash_index_down = _end_buffers_hashtable[buffer_down & HASH_TABLE_MASK]; + if (buffer_down == _end_buffers_local[hash_index_down]){ + count_down = count_down + 8192*hash_index_down - 1; + buffer_down = _end_buffers_local[0]; + return count_down; + } + + // Check point check upward + hash_index_up = _end_buffers_hashtable[buffer_up & HASH_TABLE_MASK]; + if (buffer_up == _end_buffers_local[hash_index_up]){ + count_up = 8192*hash_index_up - count_up - 1; + buffer_up = _end_buffers_local[0]; + return count_up; } NRF_P0->OUTCLR = 1 << 28; + } - return count; + return count_up; } void _lh2_4_pin_set_input(const gpio_t *gpio) { From 37422e0497a18974b0d08175b0d4d128d2b19d26 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 19 Jan 2024 11:04:57 +0100 Subject: [PATCH 08/13] #271 LH2: Try hashtables for dynamic checkpoints --- bsp/nrf/lh2_4.c | 58 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index dd8a09e82..67d8dc319 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -39,6 +39,10 @@ #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS) ///< How big will the hashtable for the _end_buffers #define HASH_TABLE_MASK ((1 << HASH_TABLE_BITS) - 1) ///< Mask selecting the HAS_TABLE_BITS least significant bits #define NUM_LSFR_COUNT_CHECKPOINTS 16 ///< How many lsfr checkpoints are per polynomial +#define CHECKPOINT_TABLE_BITS 5 ///< How many bits will be used for the checkpoint table for the lfsr search +#define CHECKPOINT_TABLE_SIZE (1 << CHECKPOINT_TABLE_BITS) ///< How big will the checkpoint table for the lfsr search +#define CHECKPOINT_TABLE_MASK ((1 << CHECKPOINT_TABLE_BITS) - 1) ///< Mask selecting the CHECKPOINT_TABLE_BITS least significant bits + #if defined(NRF5340_XXAA) && defined(NRF_APPLICATION) #define NRF_SPIM NRF_SPIM4_S @@ -239,6 +243,10 @@ static const uint32_t _end_buffers[LH2_4_BASESTATION_COUNT*2][NUM_LSFR_COUNT_CHE static uint8_t _end_buffers_hashtable[HASH_TABLE_SIZE] = {0}; +// Dynamic checkpoint +static uint32_t _lfsr_checkpoint_hashtable[LH2_4_BASESTATION_COUNT * 2][CHECKPOINT_TABLE_SIZE] = {0}; +static uint32_t _lfsr_checkpoint_count[LH2_4_BASESTATION_COUNT * 2][CHECKPOINT_TABLE_SIZE] = {0}; + ///! NOTE: SPIM needs an SCK pin to be defined, P1.6 is used because it's not an available pin in the BCM module static const gpio_t _lh2_4_spi_fake_sck_gpio = { .port = 1, @@ -366,6 +374,14 @@ bool _get_from_spi_ring_buffer(lh2_4_ring_buffer_t *cb, uint8_t data[SPI_BUFFER_ */ void _fill_hash_table(uint8_t * hash_table); +/** + * @brief Accesses the global tables _lfsr_checkpoint_hashtable & _lfsr_checkpoint_count + * and updates them with the last found polynomial count + * + * @param[in] cb pointer to ring buffer structure + */ +void _update_lfsr_checkpoints(uint8_t polynomial, uint32_t bits, uint32_t count); + //=========================== public =========================================== void db_lh2_4_init(db_lh2_4_t *lh2, const gpio_t *gpio_d, const gpio_t *gpio_e) { @@ -1183,15 +1199,20 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { } uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { + + bits = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked + uint32_t buffer_down = bits; + uint32_t buffer_up = bits; + uint32_t count_down = 0; uint32_t count_up = 0; - uint32_t buffer_down = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked - uint32_t buffer_up = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked uint32_t b17 = 0; uint32_t b1 = 0; uint32_t masked_buff = 0; uint8_t hash_index_down = 0; uint8_t hash_index_up = 0; + uint32_t checkpoint_down; + uint32_t checkpoint_up; // Copy const variables (Flash) into local variables (RAM) to speed up execution. uint32_t _end_buffers_local[NUM_LSFR_COUNT_CHECKPOINTS]; @@ -1219,22 +1240,41 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { NRF_P1->OUTCLR = 1 << 07; NRF_P0->OUTSET = 1 << 28; - // Check point check backward + // Check end_buffer backward count hash_index_down = _end_buffers_hashtable[buffer_down & HASH_TABLE_MASK]; if (buffer_down == _end_buffers_local[hash_index_down]){ count_down = count_down + 8192*hash_index_down - 1; buffer_down = _end_buffers_local[0]; + _update_lfsr_checkpoints(index, bits, count_down); return count_down; } - // Check point check upward + // Check end_buffer forward count hash_index_up = _end_buffers_hashtable[buffer_up & HASH_TABLE_MASK]; if (buffer_up == _end_buffers_local[hash_index_up]){ count_up = 8192*hash_index_up - count_up - 1; buffer_up = _end_buffers_local[0]; + _update_lfsr_checkpoints(index, bits, count_up); + return count_up; + } + + // Check the dynamical checkpoints, backward + checkpoint_down = _lfsr_checkpoint_hashtable[index][buffer_down & CHECKPOINT_TABLE_MASK]; + if (buffer_down == checkpoint_down){ + count_down = count_down + _lfsr_checkpoint_count[index][buffer_down & CHECKPOINT_TABLE_MASK]; + buffer_down = _end_buffers_local[0]; + _update_lfsr_checkpoints(index, bits, count_down); + return count_down; + } + + // Check the dynamical checkpoints, backward + checkpoint_up = _lfsr_checkpoint_hashtable[index][buffer_up & CHECKPOINT_TABLE_MASK]; + if (buffer_up == checkpoint_up){ + count_up = _lfsr_checkpoint_count[index][buffer_up & CHECKPOINT_TABLE_MASK] - count_up; + buffer_up = _end_buffers_local[0]; + _update_lfsr_checkpoints(index, bits, count_up); return count_up; } - NRF_P0->OUTCLR = 1 << 28; } @@ -1382,6 +1422,14 @@ void _fill_hash_table(uint8_t * hash_table){ } +void _update_lfsr_checkpoints(uint8_t polynomial, uint32_t bits, uint32_t count){ + + // Update the hashtable with the new values + _lfsr_checkpoint_hashtable[polynomial][bits & CHECKPOINT_TABLE_MASK] = bits; + + // Update count table + _lfsr_checkpoint_count[polynomial][bits & CHECKPOINT_TABLE_MASK] = count; +} //=========================== interrupts ======================================= From 339fcdf06a662f1893adbfb7e77d903c3f2e98e9 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 19 Jan 2024 16:23:04 +0100 Subject: [PATCH 09/13] #271 LH2: Add dynamically updating checkpoints --- bsp/nrf/lh2_4.c | 87 +++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 67d8dc319..59c3d94bf 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -39,9 +39,9 @@ #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS) ///< How big will the hashtable for the _end_buffers #define HASH_TABLE_MASK ((1 << HASH_TABLE_BITS) - 1) ///< Mask selecting the HAS_TABLE_BITS least significant bits #define NUM_LSFR_COUNT_CHECKPOINTS 16 ///< How many lsfr checkpoints are per polynomial -#define CHECKPOINT_TABLE_BITS 5 ///< How many bits will be used for the checkpoint table for the lfsr search -#define CHECKPOINT_TABLE_SIZE (1 << CHECKPOINT_TABLE_BITS) ///< How big will the checkpoint table for the lfsr search -#define CHECKPOINT_TABLE_MASK ((1 << CHECKPOINT_TABLE_BITS) - 1) ///< Mask selecting the CHECKPOINT_TABLE_BITS least significant bits +// #define CHECKPOINT_TABLE_BITS 5 ///< How many bits will be used for the checkpoint table for the lfsr search +// #define CHECKPOINT_TABLE_SIZE (1 << CHECKPOINT_TABLE_BITS) ///< How big will the checkpoint table for the lfsr search +// #define CHECKPOINT_TABLE_MASK ((1 << CHECKPOINT_TABLE_BITS) - 1) ///< Mask selecting the CHECKPOINT_TABLE_BITS least significant bits #if defined(NRF5340_XXAA) && defined(NRF_APPLICATION) @@ -244,9 +244,9 @@ static const uint32_t _end_buffers[LH2_4_BASESTATION_COUNT*2][NUM_LSFR_COUNT_CHE static uint8_t _end_buffers_hashtable[HASH_TABLE_SIZE] = {0}; // Dynamic checkpoint -static uint32_t _lfsr_checkpoint_hashtable[LH2_4_BASESTATION_COUNT * 2][CHECKPOINT_TABLE_SIZE] = {0}; -static uint32_t _lfsr_checkpoint_count[LH2_4_BASESTATION_COUNT * 2][CHECKPOINT_TABLE_SIZE] = {0}; - +static uint32_t _lfsr_checkpoint_bits[LH2_4_BASESTATION_COUNT * 2][2] = {0}; +static uint32_t _lfsr_checkpoint_count[LH2_4_BASESTATION_COUNT * 2][2] = {0}; +static uint32_t _lsfr_checkpoint_average = 0; ///! NOTE: SPIM needs an SCK pin to be defined, P1.6 is used because it's not an available pin in the BCM module static const gpio_t _lh2_4_spi_fake_sck_gpio = { .port = 1, @@ -1200,7 +1200,7 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { - bits = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked + bits = bits & 0x0001FFFF; // initialize buffer to initial bits, masked uint32_t buffer_down = bits; uint32_t buffer_up = bits; @@ -1211,8 +1211,6 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { uint32_t masked_buff = 0; uint8_t hash_index_down = 0; uint8_t hash_index_up = 0; - uint32_t checkpoint_down; - uint32_t checkpoint_up; // Copy const variables (Flash) into local variables (RAM) to speed up execution. uint32_t _end_buffers_local[NUM_LSFR_COUNT_CHECKPOINTS]; @@ -1225,26 +1223,15 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { while (buffer_up != _end_buffers_local[0]) // do until buffer reaches one of the saved states { - NRF_P1->OUTSET = 1 << 07; - // LSFR backward update - b17 = buffer_down & 0x00000001; // save the "newest" bit of the buffer - buffer_down = (buffer_down & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time - masked_buff = (buffer_down) & (polynomials_local); // mask the buffer w/ the selected polynomial - buffer_down = buffer_down | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); // This weird line propagates the LSFR one bit into the past - count_down++; - - // LSFR forward update - b1 = __builtin_popcount(buffer_up & polynomials_local) & 0x01; // mask the buffer w/ the selected polynomial - buffer_up = ((buffer_up << 1) | b1) & (0x0001FFFF); - count_up++; - NRF_P1->OUTCLR = 1 << 07; + // + // CHECKPOINT CHECKING + // NRF_P0->OUTSET = 1 << 28; // Check end_buffer backward count hash_index_down = _end_buffers_hashtable[buffer_down & HASH_TABLE_MASK]; if (buffer_down == _end_buffers_local[hash_index_down]){ count_down = count_down + 8192*hash_index_down - 1; - buffer_down = _end_buffers_local[0]; _update_lfsr_checkpoints(index, bits, count_down); return count_down; } @@ -1253,30 +1240,53 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { hash_index_up = _end_buffers_hashtable[buffer_up & HASH_TABLE_MASK]; if (buffer_up == _end_buffers_local[hash_index_up]){ count_up = 8192*hash_index_up - count_up - 1; - buffer_up = _end_buffers_local[0]; _update_lfsr_checkpoints(index, bits, count_up); return count_up; } // Check the dynamical checkpoints, backward - checkpoint_down = _lfsr_checkpoint_hashtable[index][buffer_down & CHECKPOINT_TABLE_MASK]; - if (buffer_down == checkpoint_down){ - count_down = count_down + _lfsr_checkpoint_count[index][buffer_down & CHECKPOINT_TABLE_MASK]; - buffer_down = _end_buffers_local[0]; + if (buffer_down == _lfsr_checkpoint_bits[index][0]){ + count_down = count_down + _lfsr_checkpoint_count[index][0]; + _update_lfsr_checkpoints(index, bits, count_down); + return count_down; + } + if (buffer_down == _lfsr_checkpoint_bits[index][1]){ + count_down = count_down + _lfsr_checkpoint_count[index][1]; _update_lfsr_checkpoints(index, bits, count_down); return count_down; } // Check the dynamical checkpoints, backward - checkpoint_up = _lfsr_checkpoint_hashtable[index][buffer_up & CHECKPOINT_TABLE_MASK]; - if (buffer_up == checkpoint_up){ - count_up = _lfsr_checkpoint_count[index][buffer_up & CHECKPOINT_TABLE_MASK] - count_up; - buffer_up = _end_buffers_local[0]; + if (buffer_up == _lfsr_checkpoint_bits[index][0]){ + count_up = _lfsr_checkpoint_count[index][0] - count_up; + _update_lfsr_checkpoints(index, bits, count_up); + return count_up; + } + if (buffer_up == _lfsr_checkpoint_bits[index][1]){ + count_up = _lfsr_checkpoint_count[index][1] - count_up; _update_lfsr_checkpoints(index, bits, count_up); return count_up; } NRF_P0->OUTCLR = 1 << 28; + + // + // LSFR UPDATE + // + NRF_P1->OUTSET = 1 << 07; + // LSFR backward update + b17 = buffer_down & 0x00000001; // save the "newest" bit of the buffer + buffer_down = (buffer_down & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time + masked_buff = (buffer_down) & (polynomials_local); // mask the buffer w/ the selected polynomial + buffer_down = buffer_down | (((__builtin_popcount(masked_buff) ^ b17) & 0x00000001) << 16); // This weird line propagates the LSFR one bit into the past + count_down++; + + // LSFR forward update + b1 = __builtin_popcount(buffer_up & polynomials_local) & 0x01; // mask the buffer w/ the selected polynomial + buffer_up = ((buffer_up << 1) | b1) & (0x0001FFFF); + count_up++; + NRF_P1->OUTCLR = 1 << 07; + } return count_up; } @@ -1424,11 +1434,16 @@ void _fill_hash_table(uint8_t * hash_table){ void _update_lfsr_checkpoints(uint8_t polynomial, uint32_t bits, uint32_t count){ - // Update the hashtable with the new values - _lfsr_checkpoint_hashtable[polynomial][bits & CHECKPOINT_TABLE_MASK] = bits; + // Update the current running weighted sum + _lsfr_checkpoint_average = (((_lsfr_checkpoint_average*3)>>2) + (count>>2)); + + // Is the new count higher or lower than the current running average. + uint8_t index = count <= _lsfr_checkpoint_average ? 0 : 1; + + // Save the new count in the correct place in the checkpoint array + _lfsr_checkpoint_bits[polynomial][index] = bits; + _lfsr_checkpoint_count[polynomial][index] = count; - // Update count table - _lfsr_checkpoint_count[polynomial][bits & CHECKPOINT_TABLE_MASK] = count; } //=========================== interrupts ======================================= From c3e5dcebd57e3865e2157e18684a5e84b7f3b9b8 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 19 Jan 2024 19:47:28 +0100 Subject: [PATCH 10/13] #271 LH2: Increase checkpoints from 16 to 64 --- bsp/nrf/lh2_4.c | 853 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 669 insertions(+), 184 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index 59c3d94bf..f73897175 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -38,10 +38,10 @@ #define HASH_TABLE_BITS 11 ///< How many bits will be used for the hashtable for the _end_buffers #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS) ///< How big will the hashtable for the _end_buffers #define HASH_TABLE_MASK ((1 << HASH_TABLE_BITS) - 1) ///< Mask selecting the HAS_TABLE_BITS least significant bits -#define NUM_LSFR_COUNT_CHECKPOINTS 16 ///< How many lsfr checkpoints are per polynomial -// #define CHECKPOINT_TABLE_BITS 5 ///< How many bits will be used for the checkpoint table for the lfsr search -// #define CHECKPOINT_TABLE_SIZE (1 << CHECKPOINT_TABLE_BITS) ///< How big will the checkpoint table for the lfsr search -// #define CHECKPOINT_TABLE_MASK ((1 << CHECKPOINT_TABLE_BITS) - 1) ///< Mask selecting the CHECKPOINT_TABLE_BITS least significant bits +#define NUM_LSFR_COUNT_CHECKPOINTS 64 ///< How many lsfr checkpoints are per polynomial +#define CHECKPOINT_TABLE_BITS 6 ///< How many bits will be used for the checkpoint table for the lfsr search +#define CHECKPOINT_TABLE_MASK_LOW ((1 << CHECKPOINT_TABLE_BITS) - 1) ///< How big will the checkpoint table for the lfsr search +#define CHECKPOINT_TABLE_MASK_HIGH (((1 << CHECKPOINT_TABLE_BITS) - 1) << CHECKPOINT_TABLE_BITS) ///< Mask selecting the CHECKPOINT_TABLE_BITS least significant bits #if defined(NRF5340_XXAA) && defined(NRF_APPLICATION) @@ -86,162 +86,625 @@ static const uint32_t _polynomials[LH2_4_BASESTATION_COUNT*2] = { 0x00018A55, }; -static const uint32_t _end_buffers[LH2_4_BASESTATION_COUNT*2][NUM_LSFR_COUNT_CHECKPOINTS] = { +static const uint32_t _end_buffers_old[4][16] = { { // p0 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b10101010110011101, // 1/16 way - 0b10001010101011010, // 2/16 way - 0b11001100100000010, // 3/16 way - 0b01100101100011111, // 4/16 way - 0b10010001101011110, // 5/16 way - 0b10100011001011111, // 6/16 way - 0b11110001010110001, // 7/16 way - 0b10111000110011011, // 8/16 way - 0b10100110100011110, // 9/16 way - 0b11001101100010000, // 10/16 way - 0b01000101110011111, // 11/16 way - 0b11100101011110101, // 12/16 way - 0b01001001110110111, // 13/16 way - 0b11011100110011101, // 14/16 way - 0b10000110101101011, // 15/16 way + 0b10101010110011101, // 1/16 way through + 0b10001010101011010, // 2/16 way through + 0b11001100100000010, // 3/16 way through + 0b01100101100011111, // 4/16 way through + 0b10010001101011110, // 5/16 way through + 0b10100011001011111, // 6/16 way through + 0b11110001010110001, // 7/16 way through + 0b10111000110011011, // 8/16 way through + 0b10100110100011110, // 9/16 way through + 0b11001101100010000, // 10/16 way through + 0b01000101110011111, // 11/16 way through + 0b11100101011110101, // 12/16 way through + 0b01001001110110111, // 13/16 way through + 0b11011100110011101, // 14/16 way through + 0b10000110101101011, // 15/16 way through }, { // p1 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11010000110111110, // 1/16 way - 0b10110111100111100, // 2/16 way - 0b11000010101101111, // 3/16 way - 0b00101110001101110, // 4/16 way - 0b01000011000110100, // 5/16 way - 0b00010001010011110, // 6/16 way - 0b10100101111010001, // 7/16 way - 0b10011000000100001, // 8/16 way - 0b01110011011010110, // 9/16 way - 0b00100011101000011, // 10/16 way - 0b10111011010000101, // 11/16 way - 0b00110010100110110, // 12/16 way - 0b01000111111100110, // 13/16 way - 0b10001101000111011, // 14/16 way - 0b00111100110011100, // 15/16 way + 0b11010000110111110, // 1/16 way through + 0b10110111100111100, // 2/16 way through + 0b11000010101101111, // 3/16 way through + 0b00101110001101110, // 4/16 way through + 0b01000011000110100, // 5/16 way through + 0b00010001010011110, // 6/16 way through + 0b10100101111010001, // 7/16 way through + 0b10011000000100001, // 8/16 way through + 0b01110011011010110, // 9/16 way through + 0b00100011101000011, // 10/16 way through + 0b10111011010000101, // 11/16 way through + 0b00110010100110110, // 12/16 way through + 0b01000111111100110, // 13/16 way through + 0b10001101000111011, // 14/16 way through + 0b00111100110011100, // 15/16 way through + }, + { + // p2 + 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian + 0b00011011011000100, // 1/16 way through + 0b01011101010010110, // 2/16 way through + 0b11001011001101010, // 3/16 way through + 0b01110001111011010, // 4/16 way through + 0b10110110011111010, // 5/16 way through + 0b10110001110000001, // 6/16 way through + 0b10001001011101001, // 7/16 way through + 0b00000010011101011, // 8/16 way through + 0b01100010101111011, // 9/16 way through + 0b00111000001101111, // 10/16 way through + 0b10101011100111000, // 11/16 way through + 0b01111110101111111, // 12/16 way through + 0b01000011110101010, // 13/16 way through + 0b01001011100000011, // 14/16 way through + 0b00010110111101110, // 15/16 way through + }, + { + // p3 + 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian + 0b11011011110010110, // 1/16 way through + 0b11000100000001101, // 2/16 way through + 0b11100011000010110, // 3/16 way through + 0b00011111010001100, // 4/16 way through + 0b11000001011110011, // 5/16 way through + 0b10011101110001010, // 6/16 way through + 0b00001011001111000, // 7/16 way through + 0b00111100010000101, // 8/16 way through + 0b01001111001010100, // 9/16 way through + 0b01011010010110011, // 10/16 way through + 0b11111101010001100, // 11/16 way through + 0b00110101011011111, // 12/16 way through + 0b01110110010101011, // 13/16 way through + 0b00010000110100010, // 14/16 way through + 0b00010111110101110, // 15/16 way through + }, +}; + +static const uint32_t _end_buffers[LH2_4_BASESTATION_COUNT*2][NUM_LSFR_COUNT_CHECKPOINTS] = { + { + // p0 + 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian + 0b10000000000011010, + 0b10000001010110001, + 0b10111000000110000, + 0b10101010110011101, + 0b10100001010111011, + 0b01011000111100110, + 0b00011011100000011, + 0b10001010101011010, + 0b01001010001111101, + 0b00010010100011000, + 0b00000100111000011, + 0b11001100100000010, + 0b00111111101111011, + 0b10110010010110001, + 0b01110001000110111, + 0b01100101100011111, + 0b11010001101010100, + 0b00011011101110011, + 0b10001110100000011, + 0b10010001101011110, + 0b11011011010100011, + 0b11011111100110001, + 0b00010100010110011, + 0b10100011001011111, + 0b00111110110000100, + 0b10001100001010011, + 0b11100011011100000, + 0b11110001010110001, + 0b11101000000111110, + 0b00011010010010011, + 0b10101010010100101, + 0b10111000110011011, + 0b10111111000001001, + 0b00101101110010101, + 0b10100100100011100, + 0b10100110100011110, + 0b11001000101110010, + 0b11100111100101101, + 0b00111101010100010, + 0b11001101100010000, + 0b00001001001100101, + 0b10001010111010001, + 0b01000111000001001, + 0b01000101110011111, + 0b00111100011000011, + 0b11111111000001001, + 0b11101101110000010, + 0b11100101011110101, + 0b01000010100110110, + 0b10110111000100001, + 0b10010110011001011, + 0b01001001110110111, + 0b01011011110010010, + 0b01000110110010010, + 0b01100101110010010, + 0b11011100110011101, + 0b01000011010111101, + 0b10010110101000000, + 0b01011111011001111, + 0b10000110101101011, + 0b00101100111011001, + 0b10010101110100110, + 0b00001110011011111, + }, + { + // p1 + 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian + 0b00000001111100000, + 0b11111111000111010, + 0b10111000111011000, + 0b11010000110111110, + 0b10001100001111010, + 0b10010111100011101, + 0b10011100011010010, + 0b10110111100111100, + 0b11101101100100000, + 0b11110110110010101, + 0b01110110111101110, + 0b11000010101101111, + 0b01101111000011101, + 0b01000000000111001, + 0b01010101101000101, + 0b00101110001101110, + 0b11010000111010000, + 0b00001100001001010, + 0b11011100001000011, + 0b01000011000110100, + 0b00001101001011100, + 0b11010100111100011, + 0b10100011000111100, + 0b00010001010011110, + 0b01011011010101010, + 0b10101110010010010, + 0b10100001010111011, + 0b10100101111010001, + 0b01010111100110110, + 0b11110110001100000, + 0b10000101000110111, + 0b10011000000100001, + 0b00110001110110000, + 0b10100001010001101, + 0b11100111111101000, + 0b01110011011010110, + 0b11010000101111011, + 0b10101001001000100, + 0b00010000111001111, + 0b00100011101000011, + 0b00110110001000011, + 0b11110011101001111, + 0b10100101011110000, + 0b10111011010000101, + 0b11001010000111101, + 0b11101011111010011, + 0b00000001110111100, + 0b00110010100110110, + 0b00101111010110001, + 0b11101110010000001, + 0b10011100010011101, + 0b01000111111100110, + 0b01100111101100011, + 0b10100100000010011, + 0b11011100110100101, + 0b10001101000111011, + 0b01001010000111000, + 0b11100110011110111, + 0b11111000000100100, + 0b00111100110011100, + 0b11011000110000110, + 0b00011011111011000, + 0b10011000101010001, }, { // p2 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b00011011011000100, // 1/16 way - 0b01011101010010110, // 2/16 way - 0b11001011001101010, // 3/16 way - 0b01110001111011010, // 4/16 way - 0b10110110011111010, // 5/16 way - 0b10110001110000001, // 6/16 way - 0b10001001011101001, // 7/16 way - 0b00000010011101011, // 8/16 way - 0b01100010101111011, // 9/16 way - 0b00111000001101111, // 10/16 way - 0b10101011100111000, // 11/16 way - 0b01111110101111111, // 12/16 way - 0b01000011110101010, // 13/16 way - 0b01001011100000011, // 14/16 way - 0b00010110111101110, // 15/16 way + 0b11001110010011010, + 0b00111011100000111, + 0b10011010101111011, + 0b00011011011000100, + 0b01100001111001001, + 0b00100011000101100, + 0b01110100001110110, + 0b01011101010010110, + 0b00011111001011100, + 0b11111101101001011, + 0b00110100010100010, + 0b11001011001101010, + 0b01100110111001010, + 0b01000010000110001, + 0b00011011111101001, + 0b01110001111011010, + 0b01011010100110111, + 0b10010110101101111, + 0b01001001010111011, + 0b10110110011111010, + 0b10010001010110000, + 0b11001011000000110, + 0b00001011000001000, + 0b10110001110000001, + 0b11111010001101100, + 0b01110110010001010, + 0b00100000010110111, + 0b10001001011101001, + 0b00100101110000111, + 0b10001100100001100, + 0b10000011101111100, + 0b00000010011101011, + 0b01010000011010001, + 0b01011110000100010, + 0b00100100000010110, + 0b01100010101111011, + 0b01101010010110001, + 0b00110111001111111, + 0b11010110101011110, + 0b00111000001101111, + 0b01000010110010001, + 0b01010110110011001, + 0b00110011110100111, + 0b10101011100111000, + 0b11010010001000100, + 0b11001011111100111, + 0b01001000000110001, + 0b01111110101111111, + 0b01111000110111110, + 0b11101100001001001, + 0b00011010000000010, + 0b01000011110101010, + 0b11111111111010011, + 0b10001011001010010, + 0b00100100100111000, + 0b01001011100000011, + 0b11111101110110110, + 0b10111111010001011, + 0b10101111001100000, + 0b00010110111101110, + 0b10100010011011000, + 0b01111001011100111, + 0b10000110110011101, }, { // p3 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11011011110010110, // 1/16 way - 0b11000100000001101, // 2/16 way - 0b11100011000010110, // 3/16 way - 0b00011111010001100, // 4/16 way - 0b11000001011110011, // 5/16 way - 0b10011101110001010, // 6/16 way - 0b00001011001111000, // 7/16 way - 0b00111100010000101, // 8/16 way - 0b01001111001010100, // 9/16 way - 0b01011010010110011, // 10/16 way - 0b11111101010001100, // 11/16 way - 0b00110101011011111, // 12/16 way - 0b01110110010101011, // 13/16 way - 0b00010000110100010, // 14/16 way - 0b00010111110101110, // 15/16 way + 0b00101100000100010, + 0b10001100001111000, + 0b10001110100110011, + 0b11011011110010110, + 0b00010010100101011, + 0b01001100111011111, + 0b00110011010010101, + 0b11000100000001101, + 0b01010011010101100, + 0b01111100110100001, + 0b10011011011110001, + 0b11100011000010110, + 0b00010000010000010, + 0b10110110011111000, + 0b00110111011100001, + 0b00011111010001100, + 0b00000101100001100, + 0b01011000011110010, + 0b10111011011111111, + 0b11000001011110011, + 0b11011000000000011, + 0b00011011000101100, + 0b10100101100010011, + 0b10011101110001010, + 0b11110010001000101, + 0b10001011101110001, + 0b01010010000000110, + 0b00001011001111000, + 0b10000000110111001, + 0b00010001010100001, + 0b10100111110001010, + 0b00111100010000101, + 0b11001101011101111, + 0b11000010010010010, + 0b01100010001001001, + 0b01001111001010100, + 0b10000000010000100, + 0b11010011011101100, + 0b01000111000101000, + 0b01011010010110011, + 0b10100000101001111, + 0b10101110100001100, + 0b00010101100111011, + 0b11111101010001100, + 0b01100000101101000, + 0b10001011110011100, + 0b10001100100101101, + 0b00110101011011111, + 0b11010110000110010, + 0b11101011101110000, + 0b00010111001011011, + 0b01110110010101011, + 0b00111001000001010, + 0b10111101110100011, + 0b11001111000000011, + 0b00010000110100010, + 0b01111110100101011, + 0b00011001010001101, + 0b11111001111101101, + 0b00010111110101110, + 0b10011101110100010, + 0b10011100001001111, + 0b00101011101001101, }, { // p4 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11000011111110101, // 1/16 way - 0b01111110000100101, // 2/16 way - 0b01011100011111100, // 3/16 way - 0b11001000111100000, // 4/16 way - 0b00010001010010110, // 5/16 way - 0b00100010100111011, // 6/16 way - 0b10111101100101000, // 7/16 way - 0b01000101010111010, // 8/16 way - 0b10111101100011011, // 9/16 way - 0b11110100111101000, // 10/16 way - 0b11100010000111100, // 11/16 way - 0b11010010101101010, // 12/16 way - 0b01101100110010010, // 13/16 way - 0b11111000100010000, // 14/16 way - 0b00111011101100011, // 15/16 way + 0b00111101010111100, + 0b01010011100100100, + 0b01100001100011000, + 0b11000011111110101, + 0b00100100011111011, + 0b00010000110000011, + 0b10001000111100101, + 0b01111110000100101, + 0b11010110111101011, + 0b01110001000011100, + 0b10101100100110100, + 0b01011100011111100, + 0b11001111011011111, + 0b00100101010010110, + 0b00010011000100100, + 0b11001000111100000, + 0b10110111000100001, + 0b00110000001111000, + 0b10101100001001111, + 0b00010001010010110, + 0b01001001110100010, + 0b01010001110000110, + 0b00101110010101101, + 0b00100010100111011, + 0b01000010110100110, + 0b01110110111010010, + 0b10101110001110110, + 0b10111101100101000, + 0b01000010111011011, + 0b11010100011001110, + 0b01010011110010111, + 0b01000101010111010, + 0b11110110100000001, + 0b01101011000100010, + 0b01101101111000101, + 0b10111101100011011, + 0b10011110001001000, + 0b00110101110101111, + 0b10011111101011101, + 0b11110100111101000, + 0b10110000011111110, + 0b00001100010101110, + 0b11100011111011001, + 0b11100010000111100, + 0b11001001011110111, + 0b10000011010010101, + 0b01010001101001001, + 0b11010010101101010, + 0b01001101001100111, + 0b00110110011011100, + 0b00111000010100101, + 0b01101100110010010, + 0b01000010000010001, + 0b01001000111110101, + 0b10101110010001100, + 0b11111000100010000, + 0b10111001000011000, + 0b00100101111101011, + 0b00111011000100010, + 0b00111011101100011, + 0b10001010110100100, + 0b10011010101100001, + 0b10111100101010110, }, { // p5 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11001000010011110, // 1/16 way - 0b10011010101100010, // 2/16 way - 0b01100111010001011, // 3/16 way - 0b11011011100110100, // 4/16 way - 0b01001000001001100, // 5/16 way - 0b11000001001111010, // 6/16 way - 0b11001000010010000, // 7/16 way - 0b10001110110110000, // 8/16 way - 0b11111001001100101, // 9/16 way - 0b01100001010010111, // 10/16 way - 0b01000110101100010, // 11/16 way - 0b11010011000101000, // 12/16 way - 0b01011001111000111, // 13/16 way - 0b10011010110011010, // 14/16 way - 0b00001001000001110, // 15/16 way + 0b10000110010011101, + 0b10010100110011101, + 0b11101111001011001, + 0b11001000010011110, + 0b01111111110111110, + 0b00100011010001111, + 0b01011101110101000, + 0b10011010101100010, + 0b00111101111110011, + 0b00110010000110101, + 0b10100111011001111, + 0b01100111010001011, + 0b00100010110011110, + 0b00011010000101110, + 0b11001001101001100, + 0b11011011100110100, + 0b00000010111000100, + 0b11111011010010001, + 0b00100100000011000, + 0b01001000001001100, + 0b10111011101111001, + 0b00111001100101000, + 0b00111011000011100, + 0b11000001001111010, + 0b10010010011100111, + 0b11000001101100001, + 0b11010010110000101, + 0b11001000010010000, + 0b00010111101011110, + 0b01000110001101000, + 0b01110001010000100, + 0b10001110110110000, + 0b00111111101100011, + 0b01000000111011101, + 0b11000001100011101, + 0b11111001001100101, + 0b10000110101001011, + 0b01011111100010011, + 0b11110001000101010, + 0b01100001010010111, + 0b01101011100011111, + 0b01101110000100100, + 0b00101101001000111, + 0b01000110101100010, + 0b10010000010110000, + 0b01111011100011110, + 0b00110011110101011, + 0b11010011000101000, + 0b01011110110001100, + 0b00101011110111001, + 0b00010100001111011, + 0b01011001111000111, + 0b00010100011100111, + 0b11011110010111101, + 0b11111000111011101, + 0b10011010110011010, + 0b01101010000110010, + 0b10101111110000010, + 0b00110111001010101, + 0b00001001000001110, + 0b00000010110111000, + 0b11010000101110001, + 0b01101010111000011, }, { // p6 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b01110110101110110, // 1/16 way - 0b00111111000100011, // 2/16 way - 0b10011010110110011, // 3/16 way - 0b11110001001110000, // 4/16 way - 0b10001101001000111, // 5/16 way - 0b01001000110110000, // 6/16 way - 0b01000011100101101, // 7/16 way - 0b00100110001001001, // 8/16 way - 0b11001011101000100, // 9/16 way - 0b11001011011100111, // 10/16 way - 0b11001101100101000, // 11/16 way - 0b00100011001101100, // 12/16 way - 0b01011101001100000, // 13/16 way - 0b11111001011001111, // 14/16 way - 0b01001101000100110, // 15/16 way + 0b00010010000111001, + 0b01001101111111100, + 0b00010100111011111, + 0b01110110101110110, + 0b01010101000001110, + 0b00111001110010110, + 0b00011000111001010, + 0b00111111000100011, + 0b00111010001001001, + 0b10010001101110110, + 0b10011110001110100, + 0b10011010110110011, + 0b11110010100110101, + 0b11100001000101111, + 0b11010000111111101, + 0b11110001001110000, + 0b10010001111000110, + 0b10100011001000001, + 0b10001000011101000, + 0b10001101001000111, + 0b11100111010110100, + 0b00001010100010001, + 0b01110010010010001, + 0b01001000110110000, + 0b01011010000010000, + 0b01100110100001100, + 0b10111001000100001, + 0b01000011100101101, + 0b10111010001110010, + 0b11011001111000001, + 0b10110000010001111, + 0b00100110001001001, + 0b11000011000110001, + 0b11110011100010110, + 0b01011101101010000, + 0b11001011101000100, + 0b11100001011010110, + 0b11111111100000101, + 0b10000100111001101, + 0b11001011011100111, + 0b11101110011001010, + 0b11001100100001001, + 0b10001001000101101, + 0b11001101100101000, + 0b01011001111000100, + 0b01111010110011010, + 0b00110011110100011, + 0b00100011001101100, + 0b11101010000001011, + 0b11110100100010110, + 0b11011011000111000, + 0b01011101001100000, + 0b11000100010110001, + 0b10000111011101111, + 0b10010001111111101, + 0b11111001011001111, + 0b11101011111101111, + 0b00100101001100010, + 0b10100000011000110, + 0b01001101000100110, + 0b11000110110101010, + 0b01101010101001110, + 0b00010101100100011, }, { // p7 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b01000010111001111, // 1/16 way - 0b00011111000011111, // 2/16 way - 0b00100101010100111, // 3/16 way - 0b10001110011000110, // 4/16 way - 0b01000011011111110, // 5/16 way - 0b01001001000001101, // 6/16 way - 0b01110011110100100, // 7/16 way - 0b10010000110101010, // 8/16 way - 0b01010001101010011, // 9/16 way - 0b00011011000110101, // 10/16 way - 0b11001100101110100, // 11/16 way - 0b10011000101101111, // 12/16 way - 0b10100001000111100, // 13/16 way - 0b11110100001010000, // 14/16 way - 0b01010010010011110, // 15/16 way + 0b11111111111001011, + 0b10101101101100110, + 0b00100101101010000, + 0b01000010111001111, + 0b10010001000110001, + 0b10010100011110001, + 0b10010110111000011, + 0b00011111000011111, + 0b00011000111000111, + 0b01101000010001001, + 0b00001010011011000, + 0b00100101010100111, + 0b01110010111010110, + 0b00101101000111111, + 0b11111010001011001, + 0b10001110011000110, + 0b10010110011101110, + 0b11001101111111010, + 0b01001101110010110, + 0b01000011011111110, + 0b11101100100011100, + 0b10000101011010011, + 0b11111111100101101, + 0b01001001000001101, + 0b11001101110001101, + 0b01000000110011101, + 0b11000001101011011, + 0b01110011110100100, + 0b01110011011101110, + 0b01010001111111000, + 0b01011101100100000, + 0b10010000110101010, + 0b11111001001110010, + 0b10000001100101101, + 0b10000001000010111, + 0b01010001101010011, + 0b10110010100011000, + 0b11001101110101011, + 0b01000101000110100, + 0b00011011000110101, + 0b10101100110101110, + 0b10010110011010111, + 0b00110100110001110, + 0b11001100101110100, + 0b11110100100010010, + 0b11011011001101110, + 0b10100101000101111, + 0b10011000101101111, + 0b10000111001010110, + 0b01001100000100100, + 0b00101010011110001, + 0b10100001000111100, + 0b11010101101110010, + 0b11111000011101010, + 0b11000101010011110, + 0b11110100001010000, + 0b00000111000000100, + 0b01000100101100001, + 0b01101001011000111, + 0b01010010010011110, + 0b10001111110000100, + 0b11100110010111101, + 0b10000101011010000, }, }; -static uint8_t _end_buffers_hashtable[HASH_TABLE_SIZE] = {0}; +static uint16_t _end_buffers_hashtable[HASH_TABLE_SIZE] = {0}; // Dynamic checkpoint static uint32_t _lfsr_checkpoint_bits[LH2_4_BASESTATION_COUNT * 2][2] = {0}; @@ -372,7 +835,7 @@ bool _get_from_spi_ring_buffer(lh2_4_ring_buffer_t *cb, uint8_t data[SPI_BUFFER_ * * @param[in] cb pointer to ring buffer structure */ -void _fill_hash_table(uint8_t * hash_table); +void _fill_hash_table(uint16_t * hash_table); /** * @brief Accesses the global tables _lfsr_checkpoint_hashtable & _lfsr_checkpoint_count @@ -1120,7 +1583,7 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { uint32_t result = 0; uint32_t b17 = 0; uint32_t masked_buff = 0; - while (buffer != _end_buffers[index][0]) // do until buffer reaches one of the saved states + while (buffer != _end_buffers_old[index][0]) // do until buffer reaches one of the saved states { b17 = buffer & 0x00000001; // save the "newest" bit of the buffer buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time @@ -1133,65 +1596,65 @@ uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { result = 0; // reset result count++; - if ((buffer ^ _end_buffers[index][1]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][1]) == 0x00000000) { count = count + 8192 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][2]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][2]) == 0x00000000) { count = count + 16384 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][3]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][3]) == 0x00000000) { count = count + 24576 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][4]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][4]) == 0x00000000) { count = count + 32768 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][5]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][5]) == 0x00000000) { count = count + 40960 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][6]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][6]) == 0x00000000) { count = count + 49152 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][7]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][7]) == 0x00000000) { count = count + 57344 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][8]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][8]) == 0x00000000) { count = count + 65536 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][9]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][9]) == 0x00000000) { count = count + 73728 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][10]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][10]) == 0x00000000) { count = count + 81920 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][11]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][11]) == 0x00000000) { count = count + 90112 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][12]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][12]) == 0x00000000) { count = count + 98304 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][13]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][13]) == 0x00000000) { count = count + 106496 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][14]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][14]) == 0x00000000) { count = count + 114688 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } - if ((buffer ^ _end_buffers[index][15]) == 0x00000000) { + if ((buffer ^ _end_buffers_old[index][15]) == 0x00000000) { count = count + 122880 - 1; - buffer = _end_buffers[index][0]; + buffer = _end_buffers_old[index][0]; } } @@ -1229,44 +1692,60 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { // NRF_P0->OUTSET = 1 << 28; // Check end_buffer backward count - hash_index_down = _end_buffers_hashtable[buffer_down & HASH_TABLE_MASK]; + // Lower hash option in the hash table + hash_index_down = _end_buffers_hashtable[(buffer_down >> 2) & HASH_TABLE_MASK] & CHECKPOINT_TABLE_MASK_LOW; if (buffer_down == _end_buffers_local[hash_index_down]){ - count_down = count_down + 8192*hash_index_down - 1; + count_down = count_down + 2048*hash_index_down - 1; _update_lfsr_checkpoints(index, bits, count_down); return count_down; } - - // Check end_buffer forward count - hash_index_up = _end_buffers_hashtable[buffer_up & HASH_TABLE_MASK]; - if (buffer_up == _end_buffers_local[hash_index_up]){ - count_up = 8192*hash_index_up - count_up - 1; - _update_lfsr_checkpoints(index, bits, count_up); - return count_up; - } - - // Check the dynamical checkpoints, backward - if (buffer_down == _lfsr_checkpoint_bits[index][0]){ - count_down = count_down + _lfsr_checkpoint_count[index][0]; - _update_lfsr_checkpoints(index, bits, count_down); - return count_down; - } - if (buffer_down == _lfsr_checkpoint_bits[index][1]){ - count_down = count_down + _lfsr_checkpoint_count[index][1]; + // Upper hash option in the hash table + hash_index_down = (_end_buffers_hashtable[(buffer_down >> 2) & HASH_TABLE_MASK] & CHECKPOINT_TABLE_MASK_HIGH) >> CHECKPOINT_TABLE_BITS; + if (buffer_down == _end_buffers_local[hash_index_down]){ + count_down = count_down + 2048*hash_index_down - 1; _update_lfsr_checkpoints(index, bits, count_down); return count_down; } - // Check the dynamical checkpoints, backward - if (buffer_up == _lfsr_checkpoint_bits[index][0]){ - count_up = _lfsr_checkpoint_count[index][0] - count_up; + // Check end_buffer forward count + // Lower hash option in the hash table + hash_index_up = _end_buffers_hashtable[(buffer_up >> 2) & HASH_TABLE_MASK] & CHECKPOINT_TABLE_MASK_LOW; + if (buffer_up == _end_buffers_local[hash_index_up]){ + count_up = 2048*hash_index_up - count_up - 1; _update_lfsr_checkpoints(index, bits, count_up); return count_up; } - if (buffer_up == _lfsr_checkpoint_bits[index][1]){ - count_up = _lfsr_checkpoint_count[index][1] - count_up; + // Upper hash option in the hash table + hash_index_up = (_end_buffers_hashtable[(buffer_up >> 2) & HASH_TABLE_MASK] & CHECKPOINT_TABLE_MASK_HIGH) >> CHECKPOINT_TABLE_BITS; + if (buffer_up == _end_buffers_local[hash_index_up]){ + count_up = 2048*hash_index_up - count_up - 1; _update_lfsr_checkpoints(index, bits, count_up); return count_up; } + + // // Check the dynamical checkpoints, backward + // if (buffer_down == _lfsr_checkpoint_bits[index][0]){ + // count_down = count_down + _lfsr_checkpoint_count[index][0]; + // _update_lfsr_checkpoints(index, bits, count_down); + // return count_down; + // } + // if (buffer_down == _lfsr_checkpoint_bits[index][1]){ + // count_down = count_down + _lfsr_checkpoint_count[index][1]; + // _update_lfsr_checkpoints(index, bits, count_down); + // return count_down; + // } + + // // Check the dynamical checkpoints, forward + // if (buffer_up == _lfsr_checkpoint_bits[index][0]){ + // count_up = _lfsr_checkpoint_count[index][0] - count_up; + // _update_lfsr_checkpoints(index, bits, count_up); + // return count_up; + // } + // if (buffer_up == _lfsr_checkpoint_bits[index][1]){ + // count_up = _lfsr_checkpoint_count[index][1] - count_up; + // _update_lfsr_checkpoints(index, bits, count_up); + // return count_up; + // } NRF_P0->OUTCLR = 1 << 28; @@ -1418,14 +1897,20 @@ bool _get_from_spi_ring_buffer(lh2_4_ring_buffer_t *cb, uint8_t data[SPI_BUFFER_ return true; } -void _fill_hash_table(uint8_t * hash_table){ +void _fill_hash_table(uint16_t * hash_table){ // Iterate over all the checkpoints and save the HASH_TABLE_BITS 11 bits as a a index for the hashtable for (size_t poly = 0; poly < LH2_4_BASESTATION_COUNT*2; poly++) { for (size_t checkpoint = 1; checkpoint < NUM_LSFR_COUNT_CHECKPOINTS; checkpoint++) { - hash_table[_end_buffers[poly][checkpoint] & HASH_TABLE_MASK] = checkpoint; + if (hash_table[(_end_buffers[poly][checkpoint] >> 2) & HASH_TABLE_MASK] == 0) { // We shift by 2 to the right because we precomputed that that hash has the least amount of collisions in the hash table + + hash_table[(_end_buffers[poly][checkpoint] >> 2) & HASH_TABLE_MASK] = checkpoint & CHECKPOINT_TABLE_MASK_LOW; // that element of the hash table is empty, copy the checkpoint into the lower 6 bits + } + else{ + hash_table[(_end_buffers[poly][checkpoint] >> 2) & HASH_TABLE_MASK] |= (checkpoint << CHECKPOINT_TABLE_BITS) & CHECKPOINT_TABLE_MASK_HIGH; // If the element is already occupied, use the upper 6 bits + } } } From 3e1506c7ff852a2dbf38fbdd945938a4a7b8ec45 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 19 Jan 2024 20:07:41 +0100 Subject: [PATCH 11/13] #271 LH2: Start of random update tests --- bsp/nrf/lh2_4.c | 57 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index f73897175..b7b2dcd28 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -943,12 +943,14 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { // perform the demodulation + poly search on the received packets // convert the SPI reading to bits via zero-crossing counter demodulation and differential/biphasic manchester decoding + NRF_P1->OUTSET = 1 << 07; temp_bits_sweep = _demodulate_light(temp_spi_bits); + NRF_P1->OUTCLR = 1 << 07; // figure out which polynomial each one of the two samples come from. //temp_selected_polynomial_no_test = _determine_polynomial(temp_bits_sweep, &temp_bit_offset); - + NRF_P0->OUTSET = 1 << 28; temp_selected_polynomial = _determine_polynomial_test(temp_bits_sweep, &temp_bit_offset); - + NRF_P0->OUTCLR = 1 << 28; // If there was an error with the polynomial, leave without updating anything if (temp_selected_polynomial == LH2_4_POLYNOMIAL_ERROR_INDICATOR){ return; @@ -1690,7 +1692,7 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { // // CHECKPOINT CHECKING // - NRF_P0->OUTSET = 1 << 28; + // Check end_buffer backward count // Lower hash option in the hash table hash_index_down = _end_buffers_hashtable[(buffer_down >> 2) & HASH_TABLE_MASK] & CHECKPOINT_TABLE_MASK_LOW; @@ -1723,36 +1725,34 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { return count_up; } - // // Check the dynamical checkpoints, backward - // if (buffer_down == _lfsr_checkpoint_bits[index][0]){ - // count_down = count_down + _lfsr_checkpoint_count[index][0]; - // _update_lfsr_checkpoints(index, bits, count_down); - // return count_down; - // } - // if (buffer_down == _lfsr_checkpoint_bits[index][1]){ - // count_down = count_down + _lfsr_checkpoint_count[index][1]; - // _update_lfsr_checkpoints(index, bits, count_down); - // return count_down; - // } - - // // Check the dynamical checkpoints, forward - // if (buffer_up == _lfsr_checkpoint_bits[index][0]){ - // count_up = _lfsr_checkpoint_count[index][0] - count_up; - // _update_lfsr_checkpoints(index, bits, count_up); - // return count_up; - // } - // if (buffer_up == _lfsr_checkpoint_bits[index][1]){ - // count_up = _lfsr_checkpoint_count[index][1] - count_up; - // _update_lfsr_checkpoints(index, bits, count_up); - // return count_up; - // } - NRF_P0->OUTCLR = 1 << 28; + // Check the dynamical checkpoints, backward + if (buffer_down == _lfsr_checkpoint_bits[index][0]){ + count_down = count_down + _lfsr_checkpoint_count[index][0]; + _update_lfsr_checkpoints(index, bits, count_down); + return count_down; + } + if (buffer_down == _lfsr_checkpoint_bits[index][1]){ + count_down = count_down + _lfsr_checkpoint_count[index][1]; + _update_lfsr_checkpoints(index, bits, count_down); + return count_down; + } + + // Check the dynamical checkpoints, forward + if (buffer_up == _lfsr_checkpoint_bits[index][0]){ + count_up = _lfsr_checkpoint_count[index][0] - count_up; + _update_lfsr_checkpoints(index, bits, count_up); + return count_up; + } + if (buffer_up == _lfsr_checkpoint_bits[index][1]){ + count_up = _lfsr_checkpoint_count[index][1] - count_up; + _update_lfsr_checkpoints(index, bits, count_up); + return count_up; + } // // LSFR UPDATE // - NRF_P1->OUTSET = 1 << 07; // LSFR backward update b17 = buffer_down & 0x00000001; // save the "newest" bit of the buffer buffer_down = (buffer_down & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time @@ -1764,7 +1764,6 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { b1 = __builtin_popcount(buffer_up & polynomials_local) & 0x01; // mask the buffer w/ the selected polynomial buffer_up = ((buffer_up << 1) | b1) & (0x0001FFFF); count_up++; - NRF_P1->OUTCLR = 1 << 07; } return count_up; From 538fcf33856f0e8b7d36b0f6e16724a870f35997 Mon Sep 17 00:00:00 2001 From: Said Alvarado Date: Fri, 19 Jan 2024 20:20:44 +0100 Subject: [PATCH 12/13] #271 LH2: implement poly_check() with _pop_count() --- bsp/nrf/lh2_4.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index b7b2dcd28..e920cefbb 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -1399,9 +1399,7 @@ uint64_t _demodulate_light(uint8_t *sample_buffer) { // bad input variable name uint64_t _poly_check(uint32_t poly, uint32_t bits, uint8_t numbits) { uint64_t bits_out = 0; uint8_t shift_counter = 1; - uint8_t result = 0; - uint8_t ii = 0; - uint32_t masked_buff = 0; + uint8_t b1 = 0; uint32_t buffer = bits; // mask to prevent bit overflow poly &= 0x00001FFFF; // mask to prevent silliness bits_out |= buffer; // initialize 17 LSBs of result @@ -1409,14 +1407,11 @@ uint64_t _poly_check(uint32_t poly, uint32_t bits, uint8_t numbits) { while (shift_counter <= numbits) { bits_out = bits_out << 1; // shift left (forward in time) by 1 - masked_buff = ((buffer) & (poly)); // mask the buffer with the selected polynomial - for (ii = 0; ii < 17; ii++) { - result = result ^ ((((masked_buff)) >> ii) & (0x00000001)); // cumulative sum of buffer&poly - } - buffer = (buffer & 0x0000FFFF) << 1; - buffer |= ((result) & (0x01)); - bits_out |= ((result) & (0x01)); // put result of the XOR operation into the new bit - result = 0; + + b1 = __builtin_popcount(buffer & poly) & 0x01; // mask the buffer w/ the selected polynomial + buffer = ((buffer << 1) | b1) & (0x0001FFFF); + + bits_out |= ((b1) & (0x01)); // put result of the XOR operation into the new bit shift_counter++; } return bits_out; @@ -1531,7 +1526,8 @@ uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { // Check against all the known polynomials for (uint8_t i = 0; i Date: Tue, 23 Jan 2024 16:10:24 +0100 Subject: [PATCH 13/13] #271 LH2: Remove benchmark code from the driver. --- bsp/lh2_4.h | 11 - bsp/nrf/lh2_4.c | 303 ++---------------- .../01bsp_lighthouse_4/01bsp_lighthouse_4.c | 24 +- 3 files changed, 23 insertions(+), 315 deletions(-) diff --git a/bsp/lh2_4.h b/bsp/lh2_4.h index 1494b2355..3733d9837 100644 --- a/bsp/lh2_4.h +++ b/bsp/lh2_4.h @@ -23,20 +23,10 @@ //=========================== defines ========================================== -#define LH2_4_LOCATIONS_COUNT 2 ///< Number of computed locations #define LH2_4_BASESTATION_COUNT 4 ///< Number of supported concurrent basestations - #define LH2_4_SWEEP_COUNT 2 ///< Number of laser sweeps per basestations rotation -/// LH2 internal state -typedef enum { - DB_LH2_4_IDLE, ///< the lh2 engine is idle - DB_LH2_4_RUNNING, ///< the lh2 engine is running - DB_LH2_4_RAW_DATA_READY, ///< some lh2 raw data is available - DB_LH2_4_LOCATION_READY, ///< some lh2 location is ready to be read -} db_lh2_4_state_t; - /// LH2 data ready buffer state typedef enum { DB_LH2_4_NO_NEW_DATA, ///< The data occupying this spot of the buffer has already been sent. @@ -59,7 +49,6 @@ typedef struct __attribute__((packed)) { /// LH2 instance (one row per laser sweep, and one column per basestation.) typedef struct { - db_lh2_4_state_t state; ///< current state of the lh2 engine db_lh2_4_raw_data_t raw_data[2][LH2_4_BASESTATION_COUNT]; ///< raw data decoded from the lighthouse db_lh2_4_location_t locations[2][LH2_4_BASESTATION_COUNT]; ///< buffer holding the computed locations uint32_t timestamps[2][LH2_4_BASESTATION_COUNT]; ///< timestamp of when the raw data was received diff --git a/bsp/nrf/lh2_4.c b/bsp/nrf/lh2_4.c index e920cefbb..51e78eb2c 100644 --- a/bsp/nrf/lh2_4.c +++ b/bsp/nrf/lh2_4.c @@ -39,6 +39,7 @@ #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS) ///< How big will the hashtable for the _end_buffers #define HASH_TABLE_MASK ((1 << HASH_TABLE_BITS) - 1) ///< Mask selecting the HAS_TABLE_BITS least significant bits #define NUM_LSFR_COUNT_CHECKPOINTS 64 ///< How many lsfr checkpoints are per polynomial +#define DISTANCE_BETWEEN_LSFR_CHECKPOINTS 2048 ///< How many lsfr checkpoints are per polynomial #define CHECKPOINT_TABLE_BITS 6 ///< How many bits will be used for the checkpoint table for the lfsr search #define CHECKPOINT_TABLE_MASK_LOW ((1 << CHECKPOINT_TABLE_BITS) - 1) ///< How big will the checkpoint table for the lfsr search #define CHECKPOINT_TABLE_MASK_HIGH (((1 << CHECKPOINT_TABLE_BITS) - 1) << CHECKPOINT_TABLE_BITS) ///< Mask selecting the CHECKPOINT_TABLE_BITS least significant bits @@ -86,85 +87,6 @@ static const uint32_t _polynomials[LH2_4_BASESTATION_COUNT*2] = { 0x00018A55, }; -static const uint32_t _end_buffers_old[4][16] = { - { - // p0 - 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b10101010110011101, // 1/16 way through - 0b10001010101011010, // 2/16 way through - 0b11001100100000010, // 3/16 way through - 0b01100101100011111, // 4/16 way through - 0b10010001101011110, // 5/16 way through - 0b10100011001011111, // 6/16 way through - 0b11110001010110001, // 7/16 way through - 0b10111000110011011, // 8/16 way through - 0b10100110100011110, // 9/16 way through - 0b11001101100010000, // 10/16 way through - 0b01000101110011111, // 11/16 way through - 0b11100101011110101, // 12/16 way through - 0b01001001110110111, // 13/16 way through - 0b11011100110011101, // 14/16 way through - 0b10000110101101011, // 15/16 way through - }, - { - // p1 - 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11010000110111110, // 1/16 way through - 0b10110111100111100, // 2/16 way through - 0b11000010101101111, // 3/16 way through - 0b00101110001101110, // 4/16 way through - 0b01000011000110100, // 5/16 way through - 0b00010001010011110, // 6/16 way through - 0b10100101111010001, // 7/16 way through - 0b10011000000100001, // 8/16 way through - 0b01110011011010110, // 9/16 way through - 0b00100011101000011, // 10/16 way through - 0b10111011010000101, // 11/16 way through - 0b00110010100110110, // 12/16 way through - 0b01000111111100110, // 13/16 way through - 0b10001101000111011, // 14/16 way through - 0b00111100110011100, // 15/16 way through - }, - { - // p2 - 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b00011011011000100, // 1/16 way through - 0b01011101010010110, // 2/16 way through - 0b11001011001101010, // 3/16 way through - 0b01110001111011010, // 4/16 way through - 0b10110110011111010, // 5/16 way through - 0b10110001110000001, // 6/16 way through - 0b10001001011101001, // 7/16 way through - 0b00000010011101011, // 8/16 way through - 0b01100010101111011, // 9/16 way through - 0b00111000001101111, // 10/16 way through - 0b10101011100111000, // 11/16 way through - 0b01111110101111111, // 12/16 way through - 0b01000011110101010, // 13/16 way through - 0b01001011100000011, // 14/16 way through - 0b00010110111101110, // 15/16 way through - }, - { - // p3 - 0x00000000000000001, // [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] starting seed, little endian - 0b11011011110010110, // 1/16 way through - 0b11000100000001101, // 2/16 way through - 0b11100011000010110, // 3/16 way through - 0b00011111010001100, // 4/16 way through - 0b11000001011110011, // 5/16 way through - 0b10011101110001010, // 6/16 way through - 0b00001011001111000, // 7/16 way through - 0b00111100010000101, // 8/16 way through - 0b01001111001010100, // 9/16 way through - 0b01011010010110011, // 10/16 way through - 0b11111101010001100, // 11/16 way through - 0b00110101011011111, // 12/16 way through - 0b01110110010101011, // 13/16 way through - 0b00010000110100010, // 14/16 way through - 0b00010111110101110, // 15/16 way through - }, -}; - static const uint32_t _end_buffers[LH2_4_BASESTATION_COUNT*2][NUM_LSFR_COUNT_CHECKPOINTS] = { { // p0 @@ -710,6 +632,7 @@ static uint16_t _end_buffers_hashtable[HASH_TABLE_SIZE] = {0}; static uint32_t _lfsr_checkpoint_bits[LH2_4_BASESTATION_COUNT * 2][2] = {0}; static uint32_t _lfsr_checkpoint_count[LH2_4_BASESTATION_COUNT * 2][2] = {0}; static uint32_t _lsfr_checkpoint_average = 0; + ///! NOTE: SPIM needs an SCK pin to be defined, P1.6 is used because it's not an available pin in the BCM module static const gpio_t _lh2_4_spi_fake_sck_gpio = { .port = 1, @@ -756,7 +679,6 @@ uint64_t _poly_check(uint32_t poly, uint32_t bits, uint8_t numbits); * @return polynomial, indicating which polynomial was found, or FF for error (polynomial not found). */ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val); -uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val); /** * @brief counts the number of 1s in a 64-bit @@ -776,7 +698,6 @@ uint64_t _hamming_weight(uint64_t bits_in); * @return count: location of the sequence */ uint32_t _reverse_count_p(uint8_t index, uint32_t bits); -uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits); /** * @brief Set a gpio as an INPUT with no pull-up or pull-down @@ -895,24 +816,25 @@ void db_lh2_4_init(db_lh2_4_t *lh2, const gpio_t *gpio_d, const gpio_t *gpio_e) } void db_lh2_4_start(db_lh2_4_t *lh2) { - // db_lh2_4_reset(lh2); - // NRF_PPI->CHENSET = (1 << PPI_SPI_START_CHAN) | (1 << PPI_SPI_STOP_CHAN); NRF_PPI->TASKS_CHG[0].EN = 1; - lh2->state = DB_LH2_4_RUNNING; } void db_lh2_4_stop(db_lh2_4_t *lh2) { - // NRF_PPI->CHENCLR = (1 << PPI_SPI_START_CHAN) | (1 << PPI_SPI_STOP_CHAN); NRF_PPI->TASKS_CHG[0].DIS = 1; - lh2->state = DB_LH2_4_IDLE; + } void db_lh2_4_reset(db_lh2_4_t *lh2) { - _lh2_4_vars.transfer_counter = 0; - _lh2_4_vars.lha_packet_counter = 0; - _lh2_4_vars.lhb_packet_counter = 0; - _lh2_4_vars.buffers_ready = false; - lh2->state = DB_LH2_4_RUNNING; + // compute LFSR locations and detect invalid packets + for (uint8_t basestation = 0; basestation < LH2_4_BASESTATION_COUNT; basestation++) { + for (uint8_t sweep = 0; sweep < 2; sweep++){ + + // Remove the flags indicating available data + lh2->data_ready[sweep][basestation] = DB_LH2_4_NO_NEW_DATA; + lh2->timestamps[sweep][basestation] = 0; + // We won't actually clear the data, it's not worth the computational effort. + } + } } void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { @@ -929,37 +851,30 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { uint32_t temp_timestamp; uint64_t temp_bits_sweep; uint8_t temp_selected_polynomial; - //uint8_t temp_selected_polynomial_no_test; int8_t temp_bit_offset; int8_t sweep; // stop the interruptions while you're reading the data. - // db_lh2_4_stop(lh2); bool error = _get_from_spi_ring_buffer(&_lh2_4_vars.data, temp_spi_bits, &temp_timestamp); - // db_lh2_4_start(lh2); if (!error) { return; } // perform the demodulation + poly search on the received packets // convert the SPI reading to bits via zero-crossing counter demodulation and differential/biphasic manchester decoding - NRF_P1->OUTSET = 1 << 07; temp_bits_sweep = _demodulate_light(temp_spi_bits); - NRF_P1->OUTCLR = 1 << 07; // figure out which polynomial each one of the two samples come from. //temp_selected_polynomial_no_test = _determine_polynomial(temp_bits_sweep, &temp_bit_offset); - NRF_P0->OUTSET = 1 << 28; temp_selected_polynomial = _determine_polynomial_test(temp_bits_sweep, &temp_bit_offset); - NRF_P0->OUTCLR = 1 << 28; // If there was an error with the polynomial, leave without updating anything if (temp_selected_polynomial == LH2_4_POLYNOMIAL_ERROR_INDICATOR){ return; } // Figure in which of the two sweep slots we should save the new data. - if (lh2->timestamps[0][temp_selected_polynomial >> 1] <= lh2->timestamps[1][temp_selected_polynomial >> 1]) {// Either: They are both equal to zero. The structure is empty - sweep = 0; // The data in the first slot is older. - } // either way, use the first slot + if (lh2->timestamps[0][temp_selected_polynomial >> 1] <= lh2->timestamps[1][temp_selected_polynomial >> 1]) { // Either: They are both equal to zero. The structure is empty + sweep = 0; // The data in the first slot is older. + } // either way, use the first slot else { // The data in the second slot is older. sweep = 1; } @@ -976,7 +891,7 @@ void db_lh2_4_process_raw_data(db_lh2_4_t *lh2) { void db_lh2_4_process_location(db_lh2_4_t *lh2) { - uint32_t lfsr_loc_temp, lsfr_loc_temp_test; + uint32_t lfsr_loc_temp; // compute LFSR locations and detect invalid packets for (uint8_t basestation = 0; basestation < LH2_4_BASESTATION_COUNT; basestation++) { @@ -987,28 +902,13 @@ void db_lh2_4_process_location(db_lh2_4_t *lh2) { // Copy the selected polynomial lh2->locations[sweep][basestation].selected_polynomial = lh2->raw_data[sweep][basestation].selected_polynomial; - // Copmute and save the lsfr location. + // Copmute and save the lsfr location. lfsr_loc_temp = _reverse_count_p( - lh2->raw_data[sweep][basestation].selected_polynomial, - lh2->raw_data[sweep][basestation].bits_sweep >> (47 - lh2->raw_data[sweep][basestation].bit_offset)) - - lh2->raw_data[sweep][basestation].bit_offset; - - lh2->locations[sweep][basestation].lfsr_location = lfsr_loc_temp; - - NRF_P1->OUTCLR = 1 << 11; - NRF_P0->OUTSET = 1 << 29; - lsfr_loc_temp_test = _reverse_count_p_test( - lh2->raw_data[sweep][basestation].selected_polynomial, - lh2->raw_data[sweep][basestation].bits_sweep >> (47 - lh2->raw_data[sweep][basestation].bit_offset)) - - lh2->raw_data[sweep][basestation].bit_offset; - NRF_P0->OUTCLR = 1 << 29; - lh2->locations[sweep][basestation].lfsr_location = lsfr_loc_temp_test; - - - if (lfsr_loc_temp != lsfr_loc_temp_test){ - NRF_P1->OUTSET = 1 << 11; - } + lh2->raw_data[sweep][basestation].selected_polynomial, + lh2->raw_data[sweep][basestation].bits_sweep >> (47 - lh2->raw_data[sweep][basestation].bit_offset) + ) - lh2->raw_data[sweep][basestation].bit_offset; + lh2->locations[sweep][basestation].lfsr_location = lfsr_loc_temp; // Mark the data point as processed lh2->data_ready[sweep][basestation] = DB_LH2_4_PROCESSED_DATA_AVAILABLE; @@ -1422,79 +1322,6 @@ uint8_t _determine_polynomial(uint64_t chipsH1, int8_t *start_val) { // TODO: make function a void and modify memory directly // TODO: rename chipsH1 to something relevant... like bits? - *start_val = 0; // TODO: remove this? possible that I modify start value during the demodulation process - - int32_t bits_N_for_comp = 47 - *start_val; - uint32_t bit_buffer1 = (uint32_t)(((0xFFFF800000000000) & chipsH1) >> 47); - uint64_t bits_from_poly[LH2_4_BASESTATION_COUNT*2] = { 0 }; - uint64_t weights[LH2_4_BASESTATION_COUNT*2] = { 0xFFFFFFFFFFFFFFFF }; - uint8_t selected_poly = LH2_4_POLYNOMIAL_ERROR_INDICATOR; // initialize to error condition - uint8_t min_weight_idx = LH2_4_POLYNOMIAL_ERROR_INDICATOR; - uint64_t min_weight = LH2_4_POLYNOMIAL_ERROR_INDICATOR; - uint64_t bits_to_compare = 0; - int32_t threshold = POLYNOMIAL_BIT_ERROR_INITIAL_THRESHOLD; - uint32_t test_iteration = 0; - // try polynomial vs. first buffer bits - // this search takes 17-bit sequences and runs them forwards through the polynomial LFSRs. - // if the remaining detected bits fit well with the chosen 17-bit sequence and a given polynomial, it is treated as "correct" - // in case of bit errors at the beginning of the capture, the 17-bit sequence is shifted (to a max of 8 bits) - // in case of bit errors at the end of the capture, the ending bits are removed (to a max of - // removing bits reduces the threshold correspondingly, as incorrect packet detection will cause a significant delay in location estimate - - // run polynomial search on the first capture - while (1) { - if(test_iteration ==9){ - NRF_P0->OUTSET = 1 << 28; - } - - // TODO: do this math stuff in multiple operations to: (a) make it readable (b) ensure order-of-execution - bit_buffer1 = (uint32_t)(((0xFFFF800000000000 >> (*start_val)) & chipsH1) >> (64 - 17 - (*start_val))); - bits_to_compare = (chipsH1 & (0xFFFFFFFFFFFFFFFF << (64 - 17 - (*start_val) - bits_N_for_comp))); - // reset the minimum polynomial match found - min_weight_idx = LH2_4_POLYNOMIAL_ERROR_INDICATOR; - min_weight = LH2_4_POLYNOMIAL_ERROR_INDICATOR; - // Check against all the known polynomials - for (uint8_t i = 0; i 8) { - *start_val = 0; - bits_N_for_comp = bits_N_for_comp + 1; - if (threshold > 1) { - threshold = threshold - 1; - } else if (threshold == 1) { // keep threshold at ones, but you're probably screwed with an unlucky bit error - threshold = 1; - } - } else { - *start_val = *start_val + 1; - bits_N_for_comp = bits_N_for_comp - 1; - } -test_iteration++; - } - return selected_poly; -} - -uint8_t _determine_polynomial_test(uint64_t chipsH1, int8_t *start_val) { - // check which polynomial the bit sequence is part of - // TODO: make function a void and modify memory directly - // TODO: rename chipsH1 to something relevant... like bits? - *start_val = 8; // TODO: remove this? possible that I modify start value during the demodulation process int32_t bits_N_for_comp = 47 - *start_val; @@ -1575,91 +1402,6 @@ uint64_t _hamming_weight(uint64_t bits_in) { // TODO: bad name for function? or } uint32_t _reverse_count_p(uint8_t index, uint32_t bits) { - uint32_t count = 0; - uint32_t buffer = bits & 0x0001FFFFF; // initialize buffer to initial bits, masked - uint8_t ii = 0; // loop variable for cumulative sum - uint32_t result = 0; - uint32_t b17 = 0; - uint32_t masked_buff = 0; - while (buffer != _end_buffers_old[index][0]) // do until buffer reaches one of the saved states - { - b17 = buffer & 0x00000001; // save the "newest" bit of the buffer - buffer = (buffer & (0x0001FFFE)) >> 1; // shift the buffer right, backwards in time - masked_buff = (buffer) & (_polynomials[index]); // mask the buffer w/ the selected polynomial - for (ii = 0; ii < 17; ii++) { - result = result ^ (((masked_buff) >> ii) & (0x00000001)); // cumulative sum of buffer&poly - } - result = result ^ b17; - buffer = buffer | (result << 16); // update buffer w/ result - result = 0; // reset result - count++; - - if ((buffer ^ _end_buffers_old[index][1]) == 0x00000000) { - count = count + 8192 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][2]) == 0x00000000) { - count = count + 16384 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][3]) == 0x00000000) { - count = count + 24576 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][4]) == 0x00000000) { - count = count + 32768 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][5]) == 0x00000000) { - count = count + 40960 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][6]) == 0x00000000) { - count = count + 49152 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][7]) == 0x00000000) { - count = count + 57344 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][8]) == 0x00000000) { - count = count + 65536 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][9]) == 0x00000000) { - count = count + 73728 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][10]) == 0x00000000) { - count = count + 81920 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][11]) == 0x00000000) { - count = count + 90112 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][12]) == 0x00000000) { - count = count + 98304 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][13]) == 0x00000000) { - count = count + 106496 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][14]) == 0x00000000) { - count = count + 114688 - 1; - buffer = _end_buffers_old[index][0]; - } - if ((buffer ^ _end_buffers_old[index][15]) == 0x00000000) { - count = count + 122880 - 1; - buffer = _end_buffers_old[index][0]; - } - - } - return count; -} - -uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { bits = bits & 0x0001FFFF; // initialize buffer to initial bits, masked uint32_t buffer_down = bits; @@ -1681,7 +1423,6 @@ uint32_t _reverse_count_p_test(uint8_t index, uint32_t bits) { _end_buffers_local[i] = _end_buffers[index][i]; } - while (buffer_up != _end_buffers_local[0]) // do until buffer reaches one of the saved states { diff --git a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c index 86a6f54c9..2edeb608d 100644 --- a/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c +++ b/projects/01bsp_lighthouse_4/01bsp_lighthouse_4.c @@ -28,17 +28,6 @@ static db_lh2_4_t _lh2; //=========================== main ============================================= -/* -P0.30 -> TS4231 Data pin -P1.04 -> SPI SCLK -P0.29 -> High -> at the start of _reverse_p_count() - Low - at the end of _reverse_p_count() -P1.07 -> HIGH during the LSFR update in _reverse_p_count() -P0.28 -> HIGH during the checkpoint check in _reverse_p_count(). -P1.11 -> Goes high if the original function and the function with the changes being tested gives differet results - -*/ - /** * @brief The program starts executing here. @@ -50,11 +39,6 @@ int main(void) { // Initialize the LH2 db_lh2_4_init(&_lh2, &db_lh2_d, &db_lh2_e); db_lh2_4_start(&_lh2); - NRF_P0->DIRSET = 1 << 29; - NRF_P0->DIRSET = 1 << 28; - NRF_P1->DIRSET = 1 << 07; - NRF_P1->DIRSET = 1 << 11; - while (1) { // wait until something happens e.g. an SPI interrupt @@ -65,15 +49,9 @@ int main(void) { // the location function has to be running all the time db_lh2_4_process_location(&_lh2); __NOP(); - } - - - //db_lh2_4_start(&_lh2); - + } } // one last instruction, doesn't do anything, it's just to have a place to put a breakpoint. __NOP(); } -// NRF_P0->OUTSET = 1 << 29; -// NRF_P0->OUTCLR = 1 << 29; \ No newline at end of file