Skip to content

Commit 0e3d472

Browse files
committed
attempt to change stuff
1 parent f20a46d commit 0e3d472

File tree

3 files changed

+238
-55
lines changed

3 files changed

+238
-55
lines changed

module/core/src/device/wombat/wombat_device.cpp

+53-54
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class WombatDevice : public kipr::core::Device
2828
{
2929
public:
3030
WombatDevice()
31-
: spi_fd_(-1)
31+
: spi_fd_(-1),
32+
count(0)
3233
{
3334
spi_fd_ = open(SPI_FILE_SYSTEM, O_RDWR);
3435
if (spi_fd_ <= 0)
@@ -51,7 +52,7 @@ class WombatDevice : public kipr::core::Device
5152
{
5253
std::uint8_t read_buffer[REG_ALL_COUNT];
5354

54-
transfer(nullptr, read_buffer, sizeof (read_buffer));
55+
transfer(nullptr, read_buffer, sizeof(read_buffer));
5556

5657
return read_buffer[address];
5758
}
@@ -60,87 +61,84 @@ class WombatDevice : public kipr::core::Device
6061
{
6162
std::uint8_t read_buffer[REG_ALL_COUNT];
6263

63-
transfer(nullptr, read_buffer, sizeof (read_buffer));
64+
transfer(nullptr, read_buffer, sizeof(read_buffer));
6465

6566
return (
66-
read_buffer[address] << 8 |
67-
read_buffer[address + 1] << 0
68-
);
67+
read_buffer[address] << 8 |
68+
read_buffer[address + 1] << 0);
6969
}
7070

7171
virtual std::uint32_t r32(const std::uint8_t address) override
7272
{
7373
std::uint8_t read_buffer[REG_ALL_COUNT];
7474

75-
transfer(nullptr, read_buffer, sizeof (read_buffer));
75+
transfer(nullptr, read_buffer, sizeof(read_buffer));
7676

7777
return (
78-
read_buffer[address] << 24 |
79-
read_buffer[address + 1] << 16 |
80-
read_buffer[address + 2] << 8 |
81-
read_buffer[address + 3] << 0
82-
);
78+
read_buffer[address] << 24 |
79+
read_buffer[address + 1] << 16 |
80+
read_buffer[address + 2] << 8 |
81+
read_buffer[address + 3] << 0);
8382
}
8483

8584
virtual void w8(const std::uint8_t address, const std::uint8_t value) override
8685
{
8786
const std::uint8_t write_buffer[7] = {
88-
'J',
89-
WALLABY_SPI_VERSION,
90-
1,
91-
1,
92-
address,
93-
value,
94-
'S'
95-
};
96-
97-
transfer(write_buffer, nullptr, sizeof (write_buffer));
87+
'J',
88+
WALLABY_SPI_VERSION,
89+
count,
90+
1,
91+
address,
92+
value,
93+
'S'};
94+
++count;
95+
96+
transfer(write_buffer, nullptr, sizeof(write_buffer));
9897
}
9998

10099
virtual void w16(const std::uint8_t address, const std::uint16_t value) override
101100
{
102101
const std::uint8_t write_buffer[9] = {
103-
'J',
104-
WALLABY_SPI_VERSION,
105-
1,
106-
2,
107-
address,
108-
(value & 0xFF00) >> 8,
109-
address + 1,
110-
(value & 0x00FF) >> 0,
111-
'S'
112-
};
113-
114-
transfer(write_buffer, nullptr, sizeof (write_buffer));
102+
'J',
103+
WALLABY_SPI_VERSION,
104+
count,
105+
2,
106+
address,
107+
(value & 0xFF00) >> 8,
108+
address + 1,
109+
(value & 0x00FF) >> 0,
110+
'S'};
111+
++count;
112+
113+
transfer(write_buffer, nullptr, sizeof(write_buffer));
115114
}
116115

