lolxnano
Code to control the LoLX SiPM biasing board from the connected nanoPi
Loading...
Searching...
No Matches
nanopi.h
1#ifndef NANOPI_H
2#define NANOPI_H
3
4#define ADDR0 7 // gpio 203
5#define ADDR1 1 // gpio 6
6#define ADDR2 15 // gpio 198
7#define MOSI 12 // gpio 64
8#define MISO 13 // gpio 65
9#define CLK 14 // gpio 66
10#define HVADCCS 4 // gpio 200
11#define HVDACCS 5 // gpio 201
12//#define ENHV 16 // gpio 199
13#define RSTHV 0 // gpio 0
14#define ENQP 2 // gpio 2
15
16#define START 6
17
18#define SPICHAN 0
19#define SPISPEED 1000000
20#define SPIMODE 2
21
22#define DAC_ADDR12 5
23#define DAC_ADDR34 2
24#define RST_ALL 7
25
26#define ADC_ADDR1 6
27#define ADC_ADDR2 4
28#define ADC_ADDR3 3
29#define ADC_ADDR4 1
30
31#include <wiringPi.h>
32#include <wiringPiSPI.h>
33#include <iostream>
34#include <cstdint>
35#include <unistd.h>
36#ifdef DEBUG
37static const bool gverbose = true;
38
39#else
40static const bool gverbose = false;
41#endif
42
43
49class NanoPi
50{
51 public:
52 bool InitGPIO();
53 bool InitSPI();
54 void AddrOff();
55 void SetAddr(short addr);
56
58 bool HasSPI(){ return(fdspi >= 0); }
60 bool HasGPIO(){ return(fdgpio >= 0); }
69 void SendWord(short cselect, unsigned char *buf, uint8_t nbytes);
71 uint8_t Exch8(short cselect, uint8_t data){ SendWord(cselect, (unsigned char*)&data, 1); return data; }
73 uint16_t Exch16(short cselect, uint16_t data){ SendWord(cselect, (unsigned char*)&data, 2); return data; }
75 uint32_t Exch24(short cselect, uint32_t data){ SendWord(cselect, (unsigned char*)&data, 3); return data; }
77 uint32_t Exch32(short cselect, uint32_t data){ SendWord(cselect, (unsigned char*)&data, 4); return data; }
79 void ResetAll(){ SetAddr(RST_ALL); ResetHV(); AddrOff(); }
81 void ResetHV(){ digitalWrite(RSTHV, LOW); usleep(100); digitalWrite(RSTHV, HIGH); }
83 void EnableQP(bool on = true){ EnQP( on?HIGH:LOW );}
85 void StartADC(){ digitalWrite(START, HIGH);}
87 void StopADC(){ digitalWrite(START, LOW);}
89 static uint32_t SpiConvert(const uint32_t &input, unsigned short nbytes = 4);
90
91 private:
92 void EnQP(int level = HIGH){ digitalWrite(ENQP, level);}
93 int fdspi = 0;
94 int fdgpio = 0;
95 const char addrbit[3] = { ADDR0, ADDR1, ADDR2 };
96};
97
98#endif
99
100
101
102/* emacs
103 * Local Variables:
104 * tab-width: 8
105 * c-basic-offset: 3
106 * indent-tabs-mode: nil
107 * End:
108 */
Class to control GPIO and SPI communication from a nanoPi Neo2.
Definition nanopi.h:50
bool HasGPIO()
Check whether GPIO was correctly initialized.
Definition nanopi.h:60
void SendWord(short cselect, unsigned char *buf, uint8_t nbytes)
Send data word via SPI.
Definition nanopi.cc:82
void StopADC()
Set ADC start pin LOW.
Definition nanopi.h:87
void ResetHV()
Set reset pin on HV ADC and DAC.
Definition nanopi.h:81
void StartADC()
Set ADC start pin HIGH.
Definition nanopi.h:85
bool InitSPI()
Initialize SPI communication, not specific to LoLX.
Definition nanopi.cc:36
uint32_t Exch24(short cselect, uint32_t data)
Exchange 3 bytes.
Definition nanopi.h:75
uint8_t Exch8(short cselect, uint8_t data)
Exchange 1 byte.
Definition nanopi.h:71
bool HasSPI()
Check whether SPI was correctly initialized.
Definition nanopi.h:58
void SetAddr(short addr)
Select SPI device via the SPI channel select encoder.
Definition nanopi.cc:62
static uint32_t SpiConvert(const uint32_t &input, unsigned short nbytes=4)
Shuffle bytes around for correct SPI order and back.
Definition nanopi.cc:93
void EnableQP(bool on=true)
Enable/disable charge pump.
Definition nanopi.h:83
uint16_t Exch16(short cselect, uint16_t data)
Exchange 2 bytes.
Definition nanopi.h:73
uint32_t Exch32(short cselect, uint32_t data)
Exchange 4 bytes.
Definition nanopi.h:77
bool InitGPIO()
Initialize GPIO communication for LoLX-specific pins.
Definition nanopi.cc:10
void AddrOff()
Set the SPI channel select encoder to zero, i.e. select no SPI device.
Definition nanopi.cc:54
void ResetAll()
Set reset pin on all SPI devices.
Definition nanopi.h:79