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
|
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2022 Matthias Schiffer <mschiffer@universe-factory.net>
*/
#include <spi.h>
#include <spi-nor.h>
#include <stdio.h>
#define CMD_READ 0x03
#define CMD_READ_ID 0x9F
int spi_nor_read_id(void)
{
int ret;
const uint8_t tx_buf[1] = {CMD_READ_ID};
uint8_t rx_buf[3] = {};
struct spi_transfer t[2] = {{
.tx_buf = tx_buf,
.rx_buf = NULL,
.len = sizeof(tx_buf),
}, {
.tx_buf = NULL,
.rx_buf = rx_buf,
.len = sizeof(rx_buf),
}};
ret = spi_xfer(t, ARRAY_SIZE(t));
if (ret) {
puts("SPI transfer failed\n");
return ret;
}
puts("Flash JECED ID: ");
put_array(rx_buf, sizeof(rx_buf));
return 0;
}
int spi_nor_read_data(void *dest, size_t pos, size_t len)
{
int ret;
while (len > 0) {
uint8_t cmd[4] = {CMD_READ, pos >> 16, pos >> 8, pos};
size_t block_len = min(len, spi_max_xfer() - sizeof(cmd));
struct spi_transfer t[2] = {{
.tx_buf = cmd,
.rx_buf = NULL,
.len = sizeof(cmd),
}, {
.tx_buf = NULL,
.rx_buf = dest,
.len = block_len,
}};
ret = spi_xfer(t, ARRAY_SIZE(t));
if (ret) {
puts("SPI transfer failed\n");
return ret;
}
pos += block_len;
dest += block_len;
len -= block_len;
}
return 0;
}
|