Coder Social home page Coder Social logo

avr-n64's Introduction

avr-n64

Read state from a Nintendo 64 controller using an ATmega32 microcontroller and light up LEDs.

Demo

Using this library, it's possible to convert a Nintendo 64 controller into a USB HID game pad, as in the following project:

https://github.com/jaredkrinke/avr-n64-usb

Library notes

The communication library is implemented a single header file: avr-n64.h. To get the state of a controller, call this function:

void n64_controller_get_state(n64_controller_state_t* state);

The library assumes the controller data line is connected to port B, bit 0.

Note: while the code is written in C, it assumes specific optimized compiler output (e.g. loop unrolling). See CFLAGS in Makefile for more detail.

Protocol notes

The controller uses 3 wires: two for power, one for half-duplex serial communication based on a 1 MHz clock with roughly 31 kbps of bandwidth.

Connector pin out

Facing the "plug" side of the controller connector, the pins are (left to right): ground, data, 3.3 volts.

Data line

The data line is an open collector with (as measured) an 800 ohm built-in pull-up resistor (which keeps the line high, at 3.3 volts). Communication is initiated by connecting to ground (driving voltage to zero), and eventually disconnecting and letting the line go high again. Note: the host should never drive this line high.

Encoding

Bits are sent (always most significant bit first) using the following encoding:

  • 1 microsecond low
  • 2 microseconds at either low (for a zero) or high (for a one)
  • 1 microsecond high

Communication is terminated by 3 microsecond stop bit:

  • 1 microsecond low, 2 microseconds high for the host
  • 2 microseconds low, 1 microsecond high for the controller

Message passing

The host initiates communication by sending 1 byte messages. The 0x01 message polls the controller for state. The controller responds with 4 bytes (plus a stop bit):

  • A, B, Z, Start, Up, Down, Left, Right
  • Reset, Reserved, L, R, C Up, C Down, C Left, C Right
  • Analog joystick horizontal position (signed two's complement integer)
  • Analog joystick vertical position

Implementation notes

This implementation uses an ATmega32 (DIP package) running at >= 4 MHz (tested on 4 MHz internal oscillator and 12 MHz external crystal), with a power supply of 5 volts (tested with a repurposed cell phone charger). A 3.3 volt linear regulator supplies power to the controller.

Timing

This setup uses a software implementation of the protocol ("bit banging"). The code assumes specific optimized compiler output (e.g. loop unrolling and using "cbi" and "sbi" instructions for manipulating the data pin), and uses "nop" instructions to ensure a 1 MHz data clock.

Open collector output pin

The ATmega32 doesn't have an obvious "open collector output" mode, but one can be implemented by:

  • Initially set PORTxn to 0x00
  • To write a zero, set DDRxn to 1 (this drives the line low)
  • To write a one, set DDRxn to 0 (this puts the pin in a disconnected/tri-state/high-impedance state)
  • While DDRxn is 0, read PINxn for input (3.3 volts is above the sensitivity of the input pin)

External resources

These resources go into more detail about the controller protocol:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.