鸿蒙OpenHarmony轻量体系源码分析

鼠扑  论坛元老 | 2024-7-22 23:24:34 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 2017|帖子 2017|积分 6051

一、前言



  • 当前的开源鸿蒙LiteOS-M和LiteOS-A是通过gn和ninja编译的。
  • Gitee官方只介绍了LiteOS-M的gn + ninja编译的流程,针对M3利用Keil编译的流程可能要参考社区代码

    • harmonyos_next / stm32f103_simulator_keil
    • OpenHarmony / kernel_liteos_m
    • 利用Keil编译ARM Cortex-M3时,只需要kernel_liteos_m堆栈和Keil工程堆栈,不需要全量的OpenHarmony代码

  • 由此可知,开源鸿蒙轻量体系只是简单的由LiteOS-M内核 + 一些芯片厂商的驱动组成
  • LiteOS-M 编码规范

    • OpenHarmony轻内核编码规范
    • OpenHarmony C语言编程规范

  • 开辟板移植流程

    • 轻量体系STM32F407芯片移植案例

      • 确定主频、选定默认打印输出的串口、printf标准输入输出重定向到默认串口、指定内存范围给内核、提供定时器给内核

    • 移植芯片内核架构的流程
    • LiteOS-M内核BUILD.gn编写指南

