2021-02-25 04:00 AM
Hi everyone,
So, i would like to test SPI1 communication on my stm32F103, using just the clock and MOSI line by sending 0x55 data on MOSI line ,
but when i see the traces on logic analyzer, i'm getting a weird values !
as seen in this capture picture, furthemore the clock signal is not symetric,
This is my code , if anyone has suggestion
Thank you .
#include "stm32f10x.h" // Device header
void Config_GPIO(void);
void SPI_config(void);
void DelayTimerUs(int n);
int main(){
Config_GPIO();
SPI_config();
while(1){
while ( (SPI1->SR & SPI_SR_TXE)==0){}
SPI1->DR = 0x55;
DelayTimerUs(1000);
}
}
void Config_GPIO(void){
// Enable RCC for APB2
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
// Ports configuration (AF)
//PA5-->clock AF push-pull
GPIOA->CRL |= GPIO_CRL_CNF5_1;
GPIOA->CRL &=~ GPIO_CRL_CNF5_0;
GPIOA->CRL |= GPIO_CRL_MODE5_0 | GPIO_CRL_MODE5_1;
//PA7-->MOSI AF full-duplex Master AF push-pull
GPIOA->CRL |= GPIO_CRL_CNF7_1;
GPIOA->CRL &=~ GPIO_CRL_CNF7_0;
GPIOA->CRL |= GPIO_CRL_MODE7_0 | GPIO_CRL_MODE7_1;
//PA6-->MISO AF Input floating/Input pull-up
GPIOA->CRL &=~ GPIO_CRL_CNF6_1;
GPIOA->CRL |= GPIO_CRL_CNF6_0;
GPIOA->CRL &=~ GPIO_CRL_MODE6_0 | GPIO_CRL_MODE6_1;
//PA4-->NSS AF push-pull
GPIOA->CRL |= GPIO_CRL_CNF4_1;
GPIOA->CRL &=~ GPIO_CRL_CNF4_0;
GPIOA->CRL |= GPIO_CRL_MODE4_0 | GPIO_CRL_MODE4_1;
}
void SPI_config(void){
SPI1->CR1 &=~ SPI_CR1_BIDIMODE; //2 line selected
SPI1->CR1 |= SPI_CR1_BIDIOE; // Transmitter
SPI1->CR1 &=~ SPI_CR1_DFF;
SPI1->CR1 &=~SPI_CR1_RXONLY ; // Full duplex (Transmit and receive)
SPI1->CR1 &=~ SPI_CR1_SSM ; // Software slave management disabled
SPI1->CR1 |= SPI_CR1_LSBFIRST;
// Baud rate control
SPI1->CR1 &=~ SPI_CR1_BR_0 ; // Set BR=32Khz 100 Fpclk=72Mhz/128
SPI1->CR1 |= SPI_CR1_BR_2 |SPI_CR1_BR_1 ;
SPI1->CR1 |= SPI_CR1_MSTR; // Master configuration
SPI1->CR2 |= SPI_CR2_SSOE;
SPI1->CR1 &=~ SPI_CR1_CPHA | SPI_CR1_CPOL;
// Interruption flag for RX and TX enabled
SPI1->CR2 |= SPI_CR2_TXEIE | SPI_CR2_RXNEIE;
// SPI1 enabled
SPI1->CR1 |= SPI_CR1_SPE;
}
void DelayTimerUs(int n){
// 1 Mhz ---> 1Us
//ARR = n (n=1 --> 1us) (n=2 --> 2us) ....
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = 36;
TIM2->ARR = n;
TIM2->CNT = 0;
TIM2->CR1 = TIM_CR1_CEN;
for( int i =0; i<n; i++){
while (!(TIM2->SR & (1<<0))) {} // while UIF flag =0
}
TIM2->SR &=~ (1<<0);
TIM2->CR1 &=~ TIM_CR1_CEN;
}