嵌入式和物联网系统学习经验总结

Microcontroller和Microprocessor啥区别?
Microcontroller就是我们说的微控制器,或者说单片机,通常包括CPU和其它parts,包括程序存储器(只读存储器或者闪存), 数据存储器(随机存储器), IO, Clock等在一起。CPU通常计算能力没那么强,而且比较便宜。
Microprocessor就是我们说的微处理器,通常只包括一个计算能力很强的CPU,比较贵。它会连到外设和其它parts。
Microcontroller把CPU和memory/IO/clock这些部件集成在一个芯片上。这样做的好处有:

  1. Lower cost (one part replaces many parts)
  2. More reliable (fewer packages, fewer interconnects)
  3. Better performance
  4. Faster (信号都在一个板子上)
  5. Lower RF signature

注意:
https://zhuanlan.zhihu.com/p/126269758
MCU也就是单片机中几乎都是用哈佛结构,譬如广泛使用的51单片机、典型的STM32单片机(核心是ARM Cortex-M系列的)都是哈佛结构。原因1)是哈佛结构更加稳定安全,2)速度会更快一些,3)MCU的指令和数据需求量都不大,配置不高,所以内存往往使用SRAM而不使用DRAM,这样就导致它更适合哈佛结构。
PC和服务器芯片(譬如Intel AMD那些出的),ARMCortex-A系列嵌入式芯片(譬如核心是ARM CortexA8的三星S5PV210,譬如华为的麒麟970等手机芯片)等都是冯诺依曼结构。这些系统都需要大量内存,所以工作内存都是DRAM,因为他们更适合使用冯诺依曼系统。

CPU
ARM 是ARM公司开发,包括Coretx-M, Cortex-R 和 Cortex-A
Cortex-M: 定位是中低端市场,也就是我们的单片机,也叫微控制器,我们最常用的STM32F103就是Cortex-M3的内核。
Coretx-R: 定位高端实时系统,比如说医疗、工业、汽车、基带等等领域。适合做microprocessor?
Cortex-A: 定位是通用市场,典型的就是平板电脑、手机、数字电视、机顶盒、服务器、打印机等领域。大家手机里装的就是Cortex-A。

AVR: 是ATMEL公司开发,更便宜。
What is difference between ARM and AVR?
AVR and ARM comes under the family of micro-controller. But ARM can be used as both Microcontroller or as Microprocessor. ARM micro-controller and AVR micro-controller differs from each other in terms of different architecture and different sets of instruction, speed, cast, Memory, Power Consumption, Bus Width etc.

微控制器 MicroController:
STM32 是意法半导体 STMicroelectronics开发,基于Cortex-M。我的理解是STM32 microcontroller 在Cortex-M之上还集成了Flash和SRAM memory等。适合用于消费类、工业控制、机器人、医疗设备、汽车等应用程序。比ESP32具有更多的通用性。
ESP32 是乐鑫科技Espressif公司开发(好像是国货?),基于Tensilica processor(这个是Cadence开发的CPU,不是ARM)。通常用于物联网设备,家庭自动化,Wi-Fi控制,更多是作为一个蓝牙、WiFi的中继功能。
MSP430 是德州仪器TI开发,16位低功耗单片机,适合低功耗嵌入式设备。有专门的TI CCS调试软件跟它配套。

开发板
Raspberry Pi
Arduino: Arduino社区支持8位的AVR系列MCU(来自Atmel),以及32位的ARM Cortex-M系列和ESP系列MCU。

关于ARM

  1. AMBA (Advanced Microcontroller Bus Architecture) 是高级微处理器总线架构
  2. AMBA 用到的总线有
    AHB (Advanced High-performance Bus) 高级高性能总线
    ASB (Advanced System Bus) 高级系统总线—-用的很少
    APB (Advanced Peripheral Bus) 高级外围总线
    AXI (Advanced eXtensible Interface) 高级可拓展接口
  3. AHB主要是针对高效率、高频宽及快速系统模块所设计的总线,它可以连接如微处理器、芯片上或芯片外的内存模块和DMA等高效率模块;
    APB主要用在低速且低功率的外围,可针对外围设备作功率消耗及复杂接口的最佳化;APB在AHB和低带宽的外围设备之间提供了通信的桥梁,所以APB是AHB或ASB的二级拓展总线 ;
    AXI:高速度、高带宽,管道化互联,单向通道,只需要首地址,读写并行,支持乱序,支持非对齐操作,有效支持初始延迟较高的外设,连线非常多;
  4. AHB与AXI、APB的区别与联系
    AHB是先进的高性能总线,AXI是先进的可扩展接口,APB是高级外围总线;
    AHB和APB都是单通道总线,不支持读写并行;而AXI是多通道总线,总共分为五个通道,能够实现读写并行;
    AHB和AXI都是多主/从设备,且通过仲裁机制实现总线控制权的分配;而APB是单主设备多从设备,其主设备就是APB桥,不具有仲裁机制;
    在数据操作方面,AHB和AXI支持突发传输,APB不支持;此外,AXI支持数据的非对齐操作,AHB不支持;

中断和轮询

  1. 如果IO任务频繁发生,可以考虑轮询,这样CPU效率比较高,而且实现简单。
  2. 如果IO任务偶尔发生,可以考虑中断,这样可以有low-power,因为CPU大部分时间可能在睡觉。
  3. 如果IO任务需要马上响应,可以考虑中断。
    关于中断:
  4. 基于时钟的中断,优先级要很高

