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.

Benito.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 Benito project. This file contains the main tasks of
  29. * the project and is responsible for the initial application hardware configuration.
  30. */
  31. #include "Benito.h"
  32. /** Circular buffer to hold data from the serial port before it is sent to the host. */
  33. static RingBuffer_t USARTtoUSB_Buffer;
  34. /** Underlying data buffer for \ref USARTtoUSB_Buffer, where the stored bytes are located. */
  35. static uint8_t USARTtoUSB_Buffer_Data[128];
  36. /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
  37. volatile struct
  38. {
  39. uint8_t ResetPulse; /**< Milliseconds remaining for target /RESET pulse */
  40. uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
  41. uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
  42. uint8_t PingPongLEDPulse; /**< Milliseconds remaining for enumeration Tx/Rx ping-pong LED pulse */
  43. } PulseMSRemaining;
  44. /** Milliseconds remaining until the receive buffer is flushed to the USB host */
  45. uint8_t FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS;
  46. /** LUFA CDC Class driver interface configuration and state information. This structure is
  47. * passed to all CDC Class driver functions, so that multiple instances of the same class
  48. * within a device can be differentiated from one another.
  49. */
  50. USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
  51. {
  52. .Config =
  53. {
  54. .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
  55. .DataINEndpoint =
  56. {
  57. .Address = CDC_TX_EPADDR,
  58. .Size = CDC_TXRX_EPSIZE,
  59. .Banks = 1,
  60. },
  61. .DataOUTEndpoint =
  62. {
  63. .Address = CDC_RX_EPADDR,
  64. .Size = CDC_TXRX_EPSIZE,
  65. .Banks = 1,
  66. },
  67. .NotificationEndpoint =
  68. {
  69. .Address = CDC_NOTIFICATION_EPADDR,
  70. .Size = CDC_NOTIFICATION_EPSIZE,
  71. .Banks = 1,
  72. },
  73. },
  74. };
  75. /** Main program entry point. This routine contains the overall program flow, including initial
  76. * setup of all components and the main program loop.
  77. */
  78. int main(void)
  79. {
  80. SetupHardware();
  81. RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data));
  82. GlobalInterruptEnable();
  83. for (;;)
  84. {
  85. /* Echo bytes from the host to the target via the hardware USART */
  86. if ((UCSR1A & (1 << UDRE1)) && CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
  87. {
  88. UDR1 = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
  89. LEDs_TurnOnLEDs(LEDMASK_TX);
  90. PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
  91. }
  92. /* Check if the millisecond timer has elapsed */
  93. if (TIFR0 & (1 << OCF0A))
  94. {
  95. /* Clear flush timer expiry flag */
  96. TIFR0 |= (1 << TOV0);
  97. /* Check if the reset pulse period has elapsed, if so tristate the target reset line */
  98. if (PulseMSRemaining.ResetPulse && !(--PulseMSRemaining.ResetPulse))
  99. {
  100. LEDs_TurnOffLEDs(LEDMASK_BUSY);
  101. AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;
  102. }
  103. /* Check if the LEDs should be ping-ponging (during enumeration) */
  104. if (PulseMSRemaining.PingPongLEDPulse && !(--PulseMSRemaining.PingPongLEDPulse))
  105. {
  106. LEDs_ToggleLEDs(LEDMASK_TX | LEDMASK_RX);
  107. PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS;
  108. }
  109. /* Turn off TX LED(s) once the TX pulse period has elapsed */
  110. if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))
  111. LEDs_TurnOffLEDs(LEDMASK_TX);
  112. /* Turn off RX LED(s) once the RX pulse period has elapsed */
  113. if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))
  114. LEDs_TurnOffLEDs(LEDMASK_RX);
  115. /* Check if the receive buffer flush period has expired */
  116. uint16_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
  117. if (!(--FlushPeriodRemaining) || (BufferCount > 200))
  118. {
  119. FlushPeriodRemaining = RECEIVE_BUFFER_FLUSH_MS;
  120. /* Start RX LED indicator pulse */
  121. if (BufferCount)
  122. {
  123. LEDs_TurnOnLEDs(LEDMASK_RX);
  124. PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
  125. }
  126. /* Echo bytes from the target to the host via the virtual serial port */
  127. while (BufferCount--)
  128. {
  129. /* Try to send the next byte of data to the host, abort if there is an error without dequeuing */
  130. if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
  131. RingBuffer_Peek(&USARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
  132. {
  133. break;
  134. }
  135. /* Dequeue the already sent byte from the buffer now we have confirmed that no transmission error occurred */
  136. RingBuffer_Remove(&USARTtoUSB_Buffer);
  137. }
  138. }
  139. }
  140. CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
  141. USB_USBTask();
  142. }
  143. }
  144. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  145. void SetupHardware(void)
  146. {
  147. #if (ARCH == ARCH_AVR8)
  148. /* Disable watchdog if enabled by bootloader/fuses */
  149. MCUSR &= ~(1 << WDRF);
  150. wdt_disable();
  151. /* Disable clock division */
  152. clock_prescale_set(clock_div_1);
  153. #endif
  154. /* Hardware Initialization */
  155. LEDs_Init();
  156. USB_Init();
  157. /* Millisecond Timer Interrupt */
  158. OCR0A = (F_CPU / 64 / 1000);
  159. TCCR0A = (1 << WGM01);
  160. TCCR0B = ((1 << CS01) | (1 << CS00));
  161. /* Tristate target /RESET Line */
  162. AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
  163. AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;
  164. }
  165. /** Event handler for the library USB Connection event. */
  166. void EVENT_USB_Device_Connect(void)
  167. {
  168. PulseMSRemaining.PingPongLEDPulse = PING_PONG_LED_PULSE_MS;
  169. LEDs_SetAllLEDs(LEDMASK_TX);
  170. }
  171. /** Event handler for the library USB Disconnection event. */
  172. void EVENT_USB_Device_Disconnect(void)
  173. {
  174. PulseMSRemaining.PingPongLEDPulse = 0;
  175. LEDs_SetAllLEDs(LEDS_NO_LEDS);
  176. }
  177. /** Event handler for the library USB Configuration Changed event. */
  178. void EVENT_USB_Device_ConfigurationChanged(void)
  179. {
  180. bool ConfigSuccess = true;
  181. ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
  182. PulseMSRemaining.PingPongLEDPulse = 0;
  183. LEDs_SetAllLEDs(ConfigSuccess ? LEDS_NO_LEDS : LEDMASK_ERROR);
  184. }
  185. /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
  186. * the device from the USB host before passing along unhandled control requests to the library for processing
  187. * internally.
  188. */
  189. void EVENT_USB_Device_ControlRequest(void)
  190. {
  191. CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
  192. }
  193. /** Event handler for the CDC Class driver Line Encoding Changed event.
  194. *
  195. * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
  196. */
  197. void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  198. {
  199. uint8_t ConfigMask = 0;
  200. switch (CDCInterfaceInfo->State.LineEncoding.ParityType)
  201. {
  202. case CDC_PARITY_Odd:
  203. ConfigMask = ((1 << UPM11) | (1 << UPM10));
  204. break;
  205. case CDC_PARITY_Even:
  206. ConfigMask = (1 << UPM11);
  207. break;
  208. }
  209. if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)
  210. ConfigMask |= (1 << USBS1);
  211. switch (CDCInterfaceInfo->State.LineEncoding.DataBits)
  212. {
  213. case 6:
  214. ConfigMask |= (1 << UCSZ10);
  215. break;
  216. case 7:
  217. ConfigMask |= (1 << UCSZ11);
  218. break;
  219. case 8:
  220. ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
  221. break;
  222. }
  223. /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
  224. UCSR1B = 0;
  225. UCSR1A = 0;
  226. UCSR1C = 0;
  227. /* Set the new baud rate before configuring the USART */
  228. UBRR1 = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
  229. /* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */
  230. UCSR1C = ConfigMask;
  231. UCSR1A = (1 << U2X1);
  232. UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
  233. }
  234. /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer
  235. * for later transmission to the host.
  236. */
  237. ISR(USART1_RX_vect, ISR_BLOCK)
  238. {
  239. uint8_t ReceivedByte = UDR1;
  240. if (USB_DeviceState == DEVICE_STATE_Configured)
  241. RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte);
  242. }
  243. /** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
  244. *
  245. * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
  246. */
  247. void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  248. {
  249. static bool PreviousDTRState = false;
  250. bool CurrentDTRState = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);
  251. /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */
  252. if (!(PreviousDTRState) && CurrentDTRState)
  253. {
  254. LEDs_SetAllLEDs(LEDMASK_BUSY);
  255. AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;
  256. PulseMSRemaining.ResetPulse = AVR_RESET_PULSE_MS;
  257. }
  258. PreviousDTRState = CurrentDTRState;
  259. }