Domy131097

[LV6] Mikroračunalni sustavi - SPI

May 5th, 2018
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ******************************************************************************
  3. SPI - Serial Peripheral Interface
  4. ******************************************************************************
  5. */
  6.  
  7. /* Includes */
  8. #include <stddef.h>
  9. #include "stm32f10x.h"
  10. #include "stm32f10x_rcc.h"
  11. #include "stm32f10x_gpio.h"
  12. #include "stm32f10x_spi.h"
  13.  
  14. void initGPIO(void);
  15. void initSPI(void);
  16. void SPI_WriteRead(SPI_TypeDef *SPIx, uint8_t *txBuff, uint8_t *rxBuff, int count);
  17.  
  18. uint8_t tx_buff[10] = {1, 3, 5, 7, 9, 11, 13, 17}; //registar koji saljemo
  19. uint8_t rx_buff[10]; //registar u koji primamo podatke
  20.  
  21. int main(void)
  22. {
  23.     //inicijalizacija
  24.     initGPIO();
  25.     initSPI();
  26.  
  27.     //ciklocko ponavljanje
  28.     while(1){
  29.         GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); //palimo ledicu
  30.         SPI_WriteRead(SPI1, tx_buff, rx_buff, 5); //saljemo prvih 5 podataka
  31.         GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); //gasimo ledicu
  32.     }
  33.  
  34. }
  35.  
  36. //GPIO za SPI1
  37. void initGPIO(void) {
  38.     GPIO_InitTypeDef GPIO_InitStruct;
  39.  
  40.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  41.  
  42.     //MOSI PA7
  43.     GPIO_StructInit(&GPIO_InitStruct);
  44.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
  45.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  46.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  47.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  48.  
  49.     //MISO PA6
  50.     GPIO_StructInit(&GPIO_InitStruct);
  51.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
  52.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  53.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  54.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  55.  
  56.     //SCLK PA5
  57.     GPIO_StructInit(&GPIO_InitStruct);
  58.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
  59.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  60.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  61.     GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_RESET); //postavlja se u neaktivnu razinu (neaktivan u 0)
  62.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  63.  
  64.     //SS PA4
  65.     GPIO_StructInit(&GPIO_InitStruct);
  66.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
  67.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  68.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  69.     GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); //postavlja se u neaktivnu razinu (neaktivan u 1)
  70.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  71.  
  72. }
  73.  
  74. //SPI1
  75. void initSPI(void) {
  76.     SPI_InitTypeDef SPI_InitStruct;
  77.  
  78.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  79.  
  80.     SPI_StructInit(&SPI_InitStruct);
  81.     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  82.     SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
  83.     SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; //vecina spi 8b
  84.     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
  85.     SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; //reagira na 1. brid
  86.     SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; //neaktivan u 0
  87.     SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; //Ukoliko je brzina uređaja s kojim komuniciramo 3MHz (72/3 = 24 => prvi veci 32)
  88.     SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; //rucno kontroliranje
  89.     //SPI_InitStruct.SPI_CRCPolynomial => CRC ispravak pogrešaka kod prijenosa podataka
  90.  
  91.     SPI_Init(SPI1, &SPI_InitStruct);
  92.  
  93.     SPI_Cmd(SPI1, ENABLE);
  94. }
  95.  
  96. void SPI_WriteRead(SPI_TypeDef *SPIx, uint8_t *txBuff, uint8_t *rxBuff, int count){
  97.     SPI_I2S_SendData(SPIx, *txBuff++); //saljemo 1.
  98.  
  99.     for(int i = 0; i < count - 1; i++) { //saljemo count-1
  100.         while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == 0); //cekamo da se svi podaci uspjesno porenesu (da flag bude 1)
  101.         SPI_I2S_SendData(SPIx, *txBuff++);
  102.  
  103.         while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == 0);
  104.         *rxBuff++ = SPI_I2S_ReceiveData(SPIx);
  105.     }
  106.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == 0); //saljemo zadnji
  107.     *rxBuff++ = SPI_I2S_ReceiveData(SPIx);
  108.  
  109.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET); //provjera dal je sve zavrseno
  110. }
RAW Paste Data Copied