-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathanalogShield.h
157 lines (132 loc) · 5.26 KB
/
analogShield.h
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/************************************************************************/
/* */
/* analogShield.h -- Library for Analog Shield */
/* Version - 2.1 */
/* */
/************************************************************************/
/* Author: William J. Esposito */
/* Copyright 2014, Digilent Inc. */
/************************************************************************/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/************************************************************************/
/* Module Description: */
/* */
/* This module contains the implementation of the object class that */
/* forms a chipKIT/Aruino interface to the Analog Shield functions of */
/* the Texas Instrument chipKit/arduino shield. */
/* */
/* */
/************************************************************************/
/* Revision History: */
/* */
/* 04/22/2014(WilliamE): Created */
/* 05/27/2014(MarshallW): Modified for readability and content */
/* 02/16/2015(MarshallW): ChipKIT Efficiency update! */
/* 03/23/2015(WilliamE): Mega 2560 Support */
/* */
/* Todo: */
/* - Framework for DUE added but not tested */
/* */
/************************************************************************/
/* ------------------------------------------------------------ */
/* Include File Definitions */
/* ------------------------------------------------------------ */
#ifndef _analogShield_h_
#define _analogShield_h_
#if defined(__PIC32MX__)
#include <WProgram.h>
#include <inttypes.h>
#include <SPI.h>
#elif defined(__AVR__)
#include <stdio.h>
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <SPI.h>
#elif defined (__SAM3X8E__)
#include <stdio.h>
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <SPI.h>
//define some SPI configs
#define SPI_CLOCK_DIV4 0x00
#define SPI_CLOCK_DIV16 0x01
#define SPI_CLOCK_DIV64 0x02
#define SPI_CLOCK_DIV128 0x03
#define SPI_CLOCK_DIV2 0x04
#define SPI_CLOCK_DIV8 0x05
#define SPI_CLOCK_DIV32 0x06
//#define SPI_CLOCK_DIV64 0x07
#define SPI_MODE0 0x00
#define SPI_MODE1 0x04
#define SPI_MODE2 0x08
#define SPI_MODE3 0x0C
#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
#else //throw error
#error "Incorrect processor type selected"
#endif
#include <stdint.h>
//pins
#define adccs 2
#define adcbusy 3
#define syncPin 5
#define ldacPin 6
#define DIFF_MODE true
class analogShield {
private:
// SPI Configuration methods
void writeNoUpdate(int channel, unsigned int value);
void writeAllUpdate(int channel, unsigned int value);
int shieldMode;
void setChannelAndModeByte(byte channel, bool mode);
public:
#if defined(__PIC32MX__)
volatile uint32_t *ADCCSSet;
volatile uint32_t *ADCCSClr;
uint32_t ADCCSPinMask;
volatile uint32_t *syncPinSet;
volatile uint32_t *syncPinClr;
uint32_t syncPinPinMask;
volatile uint32_t *ldacPinSet;
volatile uint32_t *ldacPinClr;
uint32_t ldacPinPinMask;
volatile uint32_t *ADCBusyPtr;
uint32_t ADCBusyPinMask;
#elif defined(__SAM3X8E__)
volatile uint32_t *ADCCSPtr;
uint32_t SetADCCSPinMask;
uint32_t ClrADCCSPinMask;
volatile uint32_t *syncPinPtr;
uint32_t SetsyncPinPinMask;
uint32_t ClrsyncPinPinMask;
volatile uint32_t *ldacPinPtr;
uint32_t SetldacPinPinMask;
uint32_t ClrldacPinPinMask;
volatile uint32_t *ADCBusyPtr;
uint32_t ADCBusyPinMask;
#endif
analogShield();
unsigned int read(int channel, bool mode = false);
int signedRead(int channel, bool mode = false);
void write(int channel, unsigned int value); //any one channel
void write(unsigned int value0, unsigned int value1, bool simul); //channels 0 and 1
void write(unsigned int value0, unsigned int value1, unsigned int value2, bool simul); //channels 0-3
void write(unsigned int value0, unsigned int value1, unsigned int value2, unsigned int value3, bool simul); //all channels
static void begin(); // Default
static void end();
};
extern analogShield analog;
#endif