📚 Assignment 5 CMPE2250: UART Packet Deserialization & Time Synchronization
-
Now that we have studied Timers, UART hardware, and Serialization / Deserialization, we will combine these concepts to create an autonomous real-time clock tracking engine on the STM32G0B1RE Nucleo-64 board. The firmware maintains timekeeping internally using a SysTick timebase and synchronizes on demand via structured binary packets received over USART2 from a desktop host application.
-
Unlike previous microcontroller boards with physical LCD shields, this implementation outputs telemetry and clock state directly over The Debug Console through the ST-LINK Virtual COM port using standard I/O redirection (
printf). User mode toggling is performed using the onboard blue user pushbutton (B1 on PC13). The packet Synchronization (Rx) is handled by the USART2 receiver timeout interrupt, which generates an interrupt on a silent line to mark packet completion.
📋 Hardware Configuration (NUCLEO-G0B1RE)
| Peripheral / Signal | Pin / Resource | Configuration | Function / Notes |
|---|---|---|---|
| System Clock | PLL @ 50 MHz | Core Clock | Configured via Clock_InitPll(PLL_50MHZ) |
| USART2 TX | PA2 |
Alternate Function 1 (AF1) |
J-Link Virtual COM Port transmit |
| USART2 RX | PA3 |
Alternate Function 1 (AF1) |
J-Link Virtual COM Port receive |
| User Pushbutton (B1) | PC13 |
Digital Input (Active Low) | Toggles between Local Time and Unix Time display modes |
| Timebase | SysTick | Interrupt @ 1 ms | Divides into 10 ms ticks (hundredths of seconds) |
| Packet Framer | USART2 RTO | Receiver Timeout Interrupt | Generates interrupt on silent line to mark packet completion |
1️⃣ Part A: SysTick 10 ms Timebase & Local Clock Engine
Configure the Timebase:
- Configure the ARM Cortex-M0+
SysTicktimer to fire an interrupt once every millisecond:
<pre class="language-c">
SysTick_Config(SystemCoreClock / 1000);
</pre>
- Inside
SysTick_Handler(), implement a software prescaler to produce a 10 ms tick event (representing hundredths of a second, $0.01\text{ s}$).
Maintain Local Time Counters:
* Track the elapsed time across:
* `HuSeconds` (0 to 99)
* `Seconds` (0 to 59)
* `Minutes` (0 to 59)
* `Hours` (0 to 23)
* Every rollover of 100 hundredths of a second (1 second elapsed), set a volatile display update flag (`update = 1`).
Output Formatting:
- When
updateis flagged, format the output to the debug terminal in 24-hour format:hh:mm:ss.
2️⃣ Part B: Unix Time & Pushbutton Mode Toggling
Unix Epoch Counter:
-
Maintain a 32-bit unsigned integer (
volatile uint32_t unixTime) representing total elapsed seconds since January 1, 1970. -
Increment
unixTimeonce every second inside the 10 ms rollover chain. -
Thought Question: What is the maximum number of seconds that can be counted using a standard 32-bit unsigned variable before it rolls over, and what year does that bring us to?
User Mode Toggle (PC13):
-
Initialize
PC13as a digital input. The Nucleo blue pushbutton is pulled high and drops low when pressed. -
Implement software edge detection/debouncing. Each press of the button must toggle the display mode between:
-
Local Time Mode: Prints day, date, and 24-hour local time (e.g.,
Wed, Jan 31, 2024 / 18:36:58). -
Unix Time Mode: Prints the 10-digit raw decimal epoch counter (
%010lu).
-
-
Both time counters must continuously update in the background regardless of which mode is actively being printed to the console.
3️⃣ Part C: Packet Deserialization & USART Receiver Timeout (RTO)
-
To synchronize with the host application, binary packets are framed using the STM32G0 Receiver Timeout (RTO) peripheral feature rather than fixed line endings.
-
You may use DMA or interrupt-driven USART reception, but the packet framing must be handled by the RTO feature. The RTO generates an interrupt on a silent line to mark packet completion.
3.1 Packet Protocol Definitions
The incoming stream starts with a 1-byte command header:
enum Header
{
None = 0x00,
UnixTime = 0xA1, // Followed by 4-byte uint32_t (Total: 5 bytes)
LocalTime = 0xA2, // Followed by 12-byte DateTimeStruct (Total: 13 bytes)
RGB = 0xA3,
Servo = 0xA4,
TextMsg = 0xB1,
Counter = 0xB2
};
The C# desktop application packs the local time according to this byte-aligned specification:
typedef struct DateTimeStruct_Typedef
{
uint16_t Year; // 2 bytes (e.g., 2026)
uint16_t Month; // 2 bytes (1 - 12)
uint16_t MonthDay; // 2 bytes (1 - 31)
uint16_t Day; // 2 bytes (Day of week: 1 = Sun, 2 = Mon, ...)
uint8_t Hours; // 1 byte (0 - 23)
uint8_t Minutes; // 1 byte (0 - 59)
uint8_t Seconds; // 1 byte (0 - 59)
uint8_t HuSeconds; // 1 byte (0 - 99)
} DateTimeStruct; // Total: 12 bytes
3.2 Receiver Timeout (RTO) Configuration
Because packet payloads differ in size (5 bytes vs. 13 bytes) without explicit delimiter characters, the hardware must detect an idle line:
-
Configure USART2 for 115200 Baud, 8 Data Bits, 1 Stop Bit.
-
Enable Receiver Timeout by configuring the
USART2->RTORregister, setting theRTOENbit inUSART2->CR2, and enabling theRTOIEinterrupt bit inUSART2->CR1. -
The timeout duration corresponds to silent bit cycles: \(\text{Timeout Counts} \approx \frac{115200 \text{ baud}}{1000} \approx 115 \text{ bit cycles per millisecond}\)
-
Route and enable
USART2_LPUART2_IRQnin the NVIC.
3.3 Interrupt Handling & Deserialization
-
Byte Reception (
RXNE): Read incoming bytes and append them sequentially intorxBuffer[]while trackingrxIndex. -
Packet Framing (
RTOF): When the line goes silent and triggers the RTO interrupt, clear the flag viaUSART2->ICR = USART_ICR_RTOCFand raiserxComplete = 1. -
Payload Unpacking:
-
If
rxBuffer[0] == 0xA1: Unpack the next 4 bytes intounixTimeusingmemcpy, switch the active display mode to Unix Time, and print a synchronization acknowledgement. -
If
rxBuffer[0] == 0xA2: Unpack the next 12 bytes into thedateTimestruct usingmemcpy, switch the active display mode to Local Time, and print a synchronization acknowledgement. -
Any other header byte: Output
Invalid packetalong with the number of bytes received.
-
-
Map numerical month and day values to lookup string tables (
"JAN","FEB","MON","TUE", etc.) when formatting the Local Time display string.
4️⃣ Deliverables & Marking Criteria
Demonstration:
-
10 ms / 1-second Local Clock: Terminal clock updates smoothly once every second.
-
Pushbutton Display Toggle: Pressing B1 toggles between formatted local date/time and the 10-digit Unix epoch.
-
Host Synchronization: Trigger both Unix Time and Local Time sync from the host PC application; verify that the terminal updates internal counters and switches display modes automatically.
-
Arbitrary Packet Rejection: Transmit arbitrary text or byte packets (e.g., via generic serial console) to demonstrate packet length logging and invalid header rejection.
Code Submission:
- Submit your documented code to the course GitHub repository before the designated lab deadline.