Skip to content

Commit

Permalink
Refactor RDY signal, use ADC if available
Browse files Browse the repository at this point in the history
  • Loading branch information
microtherion committed Dec 31, 2015
1 parent 4b24b55 commit 4c7db61
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
10 changes: 5 additions & 5 deletions ScratchMonkey.avrsackproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>Board</key>
<string>uno</string>
<string>pro5v328</string>
<key>Files</key>
<dict>
<key>Children</key>
Expand Down Expand Up @@ -236,26 +236,26 @@
<key>Expanded</key>
<true/>
<key>Name</key>
<string>ScratchMonkey</string>
<string>ScratchMonkey-HWIF</string>
<key>Type</key>
<string>Project</string>
</dict>
<key>FontSize</key>
<integer>12</integer>
<key>Port</key>
<string>Repleo-CH341-00001014</string>
<string>usbserial-DA00EMMM</string>
<key>Programmer</key>
<string></string>
<key>RecentBoards</key>
<array>
<string>pro5v328</string>
<string>mega2560</string>
<string>uno</string>
<string>leonardo</string>
<string>mega2560</string>
<string>attiny84at1</string>
<string>attiny43uat8</string>
<string>attiny43uat1</string>
<string>atmega328at1</string>
<string>attiny85at8</string>
</array>
<key>RecentProgrammers</key>
<array>
Expand Down
5 changes: 2 additions & 3 deletions ScratchMonkey/SMoHVPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <SPI.h>

enum {
HVPP_RDY = SMoHWIF::HVPP::RDY,
HVPP_XTAL = SMoHWIF::HVPP::XTAL
};

