0
0
Embedded Cprogramming~10 mins

Ring buffer for UART receive in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the ring buffer size.

Embedded C
volatile char uart_buffer[[1]];
Drag options to blanks, or click blank then click option'
A64
B256
C512
D128
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing too large a buffer wastes memory.
Choosing too small a buffer may cause overflow.
2fill in blank
medium

Complete the code to increment the head index in the ring buffer.

Embedded C
head = (head + [1]) % BUFFER_SIZE;
Drag options to blanks, or click blank then click option'
A0
B2
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 does not move the head.
Using -1 causes underflow.
3fill in blank
hard

Fix the error in the condition to check if the buffer is full.

Embedded C
if (((head + 1) % BUFFER_SIZE) == [1]) { /* buffer full */ }
Drag options to blanks, or click blank then click option'
A0
Bhead
CBUFFER_SIZE
Dtail
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to head causes wrong condition.
Comparing to BUFFER_SIZE is invalid index.
4fill in blank
hard

Fill both blanks to read a byte from the buffer and update the tail index.

Embedded C
char data = uart_buffer[[1]];
tail = (tail [2] 1) % BUFFER_SIZE;
Drag options to blanks, or click blank then click option'
Atail
B+
C-
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of tail to read data.
Decrementing tail causes errors.
5fill in blank
hard

Fill all three blanks to write a byte to the buffer and update the head index safely.

Embedded C
if (((head + 1) % BUFFER_SIZE) != [1]) {
    uart_buffer[[2]] = data;
    head = (head [3] 1) % BUFFER_SIZE;
}
Drag options to blanks, or click blank then click option'
Atail
Bhead
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Writing data at tail instead of head.
Using '-' instead of '+' to increment head.