Ajouter des commentaires au code

This commit is contained in:
Oskar Orvik 2025-11-09 19:00:46 +01:00
parent a54c43911f
commit 1283e0f110
6 changed files with 35 additions and 43 deletions

Binary file not shown.

View file

@ -1,20 +1,21 @@
#include "stm32f10x.h" #include "stm32f10x.h"
#include <stdlib.h> #include <stdlib.h>
int ChercherEtat(GPIO_TypeDef * GPIO, int pin){ int ChercherEtat(GPIO_TypeDef * GPIO, int pin){ // Trouvons la valeur d'un broche sur un certain GPIO
return((GPIO -> IDR & (0x01 << pin))); return((GPIO -> IDR & (0x01 << pin)));
} }
void ResetBroche(uint32_t GPIO, int Broche){ void ResetBroche(uint32_t GPIO, int Broche){ // Mettre à zero d'un certain broche d'un certain GPIO
GPIO -> BSRR |= BSBroche; GPIO -> BSRR |= BSBroche;
} }
void SetBroche(uint32_t GPIO, int Broche){ void SetBroche(uint32_t GPIO, int Broche){ // Mettre à zero d'un certain broche d'un certain GPIO
GPIO -> BSRR |= BSBroche << 16; GPIO -> BSRR |= BSBroche << 16;
} }
void ConfigureGPIO(uint32_t GPIO, int Broche, int IO, char Mode){ void ConfigureGPIO(uint32_t GPIO, int Broche, int IO, char Mode){ // Mettre un broche d'un GPIO sur un mode
//Start clock // Possble de améliorer avec des int à la place de string
//Start clock pour les GPIO concernés
if(GPIO == GPIOA){ if(GPIO == GPIOA){
RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN; RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN;
} }
@ -27,7 +28,7 @@ else if(GPIO == GPIOC){
else if(GPIO == GPIOD){ else if(GPIO == GPIOD){
RCC -> APB2ENR |= RCC_APB2ENR_IOPDEN; RCC -> APB2ENR |= RCC_APB2ENR_IOPDEN;
} }
// Cas d'u CRL, broche 0 à 7
if (Broche < 8) { if (Broche < 8) {
GPIO -> CRL &= ~(0x1 << Broche *4) & ~(0x1 << Broche *4 +1) & ~(0x1 << Broche *4 + 2) & ~(0x1 << Broche *4 + 3); // Clean bits GPIO -> CRL &= ~(0x1 << Broche *4) & ~(0x1 << Broche *4 +1) & ~(0x1 << Broche *4 + 2) & ~(0x1 << Broche *4 + 3); // Clean bits
if (IO == 0){ //Input if (IO == 0){ //Input
@ -38,6 +39,7 @@ else if(GPIO == GPIOD){
GPIO -> CRL |= (0x1 << 6*4 + 1); GPIO -> CRL |= (0x1 << 6*4 + 1);
} }
else { else {
return; // Mode invalid
} }
} }
else if ( IO < 5) { // Output else if ( IO < 5) { // Output
@ -53,6 +55,7 @@ else if(GPIO == GPIOD){
GPIO -> CRL |= (0x1 << Broche * 4) | (0x1 << Broche * 4 + 1); GPIO -> CRL |= (0x1 << Broche * 4) | (0x1 << Broche * 4 + 1);
} }
else { else {
return; // Mode invalid
} }
} }
else{ else{
@ -68,7 +71,8 @@ else if(GPIO == GPIOD){
else if (strcmp(Mode,"Pull-Up") || strcmp(Mode,"Pull-Down")){ else if (strcmp(Mode,"Pull-Up") || strcmp(Mode,"Pull-Down")){
GPIO -> CRH |= (0x1 << 6*4 + 1); GPIO -> CRH |= (0x1 << 6*4 + 1);
} }
else { else {
return; // Mode invalid or doesn't exist
} }
} }
else if ( IO < 5) { // Output else if ( IO < 5) { // Output
@ -83,6 +87,7 @@ else if(GPIO == GPIOD){
GPIO -> CRH |= (0x1 << Broche * 4) | (0x1 << Broche * 4 + 1); GPIO -> CRH |= (0x1 << Broche * 4) | (0x1 << Broche * 4 + 1);
} }
else { else {
return; // Mode invalid or doesn't exits
} }
} }
else{ else{

View file

@ -3,6 +3,7 @@
#include "Timer.h" #include "Timer.h"
#include "PWM.h" #include "PWM.h"
#include "GPIO.h" #include "GPIO.h"
// Variables
#define ARR_TIM1 0xFFAD #define ARR_TIM1 0xFFAD
#define PSC_TIM1 0xFF #define PSC_TIM1 0xFF
#define ARR_TIM2 0xFFAD #define ARR_TIM2 0xFFAD
@ -11,7 +12,6 @@
#define PSC_TIM3 0x0 #define PSC_TIM3 0x0
volatile int g_tick_count; volatile int g_tick_count;
void Test(void){ void Test(void){
// Signal // Signal
g_tick_count++; g_tick_count++;
@ -20,10 +20,10 @@ void Test(void){
void ConfigureBroches(){ void ConfigureBroches(){
ConfigureGPIO(GPIOA, 6, 4, Push-Pull); ConfigureGPIO(GPIOA, 6, 4, Push-Pull); // Cela équivaut à :
//GPIOA->CRL &= ~(0xF << 6*4); // clean pin 6 // GPIOA->CRL &= ~(0xF << 6*4); // clean pin 6
//GPIOA->CRL |= (0xB << 6*4); // Alternate Function output, Push-Pull max 50 Hz // GPIOA->CRL |= (0xB << 6*4); // Alternate Function output, Push-Pull max 50 Hz
// Exemples d'_tilisation
//Mettre Broche 5 sur input Pull-up/down //Mettre Broche 5 sur input Pull-up/down
ConfigureGPIO(GPIOC, 3, 0, Pull-Up); ConfigureGPIO(GPIOC, 3, 0, Pull-Up);
//GPIOC ->CRL &= ~(0xF << (4 *3)); //GPIOC ->CRL &= ~(0xF << (4 *3));
@ -51,9 +51,7 @@ void ConfigureIT(){
//MyTimer_ActiveIT(TIM2, 4, Test); //start interruption with priority 4 //MyTimer_ActiveIT(TIM2, 4, Test); //start interruption with priority 4
//MyTimer_ActiveIT(TIM1, 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 MyTimer_ActiveIT(TIM3, 4, Test); //start interruption with priority 4
} }
void ConfigurePWM(){ void ConfigurePWM(){
MyTimer_PWM(TIM1, 1); MyTimer_PWM(TIM1, 1);
MyTimer_Set_DutyCycle(TIM1, 1, 20.0); MyTimer_Set_DutyCycle(TIM1, 1, 20.0);

View file

@ -1,10 +1,10 @@
#include "stm32f10x.h" #include "stm32f10x.h"
void ConfigHorloge(void) { void ConfigHorloge(void) { // Peut-être redondant ??
RCC->APB2ENR |= (0x01 << 2) | (0x01 << 3) | (0x01 << 4) | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_TIM1EN; RCC->APB2ENR |= (0x01 << 2) | (0x01 << 3) | (0x01 << 4) | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_TIM1EN;
}; };
void ConfigBroche(void){ void ConfigBroche(void){ //
//Mettre Broche 5 GPIOA à output push-pull //Mettre Broche 5 GPIOA à output push-pull
GPIOA ->CRL &= ~(0x1 << (5*4 + 2)); //0x44144444; GPIOA ->CRL &= ~(0x1 << (5*4 + 2)); //0x44144444;
GPIOA ->CRL |= (0x1 << 5*4); //0x44144444; GPIOA ->CRL |= (0x1 << 5*4); //0x44144444;
@ -17,16 +17,15 @@ void ConfigBroche(void){
GPIOA ->CRH |= (0x1 ); GPIOA ->CRH |= (0x1 );
}; };
int BoutonAppuye(void){ int BoutonAppuye(void){ // Peut être modifié avec ChercherEtat
return((GPIOA -> IDR & (0x01 << 9))); return((GPIOA -> IDR & (0x01 << 9)));
//return(ChercherEtat(GPIOA, 9));
} }
void AllumerLED(void){ void AllumerLED(void){
GPIOA -> ODR &= ~(0x1 << 8); GPIOA -> ODR &= ~(0x1 << 8); // Peut être modifié avec SetBroche
} }
void EteindreLED(void){ void EteindreLED(void){
GPIOA -> ODR |= (0x1 << 8); GPIOA -> ODR |= (0x1 << 8); // Peut être modifié avec ResetBroche
} }
void TogglePin(GPIO_TypeDef*GPIO, int Broche){ void TogglePin(GPIO_TypeDef*GPIO, int Broche){ // Redondant
GPIO -> ODR = GPIO -> ODR ^ (0x1 << Broche); GPIO -> ODR = GPIO -> ODR ^ (0x1 << Broche);
} }

View file

@ -2,7 +2,7 @@
#include "PWM.h" #include "PWM.h"
void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) { void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) { // Activer PWM sur un output
// preload // preload
Timer->CR1 |= TIM_CR1_ARPE; Timer->CR1 |= TIM_CR1_ARPE;
@ -41,17 +41,15 @@ void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) {
Timer->BDTR |= TIM_BDTR_MOE; Timer->BDTR |= TIM_BDTR_MOE;
} }
} }
void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent) { void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent) {
unsigned short ccr_value; unsigned short ccr_value;
// Percentages between 0 and 100 // Percentages between 0 and 100
if (DutyCycle_Percent > 100.0) DutyCycle_Percent = 100.0; if (DutyCycle_Percent > 100.0) DutyCycle_Percent = 100.0;
if (DutyCycle_Percent < 0.0) DutyCycle_Percent = 0.0; if (DutyCycle_Percent < 0.0) DutyCycle_Percent = 0.0;
// calcule of crr // calcule of crr
ccr_value = (unsigned short)((DutyCycle_Percent / 100.0) * (Timer->ARR)); 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) { switch (Channel) {
case 1: case 1:
Timer->CCR1 = ccr_value; Timer->CCR1 = ccr_value;

View file

@ -6,21 +6,20 @@
//RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN; // Enable TIM2 //RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN; // Enable TIM2
//RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable TIM1 //RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable TIM1
void MyTimer_Base_Init( TIM_TypeDef * Timer , unsigned short ValARR , unsigned short ValPSC ) { void MyTimer_Base_Init( TIM_TypeDef * Timer , unsigned short ValARR , unsigned short ValPSC ) { // Configuration du timer
Timer -> PSC=(ValPSC); Timer -> PSC=(ValPSC);
Timer-> ARR = (ValARR); Timer-> ARR = (ValARR);
Timer->EGR |= TIM_EGR_UG; Timer->EGR |= TIM_EGR_UG;
}; };
static void (*p_IT_functions[4])(void); 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)) { void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void)) {
//Enable interruption requisition //Enable interruption requisition
Timer->DIER |= TIM_DIER_UIE; // Update interrupt enable Timer->DIER |= TIM_DIER_UIE; // Update interrupt enable
//Id the interruption timer routine //Id the interruption timer routine
IRQn_Type IRQn; IRQn_Type IRQn;
int timer_index = -1; // Indice pour notre array des pointeurs int timer_index = -1; // Indice pour notre array des pointeurs
@ -56,15 +55,14 @@ void TIM2_IRQHandler(void) {
if (p_IT_functions[0] != 0) { if (p_IT_functions[0] != 0) {
p_IT_functions[0](); // Execute fonction p_IT_functions[0](); // Execute fonction
} }
// TogglePin(GPIOA, 5); // Sans utiliser l'array de pointeurs
}; };
void TIM3_IRQHandler(void) { void TIM3_IRQHandler(void) {
// Clean flag // Clean flag
TIM3->SR &= ~TIM_SR_UIF; TIM3->SR &= ~TIM_SR_UIF;
//Call function //Call function
if (p_IT_functions[1] != 0) { if (p_IT_functions[1] != 0) {
p_IT_functions[1](); // Execute function p_IT_functions[1](); // Execute function
} }
}; };
void TIM4_IRQHandler(void) { void TIM4_IRQHandler(void) {
// Clean flag // Clean flag
@ -74,16 +72,12 @@ void TIM4_IRQHandler(void) {
p_IT_functions[2](); // Execute function p_IT_functions[2](); // Execute function
} }
}; };
// IT PWM // IT PWM
void TIM1_CC_IRQHandler(void) { void TIM1_CC_IRQHandler(void) {
// Clean flag // Clean flag
TIM1 -> DIER &= ~TIM_DIER_CC1IE; TIM1 -> DIER &= ~TIM_DIER_CC1IE;
//Set bit //Set bit
GPIOA -> ODR |= (0x1 << 8); GPIOA -> ODR |= (0x1 << 8);
}; };
void TIM1_UP_IRQHandler(void) { void TIM1_UP_IRQHandler(void) {
@ -94,8 +88,6 @@ void TIM1_UP_IRQHandler(void) {
GPIOA -> ODR &= ~(0x1 << 8); GPIOA -> ODR &= ~(0x1 << 8);
}; };
void EnableTimer(TIM_TypeDef *Timer){ void EnableTimer(TIM_TypeDef *Timer){
if(Timer == TIM2){ if(Timer == TIM2){
RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN; RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN;