Commits

Trammell Hudson committed ce8cd70 Draft

Use inlined bits.h from scopeclock

Comments (0)

Files changed (3)

 # Optimization level, can be [0, 1, 2, 3, s]. 
 #     0 = turn off optimization. s = optimize for size.
 #     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
-OPT = s
+OPT = 3
 
 
 # Debugging format.
 #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(
+__bits_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)
-	{
-#if 0
-	case 0xA:
-		set_bit(DDRA, pin, value);
-		return;
-#endif
-	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;
-	}
+	__inline_ddr(id, value);
 }
 
 
 void
-out(
+__bits_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)
-	{
-#if 0
-	case 0xA:
-		set_bit(PORTA, pin, value);
-		return;
-#endif
-	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"));
+	__inline_out(id, value);
 }
 
 
 uint8_t
-in(
+__bits_in(
 	const uint8_t id
 )
 {
-	const uint8_t port = (id >> 4) & 0xF;
-	const uint8_t pin = (id >> 0) & 0xF;
-
-	switch (port)
-	{
-#if 0
-	case 0xA:
-		return get_bit(PINA, pin);
-#endif
-	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;
+	return __inline_in(id);
 }
+/** \file
+ * Easy access to AVR ports.
+ *
+ * For compile time constants, the operations will be single
+ * instruction.  For runtime constants a function call will
+ * be required.
+ */
 #ifndef _prom_bits_h_
 #define _prom_bits_h_
 
 #define STR(X) _STR(X)
 
 
+
+/*
+#if defined(__AVR_AT90USB162__) \
+	case 0xA: func(cat(prefix, A), pin); break; \
+#endif
+*/
+
+#define cat(x,y) x ## y
+
+#define BITS_FOR_ALL(prefix, func, port, pin) do { \
+	switch (port) \
+	{ \
+	case 0xB: func(cat(prefix, B), pin); break; \
+	case 0xC: func(cat(prefix, C), pin); break; \
+	case 0xD: func(cat(prefix, D), pin); break; \
+	case 0xE: func(cat(prefix, E), pin); break; \
+	case 0xF: func(cat(prefix, F), pin); break; \
+	} \
+} while (0)
+
+
+/** Function call version for non-compile time constants */
 extern void
-ddr(
+__bits_out(
+	const uint8_t port,
+	uint8_t value
+);
+
+
+static inline void
+__inline_out(
+	const uint8_t id,
+	const uint8_t value
+)
+{
+	const uint8_t port = (id >> 4) & 0xF;
+	const uint8_t pin = (id >> 0) & 0xF;
+
+	if (value)
+		BITS_FOR_ALL(PORT,sbi, port, pin);
+	else
+		BITS_FOR_ALL(PORT,cbi, port, pin);
+}
+
+
+static inline void
+out(
+	const uint8_t port,
+	uint8_t value
+)
+{
+	if (__builtin_constant_p(port)
+	&&  __builtin_constant_p(value))
+		__inline_out(port, value);
+	else
+		__bits_out(port, value);
+}
+
+
+extern void
+__bits_ddr(
 	const uint8_t port,
 	const uint8_t value
 );
 
 
-extern void
-out(
+static inline void
+__inline_ddr(
+	const uint8_t id,
+	const uint8_t value
+)
+{
+	const uint8_t port = (id >> 4) & 0xF;
+	const uint8_t pin = (id >> 0) & 0xF;
+
+	if (value)
+		BITS_FOR_ALL(DDR,sbi, port, pin);
+	else
+		BITS_FOR_ALL(DDR,cbi, port, pin);
+}
+
+
+static inline void
+ddr(
 	const uint8_t port,
 	uint8_t value
+)
+{
+	if (__builtin_constant_p(port)
+	&&  __builtin_constant_p(value))
+		__inline_ddr(port, value);
+	else
+		__bits_ddr(port, value);
+}
+
+
+extern uint8_t
+__bits_in(
+	const uint8_t port
 );
 
 
-extern uint8_t
+static inline uint8_t
+__inline_in(
+	const uint8_t id
+)
+{
+	const uint8_t port = (id >> 4) & 0xF;
+	const uint8_t pin = (id >> 0) & 0xF;
+
+	BITS_FOR_ALL(PIN,return bit_is_set, port, pin);
+	return -1; // unreached?
+}
+
+
+static inline uint8_t
 in(
-	uint8_t port
-);
+	const uint8_t port
+)
+{
+	if (__builtin_constant_p(port))
+		return __inline_in(port);
+	else
+		return __bits_in(port);
+}
+
 
 #endif
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.