Keyboard firmwares for Atmel AVR and Cortex-M
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AVRISP-MKII.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * 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. /** Main program entry point. This routine contains the overall program flow, including initial
  33. * setup of all components and the main program loop.
  34. */
  35. int main(void)
  36. {
  37. SetupHardware();
  38. V2Protocol_Init();
  39. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  40. GlobalInterruptEnable();
  41. for (;;)
  42. {
  43. #if (BOARD == BOARD_USBTINYMKII)
  44. /* On the USBTINY-MKII target, there is a secondary LED which indicates the current selected power
  45. mode - either VBUS, or sourced from the VTARGET pin of the programming connectors */
  46. LEDs_ChangeLEDs(LEDMASK_VBUSPOWER, (PIND & (1 << 0)) ? 0 : LEDMASK_VBUSPOWER);
  47. #endif
  48. AVRISP_Task();
  49. USB_USBTask();
  50. }
  51. }
  52. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  53. void SetupHardware(void)
  54. {
  55. #if (ARCH == ARCH_AVR8)
  56. /* Disable watchdog if enabled by bootloader/fuses */
  57. MCUSR &= ~(1 << WDRF);
  58. wdt_disable();
  59. /* Disable clock division */
  60. clock_prescale_set(clock_div_1);
  61. #endif
  62. /* Hardware Initialization */
  63. LEDs_Init();
  64. #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
  65. UpdateCurrentCompatibilityMode();
  66. #endif
  67. /* USB Stack Initialization */
  68. USB_Init();
  69. }
  70. /** Event handler for the library USB Connection event. */
  71. void EVENT_USB_Device_Connect(void)
  72. {
  73. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  74. }
  75. /** Event handler for the library USB Disconnection event. */
  76. void EVENT_USB_Device_Disconnect(void)
  77. {
  78. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  79. }
  80. /** Event handler for the library USB Configuration Changed event. */
  81. void EVENT_USB_Device_ConfigurationChanged(void)
  82. {
  83. bool ConfigSuccess = true;
  84. /* Setup AVRISP Data OUT endpoint */
  85. ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
  86. /* Setup AVRISP Data IN endpoint if it is using a physically different endpoint */
  87. if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK))
  88. ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
  89. /* Indicate endpoint configuration success or failure */
  90. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  91. }
  92. /** Processes incoming V2 Protocol commands from the host, returning a response when required. */
  93. void AVRISP_Task(void)
  94. {
  95. /* Device must be connected and configured for the task to run */
  96. if (USB_DeviceState != DEVICE_STATE_Configured)
  97. return;
  98. V2Params_UpdateParamValues();
  99. Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
  100. /* Check to see if a V2 Protocol command has been received */
  101. if (Endpoint_IsOUTReceived())
  102. {
  103. LEDs_SetAllLEDs(LEDMASK_BUSY);
  104. /* Pass off processing of the V2 Protocol command to the V2 Protocol handler */
  105. V2Protocol_ProcessCommand();
  106. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  107. }
  108. }
  109. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  110. * documentation) by the application code so that the address and size of a requested descriptor can be given
  111. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  112. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  113. * USB host.
  114. *
  115. * \param[in] wValue Descriptor type and index to retrieve
  116. * \param[in] wIndex Sub-index to retrieve (such as a localized string language)
  117. * \param[out] DescriptorAddress Address of the retrieved descriptor
  118. * \param[out] DescriptorMemorySpace Memory space that the descriptor is stored in
  119. *
  120. * \return Length of the retrieved descriptor in bytes, or NO_DESCRIPTOR if the descriptor was not found
  121. */
  122. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  123. const uint8_t wIndex,
  124. const void** const DescriptorAddress,
  125. uint8_t* DescriptorMemorySpace)
  126. {
  127. return AVRISP_GetDescriptor(wValue, wIndex, DescriptorAddress, DescriptorMemorySpace);
  128. }