summaryrefslogtreecommitdiff
path: root/libhw_cr/w5500_ll.c
blob: c60e045b719b5608a78c24ea32aa3cec68ae1b44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* libhw_cr/w5500_ll.c - Low-level library for the WIZnet W5500 chip
 *
 * Based entirely on the W5500 datasheet (v1.1.0), not on reference code.
 * https://docs.wiznet.io/img/products/w5500/W5500_ds_v110e.pdf
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <string.h> /* for memcmp() and mempy() */

#include <libmisc/alloc.h>  /* for stack_alloc() */
#include <libmisc/fmt.h>

#include "w5500_ll.h"

#if CONFIG_W5500_LL_DEBUG
static void fmt_print_ctl_block(lo_interface fmt_dest, uint8_t b) {
	static char *strs[] = {
		"RES",
		"REG",
		"TX",
		"RX",
	};
	fmt_print("CTL_BLOCK_SOCK(", (base10, (((b)>>5) & 0b111)), ", ", strs[((b)>>3)&0b11], ")");
}
#endif

void _w5500ll_n_writev(const char *func,
                       lo_interface spi spidev, uint16_t addr, uint8_t block,
                       const struct iovec *iov, int iovcnt,
                       size_t skip, size_t max)
{
	assert(!LO_IS_NULL(spidev));
	assert((block & ~CTL_MASK_BLOCK) == 0);
	assert(iov);
	assert(iovcnt > 0);
#if CONFIG_W5500_LL_DEBUG
	log_n_debugln(W5500_LL,
	              func, "(): w5500ll_write(spidev",
	              ", addr=", (base16_u16_, addr),
	              ", block=", (ctl_block, block),
	              ", iov",
	              ", iovcnt=", iovcnt,
	              ")");
#endif

	uint8_t header[] = {
		(uint8_t)((addr >> 8) & 0xFF),
		(uint8_t)(addr & 0xFF),
		(block & CTL_MASK_BLOCK) | CTL_W | CTL_OM_VDM,
	};
	int inner_cnt = 1+io_slice_cnt(iov, iovcnt, skip, max);
	struct duplex_iovec *inner = stack_alloc(inner_cnt, struct duplex_iovec);
	inner[0] = (struct duplex_iovec){
		.iov_read_to    = IOVEC_DISCARD,
		.iov_write_from = header,
		.iov_len        = sizeof(header),
	};
	io_slice_wr_to_duplex(&inner[1], iov, iovcnt, skip, max);
	LO_CALL(spidev, readwritev, inner, inner_cnt);
}


void _w5500ll_n_readv(const char *func,
                      lo_interface spi spidev, uint16_t addr, uint8_t block,
                      const struct iovec *iov, int iovcnt,
                      size_t max)
{
	assert(!LO_IS_NULL(spidev));
	assert((block & ~CTL_MASK_BLOCK) == 0);
	assert(iov);
	assert(iovcnt > 0);
#if CONFIG_W5500_LL_DEBUG
	log_n_debugln(W5500_LL,
	              func, "(): w5500ll_read(spidev",
	              ", addr=", (base16_u16_, addr),
	              ", block=", (ctl_block, block),
	              ", iov",
	              ", iovcnt=", iovcnt,
	              ")");
#endif

	uint8_t header[] = {
		(uint8_t)((addr >> 8) & 0xFF),
		(uint8_t)(addr & 0xFF),
		(block & CTL_MASK_BLOCK) | CTL_R | CTL_OM_VDM,
	};
	int inner_cnt = 1+io_slice_cnt(iov, iovcnt, 0, max);
	struct duplex_iovec *inner = stack_alloc(inner_cnt, struct duplex_iovec);
	inner[0] = (struct duplex_iovec){
		.iov_read_to    = IOVEC_DISCARD,
		.iov_write_from = header,
		.iov_len        = sizeof(header),
	};
	io_slice_rd_to_duplex(&inner[1], iov, iovcnt, 0, max);
	LO_CALL(spidev, readwritev, inner, inner_cnt);
}

void _w5500ll_n_read_repeated(const char *func,
                              lo_interface spi spidev, uint16_t addr, uint8_t block,
                              void *buf, void *buf_scratch, size_t len)
{
               _w5500ll_n_readv(func, spidev, addr, block,
                                &((struct iovec){.iov_base=buf, .iov_len=len}), 1,
                                0);
               for (;;) {
	               _w5500ll_n_readv(func, spidev, addr, block,
	                                &((struct iovec){.iov_base=buf_scratch, .iov_len=len}), 1,
	                                0);
	               if (memcmp(buf, buf_scratch, len) == 0)
		               break;
	               memcpy(buf, buf_scratch, len);
               }
}