forked from 981213/ch347-nor-prog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi-op.cpp
66 lines (58 loc) · 1.66 KB
/
spi-op.cpp
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
//
// Copyright (c) 2022 Chuanhong Guo <gch981213@gmail.com>
//
#include "spi-op.h"
#include "ch347.h"
#include <cstdlib>
#include <cstring>
struct ch347_priv *priv;
bool SPIDeviceInit(int *clk_khz) {
int ret;
priv = ch347_open();
if (!priv)
exit(-1);
ret = ch347_setup_spi(priv, 3, false, false, false);
if (ret)
return false;
int freq = 6000;
ret = ch347_set_spi_freq(priv, clk_khz ? clk_khz : &freq);
return ret == 0;
}
void SPIDeviceRelease() {
ch347_close(priv);
}
bool SPIWrite(const unsigned char *data, unsigned int size) {
int ret;
ch347_set_cs(priv, 0, 0);
ret = ch347_spi_tx(priv, data, size);
ch347_set_cs(priv, 0, 1);
return ret == 0;
}
bool SPIWriteTwo(const unsigned char *in1, unsigned int in1_size, const unsigned char *in2, unsigned int in2_size) {
int ret;
ch347_set_cs(priv, 0, 0);
ret = ch347_spi_tx(priv, in1, in1_size);
if (ret)
return false;
ret = ch347_spi_tx(priv, in2, in2_size);
ch347_set_cs(priv, 0, 1);
return ret == 0;
}
bool SPIWriteThenRead(const unsigned char *in, unsigned int in_size, unsigned char *out, unsigned int out_size) {
int ret;
uint8_t buf[16];
ch347_set_cs(priv, 0, 0);
if (in_size + out_size <= sizeof(buf)) {
memcpy(buf, in, in_size);
memset(buf + in_size, 0, out_size);
ret = ch347_spi_trx_full_duplex(priv, buf, in_size + out_size);
memcpy(out, buf + in_size, out_size);
} else {
ret = ch347_spi_tx(priv, in, in_size);
if (ret)
return false;
ret = ch347_spi_rx(priv, out, out_size);
}
ch347_set_cs(priv, 0, 1);
return ret == 0;
}