117116
virtual void w32(const std::uint8_t address, const std::uint32_t value) override
118117
{
119118
const std::uint8_t write_buffer[13] = {
120-
'J',
121-
WALLABY_SPI_VERSION,
122-
1,
123-
4,
124-
address,
125-
(value & 0xFF000000) >> 24,
126-
address + 1,
127-
(value & 0x00FF0000) >> 16,
128-
address + 2,
129-
(value & 0x0000FF00) >> 8,
130-
address + 3,
131-
(value & 0x000000FF) >> 0,
132-
'S'
133-
};
134-
135-
transfer(write_buffer, nullptr, sizeof (write_buffer));
119+
'J',
120+
WALLABY_SPI_VERSION,
121+
count,
122+
4,
123+
address,
124+
(value & 0xFF000000) >> 24,
125+
address + 1,
126+
(value & 0x00FF0000) >> 16,
127+
address + 2,
128+
(value & 0x0000FF00) >> 8,
129+
address + 3,
130+
(value & 0x000000FF) >> 0,
131+
'S'};
132+
++count;
133+
134+
transfer(write_buffer, nullptr, sizeof(write_buffer));
136135
}
137136

138137
private:
139138
bool transfer(
140-
const std::uint8_t *const write_buffer,
141-
std::uint8_t *const read_buffer,
142-
const std::size_t size
143-
)
139+
const std::uint8_t *const write_buffer,
140+
std::uint8_t *const read_buffer,
141+
const std::size_t size)
144142
{
145143
std::lock_guard<std::mutex> lock(mut_);
146144

@@ -173,6 +171,7 @@ class WombatDevice : public kipr::core::Device
173171

174172
int spi_fd_;
175173
std::mutex mut_;
174+
std::uint8_t count;
176175
};
177176

178177
struct WombatDeviceDescriptor

