Advertisement
phillip_bourdon234

SPI_Driver.c

Feb 28th, 2021
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "GPIO_Driver.h"
  3. #include "SPI_drive.h"
  4. #include "delay.h"
  5.  
  6. void spi_write(char data)
  7. {      
  8.         uint8_t dumby_read = 0;
  9.    
  10.         dumby_read = (uint8_t) SPI1->DR; //this is neccessary to prevent OVR errors
  11.    
  12.         SPI1->DR = data;
  13.  
  14.         while( SPI1->SR != 0x0002 )
  15.         {
  16.             dumby_read = (uint8_t) SPI1->DR;
  17.         }  
  18.        
  19. }
  20.  
  21.  
  22. uint8_t spi_read()
  23. {
  24.     return (uint8_t) SPI1->DR;
  25. }
  26.  
  27. void spi_init_master()
  28. {
  29.     //enable clocks for SPI1, PORTA, and Alternate Function IO
  30.     RCC->APB2ENR |= RCC_APB2ENR_SPI1EN | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN;
  31.    
  32.     //configure the MOSI pin
  33.     GPIO_TYPE MOSI;
  34.     MOSI.port = PORTA;
  35.     MOSI.pin = 7;
  36.     MOSI.mode = OUTPUT;
  37.     MOSI.mode_type = OUTPUT_ALT_FUNCTION;
  38.     MOSI.speed = SPEED_10MHZ;
  39.    
  40.     //configure the MISO pin
  41.     GPIO_TYPE MISO;
  42.     MISO.port = PORTA;
  43.     MISO.pin = 6;
  44.     MISO.mode = INPUT;
  45.     MISO.mode_type = INPUT_PU_PD;
  46.    
  47.     //configure the SCK pin
  48.     GPIO_TYPE SCK;
  49.     SCK.port = PORTA;
  50.     SCK.pin = 5;
  51.     SCK.mode = OUTPUT;
  52.     SCK.mode_type = OUTPUT_ALT_FUNCTION;
  53.     SCK.speed = SPEED_10MHZ;
  54.    
  55.     //initialize all of the pins
  56.    
  57.     init_gpio(MOSI);
  58.     init_gpio(MISO);
  59.     init_gpio(SCK);
  60.    
  61.     //confiure SPI1 for master
  62.     SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0 | SPI_CR1_MSTR;
  63.     SPI1->CR2 |= SPI_CR2_SSOE;
  64.    
  65.     //enable SPI
  66.     SPI1->CR1 |= SPI_CR1_SPE;
  67.  
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement