Skip to content

Microprocessors and Embedded Systems

University Electrical Engineering Topic 8 7:37 English narration · English and 中文 subtitles you can switch

space play · ←/→ 5s · j/l 10s · f fullscreen · c subtitles · ,/. speed

Chapters

Transcript
The digital logic course built gates and flip-flops; this course builds the machine that runs programs. 数字逻辑课程构建了逻辑门和触发器;本课程要构建一台运行程序的机器。
Embedded work is verified from three directions. 嵌入式系统的工作要从三个方面验证。
Widths: does each value fit its register? 位宽:每个值都能放进它的寄存器吗?
Clocks: does the timing close? 时钟:时序能满足要求吗?
Traces: does the code do what you claimed? 执行轨迹:代码是否完成了你声称的任务?
Every register computation, timer setting and code fragment in this course is checked all three ways. 本课程中的每一次寄存器计算、每一个定时器设置和每一段代码,都要从这三个方面检查。
A memory map is arithmetic. 存储器映射就是算术运算。
N address lines reach 2 to the N locations, and the end address is the base plus the size minus one. N 条地址线可以访问 2 的 N 次方个地址,结束地址等于基地址加上大小再减一。
Flash, SRAM and peripherals start at 0, 2 and 4 in the top hex digit, so they can never overlap. 闪存、SRAM 和外设的最高十六进制位分别从 0、2 和 4 开始,因此它们永远不会重叠。
A timer counting to a reload of 4999 makes 5000 counts, because zero counts too. 定时器计数到重装载值 4999 时,一共计数 5000 次,因为零也算一次。
Walk one lap of the fetch, decode, execute loop: the program counter points into flash, the instruction runs, and the counter moves on. 我们来走一遍取指、译码、执行的循环:程序计数器指向闪存中的指令,指令执行后,计数器继续向前移动。
Then place the two memory regions: 64 kilobytes of flash needs 16 address lines from address 0, and 8 kilobytes of SRAM needs 13 lines from 0x2000_0000. 然后确定两个存储区域:64 千字节的闪存从地址 0 开始,需要 16 条地址线;8 千字节的 SRAM 从 0x2000_0000 开始,需要 13 条地址线。
Their top bits keep them apart. 它们的高位使两个区域保持分离。
In the fetch step, the next instruction is copied from memory and the program counter is incremented. 在取指阶段,下一条指令从存储器复制过来,程序计数器递增。
The control unit decodes it. 控制单元对指令进行译码。
In the execute step, it is carried out, and the ALU may calculate, putting the result in the accumulator. 在执行阶段,指令被执行,ALU 可能会进行计算,并将结果放入累加器。
The cycle repeats millions of times a second. 这个循环每秒重复数百万次。
Hardware registers are volatile pointers, because their contents change outside the program. 硬件寄存器要用 volatile 指针访问,因为它们的内容会在程序控制之外发生变化。
The vocabulary is set, clear, toggle and test a bit, always by read-modify-write, so the other bits stay safe. 相关操作包括置位、清零、翻转和测试某一位,始终采用读—修改—写的方式,以免影响其他位。
A bare assignment would clobber them. 直接赋值会覆盖其他位。
Without volatile, an optimising compiler may delete a delay loop entirely, or read a flag only once. 如果没有 volatile,优化编译器可能会完全删除一个延时循环,或者只读取一次标志位。
Read the same act three ways. 从三个角度来看同一个操作。
The C line ORs 0x20 into a register. 这行 C 代码将 0x20 与寄存器的值按位或。
The compiler turns it into a load, an OR and a store. 编译器将它转换为一次加载、一次按位或和一次存储。
On the bus, that means two transfers: one read and one write. 在总线上,这意味着两次传输:一次读和一次写。
Knowing all three views tells you why an interrupt arriving between the read and the write can lose an update. 了解这三个角度,就能明白为什么在读和写之间到来的中断可能导致一次更新丢失。
Polling burns every cycle it waits: one millisecond at 84 megahertz is about 84,000 cycles, all of the processor. 轮询等待时会耗费每一个时钟周期:在 84 兆赫兹下,1 毫秒约为 84,000 个周期,占用了处理器的全部时间。
Sending 64 bytes over SPI by polling costs 896 processor cycles; with DMA it costs 40, freeing 95.5 per cent. 通过轮询经 SPI 发送 64 字节需要 896 个处理器周期;使用 DMA 只需 40 个,释放了 95.5% 的处理器时间。
Double the buffer and polling doubles, while DMA's processor cost stays at 40. 缓冲区大小翻倍,轮询的开销也会翻倍,而 DMA 的处理器开销仍为 40。
An active-low button also needs its pull-up resistor. 低电平有效的按钮还需要上拉电阻。
Each pin has a direction bit and a data bit. 每个引脚都有一个方向位和一个数据位。
Configure the direction bit once: here pin 0 is an input, so it listens, and pin 1 is an output, so it drives. 方向位只需配置一次:这里引脚 0 是输入,因此它接收信号;引脚 1 是输出,因此它驱动信号。
After that, the data bit either reports what the pin sees or sets what it drives. 之后,数据位要么反映引脚检测到的状态,要么设定引脚输出的状态。
Direction says which way; data says what. 方向位决定方向,数据位决定状态。
To make a 1 hertz tick from 84 megahertz, split 84 million into two factors that each fit a 16-bit register: 8400 and 10,000, stored as 8399 and 9999. 要从 84 兆赫兹产生 1 赫兹的节拍,就把 8400 万分解成两个都能放进 16 位寄存器的因数:8400 和 10,000,分别存为 8399 和 9999。
A factor of 84,000 would not fit. 因数 84,000 则放不进去。
Keep interrupt routines short: two of them cost 3.6 per cent of the processor. 中断服务程序要简短:其中两个占用处理器时间的 3.6%。
Debouncing with five samples buys 4 milliseconds of immunity. 用五次采样消抖可获得 4 毫秒的抗抖时间。
Follow the divider chain: 84 megahertz, divided by the prescaler of 8400, gives a 10 kilohertz tick. 沿着分频链来看:84 兆赫兹除以 8400 的预分频系数,得到 10 千赫兹的节拍。
Divided again by the reload of 10,000, that gives one interrupt per second. 再除以 10,000 的重装载计数,得到每秒一次中断。
Both factors fit in 16 bits, which is the real design check. 两个因数都能放进 16 位,这才是设计时真正需要检查的。
The registers store each factor minus one. 寄存器中存储的是各因数减一。
Three serial protocols sit on one trade-off. 三种串行协议体现了不同的权衡。
UART has no clock wire and sends 10 bits per byte, 86.8 microseconds at 115,200 baud. UART 没有时钟线,每字节发送 10 位,在 115,200 波特下需要 86.8 微秒。
SPI is clocked and full duplex: at 10 megahertz a byte takes 0.8 microseconds, about 108 times faster. SPI 使用时钟,支持全双工:在 10 兆赫兹下,一个字节需要 0.8 微秒,速度约快 108 倍。
I2C uses only two wires and addresses each device: a 12-byte packet takes about 275 microseconds at 400 kilohertz. I2C 只用两根线,并通过地址区分设备:在 400 千赫兹下,一个 12 字节数据包需要约 275 微秒。
Count the bits in this 8N1 frame: one start bit, eight data bits of 0x41, sent least significant first, then one stop bit. 数一数这个 8N1 帧中的位:一个起始位,0x41 的八个数据位,最低有效位先发送,然后是一个停止位。
That is ten bit times for every byte, with no clock wire, so both ends must agree on the baud rate. 每个字节共占十个位时间,而且没有时钟线,因此两端必须约定相同的波特率。
The framing bits are part of the price: 86.8 microseconds per byte at 115,200 baud. 帧格式位也是需要付出的开销:在 115,200 波特下,每个字节需要 86.8 微秒。
Serial transmission sends the bits one after another along a single wire. 串行传输沿一根线逐位发送。
Parallel transmission sends several bits together, one per wire. 并行传输同时发送多个位,每根线传输一位。
Parallel looks faster, but over any distance the wires drift out of step, so long links and most modern buses are serial. 并行看起来更快,但传输一段距离后,各线上的信号会逐渐不同步,因此长距离链路和大多数现代总线都采用串行传输。
An ADC's step size is the reference voltage over 2 to the N. ADC 的步长等于参考电压除以 2 的 N 次方。
A 12-bit converter on 3.3 volts has steps of 0.806 millivolts, and 1.65 volts reads exactly 2048, a self-checking mid-scale point. 参考电压为 3.3 伏的 12 位转换器,步长为 0.806 毫伏,输入 1.65 伏时读数恰好是 2048,这是一个可以自检的量程中点。
An accelerometer spanning 4 g over 4096 codes resolves 0.977 milli-g. 量程跨越 4 g、共有 4096 个码值的加速度计,其分辨率为 0.977 毫 g。
A 33 kilohm sense resistor uses the full range for 100 microamps. 对于 100 微安,33 千欧的检测电阻可以用满整个量程。
Each code owns exactly one step, one LSB wide: 0.806 millivolts for a 12-bit converter on 3.3 volts. 每个码值恰好对应一个步长,即 1 LSB 的宽度:对于参考电压为 3.3 伏的 12 位转换器,这个宽度是 0.806 毫伏。
Mid-scale, 1.65 volts, is half the reference, so it lands exactly on code 2048. 量程中点 1.65 伏是参考电压的一半,因此恰好对应码值 2048。
Measure that point first: it catches wiring and reference errors that a single calibration can hide. 先测量这个点:它可以发现单次校准可能掩盖的接线和参考电压错误。
Convert every result into physical units. 把每个结果都转换为物理单位。
Debug by bisection: instrument at the boundary between what you trust and what you suspect, and each observation halves the search. 用二分法调试:在可信部分和可疑部分的边界设置检测点,每次观察都能将搜索范围减半。
If the main-loop LED stops but the interrupt LED keeps ticking, the fault is in the main loop. 如果主循环的 LED 停止闪烁,但中断的 LED 仍在闪烁,故障就在主循环中。
Kick the watchdog only from the main loop. 只在主循环中喂看门狗。
Put hard deadlines in interrupts, and parsing and display in the main loop. 把有严格截止时间的任务放在中断中,把解析和显示放在主循环中。
Name the four steps: reproduce the fault, bisect to find it, fix one thing, then verify again. 说出四个步骤:复现故障,二分查找故障,修复一处问题,然后再次验证。
Each probe halves the range, so ten probes narrow a thousand instructions to one. 每次探测都将范围减半,因此十次探测可以将一千条指令缩小到一条。
The dashed net around everything is the watchdog: if the main loop ever hangs, it resets the system, catching what the loop itself misses. 围绕所有部分的虚线网代表看门狗:如果主循环挂起,它就会复位系统,捕获循环自身无法发现的问题。
Now test yourself from memory, then compute. 现在先凭记忆自测,再进行计算。
How do you make a 1 hertz tick from 84 megahertz with 16-bit registers? 怎样用 16 位寄存器从 84 兆赫兹产生 1 赫兹的节拍?
How long does one byte take on a 115,200 baud 8N1 link? 在 115,200 波特的 8N1 链路上发送一个字节需要多长时间?
What code does a 12-bit converter with a 3.3 volt reference give for 1.65 volts? 参考电压为 3.3 伏的 12 位转换器对 1.65 伏会给出什么码值?
Pause, answer, then check. 暂停、作答,然后核对。

Comments

    Log in or create account

    IGCSE, A-Level & AP