Pilotes avec couche service girouette

This commit is contained in:
Oskar Orvik 2025-11-18 10:45:06 +01:00
parent 4b20cbfb0e
commit 277a18fd56
14 changed files with 388 additions and 0 deletions

View file

@ -0,0 +1,14 @@
#include "stm32f10x.h"
#define In_Floating 0x4
#define In_PullDown 0x8
#define In_PullUp 0x8
#define In_Analog 0x0
#define Out_Ppull 0x3
#define Out_OD 0x7
#define AltOut_Ppull 0xB
#define AltOut_OD 0xF
extern void MyGPIO_Init(GPIO_TypeDef * GPIO, char pin, char conf );
extern int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin); // renvoie 0 ou autre chose different de 0
extern void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin);
extern void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin);
extern void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin);

View file

@ -0,0 +1,4 @@
#include "stm32f10x.h"
extern void configEncoder(TIM_TypeDef * Timer);
extern void configChannel();
extern int returnAngle (TIM_TypeDef * Timer);

12
Pilotes/Include/MyTimer.h Normal file
View file

@ -0,0 +1,12 @@
#include "stm32f10x.h"
//TIMERS start
#define MyTimer_Base_Start(Timer) (Timer->CR1 |= TIM_CR1_CEN)
#define MyTimer_Base_Stop(Timer) (Timer -> CR1 =(0x0))
// IT
extern volatile int g_tick_count; // Declara que a variável existe em outro arquivo
void Test(void);
void ConfigureIT();
// PWM
void ConfigurePWM();
void ConfigureTimers();

8
Pilotes/Include/Nucleo.h Normal file
View file

@ -0,0 +1,8 @@
#include "stm32f10x.h"
// Config
extern void ConfigHorloge(void);
extern void ConfigBroche(void);
// Gestion des IO Spesifiques
extern int BoutonAppuye(void);
extern void AllumerLED(void);
extern void EteindreLED(void);

4
Pilotes/Include/PWM.h Normal file
View file

@ -0,0 +1,4 @@
#include "stm32f10x.h"
// Config
extern void MyTimer_PWM(TIM_TypeDef *Timer, char Channel);
extern void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent);

12
Pilotes/Include/Timer.h Normal file
View file

@ -0,0 +1,12 @@
#include "stm32f10x.h"
// Config de timer
extern void MyTimer_Base_Init(TIM_TypeDef *Timer , unsigned short ValARR , unsigned short ValPSC );
extern void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void));
// Fonctions d'interruption
extern void TIM2_IRQHandler(void);
extern void TIM3_IRQHandler(void);
extern void TIM4_IRQHandler(void);
extern void TIM1_CC_IRQnHandler(void);
extern void TIM1_UP_IRQnHandler(void);
// Enable timers
void EnableTimer(TIM_TypeDef *Timer);

BIN
Pilotes/Include/test.h Normal file

Binary file not shown.

BIN
Pilotes/Source/ADC.c Normal file

Binary file not shown.

View file

@ -0,0 +1,71 @@
#include "stm32f10x.h"
#define In_Floating 0x4
#define In_PullDown 0x8
#define In_PullUp 0x8
#define In_Analog 0x0
#define Out_Ppull 0x3
#define Out_OD 0x7
#define AltOut_Ppull 0xB
#define AltOut_OD 0xF
void MyGPIO_Init(GPIO_TypeDef * GPIO, char pin, char conf ){
int shift_pin;
//Start clock for relevant GPIO
if(GPIO == GPIOA){
RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN;
}
else if(GPIO == GPIOB){
RCC -> APB2ENR |= RCC_APB2ENR_IOPBEN;
}
else if(GPIO == GPIOC){
RCC -> APB2ENR |= RCC_APB2ENR_IOPCEN;
}
else if(GPIO == GPIOD){
RCC -> APB2ENR |= RCC_APB2ENR_IOPDEN;
}
if(pin < 8){//CRL zone
shift_pin = pin*4;
GPIO -> CRL &= ~(0xF << shift_pin);
//PullUp and PullDown have the same conf number, so we need to change the ODR to diferenciate them both
if(conf == In_PullUp){
GPIO -> CRL |= ( In_PullUp << shift_pin);
GPIO -> ODR |= (1<<pin);
}
else if(conf == In_PullDown){
GPIO -> CRL |= ( In_PullDown << shift_pin);
GPIO -> ODR &= ~(1<<pin);
}
else{
GPIO -> CRL |= ( conf << shift_pin);
}
}
else{//CRH zone
shift_pin = (pin-8)*4;
GPIO -> CRH &= ~(0xF << shift_pin);
if(conf == In_PullUp){
GPIO -> CRH |= ( In_PullUp << shift_pin);
GPIO -> ODR |= (1<<pin);
}
else if(conf == In_PullDown){
GPIO -> CRH |= ( In_PullDown << shift_pin);
GPIO -> ODR &= ~(1<<pin);
}
else{
GPIO -> CRH |= ( conf << shift_pin);
}
}
}
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 -> BSRR = (1<<GPIO_Pin);//1 on set zone
}
void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin){
GPIO -> BSRR = (1<<(GPIO_Pin+16));//1 on reset zone
}
void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin){
GPIO -> ODR = GPIO -> ODR ^ (0x1 << GPIO_Pin);
}

View file

@ -0,0 +1,30 @@
#include "stm32f10x.h"
#include "MyTimer.h"
#include "Nucleo.h"
#include "Timer.h"
#include "DriverGPIO.h"
#include "Girouette.h"
#define POSITIONS 4*360
void configEncoder(TIM_TypeDef * Timer){
Timer -> CCMR1 |= TIM_CCMR1_CC1S; // TI1FP1 mapped on TI1
Timer -> CCMR1 |= TIM_CCMR1_CC2S; // TI1FP2 mapped on TI2
Timer -> CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP); // TI1FP1 output non-inverted
Timer -> CCMR1 &= ~(TIM_CCMR1_IC1F); // Input capture 1 filter, no filter
Timer -> CCER &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP); // TI1FP2 output non-inverted
Timer -> CCMR2 &= ~(TIM_CCMR1_IC2F); // Input capture 2 filter, no filter
Timer -> SMCR &= ~TIM_SMCR_SMS; // Reset SMS-bits
Timer -> SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;// SMS = "011"
Timer -> CR1 |= TIM_CR1_CEN; // Enable counter
Timer -> ARR = 0xFD20; // Setting ARR as 1440*45
}
void configChannel(){
MyGPIO_Init(GPIOA,0,In_Floating ); // GPIOA pin 0 in mode floating TIM2_CH1
MyGPIO_Init(GPIOA,1,In_Floating ); // GPIOA pin 1 in mode floating TIM2_CH2
MyGPIO_Init(GPIOA,8,In_Floating ); // GPIOA pin 7 in mode floating Index
}
int returnAngle (TIM_TypeDef * Timer){
return(Timer -> CNT / POSITIONS * 360);
}

39
Pilotes/Source/MyTimer.c Normal file
View file

@ -0,0 +1,39 @@
#include "stm32f10x.h"
#include "Timer.h"
#include "MyTimer.h"
#include "PWM.h"
#include "DriverGPIO.h"
// Variables
#define ARR_TIM1 0xFFAD
#define PSC_TIM1 0xFF
#define ARR_TIM2 0xFFAD
#define PSC_TIM2 0x0225
#define ARR_TIM3 0x2CF
#define PSC_TIM3 0x0
volatile int g_tick_count;
void Test(void){
// Signal
g_tick_count++;
MyGPIO_Toggle(GPIOA, 8);
}
void ConfigureTimers(){
// Config ARR & PSC
//MyTimer_Base_Init(TIM2, ARR_TIM2, PSC_TIM2);
MyTimer_Base_Init(TIM1, ARR_TIM1, PSC_TIM1);
MyTimer_Base_Init(TIM3, ARR_TIM2, PSC_TIM2);
// Enable timer clock
EnableTimer(TIM1);
EnableTimer(TIM2);
EnableTimer(TIM3);
}
void ConfigureIT(){ // Activate general interuption with a function and priority
//MyTimer_ActiveIT(TIM2, 4, Test); //start interruption with priority 4
//MyTimer_ActiveIT(TIM1, 4, Test); //start interruption with priority 4
MyTimer_ActiveIT(TIM3, 4, Test); //start interruption with priority 4
}
void ConfigurePWM(){ // Set dutycycle with timer
MyTimer_PWM(TIM1, 1);
MyTimer_Set_DutyCycle(TIM1, 1, 20.0);
}

25
Pilotes/Source/Nucleo.c Normal file
View file

@ -0,0 +1,25 @@
#include "stm32f10x.h"
#include "../Include/Nucleo.h"
#include "DriverGPIO.h"
void ConfigHorloge(void) { // Peut-être redondant ??
RCC->APB2ENR |= (0x01 << 2) | (0x01 << 3) | (0x01 << 4) | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_TIM1EN;
};
void ConfigBroche(void){ //
//Mettre Broche 5 GPIOA à output push-pull
MyGPIO_Init(GPIOA, 5, Out_Ppull);
//Mettre broche 8 sur GPIOA à output open drain
MyGPIO_Init(GPIOA, 8, Out_OD);
};
int BoutonAppuye(void){ // Peut être modifié avec ChercherEtat
return(MyGPIO_Read(GPIOA, 9));
}
void AllumerLED(void){
MyGPIO_Set(GPIOA, 8);
}
void EteindreLED(void){
MyGPIO_Reset(GPIOA, 8);
}

67
Pilotes/Source/PWM.c Normal file
View file

