-
Notifications
You must be signed in to change notification settings - Fork 4
/
waveform_generator_registers.c
345 lines (313 loc) · 11.8 KB
/
waveform_generator_registers.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Licensed under the EUPL V.1.1, Lizenziert unter EUPL V.1.1
*/
/*
* Author: Michail Pligouroudis 30/05/2012
* modified (heavily rather rebuild): Peter Zumbruch
*/
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "read_write_register.h"
#include "waveform_generator_registers.h"
#include "adc.h"
#include "api.h"
#include "api_define.h"
#include "api_global.h"
#include "api_debug.h"
//static const char string_sx_x_[] PROGMEM = "%s%x %x ";
static const char string_sx_x_Si_S_[] PROGMEM = "%s%x %x %S%i %S ";
static const char string_sSi_S_S[] PROGMEM = "%s%S%i %S %S";
static const char string_Delay[] PROGMEM = "Delay";
static const char string_WAVEFORM_OUTPUT[] PROGMEM = "WAVEFORM OUTPUT";
static const char string_PSEUDORANDOM[] PROGMEM = "PSEUDORANDOM";
static const char string_MSB[] PROGMEM = "MSB";
static const char string_MIDDLE[] PROGMEM = "MIDDLE";
static const char string_LSB[] PROGMEM = "LSB";
static const char string_is_written[] PROGMEM = "is written";
void waveformGeneratorWriteRegister(struct uartStruct *ptr_uartStruct)
{
uint8_t status = 0;
uint8_t waveformGeneratorRegisterAddress, value;
switch(ptr_uartStruct->number_of_arguments)
{
case 0:
case 1:
CommunicationError_p(ERRA, SERIAL_ERROR_too_few_arguments, TRUE, NULL);
status = 1;
break;
default:
break;
}
if ( 0 != status )
{
return;
}
waveformGeneratorRegisterAddress = ptr_uartStruct->Uart_Message_ID;
value = ptr_uartStruct->Uart_Mask;
//declare UART in order to send data with baud=1,025Mbps
waveformGeneratorDeclareUARTtoSendData();
//sending your new data
registerWriteInto8bitRegister(UDR1_register_of_ATMEL_address, waveformGeneratorRegisterAddress);
registerWriteInto8bitRegister(UDR1_register_of_ATMEL_address, value);
// print response
clearString(uart_message_string, BUFFER_SIZE);
createReceiveHeader(ptr_uartStruct, uart_message_string, BUFFER_SIZE);
switch (waveformGeneratorRegisterAddress)
{
case CONTROL_REGISTER:
{
switch (value)
{
case DEBUG:
snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("%s DEBUG"),
uart_message_string);
break;
case RESET:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%sRESET STATUS"), uart_message_string);
break;
case PSEUDORANDOM:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%s%S %S"), uart_message_string, string_PSEUDORANDOM, string_WAVEFORM_OUTPUT);
break;
case SQUARE:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%sSQUARE %S"), uart_message_string, string_WAVEFORM_OUTPUT);
break;
case PULSE:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%sPULSE %S"), uart_message_string, string_WAVEFORM_OUTPUT);
break;
case PSEUDORAND_TIME_PULSE:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%s%S TIME PULSE(100ns) %S"), uart_message_string, string_PSEUDORANDOM, string_WAVEFORM_OUTPUT);
break;
default:
CommunicationError_p(ERRA, GENERAL_ERROR_invalid_argument, TRUE, NULL);
status = 1;
break;
}
break;
}
case DELAY1_MSB_REGISTER:
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sSi_S_S,
uart_message_string,
string_Delay, 1, string_MSB, string_is_written);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%sDelay1 MSB is written"),
// uart_message_string);
break;
}
case DELAY1_MIDDLE_REGISTER:
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sSi_S_S,
uart_message_string,
string_Delay, 1, string_MIDDLE, string_is_written);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%sDelay1 MIDDLE is written"),
// uart_message_string);
break;
}
case DELAY1_LSB_REGISTER:
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sSi_S_S,
uart_message_string,
string_Delay, 1, string_LSB, string_is_written);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%sDelay1 LSB is written"),
// uart_message_string);
break;
}
case DELAY2_MSB_REGISTER:
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sSi_S_S,
uart_message_string,
string_Delay, 2, string_MSB, string_is_written);
// snprintf_P(uart_message_string, BUFFER_SIZE - 2,
// PSTR("%sDelay2 MSB is written"),
// uart_message_string);
break;
}
case DELAY2_MIDDLE_REGISTER:
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sSi_S_S,
uart_message_string,
string_Delay, 2, string_MIDDLE, string_is_written);
// snprintf_P(uart_message_string, BUFFER_SIZE - 2,
// PSTR("%sDelay2 MIDDLE is written"),
// uart_message_string);
break;
}
case DELAY2_LSB_REGISTER:
{
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sSi_S_S,
uart_message_string,
string_Delay, 2, string_LSB, string_is_written);
// snprintf_P(uart_message_string, BUFFER_SIZE - 2,
// PSTR("%sDelay2 LSB is written"),
// uart_message_string);
break;
}
default:
CommunicationError_p(ERRA, GENERAL_ERROR_invalid_argument, TRUE, NULL);
status = 1;
break;
}
if ( 0 == status)
{
UART0_Send_Message_String_p(NULL, 0);
}
return;
} //END of write_double_Register function
void waveformGeneratorReadRegister(struct uartStruct *ptr_uartStruct)
{
int step = 0;
uint8_t registers[7];
//declare UART in order to send data with baud=1,025Mbps
waveformGeneratorDeclareUARTtoSendData();
//control register address
registerWriteInto8bitRegister( UDR1_register_of_ATMEL_address, CONTROL_REGISTER); //0x00
//control register in read status
registerWriteInto8bitRegister( UDR1_register_of_ATMEL_address, READ); //0xA0
//declare UART in order to receive data with baud=1,025Mbps
waveformGeneratorDeclareUARTtoReceiveData();
_delay_us(32); //string_Delay in order to synchronize the first recieving byte
// saving value of the registers from FPGA at the table "registers[]"
for (step = 0; step < MAX_REGISTER_INDEX; step++)
{
registers[step] = registerReadFrom8bitRegister((uint8_t) UDR1_register_of_ATMEL_address & 0xFF );
_delay_us(16); // string_Delay between each byte
} //end for loop
//declare UART in order to send data with baud=1,025Mbps (twice on purpose!)
waveformGeneratorDeclareUARTtoSendData();
waveformGeneratorDeclareUARTtoSendData();
// print results
switch(ptr_uartStruct->number_of_arguments)
{
case 0:
waveformGeneratorPrintSingleRegister( ( uint8_t) SHOW_ALL ,
registers, sizeof(registers)/sizeof(uint8_t) );
break;
default:
waveformGeneratorPrintSingleRegister( ( uint8_t) (ptr_uartStruct->Uart_Message_ID & 0xFF),
registers, sizeof(registers)/sizeof(uint8_t) );
break;
}
} //end of waveformGeneratorReadRegister function
void waveformGeneratorPrintSingleRegister( uint8_t registerId, uint8_t registers[], uint8_t size )
{
uint8_t status = 0 ;
clearString(uart_message_string, BUFFER_SIZE);
createReceiveHeader(ptr_uartStruct, uart_message_string, BUFFER_SIZE);
switch (registerId)
{
case DELAY1_MSB_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sx_x_Si_S_, uart_message_string,
DELAY1_MSB_REGISTER, registers[DELAY1_MSB_REGISTER_INDEX],
string_Delay, 1, string_MSB);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%s%x %x Delay1 MSB "), uart_message_string,
// DELAY1_MSB_REGISTER, registers[DELAY1_MSB_REGISTER_INDEX]);
break;
case DELAY1_MIDDLE_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sx_x_Si_S_, uart_message_string,
DELAY1_MIDDLE_REGISTER, registers[DELAY1_MIDDLE_REGISTER_INDEX],
string_Delay, 1, string_MIDDLE);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%s%x %x Delay1 MIDDLE "), uart_message_string,
// DELAY1_MIDDLE_REGISTER, registers[DELAY1_MIDDLE_REGISTER_INDEX]);
break;
case DELAY1_LSB_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sx_x_Si_S_, uart_message_string,
DELAY1_LSB_REGISTER, registers[DELAY1_LSB_REGISTER_INDEX],
string_Delay, 1, string_LSB);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%s%x %x Delay1 LSB "), uart_message_string,
// DELAY1_LSB_REGISTER, registers[DELAY1_LSB_REGISTER_INDEX]);
break;
case DELAY2_MSB_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sx_x_Si_S_, uart_message_string,
DELAY2_MSB_REGISTER, registers[DELAY2_MSB_REGISTER_INDEX],
string_Delay, 2, string_MSB);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%s%x %x Delay2 MSB "), uart_message_string,
// DELAY2_MSB_REGISTER, registers[DELAY2_MSB_REGISTER_INDEX]);
break;
case DELAY2_MIDDLE_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sx_x_Si_S_, uart_message_string,
DELAY2_MIDDLE_REGISTER, registers[DELAY2_MIDDLE_REGISTER_INDEX],
string_Delay, 2, string_MIDDLE);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%s%x %x Delay2 MIDDLE "), uart_message_string,
// DELAY2_MIDDLE_REGISTER, registers[DELAY2_MIDDLE_REGISTER_INDEX]);
break;
case DELAY2_LSB_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
string_sx_x_Si_S_, uart_message_string,
DELAY2_LSB_REGISTER, registers[DELAY2_LSB_REGISTER_INDEX],
string_Delay, 2, string_LSB);
// snprintf_P(uart_message_string, BUFFER_SIZE - 1,
// PSTR("%s%x %x Delay2 LSB "), uart_message_string,
// DELAY2_LSB_REGISTER, registers[DELAY2_LSB_REGISTER_INDEX]);
break;
case CONTROL_REGISTER:
snprintf_P(uart_message_string, BUFFER_SIZE - 1,
PSTR("%s%x %x Control Register "), uart_message_string,
CONTROL_REGISTER, registers[CONTROL_REGISTER_INDEX]);
break;
case SHOW_ALL: /*recursive call*/
waveformGeneratorPrintSingleRegister(DELAY1_MSB_REGISTER, registers, size);
waveformGeneratorPrintSingleRegister(DELAY1_MIDDLE_REGISTER, registers, size);
waveformGeneratorPrintSingleRegister(DELAY1_LSB_REGISTER, registers, size);
waveformGeneratorPrintSingleRegister(DELAY2_MSB_REGISTER, registers, size);
waveformGeneratorPrintSingleRegister(DELAY2_MIDDLE_REGISTER, registers, size);
waveformGeneratorPrintSingleRegister(DELAY2_LSB_REGISTER, registers, size);
waveformGeneratorPrintSingleRegister(CONTROL_REGISTER, registers, size);
break;
default:
CommunicationError_p(ERRG, dynamicMessage_ErrorIndex, TRUE, PSTR("wrong register address"));
status = 1;
break;
}
if ( 0 == status)
{
UART0_Send_Message_String_p(NULL, 0);
}
return;
}
void waveformGeneratorDeclareUARTtoSendData(void)
{
//declare UART in order to send data with baud=1,025Mbps
registerWriteInto8bitRegister(UCSR1C_register_of_ATMEL_address, UCSR1C_register_of_ATMEL_value);
registerWriteInto8bitRegister(UBRR1L_register_of_ATMEL_address, UBRR1L_register_of_ATMEL_value);
registerWriteInto8bitRegister(UBRR1H_register_of_ATMEL_address, UBRR1H_register_of_ATMEL_value);
registerWriteInto8bitRegister(UCSR1B_register_of_ATMEL_address, UCSR1B_register_of_ATMEL_value_transmit);
registerWriteInto8bitRegister(UCSR1A_register_of_ATMEL_address, UCSR1A_register_of_ATMEL_value);
}
void waveformGeneratorDeclareUARTtoReceiveData(void)
{
//declare UART in order to receive data with baud=1,025Mbps
registerWriteInto8bitRegister(UCSR1C_register_of_ATMEL_address, UCSR1C_register_of_ATMEL_value);
registerWriteInto8bitRegister(UBRR1L_register_of_ATMEL_address, UBRR1L_register_of_ATMEL_value);
registerWriteInto8bitRegister(UBRR1H_register_of_ATMEL_address, UBRR1H_register_of_ATMEL_value);
registerWriteInto8bitRegister(UCSR1A_register_of_ATMEL_address, UCSR1A_register_of_ATMEL_value);
registerWriteInto8bitRegister(UCSR1B_register_of_ATMEL_address, UCSR1B_register_of_ATMEL_value_receive);
}