Added basic library, removed specific ones

This commit is contained in:
Yohan Boujon 2023-03-31 09:44:52 +02:00
parent 93198a8dd6
commit 9564e30a93
12 changed files with 0 additions and 307 deletions

View file

@ -1,27 +0,0 @@
#ifndef MYGPIO_H
#define MYGPIO_H
#include "stm32f10x.h"
typedef struct {
GPIO_TypeDef * GPIO;
char GPIO_Pin; //numero de 0 a 15
char GPIO_Conf; //voir ci dessous
} MyGPIO_Struct_TypeDef;
#define In_Floating 0x4
#define In_PullDown 0x7 //faire
#define In_PullUp 0x8 //faire
#define In_Analog 0x0
#define Out_Ppull 0x2
#define Out_OD 0x6
#define AltOut_Ppull 0xA
#define AltOut_OD 0xE
void MyGPIO_InitClock(void);
void MyGPIO_Init(MyGPIO_Struct_TypeDef * GPIOStructPtr);
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);
#endif

View file

@ -1,79 +0,0 @@
#include "gpio.h"
void MyGPIO_InitClock(void) {
RCC->APB2ENR |= (0x01 << 2) | (0x01 << 3) | (0x01 << 4);
}
void MyGPIO_Init(MyGPIO_Struct_TypeDef * GPIOStructPtr) {
if (GPIOStructPtr->GPIO_Pin >= 8) {
switch (GPIOStructPtr->GPIO_Conf) {
case In_PullDown:
GPIOStructPtr->GPIO->CRH &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin % 8)));
GPIOStructPtr->GPIO->CRH |= (0x8 << (4 * (GPIOStructPtr->GPIO_Pin % 8)));
GPIOStructPtr->GPIO->ODR &= (0x0 << GPIOStructPtr->GPIO_Pin);
break;
case In_PullUp:
GPIOStructPtr->GPIO->CRH &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin % 8)));
GPIOStructPtr->GPIO->CRH |= (0x8 << (4 * (GPIOStructPtr->GPIO_Pin % 8)));
GPIOStructPtr->GPIO->ODR |= (0x1 << GPIOStructPtr->GPIO_Pin);
break;
case In_Floating:
case In_Analog:
case Out_Ppull:
case Out_OD:
case AltOut_Ppull:
case AltOut_OD:
GPIOStructPtr->GPIO->CRH &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin % 8)));
GPIOStructPtr->GPIO->CRH |= (GPIOStructPtr->GPIO_Conf << (4 * (GPIOStructPtr->GPIO_Pin % 8)));
break;
}
}
else {
switch (GPIOStructPtr->GPIO_Conf) {
case In_PullDown:
GPIOStructPtr->GPIO->CRH &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin)));
GPIOStructPtr->GPIO->CRH |= (0x8 << (4 * (GPIOStructPtr->GPIO_Pin)));
GPIOStructPtr->GPIO->ODR &= (0x0 << GPIOStructPtr->GPIO_Pin);
break;
case In_PullUp:
GPIOStructPtr->GPIO->CRL &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin)));
GPIOStructPtr->GPIO->CRL |= (0x8 << (4 * (GPIOStructPtr->GPIO_Pin)));
GPIOStructPtr->GPIO->ODR |= (0x1 << GPIOStructPtr->GPIO_Pin);
break;
case In_Floating:
case In_Analog:
case Out_Ppull:
case Out_OD:
case AltOut_Ppull:
case AltOut_OD:
GPIOStructPtr->GPIO->CRL &= ~(0xF << (4 * (GPIOStructPtr->GPIO_Pin)));
GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf << (4 * (GPIOStructPtr->GPIO_Pin)));
break;
}
}
}
int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin) {
return ((GPIO->IDR & (0x1 << GPIO_Pin)) >> GPIO_Pin);
}
void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin) {
GPIO->ODR |= (0x1 << GPIO_Pin);
}
void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin) {
GPIO->ODR &= (0x0 << GPIO_Pin);
}
void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin) {
if (MyGPIO_Read(GPIO, GPIO_Pin) == 0x1) {
MyGPIO_Reset(GPIO, GPIO_Pin);
}
else {
MyGPIO_Set(GPIO, GPIO_Pin);
}
}

View file

