Browse Source

add reform2 trackball firmware

reform2-nitrogen8m
mntmn 6 months ago
parent
commit
a267be9cfb
100 changed files with 28870 additions and 0 deletions
  1. +126
    -0
      reform2-trackball-fw/Config/LUFAConfig.h
  2. +261
    -0
      reform2-trackball-fw/Descriptors.c
  3. +100
    -0
      reform2-trackball-fw/Descriptors.h
  4. +42
    -0
      reform2-trackball-fw/Makefile
  5. +333
    -0
      reform2-trackball-fw/Mouse.c
  6. +77
    -0
      reform2-trackball-fw/Mouse.h
  7. +1
    -0
      reform2-trackball-fw/debug.sh
  8. +7
    -0
      reform2-trackball-fw/flash.sh
  9. +1449
    -0
      reform2-trackball-fw/i2cmaster/doxygen.css
  10. BIN
     
  11. +335
    -0
      reform2-trackball-fw/i2cmaster/i2cmaster.S
  12. +176
    -0
      reform2-trackball-fw/i2cmaster/i2cmaster.h
  13. +330
    -0
      reform2-trackball-fw/i2cmaster/makefile.i2cmaster
  14. +330
    -0
      reform2-trackball-fw/i2cmaster/makefile.twimaster
  15. +384
    -0
      reform2-trackball-fw/i2cmaster/manual__i2cmaster.html
  16. +78
    -0
      reform2-trackball-fw/i2cmaster/test_i2cmaster.c
  17. +202
    -0
      reform2-trackball-fw/i2cmaster/twimaster.c
  18. +15
    -0
      reform2-trackball-fw/lufa-master/.gitignore
  19. +99
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/BootloaderAPI.c
  20. +59
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/BootloaderAPI.h
  21. +91
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/BootloaderAPITable.S
  22. +656
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/BootloaderCDC.c
  23. +145
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/BootloaderCDC.h
  24. +242
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/BootloaderCDC.txt
  25. +50
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/Config/AppConfig.h
  26. +93
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/Config/LUFAConfig.h
  27. +244
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/Descriptors.c
  28. +158
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/Descriptors.h
  29. +66
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/LUFA CDC Bootloader.inf
  30. +161
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/asf.xml
  31. +2396
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/doxyfile
  32. +62
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/CDC/makefile
  33. +99
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/BootloaderAPI.c
  34. +59
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/BootloaderAPI.h
  35. +91
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/BootloaderAPITable.S
  36. +844
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/BootloaderDFU.c
  37. +216
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/BootloaderDFU.h
  38. +235
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/BootloaderDFU.txt
  39. +48
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/Config/AppConfig.h
  40. +93
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/Config/LUFAConfig.h
  41. +185
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/Descriptors.c
  42. +194
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/Descriptors.h
  43. +156
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/asf.xml
  44. +2396
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/doxyfile
  45. +62
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/DFU/makefile
  46. +211
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/BootloaderHID.c
  47. +73
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/BootloaderHID.h
  48. +105
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/BootloaderHID.txt
  49. +93
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/Config/LUFAConfig.h
  50. +187
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/Descriptors.c
  51. +80
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/Descriptors.h
  52. +1
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/HostLoaderApp/.gitignore
  53. +40
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/HostLoaderApp/Makefile
  54. +21
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/HostLoaderApp/Makefile.bsd
  55. +674
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/HostLoaderApp/gpl3.txt
  56. +1013
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/HostLoaderApp/hid_bootloader_cli.c
  57. +129
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py
  58. +123
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/asf.xml
  59. +2398
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/doxyfile
  60. +55
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/HID/makefile
  61. +99
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/BootloaderAPI.c
  62. +64
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/BootloaderAPI.h
  63. +102
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/BootloaderAPITable.S
  64. +266
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/BootloaderMassStorage.c
  65. +101
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/BootloaderMassStorage.h
  66. +240
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/BootloaderMassStorage.txt
  67. +47
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Config/AppConfig.h
  68. +93
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Config/LUFAConfig.h
  69. +157
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Descriptors.c
  70. +88
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Descriptors.h
  71. +294
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Lib/SCSI.c
  72. +84
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Lib/SCSI.h
  73. +482
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Lib/VirtualFAT.c
  74. +302
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/Lib/VirtualFAT.h
  75. +156
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/asf.xml
  76. +2396
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/doxyfile
  77. +75
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/MassStorage/makefile
  78. +99
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/BootloaderAPI.c
  79. +57
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/BootloaderAPI.h
  80. +91
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/BootloaderAPITable.S
  81. +488
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/BootloaderPrinter.c
  82. +110
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/BootloaderPrinter.h
  83. +202
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/BootloaderPrinter.txt
  84. +93
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/Config/LUFAConfig.h
  85. +194
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/Descriptors.c
  86. +96
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/Descriptors.h
  87. +159
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/asf.xml
  88. +2396
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/doxyfile
  89. +62
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/Printer/makefile
  90. +46
    -0
      reform2-trackball-fw/lufa-master/Bootloaders/makefile
  91. +82
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/Board/Board.h
  92. +92
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/Board/Buttons.h
  93. +197
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/Board/Dataflash.h
  94. +104
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/Board/Joystick.h
  95. +132
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/Board/LEDs.h
  96. +89
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/BoardDeviceMap.cfg
  97. +115
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/Test.c
  98. +69
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/makefile
  99. +35
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BoardDriverTest/makefile.test
  100. +167
    -0
      reform2-trackball-fw/lufa-master/BuildTests/BootloaderTest/BootloaderDeviceMap.cfg

+ 126
- 0
reform2-trackball-fw/Config/LUFAConfig.h View File

@@ -0,0 +1,126 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2018.

dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/

/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)

Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/

/** \file
* \brief LUFA Library Configuration Header File
*
* This header file is used to configure LUFA's compile time options,
* as an alternative to the compile time constants supplied through
* a makefile.
*
* For information on what each token does, refer to the LUFA
* manual section "Summary of Compile Tokens".
*/

#ifndef _LUFA_CONFIG_H_
#define _LUFA_CONFIG_H_

#if (ARCH == ARCH_AVR8)

/* Non-USB Related Configuration Tokens: */
// #define DISABLE_TERMINAL_CODES

/* USB Class Driver Related Tokens: */
// #define HID_HOST_BOOT_PROTOCOL_ONLY
// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here}
// #define HID_USAGE_STACK_DEPTH {Insert Value Here}
// #define HID_MAX_COLLECTIONS {Insert Value Here}
// #define HID_MAX_REPORTITEMS {Insert Value Here}
// #define HID_MAX_REPORT_IDS {Insert Value Here}
// #define NO_CLASS_DRIVER_AUTOFLUSH

/* General USB Driver Related Tokens: */
// #define ORDERED_EP_CONFIG
#define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)
#define USB_DEVICE_ONLY
// #define USB_HOST_ONLY
// #define USB_STREAM_TIMEOUT_MS {Insert Value Here}
// #define NO_LIMITED_CONTROLLER_CONNECT
// #define NO_SOF_EVENTS

/* USB Device Mode Driver Related Tokens: */
// #define USE_RAM_DESCRIPTORS
#define USE_FLASH_DESCRIPTORS
// #define USE_EEPROM_DESCRIPTORS
// #define NO_INTERNAL_SERIAL
#define FIXED_CONTROL_ENDPOINT_SIZE 8
// #define DEVICE_STATE_AS_GPIOR {Insert Value Here}
#define FIXED_NUM_CONFIGURATIONS 1
// #define CONTROL_ONLY_DEVICE
// #define INTERRUPT_CONTROL_ENDPOINT
// #define NO_DEVICE_REMOTE_WAKEUP
// #define NO_DEVICE_SELF_POWER

/* USB Host Mode Driver Related Tokens: */
// #define HOST_STATE_AS_GPIOR {Insert Value Here}
// #define USB_HOST_TIMEOUT_MS {Insert Value Here}
// #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here}
// #define NO_AUTO_VBUS_MANAGEMENT
// #define INVERTED_VBUS_ENABLE_LINE

#elif (ARCH == ARCH_XMEGA)

/* Non-USB Related Configuration Tokens: */
// #define DISABLE_TERMINAL_CODES

/* USB Class Driver Related Tokens: */
// #define HID_HOST_BOOT_PROTOCOL_ONLY
// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here}
// #define HID_USAGE_STACK_DEPTH {Insert Value Here}
// #define HID_MAX_COLLECTIONS {Insert Value Here}
// #define HID_MAX_REPORTITEMS {Insert Value Here}
// #define HID_MAX_REPORT_IDS {Insert Value Here}
// #define NO_CLASS_DRIVER_AUTOFLUSH

/* General USB Driver Related Tokens: */
#define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_RC32MCLKSRC | USB_OPT_BUSEVENT_PRIHIGH)
// #define USB_STREAM_TIMEOUT_MS {Insert Value Here}
// #define NO_LIMITED_CONTROLLER_CONNECT
// #define NO_SOF_EVENTS

/* USB Device Mode Driver Related Tokens: */
// #define USE_RAM_DESCRIPTORS
#define USE_FLASH_DESCRIPTORS
// #define USE_EEPROM_DESCRIPTORS
// #define NO_INTERNAL_SERIAL
#define FIXED_CONTROL_ENDPOINT_SIZE 8
// #define DEVICE_STATE_AS_GPIOR {Insert Value Here}
#define FIXED_NUM_CONFIGURATIONS 1
// #define CONTROL_ONLY_DEVICE
#define MAX_ENDPOINT_INDEX 1
// #define NO_DEVICE_REMOTE_WAKEUP
// #define NO_DEVICE_SELF_POWER

#else

#error Unsupported architecture for this LUFA configuration file.

#endif
#endif

+ 261
- 0
reform2-trackball-fw/Descriptors.c View File

@@ -0,0 +1,261 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2018.

dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/

/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)

Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/

/** \file
*
* USB Device Descriptors, for library use when in USB device mode. Descriptors are special
* computer-readable structures which the host requests upon device enumeration, to determine
* the device's capabilities and functions.
*/

#include "Descriptors.h"

/** HID class report descriptor. This is a special descriptor constructed with values from the
* USBIF HID class specification to describe the reports and capabilities of the HID device. This
* descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
* the device will send, and what it may be sent back from the host. Refer to the HID specification for
* more details on HID report descriptors.
*/
const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
{
/* Use the HID class driver's standard Mouse report.
* Min X/Y Axis values: -1
* Max X/Y Axis values: 1
* Min physical X/Y Axis values (used to determine resolution): -1
* Max physical X/Y Axis values (used to determine resolution): 1
* Buttons: 3
* Absolute screen coordinates: false
*/
//HID_DESCRIPTOR_MOUSE(-127, 127, -127, 127, 3, false)

HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
HID_RI_USAGE(8, 0x02), /* Mouse */
HID_RI_COLLECTION(8, 0x01), /* Application */
HID_RI_USAGE(8, 0x01), /* Pointer */
HID_RI_COLLECTION(8, 0x00), /* Physical */
HID_RI_USAGE_PAGE(8, 0x09), /* Button */
HID_RI_USAGE_MINIMUM(8, 0x01),
HID_RI_USAGE_MAXIMUM(8, 0x03),
HID_RI_LOGICAL_MINIMUM(8, 0x00),
HID_RI_LOGICAL_MAXIMUM(8, 0x01),
HID_RI_REPORT_COUNT(8, 0x03),
HID_RI_REPORT_SIZE(8, 0x01),
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
HID_RI_REPORT_COUNT(8, 0x01), // 5 bits padding
HID_RI_REPORT_SIZE(8, 0x05),
HID_RI_INPUT(8, HID_IOF_CONSTANT),
HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
HID_RI_USAGE(8, 0x30), /* Usage X */
HID_RI_USAGE(8, 0x31), /* Usage Y */
HID_RI_LOGICAL_MINIMUM(8, -127),
HID_RI_LOGICAL_MAXIMUM(8, 127),
HID_RI_PHYSICAL_MINIMUM(8, -127),
HID_RI_PHYSICAL_MAXIMUM(8, 127),
HID_RI_REPORT_COUNT(8, 0x02),
HID_RI_REPORT_SIZE(8, 0x08),
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
// ---
HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
HID_RI_USAGE(8, 0x38), /* Usage Wheel */
HID_RI_LOGICAL_MINIMUM(8, -127),
HID_RI_LOGICAL_MAXIMUM(8, 127),
HID_RI_PHYSICAL_MINIMUM(8, -127),
HID_RI_PHYSICAL_MAXIMUM(8, 127),
HID_RI_REPORT_COUNT(8, 0x01),
HID_RI_REPORT_SIZE(8, 0x08),
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
// ---
HID_RI_END_COLLECTION(0),
HID_RI_END_COLLECTION(0),
};

/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
* device characteristics, including the supported USB version, control endpoint size and the
* number of device configurations. The descriptor is read out by the USB host when the enumeration
* process begins.
*/
const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},

.USBSpecification = VERSION_BCD(1,1,0),
.Class = USB_CSCP_NoDeviceClass,
.SubClass = USB_CSCP_NoDeviceSubclass,
.Protocol = USB_CSCP_NoDeviceProtocol,

.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,

.VendorID = 0x03EB,
.ProductID = 0x2041,
.ReleaseNumber = VERSION_BCD(0,0,1),

.ManufacturerStrIndex = STRING_ID_Manufacturer,
.ProductStrIndex = STRING_ID_Product,
.SerialNumStrIndex = NO_DESCRIPTOR,

