当前位置:网站首页>Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers
Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers
2022-08-10 16:50:00 【Guangdong education embedded】
A fundamental issue in driver design is deciding how to map to peripheral registers.Embedded developers have used many different approaches over the years, such as building structures to define bitmaps, or simply writing desired values to registers; however, my all-time favorite approach is to create a map that maps to the peripheralAn array of pointers to registers.This approach provides an elegant way of grouping peripheral registers into logical channels and provides an easy way to initialize peripherals and access their data.The pointer array approach is easily portable and can be used to create standard APIs and application code that work on different hardware platforms, allowing application code to be shared.When written properly, it can also create code that is easier to read and understand, making software maintenance easier.
The concept of an array of pointers is a relatively straightforward way to map to peripherals.The idea is to create an array where each index of the array is a pointer to a specific type of peripheral register.For example, for a microcontroller with multiple GPIO ports, an array of pointers would be set up to access the direction register for each available port (Listing 1).Another array of pointers will be set up to access the input and output registers.Each register type will be associated with its own array of pointers.
Listing 1 – GPIO pointer array
It is important to note how the array of pointers is declared.The pointer array portsddr is a constant pointer to a mutable uint16.Note that declarations are defined from right to left.A pointer to a register is a constant pointer, but declaring it as a volatile uint16_t informs the compiler that the value pointed to may change on its own without software interaction.
There are many benefits to embedded developers using this approach to memory mapping.First, it allows functionally identical registers to be logically grouped together.This allows software engineers to treat each peripheral as an independent channel of the MCU.For example, Timer 1 and Timer 2 can be seen as two different timer channels.Setting each timer's period register requires simply writing to the appropriate channel index of the period pointer array.The index of the pointer array becomes the channel access index.For example, pointer array index 0 would be associated with timer 1; pointer array index 1 would be associated with timer 2.
Next, when peripherals start to look like channels, it becomes easy to create an abstract method that not only initializes, but also accesses each peripheral's data.This allows a simple loop to initialize each peripheral (Listing 2).The peripheral's data can be accessed simply by using the correct channel index.This makes the driver framework not only easy to understand and reuse, but also a framework for abstracting device registers.
Listing 2 - Timer initialization loop
Finally, it allows developers to create configuration tables for each peripheral.Instead of always writing custom initialization code, embedded developers can create a reusable driver that takes configuration tables as parameters.The initialization function then loops one channel at a time and initializes the peripheral registers via the array of pointers.This makes the driver a library module that is tested again and again, producing proven code that can speed up the next project.
边栏推荐
猜你喜欢
随机推荐
MySQL数据库完全备份与增量备份怎么实现
PYSPARK ON YARN报错集合
软件工程基础知识--需求分析
数据治理项目成功的要点,企业培养数据要把握好关键环节
面了个腾讯25k+出来的,他让我见识到什么基础的天花板
C专家编程 第10章 再论指针 10.5 使用指针向函数传递一个多维数组
李斌带不动的长安新能源高端梦,华为和“宁王”能救吗?
蓝桥ROS之 cmake gcc g++ 默认版本和升级
C language symbols on how to use
华为-坐标移动
Meaning of CDF graph
商业智能BI行业分析思维框架:铅酸蓄电池行业(二)
使用Jedis连接linux上的redis
FTXUI基础笔记(botton按钮组件基础)
Pigsty:开箱即用的开源数据库发行版
C专家编程 第10章 再论指针 10.8 轻松一下---程序检验的限制
Go+:首个顺应 “三位一体” 发展潮流的编程语言
个人博客部署注意的几点问题
如何将jpg静图做成gif动图?教你1分钟快速合成gif
视频转成gif动图怎么操作?仅需三步在线完成视频转gif