Remove unused W6300 API: phy, net config, TCP, keepalive, peek, and related types

This commit is contained in:
Ian Gulliver
2026-04-10 21:06:52 +09:00
parent 712110aace
commit af308b5aac
2 changed files with 20 additions and 710 deletions

View File

@@ -255,17 +255,11 @@ constexpr uint8_t PHY_LINK_ON = 1;
constexpr uint8_t PHY_POWER_NORM = 0; constexpr uint8_t PHY_POWER_NORM = 0;
constexpr uint8_t PHY_POWER_DOWN = 1; constexpr uint8_t PHY_POWER_DOWN = 1;
constexpr uint8_t PACK_NONE = static_cast<uint8_t>(pack_info::none); constexpr uint8_t PACK_NONE = 0x00;
constexpr uint8_t PACK_FIRST = static_cast<uint8_t>(pack_info::first); constexpr uint8_t PACK_FIRST = 1 << 1;
constexpr uint8_t PACK_REMAINED = static_cast<uint8_t>(pack_info::remained); constexpr uint8_t PACK_REMAINED = 1 << 2;
constexpr uint8_t PACK_COMPLETED = static_cast<uint8_t>(pack_info::completed); constexpr uint8_t PACK_COMPLETED = 1 << 3;
constexpr uint8_t PACK_IPv6 = static_cast<uint8_t>(pack_info::ipv6); constexpr uint8_t PACK_IPv6 = 1 << 7;
enum sockint_kind { sik_connected = 1, sik_disconnected = 2, sik_received = 4, sik_timeout = 8, sik_sent = 16, sik_all = 0x1F };
constexpr uint8_t TCPSOCK_MODE = static_cast<uint8_t>(tcp_sock_info::mode);
constexpr uint8_t SPI_READ = (0x00 << 5); constexpr uint8_t SPI_READ = (0x00 << 5);
constexpr uint8_t SPI_WRITE = (0x01 << 5); constexpr uint8_t SPI_WRITE = (0x01 << 5);
@@ -913,27 +907,8 @@ void recv_ignore(uint8_t sn, uint16_t len) {
} }
void mdio_write(uint8_t phyregaddr, uint16_t var) {
set_phyrar(phyregaddr);
set_phydir(var);
set_phyacr(PHYACR_WRITE);
while (get_phyacr());
}
uint16_t mdio_read(uint8_t phyregaddr) {
set_phyrar(phyregaddr);
set_phyacr(PHYACR_READ);
while (get_phyacr());
return get_phydor();
}
} // namespace } // namespace
static uint8_t dns_[4];
static uint8_t dns6_[16];
static ipconf_mode ipmode_;
static constexpr char CHIP_ID[] = "W6300";
void soft_reset() { void soft_reset() {
@@ -984,196 +959,13 @@ void clear_interrupt(intr_kind intr) {
set_slirclr((uint8_t)((uint32_t)intr >> 16)); set_slirclr((uint8_t)((uint32_t)intr >> 16));
} }
intr_kind get_interrupt() {
uint32_t ret = get_sir();
ret = (ret << 8) + get_ir();
ret = (((uint32_t)get_slir()) << 16) | ret;
return (intr_kind)ret;
}
void set_interrupt_mask(intr_kind intr) { void set_interrupt_mask(intr_kind intr) {
set_imr((uint8_t)intr); set_imr((uint8_t)intr);
set_simr((uint8_t)((uint16_t)intr >> 8)); set_simr((uint8_t)((uint16_t)intr >> 8));
set_slimr((uint8_t)((uint32_t)intr >> 16)); set_slimr((uint8_t)((uint32_t)intr >> 16));
} }
intr_kind get_interrupt_mask() {
uint32_t ret = get_simr();
ret = (ret << 8) + get_imr();
ret = (((uint32_t)get_slimr()) << 16) | ret;
return (intr_kind)ret;
}
int8_t get_phy_link() {
if (mdio_read(PHYRAR_BMSR) & BMSR_LINK_STATUS) return PHY_LINK_ON;
return PHY_LINK_OFF;
}
int8_t get_phy_power_mode() {
if (mdio_read(PHYRAR_BMCR) & BMCR_PWDN) return PHY_POWER_DOWN;
return PHY_POWER_NORM;
}
void reset_phy() {
mdio_write(PHYRAR_BMCR, mdio_read(PHYRAR_BMCR) | BMCR_RST);
while (mdio_read(PHYRAR_BMCR) & BMCR_RST);
}
void set_phy_conf(const phy_conf& phyconf) {
uint16_t tmp = mdio_read(PHYRAR_BMCR);
if (phyconf.mode == PHY_MODE_TE) {
set_phycr1(get_phycr1() | PHYCR1_TE);
set_phycr0(PHYCR0_AUTO);
} else {
set_phycr1(get_phycr1() & ~PHYCR1_TE);
if (phyconf.mode == PHY_MODE_AUTONEGO) {
tmp |= BMCR_ANE;
} else {
tmp &= ~(BMCR_ANE | BMCR_DPX | BMCR_SPD);
if (phyconf.duplex == PHY_DUPLEX_FULL) tmp |= BMCR_DPX;
if (phyconf.speed == PHY_SPEED_100) tmp |= BMCR_SPD;
}
mdio_write(PHYRAR_BMCR, tmp);
}
}
phy_conf get_phy_conf() {
phy_conf result;
uint16_t tmp = mdio_read(PHYRAR_BMCR);
result.mode = (get_phycr1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & BMCR_ANE) ? PHY_MODE_AUTONEGO : PHY_MODE_MANUAL);
result.duplex = (tmp & BMCR_DPX) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF;
result.speed = (tmp & BMCR_SPD) ? PHY_SPEED_100 : PHY_SPEED_10;
return result;
}
phy_conf get_phy_status() {
phy_conf result;
uint8_t tmp = get_physr();
result.mode = (get_phycr1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & (1 << 5)) ? PHY_MODE_MANUAL : PHY_MODE_AUTONEGO);
result.speed = (tmp & PHYSR_SPD) ? PHY_SPEED_10 : PHY_SPEED_100;
result.duplex = (tmp & PHYSR_DPX) ? PHY_DUPLEX_HALF : PHY_DUPLEX_FULL;
return result;
}
void set_phy_power_mode(uint8_t pmode) {
uint16_t tmp = mdio_read(PHYRAR_BMCR);
if (pmode == PHY_POWER_DOWN) tmp |= BMCR_PWDN;
else tmp &= ~BMCR_PWDN;
mdio_write(PHYRAR_BMCR, tmp);
}
void set_net_info(const net_info& p) {
set_shar(const_cast<uint8_t*>(p.mac.data())); set_gar(const_cast<uint8_t*>(p.gw.data()));
set_subr(const_cast<uint8_t*>(p.sn.data())); set_sipr(const_cast<uint8_t*>(p.ip.data()));
set_ga6r(const_cast<uint8_t*>(p.gw6.data())); set_sub6r(const_cast<uint8_t*>(p.sn6.data()));
set_llar(const_cast<uint8_t*>(p.lla.data())); set_guar(const_cast<uint8_t*>(p.gua.data()));
memcpy(dns_, p.dns.data(), 4);
memcpy(dns6_, p.dns6.data(), 16);
ipmode_ = p.ipmode;
}
net_info get_net_info() {
net_info p = {};
get_shar(p.mac.data()); get_gar(p.gw.data()); get_subr(p.sn.data()); get_sipr(p.ip.data());
get_ga6r(p.gw6.data()); get_sub6r(p.sn6.data()); get_llar(p.lla.data()); get_guar(p.gua.data());
memcpy(p.dns.data(), dns_, 4);
memcpy(p.dns6.data(), dns6_, 16);
p.ipmode = ipmode_;
return p;
}
void set_net_mode(netmode_type netmode) {
uint32_t tmp = (uint32_t)netmode;
set_netmr((uint8_t)tmp);
set_netmr2((uint8_t)(tmp >> 8));
set_net4mr((uint8_t)(tmp >> 16));
set_net6mr((uint8_t)(tmp >> 24));
}
netmode_type get_net_mode() {
uint32_t ret = get_netmr();
ret = (ret << 8) + get_netmr2();
ret = (ret << 16) + get_net4mr();
ret = (ret << 24) + get_net6mr();
return (netmode_type)ret;
}
void set_timeout(const net_timeout& t) {
set_rcr(t.s_retry_cnt); set_rtr(t.s_time_100us);
set_slrcr(t.sl_retry_cnt); set_slrtr(t.sl_time_100us);
}
net_timeout get_timeout() {
return {get_rcr(), get_rtr(), get_slrcr(), get_slrtr()};
}
int8_t send_arp(arp_request& arp) {
uint8_t tmp;
if (arp.destinfo.len == 16) { set_sldip6r(const_cast<uint8_t*>(arp.destinfo.ip.data())); set_slcr(SLCR_ARP6); }
else { set_sldip4r(const_cast<uint8_t*>(arp.destinfo.ip.data())); set_slcr(SLCR_ARP4); }
while (get_slcr());
while ((tmp = get_slir()) == 0x00);
set_slirclr(~SLIR_RA);
if (tmp & (SLIR_ARP4 | SLIR_ARP6)) { get_sldhar(arp.dha.data()); return 0; }
return -1;
}
int8_t send_ping(const ping_request& ping) {
uint8_t tmp;
set_pingidr(ping.id); set_pingseqr(ping.seq);
if (ping.destinfo.len == 16) { set_sldip6r(const_cast<uint8_t*>(ping.destinfo.ip.data())); set_slcr(SLCR_PING6); }
else { set_sldip4r(const_cast<uint8_t*>(ping.destinfo.ip.data())); set_slcr(SLCR_PING4); }
while (get_slcr());
while ((tmp = get_slir()) == 0x00);
set_slirclr(~SLIR_RA);
if (tmp & (SLIR_PING4 | SLIR_PING6)) return 0;
return -1;
}
int8_t send_dad(std::span<const uint8_t, 16> ipv6) {
uint8_t tmp;
set_sldip6r(const_cast<uint8_t*>(ipv6.data())); set_slcr(SLCR_NS);
while (get_slcr());
while ((tmp = get_slir()) == 0x00);
set_slirclr(~SLIR_RA);
if (tmp & SLIR_TOUT) return 0;
return -1;
}
int8_t send_slaac(prefix& pfx) {
uint8_t tmp;
set_slcr(SLCR_RS);
while (get_slcr());
while ((tmp = get_slir()) == 0x00);
set_slirclr(~SLIR_RA);
if (tmp & SLIR_RS) {
pfx.len = get_plr(); pfx.flag = get_pfr();
pfx.valid_lifetime = get_vltr(); pfx.preferred_lifetime = get_pltr();
get_par(pfx.prefix.data());
return 0;
}
return -1;
}
int8_t send_unsolicited() {
uint8_t tmp;
set_slcr(SLCR_UNA);
while (get_slcr());
while ((tmp = get_slir()) == 0x00);
set_slirclr(~SLIR_RA);
if (tmp & SLIR_TOUT) return 0;
return -1;
}
int8_t get_prefix(prefix& pfx) {
if (get_slir() & SLIR_RA) {
pfx.len = get_plr(); pfx.flag = get_pfr();
pfx.valid_lifetime = get_vltr(); pfx.preferred_lifetime = get_pltr();
get_par(pfx.prefix.data());
set_slirclr(SLIR_RA);
}
return -1;
}
constexpr uint16_t SOCK_ANY_PORT_NUM = 0xC000; constexpr uint16_t SOCK_ANY_PORT_NUM = 0xC000;
static uint16_t sock_any_port = SOCK_ANY_PORT_NUM; static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
@@ -1193,6 +985,20 @@ uint8_t sock_pack_info[sock_count] = {0,};
#define CHECK_SOCKDATA() do { if(len == 0) FAIL(data_len); } while(0) #define CHECK_SOCKDATA() do { if(len == 0) FAIL(data_len); } while(0)
#define CHECK_IPZERO(addr, addrlen) do { uint16_t ipzero=0; for(uint8_t i=0; i<addrlen; i++) ipzero += (uint16_t)addr[i]; if(ipzero == 0) FAIL(ip_invalid); } while(0) #define CHECK_IPZERO(addr, addrlen) do { uint16_t ipzero=0; for(uint8_t i=0; i<addrlen; i++) ipzero += (uint16_t)addr[i]; if(ipzero == 0) FAIL(ip_invalid); } while(0)
static std::expected<void, sock_error> close(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
CHECK_SOCKNUM();
set_sn_cr(sn, SN_CR_CLOSE);
while (get_sn_cr(sn));
set_sn_ir(sn, 0xFF);
sock_io_mode_bits &= ~(1 << sn);
sock_is_sending &= ~(1 << sn);
sock_remained_size[sn] = 0;
sock_pack_info[sn] = PACK_NONE;
while (get_sn_sr(sn) != SOCK_CLOSED);
return {};
}
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, port_num port, sock_flag flag) {
uint8_t sn = static_cast<uint8_t>(sid); uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port); uint16_t p = static_cast<uint16_t>(port);
@@ -1259,168 +1065,11 @@ std::expected<socket_id, sock_error> open_socket(socket_id sid, protocol proto,
sock_io_mode_bits |= ((fl & (static_cast<uint8_t>(sock_flag::io_nonblock) >> 3)) << sn); sock_io_mode_bits |= ((fl & (static_cast<uint8_t>(sock_flag::io_nonblock) >> 3)) << sn);
sock_is_sending &= ~(1 << sn); sock_is_sending &= ~(1 << sn);
sock_remained_size[sn] = 0; sock_remained_size[sn] = 0;
sock_pack_info[sn] = static_cast<uint8_t>(pack_info::completed); sock_pack_info[sn] = PACK_COMPLETED;
while (get_sn_sr(sn) == SOCK_CLOSED); while (get_sn_sr(sn) == SOCK_CLOSED);
return sid; return sid;
} }
std::expected<void, sock_error> close(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
CHECK_SOCKNUM();
set_sn_cr(sn, SN_CR_CLOSE);
while (get_sn_cr(sn));
set_sn_ir(sn, 0xFF);
sock_io_mode_bits &= ~(1 << sn);
sock_is_sending &= ~(1 << sn);
sock_remained_size[sn] = 0;
sock_pack_info[sn] = PACK_NONE;
while (get_sn_sr(sn) != SOCK_CLOSED);
return {};
}
std::expected<void, sock_error> listen(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
CHECK_SOCKNUM();
CHECK_TCPMODE();
CHECK_SOCKINIT();
set_sn_cr(sn, SN_CR_LISTEN);
while (get_sn_cr(sn));
while (get_sn_sr(sn) != SOCK_LISTEN) {
close(sid);
FAIL(sock_closed);
}
return {};
}
std::expected<void, sock_error> connect(socket_id sid, const ip_address& addr, port_num port) {
uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port);
uint8_t addrlen = addr.len;
CHECK_SOCKNUM();
CHECK_TCPMODE();
CHECK_SOCKINIT();
CHECK_IPZERO(addr.ip.data(), addrlen);
if (p == 0) FAIL(port_zero);
set_sn_dportr(sn, p);
if (addrlen == 16) {
if (get_sn_mr(sn) & 0x08) {
set_sn_dip6r(sn, const_cast<uint8_t*>(addr.ip.data()));
set_sn_cr(sn, SN_CR_CONNECT6);
} else {
FAIL(sock_mode);
}
} else {
if (get_sn_mr(sn) == SN_MR_TCP6) FAIL(sock_mode);
set_sn_dipr(sn, const_cast<uint8_t*>(addr.ip.data()));
set_sn_cr(sn, SN_CR_CONNECT);
}
while (get_sn_cr(sn));
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
while (get_sn_sr(sn) != SOCK_ESTABLISHED) {
if (get_sn_ir(sn) & SN_IR_TIMEOUT) {
set_sn_ir(sn, SN_IR_TIMEOUT);
FAIL(timeout);
}
if (get_sn_sr(sn) == SOCK_CLOSED) FAIL(sock_closed);
}
return {};
}
std::expected<void, sock_error> disconnect(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
CHECK_SOCKNUM();
CHECK_TCPMODE();
if (get_sn_sr(sn) != SOCK_CLOSED) {
set_sn_cr(sn, SN_CR_DISCON);
while (get_sn_cr(sn));
sock_is_sending &= ~(1 << sn);
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
while (get_sn_sr(sn) != SOCK_CLOSED) {
if (get_sn_ir(sn) & SN_IR_TIMEOUT) {
close(sid);
FAIL(timeout);
}
}
}
return {};
}
std::expected<uint16_t, sock_error> send(socket_id sid, std::span<const uint8_t> buf) {
uint8_t sn = static_cast<uint8_t>(sid);
uint8_t tmp = 0;
uint16_t freesize = 0;
uint16_t len = buf.size();
freesize = get_sn_tx_max(sn);
if (len > freesize) len = freesize;
while (1) {
freesize = (uint16_t)get_sn_tx_fsr(sn);
tmp = get_sn_sr(sn);
if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT)) {
if (tmp == SOCK_CLOSED) close(sid);
FAIL(sock_status);
}
if ((sock_io_mode_bits & (1 << sn)) && (len > freesize)) FAIL(busy);
if (len <= freesize) break;
}
send_data(sn, const_cast<uint8_t*>(buf.data()), len);
if (sock_is_sending & (1 << sn)) {
while (!(get_sn_ir(sn) & SN_IR_SENDOK)) {
tmp = get_sn_sr(sn);
if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT)) {
if ((tmp == SOCK_CLOSED) || (get_sn_ir(sn) & SN_IR_TIMEOUT)) close(sid);
FAIL(sock_status);
}
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
}
set_sn_ir(sn, SN_IR_SENDOK);
}
set_sn_cr(sn, SN_CR_SEND);
while (get_sn_cr(sn));
sock_is_sending |= (1 << sn);
return len;
}
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 tmp = 0;
uint16_t recvsize = 0;
CHECK_SOCKNUM();
CHECK_SOCKMODE(SN_MR_TCP);
CHECK_SOCKDATA();
recvsize = get_sn_rx_max(sn);
if (recvsize < len) len = recvsize;
while (1) {
recvsize = (uint16_t)get_sn_rx_rsr(sn);
tmp = get_sn_sr(sn);
if (tmp != SOCK_ESTABLISHED) {
if (tmp == SOCK_CLOSE_WAIT) {
if (recvsize != 0) break;
else if (get_sn_tx_fsr(sn) == get_sn_tx_max(sn)) {
close(sid);
FAIL(sock_status);
}
} else {
close(sid);
FAIL(sock_status);
}
}
if (recvsize != 0) break;
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
};
if (recvsize < len) len = recvsize;
recv_data(sn, buf.data(), len);
set_sn_cr(sn, SN_CR_RECV);
while (get_sn_cr(sn));
return len;
}
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> sendto(socket_id sid, std::span<const uint8_t> buf, const ip_address& addr, port_num port) {
uint8_t sn = static_cast<uint8_t>(sid); uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port); uint16_t p = static_cast<uint16_t>(port);
@@ -1592,27 +1241,6 @@ std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> b
} }
std::optional<uint16_t> peek_socket_msg(socket_id sid, std::span<const uint8_t> submsg) {
uint16_t subsize = submsg.size();
uint8_t sn = static_cast<uint8_t>(sid);
uint32_t rx_ptr = 0;
uint16_t i = 0, sub_idx = 0;
if ((get_sn_rx_rsr(sn) > 0) && (subsize > 0)) {
rx_ptr = ((uint32_t)get_sn_rx_rd(sn) << 8) + RXBUF_BLOCK(sn);
sub_idx = 0;
for (i = 0; i < get_sn_rx_rsr(sn); i++) {
if (reg_read(rx_ptr) == submsg[sub_idx]) {
sub_idx++;
if (sub_idx == subsize) return static_cast<uint16_t>(i + 1 - sub_idx);
} else {
sub_idx = 0;
}
rx_ptr = offset_inc(rx_ptr, 1);
}
}
return std::nullopt;
}
void reset() { void reset() {
gpio_init(PIN_RST); gpio_init(PIN_RST);
gpio_set_dir(PIN_RST, GPIO_OUT); gpio_set_dir(PIN_RST, GPIO_OUT);
@@ -1641,13 +1269,6 @@ bool check() {
return get_cidr() == 0x6300; return get_cidr() == 0x6300;
} }
void init_net(const net_info& info) {
chip_unlock();
net_unlock();
set_net_info(info);
}
std::expected<void, sock_error> set_socket_io_mode(socket_id sid, sock_io_mode mode) { std::expected<void, sock_error> set_socket_io_mode(socket_id sid, sock_io_mode mode) {
uint8_t sn = static_cast<uint8_t>(sid); uint8_t sn = static_cast<uint8_t>(sid);
if (sn >= sock_count) FAIL(sock_num); if (sn >= sock_count) FAIL(sock_num);
@@ -1657,161 +1278,12 @@ std::expected<void, sock_error> set_socket_io_mode(socket_id sid, sock_io_mode m
return {}; return {};
} }
sock_io_mode get_socket_io_mode(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
return static_cast<sock_io_mode>((sock_io_mode_bits >> sn) & 0x01);
}
uint16_t get_socket_max_tx(socket_id sid) {
return get_sn_tx_max(static_cast<uint8_t>(sid));
}
uint16_t get_socket_max_rx(socket_id sid) {
return get_sn_rx_max(static_cast<uint8_t>(sid));
}
std::expected<void, sock_error> clear_socket_interrupt(socket_id sid, uint8_t flags) {
uint8_t sn = static_cast<uint8_t>(sid);
if (sn >= sock_count) FAIL(sock_num);
if (flags > sik_all) FAIL(arg);
set_sn_ir(sn, flags);
return {};
}
uint8_t get_socket_interrupt(socket_id sid) {
return get_sn_ir(static_cast<uint8_t>(sid));
}
std::expected<void, sock_error> set_socket_interrupt_mask(socket_id sid, uint8_t mask) {
uint8_t sn = static_cast<uint8_t>(sid);
if (sn >= sock_count) FAIL(sock_num);
if (mask > sik_all) FAIL(arg);
set_sn_imr(sn, mask);
return {};
}
uint8_t get_socket_interrupt_mask(socket_id sid) {
return get_sn_imr(static_cast<uint8_t>(sid));
}
std::expected<void, sock_error> set_socket_prefer(socket_id sid, srcv6_prefer pref) {
uint8_t sn = static_cast<uint8_t>(sid);
if (sn >= sock_count) FAIL(sock_num);
uint8_t v = static_cast<uint8_t>(pref);
if ((v & 0x03) == 0x01) FAIL(arg);
set_sn_psr(sn, v);
return {};
}
srcv6_prefer get_socket_prefer(socket_id sid) {
return static_cast<srcv6_prefer>(get_sn_psr(static_cast<uint8_t>(sid)));
}
void set_socket_ttl(socket_id sid, uint8_t ttl) {
set_sn_ttl(static_cast<uint8_t>(sid), ttl);
}
uint8_t get_socket_ttl(socket_id sid) {
return get_sn_ttl(static_cast<uint8_t>(sid));
}
void set_socket_tos(socket_id sid, uint8_t tos) {
set_sn_tos(static_cast<uint8_t>(sid), tos);
}
uint8_t get_socket_tos(socket_id sid) {
return get_sn_tos(static_cast<uint8_t>(sid));
}
void set_socket_mss(socket_id sid, uint16_t mss) {
set_sn_mssr(static_cast<uint8_t>(sid), mss);
}
uint16_t get_socket_mss(socket_id sid) {
return get_sn_mssr(static_cast<uint8_t>(sid));
}
void set_socket_dest_mac(socket_id sid, const std::array<uint8_t, 6>& mac) { void set_socket_dest_mac(socket_id sid, const std::array<uint8_t, 6>& mac) {
set_sn_dhar(static_cast<uint8_t>(sid), const_cast<uint8_t*>(mac.data())); set_sn_dhar(static_cast<uint8_t>(sid), const_cast<uint8_t*>(mac.data()));
} }
void set_socket_dest_ip(socket_id sid, const ip_address& addr) {
uint8_t sn = static_cast<uint8_t>(sid);
if (addr.len == 16) set_sn_dip6r(sn, const_cast<uint8_t*>(addr.ip.data()));
else set_sn_dipr(sn, const_cast<uint8_t*>(addr.ip.data()));
}
ip_address get_socket_dest_ip(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
ip_address addr = {};
if (get_sn_esr(sn) & TCPSOCK_MODE) {
get_sn_dip6r(sn, addr.ip.data());
addr.len = 16;
} else {
get_sn_dipr(sn, addr.ip.data());
addr.len = 4;
}
return addr;
}
void set_socket_dest_port(socket_id sid, port_num port) {
set_sn_dportr(static_cast<uint8_t>(sid), static_cast<uint16_t>(port));
}
port_num get_socket_dest_port(socket_id sid) {
return static_cast<port_num>(get_sn_dportr(static_cast<uint8_t>(sid)));
}
std::expected<void, sock_error> send_keepalive(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
if ((get_sn_mr(sn) & 0x03) != 0x01) FAIL(sock_mode);
if (get_sn_kpalvtr(sn) != 0) FAIL(sock_opt);
set_sn_cr(sn, SN_CR_SEND_KEEP);
while (get_sn_cr(sn) != 0) {
if (get_sn_ir(sn) & SN_IR_TIMEOUT) {
set_sn_ir(sn, SN_IR_TIMEOUT);
FAIL(timeout);
}
}
return {};
}
void set_socket_keepalive_auto(socket_id sid, uint8_t interval) {
set_sn_kpalvtr(static_cast<uint8_t>(sid), interval);
}
uint8_t get_socket_keepalive_auto(socket_id sid) {
return get_sn_kpalvtr(static_cast<uint8_t>(sid));
}
uint16_t get_socket_send_buf(socket_id sid) {
return get_sn_tx_fsr(static_cast<uint8_t>(sid));
}
uint16_t get_socket_recv_buf(socket_id sid) { uint16_t get_socket_recv_buf(socket_id sid) {
return get_sn_rx_rsr(static_cast<uint8_t>(sid)); return get_sn_rx_rsr(static_cast<uint8_t>(sid));
} }
uint8_t get_socket_status(socket_id sid) {
return get_sn_sr(static_cast<uint8_t>(sid));
}
uint8_t get_socket_ext_status(socket_id sid) {
return get_sn_esr(static_cast<uint8_t>(sid)) & 0x07;
}
uint8_t get_socket_mode(socket_id sid) {
return get_sn_mr(static_cast<uint8_t>(sid)) & 0x0F;
}
uint16_t get_socket_remain_size(socket_id sid) {
uint8_t sn = static_cast<uint8_t>(sid);
if (get_sn_mr(sn) & 0x01) return get_sn_rx_rsr(sn);
return sock_remained_size[sn];
}
pack_info get_socket_pack_info(socket_id sid) {
return static_cast<pack_info>(sock_pack_info[static_cast<uint8_t>(sid)]);
}
} // namespace w6300 } // namespace w6300

View File

@@ -2,7 +2,6 @@
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <expected> #include <expected>
#include <optional>
#include <span> #include <span>
namespace w6300 { namespace w6300 {
@@ -65,35 +64,6 @@ constexpr uint8_t operator&(sock_flag a, sock_flag b) {
return static_cast<uint8_t>(a) & static_cast<uint8_t>(b); return static_cast<uint8_t>(a) & static_cast<uint8_t>(b);
} }
enum class pack_info : uint8_t {
none = 0x00,
first = 1 << 1,
remained = 1 << 2,
completed = 1 << 3,
ipv6_lla = (1 << 7) | (1 << 4),
ipv6_multi = (1 << 7) | (1 << 5),
ipv6_allnode = (1 << 7) | (1 << 6),
ipv6 = 1 << 7,
};
constexpr pack_info operator|(pack_info a, pack_info b) {
return static_cast<pack_info>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
}
constexpr uint8_t operator&(pack_info a, pack_info b) {
return static_cast<uint8_t>(a) & static_cast<uint8_t>(b);
}
enum class srcv6_prefer : uint8_t {
auto_select = 0x00,
lla = 0x02,
gua = 0x03,
};
enum class tcp_sock_info : uint8_t {
mode = 1 << 2,
op = 1 << 1,
sip = 1 << 0,
};
enum class sock_io_mode : uint8_t { enum class sock_io_mode : uint8_t {
block = 0, block = 0,
nonblock = 1, nonblock = 1,
@@ -111,158 +81,26 @@ enum intr_kind : uint32_t {
ik_int_all = 0x00FFFF97 ik_int_all = 0x00FFFF97
}; };
struct phy_conf {
uint8_t by;
uint8_t mode;
uint8_t speed;
uint8_t duplex;
};
enum ipconf_mode : uint8_t {
ipconf_none = 0x00, ipconf_static_v4 = 0x01, ipconf_static_v6 = 0x02,
ipconf_static_all = 0x03, ipconf_slaac_v6 = 0x04,
ipconf_dhcp_v4 = 0x10, ipconf_dhcp_v6 = 0x20, ipconf_dhcp_all = 0x30
};
enum dhcp_mode : uint8_t { dhcp_static = 1, dhcp_dynamic };
struct net_info {
std::array<uint8_t, 6> mac;
std::array<uint8_t, 4> ip;
std::array<uint8_t, 4> sn;
std::array<uint8_t, 4> gw;
std::array<uint8_t, 16> lla;
std::array<uint8_t, 16> gua;
std::array<uint8_t, 16> sn6;
std::array<uint8_t, 16> gw6;
std::array<uint8_t, 4> dns;
std::array<uint8_t, 16> dns6;
ipconf_mode ipmode;
dhcp_mode dhcp;
};
enum netmode_type : uint32_t {
nm_ipb_v4 = (1 << 0), nm_ipb_v6 = (1 << 1), nm_wol = (1 << 2),
nm_pb6_multi = (1 << 4), nm_pb6_allnode = (1 << 5), nm_mr_mask = 0x37,
nm_pppoe = (1 << 8), nm_dha_select = (1 << 15), nm_mr2_mask = (0x09 << 8),
nm_pb4_all = (1 << 16), nm_trstb_v4 = (1 << 17), nm_parp_v4 = (1 << 18),
nm_unrb_v4 = (1 << 19), nm_net4_mask = (0x0F << 16),
nm_pb6_all = (1 << 24), nm_trstb_v6 = (1 << 25), nm_parp_v6 = (1 << 26),
nm_unrb_v6 = (1 << 27), nm_net6_mask = (0x0F << 24),
nm_mask_all = 0x0F0F0937
};
struct net_timeout {
uint8_t s_retry_cnt;
uint16_t s_time_100us;
uint8_t sl_retry_cnt;
uint16_t sl_time_100us;
};
struct ip_address { struct ip_address {
std::array<uint8_t, 16> ip; std::array<uint8_t, 16> ip;
uint8_t len; uint8_t len;
}; };
struct prefix {
uint8_t len;
uint8_t flag;
uint32_t valid_lifetime;
uint32_t preferred_lifetime;
std::array<uint8_t, 16> prefix;
};
struct arp_request {
ip_address destinfo;
std::array<uint8_t, 6> dha;
};
struct ping_request {
uint16_t id;
uint16_t seq;
ip_address destinfo;
};
void init_spi(); void init_spi();
void init_critical_section(); void init_critical_section();
void reset(); void reset();
void init(); void init();
bool check(); bool check();
void init_net(const net_info& info);
void soft_reset();
int8_t init_buffers(std::span<const uint8_t> txsize, std::span<const uint8_t> rxsize);
void clear_interrupt(intr_kind intr); void clear_interrupt(intr_kind intr);
intr_kind get_interrupt();
void set_interrupt_mask(intr_kind intr); void set_interrupt_mask(intr_kind intr);
intr_kind get_interrupt_mask();
int8_t get_phy_link();
int8_t get_phy_power_mode();
void reset_phy();
void set_phy_conf(const phy_conf& conf);
phy_conf get_phy_conf();
phy_conf get_phy_status();
void set_phy_power_mode(uint8_t pmode);
void set_net_info(const net_info& info);
net_info get_net_info();
void set_net_mode(netmode_type mode);
netmode_type get_net_mode();
void set_timeout(const net_timeout& timeout);
net_timeout get_timeout();
int8_t send_arp(arp_request& arp);
int8_t send_ping(const ping_request& ping);
int8_t send_dad(std::span<const uint8_t, 16> ipv6);
int8_t send_slaac(prefix& pfx);
int8_t send_unsolicited();
int8_t get_prefix(prefix& pfx);
std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag); std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag);
std::expected<void, sock_error> close(socket_id sn);
std::expected<void, sock_error> listen(socket_id sn);
std::expected<void, sock_error> disconnect(socket_id sn);
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> connect(socket_id sn, const ip_address& addr, port_num port);
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> 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<uint16_t, sock_error> recvfrom(socket_id sn, std::span<uint8_t> buf, ip_address& addr, port_num& port);
std::expected<void, sock_error> set_socket_io_mode(socket_id sn, sock_io_mode mode); std::expected<void, sock_error> set_socket_io_mode(socket_id sn, sock_io_mode mode);
sock_io_mode get_socket_io_mode(socket_id sn);
uint16_t get_socket_max_tx(socket_id sn);
uint16_t get_socket_max_rx(socket_id sn);
std::expected<void, sock_error> clear_socket_interrupt(socket_id sn, uint8_t flags);
uint8_t get_socket_interrupt(socket_id sn);
std::expected<void, sock_error> set_socket_interrupt_mask(socket_id sn, uint8_t mask);
uint8_t get_socket_interrupt_mask(socket_id sn);
std::expected<void, sock_error> set_socket_prefer(socket_id sn, srcv6_prefer pref);
srcv6_prefer get_socket_prefer(socket_id sn);
void set_socket_ttl(socket_id sn, uint8_t ttl);
uint8_t get_socket_ttl(socket_id sn);
void set_socket_tos(socket_id sn, uint8_t tos);
uint8_t get_socket_tos(socket_id sn);
void set_socket_mss(socket_id sn, uint16_t mss);
uint16_t get_socket_mss(socket_id sn);
void set_socket_dest_mac(socket_id sn, const std::array<uint8_t, 6>& mac); void set_socket_dest_mac(socket_id sn, const std::array<uint8_t, 6>& mac);
void set_socket_dest_ip(socket_id sn, const ip_address& addr);
ip_address get_socket_dest_ip(socket_id sn);
void set_socket_dest_port(socket_id sn, port_num port);
port_num get_socket_dest_port(socket_id sn);
std::expected<void, sock_error> send_keepalive(socket_id sn);
void set_socket_keepalive_auto(socket_id sn, uint8_t interval);
uint8_t get_socket_keepalive_auto(socket_id sn);
uint16_t get_socket_send_buf(socket_id sn);
uint16_t get_socket_recv_buf(socket_id sn); uint16_t get_socket_recv_buf(socket_id sn);
uint8_t get_socket_status(socket_id sn);
uint8_t get_socket_ext_status(socket_id sn);
uint8_t get_socket_mode(socket_id sn);
uint16_t get_socket_remain_size(socket_id sn);
pack_info get_socket_pack_info(socket_id sn);
std::optional<uint16_t> peek_socket_msg(socket_id sn, std::span<const uint8_t> submsg);
} // namespace w6300 } // namespace w6300