-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptoTWIM.c
More file actions
289 lines (221 loc) · 7.67 KB
/
OptoTWIM.c
File metadata and controls
289 lines (221 loc) · 7.67 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* OptoTWIM.c
*
*
* Author: Caroline
*/
#include "OptoTWIM.h"
uint8_t init_twim(volatile avr32_twim_t *twim, uint32_t twim_speed, uint32_t twim_addr, uint32_t pb_freq){
uint8_t r = 1;
// Configure TWIM registers
twim->CR.men = 1; // Master transfer enable
twim->CR.swrst = 1; // reset the TWIM interface
twim->CR.smdis = 1;
// Clear SCR
twim->scr = 0xFFFFFFFF;
// Set the TWIM speed
set_twim_speed(twim, twim_speed,pb_freq);
// Test that the chip responds
if(!twim_ping_chip(twim, twim_addr)){
// Chip response failed
if((twim->sr & AVR32_TWIM_SR_ARBLST_MASK) || (twim->sr & AVR32_TWIM_SR_ANAK_MASK)){
// Was it an address NAK or SDA bus error?
r=0;
}
// Other error
r=0;
}
// Initialization successful;
return r;
}
void set_twim_speed(volatile avr32_twim_t *twim, uint32_t twim_speed, uint32_t pb_f){
// clock configuration
uint8_t cwgr_exp = 0; // clock divider
uint32_t cwgr_prescal = (pb_f/twim_speed/2); // prescaled value for TWCK
// the TWCK must fit into an 8 bit field
// the clock divider must fit in a 3 bit field
while((cwgr_prescal > 0xFF) && (cwgr_exp <= 0x07)){
// the clock divider can be increased
// TWCK must be reduced by a factor of 2
cwgr_exp++;
cwgr_prescal /= 2;
}
// Set the clock settings for the desired TWCK
twim->cwgr = (((cwgr_prescal/2) << AVR32_TWIM_CWGR_LOW_OFFSET)|((cwgr_prescal- cwgr_prescal/2) << AVR32_TWIM_CWGR_HIGH_OFFSET)
|(cwgr_exp << AVR32_TWIM_CWGR_EXP_OFFSET)|(0 << AVR32_TWIM_CWGR_DATA_OFFSET)|(cwgr_prescal << AVR32_TWIM_CWGR_STASTO_OFFSET));
}
uint8_t twim_ping_chip(volatile avr32_twim_t *twim, uint32_t addr){
uint8_t test_data = 0;
if(twim_write(twim, test_data, addr, 0)){
// TWIM write successful
return 1;
}
return 0; // TWIM write failed
}
uint8_t twim_write(volatile avr32_twim_t *twim, uint16_t buff_data, uint32_t twim_addr, uint8_t dac_cmd){
// Command for DAC write
// binary: 01011000
// Where: 010 = cmd code (see DAC datasheet)
// 11 = write code
// 00 = channel A select
// 0 = UDAC'
uint8_t cmd_dac_wr = 88;
// Clear THR
twim->thr = 0x00000000;
twim->scr = 0xFFFFFFFF;
twim->CR.mdis = 1;
// TODO: ADD dac_cmd support (see DAC data sheet for transmission formats)
switch(dac_cmd){
// Ping DAC
case 0:
// Set data buffer
twim->thr = buff_data << AVR32_TWIM_THR_TXDATA_OFFSET;
// Set command register to start transfer
twim->cmdr = ((twim_addr << AVR32_TWIM_CMDR_SADR_OFFSET)|(0 << AVR32_TWIM_CMDR_NBYTES_OFFSET)
|(1 << AVR32_TWIM_CMDR_VALID_OFFSET)|(1 << AVR32_TWIM_CMDR_START_OFFSET)|(1 << AVR32_TWIM_CMDR_STOP_OFFSET));
// Enable master to start transfer
twim->CR.men = 1;
// Wait for transfer to finish
while(!(twim->sr & AVR32_TWIM_SR_IDLE_MASK));
break;
// Wakeup DAC
case 1:
// Set data buffer
twim->thr = 0 << AVR32_TWIM_THR_TXDATA_OFFSET;
// Set command register to start transfer
twim->cmdr = ((twim_addr << AVR32_TWIM_CMDR_SADR_OFFSET)|(2 << AVR32_TWIM_CMDR_NBYTES_OFFSET)
|(1 << AVR32_TWIM_CMDR_VALID_OFFSET)|(1 << AVR32_TWIM_CMDR_START_OFFSET)|(1 << AVR32_TWIM_CMDR_STOP_OFFSET));
// Enable master to start transfer
twim->CR.men = 1;
// Wait to load next byte
while(!(twim->sr & AVR32_TWIM_SR_TXRDY_MASK));
twim->thr = buff_data << AVR32_TWIM_THR_TXDATA_OFFSET;
// Wait for transfer to finish
while(!(twim->sr & AVR32_TWIM_SR_IDLE_MASK));
break;
// Software Update
case 2:
// Set data buffer
twim->thr = 0 << AVR32_TWIM_THR_TXDATA_OFFSET;
// Set command register to start transfer
twim->cmdr = ((twim_addr << AVR32_TWIM_CMDR_SADR_OFFSET)|(2 << AVR32_TWIM_CMDR_NBYTES_OFFSET)
|(1 << AVR32_TWIM_CMDR_VALID_OFFSET)|(1 << AVR32_TWIM_CMDR_START_OFFSET)|(1 << AVR32_TWIM_CMDR_STOP_OFFSET));
// Enable master to start transfer
twim->CR.men = 1;
// Wait to load next byte
while(!(twim->sr & AVR32_TWIM_SR_TXRDY_MASK));
twim->thr = buff_data << AVR32_TWIM_THR_TXDATA_OFFSET;
// Wait for transfer to finish
while(!(twim->sr & AVR32_TWIM_SR_IDLE_MASK));
break;
// Reset DAC
case 3:
// Set data buffer
twim->thr = 0 << AVR32_TWIM_THR_TXDATA_OFFSET;
// Set command register to start transfer
twim->cmdr = ((twim_addr << AVR32_TWIM_CMDR_SADR_OFFSET)|(2 << AVR32_TWIM_CMDR_NBYTES_OFFSET)
|(1 << AVR32_TWIM_CMDR_VALID_OFFSET)|(1 << AVR32_TWIM_CMDR_START_OFFSET)|(1 << AVR32_TWIM_CMDR_STOP_OFFSET));
// Enable master to start transfer
twim->CR.men = 1;
// Wait to load next byte
while(!(twim->sr & AVR32_TWIM_SR_TXRDY_MASK));
twim->thr = buff_data << AVR32_TWIM_THR_TXDATA_OFFSET;
// Wait for transfer to finish
while(!(twim->sr & AVR32_TWIM_SR_IDLE_MASK));
break;
// Write to DAC
case 4:
// Set data buffer
twim->thr = cmd_dac_wr << AVR32_TWIM_THR_TXDATA_OFFSET;
// Set command register to start transfer
twim->cmdr = ((twim_addr << AVR32_TWIM_CMDR_SADR_OFFSET)|(3 << AVR32_TWIM_CMDR_NBYTES_OFFSET)
|(1 << AVR32_TWIM_CMDR_VALID_OFFSET)|(1 << AVR32_TWIM_CMDR_START_OFFSET)|(1 << AVR32_TWIM_CMDR_STOP_OFFSET));
// Enable master to start transfer
twim->CR.men = 1;
// Wait to load next byte (1st data byte)
while(!(twim->sr & AVR32_TWIM_SR_TXRDY_MASK));
twim->thr = ((uint8_t)((buff_data & 0x0F00) >> 8)) << AVR32_TWIM_THR_TXDATA_OFFSET;
// 2nd data byte
while(!(twim->sr & AVR32_TWIM_SR_TXRDY_MASK));
twim->thr = ((uint8_t)(buff_data & 0x00FF)) << AVR32_TWIM_THR_TXDATA_OFFSET;
// Wait for transfer to finish
while(!(twim->sr & AVR32_TWIM_SR_IDLE_MASK));
break;
// Invalid command
default:
return 0;
}
if((twim->sr & AVR32_TWIM_SR_ARBLST_MASK) || (twim->sr & AVR32_TWIM_SR_DNAK_MASK) || (twim->sr & AVR32_TWIM_SR_ANAK_MASK)){
// Transfer failed due to no ACK received or state issue in SDA bus
if(twim->sr & AVR32_TWIM_SR_DNAK_MASK){
return 0;
}
else if(twim->sr & AVR32_TWIM_SR_ARBLST_MASK){
return 0;
}
else if(twim->sr & AVR32_TWIM_SR_ANAK_MASK){
return 0;
}
return 0;
}
// Transfer successful
return 1;
}
uint8_t dac_wakeup_cmd(volatile avr32_twim_t *twim, uint32_t twim_addr){
uint8_t twim_buffer;
twim_buffer = 9;
// Clear status register
twim->scr = 0xFFFFFFFF;
// Write
twim_write(twim, twim_buffer, twim_addr, 1);
// Error checks
if((twim->sr & AVR32_TWIM_SR_ARBLST_MASK) || (twim->sr & AVR32_TWIM_SR_DNAK_MASK) || (twim->sr & AVR32_TWIM_SR_ANAK_MASK)){
// uh oh problem
return 0;
}
// success
return 1;
}
uint8_t dac_software_update(volatile avr32_twim_t *twim, uint32_t twim_addr){
uint8_t twim_buffer;
twim_buffer = 8;
// Clear status register
twim->scr = 0xFFFFFFFF;
// Write
twim_write(twim, twim_buffer, twim_addr, 2);
// Error checks
if((twim->sr & AVR32_TWIM_SR_ARBLST_MASK) || (twim->sr & AVR32_TWIM_SR_DNAK_MASK) || (twim->sr & AVR32_TWIM_SR_ANAK_MASK)){
// uh oh problem
return 0;
}
// success
return 1;
}
uint8_t dac_reset_cmd(volatile avr32_twim_t *twim, uint32_t twim_addr){
uint8_t twim_buffer;
twim_buffer = 6;
// Clear status register
twim->scr = 0xFFFFFFFF;
// Write
twim_write(twim, twim_buffer, twim_addr, 3);
// Error checks
if((twim->sr & AVR32_TWIM_SR_ARBLST_MASK) || (twim->sr & AVR32_TWIM_SR_DNAK_MASK) || (twim->sr & AVR32_TWIM_SR_ANAK_MASK)){
// uh oh problem
return 0;
}
// success
return 1;
}
uint8_t dac_write_cmd(volatile avr32_twim_t *twim, uint32_t twim_addr, uint16_t twim_buffer){
// Clear status register
twim->scr = 0xFFFFFFFF;
// Write
twim_write(twim, twim_buffer, twim_addr, 4);
// Error checks
if((twim->sr & AVR32_TWIM_SR_ARBLST_MASK) || (twim->sr & AVR32_TWIM_SR_DNAK_MASK) || (twim->sr & AVR32_TWIM_SR_ANAK_MASK)){
// uh oh problem
return 0;
}
// success
return 1;
}