diff --git a/MDK-ARM/Project.uvoptx b/MDK-ARM/Project.uvoptx index 542aa50..b46b6c8 100644 --- a/MDK-ARM/Project.uvoptx +++ b/MDK-ARM/Project.uvoptx @@ -345,7 +345,7 @@ 0 DLGDARM - (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=-1,-1,-1,-1,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=-1,-1,-1,-1,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) + (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=100,129,658,654,0)(121=-1,-1,-1,-1,0)(122=1005,156,1563,681,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=-1,-1,-1,-1,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=1238,79,1832,588,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) 0 @@ -382,9 +382,9 @@ 0 0 - 46 + 39 1 -
134219406
+
134224104
0 0 0 @@ -393,16 +393,44 @@ 1 ../Src/main.c - \\NUCLEO_F103RB\../Src/main.c\46 + \\NUCLEO_F103RB\../Src/main.c\39 +
+ + 1 + 0 + 40 + 1 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + ../Src/main.c + +
+ + + 0 + 1 + adcRaw,0x0A + + + 1 + 1 + adcVolt + + 0 0 1 - 1 + 0 0 0 0 @@ -438,6 +466,13 @@ + + + 0 + ((PORTA & 0x00000200) >> 9 & 0x200) >> 9 + 00800000000000000000000000000000E0FFEF400100000000000000000000000000000028504F5254412026203078303030303032303029203E3E2039000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000F03F170000000000000000000000000000000000000000010008 + + 1 0 @@ -618,6 +653,18 @@ 0 0 + + 2 + 14 + 1 + 0 + 0 + 0 + ..\Services\Scheduler.c + Scheduler.c + 0 + 0 + @@ -628,7 +675,7 @@ 0 3 - 14 + 15 1 0 0 @@ -640,9 +687,9 @@ 3 - 15 + 16 1 - 0 + 1 0 0 ..\MyDrivers\ADC.c @@ -652,7 +699,7 @@ 3 - 16 + 17 1 0 0 @@ -664,9 +711,9 @@ 3 - 17 + 18 1 - 0 + 1 0 0 ..\MyDrivers\USART.c @@ -684,7 +731,7 @@ 0 4 - 18 + 19 1 0 0 @@ -696,7 +743,7 @@ 4 - 19 + 20 1 0 0 @@ -708,7 +755,7 @@ 4 - 20 + 21 1 0 0 @@ -718,6 +765,42 @@ 0 0 + + 4 + 22 + 1 + 0 + 0 + 0 + ..\LLDrivers\src\stm32f1xx_ll_gpio.c + stm32f1xx_ll_gpio.c + 0 + 0 + + + 4 + 23 + 1 + 0 + 0 + 0 + ..\LLDrivers\src\stm32f1xx_ll_usart.c + stm32f1xx_ll_usart.c + 0 + 0 + + + 4 + 24 + 1 + 0 + 0 + 0 + ..\LLDrivers\src\stm32f1xx_ll_adc.c + stm32f1xx_ll_adc.c + 0 + 0 + @@ -728,7 +811,7 @@ 0 5 - 21 + 25 5 0 0 @@ -748,7 +831,7 @@ 0 6 - 22 + 26 1 0 0 @@ -768,7 +851,7 @@ 0 7 - 23 + 27 2 0 0 diff --git a/MDK-ARM/Project.uvprojx b/MDK-ARM/Project.uvprojx index 881272c..254be84 100644 --- a/MDK-ARM/Project.uvprojx +++ b/MDK-ARM/Project.uvprojx @@ -185,7 +185,6 @@ 0 0 0 - 0 0 0 8 @@ -352,7 +351,7 @@ 0 0 0 - 4 + 0 @@ -453,6 +452,11 @@ 1 ..\Services\Voltage.c + + Scheduler.c + 1 + ..\Services\Scheduler.c + @@ -498,6 +502,21 @@ 1 ..\LLDrivers\src\stm32f1xx_ll_tim.c + + stm32f1xx_ll_gpio.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_gpio.c + + + stm32f1xx_ll_usart.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_usart.c + + + stm32f1xx_ll_adc.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_adc.c + @@ -539,7 +558,7 @@ Simulateur 0x4 ARM-ADS - 5060750::V5.06 update 6 (build 750)::.\ARMCC + 5060750::V5.06 update 6 (build 750)::ARMCC 0 @@ -714,7 +733,6 @@ 0 0 0 - 0 0 0 8 @@ -881,7 +899,7 @@ 0 0 0 - 4 + 0 @@ -982,6 +1000,11 @@ 1 ..\Services\Voltage.c + + Scheduler.c + 1 + ..\Services\Scheduler.c + @@ -1027,6 +1050,21 @@ 1 ..\LLDrivers\src\stm32f1xx_ll_tim.c + + stm32f1xx_ll_gpio.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_gpio.c + + + stm32f1xx_ll_usart.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_usart.c + + + stm32f1xx_ll_adc.c + 1 + ..\LLDrivers\src\stm32f1xx_ll_adc.c + @@ -1080,19 +1118,4 @@ - - - - <Project Info> - - - - - - 0 - 1 - - - - diff --git a/MyDrivers/ADC.c b/MyDrivers/ADC.c index e305f04..7fbd2d0 100644 --- a/MyDrivers/ADC.c +++ b/MyDrivers/ADC.c @@ -1 +1,52 @@ #include "ADC.h" +#include "stm32f1xx_ll_bus.h" // Pour horloge + +void ADC_conf(ADC_TypeDef *adc, uint32_t voie) +{ + if (adc == ADC1) { + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); + } else if (adc == ADC2) { + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC2); + } + + // Division de la frequence + RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; + + // Fixe le nombre de conversion à 1 + adc->SQR1&= ADC_SQR1_L; + + // Indique la voie a convertir + adc->SQR3|= voie; + + // Calibration + adc->CR2 |= ADC_CR2_CAL_Msk; + while ((adc->CR2 & ADC_CR2_CAL_Msk)); +} + + +void ADC_start(ADC_TypeDef *adc) +{ + adc->CR2 |= ADC_CR2_ADON; +} + + +uint16_t ADC_readRaw(ADC_TypeDef *adc) +{ + // Lancement de la conversion + adc->CR2 |= ADC_CR2_ADON; + while(!(ADC1->SR & ADC_SR_EOC)) {} + + return ADC1->DR & ADC_DR_DATA_Msk; +} + + +double ADC_convertToVolt(uint16_t value) +{ + return ((double) value) / 4095.0 * 3.3; +} + + +double ADC_readVolt(ADC_TypeDef *adc) +{ + return ADC_convertToVolt(ADC_readRaw(adc)); +} diff --git a/MyDrivers/ADC.h b/MyDrivers/ADC.h index c4ee01e..b73929d 100644 --- a/MyDrivers/ADC.h +++ b/MyDrivers/ADC.h @@ -1,4 +1,15 @@ #ifndef ADC_H #define ADC_H +#include "stm32f1xx_ll_adc.h" + +void ADC_conf(ADC_TypeDef *adc, uint32_t voie); +void ADC_start(ADC_TypeDef *adc); + +uint16_t ADC_readRaw(ADC_TypeDef *adc); +double ADC_readVolt(ADC_TypeDef *adc); + +double ADC_convertToVolt(uint16_t value); + + #endif diff --git a/MyDrivers/USART.c b/MyDrivers/USART.c index 1273ce3..54706c1 100644 --- a/MyDrivers/USART.c +++ b/MyDrivers/USART.c @@ -1 +1,58 @@ #include "USART.h" +#include "GPIO.h" +#include "stm32f1xx_ll_bus.h" // Pour horloge + + +void Usart_conf(USART_TypeDef *USARTx) { + int txPin; + GPIO_TypeDef *usartGpio; + + if (USARTx == USART1) { + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + txPin = LL_GPIO_PIN_9; + usartGpio = GPIOA; + } else if (USARTx == USART2) { + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + txPin = LL_GPIO_PIN_2; + usartGpio = GPIOA; + } else if (USARTx == USART3) { + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB); + txPin = LL_GPIO_PIN_10; + usartGpio = GPIOB; + } + + LL_USART_InitTypeDef usartInit; + LL_USART_StructInit(&usartInit); + usartInit.DataWidth = LL_USART_DATAWIDTH_8B; + usartInit.BaudRate = 9600; + usartInit.TransferDirection = LL_USART_DIRECTION_TX_RX; + LL_USART_Init(USARTx, &usartInit); + + GPIO_conf(usartGpio, txPin, LL_GPIO_MODE_ALTERNATE, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_UP); +} + + + +void Usart_enable(USART_TypeDef *USARTx) { + LL_USART_Enable(USARTx); +} + + +void sendChar(USART_TypeDef *USARTx, char c) { + LL_USART_TransmitData8(USARTx, c); + while (!LL_USART_IsActiveFlag_TXE(USARTx)) {} +} + + +void Usart_send(USART_TypeDef *USARTx, char *msg, int length) { + for (int i = 0; i < length; i++) { + sendChar(USARTx, msg[i]); + } +} + + + + diff --git a/MyDrivers/USART.h b/MyDrivers/USART.h index b72fa38..3c81502 100644 --- a/MyDrivers/USART.h +++ b/MyDrivers/USART.h @@ -1,5 +1,10 @@ #ifndef USART_H #define USART_H +#include "stm32f1xx_ll_usart.h" + +void Usart_conf(USART_TypeDef *USARTx); +void Usart_enable(USART_TypeDef *USARTx); +void Usart_send(USART_TypeDef *USARTx, char *msg, int length); #endif diff --git a/Src/main.c b/Src/main.c index 1d6d586..77bdaaf 100644 --- a/Src/main.c +++ b/Src/main.c @@ -22,28 +22,36 @@ #include "Sail.h" #include "Scheduler.h" +#include "ADC.h" +#include "GPIO.h" void SystemClock_Config(void); /* Private functions ---------------------------------------------------------*/ int counter = 1; +int adcRaw = 0; +double adcVolt = 0.0; void backgroundTask() { counter++; Sail_background(); + adcRaw = ADC_readRaw(ADC1); + adcVolt = ADC_convertToVolt(adcRaw); } void configurePeripherals() { Sail_conf(); + ADC_conf(ADC1, 12); } void startPeripherals() { Sail_start(); + ADC_start(ADC1); } /** @@ -67,7 +75,7 @@ int main(void) Scheduler_conf(backgroundTask); Scheduler_start(); - + while (1) { }