@ -1,162 +0,0 @@
#include "timerdriver.h"
void (* pFnc) (void); /* déclaration dun pointeur de fonction */
void Init_Periph (void (* ptrFonction)(void))
{
pFnc = ptrFonction; /* affectation du pointeur */
}
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer)
{
//TIM1 uses the APB2ENR register from RCC. The others uses the APB1ENR, so we check this value.
if(Timer->Timer == TIM1)
{
RCC->APB2ENR |= TimerX2Int(Timer->Timer);
} else {
RCC->APB1ENR |= TimerX2Int(Timer->Timer);
}
Timer->Timer->ARR = Timer->ARR;
Timer->Timer->PSC = Timer->PSC;
}
void MyTimer_ActiveIT(TIM_TypeDef * TimerX, uint8_t Prio)
{
uint8_t positionTimerIT = TimerIT2UInt(TimerX);
TimerX->DIER |= (0x1<<UIE);
NVIC->IP[positionTimerIT] |= (Prio << 0x4);
NVIC->ISER[0] |= (0x1<<positionTimerIT);
}
void MyTimer_PWM_Init(MyPWM_Struct_TypeDef * PWM)
{
switch(PWM->channel)
{
case 2:
PWM->Timer->CCMR1 |= (PWMMode_1<<OC24M_START); //We activate the PWM Mode 1 for the given channel
PWM->Timer->CCR2 = PWM->CCR; //Compare Capture register count
break;
case 3:
PWM->Timer->CCMR2 |= (PWMMode_1<<OC13M_START);
PWM->Timer->CCR3 = PWM->CCR;
break;
case 4:
PWM->Timer->CCMR2 |= (PWMMode_1<<OC24M_START);
PWM->Timer->CCR4 = PWM->CCR;
break;
default:
PWM->Timer->CCMR1 |= (PWMMode_1<<OC13M_START);
PWM->Timer->CCR1 = PWM->CCR;
break;
}
PWM->Timer->CCER |= (1<<4*(PWM->channel-1)); //enable capture/compare registers
}
MyGPIO_Struct_TypeDef GPIOFromPWM(MyPWM_Struct_TypeDef PWM)
{
//use of C99 compound literal for return statement, may not work on C90.
if(PWM.Timer == TIM2)
{
//PA0 -> TIM2,CH1... iteration
return (MyGPIO_Struct_TypeDef){GPIOA,PWM.channel-1,AltOut_Ppull};
}
else if(PWM.Timer == TIM3)
{
if(PWM.channel > 2) {
return (MyGPIO_Struct_TypeDef){GPIOB,PWM.channel-3,AltOut_Ppull}; //PB0 -> TIM3,CH3;PB1 -> TIM3,CH4
}
else {
return (MyGPIO_Struct_TypeDef){GPIOA,PWM.channel+5,AltOut_Ppull}; //PA6 -> TIM3,CH1;PA7 -> TIM3,CH2
}
}
else if(PWM.Timer == TIM4)
{
return (MyGPIO_Struct_TypeDef){GPIOB,PWM.channel+5,AltOut_Ppull}; //PB6 -> TIM4,CH1... iteration
}
else { //TIM1 case
return (MyGPIO_Struct_TypeDef){GPIOA,PWM.channel+7,AltOut_Ppull};//PA8 -> TIM1,CH1... iteration
}
}
int TimerX2Int(TIM_TypeDef * TimerX)
{
if(TimerX == TIM1)
{
return (0x01 << 11);
} else if (TimerX == TIM2){
return (0x01 << 0);
} else if (TimerX == TIM3){
return (0x01 << 1);
} else if (TimerX == TIM4){
return (0x01 << 2);
} /*else if (TimerX == TIM5){
return (0x01 << 3);
} else if (TimerX == TIM6){
return (0x01 << 4);
} else if (TimerX == TIM7){
return (0x01 << 5);
} else if (TimerX == TIM8){
return (0x01 << 13);
} else if (TimerX == TIM9){ //For now we dont do timer > 4
return (0x01 << 19);
} else if (TimerX == TIM10){
return (0x01 << 20);
} else if (TimerX == TIM11){
return (0x01 << 21);
} else if (TimerX == TIM12){
return (0x01 << 6);
} else if (TimerX == TIM13){
return (0x01 << 7);
} else if (TimerX == TIM14){
return (0x01 << 8);
}*/ else {
return -1;
};
}
uint8_t TimerIT2UInt(TIM_TypeDef * TimerX)
{
if(TimerX == TIM1)
{
return TIM1_CC_IRQn;
} else if(TimerX == TIM2)
{
return TIM2_IRQn;
} else if(TimerX == TIM3)
{
return TIM3_IRQn;
} else if(TimerX == TIM4)
{
return 30;
} else {
return 0;
}
}
void TIM1_CC_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM1->SR &= ~(0x1<<UIF);//reset flag
}
void TIM2_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM2->SR &= ~(0x1<<UIF);//reset flag
}
void TIM3_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM3->SR &= ~(0x1<<UIF);//reset flag
}
void TIM4_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM4->SR &= ~(0x1<<UIF);//reset flag
}

View file

@ -1,39 +0,0 @@
#ifndef TIMERDRIVER_H
#define TIMERDRIVER_H
#include "stm32f10x.h"
#include "gpiodriver.h"
#define CEN 0x0
#define UIE 0x0
#define UIF 0x0
#define MOE 0xF
#define OC13M_START 0x4
#define OC24M_START 0xC
#define PWMMode_1 0x6
typedef struct {
TIM_TypeDef * Timer; //TIM1 -> TIM4
unsigned short ARR;
unsigned short PSC;
} MyTimer_Struct_TypeDef;
typedef struct {
TIM_TypeDef * Timer; //TIM1 -> TIM4
uint8_t channel;
uint16_t CCR;
} MyPWM_Struct_TypeDef;
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer);
int TimerX2Int(TIM_TypeDef * TimerX);
uint8_t TimerIT2UInt(TIM_TypeDef * TimerX);
void MyTimer_ActiveIT(TIM_TypeDef * TimerX, uint8_t Prio);
void Init_Periph (void (* ptrFonction)(void));
void MyTimer_PWM_Init(MyPWM_Struct_TypeDef * PWM);
MyGPIO_Struct_TypeDef GPIOFromPWM(MyPWM_Struct_TypeDef PWM);
#define MyTimer_Base_Start(Timer) (Timer->CR1 |= (0x01<<CEN))
#define MyTimer_Base_Stop(Timer) (Timer->CR1 &= ~(0x01<<CEN))
#define MyPWM_Base_Start(Timer) (Timer->BDTR |= (1<<MOE)) //Main output enable
#define MyPWM_Base_Stop(Timer) (Timer->BDTR &= ~(1<<MOE)) //Main output enable
#endif