Receive from a GPS module, from its interface description
Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence. A medium embedded C…
medium · Reading datasheets and schematics · datasheets · driver · uart
A GPS module sends position sentences to UART1. Working from the Datasheet excerpt below, write:
gps_uart_init(), which sets the baud rate divider and frame format the module needs and enables the UART with only the receiver on.gps_read_line(buf, max), which waits for the next sentence and copies it intobufwithout its CR LF, as a string. It skips anything before the$, stores at mostmax − 1characters, and returns the sentence's length.
Datasheet excerpt
Serial interface (GPS module datasheet)
| Parameter | Value |
|---|---|
| Baud rate | 9600 (default) |
| Frame | 8 data bits, no parity, 1 stop bit, least significant bit first |
| Output | NMEA 0183 sentences, each starting with $ and ending with CR LF (\r\n), at most 82 characters |
USART1 (microcontroller reference manual)
USART1's kernel clock on this board is 16 MHz.
| Offset | Register | Description |
|---|---|---|
| 0x00 | SR | bit 5 RXNE: DR holds a received byte. bit 3 ORE: overrun |
| 0x04 | DR | the received byte; reading it clears RXNE |
| 0x08 | BRR | baud rate divider: f_CK / baud, rounded to the nearest integer |
| 0x0C | CR1 | bit 13 UE: enable. bit 12 M: 0 = 8 data bits, 1 = 9. bit 10 PCE: parity. bit 3 TE: transmitter. bit 2 RE: receiver |
The module's RX pin is not connected, so the microcontroller's transmitter stays off.
Starter code
void gps_uart_init(void) {
UART1->BRR = UART_CLOCK / GPS_BAUD;
UART1->CR1 = UART_CR1_UE | UART_CR1_TE | UART_CR1_RE;
}
int gps_read_line(char *buf, int max) {
int n = 0;
for (;;) {
while (!(UART1->SR & UART_SR_RXNE)) {
}
char c = (char)UART1->DR;
if (c == '\n') break;
if (n < max - 1) buf[n++] = c;
}
buf[n] = '\0';
return n;
}Example tests
One sentence
gps_uart_init();
char line[83];
int n = gps_read_line(line, sizeof line);
printf("BRR %u CR1 0x%04X\n%d [%s]\n", (unsigned)UART1->BRR, (unsigned)UART1->CR1, n, line);Expected output:
BRR 1667 CR1 0x2004
70 [$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76]Learn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- Route a UART to its pins from the alternate-function table · Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Configure a sensor over SPI from its datasheet · Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding.
- Fast-mode I2C and the EEPROM's address, from three documents · Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- A relay and a sensor's power switch, from the transistors · One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.
- Choose a DMA stream from the request mapping table · Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register.