UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
demo_fram.c
1 /* firmware.h contains all relevant headers */
2 #include "../firmware.h"
3 
4 #include <stdio.h>
5 
6 #define UART_INTERFACE UART_CAMERA
7 
8 static uint32_t address;
9 
10 #define DATA_BUFFER_LENGTH 50
11 static uint8_t data_write[DATA_BUFFER_LENGTH];
12 static uint8_t data_read[DATA_BUFFER_LENGTH];
13 
14 #define PRINT_BUFFER_LENGTH 200
15 uint32_t print_buffer_counter;
16 
17 #define PRINTF_BUFFER_COUNTER_APPEND_ARGS(output, print_buffer_counter, ...) \
18  print_buffer_counter += (size_t)snprintf( \
19  (output+print_buffer_counter), \
20  (PRINT_BUFFER_LENGTH-print_buffer_counter), \
21  __VA_ARGS__ \
22  );
23 
24 int main(void)
25 {
26  uint32_t i;
27  char output[PRINT_BUFFER_LENGTH];
28 
29  Board_init();
30 
31  UART_init(UART_INTERFACE, 9600);
32  UART_puts(UART_INTERFACE, "\r\nFRAM Memory Demo\r\n");
33 
34  while(1)
35  {
36  LED_on(LED_B);
37 
38  if(FRAM_selfTest())
39  {
40  UART_puts(UART_INTERFACE, "Self-test: PASS\r\n");
41  }
42  else
43  {
44  UART_puts(UART_INTERFACE, "Self-test: FAIL\r\n");
45  }
46 
47  /* Generate Random address */
48  address = Random(FRAM_MAX_ADDRESS);
49 
50  /* Generate Random data */
51  for(i=0;i<DATA_BUFFER_LENGTH;i++)
52  {
53  data_write[i] = (uint8_t)Random(UINT8_MAX-1);
54  data_read[i] = 0x00;
55  }
56 
57  print_buffer_counter = 0;
58  PRINTF_BUFFER_COUNTER_APPEND_ARGS(
59  output,
60  print_buffer_counter,
61  "Write:\r\n * Address: 0x%08lx\r\n * Data: 0x",
62  address
63  );
64  for(i=0;i<DATA_BUFFER_LENGTH;i++)
65  {
66  PRINTF_BUFFER_COUNTER_APPEND_ARGS(
67  output,
68  print_buffer_counter,
69  "%02x",
70  data_write[i]
71  );
72  }
73  PRINTF_BUFFER_COUNTER_APPEND_ARGS(
74  output,
75  print_buffer_counter,
76  "\r\n"
77  );
78  UART_puts(UART_INTERFACE, output);
79 
80  FRAM_write(address, data_write, DATA_BUFFER_LENGTH);
81 
82  FRAM_read(address, data_read, DATA_BUFFER_LENGTH);
83 
84  print_buffer_counter = 0;
85  PRINTF_BUFFER_COUNTER_APPEND_ARGS(
86  output,
87  print_buffer_counter,
88  "Read:\r\n * Address: 0x%08lx\r\n * Data: 0x",
89  address
90  );
91  for(i=0;i<DATA_BUFFER_LENGTH;i++)
92  {
93  PRINTF_BUFFER_COUNTER_APPEND_ARGS(
94  output,
95  print_buffer_counter,
96  "%02x",
97  data_read[i]
98  );
99  }
100  PRINTF_BUFFER_COUNTER_APPEND_ARGS(
101  output,
102  print_buffer_counter,
103  "\r\n"
104  );
105  UART_puts(UART_INTERFACE, output);
106 
107  if(memcmp(data_read, data_write, DATA_BUFFER_LENGTH) == 0)
108  {
109  UART_puts(UART_INTERFACE, "Buffer Match: PASS\r\n");
110  }
111  else
112  {
113  UART_puts(UART_INTERFACE, "Buffer Match: FAIL\r\n");
114  }
115 
116  UART_puts(UART_INTERFACE, "---------------\r\n");
117  LED_off(LED_B);
118 
119  Delay_ms(5000);
120  }
121 }
void Delay_ms(uint32_t milliseconds)
Definition: delay.c:19
void UART_init(uint8_t uart_num, uint32_t baudrate)
Definition: uart.c:78
uint32_t Random(uint32_t max)
Definition: random.c:21
bool FRAM_selfTest(void)
Definition: fram.c:17
void FRAM_read(uint32_t address, uint8_t *data, uint32_t length)
Definition: fram.c:36
void LED_off(uint8_t led_num)
Definition: led.c:20
int main(void)
Runs all module tests.
Definition: test.c:19
void FRAM_write(uint32_t address, uint8_t *data, uint32_t length)
Definition: fram.c:25
void UART_puts(uint8_t uart_num, char *str)
Definition: uart.c:129
void Board_init(void)
Definition: board.c:13
void LED_on(uint8_t led_num)
Definition: led.c:15