summaryrefslogtreecommitdiff
path: root/util/uut/cmd.c
blob: 57cf75a29e1161e70d279a0a1c0d01a46d081abe (plain)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* This file creates the UART Program Protocol API commands. */

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "cmd.h"
#include "lib_crc.h"
#include "main.h"

 /* Extracting Byte - 8 bit: MSB, LSB */
#define MSB(u16) ((uint8_t)((uint16_t)(u16) >> 8))
#define LSB(u16) ((uint8_t)(u16))

/*----------------------------------------------------------------------------
 *  SPI Flash commands
 *---------------------------------------------------------------------------
 */

#define SPI_READ_JEDEC_ID_CMD 0x9F
#define SPI_WRITE_ENABLE_CMD 0x06
#define SPI_WRITE_DISABLE_CMD 0x04
#define SPI_READ_STATUS_REG_CMD 0x05
#define SPI_WRITE_STATUS_REG_CMD 0x01
#define SPI_READ_DATA_CMD 0x03
#define SPI_PAGE_PRGM_CMD 0x02
#define SPI_SECTOR_ERASE_CMD 0xD8
#define SPI_BULK_ERASE_CMD 0xC7
#define SPI_READ_PID_CMD 0x90

union cmd_addr {
	uint8_t c_adr[4];
	uint32_t h_adr;
};

/*----------------------------------------------------------------------------
 * Function implementation
 *---------------------------------------------------------------------------
 */

/*----------------------------------------------------------------------------
 * Function:	cmd_create_sync
 *
 * Parameters:	   cmd_info - Pointer to a command buffer.
 *		   cmd_len  - Pointer to command length.
 * Returns:	   none.
 * Side effects:
 * Description:
 *		Create a Host to Device SYNC protocol command.
 *		The total command length is written to 'cmd_len'.
 *---------------------------------------------------------------------------
 */
void cmd_create_sync(uint8_t *cmd_info, uint32_t *cmd_len)
{
	uint32_t len = 0;

	/* Build the command buffer */
	cmd_info[len++] = UFPP_H2D_SYNC_CMD;

	/* Return total command length */
	*cmd_len = len;
}

/*---------------------------------------------------------------------------
 * Function:       cmd_create_write
 *
 * Parameters:	addr    - Memory address to write to.
 *		size    - Size of daya (in bytes) to write to memory.
 *		data_buf - Pointer to data buffer containing raw data to write.
 *		cmd_info - Pointer to a command buffer.
 *		cmd_len  - Pointer to command length.
 * Returns:     none.
 * Side effects:
 * Description:
 *	Create a WRITE protocol command.
 *	The command buffer is enclosed by CRC, calculated on command
 *	information and raw data.
 *	The total command length is written to 'cmd_len'.
 *---------------------------------------------------------------------------
 */
