libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
adc.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 <adc.h>
24 #include <errno.h>
25 #include <log.h>
26 #include <platform.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 static struct arm_shared_t adc_handle;
31 static volatile uint32_t *adc = NULL;
32 
33 static const uint32_t twopow16 = 0b10000000000000000;
34 
35 bool invalid_channel_adc(const adc_channel_t channel) {
36  if (channel == ADC0) {
37  return false;
38  }
39  if (channel == ADC1) {
40  return false;
41  }
42  if (channel == ADC2) {
43  return false;
44  }
45  if (channel == ADC3) {
46  return false;
47  }
48  if (channel == ADC4) {
49  return false;
50  }
51  if (channel == ADC5) {
52  return false;
53  }
54  return true;
55 }
56 
57 bool initialized_adc(void) {
58  if (adc == NULL) {
59  return false;
60  }
61  return true;
62 }
63 
65  if (!initialized_adc()) {
66  pynq_error("The ADC has not been initialized\n");
67  }
68  return true;
69 }
70 
71 bool check_channel_adc(const adc_channel_t channel) {
72  if (invalid_channel_adc(channel)) {
73  pynq_error("Invalid ADC channel %d\n", channel);
74  }
75  return true;
76 }
77 
78 void adc_init(void) { adc = arm_shared_init(&adc_handle, xadc_wiz_0, 4096); }
79 
80 void adc_destroy(void) {
81  if (adc != NULL) {
82  (void)arm_shared_close(&adc_handle);
83  adc = NULL;
84  }
85 }
86 
87 double adc_read_channel(const adc_channel_t channel) {
88  (void)check_channel_adc(channel);
89  (void)check_initialized_adc();
90 
91  // TODO we need to calibrate this
92  double value = adc[channel] * (3.23 / twopow16);
93 
94  return value;
95 }
96 
98  (void)check_channel_adc(channel);
99  (void)check_initialized_adc();
100 
101  if (adc == NULL) {
102  return UINT32_MAX;
103  }
104  uint32_t value = adc[channel];
105 
106  return value;
107 }
adc_channel_t
adc_channel_t
Enumerate the different available ADC channels.
Definition: adc.h:43
pynq_error
#define pynq_error(...)
Definition: log.h:118
adc_destroy
void adc_destroy(void)
De-initialize the ADC library and free up the used memory in the shared memory space.
Definition: adc.c:80
arm_shared_memory_system.h
ADC1
@ ADC1
Definition: adc.h:47
check_initialized_adc
bool check_initialized_adc(void)
Definition: adc.c:64
initialized_adc
bool initialized_adc(void)
Check if ADC has been initialized.
Definition: adc.c:57
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
adc.h
ADC0
@ ADC0
Definition: adc.h:45
adc_init
void adc_init(void)
Initialization of the ADC library.
Definition: adc.c:78
ADC2
@ ADC2
Definition: adc.h:49
ADC3
@ ADC3
Definition: adc.h:51
ADC5
@ ADC5
Definition: adc.h:55
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
invalid_channel_adc
bool invalid_channel_adc(const adc_channel_t channel)
Definition: adc.c:35
check_channel_adc
bool check_channel_adc(const adc_channel_t channel)
Definition: adc.c:71
adc_read_channel
double adc_read_channel(const adc_channel_t channel)
Definition: adc.c:87
ADC4
@ ADC4
Definition: adc.h:53
adc_read_channel_raw
uint32_t adc_read_channel_raw(adc_channel_t channel)
Definition: adc.c:97