diff --git a/Services/Plateau.c b/Services/Plateau.c index 058cfec..3d315b2 100644 --- a/Services/Plateau.c +++ b/Services/Plateau.c @@ -4,6 +4,7 @@ #include #include #include +#include void handler_USART1 (void) { //Pour le projet : Lancer PWM en fonction de la valeur du curseur @@ -18,7 +19,6 @@ void handler_USART1 (void) { MyGPIO_Reset(GPIOA, 1); Mytimer_PWM_cycle(TIM3, 3, data); } - } void Plateau_Init (void){ diff --git a/drivers/MySPI.c b/drivers/MySPI.c new file mode 100644 index 0000000..5469808 --- /dev/null +++ b/drivers/MySPI.c @@ -0,0 +1,39 @@ +#include +#include +#include + +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->APB2ENR |= 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_MSTR; // Mise en mode maitre du SPI + 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_CPOL; // Mettre le SCK a 1 au repose + SPI->CR1 |= SPI_CR1_CPHA; // Mettre SCK en rising edge + SPI->CR1 |= SPI_CR1_SPE; // Enable SPI +} + +void MySPI_Send(char ByteToSend){ + MyGPIO_Reset(GPIOA,4); // Il faudrait utiliser la fonction clear NSS + SPI1->DR = ByteToSend; + while (!(SPI1->SR & SPI_SR_TXE)){} + while (SPI1->SR & SPI_SR_BSY){} + MyGPIO_Set(GPIOA,4); // Il faudrait utiliser la fonction set NSS +} \ No newline at end of file diff --git a/drivers/MySPI.h b/drivers/MySPI.h index b5f9aef..f1ceb9b 100644 --- a/drivers/MySPI.h +++ b/drivers/MySPI.h @@ -2,7 +2,7 @@ #ifndef INC_MYSPI_H_ #define INC_MYSPI_H_ -#include "stm32f10x.h" +#include /************************************************************************************* ===================== By Periph team INSA GEI 2022 ===========================