[nRF51822] 8、基础实验代码解析大全 · 实验11 - PPI
前一篇分析了前十个基础实验的代码,从这里开始分析后十个~
一、PPI原理:
PPI(Programmable Peripheral Interconnect),中文翻译为可编程外设互连。
在nRF51822 内部设置了PPI 方式,可以通过任务和事件让不同外设之间进行互连,而不需要CPU 进行参与。
PPI 通过通道让任务和事件连接在一起。PPI 通道由两个端点组成:
- 任务端点:Task End-Point (TEP)。
- 事件端点:Event End-Point (EEP)。
所谓的互联就是将任务端点写入需要连接的任务寄存器地址,事件端点写入需要连接事件寄存器地址,之后,使能该PPI 通道,即实现了任务和事件的互联。
可以通过如下两种方式使能和关闭PPI 通道:
- 1) 通过独立设置CHEN,CHENSET 和CHENCLR 寄存器。
- 2) 通过PPI 通道组的使能和关闭任务。使用这种方式,在触发任务之前,需要先配置好哪些PPI 通道属于哪个组。
二、运行逻辑:
实验中,用到了3 个定时器:Timer 0、Timer 1 和Timer 2。
1) Timer 0 配置为计数器,在主循环中每100ms 被触发一次,并通过串口打印出计数值。
2) Timer 1 每个偶数秒(2、4、6、8……)产生一次比较匹配事件,该事件通过PPI通道0 和Timer 0 的STOP Task 互联,互联后通过该事件触发Timer 0 的STOP Task。
3) Timer 2 每个奇数秒(1、3、5、7……)产生一次比较匹配事件,该事件通过PPI通道1 和Timer 0 的START Task 互联,互联后通过该事件触发Timer 0 的START Task。
实验原理框图如图1 所示:

三、核心代码分析
系统运行后,在循环中Timer 0 计数器的计数值每100ms 增加一次,在偶数秒时,Timer2 产生比较匹配事件,通过PPI 触发Timer 0 的STOP Task,Timer 0 停止计数。此时,尽管主循环中每隔100ms 触发一次Timer 0 计数,但是由于Timer 0 已经停止,所以,计数值不会增加。每个奇数秒,Timer2 产生比较匹配事件,通过PPI 触发Timer 0 的START Task,Timer 0 恢复计数。
main函数部分:
int main(void)
{
timer0_init(); // Timer used to blink the LEDs.
timer1_init(); // Timer to generate events on even number of seconds.
timer2_init(); // Timer to generate events on odd number of seconds.
ppi_init(); // PPI to redirect the event to timer start/stop tasks. 串口初始化(略) // Enabling constant latency as indicated by PAN 11 "HFCLK: Base current with HFCLK
// running is too high" found at Product Anomaly document found at
// https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
//
// @note This example does not go to low power mode therefore constant latency is not needed.
// However this setting will ensure correct behaviour when routing TIMER events through
// PPI (shown in this example) and low power mode simultaneously.
NRF_POWER->TASKS_CONSTLAT = ; // Start clock.
nrf_drv_timer_enable(&timer0);
nrf_drv_timer_enable(&timer1);
nrf_drv_timer_enable(&timer2); // Loop and increment the timer count value and capture value into LEDs. @note counter is only incremented between TASK_START and TASK_STOP.
while (true)
{ printf("Current cout: %d\r\n", (int)nrf_drv_timer_capture(&timer0,NRF_TIMER_CC_CHANNEL0)); /* increment the counter */
nrf_drv_timer_increment(&timer0); nrf_delay_ms();
}
}
定时器初始化部分:
// Timer even handler. Not used since timer is used only for PPI.
void timer_event_handler(nrf_timer_event_t event_type, void * p_context){} /** @brief Function for Timer 0 initialization, which will be started and stopped by timer1 and timer2 using PPI.
*/
static void timer0_init(void)
{
ret_code_t err_code = nrf_drv_timer_init(&timer0, NULL, timer_event_handler);
APP_ERROR_CHECK(err_code);
} /** @brief Function for Timer 1 initialization.
* @details Initializes Timer 1 peripheral, creates event and interrupt every 2 seconds,
* by configuring CC[0] to timer overflow value, we create events at even number of seconds
* for example, events are created at 2,4,6 ... seconds. This event can be used to stop Timer 0
* with Timer1->Event_Compare[0] triggering Timer 0 TASK_STOP through PPI.
*/
static void timer1_init(void)
{
// Configure Timer 1 to overflow every 2 seconds. Check TIMER1 configuration for details
// The overflow occurs every 0xFFFF/(SysClk/2^PRESCALER).
// = 65535/31250 = 2.097 sec
ret_code_t err_code = nrf_drv_timer_init(&timer1, NULL, timer_event_handler);
APP_ERROR_CHECK(err_code); nrf_drv_timer_extended_compare(&timer1, NRF_TIMER_CC_CHANNEL0, 0xFFFFUL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);//比较模式,Timer 1 每个偶数秒(2、4、6、8……)产生一次比较匹配事件,该事件通过PPI通道0 和Timer 0 的STOP Task 互联,互联后通过该事件触发Timer 0 的STOP Task。
} /** @brief Function for Timer 2 initialization.
* @details Initializes Timer 2 peripheral, creates event and interrupt every 2 seconds
* by configuring CC[0] to half of timer overflow value. Events are created at odd number of seconds.
* For example, events are created at 1,3,5,... seconds. This event can be used to start Timer 0
* with Timer2->Event_Compare[0] triggering Timer 0 TASK_START through PPI.
*/
static void timer2_init(void)
{
// Generate interrupt/event when half of time before the timer overflows has past, that is at 1,3,5,7... seconds from start.
// Check TIMER1 configuration for details
// now the overflow occurs every 0xFFFF/(SysClk/2^PRESCALER)
// = 65535/31250 = 2.097 sec */
ret_code_t err_code = nrf_drv_timer_init(&timer2, NULL, timer_event_handler);
APP_ERROR_CHECK(err_code); nrf_drv_timer_extended_compare(&timer2, NRF_TIMER_CC_CHANNEL0, 0x7FFFUL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);//Timer 2 每个奇数秒(1、3、5、7……)产生一次比较匹配事件,该事件通过PPI通道1 和Timer 0 的START Task 互联,互联后通过该事件触发Timer 0 的START Task。
}
PPI连接事件部分:
/** @brief Function for initializing the PPI peripheral.
*/
static void ppi_init(void)
{
uint32_t err_code = NRF_SUCCESS; err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code); // Configure 1st available PPI channel to stop TIMER0 counter on TIMER1 COMPARE[0] match, which is every even number of seconds.
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1);
APP_ERROR_CHECK(err_code);
13 err_code = nrf_drv_ppi_channel_assign(ppi_channel1,//PPI连接事件
14 nrf_drv_timer_event_address_get(&timer1, NRF_TIMER_EVENT_COMPARE0),
15 nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_STOP));
APP_ERROR_CHECK(err_code); // Configure 2nd available PPI channel to start timer0 counter at TIMER2 COMPARE[0] match, which is every odd number of seconds.
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel2);
APP_ERROR_CHECK(err_code);
21 err_code = nrf_drv_ppi_channel_assign(ppi_channel2,
22 nrf_drv_timer_event_address_get(&timer2, NRF_TIMER_EVENT_COMPARE0),
23 nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_START));
APP_ERROR_CHECK(err_code); // Enable both configured PPI channels
err_code = nrf_drv_ppi_channel_enable(ppi_channel1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel2);
APP_ERROR_CHECK(err_code);
}
@beautifulzzzz - 物联网&普适计算实践者
e-mail:beautifulzzzz@qq.com
i-blog:blog.beautifulzzzz.com
[nRF51822] 8、基础实验代码解析大全 · 实验11 - PPI的更多相关文章
- [nRF51822] 12、基础实验代码解析大全 · 实验19 - PWM
一.PWM概述: PWM(Pulse Width Modulation):脉冲宽度调制技术,通过对一系列脉冲的宽度进行调制,来等效地获得所需要波形. PWM 的几个基本概念: 1) 占空比:占空比是指 ...
- [nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写
一.实验内容: 通过串口发送单个字符到NRF51822,NRF51822 接收到字符后将其写入到FLASH 的最后一页,之后将其读出并通过串口打印出数据. 二.nRF51822芯片内部flash知识 ...
- [nRF51822] 10、基础实验代码解析大全 · 实验15 - RTC
一.实验内容: 配置NRF51822 的RTC0 的TICK 频率为8Hz,COMPARE0 匹配事件触发周期为3 秒,并使能了TICK 和COMPARE0 中断. TICK 中断中驱动指示灯D1 翻 ...
- [nRF51822] 9、基础实验代码解析大全 · 实验12 - ADC
一.本实验ADC 配置 分辨率:10 位. 输入通道:5,即使用输入通道AIN5 检测电位器的电压. ADC 基准电压:1.2V. 二.NRF51822 ADC 管脚分布 NRF51822 的ADC ...
- [nRF51822] 7、基础实验代码解析大全(前十)
实验01 - GPIO输出控制LED 引脚输出配置:nrf_gpio_cfg_output(LED_1); 引脚输出置高:nrf_gpio_pin_set(LED_1); 引脚电平转换:nrf_gpi ...
- 基础Gan代码解析
initializer总结: #f.constant_initializer(value) 将变量初始化为给定的常量,初始化一切所提供的值. #tf.random_normal_initializer ...
- MYSQL常见出错mysql_errno()代码解析
如题,今天遇到怎么一个问题, 在理论上代码是不会有问题的,但是还是报了如上的错误,把sql打印出來放到DB中却可以正常执行.真是郁闷,在百度里面 渡 了很久没有相关的解释,到时找到几个没有人回复的 & ...
- 【原创】大数据基础之Spark(5)Shuffle实现原理及代码解析
一 简介 Shuffle,简而言之,就是对数据进行重新分区,其中会涉及大量的网络io和磁盘io,为什么需要shuffle,以词频统计reduceByKey过程为例, serverA:partition ...
- 【原创】大数据基础之Spark(4)RDD原理及代码解析
一 简介 spark核心是RDD,官方文档地址:https://spark.apache.org/docs/latest/rdd-programming-guide.html#resilient-di ...
随机推荐
- oracle普通表转分区表(在线重定义方式)
1.1.TAB_TAOBAO_BILL 1.1.1检查下这张表是否可以在线重定义,无报错表示可以,报错会给出错误信息: exec dbms_redefinition.can_redef_table(' ...
- ADT + JNI实例
Author: Maddock Date: 2015-07-09 本文简单记录了Android中利用jni开发程序初级教程: 步骤 1 下载安装ADT 2 配置NDK 3 新建安卓工程 4 测试jni ...
- ubuntu14.04下配置Java环境以及安装最新版本的eclipse
首先是配置JDK 步骤一:下载最新版本的JDK,链接:http://www.oracle.com/technetwork/java/javase/downloads/index.html 步骤二:首先 ...
- Java 之 异常处理
1.异常: a.问题(Throwable):异常(Exception).错误(Error) 异常(Exception):运行时异常(RuntimeException).编译时异常(Unhandle ...
- python 小程序 比较目录间的差异
比较目录间的差异: I 只按照名称做了比较,如果目录的文件名称相同,但是内容不同脚本认为为相同文件 II 针对目录下面的目录没有循环比较,只是比较了目录的名称 import sys, os def d ...
- 查看java源码显示source not found
除了下载的jar包之外,还要下载src源码的zip包. 进项目的Java Build Path里找到你想看源码的jar包 Source Attachment Configuration--Extern ...
- 【刷题记录】GCJ 2.71~2.72
GCJ 271 [题目大意] Minimum Scalar Product 有两个东西(滑稽)v1=(x1,x2,x3,……,xn)和v2=(y1,y2,……yn),允许任意交换v1和v2中各数字的顺 ...
- 亲临现场不是梦,2017央视春晚推出VR直播
自里约奥运会首次试水VR直播 后,用户开始关注这种观影方式,一瞬间VR直播开始流行.就在月初,江苏卫视宣布2017年跨年晚会将进行VR全景直播.当然,央视是绝对不会错过这中潮流方式. 据悉,央视201 ...
- RESTFUL Architecture
Just review some articles about RESTFUL stuff, my understanding is RESTFUL is another more general v ...
- 步骤进度条 css
用css写一个简单的步骤进度条 html代码: <h4>南京游玩</h4> <ul class="step-list"> <li> ...