-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPart_192.c
144 lines (112 loc) · 3.94 KB
/
Part_192.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*Copyright (c) 2017, Adam Taylor
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project*/
#include <stdio.h>
#include "xparameters.h"
#include "xil_cache.h"
#include "PmodAD2.h"
#include "xspips.h"
#define SPI_MIO XPAR_XSPIPS_0_DEVICE_ID
#define max_count 4095
static XSpiPs SpiInstance_MIO;
u8 op_buff[4] = {0x00,0x01,0x02,0xaa};
#ifdef XPAR_MICROBLAZE_ID
#include "microblaze_sleep.h"
#else
#include "sleep.h"
#endif
void DemoInitialize();
void DemoRun();
//static void SendHandler(PmodAD2 *InstancePtr);
//static void ReceiveHandler(PmodAD2 *InstancePtr);
PmodAD2 myDevice;
//void print(char *str);
int main()
{
Xil_ICacheEnable();
Xil_DCacheEnable();
DemoInitialize();
DemoRun();
return 0;
}
void DemoInitialize()
{
XSpiPs_Config *SpiConfig_MIO;
int Status;
xil_printf("UART Established\n\r");
SpiConfig_MIO = XSpiPs_LookupConfig((u16)SPI_MIO);
Status = XSpiPs_CfgInitialize(&SpiInstance_MIO, SpiConfig_MIO,SpiConfig_MIO->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XSpiPs_SetOptions(&SpiInstance_MIO, XSPIPS_MASTER_OPTION | XSPIPS_CLK_PHASE_1_OPTION);
XSpiPs_SetClkPrescaler(&SpiInstance_MIO, XSPIPS_CLK_PRESCALE_128);
//internal reference
op_buff[0] = 0x10;
op_buff[1] = 0x00;
op_buff[2] = 0x00;
op_buff[3] = 0x01;
XSpiPs_PolledTransfer(&SpiInstance_MIO, op_buff, NULL,4);
//power up
op_buff[0] = 0x08;
op_buff[1] = 0x00;
op_buff[2] = 0x00;
op_buff[3] = 0xff;
XSpiPs_PolledTransfer(&SpiInstance_MIO, op_buff, NULL,4);
AD2_begin(&myDevice, XPAR_PMODAD2_0_AXI_LITE_IIC_BASEADDR, AD2_IIC_ADDR);
//AD2_SetupInterruptSystem(&myDevice, XPAR_PS7_SCUGIC_0_DEVICE_ID, XPAR_FABRIC_AXI_IIC_0_IIC2INTC_IRPT_INTR, SendHandler, ReceiveHandler);
//XIic_Start(&myDevice.AD2Iic);
}
void DemoRun()
{
//u16 an[4],
u16 conv;
u8 buf[2] = {0,0};
double val;
u16 count;
//int nan = 4, i = 0;
printf("Demo Started\n\r");
AD2_WriteConfig(&myDevice, AD2_DEFAULT_CONFIG);
//Default config is 0xF0, all channels on
printf("AD2 Configured\n\r");
op_buff[0] = 0x03;
op_buff[3] = 0x00;
while(1) {
AD2_ReadConv(&myDevice, buf, 2);
conv = (buf[0] << 8) | buf[1];
val = (double) AD2_CONV_VALUE(conv);
val = (val * 3.3)/4096.0;
printf("CHANNEL %d = %d.%d V\n\r", AD2_CONV_CHANNEL(conv), (int)val, (int)(val*100)%100);
//an[AD2_CONV_CHANNEL(conv)] = AD2_CONV_VALUE(conv);
if (count == 2048) {
count = 0;
}
else {
count ++;
}
op_buff[1] = 0xff & count >>8;
op_buff[1] = 0xf0 | op_buff[1];
op_buff[2] = (u8) count;
XSpiPs_PolledTransfer(&SpiInstance_MIO, op_buff, NULL,4);
//usleep(1000000);
}
}