1
0

Mouse.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include <avr/pgmspace.h>
  27. #include <avr/io.h>
  28. #include <stdlib.h>
  29. #include "i2cmaster/i2cmaster.h"
  30. #define output_low(port,pin) port &= ~(1<<pin)
  31. #define output_high(port,pin) port |= (1<<pin)
  32. #define set_input(portdir,pin) portdir &= ~(1<<pin)
  33. #define set_output(portdir,pin) portdir |= (1<<pin)
  34. // Registers
  35. #define Product_ID 0x00
  36. #define Revision_ID 0x01
  37. #define Motion 0x02
  38. #define Delta_X_L 0x03
  39. #define Delta_X_H 0x04
  40. #define Delta_Y_L 0x05
  41. #define Delta_Y_H 0x06
  42. #define SQUAL 0x07
  43. #define Raw_Data_Sum 0x08
  44. #define Maximum_Raw_data 0x09
  45. #define Minimum_Raw_data 0x0A
  46. #define Shutter_Lower 0x0B
  47. #define Shutter_Upper 0x0C
  48. #define Control 0x0D
  49. #define Config1 0x0F
  50. #define Config2 0x10
  51. #define Angle_Tune 0x11
  52. #define Frame_Capture 0x12
  53. #define SROM_Enable 0x13
  54. #define Run_Downshift 0x14
  55. #define Rest1_Rate_Lower 0x15
  56. #define Rest1_Rate_Upper 0x16
  57. #define Rest1_Downshift 0x17
  58. #define Rest2_Rate_Lower 0x18
  59. #define Rest2_Rate_Upper 0x19
  60. #define Rest2_Downshift 0x1A
  61. #define Rest3_Rate_Lower 0x1B
  62. #define Rest3_Rate_Upper 0x1C
  63. #define Observation 0x24
  64. #define Data_Out_Lower 0x25
  65. #define Data_Out_Upper 0x26
  66. #define Raw_Data_Dump 0x29
  67. #define SROM_ID 0x2A
  68. #define Min_SQ_Run 0x2B
  69. #define Raw_Data_Threshold 0x2C
  70. #define Config5 0x2F
  71. #define Power_Up_Reset 0x3A
  72. #define Shutdown 0x3B
  73. #define Inverse_Product_ID 0x3F
  74. #define LiftCutoff_Tune3 0x41
  75. #define Angle_Snap 0x42
  76. #define LiftCutoff_Tune1 0x4A
  77. #define Motion_Burst 0x50
  78. #define LiftCutoff_Tune_Timeout 0x58
  79. #define LiftCutoff_Tune_Min_Length 0x5A
  80. #define SROM_Load_Burst 0x62
  81. #define Lift_Config 0x63
  82. #define Raw_Data_Burst 0x64
  83. #define LiftCutoff_Tune2 0x65
  84. uint8_t adns_init_complete=0;
  85. volatile int xydat[2];
  86. //#include "PWM3360DM_srom_0x04.c"
  87. #include "Mouse.h"
  88. /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
  89. static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_WheelMouseReport_Data_t)];
  90. /** LUFA HID Class driver interface configuration and state information. This structure is
  91. * passed to all HID Class driver functions, so that multiple instances of the same class
  92. * within a device can be differentiated from one another.
  93. */
  94. USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
  95. {
  96. .Config =
  97. {
  98. .InterfaceNumber = INTERFACE_ID_Mouse,
  99. .ReportINEndpoint =
  100. {
  101. .Address = MOUSE_EPADDR,
  102. .Size = MOUSE_EPSIZE,
  103. .Banks = 1,
  104. },
  105. .PrevReportINBuffer = PrevMouseHIDReportBuffer,
  106. .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
  107. },
  108. };
  109. void led_error(void) {
  110. DDRC = 0b11110100;
  111. }
  112. void led_ok(void) {
  113. DDRC = 0;
  114. }
  115. // FIXME QUESTIONABLE
  116. int convTwosComp(int b) {
  117. //Convert from 2's complement
  118. if (b & 0x80) {
  119. b = -1 * ((b ^ 0xff) + 1);
  120. }
  121. return b;
  122. }
  123. // LM PD0 input pullup
  124. // RM PD1 input pullup
  125. #define ADDR_SENSOR (0x79<<1)
  126. uint8_t twi_write_reg[1];
  127. uint8_t twi_write_buf[10];
  128. uint8_t twi_read_buf[10];
  129. void SetupHardware(void)
  130. {
  131. /* Disable watchdog if enabled by bootloader/fuses */
  132. MCUSR &= ~(1 << WDRF);
  133. wdt_disable();
  134. // Disable clock division
  135. // this should yield 8Mhz with internal osc
  136. clock_prescale_set(clock_div_1);
  137. DDRD = 0b00000000;
  138. //DDRB = 0b11000110;
  139. DDRC = 0b00000000;
  140. output_high(PORTD, 0); // enable input pullup for LMB
  141. output_high(PORTD, 1); // enable input pullup for RMB
  142. output_high(PORTD, 2); // enable input pullup for RMB
  143. output_high(PORTD, 3); // enable input pullup for RMB
  144. output_high(PORTD, 4); // enable input pullup for RMB
  145. //output_high(PORTC, 5);
  146. // no jtag plox
  147. //MCUCR |=(1<<JTD);
  148. //MCUCR |=(1<<JTD);
  149. //SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
  150. // SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
  151. i2c_init();
  152. USB_Init();
  153. Delay_MS(1000);
  154. led_error();
  155. Delay_MS(1000);
  156. if (!i2c_start(ADDR_SENSOR|I2C_WRITE)) {
  157. i2c_write(0x7f);
  158. i2c_write(0x0);
  159. i2c_stop();
  160. led_ok();
  161. }
  162. if (!i2c_start(ADDR_SENSOR|I2C_WRITE)) {
  163. i2c_write(0x05);
  164. i2c_write(0x01);
  165. i2c_stop();
  166. led_ok();
  167. }
  168. Delay_MS(100);
  169. }
  170. /** Event handler for the library USB Connection event. */
  171. void EVENT_USB_Device_Connect(void)
  172. {
  173. }
  174. /** Event handler for the library USB Disconnection event. */
  175. void EVENT_USB_Device_Disconnect(void)
  176. {
  177. }
  178. /** Event handler for the library USB Configuration Changed event. */
  179. void EVENT_USB_Device_ConfigurationChanged(void)
  180. {
  181. bool ConfigSuccess = true;
  182. ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
  183. USB_Device_EnableSOFEvents();
  184. }
  185. /** Event handler for the library USB Control Request reception event. */
  186. void EVENT_USB_Device_ControlRequest(void)
  187. {
  188. HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
  189. }
  190. /** Event handler for the USB device Start Of Frame event. */
  191. void EVENT_USB_Device_StartOfFrame(void)
  192. {
  193. HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
  194. }
  195. /** HID class driver callback function for the creation of HID reports to the host.
  196. *
  197. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  198. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  199. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  200. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  201. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  202. *
  203. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  204. */
  205. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  206. uint8_t* const ReportID,
  207. const uint8_t ReportType,
  208. void* ReportData,
  209. uint16_t* const ReportSize)
  210. {
  211. if (ReportType==HID_REPORT_ITEM_Feature) return false;
  212. int8_t nx = 0;
  213. int8_t ny = 0;
  214. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  215. i2c_write(0x02);
  216. i2c_rep_start(ADDR_SENSOR|I2C_READ);
  217. uint8_t ret = i2c_readNak();
  218. i2c_stop();
  219. if (ret & 0xf0) {
  220. i2c_start_wait(ADDR_SENSOR|I2C_WRITE);
  221. i2c_write(0x03);
  222. i2c_rep_start(ADDR_SENSOR|I2C_READ);
  223. nx = i2c_readAck();
  224. ny = i2c_readNak();
  225. i2c_stop();
  226. }
  227. led_ok();
  228. USB_WheelMouseReport_Data_t* MouseReport = (USB_WheelMouseReport_Data_t*)ReportData;
  229. if (!(PIND&(1<<4))) {
  230. MouseReport->Button |= 1;
  231. }
  232. if (!(PIND&(1<<3))) {
  233. MouseReport->Button |= 2;
  234. }
  235. if (!(PIND&(1<<1))) {
  236. MouseReport->Button |= 4;
  237. }
  238. if (!(PIND&(1<<2))) {
  239. MouseReport->Button |= 1;
  240. }
  241. MouseReport->Wheel = 0;
  242. if (!(PIND&1) || !(PIND&(1<<2))) {
  243. // wheel
  244. MouseReport->Wheel = -ny;
  245. led_error();
  246. } else {
  247. MouseReport->X = 2*abs(nx)*nx;
  248. MouseReport->Y = 2*abs(ny)*ny;
  249. }
  250. *ReportSize = sizeof(USB_WheelMouseReport_Data_t);
  251. return true;
  252. }
  253. /** HID class driver callback function for the processing of HID reports from the host.
  254. *
  255. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  256. * \param[in] ReportID Report ID of the received report from the host
  257. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  258. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  259. * \param[in] ReportSize Size in bytes of the received HID report
  260. */
  261. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  262. const uint8_t ReportID,
  263. const uint8_t ReportType,
  264. const void* ReportData,
  265. const uint16_t ReportSize)
  266. {
  267. // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
  268. }
  269. int main(void)
  270. {
  271. SetupHardware();
  272. GlobalInterruptEnable();
  273. for (;;)
  274. {
  275. HID_Device_USBTask(&Mouse_HID_Interface);
  276. USB_USBTask();
  277. }
  278. }