Skip to content

Commit 9780124

Browse files
author
bitluni
committed
release
1 parent af0ddcd commit 9780124

File tree

18 files changed

+1431
-230
lines changed

18 files changed

+1431
-230
lines changed

ClusterRaymarcher/src/ClientBus.h

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class ClientBus: public Bus
77
{
88
public:
99
bool busy;
10+
uint32_t lastIrqTime;
11+
1012
RingBuffer<> inBuffer;
1113
RingBuffer<> outBuffer;
1214

@@ -18,10 +20,11 @@ class ClientBus: public Bus
1820

1921
void handleRequest()
2022
{
23+
lastIrqTime = getTime();
2124
RequestType type = getType();
2225
if(state != STATE_IDLE || type == REQUEST_RESET)
2326
{
24-
if(state == STATE_RECEIVE) disableReceive();
27+
if(state == STATE_RECEIVE || state == STATE_RECEIVE_BROADCAST) disableReceive();
2528
if(state == STATE_TRANSMIT) disableTransmit();
2629
inBuffer.clear();
2730
outBuffer.clear();
@@ -33,29 +36,39 @@ class ClientBus: public Bus
3336
case REQUEST_RECEIVE:
3437
{
3538
//debug(1);
36-
uint8_t data = getData();
37-
if(data != 255 && data != id) break;
39+
if(getData() != id) break;
40+
if(inBuffer.space() == 0)
41+
setFULL();
42+
enableReceive();
43+
state = STATE_RECEIVE;
44+
setACK();
45+
return;
3846
}
3947
case REQUEST_BROADCAST:
40-
enableReceive();
41-
if(inBuffer.space() == 0) setFULL();
42-
state = STATE_RECEIVE;
43-
setACK();
44-
break;
48+
{
49+
if(getData() != 255) break;
50+
if(inBuffer.space() == 0) break;
51+
enableReceive();
52+
state = STATE_RECEIVE_BROADCAST;
53+
return;
54+
}
4555
case REQUEST_TRANSMIT:
46-
if(getData() != id) break;
47-
enableTransmit();
48-
state = STATE_TRANSMIT;
49-
if(outBuffer.size == 0 || busy) setEOT();
50-
setACK();
51-
break;
56+
{
57+
if(getData() != id) break;
58+
enableTransmit();
59+
state = STATE_TRANSMIT;
60+
if(outBuffer.size == 0 || busy) setEOT();
61+
setACK();
62+
return;
63+
}
5264
case REQUEST_RESET:
5365
break;
5466
}
5567
}
5668

5769
void handleDataClockHigh()
5870
{
71+
lastIrqTime = getTime();
5972
//debug(1);
6073
bool eot = getEOT();
6174
resetACK();
@@ -72,27 +85,39 @@ class ClientBus: public Bus
7285

7386
void handleDataClockLow()
7487
{
75-
if(state == STATE_RECEIVE)
76-
{
77-
uint8_t data = getData();
78-
inBuffer.write(data);
79-
if(inBuffer.space() == 0)
80-
setFULL();
81-
setACK();
82-
}
83-
else
84-
if(state == STATE_TRANSMIT)
85-
{
86-
uint8_t data;
87-
outBuffer.read(data);
88-
setData(data);
89-
if(outBuffer.size == 0) setEOT();
90-
setACK();
91-
}
92-
else
88+
lastIrqTime = getTime();
89+
switch(state)
9390
{
94-
state = STATE_ERROR; //error, not in receive or transmit state
95-
return;
91+
case STATE_RECEIVE:
92+
{
93+
uint8_t data = getData();
94+
inBuffer.write(data);
95+
if(inBuffer.space() == 0)
96+
setFULL();
97+
setACK();
98+
return;
99+
}
100+
case STATE_TRANSMIT:
101+
{
102+
uint8_t data;
103+
outBuffer.read(data);
104+
setData(data);
105+
if(outBuffer.size == 0)
106+
setEOT();
107+
waitDATA(data, 0, timeoutSignalChange);
108+
if(outBuffer.size == 0)
109+
waitEOT(true, 0, timeoutSignalChange);
110+
setACK();
111+
return;
112+
}
113+
case STATE_RECEIVE_BROADCAST:
114+
{
115+
uint8_t data = getData();
116+
inBuffer.write(data); //TODO: if fails remove broken packet
117+
return;
118+
}
119+
default:
120+
state = STATE_ERROR; //error, not in receive or transmit state
96121
}
97122
}
98123

@@ -101,4 +126,18 @@ class ClientBus: public Bus
101126
virtual void setCLK() {};
102127
virtual void resetCLK() {};
103128
virtual void resetType() {};
129+
130+
bool processTimeout()
131+
{
132+
if(state == STATE_IDLE) return false;
133+
if(getTime() - lastIrqTime > timeoutResponse)
134+
{
135+
resetSignals(0);
136+
inBuffer.clear();
137+
outBuffer.clear();
138+
state = STATE_IDLE;
139+
return true; //timeout
140+
}
141+
return false;
142+
}
104143
};

ClusterRaymarcher/src/ClientBusCH32V003.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,29 @@ class ClientBusCH32V003: public ClientBus
2222
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
2323
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
2424

25-
uint32_t cfg = 0;
25+
/*uint32_t cfg = 0;
2626
for(int i = 0; i < 8; i++)
27-
cfg |= (0b0100 /*open drain*/ | 0b11 /*30MHz output*/) << (4 * i);
27+
cfg |= (0b0100 | 0b11 ) << (4 * i); // open drain output
2828
GPIOC->OUTDR = 0b11111111; //for open drain need to leave high for others to speak
2929
GPIOC->CFGLR = cfg;
3030
cfg = 0;
3131
for(int i = 0; i < 8; i++)
3232
if(i == 1 || i == 7) //avoid D1 and D7 to have swio still active
33-
cfg |= (0b0100 /*floating*/ | 0b00 /*input*/) << (4 * i);
33+
cfg |= (0b0100 | 0b00 ) << (4 * i); //floating input
3434
else
35-
cfg |= (0b0100 /*open drain*/ | 0b11 /*30MHz output*/) << (4 * i);
35+
cfg |= (0b0100| 0b11 ) << (4 * i); //open drain output
36+
*/
37+
GPIOC->OUTDR = 0b11111111;
38+
GPIOC->CFGLR = 0x77777777; //open drain (except d1 and d7
39+
//GPIOC->CFGLR = 0x44444444; //floating
3640

