daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f4_discovery.h"
  3.  
  4. /* Private variables ---------------------------------------------------------*/
  5.  
  6. /* Private functions ---------------------------------------------------------*/
  7. // write to SPI1
  8. void SPI1_Write(int8_t data)
  9. {
  10.   while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}
  11.   SPI_I2S_SendData(SPI1, data);
  12.  
  13.   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}
  14.   SPI_I2S_ReceiveData(SPI1);
  15. }
  16.  
  17. // read from SPI1
  18. int8_t SPI1_Read()
  19. {
  20.   while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}
  21.   SPI_I2S_SendData(SPI1, 0x00);
  22.  
  23.   while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}
  24.   return SPI_I2S_ReceiveData(SPI1);
  25. }
  26.  
  27. void delay(uint32_t delay) {
  28.   while(delay--);
  29. }
  30.  
  31. volatile int8_t data[2];
  32.  
  33. void initSPI1(void)
  34. {
  35.   // RCC
  36.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  37.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  38.   // PA5, PA6, PA7 for MISO, MOSI, and SCLK
  39.   GPIO_InitTypeDef GPIO_InitStructure;
  40.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  41.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  42.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  43.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  44.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  45.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  46.  
  47.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
  48.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
  49.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
  50.  
  51.   // SPI1 INIT
  52.   SPI_InitTypeDef SPI_InitStruct;
  53.   SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  54.   SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
  55.   SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
  56.   SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
  57.   SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
  58.   SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
  59.   SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  60.   SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
  61.   SPI_InitStruct.SPI_CRCPolynomial = 7;
  62.   SPI_Init(SPI1, &SPI_InitStruct);
  63.  
  64.   // SPI1 ENABLE
  65.   SPI_Cmd(SPI1, ENABLE);
  66. }
  67.  
  68. void initMotionSensor()
  69. {
  70.   // RCC
  71.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  72.   // PE3 for slave select
  73.   GPIO_InitTypeDef SS;
  74.   SS.GPIO_Pin = GPIO_Pin_3;
  75.   SS.GPIO_Mode = GPIO_Mode_OUT;
  76.   SS.GPIO_OType = GPIO_OType_PP;
  77.   SS.GPIO_PuPd = GPIO_PuPd_NOPULL;
  78.   SS.GPIO_Speed = GPIO_Speed_2MHz;
  79.   GPIO_Init(GPIOE, &SS);
  80.   GPIO_SetBits(GPIOE, GPIO_Pin_3);
  81.  
  82.   // configure and start sensor
  83.   delay(500);
  84.   GPIO_ResetBits(GPIOE, GPIO_Pin_3);
  85.   //Pisanje 0x47 na naslov 0x20
  86.   SPI1_Write(0x20);
  87.   SPI1_Write(0x47);
  88.   GPIO_SetBits(GPIOE, GPIO_Pin_3);
  89.   delay(500);
  90. }
  91.  
  92. void initUSART1()
  93. {
  94.   // RCC
  95.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  96.   // USART1 init
  97.   USART_InitTypeDef USART_InitStruct;
  98.   USART_InitStruct.USART_BaudRate = 115200;
  99.   USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  100.   USART_InitStruct.USART_StopBits = USART_StopBits_1;
  101.   USART_InitStruct.USART_Parity = USART_Parity_No;
  102.   USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  103.   USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  104.   USART_Init(USART1, &USART_InitStruct);
  105.   // USART1 enable
  106.   USART_Cmd(USART1, ENABLE);
  107.   // PB6 and PB7 for USART1 Tx and Rx
  108.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  109.   GPIO_InitTypeDef GPIO_InitStructure;
  110.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  111.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  112.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  113.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  114.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  115.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  116.  
  117.   //GPIO pini funkcija
  118.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
  119.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
  120. }
  121.  
  122. void initDMA2()
  123. {
  124.   // RCC
  125.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  126.   // init DMA2
  127.   DMA_InitTypeDef DMA_InitStructure;
  128.   DMA_InitStructure.DMA_Channel = DMA_Channel_4;
  129.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  130.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) data; //Sprememba
  131.   DMA_InitStructure.DMA_BufferSize = 2;
  132.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  133.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  134.   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART1->DR); //Sprememba
  135.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  136.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  137.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  138.   DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  139.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  140.   //DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  141.   //DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  142.   DMA_Init(DMA2_Stream7, &DMA_InitStructure);           //Stream??
  143.   // enable USART1 DMA Tx
  144.   USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
  145.   // enable DMA2 stream
  146.   DMA_Cmd(DMA2_Stream7, ENABLE);
  147. }
  148.  
  149. /**
  150.   * @brief   Main program
  151.   * @param  None
  152.   * @retval None
  153.   */
  154. int main(void)
  155. {
  156.   // SPI1
  157.   initSPI1();
  158.  
  159.   // LIS motion sensor
  160.   initMotionSensor();
  161.  
  162.   // USART1
  163.   initUSART1();
  164.  
  165.   // DMA2
  166.   initDMA2();
  167.  
  168.   while (1)
  169.   {
  170.     // read x
  171.     GPIO_ResetBits(GPIOE, GPIO_Pin_3);
  172.     SPI1_Write(0x29 | 0x80);
  173.     data[0] = SPI1_Read();
  174.     GPIO_SetBits(GPIOE, GPIO_Pin_3);
  175.     delay(500);
  176.     // read y
  177.     GPIO_ResetBits(GPIOE, GPIO_Pin_3);
  178.     SPI1_Write(0x2B | 0x80);
  179.     data[1] = SPI1_Read();
  180.     GPIO_SetBits(GPIOE, GPIO_Pin_3);
  181.    
  182.   }
  183. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top