Zephyr -- System Call

C Prototype 系统调用函数必须以__syscall开头,在include/或SYSCALL_INCLUDE_DIRS目录下被声明。 scripts/build/parse_syscalls.py会parse__syscall这个标记,并且函数有以下限制: 参数不能传入数组,比如int foo[] or int foo[12], 必须用int *foo代替。 函数指针不能正常解析,需要先对函数指针typedef,再传入。 定义了系统调用的xxx.h头文件必须要文件末尾加上同名的#include <syscall/xxx.h>。 Invocation Context 调用上下文 如果定义了CONFIG_USERSPACE,所有的system call APIs都会直接调用对应的implementation function。 如果定义了__ZEPHYR_SUPERVISOR__,表示所有code都在supervisor mode下运行,直接调用对应的implementation function。 如果定义了__ZEPHYR_USER__ Implementation Detail 实现细节 scripts/build/gencalls.py会把parse到的system calls放进/build/include/generated/syscall_list.h中。以K_SYSCALL_前缀加上API。 所有可以调用的system calls被保存在syscall_dispatch.c中的_k_syscall_table中。 system calls的API实现被保存在了/build/include/generated/syscalls/xxx.h 这些文件被/include/xxx.h文件最后include进去,完成函数的定义。e.g // include/i2c.h __syscall int i2c_configure(const struct device *dev, uint32_t dev_config); //声明 static inline int z_impl_i2c_configure(const struct device *dev, uint32_t dev_config) { const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api; return api->configure(dev, dev_config); } //... #include <syscalls/i2c....

2023-12-18 · 1 min

Zephyr -- SPI framework

DeviceTree spi0: spi@f0020000 { compatible = "snps,designware-spi"; #address-cells = <1>; #size-cells = <0>; reg = <0xf0020000 0x100>; interrupts = <40 1>; pinctrl-0 = <&spi2_default>; pinctrl-1 = <&spi2_sleep>; pinctrl-names = "default", "sleep"; fifo-depth = <32>; // 必须参数 clocks = <&sysclk>; // 和下面clock-frequency二选一,优先从&sysclk中获取clock-frequency clock-frequency = <200000000>; cs-gpios = <&gpio0 18 GPIO_ACTIVE_LOW>; // 用gpio代替cs,可选参数 status = "disabled"; slow@0 { // spi设备 compatible = "test-spi-loopback-slow"; reg = <0>; spi-max-frequency = <500000>; }; fast@0 { // @x 是指第几根片选 compatible = "test-spi-loopback-fast"; reg = <0>; // 0表示spi master controller,1表示spi slave controller spi-max-frequency = <1000000>; }; }; Consumer(应用层和其他driver如何调用spi接口) 方法1:...

2023-12-15 · 3 min

Zephyr -- I2C framework

Device Tree Consumer(应用层和其他driver如何调用i2c接口) 方法1: 设备树API // 获取i2c device node id #define I2C_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(test_i2c) // i2c deivce的compatible,不是i2c bus // 通过I2C_DT_SPEC_GET宏从设备树获得i2c_dt_spec结构体 struct i2c_dt_spec { const struct device *bus; uint16_t addr; }; #define I2C_DT_SPEC_GET_ON_I2C(node_id) \ .bus = DEVICE_DT_GET(DT_BUS(node_id)), \ .addr = DT_REG_ADDR(node_id) static struct i2c_dt_spec i2c = I2C_DT_SPEC_GET(I2C_DEV); 可以调用i2c_xxx_dt进行i2c传输了 static inline int i2c_write_dt(const struct i2c_dt_spec *spec, const uint8_t *buf, uint32_t num_bytes) static inline int i2c_read_dt(const struct i2c_dt_spec *spec, uint8_t *buf, uint32_t num_bytes) static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec, const void *write_buf, size_t num_write, void *read_buf, size_t num_read) 方法2:...

2023-12-15 · 3 min

Vim, Tmux, and Vim plugins

Basic Command-line :e {name of file} open file for editing :ls show open buffers :help {topic} open help :help :w opens help for the :w command :help w opens help for the w movement Movement Movements in Vim are also called “nouns”. Basic movement: hjkl (left, down, up, right) Words: w (next word), b (beginning of word), e (end of word) Lines: 0 (beginning of line), ^ (first non-blank character), $ (end of line) Screen: H (top of screen), M (middle of screen), L (bottom of screen) Scroll: Ctrl-u (up), Ctrl-d (down) File: gg (beginning of file), G (end of file) Line numbers: :{number}<CR> or {number}G (line {number}) Misc: % (corresponding item) Find: f{character}, t{character}, F{character}, T{character} find/to forward/backward {character} on the current line , / ; for navigating matches Search: /{regex}, n / N for navigating matches Edits Vim’s editing commands are also called “verbs”...

2023-12-13 · 5 min

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