Init/send SPI

This commit is contained in:
leo 2025-11-22 00:32:52 +01:00
parent 3909057917
commit 69fba14e55
3 changed files with 41 additions and 2 deletions

View file

@ -4,6 +4,7 @@
#include <GPIO.h> #include <GPIO.h>
#include <ADC.h> #include <ADC.h>
#include <USART.h> #include <USART.h>
#include <MySPI.h>
void handler_USART1 (void) { void handler_USART1 (void) {
//Pour le projet : Lancer PWM en fonction de la valeur du curseur //Pour le projet : Lancer PWM en fonction de la valeur du curseur
@ -18,7 +19,6 @@ void handler_USART1 (void) {
MyGPIO_Reset(GPIOA, 1); MyGPIO_Reset(GPIOA, 1);
Mytimer_PWM_cycle(TIM3, 3, data); Mytimer_PWM_cycle(TIM3, 3, data);
} }
} }
void Plateau_Init (void){ void Plateau_Init (void){

39
drivers/MySPI.c Normal file
View file

@ -0,0 +1,39 @@
#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->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
}

View file

@ -2,7 +2,7 @@
#ifndef INC_MYSPI_H_ #ifndef INC_MYSPI_H_
#define INC_MYSPI_H_ #define INC_MYSPI_H_
#include "stm32f10x.h" #include <stm32f10x.h>
/************************************************************************************* /*************************************************************************************
===================== By Periph team INSA GEI 2022 =========================== ===================== By Periph team INSA GEI 2022 ===========================