Skip to content

Commit 4bf2869

Browse files
committed
ESP8266AVRISP: allow setting SPI freq and reset, postpone SPI init
1 parent d5ab22f commit 4bf2869

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

libraries/ESP8266AVRISP/src/ESP8266AVRISP.cpp

+26-35
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,28 @@ extern "C" {
4747

4848
#define beget16(addr) (*addr * 256 + *(addr+1))
4949

50-
ESP8266AVRISP::ESP8266AVRISP(uint16_t port, uint8_t reset_pin, uint32_t spi_freq):
51-
_reset_pin(reset_pin), _server(WiFiServer(port)), _state(AVRISP_STATE_IDLE),
52-
_spi_freq(spi_freq)
50+
ESP8266AVRISP::ESP8266AVRISP(uint16_t port, uint8_t reset_pin, uint32_t spi_freq, bool reset_state):
51+
_reset_pin(reset_pin), _reset_state(reset_state), _spi_freq(spi_freq),
52+
_server(WiFiServer(port)), _state(AVRISP_STATE_IDLE)
5353
{
54+
pinMode(_reset_pin, OUTPUT);
55+
setReset(_reset_state);
5456
}
5557

5658
void ESP8266AVRISP::begin() {
57-
pinMode(_reset_pin, OUTPUT);
58-
digitalWrite(_reset_pin, AVRISP_RESET_OFF);
5959
_server.begin();
6060
}
6161

62+
void ESP8266AVRISP::setSpiFrequency(uint32_t freq) {
63+
_spi_freq = freq;
64+
if (_state == AVRISP_STATE_ACTIVE) {
65+
SPI.setFrequency(freq);
66+
}
67+
}
68+
6269
void ESP8266AVRISP::setReset(bool rst) {
63-
if (rst) {
70+
_reset_state = rst;
71+
if (_reset_state) {
6472
digitalWrite(_reset_pin, AVRISP_RESET_ON);
6573
} else {
6674
digitalWrite(_reset_pin, AVRISP_RESET_OFF);
@@ -88,9 +96,14 @@ AVRISPState_t ESP8266AVRISP::update() {
8896
if (!_client.connected()) {
8997
_client.stop();
9098
AVRISP_DEBUG("client disconnect");
91-
SPI.end();
92-
digitalWrite(_reset_pin, AVRISP_RESET_OFF);
99+
if (pmode) {
100+
SPI.end();
101+
pmode = 0;
102+
}
103+
setReset(_reset_state);
93104
_state = AVRISP_STATE_IDLE;
105+
} else {
106+
_reject_incoming();
94107
}
95108
break;
96109
}
@@ -104,11 +117,6 @@ AVRISPState_t ESP8266AVRISP::serve() {
104117
// should not be called when idle, error?
105118
break;
106119
case AVRISP_STATE_PENDING: {
107-
// enter reset, setup SPI
108-
SPI.begin();
109-
SPI.setFrequency(_spi_freq);
110-
SPI.setHwCs(false);
111-
digitalWrite(_reset_pin, AVRISP_RESET_ON);
112120
_state = AVRISP_STATE_ACTIVE;
113121
// fallthrough
114122
}
@@ -213,31 +221,14 @@ void ESP8266AVRISP::set_parameters() {
213221
+ buff[17] * 0x00010000
214222
+ buff[18] * 0x00000100
215223
+ buff[19];
216-
217-
// AVRISP_DEBUG("devicecode = %d", param.devicecode);
218-
// AVRISP_DEBUG("revision = %d", param.revision);
219-
// AVRISP_DEBUG("progtype = %d", param.progtype);
220-
// AVRISP_DEBUG("parmode = %d", param.parmode);
221-
// AVRISP_DEBUG("polling = %d", param.polling);
222-
// AVRISP_DEBUG("selftimed = %d", param.selftimed);
223-
// AVRISP_DEBUG("lockbytes = %d", param.lockbytes);
224-
// AVRISP_DEBUG("fusebytes = %d", param.fusebytes);
225-
// AVRISP_DEBUG("flashpoll = %d", param.flashpoll);
226-
// AVRISP_DEBUG("eeprompoll = %d", param.eeprompoll);
227-
// AVRISP_DEBUG("pagesize = %d", param.pagesize);
228-
// AVRISP_DEBUG("eepromsize = %d", param.eepromsize);
229-
230-
// AVRISP_DEBUG("flashsize = %d", param.flashsize);
231-
232224
}
233225

234226
void ESP8266AVRISP::start_pmode() {
227+
SPI.begin();
228+
SPI.setFrequency(_spi_freq);
229+
SPI.setHwCs(false);
235230

236-
// SPI already begun when entering ACTIVE state
237-
//SPI.begin();
238-
//SPI.setFrequency(AVRISP_SPI_FREQ);
239-
240-
// following delays may not work on all targets...
231+
// try to sync the bus
241232
SPI.transfer(0x00);
242233
digitalWrite(_reset_pin, AVRISP_RESET_OFF);
243234
delayMicroseconds(50);
@@ -250,7 +241,7 @@ void ESP8266AVRISP::start_pmode() {
250241

251242
void ESP8266AVRISP::end_pmode() {
252243
SPI.end();
253-
digitalWrite(_reset_pin, AVRISP_RESET_OFF);
244+
setReset(_reset_state);
254245
pmode = 0;
255246
}
256247

libraries/ESP8266AVRISP/src/ESP8266AVRISP.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Original version:
1818
// #define AVRISP_ACTIVE_HIGH_RESET
1919

2020
// SPI clock frequency in Hz
21-
#define AVRISP_SPI_FREQ 1e6
21+
#define AVRISP_SPI_FREQ 300e3
2222

2323
// programmer states
2424
typedef enum {
@@ -47,10 +47,13 @@ typedef struct {
4747

4848
class ESP8266AVRISP {
4949
public:
50-
ESP8266AVRISP(uint16_t port, uint8_t reset_pin, uint32_t spi_freq=AVRISP_SPI_FREQ);
50+
ESP8266AVRISP(uint16_t port, uint8_t reset_pin, uint32_t spi_freq=AVRISP_SPI_FREQ, bool reset_state=false);
5151

5252
void begin();
5353

54+
// set the SPI clock frequency
55+
void setSpiFrequency(uint32_t);
56+
5457
// control the state of the RESET pin of the target
5558
// see AVRISP_ACTIVE_HIGH_RESET
5659
void setReset(bool);
@@ -104,6 +107,7 @@ class ESP8266AVRISP {
104107
WiFiClient _client;
105108
AVRISPState_t _state;
106109
uint8_t _reset_pin;
110+
bool _reset_state;
107111

108112
// programmer settings, set by remote end
109113
AVRISP_parameter_t param;

0 commit comments

Comments
 (0)