]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-git/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[qmk_firmware.git] / protocol / lufa / LUFA-git / Projects / AVRISP-MKII / Lib / ISP / ISPTarget.h
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all 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 disclaims 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  *  Header file for ISPTarget.c.
34  */
35
36 #ifndef _ISP_TARGET_
37 #define _ISP_TARGET_
38
39         /* Includes: */
40                 #include <avr/io.h>
41                 #include <avr/pgmspace.h>
42                 #include <util/delay.h>
43
44                 #include <LUFA/Drivers/USB/USB.h>
45                 #include <LUFA/Drivers/Peripheral/SPI.h>
46
47                 #include "../V2Protocol.h"
48                 #include "ISPProtocol.h"
49                 #include "Config/AppConfig.h"
50
51         /* Preprocessor Checks: */
52                 #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
53                         #undef ENABLE_ISP_PROTOCOL
54
55                         #if !defined(ENABLE_XPROG_PROTOCOL)
56                                 #define ENABLE_XPROG_PROTOCOL
57                         #endif
58                 #endif
59
60         /* Macros: */
61                 /** Low level device command to issue an extended FLASH address, for devices with over 128KB of FLASH. */
62                 #define LOAD_EXTENDED_ADDRESS_CMD     0x4D
63
64                 /** Macro to convert an ISP frequency to a number of timer clock cycles for the software SPI driver. */
65                 #define TIMER_COMP(freq)              (((F_CPU / 8) / 2 / freq) - 1)
66
67                 /** ISP rescue clock speed in Hz, for clocking targets with incorrectly set fuses. */
68                 #define ISP_RESCUE_CLOCK_SPEED        4000000
69
70         /* External Variables: */
71                 extern bool HardwareSPIMode;
72
73         /* Function Prototypes: */
74                 void    ISPTarget_EnableTargetISP(void);
75                 void    ISPTarget_DisableTargetISP(void);
76                 void    ISPTarget_ConfigureRescueClock(void);
77                 void    ISPTarget_ConfigureSoftwareSPI(const uint8_t SCKDuration);
78                 uint8_t ISPTarget_TransferSoftSPIByte(const uint8_t Byte);
79                 void    ISPTarget_ChangeTargetResetLine(const bool ResetTarget);
80                 uint8_t ISPTarget_WaitWhileTargetBusy(void);
81                 void    ISPTarget_LoadExtendedAddress(void);
82                 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode,
83                                                       const uint16_t PollAddress,
84                                                       const uint8_t PollValue,
85                                                       const uint8_t DelayMS,
86                                                       const uint8_t ReadMemCommand);
87
88         /* Inline Functions: */
89                 /** Sends a byte of ISP data to the attached target, using the appropriate SPI hardware or
90                  *  software routines depending on the selected ISP speed.
91                  *
92                  *  \param[in] Byte  Byte of data to send to the attached target
93                  */
94                 static inline void ISPTarget_SendByte(const uint8_t Byte)
95                 {
96                         if (HardwareSPIMode)
97                           SPI_SendByte(Byte);
98                         else
99                           ISPTarget_TransferSoftSPIByte(Byte);
100                 }
101
102                 /** Receives a byte of ISP data from the attached target, using the appropriate
103                  *  SPI hardware or software routines depending on the selected ISP speed.
104                  *
105                  *  \return Received byte of data from the attached target
106                  */
107                 static inline uint8_t ISPTarget_ReceiveByte(void)
108                 {
109                         uint8_t ReceivedByte;
110
111                         if (HardwareSPIMode)
112                           ReceivedByte = SPI_ReceiveByte();
113                         else
114                           ReceivedByte = ISPTarget_TransferSoftSPIByte(0x00);
115
116                         #if defined(INVERTED_ISP_MISO)
117                         return ~ReceivedByte;
118                         #else
119                         return  ReceivedByte;
120                         #endif
121                 }
122
123                 /** Sends and receives a byte of ISP data to and from the attached target, using the
124                  *  appropriate SPI hardware or software routines depending on the selected ISP speed.
125                  *
126                  *  \param[in] Byte  Byte of data to send to the attached target
127                  *
128                  *  \return Received byte of data from the attached target
129                  */
130                 static inline uint8_t ISPTarget_TransferByte(const uint8_t Byte)
131                 {
132                         uint8_t ReceivedByte;
133
134                         if (HardwareSPIMode)
135                           ReceivedByte = SPI_TransferByte(Byte);
136                         else
137                           ReceivedByte = ISPTarget_TransferSoftSPIByte(Byte);
138
139                         #if defined(INVERTED_ISP_MISO)
140                         return ~ReceivedByte;
141                         #else
142                         return  ReceivedByte;
143                         #endif
144                 }
145
146 #endif
147