@ -0,0 +1,67 @@
#include "stm32f10x.h"
#include "../Include/PWM.h"
void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) { // Activer PWM sur un output
// preload
Timer->CR1 |= TIM_CR1_ARPE;
switch (Channel) {
case 1:
// Config o channel 1 in "PWM Mode 1" and enable preload of CCR1
Timer->CCMR1 &= ~TIM_CCMR1_OC1M; // clean bit modes
Timer->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; // Mode PWM 1
Timer->CCMR1 |= TIM_CCMR1_OC1PE; // enable preload
// enable exit 1 (Output enable)
Timer->CCER |= TIM_CCER_CC1E;
break;
case 2:
Timer->CCMR1 &= ~TIM_CCMR1_OC2M;
Timer->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1;
Timer->CCMR1 |= TIM_CCMR1_OC2PE;
Timer->CCER |= TIM_CCER_CC2E;
break;
case 3:
Timer->CCMR2 &= ~TIM_CCMR2_OC3M;
Timer->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1;
Timer->CCMR2 |= TIM_CCMR2_OC3PE;
Timer->CCER |= TIM_CCER_CC3E;
break;
case 4:
Timer->CCMR2 &= ~TIM_CCMR2_OC4M;
Timer->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1;
Timer->CCMR2 |= TIM_CCMR2_OC4PE;
Timer->CCER |= TIM_CCER_CC4E;
break;
}
// special case(specific timers)
if (Timer == TIM1 || Timer == TIM8) {
Timer->BDTR |= TIM_BDTR_MOE;
}
}
void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent) {
unsigned short ccr_value;
// Percentages between 0 and 100
if (DutyCycle_Percent > 100.0) DutyCycle_Percent = 100.0;
if (DutyCycle_Percent < 0.0) DutyCycle_Percent = 0.0;
// calcule of crr
ccr_value = (unsigned short)((DutyCycle_Percent / 100.0) * (Timer->ARR));
// Assigner le valaur pour le registre de comparison pour le channel qui est pertient
switch (Channel) {
case 1:
Timer->CCR1 = ccr_value;
break;
case 2:
Timer->CCR2 = ccr_value;
break;
case 3:
Timer->CCR3 = ccr_value;
break;
case 4:
Timer->CCR4 = ccr_value;
break;
}
}

102
Pilotes/Source/Timer.c Normal file
View file

@ -0,0 +1,102 @@
#include "stm32f10x.h"
#include "../Include/Timer.h"
//REMEMBER TO ENALBLE TIMERS
//EXAMPLES
//RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN; // Enable TIM2
//RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable TIM1
void MyTimer_Base_Init( TIM_TypeDef * Timer , unsigned short ValARR , unsigned short ValPSC ) { // Configuration du timer
Timer -> PSC=(ValPSC);
Timer-> ARR = (ValARR);
Timer->EGR |= TIM_EGR_UG;
};
static void (*p_IT_functions[4])(void); // Pour créer l'array des fonctions
void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void)) {
//Enable interruption requisition
Timer->DIER |= TIM_DIER_UIE; // Update interrupt enable
//Id the interruption timer routine
IRQn_Type IRQn;
int timer_index = -1; // Indice pour notre array des pointeurs
if (Timer == TIM2) {
IRQn = TIM2_IRQn;
timer_index = 0;
} else if (Timer == TIM3) {
IRQn = TIM3_IRQn;
timer_index = 1;
} else if (Timer == TIM4) {
IRQn = TIM4_IRQn;
timer_index = 2;
}
// Keep the pointer of the valid timer function
if (timer_index != -1) {
p_IT_functions[timer_index] = IT_function; // index the function
} else {
return; // Timer invalid
}
// set interruption priority
NVIC_SetPriority(IRQn, Prio);
// Enable routine
NVIC_EnableIRQ(IRQn);
};
void TIM2_IRQHandler(void) {
// Clean flag
TIM2->SR &= ~TIM_SR_UIF; // Drapeau d'interuption
//Call function
if (p_IT_functions[0] != 0) {
p_IT_functions[0](); // Execute fonction
}
};
void TIM3_IRQHandler(void) {
// Clean flag
TIM3->SR &= ~TIM_SR_UIF;
//Call function
if (p_IT_functions[1] != 0) {
p_IT_functions[1](); // Execute function
}
};
void TIM4_IRQHandler(void) {
// Clean flag
TIM4->SR &= ~TIM_SR_UIF;
//Call function
if (p_IT_functions[2] != 0) {
p_IT_functions[2](); // Execute function
}
};
// IT PWM
void TIM1_CC_IRQHandler(void) {
// Clean flag
TIM1 -> DIER &= ~TIM_DIER_CC1IE;
//Set bit
GPIOA -> ODR |= (0x1 << 8);
};
void TIM1_UP_IRQHandler(void) {
// Clean flag
TIM1-> DIER &= ~TIM_DIER_TIE;
//Reset bit
GPIOA -> ODR &= ~(0x1 << 8);
};
void EnableTimer(TIM_TypeDef *Timer){
if(Timer == TIM2){
RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN;
}
else if(Timer == TIM3){
RCC -> APB1ENR |= RCC_APB1ENR_TIM3EN;
}
else if(Timer == TIM4){
RCC -> APB1ENR |= RCC_APB1ENR_TIM4EN;
}
else if(Timer == TIM1){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
}
else{
}
}