Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ******************************************************************************
- SPI - Serial Peripheral Interface
- ******************************************************************************
- */
- /* Includes */
- #include <stddef.h>
- #include "stm32f10x.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_spi.h"
- void initGPIO(void);
- void initSPI(void);
- void SPI_WriteRead(SPI_TypeDef *SPIx, uint8_t *txBuff, uint8_t *rxBuff, int count);
- uint8_t tx_buff[10] = {1, 3, 5, 7, 9, 11, 13, 17}; //registar koji saljemo
- uint8_t rx_buff[10]; //registar u koji primamo podatke
- int main(void)
- {
- //inicijalizacija
- initGPIO();
- initSPI();
- //ciklocko ponavljanje
- while(1){
- GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); //palimo ledicu
- SPI_WriteRead(SPI1, tx_buff, rx_buff, 5); //saljemo prvih 5 podataka
- GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); //gasimo ledicu
- }
- }
- //GPIO za SPI1
- void initGPIO(void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- //MOSI PA7
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- //MISO PA6
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- //SCLK PA5
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_RESET); //postavlja se u neaktivnu razinu (neaktivan u 0)
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- //SS PA4
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); //postavlja se u neaktivnu razinu (neaktivan u 1)
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- //SPI1
- void initSPI(void) {
- SPI_InitTypeDef SPI_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
- SPI_StructInit(&SPI_InitStruct);
- SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
- SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; //vecina spi 8b
- SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; //reagira na 1. brid
- SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; //neaktivan u 0
- SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; //Ukoliko je brzina uređaja s kojim komuniciramo 3MHz (72/3 = 24 => prvi veci 32)
- SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; //rucno kontroliranje
- //SPI_InitStruct.SPI_CRCPolynomial => CRC ispravak pogrešaka kod prijenosa podataka
- SPI_Init(SPI1, &SPI_InitStruct);
- SPI_Cmd(SPI1, ENABLE);
- }
- void SPI_WriteRead(SPI_TypeDef *SPIx, uint8_t *txBuff, uint8_t *rxBuff, int count){
- SPI_I2S_SendData(SPIx, *txBuff++); //saljemo 1.
- for(int i = 0; i < count - 1; i++) { //saljemo count-1
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == 0); //cekamo da se svi podaci uspjesno porenesu (da flag bude 1)
- SPI_I2S_SendData(SPIx, *txBuff++);
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == 0);
- *rxBuff++ = SPI_I2S_ReceiveData(SPIx);
- }
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == 0); //saljemo zadnji
- *rxBuff++ = SPI_I2S_ReceiveData(SPIx);
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET); //provjera dal je sve zavrseno
- }
Add Comment
Please, Sign In to add comment