Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ISPTarget.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Header file for ISPTarget.c.
  29. */
  30. #ifndef _ISP_TARGET_
  31. #define _ISP_TARGET_
  32. /* Includes: */
  33. #include <avr/io.h>
  34. #include <avr/pgmspace.h>
  35. #include <util/delay.h>
  36. #include <LUFA/Drivers/USB/USB.h>
  37. #include <LUFA/Drivers/Peripheral/SPI.h>
  38. #include "../V2Protocol.h"
  39. #include "ISPProtocol.h"
  40. #include "Config/AppConfig.h"
  41. /* Preprocessor Checks: */
  42. #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
  43. #undef ENABLE_ISP_PROTOCOL
  44. #if !defined(ENABLE_XPROG_PROTOCOL)
  45. #define ENABLE_XPROG_PROTOCOL
  46. #endif
  47. #endif
  48. /* Macros: */
  49. /** Low level device command to issue an extended FLASH address, for devices with over 128KB of FLASH. */
  50. #define LOAD_EXTENDED_ADDRESS_CMD 0x4D
  51. /** Macro to convert an ISP frequency to a number of timer clock cycles for the software SPI driver. */
  52. #define TIMER_COMP(freq) (((F_CPU / 8) / 2 / freq) - 1)
  53. /** ISP rescue clock speed in Hz, for clocking targets with incorrectly set fuses. */
  54. #define ISP_RESCUE_CLOCK_SPEED 4000000
  55. /* External Variables: */
  56. extern bool HardwareSPIMode;
  57. /* Function Prototypes: */
  58. void ISPTarget_EnableTargetISP(void);
  59. void ISPTarget_DisableTargetISP(void);
  60. void ISPTarget_ConfigureRescueClock(void);
  61. void ISPTarget_ConfigureSoftwareSPI(const uint8_t SCKDuration);
  62. uint8_t ISPTarget_TransferSoftSPIByte(const uint8_t Byte);
  63. void ISPTarget_ChangeTargetResetLine(const bool ResetTarget);
  64. uint8_t ISPTarget_WaitWhileTargetBusy(void);
  65. void ISPTarget_LoadExtendedAddress(void);
  66. uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode,
  67. const uint16_t PollAddress,
  68. const uint8_t PollValue,
  69. const uint8_t DelayMS,
  70. const uint8_t ReadMemCommand);
  71. /* Inline Functions: */
  72. /** Sends a byte of ISP data to the attached target, using the appropriate SPI hardware or
  73. * software routines depending on the selected ISP speed.
  74. *
  75. * \param[in] Byte Byte of data to send to the attached target
  76. */
  77. static inline void ISPTarget_SendByte(const uint8_t Byte)
  78. {
  79. if (HardwareSPIMode)
  80. SPI_SendByte(Byte);
  81. else
  82. ISPTarget_TransferSoftSPIByte(Byte);
  83. }
  84. /** Receives a byte of ISP data from the attached target, using the appropriate
  85. * SPI hardware or software routines depending on the selected ISP speed.
  86. *
  87. * \return Received byte of data from the attached target
  88. */
  89. static inline uint8_t ISPTarget_ReceiveByte(void)
  90. {
  91. uint8_t ReceivedByte;
  92. if (HardwareSPIMode)
  93. ReceivedByte = SPI_ReceiveByte();
  94. else
  95. ReceivedByte = ISPTarget_TransferSoftSPIByte(0x00);
  96. #if defined(INVERTED_ISP_MISO)
  97. return ~ReceivedByte;
  98. #else
  99. return ReceivedByte;
  100. #endif
  101. }
  102. /** Sends and receives a byte of ISP data to and from the attached target, using the
  103. * appropriate SPI hardware or software routines depending on the selected ISP speed.
  104. *
  105. * \param[in] Byte Byte of data to send to the attached target
  106. *
  107. * \return Received byte of data from the attached target
  108. */
  109. static inline uint8_t ISPTarget_TransferByte(const uint8_t Byte)
  110. {
  111. uint8_t ReceivedByte;
  112. if (HardwareSPIMode)
  113. ReceivedByte = SPI_TransferByte(Byte);
  114. else
  115. ReceivedByte = ISPTarget_TransferSoftSPIByte(Byte);
  116. #if defined(INVERTED_ISP_MISO)
  117. return ~ReceivedByte;
  118. #else
  119. return ReceivedByte;
  120. #endif
  121. }
  122. #endif