Landin library reference discuss source

core/line/line.ldn

1--  Serial line settings: the rate and the frame of an asynchronous serial
2--  line, and the arithmetic that turns a rate into the divisor a UART's
3--  baud-rate generator is programmed with.  Nothing here reads or writes a
4--  register, so every target has it and a driver keeps its device: the
5--  driver computes every register image through this module, refuses a
6--  setting its peripheral cannot carry before it writes anything, and only
7--  then writes them, so a refusal leaves the line as it was (D955).  The
8--  arithmetic is integer only, since a Cortex-M0 has no float unit.
9
10--- No parity bit.
11public no_parity: atom
12--- A parity bit that makes the number of ones in the data bits and itself
13--- even.
14public even_parity: atom
15--- A parity bit that makes the number of ones in the data bits and itself
16--- odd.
17public odd_parity: atom
18--- A frame's parity: `no_parity`, `even_parity` or `odd_parity`.
19public parity: type = no_parity | even_parity | odd_parity
20
21--- One stop bit after the data and the parity.
22public one_stop_bit: atom
23--- Two stop bits after the data and the parity.
24public two_stop_bits: atom
25--- A frame's stop bits: `one_stop_bit` or `two_stop_bits`.
26public stop_bits: type = one_stop_bit | two_stop_bits
27
28--- A rate the peripheral cannot be set to: zero, one whose divisor its
29--- register cannot hold, or one it would miss by more than `tolerance`.
30public unsupported_rate: atom
31--- A frame the peripheral cannot send and receive: data bits outside 5 to
32--- 9, or a combination its driver refuses, which the driver's own
33--- documentation names.
34public unsupported_format: atom
35
36--- A line's settings: its rate in bits per second, the data bits of a
37--- frame, 5 to 9, its parity and its stop bits.  A frame is a start bit,
38--- the data bits, the parity bit when there is one and the stop bits.
39public settings: type = struct
40    rate: u32
41    data_bits: u8
42    parity: parity
43    stop_bits: stop_bits
44end settings
45
46--- 115,200 bits per second, eight data bits, no parity and one stop bit:
47--- the line every board port starts at, and the derived driver's default.
48public default_settings: settings = (rate: 115200, data_bits: 8,
49                                     parity: no_parity,
50                                     stop_bits: one_stop_bit)
51
52--- The largest error `divisor` accepts between the rate asked for and the
53--- rate its divisor gives, in parts per thousand: 20, two percent, about
54--- half of what a receiver sampling 16 times a bit tolerates over an
55--- eleven-bit frame, which leaves the other half to the far end's clock
56--- (D956).
57public tolerance: u32 = 20
58
59--- Refuse settings no UART can carry: a rate of zero with
60--- `unsupported_rate`, and fewer than 5 or more than 9 data bits with
61--- `unsupported_format`.  Each driver refuses what its own peripheral
62--- cannot do besides.
63public check: (line: settings) -> none ! unsupported_rate | unsupported_format =
64    fail unsupported_rate when line.rate == 0
65    fail unsupported_format when line.data_bits < 5 or line.data_bits > 9
66end check
67
68--- The bits of one frame: the start bit, the data bits, the parity bit
69--- when there is one, and the stop bits.  Eleven for 8E1, ten for 8N1.
70public frame_bits: (line: settings) -> (bits: u32) =
71    bits = 1 + u32(line.data_bits)
72    if line.parity <> no_parity then bits += 1 end if
73    bits += if line.stop_bits == two_stop_bits then 2 else 1 end if
74end frame_bits
75
76--- The bits of a received byte that hold a frame's data, the low
77--- `data_bits` of them: 0x7f for seven data bits, all eight for eight or
78--- nine.  A UART may leave other bits above the data in what it receives,
79--- an STM32 USART the parity bit, so a driver keeps only these (D965).
80public data_mask: (line: settings) -> (mask: u8) =
81    mask = 0xff
82    if line.data_bits < 8 then mask = u8((u32(1) << u32(line.data_bits)) - 1) end if
83end data_mask
84
85--- The divisor closest to `clock / (oversampling * rate)` with
86--- `fraction_bits` bits after the point, as the register holds it: the
87--- value of `clock * 2^fraction_bits / (oversampling * rate)` rounded to
88--- the nearest integer, halves up.  A peripheral that samples each bit
89--- `oversampling` times from a `clock` in hertz then runs at
90--- `clock * 2^fraction_bits / (oversampling * value)` bits per second.
91--- It refuses with `unsupported_rate` a rate of zero, a value outside
92--- `least` to `most`, the register's range, and a rate the value misses
93--- by more than `tolerance`, and computes nothing else, so the caller
94--- writes no register until it has every value.  For the peripherals the
95--- repository's drivers program:
96---
97--- * an Arm PL011, whose IBRD and FBRD hold 16 integer and 6 fraction
98---   bits of a divisor of a clock sampled 16 times a bit: oversampling 16,
99---   fraction bits 6, least 64 (IBRD 1, FBRD 0) and most 65535 * 64 (IBRD
100---   65535, FBRD 0); IBRD is `value >> 6` and FBRD `value & 63`;
101--- * an STM32 USART, whose BRR is the divisor itself: oversampling 16,
102---   fraction bits 4, least 16 and most 65535; oversampling by 8 is
103---   oversampling 8 with the same bits, whose BRR moves the low four bits
104---   one place right;
105--- * an Espressif UART, whose CLKDIV holds an integer and four fraction
106---   bits of a divisor of a clock sampled once a bit: oversampling 1,
107---   fraction bits 4, and the integer field's range shifted left four.
108---
109--- `clock` may be any `u32`; `oversampling` is at least one and
110--- `fraction_bits` at most 24, which every register here is well inside.
111public divisor: (clock: u32, rate: u32, oversampling: u32, fraction_bits: u32,
112                 least: u32, most: u32) -> (value: u32) ! unsupported_rate =
113    fail unsupported_rate when rate == 0
114    --  Below 2^56 and 2^64: no product or sum here wraps.
115    scaled := u64(clock) << u64(fraction_bits)
116    step := u64(oversampling) *% u64(rate)
117    nearest := (scaled +% step / 2) / step
118    fail unsupported_rate when nearest < u64(least) or nearest > u64(most)
119    --  The rate the value gives misses the one asked for by
120    --  |scaled - nearest * step| / (nearest * step); `nearest` is at
121    --  least one here, since a least of zero meets the error below.
122    given := nearest * step
123    missed := if given > scaled then given - scaled else scaled - given end if
124    fail unsupported_rate when given == 0 or missed * 1000 > u64(tolerance) * given
125    value = u32(nearest)
126end divisor