forked from PaulFreund/libSWD-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibswd_memap.c
1039 lines (907 loc) · 39.7 KB
/
libswd_memap.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Serial Wire Debug Open Library.
* MEM-AP Routines Body File.
*
* Copyright (C) 2010-2014, Tomasz Boleslaw CEDRO (http://www.tomek.cedro.info)
* 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. Redistributions 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 the Tomasz Boleslaw CEDRO nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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.*
*
* Written by Tomasz Boleslaw CEDRO <cederom@tlen.pl>, 2010-2014;
*
*/
/** \file libswd_memap.c MEM-AP related routines. */
#include <libswd.h>
/*******************************************************************************
* \defgroup libswd_memap High-level MEM-AP (Memory Access Port) operations.
* These routines are based on DAP operations.
* Return values: negative number on error, LIBSWD_OK on success.
******************************************************************************/
/** Initialize the MEM-AP.
* This function will set DbgSwEnable, DeviceEn, 32-bit Size in CSW.
* This function will disable Tar Auto Increment.
* Use libswd_memap_setup() to set specific CSW and TAR values.
* \param *libswdctx swd context to work on.
* \return LIBSWD_OK on success or LIBSWD_ERROR code on failure.
*/
int libswd_memap_init(libswd_ctx_t *libswdctx, libswd_operation_t operation){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Executing libswd_memap_init(*libswdctx=%p, operation=%s)...\n",
(void*)libswdctx, libswd_operation_string(operation) );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
int res=0, *memapidr, *memapbase, *memapcswp, memapcsw;
// Verify if DAP is already initialized, do so in necessary.
if (!libswdctx->log.dp.initialized)
{
int *idcode;
res=libswd_dap_init(libswdctx, operation, &idcode);
if (res<0) goto libswd_memap_init_error;
}
// Select MEM-AP.
//res=libswd_ap_select(libswdctx, operation, LIBSWD_MEMAP_APSEL_VAL);
//if (res<0) goto libswd_memap_init_error;
// TODO: DO WE NEED LIBSWD_AP_SELECT ???
// Check IDentification Register, use cached value if possible.
if (!libswdctx->log.memap.idr)
{
res=libswd_ap_read(libswdctx, operation, LIBSWD_MEMAP_IDR_ADDR, &memapidr);
if (res<0) goto libswd_memap_init_error;
libswdctx->log.memap.idr=*memapidr;
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_init(): MEM-AP IDR=0x%08X\n",
libswdctx->log.memap.idr );
// Check Debug BASE Address Register, use cached value if possible.
if (!libswdctx->log.memap.base)
{
res=libswd_ap_read(libswdctx, operation, LIBSWD_MEMAP_BASE_ADDR, &memapbase);
if (res<0) goto libswd_memap_init_error;
libswdctx->log.memap.base=*memapbase;
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_init(): MEM-AP BASE=0x%08X\n",
libswdctx->log.memap.base );
// Setup the CSW (MEM-AP Control and Status) register.
memapcsw=0;
// Check if DbgSwEnable bit is set, set if necessary.
memapcsw|=LIBSWD_MEMAP_CSW_DBGSWENABLE;
memapcsw&=~LIBSWD_MEMAP_CSW_ADDRINC;
memapcsw&=~LIBSWD_MEMAP_CSW_SIZE;
memapcsw|=LIBSWD_MEMAP_CSW_SIZE_32BIT;
// Write new CSW value.
res=libswd_ap_write(libswdctx, operation, LIBSWD_MEMAP_CSW_ADDR, &memapcsw);
if (res<0) goto libswd_memap_init_error;
// Read back and cache CSW value.
res=libswd_ap_read(libswdctx, operation, LIBSWD_MEMAP_CSW_ADDR, &memapcswp);
if (res<0) goto libswd_memap_init_error;
libswdctx->log.memap.csw=(*memapcswp);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_init(): MEM-AP CSW=0x%08X\n",
libswdctx->log.memap.csw);
// Mark MEM-AP as configured.
libswdctx->log.memap.initialized=1;
return LIBSWD_OK;
libswd_memap_init_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"LIBSWD_E: libswd_memap_init(): Cannot initialize MEM-AP (%s)!\n",
libswd_error_string(res) );
return res;
}
/** Setup the MEM-AP.
* Use this function to setup CSW and TAR values for given MEM-AP operations.
* This setup needs to be done before MEM-AP with different access size.
* Function will try to compare agains chahed values to save bus traffic.
* This function will set DBGSWENABLE and PROT bits in CSW by default.
* \param *libswd LibSWD context to work on.
* \param operation is the LIBSWD_OPERATION type.
* \param csw is the CSW register value to be set.
* \param tar is the TAR register value to be set.
* \return LIBSWD_OK on success, LIBSWD_ERROR otherwise.
*/
int libswd_memap_setup(libswd_ctx_t *libswdctx, libswd_operation_t operation, int csw, int tar){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_setup(*libswdctx=%p, operation=%s, csw=0x%08X, tar=0x%08X)...\n",
(void*)libswdctx, libswd_operation_string(operation), csw, tar );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
int res, *memapcswp, memapcsw, *memaptarp;
// Verify if MEM-AP is already initialized, do so in necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_setup_error;
}
// Remember to set these bits not to lock-out the Debug...
memapcsw=csw|LIBSWD_MEMAP_CSW_DBGSWENABLE;
memapcsw|=LIBSWD_MEMAP_CSW_PROT; // PROT ENABLES DEBUG!!
// Update MEM-AP CSW register if necessary.
if (memapcsw!=libswdctx->log.memap.csw)
{
// Write register value.
res=libswd_ap_write(libswdctx, operation, LIBSWD_MEMAP_CSW_ADDR, &memapcsw);
if (res<0) goto libswd_memap_setup_error;
// Read-back and cache CSW value.
res=libswd_ap_read(libswdctx, operation, LIBSWD_MEMAP_CSW_ADDR, &memapcswp);
if (res<0) goto libswd_memap_setup_error;
libswdctx->log.memap.csw=(*memapcswp);
}
// Update MEM-AP TAR register if necessary.
if (tar!=libswdctx->log.memap.tar)
{
// Write register value.
res=libswd_ap_write(libswdctx, operation, LIBSWD_MEMAP_TAR_ADDR, &tar);
if (res<0) goto libswd_memap_setup_error;
// Read-back and cache TAR value.
res=libswd_ap_read(libswdctx, operation, LIBSWD_MEMAP_TAR_ADDR, &memaptarp);
if (res<0) goto libswd_memap_setup_error;
libswdctx->log.memap.tar=(*memaptarp);
}
return LIBSWD_OK;
libswd_memap_setup_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"LIBSWD_E: libswd_memap_setup(): Cannot setup MEM-AP (%s)!\n",
libswd_error_string(res) );
return res;
}
/** Generic read using MEM-AP into char array.
* Data are stored into char array. Count shows CHAR elements.
* Remember to setup MEM-AP first for valid access!
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to read with MEM-AP.
* \param count is the number of bytes to read.
* \param *data is the pointer to char array where result will be stored.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_read_char(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, char *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_read_char(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, *data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void*)data );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int i, loc, res=0, accsize=0, *memapdrw;
float tdeltam;
struct timeval tstart, tstop;
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_read_char_error;
}
// Verify the count parameter to be access boundary.
switch (libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_SIZE)
{
case LIBSWD_MEMAP_CSW_SIZE_8BIT:
accsize=1;
break;
case LIBSWD_MEMAP_CSW_SIZE_16BIT:
accsize=2;
break;
case LIBSWD_MEMAP_CSW_SIZE_32BIT:
accsize=4;
break;
default:
res=LIBSWD_ERROR_MEMAPACCSIZE;
goto libswd_memap_read_char_error;
}
if (count%accsize) count=count-(count%accsize);
// Check for alignment issues.
if ((addr%accsize)!=0)
{
res=LIBSWD_ERROR_MEMAPALIGN;
goto libswd_memap_read_char_error;
}
// Mark start time for transfer speed measurement.
gettimeofday(&tstart, NULL);
// Perform word-by-word read operation and implode result into char array.
if (!(libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_ADDRINC))
{
// Use manual TAR incrementation (slower).
for (i=0;i<count;i+=accsize)
{
int tmp;
int drw_shift;
loc=addr+i;
// Calculate the offset in DRW where the data should be
// see Data byte-laning in the ARM debug interface v5 documentation
// note: this only works for little endian systems.
drw_shift=8*(loc%4);
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_read_char() reading address 0x%08X (speed %fKB/s)\r",
loc, count/tdeltam );
fflush(0);
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_read_char_error;
libswdctx->log.memap.tar=loc;
// Read data from DRW register.
res=libswd_ap_read(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &memapdrw);
if (res<0) goto libswd_memap_read_char_error;
libswdctx->log.memap.drw=*memapdrw;
// apply the data byte-laning shift
tmp=*memapdrw >>= drw_shift;
memcpy((void*)data+i, &tmp, accsize);
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
else
{
// Use TAR Auto Increment (faster).
// TAR auto increment is only guaranteed to work on the bottom 10 bits
// of the TAR register. Above that it is implementation defined.
// We use 1024 byte chunks as it will work on every platform
// and one TAR write every 1024 bytes is not adding too much overhead.
const unsigned int BOUNDARY = 1024;
unsigned int i;
// Check if packed transfer, if so use word access.
if (libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_ADDRINC_PACKED) accsize=4;
for (loc = addr, i = 0; loc < (addr + count); loc += accsize, i+= accsize)
{
int tmp;
int drw_shift;
// Calculate the offset in DRW where the data should be
// see Data byte-laning in the ARM debug interface v5 documentation
// note: this only works for little endian systems.
drw_shift=8*(loc%4);
// only write the TAR register, if this is the first time through the loop.
// or if we've passed over the boundary where TAR auto inc isn't guaranteed
// to work anymore.
if (loc == addr ||
(loc % BOUNDARY) == 0)
{
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_read_char_error;
libswdctx->log.memap.tar=loc;
}
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_read_char() reading address 0x%08X (speed %fKB/s)\r",
loc, count/tdeltam);
fflush(0);
// Read data from the DRW register.
res=libswd_ap_read(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &memapdrw);
if (res<0) goto libswd_memap_read_char_error;
libswdctx->log.memap.drw=*memapdrw;
// apply the data byte-laning shift
tmp=*memapdrw >>= drw_shift;
memcpy((void*)data + i, &tmp, accsize);
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
return LIBSWD_OK;
libswd_memap_read_char_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"\nLIBSWD_E: libswd_memap_read_char(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic read using MEM-AP to char array, with prior CSW setup.
* Data are stored into char array.
* Remember to setup CSW first for valid bus access!
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to read with MEM-AP.
* \param count is the number of bytes to read.
* \param *data is the pointer to char array where result will be stored.
* \param csw is the value of csw register to write prior data write.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_read_char_csw(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, char *data, int csw){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_read_char_csw(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, *data=%p, csw=0x%X)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data, csw);
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int res=0, accsize=0;
// Calculate required access size based on CSW value.
switch (csw&LIBSWD_MEMAP_CSW_SIZE)
{
case LIBSWD_MEMAP_CSW_SIZE_8BIT:
accsize=1;
break;
case LIBSWD_MEMAP_CSW_SIZE_16BIT:
accsize=2;
break;
case LIBSWD_MEMAP_CSW_SIZE_32BIT:
accsize=4;
break;
default:
res=LIBSWD_ERROR_MEMAPACCSIZE;
goto libswd_memap_read_char_csw_error;
}
// Verify the count parameter to be access boundary.
if (count%accsize) count=count-(count%accsize);
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_read_char_csw_error;
}
// Setup MEM-AP CSW and TAR.
res=libswd_memap_setup(libswdctx, operation, csw, addr);
if (res<0) goto libswd_memap_read_char_csw_error;
// Perform the read operation.
res=libswd_memap_read_char(libswdctx, operation, addr, count, data);
if (res<0) goto libswd_memap_read_char_csw_error;
return LIBSWD_OK;
libswd_memap_read_char_csw_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"\nLIBSWD_E: libswd_memap_read_char_csw(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic read using MEM-AP to char array, using 32-bit data access.
* Data are stored into char array.
* Remember to setup CSW first for valid bus width!
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to read with MEM-AP.
* \param count is the number of bytes to read.
* \param *data is the pointer to char array where result will be stored.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_read_char_32(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, char *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_read_char_32(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, *data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data );
return libswd_memap_read_char_csw(libswdctx, operation, addr, count, data, LIBSWD_MEMAP_CSW_SIZE_32BIT|LIBSWD_MEMAP_CSW_ADDRINC_SINGLE);
}
/** Generic read using MEM-AP into int array.
* Data are stored into int array. Count shows INT elements.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to read with MEM-AP.
* \param count is the number of words to read.
* \param *data is the pointer to int array where result will be stored.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_read_int(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, int *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_read_int(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, *data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data);
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int i, loc, res, *memapdrw;
float tdeltam;
struct timeval tstart, tstop;
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_read_int_error;
}
// Mark start time for transfer speed measurement.
gettimeofday(&tstart, NULL);
// Perform word-by-word read operation and store result into int array.
if (!(libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_ADDRINC))
{
// Use manual TAR incrementation (slower).
for (i=0;i<count;i++)
{
loc=addr+i*4;
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_read_int() reading address 0x%08X (speed %fKB/s)\r",
loc, count*4/tdeltam );
fflush(0);
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_read_int_error;
libswdctx->log.memap.tar=loc;
// Read data from DRW register.
res=libswd_ap_read(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &memapdrw);
if (res<0) goto libswd_memap_read_int_error;
libswdctx->log.memap.drw=*memapdrw;
data[i]=*memapdrw;
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
else
{
// Use TAR Auto Increment (faster).
// TAR auto increment is only guaranteed to work on the bottom 10 bits
// of the TAR register. Above that it is implementation defined.
// We use 1024 byte chunks as it will work on every platform
// and one TAR write every 1024 bytes is not adding too much overhead.
const unsigned int BOUNDARY = 1024;
unsigned int i;
for (loc = addr, i = 0; loc < (addr + (count * 4)); loc += 4, i++)
{
// only write the TAR register, if this is the first time through the loop.
// or if we've passed over the boundary where TAR auto inc isn't guaranteed
// to work anymore.
if (loc == addr ||
(loc % BOUNDARY) == 0)
{
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_read_int_error;
libswdctx->log.memap.tar=loc;
}
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_read_int() reading address 0x%08X (speed %fKB/s)\r",
loc, count*4/tdeltam );
fflush(0);
// Read data from the DRW register.
res=libswd_ap_read(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &memapdrw);
if (res<0) goto libswd_memap_read_int_error;
libswdctx->log.memap.drw=*memapdrw;
data[i]=*memapdrw;
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
return LIBSWD_OK;
libswd_memap_read_int_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"\nLIBSWD_E: libswd_memap_read_int(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic read using MEM-AP, with prior CSW MEM-AP access setup.
* Data are stored into int array.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to read with MEM-AP.
* \param count is the number of words to read.
* \param *data is the pointer to int array where result will be stored.
* \param csw is the value of csw register to write prior data write.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_read_int_csw(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, int *data, int csw){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_read_int_csw(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, *data=%p, csw=0x%X)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data, csw );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int res=0;
// Calculate required access size based on CSW value.
switch (csw&LIBSWD_MEMAP_CSW_SIZE)
{
case LIBSWD_MEMAP_CSW_SIZE_8BIT:
case LIBSWD_MEMAP_CSW_SIZE_16BIT:
case LIBSWD_MEMAP_CSW_SIZE_32BIT:
break;
default:
res=LIBSWD_ERROR_MEMAPACCSIZE;
goto libswd_memap_read_int_csw_error;
}
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_read_int_csw_error;
}
// Setup MEM-AP CSW and TAR.
res=libswd_memap_setup(libswdctx, operation, csw, addr);
if (res<0) goto libswd_memap_read_int_csw_error;
// Perform the read operation.
res=libswd_memap_read_int(libswdctx, operation, addr, count, data);
if (res<0) goto libswd_memap_read_int_csw_error;
return LIBSWD_OK;
libswd_memap_read_int_csw_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"\nLIBSWD_E: libswd_memap_read_int_csw(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic read using MEM-AP, with prior 32-bit MEM-AP access setup.
* Data are stored into int array.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to read with MEM-AP.
* \param count is the number of words to read.
* \param *data is the pointer to int array where result will be stored.
* \param csw is the value of csw register to write prior data write.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_read_int_32(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, int *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_read_int_32(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, *data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data);
return libswd_memap_read_int_csw(libswdctx, operation, addr, count, data, LIBSWD_MEMAP_CSW_SIZE_32BIT|LIBSWD_MEMAP_CSW_ADDRINC_SINGLE);
}
/** Generic write using MEM-AP from char array.
* Data are read from char array. Count shows CHAR elements.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to write with MEM-AP.
* \param count is the number of bytes to write.
* \param *data is the pointer to data to be written.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_write_char(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, char *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_write_char(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, **data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void*)data );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int i, loc, res=0, accsize=0;
float tdeltam;
struct timeval tstart, tstop;
// Initialize MEM-AP if neessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_write_char_error;
}
// Verify the count parameter to be access boundary.
switch (libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_SIZE)
{
case LIBSWD_MEMAP_CSW_SIZE_8BIT:
accsize=1;
break;
case LIBSWD_MEMAP_CSW_SIZE_16BIT:
accsize=2;
break;
case LIBSWD_MEMAP_CSW_SIZE_32BIT:
accsize=4;
break;
default:
res=LIBSWD_ERROR_MEMAPACCSIZE;
goto libswd_memap_write_char_error;
}
if (count%accsize) count=count-(count%accsize);
// check for alignment issues.
if ((addr%accsize)!=0)
{
res=LIBSWD_ERROR_MEMAPALIGN;
goto libswd_memap_write_char_error;
}
// Mark start time for transfer speed measurement.
gettimeofday(&tstart, NULL);
// Perform word-by-word write operation from char array.
// Use write method that match the CSW AddrInc configuration.
if (!(libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_ADDRINC))
{
// Use manual TAR incrementation (slower).
for (i=0;i<count;i+=accsize)
{
int drw_shift;
loc=addr+i;
// Calculate the offset in DRW where the data should go
// see Data byte-laning in the ARM debug interface v5 documentation
// note: this only works for little endian systems.
drw_shift=8*(loc%4);
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_write_char() writing address 0x%08X (speed %fKB/s)\r",
loc, count/tdeltam );
fflush(0);
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_write_char_error;
libswdctx->log.memap.tar=loc;
// Implode and Write data to DRW register.
memcpy((void*)&libswdctx->log.memap.drw, data+i, accsize);
// apply the data byte-laning shift
libswdctx->log.memap.drw <<= drw_shift;
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &libswdctx->log.memap.drw);
if (res<0) goto libswd_memap_write_char_error;
}
}
else
{
// Use TAR Auto Increment (faster).
// TAR auto increment is only guaranteed to work on the bottom 10 bits
// of the TAR register. Above that it is implementation defined.
// We use 1024 byte chunks as it will work on every platform
// and one TAR write every 1024 bytes is not adding too much overhead.
const unsigned int BOUNDARY = 1024;
unsigned int i;
// Check if packed transfer, if so use word access.
if (libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_ADDRINC_PACKED) accsize=4;
for (loc = addr, i = 0; loc < (addr + count); loc += accsize, i+= accsize)
{
int drw_shift;
// Calculate the offset in DRW where the data should go
// see Data byte-laning in the ARM debug interface v5 documentation
// note: this only works for little endian systems.
drw_shift=8*(loc%4);
// only write the TAR register, if this is the first time through the loop.
// or if we've passed over the boundary where TAR auto inc isn't guaranteed
// to work anymore.
if (loc == addr ||
(loc % BOUNDARY) == 0)
{
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_write_char_error;
libswdctx->log.memap.tar=loc;
}
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_write_char() writing address 0x%08X (speed %fKB/s)\r",
loc, count/tdeltam );
fflush(0);
// apply the data byte-laning shift and write to the DRW register
memcpy((void*)&libswdctx->log.memap.drw, data + i, accsize);
libswdctx->log.memap.drw <<= drw_shift;
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &libswdctx->log.memap.drw);
if (res<0) goto libswd_memap_write_char_error;
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
return LIBSWD_OK;
libswd_memap_write_char_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"LIBSWD_E: libswd_memap_write_char(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic write using MEM-AP from char array, with prior CSW setup.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to write with MEM-AP.
* \param count is the number of bytes to write.
* \param *data is the pointer to data to be written.
* \param csw is the value of csw register to write prior data write.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_write_char_csw(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, char *data, int csw){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entring libswd_memap_write_char_csw(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, **data=%p, csw=0x%X)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data, csw );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int res=0, accsize=0;
// Calculate required access size based on CSW value.
switch (csw&LIBSWD_MEMAP_CSW_SIZE)
{
case LIBSWD_MEMAP_CSW_SIZE_8BIT:
accsize=1;
break;
case LIBSWD_MEMAP_CSW_SIZE_16BIT:
accsize=2;
break;
case LIBSWD_MEMAP_CSW_SIZE_32BIT:
accsize=4;
break;
default:
res=LIBSWD_ERROR_MEMAPACCSIZE;
goto libswd_memap_write_char_csw_error;
}
// Verify the count parameter to be access boundary.
if (count%accsize) count=count-(count%accsize);
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_write_char_csw_error;
}
// Setup MEM-AP CSW and TAR.
res=libswd_memap_setup(libswdctx, operation, csw, addr);
if (res<0) goto libswd_memap_write_char_csw_error;
res=libswd_memap_write_char(libswdctx, operation, addr, count, data);
if (res<0) goto libswd_memap_write_char_csw_error;
return LIBSWD_OK;
libswd_memap_write_char_csw_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"LIBSWD_E: libswd_memap_write_char_csw(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic write using MEM-AP from char array, using 32-bit access.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to write with MEM-AP.
* \param count is the number of bytes to write.
* \param *data is the pointer to data to be written.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_write_char_32(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, char *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_write_char_32(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, **data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data);
return libswd_memap_write_char_csw(libswdctx, operation, addr, count, data, LIBSWD_MEMAP_CSW_SIZE_32BIT|LIBSWD_MEMAP_CSW_ADDRINC_SINGLE);
}
/** Generic write using MEM-AP from int array.
* Data are stored into char array.
* Remember to setup CSW first for valid bus access!
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to write with MEM-AP.
* \param count is the number of words to write.
* \param *data is the pointer to int data array to be written.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_write_int(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, int *data){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_write_int(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, **data=%p)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void*)data);
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int i, loc, res=0;
float tdeltam;
struct timeval tstart, tstop;
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);
if (res<0) goto libswd_memap_write_int_error;
}
// Mark start time for transfer speed measurement.
gettimeofday(&tstart, NULL);
// Perform word-by-word write operation from int array.
if (!(libswdctx->log.memap.csw&LIBSWD_MEMAP_CSW_ADDRINC))
{
// Use manual TAR incrementation (slower).
for (i=0;i<count;i++)
{
loc=addr+i*4;
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_write_int() writing address 0x%08X (speed %fKB/s)\r",
loc, count*4/tdeltam );
fflush(0);
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_write_int_error;
libswdctx->log.memap.tar=loc;
// Implode and Write data to DRW register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, data+i);
if (res<0) goto libswd_memap_write_int_error;
libswdctx->log.memap.drw=data[i];
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
else
{
// Use TAR Auto Increment (faster).
// TAR auto increment is only guaranteed to work on the bottom 10 bits
// of the TAR register. Above that it is implementation defined.
// We use 1024 byte chunks as it will work on every platform
// and one TAR write every 1024 bytes is not adding too much overhead.
const unsigned int BOUNDARY = 1024;
unsigned int i;
for (loc = addr, i = 0; loc < (addr + (count * 4)); loc += 4, i++)
{
// only write the TAR register, if this is the first time through the loop.
// or if we've passed over the boundary where TAR auto inc isn't guaranteed
// to work anymore.
if (loc == addr ||
(loc % BOUNDARY) == 0)
{
// Pass address to TAR register.
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_TAR_ADDR, &loc);
if (res<0) goto libswd_memap_write_int_error;
libswdctx->log.memap.tar=loc;
}
// Measure transfer speed.
gettimeofday(&tstop, NULL);
tdeltam=fabsf((tstop.tv_sec-tstart.tv_sec)*1000+(tstop.tv_usec-tstart.tv_usec)/1000);
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO,
"LIBSWD_I: libswd_memap_write_int() writing address 0x%08X (speed %fKB/s)\r",
loc, count*4/tdeltam );
fflush(0);
// Write data to DRW register.
libswdctx->log.memap.drw=data[i];
res=libswd_ap_write(libswdctx, LIBSWD_OPERATION_EXECUTE, LIBSWD_MEMAP_DRW_ADDR, &libswdctx->log.memap.drw);
if (res<0) goto libswd_memap_write_int_error;
}
libswd_log(libswdctx, LIBSWD_LOGLEVEL_INFO, "\n");
}
return LIBSWD_OK;
libswd_memap_write_int_error:
libswd_log(libswdctx, LIBSWD_LOGLEVEL_ERROR,
"\nLIBSWD_E: libswd_memap_write_int(): %s\n",
libswd_error_string(res) );
return res;
}
/** Generic write using MEM-AP from int array, with prior CSW MEM-AP setup.
* \param *libswdctx swd context to work on.
* \param operation can be LIBSWD_OPERATION_ENQUEUE or LIBSWD_OPERATION_EXECUTE.
* \param addr is the start address of the data to write with MEM-AP.
* \param count is the number of words to write.
* \param *data is the pointer to int data array to be written.
* \param csw is the value of csw register to write prior data write.
* \return number of elements/words processed or LIBSWD_ERROR code on failure.
*/
int libswd_memap_write_int_csw(libswd_ctx_t *libswdctx, libswd_operation_t operation, int addr, int count, int *data, int csw){
libswd_log(libswdctx, LIBSWD_LOGLEVEL_DEBUG,
"LIBSWD_D: Entering libswd_memap_write_int_csw(*libswdctx=%p, operation=%s, addr=0x%08X, count=0x%08X, **data=%p, csw=0x%X)...\n",
(void*)libswdctx, libswd_operation_string(operation),
addr, count, (void**)data, csw );
if (libswdctx==NULL) return LIBSWD_ERROR_NULLCONTEXT;
if (operation!=LIBSWD_OPERATION_ENQUEUE && operation!=LIBSWD_OPERATION_EXECUTE)
return LIBSWD_ERROR_BADOPCODE;
int res=0;
// Calculate required access size based on CSW value.
switch (csw&LIBSWD_MEMAP_CSW_SIZE)
{
case LIBSWD_MEMAP_CSW_SIZE_8BIT:
case LIBSWD_MEMAP_CSW_SIZE_16BIT:
case LIBSWD_MEMAP_CSW_SIZE_32BIT:
break;
default:
res=LIBSWD_ERROR_MEMAPACCSIZE;
goto libswd_memap_write_int_csw_error;
}
// Initialize MEM-AP if necessary.
if (!libswdctx->log.memap.initialized)
{
res=libswd_memap_init(libswdctx, operation);