-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathcurrent_source_ac.h
More file actions
72 lines (64 loc) · 2.3 KB
/
Copy pathcurrent_source_ac.h
File metadata and controls
72 lines (64 loc) · 2.3 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
/*
* Copyright (c) 2017 The University of Manchester
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! \dir
//! \brief Alternating current source functions
//! \file
//! \brief Functions called for an alternating current source
#ifndef _CURRENT_SOURCE_AC_H_
#define _CURRENT_SOURCE_AC_H_
#include <sincos.h>
// Structure for AC current source
typedef struct ac_source_t {
uint32_t start;
uint32_t stop;
REAL amplitude;
REAL offset;
REAL frequency;
REAL phase;
} ac_source_t;
static ac_source_t **ac_source;
static bool current_source_ac_init(uint32_t n_ac_sources, uint32_t *next) {
ac_source = spin1_malloc(n_ac_sources * sizeof(uint32_t*));
for (uint32_t n_ac=0; n_ac < n_ac_sources; n_ac++) {
ac_source[n_ac] = spin1_malloc(sizeof(ac_source_t));
if (ac_source[n_ac] == NULL) {
log_error("Unable to allocate AC source parameters - out of DTCM");
return false;
}
*next += sizeof(ac_source_t) / 4;
}
return true;
}
static bool current_source_ac_load_parameters(
address_t cs_address, uint32_t n_ac_sources, uint32_t *next) {
for (uint32_t n_ac=0; n_ac < n_ac_sources; n_ac++) {
spin1_memcpy(ac_source[n_ac], &cs_address[*next], sizeof(ac_source_t));
*next += sizeof(ac_source_t) / 4;
}
return true;
}
static REAL current_source_ac_get_offset(uint32_t cs_index, uint32_t time) {
if ((time >= ac_source[cs_index]->start) && (time < ac_source[cs_index]->stop)) {
REAL time_value = kbits((time - ac_source[cs_index]->start) << 15);
REAL sin_value = sink((time_value * ac_source[cs_index]->frequency) +
ac_source[cs_index]->phase);
REAL ac_current_offset = ac_source[cs_index]->offset + (
ac_source[cs_index]->amplitude * sin_value);
return ac_current_offset;
}
return ZERO;
}
#endif // _CURRENT_SOURCE_AC_H_