#include <util/atomic.h>
#include <limits.h>
#include "debug.h"
-#include "pin.h"
+#include "gpio.h"
/*
5 1024 x4 1024 x4 128 x2
6 256 x2
7 1024 x4
---------------------------------------------------
+--------------------------------------------------
*/
#define PWMNEG 0b11
-const FLASH uint8_t prescale_factors_01[] =
+const FLASH uint8_t prescale_factors_01[] =
{ 8, 8, 4, 4, 0 };
-const FLASH uint8_t prescale_factors_2[] =
+const FLASH uint8_t prescale_factors_2[] =
{ 8, 4, 2, 2, 2, 4, 0 };
typedef volatile struct {
};
-const FLASH struct pindef_s pinlist[PIN_MAX] = {
+const FLASH struct pindef_s pinlist[GPIO_MAX] = {
{ (port_t *) &PING, _BV(5), TIMER0 | CHANB },
{ (port_t *) &PING, _BV(4), NO_TIMER },
{ (port_t *) &PINB, _BV(4), TIMER2 | CHANA },
{ (port_t *) &PING, _BV(0), NO_TIMER },
{ (port_t *) &PINE, _BV(7), NO_TIMER },
};
-
-void pin_timer_off(uint8_t timertype)
+
+void gpio_timer_off(uint8_t timertype)
{
uint8_t chan_mask;
-
+
if (timertype & CHANA)
chan_mask = 0xc0;
else
}
}
-int pin_config(int pin, pinmode_t mode)
+int gpio_config(int pin, gpiomode_t mode)
{
if ((unsigned) pin >= ARRAY_SIZE(pinlist)) {
/* Invalid pin number */
port_t *p = pinlist[pin].adr;
uint8_t bit = pinlist[pin].mask;
-
+
switch (mode) {
case INPUT:
- pin_timer_off(pinlist[pin].timer);
+ gpio_timer_off(pinlist[pin].timer);
ATOMIC_BLOCK(ATOMIC_FORCEON) {
p->ddr &= ~bit;
p->pout &= ~bit;
}
break;
case INPUT_PULLUP:
- pin_timer_off(pinlist[pin].timer);
+ gpio_timer_off(pinlist[pin].timer);
ATOMIC_BLOCK(ATOMIC_FORCEON) {
p->ddr &= ~bit;
p->pout |= bit;
}
break;
case OUTPUT:
- pin_timer_off(pinlist[pin].timer);
+ gpio_timer_off(pinlist[pin].timer);
case OUTPUT_TIMER:
ATOMIC_BLOCK(ATOMIC_FORCEON) {
p->ddr |= bit;
return 0;
}
-void pin_write(int pin, uint8_t val)
+void gpio_write(int pin, uint8_t val)
{
port_t *p = pinlist[pin].adr;
uint8_t bit = pinlist[pin].mask;
-
+
ATOMIC_BLOCK(ATOMIC_FORCEON) {
if (val)
p->pout |= bit;
}
}
-int pin_read(int pin)
+int gpio_read(int pin)
{
port_t *p = pinlist[pin].adr;
uint8_t bit = pinlist[pin].mask;
-
+
return (p->pin & bit) != 0;
}
-pinmode_t pin_config_get(int pin)
+gpiomode_t gpio_config_get(int pin)
{
uint8_t timertype = pinlist[pin].timer;
if (timertype & TIMER) {
-
+
uint8_t chan_mask;
if (timertype & CHANA)
chan_mask = 0xc0;
break;
}
}
-
+
port_t *p = pinlist[pin].adr;
uint8_t bit = pinlist[pin].mask;
if (p->ddr & bit)
return OUTPUT;
-
+
if (p->pout & bit)
return INPUT_PULLUP;
-
+
return INPUT;
}
-
+
/*
* return -1: pin has no timer output
* 0: pin is not configured for timer output
* > 0: divider
*/
-
-long pin_clockdiv_get(int pin)
+
+long gpio_clockdiv_get(int pin)
{
long divider;
uint8_t prescale;
if ((timertype & TIMER) == 0)
return -1;
- if (pin_config_get(pin) != OUTPUT_TIMER)
+ if (gpio_config_get(pin) != OUTPUT_TIMER)
return 0;
-
+
switch (timertype & TIMER) {
case TIMER0:
prescale = TCCR0B;
prescale = (prescale & 0x07) - 1;
divider += 1;
- pstab = (timertype & TIMER) == TIMER2 ?
+ pstab = (timertype & TIMER) == TIMER2 ?
prescale_factors_2 : prescale_factors_01;
-
+
while (prescale--)
divider *= pstab[prescale];
-
+
if ((timertype & (CHANA|T_16BIT)) == CHANA)
divider *= 2;
-
+
return divider;
}
-int pin_clockdiv_set(int pin, unsigned long divider)
+int gpio_clockdiv_set(int pin, unsigned long divider)
{
unsigned long ltop;
uint16_t top;
if (divider < 2)
return -1;
-
+
ltop = divider;
if ((timertype & (CHANA|T_16BIT)) == CHANA)
ltop /= 2;
-
+
if (ltop > 1024 * ((timertype & T_16BIT) ? (1L<<16) : (1L<<8)))
return -1;
prescale = 1;
- pstab = (timertype & TIMER) == TIMER2 ?
+ pstab = (timertype & TIMER) == TIMER2 ?
prescale_factors_2 : prescale_factors_01;
-
+
// debug("** clockdiv_set: pin: %d, ltop: %lu, prescale: %d\n",
// pin, ltop, prescale);
PING |= _BV(0); /* Debug */
- pin_config(pin, OUTPUT_TIMER);
+ gpio_config(pin, OUTPUT_TIMER);
return 0;
}
-