55 lines
No EOL
1.9 KiB
C
55 lines
No EOL
1.9 KiB
C
#include <stm32f10x.h>
|
|
#include <MySPI.h>
|
|
#include <GPIO.h>
|
|
|
|
void MySPI_Init(SPI_TypeDef * SPI){
|
|
if (SPI == SPI1) {
|
|
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; // Enable horloge
|
|
SPI->CR1 |= SPI_CR1_BR; // Mise FSCK a 281kHz pour SP1
|
|
// Initialisationdes PORTS IOs
|
|
MyGPIO_Init(GPIOA,4,Out_Ppull);
|
|
MyGPIO_Init(GPIOA,5,AltOut_Ppull);
|
|
MyGPIO_Init(GPIOA,6,In_Floating);
|
|
MyGPIO_Init(GPIOA,7,AltOut_Ppull);
|
|
}
|
|
else if (SPI == SPI2) {
|
|
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
|
|
SPI->CR1 |= SPI_CR1_BR_1 | SPI_CR1_BR_2;
|
|
MyGPIO_Init(GPIOB,12,Out_Ppull);
|
|
MyGPIO_Init(GPIOB,13,AltOut_Ppull);
|
|
MyGPIO_Init(GPIOB,14,In_Floating);
|
|
MyGPIO_Init(GPIOB,15,AltOut_Ppull);
|
|
}
|
|
SPI->CR1 |= SPI_CR1_CPOL; // Mettre le SCK a 1 au repose
|
|
SPI->CR1 |= SPI_CR1_CPHA; // Mettre SCK en rising edge
|
|
SPI->CR1 |= SPI_CR1_SSM; // NSS géré par software
|
|
SPI->CR1 |= SPI_CR1_SSI; // pour permettre de gerer nss par software
|
|
if (SPI == SPI1) MyGPIO_Set(GPIOA,4); // Met NSS a etat haut quand on est pas en transmission ou reception
|
|
else if (SPI == SPI2) MyGPIO_Set(GPIOB,12);
|
|
SPI->CR1 |= SPI_CR1_MSTR; // Mise en mode maitre du SPI
|
|
SPI->CR1 |= SPI_CR1_SPE; // Enable SPI
|
|
}
|
|
|
|
void MySPI_Send(char ByteToSend){
|
|
while (!(SPI1->SR & SPI_SR_TXE)){} // Attend que DR soit vide
|
|
SPI1->DR = ByteToSend; // On met notre donnée dans DR
|
|
while (!(SPI1->SR & SPI_SR_RXNE)){} // Attente de réception du byte poubelle sur MISO
|
|
while (SPI1->SR & SPI_SR_BSY){}
|
|
char poubelle = SPI1->DR; // Je pense qu'on pourrait clear RXNE a la place : SPI1->SR &=~ SPI_SR_RXNE
|
|
}
|
|
|
|
char MySPI_Read(void){
|
|
while (!(SPI1->SR & SPI_SR_TXE)){} // Attend que DR soit vide
|
|
SPI1->DR = 0x00; // Pour trasmettre la clock
|
|
while (!(SPI1->SR & SPI_SR_RXNE)) {} // On attend de recevoir dans buffer de reception le byte
|
|
while (SPI1->SR & SPI_SR_BSY){}
|
|
return SPI1->DR;
|
|
}
|
|
|
|
void MySPI_Set_NSS(void){
|
|
MyGPIO_Set(GPIOA,4);
|
|
}
|
|
|
|
void MySPI_Clear_NSS(void){
|
|
MyGPIO_Reset(GPIOA,4);
|
|
} |