Skip to content

Commit 804d549

Browse files
committedMar 27, 2024·
First draft of high-level API for controlling various DIOs.
This works, but needs refinement and extension.
1 parent b8f7e9a commit 804d549

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* This example has been tested with the Arduino 10BASE-T1S (T1TOS) shield and
3+
* can be used to control the value of various DIO output pins.
4+
*
5+
* Author:
6+
* Alexander Entinger
7+
*/
8+
9+
/**************************************************************************************
10+
* INCLUDE
11+
**************************************************************************************/
12+
13+
#include <Arduino_10BASE_T1S.h>
14+
15+
#include <SPI.h>
16+
17+
/**************************************************************************************
18+
* CONSTANTS
19+
**************************************************************************************/
20+
21+
static uint8_t const T1S_PLCA_NODE_ID = 0; /* Doubles as PLCA coordinator. */
22+
23+
static IPAddress const ip_addr {192, 168, 42, 100 + T1S_PLCA_NODE_ID};
24+
static IPAddress const network_mask{255, 255, 255, 0};
25+
static IPAddress const gateway {192, 168, 42, 100};
26+
27+
static T1SPlcaSettings const t1s_plca_settings{T1S_PLCA_NODE_ID};
28+
static T1SMacSettings const t1s_default_mac_settings;
29+
30+
/**************************************************************************************
31+
* GLOBAL VARIABLES
32+
**************************************************************************************/
33+
34+
auto const tc6_io = new TC6::TC6_Io
35+
( SPI
36+
, CS_PIN
37+
, RESET_PIN
38+
, IRQ_PIN);
39+
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
40+
41+
/**************************************************************************************
42+
* SETUP/LOOP
43+
**************************************************************************************/
44+
45+
void setup()
46+
{
47+
Serial.begin(115200);
48+
while (!Serial) { }
49+
delay(1000);
50+
51+
/* Initialize digital IO interface for interfacing
52+
* with the LAN8651.
53+
*/
54+
pinMode(IRQ_PIN, INPUT_PULLUP);
55+
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),
56+
[]() { tc6_io->onInterrupt(); },
57+
FALLING);
58+
59+
/* Initialize IO module. */
60+
if (!tc6_io->begin())
61+
{
62+
Serial.println("'TC6_Io::begin(...)' failed.");
63+
for (;;) { }
64+
}
65+
66+
MacAddress const mac_addr = MacAddress::create_from_uid();
67+
68+
if (!tc6_inst->begin(ip_addr
69+
, network_mask
70+
, gateway
71+
, mac_addr
72+
, t1s_plca_settings
73+
, t1s_default_mac_settings))
74+
{
75+
Serial.println("'TC6::begin(...)' failed.");
76+
for (;;) { }
77+
}
78+
79+
Serial.print("IP\t");
80+
Serial.println(ip_addr);
81+
Serial.println(mac_addr);
82+
Serial.println(t1s_plca_settings);
83+
Serial.println(t1s_default_mac_settings);
84+
85+
// If Power Provider, turn on LOCAL_ENABLE and turn on T1S_DISABLE
86+
//tc6_inst->digitalWrite(1,1,1);
87+
//tc6_inst->digitalWrite(0,0,0);
88+
}
89+
90+
void loop()
91+
{
92+
/* Services the hardware and the protocol stack.
93+
* Must be called cyclic. The faster the better.
94+
*/
95+
tc6_inst->service();
96+
97+
static unsigned long prev_dio_toogle = 0;
98+
auto const now = millis();
99+
100+
if ((now - prev_dio_toogle) > 5000)
101+
{
102+
prev_dio_toogle = now;
103+
104+
static bool dio_val = true;
105+
106+
Serial.print("DIO A0 = ");
107+
Serial.println(dio_val);
108+
109+
/* Modify this function call parameter if you want
110+
* to test a different LAN8651 DIO.
111+
*/
112+
tc6_inst->digitalWrite(TC6::DIO::A0, dio_val);
113+
dio_val = !dio_val;
114+
}
115+
}

