Added adc and uart

This commit is contained in:
Yohan Boujon 2023-03-31 09:37:30 +02:00
parent bf895ce55a
commit 93198a8dd6
4 changed files with 193 additions and 0 deletions

27
driver-algu/inc/UART.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef UART_H
#define UART_H
#include "stm32f10x.h"
typedef struct {
USART_TypeDef * UART;
unsigned int BaudRate;
unsigned char Wlengh;
unsigned char Wparity;
unsigned char Wstop;
}MyUART_Struct_Typedef;
#define parity_none 0x0
#define parity_even 0x1
#define parity_odd 0x2
#define Wlengh8 0x0
#define Wlengh9 0X1
#define stop1b 0x0
#define stop2b 0x2
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr);
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr);
#endif

91
driver-algu/src/UART.c Normal file
View file

@ -0,0 +1,91 @@
#include "UART.h"
#include "gpio.h"
//faire GPIO
char data1 = 0x00;
char data2 = 0x00;
char data3 = 0x00;
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){
if (UARTStructPtr->UART == USART1)
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
if (UARTStructPtr->UART == USART2)
RCC->APB2ENR |= RCC_APB1ENR_USART2EN;
if (UARTStructPtr->UART == USART3)
RCC->APB2ENR |= RCC_APB1ENR_USART3EN;
UARTStructPtr->UART->BRR = 72000000/(UARTStructPtr->BaudRate);
UARTStructPtr->UART->CR1 |= ((UARTStructPtr->Wlengh)<<12);
UARTStructPtr->UART->CR1 |= (0x1<<10);
if(UARTStructPtr->Wparity == parity_none)
UARTStructPtr->UART->CR1 &= ~(0x1<<10);
if(UARTStructPtr->Wparity == parity_odd)
UARTStructPtr->UART->CR1 |= (0x1<<9);
if(UARTStructPtr->Wparity == parity_even)
UARTStructPtr->UART->CR1 &= ~(0x1<<9);
if(UARTStructPtr->Wstop == stop1b)
UARTStructPtr->UART->CR2 &= ~(0x3<<12);
if(UARTStructPtr->Wstop == stop2b){
UARTStructPtr->UART->CR2 &= ~(0x3<<12);
UARTStructPtr->UART->CR2 |= (0x1<<13);
}
UARTStructPtr->UART->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE);
NVIC_EnableIRQ(USART1_IRQn);
}
void USART1_IRQHandler(void)
{
// Check if receive data register not empty
if(USART1->SR & USART_SR_RXNE)
{
data1 = USART1->DR; // read received data
// do something with received data
}
}
void USART2_IRQHandler(void)
{
// Check if receive data register not empty
if(USART2->SR & USART_SR_RXNE)
{
data2 = USART2->DR; // read received data
// do something with received data
}
}
void USART3_IRQHandler(void)
{
// Check if receive data register not empty
if(USART3->SR & USART_SR_RXNE)
{
data3 = USART3->DR; // read received data
// do something with received data
}
}
char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr)
{
if(UARTStructPtr->UART == USART1)
return data1;
else if(UARTStructPtr->UART == USART2)
return data2;
else if(UARTStructPtr->UART == USART3)
return data3;
else
return 0xFF;
}
/* exemple utilisation fonction :
UART1.UART = USART1; // choix UART (USART1, USART2, USART3)
UART1.BaudRate = 9600; // Vitesse de communication MAX 118200
UART1.Wlengh = Wlengh8; // longeur du mot (Wlengh8 ,Wlengh9)
UART1.Wparity = parity_even; // parité (parity_even, parity_odd, parity_none)
UART1.Wstop = stop1b; // nombre bit de stop (stop1b, stop2b)
MyUART_Init(&UART1); // initialisation
*/

44
driver-siyo/adcdriver.c Normal file
View file

@ -0,0 +1,44 @@
#include "adcdriver.h"
void (* pFncADC) (void); /* déclaration dun pointeur de fonction */
void MyADC_Init_Periph (void (* ptrFonction)(void))
{
pFncADC = ptrFonction; /* affectation du pointeur */
}
void MyADC_Init(MyADC_Struct_TypeDef * ADCStructPtr)
{
RCC->CFGR |= RCC_CFGR_ADCPRE_1; // ADC Prescaler : divided by 6 -> 72MHz to 12MHz
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //We activate the clock first
if(ADCStructPtr->channel < 10) //Cycle and channel selection -> permet de regler en fonction de la résistance donnée
{
ADCStructPtr->ADC->SMPR2 |= (ADCStructPtr->cycle<<(ADCStructPtr->channel*3)); //Channel < 10
}
else {
ADCStructPtr->ADC->SMPR1 |= (ADCStructPtr->cycle<<((ADCStructPtr->channel-10)*3)); //Channel > 10
}
ADCStructPtr->ADC->SQR1 &= ADC_SQR1_L; //Channel sequence length -> 1 conversion (0000)
ADCStructPtr->ADC->SQR3 |= ADCStructPtr->channel; //Sequence Reader, seulement le SQ1 est lu dans notre cas, nous associons un channel à ce dernier.
ADCStructPtr->ADC->CR2 |= ADC_CR2_EXTTRIG; //Activation du trigger externe
ADCStructPtr->ADC->CR2 |= ADC_CR2_EXTSEL; //Event externe choisie : SWSTART
MyADC_ActiveIT(ADCStructPtr->ADC,0);
ADCStructPtr->ADC->CR2 |= ADC_CR2_ADON; //Init l'ADC
MyADC_Base_Start(ADCStructPtr->ADC); //Debut du premier ADC
}
void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio)
{
ADC->CR1 |= ADC_CR1_EOCIE; //Interruption active
NVIC->IP[ADC1_2_IRQn] |= (Prio << 0x4); //Prio de l'interruption (p.197 manuel reference RM0008 pour ADC1_IRQn)
NVIC->ISER[0] |= (0x1<<ADC1_2_IRQn); //Active l'interruption au niveau NVIC (p.119 manuel programming pour ISER[0])
}
void ADC1_2_IRQHandler(void)
{
if (pFncADC != 0)
(*pFncADC) (); /* appel indirect de la fonction */
MyADC_Base_Start(ADC1);
ADC1->SR &= ~ADC_SR_EOC; //Prochaine lecture pouvant être effectuée.
}

31
driver-siyo/adcdriver.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef ADCDRIVER_H
#define ADCDRIVER_H
#include "stm32f10x.h"
#include "gpiodriver.h"
typedef enum{
cycles1d5 = 0b000,
cycles7d5 = 0b001,
cycles13d5 = 0b010,
cycles28d5 = 0b011,
cycles41d5 = 0b100,
cycles55d5 = 0b101,
cycles71d5 = 0b110,
cycles239d5 = 0b111
} MyADC_Cycle_t;
typedef struct
{
ADC_TypeDef * ADC; //ADC 1 or 2
uint8_t channel; //channel 0 -> 17
MyADC_Cycle_t cycle; //channel cycle
} MyADC_Struct_TypeDef;
void MyADC_Init(MyADC_Struct_TypeDef * ADCStructPtr);
void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio);
void MyADC_Init_Periph (void (* ptrFonction)(void));
MyGPIO_Struct_TypeDef GPIOFromADC(MyADC_Struct_TypeDef ADC);
#define MyADC_Base_Start(ADC) (ADC->CR2 |= ADC_CR2_SWSTART)
#endif