AVRISP-MKII.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 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. * Main source file for the AVRISP project. This file contains the main tasks of
  29. * the project and is responsible for the initial application hardware configuration.
  30. */
  31. #include "AVRISP-MKII.h"
  32. #if (BOARD != BOARD_NONE)
  33. /* Some board hardware definitions (e.g. the Arduino Micro) have their LEDs defined on the same pins
  34. as the ISP, PDI or TPI interfaces (see the accompanying project documentation). If a board other
  35. than NONE is selected (to enable the LED driver with the programmer) you should double-check that
  36. no conflicts will occur. If there is a conflict, turn off the LEDs (set BOARD to NONE in the makefile)
  37. or define a custom board driver (see the LUFA manual) with alternative LED mappings.
  38. */
  39. #warning Board specific drivers have been selected; make sure the board LED driver does not conflict with the programmer ISP/PDI/TPI interfaces.
  40. #endif
  41. /** Main program entry point. This routine contains the overall program flow, including initial
  42. * setup of all components and the main program loop.
  43. */
  44. int main(void)
  45. {
  46. SetupHardware();
  47. V2Protocol_Init();
  48. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  49. GlobalInterruptEnable();
  50. for (;;)
  51. {
  52. #if (BOARD == BOARD_USBTINYMKII)
  53. /* On the USBTINY-MKII target, there is a secondary LED which indicates the current selected power
  54. mode - either VBUS, or sourced from the VTARGET pin of the programming connectors */
  55. LEDs_ChangeLEDs(LEDMASK_VBUSPOWER, (PIND & (1 << 0)) ? 0 : LEDMASK_VBUSPOWER);
  56. #endif
  57. AVRISP_Task();
  58. USB_USBTask();
  59. }
  60. }
  61. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  62. void SetupHardware(void)
  63. {
  64. #if (ARCH == ARCH_AVR8)
  65. /* Disable watchdog if enabled by bootloader/fuses */
  66. MCUSR &= ~(1 << WDRF);
  67. wdt_disable();
  68. /* Disable clock division */
  69. clock_prescale_set(clock_div_1);
  70. #endif
  71. /* Hardware Initialization */
  72. LEDs_Init();
  73. #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
  74. UpdateCurrentCompatibilityMode();
  75. #endif
  76. /* USB Stack Initialization */
  77. USB_Init();
  78. }
  79. /** Event handler for the library USB Connection event. */
  80. void EVENT_USB_Device_Connect(void)
  81. {
  82. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  83. }
  84. /** Event handler for the library USB Disconnection event. */
  85. void EVENT_USB_Device_Disconnect(void)
  86. {
  87. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  88. }
  89. /** Event handler for the library USB Configuration Changed event. */
  90. void EVENT_USB_Device_ConfigurationChanged(void)
  91. {
  92. bool ConfigSuccess = true;
  93. /* Setup AVRISP Data OUT endpoint */
  94. ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
  95. /* Setup AVRISP Data IN endpoint if it is using a physically different endpoint */
  96. if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK))
  97. ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
  98. /* Indicate endpoint configuration success or failure */
  99. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  100. }
  101. /** Processes incoming V2 Protocol commands from the host, returning a response when required. */
  102. void AVRISP_Task(void)
  103. {
  104. /* Device must be connected and configured for the task to run */
  105. if (USB_DeviceState != DEVICE_STATE_Configured)
  106. return;
  107. V2Params_UpdateParamValues();
  108. Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
  109. /* Check to see if a V2 Protocol command has been received */
  110. if (Endpoint_IsOUTReceived())
  111. {
  112. LEDs_SetAllLEDs(LEDMASK_BUSY);
  113. /* Pass off processing of the V2 Protocol command to the V2 Protocol handler */
  114. V2Protocol_ProcessCommand();
  115. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  116. }
  117. }
  118. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  119. * documentation) by the application code so that the address and size of a requested descriptor can be given
  120. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  121. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  122. * USB host.
  123. *
  124. * \param[in] wValue Descriptor type and index to retrieve
  125. * \param[in] wIndex Sub-index to retrieve (such as a localized string language)
  126. * \param[out] DescriptorAddress Address of the retrieved descriptor
  127. *
  128. * \return Length of the retrieved descriptor in bytes, or NO_DESCRIPTOR if the descriptor was not found
  129. */
  130. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  131. const uint16_t wIndex,
  132. const void** const DescriptorAddress)
  133. {
  134. return AVRISP_GetDescriptor(wValue, wIndex, DescriptorAddress);
  135. }