core/line
skip to the library navigationShared: available on every enabled target.
Serial line settings: a UART's rate and frame, the divisor its baud-rate generator is set to, and the refusals every driver shares.
| Call | What it does |
|---|---|
line.settings | A line: its rate in bits per second, data_bits, parity and stop_bits. |
line.default_settings | 115,200 bits per second, 8N1. |
line.check | Refuse a rate of zero and data bits outside 5 to 9. |
line.divisor | The divisor nearest a rate, as a register holds it, or a refusal. |
line.frame_bits | The bits of one frame, start and stop bits included. |
line.data_mask | The bits of a received byte that hold the frame's data. |
parity is no_parity, even_parity or odd_parity, and stop_bits is one_stop_bit or two_stop_bits.
Nothing in the module reads or writes a register, so it serves every target. A driver computes each register image with it and encodes its device's own bits, refuses what its peripheral cannot carry, and only then writes. A refusal leaves the line as it was.
divisor(clock, rate, oversampling, fraction_bits, least, most) gives the integer nearest clock * 2^fraction_bits / (oversampling * rate), halves up. It refuses two results with line.unsupported_rate:
- a value outside
leasttomost, the register's range; - a value whose rate misses by more than
line.tolerance, two percent. That is half of what a receiver sampling 16 times a bit allows over an eleven-bit frame, leaving the rest to the other end.
Three peripherals as examples:
- An Arm PL011 is oversampling 16 with 6 fraction bits, its IBRD
value >> 6and FBRDvalue & 63. - An STM32 USART's BRR is oversampling 16 with 4.
- An Espressif UART's CLKDIV is oversampling 1 with 4, FRAG the low four bits.
The arithmetic is integer only, on u64, which a Cortex-M0 links libgcc's helpers for: the derived driver's image grew by about 3.8 KB at size/auto.
line.unsupported_format is a frame the peripheral cannot carry. check gives it for data bits outside 5 to 9, and each driver gives it for what its own peripheral lacks, nine data bits on every port in this repository. Both atoms have words in failure.words.
A UART may leave other bits above the data bits it receives: an STM32 USART keeps the parity bit at the word's top, bit 7 of seven data bits. Every port's read returns a byte ANDed with data_mask, so a byte read is its frame's data and nothing else.
Items
Atoms 7
Values 2
Functions 4
Also on this page
public parity: type = no_parity | even_parity | odd_parity
A frame's parity: no_parity, even_parity or odd_parity.
A line's settings: its rate in bits per second, the data bits of a frame, 5 to 9, its parity and its stop bits. A frame is a start bit, the data bits, the parity bit when there is one and the stop bits.
public stop_bits: type = one_stop_bit | two_stop_bits
A frame's stop bits: one_stop_bit or two_stop_bits.
public even_parity: atom
A parity bit that makes the number of ones in the data bits and itself even.
public no_parity: atom
No parity bit.
public odd_parity: atom
A parity bit that makes the number of ones in the data bits and itself odd.
public one_stop_bit: atom
One stop bit after the data and the parity.
public two_stop_bits: atom
Two stop bits after the data and the parity.
public unsupported_format: atom
A frame the peripheral cannot send and receive: data bits outside 5 to 9, or a combination its driver refuses, which the driver's own documentation names.
public unsupported_rate: atom
A rate the peripheral cannot be set to: zero, one whose divisor its register cannot hold, or one it would miss by more than tolerance.
public default_settings: settings = (rate: 115200, data_bits: 8,
parity: no_parity,
stop_bits: one_stop_bit)
115,200 bits per second, eight data bits, no parity and one stop bit: the line every board port starts at, and the derived driver's default.
public tolerance: u32 = 20
The largest error divisor accepts between the rate asked for and the rate its divisor gives, in parts per thousand: 20, two percent, about half of what a receiver sampling 16 times a bit tolerates over an eleven-bit frame, which leaves the other half to the far end's clock (D956).
public check: (line: settings) -> none ! unsupported_rate | unsupported_format
Refuse settings no UART can carry: a rate of zero with unsupported_rate, and fewer than 5 or more than 9 data bits with unsupported_format. Each driver refuses what its own peripheral cannot do besides.
public data_mask: (line: settings) -> (mask: u8)
The bits of a received byte that hold a frame's data, the low data_bits of them: 0x7f for seven data bits, all eight for eight or nine. A UART may leave other bits above the data in what it receives, an STM32 USART the parity bit, so a driver keeps only these (D965).
public divisor: (clock: u32, rate: u32, oversampling: u32, fraction_bits: u32,
least: u32, most: u32) -> (value: u32) ! unsupported_rate
The divisor closest to clock / (oversampling * rate) with fraction_bits bits after the point, as the register holds it: the value of clock * 2^fraction_bits / (oversampling * rate) rounded to the nearest integer, halves up. A peripheral that samples each bit oversampling times from a clock in hertz then runs at clock * 2^fraction_bits / (oversampling * value) bits per second. It refuses with unsupported_rate a rate of zero, a value outside least to most, the register's range, and a rate the value misses by more than tolerance, and computes nothing else, so the caller writes no register until it has every value. For the peripherals the repository's drivers program:
- an Arm PL011, whose IBRD and FBRD hold 16 integer and 6 fraction bits of a divisor of a clock sampled 16 times a bit: oversampling 16, fraction bits 6, least 64 (IBRD 1, FBRD 0) and most 65535 * 64 (IBRD 65535, FBRD 0); IBRD is
value >> 6and FBRDvalue & 63; - an STM32 USART, whose BRR is the divisor itself: oversampling 16, fraction bits 4, least 16 and most 65535; oversampling by 8 is oversampling 8 with the same bits, whose BRR moves the low four bits one place right;
- an Espressif UART, whose CLKDIV holds an integer and four fraction bits of a divisor of a clock sampled once a bit: oversampling 1, fraction bits 4, and the integer field's range shifted left four.
clock may be any u32; oversampling is at least one and fraction_bits at most 24, which every register here is well inside.
public frame_bits: (line: settings) -> (bits: u32)
The bits of one frame: the start bit, the data bits, the parity bit when there is one, and the stop bits. Eleven for 8E1, ten for 8N1.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 123 lines
import core/line -- A peripheral family as line.divisor takes it: its clock, oversampling, -- fraction bits and register range. family: type = struct clock: u32 oversampling: u32 fraction_bits: u32 least: u32 most: u32 end family -- The derived driver's PL011 at 48 MHz and the Picos' at 12 MHz, the -- STM32H753's USART3 at 64 MHz oversampling by 16 and by 8, and the ESP -- UARTs: the ESP32's 20-bit CLKDIV at 40 MHz, and the 12-bit one of the -- S3 at 40 MHz, the C5 at 48 MHz and the C3 at 20 MHz. families: [8]family = [ (clock: 48000000, oversampling: 16, fraction_bits: 6, least: 64, most: 4194240), (clock: 12000000, oversampling: 16, fraction_bits: 6, least: 64, most: 4194240), (clock: 64000000, oversampling: 16, fraction_bits: 4, least: 16, most: 65535), (clock: 64000000, oversampling: 8, fraction_bits: 4, least: 16, most: 65535), (clock: 40000000, oversampling: 1, fraction_bits: 4, least: 256, most: 16777215), (clock: 40000000, oversampling: 1, fraction_bits: 4, least: 256, most: 65535), (clock: 48000000, oversampling: 1, fraction_bits: 4, least: 256, most: 65535), (clock: 20000000, oversampling: 1, fraction_bits: 4, least: 256, most: 65535)] -- The divisor for RATE, or zero where it is refused. divisor: (f: family, rate: u32) -> (value: u32) = value = line.divisor(f.clock, rate, f.oversampling, f.fraction_bits, f.least, f.most) else (problem) _ = problem 0 end end divisor -- Whether VALUE, as a divisor of F for RATE, gives a rate that misses -- RATE by no more than `line.tolerance` parts per thousand. within: (f: family, rate: u32, value: u32) -> (yes: bool) = scaled: u64 = u64(f.clock) << u64(f.fraction_bits) given: u64 = u64(value) * u64(f.oversampling) * u64(rate) missed: u64 = if given > scaled then given - scaled else scaled - given end if yes = missed * 1000 <= u64(line.tolerance) * given end within -- Each family's divisor for every rate from 50 to 6,000,000, each the -- last plus a seventh and one, folded into one word as -- scripts/tests/test_line.py folds its own exact answers; every divisor -- given is within `line.tolerance`, or the fold is zero. sweep: () -> (fold: u32) = fold = 0 for f in families do mut rate: u32 = 50 while rate <= 6000000 do value: u32 = divisor(f, rate) if value <> 0 and not within(f, rate, value) then fold = 0 return end if fold = fold *% 1000003 +% value rate = rate + rate / 7 + 1 end while end for end sweep -- Whether SETTINGS are refused with WANTED. refused: (settings: line.settings, wanted: line.unsupported_rate | line.unsupported_format) -> (yes: bool) = yes = false line.check(settings) else (problem) yes = problem == wanted end end refused exercise: () -> (ok: bool) = ok = false -- 115,200 baud as every port writes it today: IBRD 26 and FBRD 3, -- IBRD 6 and FBRD 33, BRR 556, and CLKDIV 347 and FRAG 4. return when divisor(families[0], 115200) <> 26 * 64 + 3 or divisor(families[1], 115200) <> 6 * 64 + 33 or divisor(families[2], 115200) <> 556 or divisor(families[4], 115200) <> 347 * 16 + 4 -- Below the PL011's slowest rate and above its fastest; and 3.9 -- Mbaud at 64 MHz, whose BRR of 16 is 2.6 percent fast, past -- `line.tolerance`, where 3.8 Mbaud's BRR of 17 is within it. return when divisor(families[0], 45) <> 0 or divisor(families[0], 46) <> 4173913 or divisor(families[0], 3100000) <> 0 or divisor(families[2], 3900000) <> 0 or divisor(families[2], 3800000) <> 17 or divisor(families[5], 9600) <> 0 or within(families[2], 3900000, 16) or not within(families[2], 3800000, 17) _ = line.divisor(48000000, 0, 16, 6, 64, 4194240) else (problem) return when problem <> line.unsupported_rate 0 end seven_even: line.settings = (rate: 9600, data_bits: 7, parity: line.even_parity, stop_bits: line.two_stop_bits) four: line.settings = (rate: 9600, data_bits: 4, parity: line.no_parity, stop_bits: line.one_stop_bit) stopped: line.settings = (rate: 0, data_bits: 8, parity: line.odd_parity, stop_bits: line.one_stop_bit) line.check(seven_even) else (problem) _ = problem return end return when not refused(four, line.unsupported_format) or not refused(stopped, line.unsupported_rate) return when line.frame_bits(line.default_settings) <> 10 or line.frame_bits(seven_even) <> 11 -- Seven data bits keep the low seven of a byte, which an STM32 USART -- shares with the parity bit; eight keep all of them. return when line.data_mask(seven_even) <> 0x7f or line.data_mask(line.default_settings) <> 0xff or line.data_mask(stopped) <> 0xff ok = sweep() == 1195194680 end exercise public main: () -> (code: i32) = code = 1 if exercise() then code = 42 end if end main