/
arm_vsi0.py
271 lines (203 loc) · 6.92 KB
/
arm_vsi0.py
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
# Copyright (c) 2021-2022 Arm Limited. All rights reserved.
# Virtual Streaming Interface instance 0 Python script: Audio Input
##@addtogroup arm_vsi0_py_audio_in
# @{
#
##@package arm_vsi0_audio_in
#Documentation for VSI Audio Input module.
#
#More details.
import logging
import wave
## Set verbosity level
#verbosity = logging.DEBUG
verbosity = logging.ERROR
# [debugging] Verbosity settings
level = { 10: "DEBUG", 20: "INFO", 30: "WARNING", 40: "ERROR" }
logging.basicConfig(format='Py: VSI0: [%(levelname)s]\t%(message)s', level = verbosity)
logging.info("Verbosity level is set to " + level[verbosity])
# IRQ registers
IRQ_Status = 0
# Timer registers
Timer_Control = 0
Timer_Interval = 0
# Timer Control register definitions
Timer_Control_Run_Msk = 1<<0
Timer_Control_Periodic_Msk = 1<<1
Timer_Control_Trig_IRQ_Msk = 1<<2
Timer_Control_Trig_DMA_Msk = 1<<3
# DMA registers
DMA_Control = 0
# DMA Control register definitions
DMA_Control_Enable_Msk = 1<<0
DMA_Control_Direction_Msk = 1<<1
DMA_Control_Direction_P2M = 0<<1
DMA_Control_Direction_M2P = 1<<1
# User registers
Regs = [0] * 64
CONTROL = 0 # Regs[0]
CHANNELS = 0 # Regs[1]
SAMPLE_BITS = 0 # Regs[2]
SAMPLE_RATE = 0 # Regs[3]
# User CONTROL register definitions
CONTROL_ENABLE_Msk = 1<<0
# Data buffer
Data = bytearray()
## Open WAVE file (store object into global WAVE object)
# @param name name of WAVE file to open
def openWAVE(name):
global WAVE
logging.info("Open WAVE file (read mode): {}".format(name))
WAVE = wave.open(name, 'rb')
logging.info(" Number of channels: {}".format(WAVE.getnchannels()))
logging.info(" Sample bits: {}".format(WAVE.getsampwidth() * 8))
logging.info(" Sample rate: {}".format(WAVE.getframerate()))
logging.info(" Number of frames: {}".format(WAVE.getnframes()))
## Read WAVE frames (global WAVE object) into global AudioFrames object
# @param n number of frames to read
# @return frames frames read
def readWAVE(n):
global WAVE
logging.info("Read WAVE frames")
frames = WAVE.readframes(n)
return frames
## Close WAVE file (global WAVE object)
def closeWAVE():
global WAVE
logging.info("Close WAVE file")
WAVE.close()
## Load audio frames into global Data buffer
# @param block_size size of block to load (in bytes)
def loadAudioFrames(block_size):
global Data
logging.info("Load audio frames into data buffer")
frame_size = CHANNELS * ((SAMPLE_BITS + 7) // 8)
frames_max = block_size // frame_size
Data = readWAVE(frames_max)
## Initialize
def init():
logging.info("Python function init() called")
## Read interrupt request (the VSI IRQ Status Register)
# @return value value read (32-bit)
def rdIRQ():
global IRQ_Status
logging.info("Python function rdIRQ() called")
value = IRQ_Status
logging.debug("Read interrupt request: {}".format(value))
return value
## Write interrupt request (the VSI IRQ Status Register)
# @param value value to write (32-bit)
# @return value value written (32-bit)
def wrIRQ(value):
global IRQ_Status
logging.info("Python function wrIRQ() called")
IRQ_Status = value
logging.debug("Write interrupt request: {}".format(value))
return value
## Write Timer registers (the VSI Timer Registers)
# @param index Timer register index (zero based)
# @param value value to write (32-bit)
# @return value value written (32-bit)
def wrTimer(index, value):
global Timer_Control, Timer_Interval
logging.info("Python function wrTimer() called")
if index == 0:
Timer_Control = value
logging.debug("Write Timer_Control: {}".format(value))
elif index == 1:
Timer_Interval = value
logging.debug("Write Timer_Interval: {}".format(value))
return value
## Timer event (called at Timer Overflow)
def timerEvent():
logging.info("Python function timerEvent() called")
## Write DMA registers (the VSI DMA Registers)
# @param index DMA register index (zero based)
# @param value value to write (32-bit)
# @return value value written (32-bit)
def wrDMA(index, value):
global DMA_Control
logging.info("Python function wrDMA() called")
if index == 0:
DMA_Control = value
logging.debug("Write DMA_Control: {}".format(value))
return value
## Read data from peripheral for DMA P2M transfer (VSI DMA)
# @param size size of data to read (in bytes, multiple of 4)
# @return data data read (bytearray)
def rdDataDMA(size):
global Data
logging.info("Python function rdDataDMA() called")
loadAudioFrames(size)
n = min(len(Data), size)
data = bytearray(size)
data[0:n] = Data[0:n]
logging.debug("Read data ({} bytes)".format(size))
return data
## Write data to peripheral for DMA M2P transfer (VSI DMA)
# @param data data to write (bytearray)
# @param size size of data to write (in bytes, multiple of 4)
def wrDataDMA(data, size):
global Data
logging.info("Python function wrDataDMA() called")
Data = data
logging.debug("Write data ({} bytes)".format(size))
return
## Write CONTROL register (user register)
# @param value value to write (32-bit)
def wrCONTROL(value):
global CONTROL
if ((value ^ CONTROL) & CONTROL_ENABLE_Msk) != 0:
if (value & CONTROL_ENABLE_Msk) != 0:
logging.info("Enable Receiver")
openWAVE('test.wav')
else:
logging.info("Disable Receiver")
closeWAVE()
CONTROL = value
## Write CHANNELS register (user register)
# @param value value to write (32-bit)
def wrCHANNELS(value):
global CHANNELS
CHANNELS = value
logging.info("Number of channels: {}".format(value))
## Write SAMPLE_BITS register (user register)
# @param value value to write (32-bit)
def wrSAMPLE_BITS(value):
global SAMPLE_BITS
SAMPLE_BITS = value
logging.info("Sample bits: {}".format(value))
## Write SAMPLE_RATE register (user register)
# @param value value to write (32-bit)
def wrSAMPLE_RATE(value):
global SAMPLE_RATE
SAMPLE_RATE = value
logging.info("Sample rate: {}".format(value))
## Read user registers (the VSI User Registers)
# @param index user register index (zero based)
# @return value value read (32-bit)
def rdRegs(index):
global Regs
logging.info("Python function rdRegs() called")
value = Regs[index]
logging.debug("Read user register at index {}: {}".format(index, value))
return value
## Write user registers (the VSI User Registers)
# @param index user register index (zero based)
# @param value value to write (32-bit)
# @return value value written (32-bit)
def wrRegs(index, value):
global Regs
logging.info("Python function wrRegs() called")
if index == 0:
wrCONTROL(value)
elif index == 1:
wrCHANNELS(value)
elif index == 2:
wrSAMPLE_BITS(value)
elif index == 3:
wrSAMPLE_RATE(value)
Regs[index] = value
logging.debug("Write user register at index {}: {}".format(index, value))
return value
## @}