xv6_chapter1 Operating system interfaces

XV6 实现的所有系统调用: int fork() //Create a process, return child’s PID. void exit(int status) //Terminate the current process; status reported to wait(). No return. int wait(int *status) //Wait for a child to exit; exit status in *status; returns child PID. int kill(int pid) //Terminate process PID. Returns 0, or -1 for error. int getpid() //Return the current process’s PID. int sleep(int n) //Pause for n clock ticks. int exec(char *file, char *argv[]) //Load a file and execute it with arguments; only returns if error....

2023-12-30 · 3 min

Zephyr -- Pinctrl Subsystem

Device Tree Pinctrl controller节点: 所有可选的支持属性可以查阅/dts/bindings/pinctrl/pincfg-node.yaml,支持配置上下拉,驱动能力等等。具体支持属性要参考soc的yaml文件/dts/bindings/pinctrl/xxx.yaml 当设备节点调用perip0_default的时候,group1~N都会被apply。 /* board-pinctrl.dtsi */ #include <vnd-soc-pkgxx.h> &pinctrl { /* Node with pin configuration for default state */ periph0_default: periph0_default { group1 { /* Mappings: PERIPH0_SIGA -> PX0, PERIPH0_SIGC -> PZ1 */ pinmux = <PERIPH0_SIGA_PX0>, <PERIPH0_SIGC_PZ1>; /* Pins PX0 and PZ1 have pull-up enabled */ bias-pull-up; }; ... groupN { /* Mappings: PERIPH0_SIGB -> PY7 */ pinmux = <PERIPH0_SIGB_PY7>; }; }; }; 使用pinctrl的设备节点: &uart0 { pinctrl-0 = <&uart0_default>; pinctrl-1 = <&uart0_sleep>; pinctrl-names = "default", "sleep"; }; 默认支持default, sleep两种属性,也可以自定义属性,比如slow, fast,这样需要在具体driver中自定义PINCTRL_STATE_XXX, 比如...

2023-12-25 · 3 min

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 and Vim plugins

VIM Starting VIM :w file.txt Buffers, Windows, and Tabs Buffers Buffers 是一个内存空间,您可以在其中写入和编辑一些文本。当你在 Vim 中打开一个文件时,数据被绑定到一个缓冲区。当你在 Vim 中打开 3 个文件时,你有 3 个缓冲区。 # 展示缓冲区的三个命令 :buffers :ls :files # 跳转缓冲区 :bnext # 下一个缓冲区 :buffer + file name :buffer + buffer number Ctrl-^ # 删除缓冲区 :bdelete + file name :bdelete + buffer number # 退出所有缓冲区 :qall :qall! :wqall Windows windows 是展示 buffer 的显示 UI. :split filename :vsplit filename :new filename Ctrl-W H/J/K/L # move to left/below/upper/right window Ctrl-W V/S # open a vertical/horizontal split Ctrl-W C # close a window Ctrl-W O # make the current window the only one on screen Tabs Tab 是一系列 windows。...

2023-12-13 · 2 min

Tmux

常用快捷键 ~/.tmux.conf 可修改 tmux 配置。 tmux: open a new session. C-b -> C-a C-b %: 左右分屏。改为-> C-a | C-b ": 上下分屏。-> C-a - C-b <arrow key>:在 panes 间移动。-> alt + arrow exit or hit Ctrl-d:退出当前 pane。 C-b c: new window. C-b p: previous window. C-b n: next window. C-b <number> : move to window n. C-b ?: help message. C-b z: make a pane go full screen. Hit C-b z again to shrink it back to its previous size...

2023-12-13 · 1 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