view halpp/include/gpio.h @ 3:3d9705e842f8

working on rcc and bus
author cin
date Wed, 18 Jan 2017 01:07:59 +0300
parents 0c59e7a7782a
children 0d3eea2dd7ea
line wrap: on
line source

#pragma once

namespace halpp {
	typedef enum {
		GpioSpeedLow = 0, // below 5 MHz
		GpioSpeedMedium = 1, // 5-25 MHz
		GpioSpeedHigh = 2, // 25-50 MHz
		GpioSpeedVeryHigh = 3 // above 50 MHz
	} GpioSpeed;

	typedef enum {
		GpioModeInput = 0,
		GpioModeOutput = 1,
		GpioModeAlternative = 2
	} GpioMode;

	typedef enum {
		GpioNoPull = 0,
		GpioPullUp = 1,
		GpioPullDown = 2,
	} GpioPullMode;

	typedef enum {
		GpioPushPull = 0,
		GpioOpenDrain = 1
	} GpioOutputType;

	template <typename TDev> class TGpioTraits {
		explicit TGpioTraits(const TGpioTraits<TDev>&);
		TGpioTraits();
	public:
		static void SetPinSpeed(unsigned short pin, GpioSpeed speed) {
			TDev::instance().OSPEEDR = (TDev::instance().OSPEED & (~(3U << (pin * 2)))) | (speed << (pin * 2));
		}

		static void SetPinMode(unsigned short pin, GpioMode mode) {
			TDev::instance().MODER = (TDev::instance().MODER & (~(3U << (pin * 2)))) | (mode << (pin * 2));
		}

		static void SetPinPullMode(unsigned short pin, GpioPullMode mode) {
			TDev::instance().PUPDR = (TDev::instance().PUPDR & (~(3U << (pin * 2)))) | (mode << (pin * 2));
		}

		static void SetPinOutputType(unsigned short pin, GpioOutputType type) {
			TDev::instance().OTYPER = (TDev::instance().OTYPER & (~(1U << pin))) | (type << pin);
		}

		static void TooglePin(unsigned short pin) {
			TDev::instance().ODR ^= 1U << pin;
		}

		static void WritePin(unsigned short pin, unsigned short value) {
			TDev::instance().ODR = (TDev::instance().ODR & (~(1U << pin))) | ((value & 1U) << pin);
		}

	};

}