.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};

/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
* of the device in one of its supported configurations, including information about any device interfaces
* and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
* a configuration so that the host may correctly communicate with the USB device.
*/
const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Config =
{
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},

.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
.TotalInterfaces = 1,

.ConfigurationNumber = 1,
.ConfigurationStrIndex = NO_DESCRIPTOR,

.ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),

.MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
},

.HID_Interface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

.InterfaceNumber = INTERFACE_ID_Mouse,
.AlternateSetting = 0x00,

.TotalEndpoints = 1,

.Class = HID_CSCP_HIDClass,
.SubClass = HID_CSCP_BootSubclass,
.Protocol = HID_CSCP_MouseBootProtocol,

.InterfaceStrIndex = NO_DESCRIPTOR
},

.HID_MouseHID =
{
.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},

.HIDSpec = VERSION_BCD(1,1,1),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = HID_DTYPE_Report,
.HIDReportLength = sizeof(MouseReport)
},

.HID_ReportINEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},

.EndpointAddress = MOUSE_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = MOUSE_EPSIZE,
.PollingIntervalMS = 0x05
}
};

/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
* the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
* via the language ID table available at USB.org what languages the device supports for its string descriptors.
*/
const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);

/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
* form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
* Descriptor.
*/
const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"MNT");

/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
* and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
* Descriptor.
*/
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Reform Trackball");

/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
* documentation) by the application code so that the address and size of a requested descriptor can be given
* to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
* is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
* USB host.
*/
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint16_t wIndex,
const void** const DescriptorAddress)
{
const uint8_t DescriptorType = (wValue >> 8);
const uint8_t DescriptorNumber = (wValue & 0xFF);

const void* Address = NULL;
uint16_t Size = NO_DESCRIPTOR;

switch (DescriptorType)
{
case DTYPE_Device:
Address = &DeviceDescriptor;
Size = sizeof(USB_Descriptor_Device_t);
break;
case DTYPE_Configuration:
Address = &ConfigurationDescriptor;
Size = sizeof(USB_Descriptor_Configuration_t);
break;
case DTYPE_String:
switch (DescriptorNumber)
{
case STRING_ID_Language:
Address = &LanguageString;
Size = pgm_read_byte(&LanguageString.Header.Size);
break;
case STRING_ID_Manufacturer:
Address = &ManufacturerString;
Size = pgm_read_byte(&ManufacturerString.Header.Size);
break;
case STRING_ID_Product:
Address = &ProductString;
Size = pgm_read_byte(&ProductString.Header.Size);
break;
}

break;
case HID_DTYPE_HID:
Address = &ConfigurationDescriptor.HID_MouseHID;
Size = sizeof(USB_HID_Descriptor_HID_t);
break;
case HID_DTYPE_Report:
Address = &MouseReport;
Size = sizeof(MouseReport);
break;
}

*DescriptorAddress = Address;
return Size;
}


+ 100
- 0
reform2-trackball-fw/Descriptors.h View File

@@ -0,0 +1,100 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2018.

dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/

/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)

Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/

/** \file
*
* Header file for Descriptors.c.
*/

#ifndef _DESCRIPTORS_H_
#define _DESCRIPTORS_H_

/* Includes: */
#include <LUFA/Drivers/USB/USB.h>

#include <avr/pgmspace.h>

/* Macros: */
/** Endpoint address of the Mouse HID reporting IN endpoint. */
#define MOUSE_EPADDR (ENDPOINT_DIR_IN | 1)

/** Size in bytes of the Mouse HID reporting IN endpoint. */
#define MOUSE_EPSIZE 8

/* Type Defines: */
/** Type define for the device configuration descriptor structure. This must be defined in the
* application code, as the configuration descriptor contains several sub-descriptors which
* vary between devices, and which describe the device's usage to the host.
*/
typedef struct
{
USB_Descriptor_Configuration_Header_t Config;

// Mouse HID Interface
USB_Descriptor_Interface_t HID_Interface;
USB_HID_Descriptor_HID_t HID_MouseHID;
USB_Descriptor_Endpoint_t HID_ReportINEndpoint;
} USB_Descriptor_Configuration_t;

/** Enum for the device interface descriptor IDs within the device. Each interface descriptor
* should have a unique ID index associated with it, which can be used to refer to the
* interface from other descriptors.
*/
enum InterfaceDescriptors_t
{
INTERFACE_ID_Mouse = 0, /**< Mouse interface descriptor ID */
};

/** Enum for the device string descriptor IDs within the device. Each string descriptor should
* have a unique ID index associated with it, which can be used to refer to the string from
* other descriptors.
*/
enum StringDescriptors_t
{
STRING_ID_Language = 0, /**< Supported Languages string descriptor ID (must be zero) */
STRING_ID_Manufacturer = 1, /**< Manufacturer string ID */
STRING_ID_Product = 2, /**< Product string ID */
};

/* Function Prototypes: */
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint16_t wIndex,
const void** const DescriptorAddress)
ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3);

typedef struct
{
uint8_t Button;
int8_t X;
int8_t Y;
int8_t Wheel;
} ATTR_PACKED USB_WheelMouseReport_Data_t;

#endif

+ 42
- 0
reform2-trackball-fw/Makefile View File

@@ -0,0 +1,42 @@
#
# LUFA Library
# Copyright (C) Dean Camera, 2018.
#
# dean [at] fourwalledcubicle [dot] com
# www.lufa-lib.org
#
# --------------------------------------
# LUFA Project Makefile.
# --------------------------------------

# Run "make help" for target help.

MCU = atmega32u2
ARCH = AVR8
F_CPU = 16000000
F_USB = $(F_CPU)
OPTIMIZATION = s
TARGET = Mouse
SRC = $(TARGET).c i2cmaster/i2cmaster.S Descriptors.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS)
LUFA_PATH = ./lufa-master/LUFA
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/
LD_FLAGS =

# Default target
all:

# Include LUFA-specific DMBS extension modules
DMBS_LUFA_PATH ?= $(LUFA_PATH)/Build/LUFA
include $(DMBS_LUFA_PATH)/lufa-sources.mk
include $(DMBS_LUFA_PATH)/lufa-gcc.mk

# Include common DMBS build system modules
DMBS_PATH ?= $(LUFA_PATH)/Build/DMBS/DMBS
include $(DMBS_PATH)/core.mk
include $(DMBS_PATH)/cppcheck.mk
include $(DMBS_PATH)/doxygen.mk
include $(DMBS_PATH)/dfu.mk
include $(DMBS_PATH)/gcc.mk
include $(DMBS_PATH)/hid.mk
include $(DMBS_PATH)/avrdude.mk
include $(DMBS_PATH)/atprogram.mk

+ 333
- 0
reform2-trackball-fw/Mouse.c View File

@@ -0,0 +1,333 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2018.

dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/

/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)

Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/

#include <avr/pgmspace.h>
#include <avr/io.h>
#include <stdlib.h>

#include "i2cmaster/i2cmaster.h"

#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)

// Registers
#define Product_ID 0x00
#define Revision_ID 0x01
#define Motion 0x02
#define Delta_X_L 0x03
#define Delta_X_H 0x04
#define Delta_Y_L 0x05
#define Delta_Y_H 0x06
#define SQUAL 0x07
#define Raw_Data_Sum 0x08
#define Maximum_Raw_data 0x09
#define Minimum_Raw_data 0x0A
#define Shutter_Lower 0x0B
#define Shutter_Upper 0x0C
#define Control 0x0D
#define Config1 0x0F
#define Config2 0x10
#define Angle_Tune 0x11
#define Frame_Capture 0x12
#define SROM_Enable 0x13
#define Run_Downshift 0x14
#define Rest1_Rate_Lower 0x15
#define Rest1_Rate_Upper 0x16
#define Rest1_Downshift 0x17
#define Rest2_Rate_Lower 0x18
#define Rest2_Rate_Upper 0x19
#define Rest2_Downshift 0x1A
#define Rest3_Rate_Lower 0x1B
#define Rest3_Rate_Upper 0x1C
#define Observation 0x24
#define Data_Out_Lower 0x25
#define Data_Out_Upper 0x26
#define Raw_Data_Dump 0x29
#define SROM_ID 0x2A
#define Min_SQ_Run 0x2B
#define Raw_Data_Threshold 0x2C
#define Config5 0x2F
#define Power_Up_Reset 0x3A
#define Shutdown 0x3B
#define Inverse_Product_ID 0x3F
#define LiftCutoff_Tune3 0x41
#define Angle_Snap 0x42
#define LiftCutoff_Tune1 0x4A
#define Motion_Burst 0x50
#define LiftCutoff_Tune_Timeout 0x58
#define LiftCutoff_Tune_Min_Length 0x5A
#define SROM_Load_Burst 0x62
#define Lift_Config 0x63
#define Raw_Data_Burst 0x64
#define LiftCutoff_Tune2 0x65

uint8_t adns_init_complete=0;
volatile int xydat[2];

//#include "PWM3360DM_srom_0x04.c"
#include "Mouse.h"

/** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_WheelMouseReport_Data_t)];

/** LUFA HID Class driver interface configuration and state information. This structure is
* passed to all HID Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_Mouse,
.ReportINEndpoint =
{
.Address = MOUSE_EPADDR,
.Size = MOUSE_EPSIZE,
.Banks = 1,
},
.PrevReportINBuffer = PrevMouseHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
},
};


void led_error(void) {
DDRC = 0b11110100;
}

void led_ok(void) {
DDRC = 0;
}


// FIXME QUESTIONABLE
int convTwosComp(int b) {
//Convert from 2's complement
if (b & 0x80) {
b = -1 * ((b ^ 0xff) + 1);
}
return b;
}

// LM PD0 input pullup
// RM PD1 input pullup

#define ADDR_SENSOR (0x79<<1)

uint8_t twi_write_reg[1];
uint8_t twi_write_buf[10];
uint8_t twi_read_buf[10];

void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();

// Disable clock division
// this should yield 8Mhz with internal osc
clock_prescale_set(clock_div_1);

DDRD = 0b00000000;
//DDRB = 0b11000110;
DDRC = 0b00000000;
output_high(PORTD, 0); // enable input pullup for LMB
output_high(PORTD, 1); // enable input pullup for RMB
output_high(PORTD, 2); // enable input pullup for RMB
output_high(PORTD, 3); // enable input pullup for RMB
output_high(PORTD, 4); // enable input pullup for RMB
//output_high(PORTC, 5);
// no jtag plox
//MCUCR |=(1<<JTD);
//MCUCR |=(1<<JTD);
//SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
// SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);

i2c_init();
USB_Init();

Delay_MS(1000);
led_error();
Delay_MS(1000);

if (!i2c_start(ADDR_SENSOR|I2C_WRITE)) {
i2c_write(0x7f);
i2c_write(0x0);
i2c_stop();

led_ok();
}
if (!i2c_start(ADDR_SENSOR|I2C_WRITE)) {
i2c_write(0x05);
i2c_write(0x01);
i2c_stop();

led_ok();
}
Delay_MS(100);
}

/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
}

/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
}

/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
bool ConfigSuccess = true;

ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);

USB_Device_EnableSOFEvents();
}

/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
}

/** Event handler for the USB device Start Of Frame event. */
void EVENT_USB_Device_StartOfFrame(void)
{
HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
}

/** HID class driver callback function for the creation of HID reports to the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
* \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
* \param[out] ReportData Pointer to a buffer where the created report should be stored
* \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
*
* \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
*/
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize)
{
if (ReportType==HID_REPORT_ITEM_Feature) return false;
int8_t nx = 0;
int8_t ny = 0;

i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
i2c_write(0x02);
i2c_rep_start(ADDR_SENSOR|I2C_READ);
uint8_t ret = i2c_readNak();
i2c_stop();
if (ret & 0xf0) {
i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
i2c_write(0x03);
i2c_rep_start(ADDR_SENSOR|I2C_READ);
nx = i2c_readAck();
ny = i2c_readNak();
i2c_stop();
}
led_ok();
USB_WheelMouseReport_Data_t* MouseReport = (USB_WheelMouseReport_Data_t*)ReportData;

if (!(PIND&(1<<4))) {
MouseReport->Button |= 1;
}
if (!(PIND&(1<<3))) {
MouseReport->Button |= 2;
}
if (!(PIND&(1<<1))) {
MouseReport->Button |= 4;
}
if (!(PIND&(1<<2))) {
MouseReport->Button |= 1;
}

MouseReport->Wheel = 0;
if (!(PIND&1) || !(PIND&(1<<2))) {
// wheel
MouseReport->Wheel = -ny;
led_error();
} else {
MouseReport->X = 2*abs(nx)*nx;
MouseReport->Y = 2*abs(ny)*ny;
}

*ReportSize = sizeof(USB_WheelMouseReport_Data_t);

return true;
}

/** HID class driver callback function for the processing of HID reports from the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in] ReportID Report ID of the received report from the host
* \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
* \param[in] ReportData Pointer to a buffer where the received report has been stored
* \param[in] ReportSize Size in bytes of the received HID report
*/
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize)
{
// Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
}


int main(void)
{
SetupHardware();
GlobalInterruptEnable();

for (;;)
{
HID_Device_USBTask(&Mouse_HID_Interface);
USB_USBTask();
}
}


+ 77
- 0
reform2-trackball-fw/Mouse.h View File

@@ -0,0 +1,77 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2018.

dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/

/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)

Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/

