NRF52832之原子操作

nrfx_atomic_u32_store和nrfx_atomic_u32_fetch_store函数赋值操作

    nrfx_atomic_u32_t test;
	uint32_t ret;
    ret	= nrfx_atomic_u32_store(&test,4);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	 ret	= nrfx_atomic_u32_store(&test,3);
	 NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret = nrfx_atomic_u32_fetch_store(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
    ret = nrfx_atomic_u32_fetch_store(&test,8);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_u32_store函数操作返回值和操作的原子变量值都一样
  • nrfx_atomic_u32_fetch_store函数操作返回值为原子变量操作前的值,操作后的原子变量值为传参的值

nrfx_atomic_u32_or和nrfx_atomic_u32_fetch_or函数按位或操作

    nrfx_atomic_u32_t test = 0;
	uint32_t ret;
    ret	= nrfx_atomic_u32_or(&test,4);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	 ret	= nrfx_atomic_u32_or(&test,3);
	 NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret = nrfx_atomic_u32_fetch_or(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
    ret = nrfx_atomic_u32_fetch_or(&test,8);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_u32_or函数操作返回值和操作的原子变量值都一样
  • nrfx_atomic_u32_fetch_or函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值按位或上传参的值

nrfx_atomic_u32_and和nrfx_atomic_u32_fetch_and函数按位与操作

    nrfx_atomic_u32_t test = 0xff;
    uint32_t ret;
    ret	= nrfx_atomic_u32_and(&test,0xfe);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	 ret	= nrfx_atomic_u32_and(&test,0xfd);
	 NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret = nrfx_atomic_u32_fetch_and(&test,0xfb);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
    ret = nrfx_atomic_u32_fetch_and(&test,0xf7);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_u32_and函数操作返回值和操作的原子变量值都一样
  • nrfx_atomic_u32_fetch_and函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值按位与上传参的值

nrfx_atomic_u32_xor和nrfx_atomic_u32_fetch_xor函数按位异或操作

    nrfx_atomic_u32_t test = 0;
	uint32_t ret;
    ret	= nrfx_atomic_u32_xor(&test,1);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret	= nrfx_atomic_u32_xor(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret = nrfx_atomic_u32_fetch_xor(&test,4);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
    ret = nrfx_atomic_u32_fetch_xor(&test,8);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_u32_xor函数操作返回值和操作的原子变量值都一样
  • nrfx_atomic_u32_fetch_xor函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值按位异或上传参的值

nrfx_atomic_u32_add和nrfx_atomic_u32_fetch_add函数加法操作

    nrfx_atomic_u32_t test = 0;
	uint32_t ret;
    ret	= nrfx_atomic_u32_add(&test,1);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret	= nrfx_atomic_u32_add(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret = nrfx_atomic_u32_fetch_add(&test,1);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
    ret = nrfx_atomic_u32_fetch_add(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_u32_add函数操作返回值和操作的原子变量值都一样
  • nrfx_atomic_u32_fetch_add函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值加上传参的值

nrfx_atomic_u32_sub和nrfx_atomic_u32_fetch_sub函数减法操作

    nrfx_atomic_u32_t test = 10;
	uint32_t ret;
    ret	= nrfx_atomic_u32_sub(&test,1);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret	= nrfx_atomic_u32_sub(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret = nrfx_atomic_u32_fetch_sub(&test,1);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
    ret = nrfx_atomic_u32_fetch_sub(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_u32_sub函数操作返回值和操作的原子变量值都一样
  • nrfx_atomic_u32_fetch_sub函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值减去传参的值

nrfx_atomic_flag_set和nrfx_atomic_flag_set_fetch函数设置成true操作

	nrfx_atomic_u32_t test = 0;
	uint32_t ret;
  
 
	ret	= nrfx_atomic_flag_set(&test);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	test = 0;
	
    ret = nrfx_atomic_flag_set_fetch(&test);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_flag_set函数操作返回值和操作的原子变量值都一样为真
  • nrfx_atomic_flag_set_fetch函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值为真

nrfx_atomic_flag_clear和nrfx_atomic_flag_clear_fetch函数清除成false操作

    nrfx_atomic_u32_t test = 1;
	uint32_t ret;
  
 
	ret	= nrfx_atomic_flag_clear(&test);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	test = 1;
	
    ret = nrfx_atomic_flag_clear_fetch(&test);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述
  • nrfx_atomic_flag_clear函数操作返回值和操作的原子变量值都一样为假
  • nrfx_atomic_flag_clear_fetch函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值为假

nrfx_atomic_u32_sub_hs和nrfx_atomic_u32_fetch_sub_hs函数减法限制操作

    nrfx_atomic_u32_t test = 10;
	uint32_t ret;
  
 
	ret	= nrfx_atomic_u32_sub_hs(&test,1);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	ret	= nrfx_atomic_u32_sub_hs(&test,10);
	NRF_LOG_INFO("ret %d test %d",ret,test);
	
	test = 10;
	
    ret = nrfx_atomic_u32_fetch_sub_hs(&test,2);
	NRF_LOG_INFO("ret %d test %d",ret,test);

	ret = nrfx_atomic_u32_fetch_sub_hs(&test,10);
	NRF_LOG_INFO("ret %d test %d",ret,test);
  • 运行结果
    在这里插入图片描述 - nrfx_atomic_u32_sub函数操作返回值和操作的原子变量值都一样,如果原子变量值小于传参的值,则原子变量操作的值和返回值为原子变量操作前的值
  • nrfx_atomic_u32_fetch_sub函数操作返回值为原子变量操作前的值,操作后的原子变量值为操作前原子变量值减去传参的值,如果原子变量值小于传参的值,则原子变量操作的值和返回值为原子变量操作前的值

nrfx_atomic_u32_cmp_exch函数比较操作

    nrfx_atomic_u32_t test = 10;
	bool ret;
  
	uint32_t exp = 10;
 
	ret	= nrfx_atomic_u32_cmp_exch(&test,&exp,6);
	NRF_LOG_INFO("ret %d test %d exp %d",ret,test,exp);
	
	test = 8;

    ret	= nrfx_atomic_u32_cmp_exch(&test,&exp,5);
	NRF_LOG_INFO("ret %d test %d exp %d",ret,test,exp);

运行结果
在这里插入图片描述

  • nrfx_atomic_u32_cmp_exch函数参数1和参数2的值相等,返回值返回true,并且参数1的值更新为参数3的值,如果参数1的值不等于参数2的值,返回值返回false,并且参数2的值更新为参数1的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值