MACRAW-only: simplify send/recv API, remove non-MACRAW code paths and unused types

This commit is contained in:
Ian Gulliver
2026-04-10 21:37:03 +09:00
parent bee0fa3aef
commit 394628b8da
4 changed files with 28 additions and 221 deletions

View File

@@ -236,7 +236,6 @@ constexpr uint8_t PACK_NONE = 0x00;
constexpr uint8_t PACK_FIRST = 1 << 1;
constexpr uint8_t PACK_REMAINED = 1 << 2;
constexpr uint8_t PACK_COMPLETED = 1 << 3;
constexpr uint8_t PACK_IPv6 = 1 << 7;
constexpr uint8_t SPI_READ = (0x00 << 5);
constexpr uint8_t SPI_WRITE = (0x01 << 5);
@@ -273,11 +272,7 @@ constexpr uint32_t REG_SN_CR(uint8_t n) { return (0x0010 << 8) + SREG_BLOCK
constexpr uint32_t REG_SN_IR(uint8_t n) { return (0x0020 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_IRCLR(uint8_t n) { return (0x0028 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_SR(uint8_t n) { return (0x0030 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_PORTR(uint8_t n) { return (0x0114 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_DHAR(uint8_t n) { return (0x0118 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_DIPR(uint8_t n) { return (0x0120 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_DIP6R(uint8_t n) { return (0x0130 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_DPORTR(uint8_t n) { return (0x0140 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_MR2(uint8_t n) { return (0x0144 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_TX_BSR(uint8_t n) { return (0x0200 << 8) + SREG_BLOCK(n); }
constexpr uint32_t REG_SN_TX_FSR(uint8_t n) { return (0x0204 << 8) + SREG_BLOCK(n); }
@@ -289,26 +284,10 @@ constexpr uint32_t REG_SN_RX_RD(uint8_t n) { return (0x0228 << 8) + SREG_BLOCK
constexpr uint8_t SYSR_CHPL = 1 << 7;
constexpr uint8_t SYSR_NETL = 1 << 6;
constexpr uint8_t SYCR0_RST = 0x00;
constexpr uint8_t SN_MR_MULTI = 1 << 7;
constexpr uint8_t SN_MR_UNIB = 1 << 4;
constexpr uint8_t SN_MR_TCP = 0x01;
constexpr uint8_t SN_MR_TCP4 = SN_MR_TCP;
constexpr uint8_t SN_MR_UDP = 0x02;
constexpr uint8_t SN_MR_UDP4 = SN_MR_UDP;
constexpr uint8_t SN_MR_IPRAW = 0x03;
constexpr uint8_t SN_MR_IPRAW4 = SN_MR_IPRAW;
constexpr uint8_t SN_MR_MACRAW = 0x07;
constexpr uint8_t SN_MR_TCP6 = 0x09;
constexpr uint8_t SN_MR_UDP6 = 0x0A;
constexpr uint8_t SN_MR_IPRAW6 = 0x0B;
constexpr uint8_t SN_MR_TCPD = 0x0D;
constexpr uint8_t SN_MR_UDPD = 0x0E;
constexpr uint8_t SN_MR2_DHAM = 1 << 1;
constexpr uint8_t SN_MR2_FARP = 1 << 0;
constexpr uint8_t SN_CR_OPEN = 0x01;
constexpr uint8_t SN_CR_CLOSE = 0x10;
constexpr uint8_t SN_CR_SEND = 0x20;
constexpr uint8_t SN_CR_SEND6 = 0xA0;
constexpr uint8_t SN_CR_RECV = 0x40;
constexpr uint8_t SN_IR_SENDOK = 0x10;
constexpr uint8_t SN_IR_TIMEOUT = 0x08;
@@ -354,24 +333,13 @@ void net_lock() { set_netlckr(0xC5); }
void net_unlock() { set_netlckr(0x3A); }
void set_sn_mr(uint8_t sn, uint8_t v) { reg_write(REG_SN_MR(sn), v); }
uint8_t get_sn_mr(uint8_t sn) { return reg_read(REG_SN_MR(sn)); }
void set_sn_cr(uint8_t sn, uint8_t v) { reg_write(REG_SN_CR(sn), v); }
uint8_t get_sn_cr(uint8_t sn) { return reg_read(REG_SN_CR(sn)); }
uint8_t get_sn_ir(uint8_t sn) { return reg_read(REG_SN_IR(sn)); }
void set_sn_irclr(uint8_t sn, uint8_t v) { reg_write(REG_SN_IRCLR(sn), v); }
void set_sn_ir(uint8_t sn, uint8_t v) { set_sn_irclr(sn, v); }
uint8_t get_sn_sr(uint8_t sn) { return reg_read(REG_SN_SR(sn)); }
void set_sn_portr(uint8_t sn, uint16_t v) {
reg_write(REG_SN_PORTR(sn), (uint8_t)(v >> 8));
reg_write(offset_inc(REG_SN_PORTR(sn), 1), (uint8_t)v);
}
void set_sn_dhar(uint8_t sn, uint8_t* v) { reg_write_buf(REG_SN_DHAR(sn), v, 6); }
void set_sn_dipr(uint8_t sn, uint8_t* v) { reg_write_buf(REG_SN_DIPR(sn), v, 4); }
void set_sn_dip6r(uint8_t sn, uint8_t* v) { reg_write_buf(REG_SN_DIP6R(sn), v, 16); }
void set_sn_dportr(uint8_t sn, uint16_t v) {
reg_write(REG_SN_DPORTR(sn), (uint8_t)(v >> 8));
reg_write(offset_inc(REG_SN_DPORTR(sn), 1), (uint8_t)v);
}
void set_sn_mr2(uint8_t sn, uint8_t v) { reg_write(REG_SN_MR2(sn), v); }
void set_sn_tx_bsr(uint8_t sn, uint8_t v) { reg_write(REG_SN_TX_BSR(sn), v); }
void set_sn_txbuf_size(uint8_t sn, uint8_t v) { set_sn_tx_bsr(sn, v); }
@@ -460,11 +428,6 @@ void recv_data(uint8_t sn, uint8_t *data, uint16_t len) {
set_sn_rx_rd(sn, ptr);
}
void recv_ignore(uint8_t sn, uint16_t len) {
set_sn_rx_rd(sn, get_sn_rx_rd(sn) + len);
}
void soft_reset() {
uint8_t gw[4], sn[4], sip[4], mac[6];
uint8_t gw6[16], sn6[16], lla[16], gua[16];
@@ -505,9 +468,6 @@ int8_t init_buffers(std::span<const uint8_t> txsize, std::span<const uint8_t> rx
return 0;
}
constexpr uint16_t SOCK_ANY_PORT_NUM = 0xC000;
uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
uint16_t sock_io_mode_bits = 0;
uint16_t sock_is_sending = 0;
uint16_t sock_remained_size[sock_count] = {0,};
@@ -550,66 +510,15 @@ void set_interrupt_mask(intr_kind intr) {
}
std::expected<socket_id, sock_error> open_socket(socket_id sid, protocol proto, port_num port, sock_flag flag) {
std::expected<socket_id, sock_error> open_socket(socket_id sid, protocol proto, sock_flag flag) {
uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port);
uint8_t pr = static_cast<uint8_t>(proto);
uint8_t fl = static_cast<uint8_t>(flag);
uint8_t taddr[16];
CHECK_SOCKNUM();
switch (pr & 0x0F) {
case SN_MR_TCP4:
get_sipr(taddr);
CHECK_IPZERO(taddr, 4);
break;
case SN_MR_TCP6:
get_llar(taddr);
CHECK_IPZERO(taddr, 16);
break;
case SN_MR_TCPD:
get_sipr(taddr);
CHECK_IPZERO(taddr, 4);
get_llar(taddr);
CHECK_IPZERO(taddr, 16);
break;
case SN_MR_UDP:
case SN_MR_UDP6:
case SN_MR_UDPD:
case SN_MR_MACRAW:
case SN_MR_IPRAW4:
case SN_MR_IPRAW6:
break;
default:
FAIL(sock_mode);
}
if ((fl & 0x04) != 0) FAIL(sock_flag);
if (fl != 0) {
switch (pr) {
case SN_MR_MACRAW:
if ((fl & (SN_MR2_DHAM | SN_MR2_FARP)) != 0) FAIL(sock_flag);
break;
case SN_MR_TCP4:
case SN_MR_TCP6:
case SN_MR_TCPD:
if ((fl & (SN_MR_MULTI | SN_MR_UNIB)) != 0) FAIL(sock_flag);
break;
case SN_MR_IPRAW4:
case SN_MR_IPRAW6:
if (fl != 0) FAIL(sock_flag);
break;
default:
break;
}
}
if ((pr & 0x0F) != SN_MR_MACRAW) FAIL(sock_mode);
close(sid);
set_sn_mr(sn, (pr | (fl & 0xF0)));
set_sn_mr2(sn, fl & 0x03);
if (!p) {
p = sock_any_port++;
if (sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM;
}
set_sn_portr(sn, p);
set_sn_cr(sn, SN_CR_OPEN);
while (get_sn_cr(sn));
sock_io_mode_bits &= ~(1 << sn);
@@ -621,49 +530,13 @@ std::expected<socket_id, sock_error> open_socket(socket_id sid, protocol proto,
return sid;
}
std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_t> buf, const ip_address& addr, port_num port) {
std::expected<uint16_t, sock_error> send(socket_id sid, std::span<const uint8_t> buf) {
uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port);
uint16_t len = buf.size();
uint8_t addrlen = addr.len;
uint8_t tmp = 0;
uint8_t tcmd = SN_CR_SEND;
uint16_t freesize = 0;
CHECK_SOCKNUM();
switch (get_sn_mr(sn) & 0x0F) {
case SN_MR_UDP:
case SN_MR_MACRAW:
case SN_MR_IPRAW:
case SN_MR_IPRAW6:
break;
default:
FAIL(sock_mode);
}
tmp = get_sn_mr(sn);
if (tmp != SN_MR_MACRAW) {
if (addrlen == 16) {
if (tmp & 0x08) {
set_sn_dip6r(sn, const_cast<uint8_t*>(addr.ip.data()));
tcmd = SN_CR_SEND6;
} else {
FAIL(sock_mode);
}
} else if (addrlen == 4) {
if (tmp == SN_MR_UDP6 || tmp == SN_MR_IPRAW6) FAIL(sock_mode);
set_sn_dipr(sn, const_cast<uint8_t*>(addr.ip.data()));
tcmd = SN_CR_SEND;
} else {
FAIL(ip_invalid);
}
}
if ((tmp & 0x03) == 0x02) {
if (p) {
set_sn_dportr(sn, p);
} else {
FAIL(port_zero);
}
}
freesize = get_sn_tx_max(sn);
if (len > freesize) len = freesize;
@@ -674,7 +547,7 @@ std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_
if (len <= freesize) break;
};
send_data(sn, const_cast<uint8_t*>(buf.data()), len);
set_sn_cr(sn, tcmd);
set_sn_cr(sn, SN_CR_SEND);
while (get_sn_cr(sn));
while (1) {
tmp = get_sn_ir(sn);
@@ -689,26 +562,15 @@ std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_
return len;
}
std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> buf, ip_address& addr, port_num& port) {
std::expected<uint16_t, sock_error> recv(socket_id sid, std::span<uint8_t> buf) {
uint8_t sn = static_cast<uint8_t>(sid);
uint16_t len = buf.size();
uint8_t mr;
uint8_t head[8];
uint8_t head[2];
uint16_t pack_len = 0;
CHECK_SOCKNUM();
CHECK_SOCKDATA();
switch ((mr = get_sn_mr(sn)) & 0x0F) {
case SN_MR_UDP:
case SN_MR_IPRAW:
case SN_MR_IPRAW6:
case SN_MR_MACRAW:
break;
default:
FAIL(sock_mode);
}
if (sock_remained_size[sn] == 0) {
while (1) {
pack_len = get_sn_rx_rsr(sn);
@@ -724,59 +586,22 @@ std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> b
recv_data(sn, head, 2);
set_sn_cr(sn, SN_CR_RECV);
while (get_sn_cr(sn));
pack_len = head[0] & 0x07;
pack_len = (pack_len << 8) + head[1];
switch (mr & 0x07) {
case SN_MR_UDP4:
case SN_MR_UDP6:
case SN_MR_UDPD:
sock_pack_info[sn] = head[0] & 0xF8;
if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16;
else addr.len = 4;
recv_data(sn, addr.ip.data(), addr.len);
set_sn_cr(sn, SN_CR_RECV);
while (get_sn_cr(sn));
break;
case SN_MR_MACRAW:
if (sock_remained_size[sn] == 0) {
sock_remained_size[sn] = head[0];
sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[1] - 2;
if (sock_remained_size[sn] > 1514) {
close(sid);
FAIL(fatal_packlen);
}
sock_pack_info[sn] = PACK_FIRST;
if (sock_remained_size[sn] == 0) {
sock_remained_size[sn] = head[0];
sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[1] - 2;
if (sock_remained_size[sn] > 1514) {
close(sid);
FAIL(fatal_packlen);
}
if (len < sock_remained_size[sn]) pack_len = len;
else pack_len = sock_remained_size[sn];
recv_data(sn, buf.data(), pack_len);
break;
case SN_MR_IPRAW6:
case SN_MR_IPRAW4:
if (sock_remained_size[sn] == 0) {
sock_pack_info[sn] = head[0] & 0xF8;
if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16;
else addr.len = 4;
recv_data(sn, addr.ip.data(), addr.len);
set_sn_cr(sn, SN_CR_RECV);
while (get_sn_cr(sn));
}
break;
default:
recv_ignore(sn, pack_len);
sock_remained_size[sn] = pack_len;
break;
sock_pack_info[sn] = PACK_FIRST;
}
if (len < sock_remained_size[sn]) pack_len = len;
else pack_len = sock_remained_size[sn];
recv_data(sn, buf.data(), pack_len);
sock_remained_size[sn] = pack_len;
sock_pack_info[sn] |= PACK_FIRST;
if ((get_sn_mr(sn) & 0x03) == 0x02) {
recv_data(sn, head, 2);
port = static_cast<port_num>((((uint16_t)head[0]) << 8) + head[1]);
set_sn_cr(sn, SN_CR_RECV);
while (get_sn_cr(sn));
}
if (len < sock_remained_size[sn]) pack_len = len;
else pack_len = sock_remained_size[sn];

View File

@@ -9,7 +9,6 @@ namespace w6300 {
constexpr int sock_count = 8;
enum class socket_id : uint8_t {};
enum class port_num : uint16_t {};
enum class sock_error : int16_t {
busy = 0,
@@ -81,11 +80,6 @@ enum intr_kind : uint32_t {
ik_int_all = 0x00FFFF97
};
struct ip_address {
std::array<uint8_t, 16> ip;
uint8_t len;
};
void init_spi();
void reset();
void init();
@@ -94,9 +88,9 @@ bool check();
void clear_interrupt(intr_kind intr);
void set_interrupt_mask(intr_kind intr);
std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag);
std::expected<uint16_t, sock_error> sendto(socket_id sn, std::span<const uint8_t> buf, const ip_address& addr, port_num port);
std::expected<uint16_t, sock_error> recvfrom(socket_id sn, std::span<uint8_t> buf, ip_address& addr, port_num& port);
std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, sock_flag flag);
std::expected<uint16_t, sock_error> send(socket_id sn, std::span<const uint8_t> buf);
std::expected<uint16_t, sock_error> recv(socket_id sn, std::span<uint8_t> buf);
std::expected<void, sock_error> set_socket_io_mode(socket_id sn, sock_io_mode mode);
void set_socket_dest_mac(socket_id sn, const std::array<uint8_t, 6>& mac);