当前位置:网站首页>Friends let me see this code
Friends let me see this code
2022-07-25 13:14:00 【Embedded Linux,】
On the weekend , A wechat friend asked me to look at a piece of code
Before writing the following article , Let me start with a few words
Writing code is a very interesting thing , At the same time, it is also a matter that we need to take seriously , I don't think it's necessary to write the code as unreadable as the great God , But at least be logical , Cannot appear confusion Don't know which What happened .
Last year , I am also constantly improving my programming ability , Previous work , Many of them do things like processing systems and drivers , But the actual application programming does not have a strong foundation . Is also constantly learning and moving forward , I also thank my friends and colleagues for their help and support .
——
This code looks like this
static unsigned short timer_tick_cnt = 0;
static ___interrupt USER_TIMER void timer_isr(void)// Interrupt service function
{
if (TMR->CON & BIT(15)) // Timer overflow interrupt flag bit
{
TMR->CON |= BIT(14);// Clearing and interrupting
///putchar('@');
//todo, Interrupt function execution program ...
timer_tick_cnt++;
}
}
static unsigned short timer_tick_get_current_timer(void)
{
unsigned short timer_tick_count = timer_tick_cnt;
return timer_tick_count;
}
unsigned short usRecord = 0;
unsigned char timer_tick_timeout_wait(unsigned short *p_var, unsigned short timeout)
{
if( timer_tick_get_current_timer() - *p_var < timeout )
{
///usRecord = timer_tick_get_current_timer();
///printf("=======rec=%d,*p_var=%d=====\n",usRecord,*p_var);
///printf("====TIMER return 0====\n");
return 0;
}
*p_var = timer_tick_get_current_timer();
///usRecord///printf("====TIMER return 1====\n");
return 1;
}
unsigned short testTimer = 0;
#define TIME_TICK_1MS_1S 1000
void testHandler(void)
{
if(timer_tick_timeout_wait(&testTimer,TIME_TICK_1MS_1S))//1ms Interrupt overflow of , so 1000 by 1s
{
printf("helloworld\n");// The problem is if timer_tick_timeout_wait Parameters of timeout Defined as unsigned short, In about 65s(65535ms) This function always returns 0 Instead of returning 1,unsigned int normal
}
}
int main(void)
{
timerInit();//1ms The timer is interrupted , This function is not a problem , The problem lies below
while(1)
{
testHandler();
}
}This is the original code , I didn't make any changes .
The problem he described in the code is relatively clear
// The problem is if timer_tick_timeout_wait Parameters of timeout Defined as unsigned short, In about 65s(65535ms) This function always returns 0 Instead of returning 1,unsigned int normal
Careful people will find a problem

this timer_tick_cnt Damn variable keeps increasing
I wrote an article before , It's the timestamp problem in the kernel , If there is a problem with the variable definition of saving timestamp , That may also cause time problems .
That's why he found something wrong with his code .
——
Let me talk about other situations
The original intention of the system he designed is that the system timer executes a function after a specified time , But directly take the variables in the timer to the outside to judge , It's not very good here .
The timer should only complete the timed things , As for the time , Just tell other tasks outside , This can achieve high cohesion .
Variable timer_tick_cnt Operate in other places , It will be very uncomfortable to check the following problems .


——
Variable name 、 Function name 、 Code style.
It's just ugly , Underline some places 、 Humps are used in some places .

And the execution of this function , I can't understand it

CPU That's how you're tired .
About variable and function naming website , I recommend this , This website is very suitable for us
https://www.chtml.cn

It's not much better than you think .
——
The meaning of macros is not clear

Naming macros like this is very bad , First, 1MS again 1S, So what is this macro , It's hard to understand the meaning without looking at the code .
If I were , I'll show you what this macro does , But then add MS, Because this event is MS Time for .
——
You may also find the naming of functions , Some are humps , Some underline .
That's not the point , The key is that the intention of some functions is not clear , I don't know what the function is .
These are my own personal views
I think writing code is to treat a work of Art , Do this thing well , Well done , It's a pleasure for us .
Let's encourage it !


边栏推荐
- TCP的拥塞控制
- 【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
- 部署Apache网站服务以及访问控制的实现
- Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
- Django 2 ----- 数据库与Admin
- mysql函数汇总之日期和时间函数
- 简单了解流
- R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
- 【历史上的今天】7 月 25 日:IBM 获得了第一项专利;Verizon 收购雅虎;亚马逊发布 Fire Phone
- 央行数研所穆长春:数字人民币可控匿名是维护公众利益和金融安全的客观需要
猜你喜欢

Zero basic learning canoe panel (16) -- clock control/panel control/start stop control/tab control

Design and principle of thread pool

程序员奶爸自制AI喂奶检测仪,预判宝宝饿点,不让哭声影响老婆睡眠

【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】

Django 2 ----- database and admin
![Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]](/img/70/2b5130be16d7699ef7db58d9065253.png)
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]

从输入网址到网页显示

G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像

ThreadLocal&Fork/Join

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
随机推荐
Requirements specification template
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
mysql函数汇总之日期和时间函数
B树和B+树
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
Excel import and export source code analysis
arm架构移植alsa-lib和alsa-utils一路畅通
Mu Changchun, data Research Institute of the central bank: controllable anonymity of digital RMB is an objective need to safeguard public interests and financial security
Zero basic learning canoe panel (13) -- trackbar
pytorch创建自己的Dataset加载数据集
需求规格说明书模板
ECCV 2022 | 登顶SemanticKITTI!基于二维先验辅助的激光雷达点云语义分割
Generate SQL script file by initializing the latest warehousing time of vehicle attributes
如何理解Keras中的指标Metrics
[six articles talk about scalablegnn] around www 2022 best paper PASCA
go : gin 自定义日志输出格式
Docekr learning - MySQL 8 master-slave replication setup deployment
Oran special series-21: major players (equipment manufacturers) and their respective attitudes and areas of expertise
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
详解浮点数的精度问题