-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_x-cube-ai.c
402 lines (347 loc) · 11.4 KB
/
app_x-cube-ai.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#ifdef __cplusplus
extern "C"
{
#endif
/**
******************************************************************************
* @file : app_x-cube-ai.c
* @brief : AI program body
******************************************************************************
* This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* Copyright (c) 2018 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "app_x-cube-ai.h"
#include "bsp_ai.h"
#include "aiSystemPerformance.h"
#include "ai_datatypes_defines.h"
#include "data_Processor.h"
#include "ai_utilities.h"
/*************************************************************************
*
*/
extern I2C_HandleTypeDef hi2c1;
/*Accelerometer module address*/
#define adxl_address 0x53<<1
/* USER CODE BEGIN 0 */
uint8_t AccelData[6];
uint8_t chipid = 0;
int16_t x, y, z;
static DATA_input_t ACC_Value_Raw;
static uint8_t Output;
/* USER CODE END 0 */
/* Write the value into corresponding register address*/
void AccelWrite (uint8_t reg, uint8_t value)
{
uint8_t data[2];
data[0] = reg;
data[1] = value;
HAL_I2C_Master_Transmit(&hi2c1, adxl_address, data, 2, 100);
}
/* Read the values from corresponding register address*/
void AccelReadValues (uint8_t reg, uint8_t numberofbytes)
{
HAL_I2C_Mem_Read(&hi2c1, adxl_address, reg, 1, (uint8_t *) AccelData,
numberofbytes, 100);
}
/*Read the address of chip */
void AccelReadAddress(uint8_t reg)
{
HAL_I2C_Mem_Read(&hi2c1, adxl_address, reg, 1, &chipid, 1, 100);
}
void AccelInit(void)
{
AccelReadValues(0x00, 1);
AccelWrite(0x2d, 0x00); // reset all bits
AccelWrite(0x2d, 0x08); // power_cntl measure and wake up 8hz*/
AccelWrite(0x31, 0x00); // data_format range= +- 2g
}
/* USER CODE END 0 */
void MX_X_CUBE_AI_Init(void)
{
MX_UARTx_Init();
printf("Print: AR=%s\r\n", "testing");
AccelInit();
DATA_InitProcesser();
}
extern UART_HandleTypeDef huart2;
void MX_X_CUBE_AI_Process(void)
{
AccelReadValues(0x32, 6);
x = ((AccelData[1] << 8) | AccelData[0]);
y = ((AccelData[3] << 8) | AccelData[2]);
z = ((AccelData[5] << 8) | AccelData[4]);
ACC_Value_Raw.AccX = (x * .039);
ACC_Value_Raw.AccY = (y * .039);
ACC_Value_Raw.AccZ = (z * .039);
HAL_Delay(50);
Output = DATA_Infer(ACC_Value_Raw);
switch(Output)
{
case 0:
printf("fault1");
break;
case 1:
printf("fault2");
break;
case 2:
printf("fault3");
break;
case 3:
printf("Normal");
break;
default:
printf("None");
break;
}
printf("\r\n");
}
/* Multiple network support --------------------------------------------------*/
#include <string.h>
#include "ai_datatypes_defines.h"
static const ai_network_entry_t networks[AI_MNETWORK_NUMBER] = { { .name =
(const char *) AI_NETWORK_MODEL_NAME, .config = AI_NETWORK_DATA_CONFIG,
.ai_get_info = ai_network_get_info, .ai_create = ai_network_create,
.ai_destroy = ai_network_destroy, .ai_get_error = ai_network_get_error,
.ai_init = ai_network_init, .ai_run = ai_network_run, .ai_forward =
ai_network_forward, .ai_data_weights_get_default =
ai_network_data_weights_get, .params = {
AI_NETWORK_DATA_WEIGHTS(0),
AI_NETWORK_DATA_ACTIVATIONS(0) }, }, };
struct network_instance
{
const ai_network_entry_t *entry;
ai_handle handle;
ai_network_params params;
};
/* Number of instance is aligned on the number of network */
AI_STATIC struct network_instance gnetworks[AI_MNETWORK_NUMBER] = { 0 };
AI_DECLARE_STATIC
ai_bool ai_mnetwork_is_valid(const char* name, const ai_network_entry_t *entry)
{
if (name && (strlen(entry->name) == strlen(name))
&& (strncmp(entry->name, name, strlen(entry->name)) == 0))
return true;
return false;
}
AI_DECLARE_STATIC
struct network_instance *ai_mnetwork_handle(struct network_instance *inst)
{
for (int i = 0; i < AI_MNETWORK_NUMBER; i++)
{
if ((inst) && (&gnetworks[i] == inst))
return inst;
else if ((!inst) && (gnetworks[i].entry == NULL))
return &gnetworks[i];
}
return NULL;
}
AI_DECLARE_STATIC
void ai_mnetwork_release_handle(struct network_instance *inst)
{
for (int i = 0; i < AI_MNETWORK_NUMBER; i++)
{
if ((inst) && (&gnetworks[i] == inst))
{
gnetworks[i].entry = NULL;
return;
}
}
}
AI_API_ENTRY
const char* ai_mnetwork_find(const char *name, ai_int idx)
{
const ai_network_entry_t *entry;
for (int i = 0; i < AI_MNETWORK_NUMBER; i++)
{
entry = &networks[i];
if (ai_mnetwork_is_valid(name, entry))
return entry->name;
else
{
if (!idx--)
return entry->name;
}
}
return NULL;
}
AI_API_ENTRY
ai_error ai_mnetwork_create(const char *name, ai_handle* network,
const ai_buffer* network_config)
{
const ai_network_entry_t *entry;
const ai_network_entry_t *found = NULL;
ai_error err;
struct network_instance *inst = ai_mnetwork_handle(NULL);
if (!inst)
{
err.type = AI_ERROR_ALLOCATION_FAILED;
err.code = AI_ERROR_CODE_NETWORK;
return err;
}
for (int i = 0; i < AI_MNETWORK_NUMBER; i++)
{
entry = &networks[i];
if (ai_mnetwork_is_valid(name, entry))
{
found = entry;
break;
}
}
if (!found)
{
err.type = AI_ERROR_INVALID_PARAM;
err.code = AI_ERROR_CODE_NETWORK;
return err;
}
if (network_config == NULL)
err = found->ai_create(network, found->config);
else
err = found->ai_create(network, network_config);
if ((err.code == AI_ERROR_CODE_NONE) && (err.type == AI_ERROR_NONE))
{
inst->entry = found;
inst->handle = *network;
*network = (ai_handle*) inst;
}
return err;
}
AI_API_ENTRY
ai_handle ai_mnetwork_destroy(ai_handle network)
{
struct network_instance *inn;
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn)
{
ai_handle hdl = inn->entry->ai_destroy(inn->handle);
if (hdl != inn->handle)
{
ai_mnetwork_release_handle(inn);
network = AI_HANDLE_NULL;
}
}
return network;
}
AI_API_ENTRY
ai_bool ai_mnetwork_get_info(ai_handle network, ai_network_report* report)
{
struct network_instance *inn;
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn)
return inn->entry->ai_get_info(inn->handle, report);
else
return false;
}
AI_API_ENTRY
ai_error ai_mnetwork_get_error(ai_handle network)
{
struct network_instance *inn;
ai_error err;
err.type = AI_ERROR_INVALID_PARAM;
err.code = AI_ERROR_CODE_NETWORK;
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn)
return inn->entry->ai_get_error(inn->handle);
else
return err;
}
AI_API_ENTRY
ai_bool ai_mnetwork_init(ai_handle network, const ai_network_params* params)
{
struct network_instance *inn;
ai_network_params par;
/* TODO: adding check ai_buffer activations/weights shape coherence */
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn)
{
par = inn->entry->params;
if (params->activations.n_batches)
par.activations = params->activations;
else
par.activations.data = params->activations.data;
if (params->params.n_batches)
par.params = params->params;
else
par.params.data = inn->entry->ai_data_weights_get_default();
return inn->entry->ai_init(inn->handle, &par);
} else
return false;
}
AI_API_ENTRY
ai_i32 ai_mnetwork_run(ai_handle network, const ai_buffer* input,
ai_buffer* output)
{
struct network_instance* inn;
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn)
return inn->entry->ai_run(inn->handle, input, output);
else
return 0;
}
AI_API_ENTRY
ai_i32 ai_mnetwork_forward(ai_handle network, const ai_buffer* input)
{
struct network_instance *inn;
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn)
return inn->entry->ai_forward(inn->handle, input);
else
return 0;
}
AI_API_ENTRY
int ai_mnetwork_get_private_handle(ai_handle network, ai_handle *phandle,
ai_network_params *pparams)
{
struct network_instance* inn;
inn = ai_mnetwork_handle((struct network_instance *) network);
if (inn && phandle && pparams)
{
*phandle = inn->handle;
*pparams = inn->params;
return 0;
} else
return -1;
}
#ifdef __cplusplus
}
#endif