‎src/microchip/TC6_Arduino_10BASE_T1S.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ void TC6_Arduino_10BASE_T1S::digitalWrite(bool dioa0, bool dioa1, bool dioa2)
198198
TC6Regs_SetDio(_lw.tc.tc6, dioa0, dioa1, dioa2);
199199
}
200200

201+
void TC6_Arduino_10BASE_T1S::digitalWrite(DIO const dio, bool const value)
202+
{
203+
if (dio == DIO::A0)
204+
digitalWrite_A0(value);
205+
}
206+
201207
void TC6_Arduino_10BASE_T1S::service()
202208
{
203209
sys_check_timeouts(); /* LWIP timers - ARP, DHCP, TCP, etc. */
@@ -237,6 +243,23 @@ bool TC6_Arduino_10BASE_T1S::sendWouldBlock()
237243
return wouldBlock;
238244
}
239245

246+
void TC6_Arduino_10BASE_T1S::digitalWrite_A0(bool const value)
247+
{
248+
static bool is_dio_a0_enabled = false;
249+
if (!is_dio_a0_enabled)
250+
{
251+
TC6Regs_EnableDio_A0(_lw.tc.tc6);
252+
is_dio_a0_enabled = true;
253+
}
254+
255+
static bool dio_a0_val = false;
256+
if (value != dio_a0_val)
257+
{
258+
TC6Regs_ToggleDio_A0(_lw.tc.tc6);
259+
dio_a0_val = value;
260+
}
261+
}
262+
240263
/**************************************************************************************
241264
* LWIP CALLBACKS
242265
**************************************************************************************/

‎src/microchip/TC6_Arduino_10BASE_T1S.h

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ typedef struct
6161
namespace TC6
6262
{
6363

64+
/**************************************************************************************
65+
* TYPEDEF
66+
**************************************************************************************/
67+
68+
enum class DIO { A0, A1, A2, A3, A4, B0 };
69+
6470
/**************************************************************************************
6571
* CLASS DECLARATION
6672
**************************************************************************************/
@@ -85,6 +91,8 @@ class TC6_Arduino_10BASE_T1S : public Arduino_10BASE_T1S_PHY_Interface
8591

8692
void digitalWrite(bool dioa0, bool dioa1, bool dioa2);
8793

94+
void digitalWrite(DIO const dio, bool const value);
95+
8896
bool getPlcaStatus(TC6LwIP_On_PlcaStatus on_plca_status);
8997
bool enablePlca();
9098

@@ -95,6 +103,8 @@ class TC6_Arduino_10BASE_T1S : public Arduino_10BASE_T1S_PHY_Interface
95103
TC6_Io * _tc6_io;
96104
TC6LwIP_t _lw;
97105
T1SPlcaSettings _t1s_plca_settings;
106+
107+
void digitalWrite_A0(bool const value);
98108
};
99109

100110
/**************************************************************************************

‎src/microchip/lib/libtc6/inc/tc6-regs.h

+39-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Microchip or any third party.
3838
#include <stdint.h>
3939
#include "tc6.h"
4040

41+
#include <type_traits>
42+
4143
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
4244
/* DEFINITIONS */
4345
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
@@ -82,6 +84,18 @@ typedef enum
8284
TC6Regs_Event_Unsupported_Hardware
8385
} TC6Regs_Event_t;
8486

