本帖最后由 CC2530 于 2011-7-2 16:23 编辑
#include "stm8l15x_conf.h"
#include "uni_int.h"
#include "iar_macro.h"
#include "util_macro.h"
#define STM8_IO_REG_WITH_DELAY 1
#if STM8_IO_REG_WITH_DELAY==1
void __delay_1(uint16 n);
#endif
template <uint8 C> class STM8_IO_REG_BIT_t
{
public:
__always_inline__ static STM8_IO_REG_BIT_t<C> &Attach(volatile GPIO_TypeDef * REG_ADDR)
{
return *(STM8_IO_REG_BIT_t<C> *)REG_ADDR;
}
GPIO_TypeDef GPIO;
__always_inline__ STM8_IO_REG_BIT_t<C> &Set()
{
GPIO.ODR |= 1U<<C;
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &Clr()
{
GPIO.ODR &= ~(1U<<C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &Toggle()
{
GPIO.ODR ^= 1U<<C;
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &Write(uint8 value)
{
if(value)
{
GPIO.ODR |= 1U<<C;
}
else
{
GPIO.ODR &= ~(1U<<C);
}
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &Read(uint8 &value)
{
if(GPIO.IDR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeOut()
{
GPIO.DDR |= _BV( C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeIn()
{
GPIO.DDR &= ~_BV( C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &SetCR1(void)
{
GPIO.CR1 |= _BV( C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &ClrCR1(void)
{
GPIO.CR1 &= ~_BV( C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &SetCR2(void)
{
GPIO.CR2 |= _BV( C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &ClrCR2(void)
{
GPIO.CR2 &= ~_BV( C);
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeInputFloatingWithoutInterrupt(void)
{
this->MakeIn().ClrCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeInputPullupWithoutInterrupt(void)
{
this->MakeIn().SetCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeInputFloatingWithInterrupt(void)
{
this->MakeIn().ClrCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeInputPullupWithInterrupt(void)
{
this->MakeIn().SetCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeOutputOpenDrainSlow(void)
{
this->MakeOut().ClrCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeOutputPushPullSlow(void)
{
this->MakeOut().SetCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeOutputOpenDrainFast(void)
{
this->MakeOut().ClrCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &MakeOutputPushPullFast(void)
{
this->MakeOut().SetCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &ReadIn(uint8 &value)
{
if(GPIO.IDR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &ReadOut(uint8 &value)
{
if(GPIO.ODR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_REG_BIT_t<C> &ReadDir(uint8 &value)
{
if(GPIO.DDR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
#if STM8_IO_REG_WITH_DELAY==1
__always_inline__ STM8_IO_REG_BIT_t<C> &Delay(uint16 n)
{
__delay_1(n);
return *this;
}
#endif
};
template <uint8 C,uint8 P> class STM8_IO_POLARITY_REG_BIT_t
{
public:
__always_inline__ static STM8_IO_POLARITY_REG_BIT_t<C,P> &Attach(volatile GPIO_TypeDef * REG_ADDR)
{
return *(STM8_IO_POLARITY_REG_BIT_t<C,P> *)REG_ADDR;
}
GPIO_TypeDef GPIO;
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Set()
{
GPIO.ODR |= 1U<<C;
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Clr()
{
GPIO.ODR &= ~(1U<<C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Toggle()
{
GPIO.ODR ^= 1U<<C;
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Write(uint8 value)
{
if(value)
{
GPIO.ODR |= 1U<<C;
}
else
{
GPIO.ODR &= ~(1U<<C);
}
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Read(uint8 &value)
{
if(GPIO.IDR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOut()
{
GPIO.DDR |= _BV( C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeIn()
{
GPIO.DDR &= ~_BV( C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &SetCR1(void)
{
GPIO.CR1 |= _BV( C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &ClrCR1(void)
{
GPIO.CR1 &= ~_BV( C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &SetCR2(void)
{
GPIO.CR2 |= _BV( C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &ClrCR2(void)
{
GPIO.CR2 &= ~_BV( C);
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputFloatingWithoutInterrupt(void)
{
this->MakeIn().ClrCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputPullupWithoutInterrupt(void)
{
this->MakeIn().SetCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputFloatingWithInterrupt(void)
{
this->MakeIn().ClrCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeInputPullupWithInterrupt(void)
{
this->MakeIn().SetCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputOpenDrainSlow(void)
{
this->MakeOut().ClrCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputPushPullSlow(void)
{
this->MakeOut().SetCR1().ClrCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputOpenDrainFast(void)
{
this->MakeOut().ClrCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &MakeOutputPushPullFast(void)
{
this->MakeOut().SetCR1().SetCR2();
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &ReadIn(uint8 &value)
{
if(GPIO.IDR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &ReadOut(uint8 &value)
{
if(GPIO.ODR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &ReadDir(uint8 &value)
{
if(GPIO.DDR & (1U<<C))
{
value=1;
}
else
{
value=0;
}
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &On()
{
if(P)
{
this->Set();
}
else
{
this->Clr();
}
return *this;
}
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Off()
{
if(P)
{
this->Clr();
}
else
{
this->Set();
}
return *this;
}
#if STM8_IO_REG_WITH_DELAY==1
__always_inline__ STM8_IO_POLARITY_REG_BIT_t<C,P> &Delay(uint16 n)
{
__delay_1(n);
return *this;
}
#endif
};
uint8 x;
#define LED_1 (STM8_IO_REG_BIT_t<1>::Attach(GPIOD)) //LED_1:PD:1
#define LED_2 (STM8_IO_POLARITY_REG_BIT_t<2,1>::Attach(GPIOD)) //LED_2:PD2,高电平亮
void test_io(void)
{
LED_1.MakeOutputPushPullFast().Set().Delay(100).Clr().Delay(100).Toggle().Delay(100).Write(0).Delay(100).Write(1).Delay(100).MakeInputFloatingWithoutInterrupt().Delay(100).Read(x).MakeOutputPushPullFast().Write(x).Delay(100);
LED_2.MakeOutputPushPullFast().On().Delay(100).Off().Delay(100).Set().Delay(100).Clr().Delay(100).Toggle().Delay(100).Write(0).Delay(100).Write(1).Delay(100).MakeInputFloatingWithoutInterrupt().Delay(100).Read(x).MakeOutputPushPullFast().Write(x).Delay(100);
}
int main()
{
test_io();
while(1);
} |