1
0

Mouse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <LUFA/Drivers/Peripheral/SPI.h>
  27. #include <avr/pgmspace.h>
  28. #include <avr/io.h>
  29. #include <stdlib.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. // FIXME QUESTIONABLE
  110. int convTwosComp(int b) {
  111. //Convert from 2's complement
  112. if (b & 0x80) {
  113. b = -1 * ((b ^ 0xff) + 1);
  114. }
  115. return b;
  116. }
  117. void adns_com_begin(void) {
  118. // pull chip select low
  119. output_low(PORTB, 7);
  120. }
  121. void adns_com_end(void) {
  122. // pull chip select high
  123. output_high(PORTB, 7);
  124. }
  125. uint8_t adns_read_reg(uint8_t reg_addr) {
  126. adns_com_begin();
  127. // send adress of the register, with MSBit = 0 to indicate it's a read
  128. SPI_SendByte(reg_addr & 0x7f );
  129. _delay_us(100); // tSRAD
  130. // read data
  131. uint8_t data = SPI_ReceiveByte();
  132. _delay_us(1); // tSCLK-NCS for read operation is 120ns
  133. adns_com_end();
  134. _delay_us(19); // tSRW/tSRR (=20us) minus tSCLK-NCS
  135. return data;
  136. }
  137. void adns_write_reg(uint8_t reg_addr, uint8_t data) {
  138. adns_com_begin();
  139. //send adress of the register, with MSBit = 1 to indicate it's a write
  140. SPI_SendByte(reg_addr | 0x80 );
  141. //sent data
  142. SPI_SendByte(data);
  143. _delay_us(20); // tSCLK-NCS for write operation
  144. adns_com_end();
  145. _delay_us(100); // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
  146. }
  147. void adns_upload_firmware(void) {
  148. // send the firmware to the chip, cf p.18 of the datasheet
  149. //Serial.println("Uploading firmware...");
  150. //Write 0 to Rest_En bit of Config2 register to disable Rest mode.
  151. adns_write_reg(Config2, 0x20);
  152. // write 0x1d in SROM_enable reg for initializing
  153. adns_write_reg(SROM_Enable, 0x1d);
  154. // wait for more than one frame period
  155. Delay_MS(10); // assume that the frame rate is as low as 100fps... even if it should never be that low
  156. // write 0x18 to SROM_enable to start SROM download
  157. adns_write_reg(SROM_Enable, 0x18);
  158. // write the SROM file (=firmware data)
  159. adns_com_begin();
  160. SPI_SendByte(SROM_Load_Burst | 0x80); // write burst destination adress
  161. _delay_us(15);
  162. // send all bytes of the firmware
  163. uint8_t c;
  164. for (int i = 0; i < firmware_length; i++) {
  165. c = (uint8_t)pgm_read_byte(firmware_data + i);
  166. SPI_SendByte(c);
  167. _delay_us(15);
  168. }
  169. //Read the SROM_ID register to verify the ID before any other register reads or writes.
  170. adns_read_reg(SROM_ID);
  171. //Write 0x00 to Config2 register for wired mouse or 0x20 for wireless mouse design.
  172. adns_write_reg(Config2, 0x00);
  173. // set initial CPI resolution
  174. // 0x01: 200
  175. // 0x02: 300
  176. // 0x31: 5000
  177. // 0x77: 12000
  178. adns_write_reg(Config1, 0x04);
  179. adns_write_reg(Angle_Snap, 0x0);
  180. adns_com_end();
  181. }
  182. void adns_startup(void) {
  183. adns_com_end(); // ensure that the serial port is reset
  184. adns_com_begin();
  185. adns_com_end();
  186. adns_write_reg(Power_Up_Reset, 0x5a); // force reset
  187. Delay_MS(50); // wait for it to reboot
  188. // read registers 0x02 to 0x06 (and discard the data)
  189. adns_read_reg(Motion);
  190. adns_read_reg(Delta_X_L);
  191. adns_read_reg(Delta_X_H);
  192. adns_read_reg(Delta_Y_L);
  193. adns_read_reg(Delta_Y_H);
  194. // upload the firmware
  195. adns_upload_firmware();
  196. Delay_MS(10);
  197. //Serial.println("Optical Chip Initialized");
  198. adns_init_complete = 1;
  199. }
  200. // SS PB7 output
  201. // MT PB5 input
  202. // SC PB1 output
  203. // MO PB2 output
  204. // MI PB3 input
  205. // RS PB6 output
  206. // LM PD0 input pullup
  207. // RM PD1 input pullup
  208. void SetupHardware(void)
  209. {
  210. //#if (ARCH == ARCH_AVR8)
  211. /* Disable watchdog if enabled by bootloader/fuses */
  212. MCUSR &= ~(1 << WDRF);
  213. wdt_disable();
  214. /* Disable clock division */
  215. clock_prescale_set(clock_div_1);
  216. DDRD = 0b00000000;
  217. DDRB = 0b11000110;
  218. output_high(PORTD, 0); // enable input pullup for LMB
  219. output_high(PORTD, 1); // enable input pullup for RMB
  220. output_high(PORTB, 6); // disable adns reset
  221. // no jtag plox
  222. //MCUCR |=(1<<JTD);
  223. //MCUCR |=(1<<JTD);
  224. SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
  225. SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
  226. adns_startup();
  227. USB_Init();
  228. }
  229. /** Event handler for the library USB Connection event. */
  230. void EVENT_USB_Device_Connect(void)
  231. {
  232. }
  233. /** Event handler for the library USB Disconnection event. */
  234. void EVENT_USB_Device_Disconnect(void)
  235. {
  236. }
  237. /** Event handler for the library USB Configuration Changed event. */
  238. void EVENT_USB_Device_ConfigurationChanged(void)
  239. {
  240. bool ConfigSuccess = true;
  241. ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
  242. USB_Device_EnableSOFEvents();
  243. }
  244. /** Event handler for the library USB Control Request reception event. */
  245. void EVENT_USB_Device_ControlRequest(void)
  246. {
  247. HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
  248. }
  249. /** Event handler for the USB device Start Of Frame event. */
  250. void EVENT_USB_Device_StartOfFrame(void)
  251. {
  252. HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
  253. }
  254. char wheelmode = 0;
  255. /** HID class driver callback function for the creation of HID reports to the host.
  256. *
  257. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  258. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  259. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  260. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  261. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  262. *
  263. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  264. */
  265. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  266. uint8_t* const ReportID,
  267. const uint8_t ReportType,
  268. void* ReportData,
  269. uint16_t* const ReportSize)
  270. {
  271. if (ReportType==HID_REPORT_ITEM_Feature) return false;
  272. //USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
  273. USB_WheelMouseReport_Data_t* MouseReport = (USB_WheelMouseReport_Data_t*)ReportData;
  274. int nx = 0;
  275. int ny = 0;
  276. if (adns_init_complete) {
  277. //write 0x01 to Motion register and read from it to freeze the motion values and make them available
  278. adns_write_reg(Motion, 0x01);
  279. adns_read_reg(Motion);
  280. // x and y are swapped
  281. nx = convTwosComp((int)adns_read_reg(Delta_Y_L));
  282. ny = convTwosComp((int)adns_read_reg(Delta_X_L));
  283. }
  284. if (!(PIND&1) && !(PIND&2)) {
  285. if (wheelmode==1) {
  286. wheelmode=2;
  287. }
  288. }
  289. else if (!(PIND&1)) {
  290. if (wheelmode<2) {
  291. MouseReport->Button |= 1;
  292. }
  293. if (wheelmode==0) {
  294. wheelmode=1;
  295. }
  296. else if (wheelmode==3 && (PIND&2)) {
  297. wheelmode=0;
  298. }
  299. }
  300. else if (!(PIND&2)) {
  301. if (wheelmode==0) {
  302. MouseReport->Button |= 2;
  303. }
  304. else if (wheelmode==3) {
  305. MouseReport->Button |= 4;
  306. }
  307. } else {
  308. if (wheelmode<2) wheelmode=0;
  309. if (wheelmode==2) wheelmode=3;
  310. }
  311. //if ((nx!=0 || ny!=0) && (wheelmode>0 && wheelmode<3)) wheelmode=0;
  312. MouseReport->Wheel = 0;
  313. if (wheelmode<2) {
  314. MouseReport->X = nx;
  315. MouseReport->Y = ny;
  316. } else if (wheelmode>=3) {
  317. MouseReport->Wheel = -ny/3;
  318. }
  319. *ReportSize = sizeof(USB_WheelMouseReport_Data_t);
  320. return true;
  321. }
  322. /** HID class driver callback function for the processing of HID reports from the host.
  323. *
  324. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  325. * \param[in] ReportID Report ID of the received report from the host
  326. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  327. * \param[in] ReportData Pointer to a buffer where the received report has been stored
  328. * \param[in] ReportSize Size in bytes of the received HID report
  329. */
  330. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  331. const uint8_t ReportID,
  332. const uint8_t ReportType,
  333. const void* ReportData,
  334. const uint16_t ReportSize)
  335. {
  336. // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
  337. }
  338. int main(void)
  339. {
  340. SetupHardware();
  341. GlobalInterruptEnable();
  342. for (;;)
  343. {
  344. HID_Device_USBTask(&Mouse_HID_Interface);
  345. USB_USBTask();
  346. }
  347. }