/** \file
*
* Header file for Mouse.c.
*/

#ifndef _MOUSE_H_
#define _MOUSE_H_

/* Includes: */
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <string.h>

#include "Descriptors.h"

#include <LUFA/Drivers/Board/Joystick.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include <LUFA/Drivers/Board/Buttons.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Platform/Platform.h>

/* Function Prototypes: */
void SetupHardware(void);

void EVENT_USB_Device_Connect(void);
void EVENT_USB_Device_Disconnect(void);
void EVENT_USB_Device_ConfigurationChanged(void);
void EVENT_USB_Device_ControlRequest(void);
void EVENT_USB_Device_StartOfFrame(void);

bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize);
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize);

#endif


+ 1
- 0
reform2-trackball-fw/debug.sh View File

@@ -0,0 +1 @@
sudo usbhid-dump -a 003:019 -es

+ 7
- 0
reform2-trackball-fw/flash.sh View File

@@ -0,0 +1,7 @@


dfu-programmer atmega32u2 erase --suppress-bootloader-mem

dfu-programmer atmega32u2 flash ./Mouse.hex --suppress-bootloader-mem

dfu-programmer atmega32u2 start

+ 1449
- 0
reform2-trackball-fw/i2cmaster/doxygen.css
File diff suppressed because it is too large
View File


BIN
View File


+ 335
- 0
reform2-trackball-fw/i2cmaster/i2cmaster.S View File

@@ -0,0 +1,335 @@
;*************************************************************************
; Title : I2C (Single) Master Implementation
; Author: Peter Fleury <pfleury@gmx.ch>
; based on Atmel Appl. Note AVR300
; File: $Id: i2cmaster.S,v 1.13 2015/09/16 11:21:00 peter Exp $
; Software: AVR-GCC 4.x
; Target: any AVR device
;
; DESCRIPTION
; Basic routines for communicating with I2C slave devices. This
; "single" master implementation is limited to one bus master on the
; I2C bus.
;
; Based on the Atmel Application Note AVR300, corrected and adapted
; to GNU assembler and AVR-GCC C call interface
; Replaced the incorrect quarter period delays found in AVR300 with
; half period delays.
;
; USAGE
; These routines can be called from C, refere to file i2cmaster.h.
; See example test_i2cmaster.c
; Adapt the SCL and SDA port and pin definitions and eventually
; the delay routine to your target !
; Use 4.7k pull-up resistor on the SDA and SCL pin.
;
; NOTES
; The I2C routines can be called either from non-interrupt or
; interrupt routines, not both.
;
;*************************************************************************


#include <avr/io.h>

#undef SCL_PORT
#undef SCL_DDR


;******----- Adapt these SCA and SCL port and pin definition to your target !!
;
#define SDA 1 // SDA Port D, Pin 4
#define SCL 2 // SCL Port D, Pin 5
#define SDA_PORT PORTB // SDA Port D
#define SCL_PORT PORTB // SCL Port D

;******----------------------------------------------------------------------


;-- map the IO register back into the IO address space
#define SDA_DDR (_SFR_IO_ADDR(SDA_PORT) - 1)
#define SCL_DDR (_SFR_IO_ADDR(SCL_PORT) - 1)
#define SDA_OUT _SFR_IO_ADDR(SDA_PORT)
#define SCL_OUT _SFR_IO_ADDR(SCL_PORT)
#define SDA_IN (_SFR_IO_ADDR(SDA_PORT) - 2)
#define SCL_IN (_SFR_IO_ADDR(SCL_PORT) - 2)


#ifndef __tmp_reg__
#define __tmp_reg__ 0
#endif


.section .text

;*************************************************************************
; delay half period
; For I2C in normal mode (100kHz), use T/2 > 5us
; For I2C in fast mode (400kHz), use T/2 > 1.25us
;*************************************************************************
.stabs "",100,0,0,i2c_delay_T2
.stabs "i2cmaster.S",100,0,0,i2c_delay_T2
.func i2c_delay_T2 ; delay 5.0 microsec with 4 Mhz crystal
i2c_delay_T2: ; 3 cycles
#if F_CPU <= 4000000UL
rjmp 1f ; 2 "
1: rjmp 2f ; 2 "
2: rjmp 3f ; 2 "
3: rjmp 4f ; 2 "
4: rjmp 5f ; 2 "
5: rjmp 6f ; 2 "
6: nop ; 1 "
ret ; 4 " total 20 cyles = 5.0 microsec with 4 Mhz crystal
#elif F_CPU <= 8000000UL
push r24 ; 2 cycle
ldi r24, 7 ; 1 cycle
nop ; 1 cycle
1: sbiw r24, 1 ; 2 cycle
brne 1b ; 2 or 1 cycle, 4 cycles per loop
pop r24 ; 2 ycle
ret ; 4 cycle = total 60 cycles = 5.0 microsec with 12 Mhz crystal
#elif F_CPU <= 12000000UL
push r24 ; 2 cycle
ldi r24, 12 ; 1 cycle
nop ; 1 cycle
1: sbiw r24, 1 ; 2 cycle
brne 1b ; 2 or 1 cycle, 4 cycles per loop
pop r24 ; 2 ycle
ret ; 4 cycle = total 60 cycles = 5.0 microsec with 12 Mhz crystal
#elif F_CPU <= 16000000UL
push r24 ; 2 cycle
ldi r24, 17 ; 1 cycle
nop ; 1 cycle
1: sbiw r24, 1 ; 2 cycle
brne 1b ; 2 or 1 cycle, 4 cycles per loop
pop r24 ; 2 ycle
ret ; 4 cycle = total 80 cycles = 5.0 microsec with 16 Mhz crystal
#else
push r24 ; 2 cycle
ldi r24, 22 ; 1 cycle
nop ; 1 cycle
1: sbiw r24, 1 ; 2 cycle
brne 1b ; 2 or 1 cycle, 4 cycles per loop
pop r24 ; 2 ycle
ret ; 4 cycle = total 100 cycles = 5.0 microsec with 20 Mhz crystal
#endif
.endfunc ;


;*************************************************************************
; Initialization of the I2C bus interface. Need to be called only once
;
; extern void i2c_init(void)
;*************************************************************************
.global i2c_init
.func i2c_init
i2c_init:
cbi SDA_DDR,SDA ;release SDA
cbi SCL_DDR,SCL ;release SCL
cbi SDA_OUT,SDA
cbi SCL_OUT,SCL
ret
.endfunc


;*************************************************************************
; Issues a start condition and sends address and transfer direction.
; return 0 = device accessible, 1= failed to access device
;
; extern unsigned char i2c_start(unsigned char addr);
; addr = r24, return = r25(=0):r24
;*************************************************************************

.global i2c_start
.func i2c_start
i2c_start:
sbi SDA_DDR,SDA ;force SDA low
rcall i2c_delay_T2 ;delay T/2
rcall i2c_write ;write address
ret
.endfunc


;*************************************************************************
; Issues a repeated start condition and sends address and transfer direction.
; return 0 = device accessible, 1= failed to access device
;
; extern unsigned char i2c_rep_start(unsigned char addr);
; addr = r24, return = r25(=0):r24
;*************************************************************************

.global i2c_rep_start
.func i2c_rep_start
i2c_rep_start:
sbi SCL_DDR,SCL ;force SCL low
rcall i2c_delay_T2 ;delay T/2
cbi SDA_DDR,SDA ;release SDA
rcall i2c_delay_T2 ;delay T/2
cbi SCL_DDR,SCL ;release SCL
rcall i2c_delay_T2 ;delay T/2
sbi SDA_DDR,SDA ;force SDA low
rcall i2c_delay_T2 ;delay T/2
rcall i2c_write ;write address
ret
.endfunc


;*************************************************************************
; Issues a start condition and sends address and transfer direction.
; If device is busy, use ack polling to wait until device is ready
;
; extern void i2c_start_wait(unsigned char addr);
; addr = r24
;*************************************************************************

.global i2c_start_wait
.func i2c_start_wait
i2c_start_wait:
mov __tmp_reg__,r24
i2c_start_wait1:
sbi SDA_DDR,SDA ;force SDA low
rcall i2c_delay_T2 ;delay T/2
mov r24,__tmp_reg__
rcall i2c_write ;write address
tst r24 ;if device not busy -> done
breq i2c_start_wait_done
rcall i2c_stop ;terminate write operation
rjmp i2c_start_wait1 ;device busy, poll ack again
i2c_start_wait_done:
ret
.endfunc


;*************************************************************************
; Terminates the data transfer and releases the I2C bus
;
; extern void i2c_stop(void)
;*************************************************************************

.global i2c_stop
.func i2c_stop
i2c_stop:
sbi SCL_DDR,SCL ;force SCL low
sbi SDA_DDR,SDA ;force SDA low
rcall i2c_delay_T2 ;delay T/2
cbi SCL_DDR,SCL ;release SCL
rcall i2c_delay_T2 ;delay T/2
cbi SDA_DDR,SDA ;release SDA
rcall i2c_delay_T2 ;delay T/2
ret
.endfunc


;*************************************************************************
; Send one byte to I2C device
; return 0 = write successful, 1 = write failed
;
; extern unsigned char i2c_write( unsigned char data );
; data = r24, return = r25(=0):r24
;*************************************************************************
.global i2c_write
.func i2c_write
i2c_write:
sec ;set carry flag
rol r24 ;shift in carry and out bit one
rjmp i2c_write_first
i2c_write_bit:
lsl r24 ;if transmit register empty
i2c_write_first:
breq i2c_get_ack
sbi SCL_DDR,SCL ;force SCL low
brcc i2c_write_low
nop
cbi SDA_DDR,SDA ;release SDA
rjmp i2c_write_high
i2c_write_low:
sbi SDA_DDR,SDA ;force SDA low
rjmp i2c_write_high
i2c_write_high:
rcall i2c_delay_T2 ;delay T/2
cbi SCL_DDR,SCL ;release SCL
rcall i2c_delay_T2 ;delay T/2
rjmp i2c_write_bit
i2c_get_ack:
sbi SCL_DDR,SCL ;force SCL low
cbi SDA_DDR,SDA ;release SDA
rcall i2c_delay_T2 ;delay T/2
cbi SCL_DDR,SCL ;release SCL
i2c_ack_wait:
sbis SCL_IN,SCL ;wait SCL high (in case wait states are inserted)
rjmp i2c_ack_wait
clr r24 ;return 0
sbic SDA_IN,SDA ;if SDA high -> return 1
ldi r24,1
rcall i2c_delay_T2 ;delay T/2
clr r25
ret
.endfunc



;*************************************************************************
; read one byte from the I2C device, send ack or nak to device
; (ack=1, send ack, request more data from device
; ack=0, send nak, read is followed by a stop condition)
;
; extern unsigned char i2c_read(unsigned char ack);
; ack = r24, return = r25(=0):r24
; extern unsigned char i2c_readAck(void);
; extern unsigned char i2c_readNak(void);
; return = r25(=0):r24
;*************************************************************************
.global i2c_readAck
.global i2c_readNak
.global i2c_read
.func i2c_read
i2c_readNak:
clr r24
rjmp i2c_read
i2c_readAck:
ldi r24,0x01
i2c_read:
ldi r23,0x01 ;data = 0x01
i2c_read_bit:
sbi SCL_DDR,SCL ;force SCL low
cbi SDA_DDR,SDA ;release SDA (from previous ACK)
rcall i2c_delay_T2 ;delay T/2
cbi SCL_DDR,SCL ;release SCL
rcall i2c_delay_T2 ;delay T/2
i2c_read_stretch:
sbis SCL_IN, SCL ;loop until SCL is high (allow slave to stretch SCL)
rjmp i2c_read_stretch
clc ;clear carry flag
sbic SDA_IN,SDA ;if SDA is high
sec ; set carry flag
rol r23 ;store bit
brcc i2c_read_bit ;while receive register not full
i2c_put_ack:
sbi SCL_DDR,SCL ;force SCL low
cpi r24,1
breq i2c_put_ack_low ;if (ack=0)
cbi SDA_DDR,SDA ; release SDA
rjmp i2c_put_ack_high
i2c_put_ack_low: ;else
sbi SDA_DDR,SDA ; force SDA low
i2c_put_ack_high:
rcall i2c_delay_T2 ;delay T/2
cbi SCL_DDR,SCL ;release SCL
i2c_put_ack_wait:
sbis SCL_IN,SCL ;wait SCL high
rjmp i2c_put_ack_wait
rcall i2c_delay_T2 ;delay T/2
mov r24,r23
clr r25
ret
.endfunc


+ 176
- 0
reform2-trackball-fw/i2cmaster/i2cmaster.h View File

@@ -0,0 +1,176 @@
#ifndef _I2CMASTER_H
#define _I2CMASTER_H
/*************************************************************************
* Title: C include file for the I2C master interface
* (i2cmaster.S or twimaster.c)
* Author: Peter Fleury <pfleury@gmx.ch>
* File: $Id: i2cmaster.h,v 1.12 2015/09/16 09:27:58 peter Exp $
* Software: AVR-GCC 4.x
* Target: any AVR device
* Usage: see Doxygen manual
**************************************************************************/

/**
@file
@defgroup pfleury_ic2master I2C Master library
@code #include <i2cmaster.h> @endcode
@brief I2C (TWI) Master Software Library

Basic routines for communicating with I2C slave devices. This single master
implementation is limited to one bus master on the I2C bus.

This I2c library is implemented as a compact assembler software implementation of the I2C protocol
which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c).
Since the API for these two implementations is exactly the same, an application can be linked either against the
software I2C implementation or the hardware I2C implementation.

Use 4.7k pull-up resistor on the SDA and SCL pin.
Adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module
i2cmaster.S to your target when using the software I2C implementation !
Adjust the CPU clock frequence F_CPU in twimaster.c or in the Makfile when using the TWI hardware implementaion.

@note
The module i2cmaster.S is based on the Atmel Application Note AVR300, corrected and adapted
to GNU assembler and AVR-GCC C call interface.
Replaced the incorrect quarter period delays found in AVR300 with
half period delays.
@author Peter Fleury pfleury@gmx.ch http://tinyurl.com/peterfleury
@copyright (C) 2015 Peter Fleury, GNU General Public License Version 3
@par API Usage Example
The following code shows typical usage of this library, see example test_i2cmaster.c

@code

#include <i2cmaster.h>


int main(void)
{
unsigned char ret;

i2c_init(); // initialize I2C library

// write 0x75 to EEPROM address 5 (Byte Write)
i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
i2c_write(0x05); // write address = 5
i2c_write(0x75); // write value 0x75 to EEPROM
i2c_stop(); // set stop conditon = release bus


// read previously written value back from EEPROM address 5
i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode

i2c_write(0x05); // write address = 5
i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode

ret = i2c_readNak(); // read one byte from EEPROM
i2c_stop();

for(;;);
}
@endcode

*/


/**@{*/

#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
#endif

#include <avr/io.h>

/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
#define I2C_READ 1

/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
#define I2C_WRITE 0


/**
@brief initialize the I2C master interace. Need to be called only once
@return none
*/
extern void i2c_init(void);


/**
@brief Terminates the data transfer and releases the I2C bus
@return none
*/
extern void i2c_stop(void);


/**
@brief Issues a start condition and sends address and transfer direction
@param addr address and transfer direction of I2C device
@retval 0 device accessible
@retval 1 failed to access device
*/
extern unsigned char i2c_start(unsigned char addr);


/**
@brief Issues a repeated start condition and sends address and transfer direction

@param addr address and transfer direction of I2C device
@retval 0 device accessible
@retval 1 failed to access device
*/
extern unsigned char i2c_rep_start(unsigned char addr);


/**
@brief Issues a start condition and sends address and transfer direction
If device is busy, use ack polling to wait until device ready
@param addr address and transfer direction of I2C device
@return none
*/
extern void i2c_start_wait(unsigned char addr);

/**
@brief Send one byte to I2C device
@param data byte to be transfered
@retval 0 write successful
@retval 1 write failed
*/
extern unsigned char i2c_write(unsigned char data);


/**
@brief read one byte from the I2C device, request more data from device
@return byte read from I2C device
*/
extern unsigned char i2c_readAck(void);

/**
@brief read one byte from the I2C device, read is followed by a stop condition
@return byte read from I2C device
*/
extern unsigned char i2c_readNak(void);

/**
@brief read one byte from the I2C device
Implemented as a macro, which calls either @ref i2c_readAck or @ref i2c_readNak
@param ack 1 send ack, request more data from device<br>
0 send nak, read is followed by a stop condition
@return byte read from I2C device
*/
extern unsigned char i2c_read(unsigned char ack);
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();



/**@}*/
#endif

+ 330
- 0
reform2-trackball-fw/i2cmaster/makefile.i2cmaster View File

@@ -0,0 +1,330 @@
# ----------------------------------------------------------------------------
# Makefile to compile and link the I2C Master library and test program
# Author: Peter Fleury
# File: $Id: makefile.i2cmaster,v 1.3 2015/09/16 11:30:30 peter Exp $
#
# Adjust MCU and F_CPU below to your AVR target
#----------------------------------------------------------------------------
# usage:
#
# make = build software
# make clean = Clean out built project files
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#
# make filename.s = Just compile filename.c into the assembler code only.
# make filename.i = Create a preprocessed source file
#
# To rebuild project do "make clean" then "make"
#----------------------------------------------------------------------------
# MCU name
MCU = attiny2313a
# Processor frequency.
# This will define a symbol, F_CPU, in all source code files equal to the
# processor frequency. You can then use this symbol in your source code to
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
# automatically to create a 32-bit value in your source code.
F_CPU = 4000000
# Target file name (without extension).
TARGET = test_i2cmaster
# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c
# List Assembler source files here.
# Make them always end in a capital .S. Files ending in a lowercase .s
# will not be considered source files but generated files (assembler
# output from the compiler), and will be deleted upon "make clean"!
# Even though the DOS/Win* filesystem matches both .s and .S the same,
# it will preserve the spelling of the filenames, and gcc itself does
# care about how the name is spelled on its command-line.
ASRC = i2cmaster.S
# List any extra directories to look for include files here.
# Each directory must be seperated by a space.
# Use forward slashes for directory separators.
# For a directory that has spaces, enclose it in quotes.
EXTRAINCDIRS =
#specify alternate location of sources files
# (unlike VPATH= which is a search path for all prerequisites, not just source files)
#vpath %.c /<directory>/
#vpath %.S /<directory>/
# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
# Place -D or -U options here
CDEFS =
# Place -I options here
CINCS =
#---------------- Compiler Options ----------------
# -gdwarf-2: generate debugging information
# -O*: optimization level
# -f...: tuning, see GCC manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns...: create assembler listing
CFLAGS = -DF_CPU=$(F_CPU)UL
CFLAGS += $(CDEFS) $(CINCS)
CFLAGS += -I. $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += -std=gnu99
CFLAGS += -gdwarf-2
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -Wall -Wstrict-prototypes
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
CFLAGS += -save-temps
#---------------- Assembler Options ----------------
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns=$(<:.S=.lst) create listing
# -gdwarf-2: generate debugging information
ASFLAGS = -DF_CPU=$(F_CPU)UL -x assembler-with-cpp -Wa,-gdwarf2
#---------------- Library Options ----------------
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB =
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)
MATH_LIB = -lm
#---------------- External Memory Options ----------------
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
EXTMEMOPTS =
#---------------- Linker Options ----------------
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(EXTMEMOPTS)
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#---------------- Programming Options (avrdude) ----------------
# Programming hardware: Type: avrdude -c ? to get a full listing.
AVRDUDE_PROGRAMMER = usbasp
# usb, com1 = serial port, lpt1 = parallel port
AVRDUDE_PORT = USB
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
# Uncomment the following if you do /not/ wish a verification to be performed after programming the device.
#AVRDUDE_NO_VERIFY = -V
# Disable save mode for fuses
AVRDUDE_NO_SAFEMODE = -u
# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
#AVRDUDE_ERASE_COUNTER = -y
# Increase verbosity level.
#AVRDUDE_VERBOSE = -v -v
# Adjust programming speed of USBasp
# no -B switch 375khz (default)
# -B 2000 500 hz
# -B 1000 1khz
# -B 500 2khz
# -B 250 4khz
# -B 125 8 khz
# -B 62 16khz
# -B 31 32khz * the cutoff for bit banged isp
# -B 10 93.75 khz
# -B 5 187.5 khz
# -B 2 375 khz
# -B 1 750 khz
# -B .5 1.5mhz
#AVRDUDE_SPEED = -B .5
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_NO_SAFEMODE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
AVRDUDE_FLAGS += $(AVRDUDE_SPEED)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
#============================================================================
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Define programs and commands.
#SHELL = win-bash
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -rf
COPY = cp
WINSHELL = cmd
# Define Messages
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:
# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
# Define all listing files.
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst)
# Compiler flags to generate dependency files.
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. $(ASFLAGS)
# Default target.
all: gccversion $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).lss $(TARGET).sym size
# Display compiler version information.
gccversion :
@echo $(OBJ1)
@$(CC) --version
# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
%.eep: %.elf
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
# Create extended listing file from ELF output file.
%.lss: %.elf
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S $< > $@
# Create a symbol table from ELF output file.
%.sym: %.elf
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
@echo $(MSG_LINKING) $@
$(CC) -mmcu=$(MCU) $(LDFLAGS) $^ --output $(@F)
# Compile: create object files from C source files.
%.o : %.c
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $(@F)
# Compile: create assembler files from C source files.
%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $(@F)
# Assemble: create object files from assembler source files.
%.o : %.S
@echo $(MSG_ASSEMBLING) $<
$(CC) -c $(ALL_ASFLAGS) $< -o $(@F)
# Create preprocessed source for use in sending a bug report.
%.i : %.c
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $(@F)
# Display size of file.
size: ${TARGET}.elf
@avr-size -C --mcu=${MCU} ${TARGET}.elf
# Program the device.
program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
# Delete all generated files.
clean:
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf $(TARGET).map $(TARGET).sym $(TARGET).lss $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(SRC:.c=.i) .dep/
# Include the dependency files.
#-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
-include $(shell mkdir .dep 2>NUL) $(wildcard .dep/*)
# Listing of phony targets.
.PHONY : all size gccversion clean program

+ 330
- 0
reform2-trackball-fw/i2cmaster/makefile.twimaster View File

@@ -0,0 +1,330 @@
# ----------------------------------------------------------------------------
# Makefile to compile and link the TWI master library and test program
# Author: Peter Fleury
# File: $Id: makefile.twimaster,v 1.2 2015/09/16 13:09:15 peter Exp $
#
# Adjust MCU and F_CPU below to your AVR target
#----------------------------------------------------------------------------
# usage:
#
# make = build software
# make clean = Clean out built project files
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#
# make filename.s = Just compile filename.c into the assembler code only.
# make filename.i = Create a preprocessed source file
#
# To rebuild project do "make clean" then "make"
#----------------------------------------------------------------------------
# MCU name
MCU = atmega8
# Processor frequency.
# This will define a symbol, F_CPU, in all source code files equal to the
# processor frequency. You can then use this symbol in your source code to
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
# automatically to create a 32-bit value in your source code.
F_CPU = 4000000
# Target file name (without extension).
TARGET = test_i2cmaster
# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c twimaster.c
# List Assembler source files here.
# Make them always end in a capital .S. Files ending in a lowercase .s
# will not be considered source files but generated files (assembler
# output from the compiler), and will be deleted upon "make clean"!
# Even though the DOS/Win* filesystem matches both .s and .S the same,
# it will preserve the spelling of the filenames, and gcc itself does
# care about how the name is spelled on its command-line.
ASRC =
# List any extra directories to look for include files here.
# Each directory must be seperated by a space.
# Use forward slashes for directory separators.
# For a directory that has spaces, enclose it in quotes.
EXTRAINCDIRS =
#specify alternate location of sources files
# (unlike VPATH= which is a search path for all prerequisites, not just source files)
#vpath %.c /<directory>/
#vpath %.S /<directory>/
# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
# Place -D or -U options here
CDEFS =
# Place -I options here
CINCS =
#---------------- Compiler Options ----------------
# -gdwarf-2: generate debugging information
# -O*: optimization level
# -f...: tuning, see GCC manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns...: create assembler listing
CFLAGS = -DF_CPU=$(F_CPU)UL
CFLAGS += $(CDEFS) $(CINCS)
CFLAGS += -I. $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += -std=gnu99
CFLAGS += -gdwarf-2
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -Wall -Wstrict-prototypes
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
CFLAGS += -save-temps
#---------------- Assembler Options ----------------
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns=$(<:.S=.lst) create listing
# -gdwarf-2: generate debugging information
ASFLAGS = -DF_CPU=$(F_CPU)UL -x assembler-with-cpp -Wa,-gdwarf2
#---------------- Library Options ----------------
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB =
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)
MATH_LIB = -lm
#---------------- External Memory Options ----------------
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
EXTMEMOPTS =
#---------------- Linker Options ----------------
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(EXTMEMOPTS)
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#---------------- Programming Options (avrdude) ----------------
# Programming hardware: Type: avrdude -c ? to get a full listing.
AVRDUDE_PROGRAMMER = usbasp
# usb, com1 = serial port, lpt1 = parallel port
AVRDUDE_PORT = USB
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
# Uncomment the following if you do /not/ wish a verification to be performed after programming the device.
#AVRDUDE_NO_VERIFY = -V
# Disable save mode for fuses
AVRDUDE_NO_SAFEMODE = -u
# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
#AVRDUDE_ERASE_COUNTER = -y
# Increase verbosity level.
#AVRDUDE_VERBOSE = -v -v
# Adjust programming speed of USBasp
# no -B switch 375khz (default)
# -B 2000 500 hz
# -B 1000 1khz
# -B 500 2khz
# -B 250 4khz
# -B 125 8 khz
# -B 62 16khz
# -B 31 32khz * the cutoff for bit banged isp
# -B 10 93.75 khz
# -B 5 187.5 khz
# -B 2 375 khz
# -B 1 750 khz
# -B .5 1.5mhz
#AVRDUDE_SPEED = -B .5
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_NO_SAFEMODE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
AVRDUDE_FLAGS += $(AVRDUDE_SPEED)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
#============================================================================
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Define programs and commands.
#SHELL = win-bash
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -rf
COPY = cp
WINSHELL = cmd
# Define Messages
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:
# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
# Define all listing files.
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst)
# Compiler flags to generate dependency files.
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. $(ASFLAGS)
# Default target.
all: gccversion $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).lss $(TARGET).sym size
# Display compiler version information.
gccversion :
@echo $(OBJ1)
@$(CC) --version