3741
GPIOD->OUTDR = 0b01111101;
38-
GPIOD->CFGLR = cfg;
42+
//GPIOD->CFGLR = cfg;
43+
//GPIOD->CFGLR = 0x44444444; //floating
44+
GPIOD->CFGLR = 0x47777747; //open drain (except d1 and d7
3945

40-
GPIOA->CFGLR = (GPIOA->CFGLR & 0b000011110000) | ((0b0100 /*floating*/ | 0b00 /*input*/) << (4 * 2));
46+
//GPIOA->CFGLR = (GPIOA->CFGLR & 0b000011110000) | ((0b0100 /*floating*/ | 0b00 /*input*/) << (4 * 2));
47+
GPIOA->CFGLR = (GPIOA->CFGLR & 0x0f0) | 0x400;
4148

4249
//initialize interrupts
4350
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
@@ -67,6 +74,8 @@ class ClientBusCH32V003: public ClientBus
6774

6875
virtual void enableTransmit()
6976
{
77+
//GPIOC->OUTDR = 0b11111111;
78+
//GPIOC->CFGLR = 0x77777777; //open drain (except d1 and d7)
7079
EXTI->INTENR |= EXTI_INTENR_MR0; // Enable EXT7 on line 0
7180
}
7281

@@ -80,6 +89,7 @@ class ClientBusCH32V003: public ClientBus
8089
{
8190
EXTI->INTENR &= ~EXTI_INTENR_MR0; // Disable EXT7 on line 0
8291
GPIOC->BSHR = 0b11111111; //release open drain lines
92+
//GPIOC->CFGLR = 0x44444444; //floating
8393
GPIOD->BSHR = 0b01111101; //release open drain lines, avoid D1 and D7 here to have swio still active
8494
}
8595