二、源码分析



  • 源码下载:OpenHarmony / kernel_liteos_m

    • 代码总量只有8M,代码量和uC/OS这类操作体系雷同,因为代码量不多,因此一个人也是能分析完的;uC/OS虽然是开源、个人免费,但是商用是要收费的,所以ARM Cortex-M之类的芯片利用LiteOS-M大概RT-Thread这类操作体系还是蛮划算的,需要技术支持的话也可以找干系公司进行付费咨询。
    • 代码结构如下:

  1. jim@DESKTOP-SVP3BEM MINGW64 /d/1_git/openHarmony/kernel/liteos_m (master)
  2. $ tree .
  3. .
  4. |-- BUILD.gn        /* 老版本的LiteOS使用Makefile编译,新版本还可以用gn + ninja */
  5. |-- Kconfig
  6. |-- Makefile
  7. |-- arch                /* 硬件相关的代码,按内核IP公司 - IP系列进行区分 */
  8. |   |-- BUILD.gn
  9. |   |-- Kconfig
  10. |   |-- arm
  11. |   |   |-- BUILD.gn
  12. |   |   |-- Kconfig
  13. |   |   |-- arm9
  14. |   |   |   .......
  15. |   |   |-- common        /* 相同芯片内核IP公司通用的函数和接口 */
  16. |   |   |   |-- BUILD.gn
  17. |   |   |   |-- los_common_interrupt.c
  18. |   |   |   `-- los_common_interrupt.h
  19. |   |   |-- cortex-m3        /* M3只支持Keil编译,之后的版本如M4、Cortex-A则是在Linux + gcc下编译 */
  20. |   |   |   `-- keil        /* 操作系统底层相关的支持,也是操作系统移植的重点 */
  21. |   |   |       |-- los_arch_atomic.h
  22. |   |   |       |-- los_arch_context.h
  23. |   |   |       |-- los_arch_interrupt.h
  24. |   |   |       |-- los_arch_timer.h
  25. |   |   |       |-- los_atomic.S
  26. |   |   |       |-- los_context.c
  27. |   |   |       |-- los_dispatch.S
  28. |   |   |       |-- los_exc.S
  29. |   |   |       |-- los_interrupt.c
  30. |   |   |       |-- los_startup.s        /* 芯片上电后执行的第二行代码,芯片上电后的第一行代码一般都融合进了编译器中,由芯片原厂实现,写代码时看不到 */
  31. |   |   |       `-- los_timer.c                /* 需要给操作系统的定时器接口,用于时间片轮转 */
  32. |   |   |-- cortex-m33
  33. |   |   |   ......
  34. |   |   |-- cortex-m4
  35. |   |   |   .......
  36. |   |   |-- cortex-m55
  37. |   |   |   .......
  38. |   |   |-- cortex-m7
  39. |   |   |   .......
  40. |   |-- csky
  41. |   |   .......
  42. |   |-- include                /* 所有芯片对操作系统暴露出的统一接口 */
  43. |   |   |-- los_arch.h
  44. |   |   |-- los_atomic.h
  45. |   |   |-- los_context.h
  46. |   |   |-- los_interrupt.h
  47. |   |   |-- los_mpu.h
  48. |   |   `-- los_timer.h
  49. |   |-- risc-v
  50. |   |   ......
  51. |   `-- xtensa
  52. |       ......
  53. |        /* arch结构和Linux源码类似,一组类似的模块会暴露出统一的接口放在.h中,而.c的实现会分散到各处的不同模块,这和普通裸机代码中.c和.h在一起的结构不一样 */
  54. |-- bundle.json
  55. |-- components        /* 支持的插件,可选 */
  56. |   |-- BUILD.gn
  57. |   |-- backtrace
  58. |   |   |-- BUILD.gn
  59. |   |   |-- los_backtrace.c
  60. |   |   `-- los_backtrace.h
  61. |   |-- cppsupport        /* 是否支持C++ */
  62. |   |   |-- BUILD.gn
  63. |   |   |-- los_cppsupport.c
  64. |   |   `-- los_cppsupport.h
  65. |   |-- cpup
  66. |   |   |-- BUILD.gn
  67. |   |   |-- los_cpup.c
  68. |   |   `-- los_cpup.h
  69. |   |-- debugtools
  70. |   |   |-- BUILD.gn
  71. |   |   |-- los_debugtools.h
  72. |   |   |-- los_hwidump.c
  73. |   |   |-- los_schedtrace.c
  74. |   |   `-- los_stackdump.c
  75. |   |-- dynlink
  76. |   |   |-- BUILD.gn
  77. |   |   |-- los_dynlink.c
  78. |   |   |-- los_dynlink.h
  79. |   |   `-- script
  80. |   |       `-- so_parse
  81. |   |-- exchook
  82. |   |   |-- BUILD.gn
  83. |   |   |-- los_exc_info.c
  84. |   |   |-- los_exc_info.h
  85. |   |   |-- los_exchook.c
  86. |   |   `-- los_exchook.h
  87. |   |-- fs        /* 操作系统的四大模块之一:文件系统 */
  88. |   |   |-- BUILD.gn
  89. |   |   |-- Kconfig
  90. |   |   |-- fatfs        /* FAT32最常用,可以用于U盘、Flash、网盘等 */
  91. |   |   |   |-- BUILD.gn
  92. |   |   |   |-- Kconfig
  93. |   |   |   |-- fatfs.c
  94. |   |   |   |-- fatfs.h
  95. |   |   |   `-- fatfs_conf.h
  96. |   |   |-- littlefs
  97. |   |   |   ......
  98. |   |   `-- vfs
  99. |   |       ......
  100. |   |-- iar_tls
  101. |   |   |-- los_iar_tls.c
  102. |   |   `-- los_iar_tls.h
  103. |   |-- lmk
  104. |   |   |-- BUILD.gn
  105. |   |   |-- los_lmk.c
  106. |   |   `-- los_lmk.h
  107. |   |-- lms
  108. |   |   |-- BUILD.gn
  109. |   |   |-- Kconfig
  110. |   |   |-- lms_libc.c
  111. |   |   |-- los_lms.c
  112. |   |   |-- los_lms.h
  113. |   |   `-- los_lms_pri.h
  114. |   |-- net                /* 操作系统的第五大功能:通信/网络通信 */
  115. |   |   |-- BUILD.gn
  116. |   |   |-- lwip-2.1        /* 像网络、U盘之类的大型通信类驱动代码会很多,有时甚至会比操作系统本身的代码量还多 */
  117. |   |   |   |-- BUILD.gn
  118. |   |   |   |-- enhancement
  119. |   |   |   |   `-- src
  120. |   |   |   |       |-- fixme.c
  121. |   |   |   |       `-- lwip_ifaddrs.c
  122. |   |   |   |-- lwip_porting.gni
  123. |   |   |   `-- porting
  124. |   |   |       |-- include
  125. |   |   |       |   |-- arch
  126. |   |   |       |   |   |-- cc.h
  127. |   |   |       |   |   |-- perf.h
  128. |   |   |       |   |   `-- sys_arch.h
  129. |   |   |       |   |-- lwip
  130. |   |   |       |   |   |-- api_shell.h
  131. |   |   |       |   |   |-- dhcp.h
  132. |   |   |       |   |   |-- inet.h
  133. |   |   |       |   |   |-- lwipopts.h
  134. |   |   |       |   |   |-- netdb.h
  135. |   |   |       |   |   |-- netif.h
  136. |   |   |       |   |   |-- netifapi.h
  137. |   |   |       |   |   `-- sockets.h
  138. |   |   |       |   `-- lwipopts.h
  139. |   |   |       `-- src
  140. |   |   |           |-- api_shell.c
  141. |   |   |           |-- driverif.c
  142. |   |   |           |-- lwip_init.c
  143. |   |   |           |-- netdb_porting.c
  144. |   |   |           |-- sockets_porting.c
  145. |   |   |           `-- sys_arch.c
  146. |   |   `-- test
  147. |   |       ......
  148. |   |-- power        /* 低功耗模块 */
  149. |   |   |-- BUILD.gn
  150. |   |   |-- los_pm.c
  151. |   |   `-- los_pm.h
  152. |   |-- security        /* 权限管理模块 */
  153. |   |   |-- BUILD.gn
  154. |   |   |-- box
  155. |   |   |   |-- BUILD.gn
  156. |   |   |   |-- los_box.c
  157. |   |   |   `-- los_box.h
  158. |   |   |-- syscall
  159. |   |   |   |-- BUILD.gn
  160. |   |   |   |-- los_syscall.c
  161. |   |   |   |-- los_syscall.h
  162. |   |   |   |-- pthread_syscall.c
  163. |   |   |   `-- syscall_lookup.h
  164. |   |   `-- userlib
  165. |   |       `-- BUILD.gn
  166. |   |-- shell        /* 命令行 */
  167. |   |   |-- BUILD.gn
  168. |   |   |-- Kconfig
  169. |   |   |-- include
  170. |   |   |   |-- shcmd.h
  171. |   |   |   |-- shcmdparse.h
  172. |   |   |   |-- shell.h
  173. |   |   |   |-- shmsg.h
  174. |   |   |   `-- show.h
  175. |   |   `-- src
  176. |   |       |-- base
  177. |   |       |   |-- shcmd.c
  178. |   |       |   |-- shcmdparse.c
  179. |   |       |   |-- shmsg.c
  180. |   |       |   `-- show.c
  181. |   |       `-- cmds
  182. |   |           |-- date_shell.c
  183. |   |           |-- fullpath.c
  184. |   |           |-- mempt_shellcmd.c
  185. |   |           |-- shell_shellcmd.c
  186. |   |           |-- task_shellcmd.c
  187. |   |           `-- vfs_shellcmd.c
  188. |   |-- signal
  189. |   |   |-- BUILD.gn
  190. |   |   |-- Kconfig
  191. |   |   |-- los_signal.c
  192. |   |   `-- los_signal.h
  193. |   `-- trace        /* 调试程序用 */
  194. |       ......
  195. |-- config.gni
  196. |-- config_iccarm.gni
  197. |-- drivers                /* 操作系统四大模块之二:设备管理 */
  198. |   `-- Kconfig
  199. |        /* 具体的外设驱动由芯片原厂在上层文件夹的device和vendor文件夹中提供 */
  200. |-- figures                /* 几张介绍本操作系统内核结构的图片 */
  201. |   ......
  202. |-- kal
  203. |   |-- BUILD.gn
  204. |   |-- Kconfig
  205. |   |-- cmsis        /* ARM格式的中间件统一接口 */
  206. |   |   |-- BUILD.gn
  207. |   |   |-- Kconfig
  208. |   |   |-- cmsis_liteos2.c
  209. |   |   |-- cmsis_os.h
  210. |   |   |-- cmsis_os2.h
  211. |   |   |-- hos_cmsis_adp.h
  212. |   |   `-- kal.h
  213. |   |-- libc        /* C语言标准库,其实现一般都是在编译器中,由芯片原厂完成,所以这里只有头文件 */
  214. |   |   |-- BUILD.gn
  215. |   |   |-- Kconfig
  216. |   |   |-- iccarm
  217. |   |   |   `-- BUILD.gn
  218. |   |   |-- musl
  219. |   |   |   `-- BUILD.gn
  220. |   |   `-- newlib
  221. |   |       |-- BUILD.gn
  222. |   |       `-- porting
  223. |   |           |-- include
  224. |   |           |   |-- arpa
  225. |   |           |   |   `-- inet.h
  226. |   |           |   |-- byteswap.h
  227. |   |           |   |-- dirent.h
  228. |   |           |   |-- endian.h
  229. |   |           |   |-- ifaddrs.h
  230. |   |           |   |-- limits.h
  231. |   |           |   |-- malloc.h
  232. |   |           |   |-- mqueue.h
  233. |   |           |   |-- net
  234. |   |           |   |   |-- ethernet.h
  235. |   |           |   |   |-- if.h
  236. |   |           |   |   `-- if_arp.h
  237. |   |           |   |-- netdb.h
  238. |   |           |   |-- netinet
  239. |   |           |   |   |-- if_ether.h
  240. |   |           |   |   |-- in.h
  241. |   |           |   |   |-- ip.h
  242. |   |           |   |   `-- tcp.h
  243. |   |           |   |-- poll.h
  244. |   |           |   |-- semaphore.h
  245. |   |           |   |-- sys
  246. |   |           |   |   |-- _pthreadtypes.h
  247. |   |           |   |   |-- fcntl.h
  248. |   |           |   |   |-- features.h
  249. |   |           |   |   |-- ioctl.h
  250. |   |           |   |   |-- mount.h
  251. |   |           |   |   |-- prctl.h
  252. |   |           |   |   |-- sched.h
  253. |   |           |   |   |-- select.h
  254. |   |           |   |   |-- socket.h
  255. |   |           |   |   |-- statfs.h
  256. |   |           |   |   |-- uio.h
  257. |   |           |   |   `-- un.h
  258. |   |           |   `-- time.h
  259. |   |           `-- src
  260. |   |               |-- hook_adapt.c
  261. |   |               `-- network
  262. |   |                   |-- htonl.c
  263. |   |                   |-- htons.c
  264. |   |                   |-- ntohl.c
  265. |   |                   `-- ntohs.c
  266. |   |-- libsec
  267. |   |   `-- BUILD.gn
  268. |   `-- posix        /* 操作系统给应用暴露出来的通用接口 */
  269. |       |-- BUILD.gn
  270. |       |-- Kconfig
  271. |       |-- include
  272. |       |   |-- libc.h
  273. |       |   |-- pipe_impl.h
  274. |       |   |-- poll_impl.h
  275. |       |   `-- rtc_time_hook.h
  276. |       `-- src
  277. |           |-- errno.c
  278. |           |-- libc.c
  279. |           |-- libc_config.h
  280. |           |-- malloc.c        /* 操作系统的四大模块之三:内存管理 */
  281. |           |-- map_error.c
  282. |           |-- map_error.h
  283. |           |-- mqueue.c
  284. |           |-- mqueue_impl.h
  285. |           |-- pipe.c
  286. |           |-- poll.c
  287. |           |-- pthread.c
  288. |           |-- pthread_attr.c
  289. |           |-- pthread_cond.c
  290. |           |-- pthread_mutex.c
  291. |           |-- semaphore.c
  292. |           |-- signal.c
  293. |           |-- time.c
  294. |           `-- time_internal.h
  295. |-- kernel        /* 操作系统的四大模块之四:进程管理 */
  296. |   |-- BUILD.gn
  297. |   |-- include
  298. |   |   |-- los_config.h
  299. |   |   |-- los_event.h
  300. |   |   |-- los_membox.h
  301. |   |   |-- los_memory.h
  302. |   |   |-- los_mux.h
  303. |   |   |-- los_queue.h
  304. |   |   |-- los_sched.h
  305. |   |   |-- los_sem.h
  306. |   |   |-- los_sortlink.h
  307. |   |   |-- los_swtmr.h
  308. |   |   |-- los_task.h
  309. |   |   `-- los_tick.h
  310. |   `-- src
  311. |       |-- los_event.c
  312. |       |-- los_init.c
  313. |       |-- los_mux.c
  314. |       |-- los_queue.c
  315. |       |-- los_sched.c
  316. |       |-- los_sem.c
  317. |       |-- los_sortlink.c
  318. |       |-- los_swtmr.c
  319. |       |-- los_task.c
  320. |       |-- los_tick.c
  321. |       `-- mm
  322. |           |-- los_membox.c
  323. |           `-- los_memory.c
  324. |-- liteos.gni
  325. |-- testsuites        /* 移植系统后进行自测用的,不用关心 */
  326. |   ......
  327. |-- tools
  328. |   `-- mem_analysis.py
  329. `-- utils
  330.     |-- BUILD.gn
  331.     |-- internal
  332.     |   |-- los_hook_types.h
  333.     |   `-- los_hook_types_parse.h
  334.     |-- los_compiler.h
  335.     |-- los_debug.c
  336.     |-- los_debug.h        /* 串口调试输出的级别 */
  337.     |-- los_error.c
  338.     |-- los_error.h        /* 所有模块都会用的,要返回的错误码 */
  339.     |-- los_hook.c
  340.     |-- los_hook.h
  341.     |-- los_list.h        /* 链表,队列、模块缓存的基础 */
  342.     `-- los_reg.h
  343. 198 directories, 1571 files
复制代码


  • 底子的操作体系内核代码里没有太多可借鉴的软件结构,它们都是针对某一项功能而实现一项功能,通用的东西较少,接下来我会从操作体系移植的角度稍微分析一下。
为了能让各人更好的学习鸿蒙(HarmonyOS NEXT)开辟技术,这边特意整理了《鸿蒙开辟学习手册》(共计890页),希望对各人有所帮助:https://qr21.cn/FV7h05
《鸿蒙开辟学习手册》:

如何快速入门:https://qr21.cn/FV7h05


  • 基本概念
  • 构建第一个ArkTS应用
  • ……

开辟底子知识:https://qr21.cn/FV7h05


  • 应用底子知识
  • 配置文件
  • 应用数据管理
  • 应用安全管理
  • 应用隐私保护
  • 三方应用调用管控机制
  • 资源分类与访问
  • 学习ArkTS语言
  • ……

基于ArkTS 开辟:https://qr21.cn/FV7h05


  • Ability开辟
  • UI开辟
  • 公共事件与关照
  • 窗口管理
  • 媒体
  • 安全
  • 网络与链接
  • 电话服务
  • 数据管理
  • 后台使命(Background Task)管理
  • 设备管理
  • 设备利用信息统计
  • DFX
  • 国际化开辟
  • 折叠屏系列
  • ……

鸿蒙开辟口试真题(含参考答案):https://qr18.cn/F781PH


鸿蒙开辟口试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开辟必备口试题
2.性能优化方向
3.架构方向
4.鸿蒙开辟体系底层方向
5.鸿蒙音视频开辟方向
6.鸿蒙车载开辟方向
7.鸿蒙南向开辟方向


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表