Commit a44b424c authored by Martin Vítek's avatar Martin Vítek
Browse files

Replaced templated GPIO with normal class

It seems, that it's very efficient - 1 to 2 instructions
                                   - 0B RAM usage
parent 31ff4652
......@@ -3,66 +3,103 @@
#include <avr/io.h>
#include <stdint.h>
/*
* If this will produce lot of instructions, it's possible to pass address of first port register - PORTA
* and registers are next 23 addresses in memory
*/
enum modes
{
INVERTED,
TOTEM_POLE,
BUS_KEEPER,
TOTEM_PULL_UP,
TOTEM_PULL_DOWN,
WIRED_OR,
WIRED_AND,
WIRED_OR_PULL_DOWN,
WIRED_AND_PULL_UP
INVERTED = PORT_INVEN_bm,
TOTEM_POLE = PORT_OPC_TOTEM_gc,
BUS_KEEPER = PORT_OPC_BUSKEEPER_gc,
TOTEM_PULL_UP = PORT_OPC_PULLDOWN_gc,
TOTEM_PULL_DOWN = PORT_OPC_PULLUP_gc,
WIRED_OR = PORT_OPC_WIREDOR_gc,
WIRED_AND = PORT_OPC_WIREDAND_gc,
WIRED_OR_PULL_DOWN = PORT_OPC_WIREDORPULL_gc,
WIRED_AND_PULL_UP = PORT_OPC_WIREDANDPULL_gc
};
enum dirs
enum directions
{
INPUT,
OUTPUT
};
template<PORT_struct *port, uint8_t pin_bm>
class gpio
{
private:
volatile PORT_t &port;
uint8_t pin_bm;
public:
gpio(bool mode = TOTEM_POLE, bool dir = OUTPUT, bool inv = false)
gpio(PORT_t &port, uint8_t pin_bm): port(port),
pin_bm(pin_bm)
{
port->DIRSET = pin_bm;
port->OUTCLR = pin_bm;
//port->PIN1CTRL
}
gpio()
void set_direction(directions direction)
{
if (direction == INPUT) port.DIRCLR = pin_bm;
else if (direction == OUTPUT) port.DIRSET = pin_bm;
else return;
}
void set_mode(modes mode)
{
*(&port.PIN0CTRL+pin_bm) &= (0x38);
*(&port.PIN0CTRL+pin_bm) |= mode;
}
void set_invert(bool state)
{
if (state) *(&port.PIN0CTRL+pin_bm) |= PORT_INVEN_bm;
else *(&port.PIN0CTRL+pin_bm) &= ~PORT_INVEN_bm;
}
void on()
{
port->OUTSET = pin_bm;
port.OUTSET = pin_bm;
}
void off()
{
port->OUTCLR = pin_bm;
port.OUTCLR = pin_bm;
}
void toggle()
{
port->OUTTGL = pin_bm;
port.OUTTGL = pin_bm;
}
bool read()
{
return port.IN & pin_bm;
}
};
/*
template <PORT_t *port, uint8_t pin_mask>
void on()
class output
{
port->OUTSET = pin_mask;
private:
volatile PORT_t &port;
uint8_t pin_bm;
public:
output(PORT_t &port, uint8_t pin_bm): port(port),
pin_bm(pin_bm)
{
port.DIRSET = pin_bm;
}
void on()
{
port.OUTSET = pin_bm;
}
void off()
{
port.OUTCLR = pin_bm;
}
void toggle()
{
port.OUTTGL = pin_bm;
}
};
*/
\ No newline at end of file
......@@ -5,17 +5,17 @@
int main()
{
gpio<PORTD, PIN0_bm> led();
//gpio<&PORTD, PIN1_bm> lcd();
gpio led(PORTD, PIN0_bm);
led.set_direction(OUTPUT);
//on<&PORTD, PIN0_bm>();
for(;;)
{
//led.toggle();
_delay_ms(250);
led.toggle();
_delay_ms(500);
//led.toggle();
_delay_ms(250);
led.toggle();
_delay_ms(500);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment