regular_conversion_manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. ******************************************************************************
  3. * @file regular_conversion_manager.c
  4. * @author Motor Control SDK Team, ST Microelectronics
  5. * @brief This file provides firmware functions that implement the following features
  6. * of the regular_conversion_manager component of the Motor Control SDK:
  7. * Register conversion with or without callback
  8. * Execute regular conv directly from Temperature and VBus sensors
  9. * Execute user regular conversion scheduled by medium frequency task
  10. * Manage user conversion state machine
  11. * +
  12. * +
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  18. * All rights reserved.</center></h2>
  19. *
  20. * This software component is licensed by ST under Ultimate Liberty license
  21. * SLA0044, the "License"; You may not use this file except in compliance with
  22. * the License. You may obtain a copy of the License at:
  23. * www.st.com/SLA0044
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "regular_conversion_manager.h"
  29. #include "mc_config.h"
  30. /** @addtogroup MCSDK
  31. * @{
  32. */
  33. /** @defgroup RCM Regular Conversion Manager
  34. * @brief Regular Conversion Manager component of the Motor Control SDK
  35. *
  36. * MotorControl SDK makes an extensive usage of ADCs. Some conversions are timing critical
  37. * like current reading, and some have less constraints. If an ADC offers both Injected and Regular,
  38. * channels, critical conversions will be systematically done on Injected channels, because they
  39. * interrupt any ongoing regular conversion so as to be executed without delay.
  40. * Others conversions, mainly Bus voltage, and Temperature sensing are performed with regular channels.
  41. * If users wants to perform ADC conversions with an ADC already used by MC SDK, they must use regular
  42. * conversions. It is forbidden to use Injected channel on an ADC that is already in use for current reading.
  43. * As usera and MC-SDK may share ADC regular scheduler, this component intents to manage all the
  44. * regular conversions.
  45. *
  46. * If users wants to execute their own conversion, they first have to register it through the
  47. * RCM_RegisterRegConv_WithCB() or RCM_RegisterRegConv() APIs. Multiple conversions can be registered,
  48. * but only one can be scheduled at a time .
  49. *
  50. * A requested user regular conversion will be executed by the medium frequency task after the
  51. * MC-SDK regular safety conversions: Bus voltage and Temperature.
  52. *
  53. * If a callback is registered, particular care must be taken with the code executed inside the CB.
  54. * The callback code is executed under Medium frequency task IRQ context (Systick).
  55. *
  56. * If the Users do not register a callback, they must poll the RCM state machine to know if
  57. * a conversion is ready to be read, scheduled, or free to be scheduled. This is performed through
  58. * the RCM_GetUserConvState() API.
  59. *
  60. * If the state is RCM_USERCONV_IDLE, a conversion is ready to be scheduled.
  61. * if a conversion is already scheduled, the returned value is RCM_USERCONV_REQUESTED.
  62. * if a conversion is ready to be read, the returned value is RCM_USERCONV_EOC.
  63. * In RCM_USERCONV_EOC state, a call to RCM_GetUserConv will consume the value, and set the state machine back
  64. * to RCM_USERCONV_IDLE state. It implies that a second call without new conversion performed,
  65. * will send back 0xffff which is an error value meaning that the data is not available.
  66. * If a conversion request is executed, but the previous conversion has not been completed, nor consumed,
  67. * the request is discarded and the RCM_RequestUserConv return false.
  68. *
  69. * If a callback is registered, the data read is sent back to the callback parameters, and therefor consumed.
  70. * @{
  71. */
  72. /* Private typedef -----------------------------------------------------------*/
  73. typedef struct
  74. {
  75. RCM_exec_cb_t cb;
  76. void * data;
  77. } RCM_callback_t;
  78. /* Private defines -----------------------------------------------------------*/
  79. /**
  80. * @brief Number of regular conversion allowed By default.
  81. *
  82. * In single drive configuration, it is defined to 4. 2 of them are consumed by
  83. * Bus voltage and temperature reading. This leaves 2 handles available for
  84. * user conversions
  85. *
  86. * In dual drives configuration, it is defined to 6. 2 of them are consumed by
  87. * Bus voltage and temperature reading for each motor. This leaves 2 handles
  88. * available for user conversion.
  89. *
  90. * Defined to 4 here.
  91. */
  92. #define RCM_MAX_CONV 4
  93. /* Global variables ----------------------------------------------------------*/
  94. RegConv_t * RCM_handle_array [RCM_MAX_CONV];
  95. RCM_callback_t RCM_CB_array [RCM_MAX_CONV];
  96. uint8_t RCM_UserConvHandle;
  97. uint16_t RCM_UserConvValue;
  98. RCM_UserConvState_t RCM_UserConvState;
  99. /* Private function prototypes -----------------------------------------------*/
  100. /* Private functions ---------------------------------------------------------*/
  101. /**
  102. * @brief Registers a regular conversion, and attaches a callback.
  103. *
  104. * This function registers a regular ADC conversion that can be later scheduled for execution. It
  105. * returns a handle that uniquely identifies the conversion. This handle is used in the other API
  106. * of the Regular Converion Manager to reference the registered conversion.
  107. *
  108. * A regular conversion is defined by an ADC + ADC channel pair. If a registration already exists
  109. * for the requested ADC + ADC channel pair, the same handle will be reused.
  110. *
  111. * The regular conversion is registered along with a callback that is executed each time the
  112. * conversion has completed. The callback is invoked with two parameters:
  113. *
  114. * - the handle of the regular conversion
  115. * - a data pointer, supplied by uthe users at registration time.
  116. *
  117. * The registration may fail if there is no space left for additional conversions. The
  118. * maximum number of regular conversion that can be registered is defined by #RCM_MAX_CONV.
  119. *
  120. * @note Users who do not want a callback to be executed at the end of the conversion,
  121. * should use RCM_RegisterRegConv() instead.
  122. *
  123. * @param regConv Pointer to the regular conversion parameters.
  124. * Contains ADC, Channel and sampling time to be used.
  125. *
  126. * @param fctCB Function called once the regular conversion is executed.
  127. *
  128. * @param data Used to save a user context. this parameter will be send back by
  129. * the fctCB function. @b Note: This parameter can be NULL if not used.
  130. *
  131. * @retval the handle of the registered conversion or 255 if the registration failed
  132. */
  133. uint8_t RCM_RegisterRegConv_WithCB (RegConv_t * regConv, RCM_exec_cb_t fctCB, void *data)
  134. {
  135. uint8_t handle;
  136. handle = RCM_RegisterRegConv(regConv);
  137. if (handle < RCM_MAX_CONV)
  138. {
  139. RCM_CB_array [handle].cb = fctCB;
  140. RCM_CB_array [handle].data = data;
  141. }
  142. return handle;
  143. }
  144. /**
  145. * @brief Registers a regular conversion.
  146. *
  147. * This function registers a regular ADC conversion that can be later scheduled for execution. It
  148. * returns a handle that uniquely identifies the conversion. This handle is used in the other API
  149. * of the Regular Converion Manager to reference the registered conversion.
  150. *
  151. * A regular conversion is defined by an ADC + ADC channel pair. If a registration already exists
  152. * for the requested ADC + ADC channel pair, the same handle will be reused.
  153. *
  154. * The registration may fail if there is no space left for additional conversions. The
  155. * maximum number of regular conversion that can be registered is defined by #RCM_MAX_CONV.
  156. *
  157. * @note Users who do not want a callback to be executed at the end of the conversion,
  158. * should use RCM_RegisterRegConv() instead.
  159. *
  160. * @param regConv Pointer to the regular conversion parameters.
  161. * Contains ADC, Channel and sampling time to be used.
  162. *
  163. * @retval the handle of the registered conversion or 255 if the registration failed
  164. */
  165. uint8_t RCM_RegisterRegConv(RegConv_t * regConv)
  166. {
  167. uint8_t handle=255;
  168. uint8_t i=0;
  169. /* Parse the array to be sure that same
  170. * conversion does not already exist*/
  171. while (i < RCM_MAX_CONV)
  172. {
  173. if ( RCM_handle_array [i] == 0 && handle > RCM_MAX_CONV)
  174. {
  175. handle = i; /* First location available, but still looping to check that this config does not already exist*/
  176. }
  177. /* Ticket 64042 : If RCM_handle_array [i] is null access to data member will cause Memory Fault. */
  178. if ( RCM_handle_array [i] != 0 )
  179. {
  180. if ((RCM_handle_array [i]->channel == regConv->channel) &&
  181. (RCM_handle_array [i]->regADC == regConv->regADC))
  182. {
  183. handle =i; /* Reuse the same handle */
  184. i = RCM_MAX_CONV; /* we can skip the rest of the loop*/
  185. }
  186. }
  187. i++;
  188. }
  189. if (handle < RCM_MAX_CONV )
  190. {
  191. RCM_handle_array [handle] = regConv;
  192. RCM_CB_array [handle].cb = NULL; /* if a previous callback was attached, it is cleared*/
  193. if (LL_ADC_IsEnabled(regConv->regADC) == 0 )
  194. {
  195. LL_ADC_DisableIT_EOS(regConv->regADC);
  196. LL_ADC_ClearFlag_EOS(regConv->regADC);
  197. LL_ADC_DisableIT_JEOS(regConv->regADC);
  198. LL_ADC_ClearFlag_JEOS(regConv->regADC);
  199. /* Before starting a calibration, the ADC must have been in power-on state (ADON bit = 1) for
  200. * at least two ADC clock cycles. */
  201. LL_ADC_Enable( regConv->regADC );
  202. LL_ADC_StartCalibration( regConv->regADC);
  203. while ( LL_ADC_IsCalibrationOnGoing( regConv->regADC ) )
  204. { }
  205. }
  206. else
  207. {
  208. }
  209. /* reset regular conversion sequencer length set by cubeMX */
  210. LL_ADC_REG_SetSequencerLength( regConv->regADC, LL_ADC_REG_SEQ_SCAN_DISABLE );
  211. /* configure the sampling time (should already be configured by for non user conversions)*/
  212. LL_ADC_SetChannelSamplingTime ( regConv->regADC, __LL_ADC_DECIMAL_NB_TO_CHANNEL(regConv->channel) ,regConv->samplingTime);
  213. }
  214. else
  215. {
  216. /* Nothing to do handle is already set to error value : 255 */
  217. }
  218. return handle;
  219. }
  220. /*
  221. * This function is used to read the result of a regular conversion.
  222. * This function polls on the ADC end of conversion.
  223. * As ADC have injected channels for currents sensing,
  224. * There is no issue to execute regular conversion asynchronously.
  225. *
  226. * NOTE: This function is not part of the public API and users should not call it.
  227. */
  228. uint16_t RCM_ExecRegularConv (uint8_t handle)
  229. {
  230. uint16_t retVal;
  231. LL_ADC_REG_SetSequencerRanks( RCM_handle_array[handle]->regADC,
  232. LL_ADC_REG_RANK_1,
  233. __LL_ADC_DECIMAL_NB_TO_CHANNEL( RCM_handle_array[handle]->channel ) );
  234. LL_ADC_REG_ReadConversionData12( RCM_handle_array[handle]->regADC );
  235. LL_ADC_REG_StartConversionSWStart(RCM_handle_array[handle]->regADC);
  236. while ( LL_ADC_IsActiveFlag_EOS (RCM_handle_array[handle]->regADC ) == 0u) {}
  237. retVal = LL_ADC_REG_ReadConversionData12( RCM_handle_array[handle]->regADC );
  238. return retVal;
  239. }
  240. /**
  241. * @brief Schedules a regular conversion for execution.
  242. *
  243. * This function requests the execution of the user-defined regular conversion identified
  244. * by @p handle. All user defined conversion requests must be performed inside routines with the
  245. * same priority level. If a previous regular conversion request is pending this function has no
  246. * effect, for this reason is better to call RCM_GetUserConvState() and check if the state is
  247. * #RCM_USERCONV_IDLE before calling RCM_RequestUserConv().
  248. *
  249. * @param handle used for the user conversion.
  250. *
  251. * @return true if the regular conversion could be scheduled and false otherwise.
  252. */
  253. bool RCM_RequestUserConv(uint8_t handle)
  254. {
  255. bool retVal = false;
  256. if (RCM_UserConvState == RCM_USERCONV_IDLE)
  257. {
  258. RCM_UserConvHandle = handle;
  259. /* must be done last so that RCM_UserConvHandle already has the right value */
  260. RCM_UserConvState = RCM_USERCONV_REQUESTED;
  261. retVal = true;
  262. }
  263. return retVal;
  264. }
  265. /**
  266. * @brief Returns the last user-defined regular conversion that was executed.
  267. *
  268. * This function returns a valid result if the state returned by
  269. * RCM_GetUserConvState is #RCM_USERCONV_EOC.
  270. *
  271. * @retval uint16_t The converted value or 0xFFFF in case of conversion error.
  272. */
  273. uint16_t RCM_GetUserConv(void)
  274. {
  275. uint16_t hRetVal = 0xFFFFu;
  276. if (RCM_UserConvState == RCM_USERCONV_EOC)
  277. {
  278. hRetVal = RCM_UserConvValue;
  279. RCM_UserConvState = RCM_USERCONV_IDLE;
  280. }
  281. return hRetVal;
  282. }
  283. /*
  284. * This function must be scheduled by mc_task.
  285. * It executes the current user conversion that has been selected by the
  286. * latest call to RCM_RequestUserConv
  287. *
  288. * NOTE: This function is not part of the public API and users should not call it.
  289. */
  290. void RCM_ExecUserConv ()
  291. {
  292. if (RCM_UserConvState == RCM_USERCONV_REQUESTED)
  293. {
  294. RCM_UserConvValue = RCM_ExecRegularConv (RCM_UserConvHandle);
  295. RCM_UserConvState = RCM_USERCONV_EOC;
  296. if (RCM_CB_array [RCM_UserConvHandle].cb != NULL)
  297. {
  298. RCM_UserConvState = RCM_USERCONV_IDLE;
  299. RCM_CB_array [RCM_UserConvHandle].cb (RCM_UserConvHandle, RCM_UserConvValue ,RCM_CB_array [RCM_UserConvHandle].data);
  300. }
  301. }
  302. }
  303. /**
  304. * @brief Returns the status of the last requested regular conversion.
  305. *
  306. * It can be one of the following values:
  307. * - UDRC_STATE_IDLE no regular conversion request pending.
  308. * - UDRC_STATE_REQUESTED regular conversion has been requested and not completed.
  309. * - UDRC_STATE_EOC regular conversion has been completed but not readed from the user.
  310. *
  311. * @retval The state of the last user-defined regular conversion.
  312. */
  313. RCM_UserConvState_t RCM_GetUserConvState(void)
  314. {
  315. return RCM_UserConvState;
  316. }
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/