libpynq (release 5EWC0-2023 version 0.2.6 of 2023-10-24 17:28)
Loading...
Searching...
No Matches
adc.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 <adc.h>
24#include <errno.h>
25#include <log.h>
26#include <platform.h>
27#include <stdio.h>
28#include <stdlib.h>
29
30static struct arm_shared_t adc_handle;
31static volatile uint32_t *adc = NULL;
32
33static const uint32_t twopow16 = 0b10000000000000000;
34
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
57bool 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
71bool 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
78void adc_init(void) { adc = arm_shared_init(&adc_handle, xadc_wiz_0, 4096); }
79
80void adc_destroy(void) {
81 if (adc != NULL) {
82 (void)arm_shared_close(&adc_handle);
83 adc = NULL;
84 }
85}
86
87double adc_read_channel(const adc_channel_t channel) {
88 (void)check_channel_adc(channel);
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);
100
101 if (adc == NULL) {
102 return UINT32_MAX;
103 }
104 uint32_t value = adc[channel];
105
106 return value;
107}
bool invalid_channel_adc(const adc_channel_t channel)
Definition adc.c:35
bool check_initialized_adc(void)
Definition adc.c:64
bool check_channel_adc(const adc_channel_t channel)
Definition adc.c:71
void adc_init(void)
Initialization of the ADC library.
Definition adc.c:78
bool initialized_adc(void)
Check if ADC has been initialized.
Definition adc.c:57
double adc_read_channel(const adc_channel_t channel)
Definition adc.c:87
uint32_t adc_read_channel_raw(adc_channel_t channel)
Definition adc.c:97
void adc_destroy(void)
De-initialize the ADC library and free up the used memory in the shared memory space.
Definition adc.c:80
adc_channel_t
Enumerate the different available ADC channels.
Definition adc.h:43
@ ADC5
Definition adc.h:55
@ ADC2
Definition adc.h:49
@ ADC4
Definition adc.h:53
@ ADC1
Definition adc.h:47
@ ADC3
Definition adc.h:51
@ ADC0
Definition adc.h:45
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