1、Porting Zephyr OS to a SoC(Cortex-R5)M Tamseel ShamsAssociate Staff EngineerSamsung Semiconductor India ResearchAgenda About Zephyr OS Porting Components Hardware Support Implementation Debugging Tips Key tools and build commands Timers UART Driver Things for FutureAbout Zephyr OS Zephyr is a small
2、real-time operating system(RTOS)for connected,resource-constrained and embedded devices Zephyr OS is based on small scale kernel to support simple embedded system It is a mini kernel which provides similar features like Linux kernel.Like memory management,multi threading,scheduler,interrupt handling
3、 Supports wide verity of CPU architectures Provides build system(west)which makes easy to compile multiple kernel modules along with user applications Provides SDK which has tool chains for different architectures at one placeFig.1:Zephyr System ArchitectureImage Reference:https:/ this session will
4、discuss about key folder changes and adding new project to Zephyr Adding a new SoC Adding a new Board Adding new Drivers Adding Device TrePorting ComponentsImage Reference:https:/docs.zephyrproject.org/3.1.0/hardware/porting/board_porting.htmlFig.2:Zephyr Configuration HierarchyHardware Support Impl
5、ementationFig.3 Architecture:arm CPU core:CORTEX_R5 SoC family:_(e.g.samsung_xyz)SoC Series:CR5 SoC:CR5 Drivers:Serial,Timer,Interrupt,etc.Board:Samsung BoardHardware Configuration Hierarchy:Architecture-arc,arm,nios2,posix,riscv32,x86,xtensa,etc.CPU Core-Implements early boot sequence,interrupt and
6、 exception handling,thread context switching,thread creation and termination,CPU idling/power management,fault management,linker scripts and toolchains.E.g.ARCV2,CORTEX_M0PLUS,CORTEX_R5,etc.SoC Family-Represents a single SoC type that can have more than one variations in terms of peripherals and fea
7、tures.E.g.KINETIS,IMX,NRF,EXX32,STM32,etc.Hardware Support Implementation SoC Series-Represents the specific peripherals and features for the SoC family variations.E.g.NRF51X,NRF52X,etc.SoC-The actual SoC that is“soldered”in the hardware platform and its configuration.E.g.MKL25Z32VFM4,MCIMX7D5EVM10S
8、C,etc.Drivers-Include device model responsible for configuring and initialize drivers.Each driver follows a device model API and a specific driver type API.E.g.interrupt controller,timer,serial communications,etc.Board-Includes a SoC and its associated peripherals and features including external com
9、ponents and devices.E.g.NRF51_BLENANO,NUCLEO_F103RB,etc.Hardware Support ImplementationHardware Support Implementation Top level hardware configurations are defined via Kconfigs and the final processing results located in the files:build/zephyr/.configbuild/zephyr/include/generated/autoconf.h Low le
10、vel hardware specific configurations are defined via device tree and the final processing results located in the files:build/zephyr/include/generated/generated_dts_board.confbuild/zephyr/include/generated/generated_dts_board.h Defines the SOC_FAMILY,SOC_SERIES,SOC and SOC_PART_NUMBER configs Located
11、 at:soc/(soc/arm/samsung_xyz/cr5/)Each SoC folder holds all its basic information about processor,IRQ controller,flash details,linkers,soc related init configurations Called in the system initialization process with priority 0 Provides a soc.h header which will be included by the board and drivers s
12、ources Contains a set of Kconfig files,linker definitions,and device tree fixupsAdding a new SoCsoc/arm/samsung_xyz/SOC Family CMakeLists.txt Kconfig Kconfig.defconfig Kconfig.soc Kconfig.zephyrver cr5 SOC variant CMakeLists.txt Kconfig.defconfig.series Kconfig.series Kconfig.soc linker.ld soc.c soc
13、.hAdding a new SoC Default Architecture,SOC_FAMILY,SOC_SERIES configs are selected inboards/_defconfig(boards/arm/samsung_xyz/samsung_cr5_defconfig)Has a dtsi defining peripherals and features properties presented in the SoC and is located atdts/_.dtsi(dts/arm/samsung_xyz/samsung_cr5.dtsi)Device tre
14、e include files which contains main SoC details,and IP nodes like IRQ controller,CPU information,etc.These dtsi files will be included in actual board dts files Dtsi files are reusable there is no strict rule to create new dtsi file every soc Location is dts/_.dtsi(dts/arm/samsung_xyz/samsung_cr5.dt
15、si)Instead of requiring additional runtime memory to store the DTB blob,the DTS information is used during compile time The compiled DTS files are then cross checked using rules specified in YAML bindings.The extracted information is placed in a header file that is used by the rest of the code as th
16、e project is compiledAdding DTS fileAdding DTS filedts/arm/samsung_xyz/samsung_cr5.dtsi samsung_m0.dtsicpus#address-cells=;#size-cells=;cpu0 device_type=cpu;compatible=arm,cortex-r5;reg=;soc interrupt-parent=;gic:interrupt-controller8000000 compatible=arm,gic;reg=,;interrupt-controller;#interrupt-ce
17、lls=;label=GIC;status=okay;Adding Board This folder represent actual application hardware Located at boards/(boards/arm/samsung_xyz/)Board specific init information will be defined here Contains a _defconfig file to select which SoC and basic features and interfaces includedAdding Boardboards/arm/sa
18、msung/board.c CMakeLists.txt Kconfig.board Kconfig.defconfig samsung_cr5_defconfig samsung_cr5.dts samsung_cr5.yaml While compiling zephyr image we always refer board name,which indirectly includes all soc,dtsi related information Contains a board.h to be used by the drivers and applications May pro
19、vide a board.cmake to instruct how to flash/debug Includes a.yaml file to list the board properties:e.g.flash and ram sizes and toolchain used,etc.Located at drivers/New drivers are added under drivers folder,and if the driver related to any specific protocol like serial,dma,usb,etc.should add under
20、 that sub folder Selection and configuration done via Kconfigs and device tree Initialization performed during the kernel boot Yaml file to describe the device tree nodes and properties Device tree file to define driver properties and configurationsAdding Drivers Steps to add driver to compilation:o
21、Apart from driver files,Kconfig files need to be created or need to give driver entry in main Kconfig file.oAdd respective driver entry in CMakeList of that driver folder./drivers/serial/CMakeLists.txt Kconfig|Kconfig.cr5 Steps to add driver to init sequence:#define DT_DRV_COMPAT cr5_uart(this same
22、as device tree compatible name)DEVICE_AND_API_INIT this macro has different variants with different arguments but all it do add drivers to init sequence.Hook driver APIs to zephyr frame work.Adding Drivers Look at other source code reference to understand what needs to be done to initialize the SoC
23、Try to print to UART(accessing the registers directly)in the SoC initialization to guarantee that the core is up and running Implement the UART driver first,printk is life Turn on the System Logging or Logger Turn on asserts(CONFIG_ASSERT)to try to catch errors Use a on-chip debugger(UltraSoC,J-Link
24、,ULINK etc.)Debugging Tips Zephyr build system use these important tools Cmake-Holds all source code build steps Yaml-these files provide information,used mostly for documentation,on help commands West-this tool provide build commands which internally use cmakelist which are defined in each sub dire
25、ctory Zephyr uses west commands to build west init (To init zephyr build)west board(To list available boards)west build-b samsung_cr5 samples/hello_world/-p Key tools and build commands A timer is a kernel object that measures the passage of time using the kernels system clock When a timers specifie
26、d time limit is reached it can perform an application-defined action,or wait Any number of timers can be defined.Each timer is referenced by its memory address A timer has the following key properties:duration,period,expiry function,stop function Implementation available for using timers:Defining a
27、timer-A timer is defined using a variable of type k_timer.It must then be initialized by calling k_timer_init()Using a Timer Expiry Function Reading Timer Status Using Timer Status SynchronizationTimers This sample demonstrates how to use the UART serial driver with a simple echo bot It reads data f
28、rom the console and echoes the characters back after an end of line(return key)is received The polling API is used for sending data and the interrupt-driven API for receiving By default,the UART peripheral that is normally used for the Zephyr shell is used,so that almost every board should be suppor
29、ted Building and runningwest build-b nrf52840dk_nrf52840 samples/drivers/uart/echo_botwest flash Sample OutputHello!Im your echo bot.Tell me something and press enter:#Type e.g.Hi there!and hit enter!Echo:Hi there!UART Driver Zephyr has frame work for most of generic drivers but how to add individua
30、l drivers to it need to be explored.Device tree can hold most of IP hardware details like clocks,IRQ line,address,register set,etc.and all these entries can be read from device driver itself with defined macros which need to be explored.All pre defined linker scripts,dtsi files can be modified and used in build.This areas need to be explored if any booting issues facedThings for FutureTHANK YOU