Switch to hardware delays and timeouts via a hardware timer in the V2 Protocol handler.
[pub/USBasp.git] / Projects / Incomplete / AVRISP / Lib / V2ProtocolTarget.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Target-related functions for the V2 Protocol decoder.
34 */
35
36 #include "V2ProtocolTarget.h"
37
38 /** Current memory address for FLASH/EEPROM memory read/write commands */
39 uint32_t CurrentAddress;
40
41 uint8_t V2Protocol_GetSPIPrescalerMask(void)
42 {
43 static const uint8_t SPIMaskFromSCKDuration[] =
44 {
45 #if (F_CPU == 8000000)
46 SPI_SPEED_FCPU_DIV_2,
47 #endif
48 SPI_SPEED_FCPU_DIV_2, SPI_SPEED_FCPU_DIV_4, SPI_SPEED_FCPU_DIV_8,
49 SPI_SPEED_FCPU_DIV_16, SPI_SPEED_FCPU_DIV_32, SPI_SPEED_FCPU_DIV_64
50 #if (F_CPU == 16000000)
51 , SPI_SPEED_FCPU_DIV_128
52 #endif
53 };
54
55 uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);
56
57 if (SCKDuration >= sizeof(SPIMaskFromSCKDuration))
58 SCKDuration = (sizeof(SPIMaskFromSCKDuration) - 1);
59
60 return SPIMaskFromSCKDuration[SCKDuration];
61 }
62
63 void V2Protocol_ChangeTargetResetLine(bool ResetTarget)
64 {
65 if (ResetTarget)
66 {
67 RESET_LINE_DDR |= RESET_LINE_MASK;
68
69 if (!(V2Params_GetParameterValue(PARAM_RESET_POLARITY)))
70 RESET_LINE_PORT |= RESET_LINE_MASK;
71 }
72 else
73 {
74 RESET_LINE_PORT &= ~RESET_LINE_MASK;
75 RESET_LINE_DDR &= ~RESET_LINE_MASK;
76 }
77 }
78
79 void V2Protocol_DelayMS(uint8_t MS)
80 {
81 TCNT0 = 0;
82 while (TCNT0 < MS);
83 }
84
85 uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAddress, uint8_t PollValue,
86 uint8_t DelayMS, bool IsFlashMemory, uint8_t ReadMemCommand)
87 {
88 uint8_t ProgrammingStatus = STATUS_CMD_OK;
89
90 /* Determine method of Programming Complete check */
91 switch (ProgrammingMode & ~(PROG_MODE_PAGED_WRITES_MASK | PROG_MODE_COMMIT_PAGE_MASK))
92 {
93 case PROG_MODE_WORD_TIMEDELAY_MASK:
94 case PROG_MODE_PAGED_TIMEDELAY_MASK:
95 V2Protocol_DelayMS(DelayMS);
96 break;
97 case PROG_MODE_WORD_VALUE_MASK:
98 case PROG_MODE_PAGED_VALUE_MASK:
99 if (IsFlashMemory && (PollAddress & 0x01))
100 {
101 ReadMemCommand |= READ_WRITE_ODD_BYTE_MASK;
102 PollAddress >>= 1;
103 }
104
105 TCNT0 = 0;
106
107 do
108 {
109 SPI_SendByte(ReadMemCommand);
110 SPI_SendByte(PollAddress >> 8);
111 SPI_SendByte(PollAddress & 0xFF);
112 }
113 while ((SPI_TransferByte(0x00) != PollValue) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS));
114
115 if (TCNT0 >= TARGET_BUSY_TIMEOUT_MS)
116 ProgrammingStatus = STATUS_RDY_BSY_TOUT;
117
118 break;
119 case PROG_MODE_WORD_READYBUSY_MASK:
120 case PROG_MODE_PAGED_READYBUSY_MASK:
121 ProgrammingStatus = V2Protocol_WaitWhileTargetBusy();
122 }
123
124 return ProgrammingStatus;
125 }
126
127 uint8_t V2Protocol_WaitWhileTargetBusy(void)
128 {
129 uint8_t ResponseByte;
130
131 TCNT0 = 0;
132
133 do
134 {
135 SPI_SendByte(0xF0);
136 SPI_SendByte(0x00);
137
138 SPI_SendByte(0x00);
139 ResponseByte = SPI_ReceiveByte();
140 }
141 while ((ResponseByte & 0x01) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS));
142
143 if (TCNT0 >= TARGET_BUSY_TIMEOUT_MS)
144 return STATUS_RDY_BSY_TOUT;
145 else
146 return STATUS_CMD_OK;
147 }
148
149 void V2Protocol_LoadExtendedAddress(void)
150 {
151 SPI_SendByte(0x4D);
152 SPI_SendByte(0x00);
153 SPI_SendByte((CurrentAddress & 0x00FF0000) >> 16);
154 SPI_SendByte(0x00);
155 }