Moved out target-related V2 protocol commands into a seperate file for the AVRISP...
[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 /** Table of masks for SPI_Init() from a given PARAM_SCK_DURATION value */
42 static const uint8_t SPIMaskFromSCKDuration[] =
43 {
44 #if (F_CPU == 8000000)
45 SPI_SPEED_FCPU_DIV_2,
46 #endif
47 SPI_SPEED_FCPU_DIV_2, SPI_SPEED_FCPU_DIV_4, SPI_SPEED_FCPU_DIV_8,
48 SPI_SPEED_FCPU_DIV_16, SPI_SPEED_FCPU_DIV_32, SPI_SPEED_FCPU_DIV_64
49 #if (F_CPU == 16000000)
50 , SPI_SPEED_FCPU_DIV_128
51 #endif
52 };
53
54
55 uint8_t V2Protocol_GetSPIPrescalerMask(void)
56 {
57 uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);
58
59 if (SCKDuration >= sizeof(SPIMaskFromSCKDuration))
60 SCKDuration = (sizeof(SPIMaskFromSCKDuration) - 1);
61
62 return SPIMaskFromSCKDuration[SCKDuration];
63 }
64
65 void V2Protocol_ChangeTargetResetLine(bool ResetTarget)
66 {
67 if (ResetTarget)
68 {
69 RESET_LINE_DDR |= RESET_LINE_MASK;
70
71 if (!(V2Params_GetParameterValue(PARAM_RESET_POLARITY)))
72 RESET_LINE_PORT |= RESET_LINE_MASK;
73 }
74 else
75 {
76 RESET_LINE_PORT &= ~RESET_LINE_MASK;
77 RESET_LINE_DDR &= ~RESET_LINE_MASK;
78 }
79 }
80
81 void V2Protocol_DelayMS(uint8_t MS)
82 {
83 while (MS--)
84 _delay_ms(1);
85 }
86
87 uint8_t V2Protocol_WaitWhileTargetBusy(void)
88 {
89 uint8_t TimeoutMS = TARGET_BUSY_TIMEOUT_MS;
90 uint8_t ResponseByte;
91
92 do
93 {
94 V2Protocol_DelayMS(1);
95
96 SPI_SendByte(0xF0);
97 SPI_SendByte(0x00);
98
99 SPI_SendByte(0x00);
100 ResponseByte = SPI_ReceiveByte();
101 }
102 while ((ResponseByte & 0x01) && (TimeoutMS--));
103
104 if (!(TimeoutMS))
105 return STATUS_CMD_TOUT;
106 else
107 return STATUS_CMD_OK;
108 }
109
110 void V2Protocol_LoadExtendedAddress(void)
111 {
112 SPI_SendByte(0x4D);
113 SPI_SendByte(0x00);
114 SPI_SendByte((CurrentAddress & 0x00FF0000) >> 16);
115 SPI_SendByte(0x00);
116 }