87+
enum class PADCTRL_A0SEL : uint32_t
88+
{
89+
EVENT_CAPTURE = 0x00,
90+
EVENT_GENERATOR_0 = 0x01
91+
};
92+
constexpr uint32_t PADCTRL_A0SEL_MASK = 0x00000003;
93+
94+
enum class EG0CTL : uint32_t
95+
{
96+
START = 0,
97+
};
98+
8599
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
86100
/* PUBLIC API */
87101
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
@@ -128,6 +142,8 @@ bool TC6Regs_SetPlca(TC6_t *pInst, bool plcaEnable, uint8_t nodeId, uint8_t node
128142
*/
129143
bool TC6Regs_SetDio(TC6_t *pTC6, bool dioa0, bool dioa1, bool dioa2);
130144

145+
void TC6Regs_EnableDio_A0(TC6_t *pTC6);
146+
void TC6Regs_ToggleDio_A0(TC6_t *pTC6);
131147

132148
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
133149
/* Implementation of TC6 Callback */
@@ -159,6 +175,28 @@ uint32_t TC6Regs_CB_GetTicksMs(void);
159175
* \param event - Enumeration matching to the occured event.
160176
* \param pTag - The exact same pointer, which was given along with the TC6Regs_Init() function.
161177
*/
162-
void TC6Regs_CB_OnEvent(TC6_t *pInst, TC6Regs_Event_t event, void *pTag);
178+
void TC6Regs_CB_OnEvent(TC6_t *pInst, TC6Regs_Event_t event, void *pTag);
179+
180+
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
181+
/* CONVERSTION FUNCTIONS */
182+
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
183+
184+
template <typename Enumeration>
185+
constexpr auto to_integer(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
186+
{
187+
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
188+
}
189+
190+
template <typename Enumeration>
191+
constexpr auto bp(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
192+
{
193+
return to_integer(value);
194+
}
195+
196+
template <typename Enumeration>
197+
constexpr auto bm(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
198+
{
199+
return (1 << to_integer(value));
200+
}
163201

164202
#endif /* TC6_REGS_H_ */

‎src/microchip/lib/libtc6/src/tc6-regs.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,40 @@ bool TC6Regs_SetDio(TC6_t *pTC6, bool dioa0, bool dioa1, bool dioa2)
211211
return true;
212212
}
213213

214+
static bool is_dio_a0_op_done = false;
215+
void TC6_Dio_A0_Callback(TC6_t *pInst, bool success, uint32_t addr, uint32_t value, void *pTag, void *pGlobalTag)
216+
{
217+
is_dio_a0_op_done = true;
218+
}
219+
220+
void TC6Regs_EnableDio_A0(TC6_t *pTC6)
221+
{
222+
TC6Reg_t * pReg = GetContext(pTC6);
223+
224+
/* Configure as output, PADCTRL = 0x0088. */
225+
uint32_t reg_val = to_integer(PADCTRL_A0SEL::EVENT_GENERATOR_0);
226+
uint32_t reg_mask = PADCTRL_A0SEL_MASK;
227+
228+
is_dio_a0_op_done = false;
229+
while (!is_dio_a0_op_done && !TC6_ReadModifyWriteRegister(pReg->pTC6, 0x000A0088, reg_val, reg_mask, CONTROL_PROTECTION, TC6_Dio_A0_Callback, NULL)) {
230+
TC6_Service(pReg->pTC6, true);
231+
}
232+
}
233+
234+
void TC6Regs_ToggleDio_A0(TC6_t *pTC6)
235+
{
236+
TC6Reg_t * pReg = GetContext(pTC6);
237+
238+
/* Toggle output, EG0CTL = 0x0226. */
239+
uint32_t reg_val = bm(EG0CTL::START);
240+
uint32_t reg_mask = bm(EG0CTL::START);;
241+
242+
is_dio_a0_op_done = false;
243+
while (!is_dio_a0_op_done && !TC6_ReadModifyWriteRegister(pReg->pTC6, 0x000A0226, reg_val, reg_mask, CONTROL_PROTECTION, TC6_Dio_A0_Callback, NULL)) {
244+
TC6_Service(pReg->pTC6, true);
245+
}
246+
}
247+
214248
uint8_t TC6Regs_GetChipRevision(TC6_t *pTC6)
215249
{
216250
TC6Reg_t *pReg = GetContext(pTC6);

0 commit comments

Comments
 (0)