ClusterRaymarcher/src/bus.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,34 @@ enum BusInstruction
77
BUS_RAYMARCHER_INIT = 0x10,
88
BUS_RAYMARCHER_RENDER_PIXEL = 0x11,
99
BUS_RAYMARCHER_RENDER_PIXEL_RESULT = 0x12,
10+
BUS_RAYMARCHER_CAM_POS = 0x13,
11+
12+
BUS_SHA256_START = 0x20,
13+
BUS_SHA256_END = 0x21,
14+
BUS_SHA256_COUNT = 0x22,
1015

1116
BUS_LED = 0xd0,
1217
BUS_PING = 0xd1,
1318

14-
BUS_CLIENT_RESET = 0xe0, //reset client
19+
BUS_CLIENT_RESET = 0xe0, //reset client
1520
BUS_CLIENT_SET_INDEX = 0xe4,
16-
BUS_CLIENT_ERROR = 0xe5, //error in client
21+
BUS_CLIENT_ERROR = 0xe5, //error in client
22+
BUS_CLIENT_TIMINGS = 0xe7, //set timeout for client
1723

1824
BUS_HOST_RESET = 0xf0,
19-
BUS_HOST_FORWARD = 0xf1, //forward packet to client
20-
BUS_HOST_BROADCAST = 0xf2, //broadcast packet to flagged clients
21-
BUS_HOST_FETCH = 0xf3, //fetch data from client
22-
BUS_HOST_LINES_STATE = 0xf4,
25+
BUS_HOST_FORWARD = 0xf1, //forward packet to client
26+
BUS_HOST_BROADCAST = 0xf2, //broadcast packet to flagged clients
27+
BUS_HOST_FETCH = 0xf3, //fetch data from client
28+
2329
BUS_HOST_ERROR = 0xf5,
2430
BUS_HOST_SUCCESS = 0xf6,
31+
BUS_HOST_TIMINGS = 0xf7, //set timeout for host
32+
BUS_HOST_GET_LINES = 0xf8,
33+
BUS_HOST_SET_LINES = 0xf9,
34+
2535
};
2636

27-
template<int bufferSize = 32> //needs to be power of two
37+
template<int bufferSize = 32> //needs to be power of two
2838
class RingBuffer
2939
{
3040
public:
@@ -84,6 +94,15 @@ class RingBuffer
8494
return true;
8595
}
8696

97+
bool read(int16_t &data)
98+
{
99+
uint8_t *d = (uint8_t*)(&data);
100+
if(size < 2) return false;
101+
read(d[0]);
102+
read(d[1]);
103+
return true;
104+
}
105+
87106
bool read(uint32_t &data)
88107
{
89108
if(size < 4) return false;
@@ -126,6 +145,7 @@ class Bus
126145
STATE_IDLE = 0x00,
127146
STATE_RECEIVE = 0x01,
128147
STATE_TRANSMIT = 0x02,
148+
STATE_RECEIVE_BROADCAST = 0x03,
129149
STATE_ERROR = 0xff
130150
};
131151

@@ -146,6 +166,8 @@ class Bus
146166

147167
State state;
148168
uint8_t id;
169+
uint32_t timeoutSignalChange;
170+
uint32_t timeoutResponse;
149171

150172
Bus()
151173
:state(STATE_IDLE), id(0)
@@ -156,6 +178,8 @@ class Bus
156178

157179
bool init()
158180
{
181+
timeoutSignalChange = 100000; //100000us
182+
timeoutResponse = 100000; //100000us
159183
state = STATE_ERROR;
160184
if(initIo()) return false;
161185
state = STATE_IDLE;

0 commit comments

Comments
 (0)