Cache invalidate vs Cache flush:
A cache invalidate simply marks the cache contents as invalid. So the next time you access data, you will get what is in memory.
A cache flush writes back data from cache into memory.
These two will differ only when memory contents get out of sync with cache contents, which will happen only when you have DMA or a 2nd processor writing to the same memory location.

Some examples:

  1. DMA transfer data from DDR memory to HW buffer.
    DDR buffer should be flushed before DMA. Driver buffer is non cacheable so no need for invalidation.
  2. DMA transfer data from HW buffer to DDR (HW received data and want to make it available for CPU)
    DDR buffer should be invalidated before or after (look NOTE below for details) DMA transfering to prevent CPU to use ‘old’ data from cache. Flushing of HW buffer is redundant.
  3. DMA transfer from DDR into another region of DDR.
    ‘Source’ buffer should be flushed, ‘Destination’ buffer should be invalidated. So valid data is in memory for DMA before transfer, and CPU doesn’t take a ‘dirt’ from cache after DMA did it’s job.

嵌入式系统提高可靠性方法:

  1. 加Lock for race condition
  2. 加Watch dog及早发现错误。可以加一个专门的watch dog task,优先级设很高。

关于阻塞/非阻塞/同步/异步 IO模型:
我认为同步就是用回调函数。
比较好的链接有:
https://www.cnblogs.com/f-ck-need-u/p/7624733.html

同步通信和异步通信
不要跟上面的同步/异步 IO搞混了。
这里的同步通信是指通信双方有时钟同步,异步通信是指通信双方没有时钟同步,靠发送数据帧里面的开始/终止 flag来同步
同步串行通信协议: SPISATA
异步串行通信协议: I2C (数据帧有起始/终止位), UART(双方协商波特率), CAN(从隐性到显性的下降沿可以用作同步,有硬同步和重同步两种)

串行通信和异步通信
大部分都是串行通信,好像只有打印机的通信是异步的?
下面这个表不错,来自https://blog.csdn.net/qq_18677445/article/details/102469255

SPI: SPI通讯接口的扩展常用于高速的或大数据容量的功能外设拓展,譬如WiFi、Ethernet、SD/TF卡、大容量高速数据存储器等。与I2C通讯接口相比, 虽然SPI通讯接口的拓扑需要占用更多个I/O引脚用于片选或握手信号,但SPI接口的时钟频率远高于I2C。此外,SPI通讯接口支持全双工通讯,但I2C是半双工的。 我们直到SD/TF卡的存储器容量可以按千兆字节(即GB)来计量,而NOR结构型FlashROM的存储容量仅以MB计量,两者的存取速度相差很大(后者速度更快), 而且这两类存储器都采用SPI或QSPI等接口。大容量存储器不使用I2C通讯接口的另一个原因是,I2C的总线寻址和大容量存储器的地址管理会造成数据存取过程中地址信息的传输将占用大量时间, 数据的存取效率极地。
SPI虽然仅支持主从通讯模式,但数据传输速度几乎是I2C的1000倍。
I2C: 音视频设备、视觉传感器和点阵图形显示器接口中常用I2C或SMBus作为这些设备的参数配置通讯接口。好像I2C和SPI不同的地方还有I2C有应答机制(发送后的第9个周期要收到应答?),而SPI没有。

关于锁

  1. spinlock主要用于多核之间的同步,semaphore/mutex/disable interrupt主要用于同一个核上面不同task之间的同步
  2. spinlock可以用于interrupt handler,semaphore/mutex不可以,semaphore/mutex是基于busy/wait,task会sleep,没有办法唤醒。

关于sensor传感器系统的设计
https://www.linkedin.com/advice/1/how-do-you-design-implement-camera-sensor#write-the-software

  1. sensor的选型(low power consumption, cost, resolution, pixel size, dynamic range, sensitivity, noise, and compatibility with your camera platform,还有电气属性和接口协议等。
  2. firmware design (sensor side)
    包括sensor’s basic functions, such as initialization, configuration, power management, exposure control, gain control, readout, and data transfer. You need to follow the sensor’s programming guide, which provides instructions and examples on how to write the firmware and use the sensor’s registers, timers, interrupts, and other features.
    3.software design (host side)
    The software is the high-level software that runs on the camera’s main processor or system-on-chip (SoC). It interacts with the sensor’s firmware through an interface, such as I2C, SPI, or MIPI CSI. It performs tasks such as image processing, compression, encoding, storage, display, and transmission. The software is usually written in C, C++, or Python, and uses libraries and frameworks that support image processing, such as OpenCV, GStreamer, or TensorFlow. You need to follow the camera’s software development kit (SDK), which provides APIs and documentation on how to write the software and use the camera’s hardware and software resources.

Bare Metal vs RTOS
什么时候用Bare Metal:
4. Bare metal firmware development thrives in real-time systems where accurate timing and responsiveness are essential.
5. Bare metal firmware development is especially beneficial in resource-restricted situations, such as embedded systems with confined memory and processing capacity.
6. Bare metal firmware development is a good solution for unique or highly specialized hardware platforms. These platforms frequently need low-level modification and control since they could have distinctive features, user interfaces, or communication protocols.

MQTT vs Websocket
都是基于TCP长链接。MQTT好像更适合于短消息的发送。

通信可靠性

  1. 连接可靠 – ACK机制
  2. 传输可靠 – heartbeat

关于纠错码

  1. Checksum 只能查出1-bit的错误,CRC可以查出2-bit的错误。
物联沃分享整理
物联沃-IOTWORD物联网 » 嵌入式和物联网系统学习经验总结

发表评论