-
Notifications
You must be signed in to change notification settings - Fork 1
/
S1V30120.cpp
executable file
·260 lines (201 loc) · 8.23 KB
/
S1V30120.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
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
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: S1V30120.cpp *
* *
***************************************************************************/
#include "armlib.h"
// Header file that includes the TTS engine firmware.
#include "S1V30120_INIT_DATA_ver2_1_5.h"
/// Reserve memory for the singleton object.
static S1V30120 s1v30120SingletonObject;
/**
* Get a pointer to the S1V30120 object.
*/
S1V30120 *S1V30120::GetInstance()
{
return &s1v30120SingletonObject;
}
/**
* Setup the S1V30120 TTS engine.
*/
void S1V30120::Enable()
{
IOSetCS (true);
IOSetReset(false);
SystemControl::Sleep(5);
// Reset the hardware and allow the TTS masked ROM to boot and run.
IOSetReset(true);
SystemControl::Sleep(150);
DownloadImage();
}
/**
* Convert text to audio speech.
*
* @param text pointer to NULL terminated string
*/
void S1V30120::Speak (const char *text)
{
uint8_t payload[16];
char output[300];
output[0] = 0x01;
strcpy (output + 1, text);
responseLength = sizeof(payload);
SendRequest(ICS_TTS_SPEAK_REQUEST, (uint8_t *) (output), strlen(text) + 2);
}
/**
* Download the firmware image to the TTS engine.
*/
void S1V30120::DownloadImage()
{
uint32_t index, blockLength;
SendRequest(VERSION_REQUEST, NULL, 0);
for (index = 0; index < sizeof(TTSInitData); index += MaxDownloadBlock)
{
blockLength = sizeof(TTSInitData) - index;
if (blockLength > MaxDownloadBlock)
blockLength = MaxDownloadBlock;
SendRequest(BOOT_LOAD_REQUEST, TTSInitData + index, blockLength);
}
SendRequest(RUN_REQUEST, NULL, 0);
// Allow the downloaded image time to boot and run.
SystemControl::Sleep(150);
// Enable the TTS SPI interface.
static const uint8_t TestRequest[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
SendRequest(TEST_REQUEST, &TestRequest, sizeof(TestRequest));
// Query the version information again to make sure our firmware is running.
SendRequest(VERSION_REQUEST, NULL, 0);
static const uint8_t AudiConfig[] = { 0x00, 0x43, 0x00, 0x01, 0x00, 0, 0x00, 0x01 };
SendRequest(ICS_AUDIO_CONFIG_REQUEST, AudiConfig, sizeof(AudiConfig));
static const uint8_t PowerConfig[] = { 0x01, 0x00, 0x01, 0x00 };
SendRequest(ICS_PMAN_CONFIG_REQUEST, PowerConfig, sizeof(PowerConfig));
static const uint8_t ConfigRequest[] = { 0x01, 0x07, 0x01, 0x00, 0xc8, 0x00, 0x00, 0x00 };
SendRequest(ICS_TTS_CONFIG_REQUEST, ConfigRequest, sizeof(ConfigRequest));
}
/**
* Generate a request message for the S1V30120 TTS engine and wait for the response.
*
* @param messageID of the request message to send
* @param payload pointer to optional message payload bytes of request
* @param length number of bytes in payload
*/
bool_t S1V30120::SendRequest(uint32_t messageID, const void *payload, uint32_t length)
{
uint32_t i;
// Wrap the whole request with the chip select.
IOSetCS (false);
// Start message indicator.
SPIWrite (MessageStart);
// Number of bytes including the header.
SPIWrite ((length + 4) & 0x00ff);
SPIWrite ((length + 4) >> 8);
SPIWrite (messageID & 0x00ff);
SPIWrite (messageID >> 8);
for (i = 0; i < length; ++i)
SPIWrite (static_cast<const uint8_t *> (payload)[i]);
return IsResponse();
}
/**
* One shot that indicates the last message sent to the TTS engine has finished.
*
* @return true when current message is finished; otherwise false
*/
bool_t S1V30120::IsTTSFinished()
{
if (!IsResponse())
return false;
// Make sure the response is TTS Finished.
if (this->responseMessageID == ICS_TTS_FINISHED_IND)
return true;
return false;
}
/**
* Wait for a response message from the TTS engine.
*
* @return true if response message processed, otherwise false.
*/
bool_t S1V30120::IsResponse()
{
uint32_t read, count;
ResponseStateMachine state;
// Wrap the whole request with the chip select.
IOSetCS (false);
state = WaitMessageStart;
count = 0;
for (;;)
{
// Read a byte from the SPI bus.
read = SPIRead();
// Process based on the internal state machine.
switch (state)
{
case WaitMessageStart:
if (read == MessageStart)
state = LengthLSB;
else
{
if (++count == WaitResponseByteCount)
{
this->responseLength = 0x00;
IOSetCS (true);
return false;
}
// The TTS engine requires some processing time. So rather than flood the SPI bus with read requests, we will slow down.
// The maximum wait time is 40mS; 5mS sleep * WaitResponseByteCount (8)
SystemControl::Sleep(5);
}
break;
case LengthLSB:
this->responseLength = read;
state = LengthMSB;
break;
case LengthMSB:
this->responseLength |= (read << 8);
state = MessageIDLSB;
break;
case MessageIDLSB:
this->responseMessageID = read;
state = MessageIDMSB;
break;
case MessageIDMSB:
this->responseMessageID |= (read << 8);
state = Payload;
count = 0;
this->responseLength -= 4;
if (this->responseLength == 0)
{
IOSetCS (true);
return true;
}
break;
case Payload:
if (count < ResponseBufferLength)
this->response[count] = read;
if (++count >= this->responseLength)
{
IOSetCS (true);
return true;
} // END if
break;
} // END switch
} // END for
}