void cmd_create_write(uint32_t addr, uint32_t size, uint8_t *data_buf,
					uint8_t *cmd_info, uint32_t *cmd_len)
{
	uint32_t i;
	union cmd_addr adr_tr;
	uint16_t crc = 0;
	uint32_t len = 0;

	/* Build the command buffer */
	cmd_info[len++] = UFPP_WRITE_CMD;
	cmd_info[len++] = (uint8_t)(size - 1);

	/* Insert Address */
	adr_tr.h_adr = addr;
	cmd_info[len++] = adr_tr.c_adr[3];
	cmd_info[len++] = adr_tr.c_adr[2];
	cmd_info[len++] = adr_tr.c_adr[1];
	cmd_info[len++] = adr_tr.c_adr[0];

	/* Insert data */
	memcpy(&cmd_info[len], data_buf, size);
	len += size;

	/* Calculate CRC */
	for (i = 0; i < len; i++)
		crc = update_crc(crc, (char)cmd_info[i]);

	/* Insert CRC */
	cmd_info[len++] = MSB(crc);
	cmd_info[len++] = LSB(crc);

	/* Return total command length */
	*cmd_len = len;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_create_read
 *
 * Parameters:	addr    - Memory address to read from.
 *		size    - Size of daya (in bytes) to read from memory.
 *		cmd_info - Pointer to a command buffer.
 *		cmd_len  - Pointer to command length.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Create a READ protocol command.
 *		The command buffer is enclosed by CRC, calculated on command
 *		information and raw data.
 *		The total command length is written to 'cmd_len'.
 *---------------------------------------------------------------------------
 */
void cmd_create_read(uint32_t addr, uint8_t size, uint8_t *cmd_info,
					uint32_t *cmd_len)
{
	uint32_t i;
	union cmd_addr adr_tr;
	uint16_t crc = 0;
	uint32_t len = 0;

	/* Build the command buffer */
	cmd_info[len++] = UFPP_READ_CMD;
	cmd_info[len++] = (uint8_t)size;

	/* Insert Address */
	adr_tr.h_adr = addr;
	cmd_info[len++] = adr_tr.c_adr[3];
	cmd_info[len++] = adr_tr.c_adr[2];
	cmd_info[len++] = adr_tr.c_adr[1];
	cmd_info[len++] = adr_tr.c_adr[0];

	/* Calculate CRC */
	for (i = 0; i < len; i++)
		crc = update_crc(crc, (char)cmd_info[i]);

	/* Insert CRC */
	cmd_info[len++] = MSB(crc);
	cmd_info[len++] = LSB(crc);

	/* Return total command length */
	*cmd_len = len;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_create_exec
 *
 * Parameters:	addr    - Memory address to execute from.
 *		cmd_info - Pointer to a command buffer.
 *		cmd_len  - Pointer to command length.
 * Returns:     none.
 * Side effects:
 * Description:
 *		Create an FCALL protocol command.
 *		The command buffer is enclosed by CRC, calculated on command
 *		information and raw data.
 *		The total command length is written to 'cmd_len'.
 *---------------------------------------------------------------------------
 */
void cmd_create_exec(uint32_t addr, uint8_t *cmd_info, uint32_t *cmd_len)
{
	uint32_t i;
	union cmd_addr adr_tr;
	uint16_t crc = 0;
	uint32_t len = 0;

	/* Build the command buffer */
	cmd_info[len++] = UFPP_FCALL_CMD;
	cmd_info[len++] = 0;

	/* Insert Address */
	adr_tr.h_adr = addr;
	cmd_info[len++] = adr_tr.c_adr[3];
	cmd_info[len++] = adr_tr.c_adr[2];
	cmd_info[len++] = adr_tr.c_adr[1];
	cmd_info[len++] = adr_tr.c_adr[0];

	/* Calculate CRC */
	for (i = 0; i < len; i++)
		crc = update_crc(crc, (char)cmd_info[i]);

	/* Insert CRC */
	cmd_info[len++] = MSB(crc);
	cmd_info[len++] = LSB(crc);

	/* Return total command length */
	*cmd_len = len;
}

/*---------------------------------------------------------------------------
 * Function:        cmd_build_sync
 *
 * Parameters:	cmd_buf - Pointer to a command buffer.
 *		cmd_num - Pointer to command number.
 * Returns:	none.
 * Description:
 *		Build a synchronization command buffer.
 *		The total command number is written to 'cmd_num'.
 *---------------------------------------------------------------------------
 */
void cmd_build_sync(struct command_node *cmd_buf, uint32_t *cmd_num)
{
	uint32_t cmd = 0;

	cmd_create_sync(cmd_buf[cmd].cmd, &cmd_buf[cmd].cmd_size);
	cmd_buf[cmd].resp_size = 1;
	cmd++;

	*cmd_num = cmd;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_build_exec_exit
 *
 * Parameters:	addr   - Memory address to execute from.
 *		cmd_buf - Pointer to a command buffer.
 *		cmd_num - Pointer to command number.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Build an Excute command buffer.
 *		Command does not expect the executed code to return, that is,
 *		only FCALL protocol
 *		command code is expected.
 *		Determine the expected response size per each command.
 *		The total command number is written to 'cmd_num'.
 *---------------------------------------------------------------------------
 */
void cmd_build_exec_exit(uint32_t addr, struct command_node *cmd_buf,
						uint32_t *cmd_num)
{
	uint32_t cmd = 0;

	cmd_create_exec(addr, cmd_buf[cmd].cmd, &cmd_buf[cmd].cmd_size);
	cmd_buf[cmd].resp_size = 1;
	cmd++;

	*cmd_num = cmd;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_build_exec_ret
 *
 * Parameters:	addr   - Memory address to execute from.
 *		cmd_buf - Pointer to a command buffer.
 *		cmd_num - Pointer to command number.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Build an Excute command buffer.
 *		Command expects the executed code to return, that is,
 *		FCALL_RSLT protocol command
 *		code is expected, together with the execution result.
 *		Determine the expected response size per each command.
 *		The total command number is written to 'cmd_num'.
 *---------------------------------------------------------------------------
 */
void cmd_build_exec_ret(uint32_t addr, struct command_node *cmd_buf,
						uint32_t *cmd_num)
{
	uint32_t cmd = 0;

	cmd_create_exec(addr, cmd_buf[cmd].cmd, &cmd_buf[cmd].cmd_size);
	cmd_buf[cmd].resp_size = 3;
	cmd++;

	*cmd_num = cmd;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_disp_sync
 *
 * Parameters:	resp_buf - Pointer to a response buffer.
 * Returns:	TRUE if successful, FALSE in the case of an error.
 * Side effects:
 * Description:
 *		Display SYNC command response information.
 *---------------------------------------------------------------------------
 */
bool cmd_disp_sync(uint8_t *resp_buf)
{
	if (resp_buf[0] == (uint8_t)(UFPP_D2H_SYNC_CMD)) {
		display_color_msg(SUCCESS, "Host/Device are synchronized\n");
		return true;
	}

	display_color_msg(FAIL, "Host/Device synchronization failed!!!\n");
	return false;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_disp_write
 *
 * Parameters:	resp_buf - Pointer to a response buffer.
 *		resp_size - Response size.
 *		resp_num - Response packet number.
 * Returns:	TRUE if successful, FALSE in the case of an error.
 * Side effects:
 * Description:
 *		Display WRITE command response information.
 *---------------------------------------------------------------------------
 */
bool cmd_disp_write(uint8_t *resp_buf, uint32_t resp_size, uint32_t resp_num,
					  uint32_t total_size)
{
	if (resp_buf[0] == (uint8_t)(UFPP_WRITE_CMD)) {
		display_color_msg(SUCCESS,
			"\rTransmitted packet of size %u bytes, packet "
			"[%u]out of [%u]",
			resp_size, resp_num, total_size);
		return true;
	}

	display_color_msg(FAIL, "\nWrite packet [%lu] Failed\n", resp_num);
	return false;
}

/*-----------------------------------------------------------------------------
 * Function:	cmd_disp_read
 *
 * Parameters:	resp_buf  - Pointer to a response buffer.
 *		resp_size - Response size.
 *		resp_num  - Response packet number.
 * Returns:	TRUE if successful, FALSE in the case of an error.
 * Side effects:
 * Description:
 *		Display READ command response information.
 *---------------------------------------------------------------------------
 */
bool cmd_disp_read(uint8_t *resp_buf, uint32_t resp_size, uint32_t resp_num,
					uint32_t total_size)
{
	if (resp_buf[0] == (uint8_t)(UFPP_READ_CMD)) {
		display_color_msg(SUCCESS,
			"\rReceived packet of size %u bytes, packet [%u] out "
			"of [%u]",
			resp_size, resp_num, total_size);
		return true;
	}

	display_color_msg(FAIL, "\nRead packet [%u] Failed\n", resp_num);
	return false;
}

/*----------------------------------------------------------------------------
 * Function:	cmd_disp_data
 *
 * Parameters:	resp_buf  - Pointer to a response buffer.
 *		resp_size - Response size.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Display raw data, read from memory.
 *---------------------------------------------------------------------------
 */
void cmd_disp_data(uint8_t *resp_buf, uint32_t resp_size)
{
	uint32_t idx;
	uint32_t i;

	for (idx = 0; idx < resp_size; idx += 4) {
		if ((idx % 16) == 0)
			printf("\n");

		printf("0x");
		for (i = 4; i > 0; i--)
			printf("%02x", resp_buf[idx + i - 1]);

		if ((idx % 4) == 0)
			printf(" ");
	}

	printf("\n");
}

/*----------------------------------------------------------------------------
 * Function:	cmd_disp_flash_erase_dev
 *
 * Parameters:	resp_buf - Pointer to a response buffer.
 *		dev_num  - Flash Device Number.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Display BULK_ERASE command response information.
 *---------------------------------------------------------------------------
 */
void cmd_disp_flash_erase_dev(uint8_t *resp_buf, uint32_t dev_num)
{
	if (resp_buf[0] == (uint8_t)(UFPP_WRITE_CMD)) {
		display_color_msg(SUCCESS,
			"Flash Erase of device [%u] Passed\n", dev_num);
	} else {
		display_color_msg(
			FAIL, "Flash Erase of device [%u] Failed\n", dev_num);
	}
}

/*---------------------------------------------------------------------------
 * Function:	cmd_disp_flash_erase_sect
 *
 * Parameters:	resp_buf - Pointer to a response buffer.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Display BULK_ERASE command response information.
 *---------------------------------------------------------------------------
 */
void cmd_disp_flash_erase_sect(uint8_t *resp_buf, uint32_t dev_num)
{
	if (resp_buf[0] == (uint8_t)(UFPP_WRITE_CMD)) {
		display_color_msg(SUCCESS,
			"Sector Erase of device [%lu] Passed\n", dev_num);
	} else {
		display_color_msg(
			FAIL, "Sector Erase of device [%lu] Failed\n", dev_num);
	}
}

/*---------------------------------------------------------------------------
 * Function:	cmd_disp_exec_exit
 *
 * Parameters:	resp_buf - Pointer to a response buffer.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Display Execute command response information.
 *---------------------------------------------------------------------------
 */
void cmd_disp_exec_exit(uint8_t *resp_buf)
{
	if (resp_buf[0] == (uint8_t)(UFPP_FCALL_CMD))
		display_color_msg(SUCCESS, "Execute Command Passed\n");
	else
		display_color_msg(FAIL, "Execute Command Failed\n");
}

/*---------------------------------------------------------------------------
 * Function:	cmd_disp_exec_ret
 *
 * Parameters:	resp_buf - Pointer to a response buffer.
 * Returns:	none.
 * Side effects:
 * Description:
 *		Display Execute Result command response information.
 *---------------------------------------------------------------------------
 */
void cmd_disp_exec_ret(uint8_t *resp_buf)
{
	if (resp_buf[1] == (uint8_t)(UFPP_FCALL_RSLT_CMD)) {
		display_color_msg(SUCCESS,
			"Execute Command Passed, execution result is [0x%X]\n",
			resp_buf[2]);
	} else {
		display_color_msg(FAIL,
			"Execute Command Failed  [0x%X]  [0x%X], rslt=[0x%X]\n",
			resp_buf[0], resp_buf[1], resp_buf[2]);
	}
}