prom / bits.c

/** \file Access to AVR pins via constants.
 *
 * ddr(0xA3, 1) == enable DDRA |= (1 << 3)
 * out(0xA3, 1) == PORTA |= (1 << 3)
 * in(0xA3) == PINA & (1 << 3)
 */
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "bits.h"

#define set_bit(PORT, PIN, VALUE) do { \
		if (VALUE) \
			sbi(PORT, PIN); \
		else \
			cbi(PORT, PIN); \
	} while (0)

#define get_bit(PORT, PIN) \
	((PORT) & (1 << PIN))


void
ddr(
	const uint8_t id,
	const uint8_t value
)
{
	const uint8_t port = (id >> 4) & 0xF;
	const uint8_t pin = (id >> 0) & 0xF;

	switch (port)
	{
	case 0xA:
		set_bit(DDRA, pin, value);
		return;
	case 0xB:
		set_bit(DDRB, pin, value);
		return;
	case 0xC:
		set_bit(DDRC, pin, value);
		return;
	case 0xD:
		set_bit(DDRD, pin, value);
		return;
	case 0xE:
		set_bit(DDRE, pin, value);
		return;
	case 0xF:
		set_bit(DDRF, pin, value);
		return;
	}
}


void
out(
	const uint8_t id,
	const uint8_t value
)
{
	const uint8_t port = (id >> 4) & 0xF;
	const uint8_t pin = (id >> 0) & 0xF;

	switch (port)
	{
	case 0xA:
		set_bit(PORTA, pin, value);
		return;
	case 0xB:
		set_bit(PORTB, pin, value);
		return;
	case 0xC:
		set_bit(PORTC, pin, value);
		return;
	case 0xD:
		set_bit(PORTD, pin, value);
		return;
	case 0xE:
		set_bit(PORTE, pin, value);
		return;
	case 0xF:
		set_bit(PORTF, pin, value);
		return;
	}

	send_str(PSTR("bad\r\n"));
}


uint8_t
in(
	const uint8_t id
)
{
	const uint8_t port = (id >> 4) & 0xF;
	const uint8_t pin = (id >> 0) & 0xF;

	switch (port)
	{
	case 0xA:
		return get_bit(PINA, pin);
	case 0xB:
		return get_bit(PINB, pin);
	case 0xC:
		return get_bit(PINC, pin);
	case 0xD:
		return get_bit(PIND, pin);
	case 0xE:
		return get_bit(PINE, pin);
	case 0xF:
		return get_bit(PINF, pin);
	}

	return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.