libpynq (release 5EWC0-2023 version 0.2.6 of 2025-11-10 09:56)
Loading...
Searching...
No Matches
pulsecounter.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22#include "pulsecounter.h"
24#include "log.h"
25#include "version.h"
26#include <platform.h>
27#include <stdio.h>
28#include <string.h>
29
30static arm_shared pulsecounter_handles[NUM_PULSECOUNTERS];
31static volatile uint32_t *pulsecounter_ptrs[NUM_PULSECOUNTERS] = {
32 NULL,
33};
34
35const uint32_t PULSECOUNTER_PULSES = 0;
36const uint32_t PULSECOUNTER_COUNTER = 1;
37const uint32_t PULSECOUNTER_EDGE = 2;
38const uint32_t PULSECOUNTER_FILTER = 3;
39const uint32_t PULSECOUNTER_INTERVALS = 4;
41
43 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
44 pynq_error("invalid pci %d, must be 0..%d\n", pci, NUM_PULSECOUNTERS);
45 }
46 if (pci == PULSECOUNTER0) {
47 pulsecounter_ptrs[pci] =
48 arm_shared_init(&(pulsecounter_handles[pci]), axi_timer_0, 4096);
49 } else if (pci == PULSECOUNTER1) {
50 pulsecounter_ptrs[pci] =
51 arm_shared_init(&(pulsecounter_handles[pci]), axi_timer_1, 4096);
52 }
53
55 if (!(v.major > 0 || (v.major == 0 && v.minor >= 4))) {
56 pynq_error("Bitfile to old to use with pulsecounter.");
57 }
58}
59
61 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
62 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
63 }
64 arm_shared_close(&(pulsecounter_handles[pci]));
65 pulsecounter_ptrs[pci] = NULL;
66}
67
69 uint32_t *timestamp) {
70 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
71 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
72 }
73 uint32_t retv = pulsecounter_ptrs[pci][PULSECOUNTER_PULSES];
74 if (timestamp != NULL) {
75 *timestamp = pulsecounter_ptrs[pci][PULSECOUNTER_COUNTER];
76 }
77 return retv;
78}
79
80void pulsecounter_set_edge(const pulsecounter_index_t pci, const int edge) {
81 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
82 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
83 }
84 pulsecounter_ptrs[pci][PULSECOUNTER_EDGE] = edge;
85}
86
88 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
89 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
90 }
91 return (pulsecounter_ptrs[pci][PULSECOUNTER_EDGE]);
92}
94 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
95 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
96 }
97 // TODO: implement in hardware.
98 pulsecounter_ptrs[pci][PULSECOUNTER_PULSES] = 0;
99}
100
102 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
103 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
104 }
105 return pulsecounter_ptrs[pci][PULSECOUNTER_FILTER];
106}
107
109 uint8_t const count) {
110 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
111 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
112 }
113 if (count < 1 || count > 15) {
114 pynq_error("Filter length needs to be between 1 and 16-1.");
115 }
116 pulsecounter_ptrs[pci][PULSECOUNTER_FILTER] = count;
117}
118
120 uint32_t *const inv,
121 const uint32_t inv_length) {
122 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
123 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
124 }
125
126 for (uint32_t l = 0; l < inv_length; l++) {
127 inv[l] = pulsecounter_ptrs[pci][PULSECOUNTER_INTERVALS];
128 if (inv[l] == 0) {
129 return l + 1;
130 }
131 }
132 return inv_length;
133}
135
136 if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
137 pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
138 }
139 return pulsecounter_ptrs[pci][PULSECOUNTER_INTERVALS_COUNT];
140}
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
#define pynq_error(...)
Definition log.h:118
void pulsecounter_reset_count(const pulsecounter_index_t pci)
pulsecounter_index_t
Enum of PULSECOUNTERs. Functions use a switch numbered from 0..NUM_PULSECOUNTERS-1.
void pulsecounter_set_edge(const pulsecounter_index_t pci, const int edge)
uint32_t pulsecounter_get_pulsetrain_count(const pulsecounter_index_t pci)
uint32_t pulsecounter_get_pulsetrain(const pulsecounter_index_t pci, uint32_t *const inv, const uint32_t inv_length)
void pulsecounter_set_filter_length(const pulsecounter_index_t pci, uint8_t const count)
void pulsecounter_destroy(const pulsecounter_index_t pci)
Close the shared memory handle for the specified PULSECOUNTER index.
uint32_t pulsecounter_get_count(const pulsecounter_index_t pci, uint32_t *timestamp)
void pulsecounter_init(const pulsecounter_index_t pci)
initialize the pulsecounter specified by the index with a shared memory pointer
uint8_t pulsecounter_get_filter_length(const pulsecounter_index_t pci)
int pulsecounter_get_edge(const pulsecounter_index_t pci)
@ PULSECOUNTER0
@ NUM_PULSECOUNTERS
@ PULSECOUNTER1
version_t read_version(void)
Definition version.c:78
const uint32_t PULSECOUNTER_PULSES
const uint32_t PULSECOUNTER_COUNTER
const uint32_t PULSECOUNTER_INTERVALS_COUNT
const uint32_t PULSECOUNTER_INTERVALS
const uint32_t PULSECOUNTER_EDGE
const uint32_t PULSECOUNTER_FILTER
uint32_t major
Definition version.h:65
uint32_t minor
Definition version.h:66