Skip to content

Commit ff21347

Browse files
committed
Re-apply the Watchdog-aware fixes (aka VERILITE_WDT_MODS) after the merging the 1.6 changes from the upstream Arduino repository
During the previous merge we wad to resolve the merge conflicts by temporarily removing the following files that were modified to be "watchdog aware": libraries/WiFi/WiFiClient.cpp libraries/SD/utility/Sd2Card.cpp libraries/GSM/GSM3SoftSerial.cpp libraries/Ethernet/EthernetClient.cpp libraries/Ethernet/Dhcp.cpp hardware/arduino/cores/arduino/wiring.c hardware/arduino/cores/arduino/Arduino.h The conflicts occurred because version 1.6 of the upstream repo uses a new folder structure for the sources. This commit re-applies this fixes to the merged files in the new tree structure.
1 parent e7e2de2 commit ff21347

File tree

7 files changed

+124
-7
lines changed

7 files changed

+124
-7
lines changed

hardware/arduino/avr/cores/arduino/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
extern "C"{
3636
#endif
3737

38+
#define VERILITE_WDT_MODS
39+
3840
void yield(void);
3941

4042
#define HIGH 0x1

hardware/arduino/avr/cores/arduino/wiring.c

+29
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
#include "wiring_private.h"
2626

27+
#ifdef VERILITE_WDT_MODS
28+
#include <avr/wdt.h>
29+
#endif
30+
2731
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
2832
// the overflow handler is called every 256 ticks.
2933
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
@@ -106,6 +110,29 @@ unsigned long micros() {
106110
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
107111
}
108112

113+
#ifdef VERILITE_WDT_MODS
114+
void wdt_delay_internal(unsigned long ms)
115+
{
116+
uint16_t start = (uint16_t)micros();
117+
118+
while (ms > 0) {
119+
yield();
120+
if (((uint16_t)micros() - start) >= 1000) {
121+
ms--;
122+
start += 1000;
123+
}
124+
}
125+
}
126+
void delay(unsigned long ms) {
127+
unsigned long i;
128+
for (i=0; i < (ms/100); i++) {
129+
wdt_reset();
130+
wdt_delay_internal(100);
131+
}
132+
wdt_reset();
133+
wdt_delay_internal(ms % 100);
134+
}
135+
#else
109136
void delay(unsigned long ms)
110137
{
111138
uint16_t start = (uint16_t)micros();
@@ -118,6 +145,8 @@ void delay(unsigned long ms)
118145
}
119146
}
120147
}
148+
#endif
149+
121150

122151
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
123152
void delayMicroseconds(unsigned int us)

libraries/Ethernet/src/Dhcp.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include "Arduino.h"
1010
#include "utility/util.h"
1111

