Skyward boardcore
Loading...
Searching...
No Matches
WIZ5500.h
Go to the documentation of this file.
1/* Copyright (c) 2023 Skyward Experimental Rocketry
2 * Author: Davide Mor
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <ActiveObject.h>
27#include <miosix.h>
28
29#include <functional>
30#include <iomanip>
31
32namespace Boardcore
33{
34
38struct WizIp
39{
40 uint8_t a, b, c, d;
41
42 WizIp() = default;
43
44 constexpr WizIp(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
45 : a{a}, b{b}, c{c}, d{d}
46 {
47 }
48
49 constexpr explicit WizIp(uint32_t ip)
50 : a{static_cast<uint8_t>((ip >> 24) & 0xFF)},
51 b{static_cast<uint8_t>((ip >> 16) & 0xFF)},
52 c{static_cast<uint8_t>((ip >> 8) & 0xFF)},
53 d{static_cast<uint8_t>(ip & 0xFF)}
54 {
55 }
56
57 constexpr explicit operator uint32_t() const
58 {
59 return a << 24 | b << 16 | c << 8 | d;
60 }
61};
62
66struct WizMac
67{
68 uint8_t a, b, c, d, e, f;
69};
70
75{
76public:
77 struct PhyState
78 {
79 bool full_duplex; //< True if full duplex is enabled, false if link is
80 // only half duplex.
81 bool based_100mbps; //< True if 100Mbps, false if only 10Mpbs.
82 bool link_up; //< True if link is up, false if it is down.
83 };
84
85 using OnIpConflictCb = std::function<void()>;
86 using OnDestUnreachableCb = std::function<void(WizIp, uint16_t)>;
87
96 Wiz5500(SPIBus& bus, miosix::GpioPin cs, miosix::GpioPin intn,
97 SPI::ClockDivider clock_divider);
98 ~Wiz5500();
99
116
133
141 bool checkVersion();
142
150
156 void reset();
157
161 void handleINTn();
162
166 void setGatewayIp(WizIp ip);
167
171 void setSubnetMask(WizIp mask);
172
176 void setSourceMac(WizMac mac);
177
181 void setSourceIp(WizIp ip);
182
194 bool connectTcp(int sock_n, uint16_t src_port, WizIp dst_ip,
195 uint16_t dst_port, int timeout = -1);
196
208 bool listenTcp(int sock_n, uint16_t src_port, WizIp& dst_ip,
209 uint16_t& dst_port, int timeout = -1);
210
222 bool openUdp(int sock_n, uint16_t src_port, WizIp dst_ip, uint16_t dst_port,
223 int timeout = -1);
224
235 bool send(int sock_n, const uint8_t* data, size_t len, int timeout = -1);
236
247 ssize_t recv(int sock_n, uint8_t* data, size_t len, int timeout = -1);
248
261 ssize_t recvfrom(int sock_n, uint8_t* data, size_t len, WizIp& dst_ip,
262 uint16_t& dst_port, int timeout = -1);
263
270 void close(int sock_n, int timeout = -1);
271
272private:
273 static constexpr int NUM_THREAD_WAIT_INFOS = 16;
274 static constexpr int NUM_SOCKETS = 8;
275
276 miosix::TimedWaitResult waitForINTn(miosix::Lock<miosix::FastMutex>& l,
277 long long until);
278 int waitForSocketIrq(miosix::Lock<miosix::FastMutex>& l, int sock_n,
279 uint8_t irq_mask, long long until);
280
281 miosix::TimedWaitResult runInterruptServiceRoutine(
282 miosix::Lock<miosix::FastMutex>& l, long long until);
283
284 void spiRead(uint8_t block, uint16_t address, uint8_t* data, size_t len);
285 void spiWrite(uint8_t block, uint16_t address, const uint8_t* data,
286 size_t len);
287
288 uint8_t spiRead8(uint8_t block, uint16_t address);
289 uint16_t spiRead16(uint8_t block, uint16_t address);
290 WizIp spiReadIp(uint8_t block, uint16_t address);
291 // Avoid stupid linter error
292 // WizMac spiReadMac(uint8_t block, uint16_t address);
293
294 void spiWrite8(uint8_t block, uint16_t address, uint8_t data);
295 void spiWrite16(uint8_t block, uint16_t address, uint16_t data);
296 void spiWriteIp(uint8_t block, uint16_t address, WizIp data);
297 void spiWriteMac(uint8_t block, uint16_t address, WizMac data);
298
299 // Thread currently servicing interrupts
300 miosix::Thread* interrupt_service_thread = nullptr;
301 // Thread currently waiting for an INTn
302 miosix::Thread* intn_thread = nullptr;
303
304 struct ThreadWaitInfo
305 {
306 int sock_n;
307 uint8_t irq_mask;
308 uint8_t irq;
309 miosix::Thread* thread;
310 };
311
312 enum class SocketMode
313 {
314 TCP,
315 UDP,
316 CLOSED
317 };
318
319 struct SocketInfo
320 {
321 SocketMode mode;
322 int irq_mask;
323 };
324
325 SocketInfo socket_infos[NUM_SOCKETS];
326 ThreadWaitInfo wait_infos[NUM_THREAD_WAIT_INFOS];
327
328 OnIpConflictCb on_ip_conflict;
329 OnDestUnreachableCb on_dest_unreachable;
330
331 miosix::GpioPin intn;
332 miosix::FastMutex mutex;
333 SPISlave slave;
334};
335
336} // namespace Boardcore
337
338namespace std
339{
340inline ostream& operator<<(ostream& os, const Boardcore::WizIp& ip)
341{
342 auto old_flags = os.flags(os.dec);
343 os << (int)ip.a << "." << (int)ip.b << "." << (int)ip.c << "." << (int)ip.d;
344 os.flags(old_flags);
345 return os;
346}
347
348inline ostream& operator<<(ostream& os, const Boardcore::WizMac& mac)
349{
350 auto old_flags = os.flags(os.hex);
351 auto old_fill = os.fill('0');
352 os << std::setw(2) << (int)mac.a << ":" << std::setw(2) << (int)mac.b << ":"
353 << std::setw(2) << (int)mac.c << ":" << std::setw(2) << (int)mac.d << ":"
354 << std::setw(2) << (int)mac.e << ":" << std::setw(2) << (int)mac.f;
355 os.flags(old_flags);
356 os.fill(old_fill);
357 return os;
358}
359} // namespace std
Driver for STM32 low level SPI peripheral.
Definition SPIBus.h:61
Driver for the WizNet W5500 ethernet.
Definition WIZ5500.h:75
ssize_t recvfrom(int sock_n, uint8_t *data, size_t len, WizIp &dst_ip, uint16_t &dst_port, int timeout=-1)
Receive data from the socket (works only in UDP).
Definition WIZ5500.cpp:365
std::function< void()> OnIpConflictCb
Definition WIZ5500.h:85
void setSourceIp(WizIp ip)
Set the device IP address.
Definition WIZ5500.cpp:159
bool openUdp(int sock_n, uint16_t src_port, WizIp dst_ip, uint16_t dst_port, int timeout=-1)
Open a simple UDP socket.
Definition WIZ5500.cpp:260
void setSourceMac(WizMac mac)
Set the device MAC address.
Definition WIZ5500.cpp:153
void setOnIpConflict(OnIpConflictCb cb)
Sets the callback to be invoked when the device detects an IP. conflict.
Definition WIZ5500.cpp:82
std::function< void(WizIp, uint16_t)> OnDestUnreachableCb
Definition WIZ5500.h:86
bool checkVersion()
Checks the VERSION register. Can be used to detect device presence.
Definition WIZ5500.cpp:94
void setGatewayIp(WizIp ip)
Set global gateway ip.
Definition WIZ5500.cpp:141
void reset()
Resets the device. Performs a software resets, resetting all registers and closing all sockets.
Definition WIZ5500.cpp:113
bool listenTcp(int sock_n, uint16_t src_port, WizIp &dst_ip, uint16_t &dst_port, int timeout=-1)
Listen for a single remote TCP connection.
Definition WIZ5500.cpp:210
void handleINTn()
Handle an interrupt from INTn.
Definition WIZ5500.cpp:128
ssize_t recv(int sock_n, uint8_t *data, size_t len, int timeout=-1)
Receive data from the socket (works only in TCP).
Definition WIZ5500.cpp:327
void setSubnetMask(WizIp mask)
Set global subnet mask.
Definition WIZ5500.cpp:147
void setOnDestUnreachable(OnDestUnreachableCb cb)
Sets the callback to be invoked when the device detects an unreachable host.
Definition WIZ5500.cpp:88
bool send(int sock_n, const uint8_t *data, size_t len, int timeout=-1)
Send data through the socket (works both in TCP and UDP).
Definition WIZ5500.cpp:293
PhyState getPhyState()
Get current PHY state, can be used to poll link status, and wait for link up.
Definition WIZ5500.cpp:101
void close(int sock_n, int timeout=-1)
Close a socket.
Definition WIZ5500.cpp:422
bool connectTcp(int sock_n, uint16_t src_port, WizIp dst_ip, uint16_t dst_port, int timeout=-1)
Connect to a remote socket via TCP.
Definition WIZ5500.cpp:165
ClockDivider
SPI Clock divider.
Definition SPIDefs.h:70
Driver for the VN100S IMU.
Definition WIZ5500.h:339
ostream & operator<<(ostream &os, const Boardcore::WizIp &ip)
Definition WIZ5500.h:340
Class representing an IPv4 ip.
Definition WIZ5500.h:39
constexpr WizIp(uint32_t ip)
Definition WIZ5500.h:49
constexpr WizIp(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Definition WIZ5500.h:44
Class representing an ethernet MAC address.
Definition WIZ5500.h:67