Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Need help adapting Kade Encoder source code  (Read 2225 times)

0 Members and 1 Guest are viewing this topic.

rCadeGaming

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1256
  • Last login:December 20, 2023, 09:16:09 pm
  • Just call me Rob!
Need help adapting Kade Encoder source code
« on: March 01, 2018, 12:57:05 am »
Greetings old friends - haven't been here in a while.  :-\

I've been playing with some KADE MiniArcades I've had for a while, and I decided to try re-purposing one as a Genesis controller to USB Keyboard encoder.  My KADEs have ATMEGA32U2's in them (on a Minimus I think), and I've been coding in Atmel Studio and writing the firmware with Flip.

In any case, I finished the code to read the inputs from the Genesis controller (troubleshot and verified with an oscilloscope), but now I'm stuck on trying to output as a USB keyboard.  I'm trying to adapt usb_keyboard.c and usb_keyboard.h from the Kade source code found on GitHub:

https://github.com/kadevice/KADE/tree/master/open%20software/firmwares/KADE%20miniArcade/sources/kade-gen

I noticed that they don't seem to support the ATMEGA32U2 though.  To add it, I tried modifying the following section of keyboard.h:

Code: [Select]
#elif defined(__AVR_ATmega32U4__)
#define HW_CONFIG() (UHWCON = 0x01)
#define PLL_CONFIG() (PLLCSR = 0x12)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))

This is as far as I got:

Code: [Select]
#elif defined(__AVR_ATmega32U2__)
#define HW_CONFIG()
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
#define USB_CONFIG() (USBCON = (1<<USBE))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))

Unlike some previous attempts, this does compile and run without crashing, and my PC recognizes it as a keyboard.  The key presses sent to the PC almost never work though.  I think the problem is with the PLL divider as determined with PLL_CONFIG().  I don't really know though - by this point in the code I'm a bit out of my element.

Are any of the KADE devs still around to help with this?  KADE used to run on the ATMEGA32U2, so is there some older, unreleased version of the code that supports it?

Thanks!

PL1

  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 9403
  • Last login:Today at 09:15:50 pm
  • Designated spam hunter
Re: Need help adapting Kade Encoder source code
« Reply #1 on: March 01, 2018, 03:08:54 am »
I noticed that they don't seem to support the ATMEGA32U2 though.
Disclaimer: I'm not very good at reading code and worse at writing it.     :embarassed:

The KADE Loader program and firmwares were designed to support both versions of the Minimus AVR -- AT90USB162 and ATMega32u2.

Jon mentions in line 54 of "Makefile" that code compiled for the AT90USB162 will also work on ATMega32u2.

You should be able to define "MCU = at90usb162" like line 55 of "Makefile" . . .
Code: [Select]
# MCU name, you MUST set this to match the board you are using
# type "make clean" after changing this, so all files will be rebuilt
#
# NOTE - Stuff compiled for 16k will work on both!!
MCU = at90usb162       # Minimus AVR 16K
#MCU = atmega32u2       # Minimus AVR 32k
. . . and use the code from lines 177-181 of "keyboard.h" . . .
Code: [Select]
#if defined(__AVR_AT90USB162__)
#define HW_CONFIG()
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
#define USB_CONFIG() (USBCON = (1<<USBE))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
. . . and the "make clean" command mentioned in "Makefile" so all files are rebuilt -- unless there's something I missed.   :dunno


Scott

rCadeGaming

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1256
  • Last login:December 20, 2023, 09:16:09 pm
  • Just call me Rob!
Re: Need help adapting Kade Encoder source code
« Reply #2 on: March 01, 2018, 09:48:43 pm »
Scott,

Thanks for the quick reply.  Forgot to pay attention to the makefile - rookie mistake.

What I ended up using for the #define lines was in fact the same as those for the AT90USB162, but there is probably something else that I missed from the makefile.  I'll try setting up the exact development environment as recommended in the KADE forum so I can try compiling the unmodified code with the native makefile as a proof of concept.  From there I should be able to edit it and insert my Genesis controller code. 

Thanks for getting me back on track.  I think I'm heading in the right direction again.   :cheers:

johnnygal2

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 23
  • Last login:October 08, 2022, 03:16:04 am
  • I want to build my own arcade controls!
Re: Need help adapting Kade Encoder source code
« Reply #3 on: March 02, 2018, 04:06:08 am »
Hi,

I had the same issue when modifying the sources for atmega32u4, in that the built hex downloaded to a device but didn't have any effect when pressing inputs.
The current sources already support the atmega32u2 so you shouldn't have to change much unless the pinouts are different on the board you intend to use. The current pin/port mappings are setup as per the minimus boards.

After digging deeper into the code, I found that the mappings between pin input and key action is not contained within the source code itself. If you look in generic_main_init.c in the 'shared' directory, you will see it reading in the first 40 bytes initially for the main keys/shifted keys. It elsewhere reads in another 29 bytes for specials like autofire, etc. These 69 bytes are prefixed onto the hex by the Kade Loader UI, when you select what buttons configuration you want.
To make your own custom hex, you will need to hardcode the mappings here for your intended purpose.

You may find some more useful information in this thread, from observations I made along the way in creating my own atmega32u4 version.

http://www.kadevice.net/forum/viewtopic.php?f=22&t=413

Good luck.
« Last Edit: March 02, 2018, 04:20:38 am by johnnygal2 »

rCadeGaming

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1256
  • Last login:December 20, 2023, 09:16:09 pm
  • Just call me Rob!
Re: Need help adapting Kade Encoder source code
« Reply #4 on: March 05, 2018, 12:15:54 am »
I installed WinAVR as recommend on the KADE forum post.  This included mingw, so now I can compile properly from the makefile.  I got my code running and successfully played some Sonic Mania with my six button Genesis controller tonight  :)

I need to implement debouncing and document everything in the comments.  I'll try to do that in the next week or so and get a project thread up with pictures and the source code.

johnnygal2, I started with the code in "open software/firmwares/KADE miniArcade/sources/kade-gen, which has no reference to generic_main_init.c.  Trace what is included when compiling the hex by starting with the source files listed in the makefile (SRC = [filenames]).  In each of those files check what other files are referenced in any #include lines.  Check those files for more #include lines, etc.  I gutted main.c and rewrote it.  At this point, the only things I'm referencing are usb_keyboard.c and usb_keyboard.h, but I plan to implement something similar to mapping.c

johnnygal2

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 23
  • Last login:October 08, 2022, 03:16:04 am
  • I want to build my own arcade controls!
Re: Need help adapting Kade Encoder source code
« Reply #5 on: March 05, 2018, 02:00:19 pm »
Ahh, that's good to hear you got it working.

My mistake about 'shared/generic_main_init' - It is used by the xbox-custom and mame-custom that I have used to build my own hex, so (incorrectly) assumed it was used throughout. It sounds like kade-gen isn't used by the Kade Loader UI then and the mappings are hardcoded in source file?

Anyway, sorry if I sent you down a dead-end.

Cheers.

rCadeGaming

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1256
  • Last login:December 20, 2023, 09:16:09 pm
  • Just call me Rob!
Re: Need help adapting Kade Encoder source code
« Reply #6 on: March 06, 2018, 07:04:44 am »
No worries, it didn't cause me any trouble.

I haven't looked into the loader source code, but I'm pretty sure kade-gen is what it uses when you select "generic."  It's just a generic USB keyboard encoder, which I figured would be the best starting point.  It has its mappings in mappings.c (same directory).