当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
C专家编程 第10章 再论指针 10.3 在锯齿状数组上使用指针
剑指OfferⅡ 045.二叉树最底层最左边的值 dfs
requests库访问接口
mysql按月查询统计(统计近12个月的项目个数)
被大厂面试官参考的Redis笔记,堪称Redis面试天花板
MS | 使用小技巧不完全总结
快速申请代码签名证书方法
Taurus.MVC WebAPI 入门开发教程4:控制器方法及参数定义、获取及基础校验属性【Require】。
【21天学习挑战赛】折半查找
分享几个自动化测试的练手项目
MySQL的使用演示及操作,MySQL数据字符集的设置
聊聊云原生数据平台
rtsp 和 rtmp 推流(一)
C专家编程 第10章 再论指针 10.7 使用指针创建和使用动态数组
Spike project harvest
cmake记录
1001 A+B Format(字符串处理)
异常处理的超详细讲解
Pigsty:开箱即用的开源数据库发行版
面了个腾讯25k+出来的,他让我见识到什么基础的天花板









