Advertisement
mainManTarkin

i2c usb hid Eshifter code

Jan 16th, 2023 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.82 KB | Source Code | 0 0
  1. /*
  2. * main.asm
  3. *
  4. * Created: 1/10/2023 4:13:35 PM
  5. * Author: Brandon Davis
  6. */
  7.  
  8. /* Used Pins
  9. -----------------------------------------------------------
  10. ATMEGA used pins
  11.  
  12. PORTB - used for shifting
  13.  
  14. PORTC -
  15. pc5 (pin 28) used for TWI SCL
  16. pc4 (pin 27) used for TWO SDA
  17. pc0 (pin 23) used for level button
  18. pc1 (pin 24) used for shifter button
  19.  
  20. button change are detected using pin change intrupt
  21.  
  22. PORTD -
  23. PD2 (pin 4) used for external intrupt for shift change
  24. PD3 (pin 5) used for external intrupt for test light
  25. PD0 (pin 2) used for gpio intrupt for HID
  26. PD1 (pin 3) used to drive test LED
  27.  
  28. lfuse:E2
  29. hfuse:D7
  30. ---------------------------------------------------------------------
  31. */
  32.  
  33.  
  34. //
  35. //---------------------------------------------------------------------------
  36. //
  37. // =================Main.asm file=====================
  38. //
  39. //---------------------------------------------------------------------------
  40. //
  41.  
  42. vectorTable:
  43. rjmp start ; Reset Handler
  44. rjmp shiftStateChange ; EXTINT0 IRQ0 Handler
  45. rjmp testButton ; EXTINT1 IRQ1 Handler
  46. nop ; PCINT0 Handler
  47. rjmp buttonStateChange ; PCINT1 Handler
  48. nop ; PCINT2 Handler
  49. nop ; Watchdog Timer Handler
  50. nop ; Timer2 Compare A Handler
  51. nop ; Timer2 Compare B Handler
  52. nop ; Timer2 Overflow Handler
  53. nop ; Timer1 Capture Handler
  54. nop ; Timer1 Compare A Handler
  55. nop ; Timer1 Compare B Handler
  56. nop ; Timer1 Overflow Handler
  57. nop ; Timer0 Compare A Handler
  58. nop ; Timer0 Compare B Handler
  59. nop ; Timer0 Overflow Handler
  60. nop ; SPI Transfer Complete Handler
  61. nop ; USART, RX Complete Handler
  62. nop ; USART, UDR Empty Handler
  63. nop ; USART, TX Complete Handler
  64. nop ; ADC Conversion Complete Handler
  65. nop ; EEPROM Ready Handler
  66. nop ; Analog Comparator Handler
  67. rjmp usbTWI ; 2-wire Serial Interface Handler
  68.  
  69. ;end of vectorTable
  70. ;start IRQH
  71.  
  72. testButton:
  73.  
  74. push r30
  75. push r31
  76. push r24
  77.  
  78. ldi r30, eventLoopLow
  79. ldi r31, eventLoopHigh
  80.  
  81. ld r24, z
  82.  
  83. ori r24, addTestFlagBit
  84.  
  85. st z, r24
  86.  
  87. pop r24
  88. pop r31
  89. pop r30
  90.  
  91. reti
  92.  
  93. buttonStateChange:
  94.  
  95. push r30
  96. push r31
  97. push r24
  98.  
  99. ldi r30, eventLoopLow
  100. ldi r31, eventLoopHigh
  101.  
  102. ld r24, z
  103.  
  104. ori r24, addShiftFlagBit
  105.  
  106. st z, r24
  107.  
  108. pop r24
  109. pop r31
  110. pop r30
  111.  
  112. reti
  113.  
  114. shiftStateChange:
  115.  
  116. push r30
  117. push r31
  118. push r24
  119.  
  120. ldi r30, eventLoopLow
  121. ldi r31, eventLoopHigh
  122.  
  123. ld r24, z
  124.  
  125. ori r24, addShiftFlagBit
  126.  
  127. st z, r24
  128.  
  129. pop r24
  130. pop r31
  131. pop r30
  132.  
  133. reti
  134.  
  135. usbTWI:
  136.  
  137. push r30
  138. push r31
  139. push r24
  140.  
  141. ldi r30, eventLoopLow
  142. ldi r31, eventLoopHigh
  143.  
  144. ld r24, z
  145.  
  146. ori r24, addTwiEventFlagBit
  147.  
  148. st z, r24
  149.  
  150. ldi r30, TWCR
  151. clr r31
  152. ld r24, z
  153. andi r24, 0x44 ; disables twi intrupt
  154. st z, r24
  155.  
  156. pop r24
  157. pop r31
  158. pop r30
  159.  
  160. reti
  161.  
  162. ;end of IRQH
  163.  
  164. start: ; Main program start
  165. //intialize stack pointer
  166. ldi r16, high(RAMEND)
  167. out SPH,r16
  168. ldi r16, low(RAMEND)
  169. out SPL,r16
  170.  
  171. //clear zero register
  172. clr r1
  173.  
  174. //start delay
  175. ldi r22, 0x9A;----------------------------------
  176. ldi r23, 0x02; word ≈ 1 millisec
  177. ldi r24, 0x00;
  178. ldi r25, 0x00;--------------------------------
  179.  
  180. rcall delayLoop
  181.  
  182. //set up i/o DD Registers
  183.  
  184. ldi r24, ddrPortCMask
  185. ldi r22, ddrPortDMask
  186.  
  187. out DDRC, r24
  188. out DDRD, r22
  189.  
  190. //set up extint 0
  191.  
  192. ldi r30, EICRA
  193. clr r31
  194. ldi r24, anyLogicalChangeBitSet
  195. st z, r24
  196.  
  197. ldi r24, enableExInt0BitSet
  198. out EIMSK, r24
  199.  
  200. //set up PCI1
  201. ldi r30, PCICR
  202. ldi r24, enablePCI1BitSet
  203. st z, r24 ; turn on PCI1
  204.  
  205. ldi r30, PCMSK1
  206. ldi r24, pinChange1BitMask ; bit mask for enable the specfic PCI1 pins
  207. st z, r24
  208.  
  209. //prepare sleep mode
  210.  
  211. ldi r24, sleepModePowerDownMask
  212. out SMCR, r24
  213.  
  214.  
  215. //set hid twi interface
  216.  
  217. //fill report register with report descriptor
  218. ldi r24, eepromHidReportAddressOffset
  219. clr r25
  220. ldi r22, eepromHIDReportLength
  221. clr r23
  222. ldi r20, hidReportRegisterLow
  223. ldi r21, hidReportRegisterHigh
  224.  
  225. rcall fillReportRegister
  226.  
  227. //fill hid register with hid descriptor
  228. ldi r24, eepromHidDescriptorOffset
  229. clr r25
  230. ldi r22, eepromHidDescriptorLength
  231. clr r23
  232. ldi r20, hidDesriptorRegisterLow
  233. ldi r21, hidDesriptorRegisterHigh
  234.  
  235. rcall fillReportRegister
  236.  
  237. //init the hid driver
  238.  
  239. rcall initHidDriver
  240.  
  241. //init TWI interface
  242. ldi r24, TWIslaveAddress ;load r24 with TWI slave address 0x03
  243. clr r25
  244.  
  245. rcall setUpI2CDevice
  246.  
  247. //setup event flag pointer
  248.  
  249. ldi r26, eventLoopLow
  250. ldi r27, eventLoopHigh
  251.  
  252. //reset all External int flags
  253. ldi r25, 0xff
  254. out EIFR, r25
  255. out PCIFR, r25
  256.  
  257.  
  258. sei
  259. main:
  260.  
  261. ld r2, x
  262.  
  263. sbrc r2, twiFlagBitNum;check for TWI flag
  264. rcall mainFlagHandler
  265.  
  266. sbrc r2, shiftFlagBitNum ; shift ocurred on the exint0 pin or pin change related to the buttons
  267. rcall getShiftValues
  268. sbrc r2, testFlagBitNum ; the test button was pressed
  269. rcall testButtonActivated
  270.  
  271. rjmp main
  272.  
  273. //test fuctions (to be removed in later versions
  274. errorCatch:
  275. sbi PORTD, PortDTestLedBitNum
  276. ret
  277.  
  278. errorCatchLoop:
  279. sbi PORTD, PortDTestLedBitNum
  280. daLoopOfError:
  281. rjmp daLoopOfError
  282.  
  283.  
  284. testButtonActivated:
  285. //insert parameters for delay loop
  286. ldi r22, 0xCF;----------------------------------
  287. ldi r23, 0x07; word ≈ 3 millisec
  288. ldi r24, 0x00;
  289. ldi r25, 0x00;--------------------------------
  290.  
  291. rcall delayLoop
  292.  
  293. ld r16, x
  294.  
  295. andi r16, removeTestFlagBit
  296.  
  297. st x, r16
  298.  
  299. sbic PORTD, PortDTestLedBitNum
  300. rjmp clearTestLedDriver
  301. sbi PORTD, PortDTestLedBitNum
  302. rjmp endOfTesDelay
  303. clearTestLedDriver:
  304. cbi PORTD, PortDTestLedBitNum
  305.  
  306. endOfTesDelay:
  307.  
  308. ret
  309.  
  310. //end of test functions
  311.  
  312. getShiftValues:
  313.  
  314. //remove shift flag bit
  315. ld r16, x
  316.  
  317. andi r16, removeShiftFlagBit
  318.  
  319. st x, r16
  320.  
  321. //fetch new input shift values
  322.  
  323.  
  324.  
  325. in r24, PINB
  326. in r25, PINC
  327.  
  328. andi r25, pincButtonMask
  329.  
  330. rcall hidSetInputVal
  331. cp r24, r1
  332. brne failedInputHIDnew
  333.  
  334. rcall hidPullIntruptLine
  335. cp r24, r1
  336. brne failedInputHIDnew
  337.  
  338. rjmp doneWithShiftValuesGet
  339.  
  340. failedInputHIDnew:
  341. ori r16, addShiftFlagBit
  342. st x, r16
  343.  
  344. doneWithShiftValuesGet:
  345.  
  346. ret
  347.  
  348.  
  349. powerDownMode:
  350.  
  351. ldi r20, sleepBitMask
  352.  
  353. out SMCR, r20; set sleep enable bit
  354.  
  355. sleep
  356.  
  357. ldi r20, sleepModePowerDownMask
  358. out SMCR, r20; clear sleep enable bit
  359.  
  360. ret
  361.  
  362. mainFlagHandler:
  363.  
  364. //remove TWI flag bit
  365. ld r16, x
  366.  
  367. andi r16, removeTWIFlagBit
  368.  
  369. st x, r16
  370. //began the calls for a HID handle
  371.  
  372. rcall mainHidEventHandler ; let the hid handler take care of the TWI line
  373. rcall hidGetErrorCode ; check to see if there is anything the main program must do from this end
  374.  
  375. sbrc r24, forceResetError
  376. rcall hidPullIntruptLine
  377. sbrc r24, powerLowError
  378. rcall powerDownMode ; if device is told to power down being going into low current mode
  379.  
  380. ret
  381.  
  382. //main file defines
  383.  
  384. //exint0 pin bit defines
  385.  
  386. .equ anyLogicalChangeBitSet = 0x05
  387. .equ enableExInt0BitSet = 0x03
  388.  
  389. //enof exint0 pin bit defines
  390.  
  391. //pin change 1 defines
  392.  
  393. .equ enablePCI1BitSet = 2
  394.  
  395. //pin change 1 bit masks
  396.  
  397. .equ pinChange1BitMask = 0x03
  398.  
  399. //enof pin change 1 bit masks
  400.  
  401. //enof pin change 1 defines
  402.  
  403. //sleep mode bit mask
  404.  
  405. .equ sleepModePowerDownMask = 0x04
  406. .equ sleepBitMask = 0x05
  407. //enof sleep mode bit mask
  408.  
  409. //port setup masks
  410.  
  411. .equ ddrPortCMask = 0x0C
  412. .equ ddrPortDMask = 0xF3
  413.  
  414. .equ pincButtonMask = 0x03
  415.  
  416. //enof port setup masks
  417.  
  418. //test LED defines
  419.  
  420.  
  421. //enof test LED defines
  422.  
  423. .equ PortDTestLedBitNum = 1
  424.  
  425. //TWI HID Defines
  426.  
  427. .equ eepromHidReportAddressOffset = 0
  428. .equ eepromHIDReportLength = 31
  429.  
  430. .equ eepromHidDescriptorOffset = 33
  431. .equ eepromHidDescriptorLength = 29
  432.  
  433. .equ TWIslaveAddress = 0x06
  434.  
  435. //enof TWI HID Defines
  436.  
  437. //event flag bit defines
  438.  
  439. .equ shiftFlagBitNum = 0
  440. .equ twiFlagBitNum = 2
  441. .equ testFlagBitNum = 3
  442. //event flag AND REMOVE bit masks
  443.  
  444. .equ removeShiftFlagBit = 0xFE
  445. .equ removeTWIFlagBit = 0xFB
  446. .equ removeTestFlagBit = 0xF7
  447. //enof event flag AND REMOVE bit masks
  448.  
  449. //event flag OR ADD bit mask
  450.  
  451. .equ addShiftFlagBit = 0x01
  452. .equ addTWIFlagBit = 0x04
  453. .equ addTestFlagBit =0x08
  454. //enof event flag OR ADD bit mask
  455.  
  456.  
  457.  
  458. //enof event flag bit defines
  459.  
  460. //enof main file defines
  461.  
  462.  
  463.  
  464. //
  465. //---------------------------------------------------------------------------
  466. //
  467. // =================i2cUsbHid.asm file=====================
  468. //
  469. //---------------------------------------------------------------------------
  470. //
  471.  
  472. /*=============================================================
  473. / Important memory locations
  474. /
  475. / hidWriteRegister -
  476. / address (little endian) = 0x2A01
  477. / description = for first 2 byte I2C writes this register is what holds the address of the register to be written too.
  478. / length = 2 bytes
  479. /
  480. / hidFlag -
  481. / address = 0x2C01
  482. / description = stores information of the ongoing SLA+W transaction in the bits
  483. / it is resetted upon a SLA+W status code or an end of transmission code
  484. / length = 1 byte
  485. /
  486. / bitmap =
  487. / hidFlagRegister
  488. / ---------------------------------------------
  489. / | | CMNP | CMNCH | RP | C |
  490. / |````````````````````````````````````````````
  491. / | 7-4 | 3 | 2 | 1 | 0 |
  492. / ---------------------------------------------
  493. /
  494. / 0 = the Counting Bit is to determine which byte in the hidWriteRegister (or other register being written too) to store too by adding this bit to the low byte pointer.
  495. /
  496. / 1 = the Register Pointer Bit is set when the counting bit is incremented after having already been set.
  497. / If this bit is set then the write handler will instead store the I2C data at the register address pointed to by the hidWriteRegister.
  498. /
  499. / 2 = the Command Check Bit is set when the register is incremented with both the RP and C bits set.
  500. / This bit when set tells the write handler to check if the register that was written to is the command register.
  501. /
  502. / 3 = the Command Proccess Bit is set if the write handler function determined the register written too was the command register.
  503. / When this bit is set the write handler will rcall the command processing function and the write handler is called it will imeiditaly rcall that function
  504. / instead of handling the writes (the command process function will handles an SLA+W based on the given HID command)
  505. /
  506. / 7-4 = not used
  507. /
  508. / hidReadCounter -
  509. / address = 0x2D01
  510. / description = stores the current byte to read from into a register and write to the I2C bus during slave transmitter mode.
  511. / Is resetted by a new SLA+R transaction
  512. /
  513. /
  514. / hidErrorFlag -
  515. / address = 0x2E01
  516. / description = despite the name this is just used to store a code for the event loop to handle for command codes like: reset or power_set (should change name later)
  517. /
  518. /==============================================================
  519. */
  520.  
  521.  
  522. //intialization functions of I2C HID
  523.  
  524. initHidDriver:
  525. //prepare write register for reading HID descriptor
  526. ldi r30, hidWriteRegisterLow
  527. ldi r31, hidWriteRegisterHigh
  528.  
  529. ldi r24, hidDesriptorRegisterLow
  530. ldi r25, hidDesriptorRegisterHigh
  531.  
  532. //store write register with address of hid descriptor
  533. st z+, r24
  534. st z, r25
  535.  
  536. ret
  537.  
  538. fillReportRegister://r24 eeprom address offset | r22 length of report | r20 register desanation
  539.  
  540. mov r30, r20
  541. mov r31, r21
  542.  
  543. add r22, r24 ; add the offset to the length to get the proper comparison value
  544.  
  545. //clr r0 ; used as counter
  546. rjmp reportLoopCompare
  547.  
  548. //eeprom read loop
  549. reportEEPROMLoop:
  550. //add r24, r0 ; add to the offset for whatever r0 is
  551. out EEARL, r24 ; set read address
  552. sbi EECR,EERE ; begin read from EEPROM
  553. in r25,EEDR ; get data from eeprom data
  554.  
  555. st z+, r25 ; store in dest register and increment pointer
  556. inc r24 ; increment r0 count
  557.  
  558. reportLoopCompare:
  559. cp r24, r22
  560. brne reportEEPROMLoop
  561.  
  562.  
  563. ret
  564.  
  565.  
  566.  
  567. //end of I2C HID intialization
  568.  
  569. //I2C HID event handler
  570.  
  571. mainHidEventHandler: ; the hid event handler runs under the assumption that the global intrupt flag is off so must end with sei
  572.  
  573. rcall getI2Cstatus
  574.  
  575. //move to specfic function based on status call
  576.  
  577. //check for SLA+W status
  578.  
  579. cpi r24, beginWrite
  580. breq statusConBeginWrite
  581. cpi r24, dataRecieved
  582. breq statusConDataRecieved
  583. cpi r24, eot
  584. breq statusConEOT
  585. cpi r24, datRecivendNotAcked
  586. breq errorStatusNAcked
  587.  
  588.  
  589. rjmp codeConvertSLARbegin
  590.  
  591. statusConBeginWrite: ; slave address has been called with a write bit reset the hid flag now
  592. ldi r30, hidFlagLow
  593. ldi r31, hidFlagHigh
  594. st z, r1
  595. rjmp mainHidEventHandlerEnd
  596.  
  597. statusConDataRecieved: ; data has been recevied while in slave write mode
  598.  
  599. //rcall hidWriteHandler
  600. rjmp mainHidEventHandlerEnd
  601. statusConEOT: ; transmission ended reset hid flag for good measure
  602.  
  603. rjmp statusConBeginWrite
  604. errorStatusNAcked:
  605. rcall errorCatchLoop
  606.  
  607.  
  608. //check for SLA+R
  609. codeConvertSLARbegin:
  610. cpi r24, beginRead
  611. breq statusConBeganRead
  612. cpi r24, sentDataAcked
  613. breq statusConDataAcked
  614. cpi r24, sentDataNotAcked
  615. breq statusConDataNAcked
  616. rjmp mainHidEventHandlerEnd
  617.  
  618. statusConBeganRead:
  619. ldi r30, hidReadCounterLow
  620. ldi r31, hidReadCounterHigh
  621. st z, r1
  622. rcall hidReadHandler
  623. rjmp mainHidEventHandlerEnd
  624.  
  625. statusConDataAcked:
  626. rcall hidReadHandler
  627. rjmp mainHidEventHandlerEnd
  628. statusConDataNAcked:
  629. cbi PORTD, potbGPIOintruptPinBit ; clear the gpio pin regardless of set or not
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637. mainHidEventHandlerEnd:
  638.  
  639. //remove TWI intrupt flag so bus master can resume control
  640. //and renable global intrupts
  641. ldi r30, TWCR
  642. clr r31
  643. ld r24, z
  644. ori r24, 0x81
  645. st z, r24
  646.  
  647. ld r24, z
  648.  
  649. //test to see if ack bit was removed (remove after done testing)
  650. sbrs r24, 6
  651. rcall errorCatchLoop
  652.  
  653. ret
  654.  
  655.  
  656. hidReadHandler:
  657.  
  658. push r26
  659. push r27
  660.  
  661. //get the register from which the read is taking place on
  662. ldi r30, hidWriteRegisterLow
  663. ldi r31, hidWriteRegisterHigh
  664.  
  665. ld r26, z+
  666. ld r27, z
  667.  
  668. cpi r26, 0x2F
  669. brne loadProperEnding
  670.  
  671.  
  672. loadProperEnding:
  673. ldi r27, 0x01
  674.  
  675.  
  676. contToRead:
  677.  
  678. //get read counter (to add to current address offset)
  679. ldi r30, hidReadCounterLow
  680. ldi r31, hidReadCounterHigh
  681.  
  682. ld r0, z
  683.  
  684. add r26, r0
  685. adc r27, r1
  686. //get data to transmit on the bus
  687. ld r24, x
  688.  
  689. //increment read counter and store back
  690. inc r0
  691. st z, r0
  692.  
  693. //set data to tranmit in TWI data register
  694. rcall writeI2Cdata
  695.  
  696. pop r27
  697. pop r26
  698.  
  699. ret
  700.  
  701.  
  702. hidWriteHandler:
  703. push r26
  704. push r27
  705. push r16
  706. push r17
  707.  
  708. //get data that was written
  709.  
  710. rcall readI2Cdata
  711. mov r0, r24
  712.  
  713. //load the write flag into register 16
  714. ldi r26, hidFlagLow
  715. ldi r27, hidFlagHigh
  716.  
  717. ld r16, x
  718.  
  719. //get byte counter
  720. mov r17, r16
  721. andi r17, writeFlagCounterMask ; remove all bits except the counter bit
  722.  
  723. //check to see if a command is being processed
  724. sbrc r16, writeFlagCommandBit
  725. rcall hidCommandProcess
  726. sbrs r16, writeFlagInterRegBit
  727. rjmp normalHidWritePPointer
  728.  
  729. ldi r30, hidWriteRegisterLow
  730. ldi r31, hidWriteRegisterHigh
  731.  
  732. ld r24, z+
  733. ld r25, z
  734.  
  735. rjmp mainWritePortionOfHandler
  736. normalHidWritePPointer:
  737. ldi r24, hidWriteRegisterLow
  738. ldi r25, hidWriteRegisterHigh
  739.  
  740. mainWritePortionOfHandler:
  741. mov r30, r24
  742. mov r31, r25
  743.  
  744. add r30, r17
  745.  
  746. st z, r0
  747. inc r16
  748.  
  749. sbrs r16, writeFlagCheckCommandBit
  750. rjmp endOfHidWriteHandler
  751.  
  752. cpi r30, hidCommandRegisterLow
  753. brne endOfHidWriteHandler
  754. cpi r31, hidCommandRegisterHigh
  755. brne endOfHidWriteHandler
  756.  
  757. ori r16, writeFlagCommandProcMask
  758.  
  759.  
  760. rcall hidCommandProcess
  761.  
  762. endOfHidWriteHandler:
  763.  
  764. st x, r16
  765.  
  766. pop r17
  767. pop r16
  768. pop r27
  769. pop r26
  770.  
  771. ret
  772.  
  773. hidCommandProcess:
  774.  
  775. ldi r30, hidCommandRegisterLow
  776. ldi r31, hidCommandRegisterHigh
  777.  
  778. ld r25, z+
  779. ld r24, z
  780.  
  781. //comapre to opcodes
  782. cpi r25, resetCommandHighB
  783. breq resetCommandJump
  784. cpi r25, getReportCommandHigh
  785. breq getReportCommandJump
  786. cpi r25, setPowerCommandHigh
  787. breq setPowerCommandJump
  788. rjmp endOfCommandProc
  789. //jump table
  790. resetCommandJump:
  791. ldi r30, hidInputRegisterLow
  792. ldi r31, hidInputRegisterHigh
  793. //clear the input register for reset init
  794. st z+, r1
  795. st z+, r1
  796. st z+, r1
  797. st z, r1
  798. //set error to make sure gpio int is pulled
  799. ldi r30, hidErrorFlagLow
  800. ldi r31, hidErrorFlagHigh
  801.  
  802. ldi r23, forceResetError
  803. st z, r23
  804. rjmp endOfCommandProc
  805.  
  806. getReportCommandJump:
  807. //incomeing report read set write read register to input for reading
  808. ldi r30, hidWriteRegisterLow
  809. ldi r31, hidWriteRegisterHigh
  810.  
  811. ldi r25, hidInputRegisterLow
  812. ldi r24, hidInputRegisterHigh
  813.  
  814. st z+, r25
  815. st z, r24
  816. rjmp endOfCommandProc
  817.  
  818. setPowerCommandJump:
  819. //set error to place processor into low power sleep mode
  820. ldi r30, hidErrorFlagLow
  821. ldi r31, hidErrorFlagHigh
  822.  
  823. ldi r23, powerLowError
  824. st z, r23
  825.  
  826.  
  827.  
  828.  
  829. endOfCommandProc:
  830.  
  831.  
  832. ret
  833.  
  834.  
  835. //end of I2C HID event handlers
  836.  
  837. //I2C HID exposed driver functions
  838.  
  839. hidPullIntruptLine://if return in r24 is anything other then zero it failed to pull the line
  840.  
  841. //check to make sure no on going transaction is occuring
  842. //get the flag value
  843. ldi r30, hidFlagLow
  844. ldi r31, hidFlagHigh
  845.  
  846. ld r24, z
  847.  
  848. cp r24, r1
  849. brne hidFinnishedPullUp
  850.  
  851. //get read count as indication of ungoing read transaction
  852. ldi r30, hidReadCounterLow
  853. ldi r31, hidReadCounterHigh
  854.  
  855. ld r24, z
  856.  
  857. cp r24, r1
  858. brne hidFinnishedPullUp
  859.  
  860. //if made it here there is no ongoign I2C transaction
  861. //set input register for reading in write register
  862. ldi r30, hidWriteRegisterLow
  863. ldi r31, hidWriteRegisterHigh
  864. ldi r22, hidInputRegisterLow
  865. ldi r23, hidInputRegisterHigh
  866.  
  867. st z+, r22
  868. st z, r23
  869. //pull the gpio line high
  870.  
  871. sbi PORTD, potbGPIOintruptPinBit
  872.  
  873. mov r24, r1
  874. hidFinnishedPullUp:
  875.  
  876. ret
  877.  
  878.  
  879. hidGetErrorCode:
  880. //set z pointer to error flag ld r24 and return that code
  881. ldi r30, hidErrorFlagLow
  882. ldi r31, hidErrorFlagHigh
  883.  
  884. ld r24, z
  885.  
  886. ret
  887.  
  888. hidSetInputVal://r24-25 value to to input register
  889. //if return in r24 is anything other then zero it failed to pull the line
  890.  
  891. //check to make sure no on going transaction is occuring
  892. //get the flag value
  893. ldi r30, hidFlagLow
  894. ldi r31, hidFlagHigh
  895.  
  896. ld r20, z
  897.  
  898. cp r20, r1
  899. brne hidFillInputRegDone
  900.  
  901. //get read count as indication of ungoing read transaction
  902. ldi r30, hidReadCounterLow
  903. ldi r31, hidReadCounterHigh
  904.  
  905. ld r20, z
  906.  
  907. cp r20, r1
  908. brne hidFillInputRegDone
  909.  
  910. //if made to this point no ongoing I2C HID transaction is ocurring
  911. ldi r30, hidInputRegisterLow
  912. ldi r31, hidInputRegisterHigh
  913.  
  914. ldi r22, hidInputRegisterLength
  915.  
  916. //store (length of report) + (report data) in input register
  917. st z+, r22
  918. st z+, r1
  919. st z+, r24
  920. st z, r25
  921.  
  922. hidFillInputRegDone:
  923. mov r24, r20
  924. ret
  925.  
  926. //enof I2C HID exposed driver functions
  927.  
  928.  
  929. //hid defines
  930.  
  931. .equ writeFlagCommandBit = 3
  932. .equ writeFlagCheckCommandBit = 2
  933. .equ writeFlagInterRegBit = 1
  934.  
  935. //hid bit masks
  936.  
  937. .equ writeFlagCounterMask = 0xFE
  938. .equ writeFlagCommandProcMask = 0x08
  939.  
  940. //end of hid bit masks
  941.  
  942. //commands
  943. //the low byte on all commands contain variable data relatie to the given command opcode in the high byte
  944. //reset command
  945.  
  946. .equ resetCommandLowB = 0x00
  947. .equ resetCommandHighB = 0x01
  948. .equ resetWriteFlagBit = 16
  949.  
  950. //enof reset command
  951.  
  952. //get-report command
  953.  
  954. .equ getReportCommandLow = 0x00 ;contains report type and report id
  955. .equ getReportCommandHigh = 0x02
  956. .equ getReportWriteFlagBit = 32
  957.  
  958. //get-report bit masks
  959.  
  960. .equ reportIdBitMask = 0x0F
  961. .equ reportTypeGetBitMask = 0x30
  962. //enof get-report bit masks
  963.  
  964. //enof get-report command
  965.  
  966. //set-report command
  967.  
  968. .equ setReportCommandLow = 0x00 ; contains report type and report id
  969. .equ setReportCommandHigh = 0x03
  970. .equ setReportWriteFlagBit = 64
  971.  
  972. //enof set-report command
  973.  
  974. //set-power command
  975.  
  976. .equ setPowerCommandLow = 0x00 ; contains power setting
  977. .equ setPowerCommandHigh = 0x08
  978. .equ setPowerWriteFlagBit = 0x80
  979.  
  980. //enof set-power command
  981.  
  982. //end of commands
  983.  
  984.  
  985. //error defines
  986.  
  987. .equ forceResetError = 1
  988. .equ powerLowError = 2
  989.  
  990. //enof error defines
  991.  
  992. //pin bits
  993.  
  994. .equ potbGPIOintruptPinBit = 2
  995.  
  996. //enof pin bits
  997.  
  998. //end of hid defines
  999.  
  1000. //
  1001. //---------------------------------------------------------------------------
  1002. //
  1003. // =================basicI2cDriver.asm file=====================
  1004. //
  1005. //---------------------------------------------------------------------------
  1006. //
  1007.  
  1008.  
  1009. //I2C intialization
  1010.  
  1011. setUpI2CDevice: //r24 slave address to set (loads full byte into address register including general call address FYI)
  1012.  
  1013. //set the two wire bit rate
  1014. ldi r30, TWBR //setup pointer to the bit rate Two wire register
  1015. clr r31 ;clear high z pointer
  1016.  
  1017. ldi r25, twiBitMaskRate ; value of 2 for bit rate to achieve 400khz fast mode
  1018.  
  1019. st z, r25
  1020.  
  1021. //set the slave address
  1022. ldi r30, TWAR
  1023.  
  1024. st z, r24
  1025.  
  1026. //set the control register
  1027. ldi r30, TWCR
  1028. ldi r25, twiControlRegMask
  1029.  
  1030. st z, r25
  1031.  
  1032.  
  1033.  
  1034. ret
  1035.  
  1036. //end of I2C intialization
  1037.  
  1038.  
  1039. //I2C driver functions
  1040.  
  1041. getI2Cstatus:
  1042.  
  1043. ldi r30, TWSR
  1044. clr r31
  1045. ld r24, z
  1046.  
  1047.  
  1048. andi r24, 0xF8 ; keep only status flag
  1049.  
  1050.  
  1051.  
  1052. ret
  1053.  
  1054. readI2Cdata:
  1055.  
  1056. ldi r30, TWDR
  1057. clr r31
  1058.  
  1059. ld r24, z
  1060.  
  1061. ret
  1062.  
  1063. writeI2Cdata:
  1064.  
  1065. ldi r30, TWDR
  1066. clr r31
  1067.  
  1068. st z, r24
  1069.  
  1070. ret
  1071.  
  1072.  
  1073. //end of I2C driver functions
  1074.  
  1075.  
  1076. //I2C setup values
  1077.  
  1078. .equ twiBitMaskRate = 2
  1079. .equ twiControlRegMask = 0xC5
  1080.  
  1081. //end of I2C values
  1082.  
  1083. //I2C bit masks
  1084.  
  1085. .equ i2cStatusRegMask = 0xF8
  1086.  
  1087. //end of I2C bit masks
  1088.  
  1089. //status codes
  1090.  
  1091. //slave reciever
  1092. .equ beginWrite = 0x60
  1093. .equ dataRecieved = 0x80
  1094. .equ datRecivendNotAcked = 0x88
  1095. .equ eot = 0xA0
  1096.  
  1097. //slave transmitter
  1098. .equ beginRead = 0xA8
  1099. .equ sentDataAcked = 0xB8
  1100. .equ sentDataNotAcked = 0xC0
  1101.  
  1102.  
  1103. //end of status codes
  1104.  
  1105. //I2C status flags
  1106.  
  1107. .equ i2cBeganSLAWriteFlag = 1
  1108. .equ i2cDataRecievedFlag = 2
  1109. .equ i2cEOTflag = 4
  1110.  
  1111. .equ i2cBeganReadFlag = 8
  1112. .equ i2cDataSentAckedFlag = 10
  1113. .equ i2cDataSentNAckedFlag = 20
  1114.  
  1115. //end of I2C status flags
  1116.  
  1117.  
  1118. //I2C bit masks
  1119.  
  1120. .equ removeI2CIntruptFlag = 0x7F
  1121.  
  1122. //end of bit masks
  1123.  
  1124.  
  1125.  
  1126. //
  1127. //---------------------------------------------------------------------------
  1128. //
  1129. // =================memMap.inc file=====================
  1130. //
  1131. //---------------------------------------------------------------------------
  1132. //
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138. //hid memory region
  1139. .equ sramStartOffsetLow = 0x00
  1140. .equ sramStartOffsetHigh = 0x01
  1141. //starts at 0x0001
  1142.  
  1143. .equ hidReportRegisterLow = 0x00
  1144. .equ hidReportRegisterHigh = 0x01
  1145. .equ hidReportRegisterLength = 32
  1146.  
  1147. .equ hidCommandRegisterLow = 0x20
  1148. .equ hidCommandRegisterHigh = 0x01
  1149. .equ hidCommandRegisterLength = 2
  1150.  
  1151. .equ hidDataRegisterLow = 0x22
  1152. .equ hidDataRegisterHigh = 0x01
  1153. .equ hidDataRegisterLength = 4
  1154.  
  1155. .equ hidInputRegisterLow = 0x26
  1156. .equ hidInputRegisterHigh = 0x01
  1157. .equ hidInputRegisterLength = 4
  1158.  
  1159. .equ hidWriteRegisterLow = 0x2A
  1160. .equ hidWriteRegisterHigh = 0x01
  1161. .equ hidWriteRegisterLength = 2
  1162.  
  1163. .equ hidFlagLow = 0x2C
  1164. .equ hidFlagHigh = 0x01
  1165. .equ hidFlagLength = 1
  1166.  
  1167. .equ hidReadCounterLow = 0x2D
  1168. .equ hidReadCounterHigh = 0x01
  1169. .equ hidReadCounter = 1
  1170.  
  1171. .equ hidErrorFlagLow = 0x2E
  1172. .equ hidErrorFlagHigh = 0x01
  1173. .equ hidErrorLength = 1
  1174.  
  1175. .equ hidDesriptorRegisterLow = 0x2F
  1176. .equ hidDesriptorRegisterHigh = 0x01
  1177. .equ hidDescriptorLength = 30
  1178.  
  1179. .equ outPutRegisterLow = 0x4D
  1180. .equ outPutRegisterHigh = 0x01
  1181. .equ outputRegisterLength = 1
  1182.  
  1183. //end of hid memory region
  1184. //ends at 0x4E01
  1185.  
  1186. //eventLoop memory region
  1187. //starts at 0x4E01
  1188.  
  1189. .equ eventLoopLow = 0x4E
  1190. .equ eventLoopHigh = 0x01
  1191. .equ eventLoopLength = 1
  1192.  
  1193. //end of eventLoop region
  1194. //ends at 0x2F01
  1195.  
  1196.  
  1197. //delay script for button debouning (glorifed for loop)
  1198.  
  1199. delayLoop:; parameters unsigned 32 bit int, lsb r22 msb r25
  1200. //delay values is calculated as follows
  1201. //(MCUhz - 10) / 12 = 1 sec loop iterations * number of secs to run = 32bit input val
  1202.  
  1203. rjmp delayLoopCompare
  1204. // 5 cycles set up
  1205.  
  1206. delayLoopDec:
  1207.  
  1208. subi r22, 1
  1209. sbci r23, 0
  1210. sbci r24, 0
  1211. sbci r25, 0
  1212. //4 cycles
  1213. delayLoopCompare:
  1214. clr r0
  1215. or r0, r22
  1216. or r0, r23
  1217. or r0, r24
  1218. or r0, r25
  1219. cp r0, r1
  1220. BRNE delayLoopDec
  1221. //8 cycles total
  1222.  
  1223. //one loop is 12 cycles
  1224. delayLoopEnd:
  1225.  
  1226. //5 cycles (this includes the former BRNE statement) for clean
  1227.  
  1228.  
  1229. ret
  1230.  
  1231. //enof button debouncer
Tags: avrAssembly
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement