Driver SPI v1
This commit is contained in:
parent
69fba14e55
commit
8e0fc18a55
2 changed files with 27 additions and 9 deletions
|
|
@ -20,20 +20,38 @@ void MySPI_Init(SPI_TypeDef * SPI){
|
||||||
MyGPIO_Init(GPIOB,14,In_Floating);
|
MyGPIO_Init(GPIOB,14,In_Floating);
|
||||||
MyGPIO_Init(GPIOB,15,AltOut_Ppull);
|
MyGPIO_Init(GPIOB,15,AltOut_Ppull);
|
||||||
}
|
}
|
||||||
SPI->CR1 |= SPI_CR1_MSTR; // Mise en mode maitre du SPI
|
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_SSM; // NSS géré par software
|
||||||
SPI->CR1 |= SPI_CR1_SSI; // pour permettre de gerer nss 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
|
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);
|
else if (SPI == SPI2) MyGPIO_Set(GPIOB,12);
|
||||||
SPI->CR1 |= SPI_CR1_CPOL; // Mettre le SCK a 1 au repose
|
SPI->CR1 |= SPI_CR1_MSTR; // Mise en mode maitre du SPI
|
||||||
SPI->CR1 |= SPI_CR1_CPHA; // Mettre SCK en rising edge
|
|
||||||
SPI->CR1 |= SPI_CR1_SPE; // Enable SPI
|
SPI->CR1 |= SPI_CR1_SPE; // Enable SPI
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySPI_Send(char ByteToSend){
|
void MySPI_Send(char ByteToSend){
|
||||||
MyGPIO_Reset(GPIOA,4); // Il faudrait utiliser la fonction clear NSS
|
MySPI_Clear_NSS(); // Debut transmission
|
||||||
SPI1->DR = ByteToSend;
|
while (!(SPI1->SR & SPI_SR_TXE)){} // Attend que DR soit vide
|
||||||
while (!(SPI1->SR & SPI_SR_TXE)){}
|
SPI1->DR = ByteToSend; // On met notre donnée dans DR
|
||||||
while (SPI1->SR & SPI_SR_BSY){}
|
while (!(SPI1->SR & SPI_SR_RXNE)){} // Attente de réception du byte poubelle sur MISO
|
||||||
MyGPIO_Set(GPIOA,4); // Il faudrait utiliser la fonction set NSS
|
char poubelle = SPI1->DR; // Je pense qu'on pourrait clear RXNE a la place : SPI1->SR &=~ SPI_SR_RXNE
|
||||||
|
MySPI_Set_NSS(); // Fin transmission
|
||||||
|
}
|
||||||
|
|
||||||
|
char MySPI_Read(void){
|
||||||
|
MySPI_Clear_NSS(); //Début transmission
|
||||||
|
while (!(SPI1->SR & SPI_SR_TXE)){} // Attend que DR soit vide
|
||||||
|
MySPI_Send(0x00); // Pour trasmettre la clock
|
||||||
|
while (!(SPI1->SR & SPI_SR_RXNE)) {} // On attend de recevoir dans buffer de reception le byte
|
||||||
|
MySPI_Set_NSS(); // Fin transmission
|
||||||
|
return SPI1->DR;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MySPI_Set_NSS(void){
|
||||||
|
MyGPIO_Set(GPIOA,4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MySPI_Clear_NSS(void){
|
||||||
|
MyGPIO_Reset(GPIOA,4);
|
||||||
}
|
}
|
||||||
|
|
@ -1 +1 @@
|
||||||
Coucou
|
Fin du drivers SPI, il faudrait maintenant vérifier son fonctionnement.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue