-
Notifications
You must be signed in to change notification settings - Fork 17
/
banks.cpp
154 lines (119 loc) · 4.46 KB
/
banks.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright (c) 2007 Barry Carter <Barry.Carter@gmail.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
$Id: banks.c,v 1.7 2008/11/29 11:02:48 ginge Exp $
*/
/*
This code adds bank selection functionality. This allows the
OpenServo to add a near infinite amount of r/w registers.
USE: the bank selection register is REG_BANK_SELECT as defined
in registers.h setting this value changes the current bank of
BANK_REGISTER_COUNT registers at address MIN_BANK_REGISTER.
The maximum number of banks is defined in registers.h as
MAX_BANKS and trying to select a bank over this value will
cap to this value.
in order to read and write data from the registers, you must
supply a bank value eg. banks_read_word(uint8_t bank, ...)
Available banks are defined in banks.h
NOTE: all of these registers are r/w with no protection
care must be taken not to destroy your own data.
*/
#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 "registers.h"
#include "banks.h"
//#include "alert.h"
//#include "backemf.h"
// Register values.
uint8_t banks[MAX_BANKS][BANK_REGISTER_COUNT];
void banks_init(void)
// Function to initialize all banks.
{
uint8_t n;
/*
// Initialize all banks to zero.
for( n=0;n<MAX_BANKS;n++)
memset(&banks[n][0], 0, BANK_REGISTER_COUNT);
// Reset the bank selection to 0
registers_write_byte(REG_BANK_SELECT,0);
// Restore the read/write protected registers from EEPROM. If the
// EEPROM fails checksum this function will return zero and the
// read/write protected registers should be initialized to defaults.
if (!eeprom_restore_registers())
{
// Initialize read/write protected registers to defaults.
registers_defaults();
}
*/
}
void banks_defaults(void)
// Reset safe read/write registers to defaults.
{
// put your default bank data here
// Default TWI address.
banks_write_byte(POS_PID_BANK, REG_TWI_ADDRESS, REG_DEFAULT_TWI_ADDR);
#if ALERT_ENABLED
alert_defaults();
#endif
#if BACKEMF_ENABLED
backemf_defaults();
#endif
}
uint16_t banks_read_word(uint8_t bank, uint8_t address_hi, uint8_t address_lo)
// Read a 16-bit word from the registers.
// Interrupts are disabled during the read.
{
uint8_t sreg;
uint16_t value;
// Clear interrupts.
//asm volatile ("in %0,__SREG__\n\tcli\n\t" : "=&r" (sreg));
// nvic_globalirq_disable();
// Read the registers.
value = (banks[bank][address_hi] << 8) | banks[bank][address_lo];
// Restore status.
//asm volatile ("out __SREG__,%0\n\t" : : "r" (sreg));
// nvic_globalirq_enable();
return value;
}
void banks_write_word(uint8_t bank, uint8_t address_hi, uint8_t address_lo, uint16_t value)
// Write a 16-bit word to the registers.
// Interrupts are disabled during the write.
{
uint8_t sreg;
// Clear interrupts.
//asm volatile ("in %0,__SREG__\n\tcli\n\t" : "=&r" (sreg));
//nvic_globalirq_disable();
// Write the registers.
banks[bank][address_hi] = value >> 8;
banks[bank][address_lo] = value;
// Restore status.
//asm volatile ("out __SREG__,%0\n\t" : : "r" (sreg));
//nvic_globalirq_enable();
}
void banks_update_registers(void)
// Update any functions that need running in the bank context
// This runs in the main loop so DO NOT tie it up too long.
{
}