span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}.CodeMirror-sizer {min-height:auto !important;}
-->
span::selection, .cm-s-base16-light .CodeMirror-line > span > span::selection { background: #e0e0e0; }.cm-s-base16-light .CodeMirror-line::-moz-selection, .cm-s-base16-light .CodeMirror-line > span::-moz-selection, .cm-s-base16-light .CodeMirror-line > span > span::-moz-selection { background: #e0e0e0; }.cm-s-base16-light .CodeMirror-gutters { background: #f5f5f5; border-right: 0px; }.cm-s-base16-light .CodeMirror-guttermarker { color: #ac4142; }.cm-s-base16-light .CodeMirror-guttermarker-subtle { color: #b0b0b0; }.cm-s-base16-light .CodeMirror-linenumber { color: #b0b0b0; }.cm-s-base16-light .CodeMirror-cursor { border-left: 1px solid #505050; }.cm-s-base16-light span.cm-comment { color: #8f5536; }.cm-s-base16-light span.cm-atom { color: #aa759f; }.cm-s-base16-light span.cm-number { color: #aa759f; }.cm-s-base16-light span.cm-property, .cm-s-base16-light span.cm-attribute { color: #90a959; }.cm-s-base16-light span.cm-keyword { color: #ac4142; }.cm-s-base16-light span.cm-string { color: #f4bf75; }.cm-s-base16-light span.cm-variable { color: #90a959; }.cm-s-base16-light span.cm-variable-2 { color: #6a9fb5; }.cm-s-base16-light span.cm-def { color: #d28445; }.cm-s-base16-light span.cm-bracket { color: #202020; }.cm-s-base16-light span.cm-tag { color: #ac4142; }.cm-s-base16-light span.cm-link { color: #aa759f; }.cm-s-base16-light span.cm-error { background: #ac4142; color: #505050; }.cm-s-base16-light .CodeMirror-activeline-background { background: #DDDCDC; }.cm-s-base16-light .CodeMirror-matchingbracket { text-decoration: underline; color: white !important; }
-->
li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-latin;}.wiz-editor-body ol.wiz-list-level3 > li {list-style-type:lower-roman;}.wiz-editor-body li.wiz-list-align-style {list-style-position: inside; margin-left: -1em;}.wiz-editor-body blockquote {padding: 0 12px;}.wiz-editor-body blockquote > :first-child {margin-top:0;}.wiz-editor-body blockquote > :last-child {margin-bottom:0;}.wiz-editor-body img {border:0;max-width:100%;height:auto !important;margin:2px 0;}.wiz-editor-body table {border-collapse:collapse;border:1px solid #bbbbbb;}.wiz-editor-body td,.wiz-editor-body th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-word;box-sizing: border-box;}.wiz-editor-body td > div:first-child {margin-top:0;}.wiz-editor-body td > div:last-child {margin-bottom:0;}.wiz-editor-body img.wiz-svg-image {box-shadow:1px 1px 4px #E8E8E8;}.wiz-hide {display:none !important;}
-->

TF-A链接:
 
在阅读TF-A源代码时,看到其udelay是实现如下:
 
/***********************************************************
* Delay for the given number of microseconds. The driver must
* be initialized before calling this function.
***********************************************************/
void udelay(uint32_t usec)
{
assert((timer_ops != NULL) &&
(timer_ops->clk_mult != 0U) &&
(timer_ops->clk_div != 0U) &&
(timer_ops->get_timer_value != NULL)); uint32_t start, delta, total_delta; assert(usec < (UINT32_MAX / timer_ops->clk_div)); start = timer_ops->get_timer_value(); /* Add an extra tick to avoid delaying less than requested. */
total_delta =
div_round_up(usec * timer_ops->clk_div,
timer_ops->clk_mult) + 1U; do {
/*
* If the timer value wraps around, the subtraction will
* overflow and it will still give the correct result.
*/
delta = start - timer_ops->get_timer_value(); /* Decreasing counter */ } while (delta < total_delta);
}
 
第16行的get_timer_value返回的是一个从0xFFFFFFFF到0的递减的数,减到0后,再往下减的话,会重新变成0xFFFFFFFF。
其中第25到26行注释说即使发生了wraps around,也可以保证delta的值是正确的。下面我们看看是什么原理。
为此,下面是一个简单的模拟程序,看看发生wraps around后,两个数字相减的结果,假设下面的a,b,c的范围都是从0到0xFF,a表示第一次读到的数,b表示第二次读到的数。
 
#include <stdio.h>

int main(int argc, const char *argv[])
{
unsigned char a, b, c; a = 0x20;
b = 0x00;
c = a - b;
printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); a = 0x00;
b = 0xE0;
c = a - b;
printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); a = 0x00;
b = 0x20;
c = a - b;
printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); a = 0xF0;
b = 0x10;
c = a - b;
printf("a(0x%x) - b(0x%x) = %x\n", a, b, c); return ;
}
 
下面是运行结果:
a(0x20) - b(0x0) =
a(0x0) - b(0xe0) =
a(0x0) - b(0x20) = e0
a(0xf0) - b(0x10) = e0

