SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. **
  3. **                           Main.c
  4. **
  5. **
  6. **********************************************************************/
  7. /*
  8.    Last committed:     $Revision: 00 $
  9.    Last changed by:    $Author: $
  10.    Last changed date:  $Date:  $
  11.    ID:                 $Id:  $
  12.  
  13. **********************************************************************/
  14. #include "stm32f30x_conf.h"
  15. #include "30021_io.h"
  16. void init_SPI3(){
  17.  
  18.  
  19.  
  20.     // Enable Clocks
  21.     RCC->AHBENR  |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOCEN;    // Enable Clock for GPIO Banks A and B
  22.     RCC->APB1ENR |= RCC_APB1ENR_SPI3EN;                 // Enable Clock for SPI2
  23.  
  24.     // Connect pins to SPI2
  25.     GPIOC->AFR[1] &= ~(GPIO_AFRH_AFRH2); // Clear alternate function for PC10
  26.     GPIOC->AFR[1] |= (6 << 4*2); // Set alternate 5 function for PC10 - SCLK
  27.     GPIOC->AFR[1] &= ~(GPIO_AFRH_AFRH3); // Clear alternate function for PC11
  28.     GPIOC->AFR[1] |= (6 << 4*3);
  29.     GPIOC->AFR[1] &= ~(GPIO_AFRH_AFRH4); // Clear alternate function for PC12
  30.     GPIOC->AFR[1] |= (6 << 4*4);
  31.     GPIOA->AFR[1] &= ~(GPIO_AFRH_AFRH7); // Clear alternate function for PA15
  32.     GPIOA->AFR[1] |= (6 << 4*7);
  33.  
  34.     // Configure pins PB13 and PB15 for 10 MHz alternate function
  35.     GPIOC->OSPEEDR &= ~ (GPIO_OSPEEDER_OSPEEDR10  | GPIO_OSPEEDER_OSPEEDR11 | GPIO_OSPEEDER_OSPEEDR12);  // Clear speed register
  36.     GPIOC->OSPEEDR |=  (GPIO_OSPEEDER_OSPEEDR10_0 | GPIO_OSPEEDER_OSPEEDR11_0 | GPIO_OSPEEDER_OSPEEDR12_0);    // set speed register (0x01 - 10 MHz, 0x02 - 2 MHz, 0x03 - 50 MHz)
  37.     GPIOC->OTYPER  &= ~ (GPIO_OTYPER_OT_10 | GPIO_OTYPER_OT_11 | GPIO_OTYPER_OT_12);        // Clear output type register
  38.     GPIOC->MODER   &= ~ (GPIO_MODER_MODER10 | GPIO_MODER_MODER11 | GPIO_MODER_MODER12);// Clear mode register
  39.     GPIOC->MODER   |=  ((GPIO_MODER_MODER10_1) | (GPIO_MODER_MODER11_1) | (GPIO_MODER_MODER12_1));    // Set mode register (0x00 - Input, 0x01 - Output, 0x02 - Alternate Function, 0x03 - Analog in/out)
  40.     GPIOC->PUPDR   &= ~((GPIO_PUPDR_PUPDR10) | (GPIO_PUPDR_PUPDR11) | (GPIO_PUPDR_PUPDR12));    // Clear push/pull register
  41.     GPIOC->PUPDR   |=  ((GPIO_PUPDR_PUPDR10_1) | (GPIO_PUPDR_PUPDR11_1) | (GPIO_PUPDR_PUPDR12_1));
  42.  
  43.     GPIOA->OSPEEDR &= ~ (GPIO_OSPEEDER_OSPEEDR15);
  44.     GPIOA->OSPEEDR |= (GPIO_OSPEEDER_OSPEEDR15_0);
  45.     GPIOA->OTYPER  &= ~ (GPIO_OTYPER_OT_15);
  46.     GPIOA->MODER   &= ~ (GPIO_MODER_MODER15);
  47.     GPIOA->MODER   |=  (GPIO_MODER_MODER15_0);
  48.     GPIOA->PUPDR   &= ~(GPIO_PUPDR_PUPDR15);
  49.     GPIOA->PUPDR   |=  (GPIO_PUPDR_PUPDR15_1);
  50.  
  51.  
  52.  
  53.     GPIOA->ODR |=  GPIO_ODR_15; // CS = 1
  54.  
  55.     // Configure SPI3
  56.     SPI3->CR1 &= 0x3040; // Clear CR1 Register
  57.     SPI3->CR1 |= 0x0000; // Configure direction (0x0000 - 2 Lines Full Duplex, 0x0400 - 2 Lines RX Only, 0x8000 - 1 Line RX, 0xC000 - 1 Line TX)
  58.     SPI3->CR1 |= 0x0104; // Configure mode (0x0000 - Slave, 0x0104 - Master)
  59.     SPI3->CR1 |= 0x0002; // Configure clock polarity (0x0000 - Low, 0x0002 - High)
  60.     SPI3->CR1 |= 0x0001; // Configure clock phase (0x0000 - 1 Edge, 0x0001 - 2 Edge)
  61.     SPI3->CR1 |= 0x0200; // Configure chip select (0x0000 - Hardware based, 0x0200 - Software based)
  62.     SPI3->CR1 |= 0x0028; // Set Baud Rate Prescaler (0x0000 - 2, 0x0008 - 4, 0x0018 - 8, 0x0020 - 16, 0x0028 - 32, 0x0028 - 64, 0x0030 - 128, 0x0038 - 128)
  63.     SPI3->CR1 |= 0x0000; // Set Bit Order (0x0000 - MSB First, 0x0080 - LSB First)
  64.  
  65.     SPI3->CR2 &= ~0x0F00; // Clear CR2 Register
  66.     SPI3->CR2 |= 0x0700; // Set Number of Bits (0x0300 - 4, 0x0400 - 5, 0x0500 - 6, ...);
  67.     SPI3->I2SCFGR &= ~0x0800; // Disable I2S
  68.  
  69.     SPI3->CR2 &= ~0x1000;
  70.     SPI3->CR2 |= 0x1000; // Configure RXFIFO return at (0x0000 - Half-full (16 bits), 0x1000 - Quarter-full (8 bits))
  71.     SPI3->CR1 |= SPI_CR1_SPE; // Enable SPI3
  72.  
  73.  
  74. }
  75.  
  76. void spi3_transmit_byte(uint8_t data) {
  77.     //GPIOA->ODR &= ~GPIO_ODR_15; // CS = 0 - Start Transmission
  78.     while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) != SET) { }
  79.     SPI3->DR = data;
  80.     while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) != SET) { }
  81.     while(SPI3->SR & SPI_SR_BSY);
  82.     //for (int i=0;i<10000;i++);
  83.     //GPIOA->ODR |=  GPIO_ODR_15; // CS = 1 - End Transmission
  84. }
  85.  
  86. int main(void){
  87.     init_usb_uart(115200);
  88.     init_SPI3();
  89.   //  init_spi_lcd();
  90.     while(1){
  91.  
  92.         printf("Hello world!\n");
  93.         for (int i=0;i<100000;i++){
  94.         }
  95. GPIOA->ODR &= ~ GPIO_ODR_15;
  96. spi3_transmit_byte(0b00110100);
  97. GPIOA->ODR |= GPIO_ODR_15;
  98.  //lcd_transmit_byte('G');
  99.   }
  100. }
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