CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/Linux_HAL_Essentials/pru/rcinpru/rcinpru0.c
Views: 1800
1
/*
2
* testpru
3
*
4
*/
5
6
#define PRU0
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <stdarg.h>
10
#include <stdio.h>
11
#include <strings.h>
12
13
#include "linux_types.h"
14
#include "pru_defs.h"
15
#include "prucomm.h"
16
17
18
void add_to_ring_buffer(uint8_t v, uint16_t deltat)
19
{
20
RBUFF->buffer[RBUFF->ring_tail].pin_value = v;
21
RBUFF->buffer[RBUFF->ring_tail].delta_t = deltat;
22
RBUFF->ring_tail = (RBUFF->ring_tail + 1) % NUM_RING_ENTRIES;
23
}
24
25
static inline u32 read_PIEP_COUNT(void)
26
{
27
return PIEP_COUNT;
28
}
29
30
uint32_t read_pin(void){
31
return ((__R31&(1<<15)) != 0);
32
}
33
34
void main()
35
{
36
uint32_t last_time_us = 0;
37
uint8_t last_pin_value = 0;
38
39
/*PRU Initialisation*/
40
PRUCFG_SYSCFG &= ~SYSCFG_STANDBY_INIT;
41
PRUCFG_SYSCFG = (PRUCFG_SYSCFG &
42
~(SYSCFG_IDLE_MODE_M | SYSCFG_STANDBY_MODE_M)) |
43
SYSCFG_IDLE_MODE_NO | SYSCFG_STANDBY_MODE_NO;
44
45
/* our PRU wins arbitration */
46
PRUCFG_SPP |= SPP_PRU1_PAD_HP_EN;
47
48
/* configure timer */
49
PIEP_GLOBAL_CFG = GLOBAL_CFG_DEFAULT_INC(1) |
50
GLOBAL_CFG_CMP_INC(1);
51
PIEP_CMP_STATUS = CMD_STATUS_CMP_HIT(1); /* clear the interrupt */
52
PIEP_CMP_CMP1 = 0x0;
53
PIEP_CMP_CFG |= CMP_CFG_CMP_EN(1);
54
PIEP_GLOBAL_CFG |= GLOBAL_CFG_CNT_ENABLE;
55
56
57
RBUFF->ring_tail = 20;
58
while (1) {
59
uint32_t v;
60
while ((v=read_pin()) == last_pin_value) {
61
// noop
62
}
63
uint32_t now = read_PIEP_COUNT()/200;
64
uint32_t delta_time_us = now - last_time_us;
65
last_time_us = now;
66
67
add_to_ring_buffer(last_pin_value, delta_time_us);
68
last_pin_value = v;
69
}
70
}
71
72