UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
ldpc.c
1 #include "../../firmware.h"
2 #include "../../test.h"
3 
4 #define MESSAGE_LENGTH_TC512 32 // 256/8
5 #define CODEWORD_LENGTH_TC512 (2*MESSAGE_LENGTH_TC512)
6 
7 uint8_t packet_reference_TC512[MESSAGE_LENGTH_TC512];
8 
9 uint8_t packet_test_TC512[CODEWORD_LENGTH_TC512];
10 
11 uint8_t decode_workbuf_TC512[LABRADOR_LDPC_BF_WORKING_LEN(TC512)];
12 uint8_t message_output_TC512[LABRADOR_LDPC_OUTPUT_LEN(TC512)];
13 
14 static bool test_ldpc_TC512(void)
15 {
16  uint32_t i;
17 
18  /* Populate packet */
19  for(i=0; i<MESSAGE_LENGTH_TC512; i++)
20  {
21  packet_reference_TC512[i] = (uint8_t)(Random(255));
22  packet_test_TC512[i] = packet_reference_TC512[i];
23  }
24 
25  if(TEST_VERBOSE)
26  {
27  Debug_print("Original message\r\n");
28  buffer_print_bin(packet_test_TC512, MESSAGE_LENGTH_TC512);
29  }
30 
31  /* Interleave packet */
32  if(TEST_VERBOSE)
33  {
34  Debug_print("Encoding message..\r\n");
35  }
36  labrador_ldpc_encode(LABRADOR_LDPC_CODE_TC512, packet_test_TC512);
37 
38  if(TEST_VERBOSE)
39  {
40  Debug_print("Coded Message\r\n");
41  buffer_print_bin(packet_test_TC512, CODEWORD_LENGTH_TC512);
42  }
43 
44  /* Add random noise */
45  #define BIT_FLIPS 10
46  uint32_t bit_index;
47  for(i=0; i<BIT_FLIPS; i++)
48  {
49  bit_index = (uint8_t)(Random(CODEWORD_LENGTH_TC512));
50  packet_test_TC512[bit_index >> 3] = (uint8_t)(packet_test_TC512[bit_index >> 3] ^ (0x80 >> (bit_index & 0x07)));
51  }
52 
53  size_t iters_count = 0;
54  labrador_ldpc_decode_bf(LABRADOR_LDPC_CODE_TC512, packet_test_TC512, message_output_TC512, decode_workbuf_TC512, 50, &iters_count);
55 
56  if(TEST_VERBOSE)
57  {
58  Debug_print("Used %d/50 iterations\r\n", iters_count);
59  Debug_print("Recovered message\r\n");
60  buffer_print_bin(message_output_TC512, MESSAGE_LENGTH_TC512);
61  }
62 
63  if(memcmp(message_output_TC512, packet_reference_TC512, MESSAGE_LENGTH_TC512) == 0)
64  {
65  return true;
66  }
67  else
68  {
69  return false;
70  }
71 }
72 
73 bool test_ldpc(void)
74 {
75  return test_ldpc_TC512();
76 }
uint32_t Random(uint32_t max)
Definition: random.c:21
void Debug_print(char *fmt,...)
Definition: debug.c:16
void buffer_print_bin(uint8_t *buffer, uint32_t length)
Definition: test.c:71