implement ADC and USART

This commit is contained in:
Yohan Simard 2020-11-09 12:14:55 +01:00
parent 84a3fea450
commit 76fd320df4
7 changed files with 276 additions and 38 deletions

View file

@ -345,7 +345,7 @@
<SetRegEntry>
<Number>0</Number>
<Key>DLGDARM</Key>
<Name>(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)</Name>
<Name>(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)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -382,9 +382,9 @@
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>46</LineNumber>
<LineNumber>39</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134219406</Address>
<Address>134224104</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
@ -393,16 +393,44 @@
<BreakIfRCount>1</BreakIfRCount>
<Filename>../Src/main.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\NUCLEO_F103RB\../Src/main.c\46</Expression>
<Expression>\\NUCLEO_F103RB\../Src/main.c\39</Expression>
</Bp>
<Bp>
<Number>1</Number>
<Type>0</Type>
<LineNumber>40</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>../Src/main.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
</Breakpoint>
<WatchWindow1>
<Ww>
<count>0</count>
<WinNumber>1</WinNumber>
<ItemText>adcRaw,0x0A</ItemText>
</Ww>
<Ww>
<count>1</count>
<WinNumber>1</WinNumber>
<ItemText>adcVolt</ItemText>
</Ww>
</WatchWindow1>
<Tracepoint>
<THDelay>0</THDelay>
</Tracepoint>
<DebugFlag>
<trace>0</trace>
<periodic>1</periodic>
<aLwin>1</aLwin>
<aLwin>0</aLwin>
<aCover>0</aCover>
<aSer1>0</aSer1>
<aSer2>0</aSer2>
@ -438,6 +466,13 @@
<pszMrulep></pszMrulep>
<pSingCmdsp></pSingCmdsp>
<pMultCmdsp></pMultCmdsp>
<LogicAnalyzers>
<Wi>
<IntNumber>0</IntNumber>
<FirstString>((PORTA &amp; 0x00000200) &gt;&gt; 9 &amp; 0x200) &gt;&gt; 9</FirstString>
<SecondString>00800000000000000000000000000000E0FFEF400100000000000000000000000000000028504F5254412026203078303030303032303029203E3E2039000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000F03F170000000000000000000000000000000000000000010008</SecondString>
</Wi>
</LogicAnalyzers>
<DebugDescription>
<Enable>1</Enable>
<EnableFlashSeq>0</EnableFlashSeq>
@ -618,6 +653,18 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>2</GroupNumber>
<FileNumber>14</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Services\Scheduler.c</PathWithFileName>
<FilenameWithoutPath>Scheduler.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>
@ -628,7 +675,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>14</FileNumber>
<FileNumber>15</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -640,9 +687,9 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>15</FileNumber>
<FileNumber>16</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\MyDrivers\ADC.c</PathWithFileName>
@ -652,7 +699,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>16</FileNumber>
<FileNumber>17</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -664,9 +711,9 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>17</FileNumber>
<FileNumber>18</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\MyDrivers\USART.c</PathWithFileName>
@ -684,7 +731,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>18</FileNumber>
<FileNumber>19</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -696,7 +743,7 @@
</File>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>19</FileNumber>
<FileNumber>20</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -708,7 +755,7 @@
</File>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>20</FileNumber>
<FileNumber>21</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -718,6 +765,42 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>22</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\LLDrivers\src\stm32f1xx_ll_gpio.c</PathWithFileName>
<FilenameWithoutPath>stm32f1xx_ll_gpio.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>23</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\LLDrivers\src\stm32f1xx_ll_usart.c</PathWithFileName>
<FilenameWithoutPath>stm32f1xx_ll_usart.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>24</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\LLDrivers\src\stm32f1xx_ll_adc.c</PathWithFileName>
<FilenameWithoutPath>stm32f1xx_ll_adc.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>
@ -728,7 +811,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>5</GroupNumber>
<FileNumber>21</FileNumber>
<FileNumber>25</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -748,7 +831,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>22</FileNumber>
<FileNumber>26</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -768,7 +851,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>7</GroupNumber>
<FileNumber>23</FileNumber>
<FileNumber>27</FileNumber>
<FileType>2</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>

View file

@ -185,7 +185,6 @@
<uocXRam>0</uocXRam>
<RvdsVP>0</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>0</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -352,7 +351,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<uClangAs>0</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -453,6 +452,11 @@
<FileType>1</FileType>
<FilePath>..\Services\Voltage.c</FilePath>
</File>
<File>
<FileName>Scheduler.c</FileName>
<FileType>1</FileType>
<FilePath>..\Services\Scheduler.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -498,6 +502,21 @@
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_tim.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_ll_gpio.c</FileName>
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_gpio.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_ll_usart.c</FileName>
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_usart.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_ll_adc.c</FileName>
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_adc.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -539,7 +558,7 @@
<TargetName>Simulateur</TargetName>
<ToolsetNumber>0x4</ToolsetNumber>
<ToolsetName>ARM-ADS</ToolsetName>
<pCCUsed>5060750::V5.06 update 6 (build 750)::.\ARMCC</pCCUsed>
<pCCUsed>5060750::V5.06 update 6 (build 750)::ARMCC</pCCUsed>
<uAC6>0</uAC6>
<TargetOption>
<TargetCommonOption>
@ -714,7 +733,6 @@
<uocXRam>0</uocXRam>
<RvdsVP>0</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>0</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -881,7 +899,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<ClangAsOpt>4</ClangAsOpt>
<uClangAs>0</uClangAs>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -982,6 +1000,11 @@
<FileType>1</FileType>
<FilePath>..\Services\Voltage.c</FilePath>
</File>
<File>
<FileName>Scheduler.c</FileName>
<FileType>1</FileType>
<FilePath>..\Services\Scheduler.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -1027,6 +1050,21 @@
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_tim.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_ll_gpio.c</FileName>
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_gpio.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_ll_usart.c</FileName>
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_usart.c</FilePath>
</File>
<File>
<FileName>stm32f1xx_ll_adc.c</FileName>
<FileType>1</FileType>
<FilePath>..\LLDrivers\src\stm32f1xx_ll_adc.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -1080,19 +1118,4 @@
<files/>
</RTE>
<LayerInfo>
<Layers>
<Layer>
<LayName>&lt;Project Info&gt;</LayName>
<LayDesc></LayDesc>
<LayUrl></LayUrl>
<LayKeys></LayKeys>
<LayCat></LayCat>
<LayLic></LayLic>
<LayTarg>0</LayTarg>
<LayPrjMark>1</LayPrjMark>
</Layer>
</Layers>
</LayerInfo>
</Project>

View file

@ -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));
}

View file

@ -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

View file

@ -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]);
}
}

View file

@ -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

View file

@ -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)
{
}