Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * spi_port.hpp
- *
- * Created on: 31 марта 2015 г.
- * Author: osipov
- */
- #ifndef SPI_PORT_HPP_
- #define SPI_PORT_HPP_
- #include "board.h"
- template <int spiN>
- void spi_init() {
- }
- class Sensor;
- class Bus {
- protected:
- Bus() : mSensCnt(0) {};
- u8 mSensCnt;
- public:
- virtual ~Bus() {};
- void attachSensor(Sensor* sens) {mSensors[mSensCnt++] = sens;};
- Sensor* mSensors[3];
- virtual void update() = 0;
- };
- template <int spiN>
- class SpiBus: public Bus {
- public:
- static SpiBus& getInstance() { // http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
- static SpiBus instanse;
- return instanse;
- }
- SpiBus() {
- spi_init<spiN>();
- }
- void update() {};
- };
- class Sensor {
- public:
- Sensor() :
- mOnbus(SpiBus<2>::getInstance()) {
- mOnbus.attachSensor(this);
- };
- Bus& mOnbus;
- };
- class Axis {
- Sensor mSource;
- // Actuator mBackForce;
- int mValue;
- };
- class manipulator {
- private:
- Axis mAxis[3];
- };
- template <>
- void spi_init<2>() {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- // RCC_AHB1PeriphClockCmd(SPI3_CS_GPIO_CLK, ENABLE);
- /**SPI2 GPIO Configuration
- PB13 ------> SPI2_SCK
- PB14 ------> SPI2_MISO
- PB15 ------> SPI2_MOSI
- */
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- #endif /* SPI_PORT_HPP_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement