Init commit
This commit is contained in:
parent
fd1090fb67
commit
f99f8a0b59
4 changed files with 311 additions and 0 deletions
74
Drivers/Driver_GPIO.c
Normal file
74
Drivers/Driver_GPIO.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include "Driver_GPIO.h"
|
||||
|
||||
void MyGPIO_Activate(int nb_GPIO) {
|
||||
//activer la clock du GPIO donné
|
||||
RCC->APB2ENR |= (0x01 << (nb_GPIO +1)) ;
|
||||
//RCC->APB2ENR |= (0x01 << 2 | 0x01 << 3 | 0x01 << 4) ;
|
||||
}
|
||||
|
||||
/*
|
||||
void MyGPIO_Desactivate(int nb_GPIO) {
|
||||
//désactiver le GPIO donné
|
||||
RCC->APB2ENR &= ~(0x01 << (nb_GPIO +1)) ;
|
||||
}*/
|
||||
|
||||
//la structure est déjà remplie
|
||||
void MyGPIO_Init(MyGPIO_Struct_TypeDef * GPIOStructPtr) {
|
||||
//màj crl si on veut un pull up ou pull down
|
||||
//on reset la conf
|
||||
|
||||
if(GPIOStructPtr->GPIO_Pin > 7){
|
||||
GPIOStructPtr->GPIO->CRH &= ~(0xF << ((GPIOStructPtr->GPIO_Pin*4) - (8*4)));
|
||||
|
||||
//on met la bonne conf
|
||||
if (GPIOStructPtr->GPIO_Conf == In_PullUp) {
|
||||
//on est en pull up -> on met à 1 par défaut
|
||||
GPIOStructPtr->GPIO->CRH |= (In_PullDown << ((GPIOStructPtr->GPIO_Pin*4) - (8*4)));
|
||||
MyGPIO_Set(GPIOStructPtr->GPIO, GPIOStructPtr->GPIO_Pin);
|
||||
}
|
||||
else {
|
||||
GPIOStructPtr->GPIO->CRH |= (GPIOStructPtr->GPIO_Conf << ((GPIOStructPtr->GPIO_Pin*4) - (8*4)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
GPIOStructPtr->GPIO->CRL &= ~(0xF << (GPIOStructPtr->GPIO_Pin*4));
|
||||
|
||||
//on met la bonne conf
|
||||
if (GPIOStructPtr->GPIO_Conf == In_PullUp) {
|
||||
//on est en pull up -> on met à 1 par défaut
|
||||
GPIOStructPtr->GPIO->CRL |= (In_PullDown << (GPIOStructPtr->GPIO_Pin*4));
|
||||
MyGPIO_Set(GPIOStructPtr->GPIO, GPIOStructPtr->GPIO_Pin);
|
||||
}
|
||||
else {
|
||||
GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf << (GPIOStructPtr->GPIO_Pin * 4));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
||||
return (GPIO->IDR & (1 << GPIO_Pin)) ;
|
||||
}
|
||||
|
||||
void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin){
|
||||
//GPIO->ODR |= (1 << GPIO_Pin);
|
||||
GPIO->BSRR = (1 << GPIO_Pin) ;
|
||||
}
|
||||
|
||||
void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
||||
//GPIO->ODR &= ~(1 << GPIO_Pin);
|
||||
//d'après la data sheet, il faut écrire dans la partie des bits compris entre 16 et 31
|
||||
GPIO->BSRR = (1 << (GPIO_Pin + 16)) ;
|
||||
}
|
||||
|
||||
void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin){
|
||||
//voir si il y a une meilleur manière
|
||||
if (GPIO->ODR & (1 << GPIO_Pin)) {
|
||||
//dans ce cas c'est activé donc on le désactive
|
||||
MyGPIO_Reset(GPIO, GPIO_Pin);
|
||||
}
|
||||
else {
|
||||
//c'était désactivé donc on l'active
|
||||
MyGPIO_Set(GPIO, GPIO_Pin);
|
||||
}
|
||||
}
|
29
Drivers/Driver_GPIO.h
Normal file
29
Drivers/Driver_GPIO.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef MYGPIO_H
|
||||
#define MYGPIO_H
|
||||
#include "stm32f10x.h"
|
||||
|
||||
typedef struct {
|
||||
GPIO_TypeDef * GPIO ;
|
||||
char GPIO_Pin ;
|
||||
char GPIO_Conf ;
|
||||
} MyGPIO_Struct_TypeDef ;
|
||||
|
||||
#define In_Floating 0x4
|
||||
#define In_PullDown 0x8
|
||||
#define In_PullUp 0x9 // on le définit nous même
|
||||
#define In_Analog 0x0
|
||||
#define Out_Ppull 0x2
|
||||
#define Out_OD 0x6
|
||||
#define AltOut_Ppull 0xA
|
||||
#define AltOut_OD 0xE
|
||||
|
||||
void MyGPIO_Init(MyGPIO_Struct_TypeDef * GPIOStructPtr);
|
||||
//renvoie 0 ou autre chose différent de 0
|
||||
int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin) ;
|
||||
void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin);
|
||||
void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin);
|
||||
void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin);
|
||||
void MyGPIO_Activate(int nb_GPIO) ;
|
||||
//void MyGPIO_Desactivate(int nb_GPIO) ;
|
||||
|
||||
#endif
|
144
Drivers/Driver_TIMER.c
Normal file
144
Drivers/Driver_TIMER.c
Normal file
|
@ -0,0 +1,144 @@
|
|||
#include "Driver_TIMER.h"
|
||||
|
||||
//réservation d'un espace mémoire pour un pointeur de fonction associé à chacun des timers
|
||||
void (* IT_function_TIM1) (void) = 0x0;
|
||||
void (* IT_function_TIM2) (void) = 0x0;
|
||||
void (* IT_function_TIM3) (void) = 0x0;
|
||||
void (* IT_function_TIM4) (void) = 0x0;
|
||||
|
||||
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer) {
|
||||
Timer -> Timer -> ARR = Timer -> ARR -1;
|
||||
Timer -> Timer -> PSC = Timer -> PSC -1;
|
||||
}
|
||||
|
||||
void Activate_TIM(int i) {
|
||||
if (i==1) {
|
||||
//le timer1 est sur apb2enr
|
||||
RCC-> APB2ENR |= 0x01 << 11 ;
|
||||
}
|
||||
else {
|
||||
//les autres timers sont sur apb1enr
|
||||
RCC-> APB1ENR |= 0x01 << (i-2) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//-----------------Partie interruptions-----------------//
|
||||
//////////////////////////////////////////////////////////
|
||||
void MyTimer_Active_IT ( TIM_TypeDef * Timer , char Prio , void (* IT_function) (void)) {
|
||||
//active l'interruption sur timer et pointe vers la fonction IT_function avec la priorité prio
|
||||
|
||||
//"Trigger DMA request enabled"
|
||||
Timer -> DIER |= 1;
|
||||
|
||||
if (Timer == TIM1) {
|
||||
NVIC_EnableIRQ(TIM1_TRG_COM_IRQn);
|
||||
NVIC_SetPriority(TIM1_TRG_COM_IRQn, Prio);
|
||||
IT_function_TIM1 = IT_function ;
|
||||
}
|
||||
else if (Timer == TIM2) {
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
NVIC_SetPriority(TIM2_IRQn, Prio);
|
||||
IT_function_TIM2 = IT_function ;
|
||||
}
|
||||
else if (Timer == TIM3) {
|
||||
NVIC_EnableIRQ(TIM3_IRQn);
|
||||
NVIC_SetPriority(TIM3_IRQn, Prio);
|
||||
IT_function_TIM3 = IT_function ;
|
||||
}
|
||||
else if (Timer == TIM4) {
|
||||
NVIC_EnableIRQ(TIM4_IRQn);
|
||||
NVIC_SetPriority(TIM4_IRQn, Prio);
|
||||
IT_function_TIM4 = IT_function ;
|
||||
}
|
||||
}
|
||||
|
||||
void TIM1_TRG_COM_IRQHandler(void) {
|
||||
//on met à 0 le flag d'interruption
|
||||
TIM1->SR &= ~1;
|
||||
if (IT_function_TIM1)
|
||||
(* IT_function_TIM1) ();
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler(void) {
|
||||
//on met à 0 le flag d'interruption
|
||||
TIM2->SR &= ~1;
|
||||
if(IT_function_TIM2)
|
||||
(* IT_function_TIM2) ();
|
||||
}
|
||||
|
||||
void TIM3_IRQHandler(void) {
|
||||
//on met à 0 le flag d'interruption
|
||||
TIM3->SR &= ~1;
|
||||
if(IT_function_TIM3)
|
||||
(* IT_function_TIM3) ();
|
||||
}
|
||||
|
||||
void TIM4_IRQHandler(void) {
|
||||
//on met à 0 le flag d'interruption
|
||||
TIM4->SR &= ~1;
|
||||
if(IT_function_TIM4)
|
||||
(* IT_function_TIM4) ();
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//----------------------Partie PWM----------------------//
|
||||
//////////////////////////////////////////////////////////
|
||||
void MyTimer_PWM(TIM_TypeDef * Timer, char Channel) {
|
||||
if (Timer == TIM1) {
|
||||
TIM1 -> BDTR |= 1 << 15 ;
|
||||
}
|
||||
|
||||
switch (Channel) {
|
||||
case 1 :
|
||||
//On choisit le mode 1
|
||||
//on écrit donc '110' dans le registre OC1M de TIM1_CCMR1
|
||||
Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0;
|
||||
Timer->CCMR1 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2;
|
||||
//On autorise le registre de preload correspondant en écrivant 1 dans OC1PE
|
||||
Timer->CCMR1 |= TIM_CCMR1_OC1PE ;
|
||||
//On autorise la sortie OCx en mettant à 1 le registre CCxE
|
||||
Timer->CCER |= TIM_CCER_CC1E;
|
||||
break ;
|
||||
case 2 :
|
||||
Timer->CCMR1 &= ~TIM_CCMR1_OC2M_0;
|
||||
Timer->CCMR1 |= TIM_CCMR1_OC2M_1| TIM_CCMR1_OC2M_2;
|
||||
Timer->CCMR1 |= TIM_CCMR1_OC2PE ;
|
||||
Timer->CCER |= TIM_CCER_CC2E;
|
||||
break ;
|
||||
case 3 :
|
||||
Timer->CCMR2 &= ~TIM_CCMR2_OC3M_0;
|
||||
Timer->CCMR2 |= TIM_CCMR2_OC3M_1| TIM_CCMR2_OC3M_2;
|
||||
Timer->CCMR2 |= TIM_CCMR2_OC3PE ;
|
||||
Timer->CCER |= TIM_CCER_CC3E;
|
||||
break ;
|
||||
case 4 :
|
||||
Timer->CCMR2 &= ~TIM_CCMR2_OC4M_0;
|
||||
Timer->CCMR2 |= TIM_CCMR2_OC4M_1| TIM_CCMR2_OC4M_2;
|
||||
Timer->CCMR2 |= TIM_CCMR2_OC4PE ;
|
||||
Timer->CCER |= TIM_CCER_CC4E;
|
||||
break ;
|
||||
}
|
||||
//on autorise le registre de auto-reload preload en écrivant 1 dans le registre ARPE de CR1
|
||||
Timer -> CR1 |= TIM_CR1_ARPE ;
|
||||
}
|
||||
|
||||
void MyTimer_PWM_set_cycle(TIM_TypeDef * Timer, float rapport, char channel) {
|
||||
switch (channel) {
|
||||
case 1 :
|
||||
Timer->CCR1 = (int) (Timer -> ARR * rapport) ;
|
||||
break ;
|
||||
case 2 :
|
||||
Timer->CCR2 = (int) (Timer -> ARR * rapport) ;
|
||||
break ;
|
||||
case 3 :
|
||||
Timer->CCR3 = (int) (Timer -> ARR * rapport) ;
|
||||
break ;
|
||||
case 4 :
|
||||
Timer->CCR4 = (int) (Timer -> ARR * rapport) ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
64
Drivers/Driver_TIMER.h
Normal file
64
Drivers/Driver_TIMER.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
#ifndef MYTIMER_H
|
||||
#define MYTIMER_H
|
||||
|
||||
#include "stm32f10x.h"
|
||||
|
||||
typedef struct {
|
||||
TIM_TypeDef * Timer ;
|
||||
uint16_t ARR ;
|
||||
uint16_t PSC ;
|
||||
} MyTimer_Struct_TypeDef ;
|
||||
|
||||
/*
|
||||
*****************************************************************************************
|
||||
* @brief
|
||||
* @param -> Paramètre sous forme d’une structure (son adresse) contenant les informations de base
|
||||
* @Note -> Fonction à lancer systématiquement avant d’aller plus en détail dans les conf plus fines
|
||||
***********************************************************************************************
|
||||
*/
|
||||
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer) ;
|
||||
|
||||
void Activate_TIM(int) ;
|
||||
|
||||
|
||||
/*
|
||||
**************************************************************************************************
|
||||
* @brief
|
||||
* @param : - TIM_TypeDef * Timer : Timer concerné
|
||||
- char Prio : de 0 a 15
|
||||
* @Note : La fonction MyTimer_Base_Init doit avoir été lancée au préalable
|
||||
**************************************************************************************************
|
||||
*/
|
||||
void MyTimer_Active_IT ( TIM_TypeDef * , char , void (*) (void)) ;
|
||||
|
||||
void TIM1_TRG_COM_IRQHandler(void) ;
|
||||
void TIM2_IRQHandler(void) ;
|
||||
void TIM3_IRQHandler(void) ;
|
||||
void TIM4_IRQHandler(void) ;
|
||||
|
||||
/*
|
||||
**************************************************************************************************
|
||||
* @brief
|
||||
* @param : - TIM_TypeDef * Timer : Timer concerné
|
||||
* - char Channel : canal concerné, de 1 a 4
|
||||
* @Note : Active le channel spécifié sur le timer spécifié, la gestion de la configuration I/O n’est
|
||||
* pas faite dans cette fonction ni le réglage de la période de la PWM (ARR, PSC)
|
||||
**************************************************************************************************
|
||||
*/
|
||||
void MyTimer_PWM( TIM_TypeDef *, char) ;
|
||||
|
||||
|
||||
/*
|
||||
**************************************************************************************************
|
||||
* @brief
|
||||
* @param : - TIM_TypeDef * Timer : Timer concerné
|
||||
* - float rapport : rapport cyclique, de 0 à 1
|
||||
* - char Channel : canal concerné, de 1 a 4
|
||||
**************************************************************************************************
|
||||
*/
|
||||
void MyTimer_PWM_set_cycle(TIM_TypeDef *, float, char) ;
|
||||
|
||||
|
||||
#define MyTimer_Base_Start(Timer) (Timer->CR1 |= 0x1)
|
||||
#define MyTimer_Base_Stop(Timer) (Timer->CR1 &= ~0x1)
|
||||
#endif
|
Loading…
Reference in a new issue