当前位置:网站首页>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.
边栏推荐
- CDF 图的含义
- 李斌带不动的长安新能源高端梦,华为和“宁王”能救吗?
- x86 与 x64 架构下函数参数传递的区别【汇编语言】
- v-if指令:操作dom元素(标签)的显示与隐藏
- cmake记录
- LeetCode-876. Middle of the Linked List
- How to generate code using the Swift Package plugin
- rtsp 和 rtmp 推流(一)
- [FreeRTOS] 13 Dynamic Memory Management
- How to realize full backup and incremental backup of MySQL database
猜你喜欢
Gif动图制作怎么在线操作?一招教你快速完成gif在线制作
Annual salary of 600,000+?This 100,000-word interview assault book covers all technology stacks from Ali P5 engineers to P7
Etcd Kubernetes 集群稳定性:LIST 请求源码分析、性能评估与大规模基础服务部署调优
国内油价四连跌,但下跌趋势可能终止
开源生态与AI芯片的碰撞&Dragonfly基于P2P的镜像加速系统 | 第 39-40 期
视频转成gif动图怎么操作?仅需三步在线完成视频转gif
从宠爱到嫌弃,蒸发1500亿后,这家明星企业为何陨落?
WinUI 3 Fundamentals 5小时教学视频
Go+:首个顺应 “三位一体” 发展潮流的编程语言
险资又做LP,一出手40亿
随机推荐
秘密共享方案介绍SS
怎么设计产品手册?用什么设计产品手册?
PNG如何变gif?教你一招png秒变gif动图的方法
基础填空以及编程题
潜水员 ← 二维费用的背包问题
LeetCode-692. Top K Frequent Words
MySQL数据库命令
How to use bitwise operators in C language
一文带你彻底拿下a,b两点间等效电阻
软件配置 | pip下载第三方库文件及配置pip源的不完全总结
观测云入选 CNCF 云原生全景图
LeetCode-876. Middle of the Linked List
WIZnet 物联网设计大赛 - WizFi360大赛延迟通知
C语言按位运算符如何使用
CDF 图的含义
C专家编程 第10章 再论指针 10.3 在锯齿状数组上使用指针
视频转gif怎样操作?1分钟在线视频转gif制作
植物肉,为何在中国没法“真香”?
openpyxl绘制堆叠图
C专家编程 第10章 再论指针 10.7 使用指针创建和使用动态数组