351 lines
No EOL
9.2 KiB
C++
351 lines
No EOL
9.2 KiB
C++
/*
|
|
Cosmic Pi V1.6 Calibration script
|
|
Run this to set the EEPROM values in your Cosmic Pi, before flashing the operational software
|
|
|
|
Notes to user: If you set the thresholds/HV wrong and it goes crazy with interrupts, just re-flash and the values will
|
|
be reset before the interrupt is attached. Problem solved.
|
|
We write the values in the EEPROM at two places, one will be used for user manipulation, the other will remain there
|
|
for a 'hard reset'
|
|
This won't work unless you've got the Cosmic Pi board type and STM32 core for Arduino installed.
|
|
|
|
Features:
|
|
Working:
|
|
Set HVs
|
|
Set Thresholds
|
|
Print out events with integer
|
|
Store values in EEPROM.
|
|
|
|
To be added:
|
|
Instant rate meter to be used for getting a better idea of the rate.
|
|
A way to rate limit/lower thresholds if you crash it.
|
|
|
|
Licensed under GPL V3 or later
|
|
|
|
|
|
*/
|
|
|
|
//drive the dac channels
|
|
//set values into eeprom
|
|
#include <EEPROM.h>
|
|
|
|
/* EEProm mapping table
|
|
*EEPROM Status - byte 00
|
|
*main values
|
|
*channel 0 high byte - 01
|
|
*channel 0 low byte - 02
|
|
*channel 1 high byte - 03
|
|
*channel 1 low byte - 04
|
|
*HV bias ch 0 value - 05
|
|
*HV bias ch 1 value - 06
|
|
*
|
|
*reserve values
|
|
*channel 0 high byte - 01
|
|
*channel 0 low byte - 02
|
|
*channel 1 high byte - 03
|
|
*channel 1 low byte - 04
|
|
*HV bias ch 0 value - 05
|
|
*HV bias ch 1 value - 06
|
|
*/
|
|
|
|
//init variables
|
|
int Status = 0; //status for eeprom reads
|
|
int Data = 0; //data in/out of eeprom
|
|
// the setup function runs once when you press reset or power the board
|
|
byte smallpart1; //lsb for DAC settings
|
|
byte bigpart1; //msb for DAC settings
|
|
byte smallpart2; //lsb for DAC settings
|
|
byte bigpart2; //msb for DAC settings
|
|
int HVVal1 = 0xFE; //hv transmission value
|
|
int HVVal2 = 0xFE; //hv transmission value
|
|
|
|
|
|
//run i2c devices
|
|
#include <Wire.h>
|
|
|
|
|
|
unsigned long event_count = 0;
|
|
|
|
|
|
void printTimeAndPin(){
|
|
// print when and which pin was interrupted
|
|
event_count++;
|
|
//Serial.print(name);
|
|
Serial.print("count=");
|
|
Serial.println(event_count);
|
|
//Serial.print(";time=");
|
|
//timemeasure=millis();
|
|
//Serial.println(timemeasure-timeoffset);
|
|
}
|
|
|
|
|
|
void setup() {
|
|
|
|
pinMode(PA6, OUTPUT);
|
|
pinMode(PA4, OUTPUT);
|
|
pinMode(PA5, OUTPUT);
|
|
Serial.begin(9600);
|
|
|
|
|
|
//config SPI pins
|
|
pinMode(PC7, OUTPUT);
|
|
pinMode(PC8, OUTPUT);
|
|
pinMode(PC3, OUTPUT);
|
|
pinMode(PB13, OUTPUT);
|
|
|
|
//set input pins
|
|
pinMode(PC12, INPUT);
|
|
pinMode(PC11, INPUT);
|
|
pinMode(PB10, INPUT);
|
|
|
|
//set HV channel 1
|
|
digitalWrite(PC7, LOW);
|
|
setHV(0xAC);
|
|
digitalWrite(PC7, HIGH);
|
|
//set HV channel 2
|
|
digitalWrite(PC8, LOW);
|
|
setHV(0xAC);
|
|
digitalWrite(PC8, HIGH);
|
|
|
|
//set DACS
|
|
Wire.setSDA(PB7);
|
|
Wire.setSCL(PB8);
|
|
Wire.begin();
|
|
//initial DAC settings (both channels)
|
|
smallpart1 = 0x2F; //the lsb part
|
|
bigpart1 = 0x02; // the msb part of the dac value
|
|
smallpart2 = 0x2F; //the lsb part
|
|
bigpart2 = 0x02; // the msb part of the dac value
|
|
|
|
int address = 0x60;
|
|
Wire.beginTransmission(address);
|
|
Wire.write(B00001000); // sends five bytes
|
|
Wire.write(bigpart1); // sends one byte
|
|
Wire.write(smallpart1);
|
|
Wire.endTransmission();
|
|
|
|
Wire.beginTransmission(address);
|
|
Wire.write(B00000000); // sends five bytes
|
|
Wire.write(bigpart2); // sends one byte
|
|
Wire.write(smallpart2);
|
|
Wire.endTransmission();
|
|
|
|
attachInterrupt(digitalPinToInterrupt(PB10), printTimeAndPin, RISING);
|
|
|
|
|
|
}
|
|
|
|
void loop() {
|
|
Serial.println("Input a command!");
|
|
Serial.println("[1= set both thresholds; 2= set Ch1 threshold, 3= set Ch2 threshold, 4= set both HV thresholds, 5= set Ch1 HV threshold, 6= set Ch2 HV threshold, 7= set values in EEPROM]");
|
|
int cmd = readIntFromSerial();
|
|
switch(cmd){
|
|
case 1:
|
|
{
|
|
Serial.println("Set a threshold value [1,1024]: ");
|
|
int value = readIntFromSerial();
|
|
smallpart1 = byte(value);
|
|
bigpart1 = byte(value>>8);
|
|
smallpart2 = byte(value);
|
|
bigpart2 = byte(value>>8);
|
|
|
|
int address = 0x60;
|
|
//channel 1
|
|
Wire.beginTransmission(address);
|
|
Wire.write(B00001000); // sends five bytes
|
|
Wire.write(bigpart1); // sends one byte
|
|
Wire.write(smallpart1);
|
|
Wire.endTransmission();
|
|
//channel 2
|
|
Wire.beginTransmission(address);
|
|
Wire.write(B00000000); // sends five bytes
|
|
Wire.write(bigpart2); // sends one byte
|
|
Wire.write(smallpart2);
|
|
Wire.endTransmission();
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
Serial.println("Set a threshold value [1,1024]: ");
|
|
int value = readIntFromSerial();
|
|
smallpart1 = byte(value);
|
|
bigpart1 = byte(value>>8);
|
|
|
|
int address = 0x60;
|
|
//channel 1
|
|
Wire.beginTransmission(address);
|
|
Wire.write(B00001000); // sends five bytes
|
|
Wire.write(bigpart1); // sends one byte
|
|
Wire.write(smallpart1);
|
|
Wire.endTransmission();
|
|
break;
|
|
}
|
|
case 3:
|
|
{
|
|
Serial.println("Set a threshold value [1,1024]: ");
|
|
int value = readIntFromSerial();
|
|
smallpart2 = byte(value);
|
|
bigpart2 = byte(value>>8);
|
|
|
|
int address = 0x60;
|
|
//channel 2
|
|
Wire.beginTransmission(address);
|
|
Wire.write(B00000000); // sends five bytes
|
|
Wire.write(bigpart2); // sends one byte
|
|
Wire.write(smallpart2);
|
|
Wire.endTransmission();
|
|
break;
|
|
}
|
|
case 4:
|
|
{
|
|
Serial.println("Set an HV value [0,255]: ");
|
|
HVVal1 = readIntFromSerial();
|
|
HVVal2 = HVVal1;
|
|
//set HV channel 1
|
|
digitalWrite(PC7, LOW);
|
|
setHV(byte(HVVal1));
|
|
digitalWrite(PC7, HIGH);
|
|
//set HV channel 2
|
|
digitalWrite(PC8, LOW);
|
|
setHV(byte(HVVal2));
|
|
digitalWrite(PC8, HIGH);
|
|
break;
|
|
}
|
|
case 5:
|
|
{
|
|
Serial.println("Set an HV value [0,255]: ");
|
|
HVVal1 = readIntFromSerial();
|
|
//set HV channel 1
|
|
digitalWrite(PC7, LOW);
|
|
setHV(byte(HVVal1));
|
|
digitalWrite(PC7, HIGH);
|
|
break;
|
|
}
|
|
case 6:
|
|
{
|
|
Serial.println("Set an HV value [0,255]: ");
|
|
HVVal2 = readIntFromSerial();
|
|
//set HV channel 2
|
|
digitalWrite(PC8, LOW);
|
|
setHV(byte(HVVal2));
|
|
digitalWrite(PC8, HIGH);
|
|
break;
|
|
}
|
|
|
|
case 7:
|
|
{
|
|
//eeprom write
|
|
EEPROM.write(0x01, bigpart1);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x01, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(bigpart1, HEX);
|
|
EEPROM.write(0x02, smallpart1);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x02, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(smallpart1, HEX);
|
|
EEPROM.write(0x03, bigpart2);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x03, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(bigpart2, HEX);
|
|
EEPROM.write(0x04, smallpart2);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x04, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(smallpart2, HEX);
|
|
EEPROM.write(0x05, byte(HVVal1));
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x05, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(HVVal1, HEX);
|
|
EEPROM.write(0x06, byte(HVVal2));
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x06, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(HVVal2, HEX);
|
|
EEPROM.write(0x00, 0x01); //set the status bit, have 1 set of stored values
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x00, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(0x01, HEX);
|
|
|
|
//eeprom backup location
|
|
|
|
EEPROM.write(0x21, bigpart1);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x21, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(bigpart1, HEX);
|
|
EEPROM.write(0x22, smallpart1);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x22, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(smallpart1, HEX);
|
|
EEPROM.write(0x23, bigpart2);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x23, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(bigpart2, HEX);
|
|
EEPROM.write(0x24, smallpart2);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x24, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(smallpart2, HEX);
|
|
EEPROM.write(0x25, HVVal1);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x25, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(HVVal1, HEX);
|
|
EEPROM.write(0x26, HVVal2);
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x26, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(HVVal2, HEX);
|
|
EEPROM.write(0x00, 0x02); //set the status to show that backup positions have been written
|
|
Serial.print("EEPROM.write");
|
|
Serial.print(0x00, HEX);
|
|
Serial.print(" ");
|
|
Serial.println(0x02, HEX);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this function sets the thresholds for the MAX5387
|
|
// 1 is the first channel, 2 the second and 3 sets both at the same time
|
|
|
|
byte setHV(byte _send) // This function is what bitbangs the data
|
|
{
|
|
if (_send > 0x6F){
|
|
for(int i=0; i<8; i++) // There are 8 bits in a byte
|
|
{
|
|
digitalWrite(PC3, bitRead(_send, 7-i)); // Set MOSI
|
|
//delay(1);
|
|
digitalWrite(PB13, HIGH); // SCK high
|
|
//bitWrite(_receive, i, digitalRead(MISO_pin)); // Capture MISO
|
|
digitalWrite(PB13, LOW); // SCK low
|
|
//digitalWrite(MOSI_pin, LOW); // Set MOSI
|
|
|
|
}
|
|
//digitalWrite(SS_pin[j], HIGH); // SS high again
|
|
}
|
|
}
|
|
//return _receive; // Return the received data
|
|
|
|
|
|
int readIntFromSerial(){
|
|
int val = Serial.parseInt();
|
|
while (val == 0){
|
|
delay(100);
|
|
val = Serial.parseInt();
|
|
}
|
|
return val;
|
|
} |