Skip to content

Commit e5f63e9

Browse files
cristidragomir97facchinm
authored andcommitted
c33: ethernet: move AGT clock into library
1 parent 2ef2b3a commit e5f63e9

File tree

5 files changed

+113
-52
lines changed

5 files changed

+113
-52
lines changed

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

+14-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,16 @@
1212
/* -------------------------------------------------------------------------- */
1313
int CEthernet::begin(unsigned long timeout, unsigned long responseTimeout) {
1414
/* -------------------------------------------------------------------------- */
15+
16+
this -> ethernetTimer = new EthernetClock();
17+
this -> ethernetTimer->start();
18+
Serial.println("ethernetTimer started");
19+
delay(2);
1520
(void)responseTimeout;
1621

1722
int rv = 0;
1823

24+
1925
ni = CLwipIf::getInstance().get(NI_ETHERNET);
2026
if(ni != nullptr) {
2127
ni->DhcpSetTimeout(timeout);
@@ -56,6 +62,11 @@ int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway
5662
int CEthernet::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
5763
/* -------------------------------------------------------------------------- */
5864

65+
this -> ethernetTimer = new EthernetClock();
66+
this -> ethernetTimer->start();
67+
Serial.println("ethernetTimer started");
68+
delay(2);
69+
5970
if (ni != nullptr) {
6071
ni->config(local_ip, gateway, subnet);
6172
} else {
@@ -136,6 +147,8 @@ EthernetHardwareStatus CEthernet::hardwareStatus() {
136147
/* -------------------------------------------------------------------------- */
137148
int CEthernet::disconnect() {
138149
/* -------------------------------------------------------------------------- */
150+
this -> ethernetTimer->stop();
151+
this -> ethernetTimer = NULL;
139152
return 1;
140153
}
141154

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

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

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

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

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)