您当前的位置: 首页 > 

仙剑情缘

暂无认证

  • 0浏览

    0关注

    333博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

NRF52832之原子操作

仙剑情缘 发布时间:2021-09-19 12:01:25 ,浏览量:0

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的值
关注
打赏
1658017818
查看更多评论
立即登录/注册

微信扫码登录

0.0367s