Expand Down Expand Up @@ -173,9 +172,9 @@ HVPPReadData(uint8_t byteSel)
static bool
HVPPPollWait(uint8_t pollTimeout)
{
uint32_t target = millis()+pollTimeout+5;
uint32_t target = millis()+pollTimeout+10;
while (millis() != target)
if (digitalRead(HVPP_RDY))
if (SMoHWIF::HVPP::GetReady())
return true;
SMoCommand::SendResponse(STATUS_RDY_BSY_TOUT);
return false;
Expand Down
11 changes: 6 additions & 5 deletions ScratchMonkey/SMoHWIF_HVPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@
enum HVPP_RDY_PIN {};
enum HVPP_XTAL_PIN {};

template <typename HV_Platform, typename Control_Port, typename Data_Port,
HVPP_XTAL_PIN HVPP_XTAL = HVPP_XTAL_PIN(13), HVPP_RDY_PIN HVPP_RDY = HVPP_RDY_PIN(12)> class SMoHWIF_HVPP {
template <typename HV_Platform, typename Control_Port, typename Data_Port, typename Ready_Pin,
HVPP_XTAL_PIN HVPP_XTAL = HVPP_XTAL_PIN(13)> class SMoHWIF_HVPP {
private:
enum {
HVPP_RESET = HV_Platform::RESET,
HVPP_VCC = HV_Platform::VCC
};
public:
enum {
RDY = HVPP_RDY,
XTAL= HVPP_XTAL,
};
static void Setup(uint8_t initSignals, uint8_t powOffDelay, uint8_t latchCycles) {
pinMode(HVPP_VCC, OUTPUT);
digitalWrite(HVPP_VCC, LOW);
digitalWrite(HVPP_RESET, HIGH); // Set BEFORE pinMode, so we don't glitch LOW
pinMode(HVPP_RESET, OUTPUT);
pinMode(HVPP_RDY, INPUT);
digitalWrite(HVPP_RDY, LOW);
pinMode(HVPP_XTAL, OUTPUT);
digitalWrite(HVPP_XTAL, LOW);
DataMode(OUTPUT);
InitControlSignals();
Ready_Pin::Setup();
SetControlSignals(initSignals);

delay(powOffDelay);
Expand Down Expand Up @@ -79,6 +77,9 @@ template <typename HV_Platform, typename Control_Port, typename Data_Port,
static uint8_t GetData() {
return Data_Port::Get();
}
static bool GetReady() {
return Ready_Pin::Get();
}
};

#endif
6 changes: 4 additions & 2 deletions ScratchMonkey/SMoHWIF_Leonardo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ typedef SMoHWIF_HVSP<SMoHWIF_HV_Platform> SMoHWIF_HVSP_Platfor
const int SMoHWIF_PORT_D = 0x09;
const int SMoHWIF_PORT_F = 0x0F;
typedef SMoHWIF_Port_Dual<SMoHWIF_PORT_D, 0x0C, 0,
SMoHWIF_PORT_F, 0xF1, 0> SMoHWIF_HVPP_Control;
SMoHWIF_PORT_F, 0xF1, 0> SMoHWIF_HVPP_Control;
typedef SMoHWIF_Port_Slow<2> SMoHWIF_HVPP_Data;
typedef SMoHWIF_Input_Pin_Digital<12> SMoHWIF_HVPP_Ready;
typedef SMoHWIF_HVPP<SMoHWIF_HV_Platform,
SMoHWIF_HVPP_Control, SMoHWIF_HVPP_Data> SMoHWIF_HVPP_Platform;
SMoHWIF_HVPP_Control, SMoHWIF_HVPP_Data,
SMoHWIF_HVPP_Ready> SMoHWIF_HVPP_Platform;
typedef SMoHWIF_TPI<SMoHWIF_HV_Platform> SMoHWIF_TPI_Platform;

#endif /* _SMO_HWIF_LEONARDO_ */
Expand Down
8 changes: 5 additions & 3 deletions ScratchMonkey/SMoHWIF_Mega.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ typedef SMoHWIF_HVSP<SMoHWIF_HV_Platform> SMoHWIF_HVSP_Platfor
//
const int SMoHWIF_PORT_F = 0x0F;
const int SMoHWIF_PORT_K = 0xE6;
typedef SMoHWIF_Port_Simple<SMoHWIF_PORT_F> SMoHWIF_HVPP_Control;
typedef SMoHWIF_Port_Simple<SMoHWIF_PORT_K> SMoHWIF_HVPP_Data;
typedef SMoHWIF_Port_Simple<SMoHWIF_PORT_F> SMoHWIF_HVPP_Control;
typedef SMoHWIF_Port_Simple<SMoHWIF_PORT_K> SMoHWIF_HVPP_Data;
typedef SMoHWIF_Input_Pin_Digital<12> SMoHWIF_HVPP_Ready;
typedef SMoHWIF_HVPP<SMoHWIF_HV_Platform,
SMoHWIF_HVPP_Control, SMoHWIF_HVPP_Data> SMoHWIF_HVPP_Platform;
SMoHWIF_HVPP_Control, SMoHWIF_HVPP_Data,
SMoHWIF_HVPP_Ready> SMoHWIF_HVPP_Platform;
typedef SMoHWIF_TPI<SMoHWIF_HV_Platform> SMoHWIF_TPI_Platform;

#endif /* _SMO_HWIF_MEGA_ */
Expand Down
36 changes: 36 additions & 0 deletions ScratchMonkey/SMoHWIF_Port.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,40 @@ template <PORT_CS_PIN CS> class SMoHWIF_Port_SPI {
}
};

//
// Digital input pin
//
template <int PIN> class SMoHWIF_Input_Pin_Digital {
public:
static void Setup() {
pinMode(PIN, INPUT);
}
static void Stop() {
}
static bool Get() {
return digitalRead(PIN);
}
};

//
// Analog input pin, using the analog comparator. This is useful for
// the Arduino variants with 8 analog input, where ADC6 and ADC7 cannot
// be read as digital signals. Based on Leong Yu Siang's work.
//
template <int AIN> class SMoHWIF_Input_Pin_Analog {
static void Setup() {
ADCSRA = 0x00; // Turn off ADC
ADMUX = _BV(REFS1) | _BV(REFS0) | AIN; // 1.1V vs ADC pin
ADCSRB = _BV(ACME); // Use MUX as negative input
ACSR = _BV(ACBG); // Use 1.1V as positive input
delay(1); // Let 1.1V stabilize
}
static void Stop() {
ACSR = _BV(ACD); // Disable comparator
}
static bool Get() {
return !(ACSR & _BV(ACO)); // ACO -> input voltage < 1.1V
}
};

#endif
15 changes: 14 additions & 1 deletion ScratchMonkey/SMoHWIF_Standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ typedef SMoHWIF_Port_Dual<SMoHWIF_PORT_B, 0x03, 6,
SMoHWIF_PORT_C, 0x3F, 0> SMoHWIF_HVPP_Data;
typedef SMoHWIF_Port_Simple<SMoHWIF_PORT_D> SMoHWIF_HVPP_Control;

#if NUM_ANALOG_INPUTS==8
//
// Arduino Pro Mini et al, SMD MCU, 8 analog inputs
//
typedef SMoHWIF_Input_Pin_Analog<7> SMoHWIF_HVPP_Ready;
#else
//
// Arduino Uno, usually DIP MCU, 6 analog inputs
//
typedef SMoHWIF_Input_Pin_Digital<12> SMoHWIF_HVPP_Ready;
#endif

typedef SMoHWIF_HVPP<SMoHWIF_HV_Platform,
SMoHWIF_HVPP_Control, SMoHWIF_HVPP_Data> SMoHWIF_HVPP_Platform;
SMoHWIF_HVPP_Control, SMoHWIF_HVPP_Data,
SMoHWIF_HVPP_Ready> SMoHWIF_HVPP_Platform;

typedef SMoHWIF_TPI<SMoHWIF_HV_Platform> SMoHWIF_TPI_Platform;

Expand Down

0 comments on commit 4c7db61

Please sign in to comment.