Skip to content

Commit 9f14826

Browse files
authored
Merge pull request #322 from cristidragomir97/main
Fix Inconsistent Sleep Current by relocating the Ethernet Clock AGT Timer
2 parents 42aecbe + 447568c commit 9f14826

File tree

6 files changed

+119
-53
lines changed

6 files changed

+119
-53
lines changed

Diff for: libraries/Ethernet/src/Ethernet.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <EthernetC33.h>
2-
2+
#include <EthernetClock.h>
33
/*
44
* The old implementation of the begin set a default mac address:
55
* this does not make any sense.
@@ -12,10 +12,15 @@
1212
/* -------------------------------------------------------------------------- */
1313
int CEthernet::begin(unsigned long timeout, unsigned long responseTimeout) {
1414
/* -------------------------------------------------------------------------- */
15+
16+
ethernetTimer = new EthernetClock();
17+
ethernetTimer->start();
18+
delay(2);
1519
(void)responseTimeout;
1620

1721
int rv = 0;
1822

23+
1924
ni = CLwipIf::getInstance().get(NI_ETHERNET);
2025
if(ni != nullptr) {
2126
ni->DhcpSetTimeout(timeout);
@@ -56,6 +61,10 @@ int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway
5661
int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
5762
/* -------------------------------------------------------------------------- */
5863

64+
ethernetTimer = new EthernetClock();
65+
ethernetTimer->start();
66+
delay(2);
67+
5968
if (ni != nullptr) {
6069
ni->config(local_ip, gateway, subnet);
6170
} else {
@@ -136,6 +145,9 @@ EthernetHardwareStatus CEthernet::hardwareStatus() {
136145
/* -------------------------------------------------------------------------- */
137146
int CEthernet::disconnect() {
138147
/* -------------------------------------------------------------------------- */
148+
ethernetTimer->stop();
149+
delete(ethernetTimer);
150+
ethernetTimer = NULL;
139151
return 1;
140152
}
141153

Diff for: libraries/Ethernet/src/EthernetC33.h

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "EthernetClient.h"
1515
#include "EthernetServer.h"
16+
#include "EthernetClock.h"
1617

1718
#include "CNetIf.h"
1819
#include "lwipMem.h"
@@ -68,8 +69,11 @@ class CEthernet {
6869
IPAddress gatewayIP();
6970
IPAddress dnsServerIP();
7071

72+
7173
friend class EthernetClient;
7274
friend class EthernetServer;
75+
private:
76+
EthernetClock * ethernetTimer;
7377
};
7478

7579
extern CEthernet Ethernet;

Diff for: libraries/Ethernet/src/EthernetClock.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "EthernetClock.h"
2+
#include "pins_arduino.h"
3+
4+
#if defined(ETHERNET_CLK_PIN)
5+
6+
EthernetClock::EthernetClock() {
7+
pinPeripheral(ETHERNET_CLK_PIN, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_AGT));
8+
9+
this->TIMER_ETHERNET_extend.count_source = AGT_CLOCK_PCLKB;
10+
this->TIMER_ETHERNET_extend.agto = AGT_PIN_CFG_START_LEVEL_LOW;
11+
this->TIMER_ETHERNET_extend.agtoab_settings_b.agtoa = AGT_PIN_CFG_DISABLED;
12+
this->TIMER_ETHERNET_extend.agtoab_settings_b.agtob = AGT_PIN_CFG_DISABLED;
13+
this->TIMER_ETHERNET_extend.measurement_mode = AGT_MEASURE_DISABLED;
14+
this->TIMER_ETHERNET_extend.agtio_filter = AGT_AGTIO_FILTER_NONE;
15+
this->TIMER_ETHERNET_extend.enable_pin = AGT_ENABLE_PIN_NOT_USED;
16+
this->TIMER_ETHERNET_extend.trigger_edge = AGT_TRIGGER_EDGE_RISING;
17+
18+
this->TIMER_ETHERNET_cfg.mode = TIMER_MODE_PERIODIC;
19+
this->TIMER_ETHERNET_cfg.period_counts = (uint32_t) 0x1;
20+
this->TIMER_ETHERNET_cfg.duty_cycle_counts = 0x00;
21+
this->TIMER_ETHERNET_cfg.source_div = (timer_source_div_t) 0;
22+
this->TIMER_ETHERNET_cfg.channel = ETHERNET_AGT_TIMER_CHANNEL;
23+
this->TIMER_ETHERNET_cfg.p_callback = NULL;
24+
this->TIMER_ETHERNET_cfg.p_context = NULL;
25+
this->TIMER_ETHERNET_cfg.p_extend = &TIMER_ETHERNET_extend;
26+
this->TIMER_ETHERNET_cfg.cycle_end_ipl = (BSP_IRQ_DISABLED);
27+
this->TIMER_ETHERNET_cfg.cycle_end_irq = FSP_INVALID_VECTOR;
28+
}
29+
30+
fsp_err_t EthernetClock::start() {
31+
fsp_err_t err = R_AGT_Open(&this->TIMER_ETHERNET_ctrl,&this->TIMER_ETHERNET_cfg);
32+
if (err != FSP_SUCCESS) {
33+
return err;
34+
}
35+
err = R_AGT_Enable(&this->TIMER_ETHERNET_ctrl);
36+
if (err != FSP_SUCCESS) {
37+
return err;
38+
}
39+
err = R_AGT_Start(&this->TIMER_ETHERNET_ctrl);
40+
if (err != FSP_SUCCESS) {
41+
return err;
42+
}
43+
44+
FspTimer::set_timer_is_used(AGT_TIMER, ETHERNET_AGT_TIMER_CHANNEL);
45+
return err;
46+
}
47+
48+
fsp_err_t EthernetClock::stop() {
49+
fsp_err_t err = R_AGT_Stop(&this->TIMER_ETHERNET_ctrl);
50+
if (err != FSP_SUCCESS) {
51+
return err;
52+
} else {
53+
err = R_AGT_Close(&this->TIMER_ETHERNET_ctrl);
54+
if (err != FSP_SUCCESS) {
55+
return err;
56+
} else {
57+
err = R_AGT_Disable(&this->TIMER_ETHERNET_ctrl);
58+
if (err != FSP_SUCCESS) {
59+
return err;
60+
}
61+
}
62+
}
63+
}
64+
65+
#else
66+
67+
EthernetClock::EthernetClock() {
68+
}
69+
70+
fsp_err_t EthernetClock::start() {
71+
return FSP_SUCCESS;
72+
}
73+
74+
fsp_err_t EthernetClock::stop() {
75+
return FSP_SUCCESS;
76+
}
77+
78+
#endif

Diff for: libraries/Ethernet/src/EthernetClock.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#ifndef ETHERNET_CLOCK_H
3+
#define ETHERNET_CLOCK_H
4+
5+
#include "FspTimer.h"
6+
7+
class EthernetClock {
8+
public:
9+
EthernetClock();
10+
fsp_err_t start();
11+
fsp_err_t stop();
12+
13+
private:
14+
agt_instance_ctrl_t TIMER_ETHERNET_ctrl;
15+
agt_extended_cfg_t TIMER_ETHERNET_extend;
16+
timer_cfg_t TIMER_ETHERNET_cfg;
17+
};
18+
19+
#endif

Diff for: variants/PORTENTA_C33/pins_arduino.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,7 @@ static const uint8_t SS = PIN_SPI_CS;
208208

209209
#define RTC_CLOCK_SOURCE RTC_CLOCK_SOURCE_SUBCLK
210210

211-
#define AR_INTERNAL_VOLTAGE 1.18f
211+
#define AR_INTERNAL_VOLTAGE 1.18f
212+
213+
#define ETHERNET_AGT_TIMER_CHANNEL 3
214+
#define ETHERNET_CLK_PIN BSP_IO_PORT_06_PIN_00

Diff for: variants/PORTENTA_C33/variant.cpp

+1-51
Original file line numberDiff line numberDiff line change
@@ -216,58 +216,8 @@ int32_t getPinIndex(bsp_io_port_pin_t p) {
216216
return rv;
217217
}
218218

219-
#include "FspTimer.h"
220-
221-
#define AGT_TIMER_CHANNEL 3
222-
#define ETHERNET_CLK_PIN BSP_IO_PORT_06_PIN_00
223-
224-
agt_instance_ctrl_t TIMER_ETHERNET_ctrl;
225-
agt_extended_cfg_t TIMER_ETHERNET_extend;
226-
timer_cfg_t TIMER_ETHERNET_cfg;
227-
228-
229-
fsp_err_t startETHClock() {
230-
pinPeripheral(ETHERNET_CLK_PIN, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_AGT));
231-
232-
TIMER_ETHERNET_extend.count_source = AGT_CLOCK_PCLKB;
233-
TIMER_ETHERNET_extend.agto = AGT_PIN_CFG_START_LEVEL_LOW;
234-
TIMER_ETHERNET_extend.agtoab_settings_b.agtoa = AGT_PIN_CFG_DISABLED;
235-
TIMER_ETHERNET_extend.agtoab_settings_b.agtob = AGT_PIN_CFG_DISABLED;
236-
TIMER_ETHERNET_extend.measurement_mode = AGT_MEASURE_DISABLED;
237-
TIMER_ETHERNET_extend.agtio_filter = AGT_AGTIO_FILTER_NONE;
238-
TIMER_ETHERNET_extend.enable_pin = AGT_ENABLE_PIN_NOT_USED;
239-
TIMER_ETHERNET_extend.trigger_edge = AGT_TRIGGER_EDGE_RISING;
240-
241-
TIMER_ETHERNET_cfg.mode = TIMER_MODE_PERIODIC;
242-
TIMER_ETHERNET_cfg.period_counts = (uint32_t) 0x1;
243-
TIMER_ETHERNET_cfg.duty_cycle_counts = 0x00;
244-
TIMER_ETHERNET_cfg.source_div = (timer_source_div_t) 0;
245-
TIMER_ETHERNET_cfg.channel = AGT_TIMER_CHANNEL;
246-
TIMER_ETHERNET_cfg.p_callback = NULL;
247-
TIMER_ETHERNET_cfg.p_context = NULL;
248-
TIMER_ETHERNET_cfg.p_extend = &TIMER_ETHERNET_extend;
249-
TIMER_ETHERNET_cfg.cycle_end_ipl = (BSP_IRQ_DISABLED);
250-
TIMER_ETHERNET_cfg.cycle_end_irq = FSP_INVALID_VECTOR;
251-
252-
fsp_err_t err = R_AGT_Open(&TIMER_ETHERNET_ctrl,&TIMER_ETHERNET_cfg);
253-
if (err != FSP_SUCCESS) {
254-
return err;
255-
}
256-
err = R_AGT_Enable(&TIMER_ETHERNET_ctrl);
257-
if (err != FSP_SUCCESS) {
258-
return err;
259-
}
260-
err = R_AGT_Start(&TIMER_ETHERNET_ctrl);
261-
if (err != FSP_SUCCESS) {
262-
return err;
263-
}
264-
265-
FspTimer::set_timer_is_used(AGT_TIMER, AGT_TIMER_CHANNEL);
266-
return err;
267-
}
268-
269219
void initVariant() {
270-
startETHClock();
220+
271221
// bootloader configures LED_BUILTIN as PWM output, deconfigure it to avoid spurious signals
272222
pinMode(LED_BUILTIN, INPUT);
273223
}

0 commit comments

Comments
 (0)