-
Notifications
You must be signed in to change notification settings - Fork 17
/
packetprocess.cpp
133 lines (114 loc) · 3.23 KB
/
packetprocess.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* packetprocess.cpp
*
* Created on: 1 Sep 2012
* Author: baz
*/
#include <inttypes.h>
#include <string.h>
#include "WProgram.h"
#include "OpenServoCommon.h"
#include "config.h"
//#include "eeprom.h"
#include "pid.h"
#include "pwm.h"
//#include "step.h"
#include "registers.h"
#include "banks.h"
#include "packetcommands.h"
uint8_t packet_registers_read(uint8_t address)
// Read the byte from the specified register. This function handles the
// reading of special registers such as unused registers, redirect and
// redirected registers.
{
// Mask the most significant bit of the address.
address &= 0x7F;
// Are we reading a normal register?
if (address <= MAX_WRITE_PROTECT_REGISTER)
{
#if ROLLING_SUBTYPE
if (address == REG_DEVICE_SUBTYPE)
{
return registers_subtype_cycle();
}
#endif
// Yes. Complete the read.
return registers_read_byte(address);
}
// Are we reading an unused register.
if (address <= MAX_UNUSED_REGISTER)
{
// Block the read.
return 0;
}
// Are we reading a bank register?
if (address <= MAX_BANK_REGISTER)
{
// Defaults to returning the register n the bank array
return banks_read_byte(banks_selected_bank(), address-MIN_BANK_REGISTER);
}
// All other reads are blocked.
return 0;
}
void packet_registers_write(uint8_t address, uint8_t data)
// Write non-write protected registers. This function handles the
// writing of special registers such as unused registers, redirect and
// redirected registers.
{
// Mask the most significant bit of the address.
address &= 0x7F;
// Are we writing a read only register?
if (address <= MAX_READ_ONLY_REGISTER)
{
// Yes. Block the write.
return;
}
// Are we writing a read/write register?
if (address <= MAX_READ_WRITE_REGISTER)
{
// Check that the bank selection is in range
if (address == REG_BANK_SELECT)
{
if (data >= MAX_BANKS - 1)
{
// Set the bank number to the maximum
data = MAX_BANKS - 1;
}
}
// Yes. Complete the write.
registers_write_byte(address, data);
return;
}
// Is writing to the upper registers disabled?
if (registers_is_write_disabled() && (address <= MAX_WRITE_PROTECT_REGISTER))
{
// Yes. Block the write.
return;
}
// Are we writing a write protected register?
if (address <= MAX_WRITE_PROTECT_REGISTER)
{
// Yes. Complete the write if writes are enabled.
registers_write_byte(address, data);
return;
}
// Are we writing an unused register.
if (address <= MAX_UNUSED_REGISTER)
{
// Yes. Block the write.
return;
}
// Are we writing a bank register?
if (address <= MAX_BANK_REGISTER)
{
if (banks_selected_bank() == CONFIG_BANK)
{
// Are we writing to the configuration bank while writes are disabled?
if (registers_is_write_disabled())
return;
}
return banks_write_byte(banks_selected_bank(), address-MIN_BANK_REGISTER, data);
}
// All other writes are blocked.
return;
}