Zephyr -- Device Tree

Introduction to devicetree All Zephyr and application source code files can include and use devicetree.h. This includes device drivers, applications, tests, the kernel, etc. i2c从设备的reg地址是i2c slave address. spi从设备的reg地址是chip select. Writing property values devicetree_unfixed.h, devicetree_fixups.h: deivcetree编译出来的一些宏供C使用。 zephyr3.5只有devicetree_generated.h Devicetree bindings Binding文件用来规范设备树每个节点的表达。 The build system uses bindings to generate C macros for devicetree properties that appear in DTS files. 如果binding文件中没有列出来,不会生成C宏。 语法参考 # A high level description of the device the binding applies to: description: | This is the Vendomatic company's foo-device....

2023-12-08 · 3 min

Zephyr -- Test Framework

Testing Creating a test suite ZTEST_SUITE /** * Create and register a ztest suite. Using this macro creates a new test suite (using * ztest_test_suite). It then creates a struct ztest_suite_node in a specific linker section. * * Tests can then be run by calling ztest_run_test_suites(const void *state) by passing * in the current state. See the documentation for ztest_run_test_suites for more info. * * @param SUITE_NAME The name of the suite (see ztest_test_suite for more info) * @param PREDICATE A function to test against the state and determine if the test should run....

2023-11-30 · 2 min

Zephyr -- West

West Workspace concepts configuration file .west/config 配置文件,定义了manifest repository等。 manifest file west.yml 描述了管理的其他git仓库。可以用manifest.file覆盖。执行west update可以更新所有git仓库。 Built-in commands west help: 查看支持的命令。 west <command> -h: for detailed help. west update -r: sync的时候会rebase local commits. west compare: compare the state of the workspace against the manifest. west diff west status west forall -c <command>: 对所有仓库执行某个shell命令。 west grep west list: 所有project信息。 west manifest: 管理manifest文件。 Workspaces Topologies supported star topology, zephyr is the manifest repository star topology, a Zephyr application is the manifest repository forest topology, freestanding manifest repository West Manifests West Manifests yaml文件...

2023-11-30 · 1 min

Zephyr -- Developing with Zephyr

Getting Started Guide 设置Python虚拟环境 python3 -m venv ~/zephyrproject/.venv source ~/zephyrproject/.venv/bin/activate deactive 退出虚拟环境。 Remember to activate the virtual environment every time you start working. Build the Blinky sample cd ~/zephyrproject/zephyr west build -p always -b <your_board_name> sample/basic/blinky -p always表示a pristine build,也可以使用-p auto来自动判断是否需要pristine build. Environment Variables 创建zephyr专属的环境变量,touch ~/.zephyrrc, 加入export MY_VARIABLE=foo。 接着进入zephyr repository,执行source zephyr-env.sh source zephyr-env.sh: set ZEPHYR_BASE 为zephyr repository(内核目录). 增加一些环境变量到系统PATH. load .zephyrrc 中的配置. Application Development app目录的结构通常为: <app> ├── CMakeLists.txt ├── app.overlay ├── prj....

2023-11-30 · 2 min

Zephyr -- Device Drvier Model

struct device { const char *name; const void *config; const void *api; void * const data; }; config: 放地址映射,中断号等一些物理信息。 api: 回调函数。 data: 放reference counts, semaphores, scratch buffers等。 Device-Specific API Extensions 标准driver api没法实现的功能。 Single Driver, Multiple Instances 某个driver对应多个instances的情况,比如uart driver匹配uart0, uart1, 并且中断线不是同一个。

2023-11-30 · 1 min

I2C spec

Reference I2C 为什么要用开漏输出,而不能用推挽输出。 防止短路。两个 push pull 输出相连,如果一个输出高,一个输出低,会短路。 线与。开漏输出有一个输出端输出低,则整条线都为低,所有输出高才为高。 https://www.zhihu.com/question/534999506 https://www.eet-china.com/mp/a87499.html 以上都是针对 multiple masters 多个输出端的,如果是 single master config 的话,推挽输出是否可以? 2 I2C-bus features Only two bus lines are required; a serial data line (SDA) and a serial clock line (SCL). It is a true multi-controller bus including collision detection and arbitration to prevent data corruption if two or more controllers simultaneously initiate data transfer. Serial, 8-bit oriented, bidirectional data transfers can be made at up to 100 kbit/s in the Standard-mode, up to 400 kbit/s in the Fast-mode, up to 1 Mbit/s in Fast-mode Plus, or up to 3....

2023-09-12 · 2 min

如何避免.bss变量转化成.data段变量

发现在一个.c文件中有一个初始化为非0的全局变量或静态变量,会使这整个文件中定义的全局变量或静态变量都由.bss段转化为.data段。 .data段的变量会在mcu boot阶段的clib会对这些变量做初始化,.bss段可以透过keilc的config如下图控制是否要跳过clib的初始化为0(目前我们的配置是把IRAM配置为不要初始化) 这样的话,我们的芯片在suspend或sleep之后,由于MCU做了power gating,resume起来后MCU重新上电,会重新从MCU boot那边开始跑,.data段的变量就无法保持suspend或sleep之前的值了,又被初始化成定义时候的值了,而.bss段就不会被初始化,可以保持之前的值。 在我们目前usb产品的应用中,一般变量都是要保持suspend/sleep之前的值,所以不能产生有.data段的变量 为了不产生.data段变量,我们要注意: - 全局变量在定义的时候不能做初始化。 - 不要使用静态的局部变量。 //错误示例: U8 g_byGlobalVar = 5; //正确示例: U8 g_byGlobalVar; void InitGlobalVars(void) { if(reset_status==RESET_FROM_POWER_ON) g_byGlobalVar=5; //InitGlobalVars()函数中做初始化 }

2023-09-06 · 1 min

Cortex-M3权威指南

Reference 《Cortex-M3权威指南》 《ARMv7-M Architecture Reference Manual》 《Cortex-M3 Technical Reference Manual》 Chapter1 介绍 Cortex-M3处理器(CM3)采用ARMv7-M架构,它包括所有的16位Thumb指令集和基本的32位Thumb-2指令集架构,Cortex-M3处理器不能执行ARM指令集。 CM3的出现,还在ARM处理器中破天荒地支持了“非对齐数据访问支持”。 Chapter2 CM3概览 2.2 Registers CM3有R0-R15,16个registers。R13作为堆栈指针SP。SP有两个,但在同一时刻只能有一个可以看到,这也就是所谓的“banked”寄存器。 绝大多数16位Thumb指令只能访问R0-R7,而32位Thumb-2指令可以访问所有寄存器。 Cortex-M3拥有两个堆栈指针,然而它们是banked,因此任一时刻只能使用其中的一个。 主堆栈指针(MSP):复位后缺省使用的堆栈指针,用于操作系统内核以及异常处理例程(包括中断服务例程) 进程堆栈指针(PSP):由用户的应用程序代码使用。 R14 LR:当调用一个子程序时,由R14存储返回地址。 R15 PC: 指向当前的程序地址。如果修改它的值,就能改变程序的执行流。 2.3 操作模式和特权级别 两种操作模式:handler mode(异常服务程序代码),thread mode(应用程序代码)。 两种特权级别:特权级,用户级。 在CM3运行主应用程序时(线程模式),既可以使用特权级,也可以使用用户级;但是异常服务例程必须在特权级下执行。复位后,处理器默认进入线程模式,特权极访问。 用户级的程序不能简简单单地试图改写CONTROL寄存器就回到特权级,它必须先“申诉”:执行一条系统调用指令(SVC)。这会触发SVC异常,然后由异常服务例程(通常是操作系统的一部分)接管,如果批准了进入,则异常服务例程修改CONTROL寄存器,才能在用户级的线程模式下重新进入特权级。 2.5 存储器映射 总体来说,Cortex-M3支持4GB存储空间,如图2.6所示地被划分成若干区域。 从图中可见,不像其它的ARM架构,它们的存储器映射由半导体厂家说了算,Cortex-M3预先定义好了“粗线条的”存储器映射。 2.6 总线接口 2.9 中断和异常 11种系统异常+5个保留档位+240个外部中断。 编号 类型 优先级 简介 0 N/A N/A 没有异常在运行 1 复位 -3(最高) 复位 2 NMI -2 不可屏蔽中断(来自外部NMI输入脚) 3 硬(hard) fault -1 所有被disable的fault,都将“上访”成硬fault 4 MemManage fault 可编程 存储器管理fault,MPU访问犯规以及访问非法位置 5 总线fault 可编程 总线错误(预取流产(Abort)或数据流产) 6 用法(usage)Fault 可编程 由于程序错误导致的异常 7-10 保留 N/A N/A 11 SVCall 可编程 系统服务调用 12 调试监视器 可编程 调试监视器(断点,数据观察点,或者是外部调试请求 13 保留 N/A N/A 14 PendSV 可编程 为系统设备而设的“可悬挂请求”(pendable request) 15 SysTick 可编程 系统滴答定时器 16-255 IRQ #0~239 可编程 外中断#0~#239 2....

2023-09-06 · 2 min

DFU(Device Firmware Upgrade)

2. Overview 四个阶段: Enumeration device告知host具备的能力。通过Run-Time Descriptor中增加的DFU class-interface descriptor和functional descriptor实现。 Reconfiguration host和device同意初始固件升级。host会发送一次USB reset,设备会exports DFU descriptors出来。 Transfer Manifestation device告知设备完成升级,host会再次发送USB reset, 重新枚举设备,运行新固件。 总体流程: 3. Requests bRequest分别从DFU_DETACH: 0 ~ DFU_ABORT: 6 4. Enumeration Phase 4.1 Run-Time Descriptor Set 支持DFU的设备,在run-time时需要额外增加两个描述符: A single DFU class interface descriptor A single functional descriptor 因此Configuration descriptor的bNumInterfaces域需要加1。 Run-time DFU Interface Descriptor DFU Functional Descriptor static void PrepareDFUFunDesc(DFUFunDesc_t *pDFUFunDesc) { pDFUFunDesc->bLength = sizeof(DFUFunDesc_t); pDFUFunDesc->bDescriptorType = 0x21; pDFUFunDesc->bmAttributes = g_DFUVar.bmAttributes; /// BIT0 | BIT1 | BIT2 | BIT3 pDFUFunDesc->wDetachTimeOut = 200; pDFUFunDesc->wTransferSize = g_DFUVar....

2023-08-15 · 2 min

linux usb driver

USB function driver f_loopback.c USB composite driver zero.c usb_composite_driver struct usb_composite_driver { const char *name; // "zero" const struct usb_device_descriptor *dev; // device_desc struct usb_gadget_strings **strings; // dev_strings enum usb_device_speed max_speed; // USB_SPEED_SUPER unsigned needs_serial:1; int (*bind)(struct usb_composite_dev *cdev); // zero_bind int (*unbind)(struct usb_composite_dev *); // zero_unbind void (*disconnect)(struct usb_composite_dev *); /* global suspend hooks */ void (*suspend)(struct usb_composite_dev *); // zero_suspend void (*resume)(struct usb_composite_dev *); // zero_resume struct usb_gadget_driver gadget_driver; // composite_driver_template }; 提供usb_configuration usb_device_descriptor...

2023-06-15 · 2 min