From ef7baee973c1fdb05f433d5c2c79f51a6ac6be04 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 5 Apr 2026 07:01:43 +0900 Subject: [PATCH] Rename W6300 public API: drop wiz prefix, snake_case, verb-first functions, namespace w6300 --- picomap.cpp | 38 +++++------ w6300/w6300.cpp | 165 ++++++++++++++++++++++++------------------------ w6300/w6300.h | 123 ++++++++++++++++++------------------ 3 files changed, 166 insertions(+), 160 deletions(-) diff --git a/picomap.cpp b/picomap.cpp index 42be308..2801fc2 100644 --- a/picomap.cpp +++ b/picomap.cpp @@ -14,22 +14,22 @@ static void send_bytes(const std::vector &data) { } static bool w6300_init() { - wizchip_spi_initialize(); - wizchip_cris_initialize(); - wizchip_reset(); - wizchip_initialize(); - if (!wizchip_check()) return false; + w6300::init_spi(); + w6300::init_critical_section(); + w6300::reset(); + w6300::init(); + if (!w6300::check()) return false; pico_unique_board_id_t uid; pico_get_unique_board_id(&uid); - wiz_NetInfo net_info = {}; - net_info.mac[0] = (uid.id[0] & 0xFC) | 0x02; - net_info.mac[1] = uid.id[1]; - net_info.mac[2] = uid.id[2]; - net_info.mac[3] = uid.id[3]; - net_info.mac[4] = uid.id[4]; - net_info.mac[5] = uid.id[5]; - network_initialize(net_info); + w6300::net_info info = {}; + info.mac[0] = (uid.id[0] & 0xFC) | 0x02; + info.mac[1] = uid.id[1]; + info.mac[2] = uid.id[2]; + info.mac[3] = uid.id[3]; + info.mac[4] = uid.id[4]; + info.mac[5] = uid.id[5]; + w6300::init_net(info); return true; } @@ -65,14 +65,14 @@ int main() { reset_usb_boot(0, 1); break; case RequestInfo::ext_id: { - ResponseInfo info; + ResponseInfo resp; pico_unique_board_id_t uid; pico_get_unique_board_id(&uid); - std::copy(uid.id, uid.id + 8, info.board_id.begin()); - wiz_NetInfo net_info; - wizchip_getnetinfo(&net_info); - std::copy(net_info.mac, net_info.mac + 6, info.mac.begin()); - send_bytes(encode_response(msg->message_id, info)); + std::copy(uid.id, uid.id + 8, resp.board_id.begin()); + w6300::net_info info; + w6300::get_net_info(&info); + std::copy(info.mac, info.mac + 6, resp.mac.begin()); + send_bytes(encode_response(msg->message_id, resp)); break; } } diff --git a/w6300/w6300.cpp b/w6300/w6300.cpp index ad1f677..97947ce 100644 --- a/w6300/w6300.cpp +++ b/w6300/w6300.cpp @@ -9,6 +9,7 @@ #include "w6300.h" #include "qspi.pio.h" +namespace w6300 { namespace { #define PIO_PROGRAM_NAME wizchip_pio_spi_quad_write_read @@ -226,7 +227,7 @@ void wizchip_pio_write(uint8_t opcode, uint16_t addr, uint8_t* buf, uint16_t len constexpr int _WIZCHIP_ = 6300; constexpr uint8_t _WIZCHIP_QSPI_MODE_ = 0x02 << 6; -constexpr int _WIZCHIP_SOCK_NUM_ = WIZCHIP_SOCK_NUM; +constexpr int _SOCK_COUNT_ = SOCK_COUNT; constexpr uint16_t _PHY_IO_MODE_PHYCR_ = 0x0000; constexpr uint16_t _PHY_IO_MODE_MII_ = 0x0010; @@ -904,7 +905,7 @@ static ipconf_mode ipmode_; static constexpr char WIZCHIP_ID[] = "W6300"; -int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) { +int8_t ctl_chip(chip_ctl cwtype, void* arg) { uint8_t tmp = *(uint8_t*)arg; uint8_t* ptmp[2] = {0, 0}; switch (cwtype) { @@ -922,25 +923,25 @@ int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) { *(uint8_t*)arg = getSYSR() >> 5; break; case CW_RESET_WIZCHIP: - wizchip_sw_reset(); + soft_reset(); break; case CW_INIT_WIZCHIP: if (arg) { ptmp[0] = (uint8_t*)arg; - ptmp[1] = ptmp[0] + WIZCHIP_SOCK_NUM; + ptmp[1] = ptmp[0] + SOCK_COUNT; } - return wizchip_init(ptmp[0], ptmp[1]); + return init_buffers(ptmp[0], ptmp[1]); case CW_CLR_INTERRUPT: - wizchip_clrinterrupt(*((intr_kind*)arg)); + clear_interrupt(*((intr_kind*)arg)); break; case CW_GET_INTERRUPT: - *((intr_kind*)arg) = wizchip_getinterrupt(); + *((intr_kind*)arg) = get_interrupt(); break; case CW_SET_INTRMASK: - wizchip_setinterruptmask(*((intr_kind*)arg)); + set_interrupt_mask(*((intr_kind*)arg)); break; case CW_GET_INTRMASK: - *((intr_kind*)arg) = wizchip_getinterruptmask(); + *((intr_kind*)arg) = get_interrupt_mask(); break; case CW_SET_INTRTIME: setINTPTMR(*(uint16_t*)arg); @@ -955,26 +956,26 @@ int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) { *(uint16_t*)arg = getVER(); break; case CW_RESET_PHY: - wizphy_reset(); + reset_phy(); break; case CW_SET_PHYCONF: - wizphy_setphyconf((wiz_PhyConf*)arg); + set_phy_conf((phy_conf*)arg); break; case CW_GET_PHYCONF: - wizphy_getphyconf((wiz_PhyConf*)arg); + get_phy_conf((phy_conf*)arg); break; case CW_GET_PHYSTATUS: break; case CW_SET_PHYPOWMODE: - wizphy_setphypmode(*(uint8_t*)arg); + set_phy_power_mode(*(uint8_t*)arg); break; case CW_GET_PHYPOWMODE: - tmp = wizphy_getphypmode(); + tmp = get_phy_power_mode(); if ((int8_t)tmp == -1) return -1; *(uint8_t*)arg = tmp; break; case CW_GET_PHYLINK: - tmp = wizphy_getphylink(); + tmp = get_phy_link(); if ((int8_t)tmp == -1) return -1; *(uint8_t*)arg = tmp; break; @@ -984,14 +985,14 @@ int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) { return 0; } -int8_t ctlnetwork(ctlnetwork_type cntype, void* arg) { +int8_t ctl_net(net_ctl cntype, void* arg) { switch (cntype) { - case CN_SET_NETINFO: wizchip_setnetinfo((wiz_NetInfo*)arg); break; - case CN_GET_NETINFO: wizchip_getnetinfo((wiz_NetInfo*)arg); break; - case CN_SET_NETMODE: wizchip_setnetmode(*(netmode_type*)arg); break; - case CN_GET_NETMODE: *(netmode_type*)arg = wizchip_getnetmode(); break; - case CN_SET_TIMEOUT: wizchip_settimeout((wiz_NetTimeout*)arg); break; - case CN_GET_TIMEOUT: wizchip_gettimeout((wiz_NetTimeout*)arg); break; + case CN_SET_NETINFO: set_net_info((net_info*)arg); break; + case CN_GET_NETINFO: get_net_info((net_info*)arg); break; + case CN_SET_NETMODE: set_net_mode(*(netmode_type*)arg); break; + case CN_GET_NETMODE: *(netmode_type*)arg = get_net_mode(); break; + case CN_SET_TIMEOUT: set_timeout((net_timeout*)arg); break; + case CN_GET_TIMEOUT: get_timeout((net_timeout*)arg); break; case CN_SET_PREFER: setSLPSR(*(uint8_t*)arg); break; case CN_GET_PREFER: *(uint8_t*)arg = getSLPSR(); break; default: return -1; @@ -999,19 +1000,19 @@ int8_t ctlnetwork(ctlnetwork_type cntype, void* arg) { return 0; } -int8_t ctlnetservice(ctlnetservice_type cnstype, void* arg) { +int8_t ctl_net_service(net_service_ctl cnstype, void* arg) { switch (cnstype) { - case CNS_ARP: return wizchip_arp((wiz_ARP*)arg); - case CNS_PING: return wizchip_ping((wiz_PING*)arg); - case CNS_DAD: return wizchip_dad((uint8_t*)arg); - case CNS_SLAAC: return wizchip_slaac((wiz_Prefix*)arg); - case CNS_UNSOL_NA: return wizchip_unsolicited(); - case CNS_GET_PREFIX: return wizchip_getprefix((wiz_Prefix*)arg); + case CNS_ARP: return send_arp((arp_request*)arg); + case CNS_PING: return send_ping((ping_request*)arg); + case CNS_DAD: return send_dad((uint8_t*)arg); + case CNS_SLAAC: return send_slaac((prefix*)arg); + case CNS_UNSOL_NA: return send_unsolicited(); + case CNS_GET_PREFIX: return get_prefix((prefix*)arg); default: return -1; } } -void wizchip_sw_reset() { +void soft_reset() { uint8_t gw[4], sn[4], sip[4], mac[6]; uint8_t gw6[16], sn6[16], lla[16], gua[16]; uint8_t islock = getSYSR(); @@ -1030,71 +1031,71 @@ void wizchip_sw_reset() { if (islock & SYSR_NETL) NETLOCK(); } -int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize) { - wizchip_sw_reset(); +int8_t init_buffers(uint8_t* txsize, uint8_t* rxsize) { + soft_reset(); if (txsize) { int8_t tmp = 0; - for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) { + for (int i = 0; i < SOCK_COUNT; i++) { tmp += txsize[i]; if (tmp > 32) return -1; } - for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) setSn_TXBUF_SIZE(i, txsize[i]); + for (int i = 0; i < SOCK_COUNT; i++) setSn_TXBUF_SIZE(i, txsize[i]); } if (rxsize) { int8_t tmp = 0; - for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) { + for (int i = 0; i < SOCK_COUNT; i++) { tmp += rxsize[i]; if (tmp > 32) return -1; } - for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) setSn_RXBUF_SIZE(i, rxsize[i]); + for (int i = 0; i < SOCK_COUNT; i++) setSn_RXBUF_SIZE(i, rxsize[i]); } return 0; } -void wizchip_clrinterrupt(intr_kind intr) { +void clear_interrupt(intr_kind intr) { setIRCLR((uint8_t)intr); uint8_t sir = (uint8_t)((uint16_t)intr >> 8); - for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) + for (int i = 0; i < SOCK_COUNT; i++) if (sir & (1 << i)) setSn_IRCLR(i, 0xFF); setSLIRCLR((uint8_t)((uint32_t)intr >> 16)); } -intr_kind wizchip_getinterrupt() { +intr_kind get_interrupt() { uint32_t ret = getSIR(); ret = (ret << 8) + getIR(); ret = (((uint32_t)getSLIR()) << 16) | ret; return (intr_kind)ret; } -void wizchip_setinterruptmask(intr_kind intr) { +void set_interrupt_mask(intr_kind intr) { setIMR((uint8_t)intr); setSIMR((uint8_t)((uint16_t)intr >> 8)); setSLIMR((uint8_t)((uint32_t)intr >> 16)); } -intr_kind wizchip_getinterruptmask() { +intr_kind get_interrupt_mask() { uint32_t ret = getSIMR(); ret = (ret << 8) + getIMR(); ret = (((uint32_t)getSLIMR()) << 16) | ret; return (intr_kind)ret; } -int8_t wizphy_getphylink() { +int8_t get_phy_link() { if (wiz_mdio_read(PHYRAR_BMSR) & BMSR_LINK_STATUS) return PHY_LINK_ON; return PHY_LINK_OFF; } -int8_t wizphy_getphypmode() { +int8_t get_phy_power_mode() { if (wiz_mdio_read(PHYRAR_BMCR) & BMCR_PWDN) return PHY_POWER_DOWN; return PHY_POWER_NORM; } -void wizphy_reset() { +void reset_phy() { wiz_mdio_write(PHYRAR_BMCR, wiz_mdio_read(PHYRAR_BMCR) | BMCR_RST); while (wiz_mdio_read(PHYRAR_BMCR) & BMCR_RST); } -void wizphy_setphyconf(wiz_PhyConf* phyconf) { +void set_phy_conf(phy_conf* phyconf) { uint16_t tmp = wiz_mdio_read(PHYRAR_BMCR); if (phyconf->mode == PHY_MODE_TE) { setPHYCR1(getPHYCR1() | PHYCR1_TE); @@ -1112,28 +1113,28 @@ void wizphy_setphyconf(wiz_PhyConf* phyconf) { } } -void wizphy_getphyconf(wiz_PhyConf* phyconf) { +void get_phy_conf(phy_conf* phyconf) { uint16_t tmp = wiz_mdio_read(PHYRAR_BMCR); phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & BMCR_ANE) ? PHY_MODE_AUTONEGO : PHY_MODE_MANUAL); phyconf->duplex = (tmp & BMCR_DPX) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF; phyconf->speed = (tmp & BMCR_SPD) ? PHY_SPEED_100 : PHY_SPEED_10; } -void wizphy_getphystat(wiz_PhyConf* phyconf) { +void get_phy_status(phy_conf* phyconf) { uint8_t tmp = getPHYSR(); phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & (1 << 5)) ? PHY_MODE_MANUAL : PHY_MODE_AUTONEGO); phyconf->speed = (tmp & PHYSR_SPD) ? PHY_SPEED_10 : PHY_SPEED_100; phyconf->duplex = (tmp & PHYSR_DPX) ? PHY_DUPLEX_HALF : PHY_DUPLEX_FULL; } -void wizphy_setphypmode(uint8_t pmode) { +void set_phy_power_mode(uint8_t pmode) { uint16_t tmp = wiz_mdio_read(PHYRAR_BMCR); if (pmode == PHY_POWER_DOWN) tmp |= BMCR_PWDN; else tmp &= ~BMCR_PWDN; wiz_mdio_write(PHYRAR_BMCR, tmp); } -void wizchip_setnetinfo(wiz_NetInfo* p) { +void set_net_info(net_info* p) { setSHAR(p->mac); setGAR(p->gw); setSUBR(p->sn); setSIPR(p->ip); setGA6R(p->gw6); setSUB6R(p->sn6); setLLAR(p->lla); setGUAR(p->gua); memcpy(dns_, p->dns, 4); @@ -1141,7 +1142,7 @@ void wizchip_setnetinfo(wiz_NetInfo* p) { ipmode_ = p->ipmode; } -void wizchip_getnetinfo(wiz_NetInfo* p) { +void get_net_info(net_info* p) { getSHAR(p->mac); getGAR(p->gw); getSUBR(p->sn); getSIPR(p->ip); getGA6R(p->gw6); getSUB6R(p->sn6); getLLAR(p->lla); getGUAR(p->gua); memcpy(p->dns, dns_, 4); @@ -1149,7 +1150,7 @@ void wizchip_getnetinfo(wiz_NetInfo* p) { p->ipmode = ipmode_; } -void wizchip_setnetmode(netmode_type netmode) { +void set_net_mode(netmode_type netmode) { uint32_t tmp = (uint32_t)netmode; setNETMR((uint8_t)tmp); setNETMR2((uint8_t)(tmp >> 8)); @@ -1157,7 +1158,7 @@ void wizchip_setnetmode(netmode_type netmode) { setNET6MR((uint8_t)(tmp >> 24)); } -netmode_type wizchip_getnetmode() { +netmode_type get_net_mode() { uint32_t ret = getNETMR(); ret = (ret << 8) + getNETMR2(); ret = (ret << 16) + getNET4MR(); @@ -1165,17 +1166,17 @@ netmode_type wizchip_getnetmode() { return (netmode_type)ret; } -void wizchip_settimeout(wiz_NetTimeout* t) { +void set_timeout(net_timeout* t) { setRCR(t->s_retry_cnt); setRTR(t->s_time_100us); setSLRCR(t->sl_retry_cnt); setSLRTR(t->sl_time_100us); } -void wizchip_gettimeout(wiz_NetTimeout* t) { +void get_timeout(net_timeout* t) { t->s_retry_cnt = getRCR(); t->s_time_100us = getRTR(); t->sl_retry_cnt = getSLRCR(); t->sl_time_100us = getSLRTR(); } -int8_t wizchip_arp(wiz_ARP* arp) { +int8_t send_arp(arp_request* arp) { uint8_t tmp; if (arp->destinfo.len == 16) { setSLDIP6R(arp->destinfo.ip); setSLCR(SLCR_ARP6); } else { setSLDIP4R(arp->destinfo.ip); setSLCR(SLCR_ARP4); } @@ -1186,7 +1187,7 @@ int8_t wizchip_arp(wiz_ARP* arp) { return -1; } -int8_t wizchip_ping(wiz_PING* ping) { +int8_t send_ping(ping_request* ping) { uint8_t tmp; setPINGIDR(ping->id); setPINGSEQR(ping->seq); if (ping->destinfo.len == 16) { setSLDIP6R(ping->destinfo.ip); setSLCR(SLCR_PING6); } @@ -1198,7 +1199,7 @@ int8_t wizchip_ping(wiz_PING* ping) { return -1; } -int8_t wizchip_dad(uint8_t* ipv6) { +int8_t send_dad(uint8_t* ipv6) { uint8_t tmp; setSLDIP6R(ipv6); setSLCR(SLCR_NS); while (getSLCR()); @@ -1208,7 +1209,7 @@ int8_t wizchip_dad(uint8_t* ipv6) { return -1; } -int8_t wizchip_slaac(wiz_Prefix* prefix) { +int8_t send_slaac(prefix* prefix) { uint8_t tmp; setSLCR(SLCR_RS); while (getSLCR()); @@ -1223,7 +1224,7 @@ int8_t wizchip_slaac(wiz_Prefix* prefix) { return -1; } -int8_t wizchip_unsolicited() { +int8_t send_unsolicited() { uint8_t tmp; setSLCR(SLCR_UNA); while (getSLCR()); @@ -1233,7 +1234,7 @@ int8_t wizchip_unsolicited() { return -1; } -int8_t wizchip_getprefix(wiz_Prefix* prefix) { +int8_t get_prefix(prefix* prefix) { if (getSLIR() & SLIR_RA) { prefix->len = getPLR(); prefix->flag = getPFR(); prefix->valid_lifetime = getVLTR(); prefix->preferred_lifetime = getPLTR(); @@ -1247,10 +1248,10 @@ constexpr uint16_t SOCK_ANY_PORT_NUM = 0xC000; static uint16_t sock_any_port = SOCK_ANY_PORT_NUM; static uint16_t sock_io_mode = 0; static uint16_t sock_is_sending = 0; -static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,}; -uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,}; +static uint16_t sock_remained_size[_SOCK_COUNT_] = {0,}; +uint8_t sock_pack_info[_SOCK_COUNT_] = {0,}; -#define CHECK_SOCKNUM() do { if(sn >= _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM; } while(0) +#define CHECK_SOCKNUM() do { if(sn >= _SOCK_COUNT_) return SOCKERR_SOCKNUM; } while(0) #define CHECK_SOCKMODE(mode) do { if((getSn_MR(sn) & 0x0F) != mode) return SOCKERR_SOCKMODE; } while(0) #define CHECK_TCPMODE() do { if((getSn_MR(sn) & 0x03) != 0x01) return SOCKERR_SOCKMODE; } while(0) #define CHECK_UDPMODE() do { if((getSn_MR(sn) & 0x03) != 0x02) return SOCKERR_SOCKMODE; } while(0) @@ -1641,7 +1642,7 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16 return (int32_t)pack_len; } -int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg) { +int8_t ctl_socket(uint8_t sn, sock_ctl cstype, void* arg) { uint8_t tmp = 0; CHECK_SOCKNUM(); tmp = *((uint8_t*)arg); @@ -1687,7 +1688,7 @@ int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg) { return SOCK_OK; } -int8_t setsockopt(uint8_t sn, sockopt_type sotype, void* arg) { +int8_t set_sockopt(uint8_t sn, sockopt_type sotype, void* arg) { CHECK_SOCKNUM(); switch (sotype) { case SO_TTL: @@ -1700,7 +1701,7 @@ int8_t setsockopt(uint8_t sn, sockopt_type sotype, void* arg) { setSn_MSSR(sn, *(uint16_t*)arg); break; case SO_DESTIP: - if (((wiz_IPAddress *)arg)->len == 16) setSn_DIP6R(sn, ((wiz_IPAddress*)arg)->ip); + if (((ip_address *)arg)->len == 16) setSn_DIP6R(sn, ((ip_address*)arg)->ip); else setSn_DIPR(sn, (uint8_t*)arg); break; case SO_DESTPORT: @@ -1727,7 +1728,7 @@ int8_t setsockopt(uint8_t sn, sockopt_type sotype, void* arg) { return SOCK_OK; } -int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg) { +int8_t get_sockopt(uint8_t sn, sockopt_type sotype, void* arg) { CHECK_SOCKNUM(); switch (sotype) { case SO_FLAG: @@ -1745,11 +1746,11 @@ int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg) { case SO_DESTIP: CHECK_TCPMODE(); if (getSn_ESR(sn) & TCPSOCK_MODE) { - getSn_DIP6R(sn, ((wiz_IPAddress*)arg)->ip); - ((wiz_IPAddress*)arg)->len = 16; + getSn_DIP6R(sn, ((ip_address*)arg)->ip); + ((ip_address*)arg)->len = 16; } else { - getSn_DIPR(sn, ((wiz_IPAddress*)arg)->ip); - ((wiz_IPAddress*)arg)->len = 4; + getSn_DIPR(sn, ((ip_address*)arg)->ip); + ((ip_address*)arg)->len = 4; } break; case SO_DESTPORT: @@ -1791,7 +1792,7 @@ int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg) { return SOCK_OK; } -int16_t peeksockmsg(uint8_t sn, uint8_t* submsg, uint16_t subsize) { +int16_t peek_socket_msg(uint8_t sn, uint8_t* submsg, uint16_t subsize) { uint32_t rx_ptr = 0; uint16_t i = 0, sub_idx = 0; if ((getSn_RX_RSR(sn) > 0) && (subsize > 0)) { @@ -1810,7 +1811,7 @@ int16_t peeksockmsg(uint8_t sn, uint8_t* submsg, uint16_t subsize) { return -1; } -void wizchip_reset() { +void reset() { gpio_init(PIN_RST); gpio_set_dir(PIN_RST, GPIO_OUT); gpio_put(PIN_RST, 0); @@ -1819,26 +1820,28 @@ void wizchip_reset() { sleep_ms(100); } -void wizchip_spi_initialize() { +void init_spi() { wizchip_pio_init(); } -void wizchip_cris_initialize() { +void init_critical_section() { critical_section_init(&g_cris_sec); } -void wizchip_initialize() { +void init() { wizchip_pio_frame_end(); uint8_t memsize[2][8] = {{4, 4, 4, 4, 4, 4, 4, 4}, {4, 4, 4, 4, 4, 4, 4, 4}}; - ctlwizchip(CW_INIT_WIZCHIP, (void *)memsize); + ctl_chip(CW_INIT_WIZCHIP, (void *)memsize); } -bool wizchip_check() { +bool check() { return getCIDR() == 0x6300; } -void network_initialize(wiz_NetInfo net_info) { +void init_net(net_info net_info) { uint8_t syslock = SYS_NET_LOCK; - ctlwizchip(CW_SYS_UNLOCK, &syslock); - ctlnetwork(CN_SET_NETINFO, (void *)&net_info); + ctl_chip(CW_SYS_UNLOCK, &syslock); + ctl_net(CN_SET_NETINFO, (void *)&net_info); } + +} // namespace w6300 diff --git a/w6300/w6300.h b/w6300/w6300.h index 3f11bb1..24a9fa7 100644 --- a/w6300/w6300.h +++ b/w6300/w6300.h @@ -1,9 +1,11 @@ #pragma once #include -constexpr int WIZCHIP_SOCK_NUM = 8; +namespace w6300 { -enum ctlwizchip_type { +constexpr int SOCK_COUNT = 8; + +enum chip_ctl { CW_SYS_LOCK, CW_SYS_UNLOCK, CW_GET_SYSLOCK, CW_RESET_WIZCHIP, CW_INIT_WIZCHIP, CW_GET_INTERRUPT, CW_CLR_INTERRUPT, CW_SET_INTRMASK, CW_GET_INTRMASK, @@ -13,12 +15,12 @@ enum ctlwizchip_type { CW_SET_PHYPOWMODE, CW_GET_PHYPOWMODE, CW_GET_PHYLINK }; -enum ctlnetwork_type { +enum net_ctl { CN_SET_NETINFO, CN_GET_NETINFO, CN_SET_NETMODE, CN_GET_NETMODE, CN_SET_TIMEOUT, CN_GET_TIMEOUT, CN_SET_PREFER, CN_GET_PREFER, }; -enum ctlnetservice_type { +enum net_service_ctl { CNS_ARP, CNS_PING, CNS_DAD, CNS_SLAAC, CNS_UNSOL_NA, CNS_GET_PREFIX }; @@ -52,7 +54,7 @@ constexpr uint8_t PHY_LINK_ON = 1; constexpr uint8_t PHY_POWER_NORM = 0; constexpr uint8_t PHY_POWER_DOWN = 1; -struct wiz_PhyConf { +struct phy_conf { uint8_t by; uint8_t mode; uint8_t speed; @@ -67,7 +69,7 @@ enum ipconf_mode : uint8_t { enum dhcp_mode : uint8_t { NETINFO_STATIC = 1, NETINFO_DHCP }; -struct wiz_NetInfo { +struct net_info { uint8_t mac[6]; uint8_t ip[4]; uint8_t sn[4]; @@ -93,19 +95,19 @@ enum netmode_type : uint32_t { NM_MASK_ALL = 0x0F0F0937 }; -struct wiz_NetTimeout { +struct net_timeout { uint8_t s_retry_cnt; uint16_t s_time_100us; uint8_t sl_retry_cnt; uint16_t sl_time_100us; }; -struct wiz_IPAddress { +struct ip_address { uint8_t ip[16]; uint8_t len; }; -struct wiz_Prefix { +struct prefix { uint8_t len; uint8_t flag; uint32_t valid_lifetime; @@ -113,58 +115,57 @@ struct wiz_Prefix { uint8_t prefix[16]; }; -struct wiz_ARP { - wiz_IPAddress destinfo; +struct arp_request { + ip_address destinfo; uint8_t dha[6]; }; -struct wiz_PING { +struct ping_request { uint16_t id; uint16_t seq; - wiz_IPAddress destinfo; + ip_address destinfo; }; -int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg); -int8_t ctlnetwork(ctlnetwork_type cntype, void* arg); -int8_t ctlnetservice(ctlnetservice_type cnstype, void* arg); +int8_t ctl_chip(chip_ctl type, void* arg); +int8_t ctl_net(net_ctl type, void* arg); +int8_t ctl_net_service(net_service_ctl type, void* arg); -void wizchip_sw_reset(); -int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize); -void wizchip_clrinterrupt(intr_kind intr); -intr_kind wizchip_getinterrupt(); -void wizchip_setinterruptmask(intr_kind intr); -intr_kind wizchip_getinterruptmask(); +void soft_reset(); +int8_t init_buffers(uint8_t* txsize, uint8_t* rxsize); +void clear_interrupt(intr_kind intr); +intr_kind get_interrupt(); +void set_interrupt_mask(intr_kind intr); +intr_kind get_interrupt_mask(); -int8_t wizphy_getphylink(); -int8_t wizphy_getphypmode(); -void wizphy_reset(); -void wizphy_setphyconf(wiz_PhyConf* phyconf); -void wizphy_getphyconf(wiz_PhyConf* phyconf); -void wizphy_getphystat(wiz_PhyConf* phyconf); -void wizphy_setphypmode(uint8_t pmode); +int8_t get_phy_link(); +int8_t get_phy_power_mode(); +void reset_phy(); +void set_phy_conf(phy_conf* conf); +void get_phy_conf(phy_conf* conf); +void get_phy_status(phy_conf* conf); +void set_phy_power_mode(uint8_t pmode); -void wizchip_setnetinfo(wiz_NetInfo* pnetinfo); -void wizchip_getnetinfo(wiz_NetInfo* pnetinfo); -void wizchip_setnetmode(netmode_type netmode); -netmode_type wizchip_getnetmode(); -void wizchip_settimeout(wiz_NetTimeout* nettime); -void wizchip_gettimeout(wiz_NetTimeout* nettime); +void set_net_info(net_info* info); +void get_net_info(net_info* info); +void set_net_mode(netmode_type mode); +netmode_type get_net_mode(); +void set_timeout(net_timeout* timeout); +void get_timeout(net_timeout* timeout); -int8_t wizchip_arp(wiz_ARP* arp); -int8_t wizchip_ping(wiz_PING* ping); -int8_t wizchip_dad(uint8_t* ipv6); -int8_t wizchip_slaac(wiz_Prefix* prefix); -int8_t wizchip_unsolicited(); -int8_t wizchip_getprefix(wiz_Prefix* prefix); +int8_t send_arp(arp_request* arp); +int8_t send_ping(ping_request* ping); +int8_t send_dad(uint8_t* ipv6); +int8_t send_slaac(prefix* pfx); +int8_t send_unsolicited(); +int8_t get_prefix(prefix* pfx); -void wizchip_spi_initialize(); -void wizchip_cris_initialize(); -void wizchip_reset(); -void wizchip_initialize(); -bool wizchip_check(); -void network_initialize(wiz_NetInfo net_info); +void init_spi(); +void init_critical_section(); +void reset(); +void init(); +bool check(); +void init_net(net_info info); -using SOCKET = uint8_t; using iodata_t = uint8_t; using datasize_t = int16_t; @@ -225,19 +226,19 @@ int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag); int8_t close(uint8_t sn); int8_t listen(uint8_t sn); int8_t disconnect(uint8_t sn); -int32_t send(uint8_t sn, uint8_t * buf, uint16_t len); -int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len); +int32_t send(uint8_t sn, uint8_t* buf, uint16_t len); +int32_t recv(uint8_t sn, uint8_t* buf, uint16_t len); -typedef enum { +enum sockint_kind { SIK_CONNECTED = (1 << 0), SIK_DISCONNECTED = (1 << 1), SIK_RECEIVED = (1 << 2), SIK_TIMEOUT = (1 << 3), SIK_SENT = (1 << 4), SIK_ALL = 0x1F -} sockint_kind; +}; -typedef enum { +enum sock_ctl { CS_SET_IOMODE, CS_GET_IOMODE, CS_GET_MAXTXBUF, @@ -248,9 +249,9 @@ typedef enum { CS_GET_PREFER, CS_SET_INTMASK, CS_GET_INTMASK -} ctlsock_type; +}; -typedef enum { +enum sockopt_type { SO_FLAG, SO_TTL, SO_TOS, @@ -266,13 +267,15 @@ typedef enum { SO_MODE, SO_REMAINSIZE, SO_PACKINFO -} sockopt_type; +}; -int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg); -int8_t setsockopt(uint8_t sn, sockopt_type sotype, void* arg); -int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg); -int16_t peeksockmsg(uint8_t sn, uint8_t* submsg, uint16_t subsize); +int8_t ctl_socket(uint8_t sn, sock_ctl type, void* arg); +int8_t set_sockopt(uint8_t sn, sockopt_type type, void* arg); +int8_t get_sockopt(uint8_t sn, sockopt_type type, void* arg); +int16_t peek_socket_msg(uint8_t sn, uint8_t* submsg, uint16_t subsize); int8_t connect(uint8_t sn, uint8_t* addr, uint16_t port, uint8_t addrlen); int32_t sendto(uint8_t sn, uint8_t* buf, uint16_t len, uint8_t* addr, uint16_t port, uint8_t addrlen); int32_t recvfrom(uint8_t sn, uint8_t* buf, uint16_t len, uint8_t* addr, uint16_t* port, uint8_t* addrlen); + +} // namespace w6300