可以看到,如果是以无符号数输出的话,上面的大数减小数和小数减大数的结果是一样的。

原因是负数在计算机中是以补码的形式存放,关于补码的介绍可以参考:https://baike.baidu.com/item/%E8%A1%A5%E7%A0%81/6854613?fr=aladdin
 
可以直观的理解,在模为10的情况下,一个数加上A,跟减去(10-A)的结果一样:比如 (3 + 8)%10 = 1,而(3-(10-8))%10 = 1,二者一样。
 
或者可以手动计算一下:0 - 0xe0,理论上应该等于-0xe0,那么这个数字在计算机中如何表示呢?也就是-0xe0的补码是多少?根据负数求补码的方法,其绝对值各位取反然后再加1:
-0xe0 --> 0xe(绝对值) --> 0b11100000(二进制表示) --> 0b00011111(取反) --> 0b00100000(加一) --> 0x20,即在模为0x100的情况下,-0xe0跟0x20是一回事。
 
完。

delay timer的wrap around的更多相关文章

  1. java Timer 使用小结

    Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务.这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少. 任务的调用通过起的子线程进 ...

  2. Java Timer及TimerTarsk(摘自网络)

    Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务. 这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少.任务的调用通过起的子线程进 ...

  3. Linux时间子系统之(十七):ARM generic timer驱动代码分析

    专题文档汇总目录 Notes:ARM平台Clock/Timer架构:System counter.Timer以及两者之间关系:Per cpu timer通过CP15访问,System counter通 ...

  4. Linux时间子系统(十七) ARM generic timer驱动代码分析

    一.前言 关注ARM平台上timer driver(clocksource chip driver和clockevent chip driver)的驱动工程师应该会注意到timer硬件的演化过程.在单 ...

  5. 第一次用写一个3d轮播

    2016-07-11gallery  3d html <!doctype html><html lang="en"><head> <met ...

  6. 进程调度函数scheduler_tick()的触发原理:周期PERIODIC定时器

    参考文章: https://www.jb51.net/article/133579.htm https://blog.csdn.net/flaoter/article/details/77509553 ...

  7. 【原】AFNetworking源码阅读(一)

    [原]AFNetworking源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 AFNetworking版本:3.0.4 由于我平常并没有经常使用AFNetw ...

  8. AFNetworking 3.0 源码解读(九)之 AFNetworkActivityIndicatorManager

    让我们的APP像艺术品一样优雅,开发工程师更像是一名匠人,不仅需要精湛的技艺,而且要有一颗匠心. 前言 AFNetworkActivityIndicatorManager 是对状态栏中网络激活那个小控 ...

  9. [WPF系列]-DataBinding(数据绑定) 自定义Binding

    自定义Binding A base class for custom WPF binding markup extensions BindingDecoratorBase Code: public c ...

随机推荐

  1. nginx 动静分离之 tomcat

    配置文件示例 server { listen ; server_name www.xxx.com; location ~* "\.(jpg|png|jepg|js|css|xml|bmp|s ...

  2. CF1276 D. Tree Elimination

    CF1276 D. Tree Elimination 传送门 CodeForces Solution 考虑树型\(dp\),设\(f_{u,0/1/2/3}\)分别表示点\(u\)被自己父亲边之前的边 ...

  3. Linux搭建简单的http文件服务器

    为了让自动化脚本可以通过wget来下载安装包,需要在集群中的某个节点部署一个http文件服务器 在Ubuntu中通过apt-get install apache2 安装apache2CentOS7中通 ...

  4. 将物理机转换成vmware虚机

    随着虚拟化的快速发展,公司主要是以公有云+私有云结合的混合云部署,据我不成熟的了解,目前很少有公司会将一台单独的物理机作为服务器,在公司内部大家逐渐接受了私有云的部署方案,这样做不但可以节省硬件资源, ...

  5. 【Activiti学习之七】BPMN子流程、顺序流、流程关口

    环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.子流程 1.嵌入子流程2.调用子流程3.事件子流程4.事务子流程 二.顺序流1.条件 ...

  6. 记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法

    记一次查询超时的解决方案The timeout period elapsed...... https://www.cnblogs.com/wyt007/p/9274613.html Exception ...

  7. Python 3.X 练习集100题 02

    企业发放的奖金根据利润提成.利润(I):低于或等于10万元时,奖金可提10%:高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万之间时,高 ...

  8. JavaSE 面试题: 类初始化和实例初始化等

    JavaSE 面试题 类初始化和实例初始化等 class Father { private int i = test(); private static int j = method(); stati ...

  9. [转帖]centos7 使用kubeadm 快速部署 kubernetes 国内源

    centos7 使用kubeadm 快速部署 kubernetes 国内源 https://www.cnblogs.com/qingfeng2010/p/10540832.html 前言 搭建kube ...

  10. npm是干什么的(转)

    原文:https://zhuanlan.zhihu.com/p/24357770 网上的 npm 教程主要都在讲怎么安装.配置和使用 npm,却不告诉新人「为什么要使用 npm」.今天我就来讲讲这个话 ...