BootloaderCDC.h 4.9 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 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. * Header file for BootloaderCDC.c.
  29. */
  30. #ifndef _CDC_H_
  31. #define _CDC_H_
  32. /* Includes: */
  33. #include <avr/io.h>
  34. #include <avr/wdt.h>
  35. #include <avr/boot.h>
  36. #include <avr/eeprom.h>
  37. #include <avr/power.h>
  38. #include <avr/interrupt.h>
  39. #include <util/delay.h>
  40. #include <stdbool.h>
  41. #include "Descriptors.h"
  42. #include "BootloaderAPI.h"
  43. #include "Config/AppConfig.h"
  44. #include <LUFA/Drivers/USB/USB.h>
  45. #include <LUFA/Drivers/Board/LEDs.h>
  46. #include <LUFA/Platform/Platform.h>
  47. /* Preprocessor Checks: */
  48. #if !defined(__OPTIMIZE_SIZE__)
  49. #error This bootloader requires that it be optimized for size, not speed, to fit into the target device. Change optimization settings and try again.
  50. #endif
  51. /* Macros: */
  52. /** Version major of the CDC bootloader. */
  53. #define BOOTLOADER_VERSION_MAJOR 0x01
  54. /** Version minor of the CDC bootloader. */
  55. #define BOOTLOADER_VERSION_MINOR 0x00
  56. /** Hardware version major of the CDC bootloader. */
  57. #define BOOTLOADER_HWVERSION_MAJOR 0x01
  58. /** Hardware version minor of the CDC bootloader. */
  59. #define BOOTLOADER_HWVERSION_MINOR 0x00
  60. /** Eight character bootloader firmware identifier reported to the host when requested. */
  61. #define SOFTWARE_IDENTIFIER "LUFACDC"
  62. /** Magic bootloader key to unlock forced application start mode. */
  63. #define MAGIC_BOOT_KEY 0xDC42
  64. /* Enums: */
  65. /** Possible memory types that can be addressed via the bootloader. */
  66. enum AVR109_Memories
  67. {
  68. MEMORY_TYPE_FLASH = 'F',
  69. MEMORY_TYPE_EEPROM = 'E',
  70. };
  71. /** Possible commands that can be issued to the bootloader. */
  72. enum AVR109_Commands
  73. {
  74. AVR109_COMMAND_Sync = 27,
  75. AVR109_COMMAND_ReadEEPROM = 'd',
  76. AVR109_COMMAND_WriteEEPROM = 'D',
  77. AVR109_COMMAND_ReadFLASHWord = 'R',
  78. AVR109_COMMAND_WriteFlashPage = 'm',
  79. AVR109_COMMAND_FillFlashPageWordLow = 'c',
  80. AVR109_COMMAND_FillFlashPageWordHigh = 'C',
  81. AVR109_COMMAND_GetBlockWriteSupport = 'b',
  82. AVR109_COMMAND_BlockWrite = 'B',
  83. AVR109_COMMAND_BlockRead = 'g',
  84. AVR109_COMMAND_ReadExtendedFuses = 'Q',
  85. AVR109_COMMAND_ReadHighFuses = 'N',
  86. AVR109_COMMAND_ReadLowFuses = 'F',
  87. AVR109_COMMAND_ReadLockbits = 'r',
  88. AVR109_COMMAND_WriteLockbits = 'l',
  89. AVR109_COMMAND_EraseFLASH = 'e',
  90. AVR109_COMMAND_ReadSignature = 's',
  91. AVR109_COMMAND_ReadBootloaderSWVersion = 'V',
  92. AVR109_COMMAND_ReadBootloaderHWVersion = 'v',
  93. AVR109_COMMAND_ReadBootloaderIdentifier = 'S',
  94. AVR109_COMMAND_ReadBootloaderInterface = 'p',
  95. AVR109_COMMAND_SetCurrentAddress = 'A',
  96. AVR109_COMMAND_ReadAutoAddressIncrement = 'a',
  97. AVR109_COMMAND_ReadPartCode = 't',
  98. AVR109_COMMAND_EnterProgrammingMode = 'P',
  99. AVR109_COMMAND_LeaveProgrammingMode = 'L',
  100. AVR109_COMMAND_SelectDeviceType = 'T',
  101. AVR109_COMMAND_SetLED = 'x',
  102. AVR109_COMMAND_ClearLED = 'y',
  103. AVR109_COMMAND_ExitBootloader = 'E',
  104. };
  105. /* Type Defines: */
  106. /** Type define for a non-returning pointer to the start of the loaded application in flash memory. */
  107. typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;
  108. /* Function Prototypes: */
  109. static void CDC_Task(void);
  110. static void SetupHardware(void);
  111. void Application_Jump_Check(void) ATTR_INIT_SECTION(3);
  112. void EVENT_USB_Device_ConfigurationChanged(void);
  113. #if defined(INCLUDE_FROM_BOOTLOADERCDC_C) || defined(__DOXYGEN__)
  114. #if !defined(NO_BLOCK_SUPPORT)
  115. static void ReadWriteMemoryBlock(const uint8_t Command);
  116. #endif
  117. static uint8_t FetchNextCommandByte(void);
  118. static void WriteNextResponseByte(const uint8_t Response);
  119. #endif
  120. #endif