2026-04-04 20:21:38 +09:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2026-04-04 23:08:33 +09:00
|
|
|
#include <initializer_list>
|
2026-04-04 20:21:38 +09:00
|
|
|
#include "pico/stdlib.h"
|
2026-04-04 23:08:33 +09:00
|
|
|
#include "pico/error.h"
|
2026-04-04 20:21:38 +09:00
|
|
|
#include "pico/critical_section.h"
|
2026-04-04 23:08:33 +09:00
|
|
|
#include "hardware/dma.h"
|
|
|
|
|
#include "hardware/clocks.h"
|
2026-04-04 20:47:25 +09:00
|
|
|
#include "w6300.h"
|
2026-04-04 23:08:33 +09:00
|
|
|
#include "qspi.pio.h"
|
2026-04-04 16:22:37 +09:00
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
namespace w6300 {
|
2026-04-04 20:47:25 +09:00
|
|
|
namespace {
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
#define PIO_PROGRAM_NAME qspi
|
2026-04-04 23:08:33 +09:00
|
|
|
#define PIO_PROGRAM_FUNC __CONCAT(PIO_PROGRAM_NAME, _program)
|
|
|
|
|
#define PIO_PROGRAM_GET_DEFAULT_CONFIG_FUNC __CONCAT(PIO_PROGRAM_NAME, _program_get_default_config)
|
|
|
|
|
#define PIO_OFFSET_WRITE_BITS __CONCAT(PIO_PROGRAM_NAME, _offset_write_bits)
|
|
|
|
|
#define PIO_OFFSET_WRITE_BITS_END __CONCAT(PIO_PROGRAM_NAME, _offset_write_bits_end)
|
|
|
|
|
#define PIO_OFFSET_READ_BITS_END __CONCAT(PIO_PROGRAM_NAME, _offset_read_bits_end)
|
|
|
|
|
|
|
|
|
|
constexpr uint8_t PIN_INT = 15;
|
|
|
|
|
constexpr uint8_t PIN_CS = 16;
|
|
|
|
|
constexpr uint8_t PIO_SPI_SCK_PIN = 17;
|
|
|
|
|
constexpr uint8_t PIO_SPI_DATA_IO0_PIN = 18;
|
|
|
|
|
constexpr uint8_t PIO_SPI_DATA_IO1_PIN = 19;
|
|
|
|
|
constexpr uint8_t PIO_SPI_DATA_IO2_PIN = 20;
|
|
|
|
|
constexpr uint8_t PIO_SPI_DATA_IO3_PIN = 21;
|
|
|
|
|
constexpr uint8_t PIN_RST = 22;
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint16_t SPI_CLKDIV_MAJOR = 2;
|
|
|
|
|
constexpr uint8_t SPI_CLKDIV_MINOR = 0;
|
2026-04-04 23:08:33 +09:00
|
|
|
|
|
|
|
|
constexpr uint32_t PADS_DRIVE = PADS_BANK0_GPIO0_DRIVE_VALUE_12MA;
|
|
|
|
|
constexpr uint32_t IRQ_DELAY_NS = 100;
|
|
|
|
|
constexpr uint32_t QSPI_LOOP_CNT = 2;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
pio_hw_t *pio;
|
|
|
|
|
uint8_t pio_func_sel;
|
|
|
|
|
int8_t pio_offset;
|
|
|
|
|
int8_t pio_sm;
|
|
|
|
|
int8_t dma_out;
|
|
|
|
|
int8_t dma_in;
|
|
|
|
|
} state;
|
|
|
|
|
|
|
|
|
|
uint16_t mk_cmd_buf(uint8_t *pdst, uint8_t opcode, uint16_t addr) {
|
|
|
|
|
pdst[0] = ((opcode >> 7 & 0x01) << 4) | ((opcode >> 6 & 0x01) << 0);
|
|
|
|
|
pdst[1] = ((opcode >> 5 & 0x01) << 4) | ((opcode >> 4 & 0x01) << 0);
|
|
|
|
|
pdst[2] = ((opcode >> 3 & 0x01) << 4) | ((opcode >> 2 & 0x01) << 0);
|
|
|
|
|
pdst[3] = ((opcode >> 1 & 0x01) << 4) | ((opcode >> 0 & 0x01) << 0);
|
|
|
|
|
pdst[4] = (uint8_t)(addr >> 8);
|
|
|
|
|
pdst[5] = (uint8_t)(addr);
|
|
|
|
|
pdst[6] = 0;
|
|
|
|
|
return 7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t data_pin_mask() {
|
|
|
|
|
return (1u << PIO_SPI_DATA_IO0_PIN) | (1u << PIO_SPI_DATA_IO1_PIN) |
|
|
|
|
|
(1u << PIO_SPI_DATA_IO2_PIN) | (1u << PIO_SPI_DATA_IO3_PIN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__noinline void ns_delay(uint32_t ns) {
|
|
|
|
|
uint32_t cycles = ns * (clock_get_hz(clk_sys) >> 16u) / (1000000000u >> 16u);
|
|
|
|
|
busy_wait_at_least_cycles(cycles);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void pio_init() {
|
2026-04-04 23:08:33 +09:00
|
|
|
for (auto pin : {PIO_SPI_DATA_IO0_PIN, PIO_SPI_DATA_IO1_PIN, PIO_SPI_DATA_IO2_PIN, PIO_SPI_DATA_IO3_PIN}) {
|
|
|
|
|
gpio_init(pin);
|
|
|
|
|
gpio_set_dir(pin, GPIO_OUT);
|
|
|
|
|
gpio_put(pin, false);
|
|
|
|
|
}
|
|
|
|
|
gpio_init(PIN_CS);
|
|
|
|
|
gpio_set_dir(PIN_CS, GPIO_OUT);
|
|
|
|
|
gpio_put(PIN_CS, true);
|
|
|
|
|
gpio_init(PIN_INT);
|
|
|
|
|
gpio_set_dir(PIN_INT, GPIO_IN);
|
|
|
|
|
gpio_set_pulls(PIN_INT, false, false);
|
|
|
|
|
|
|
|
|
|
pio_hw_t *pios[2] = {pio0, pio1};
|
|
|
|
|
uint pio_index = 1;
|
|
|
|
|
if (!pio_can_add_program(pios[pio_index], &PIO_PROGRAM_FUNC)) {
|
|
|
|
|
pio_index ^= 1;
|
|
|
|
|
assert(pio_can_add_program(pios[pio_index], &PIO_PROGRAM_FUNC));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.pio = pios[pio_index];
|
|
|
|
|
state.dma_in = -1;
|
|
|
|
|
state.dma_out = -1;
|
|
|
|
|
|
|
|
|
|
static_assert(GPIO_FUNC_PIO1 == GPIO_FUNC_PIO0 + 1);
|
|
|
|
|
state.pio_func_sel = GPIO_FUNC_PIO0 + pio_index;
|
|
|
|
|
state.pio_sm = (int8_t)pio_claim_unused_sm(state.pio, true);
|
|
|
|
|
state.pio_offset = pio_add_program(state.pio, &PIO_PROGRAM_FUNC);
|
|
|
|
|
|
|
|
|
|
pio_sm_config sm_config = PIO_PROGRAM_GET_DEFAULT_CONFIG_FUNC(state.pio_offset);
|
2026-04-05 09:09:10 +09:00
|
|
|
sm_config_set_clkdiv_int_frac(&sm_config, SPI_CLKDIV_MAJOR, SPI_CLKDIV_MINOR);
|
2026-04-04 23:08:33 +09:00
|
|
|
|
|
|
|
|
hw_write_masked(&pads_bank0_hw->io[PIO_SPI_SCK_PIN],
|
|
|
|
|
(uint)PADS_DRIVE << PADS_BANK0_GPIO0_DRIVE_LSB,
|
|
|
|
|
PADS_BANK0_GPIO0_DRIVE_BITS);
|
|
|
|
|
hw_write_masked(&pads_bank0_hw->io[PIO_SPI_SCK_PIN],
|
|
|
|
|
1u << PADS_BANK0_GPIO0_SLEWFAST_LSB,
|
|
|
|
|
PADS_BANK0_GPIO0_SLEWFAST_BITS);
|
|
|
|
|
|
|
|
|
|
sm_config_set_out_pins(&sm_config, PIO_SPI_DATA_IO0_PIN, 4);
|
|
|
|
|
sm_config_set_in_pins(&sm_config, PIO_SPI_DATA_IO0_PIN);
|
|
|
|
|
sm_config_set_set_pins(&sm_config, PIO_SPI_DATA_IO0_PIN, 4);
|
|
|
|
|
sm_config_set_sideset(&sm_config, 1, false, false);
|
|
|
|
|
sm_config_set_sideset_pins(&sm_config, PIO_SPI_SCK_PIN);
|
|
|
|
|
sm_config_set_in_shift(&sm_config, false, true, 8);
|
|
|
|
|
sm_config_set_out_shift(&sm_config, false, true, 8);
|
|
|
|
|
hw_set_bits(&state.pio->input_sync_bypass, data_pin_mask());
|
|
|
|
|
pio_sm_set_config(state.pio, state.pio_sm, &sm_config);
|
|
|
|
|
pio_sm_set_consecutive_pindirs(state.pio, state.pio_sm, PIO_SPI_SCK_PIN, 1, true);
|
|
|
|
|
|
|
|
|
|
for (auto pin : {PIO_SPI_DATA_IO0_PIN, PIO_SPI_DATA_IO1_PIN, PIO_SPI_DATA_IO2_PIN, PIO_SPI_DATA_IO3_PIN}) {
|
|
|
|
|
gpio_set_function(pin, (gpio_function_t)state.pio_func_sel);
|
|
|
|
|
gpio_set_pulls(pin, false, true);
|
|
|
|
|
gpio_set_input_hysteresis_enabled(pin, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_set(pio_pins, 1));
|
|
|
|
|
|
|
|
|
|
state.dma_out = (int8_t)dma_claim_unused_channel(true);
|
|
|
|
|
state.dma_in = (int8_t)dma_claim_unused_channel(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void pio_frame_start() {
|
2026-04-04 23:08:33 +09:00
|
|
|
for (auto pin : {PIO_SPI_DATA_IO0_PIN, PIO_SPI_DATA_IO1_PIN, PIO_SPI_DATA_IO2_PIN, PIO_SPI_DATA_IO3_PIN})
|
|
|
|
|
gpio_set_function(pin, (gpio_function_t)state.pio_func_sel);
|
|
|
|
|
gpio_set_function(PIO_SPI_SCK_PIN, (gpio_function_t)state.pio_func_sel);
|
|
|
|
|
gpio_pull_down(PIO_SPI_SCK_PIN);
|
|
|
|
|
gpio_put(PIN_CS, false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void pio_frame_end() {
|
2026-04-04 23:08:33 +09:00
|
|
|
gpio_put(PIN_CS, true);
|
|
|
|
|
ns_delay(IRQ_DELAY_NS);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void pio_read(uint8_t opcode, uint16_t addr, uint8_t* buf, uint16_t len) {
|
2026-04-04 23:08:33 +09:00
|
|
|
uint8_t cmd[8] = {};
|
|
|
|
|
uint16_t cmd_len = mk_cmd_buf(cmd, opcode, addr);
|
|
|
|
|
|
|
|
|
|
pio_sm_set_enabled(state.pio, state.pio_sm, false);
|
|
|
|
|
pio_sm_set_wrap(state.pio, state.pio_sm, state.pio_offset, state.pio_offset + PIO_OFFSET_READ_BITS_END - 1);
|
|
|
|
|
pio_sm_clear_fifos(state.pio, state.pio_sm);
|
|
|
|
|
pio_sm_set_pindirs_with_mask(state.pio, state.pio_sm, data_pin_mask(), data_pin_mask());
|
|
|
|
|
pio_sm_restart(state.pio, state.pio_sm);
|
|
|
|
|
pio_sm_clkdiv_restart(state.pio, state.pio_sm);
|
|
|
|
|
|
|
|
|
|
pio_sm_put(state.pio, state.pio_sm, cmd_len * QSPI_LOOP_CNT - 1);
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_out(pio_x, 32));
|
|
|
|
|
pio_sm_put(state.pio, state.pio_sm, len - 1);
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_out(pio_y, 32));
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_jmp(state.pio_offset));
|
|
|
|
|
|
|
|
|
|
dma_channel_abort(state.dma_out);
|
|
|
|
|
dma_channel_abort(state.dma_in);
|
|
|
|
|
|
|
|
|
|
dma_channel_config out_cfg = dma_channel_get_default_config(state.dma_out);
|
|
|
|
|
channel_config_set_transfer_data_size(&out_cfg, DMA_SIZE_8);
|
|
|
|
|
channel_config_set_bswap(&out_cfg, true);
|
|
|
|
|
channel_config_set_dreq(&out_cfg, pio_get_dreq(state.pio, state.pio_sm, true));
|
|
|
|
|
dma_channel_configure(state.dma_out, &out_cfg, &state.pio->txf[state.pio_sm], cmd, cmd_len, true);
|
|
|
|
|
|
|
|
|
|
dma_channel_config in_cfg = dma_channel_get_default_config(state.dma_in);
|
|
|
|
|
channel_config_set_transfer_data_size(&in_cfg, DMA_SIZE_8);
|
|
|
|
|
channel_config_set_bswap(&in_cfg, true);
|
|
|
|
|
channel_config_set_dreq(&in_cfg, pio_get_dreq(state.pio, state.pio_sm, false));
|
|
|
|
|
channel_config_set_write_increment(&in_cfg, true);
|
|
|
|
|
channel_config_set_read_increment(&in_cfg, false);
|
|
|
|
|
dma_channel_configure(state.dma_in, &in_cfg, buf, &state.pio->rxf[state.pio_sm], len, true);
|
|
|
|
|
|
|
|
|
|
pio_sm_set_enabled(state.pio, state.pio_sm, true);
|
|
|
|
|
__compiler_memory_barrier();
|
|
|
|
|
dma_channel_wait_for_finish_blocking(state.dma_out);
|
|
|
|
|
dma_channel_wait_for_finish_blocking(state.dma_in);
|
|
|
|
|
__compiler_memory_barrier();
|
|
|
|
|
pio_sm_set_enabled(state.pio, state.pio_sm, false);
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_mov(pio_pins, pio_null));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void pio_write(uint8_t opcode, uint16_t addr, uint8_t* buf, uint16_t len) {
|
2026-04-04 23:08:33 +09:00
|
|
|
uint8_t cmd[8] = {};
|
|
|
|
|
uint16_t cmd_len = mk_cmd_buf(cmd, opcode, addr);
|
|
|
|
|
uint16_t total = len + cmd_len;
|
|
|
|
|
|
|
|
|
|
pio_sm_set_enabled(state.pio, state.pio_sm, false);
|
|
|
|
|
pio_sm_set_wrap(state.pio, state.pio_sm, state.pio_offset, state.pio_offset + PIO_OFFSET_WRITE_BITS_END - 1);
|
|
|
|
|
pio_sm_clear_fifos(state.pio, state.pio_sm);
|
|
|
|
|
pio_sm_set_pindirs_with_mask(state.pio, state.pio_sm, data_pin_mask(), data_pin_mask());
|
|
|
|
|
pio_sm_restart(state.pio, state.pio_sm);
|
|
|
|
|
pio_sm_clkdiv_restart(state.pio, state.pio_sm);
|
|
|
|
|
|
|
|
|
|
pio_sm_put(state.pio, state.pio_sm, total * QSPI_LOOP_CNT - 1);
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_out(pio_x, 32));
|
|
|
|
|
pio_sm_put(state.pio, state.pio_sm, 0);
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_out(pio_y, 32));
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_jmp(state.pio_offset));
|
|
|
|
|
|
|
|
|
|
dma_channel_abort(state.dma_out);
|
|
|
|
|
|
|
|
|
|
dma_channel_config out_cfg = dma_channel_get_default_config(state.dma_out);
|
|
|
|
|
channel_config_set_transfer_data_size(&out_cfg, DMA_SIZE_8);
|
|
|
|
|
channel_config_set_bswap(&out_cfg, true);
|
|
|
|
|
channel_config_set_dreq(&out_cfg, pio_get_dreq(state.pio, state.pio_sm, true));
|
|
|
|
|
|
|
|
|
|
pio_sm_set_enabled(state.pio, state.pio_sm, true);
|
|
|
|
|
dma_channel_configure(state.dma_out, &out_cfg, &state.pio->txf[state.pio_sm], cmd, cmd_len, true);
|
|
|
|
|
dma_channel_wait_for_finish_blocking(state.dma_out);
|
|
|
|
|
dma_channel_configure(state.dma_out, &out_cfg, &state.pio->txf[state.pio_sm], buf, len, true);
|
|
|
|
|
dma_channel_wait_for_finish_blocking(state.dma_out);
|
|
|
|
|
|
|
|
|
|
const uint32_t stall = 1u << (PIO_FDEBUG_TXSTALL_LSB + state.pio_sm);
|
|
|
|
|
state.pio->fdebug = stall;
|
|
|
|
|
while (!(state.pio->fdebug & stall)) tight_loop_contents();
|
|
|
|
|
|
|
|
|
|
__compiler_memory_barrier();
|
|
|
|
|
pio_sm_set_consecutive_pindirs(state.pio, state.pio_sm, PIO_SPI_DATA_IO0_PIN, 4, false);
|
|
|
|
|
pio_sm_exec(state.pio, state.pio_sm, pio_encode_mov(pio_pins, pio_null));
|
|
|
|
|
pio_sm_set_enabled(state.pio, state.pio_sm, false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 08:18:28 +09:00
|
|
|
using iodata_t = uint8_t;
|
|
|
|
|
using datasize_t = int16_t;
|
|
|
|
|
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr int _WIZCHIP_ = 6300;
|
|
|
|
|
constexpr uint8_t _WIZCHIP_QSPI_MODE_ = 0x02 << 6;
|
2026-04-05 07:01:43 +09:00
|
|
|
constexpr int _SOCK_COUNT_ = SOCK_COUNT;
|
2026-04-04 20:47:25 +09:00
|
|
|
|
|
|
|
|
constexpr uint16_t _PHY_IO_MODE_PHYCR_ = 0x0000;
|
|
|
|
|
constexpr uint16_t _PHY_IO_MODE_MII_ = 0x0010;
|
|
|
|
|
constexpr uint16_t _PHY_IO_MODE_ = _PHY_IO_MODE_MII_;
|
|
|
|
|
|
2026-04-05 08:18:28 +09:00
|
|
|
constexpr uint8_t SYS_CHIP_LOCK = (1 << 2);
|
|
|
|
|
constexpr uint8_t SYS_NET_LOCK = (1 << 1);
|
|
|
|
|
constexpr uint8_t SYS_PHY_LOCK = (1 << 0);
|
|
|
|
|
|
|
|
|
|
constexpr uint8_t PHY_MODE_MANUAL = 0;
|
|
|
|
|
constexpr uint8_t PHY_MODE_AUTONEGO = 1;
|
|
|
|
|
constexpr uint8_t PHY_MODE_TE = 2;
|
|
|
|
|
constexpr uint8_t PHY_CONFBY_HW = 0;
|
|
|
|
|
constexpr uint8_t PHY_CONFBY_SW = 1;
|
|
|
|
|
constexpr uint8_t PHY_SPEED_10 = 0;
|
|
|
|
|
constexpr uint8_t PHY_SPEED_100 = 1;
|
|
|
|
|
constexpr uint8_t PHY_DUPLEX_HALF = 0;
|
|
|
|
|
constexpr uint8_t PHY_DUPLEX_FULL = 1;
|
|
|
|
|
constexpr uint8_t PHY_LINK_OFF = 0;
|
|
|
|
|
constexpr uint8_t PHY_LINK_ON = 1;
|
|
|
|
|
constexpr uint8_t PHY_POWER_NORM = 0;
|
|
|
|
|
constexpr uint8_t PHY_POWER_DOWN = 1;
|
|
|
|
|
|
|
|
|
|
constexpr uint8_t PACK_NONE = static_cast<uint8_t>(pack_info::none);
|
|
|
|
|
constexpr uint8_t PACK_FIRST = static_cast<uint8_t>(pack_info::first);
|
|
|
|
|
constexpr uint8_t PACK_REMAINED = static_cast<uint8_t>(pack_info::remained);
|
|
|
|
|
constexpr uint8_t PACK_COMPLETED = static_cast<uint8_t>(pack_info::completed);
|
|
|
|
|
constexpr uint8_t PACK_IPv6 = static_cast<uint8_t>(pack_info::ipv6);
|
|
|
|
|
|
2026-04-05 08:55:51 +09:00
|
|
|
|
|
|
|
|
enum sockint_kind { SIK_CONNECTED = 1, SIK_DISCONNECTED = 2, SIK_RECEIVED = 4, SIK_TIMEOUT = 8, SIK_SENT = 16, SIK_ALL = 0x1F };
|
|
|
|
|
|
|
|
|
|
|
2026-04-05 08:18:28 +09:00
|
|
|
constexpr uint8_t TCPSOCK_MODE = static_cast<uint8_t>(tcp_sock_info::mode);
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint8_t SPI_READ = (0x00 << 5);
|
|
|
|
|
constexpr uint8_t SPI_WRITE = (0x01 << 5);
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t CREG_BLOCK = 0x00;
|
|
|
|
|
constexpr uint32_t SREG_BLOCK(uint8_t n) { return 1 + 4 * n; }
|
|
|
|
|
constexpr uint32_t TXBUF_BLOCK(uint8_t n) { return 2 + 4 * n; }
|
|
|
|
|
constexpr uint32_t RXBUF_BLOCK(uint8_t n) { return 3 + 4 * n; }
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t offset_inc(uint32_t addr, uint32_t n) { return addr + (n << 8); }
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t _CIDR_ = (0x0000 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _RTL_ = (0x0004 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _VER_ = (0x0002 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SYSR_ = (0x2000 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SYCR0_ = (0x2004 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SYCR1_ = offset_inc(_SYCR0_, 1);
|
|
|
|
|
constexpr uint32_t _TCNTR_ = (0x2016 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _TCNTRCLR_ = (0x2020 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _IR_ = (0x2100 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SIR_ = (0x2101 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLIR_ = (0x2102 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _IMR_ = (0x2104 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _IRCLR_ = (0x2108 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SIMR_ = (0x2114 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLIMR_ = (0x2124 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLIRCLR_ = (0x2128 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLPSR_ = (0x212C << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLCR_ = (0x2130 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYSR_ = (0x3000 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYRAR_ = (0x3008 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYDIR_ = (0x300C << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYDOR_ = (0x3010 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYACR_ = (0x3014 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYDIVR_ = (0x3018 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYCR0_ = (0x301C << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYCR1_ = offset_inc(_PHYCR0_, 1);
|
|
|
|
|
constexpr uint32_t _NET4MR_ = (0x4000 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _NET6MR_ = (0x4004 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _NETMR_ = (0x4008 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _NETMR2_ = (0x4009 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PTMR_ = (0x4100 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PMNR_ = (0x4104 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHAR_ = (0x4108 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PSIDR_ = (0x4110 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PMRUR_ = (0x4114 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SHAR_ = (0x4120 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _GAR_ = (0x4130 << 8) + CREG_BLOCK;
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _GA4R_ = _GAR_;
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _SUBR_ = (0x4134 << 8) + CREG_BLOCK;
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _SUB4R_ = _SUBR_;
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _SIPR_ = (0x4138 << 8) + CREG_BLOCK;
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _SIP4R_ = _SIPR_;
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _LLAR_ = (0x4140 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _GUAR_ = (0x4150 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SUB6R_ = (0x4160 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _GA6R_ = (0x4170 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLDIP6R_ = (0x4180 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLDIPR_ = (0x418C << 8) + CREG_BLOCK;
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _SLDIP4R_ = _SLDIPR_;
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _SLDHAR_ = (0x4190 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PINGIDR_ = (0x4198 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PINGSEQR_ = (0x419C << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _UIPR_ = (0x41A0 << 8) + CREG_BLOCK;
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _UIP4R_ = _UIPR_;
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _UPORTR_ = (0x41A4 << 8) + CREG_BLOCK;
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _UPORT4R_ = _UPORTR_;
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _UIP6R_ = (0x41B0 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _UPORT6R_ = (0x41C0 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _INTPTMR_ = (0x41C5 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PLR_ = (0x41D0 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PFR_ = (0x41D4 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _VLTR_ = (0x41D8 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PLTR_ = (0x41DC << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PAR_ = (0x41E0 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _ICMP6BLKR_ = (0x41F0 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _CHPLCKR_ = (0x41F4 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _NETLCKR_ = (0x41F5 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _PHYLCKR_ = (0x41F6 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _RTR_ = (0x4200 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _RCR_ = (0x4204 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLRTR_ = (0x4208 << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLRCR_ = (0x420C << 8) + CREG_BLOCK;
|
|
|
|
|
constexpr uint32_t _SLHOPR_ = (0x420F << 8) + CREG_BLOCK;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t _Sn_MR_(uint8_t n) { return (0x0000 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_PSR_(uint8_t n) { return (0x0004 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_CR_(uint8_t n) { return (0x0010 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_IR_(uint8_t n) { return (0x0020 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_IMR_(uint8_t n) { return (0x0024 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_IRCLR_(uint8_t n) { return (0x0028 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_SR_(uint8_t n) { return (0x0030 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_ESR_(uint8_t n) { return (0x0031 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_PNR_(uint8_t n) { return (0x0100 << 8) + SREG_BLOCK(n); }
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _Sn_NHR_(uint8_t n) { return _Sn_PNR_(n); }
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _Sn_TOSR_(uint8_t n) { return (0x0104 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_TTLR_(uint8_t n) { return (0x0108 << 8) + SREG_BLOCK(n); }
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _Sn_HOPR_(uint8_t n) { return _Sn_TTLR_(n); }
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _Sn_FRGR_(uint8_t n) { return (0x010C << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_MSSR_(uint8_t n) { return (0x0110 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_PORTR_(uint8_t n) { return (0x0114 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_DHAR_(uint8_t n) { return (0x0118 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_DIPR_(uint8_t n) { return (0x0120 << 8) + SREG_BLOCK(n); }
|
2026-04-04 20:47:25 +09:00
|
|
|
constexpr uint32_t _Sn_DIP4R_(uint8_t n) { return _Sn_DIPR_(n); }
|
2026-04-05 09:09:10 +09:00
|
|
|
constexpr uint32_t _Sn_DIP6R_(uint8_t n) { return (0x0130 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_DPORTR_(uint8_t n) { return (0x0140 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_MR2_(uint8_t n) { return (0x0144 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_RTR_(uint8_t n) { return (0x0180 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_RCR_(uint8_t n) { return (0x0184 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_KPALVTR_(uint8_t n) { return (0x0188 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_TX_BSR_(uint8_t n) { return (0x0200 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_TX_FSR_(uint8_t n) { return (0x0204 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_TX_RD_(uint8_t n) { return (0x0208 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_TX_WR_(uint8_t n) { return (0x020C << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_RX_BSR_(uint8_t n) { return (0x0220 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_RX_RSR_(uint8_t n) { return (0x0224 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_RX_RD_(uint8_t n) { return (0x0228 << 8) + SREG_BLOCK(n); }
|
|
|
|
|
constexpr uint32_t _Sn_RX_WR_(uint8_t n) { return (0x022C << 8) + SREG_BLOCK(n); }
|
2026-04-04 20:47:25 +09:00
|
|
|
|
|
|
|
|
constexpr uint8_t SYSR_CHPL = 1 << 7;
|
|
|
|
|
constexpr uint8_t SYSR_NETL = 1 << 6;
|
|
|
|
|
constexpr uint8_t SYSR_PHYL = 1 << 5;
|
|
|
|
|
constexpr uint8_t SYSR_IND = 1 << 5;
|
|
|
|
|
constexpr uint8_t SYSR_SPI = 1 << 0;
|
|
|
|
|
constexpr uint8_t SYCR0_RST = 0x00;
|
|
|
|
|
constexpr uint8_t SYCR1_IEN = 1 << 7;
|
|
|
|
|
constexpr uint8_t SYCR1_CLKSEL = 1 << 0;
|
|
|
|
|
constexpr uint8_t SYCR1_CLKSEL_25M = 1;
|
|
|
|
|
constexpr uint8_t SYCR1_CLKSEL_100M = 0;
|
|
|
|
|
constexpr uint8_t IR_WOL = 1 << 7;
|
|
|
|
|
constexpr uint8_t IR_UNR6 = 1 << 4;
|
|
|
|
|
constexpr uint8_t IR_IPCONF = 1 << 2;
|
|
|
|
|
constexpr uint8_t IR_UNR4 = 1 << 1;
|
|
|
|
|
constexpr uint8_t IR_PTERM = 1 << 0;
|
|
|
|
|
constexpr uint8_t SIR_INT(uint8_t n) { return 1 << n; }
|
|
|
|
|
constexpr uint8_t SLIR_TOUT = 1 << 7;
|
|
|
|
|
constexpr uint8_t SLIR_ARP4 = 1 << 6;
|
|
|
|
|
constexpr uint8_t SLIR_PING4 = 1 << 5;
|
|
|
|
|
constexpr uint8_t SLIR_ARP6 = 1 << 4;
|
|
|
|
|
constexpr uint8_t SLIR_PING6 = 1 << 3;
|
|
|
|
|
constexpr uint8_t SLIR_NS = 1 << 2;
|
|
|
|
|
constexpr uint8_t SLIR_RS = 1 << 1;
|
|
|
|
|
constexpr uint8_t SLIR_RA = 1 << 0;
|
|
|
|
|
constexpr uint8_t PSR_AUTO = 0x00;
|
|
|
|
|
constexpr uint8_t PSR_LLA = 0x02;
|
|
|
|
|
constexpr uint8_t PSR_GUA = 0x03;
|
|
|
|
|
constexpr uint8_t SLCR_ARP4 = 1 << 6;
|
|
|
|
|
constexpr uint8_t SLCR_PING4 = 1 << 5;
|
|
|
|
|
constexpr uint8_t SLCR_ARP6 = 1 << 4;
|
|
|
|
|
constexpr uint8_t SLCR_PING6 = 1 << 3;
|
|
|
|
|
constexpr uint8_t SLCR_NS = 1 << 2;
|
|
|
|
|
constexpr uint8_t SLCR_RS = 1 << 1;
|
|
|
|
|
constexpr uint8_t SLCR_UNA = 1 << 0;
|
|
|
|
|
constexpr uint8_t PHYSR_CAB = 1 << 7;
|
|
|
|
|
constexpr uint8_t PHYSR_CAB_OFF = 1 << 7;
|
|
|
|
|
constexpr uint8_t PHYSR_CAB_ON = 0 << 7;
|
|
|
|
|
constexpr uint8_t PHYSR_MODE = 7 << 3;
|
|
|
|
|
constexpr uint8_t PHYSR_MODE_AUTO = 0 << 3;
|
|
|
|
|
constexpr uint8_t PHYSR_MODE_100F = 4 << 3;
|
|
|
|
|
constexpr uint8_t PHYSR_MODE_100H = 5 << 3;
|
|
|
|
|
constexpr uint8_t PHYSR_MODE_10F = 6 << 3;
|
|
|
|
|
constexpr uint8_t PHYSR_MODE_10H = 7 << 3;
|
|
|
|
|
constexpr uint8_t PHYSR_DPX = 1 << 2;
|
|
|
|
|
constexpr uint8_t PHYSR_DPX_HALF = 1 << 2;
|
|
|
|
|
constexpr uint8_t PHYSR_DPX_FULL = 0 << 2;
|
|
|
|
|
constexpr uint8_t PHYSR_SPD = 1 << 1;
|
|
|
|
|
constexpr uint8_t PHYSR_SPD_10M = 1 << 1;
|
|
|
|
|
constexpr uint8_t PHYSR_SPD_100M = 0 << 1;
|
|
|
|
|
constexpr uint8_t PHYSR_LNK = 1 << 0;
|
|
|
|
|
constexpr uint8_t PHYSR_LNK_UP = 1 << 0;
|
|
|
|
|
constexpr uint8_t PHYSR_LNK_DOWN = 0 << 0;
|
|
|
|
|
constexpr uint8_t PHYACR_READ = 0x02;
|
|
|
|
|
constexpr uint8_t PHYACR_WRITE = 0x01;
|
|
|
|
|
constexpr uint8_t PHYDIVR_32 = 0x00;
|
|
|
|
|
constexpr uint8_t PHYDIVR_64 = 0x01;
|
|
|
|
|
constexpr uint8_t PHYDIVR_128 = 0xFF;
|
|
|
|
|
constexpr uint8_t PHYCR0_AUTO = 0x00;
|
|
|
|
|
constexpr uint8_t PHYCR0_100F = 0x04;
|
|
|
|
|
constexpr uint8_t PHYCR0_100H = 0x05;
|
|
|
|
|
constexpr uint8_t PHYCR0_10F = 0x06;
|
|
|
|
|
constexpr uint8_t PHYCR0_10H = 0x07;
|
|
|
|
|
constexpr uint8_t PHYCR1_PWDN = 1 << 5;
|
|
|
|
|
constexpr uint8_t PHYCR1_TE = 1 << 3;
|
|
|
|
|
constexpr uint8_t PHYCR1_RST = 1 << 0;
|
|
|
|
|
constexpr uint8_t NETxMR_UNRB = 1 << 3;
|
|
|
|
|
constexpr uint8_t NETxMR_PARP = 1 << 2;
|
|
|
|
|
constexpr uint8_t NETxMR_RSTB = 1 << 1;
|
|
|
|
|
constexpr uint8_t NETxMR_PB = 1 << 0;
|
|
|
|
|
constexpr uint8_t NETMR_ANB = 1 << 5;
|
|
|
|
|
constexpr uint8_t NETMR_M6B = 1 << 4;
|
|
|
|
|
constexpr uint8_t NETMR_WOL = 1 << 2;
|
|
|
|
|
constexpr uint8_t NETMR_IP6B = 1 << 1;
|
|
|
|
|
constexpr uint8_t NETMR_IP4B = 1 << 0;
|
|
|
|
|
constexpr uint8_t NETMR2_DHAS = 1 << 7;
|
|
|
|
|
constexpr uint8_t NETMR2_DHAS_ARP = 1 << 7;
|
|
|
|
|
constexpr uint8_t NETMR2_DHAS_ETH = 0 << 7;
|
|
|
|
|
constexpr uint8_t NETMR2_PPPoE = 1 << 0;
|
|
|
|
|
constexpr uint8_t ICMP6BLKR_PING6 = 1 << 4;
|
|
|
|
|
constexpr uint8_t ICMP6BLKR_MLD = 1 << 3;
|
|
|
|
|
constexpr uint8_t ICMP6BLKR_RA = 1 << 2;
|
|
|
|
|
constexpr uint8_t ICMP6BLKR_NA = 1 << 1;
|
|
|
|
|
constexpr uint8_t ICMP6BLKR_NS = 1 << 0;
|
|
|
|
|
constexpr uint8_t Sn_MR_MULTI = 1 << 7;
|
|
|
|
|
constexpr uint8_t Sn_MR_MF = 1 << 7;
|
|
|
|
|
constexpr uint8_t Sn_MR_BRDB = 1 << 6;
|
|
|
|
|
constexpr uint8_t Sn_MR_FPSH = 1 << 6;
|
|
|
|
|
constexpr uint8_t Sn_MR_ND = 1 << 5;
|
|
|
|
|
constexpr uint8_t Sn_MR_MC = 1 << 5;
|
|
|
|
|
constexpr uint8_t Sn_MR_SMB = 1 << 5;
|
|
|
|
|
constexpr uint8_t Sn_MR_MMB = 1 << 5;
|
|
|
|
|
constexpr uint8_t Sn_MR_MMB4 = Sn_MR_MMB;
|
|
|
|
|
constexpr uint8_t Sn_MR_UNIB = 1 << 4;
|
|
|
|
|
constexpr uint8_t Sn_MR_MMB6 = 1 << 4;
|
|
|
|
|
constexpr uint8_t Sn_MR_CLOSE = 0x00;
|
|
|
|
|
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_CR_OPEN = 0x01;
|
|
|
|
|
constexpr uint8_t Sn_CR_LISTEN = 0x02;
|
|
|
|
|
constexpr uint8_t Sn_CR_CONNECT = 0x04;
|
|
|
|
|
constexpr uint8_t Sn_CR_CONNECT6 = 0x84;
|
|
|
|
|
constexpr uint8_t Sn_CR_DISCON = 0x08;
|
|
|
|
|
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_SEND_KEEP = 0x22;
|
|
|
|
|
constexpr uint8_t Sn_CR_RECV = 0x40;
|
|
|
|
|
constexpr uint8_t Sn_IR_SENDOK = 0x10;
|
|
|
|
|
constexpr uint8_t Sn_IR_TIMEOUT = 0x08;
|
|
|
|
|
constexpr uint8_t Sn_IR_RECV = 0x04;
|
|
|
|
|
constexpr uint8_t Sn_IR_DISCON = 0x02;
|
|
|
|
|
constexpr uint8_t Sn_IR_CON = 0x01;
|
|
|
|
|
constexpr uint8_t SOCK_CLOSED = 0x00;
|
|
|
|
|
constexpr uint8_t SOCK_INIT = 0x13;
|
|
|
|
|
constexpr uint8_t SOCK_LISTEN = 0x14;
|
|
|
|
|
constexpr uint8_t SOCK_SYNSENT = 0x15;
|
|
|
|
|
constexpr uint8_t SOCK_SYNRECV = 0x16;
|
|
|
|
|
constexpr uint8_t SOCK_ESTABLISHED = 0x17;
|
|
|
|
|
constexpr uint8_t SOCK_FIN_WAIT = 0x18;
|
|
|
|
|
constexpr uint8_t SOCK_TIME_WAIT = 0x1B;
|
|
|
|
|
constexpr uint8_t SOCK_CLOSE_WAIT = 0x1C;
|
|
|
|
|
constexpr uint8_t SOCK_LAST_ACK = 0x1D;
|
|
|
|
|
constexpr uint8_t SOCK_UDP = 0x22;
|
|
|
|
|
constexpr uint8_t SOCK_IPRAW4 = 0x32;
|
|
|
|
|
constexpr uint8_t SOCK_IPRAW = SOCK_IPRAW4;
|
|
|
|
|
constexpr uint8_t SOCK_IPRAW6 = 0x33;
|
|
|
|
|
constexpr uint8_t SOCK_MACRAW = 0x42;
|
|
|
|
|
constexpr uint8_t Sn_ESR_TCPM = 1 << 2;
|
|
|
|
|
constexpr uint8_t Sn_ESR_TCPM_IPV4 = 0 << 2;
|
|
|
|
|
constexpr uint8_t Sn_ESR_TCPM_IPV6 = 1 << 2;
|
|
|
|
|
constexpr uint8_t Sn_ESR_TCPOP = 1 << 1;
|
|
|
|
|
constexpr uint8_t Sn_ESR_TCPOP_SVR = 0 << 1;
|
|
|
|
|
constexpr uint8_t Sn_ESR_TCPOP_CLT = 1 << 1;
|
|
|
|
|
constexpr uint8_t Sn_ESR_IP6T = 1 << 0;
|
|
|
|
|
constexpr uint8_t Sn_ESR_IP6T_LLA = 0 << 0;
|
|
|
|
|
constexpr uint8_t Sn_ESR_IP6T_GUA = 1 << 0;
|
|
|
|
|
constexpr uint8_t Sn_MR2_DHAM = 1 << 1;
|
|
|
|
|
constexpr uint8_t Sn_MR2_DHAM_AUTO = 0 << 1;
|
|
|
|
|
constexpr uint8_t Sn_MR2_DHAM_MANUAL = 1 << 1;
|
|
|
|
|
constexpr uint8_t Sn_MR2_FARP = 1 << 0;
|
|
|
|
|
constexpr uint8_t PHYRAR_BMCR = 0x00;
|
|
|
|
|
constexpr uint8_t PHYRAR_BMSR = 0x01;
|
|
|
|
|
constexpr uint16_t BMCR_RST = 1 << 15;
|
|
|
|
|
constexpr uint16_t BMCR_LB = 1 << 14;
|
|
|
|
|
constexpr uint16_t BMCR_SPD = 1 << 13;
|
|
|
|
|
constexpr uint16_t BMCR_ANE = 1 << 12;
|
|
|
|
|
constexpr uint16_t BMCR_PWDN = 1 << 11;
|
|
|
|
|
constexpr uint16_t BMCR_ISOL = 1 << 10;
|
|
|
|
|
constexpr uint16_t BMCR_REAN = 1 << 9;
|
|
|
|
|
constexpr uint16_t BMCR_DPX = 1 << 8;
|
|
|
|
|
constexpr uint16_t BMCR_COLT = 1 << 7;
|
|
|
|
|
constexpr uint16_t BMSR_100_T4 = 1 << 15;
|
|
|
|
|
constexpr uint16_t BMSR_100_FDX = 1 << 14;
|
|
|
|
|
constexpr uint16_t BMSR_100_HDX = 1 << 13;
|
|
|
|
|
constexpr uint16_t BMSR_10_FDX = 1 << 12;
|
|
|
|
|
constexpr uint16_t BMSR_10_HDX = 1 << 11;
|
|
|
|
|
constexpr uint16_t BMSR_MF_SUP = 1 << 6;
|
|
|
|
|
constexpr uint16_t BMSR_AN_COMP = 1 << 5;
|
|
|
|
|
constexpr uint16_t BMSR_REMOTE_FAULT = 1 << 4;
|
|
|
|
|
constexpr uint16_t BMSR_AN_ABILITY = 1 << 3;
|
|
|
|
|
constexpr uint16_t BMSR_LINK_STATUS = 1 << 2;
|
|
|
|
|
constexpr uint16_t BMSR_JABBER_DETECT = 1 << 1;
|
|
|
|
|
constexpr uint16_t BMSR_EXT_CAPA = 1 << 0;
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void cris_enter();
|
|
|
|
|
void cris_exit();
|
2026-04-04 20:47:25 +09:00
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
uint8_t reg_read(uint32_t AddrSel);
|
|
|
|
|
void reg_write(uint32_t AddrSel, uint8_t wb);
|
|
|
|
|
void reg_read_buf(uint32_t AddrSel, uint8_t* pBuf, datasize_t len);
|
|
|
|
|
void reg_write_buf(uint32_t AddrSel, uint8_t* pBuf, datasize_t len);
|
2026-04-04 20:47:25 +09:00
|
|
|
uint16_t getSn_TX_FSR(uint8_t sn);
|
|
|
|
|
uint16_t getSn_RX_RSR(uint8_t sn);
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint8_t getRTL() { return reg_read(_RTL_); }
|
|
|
|
|
inline uint16_t getCIDR() { return (((uint16_t)reg_read(_CIDR_) | (((reg_read(_RTL_)) & 0x0F) << 1)) << 8) + reg_read(offset_inc(_CIDR_, 1)); }
|
|
|
|
|
inline uint16_t getVER() { return (((uint16_t)reg_read(_VER_)) << 8) + reg_read(offset_inc(_VER_, 1)); }
|
|
|
|
|
inline uint8_t getSYSR() { return reg_read(_SYSR_); }
|
|
|
|
|
inline uint8_t getSYCR0() { return reg_read(_SYCR0_); }
|
|
|
|
|
inline void setSYCR0(uint8_t v) { reg_write(_SYCR0_, v); }
|
|
|
|
|
inline uint8_t getSYCR1() { return reg_read(_SYCR1_); }
|
|
|
|
|
inline void setSYCR1(uint8_t v) { reg_write(_SYCR1_, v); }
|
|
|
|
|
inline uint16_t getTCNTR() { return (((uint16_t)reg_read(_TCNTR_)) << 8) + reg_read(offset_inc(_TCNTR_, 1)); }
|
|
|
|
|
inline void setTCNTRCLR(uint8_t v) { reg_write(_TCNTRCLR_, v); }
|
|
|
|
|
inline uint8_t getIR() { return reg_read(_IR_); }
|
|
|
|
|
inline uint8_t getSIR() { return reg_read(_SIR_); }
|
|
|
|
|
inline uint8_t getSLIR() { return reg_read(_SLIR_); }
|
|
|
|
|
inline void setIMR(uint8_t v) { reg_write(_IMR_, v); }
|
|
|
|
|
inline uint8_t getIMR() { return reg_read(_IMR_); }
|
|
|
|
|
inline void setIRCLR(uint8_t v) { reg_write(_IRCLR_, v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setIR(uint8_t v) { setIRCLR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSIMR(uint8_t v) { reg_write(_SIMR_, v); }
|
|
|
|
|
inline uint8_t getSIMR() { return reg_read(_SIMR_); }
|
|
|
|
|
inline void setSLIMR(uint8_t v) { reg_write(_SLIMR_, v); }
|
|
|
|
|
inline uint8_t getSLIMR() { return reg_read(_SLIMR_); }
|
|
|
|
|
inline void setSLIRCLR(uint8_t v) { reg_write(_SLIRCLR_, v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSLIR(uint8_t v) { setSLIRCLR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSLPSR(uint8_t v) { reg_write(_SLPSR_, v); }
|
|
|
|
|
inline uint8_t getSLPSR() { return reg_read(_SLPSR_); }
|
|
|
|
|
inline void setSLCR(uint8_t v) { reg_write(_SLCR_, v); }
|
|
|
|
|
inline uint8_t getSLCR() { return reg_read(_SLCR_); }
|
|
|
|
|
inline uint8_t getPHYSR() { return reg_read(_PHYSR_); }
|
|
|
|
|
inline void setPHYRAR(uint8_t v) { reg_write(_PHYRAR_, v); }
|
|
|
|
|
inline uint8_t getPHYRAR() { return reg_read(_PHYRAR_); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setPHYDIR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(offset_inc(_PHYDIR_, 1), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(_PHYDIR_, (uint8_t)v);
|
|
|
|
|
}
|
|
|
|
|
inline uint16_t getPHYDOR() { return (((uint16_t)reg_read(offset_inc(_PHYDOR_, 1))) << 8) + reg_read(_PHYDOR_); }
|
|
|
|
|
inline void setPHYACR(uint8_t v) { reg_write(_PHYACR_, v); }
|
|
|
|
|
inline uint8_t getPHYACR() { return reg_read(_PHYACR_); }
|
|
|
|
|
inline void setPHYDIVR(uint8_t v) { reg_write(_PHYDIVR_, v); }
|
|
|
|
|
inline uint8_t getPHYDIVR() { return reg_read(_PHYDIVR_); }
|
|
|
|
|
inline void setPHYCR0(uint8_t v) { reg_write(_PHYCR0_, v); }
|
|
|
|
|
inline void setPHYCR1(uint8_t v) { reg_write(_PHYCR1_, v); }
|
|
|
|
|
inline uint8_t getPHYCR1() { return reg_read(_PHYCR1_); }
|
|
|
|
|
inline void setNET4MR(uint8_t v) { reg_write(_NET4MR_, v); }
|
|
|
|
|
inline void setNET6MR(uint8_t v) { reg_write(_NET6MR_, v); }
|
|
|
|
|
inline void setNETMR(uint8_t v) { reg_write(_NETMR_, v); }
|
|
|
|
|
inline void setNETMR2(uint8_t v) { reg_write(_NETMR2_, v); }
|
|
|
|
|
inline uint8_t getNET4MR() { return reg_read(_NET4MR_); }
|
|
|
|
|
inline uint8_t getNET6MR() { return reg_read(_NET6MR_); }
|
|
|
|
|
inline uint8_t getNETMR() { return reg_read(_NETMR_); }
|
|
|
|
|
inline uint8_t getNETMR2() { return reg_read(_NETMR2_); }
|
|
|
|
|
inline void setPTMR(uint8_t v) { reg_write(_PTMR_, v); }
|
|
|
|
|
inline uint8_t getPTMR() { return reg_read(_PTMR_); }
|
|
|
|
|
inline void setPMNR(uint8_t v) { reg_write(_PMNR_, v); }
|
|
|
|
|
inline uint8_t getPMNR() { return reg_read(_PMNR_); }
|
|
|
|
|
inline void setPHAR(uint8_t* v) { reg_write_buf(_PHAR_, v, 6); }
|
|
|
|
|
inline void getPHAR(uint8_t* v) { reg_read_buf(_PHAR_, v, 6); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setPSIDR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_PSIDR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_PSIDR_, 1), (uint8_t)v);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getPSIDR() { return (((uint16_t)reg_read(_PSIDR_)) << 8) + reg_read(offset_inc(_PSIDR_, 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setPMRUR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_PMRUR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_PMRUR_, 1), (uint8_t)v);
|
|
|
|
|
}
|
|
|
|
|
inline uint16_t getPMRUR() { return (((uint16_t)reg_read(_PMRUR_)) << 8) + reg_read(offset_inc(_PMRUR_, 1)); }
|
|
|
|
|
inline void setSHAR(uint8_t* v) { reg_write_buf(_SHAR_, v, 6); }
|
|
|
|
|
inline void getSHAR(uint8_t* v) { reg_read_buf(_SHAR_, v, 6); }
|
|
|
|
|
inline void setGAR(uint8_t* v) { reg_write_buf(_GAR_, v, 4); }
|
|
|
|
|
inline void getGAR(uint8_t* v) { reg_read_buf(_GAR_, v, 4); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setGA4R(uint8_t* v) { setGAR(v); }
|
|
|
|
|
inline void getGA4R(uint8_t* v) { getGAR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSUBR(uint8_t* v) { reg_write_buf(_SUBR_, v, 4); }
|
|
|
|
|
inline void getSUBR(uint8_t* v) { reg_read_buf(_SUBR_, v, 4); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSUB4R(uint8_t* v) { setSUBR(v); }
|
|
|
|
|
inline void getSUB4R(uint8_t* v) { getSUBR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSIPR(uint8_t* v) { reg_write_buf(_SIPR_, v, 4); }
|
|
|
|
|
inline void getSIPR(uint8_t* v) { reg_read_buf(_SIPR_, v, 4); }
|
|
|
|
|
inline void setLLAR(uint8_t* v) { reg_write_buf(_LLAR_, v, 16); }
|
|
|
|
|
inline void getLLAR(uint8_t* v) { reg_read_buf(_LLAR_, v, 16); }
|
|
|
|
|
inline void setGUAR(uint8_t* v) { reg_write_buf(_GUAR_, v, 16); }
|
|
|
|
|
inline void getGUAR(uint8_t* v) { reg_read_buf(_GUAR_, v, 16); }
|
|
|
|
|
inline void setSUB6R(uint8_t* v) { reg_write_buf(_SUB6R_, v, 16); }
|
|
|
|
|
inline void getSUB6R(uint8_t* v) { reg_read_buf(_SUB6R_, v, 16); }
|
|
|
|
|
inline void setGA6R(uint8_t* v) { reg_write_buf(_GA6R_, v, 16); }
|
|
|
|
|
inline void getGA6R(uint8_t* v) { reg_read_buf(_GA6R_, v, 16); }
|
|
|
|
|
inline void setSLDIPR(uint8_t* v) { reg_write_buf(_SLDIPR_, v, 4); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSLDIP4R(uint8_t* v) { setSLDIPR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void getSLDIPR(uint8_t* v) { reg_read_buf(_SLDIPR_, v, 4); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void getSLDIP4R(uint8_t* v) { getSLDIPR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSLDIP6R(uint8_t* v) { reg_write_buf(_SLDIP6R_, v, 16); }
|
|
|
|
|
inline void getSLDIP6R(uint8_t* v) { reg_read_buf(_SLDIP6R_, v, 16); }
|
|
|
|
|
inline void getSLDHAR(uint8_t* v) { reg_read_buf(_SLDHAR_, v, 6); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setPINGIDR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_PINGIDR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_PINGIDR_, 1), (uint8_t)v);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getPINGIDR() { return ((uint16_t)(reg_read(_PINGIDR_) << 8)) + reg_read(offset_inc(_PINGIDR_, 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setPINGSEQR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_PINGSEQR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_PINGSEQR_, 1), (uint8_t)v);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getPINGSEQR() { return ((uint16_t)(reg_read(_PINGSEQR_) << 8)) + reg_read(offset_inc(_PINGSEQR_, 1)); }
|
|
|
|
|
inline void getUIPR(uint8_t* v) { reg_read_buf(_UIPR_, v, 4); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void getUIP4R(uint8_t* v) { getUIPR(v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getUPORTR() { return (((uint16_t)reg_read(_UPORTR_)) << 8) + reg_read(offset_inc(_UPORTR_, 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint16_t getUPORT4R() { return getUPORTR(); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void getUIP6R(uint8_t* v) { reg_read_buf(_UIP6R_, v, 16); }
|
|
|
|
|
inline uint16_t getUPORT6R() { return (((uint16_t)reg_read(_UPORT6R_)) << 8) + reg_read(offset_inc(_UPORT6R_, 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setINTPTMR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_INTPTMR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_INTPTMR_, 1), (uint8_t)v);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getINTPTMR() { return (((uint16_t)reg_read(_INTPTMR_)) << 8) + reg_read(offset_inc(_INTPTMR_, 1)); }
|
|
|
|
|
inline uint8_t getPLR() { return reg_read(_PLR_); }
|
|
|
|
|
inline uint8_t getPFR() { return reg_read(_PFR_); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint32_t getVLTR() {
|
2026-04-05 09:09:10 +09:00
|
|
|
return (((uint32_t)reg_read(_VLTR_)) << 24) +
|
|
|
|
|
(((uint32_t)reg_read(offset_inc(_VLTR_, 1))) << 16) +
|
|
|
|
|
(((uint32_t)reg_read(offset_inc(_VLTR_, 2))) << 8) +
|
|
|
|
|
((uint32_t)reg_read(offset_inc(_VLTR_, 3)));
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
|
|
|
|
inline uint32_t getPLTR() {
|
2026-04-05 09:09:10 +09:00
|
|
|
return (((uint32_t)reg_read(_PLTR_)) << 24) +
|
|
|
|
|
(((uint32_t)reg_read(offset_inc(_PLTR_, 1))) << 16) +
|
|
|
|
|
(((uint32_t)reg_read(offset_inc(_PLTR_, 2))) << 8) +
|
|
|
|
|
((uint32_t)reg_read(offset_inc(_PLTR_, 3)));
|
|
|
|
|
}
|
|
|
|
|
inline void getPAR(uint8_t* v) { reg_read_buf(_PAR_, v, 16); }
|
|
|
|
|
inline void setICMP6BLKR(uint8_t v) { reg_write(_ICMP6BLKR_, v); }
|
|
|
|
|
inline uint8_t getICMP6BLKR() { return reg_read(_ICMP6BLKR_); }
|
|
|
|
|
inline void setCHPLCKR(uint8_t v) { reg_write(_CHPLCKR_, v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getCHPLCKR() { return (getSYSR() & SYSR_CHPL) >> 7; }
|
|
|
|
|
inline void CHIPLOCK() { setCHPLCKR(0xFF); }
|
|
|
|
|
inline void CHIPUNLOCK() { setCHPLCKR(0xCE); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setNETLCKR(uint8_t v) { reg_write(_NETLCKR_, v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getNETLCKR() { return (getSYSR() & SYSR_NETL) >> 6; }
|
|
|
|
|
inline void NETLOCK() { setNETLCKR(0xC5); }
|
|
|
|
|
inline void NETUNLOCK() { setNETLCKR(0x3A); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setPHYLCKR(uint8_t v) { reg_write(_PHYLCKR_, v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getPHYLCKR() { return (getSYSR() & SYSR_PHYL) >> 5; }
|
|
|
|
|
inline void PHYLOCK() { setPHYLCKR(0xFF); }
|
|
|
|
|
inline void PHYUNLOCK() { setPHYLCKR(0x53); }
|
|
|
|
|
inline void setRTR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_RTR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_RTR_, 1), (uint8_t)v);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getRTR() { return (((uint16_t)reg_read(_RTR_)) << 8) + reg_read(offset_inc(_RTR_, 1)); }
|
|
|
|
|
inline void setRCR(uint8_t v) { reg_write(_RCR_, v); }
|
|
|
|
|
inline uint8_t getRCR() { return reg_read(_RCR_); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSLRTR(uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_SLRTR_, (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_SLRTR_, 1), (uint8_t)v);
|
|
|
|
|
}
|
|
|
|
|
inline uint16_t getSLRTR() { return (((uint16_t)reg_read(_SLRTR_)) << 8) + reg_read(offset_inc(_SLRTR_, 1)); }
|
|
|
|
|
inline void setSLRCR(uint8_t v) { reg_write(_SLRCR_, v); }
|
|
|
|
|
inline uint8_t getSLRCR() { return reg_read(_SLRCR_); }
|
|
|
|
|
inline void setSLHOPR(uint8_t v) { reg_write(_SLHOPR_, v); }
|
|
|
|
|
inline uint8_t getSLHOPR() { return reg_read(_SLHOPR_); }
|
|
|
|
|
|
|
|
|
|
inline void setSn_MR(uint8_t sn, uint8_t v) { reg_write(_Sn_MR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_MR(uint8_t sn) { return reg_read(_Sn_MR_(sn)); }
|
|
|
|
|
inline void setSn_PSR(uint8_t sn, uint8_t v) { reg_write(_Sn_PSR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_PSR(uint8_t sn) { return reg_read(_Sn_PSR_(sn)); }
|
|
|
|
|
inline void setSn_CR(uint8_t sn, uint8_t v) { reg_write(_Sn_CR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_CR(uint8_t sn) { return reg_read(_Sn_CR_(sn)); }
|
|
|
|
|
inline uint8_t getSn_IR(uint8_t sn) { return reg_read(_Sn_IR_(sn)); }
|
|
|
|
|
inline void setSn_IMR(uint8_t sn, uint8_t v) { reg_write(_Sn_IMR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_IMR(uint8_t sn) { return reg_read(_Sn_IMR_(sn)); }
|
|
|
|
|
inline void setSn_IRCLR(uint8_t sn, uint8_t v) { reg_write(_Sn_IRCLR_(sn), v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_IR(uint8_t sn, uint8_t v) { setSn_IRCLR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint8_t getSn_SR(uint8_t sn) { return reg_read(_Sn_SR_(sn)); }
|
|
|
|
|
inline uint8_t getSn_ESR(uint8_t sn) { return reg_read(_Sn_ESR_(sn)); }
|
|
|
|
|
inline void setSn_PNR(uint8_t sn, uint8_t v) { reg_write(_Sn_PNR_(sn), v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_NHR(uint8_t sn, uint8_t v) { setSn_PNR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint8_t getSn_PNR(uint8_t sn) { return reg_read(_Sn_PNR_(sn)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getSn_NHR(uint8_t sn) { return getSn_PNR(sn); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSn_TOSR(uint8_t sn, uint8_t v) { reg_write(_Sn_TOSR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_TOSR(uint8_t sn) { return reg_read(_Sn_TOSR_(sn)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getSn_TOS(uint8_t sn) { return getSn_TOSR(sn); }
|
|
|
|
|
inline void setSn_TOS(uint8_t sn, uint8_t v) { setSn_TOSR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSn_TTLR(uint8_t sn, uint8_t v) { reg_write(_Sn_TTLR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_TTLR(uint8_t sn) { return reg_read(_Sn_TTLR_(sn)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_TTL(uint8_t sn, uint8_t v) { setSn_TTLR(sn, v); }
|
|
|
|
|
inline uint8_t getSn_TTL(uint8_t sn) { return getSn_TTLR(sn); }
|
|
|
|
|
inline void setSn_HOPR(uint8_t sn, uint8_t v) { setSn_TTLR(sn, v); }
|
|
|
|
|
inline uint8_t getSn_HOPR(uint8_t sn) { return getSn_TTLR(sn); }
|
|
|
|
|
inline void setSn_FRGR(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_FRGR_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_FRGR_(sn), 1), (uint8_t)v);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getSn_FRGR(uint8_t sn) { return (((uint16_t)reg_read(_Sn_FRGR_(sn))) << 8) + reg_read(offset_inc(_Sn_FRGR_(sn), 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_MSSR(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_MSSR_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_MSSR_(sn), 1), (uint8_t)v);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getSn_MSSR(uint8_t sn) { return (((uint16_t)reg_read(_Sn_MSSR_(sn))) << 8) + reg_read(offset_inc(_Sn_MSSR_(sn), 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_PORTR(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_PORTR_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_PORTR_(sn), 1), (uint8_t)v);
|
|
|
|
|
}
|
|
|
|
|
inline uint16_t getSn_PORTR(uint8_t sn) { return (((uint16_t)reg_read(_Sn_PORTR_(sn))) << 8) + reg_read(offset_inc(_Sn_PORTR_(sn), 1)); }
|
|
|
|
|
inline void setSn_DHAR(uint8_t sn, uint8_t* v) { reg_write_buf(_Sn_DHAR_(sn), v, 6); }
|
|
|
|
|
inline void getSn_DHAR(uint8_t sn, uint8_t* v) { reg_read_buf(_Sn_DHAR_(sn), v, 6); }
|
|
|
|
|
inline void setSn_DIPR(uint8_t sn, uint8_t* v) { reg_write_buf(_Sn_DIPR_(sn), v, 4); }
|
|
|
|
|
inline void getSn_DIPR(uint8_t sn, uint8_t* v) { reg_read_buf(_Sn_DIPR_(sn), v, 4); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_DIP4R(uint8_t sn, uint8_t* v) { setSn_DIPR(sn, v); }
|
|
|
|
|
inline void getSn_DIP4R(uint8_t sn, uint8_t* v) { getSn_DIPR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSn_DIP6R(uint8_t sn, uint8_t* v) { reg_write_buf(_Sn_DIP6R_(sn), v, 16); }
|
|
|
|
|
inline void getSn_DIP6R(uint8_t sn, uint8_t* v) { reg_read_buf(_Sn_DIP6R_(sn), v, 16); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_DPORTR(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_DPORTR_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_DPORTR_(sn), 1), (uint8_t)v);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getSn_DPORTR(uint8_t sn) { return (((uint16_t)reg_read(_Sn_DPORTR_(sn))) << 8) + reg_read(offset_inc(_Sn_DPORTR_(sn), 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint16_t getSn_DPORT(uint8_t sn) { return getSn_DPORTR(sn); }
|
|
|
|
|
inline void setSn_DPORT(uint8_t sn, uint16_t v) { setSn_DPORTR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline void setSn_MR2(uint8_t sn, uint8_t v) { reg_write(_Sn_MR2_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_MR2(uint8_t sn) { return reg_read(_Sn_MR2_(sn)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_RTR(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_RTR_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_RTR_(sn), 1), (uint8_t)v);
|
|
|
|
|
}
|
|
|
|
|
inline uint16_t getSn_RTR(uint8_t sn) { return (((uint16_t)reg_read(_Sn_RTR_(sn))) << 8) + reg_read(offset_inc(_Sn_RTR_(sn), 1)); }
|
|
|
|
|
inline void setSn_RCR(uint8_t sn, uint8_t v) { reg_write(_Sn_RCR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_RCR(uint8_t sn) { return reg_read(_Sn_RCR_(sn)); }
|
|
|
|
|
inline void setSn_KPALVTR(uint8_t sn, uint8_t v) { reg_write(_Sn_KPALVTR_(sn), v); }
|
|
|
|
|
inline uint8_t getSn_KPALVTR(uint8_t sn) { return reg_read(_Sn_KPALVTR_(sn)); }
|
|
|
|
|
inline void setSn_TX_BSR(uint8_t sn, uint8_t v) { reg_write(_Sn_TX_BSR_(sn), v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_TXBUF_SIZE(uint8_t sn, uint8_t v) { setSn_TX_BSR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint8_t getSn_TX_BSR(uint8_t sn) { return reg_read(_Sn_TX_BSR_(sn)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getSn_TXBUF_SIZE(uint8_t sn) { return getSn_TX_BSR(sn); }
|
|
|
|
|
inline uint16_t getSn_TxMAX(uint8_t sn) { return getSn_TX_BSR(sn) << 10; }
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getSn_TX_RD(uint8_t sn) { return (((uint16_t)reg_read(_Sn_TX_RD_(sn))) << 8) + reg_read(offset_inc(_Sn_TX_RD_(sn), 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_TX_WR(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_TX_WR_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_TX_WR_(sn), 1), (uint8_t)v);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getSn_TX_WR(uint8_t sn) { return ((uint16_t)reg_read(_Sn_TX_WR_(sn)) << 8) + reg_read(offset_inc(_Sn_TX_WR_(sn), 1)); }
|
|
|
|
|
inline void setSn_RX_BSR(uint8_t sn, uint8_t v) { reg_write(_Sn_RX_BSR_(sn), v); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline void setSn_RXBUF_SIZE(uint8_t sn, uint8_t v) { setSn_RX_BSR(sn, v); }
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint8_t getSn_RX_BSR(uint8_t sn) { return reg_read(_Sn_RX_BSR_(sn)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
inline uint8_t getSn_RXBUF_SIZE(uint8_t sn) { return getSn_RX_BSR(sn); }
|
|
|
|
|
inline uint16_t getSn_RxMAX(uint8_t sn) { return getSn_RX_BSR(sn) << 10; }
|
|
|
|
|
|
|
|
|
|
inline void setSn_RX_RD(uint8_t sn, uint16_t v) {
|
2026-04-05 09:09:10 +09:00
|
|
|
reg_write(_Sn_RX_RD_(sn), (uint8_t)(v >> 8));
|
|
|
|
|
reg_write(offset_inc(_Sn_RX_RD_(sn), 1), (uint8_t)v);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
inline uint16_t getSn_RX_RD(uint8_t sn) { return ((uint16_t)reg_read(_Sn_RX_RD_(sn)) << 8) + reg_read(offset_inc(_Sn_RX_RD_(sn), 1)); }
|
|
|
|
|
inline uint16_t getSn_RX_WR(uint8_t sn) { return ((uint16_t)reg_read(_Sn_RX_WR_(sn)) << 8) + reg_read(offset_inc(_Sn_RX_WR_(sn), 1)); }
|
2026-04-04 20:47:25 +09:00
|
|
|
static critical_section_t g_cris_sec;
|
2026-04-04 20:21:38 +09:00
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void cris_enter() {
|
2026-04-04 20:47:25 +09:00
|
|
|
critical_section_enter_blocking(&g_cris_sec);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void cris_exit() {
|
2026-04-04 20:47:25 +09:00
|
|
|
critical_section_exit(&g_cris_sec);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 16:22:37 +09:00
|
|
|
static uint8_t make_opcode(uint32_t addr, uint8_t rw) {
|
|
|
|
|
return static_cast<uint8_t>((addr & 0xFF) | rw | _WIZCHIP_QSPI_MODE_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint16_t make_addr(uint32_t addr) {
|
|
|
|
|
return static_cast<uint16_t>((addr & 0x00FFFF00) >> 8);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void reg_write(uint32_t AddrSel, uint8_t wb) {
|
|
|
|
|
cris_enter();
|
|
|
|
|
pio_frame_start();
|
|
|
|
|
pio_write(make_opcode(AddrSel, SPI_WRITE), make_addr(AddrSel), &wb, 1);
|
|
|
|
|
pio_frame_end();
|
|
|
|
|
cris_exit();
|
2026-04-04 16:22:37 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
uint8_t reg_read(uint32_t AddrSel) {
|
2026-04-04 16:22:37 +09:00
|
|
|
uint8_t ret[2] = {0};
|
2026-04-05 09:09:10 +09:00
|
|
|
cris_enter();
|
|
|
|
|
pio_frame_start();
|
|
|
|
|
pio_read(make_opcode(AddrSel, SPI_READ), make_addr(AddrSel), ret, 1);
|
|
|
|
|
pio_frame_end();
|
|
|
|
|
cris_exit();
|
2026-04-04 16:22:37 +09:00
|
|
|
return ret[0];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void reg_write_buf(uint32_t AddrSel, uint8_t* pBuf, datasize_t len) {
|
|
|
|
|
cris_enter();
|
|
|
|
|
pio_frame_start();
|
|
|
|
|
pio_write(make_opcode(AddrSel, SPI_WRITE), make_addr(AddrSel), pBuf, len);
|
|
|
|
|
pio_frame_end();
|
|
|
|
|
cris_exit();
|
2026-04-04 16:22:37 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void reg_read_buf(uint32_t AddrSel, uint8_t* pBuf, datasize_t len) {
|
|
|
|
|
cris_enter();
|
|
|
|
|
pio_frame_start();
|
|
|
|
|
pio_read(make_opcode(AddrSel, SPI_READ), make_addr(AddrSel), pBuf, len);
|
|
|
|
|
pio_frame_end();
|
|
|
|
|
cris_exit();
|
2026-04-04 16:22:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t getSn_TX_FSR(uint8_t sn) {
|
|
|
|
|
uint16_t prev_val = -1, val = 0;
|
|
|
|
|
do {
|
|
|
|
|
prev_val = val;
|
2026-04-05 09:09:10 +09:00
|
|
|
val = reg_read(_Sn_TX_FSR_(sn));
|
|
|
|
|
val = (val << 8) + reg_read(offset_inc(_Sn_TX_FSR_(sn), 1));
|
2026-04-04 16:22:37 +09:00
|
|
|
} while (val != prev_val);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t getSn_RX_RSR(uint8_t sn) {
|
|
|
|
|
uint16_t prev_val = -1, val = 0;
|
|
|
|
|
do {
|
|
|
|
|
prev_val = val;
|
2026-04-05 09:09:10 +09:00
|
|
|
val = reg_read(_Sn_RX_RSR_(sn));
|
|
|
|
|
val = (val << 8) + reg_read(offset_inc(_Sn_RX_RSR_(sn), 1));
|
2026-04-04 16:22:37 +09:00
|
|
|
} while (val != prev_val);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void send_data(uint8_t sn, uint8_t *data, uint16_t len) {
|
2026-04-04 16:22:37 +09:00
|
|
|
uint16_t ptr = getSn_TX_WR(sn);
|
2026-04-05 09:09:10 +09:00
|
|
|
uint32_t addrsel = ((uint32_t)ptr << 8) + TXBUF_BLOCK(sn);
|
|
|
|
|
reg_write_buf(addrsel, data, len);
|
2026-04-04 16:22:37 +09:00
|
|
|
ptr += len;
|
|
|
|
|
setSn_TX_WR(sn, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void recv_data(uint8_t sn, uint8_t *data, uint16_t len) {
|
2026-04-04 16:22:37 +09:00
|
|
|
if (len == 0) return;
|
|
|
|
|
uint16_t ptr = getSn_RX_RD(sn);
|
2026-04-05 09:09:10 +09:00
|
|
|
uint32_t addrsel = ((uint32_t)ptr << 8) + RXBUF_BLOCK(sn);
|
|
|
|
|
reg_read_buf(addrsel, data, len);
|
2026-04-04 16:22:37 +09:00
|
|
|
ptr += len;
|
|
|
|
|
setSn_RX_RD(sn, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void recv_ignore(uint8_t sn, uint16_t len) {
|
2026-04-04 16:22:37 +09:00
|
|
|
setSn_RX_RD(sn, getSn_RX_RD(sn) + len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
void mdio_write(uint8_t phyregaddr, uint16_t var) {
|
2026-04-04 16:22:37 +09:00
|
|
|
setPHYRAR(phyregaddr);
|
|
|
|
|
setPHYDIR(var);
|
|
|
|
|
setPHYACR(PHYACR_WRITE);
|
|
|
|
|
while (getPHYACR());
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
uint16_t mdio_read(uint8_t phyregaddr) {
|
2026-04-04 16:22:37 +09:00
|
|
|
setPHYRAR(phyregaddr);
|
|
|
|
|
setPHYACR(PHYACR_READ);
|
|
|
|
|
while (getPHYACR());
|
|
|
|
|
return getPHYDOR();
|
|
|
|
|
}
|
2026-04-04 20:21:38 +09:00
|
|
|
|
2026-04-04 20:47:25 +09:00
|
|
|
} // namespace
|
|
|
|
|
|
2026-04-04 20:21:38 +09:00
|
|
|
static uint8_t dns_[4];
|
|
|
|
|
static uint8_t dns6_[16];
|
|
|
|
|
static ipconf_mode ipmode_;
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
static constexpr char CHIP_ID[] = "W6300";
|
2026-04-04 20:21:38 +09:00
|
|
|
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void soft_reset() {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t gw[4], sn[4], sip[4], mac[6];
|
|
|
|
|
uint8_t gw6[16], sn6[16], lla[16], gua[16];
|
|
|
|
|
uint8_t islock = getSYSR();
|
|
|
|
|
|
|
|
|
|
CHIPUNLOCK();
|
|
|
|
|
getSHAR(mac); getGAR(gw); getSUBR(sn); getSIPR(sip);
|
|
|
|
|
getGA6R(gw6); getSUB6R(sn6); getLLAR(lla); getGUAR(gua);
|
|
|
|
|
setSYCR0(SYCR0_RST);
|
|
|
|
|
getSYCR0();
|
|
|
|
|
|
|
|
|
|
NETUNLOCK();
|
|
|
|
|
setSHAR(mac); setGAR(gw); setSUBR(sn); setSIPR(sip);
|
|
|
|
|
setGA6R(gw6); setSUB6R(sn6); setLLAR(lla); setGUAR(gua);
|
|
|
|
|
|
|
|
|
|
if (islock & SYSR_CHPL) CHIPLOCK();
|
|
|
|
|
if (islock & SYSR_NETL) NETLOCK();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
int8_t init_buffers(std::span<const uint8_t> txsize, std::span<const uint8_t> rxsize) {
|
2026-04-05 07:01:43 +09:00
|
|
|
soft_reset();
|
2026-04-05 15:32:20 +09:00
|
|
|
if (!txsize.empty()) {
|
2026-04-04 20:21:38 +09:00
|
|
|
int8_t tmp = 0;
|
2026-04-05 07:01:43 +09:00
|
|
|
for (int i = 0; i < SOCK_COUNT; i++) {
|
2026-04-04 20:21:38 +09:00
|
|
|
tmp += txsize[i];
|
|
|
|
|
if (tmp > 32) return -1;
|
|
|
|
|
}
|
2026-04-05 07:01:43 +09:00
|
|
|
for (int i = 0; i < SOCK_COUNT; i++) setSn_TXBUF_SIZE(i, txsize[i]);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 15:32:20 +09:00
|
|
|
if (!rxsize.empty()) {
|
2026-04-04 20:21:38 +09:00
|
|
|
int8_t tmp = 0;
|
2026-04-05 07:01:43 +09:00
|
|
|
for (int i = 0; i < SOCK_COUNT; i++) {
|
2026-04-04 20:21:38 +09:00
|
|
|
tmp += rxsize[i];
|
|
|
|
|
if (tmp > 32) return -1;
|
|
|
|
|
}
|
2026-04-05 07:01:43 +09:00
|
|
|
for (int i = 0; i < SOCK_COUNT; i++) setSn_RXBUF_SIZE(i, rxsize[i]);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void clear_interrupt(intr_kind intr) {
|
2026-04-04 20:21:38 +09:00
|
|
|
setIRCLR((uint8_t)intr);
|
|
|
|
|
uint8_t sir = (uint8_t)((uint16_t)intr >> 8);
|
2026-04-05 07:01:43 +09:00
|
|
|
for (int i = 0; i < SOCK_COUNT; i++)
|
2026-04-04 20:21:38 +09:00
|
|
|
if (sir & (1 << i)) setSn_IRCLR(i, 0xFF);
|
|
|
|
|
setSLIRCLR((uint8_t)((uint32_t)intr >> 16));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
intr_kind get_interrupt() {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint32_t ret = getSIR();
|
|
|
|
|
ret = (ret << 8) + getIR();
|
|
|
|
|
ret = (((uint32_t)getSLIR()) << 16) | ret;
|
|
|
|
|
return (intr_kind)ret;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void set_interrupt_mask(intr_kind intr) {
|
2026-04-04 20:21:38 +09:00
|
|
|
setIMR((uint8_t)intr);
|
|
|
|
|
setSIMR((uint8_t)((uint16_t)intr >> 8));
|
|
|
|
|
setSLIMR((uint8_t)((uint32_t)intr >> 16));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
intr_kind get_interrupt_mask() {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint32_t ret = getSIMR();
|
|
|
|
|
ret = (ret << 8) + getIMR();
|
|
|
|
|
ret = (((uint32_t)getSLIMR()) << 16) | ret;
|
|
|
|
|
return (intr_kind)ret;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
int8_t get_phy_link() {
|
2026-04-05 09:09:10 +09:00
|
|
|
if (mdio_read(PHYRAR_BMSR) & BMSR_LINK_STATUS) return PHY_LINK_ON;
|
2026-04-04 20:21:38 +09:00
|
|
|
return PHY_LINK_OFF;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
int8_t get_phy_power_mode() {
|
2026-04-05 09:09:10 +09:00
|
|
|
if (mdio_read(PHYRAR_BMCR) & BMCR_PWDN) return PHY_POWER_DOWN;
|
2026-04-04 20:21:38 +09:00
|
|
|
return PHY_POWER_NORM;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void reset_phy() {
|
2026-04-05 09:09:10 +09:00
|
|
|
mdio_write(PHYRAR_BMCR, mdio_read(PHYRAR_BMCR) | BMCR_RST);
|
|
|
|
|
while (mdio_read(PHYRAR_BMCR) & BMCR_RST);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
void set_phy_conf(const phy_conf& phyconf) {
|
2026-04-05 09:09:10 +09:00
|
|
|
uint16_t tmp = mdio_read(PHYRAR_BMCR);
|
2026-04-05 15:32:20 +09:00
|
|
|
if (phyconf.mode == PHY_MODE_TE) {
|
2026-04-04 20:21:38 +09:00
|
|
|
setPHYCR1(getPHYCR1() | PHYCR1_TE);
|
|
|
|
|
setPHYCR0(PHYCR0_AUTO);
|
|
|
|
|
} else {
|
|
|
|
|
setPHYCR1(getPHYCR1() & ~PHYCR1_TE);
|
2026-04-05 15:32:20 +09:00
|
|
|
if (phyconf.mode == PHY_MODE_AUTONEGO) {
|
2026-04-04 20:21:38 +09:00
|
|
|
tmp |= BMCR_ANE;
|
|
|
|
|
} else {
|
|
|
|
|
tmp &= ~(BMCR_ANE | BMCR_DPX | BMCR_SPD);
|
2026-04-05 15:32:20 +09:00
|
|
|
if (phyconf.duplex == PHY_DUPLEX_FULL) tmp |= BMCR_DPX;
|
|
|
|
|
if (phyconf.speed == PHY_SPEED_100) tmp |= BMCR_SPD;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
mdio_write(PHYRAR_BMCR, tmp);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
phy_conf get_phy_conf() {
|
|
|
|
|
phy_conf result;
|
2026-04-05 09:09:10 +09:00
|
|
|
uint16_t tmp = mdio_read(PHYRAR_BMCR);
|
2026-04-05 15:32:20 +09:00
|
|
|
result.mode = (getPHYCR1() & 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;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
phy_conf get_phy_status() {
|
|
|
|
|
phy_conf result;
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp = getPHYSR();
|
2026-04-05 15:32:20 +09:00
|
|
|
result.mode = (getPHYCR1() & 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;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void set_phy_power_mode(uint8_t pmode) {
|
2026-04-05 09:09:10 +09:00
|
|
|
uint16_t tmp = mdio_read(PHYRAR_BMCR);
|
2026-04-04 20:21:38 +09:00
|
|
|
if (pmode == PHY_POWER_DOWN) tmp |= BMCR_PWDN;
|
|
|
|
|
else tmp &= ~BMCR_PWDN;
|
2026-04-05 09:09:10 +09:00
|
|
|
mdio_write(PHYRAR_BMCR, tmp);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
void set_net_info(const net_info& p) {
|
|
|
|
|
setSHAR(const_cast<uint8_t*>(p.mac.data())); setGAR(const_cast<uint8_t*>(p.gw.data()));
|
|
|
|
|
setSUBR(const_cast<uint8_t*>(p.sn.data())); setSIPR(const_cast<uint8_t*>(p.ip.data()));
|
|
|
|
|
setGA6R(const_cast<uint8_t*>(p.gw6.data())); setSUB6R(const_cast<uint8_t*>(p.sn6.data()));
|
|
|
|
|
setLLAR(const_cast<uint8_t*>(p.lla.data())); setGUAR(const_cast<uint8_t*>(p.gua.data()));
|
|
|
|
|
memcpy(dns_, p.dns.data(), 4);
|
|
|
|
|
memcpy(dns6_, p.dns6.data(), 16);
|
|
|
|
|
ipmode_ = p.ipmode;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
net_info get_net_info() {
|
|
|
|
|
net_info p = {};
|
|
|
|
|
getSHAR(p.mac.data()); getGAR(p.gw.data()); getSUBR(p.sn.data()); getSIPR(p.ip.data());
|
|
|
|
|
getGA6R(p.gw6.data()); getSUB6R(p.sn6.data()); getLLAR(p.lla.data()); getGUAR(p.gua.data());
|
|
|
|
|
memcpy(p.dns.data(), dns_, 4);
|
|
|
|
|
memcpy(p.dns6.data(), dns6_, 16);
|
|
|
|
|
p.ipmode = ipmode_;
|
|
|
|
|
return p;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void set_net_mode(netmode_type netmode) {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint32_t tmp = (uint32_t)netmode;
|
|
|
|
|
setNETMR((uint8_t)tmp);
|
|
|
|
|
setNETMR2((uint8_t)(tmp >> 8));
|
|
|
|
|
setNET4MR((uint8_t)(tmp >> 16));
|
|
|
|
|
setNET6MR((uint8_t)(tmp >> 24));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
netmode_type get_net_mode() {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint32_t ret = getNETMR();
|
|
|
|
|
ret = (ret << 8) + getNETMR2();
|
|
|
|
|
ret = (ret << 16) + getNET4MR();
|
|
|
|
|
ret = (ret << 24) + getNET6MR();
|
|
|
|
|
return (netmode_type)ret;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
void set_timeout(const net_timeout& t) {
|
|
|
|
|
setRCR(t.s_retry_cnt); setRTR(t.s_time_100us);
|
|
|
|
|
setSLRCR(t.sl_retry_cnt); setSLRTR(t.sl_time_100us);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
net_timeout get_timeout() {
|
|
|
|
|
return {getRCR(), getRTR(), getSLRCR(), getSLRTR()};
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
int8_t send_arp(arp_request& arp) {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp;
|
2026-04-05 15:32:20 +09:00
|
|
|
if (arp.destinfo.len == 16) { setSLDIP6R(const_cast<uint8_t*>(arp.destinfo.ip.data())); setSLCR(SLCR_ARP6); }
|
|
|
|
|
else { setSLDIP4R(const_cast<uint8_t*>(arp.destinfo.ip.data())); setSLCR(SLCR_ARP4); }
|
2026-04-04 20:21:38 +09:00
|
|
|
while (getSLCR());
|
|
|
|
|
while ((tmp = getSLIR()) == 0x00);
|
|
|
|
|
setSLIRCLR(~SLIR_RA);
|
2026-04-05 15:32:20 +09:00
|
|
|
if (tmp & (SLIR_ARP4 | SLIR_ARP6)) { getSLDHAR(arp.dha.data()); return 0; }
|
2026-04-04 20:21:38 +09:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
int8_t send_ping(const ping_request& ping) {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp;
|
2026-04-05 15:32:20 +09:00
|
|
|
setPINGIDR(ping.id); setPINGSEQR(ping.seq);
|
|
|
|
|
if (ping.destinfo.len == 16) { setSLDIP6R(const_cast<uint8_t*>(ping.destinfo.ip.data())); setSLCR(SLCR_PING6); }
|
|
|
|
|
else { setSLDIP4R(const_cast<uint8_t*>(ping.destinfo.ip.data())); setSLCR(SLCR_PING4); }
|
2026-04-04 20:21:38 +09:00
|
|
|
while (getSLCR());
|
|
|
|
|
while ((tmp = getSLIR()) == 0x00);
|
|
|
|
|
setSLIRCLR(~SLIR_RA);
|
|
|
|
|
if (tmp & (SLIR_PING4 | SLIR_PING6)) return 0;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
int8_t send_dad(std::span<const uint8_t, 16> ipv6) {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp;
|
2026-04-05 15:32:20 +09:00
|
|
|
setSLDIP6R(const_cast<uint8_t*>(ipv6.data())); setSLCR(SLCR_NS);
|
2026-04-04 20:21:38 +09:00
|
|
|
while (getSLCR());
|
|
|
|
|
while ((tmp = getSLIR()) == 0x00);
|
|
|
|
|
setSLIRCLR(~SLIR_RA);
|
|
|
|
|
if (tmp & SLIR_TOUT) return 0;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
int8_t send_slaac(prefix& pfx) {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp;
|
|
|
|
|
setSLCR(SLCR_RS);
|
|
|
|
|
while (getSLCR());
|
|
|
|
|
while ((tmp = getSLIR()) == 0x00);
|
|
|
|
|
setSLIRCLR(~SLIR_RA);
|
|
|
|
|
if (tmp & SLIR_RS) {
|
2026-04-05 15:32:20 +09:00
|
|
|
pfx.len = getPLR(); pfx.flag = getPFR();
|
|
|
|
|
pfx.valid_lifetime = getVLTR(); pfx.preferred_lifetime = getPLTR();
|
|
|
|
|
getPAR(pfx.prefix.data());
|
2026-04-04 20:21:38 +09:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
int8_t send_unsolicited() {
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp;
|
|
|
|
|
setSLCR(SLCR_UNA);
|
|
|
|
|
while (getSLCR());
|
|
|
|
|
while ((tmp = getSLIR()) == 0x00);
|
|
|
|
|
setSLIRCLR(~SLIR_RA);
|
|
|
|
|
if (tmp & SLIR_TOUT) return 0;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
int8_t get_prefix(prefix& pfx) {
|
2026-04-04 20:21:38 +09:00
|
|
|
if (getSLIR() & SLIR_RA) {
|
2026-04-05 15:32:20 +09:00
|
|
|
pfx.len = getPLR(); pfx.flag = getPFR();
|
|
|
|
|
pfx.valid_lifetime = getVLTR(); pfx.preferred_lifetime = getPLTR();
|
|
|
|
|
getPAR(pfx.prefix.data());
|
2026-04-04 20:21:38 +09:00
|
|
|
setSLIRCLR(SLIR_RA);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
constexpr uint16_t SOCK_ANY_PORT_NUM = 0xC000;
|
|
|
|
|
|
|
|
|
|
static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
|
2026-04-05 08:55:51 +09:00
|
|
|
static uint16_t sock_io_mode_bits = 0;
|
2026-04-04 20:21:38 +09:00
|
|
|
static uint16_t sock_is_sending = 0;
|
2026-04-05 07:01:43 +09:00
|
|
|
static uint16_t sock_remained_size[_SOCK_COUNT_] = {0,};
|
|
|
|
|
uint8_t sock_pack_info[_SOCK_COUNT_] = {0,};
|
2026-04-04 20:21:38 +09:00
|
|
|
|
2026-04-05 08:38:16 +09:00
|
|
|
#define FAIL(e) return std::unexpected(sock_error::e)
|
|
|
|
|
#define CHECK_SOCKNUM() do { if(sn >= _SOCK_COUNT_) FAIL(sock_num); } while(0)
|
|
|
|
|
#define CHECK_SOCKMODE(mode) do { if((getSn_MR(sn) & 0x0F) != mode) FAIL(sock_mode); } while(0)
|
|
|
|
|
#define CHECK_TCPMODE() do { if((getSn_MR(sn) & 0x03) != 0x01) FAIL(sock_mode); } while(0)
|
|
|
|
|
#define CHECK_UDPMODE() do { if((getSn_MR(sn) & 0x03) != 0x02) FAIL(sock_mode); } while(0)
|
|
|
|
|
#define CHECK_IPMODE() do { if((getSn_MR(sn) & 0x07) != 0x03) FAIL(sock_mode); } while(0)
|
|
|
|
|
#define CHECK_DGRAMMODE() do { if(getSn_MR(sn) == Sn_MR_CLOSED) FAIL(sock_mode); if((getSn_MR(sn) & 0x03) == 0x01) FAIL(sock_mode); } while(0)
|
|
|
|
|
#define CHECK_SOCKINIT() do { if((getSn_SR(sn) != SOCK_INIT)) FAIL(sock_init); } 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)
|
|
|
|
|
|
|
|
|
|
std::expected<socket_id, sock_error> open_socket(socket_id sid, protocol proto, port_num port, sock_flag flag) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
|
|
|
|
uint16_t p = static_cast<uint16_t>(port);
|
2026-04-05 08:18:28 +09:00
|
|
|
uint8_t pr = static_cast<uint8_t>(proto);
|
|
|
|
|
uint8_t fl = static_cast<uint8_t>(flag);
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t taddr[16];
|
|
|
|
|
CHECK_SOCKNUM();
|
2026-04-05 08:18:28 +09:00
|
|
|
switch (pr & 0x0F) {
|
2026-04-04 20:21:38 +09:00
|
|
|
case Sn_MR_TCP4:
|
|
|
|
|
getSIPR(taddr);
|
|
|
|
|
CHECK_IPZERO(taddr, 4);
|
|
|
|
|
break;
|
|
|
|
|
case Sn_MR_TCP6:
|
|
|
|
|
getLLAR(taddr);
|
|
|
|
|
CHECK_IPZERO(taddr, 16);
|
|
|
|
|
break;
|
|
|
|
|
case Sn_MR_TCPD:
|
|
|
|
|
getSIPR(taddr);
|
|
|
|
|
CHECK_IPZERO(taddr, 4);
|
|
|
|
|
getLLAR(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:
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_mode);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
if ((fl & 0x04) != 0) FAIL(sock_flag);
|
2026-04-04 20:21:38 +09:00
|
|
|
|
2026-04-05 08:18:28 +09:00
|
|
|
if (fl != 0) {
|
|
|
|
|
switch (pr) {
|
2026-04-04 20:21:38 +09:00
|
|
|
case Sn_MR_MACRAW:
|
2026-04-05 08:38:16 +09:00
|
|
|
if ((fl & (Sn_MR2_DHAM | Sn_MR2_FARP)) != 0) FAIL(sock_flag);
|
2026-04-04 20:21:38 +09:00
|
|
|
break;
|
|
|
|
|
case Sn_MR_TCP4:
|
|
|
|
|
case Sn_MR_TCP6:
|
|
|
|
|
case Sn_MR_TCPD:
|
2026-04-05 08:38:16 +09:00
|
|
|
if ((fl & (Sn_MR_MULTI | Sn_MR_UNIB)) != 0) FAIL(sock_flag);
|
2026-04-04 20:21:38 +09:00
|
|
|
break;
|
|
|
|
|
case Sn_MR_IPRAW4:
|
|
|
|
|
case Sn_MR_IPRAW6:
|
2026-04-05 08:38:16 +09:00
|
|
|
if (fl != 0) FAIL(sock_flag);
|
2026-04-04 20:21:38 +09:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 07:30:50 +09:00
|
|
|
close(sid);
|
2026-04-05 08:18:28 +09:00
|
|
|
setSn_MR(sn, (pr | (fl & 0xF0)));
|
|
|
|
|
setSn_MR2(sn, fl & 0x03);
|
2026-04-05 07:30:50 +09:00
|
|
|
if (!p) {
|
|
|
|
|
p = sock_any_port++;
|
2026-04-04 20:21:38 +09:00
|
|
|
if (sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM;
|
|
|
|
|
}
|
2026-04-05 07:30:50 +09:00
|
|
|
setSn_PORTR(sn, p);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_OPEN);
|
|
|
|
|
while (getSn_CR(sn));
|
2026-04-05 08:55:51 +09:00
|
|
|
sock_io_mode_bits &= ~(1 << sn);
|
|
|
|
|
sock_io_mode_bits |= ((fl & (static_cast<uint8_t>(sock_flag::io_nonblock) >> 3)) << sn);
|
2026-04-04 20:21:38 +09:00
|
|
|
sock_is_sending &= ~(1 << sn);
|
|
|
|
|
sock_remained_size[sn] = 0;
|
2026-04-05 08:18:28 +09:00
|
|
|
sock_pack_info[sn] = static_cast<uint8_t>(pack_info::completed);
|
2026-04-04 20:21:38 +09:00
|
|
|
while (getSn_SR(sn) == SOCK_CLOSED);
|
2026-04-05 08:38:16 +09:00
|
|
|
return sid;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 08:38:16 +09:00
|
|
|
std::expected<void, sock_error> close(socket_id sid) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-04 20:21:38 +09:00
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
setSn_CR(sn, Sn_CR_CLOSE);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
setSn_IR(sn, 0xFF);
|
2026-04-05 08:55:51 +09:00
|
|
|
sock_io_mode_bits &= ~(1 << sn);
|
2026-04-04 20:21:38 +09:00
|
|
|
sock_is_sending &= ~(1 << sn);
|
|
|
|
|
sock_remained_size[sn] = 0;
|
|
|
|
|
sock_pack_info[sn] = PACK_NONE;
|
|
|
|
|
while (getSn_SR(sn) != SOCK_CLOSED);
|
2026-04-05 08:38:16 +09:00
|
|
|
return {};
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 08:38:16 +09:00
|
|
|
std::expected<void, sock_error> listen(socket_id sid) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-04 20:21:38 +09:00
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
CHECK_TCPMODE();
|
|
|
|
|
CHECK_SOCKINIT();
|
|
|
|
|
setSn_CR(sn, Sn_CR_LISTEN);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
while (getSn_SR(sn) != SOCK_LISTEN) {
|
2026-04-05 07:30:50 +09:00
|
|
|
close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_closed);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
return {};
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
std::expected<void, sock_error> connect(socket_id sid, const ip_address& addr, port_num port) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
|
|
|
|
uint16_t p = static_cast<uint16_t>(port);
|
2026-04-05 15:32:20 +09:00
|
|
|
uint8_t addrlen = addr.len;
|
2026-04-04 20:21:38 +09:00
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
CHECK_TCPMODE();
|
|
|
|
|
CHECK_SOCKINIT();
|
2026-04-05 15:32:20 +09:00
|
|
|
CHECK_IPZERO(addr.ip.data(), addrlen);
|
2026-04-05 08:38:16 +09:00
|
|
|
if (p == 0) FAIL(port_zero);
|
2026-04-04 20:21:38 +09:00
|
|
|
|
2026-04-05 07:30:50 +09:00
|
|
|
setSn_DPORTR(sn, p);
|
2026-04-04 20:21:38 +09:00
|
|
|
if (addrlen == 16) {
|
|
|
|
|
if (getSn_MR(sn) & 0x08) {
|
2026-04-05 15:32:20 +09:00
|
|
|
setSn_DIP6R(sn, const_cast<uint8_t*>(addr.ip.data()));
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_CONNECT6);
|
|
|
|
|
} else {
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_mode);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-04-05 08:38:16 +09:00
|
|
|
if (getSn_MR(sn) == Sn_MR_TCP6) FAIL(sock_mode);
|
2026-04-05 15:32:20 +09:00
|
|
|
setSn_DIPR(sn, const_cast<uint8_t*>(addr.ip.data()));
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_CONNECT);
|
|
|
|
|
}
|
|
|
|
|
while (getSn_CR(sn));
|
2026-04-05 08:55:51 +09:00
|
|
|
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
while (getSn_SR(sn) != SOCK_ESTABLISHED) {
|
|
|
|
|
if (getSn_IR(sn) & Sn_IR_TIMEOUT) {
|
|
|
|
|
setSn_IR(sn, Sn_IR_TIMEOUT);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(timeout);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
if (getSn_SR(sn) == SOCK_CLOSED) FAIL(sock_closed);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
return {};
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 08:38:16 +09:00
|
|
|
std::expected<void, sock_error> disconnect(socket_id sid) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-04 20:21:38 +09:00
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
CHECK_TCPMODE();
|
|
|
|
|
if (getSn_SR(sn) != SOCK_CLOSED) {
|
|
|
|
|
setSn_CR(sn, Sn_CR_DISCON);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
sock_is_sending &= ~(1 << sn);
|
2026-04-05 08:55:51 +09:00
|
|
|
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
while (getSn_SR(sn) != SOCK_CLOSED) {
|
|
|
|
|
if (getSn_IR(sn) & Sn_IR_TIMEOUT) {
|
2026-04-05 07:30:50 +09:00
|
|
|
close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(timeout);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
return {};
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 08:55:51 +09:00
|
|
|
std::expected<uint16_t, sock_error> send(socket_id sid, std::span<const uint8_t> buf) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp = 0;
|
|
|
|
|
uint16_t freesize = 0;
|
2026-04-05 08:55:51 +09:00
|
|
|
uint16_t len = buf.size();
|
2026-04-04 20:21:38 +09:00
|
|
|
|
|
|
|
|
freesize = getSn_TxMAX(sn);
|
|
|
|
|
if (len > freesize) len = freesize;
|
|
|
|
|
while (1) {
|
|
|
|
|
freesize = (uint16_t)getSn_TX_FSR(sn);
|
|
|
|
|
tmp = getSn_SR(sn);
|
|
|
|
|
if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT)) {
|
2026-04-05 07:30:50 +09:00
|
|
|
if (tmp == SOCK_CLOSED) close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_status);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 08:55:51 +09:00
|
|
|
if ((sock_io_mode_bits & (1 << sn)) && (len > freesize)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
if (len <= freesize) break;
|
|
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
send_data(sn, const_cast<uint8_t*>(buf.data()), len);
|
2026-04-04 20:21:38 +09:00
|
|
|
if (sock_is_sending & (1 << sn)) {
|
|
|
|
|
while (!(getSn_IR(sn) & Sn_IR_SENDOK)) {
|
|
|
|
|
tmp = getSn_SR(sn);
|
|
|
|
|
if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT)) {
|
2026-04-05 07:30:50 +09:00
|
|
|
if ((tmp == SOCK_CLOSED) || (getSn_IR(sn) & Sn_IR_TIMEOUT)) close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_status);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-05 08:55:51 +09:00
|
|
|
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
setSn_IR(sn, Sn_IR_SENDOK);
|
|
|
|
|
}
|
|
|
|
|
setSn_CR(sn, Sn_CR_SEND);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
sock_is_sending |= (1 << sn);
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 08:55:51 +09:00
|
|
|
std::expected<uint16_t, sock_error> recv(socket_id sid, std::span<uint8_t> buf) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-05 08:55:51 +09:00
|
|
|
uint16_t len = buf.size();
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp = 0;
|
|
|
|
|
uint16_t recvsize = 0;
|
|
|
|
|
|
|
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
CHECK_SOCKMODE(Sn_MR_TCP);
|
|
|
|
|
CHECK_SOCKDATA();
|
|
|
|
|
|
|
|
|
|
recvsize = getSn_RxMAX(sn);
|
|
|
|
|
if (recvsize < len) len = recvsize;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
recvsize = (uint16_t)getSn_RX_RSR(sn);
|
|
|
|
|
tmp = getSn_SR(sn);
|
|
|
|
|
if (tmp != SOCK_ESTABLISHED) {
|
|
|
|
|
if (tmp == SOCK_CLOSE_WAIT) {
|
|
|
|
|
if (recvsize != 0) break;
|
|
|
|
|
else if (getSn_TX_FSR(sn) == getSn_TxMAX(sn)) {
|
2026-04-05 07:30:50 +09:00
|
|
|
close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_status);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-04-05 07:30:50 +09:00
|
|
|
close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_status);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (recvsize != 0) break;
|
2026-04-05 08:55:51 +09:00
|
|
|
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (recvsize < len) len = recvsize;
|
2026-04-05 09:09:10 +09:00
|
|
|
recv_data(sn, buf.data(), len);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_RECV);
|
|
|
|
|
while (getSn_CR(sn));
|
2026-04-05 08:38:16 +09:00
|
|
|
return len;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_t> buf, const ip_address& addr, port_num port) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
|
|
|
|
uint16_t p = static_cast<uint16_t>(port);
|
2026-04-05 08:55:51 +09:00
|
|
|
uint16_t len = buf.size();
|
2026-04-05 15:32:20 +09:00
|
|
|
uint8_t addrlen = addr.len;
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t tmp = 0;
|
|
|
|
|
uint8_t tcmd = Sn_CR_SEND;
|
|
|
|
|
uint16_t freesize = 0;
|
|
|
|
|
|
|
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
switch (getSn_MR(sn) & 0x0F) {
|
|
|
|
|
case Sn_MR_UDP:
|
|
|
|
|
case Sn_MR_MACRAW:
|
|
|
|
|
case Sn_MR_IPRAW:
|
|
|
|
|
case Sn_MR_IPRAW6:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_mode);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
tmp = getSn_MR(sn);
|
|
|
|
|
if (tmp != Sn_MR_MACRAW) {
|
|
|
|
|
if (addrlen == 16) {
|
|
|
|
|
if (tmp & 0x08) {
|
2026-04-05 15:32:20 +09:00
|
|
|
setSn_DIP6R(sn, const_cast<uint8_t*>(addr.ip.data()));
|
2026-04-04 20:21:38 +09:00
|
|
|
tcmd = Sn_CR_SEND6;
|
|
|
|
|
} else {
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_mode);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
} else if (addrlen == 4) {
|
2026-04-05 08:38:16 +09:00
|
|
|
if (tmp == Sn_MR_UDP6 || tmp == Sn_MR_IPRAW6) FAIL(sock_mode);
|
2026-04-05 15:32:20 +09:00
|
|
|
setSn_DIPR(sn, const_cast<uint8_t*>(addr.ip.data()));
|
2026-04-04 20:21:38 +09:00
|
|
|
tcmd = Sn_CR_SEND;
|
|
|
|
|
} else {
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(ip_invalid);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((tmp & 0x03) == 0x02) {
|
2026-04-05 07:30:50 +09:00
|
|
|
if (p) {
|
|
|
|
|
setSn_DPORTR(sn, p);
|
2026-04-04 20:21:38 +09:00
|
|
|
} else {
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(port_zero);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freesize = getSn_TxMAX(sn);
|
|
|
|
|
if (len > freesize) len = freesize;
|
|
|
|
|
while (1) {
|
|
|
|
|
freesize = getSn_TX_FSR(sn);
|
2026-04-05 08:38:16 +09:00
|
|
|
if (getSn_SR(sn) == SOCK_CLOSED) FAIL(sock_closed);
|
2026-04-05 08:55:51 +09:00
|
|
|
if ((sock_io_mode_bits & (1 << sn)) && (len > freesize)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
if (len <= freesize) break;
|
|
|
|
|
};
|
2026-04-05 09:09:10 +09:00
|
|
|
send_data(sn, const_cast<uint8_t*>(buf.data()), len);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, tcmd);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
while (1) {
|
|
|
|
|
tmp = getSn_IR(sn);
|
|
|
|
|
if (tmp & Sn_IR_SENDOK) {
|
|
|
|
|
setSn_IR(sn, Sn_IR_SENDOK);
|
|
|
|
|
break;
|
|
|
|
|
} else if (tmp & Sn_IR_TIMEOUT) {
|
|
|
|
|
setSn_IR(sn, Sn_IR_TIMEOUT);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(timeout);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
return len;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> buf, ip_address& addr, port_num& port) {
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-05 08:55:51 +09:00
|
|
|
uint16_t len = buf.size();
|
2026-04-04 20:21:38 +09:00
|
|
|
uint8_t mr;
|
|
|
|
|
uint8_t head[8];
|
|
|
|
|
uint16_t pack_len = 0;
|
|
|
|
|
|
|
|
|
|
CHECK_SOCKNUM();
|
|
|
|
|
CHECK_SOCKDATA();
|
|
|
|
|
|
|
|
|
|
switch ((mr = getSn_MR(sn)) & 0x0F) {
|
|
|
|
|
case Sn_MR_UDP:
|
|
|
|
|
case Sn_MR_IPRAW:
|
|
|
|
|
case Sn_MR_IPRAW6:
|
|
|
|
|
case Sn_MR_MACRAW:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(sock_mode);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sock_remained_size[sn] == 0) {
|
|
|
|
|
while (1) {
|
|
|
|
|
pack_len = getSn_RX_RSR(sn);
|
2026-04-05 08:38:16 +09:00
|
|
|
if (getSn_SR(sn) == SOCK_CLOSED) FAIL(sock_closed);
|
2026-04-04 20:21:38 +09:00
|
|
|
if (pack_len != 0) {
|
|
|
|
|
sock_pack_info[sn] = PACK_NONE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-04-05 08:55:51 +09:00
|
|
|
if (sock_io_mode_bits & (1 << sn)) FAIL(busy);
|
2026-04-04 20:21:38 +09:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 09:09:10 +09:00
|
|
|
recv_data(sn, head, 2);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_RECV);
|
|
|
|
|
while (getSn_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;
|
2026-04-05 15:32:20 +09:00
|
|
|
if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16;
|
|
|
|
|
else addr.len = 4;
|
|
|
|
|
recv_data(sn, addr.ip.data(), addr.len);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_RECV);
|
|
|
|
|
while (getSn_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) {
|
2026-04-05 07:30:50 +09:00
|
|
|
close(sid);
|
2026-04-05 08:38:16 +09:00
|
|
|
FAIL(fatal_packlen);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
sock_pack_info[sn] = PACK_FIRST;
|
|
|
|
|
}
|
|
|
|
|
if (len < sock_remained_size[sn]) pack_len = len;
|
|
|
|
|
else pack_len = sock_remained_size[sn];
|
2026-04-05 09:09:10 +09:00
|
|
|
recv_data(sn, buf.data(), pack_len);
|
2026-04-04 20:21:38 +09:00
|
|
|
break;
|
|
|
|
|
case Sn_MR_IPRAW6:
|
|
|
|
|
case Sn_MR_IPRAW4:
|
|
|
|
|
if (sock_remained_size[sn] == 0) {
|
|
|
|
|
sock_pack_info[sn] = head[0] & 0xF8;
|
2026-04-05 15:32:20 +09:00
|
|
|
if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16;
|
|
|
|
|
else addr.len = 4;
|
|
|
|
|
recv_data(sn, addr.ip.data(), addr.len);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_RECV);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2026-04-05 09:09:10 +09:00
|
|
|
recv_ignore(sn, pack_len);
|
2026-04-04 20:21:38 +09:00
|
|
|
sock_remained_size[sn] = pack_len;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sock_remained_size[sn] = pack_len;
|
|
|
|
|
sock_pack_info[sn] |= PACK_FIRST;
|
|
|
|
|
if ((getSn_MR(sn) & 0x03) == 0x02) {
|
2026-04-05 09:09:10 +09:00
|
|
|
recv_data(sn, head, 2);
|
2026-04-05 15:32:20 +09:00
|
|
|
port = static_cast<port_num>((((uint16_t)head[0]) << 8) + head[1]);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_RECV);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len < sock_remained_size[sn]) pack_len = len;
|
|
|
|
|
else pack_len = sock_remained_size[sn];
|
2026-04-05 09:09:10 +09:00
|
|
|
recv_data(sn, buf.data(), pack_len);
|
2026-04-04 20:21:38 +09:00
|
|
|
setSn_CR(sn, Sn_CR_RECV);
|
|
|
|
|
while (getSn_CR(sn));
|
|
|
|
|
|
|
|
|
|
sock_remained_size[sn] -= pack_len;
|
|
|
|
|
if (sock_remained_size[sn] != 0) sock_pack_info[sn] |= PACK_REMAINED;
|
|
|
|
|
else sock_pack_info[sn] |= PACK_COMPLETED;
|
|
|
|
|
|
2026-04-05 08:38:16 +09:00
|
|
|
return pack_len;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-05 08:55:51 +09:00
|
|
|
std::optional<uint16_t> peek_socket_msg(socket_id sid, std::span<const uint8_t> submsg) {
|
|
|
|
|
uint16_t subsize = submsg.size();
|
2026-04-05 07:30:50 +09:00
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
2026-04-04 20:21:38 +09:00
|
|
|
uint32_t rx_ptr = 0;
|
|
|
|
|
uint16_t i = 0, sub_idx = 0;
|
|
|
|
|
if ((getSn_RX_RSR(sn) > 0) && (subsize > 0)) {
|
2026-04-05 09:09:10 +09:00
|
|
|
rx_ptr = ((uint32_t)getSn_RX_RD(sn) << 8) + RXBUF_BLOCK(sn);
|
2026-04-04 20:21:38 +09:00
|
|
|
sub_idx = 0;
|
|
|
|
|
for (i = 0; i < getSn_RX_RSR(sn); i++) {
|
2026-04-05 09:09:10 +09:00
|
|
|
if (reg_read(rx_ptr) == submsg[sub_idx]) {
|
2026-04-04 20:21:38 +09:00
|
|
|
sub_idx++;
|
2026-04-05 08:38:16 +09:00
|
|
|
if (sub_idx == subsize) return static_cast<uint16_t>(i + 1 - sub_idx);
|
2026-04-04 20:21:38 +09:00
|
|
|
} else {
|
|
|
|
|
sub_idx = 0;
|
|
|
|
|
}
|
2026-04-05 09:09:10 +09:00
|
|
|
rx_ptr = offset_inc(rx_ptr, 1);
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 08:38:16 +09:00
|
|
|
return std::nullopt;
|
2026-04-04 20:21:38 +09:00
|
|
|
}
|
2026-04-04 20:47:25 +09:00
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void reset() {
|
2026-04-04 20:47:25 +09:00
|
|
|
gpio_init(PIN_RST);
|
|
|
|
|
gpio_set_dir(PIN_RST, GPIO_OUT);
|
|
|
|
|
gpio_put(PIN_RST, 0);
|
|
|
|
|
sleep_ms(100);
|
|
|
|
|
gpio_put(PIN_RST, 1);
|
|
|
|
|
sleep_ms(100);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void init_spi() {
|
2026-04-05 09:09:10 +09:00
|
|
|
pio_init();
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void init_critical_section() {
|
2026-04-04 20:47:25 +09:00
|
|
|
critical_section_init(&g_cris_sec);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
void init() {
|
2026-04-05 09:09:10 +09:00
|
|
|
pio_frame_end();
|
2026-04-05 15:32:20 +09:00
|
|
|
std::array<uint8_t, 8> txsize = {4, 4, 4, 4, 4, 4, 4, 4};
|
|
|
|
|
std::array<uint8_t, 8> rxsize = {4, 4, 4, 4, 4, 4, 4, 4};
|
|
|
|
|
init_buffers(txsize, rxsize);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
bool check() {
|
2026-04-04 20:47:25 +09:00
|
|
|
return getCIDR() == 0x6300;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:32:20 +09:00
|
|
|
void init_net(const net_info& info) {
|
|
|
|
|
CHIPUNLOCK();
|
|
|
|
|
NETUNLOCK();
|
|
|
|
|
set_net_info(info);
|
2026-04-04 20:47:25 +09:00
|
|
|
}
|
2026-04-05 07:01:43 +09:00
|
|
|
|
2026-04-05 08:55:51 +09:00
|
|
|
std::expected<void, sock_error> set_socket_io_mode(socket_id sid, sock_io_mode mode) {
|
|
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
|
|
|
|
if (sn >= _SOCK_COUNT_) FAIL(sock_num);
|
|
|
|
|
if (mode == sock_io_mode::nonblock) sock_io_mode_bits |= (1 << sn);
|
|
|
|
|
else if (mode == sock_io_mode::block) sock_io_mode_bits &= ~(1 << sn);
|
|
|
|
|
else FAIL(arg);
|
|
|
|
|
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 getSn_TxMAX(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t get_socket_max_rx(socket_id sid) {
|
|
|
|
|
return getSn_RxMAX(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);
|
|
|
|
|
setSn_IR(sn, flags);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_interrupt(socket_id sid) {
|
|
|
|
|
return getSn_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);
|
|
|
|
|
setSn_IMR(sn, mask);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_interrupt_mask(socket_id sid) {
|
|
|
|
|
return getSn_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);
|
|
|
|
|
setSn_PSR(sn, v);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcv6_prefer get_socket_prefer(socket_id sid) {
|
|
|
|
|
return static_cast<srcv6_prefer>(getSn_PSR(static_cast<uint8_t>(sid)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_socket_ttl(socket_id sid, uint8_t ttl) {
|
|
|
|
|
setSn_TTL(static_cast<uint8_t>(sid), ttl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_ttl(socket_id sid) {
|
|
|
|
|
return getSn_TTL(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_socket_tos(socket_id sid, uint8_t tos) {
|
|
|
|
|
setSn_TOS(static_cast<uint8_t>(sid), tos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_tos(socket_id sid) {
|
|
|
|
|
return getSn_TOS(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_socket_mss(socket_id sid, uint16_t mss) {
|
|
|
|
|
setSn_MSSR(static_cast<uint8_t>(sid), mss);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t get_socket_mss(socket_id sid) {
|
|
|
|
|
return getSn_MSSR(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_socket_dest_ip(socket_id sid, const ip_address& addr) {
|
|
|
|
|
uint8_t sn = static_cast<uint8_t>(sid);
|
|
|
|
|
if (addr.len == 16) setSn_DIP6R(sn, const_cast<uint8_t*>(addr.ip.data()));
|
|
|
|
|
else setSn_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 (getSn_ESR(sn) & TCPSOCK_MODE) {
|
|
|
|
|
getSn_DIP6R(sn, addr.ip.data());
|
|
|
|
|
addr.len = 16;
|
|
|
|
|
} else {
|
|
|
|
|
getSn_DIPR(sn, addr.ip.data());
|
|
|
|
|
addr.len = 4;
|
|
|
|
|
}
|
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_socket_dest_port(socket_id sid, port_num port) {
|
|
|
|
|
setSn_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>(getSn_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 ((getSn_MR(sn) & 0x03) != 0x01) FAIL(sock_mode);
|
|
|
|
|
if (getSn_KPALVTR(sn) != 0) FAIL(sock_opt);
|
|
|
|
|
setSn_CR(sn, Sn_CR_SEND_KEEP);
|
|
|
|
|
while (getSn_CR(sn) != 0) {
|
|
|
|
|
if (getSn_IR(sn) & Sn_IR_TIMEOUT) {
|
|
|
|
|
setSn_IR(sn, Sn_IR_TIMEOUT);
|
|
|
|
|
FAIL(timeout);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_socket_keepalive_auto(socket_id sid, uint8_t interval) {
|
|
|
|
|
setSn_KPALVTR(static_cast<uint8_t>(sid), interval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_keepalive_auto(socket_id sid) {
|
|
|
|
|
return getSn_KPALVTR(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t get_socket_send_buf(socket_id sid) {
|
|
|
|
|
return getSn_TX_FSR(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t get_socket_recv_buf(socket_id sid) {
|
|
|
|
|
return getSn_RX_RSR(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_status(socket_id sid) {
|
|
|
|
|
return getSn_SR(static_cast<uint8_t>(sid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_ext_status(socket_id sid) {
|
|
|
|
|
return getSn_ESR(static_cast<uint8_t>(sid)) & 0x07;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t get_socket_mode(socket_id sid) {
|
|
|
|
|
return getSn_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 (getSn_MR(sn) & 0x01) return getSn_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)]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 07:01:43 +09:00
|
|
|
} // namespace w6300
|