RelayBoard.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 2010 OBinou (obconseil [at] gmail [dot] com)
  9. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** \file
  28. *
  29. * Main source file for the RelayBoard program. This file contains the main tasks of
  30. * the project and is responsible for the initial application hardware configuration.
  31. */
  32. #include "RelayBoard.h"
  33. /** Main program entry point. This routine contains the overall program flow, including initial
  34. * setup of all components and the main program loop.
  35. */
  36. int main(void)
  37. {
  38. SetupHardware();
  39. GlobalInterruptEnable();
  40. for (;;)
  41. USB_USBTask();
  42. }
  43. /** Configures the board hardware and chip peripherals for the project's functionality. */
  44. void SetupHardware(void)
  45. {
  46. #if (ARCH == ARCH_AVR8)
  47. /* Disable watchdog if enabled by bootloader/fuses */
  48. MCUSR &= ~(1 << WDRF);
  49. wdt_disable();
  50. /* Disable clock division */
  51. clock_prescale_set(clock_div_1);
  52. #endif
  53. /* Hardware Initialization */
  54. USB_Init();
  55. /* Initialize Relays */
  56. DDRC |= ALL_RELAYS;
  57. PORTC &= ~ALL_RELAYS;
  58. }
  59. /** Event handler for the library USB Control Request reception event. */
  60. void EVENT_USB_Device_ControlRequest(void)
  61. {
  62. const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 };
  63. uint8_t ControlData[2] = { 0, 0 };
  64. switch (USB_ControlRequest.bRequest)
  65. {
  66. case 0x09:
  67. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  68. {
  69. LEDs_ToggleLEDs(LEDS_LED1);
  70. Endpoint_ClearSETUP();
  71. Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData));
  72. Endpoint_ClearIN();
  73. switch (USB_ControlRequest.wValue)
  74. {
  75. case 0x303:
  76. if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1;
  77. break;
  78. case 0x306:
  79. if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2;
  80. break;
  81. case 0x309:
  82. if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3;
  83. break;
  84. case 0x30c:
  85. if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4;
  86. break;
  87. }
  88. }
  89. break;
  90. case 0x01:
  91. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  92. {
  93. LEDs_ToggleLEDs(LEDS_LED1);
  94. Endpoint_ClearSETUP();
  95. switch (USB_ControlRequest.wValue)
  96. {
  97. case 0x301:
  98. Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber));
  99. break;
  100. case 0x303:
  101. ControlData[1] = (PORTC & RELAY1) ? 2 : 3;
  102. break;
  103. case 0x306:
  104. ControlData[1] = (PORTC & RELAY2) ? 2 : 3;
  105. break;
  106. case 0x309:
  107. ControlData[1] = (PORTC & RELAY3) ? 2 : 3;
  108. break;
  109. case 0x30c:
  110. ControlData[1] = (PORTC & RELAY4) ? 2 : 3;
  111. break;
  112. }
  113. if (ControlData[1])
  114. Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData));
  115. Endpoint_ClearOUT();
  116. }
  117. break;
  118. }
  119. }