Advertisement
doktar_sha

Untitled

Mar 31st, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. /*
  2.  * spi_port.hpp
  3.  *
  4.  *  Created on: 31 марта 2015 г.
  5.  *      Author: osipov
  6.  */
  7.  
  8. #ifndef SPI_PORT_HPP_
  9. #define SPI_PORT_HPP_
  10.  
  11. #include "board.h"
  12. template <int spiN>
  13. void spi_init() {
  14.  
  15. }
  16.  
  17.  
  18. class Sensor;
  19. class Bus {
  20. protected:
  21.     Bus() : mSensCnt(0) {};
  22.     u8 mSensCnt;
  23. public:
  24.     virtual ~Bus() {};
  25.     void attachSensor(Sensor* sens) {mSensors[mSensCnt++] = sens;};
  26.     Sensor* mSensors[3];
  27.     virtual void update() = 0;
  28. };
  29.  
  30. template <int spiN>
  31. class SpiBus: public Bus {
  32. public:
  33.     static SpiBus& getInstance() { // http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
  34.         static SpiBus instanse;
  35.         return instanse;
  36.     }
  37.     SpiBus() {
  38.         spi_init<spiN>();
  39.     }
  40.     void update() {};
  41. };
  42.  
  43. class Sensor {
  44. public:
  45.     Sensor() :
  46.         mOnbus(SpiBus<2>::getInstance()) {
  47.         mOnbus.attachSensor(this);
  48.     };
  49.     Bus& mOnbus;
  50. };
  51.  
  52. class Axis {
  53.     Sensor mSource;
  54. //  Actuator mBackForce;
  55.     int mValue;
  56. };
  57.  
  58. class manipulator {
  59. private:
  60.     Axis mAxis[3];
  61.  
  62. };
  63.  
  64. template <>
  65. void spi_init<2>() {
  66.       GPIO_InitTypeDef GPIO_InitStructure;
  67.  
  68.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  69.  
  70.       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  71. //    RCC_AHB1PeriphClockCmd(SPI3_CS_GPIO_CLK, ENABLE);
  72.  
  73.       /**SPI2 GPIO Configuration
  74.         PB13     ------> SPI2_SCK
  75.         PB14     ------> SPI2_MISO
  76.         PB15     ------> SPI2_MOSI
  77.       */
  78.  
  79.       GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
  80.       GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
  81.       GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
  82.  
  83.       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  84.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  85.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  86.       GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  87.  
  88.       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  89.       GPIO_Init(GPIOB, &GPIO_InitStructure);
  90.  
  91. }
  92.  
  93.  
  94.  
  95. #endif /* SPI_PORT_HPP_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement