libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
pulsecounter.c
Go to the documentation of this file.
1 /*
2 Copyright (c) 2023 Eindhoven University of Technology
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22 #include "pulsecounter.h"
24 #include "log.h"
25 #include <platform.h>
26 #include <stdio.h>
27 #include <string.h>
28 
29 static arm_shared pulsecounter_handles[NUM_PULSECOUNTERS];
30 static volatile uint32_t *pulsecounter_ptrs[NUM_PULSECOUNTERS] = {
31  NULL,
32 };
33 
34 const uint32_t PULSECOUNTER_PULSES = 0;
35 const uint32_t PULSECOUNTER_COUNTER = 1;
36 const uint32_t PULSECOUNTER_EDGE = 2;
37 const uint32_t PULSECOUNTER_FILTER = 3;
38 
40  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
41  pynq_error("invalid pci %d, must be 0..%d\n", pci, NUM_PULSECOUNTERS);
42  }
43  if (pci == PULSECOUNTER0) {
44  pulsecounter_ptrs[pci] =
45  arm_shared_init(&(pulsecounter_handles[pci]), axi_timer_0, 4096);
46  } else if (pci == PULSECOUNTER1) {
47  pulsecounter_ptrs[pci] =
48  arm_shared_init(&(pulsecounter_handles[pci]), axi_timer_1, 4096);
49  }
50 }
51 
53  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
54  pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
55  }
56  arm_shared_close(&(pulsecounter_handles[pci]));
57  pulsecounter_ptrs[pci] = NULL;
58 }
59 
61  uint32_t *timestamp) {
62  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
63  pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
64  }
65  uint32_t retv = pulsecounter_ptrs[pci][PULSECOUNTER_PULSES];
66  if (timestamp != NULL) {
67  *timestamp = pulsecounter_ptrs[pci][PULSECOUNTER_COUNTER];
68  }
69  return retv;
70 }
71 
73  const gpio_level_t edge) {
74  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
75  pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
76  }
77  pulsecounter_ptrs[pci][PULSECOUNTER_EDGE] = edge == GPIO_LEVEL_HIGH ? 1 : 0;
78 }
79 
81  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
82  pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
83  }
84  return (pulsecounter_ptrs[pci][PULSECOUNTER_EDGE] ? GPIO_LEVEL_HIGH
85  : GPIO_LEVEL_LOW);
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  pulsecounter_ptrs[pci][PULSECOUNTER_PULSES] = 0;
92 }
93 
95  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
96  pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
97  }
98  return pulsecounter_ptrs[pci][PULSECOUNTER_FILTER];
99 }
100 
102  uint8_t const count) {
103  if (!(pci >= PULSECOUNTER0 && pci < NUM_PULSECOUNTERS)) {
104  pynq_error("invalid pci %d, must be 0..%d-1\n", pci, NUM_PULSECOUNTERS);
105  }
106  if (count < 1 || count > 15) {
107  pynq_error("Filter length needs to be between 1 and 16-1.");
108  }
109  pulsecounter_ptrs[pci][PULSECOUNTER_FILTER] = count;
110 }
PULSECOUNTER_PULSES
const uint32_t PULSECOUNTER_PULSES
Definition: pulsecounter.c:34
pulsecounter_set_edge
void pulsecounter_set_edge(const pulsecounter_index_t pci, const gpio_level_t edge)
Definition: pulsecounter.c:72
pulsecounter.h
pynq_error
#define pynq_error(...)
Definition: log.h:118
arm_shared_memory_system.h
arm_shared_t
Definition: arm_shared_memory_system.h:39
arm_shared_close
void arm_shared_close(arm_shared *handle)
Definition: arm_shared_memory_system.c:70
pulsecounter_get_count
uint32_t pulsecounter_get_count(const pulsecounter_index_t pci, uint32_t *timestamp)
Definition: pulsecounter.c:60
GPIO_LEVEL_LOW
@ GPIO_LEVEL_LOW
Definition: gpio.h:100
PULSECOUNTER_COUNTER
const uint32_t PULSECOUNTER_COUNTER
Definition: pulsecounter.c:35
pulsecounter_init
void pulsecounter_init(const pulsecounter_index_t pci)
initialize the pulsecounter specified by the index with a shared memory pointer
Definition: pulsecounter.c:39
pulsecounter_destroy
void pulsecounter_destroy(const pulsecounter_index_t pci)
Close the shared memory handle for the specified PULSECOUNTER index.
Definition: pulsecounter.c:52
PULSECOUNTER_FILTER
const uint32_t PULSECOUNTER_FILTER
Definition: pulsecounter.c:37
PULSECOUNTER1
@ PULSECOUNTER1
Definition: pulsecounter.h:47
pulsecounter_get_filter_length
uint8_t pulsecounter_get_filter_length(const pulsecounter_index_t pci)
Definition: pulsecounter.c:94
gpio_level_t
gpio_level_t
Definition: gpio.h:98
pulsecounter_reset_count
void pulsecounter_reset_count(const pulsecounter_index_t pci)
Definition: pulsecounter.c:87
PULSECOUNTER_EDGE
const uint32_t PULSECOUNTER_EDGE
Definition: pulsecounter.c:36
PULSECOUNTER0
@ PULSECOUNTER0
Definition: pulsecounter.h:46
log.h
arm_shared_init
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
Definition: arm_shared_memory_system.c:32
NUM_PULSECOUNTERS
@ NUM_PULSECOUNTERS
Definition: pulsecounter.h:48
pulsecounter_get_edge
gpio_level_t pulsecounter_get_edge(const pulsecounter_index_t pci)
Definition: pulsecounter.c:80
GPIO_LEVEL_HIGH
@ GPIO_LEVEL_HIGH
Definition: gpio.h:102
pulsecounter_index_t
pulsecounter_index_t
Enum of PULSECOUNTERs. Functions use a switch numbered from 0..NUM_PULSECOUNTERS-1.
Definition: pulsecounter.h:45
pulsecounter_set_filter_length
void pulsecounter_set_filter_length(const pulsecounter_index_t pci, uint8_t const count)
Definition: pulsecounter.c:101