hal_adc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_adc.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE ADC FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_ADC_C_
  19. // Files includes
  20. #include "hal_adc.h"
  21. #include "hal_rcc.h"
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /// @addtogroup MM32_Hardware_Abstract_Layer
  24. /// @{
  25. ////////////////////////////////////////////////////////////////////////////////
  26. /// @addtogroup ADC_HAL
  27. /// @{
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// @addtogroup ADC_Exported_Functions
  30. /// @{
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// @brief Deinitializes the adc peripheral registers to their default
  33. /// reset values.
  34. /// @param adc: select the ADC peripheral.
  35. /// @retval None.
  36. ////////////////////////////////////////////////////////////////////////////////
  37. void ADC_DeInit(ADC_TypeDef* adc)
  38. {
  39. switch (*(vu32*)&adc) {
  40. case ADC1_BASE:
  41. exRCC_APB2PeriphReset(RCC_APB2ENR_ADC1);
  42. break;
  43. case ADC2_BASE:
  44. exRCC_APB2PeriphReset(RCC_APB2ENR_ADC2);
  45. break;
  46. case ADC3_BASE:
  47. exRCC_APB2PeriphReset(RCC_APB2ENR_ADC3);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////
  54. /// @brief Initializes the adc peripheral according to the specified parameters
  55. /// in the init_struct, Please use this function if you want to be
  56. /// compatible with older versions of the library.
  57. /// @param adc: select the ADC peripheral.
  58. /// @param init_struct: pointer to an ADC_InitTypeDef structure that contains
  59. /// the configuration information for the specified ADC peripheral.
  60. /// @retval None.
  61. ////////////////////////////////////////////////////////////////////////////////
  62. void ADC_Init(ADC_TypeDef* adc, ADC_InitTypeDef* init_struct)
  63. {
  64. adc->ADCFG &= ~(ADC_CFGR_PRE | ADC_CFGR_RSLTCTL);
  65. adc->ADCFG |= (u32)(init_struct->ADC_PRESCARE) | init_struct->ADC_Resolution;
  66. adc->ADCR &= ~(ADC_CR_ALIGN | ADC_CR_MODE | ADC_CR_TRGSEL);
  67. adc->ADCR |= ((u32)init_struct->ADC_DataAlign) | init_struct->ADC_ExternalTrigConv | ((u32)init_struct->ADC_Mode);
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////
  70. /// @brief Fills each init_struct member with its default value.
  71. /// @param init_struct : pointer to an ADC_InitTypeDef structure which will be
  72. /// initialized.
  73. /// @retval None.
  74. ////////////////////////////////////////////////////////////////////////////////
  75. void ADC_StructInit(ADC_InitTypeDef* init_struct)
  76. {
  77. init_struct->ADC_Resolution = ADC_Resolution_12b;
  78. init_struct->ADC_PRESCARE = ADC_PCLK2_PRESCARE_2;
  79. init_struct->ADC_Mode = ADC_CR_IMM; //ADC_Mode_Single;
  80. init_struct->ADC_ContinuousConvMode = DISABLE; // useless
  81. init_struct->ADC_ExternalTrigConv = ADC1_ExternalTrigConv_T1_CC1;
  82. init_struct->ADC_DataAlign = ADC_DataAlign_Right;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. /// @brief Enables or disables the specified ADC peripheral.
  86. /// @param adc:select the ADC peripheral.
  87. /// @param state: new state of the adc peripheral.
  88. /// @retval None.
  89. ////////////////////////////////////////////////////////////////////////////////
  90. void ADC_Cmd(ADC_TypeDef* adc, FunctionalState state)
  91. {
  92. (state) ? (adc->ADCFG |= ADC_CFGR_ADEN) : (adc->ADCFG &= ~ADC_CFGR_ADEN);
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////
  95. /// @brief Enables or disables the specified ADC DMA request.
  96. /// @param adc: select the ADC peripheral.
  97. /// @param state: New state of the selected ADC DMA transfer.
  98. /// @retval None.
  99. ////////////////////////////////////////////////////////////////////////////////
  100. void ADC_DMACmd(ADC_TypeDef* adc, FunctionalState state)
  101. {
  102. (state) ? (adc->ADCR |= ADC_CR_DMAEN) : (adc->ADCR &= ~ADC_CR_DMAEN);
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. /// @brief Enables or disables the specified ADC interrupts.
  106. /// @param adc: select the ADC peripheral.
  107. /// @param adc_interrupt: specifies the ADC interrupt sources to be enabled or disabled.
  108. /// @param state: New state of the specified ADC interrupts.
  109. /// @retval None.
  110. ////////////////////////////////////////////////////////////////////////////////
  111. void ADC_ITConfig(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_interrupt, FunctionalState state)
  112. {
  113. if (adc_interrupt == ADC_IT_EOC)
  114. (state) ? (adc->ADCR |= ADC_CR_ADIE) : (adc->ADCR &= ~ADC_CR_ADIE);
  115. else
  116. (state) ? (adc->ADCR |= ADC_CR_ADWIE) : (adc->ADCR &= ~ADC_CR_ADWIE);
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////
  119. /// @brief Enables or disables the selected ADC software start conversion .
  120. /// @param adc: select the ADC peripheral.
  121. /// @param state: New state of the selected ADC software start conversion.
  122. /// @retval None.
  123. ////////////////////////////////////////////////////////////////////////////////
  124. void ADC_SoftwareStartConvCmd(ADC_TypeDef* adc, FunctionalState state)
  125. {
  126. (state) ? (adc->ADCR |= ADC_CR_ADST) : (adc->ADCR &= ~ADC_CR_ADST);
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. /// @brief Gets the selected ADC Software start conversion Status.
  130. /// @param adc: select the ADC peripheral.
  131. /// @retval The new state of ADC software start conversion (SET or RESET).
  132. ////////////////////////////////////////////////////////////////////////////////
  133. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* adc)
  134. {
  135. return (((adc->ADCR & ADC_CR_ADST) != (u32)RESET) ? SET : RESET);
  136. }
  137. ////////////////////////////////////////////////////////////////////////////////
  138. /// @brief Enable the selected ADC channel and configure its sample time. Please
  139. /// use this function if you want to be compatible with older versions
  140. /// of the library.
  141. /// @param adc: select the ADC peripheral.
  142. /// @param channel: the ADC channel to configure.
  143. /// @param sample_time: the ADC Channel n Sample time to configure.
  144. /// @retval None.
  145. ////////////////////////////////////////////////////////////////////////////////
  146. void ADC_RegularChannelConfig(ADC_TypeDef* adc, u32 channel, u8 rank, u32 sample_time) //ADCSAM_TypeDef
  147. {
  148. u32 tempchan;
  149. sample_time = sample_time & 0xF;
  150. tempchan = channel;
  151. if(tempchan > 8) {
  152. tempchan = tempchan & 0xF;
  153. tempchan = tempchan - 8;
  154. adc->SMPR2 &= ~(0xF << tempchan);
  155. adc->SMPR2 |= (sample_time << tempchan);
  156. }
  157. else {
  158. adc->SMPR1 &= ~(0xF << tempchan);
  159. adc->SMPR1 |= (sample_time << tempchan);
  160. }
  161. adc->ADCHS &= ~(1 << channel);
  162. adc->ADCHS |= (1 << channel);
  163. if (channel & ADC_CHSR_CHT)
  164. ADC_TempSensorVrefintCmd(ENABLE);
  165. else if (channel & ADC_CHSR_CHV)
  166. ADC_TempSensorVrefintCmd(ENABLE);
  167. }
  168. ////////////////////////////////////////////////////////////////////////////////
  169. /// @brief Enables or disables the adc conversion through external trigger.
  170. /// @param adc: select the ADC peripheral.
  171. /// @param state: New state of the selected ADC external trigger.
  172. /// @retval None.
  173. ////////////////////////////////////////////////////////////////////////////////
  174. void ADC_ExternalTrigConvCmd(ADC_TypeDef* adc, FunctionalState state)
  175. {
  176. (state) ? (adc->ADCR |= ADC_CR_TRGEN) : (adc->ADCR &= ~ADC_CR_TRGEN);
  177. }
  178. ////////////////////////////////////////////////////////////////////////////////
  179. /// @brief Configures the adc external trigger for injected channels conversion.
  180. /// @param adc: select the ADC peripheral.
  181. /// @param adc_external_trig_source: Configuring the external trigger source
  182. /// for the ADC.
  183. /// @retval None.
  184. ////////////////////////////////////////////////////////////////////////////////
  185. void ADC_ExternalTrigConvConfig(ADC_TypeDef* adc, EXTERTRIG_TypeDef adc_external_trig_source)
  186. {
  187. adc->ADCR &= ~ADC_CR_TRGSEL;
  188. adc->ADCR |= adc_external_trig_source;
  189. }
  190. ////////////////////////////////////////////////////////////////////////////////
  191. /// @brief Returns the last adc conversion result data for regular channel.
  192. /// @param adc: select the ADC peripheral.
  193. /// @retval The data conversion value.
  194. ////////////////////////////////////////////////////////////////////////////////
  195. u16 ADC_GetConversionValue(ADC_TypeDef* adc)
  196. {
  197. return (u16)adc->ADDATA;
  198. }
  199. ////////////////////////////////////////////////////////////////////////////////
  200. /// @brief Returns the last ADC conversion result data in dual mode.
  201. /// @param None
  202. /// @retval The Data conversion value.
  203. ////////////////////////////////////////////////////////////////////////////////
  204. u32 ADC_GetDualModeConversionValue()
  205. {
  206. return (*(vu32*)ADC1_BASE);
  207. }
  208. ////////////////////////////////////////////////////////////////////////////////
  209. /// @brief Enables or disables the analog watchdog.
  210. /// @param adc: to select the ADC peripheral.
  211. /// @param state: New state of the selected ADC analog watchdog.
  212. /// @retval None.
  213. ////////////////////////////////////////////////////////////////////////////////
  214. void ADC_AnalogWatchdogCmd(ADC_TypeDef* adc, FunctionalState state)
  215. {
  216. (state) ? (adc->ADCFG |= ADC_CFGR_ADWEN) : (adc->ADCFG &= ~ADC_CFGR_ADWEN);
  217. }
  218. ////////////////////////////////////////////////////////////////////////////////
  219. /// @brief Configures the high and low thresholds of the analog watchdog.
  220. /// @param adc: select the ADC peripheral.
  221. /// @param high_threshold: the ADC analog watchdog High threshold value.
  222. /// This parameter must be a 12bit value.
  223. /// @param low_threshold: the ADC analog watchdog Low threshold value.
  224. /// This parameter must be a 12bit value.
  225. /// @retval None.
  226. ////////////////////////////////////////////////////////////////////////////////
  227. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* adc, u16 high_threshold, u16 low_threshold)
  228. {
  229. u32 tempThreshold;
  230. tempThreshold = high_threshold;
  231. adc->ADCMPR = (tempThreshold << 16) | low_threshold;
  232. }
  233. ////////////////////////////////////////////////////////////////////////////////
  234. /// @brief Configures the analog watchdog guarded single channel
  235. /// @param adc: select the ADC peripheral.
  236. /// @param channel: the ADC channel to configure for the analog watchdog.
  237. /// @retval None.
  238. ////////////////////////////////////////////////////////////////////////////////
  239. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* adc, ADCCHANNEL_TypeDef channel)
  240. {
  241. adc->ADCR &= ~ADC_CR_CMPCH;
  242. adc->ADCR |= (channel << ADC_CR_CMPCH_Pos);
  243. }
  244. ////////////////////////////////////////////////////////////////////////////////
  245. /// @brief Enables or disables the temperature sensor and Vrefint channel.
  246. /// @param state: New state of the temperature sensor.
  247. /// @retval None.
  248. ////////////////////////////////////////////////////////////////////////////////
  249. void ADC_TempSensorVrefintCmd(FunctionalState state)
  250. {
  251. (state) ? (ADC1->ADCFG |= (ADC_CFGR_TEN | ADC_CFGR_VEN))
  252. : (ADC1->ADCFG &= ~(ADC_CFGR_TEN | ADC_CFGR_VEN));
  253. }
  254. ////////////////////////////////////////////////////////////////////////////////
  255. /// @brief Enables or disables the temperature sensor .
  256. /// @param state: New state of the temperature sensor.
  257. /// @retval None.
  258. ////////////////////////////////////////////////////////////////////////////////
  259. void ADC_TempSensorCmd(FunctionalState state)
  260. {
  261. ADC_TempSensorVrefintCmd(state);
  262. }
  263. ////////////////////////////////////////////////////////////////////////////////
  264. /// @brief Enables or disables the Vrefint channel.
  265. /// @param state: New state of the Vrefint channel.
  266. /// @retval None.
  267. ////////////////////////////////////////////////////////////////////////////////
  268. void ADC_VrefintCmd(FunctionalState state)
  269. {
  270. ADC_TempSensorVrefintCmd(state);
  271. }
  272. ////////////////////////////////////////////////////////////////////////////////
  273. /// @brief Enables or disables the temperature sensor and Vrefint channel.
  274. /// @param chs: temperature sensor bit & Vrefint bit.
  275. /// @param state: New state of the temperature sensor.
  276. /// @retval None.
  277. ////////////////////////////////////////////////////////////////////////////////
  278. void exADC_TempSensorVrefintCmd(u32 chs, FunctionalState state)
  279. {
  280. if (chs & ADC_CHSR_CHT) {
  281. (state) ? (ADC1->ADCFG |= ADC_CFGR_TEN)
  282. : (ADC1->ADCFG &= ~ADC_CFGR_TEN);
  283. }
  284. else if (chs & ADC_CHSR_CHV) {
  285. (state) ? (ADC1->ADCFG |= ADC_CFGR_VEN)
  286. : (ADC1->ADCFG &= ~ADC_CFGR_VEN);
  287. }
  288. }
  289. ////////////////////////////////////////////////////////////////////////////////
  290. /// @brief Checks whether the specified ADC flag is set or not.
  291. /// @param adc: select the ADC peripheral.
  292. /// @param adc_flag: specifies the flag to check.
  293. /// @retval The New state of adc_flag (SET or RESET).
  294. ////////////////////////////////////////////////////////////////////////////////
  295. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_flag)
  296. {
  297. return (adc_flag == ADC_IT_EOC) ? ((adc->ADSTA & ADC_SR_ADIF) ? SET : RESET) : ((adc->ADSTA & ADC_SR_ADWIF) ? SET : RESET);
  298. }
  299. ////////////////////////////////////////////////////////////////////////////////
  300. /// @brief Clears the adc's pending flags.
  301. /// @param adc: select the ADC peripheral.
  302. /// @param adc_flag: specifies the flag to clear.
  303. /// @retval None.
  304. ////////////////////////////////////////////////////////////////////////////////
  305. void ADC_ClearFlag(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_flag)
  306. {
  307. (adc_flag == ADC_IT_EOC) ? (adc->ADSTA |= ADC_SR_ADIF) : (adc->ADSTA |= ADC_SR_ADWIF);
  308. }
  309. ////////////////////////////////////////////////////////////////////////////////
  310. /// @brief Checks whether the specified adc's interrupt has occurred or not.
  311. /// @param adc: select the ADC peripheral.
  312. /// @param adc_interrupt: specifies the ADC interrupt source to check.
  313. /// @retval The new state of adc_interrupt (SET or RESET).
  314. ////////////////////////////////////////////////////////////////////////////////
  315. ITStatus ADC_GetITStatus(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_interrupt)
  316. {
  317. return (adc_interrupt == ADC_IT_EOC) ? ((adc->ADSTA & ADC_SR_ADIF) ? SET : RESET) : ((adc->ADSTA & ADC_SR_ADWIF) ? SET : RESET);
  318. }
  319. ////////////////////////////////////////////////////////////////////////////////
  320. /// @brief Clears the adc's interrupt pending bits.
  321. /// @param adc: select the ADC peripheral.
  322. /// @param adc_interrupt: specifies the ADC interrupt pending bit to clear.
  323. /// @retval None.
  324. ////////////////////////////////////////////////////////////////////////////////
  325. void ADC_ClearITPendingBit(ADC_TypeDef* adc, ADCFLAG_TypeDef adc_interrupt)
  326. {
  327. (adc_interrupt == ADC_IT_EOC) ? (adc->ADSTA |= ADC_SR_ADIF) : (adc->ADSTA |= ADC_SR_ADWIF);
  328. }
  329. ////////////////////////////////////////////////////////////////////////////////
  330. /// @brief Configures the adc any channels conversion rank and channel.
  331. /// @param adc: select the ADC peripheral.
  332. /// @param rank: rank can be 0x0~0xf for the convert sequence.
  333. /// @param adc_channel: Configuring the target channel to be converted.
  334. /// @retval None.
  335. ////////////////////////////////////////////////////////////////////////////////
  336. void ADC_ANY_CH_Config(ADC_TypeDef* adc, u8 rank, ADCCHANNEL_TypeDef adc_channel)
  337. {
  338. rank = rank & 0xF;
  339. if(rank < 8) {
  340. adc->CHANY0 &= ~(0x0F << (4 * rank));
  341. adc->CHANY0 |= (adc_channel << (4 * rank));
  342. }
  343. else {
  344. adc->CHANY1 &= ~(0x0F << (4 * (rank - 8)));
  345. adc->CHANY1 |= (adc_channel << (4 * (rank - 8)));
  346. }
  347. }
  348. ////////////////////////////////////////////////////////////////////////////////
  349. /// @brief Configures the adc any channels conversion Max rank number
  350. /// @param adc: select the ADC peripheral.
  351. /// @param num: Configuring the max rank number for the ADC.
  352. /// @retval None.
  353. ////////////////////////////////////////////////////////////////////////////////
  354. void ADC_ANY_NUM_Config(ADC_TypeDef* adc, u8 num)
  355. {
  356. if(num > 15) num = 15; //15 ? 16 need to be confirmed
  357. adc->ANYCFG = num;
  358. }
  359. ////////////////////////////////////////////////////////////////////////////////
  360. /// @brief Enables or disables the ANY channel converter.
  361. /// @param state: enable or disable the ANY channel converter mode.
  362. /// @retval None.
  363. ////////////////////////////////////////////////////////////////////////////////
  364. void ADC_ANY_Cmd(ADC_TypeDef* adc, FunctionalState state)
  365. {
  366. (state) ? (adc->ANYCR |= ADC1_CHANY_CR_MDEN) : (adc->ANYCR &= ~ADC1_CHANY_CR_MDEN);
  367. }
  368. ////////////////////////////////////////////////////////////////////////////////
  369. /// @brief Enables or disables the selected ADC automatic injected group
  370. /// conversion after regular one.
  371. /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
  372. /// @param state: new state of the selected ADC auto injected conversion
  373. /// This parameter can be: ENABLE or DISABLE.
  374. /// @retval None
  375. ////////////////////////////////////////////////////////////////////////////////
  376. void ADC_AutoInjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
  377. {
  378. (state) ? (adc->ANYCR |= ADC_ANY_CR_JAUTO) : (adc->ANYCR &= ~ADC_ANY_CR_JAUTO);
  379. }
  380. ////////////////////////////////////////////////////////////////////////////////
  381. /// @brief Configures the adc external trigger for injected channels conversion.
  382. /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
  383. /// @param ADC_ExtInjTrigSource: specifies the ADC trigger to start injected conversion.
  384. /// @retval None
  385. ////////////////////////////////////////////////////////////////////////////////
  386. void ADC_ExternalTrigInjectedConvertConfig(ADC_TypeDef* adc, EXTER_INJ_TRIG_TypeDef ADC_ExtInjTrigSource)
  387. {
  388. u32 tmpreg = 0;
  389. // Get the old register value
  390. tmpreg = adc->ANYCR;
  391. // Clear the old external event selection for injected group
  392. tmpreg &= ADC_ANY_CR_JTRGSEL;
  393. // Set the external event selection for injected group
  394. tmpreg |= ADC_ExtInjTrigSource;
  395. // Store the new register value
  396. adc->ANYCR = tmpreg;
  397. }
  398. ////////////////////////////////////////////////////////////////////////////////
  399. /// @brief Enables or disables the adc injected channels conversion through
  400. /// external trigger
  401. /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
  402. /// @param state: new state of the selected ADC external trigger start of
  403. /// injected conversion.
  404. /// This parameter can be: ENABLE or DISABLE.
  405. /// @retval None
  406. ////////////////////////////////////////////////////////////////////////////////
  407. void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
  408. {
  409. (state) ? (adc->ANYCR |= ADC_ANY_CR_JTRGEN) : (adc->ANYCR &= ~ADC_ANY_CR_JTRGEN);
  410. }
  411. ////////////////////////////////////////////////////////////////////////////////
  412. /// @brief Enables or disables the selected ADC start of the injected
  413. /// channels conversion.
  414. /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
  415. /// @param state: new state of the selected ADC software start injected conversion.
  416. /// This parameter can be: ENABLE or DISABLE.
  417. /// @retval None
  418. ////////////////////////////////////////////////////////////////////////////////
  419. void ADC_InjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
  420. {
  421. (state) ? (adc->ANYCR |= ADC_ANY_CR_JCEN) : (adc->ANYCR &= ~ADC_ANY_CR_JCEN);
  422. }
  423. ////////////////////////////////////////////////////////////////////////////////
  424. /// @brief Enables or disables the selected ADC start of the injected
  425. /// channels conversion.
  426. /// @param adc: where x can be 1, 2 or 3 to select the ADC peripheral.
  427. /// @param NewState: new state of the selected ADC software start injected conversion.
  428. /// This parameter can be: ENABLE or DISABLE.
  429. /// @retval None
  430. ////////////////////////////////////////////////////////////////////////////////
  431. void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* adc, FunctionalState state)
  432. {
  433. (state) ? (adc->ANYCR |= ADC_ANY_CR_JADST) : (adc->ANYCR &= ~ADC_ANY_CR_JADST);
  434. }
  435. ////////////////////////////////////////////////////////////////////////////////
  436. /// @brief Enable the selected ADC channel and configure its sample time. Please
  437. /// use this function if you want to be compatible with older versions
  438. /// of the library.
  439. /// @param adc: select the ADC peripheral.
  440. /// @param event: the ADC external event to configure.
  441. /// @param sample_time: the ADC Channel n Sample time to configure.
  442. /// @retval None.
  443. ////////////////////////////////////////////////////////////////////////////////
  444. void ADC_InjectedSequencerConfig(ADC_TypeDef* adc, u32 event, u32 sample_time)
  445. {
  446. adc->ANYCR &= ~(ADC_ANY_CR_JCEN | ADC_ANY_CR_CHANY_MDEN | ADC_ANY_CR_JTRGSEL_EXTI12 \
  447. | ADC_ANY_CR_JTRGSHIFT_512 | ADC_ANY_CR_JTRGEN);
  448. adc->ANYCR |= (ADC_ANY_CR_JCEN | ADC_ANY_CR_CHANY_MDEN | sample_time | event | ADC_ANY_CR_JTRGEN);
  449. }
  450. ////////////////////////////////////////////////////////////////////////////////
  451. /// @brief Injection channel length configuration.
  452. /// @param adc: select the ADC peripheral.
  453. /// @param Length: Injection channel length.
  454. /// @retval None.
  455. ////////////////////////////////////////////////////////////////////////////////
  456. void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* adc, ADC_INJ_SEQ_LEN_TypeDef Length)
  457. {
  458. adc->JSQR &= ~(0x03 << ADC_JSQR_JL_Pos);
  459. adc->JSQR |= Length << ADC_JSQR_JL_Pos;
  460. }
  461. ////////////////////////////////////////////////////////////////////////////////
  462. /// @brief Injection channel configuration.
  463. /// @param adc : select the ADC peripheral.
  464. /// @param off_addr : Injection channel offset address.
  465. /// @param channel: The sampling channel.
  466. /// @retval None.
  467. ////////////////////////////////////////////////////////////////////////////////
  468. void ADC_InjectedSequencerChannelConfig(ADC_TypeDef* adc, ADC_INJ_SEQ_Channel_TypeDef off_addr, ADCCHANNEL_TypeDef channel)
  469. {
  470. adc->JSQR &= ~(0x1F << (off_addr >> 2) * 5);
  471. adc->JSQR |= (channel << (off_addr >> 2) * 5);
  472. }
  473. ////////////////////////////////////////////////////////////////////////////////
  474. /// @brief Injection channel converted value.
  475. /// @param adc : select the ADC peripheral.
  476. /// @param off_addr : Injection channel offset address.
  477. /// @retval value.
  478. ////////////////////////////////////////////////////////////////////////////////
  479. u16 ADC_GetInjectedConversionValue(ADC_TypeDef* adc, ADC_INJ_SEQ_Channel_TypeDef off_addr)
  480. {
  481. u32 value;
  482. value = (*(vu32*)(*(vu32*)&adc + 0xB0 + off_addr)) - (*(vu32*)(*(vu32*)&adc + 0x7C + off_addr));
  483. return (u16)value;
  484. }
  485. ////////////////////////////////////////////////////////////////////////////////
  486. /// @brief Injection current converted value.
  487. /// @param adc : select the ADC peripheral.
  488. /// @retval value. Returns the last adc conversion result data for injected channel.
  489. ////////////////////////////////////////////////////////////////////////////////
  490. u16 ADC_GetInjectedCurrentConvertedValue(ADC_TypeDef* adc)
  491. {
  492. return (u16)adc->JDATA;
  493. }
  494. ////////////////////////////////////////////////////////////////////////////////
  495. /// @brief Injection channel compensation configuration.
  496. /// @param adc : select the ADC peripheral.
  497. /// @param off_addr : Injection channel.
  498. /// @param value : compensation value.
  499. /// @retval None.
  500. ////////////////////////////////////////////////////////////////////////////////
  501. void ADC_SetInjectedOffset(ADC_TypeDef* adc, ADC_INJ_SEQ_Channel_TypeDef off_addr, u16 value)
  502. {
  503. *(vu32*)(*(vu32*)&adc + 0x7C + off_addr) = value;
  504. }
  505. ////////////////////////////////////////////////////////////////////////////////
  506. /// @brief Get channel convertion result.
  507. /// @param adc : select the ADC peripheral.
  508. /// @param channel : Converted channel.
  509. /// @retval The Data conversion value.
  510. ////////////////////////////////////////////////////////////////////////////////
  511. u16 ADC_GetChannelConvertedValue(ADC_TypeDef* adc, ADCCHANNEL_TypeDef channel)
  512. {
  513. return (u16)(*(vu32*) ((u32)adc + 0x18 + ((u32)channel << 2)));
  514. }
  515. /// @}
  516. /// @}
  517. /// @}