-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWD_PacketLayer.c
644 lines (571 loc) · 19.5 KB
/
SWD_PacketLayer.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
/******************************************************************************
* File Name: SWD_PacketLayer.c
* Version 1.0
*
* Description:
* This file provides the source code for the packet layer functions of the
* SWD protocol. This includes SWD Read packet, SWD Write packet.
*
* Owner:
* Tushar Rastogi, Application Engineer (tusr@cypress.com)
*
* Related Document:
* AN84858 - PSoC 4 and PSoC Analog Coprocessor Programming using an External Microcontroller (HSSP)
*
* Hardware Dependency:
* CY8CKIT - 040 / CY8CKIT - 042 / CY8CKIT - 044 PSoC 4 M / CY8CKIT - 042 BLE /
* CY8CKIT - 046 / CY8CKIT - 048 / CY8CKIT - 41-40xx
*
* Code Tested With:
* PSoC Creator 4.0
* ARM GCC 4.9-2015-q1-update
*
* Note:
* The functions in SWD packet layer use the bit banging macros, functions
* in "SWD_PhysicalLayer.h"
*
******************************************************************************
* Copyright (c) 2013-2016, Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
******************************************************************************/
/******************************************************************************
* Header file Inclusion
******************************************************************************/
#include "SWD_PacketLayer.h"
/* "SWD_PhysicalLayer.h" file contains the bit banging routines for
programming */
#include "SWD_PhysicalLayer.h"
/******************************************************************************
* Global Variable definitions
******************************************************************************/
/* Stores the 8-bit SWD Packet header */
unsigned char swd_PacketHeader;
/* Stores the 3-bit SWD Packet ACK data */
unsigned char swd_PacketAck;
/* 4-byte Read, Write packet data */
unsigned char swd_PacketData[DATA_BYTES_PER_PACKET];
/******************************************************************************
* Function Definitions
******************************************************************************/
/******************************************************************************
* Function Name: Swd_SendByte
*******************************************************************************
*
* Summary:
* Sends a byte of data on the SWD lines (SWDIO, SWDCK)
*
* Parameters:
* txByte: Data byte to be sent by host on SWDIO line (Least significant bit
* sent first)
*
* Return:
* None.
*
* Note:
* This function is called for sending SWD header data in SWD Read/Write
* packets, and also to send data in a SWD Write packet.
*
******************************************************************************/
static void Swd_SendByte(unsigned char txByte)
{
unsigned char loop = 0;
/* Loop for 8-bits of a byte */
for(loop = 0; loop < 8; loop++)
{
if((txByte & LSB_BIT_MASK)==0x01) /* Send a '1' */
{
SetSwdckLow();
SetSwdioHigh();
SetSwdckHigh();
}
else /* Send a '0' */
{
SetSwdckLow();
SetSwdioLow();
SetSwdckHigh();
}
txByte = txByte >> 1; /* Make the next bit to send as LS bit */
}
}
/******************************************************************************
* Function Name: Swd_ReceiveByte
*******************************************************************************
*
* Summary:
* Receives a byte of data on the SWD lines (SWDIO, SWDCK)
*
* Parameters:
* None.
*
* Return:
* rxByte: Data byte received by host on SWDIO line (Least significant bit is
* first received bit)
*
* Note:
* This function is called for reading data bytes in a SWD Read Packet
*
******************************************************************************/
static unsigned char Swd_ReceiveByte()
{
unsigned char loop = 0;
unsigned char rxBit = 0;
unsigned char rxByte = 0;
/* Loop for 8-bits of a byte */
for(loop = 0; loop < 8; loop++)
{
SWDCK_OUTPUT_LOW;
/* Read the SWDIO input line */
rxBit = ReadSwdio();
SWDCK_OUTPUT_HIGH;
rxByte = rxByte >> 1;
if(rxBit) /* Received a '1' */
{
rxByte = rxByte | MSB_BIT_MASK;
}
else /* Received a '0' */
{
rxByte = rxByte & (~MSB_BIT_MASK);
}
}
return(rxByte);
}
/******************************************************************************
* Function Name: Swd_FirstTurnAroundPhase
*******************************************************************************
*
* Summary:
* Generates one SWDCK clock cycle for Turnaround phase of a SWD packet.
* SWDIO drive mode is changed to High-Z drive mode so that the host can read
* the data on the SWDIO.
*
* Parameters:
* None.
*
* Return:
* None.
*
* Note:
* This function is called during SWD Read/Write packets
*
******************************************************************************/
static void Swd_FirstTurnAroundPhase()
{
/* Change to High-Z drive mode for host to read the SWDIO line */
SetSwdioHizInput();
SetSwdckLow();
SetSwdckHigh();
}
/******************************************************************************
* Function Name: Swd_SecondTurnAroundPhase
*******************************************************************************
*
* Summary:
* Generates one SWDCK clock cycle for Turnaround phase of a SWD packet.
* SWDIO drive mode is changed to CMOS output drive mode so that the host can
* write data on SWDIO.
*
* Parameters:
* None.
*
* Return:
* None.
*
* Note:
* This function is called during SWD Read/Write packets
*
******************************************************************************/
static void Swd_SecondTurnAroundPhase()
{
/* Change to CMOS output drive mode for host to write to SWDIO */
SetSwdioCmosOutput();
SetSwdckLow();
SetSwdckHigh();
}
/******************************************************************************
* Function Name: Swd_GetAckSegment
********************************************************************************
*
* Summary:
* Gets the 3-bit ACK response in a SWD packet
*
* Parameters:
* None.
*
* Return:
* ack:
* 3-bit ACK response is returned as a byte. Three possible return values are:
* 0x01 - SWD_OK_ACK
* 0x02 - SWD_WAIT_ACK
* 0x04 - SWD_FAULT_ACK
* Any other return value - Undefined ACK code.
* Treat it similar to SWD_FAULT_ACK and abort operation.
*
* Note:
* This function is called during SWD Read/Write packets
*
******************************************************************************/
static unsigned char Swd_GetAckSegment()
{
unsigned char ack = 0;
unsigned char receiveBit = 0;
unsigned char loop = 0;
/* ACK bits are received lsb bit first */
for(loop = 0; loop < NUMBER_OF_ACK_BITS; loop++)
{
SetSwdckLow();
/* Store the ACK bit received */
receiveBit = ReadSwdio();
SetSwdckHigh();
/* Concatenate the ACK bit with ACK data byte */
ack = ack | (receiveBit << loop);
}
return(ack);
}
/******************************************************************************
* Function Name: Swd_SendParity
*******************************************************************************
*
* Summary:
* Sends the 1-bit parity data on SWD lines
*
* Parameters:
* parity:
* 1-bit parity data received as a byte (either '1' or '0')
*
* Return:
* None.
*
* Note:
* This function is called during SWD Write packet
*
******************************************************************************/
static void Swd_SendParity(unsigned char parity)
{
/* Make the clock low, Send SWDIO data, Make Clock high */
SetSwdckLow();
if(parity)
{
SetSwdioHigh();
}
else
{
SetSwdioLow();
}
SetSwdckHigh();
}
/******************************************************************************
* Function Name: Swd_ReceiveParity
*******************************************************************************
*
* Summary:
* Receives the 1-bit parity data on SWD lines
*
* Parameters:
* None.
*
* Return:
* parity:
* 1-bit parity data returned as a byte (either '1' or '0')
*
* Note:
* This function is called during SWD Read packet
*
******************************************************************************/
static unsigned char Swd_ReceiveParity()
{
unsigned char parity;
/* Make the clock low, Read SWDIO data, Make Clock high */
SetSwdckLow();
parity = ReadSwdio();
SetSwdckHigh();
return(parity);
}
/******************************************************************************
* Function Name: Swd_SendDummyClocks
*******************************************************************************
*
* Summary:
* Sends three SWDCK clock cycles with SWDIO line held low
*
* Parameters:
* None.
*
* Return:
* None.
*
* Note:
* This function is called during SWD Read/Write packets.
*
******************************************************************************/
static void Swd_SendDummyClocks()
{
unsigned char loop;
/* Send three SWDCK clock cycles with SWDIO line held low */
SetSwdioLow();
for(loop = 0; loop < NUMBER_OF_DUMMY_SWD_CLOCK_CYCLES; loop++)
{
SetSwdckLow();
SetSwdckHigh();
}
}
/******************************************************************************
* Function Name: Swd_CountOneBits
*******************************************************************************
*
* Summary:
* Counts the number of 1's in a byte
*
* Parameters:
* dataByte:
* Byte for which the number of 1's need to be found
*
* Return:
* count:
* Number of 1's present in DataByte
*
* Note:
* This function is used for computing parity of data
*
******************************************************************************/
static unsigned char Swd_CountOneBits(unsigned char dataByte)
{
unsigned char count = 0;
unsigned char i;
/* Loop for 8-bits of a byte */
/* Increment count variable if LS bit is set */
for(i = 0; i < 8; i++)
{
if(dataByte & LSB_BIT_MASK)
{
count++;
}
dataByte = dataByte >> 1;
}
return(count);
}
/******************************************************************************
* Function Name: Swd_ComputeDataParity
*******************************************************************************
*
* Summary:
* Computes Even parity of 4-byte data
*
* Parameters:
* swd_PacketData[DATA_BYTES_PER_PACKET] - Global variable that holds the data
*
* Return:
* Even parity bit
* 0x01 - Parity is '1' if 4-byte data has an odd number of 1's
* 0x00 - Parity is '0' if 4-byte data has an even number of 1's
*
* Note:
* This function is called during SWD Read/Write packets
*
******************************************************************************/
static unsigned char Swd_ComputeDataParity()
{
unsigned char count = 0;
unsigned char i;
/* Count number of 1's in 4-byte data */
for(i = 0; i < DATA_BYTES_PER_PACKET; i++)
{
count = count + Swd_CountOneBits(swd_PacketData[i]);
}
/* Return even parity. If Lsb bit is 1, it implies the number of 1's is odd,
and hence the even parity bit is set. For even number of 1's, parity bit
is 0 */
return(count & LSB_BIT_MASK);
}
/******************************************************************************
* Function Name: Swd_ReadPacket
*******************************************************************************
*
* Summary:
* Sends a single SWD Read packet
*
* Parameters:
* swd_PacketHeader - Global variable that holds the 8-bit header data for packet
*
* Return:
* swd_PacketData[DATA_BYTES_PER_PACKET] - Global variable that holds the 4-byte
* data that has been read
*
* swd_PacketAck - Global variable that holds the status of the SWD transaction
* ACK response is stored as a byte. Three possible ACK return values are:
* 0x01 - SWD_OK_ACK
* 0x02 - SWD_WAIT_ACK
* 0x04 - SWD_FAULT_ACK
* 0x09 - SWD_OK_ACK, but Parity error in data received
* Any other ACK value - Undefined ACK code.
* Treat it similar to SWD_FAULT_ACK and abort
* operation.
*
* Note:
* 1.)Swd_RawReadPacket() is called during SPC Polling operations when the host
* must read the SPC status register continuously till a Programming operation
* is completed or data is ready to be read.
*
* 2.)This function is called duing continuous multi byte read operations from
* the SPC_DATA register
*
* 3.)This function is called during IDCODE instruction for reading the Device
* JTAG ID
*
* 4.)This function is not called during normal SWD read transactions. To read
* a register data, two SWD Read packets must be sent (call this function
* twice). This method of reading twice has been implemented in
* SWD_ReadPacket() function which will be called during normal read
* transactions.
*
******************************************************************************/
void Swd_ReadPacket()
{
unsigned char parity;
unsigned char loop = 0;
unsigned char i;
do
{
/* 8-bit Header data */
Swd_SendByte(swd_PacketHeader);
/* First Turnaround phase */
Swd_FirstTurnAroundPhase();
/* Get the 3-bit ACK data */
swd_PacketAck = Swd_GetAckSegment();
/* Read 4-byte data and store in Global array swd_PacketData[] */
for(i = 0; i < DATA_BYTES_PER_PACKET; i++)
{
swd_PacketData[i] = Swd_ReceiveByte();
}
/* Parity phase */
parity = Swd_ReceiveParity();
/* Second Turnaround phase */
Swd_SecondTurnAroundPhase();
/* Dummy clock phase since clock is not free running */
Swd_SendDummyClocks();
/* Repeat for a WAIT ACK till timeout loop expires */
loop++;
}while((swd_PacketAck == SWD_WAIT_ACK ) && (loop < NUMBER_OF_WAIT_ACK_LOOPS));
/* For a OK ACK, check the parity bit received with parity computed */
if(swd_PacketAck == SWD_OK_ACK)
{
if(parity != Swd_ComputeDataParity())
{
/* Set the Parity error bit in ACK code */
swd_PacketAck = swd_PacketAck | SWD_PARITY_ERROR;
}
}
/* Swd_PacketAck global variable holds the status of the SWD transaction */
}
/******************************************************************************
* Function Name: Swd_WritePacket
*******************************************************************************
*
* Summary:
* Sends a single SWD Write packet
*
* Parameters:
* swd_PacketHeader - Global variable that holds the 8-bit header data for packet
* swd_PacketData[DATA_BYTES_PER_PACKET] - Global variable that holds the data
* to be sent
*
* Return:
* swd_PacketAck - Global variable that holds the status of the SWD transaction
* ACK response is stored as a byte. Three possible ACK return values are:
* 0x01 - SWD_OK_ACK
* 0x02 - SWD_WAIT_ACK
* 0x04 - SWD_FAULT_ACK
* Any other ACK value - Undefined ACK code.
* Treat it similar to SWD_FAULT_ACK and abort operation.
*
* Note:
* This function is called for Address Write, Data Write operations
*
******************************************************************************/
void Swd_WritePacket()
{
unsigned char parity;
unsigned char loop = 0;
unsigned char i;
/* Compute Even parity for 4-byte data */
parity = Swd_ComputeDataParity();
do
{
/* 8-bit Header data */
Swd_SendByte(swd_PacketHeader);
/* First Turnaround phase */
Swd_FirstTurnAroundPhase();
/* Get the 3-bit ACK data */
swd_PacketAck = Swd_GetAckSegment();
/* Second Turnaround phase */
Swd_SecondTurnAroundPhase();
/* Send 4-byte data stored in Global array swd_PacketData[] */
for(i = 0; i < DATA_BYTES_PER_PACKET; i++)
{
Swd_SendByte(swd_PacketData[i]);
}
/* Parity phase */
Swd_SendParity(parity);
/* Dummy clock phase since clock is not free running */
Swd_SendDummyClocks();
loop++;
/* Repeat for a WAIT ACK till timeout loop expires */
}while((swd_PacketAck == SWD_WAIT_ACK ) && (loop < NUMBER_OF_WAIT_ACK_LOOPS));
/* swd_PacketAck global variable holds the status of the SWD transaction */
}
/******************************************************************************
* Function Name: Swd_LineReset
*******************************************************************************
*
* Summary:
* Resets the SWD line by sending 51 SWDCK clock cycles with SWDIO line high
*
* Parameters:
* None
*
* Return:
* None
*
* Note:
* This function is called as part of SWD switching sequence
*
******************************************************************************/
void Swd_LineReset(void)
{
unsigned char i;
/* To reset SWD line, clock more than 50 SWDCK clock cycles with SWDIO
high. */
SWDIO_OUTPUT_HIGH;
for(i = 0; i < NUMBER_OF_SWD_RESET_CLOCK_CYCLES; i++)
{
SWDCK_OUTPUT_LOW;
SWDCK_OUTPUT_HIGH;
}
SWDCK_OUTPUT_LOW;
SWDIO_OUTPUT_LOW;
SWDCK_OUTPUT_HIGH;
}
/* []END OF FILE */