12+
#ifdef VERILITE_WDT_MODS
13+
// watchdog
14+
#include <avr/wdt.h>
15+
#endif
16+
1217
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
1318
{
1419
_dhcpLeaseTime=0;
@@ -35,6 +40,9 @@ void DhcpClass::reset_DHCP_lease(){
3540
//return:0 on error, 1 if request is sent and response is received
3641
int DhcpClass::request_DHCP_lease(){
3742

43+
#ifdef VERILITE_WDT_MODS
44+
wdt_reset (); // watchdog reset
45+
#endif
3846
uint8_t messageType = 0;
3947

4048

@@ -58,6 +66,9 @@ int DhcpClass::request_DHCP_lease(){
5866

5967
while(_dhcp_state != STATE_DHCP_LEASED)
6068
{
69+
#ifdef VERILITE_WDT_MODS
70+
wdt_reset (); // watchdog reset
71+
#endif
6172
if(_dhcp_state == STATE_DHCP_START)
6273
{
6374
_dhcpTransactionId++;
@@ -259,6 +270,9 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
259270

260271
while(_dhcpUdpSocket.parsePacket() <= 0)
261272
{
273+
#ifdef VERILITE_WDT_MODS
274+
wdt_reset (); // watchdog reset
275+
#endif
262276
if((millis() - startTime) > responseTimeout)
263277
{
264278
return 255;
@@ -291,7 +305,10 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
291305

292306
while (_dhcpUdpSocket.available() > 0)
293307
{
294-
switch (_dhcpUdpSocket.read())
308+
#ifdef VERILITE_WDT_MODS
309+
wdt_reset (); // watchdog reset
310+
#endif
311+
switch (_dhcpUdpSocket.read())
295312
{
296313
case endOption :
297314
break;

libraries/Ethernet/src/EthernetClient.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ extern "C" {
1212
#include "EthernetServer.h"
1313
#include "Dns.h"
1414

15+
#ifdef VERILITE_WDT_MODS
16+
// watchdog
17+
#include <avr/wdt.h>
18+
#endif
19+
1520
uint16_t EthernetClient::_srcport = 49152; //Use IANA recommended ephemeral port range 49152-65535
1621

1722
EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) {
@@ -61,6 +66,9 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) {
6166

6267
while (status() != SnSR::ESTABLISHED) {
6368
delay(1);
69+
#ifdef VERILITE_WDT_MODS
70+
wdt_reset(); // watchdog reset
71+
#endif
6472
if (status() == SnSR::CLOSED) {
6573
_sock = MAX_SOCK_NUM;
6674
return 0;
@@ -121,6 +129,9 @@ int EthernetClient::peek() {
121129

122130
void EthernetClient::flush() {
123131
::flush(_sock);
132+
//#ifdef VERILITE_WDT_MODS
133+
// wdt_reset(); // watchdog reset
134+
//#endif
124135
}
125136

126137
void EthernetClient::stop() {
@@ -132,8 +143,12 @@ void EthernetClient::stop() {
132143
unsigned long start = millis();
133144

134145
// wait a second for the connection to close
135-
while (status() != SnSR::CLOSED && millis() - start < 1000)
136-
delay(1);
146+
while (status() != SnSR::CLOSED && millis() - start < 1000) {
147+
delay(1);
148+
#ifdef VERILITE_WDT_MODS
149+
wdt_reset(); // watchdog reset
150+
#endif
151+
}
137152

138153
// if it hasn't closed, close it forcefully
139154
if (status() != SnSR::CLOSED)
@@ -152,6 +167,9 @@ uint8_t EthernetClient::connected() {
152167
}
153168

154169
uint8_t EthernetClient::status() {
170+
#ifdef VERILITE_WDT_MODS
171+
wdt_reset (); // watchdog reset
172+
#endif
155173
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
156174
return socketStatus(_sock);
157175
}

libraries/Ethernet/src/utility/socket.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
static uint16_t local_port;
55

6+
#ifdef VERILITE_WDT_MODS
7+
// watchdog
8+
#include <avr/wdt.h>
9+
#endif
10+
611
/**
712
* @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
813
* @return 1 for success else 0.
@@ -136,8 +141,11 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
136141
ret = 0;
137142
break;
138143
}
144+
#ifdef VERILITE_WDT_MODS
145+
wdt_reset (); // watchdog reset
146+
#endif
139147
yield();
140-
}
148+
}
141149
while (freesize < ret);
142150

143151
// copy data
@@ -156,6 +164,9 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
156164
return 0;
157165
}
158166
SPI.endTransaction();
167+
#ifdef VERILITE_WDT_MODS
168+
wdt_reset (); // watchdog reset
169+
#endif
159170
yield();
160171
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
161172
}
@@ -273,6 +284,9 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
273284
return 0;
274285
}
275286
SPI.endTransaction();
287+
#ifdef VERILITE_WDT_MODS
288+
wdt_reset (); // watchdog reset
289+
#endif
276290
yield();
277291
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
278292
}
@@ -364,6 +378,9 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
364378
*/
365379
void flush(SOCKET s) {
366380
// TODO
381+
//#ifdef VERILITE_WDT_MODS
382+
// wdt_reset(); // watchdog reset
383+
//#endif
367384
}
368385

369386
uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
@@ -393,6 +410,9 @@ uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
393410
return 0;
394411
}
395412
SPI.endTransaction();
413+
#ifdef VERILITE_WDT_MODS
414+
wdt_reset (); // watchdog reset
415+
#endif
396416
yield();
397417
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
398418
}
@@ -455,6 +475,9 @@ int sendUDP(SOCKET s)
455475
return 0;
456476
}
457477
SPI.endTransaction();
478+
#ifdef VERILITE_WDT_MODS
479+
wdt_reset (); // watchdog reset
480+
#endif
458481
yield();
459482
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
460483
}

libraries/SD/src/utility/Sd2Card.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#define USE_SPI_LIB
2121
#include <Arduino.h>
2222
#include "Sd2Card.h"
23+
24+
#ifdef VERILITE_WDT_MODS
25+
// watchdog
26+
#include <avr/wdt.h>
27+
#endif
28+
2329
//------------------------------------------------------------------------------
2430
#ifndef SOFTWARE_SPI
2531
#ifdef USE_SPI_LIB
@@ -566,6 +572,9 @@ uint8_t Sd2Card::waitStartBlock(void) {
566572
* the value zero, false, is returned for failure.
567573
*/
568574
uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
575+
#ifdef VERILITE_WDT_MODS
576+
wdt_reset(); // watchdog reset
577+
#endif
569578
#if SD_PROTECT_BLOCK_ZERO
570579
// don't allow write to first block
571580
if (blockNumber == 0) {

libraries/WiFi/src/WiFiClient.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ extern "C" {
2929
#include "WiFiServer.h"
3030
#include "utility/server_drv.h"
3131

32+
#ifdef VERILITE_WDT_MODS
33+
// watchdog
34+
#include <avr/wdt.h>
35+
#endif
3236

3337
uint16_t WiFiClient::_srcport = 1024;
3438

@@ -57,8 +61,12 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
5761
unsigned long start = millis();
5862

5963
// wait 4 second for the connection to close
60-
while (!connected() && millis() - start < 10000)
64+
while (!connected() && millis() - start < 10000) {
6165
delay(1);
66+
#ifdef VERILITE_WDT_MODS
67+
wdt_reset(); // watchdog reset
68+
#endif
69+
}
6270

6371
if (!connected())
6472
{
@@ -140,8 +148,12 @@ int WiFiClient::peek() {
140148
}
141149

142150
void WiFiClient::flush() {
143-
while (available())
151+
while (available()) {
152+
#ifdef VERILITE_WDT_MODS
153+
wdt_reset(); // watchdog reset
154+
#endif
144155
read();
156+
}
145157
}
146158

147159
void WiFiClient::stop() {
@@ -154,8 +166,12 @@ void WiFiClient::stop() {
154166

155167
int count = 0;
156168
// wait maximum 5 secs for the connection to close
157-
while (status() != CLOSED && ++count < 50)
169+
while (status() != CLOSED && ++count < 50) {
158170
delay(100);
171+
#ifdef VERILITE_WDT_MODS
172+
wdt_reset(); // watchdog reset
173+
#endif
174+
}
159175

160176
_sock = 255;
161177
}
@@ -178,6 +194,9 @@ uint8_t WiFiClient::status() {
178194
if (_sock == 255) {
179195
return CLOSED;
180196
} else {
197+
#ifdef VERILITE_WDT_MODS
198+
wdt_reset(); // watchdog reset
199+
#endif
181200
return ServerDrv::getClientState(_sock);
182201
}
183202
}

0 commit comments

Comments
 (0)