temp.cpp

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#include "../../device.hpp"
2+
#include "kipr/core/platform.hpp"
3+
#include "kipr/core/registers.hpp"
4+
5+
#include "kipr/log/log.hpp"
6+
7+
#include <linux/spi/spidev.h>
8+
#include <sys/ioctl.h>
9+
#include <unistd.h>
10+
#include <mutex>
11+
12+
#include <cstdlib>
13+
#include <cstring>
14+
#include <fcntl.h>
15+
#include <errno.h>
16+
#include <sys/types.h>
17+
#include <sys/stat.h>
18+
19+
#define SPI_FILE_SYSTEM ("/dev/spidev0.0")
20+
21+
namespace
22+
{
23+
const std::string NAME = "wombat";
24+
kipr::log::Log logger("core/wombat");
25+
}
26+
27+
class WombatDevice : public kipr::core::Device
28+
{
29+
public:
30+
WombatDevice()
31+
: spi_fd_(-1),
32+
count(0),
33+
read_buf(new std::uint8_t[REG_ALL_COUNT]),
34+
write_buf(new std::uint8_t[REG_ALL_COUNT])
35+
{
36+
spi_fd_ = open(SPI_FILE_SYSTEM, O_RDWR);
37+
if (spi_fd_ <= 0)
38+
{
39+
logger.fatal() << "Not found: " << SPI_FILE_SYSTEM;
40+
}
41+
clear_buffers();
42+
}
43+
44+
virtual ~WombatDevice()
45+
{
46+
close(spi_fd_);
47+
delete[] read_buf;
48+
delete[] write_buf;
49+
}
50+
51+
virtual const std::string &getName() const override
52+
{
53+
return NAME;
54+
}
55+
56+
virtual std::uint8_t r8(const std::uint8_t address) override
57+
{
58+
clear_buffers();
59+
transfer();
60+
61+
return read_buf[address];
62+
}
63+
64+
virtual std::uint16_t r16(const std::uint8_t address) override
65+
{
66+
clear_buffers();
67+
transfer();
68+
69+
return (
70+
read_buf[address] << 8 |
71+
read_buf[address + 1] << 0);
72+
}
73+
74+
virtual std::uint32_t r32(const std::uint8_t address) override
75+
{
76+
clear_buffers();
77+
transfer();
78+
79+
return (
80+
read_buf[address] << 24 |
81+
read_buf[address + 1] << 16 |
82+
read_buf[address + 2] << 8 |
83+
read_buf[address + 3] << 0);
84+
}
85+
86+
virtual void w8(const std::uint8_t address, const std::uint8_t value) override
87+
{
88+
clear_buffers();
89+
write_buf[3] = address,
90+
write_buf[4] = value,
91+
92+
transfer();
93+
}
94+
95+
virtual void w16(const std::uint8_t address, const std::uint16_t value) override
96+
{
97+
clear_buffers();
98+
write_buf[3] = 2,
99+
write_buf[4] = address,
100+
write_buf[5] = (value & 0xFF00) >> 8,
101+
write_buf[6] = address + 1,
102+
write_buf[7] = (value & 0x00FF) >> 0,
103+
104+
transfer();
105+
}
106+
107+
virtual void w32(const std::uint8_t address, const std::uint32_t value) override
108+
{
109+
clear_buffers();
110+
write_buf[3] = 4;
111+
write_buf[4] = address,
112+
write_buf[5] = (value & 0xFF000000) >> 24,
113+
write_buf[6] = address + 1,
114+
write_buf[7] = (value & 0x00FF0000) >> 16,
115+
write_buf[8] = address + 2,
116+
write_buf[9] = (value & 0x0000FF00) >> 8,
117+
write_buf[10] = address + 3,
118+
write_buf[11] = (value & 0x000000FF) >> 0,
119+
120+
transfer();
121+
}
122+
123+
private:
124+
void clear_buffers()
125+
{
126+
memset(write_buf, 0, REG_ALL_COUNT);
127+
memset(read_buf, 0, REG_ALL_COUNT);
128+
}
129+
130+
bool transfer()
131+
{
132+
std::lock_guard<std::mutex> lock(mut_);
133+
134+
++count;
135+
write_buf[0] = 'J';
136+
write_buf[1] = WALLABY_SPI_VERSION;
137+
write_buf[2] = count;
138+
write_buf[REG_ALL_COUNT - 1] = 'S';
139+
140+
struct spi_ioc_transfer xfer;
141+
memset(&xfer, 0, sizeof xfer);
142+
143+
xfer.tx_buf = (unsigned long)write_buf;
144+
xfer.rx_buf = (unsigned long)read_buf;
145+
xfer.len = REG_ALL_COUNT;
146+
xfer.speed_hz = 16000000;
147+
148+
const int status = ioctl(spi_fd_, SPI_IOC_MESSAGE(1), &xfer);
149+
150+
usleep(50); // FIXME: this makes sure we don't outrun the co-processor until interrupts are in place for DMA
151+
152+
if (status < 0)
153+
{
154+
logger.error() << "SPI_IOC_MESSAGE: " << strerror(errno);
155+
return false;
156+
}
157+
158+
if (read_buf[0] != 'J')
159+
{
160+
logger.error() << "DMA de-synchronized";
161+
return false;
162+
}
163+
164+
return true;
165+
}
166+
167+
int spi_fd_;
168+
std::mutex mut_;
169+
std::uint8_t count;
170+
std::uint8_t *write_buf;
171+
std::uint8_t *read_buf;
172+
};
173+
174+
struct WombatDeviceDescriptor
175+
{
176+
typedef WombatDevice DeviceType;
177+
178+
static bool isPresent()
179+
{
180+
return access(SPI_FILE_SYSTEM, F_OK) == 0;
181+
}
182+
};
183+
184+
KIPR_CORE_PLATFORM_DEVICE_REGISTER(WombatDeviceDescriptor);

toolchain/arm-linux-gnueabihf.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_SYSTEM_PROCESSOR armv7l)
66

77
set(triple arm-linux-gnueabihf)
88

9-
set_property(GLOBAL PROPERTY host aarch64-linux)
9+
set_property(GLOBAL PROPERTY host arm-linux)
1010
set_property(GLOBAL PROPERTY triple ${triple})
1111
set_property(GLOBAL PROPERTY arch armv7l)
1212
set_property(GLOBAL PROPERTY target_os linux)

0 commit comments

Comments
 (0)