您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

5100 行
166 KiB

  1. #include "plsr_platform.h"
  2. #include "plsr.h"
  3. static uint8_t PlsrFlashNeedsStartupRecovery(
  4. uint8_t haveValidRecord,
  5. uint8_t sectorAHasProgrammedSlot,
  6. uint32_t sectorAFirstErasedAddress,
  7. uint8_t sectorBHasProgrammedSlot,
  8. uint32_t sectorBFirstErasedAddress)
  9. {
  10. return ((haveValidRecord == 0U)
  11. && (sectorAHasProgrammedSlot != 0U)
  12. && (sectorBHasProgrammedSlot != 0U)
  13. && (sectorAFirstErasedAddress == 0UL)
  14. && (sectorBFirstErasedAddress == 0UL)) ? 1U : 0U;
  15. }
  16. #ifdef PLSR_HOST_TEST
  17. #include <string.h>
  18. static uint8_t PlsrHostPulseActive[4];
  19. static uint32_t PlsrHostFrequency[4];
  20. static uint32_t PlsrHostQueuedFrequency[4];
  21. static PLSR_PLATFORM_TIMER_SETTING PlsrHostActiveSetting[4];
  22. static PLSR_PLATFORM_TIMER_SETTING PlsrHostQueuedSetting[4];
  23. static uint8_t PlsrHostUpdatePending[4];
  24. static uint8_t PlsrHostOutputMode[4];
  25. static uint8_t PlsrHostDirectionPositive[4];
  26. static uint8_t PlsrHostAbQuarter[4];
  27. static uint8_t PlsrHostAbPhase[4];
  28. static uint32_t PlsrHostAbTransitions[4];
  29. static uint8_t PlsrHostAbStopPending[4];
  30. static uint8_t PlsrHostAbFastGated[4];
  31. static uint32_t PlsrHostAbFastGateCount;
  32. static uint32_t PlsrHostAbCleanupCount;
  33. static uint64_t PlsrHostObservedPulses[4];
  34. static uint8_t PlsrHostInputs[2];
  35. static uint8_t PlsrHostSelectedPulse;
  36. static uint8_t PlsrHostDirectionLevel;
  37. static uint8_t PlsrHostDirectionPinLevel[4];
  38. static uint32_t PlsrHostDirectionWriteCount[4];
  39. static uint32_t PlsrHostDirectionTransitionCount[4];
  40. static uint8_t PlsrHostEmitPulseOnCriticalEntry;
  41. static uint8_t PlsrHostEmitPulseOnCriticalExit;
  42. static uint8_t PlsrHostLatchPulseOnCriticalEntry;
  43. static uint8_t PlsrHostCriticalEntriesToSkip;
  44. static uint8_t PlsrHostLatchAbFinalQuarterOnStopArm;
  45. static uint8_t PlsrHostCompleteAbCycleOnQueueCommit;
  46. static uint32_t PlsrHostCriticalDepth;
  47. static uint8_t PlsrHostFailNextStart;
  48. static uint8_t PlsrHostStaleNextFrequencyAtUpdate;
  49. static uint8_t PlsrHostFailNextFrequencyAtUpdate;
  50. static uint8_t PlsrHostFailNextStopRequest;
  51. static uint8_t PlsrHostFinalArmJobPending;
  52. static uint8_t PlsrHostFinalArmJobOwner;
  53. static uint8_t PlsrHostDeferFinalArmJob;
  54. static int32_t PlsrHostCountOffset;
  55. static int32_t PlsrHostFrequencyOffsetHz;
  56. static uint8_t PlsrHostCurveMismatchPending;
  57. static uint16_t PlsrHostDiagnosticFault;
  58. static PLSR_PERSIST_PAYLOAD PlsrHostPersistentPayload;
  59. static uint8_t PlsrHostPersistentValid;
  60. static uint32_t PlsrHostSaveCount;
  61. static uint8_t PlsrHostFiniteEnabled;
  62. static uint8_t PlsrHostFiniteActive[4];
  63. static uint8_t PlsrHostFiniteComplete[4];
  64. static uint8_t PlsrHostFiniteFrequencyPending[4];
  65. static uint8_t PlsrHostCountedStreamActive[4];
  66. static uint32_t PlsrHostFiniteTarget[4];
  67. static uint32_t PlsrHostFiniteEmitted[4];
  68. static PLSR_PLATFORM_FINITE_STEP *PlsrHostFiniteSteps[4];
  69. static uint16_t PlsrHostFiniteStepCount[4];
  70. static uint16_t PlsrHostFiniteStepIndex[4];
  71. static uint16_t PlsrHostFiniteBoundaryReadIndex[4];
  72. static uint16_t PlsrHostFiniteCompletedStepCount[4];
  73. static void PlsrHostServiceFinalArmJob(void);
  74. static uint32_t PlsrHostOffsetFrequency(uint32_t frequencyHz)
  75. {
  76. int64_t adjusted = (int64_t)frequencyHz
  77. + (int64_t)PlsrHostFrequencyOffsetHz;
  78. if (adjusted < 1)
  79. {
  80. adjusted = 1;
  81. }
  82. if (adjusted > (int64_t)PLSR_FREQUENCY_MAX_HZ)
  83. {
  84. adjusted = (int64_t)PLSR_FREQUENCY_MAX_HZ;
  85. }
  86. return (uint32_t)adjusted;
  87. }
  88. static void PlsrHostLatchPulse(uint8_t pulseOutput)
  89. {
  90. if ((pulseOutput <= 3U)
  91. && (PlsrHostPulseActive[pulseOutput] != 0U))
  92. {
  93. if (PlsrHostCurveMismatchPending != 0U)
  94. {
  95. PlsrHostCurveMismatchPending = 0U;
  96. PlsrHostDiagnosticFault = 3U;
  97. }
  98. else if ((PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  99. || (PlsrHostAbStopPending[pulseOutput] == 0U))
  100. {
  101. PlsrHostFrequency[pulseOutput] =
  102. PlsrHostQueuedFrequency[pulseOutput];
  103. PlsrHostActiveSetting[pulseOutput] =
  104. PlsrHostQueuedSetting[pulseOutput];
  105. }
  106. PlsrHostObservedPulses[pulseOutput]++;
  107. PlsrHostUpdatePending[pulseOutput] = 1U;
  108. }
  109. }
  110. static void PlsrHostServicePendingPulse(uint8_t pulseOutput)
  111. {
  112. if ((pulseOutput <= 3U)
  113. && (PlsrHostUpdatePending[pulseOutput] != 0U))
  114. {
  115. PlsrHostUpdatePending[pulseOutput] = 0U;
  116. if ((PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  117. && (PlsrHostAbStopPending[pulseOutput] != 0U)
  118. && (PlsrHostAbFastGated[pulseOutput] == 0U))
  119. {
  120. PlsrHostAbFastGated[pulseOutput] = 1U;
  121. PlsrHostPulseActive[pulseOutput] = 0U;
  122. PlsrHostAbFastGateCount++;
  123. }
  124. PlsrPulseTimerIrq(pulseOutput);
  125. }
  126. if (PlsrHostDeferFinalArmJob == 0U)
  127. {
  128. PlsrHostServiceFinalArmJob();
  129. }
  130. }
  131. static void PlsrHostServiceFinalArmJob(void)
  132. {
  133. uint8_t owner;
  134. if (PlsrHostFinalArmJobPending == 0U)
  135. {
  136. return;
  137. }
  138. owner = PlsrHostFinalArmJobOwner;
  139. PlsrHostFinalArmJobPending = 0U;
  140. PlsrHostFinalArmJobOwner = 0xFFU;
  141. PlsrFinalArmJobIrq(owner);
  142. }
  143. static uint8_t PlsrHostAdvanceAbQuarter(uint8_t pulseOutput)
  144. {
  145. static const uint8_t positivePhase[4] = {0U, 2U, 3U, 1U};
  146. static const uint8_t negativePhase[4] = {0U, 1U, 3U, 2U};
  147. if ((pulseOutput > 3U)
  148. || (PlsrHostPulseActive[pulseOutput] == 0U)
  149. || (PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_AB))
  150. {
  151. return 0U;
  152. }
  153. PlsrHostAbQuarter[pulseOutput] =
  154. (uint8_t)((PlsrHostAbQuarter[pulseOutput] + 1U) & 3U);
  155. PlsrHostAbPhase[pulseOutput] =
  156. (PlsrHostDirectionPositive[pulseOutput] != 0U)
  157. ? positivePhase[PlsrHostAbQuarter[pulseOutput]]
  158. : negativePhase[PlsrHostAbQuarter[pulseOutput]];
  159. PlsrHostAbTransitions[pulseOutput]++;
  160. if (PlsrHostAbQuarter[pulseOutput] != 0U)
  161. {
  162. return 0U;
  163. }
  164. PlsrHostLatchPulse(pulseOutput);
  165. return 1U;
  166. }
  167. uint8_t PlsrPlatformInit(void)
  168. {
  169. uint8_t index;
  170. (void)memset(PlsrHostPulseActive, 0, sizeof(PlsrHostPulseActive));
  171. (void)memset(PlsrHostFrequency, 0, sizeof(PlsrHostFrequency));
  172. (void)memset(PlsrHostQueuedFrequency, 0,
  173. sizeof(PlsrHostQueuedFrequency));
  174. (void)memset(PlsrHostActiveSetting, 0,
  175. sizeof(PlsrHostActiveSetting));
  176. (void)memset(PlsrHostQueuedSetting, 0,
  177. sizeof(PlsrHostQueuedSetting));
  178. (void)memset(PlsrHostUpdatePending, 0,
  179. sizeof(PlsrHostUpdatePending));
  180. (void)memset(PlsrHostOutputMode, 0, sizeof(PlsrHostOutputMode));
  181. (void)memset(PlsrHostDirectionPositive, 0,
  182. sizeof(PlsrHostDirectionPositive));
  183. (void)memset(PlsrHostAbQuarter, 0, sizeof(PlsrHostAbQuarter));
  184. (void)memset(PlsrHostAbPhase, 0, sizeof(PlsrHostAbPhase));
  185. (void)memset(PlsrHostAbTransitions, 0,
  186. sizeof(PlsrHostAbTransitions));
  187. (void)memset(PlsrHostAbStopPending, 0,
  188. sizeof(PlsrHostAbStopPending));
  189. (void)memset(PlsrHostAbFastGated, 0,
  190. sizeof(PlsrHostAbFastGated));
  191. PlsrHostAbFastGateCount = 0UL;
  192. PlsrHostAbCleanupCount = 0UL;
  193. (void)memset(PlsrHostObservedPulses, 0,
  194. sizeof(PlsrHostObservedPulses));
  195. PlsrHostSelectedPulse = 0U;
  196. PlsrHostDirectionLevel = 0U;
  197. for (index = 0U; index < 4U; index++)
  198. {
  199. PlsrHostDirectionPinLevel[index] = 1U;
  200. PlsrHostDirectionWriteCount[index] = 0UL;
  201. PlsrHostDirectionTransitionCount[index] = 0UL;
  202. }
  203. PlsrHostEmitPulseOnCriticalEntry = 0U;
  204. PlsrHostEmitPulseOnCriticalExit = 0U;
  205. PlsrHostLatchPulseOnCriticalEntry = 0U;
  206. PlsrHostCriticalEntriesToSkip = 0U;
  207. PlsrHostLatchAbFinalQuarterOnStopArm = 0U;
  208. PlsrHostCompleteAbCycleOnQueueCommit = 0U;
  209. PlsrHostCriticalDepth = 0UL;
  210. PlsrHostFailNextStart = 0U;
  211. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  212. PlsrHostFailNextFrequencyAtUpdate = 0U;
  213. PlsrHostFailNextStopRequest = 0U;
  214. PlsrHostFinalArmJobPending = 0U;
  215. PlsrHostFinalArmJobOwner = 0xFFU;
  216. PlsrHostDeferFinalArmJob = 0U;
  217. PlsrHostCountOffset = 0L;
  218. PlsrHostFrequencyOffsetHz = 0L;
  219. PlsrHostCurveMismatchPending = 0U;
  220. PlsrHostDiagnosticFault = 0U;
  221. PlsrHostFiniteEnabled = 0U;
  222. (void)memset(PlsrHostFiniteActive, 0, sizeof(PlsrHostFiniteActive));
  223. (void)memset(PlsrHostFiniteComplete, 0,
  224. sizeof(PlsrHostFiniteComplete));
  225. (void)memset(PlsrHostFiniteFrequencyPending, 0,
  226. sizeof(PlsrHostFiniteFrequencyPending));
  227. (void)memset(PlsrHostFiniteTarget, 0, sizeof(PlsrHostFiniteTarget));
  228. (void)memset(PlsrHostFiniteEmitted, 0, sizeof(PlsrHostFiniteEmitted));
  229. (void)memset(PlsrHostFiniteSteps, 0, sizeof(PlsrHostFiniteSteps));
  230. (void)memset(PlsrHostFiniteStepCount, 0,
  231. sizeof(PlsrHostFiniteStepCount));
  232. (void)memset(PlsrHostFiniteStepIndex, 0,
  233. sizeof(PlsrHostFiniteStepIndex));
  234. (void)memset(PlsrHostFiniteBoundaryReadIndex, 0,
  235. sizeof(PlsrHostFiniteBoundaryReadIndex));
  236. (void)memset(PlsrHostFiniteCompletedStepCount, 0,
  237. sizeof(PlsrHostFiniteCompletedStepCount));
  238. return 1U;
  239. }
  240. uint8_t PlsrPlatformPrepare(uint8_t pulseOutput,
  241. uint8_t directionOutput,
  242. uint8_t directionLevel,
  243. uint8_t outputMode,
  244. uint8_t directionPositive)
  245. {
  246. uint8_t index;
  247. uint8_t pinLevel;
  248. if (PlsrHostFailNextStart != 0U)
  249. {
  250. PlsrHostFailNextStart = 0U;
  251. return 0U;
  252. }
  253. if ((pulseOutput > 3U) || (directionOutput > 3U)
  254. || (outputMode > PLSR_OUTPUT_AB)
  255. || ((outputMode == PLSR_OUTPUT_AB)
  256. && ((pulseOutput & 1U) != 0U))
  257. || (PlsrHostAbStopPending[0] != 0U)
  258. || (PlsrHostAbStopPending[2] != 0U))
  259. {
  260. return 0U;
  261. }
  262. for (index = 0U; index < 4U; index++)
  263. {
  264. PlsrHostPulseActive[index] = 0U;
  265. PlsrHostFrequency[index] = 0UL;
  266. PlsrHostQueuedFrequency[index] = 0UL;
  267. PlsrHostUpdatePending[index] = 0U;
  268. pinLevel = ((outputMode == PLSR_OUTPUT_PULSE_DIR)
  269. && (index == directionOutput)
  270. && (directionLevel != 0U)) ? 0U : 1U;
  271. PlsrHostDirectionWriteCount[index]++;
  272. if (PlsrHostDirectionPinLevel[index] != pinLevel)
  273. {
  274. PlsrHostDirectionTransitionCount[index]++;
  275. }
  276. PlsrHostDirectionPinLevel[index] = pinLevel;
  277. }
  278. PlsrHostSelectedPulse = pulseOutput;
  279. PlsrHostDirectionLevel = (directionLevel != 0U) ? 1U : 0U;
  280. PlsrHostOutputMode[pulseOutput] = outputMode;
  281. PlsrHostDirectionPositive[pulseOutput] =
  282. (directionPositive != 0U) ? 1U : 0U;
  283. PlsrHostAbQuarter[pulseOutput] = 0U;
  284. PlsrHostAbPhase[pulseOutput] = 0U;
  285. PlsrHostAbFastGated[pulseOutput] = 0U;
  286. return 1U;
  287. }
  288. uint8_t PlsrPlatformStartPulse(uint8_t pulseOutput,
  289. uint32_t firstFrequencyHz,
  290. uint32_t queuedFrequencyHz,
  291. uint32_t *actualFirstFrequencyHz,
  292. uint32_t *actualQueuedFrequencyHz)
  293. {
  294. PLSR_PLATFORM_TIMER_SETTING firstSetting;
  295. PLSR_PLATFORM_TIMER_SETTING queuedSetting;
  296. if ((pulseOutput > 3U)
  297. || (PlsrPlatformBuildTimerSetting(
  298. pulseOutput, PlsrHostOutputMode[pulseOutput],
  299. firstFrequencyHz, &firstSetting) == 0U)
  300. || (PlsrPlatformBuildTimerSetting(
  301. pulseOutput, PlsrHostOutputMode[pulseOutput],
  302. queuedFrequencyHz, &queuedSetting) == 0U))
  303. {
  304. return 0U;
  305. }
  306. return PlsrPlatformStartPrepared(pulseOutput, &firstSetting,
  307. &queuedSetting,
  308. actualFirstFrequencyHz,
  309. actualQueuedFrequencyHz);
  310. }
  311. uint8_t PlsrPlatformBuildTimerSetting(
  312. uint8_t pulseOutput,
  313. uint8_t outputMode,
  314. uint32_t requestedFrequencyHz,
  315. PLSR_PLATFORM_TIMER_SETTING *setting)
  316. {
  317. if ((pulseOutput > 3U) || (outputMode > PLSR_OUTPUT_AB)
  318. || ((outputMode == PLSR_OUTPUT_AB)
  319. && ((pulseOutput & 1U) != 0U))
  320. || (requestedFrequencyHz == 0UL)
  321. || (requestedFrequencyHz > PLSR_FREQUENCY_MAX_HZ)
  322. || (setting == NULL))
  323. {
  324. return 0U;
  325. }
  326. setting->actualFrequencyHz = requestedFrequencyHz;
  327. setting->prescaler = (outputMode == PLSR_OUTPUT_AB) ? 1U : 0U;
  328. setting->pairPrescaler = 0U;
  329. setting->period = (outputMode == PLSR_OUTPUT_AB) ? 3U : 1U;
  330. setting->compare = (outputMode == PLSR_OUTPUT_AB) ? 2U : 1U;
  331. return 1U;
  332. }
  333. uint8_t PlsrPlatformStartPrepared(
  334. uint8_t pulseOutput,
  335. const PLSR_PLATFORM_TIMER_SETTING *firstSetting,
  336. const PLSR_PLATFORM_TIMER_SETTING *queuedSetting,
  337. uint32_t *actualFirstFrequencyHz,
  338. uint32_t *actualQueuedFrequencyHz)
  339. {
  340. if ((pulseOutput > 3U) || (firstSetting == NULL)
  341. || (queuedSetting == NULL) || (actualFirstFrequencyHz == NULL)
  342. || (actualQueuedFrequencyHz == NULL)
  343. || (firstSetting->actualFrequencyHz == 0UL)
  344. || (firstSetting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ)
  345. || (queuedSetting->actualFrequencyHz == 0UL)
  346. || (queuedSetting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ)
  347. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  348. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  349. {
  350. return 0U;
  351. }
  352. PlsrHostPulseActive[pulseOutput] = 1U;
  353. PlsrHostFrequency[pulseOutput] = firstSetting->actualFrequencyHz;
  354. PlsrHostQueuedFrequency[pulseOutput] = queuedSetting->actualFrequencyHz;
  355. PlsrHostActiveSetting[pulseOutput] = *firstSetting;
  356. PlsrHostQueuedSetting[pulseOutput] = *queuedSetting;
  357. PlsrHostUpdatePending[pulseOutput] = 0U;
  358. PlsrHostSelectedPulse = pulseOutput;
  359. *actualFirstFrequencyHz = firstSetting->actualFrequencyHz;
  360. *actualQueuedFrequencyHz = queuedSetting->actualFrequencyHz;
  361. return 1U;
  362. }
  363. uint8_t PlsrPlatformSupportsFinitePulseTrain(void)
  364. {
  365. return PlsrHostFiniteEnabled;
  366. }
  367. uint8_t PlsrPlatformStartFinitePrepared(
  368. uint8_t pulseOutput,
  369. const PLSR_PLATFORM_TIMER_SETTING *setting,
  370. uint32_t pulseCount,
  371. uint32_t *actualFrequencyHz)
  372. {
  373. if ((PlsrHostFiniteEnabled == 0U) || (pulseOutput > 3U)
  374. || (setting == NULL) || (pulseCount == 0UL)
  375. || (actualFrequencyHz == NULL)
  376. || (PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_PULSE_DIR))
  377. {
  378. return 0U;
  379. }
  380. PlsrHostPulseActive[pulseOutput] = 1U;
  381. PlsrHostFiniteActive[pulseOutput] = 1U;
  382. PlsrHostFiniteComplete[pulseOutput] = 0U;
  383. PlsrHostFiniteFrequencyPending[pulseOutput] = 0U;
  384. PlsrHostCountedStreamActive[pulseOutput] = 0U;
  385. PlsrHostFiniteTarget[pulseOutput] = pulseCount;
  386. PlsrHostFiniteEmitted[pulseOutput] = 0UL;
  387. PlsrHostFiniteStepCount[pulseOutput] = 0U;
  388. PlsrHostFiniteStepIndex[pulseOutput] = 0U;
  389. PlsrHostFiniteBoundaryReadIndex[pulseOutput] = 0U;
  390. PlsrHostFiniteCompletedStepCount[pulseOutput] = 0U;
  391. PlsrHostFrequency[pulseOutput] = setting->actualFrequencyHz;
  392. PlsrHostQueuedFrequency[pulseOutput] = setting->actualFrequencyHz;
  393. PlsrHostActiveSetting[pulseOutput] = *setting;
  394. PlsrHostQueuedSetting[pulseOutput] = *setting;
  395. PlsrHostSelectedPulse = pulseOutput;
  396. *actualFrequencyHz = setting->actualFrequencyHz;
  397. return 1U;
  398. }
  399. uint8_t PlsrPlatformStartCountedStreamPrepared(
  400. uint8_t pulseOutput,
  401. const PLSR_PLATFORM_TIMER_SETTING *setting,
  402. uint32_t pulseCount,
  403. uint32_t *actualFrequencyHz)
  404. {
  405. if (PlsrPlatformStartFinitePrepared(pulseOutput, setting, pulseCount,
  406. actualFrequencyHz) == 0U)
  407. {
  408. return 0U;
  409. }
  410. PlsrHostCountedStreamActive[pulseOutput] = 1U;
  411. return 1U;
  412. }
  413. uint8_t PlsrPlatformStartFiniteSequencePrepared(
  414. uint8_t pulseOutput,
  415. PLSR_PLATFORM_FINITE_STEP *steps,
  416. uint16_t stepCount,
  417. uint32_t *actualFrequencyHz)
  418. {
  419. if ((steps == NULL) || (stepCount == 0U)
  420. || (stepCount > PLSR_PLATFORM_FINITE_STEP_MAX)
  421. || (PlsrPlatformStartFinitePrepared(
  422. pulseOutput, &steps[0].setting, steps[0].pulseCount,
  423. actualFrequencyHz) == 0U))
  424. {
  425. return 0U;
  426. }
  427. PlsrHostFiniteSteps[pulseOutput] = steps;
  428. PlsrHostFiniteStepCount[pulseOutput] = stepCount;
  429. PlsrHostFiniteStepIndex[pulseOutput] = 0U;
  430. PlsrHostFiniteBoundaryReadIndex[pulseOutput] = 0U;
  431. PlsrHostFiniteCompletedStepCount[pulseOutput] = 0U;
  432. return 1U;
  433. }
  434. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformUpdateFinitePrepared(
  435. uint8_t pulseOutput,
  436. const PLSR_PLATFORM_TIMER_SETTING *setting,
  437. uint32_t *actualFrequencyHz)
  438. {
  439. if (PlsrHostFailNextFrequencyAtUpdate != 0U)
  440. {
  441. PlsrHostFailNextFrequencyAtUpdate = 0U;
  442. return PLSR_PLATFORM_QUEUE_FAILED;
  443. }
  444. if ((pulseOutput > 3U) || (setting == NULL)
  445. || (actualFrequencyHz == NULL)
  446. || (PlsrHostFiniteActive[pulseOutput] == 0U))
  447. {
  448. return PLSR_PLATFORM_QUEUE_STALE;
  449. }
  450. if (PlsrHostStaleNextFrequencyAtUpdate != 0U)
  451. {
  452. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  453. return PLSR_PLATFORM_QUEUE_STALE;
  454. }
  455. if (PlsrHostFiniteFrequencyPending[pulseOutput] != 0U)
  456. {
  457. return PLSR_PLATFORM_QUEUE_STALE;
  458. }
  459. PlsrHostQueuedSetting[pulseOutput] = *setting;
  460. PlsrHostQueuedFrequency[pulseOutput] = setting->actualFrequencyHz;
  461. PlsrHostFiniteFrequencyPending[pulseOutput] = 1U;
  462. *actualFrequencyHz = setting->actualFrequencyHz;
  463. return PLSR_PLATFORM_QUEUE_APPLIED;
  464. }
  465. uint8_t PlsrPlatformRetargetFiniteStop(uint8_t pulseOutput,
  466. uint32_t drainPulses)
  467. {
  468. uint32_t completed;
  469. if ((pulseOutput > 3U) || (drainPulses == 0UL)
  470. || (PlsrHostFiniteActive[pulseOutput] == 0U))
  471. {
  472. return 0U;
  473. }
  474. completed = PlsrHostFiniteEmitted[pulseOutput];
  475. if (PlsrHostFiniteStepCount[pulseOutput] != 0U)
  476. {
  477. completed += PlsrHostFiniteSteps[pulseOutput][
  478. PlsrHostFiniteStepIndex[pulseOutput]].segmentPulseOffset;
  479. }
  480. PlsrHostFiniteTarget[pulseOutput] = completed + drainPulses;
  481. PlsrHostFiniteEmitted[pulseOutput] = completed;
  482. PlsrHostFiniteStepCount[pulseOutput] = 0U;
  483. PlsrHostFiniteStepIndex[pulseOutput] = 0U;
  484. PlsrHostFiniteBoundaryReadIndex[pulseOutput] = 0U;
  485. PlsrHostFiniteCompletedStepCount[pulseOutput] = 0U;
  486. return 1U;
  487. }
  488. uint8_t PlsrPlatformFiniteRetargetReady(uint8_t pulseOutput,
  489. uint32_t *activeFrequencyHz)
  490. {
  491. if ((pulseOutput > 3U) || (activeFrequencyHz == NULL)
  492. || (PlsrHostFiniteActive[pulseOutput] == 0U))
  493. {
  494. return 0U;
  495. }
  496. *activeFrequencyHz = PlsrHostFrequency[pulseOutput];
  497. return (PlsrHostFiniteStepCount[pulseOutput] == 0U) ? 1U : 0U;
  498. }
  499. uint8_t PlsrPlatformFiniteProgress(uint8_t pulseOutput,
  500. uint32_t *completedPulses)
  501. {
  502. if ((pulseOutput > 3U) || (completedPulses == NULL)
  503. || ((PlsrHostFiniteActive[pulseOutput] == 0U)
  504. && (PlsrHostFiniteComplete[pulseOutput] == 0U)))
  505. {
  506. return 0U;
  507. }
  508. if (PlsrHostFiniteStepCount[pulseOutput] != 0U)
  509. {
  510. const PLSR_PLATFORM_FINITE_STEP *step =
  511. &PlsrHostFiniteSteps[pulseOutput][
  512. PlsrHostFiniteStepIndex[pulseOutput]];
  513. *completedPulses = step->segmentPulseOffset
  514. + PlsrHostFiniteEmitted[pulseOutput];
  515. }
  516. else
  517. {
  518. *completedPulses = PlsrHostFiniteEmitted[pulseOutput];
  519. }
  520. return 1U;
  521. }
  522. uint8_t PlsrPlatformTakeFiniteCompletion(uint8_t pulseOutput,
  523. uint32_t *completedPulses)
  524. {
  525. if ((pulseOutput > 3U) || (completedPulses == NULL)
  526. || (PlsrHostFiniteComplete[pulseOutput] == 0U))
  527. {
  528. return 0U;
  529. }
  530. PlsrHostFiniteComplete[pulseOutput] = 0U;
  531. if (PlsrHostFiniteStepCount[pulseOutput] != 0U)
  532. {
  533. const PLSR_PLATFORM_FINITE_STEP *step =
  534. &PlsrHostFiniteSteps[pulseOutput][
  535. PlsrHostFiniteStepCount[pulseOutput] - 1U];
  536. *completedPulses = step->segmentPulseOffset + step->pulseCount;
  537. }
  538. else
  539. {
  540. *completedPulses = PlsrHostFiniteTarget[pulseOutput];
  541. }
  542. return 1U;
  543. }
  544. uint8_t PlsrPlatformTakeFiniteBoundary(uint8_t pulseOutput,
  545. uint8_t *segmentNumber,
  546. uint32_t *completedPulses,
  547. uint8_t *sequenceContinues,
  548. uint32_t *activeFrequencyHz)
  549. {
  550. if ((pulseOutput > 3U) || (segmentNumber == NULL)
  551. || (completedPulses == NULL) || (sequenceContinues == NULL)
  552. || (activeFrequencyHz == NULL))
  553. {
  554. return 0U;
  555. }
  556. while (PlsrHostFiniteBoundaryReadIndex[pulseOutput]
  557. < PlsrHostFiniteCompletedStepCount[pulseOutput])
  558. {
  559. uint16_t index = PlsrHostFiniteBoundaryReadIndex[pulseOutput]++;
  560. const PLSR_PLATFORM_FINITE_STEP *step =
  561. &PlsrHostFiniteSteps[pulseOutput][index];
  562. if (step->completesSegment != 0U)
  563. {
  564. *segmentNumber = step->segmentNumber;
  565. *completedPulses = step->segmentPulseOffset + step->pulseCount;
  566. *sequenceContinues =
  567. (index + 1U < PlsrHostFiniteStepCount[pulseOutput]) ? 1U : 0U;
  568. *activeFrequencyHz = (*sequenceContinues != 0U)
  569. ? PlsrHostFiniteSteps[pulseOutput][index + 1U]
  570. .setting.actualFrequencyHz
  571. : step->setting.actualFrequencyHz;
  572. return 1U;
  573. }
  574. }
  575. return 0U;
  576. }
  577. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformLoadPreparedFromIrq(
  578. uint8_t pulseOutput,
  579. const PLSR_PLATFORM_TIMER_SETTING *setting,
  580. uint32_t *actualFrequencyHz)
  581. {
  582. if (PlsrHostFailNextFrequencyAtUpdate != 0U)
  583. {
  584. PlsrHostFailNextFrequencyAtUpdate = 0U;
  585. return PLSR_PLATFORM_QUEUE_FAILED;
  586. }
  587. if ((pulseOutput > 3U) || (setting == NULL)
  588. || (actualFrequencyHz == NULL)
  589. || (setting->actualFrequencyHz == 0UL)
  590. || (setting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ))
  591. {
  592. return PLSR_PLATFORM_QUEUE_FAILED;
  593. }
  594. if (PlsrHostStaleNextFrequencyAtUpdate != 0U)
  595. {
  596. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  597. return PLSR_PLATFORM_QUEUE_STALE;
  598. }
  599. if ((PlsrHostCompleteAbCycleOnQueueCommit != 0U)
  600. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB))
  601. {
  602. PlsrHostCompleteAbCycleOnQueueCommit = 0U;
  603. PlsrTestEmitAbQuarters(4UL);
  604. }
  605. if ((PlsrHostPulseActive[pulseOutput] == 0U)
  606. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  607. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  608. {
  609. return PLSR_PLATFORM_QUEUE_STALE;
  610. }
  611. PlsrHostQueuedSetting[pulseOutput] = *setting;
  612. PlsrHostQueuedFrequency[pulseOutput] = setting->actualFrequencyHz;
  613. *actualFrequencyHz = setting->actualFrequencyHz;
  614. return PLSR_PLATFORM_QUEUE_APPLIED;
  615. }
  616. void PlsrPlatformGateFromIrq(uint8_t pulseOutput)
  617. {
  618. if ((pulseOutput <= 3U)
  619. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  620. && (PlsrHostAbFastGated[pulseOutput] != 0U))
  621. {
  622. return;
  623. }
  624. PlsrPlatformStopPulse(pulseOutput);
  625. }
  626. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformQueueFrequency(
  627. uint8_t pulseOutput,
  628. uint32_t frequencyHz,
  629. uint32_t *actualFrequencyHz)
  630. {
  631. uint32_t criticalState;
  632. PLSR_PLATFORM_TIMER_SETTING setting;
  633. if (PlsrHostFailNextFrequencyAtUpdate != 0U)
  634. {
  635. PlsrHostFailNextFrequencyAtUpdate = 0U;
  636. return PLSR_PLATFORM_QUEUE_FAILED;
  637. }
  638. if ((pulseOutput > 3U)
  639. || (PlsrPlatformBuildTimerSetting(
  640. pulseOutput, PlsrHostOutputMode[pulseOutput], frequencyHz,
  641. &setting) == 0U)
  642. || (actualFrequencyHz == NULL)
  643. || (PlsrHostPulseActive[pulseOutput] == 0U)
  644. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  645. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  646. {
  647. return PLSR_PLATFORM_QUEUE_FAILED;
  648. }
  649. if (PlsrHostStaleNextFrequencyAtUpdate != 0U)
  650. {
  651. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  652. return PLSR_PLATFORM_QUEUE_STALE;
  653. }
  654. if ((PlsrHostCompleteAbCycleOnQueueCommit != 0U)
  655. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB))
  656. {
  657. PlsrHostCompleteAbCycleOnQueueCommit = 0U;
  658. PlsrTestEmitAbQuarters(4UL);
  659. }
  660. criticalState = PlsrPlatformEnterCritical();
  661. if ((PlsrHostPulseActive[pulseOutput] == 0U)
  662. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  663. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  664. {
  665. PlsrPlatformExitCritical(criticalState);
  666. return PLSR_PLATFORM_QUEUE_STALE;
  667. }
  668. PlsrHostQueuedFrequency[pulseOutput] = setting.actualFrequencyHz;
  669. PlsrHostQueuedSetting[pulseOutput] = setting;
  670. *actualFrequencyHz = setting.actualFrequencyHz;
  671. PlsrPlatformExitCritical(criticalState);
  672. return PLSR_PLATFORM_QUEUE_APPLIED;
  673. }
  674. void PlsrPlatformDrainPendingPulse(uint8_t pulseOutput)
  675. {
  676. PlsrHostServicePendingPulse(pulseOutput);
  677. }
  678. uint32_t PlsrPlatformActiveFrequency(uint8_t pulseOutput)
  679. {
  680. if (pulseOutput > 3U)
  681. {
  682. return 0UL;
  683. }
  684. return (PlsrHostFrequencyOffsetHz != 0L)
  685. ? PlsrHostOffsetFrequency(PlsrHostFrequency[pulseOutput])
  686. : PlsrHostFrequency[pulseOutput];
  687. }
  688. uint8_t PlsrPlatformExpectedFrequency(uint8_t pulseOutput,
  689. uint8_t outputMode,
  690. uint32_t requestedFrequencyHz,
  691. uint32_t *actualFrequencyHz)
  692. {
  693. PLSR_PLATFORM_TIMER_SETTING setting;
  694. if (PlsrPlatformBuildTimerSetting(pulseOutput, outputMode,
  695. requestedFrequencyHz,
  696. &setting) == 0U)
  697. {
  698. return 0U;
  699. }
  700. *actualFrequencyHz = setting.actualFrequencyHz;
  701. return 1U;
  702. }
  703. uint64_t PlsrPlatformObservedPulses(uint8_t pulseOutput)
  704. {
  705. int64_t observed;
  706. if (pulseOutput > 3U)
  707. {
  708. return 0UL;
  709. }
  710. observed = (int64_t)PlsrHostObservedPulses[pulseOutput]
  711. + (int64_t)PlsrHostCountOffset;
  712. return (observed > 0) ? (uint64_t)observed : 0UL;
  713. }
  714. uint16_t PlsrPlatformDiagnosticFault(void)
  715. {
  716. uint16_t fault = PlsrHostDiagnosticFault;
  717. PlsrHostDiagnosticFault = 0U;
  718. return fault;
  719. }
  720. PLSR_PLATFORM_STOP_RESULT PlsrPlatformRequestStopLocked(
  721. uint8_t pulseOutput,
  722. uint8_t requireZeroBoundary)
  723. {
  724. if (pulseOutput > 3U)
  725. {
  726. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  727. }
  728. if (PlsrHostFailNextStopRequest != 0U)
  729. {
  730. PlsrHostFailNextStopRequest = 0U;
  731. PlsrPlatformStopPulse(pulseOutput);
  732. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  733. }
  734. if ((requireZeroBoundary != 0U)
  735. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  736. && (PlsrHostPulseActive[pulseOutput] != 0U)
  737. && (PlsrHostAbFastGated[pulseOutput] == 0U))
  738. {
  739. if (PlsrHostLatchAbFinalQuarterOnStopArm != 0U)
  740. {
  741. PlsrHostLatchAbFinalQuarterOnStopArm = 0U;
  742. (void)PlsrHostAdvanceAbQuarter(pulseOutput);
  743. }
  744. PlsrHostQueuedFrequency[pulseOutput] =
  745. PlsrHostFrequency[pulseOutput];
  746. PlsrHostAbStopPending[pulseOutput] = 1U;
  747. return PLSR_PLATFORM_STOP_PENDING;
  748. }
  749. PlsrPlatformStopPulse(pulseOutput);
  750. return PLSR_PLATFORM_STOP_COMPLETE;
  751. }
  752. uint8_t PlsrPlatformQueueFinalArmFromIrq(uint8_t pulseOutput)
  753. {
  754. if ((pulseOutput > 2U) || ((pulseOutput & 1U) != 0U)
  755. || (PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  756. || (PlsrHostPulseActive[pulseOutput] == 0U)
  757. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  758. {
  759. return 0U;
  760. }
  761. PlsrHostFinalArmJobOwner = pulseOutput;
  762. PlsrHostFinalArmJobPending = 1U;
  763. return 1U;
  764. }
  765. void PlsrPlatformStopPulse(uint8_t pulseOutput)
  766. {
  767. if (pulseOutput <= 3U)
  768. {
  769. if ((PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  770. && ((PlsrHostAbStopPending[pulseOutput] != 0U)
  771. || (PlsrHostAbFastGated[pulseOutput] != 0U)))
  772. {
  773. PlsrHostAbCleanupCount++;
  774. }
  775. PlsrHostPulseActive[pulseOutput] = 0U;
  776. PlsrHostFiniteActive[pulseOutput] = 0U;
  777. PlsrHostFiniteComplete[pulseOutput] = 0U;
  778. PlsrHostFrequency[pulseOutput] = 0UL;
  779. PlsrHostQueuedFrequency[pulseOutput] = 0UL;
  780. PlsrHostUpdatePending[pulseOutput] = 0U;
  781. PlsrHostAbQuarter[pulseOutput] = 0U;
  782. PlsrHostAbPhase[pulseOutput] = 0U;
  783. PlsrHostAbStopPending[pulseOutput] = 0U;
  784. PlsrHostAbFastGated[pulseOutput] = 0U;
  785. PlsrHostFrequencyOffsetHz = 0L;
  786. if (PlsrHostFinalArmJobOwner == pulseOutput)
  787. {
  788. PlsrHostFinalArmJobPending = 0U;
  789. PlsrHostFinalArmJobOwner = 0xFFU;
  790. }
  791. }
  792. }
  793. uint8_t PlsrPlatformReadInput(uint8_t inputSelection)
  794. {
  795. return (inputSelection <= 1U) ? PlsrHostInputs[inputSelection] : 0U;
  796. }
  797. uint8_t PlsrPlatformLoad(PLSR_PERSIST_PAYLOAD *payload)
  798. {
  799. if ((payload == NULL) || (PlsrHostPersistentValid == 0U))
  800. {
  801. return 0U;
  802. }
  803. *payload = PlsrHostPersistentPayload;
  804. return 1U;
  805. }
  806. void PlsrPlatformForceSafeOutputsFromFault(void)
  807. {
  808. uint8_t index;
  809. for (index = 0U; index < 4U; index++)
  810. {
  811. PlsrPlatformStopPulse(index);
  812. }
  813. }
  814. PLSR_PLATFORM_SERVICE_RESULT PlsrPlatformServicePersistence(void)
  815. {
  816. return PLSR_PLATFORM_SERVICE_READY;
  817. }
  818. uint8_t PlsrPlatformSave(const PLSR_PERSIST_PAYLOAD *payload)
  819. {
  820. if (payload == NULL)
  821. {
  822. return 0U;
  823. }
  824. PlsrHostPersistentPayload = *payload;
  825. PlsrHostPersistentValid = 1U;
  826. PlsrHostSaveCount++;
  827. return 1U;
  828. }
  829. void PlsrPlatformCheckpointConfig(const PLSR_CONFIG *config)
  830. {
  831. if (config != NULL)
  832. {
  833. PlsrHostPersistentPayload.config = *config;
  834. PlsrHostPersistentValid = 1U;
  835. }
  836. }
  837. void PlsrPlatformCheckpointPosition(int32_t position,
  838. uint8_t positionValid,
  839. uint8_t wasBusy)
  840. {
  841. PlsrHostPersistentPayload.position = position;
  842. PlsrHostPersistentPayload.positionValid = positionValid;
  843. PlsrHostPersistentPayload.wasBusy = wasBusy;
  844. PlsrHostPersistentPayload.reserved = 0U;
  845. }
  846. uint32_t PlsrPlatformEnterCritical(void)
  847. {
  848. uint32_t previousDepth = PlsrHostCriticalDepth;
  849. if (PlsrHostEmitPulseOnCriticalEntry != 0U)
  850. {
  851. if (PlsrHostCriticalEntriesToSkip != 0U)
  852. {
  853. PlsrHostCriticalEntriesToSkip--;
  854. }
  855. else
  856. {
  857. PlsrHostEmitPulseOnCriticalEntry = 0U;
  858. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  859. if (previousDepth == 0UL)
  860. {
  861. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  862. }
  863. }
  864. }
  865. if (PlsrHostLatchPulseOnCriticalEntry != 0U)
  866. {
  867. PlsrHostLatchPulseOnCriticalEntry = 0U;
  868. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  869. }
  870. PlsrHostCriticalDepth++;
  871. return previousDepth;
  872. }
  873. void PlsrPlatformExitCritical(uint32_t state)
  874. {
  875. (void)state;
  876. if (PlsrHostCriticalDepth != 0UL)
  877. {
  878. PlsrHostCriticalDepth--;
  879. }
  880. if (PlsrHostCriticalDepth != 0UL)
  881. {
  882. return;
  883. }
  884. if (PlsrHostEmitPulseOnCriticalExit != 0U)
  885. {
  886. PlsrHostEmitPulseOnCriticalExit = 0U;
  887. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  888. }
  889. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  890. }
  891. void PlsrTestSetInput(uint8_t inputSelection, uint8_t level)
  892. {
  893. if (inputSelection <= 1U)
  894. {
  895. PlsrHostInputs[inputSelection] = (level != 0U) ? 1U : 0U;
  896. }
  897. }
  898. void PlsrTestEmitPulses(uint32_t pulseCount)
  899. {
  900. if (PlsrHostFiniteActive[PlsrHostSelectedPulse] != 0U)
  901. {
  902. uint8_t pulseOutput = PlsrHostSelectedPulse;
  903. while ((pulseCount != 0UL)
  904. && (PlsrHostFiniteActive[pulseOutput] != 0U))
  905. {
  906. uint32_t available = PlsrHostFiniteTarget[pulseOutput]
  907. - PlsrHostFiniteEmitted[pulseOutput];
  908. uint32_t emitted = (pulseCount < available)
  909. ? pulseCount : available;
  910. if ((emitted != 0UL)
  911. && (PlsrHostFiniteFrequencyPending[pulseOutput] != 0U))
  912. {
  913. PlsrHostActiveSetting[pulseOutput] =
  914. PlsrHostQueuedSetting[pulseOutput];
  915. PlsrHostFrequency[pulseOutput] =
  916. PlsrHostQueuedFrequency[pulseOutput];
  917. PlsrHostFiniteFrequencyPending[pulseOutput] = 0U;
  918. }
  919. PlsrHostFiniteEmitted[pulseOutput] += emitted;
  920. PlsrHostObservedPulses[pulseOutput] += emitted;
  921. pulseCount -= emitted;
  922. if (PlsrHostFiniteEmitted[pulseOutput]
  923. == PlsrHostFiniteTarget[pulseOutput])
  924. {
  925. uint16_t index = PlsrHostFiniteStepIndex[pulseOutput];
  926. uint8_t hasNext = (index + 1U
  927. < PlsrHostFiniteStepCount[pulseOutput])
  928. ? 1U : 0U;
  929. if (PlsrHostFiniteStepCount[pulseOutput] != 0U)
  930. {
  931. PlsrHostFiniteCompletedStepCount[pulseOutput] =
  932. (uint16_t)(index + 1U);
  933. }
  934. if (hasNext != 0U)
  935. {
  936. const PLSR_PLATFORM_FINITE_STEP *next =
  937. &PlsrHostFiniteSteps[pulseOutput][index + 1U];
  938. PlsrHostFiniteStepIndex[pulseOutput]++;
  939. PlsrHostFiniteTarget[pulseOutput] = next->pulseCount;
  940. PlsrHostFiniteEmitted[pulseOutput] = 0UL;
  941. PlsrHostFrequency[pulseOutput] =
  942. next->setting.actualFrequencyHz;
  943. PlsrHostQueuedFrequency[pulseOutput] =
  944. next->setting.actualFrequencyHz;
  945. PlsrHostActiveSetting[pulseOutput] = next->setting;
  946. PlsrHostQueuedSetting[pulseOutput] = next->setting;
  947. PlsrHostFiniteFrequencyPending[pulseOutput] = 0U;
  948. }
  949. else
  950. {
  951. PlsrHostFiniteActive[pulseOutput] = 0U;
  952. PlsrHostFiniteComplete[pulseOutput] = 1U;
  953. PlsrHostPulseActive[pulseOutput] = 0U;
  954. PlsrHostFiniteFrequencyPending[pulseOutput] = 0U;
  955. PlsrHostFrequency[pulseOutput] = 0UL;
  956. PlsrHostQueuedFrequency[pulseOutput] = 0UL;
  957. }
  958. }
  959. }
  960. return;
  961. }
  962. if (PlsrHostOutputMode[PlsrHostSelectedPulse] == PLSR_OUTPUT_AB)
  963. {
  964. while ((pulseCount != 0UL)
  965. && (PlsrHostPulseActive[PlsrHostSelectedPulse] != 0U))
  966. {
  967. PlsrTestEmitAbQuarters(4UL);
  968. pulseCount--;
  969. }
  970. return;
  971. }
  972. while ((pulseCount != 0UL)
  973. && (PlsrHostPulseActive[PlsrHostSelectedPulse] != 0U))
  974. {
  975. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  976. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  977. pulseCount--;
  978. }
  979. }
  980. void PlsrTestEnableFinitePulseTrain(uint8_t enable)
  981. {
  982. PlsrHostFiniteEnabled = (enable != 0U) ? 1U : 0U;
  983. }
  984. void PlsrTestCompleteFinitePulseTrain(void)
  985. {
  986. uint8_t pulseOutput = PlsrHostSelectedPulse;
  987. if ((PlsrHostFiniteActive[pulseOutput] != 0U)
  988. && (PlsrHostPulseActive[pulseOutput] != 0U))
  989. {
  990. uint32_t remaining = PlsrHostFiniteTarget[pulseOutput]
  991. - PlsrHostFiniteEmitted[pulseOutput];
  992. PlsrHostFiniteEmitted[pulseOutput] += remaining;
  993. PlsrHostObservedPulses[pulseOutput] += remaining;
  994. PlsrHostFiniteActive[pulseOutput] = 0U;
  995. PlsrHostFiniteComplete[pulseOutput] = 1U;
  996. PlsrHostPulseActive[pulseOutput] = 0U;
  997. PlsrHostFrequency[pulseOutput] = 0UL;
  998. PlsrHostQueuedFrequency[pulseOutput] = 0UL;
  999. }
  1000. }
  1001. uint8_t PlsrTestFinitePulseTrainActive(void)
  1002. {
  1003. return PlsrHostFiniteActive[PlsrHostSelectedPulse];
  1004. }
  1005. void PlsrTestEmitPulseOnCriticalEntry(void)
  1006. {
  1007. PlsrHostCriticalEntriesToSkip = 0U;
  1008. PlsrHostEmitPulseOnCriticalEntry = 1U;
  1009. }
  1010. void PlsrTestEmitPulseAfterCriticalEntries(uint8_t entriesToSkip)
  1011. {
  1012. PlsrHostCriticalEntriesToSkip = entriesToSkip;
  1013. PlsrHostEmitPulseOnCriticalEntry = 1U;
  1014. }
  1015. void PlsrTestEmitPulseOnCriticalExit(void)
  1016. {
  1017. PlsrHostEmitPulseOnCriticalExit = 1U;
  1018. }
  1019. void PlsrTestLatchPulseOnCriticalEntry(void)
  1020. {
  1021. PlsrHostLatchPulseOnCriticalEntry = 1U;
  1022. }
  1023. void PlsrTestLatchAbFinalQuarterOnNextStopArm(void)
  1024. {
  1025. PlsrHostLatchAbFinalQuarterOnStopArm = 1U;
  1026. }
  1027. void PlsrTestCompleteAbCycleOnNextQueueCommit(void)
  1028. {
  1029. PlsrHostCompleteAbCycleOnQueueCommit = 1U;
  1030. }
  1031. void PlsrTestServicePendingPulse(void)
  1032. {
  1033. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  1034. }
  1035. void PlsrTestFailNextStart(void)
  1036. {
  1037. PlsrHostFailNextStart = 1U;
  1038. }
  1039. void PlsrTestServiceFinalArmJob(void)
  1040. {
  1041. PlsrHostServiceFinalArmJob();
  1042. }
  1043. void PlsrTestDeferFinalArmJob(uint8_t defer)
  1044. {
  1045. PlsrHostDeferFinalArmJob = (defer != 0U) ? 1U : 0U;
  1046. if (PlsrHostDeferFinalArmJob == 0U)
  1047. {
  1048. PlsrHostServiceFinalArmJob();
  1049. }
  1050. }
  1051. void PlsrTestStaleNextFrequencyAtUpdate(void)
  1052. {
  1053. PlsrHostStaleNextFrequencyAtUpdate = 1U;
  1054. }
  1055. void PlsrTestFailNextFrequencyAtUpdate(void)
  1056. {
  1057. PlsrHostFailNextFrequencyAtUpdate = 1U;
  1058. }
  1059. void PlsrTestFailNextStopRequest(void)
  1060. {
  1061. PlsrHostFailNextStopRequest = 1U;
  1062. }
  1063. void PlsrTestEmitAbQuarters(uint32_t quarterCount)
  1064. {
  1065. uint8_t pulseOutput = PlsrHostSelectedPulse;
  1066. while ((quarterCount != 0UL)
  1067. && (PlsrHostPulseActive[pulseOutput] != 0U)
  1068. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB))
  1069. {
  1070. if (PlsrHostAdvanceAbQuarter(pulseOutput) != 0U)
  1071. {
  1072. PlsrHostServicePendingPulse(pulseOutput);
  1073. }
  1074. quarterCount--;
  1075. }
  1076. }
  1077. uint8_t PlsrTestPulseIsActive(void)
  1078. {
  1079. return PlsrHostPulseActive[PlsrHostSelectedPulse];
  1080. }
  1081. uint32_t PlsrTestOutputFrequency(void)
  1082. {
  1083. return PlsrHostFrequency[PlsrHostSelectedPulse];
  1084. }
  1085. uint32_t PlsrTestQueuedFrequency(void)
  1086. {
  1087. return PlsrHostQueuedFrequency[PlsrHostSelectedPulse];
  1088. }
  1089. uint8_t PlsrTestDirectionLevel(void)
  1090. {
  1091. return PlsrHostDirectionLevel;
  1092. }
  1093. uint8_t PlsrTestDirectionPinLevel(uint8_t directionOutput)
  1094. {
  1095. return (directionOutput < 4U)
  1096. ? PlsrHostDirectionPinLevel[directionOutput] : 0U;
  1097. }
  1098. uint32_t PlsrTestDirectionWriteCount(uint8_t directionOutput)
  1099. {
  1100. return (directionOutput < 4U)
  1101. ? PlsrHostDirectionWriteCount[directionOutput] : 0UL;
  1102. }
  1103. uint32_t PlsrTestDirectionTransitionCount(uint8_t directionOutput)
  1104. {
  1105. return (directionOutput < 4U)
  1106. ? PlsrHostDirectionTransitionCount[directionOutput] : 0UL;
  1107. }
  1108. uint8_t PlsrTestAbPhase(void)
  1109. {
  1110. return PlsrHostAbPhase[PlsrHostSelectedPulse];
  1111. }
  1112. uint32_t PlsrTestAbTransitionCount(void)
  1113. {
  1114. return PlsrHostAbTransitions[PlsrHostSelectedPulse];
  1115. }
  1116. uint32_t PlsrTestAbFastGateCount(void)
  1117. {
  1118. return PlsrHostAbFastGateCount;
  1119. }
  1120. uint32_t PlsrTestAbCleanupCount(void)
  1121. {
  1122. return PlsrHostAbCleanupCount;
  1123. }
  1124. void PlsrTestInjectCountOffset(int32_t offset)
  1125. {
  1126. PlsrHostCountOffset = offset;
  1127. }
  1128. void PlsrTestInjectActiveFrequencyOffset(int32_t offsetHz)
  1129. {
  1130. PlsrHostFrequencyOffsetHz = offsetHz;
  1131. PlsrHostDiagnosticFault = 2U;
  1132. }
  1133. void PlsrTestInjectCurveMismatch(void)
  1134. {
  1135. PlsrHostCurveMismatchPending = 1U;
  1136. }
  1137. void PlsrTestClearPersistentStorage(void)
  1138. {
  1139. (void)memset(&PlsrHostPersistentPayload, 0,
  1140. sizeof(PlsrHostPersistentPayload));
  1141. (void)memset(PlsrHostInputs, 0, sizeof(PlsrHostInputs));
  1142. PlsrHostPersistentValid = 0U;
  1143. PlsrHostSaveCount = 0UL;
  1144. PlsrHostCountOffset = 0L;
  1145. PlsrHostFrequencyOffsetHz = 0L;
  1146. PlsrHostCurveMismatchPending = 0U;
  1147. PlsrHostDiagnosticFault = 0U;
  1148. }
  1149. void PlsrTestResetSaveCount(void)
  1150. {
  1151. PlsrHostSaveCount = 0UL;
  1152. }
  1153. uint32_t PlsrTestSaveCount(void)
  1154. {
  1155. return PlsrHostSaveCount;
  1156. }
  1157. uint8_t PlsrTestFlashNeedsStartupRecovery(
  1158. uint8_t haveValidRecord,
  1159. uint8_t sectorAHasProgrammedSlot,
  1160. uint32_t sectorAFirstErasedAddress,
  1161. uint8_t sectorBHasProgrammedSlot,
  1162. uint32_t sectorBFirstErasedAddress)
  1163. {
  1164. return PlsrFlashNeedsStartupRecovery(
  1165. haveValidRecord,
  1166. sectorAHasProgrammedSlot,
  1167. sectorAFirstErasedAddress,
  1168. sectorBHasProgrammedSlot,
  1169. sectorBFirstErasedAddress);
  1170. }
  1171. #else
  1172. #include "stm32f4xx_hal.h"
  1173. #include <stddef.h>
  1174. #include <string.h>
  1175. #ifndef PLSR_DEBUG_TIMING
  1176. #define PLSR_DEBUG_TIMING (0U)
  1177. #endif
  1178. #define PLSR_FLASH_SLOT_A_ADDRESS (0x080C0000UL)
  1179. #define PLSR_FLASH_SLOT_B_ADDRESS (0x080E0000UL)
  1180. #define PLSR_FLASH_SECTOR_SIZE (0x00020000UL)
  1181. #define PLSR_FLASH_MAGIC (0x50534C52UL)
  1182. #define PLSR_FLASH_VERSION_V2 (2U)
  1183. #define PLSR_FLASH_VERSION (3U)
  1184. #define PLSR_BACKUP_CONFIG_ADDRESS (BKPSRAM_BASE + 0x0100UL)
  1185. #define PLSR_BACKUP_POSITION_ADDRESS (BKPSRAM_BASE + 0x0200UL)
  1186. #define PLSR_BACKUP_CONFIG_MAGIC (0x50434647UL)
  1187. #define PLSR_BACKUP_POSITION_MAGIC (0x50504F53UL)
  1188. #define PLSR_CONFIG_V2_SIZE (offsetof(PLSR_CONFIG, outputMode))
  1189. #define PLSR_COUNTER_COUNT (2U)
  1190. #define PLSR_COUNTER_NONE (0xFFU)
  1191. #define PLSR_COUNTER_BLOCK_PULSES (65536UL)
  1192. #define PLSR_PLATFORM_FAULT_FREQUENCY (2U)
  1193. #define PLSR_PLATFORM_FAULT_CURVE (3U)
  1194. #define PLSR_TIMER_OC1_MODE_MASK (7UL << TIM_CCMR1_OC1M_Pos)
  1195. #define PLSR_TIMER_PWM1_MODE (6UL << TIM_CCMR1_OC1M_Pos)
  1196. #define PLSR_STRUCTURE_VERIFY_INTERVAL (64U)
  1197. #define PLSR_FREQUENCY_VERIFY_NONE (0U)
  1198. #define PLSR_FREQUENCY_VERIFY_NOW (1U)
  1199. #define PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ (2U)
  1200. #define PLSR_QUEUE_WRITE_GUARD_COUNTS (64UL)
  1201. #define PLSR_FINITE_WRITE_GUARD_COUNTS (128UL)
  1202. #define PLSR_FLASH_ERASE_NONE (0U)
  1203. #define PLSR_FLASH_ERASE_SECTOR_A (1U)
  1204. #define PLSR_FLASH_ERASE_SECTOR_B (2U)
  1205. #define PLSR_FLASH_ERASE_FAILED (3U)
  1206. typedef struct
  1207. {
  1208. TIM_TypeDef *timer;
  1209. GPIO_TypeDef *port;
  1210. uint16_t pin;
  1211. uint8_t pinIndex;
  1212. uint8_t alternate;
  1213. IRQn_Type irq;
  1214. uint32_t timerClockHz;
  1215. } PLSR_TIMER_MAP;
  1216. typedef struct
  1217. {
  1218. GPIO_TypeDef *port;
  1219. uint16_t pin;
  1220. } PLSR_GPIO_MAP;
  1221. typedef struct
  1222. {
  1223. uint32_t prescaler;
  1224. uint32_t period;
  1225. uint32_t compare;
  1226. uint32_t actualFrequencyHz;
  1227. } PLSR_TIMER_SETTING;
  1228. typedef struct
  1229. {
  1230. uint32_t basePrescaler;
  1231. uint32_t pairPrescaler;
  1232. uint32_t period;
  1233. uint32_t compare;
  1234. uint32_t actualFrequencyHz;
  1235. } PLSR_AB_SETTING;
  1236. typedef struct
  1237. {
  1238. uint32_t cr1;
  1239. uint32_t ccmr1;
  1240. uint32_t ccer;
  1241. uint32_t psc;
  1242. uint32_t arr;
  1243. uint32_t ccr1;
  1244. } PLSR_TIMER_SNAPSHOT;
  1245. typedef struct
  1246. {
  1247. uint32_t magic;
  1248. uint16_t version;
  1249. uint16_t payloadSize;
  1250. uint32_t generation;
  1251. } PLSR_FLASH_HEADER;
  1252. typedef struct
  1253. {
  1254. uint8_t config[PLSR_CONFIG_V2_SIZE];
  1255. int32_t position;
  1256. uint8_t positionValid;
  1257. uint8_t wasBusy;
  1258. uint16_t reserved;
  1259. } PLSR_PERSIST_PAYLOAD_V2;
  1260. typedef struct
  1261. {
  1262. uint32_t magic;
  1263. uint16_t version;
  1264. uint16_t payloadSize;
  1265. uint32_t generation;
  1266. PLSR_PERSIST_PAYLOAD payload;
  1267. uint32_t crc32;
  1268. } PLSR_FLASH_RECORD;
  1269. typedef struct
  1270. {
  1271. uint32_t magic;
  1272. uint16_t version;
  1273. uint16_t payloadSize;
  1274. uint32_t generation;
  1275. PLSR_PERSIST_PAYLOAD_V2 payload;
  1276. uint32_t crc32;
  1277. } PLSR_FLASH_RECORD_V2;
  1278. #define PLSR_FLASH_RECORD_STRIDE \
  1279. ((uint32_t)sizeof(PLSR_FLASH_RECORD))
  1280. #define PLSR_FLASH_SLOT_COUNT \
  1281. (PLSR_FLASH_SECTOR_SIZE / PLSR_FLASH_RECORD_STRIDE)
  1282. typedef char PLSR_FLASH_RECORD_SIZE_MUST_BE_228[
  1283. (sizeof(PLSR_FLASH_RECORD) == 228U) ? 1 : -1];
  1284. typedef struct
  1285. {
  1286. const PLSR_FLASH_HEADER *newest;
  1287. uint32_t firstErasedAddress;
  1288. uint8_t newestVersion;
  1289. uint8_t hasProgrammedSlot;
  1290. } PLSR_FLASH_SECTOR_SCAN;
  1291. typedef struct
  1292. {
  1293. uint32_t magic;
  1294. PLSR_CONFIG config;
  1295. uint32_t crc32;
  1296. } PLSR_BACKUP_CONFIG_RECORD;
  1297. typedef struct
  1298. {
  1299. uint32_t magic;
  1300. uint8_t config[PLSR_CONFIG_V2_SIZE];
  1301. uint32_t crc32;
  1302. } PLSR_BACKUP_CONFIG_RECORD_V2;
  1303. typedef struct
  1304. {
  1305. uint32_t magic;
  1306. uint32_t generation;
  1307. int32_t position;
  1308. uint8_t positionValid;
  1309. uint8_t wasBusy;
  1310. uint16_t reserved;
  1311. uint32_t crc32;
  1312. } PLSR_BACKUP_POSITION_RECORD;
  1313. static const PLSR_TIMER_MAP PlsrTimerMap[4] =
  1314. {
  1315. {TIM10, GPIOF, GPIO_PIN_6, 6U, GPIO_AF3_TIM10,
  1316. TIM1_UP_TIM10_IRQn, 168000000UL},
  1317. {TIM13, GPIOF, GPIO_PIN_8, 8U, GPIO_AF9_TIM13,
  1318. TIM8_UP_TIM13_IRQn, 84000000UL},
  1319. {TIM11, GPIOF, GPIO_PIN_7, 7U, GPIO_AF3_TIM11,
  1320. TIM1_TRG_COM_TIM11_IRQn, 168000000UL},
  1321. {TIM14, GPIOF, GPIO_PIN_9, 9U, GPIO_AF9_TIM14,
  1322. TIM8_TRG_COM_TIM14_IRQn, 84000000UL}
  1323. };
  1324. static const PLSR_GPIO_MAP PlsrDirectionMap[4] =
  1325. {
  1326. {GPIOH, GPIO_PIN_9},
  1327. {GPIOH, GPIO_PIN_8},
  1328. {GPIOH, GPIO_PIN_7},
  1329. {GPIOH, GPIO_PIN_6}
  1330. };
  1331. static PLSR_FLASH_RECORD PlsrFlashRecordBuffer;
  1332. static uint32_t PlsrFlashNewestAddress;
  1333. static uint32_t PlsrFlashNewestGeneration;
  1334. static uint32_t PlsrFlashNextErasedAddress[2];
  1335. static uint8_t PlsrFlashJournalInitialized;
  1336. static uint8_t PlsrFlashReserveEraseState;
  1337. static uint32_t PlsrBackupPositionGeneration;
  1338. static uint32_t PlsrTimerActiveFrequencyHz[4];
  1339. static uint32_t PlsrTimerQueuedFrequencyHz[4];
  1340. static PLSR_PLATFORM_TIMER_SETTING PlsrTimerActiveSetting[4];
  1341. static PLSR_PLATFORM_TIMER_SETTING PlsrTimerQueuedSetting[4];
  1342. static uint32_t PlsrTimerQueueGeneration[4];
  1343. static uint8_t PlsrTimerOutputMode[4];
  1344. static uint8_t PlsrTimerDirectionPositive[4];
  1345. static uint8_t PlsrTimerRunning[4];
  1346. static uint8_t PlsrFrequencyVerifyPending[4];
  1347. static uint8_t PlsrFrequencyVerifyPulseCount[4];
  1348. static volatile uint8_t PlsrDeferredPulsePending[4];
  1349. static volatile uint8_t PlsrAbVerifyOwner[4];
  1350. static volatile uint8_t PlsrAbFinalArmJobOwner[4];
  1351. static uint8_t PlsrTimerIrqActive[4];
  1352. static PLSR_AB_SETTING PlsrAbActiveSetting[4];
  1353. static PLSR_AB_SETTING PlsrAbPendingSetting[4];
  1354. static uint8_t PlsrAbFrequencyPending[4];
  1355. static uint8_t PlsrAbLagAxis[4];
  1356. static uint8_t PlsrAbStructureVerified[4];
  1357. static uint8_t PlsrAbCounterSourceAxis[4];
  1358. static uint32_t PlsrAbCounterBoundary[4];
  1359. static volatile uint8_t PlsrAbStopPending[4];
  1360. static volatile uint8_t PlsrAbFastGated[4];
  1361. static TIM_TypeDef * const PlsrCounters[PLSR_COUNTER_COUNT] =
  1362. {
  1363. TIM9, TIM12
  1364. };
  1365. static uint8_t PlsrCounterOwner[PLSR_COUNTER_COUNT];
  1366. static volatile uint64_t PlsrCounterOverflowPulses[PLSR_COUNTER_COUNT];
  1367. static uint8_t PlsrCounterIndexByOutput[4];
  1368. static uint64_t PlsrObservedPulseBase[4];
  1369. static uint64_t PlsrObservedPulsePublished[4];
  1370. static volatile uint8_t PlsrFiniteActive[4];
  1371. static volatile uint8_t PlsrFiniteCompletionPending[4];
  1372. static volatile uint8_t PlsrFiniteFrequencyPending[4];
  1373. static volatile uint8_t PlsrFiniteRetargetPending[4];
  1374. static volatile uint8_t PlsrFiniteTailStopPending[4];
  1375. static uint32_t PlsrFiniteRetargetDrainPulses[4];
  1376. static uint32_t PlsrFiniteTargetPulses[4];
  1377. static uint32_t PlsrFiniteRemainingPulses[4];
  1378. static uint8_t PlsrFiniteCounterPreload[4];
  1379. static volatile uint8_t PlsrFiniteStreamActive[4];
  1380. static volatile uint8_t PlsrFiniteStreamNextValid[4];
  1381. static volatile uint8_t PlsrFiniteStreamNextStartsSegment[4];
  1382. static volatile uint8_t PlsrFiniteStreamSourceDone[4];
  1383. static volatile uint8_t PlsrFiniteStreamSourceFault[4];
  1384. static uint32_t PlsrFiniteStreamNextPulses[4];
  1385. static PLSR_PLATFORM_TIMER_SETTING PlsrFiniteStreamNextSetting[4];
  1386. static PLSR_PLATFORM_FINITE_STEP *PlsrFiniteSteps[4];
  1387. static volatile uint16_t PlsrFiniteStepCount[4];
  1388. static volatile uint16_t PlsrFiniteStepIndex[4];
  1389. static volatile uint16_t PlsrFiniteBoundaryReadIndex[4];
  1390. static volatile uint16_t PlsrFiniteCompletedStepCount[4];
  1391. static volatile uint16_t PlsrPlatformFaultPending;
  1392. static void PlsrHandleTimerIrq(uint8_t pulseOutput);
  1393. static void PlsrCounterSuspend(uint8_t pulseOutput);
  1394. static void PlsrAbFastGate(uint8_t pulseOutput);
  1395. static void PlsrFiniteCounterIrq(uint8_t pulseOutput,
  1396. TIM_TypeDef *counter);
  1397. static void PlsrFinitePrepareNextStepIrq(uint8_t pulseOutput,
  1398. TIM_TypeDef *counter);
  1399. static void PlsrFiniteArmNextStepPrepare(uint8_t pulseOutput,
  1400. TIM_TypeDef *counter,
  1401. uint32_t blockPulses);
  1402. static void PlsrFiniteRetargetAtFallingEdge(uint8_t pulseOutput);
  1403. static void PlsrFiniteStopAtFallingEdge(uint8_t pulseOutput);
  1404. static uint8_t PlsrFinalArmJobOutput(uint8_t pulseOutput)
  1405. {
  1406. return (pulseOutput == 0U) ? 2U : 0U;
  1407. }
  1408. #if PLSR_DEBUG_TIMING
  1409. volatile uint32_t PlsrIrqCount[4];
  1410. volatile uint32_t PlsrIrqLastCycles[4];
  1411. volatile uint32_t PlsrIrqMaxCycles[4];
  1412. volatile uint32_t PlsrFinalArmQueueCount[4];
  1413. volatile uint32_t PlsrFinalArmJobLastCycles[4];
  1414. volatile uint32_t PlsrFinalArmJobMaxCycles[4];
  1415. volatile uint32_t PlsrFinalArmQueueToStopLastCycles[4];
  1416. volatile uint32_t PlsrFinalArmQueueToStopMaxCycles[4];
  1417. volatile uint32_t PlsrFiniteBlockIrqCount[4];
  1418. volatile uint32_t PlsrFiniteBlockIrqMaxCycles[4];
  1419. volatile uint32_t PlsrFiniteFinalIrqLastCycles[4];
  1420. volatile uint32_t PlsrFiniteFinalIrqMaxCycles[4];
  1421. volatile uint32_t PlsrAbReloadCounterBefore[4];
  1422. volatile uint32_t PlsrAbReloadCounterArmed[4];
  1423. volatile uint32_t PlsrAbReloadCounterStarted[4];
  1424. volatile uint32_t PlsrAbReloadCount[4];
  1425. static volatile uint32_t PlsrFinalArmQueuedAt[4];
  1426. static volatile uint8_t PlsrFinalArmQueueTimingPending[4];
  1427. #endif
  1428. static uint32_t PlsrCrc32(const void *data, uint32_t length)
  1429. {
  1430. const uint8_t *bytes = (const uint8_t *)data;
  1431. uint32_t crc = 0xFFFFFFFFUL;
  1432. uint32_t index;
  1433. uint8_t bit;
  1434. for (index = 0UL; index < length; index++)
  1435. {
  1436. crc ^= bytes[index];
  1437. for (bit = 0U; bit < 8U; bit++)
  1438. {
  1439. crc = ((crc & 1UL) != 0UL) ? ((crc >> 1U) ^ 0xEDB88320UL)
  1440. : (crc >> 1U);
  1441. }
  1442. }
  1443. return ~crc;
  1444. }
  1445. static uint8_t PlsrGenerationIsNewer(uint32_t first, uint32_t second)
  1446. {
  1447. return ((int32_t)(first - second) > 0) ? 1U : 0U;
  1448. }
  1449. static uint32_t PlsrFlashRecordCrc(const void *record,
  1450. uint32_t payloadSize)
  1451. {
  1452. const PLSR_FLASH_HEADER *header = (const PLSR_FLASH_HEADER *)record;
  1453. const uint8_t *start = (const uint8_t *)&header->version;
  1454. uint32_t length = (uint32_t)(sizeof(header->version)
  1455. + sizeof(header->payloadSize)
  1456. + sizeof(header->generation))
  1457. + payloadSize;
  1458. return PlsrCrc32(start, length);
  1459. }
  1460. static uint8_t PlsrFlashRecordVersion(const void *address)
  1461. {
  1462. const PLSR_FLASH_HEADER *header = (const PLSR_FLASH_HEADER *)address;
  1463. if (header->magic != PLSR_FLASH_MAGIC)
  1464. {
  1465. return 0U;
  1466. }
  1467. if ((header->version == PLSR_FLASH_VERSION)
  1468. && (header->payloadSize == sizeof(PLSR_PERSIST_PAYLOAD)))
  1469. {
  1470. const PLSR_FLASH_RECORD *record =
  1471. (const PLSR_FLASH_RECORD *)address;
  1472. return (record->crc32
  1473. == PlsrFlashRecordCrc(record, sizeof(record->payload)))
  1474. ? PLSR_FLASH_VERSION : 0U;
  1475. }
  1476. if ((header->version == PLSR_FLASH_VERSION_V2)
  1477. && (header->payloadSize == sizeof(PLSR_PERSIST_PAYLOAD_V2)))
  1478. {
  1479. const PLSR_FLASH_RECORD_V2 *record =
  1480. (const PLSR_FLASH_RECORD_V2 *)address;
  1481. return (record->crc32
  1482. == PlsrFlashRecordCrc(record, sizeof(record->payload)))
  1483. ? PLSR_FLASH_VERSION_V2 : 0U;
  1484. }
  1485. return 0U;
  1486. }
  1487. static uint8_t PlsrFlashSlotIsErased(uint32_t address)
  1488. {
  1489. const uint32_t *words = (const uint32_t *)address;
  1490. uint32_t index;
  1491. for (index = 0UL;
  1492. index < (PLSR_FLASH_RECORD_STRIDE / sizeof(uint32_t));
  1493. index++)
  1494. {
  1495. if (words[index] != 0xFFFFFFFFUL)
  1496. {
  1497. return 0U;
  1498. }
  1499. }
  1500. return 1U;
  1501. }
  1502. static uint8_t PlsrFlashSectorIsErased(uint32_t address)
  1503. {
  1504. const uint32_t *words = (const uint32_t *)address;
  1505. uint32_t index;
  1506. for (index = 0UL;
  1507. index < (PLSR_FLASH_SECTOR_SIZE / sizeof(uint32_t)); index++)
  1508. {
  1509. if (words[index] != 0xFFFFFFFFUL)
  1510. {
  1511. return 0U;
  1512. }
  1513. }
  1514. return 1U;
  1515. }
  1516. static void PlsrFlashScanSector(uint32_t sectorAddress,
  1517. PLSR_FLASH_SECTOR_SCAN *scan)
  1518. {
  1519. uint32_t index;
  1520. (void)memset(scan, 0, sizeof(*scan));
  1521. for (index = 0UL; index < PLSR_FLASH_SLOT_COUNT; index++)
  1522. {
  1523. uint32_t slotAddress = sectorAddress
  1524. + index * PLSR_FLASH_RECORD_STRIDE;
  1525. const PLSR_FLASH_HEADER *header =
  1526. (const PLSR_FLASH_HEADER *)slotAddress;
  1527. uint8_t version = PlsrFlashRecordVersion(header);
  1528. uint8_t erased = PlsrFlashSlotIsErased(slotAddress);
  1529. if ((version != 0U)
  1530. && ((scan->newest == NULL)
  1531. || (PlsrGenerationIsNewer(header->generation,
  1532. scan->newest->generation) != 0U)))
  1533. {
  1534. scan->newest = header;
  1535. scan->newestVersion = version;
  1536. }
  1537. if ((scan->firstErasedAddress == 0UL) && (erased != 0U))
  1538. {
  1539. scan->firstErasedAddress = slotAddress;
  1540. }
  1541. if (erased == 0U)
  1542. {
  1543. scan->hasProgrammedSlot = 1U;
  1544. }
  1545. }
  1546. for (index = PLSR_FLASH_SLOT_COUNT * PLSR_FLASH_RECORD_STRIDE;
  1547. index < PLSR_FLASH_SECTOR_SIZE; index += sizeof(uint32_t))
  1548. {
  1549. if (*(const uint32_t *)(sectorAddress + index) != 0xFFFFFFFFUL)
  1550. {
  1551. scan->hasProgrammedSlot = 1U;
  1552. }
  1553. }
  1554. }
  1555. static const PLSR_FLASH_HEADER *PlsrFlashSelectNewest(
  1556. const PLSR_FLASH_SECTOR_SCAN *scanA,
  1557. const PLSR_FLASH_SECTOR_SCAN *scanB,
  1558. uint8_t *version,
  1559. uint32_t *sectorAddress)
  1560. {
  1561. const PLSR_FLASH_SECTOR_SCAN *selectedScan;
  1562. if (scanA->newest == NULL)
  1563. {
  1564. selectedScan = (scanB->newest != NULL) ? scanB : NULL;
  1565. }
  1566. else if ((scanB->newest != NULL)
  1567. && (PlsrGenerationIsNewer(scanB->newest->generation,
  1568. scanA->newest->generation) != 0U))
  1569. {
  1570. selectedScan = scanB;
  1571. }
  1572. else
  1573. {
  1574. selectedScan = scanA;
  1575. }
  1576. if (selectedScan == NULL)
  1577. {
  1578. *version = 0U;
  1579. *sectorAddress = 0UL;
  1580. return NULL;
  1581. }
  1582. *version = selectedScan->newestVersion;
  1583. *sectorAddress = (selectedScan == scanA)
  1584. ? PLSR_FLASH_SLOT_A_ADDRESS
  1585. : PLSR_FLASH_SLOT_B_ADDRESS;
  1586. return selectedScan->newest;
  1587. }
  1588. static const PLSR_FLASH_HEADER *PlsrFlashInitializeJournal(
  1589. PLSR_FLASH_SECTOR_SCAN *scanA,
  1590. PLSR_FLASH_SECTOR_SCAN *scanB,
  1591. uint8_t *version,
  1592. uint32_t *sectorAddress)
  1593. {
  1594. const PLSR_FLASH_HEADER *newest;
  1595. PlsrFlashScanSector(PLSR_FLASH_SLOT_A_ADDRESS, scanA);
  1596. PlsrFlashScanSector(PLSR_FLASH_SLOT_B_ADDRESS, scanB);
  1597. newest = PlsrFlashSelectNewest(scanA, scanB, version, sectorAddress);
  1598. PlsrFlashNextErasedAddress[0] = scanA->firstErasedAddress;
  1599. PlsrFlashNextErasedAddress[1] = scanB->firstErasedAddress;
  1600. PlsrFlashNewestAddress = (uint32_t)newest;
  1601. PlsrFlashNewestGeneration = (newest != NULL)
  1602. ? newest->generation : 0UL;
  1603. PlsrFlashJournalInitialized = 1U;
  1604. return newest;
  1605. }
  1606. static uint8_t PlsrFlashSectorIndex(uint32_t address)
  1607. {
  1608. return (address >= PLSR_FLASH_SLOT_B_ADDRESS) ? 1U : 0U;
  1609. }
  1610. static uint8_t PlsrFlashAddressIsJournalSlot(uint8_t sectorIndex,
  1611. uint32_t address)
  1612. {
  1613. uint32_t sectorAddress = (sectorIndex == 0U)
  1614. ? PLSR_FLASH_SLOT_A_ADDRESS
  1615. : PLSR_FLASH_SLOT_B_ADDRESS;
  1616. uint32_t offset;
  1617. if ((address < sectorAddress)
  1618. || (address >= sectorAddress + PLSR_FLASH_SECTOR_SIZE))
  1619. {
  1620. return 0U;
  1621. }
  1622. offset = address - sectorAddress;
  1623. return ((offset % PLSR_FLASH_RECORD_STRIDE) == 0UL)
  1624. && ((offset / PLSR_FLASH_RECORD_STRIDE)
  1625. < PLSR_FLASH_SLOT_COUNT) ? 1U : 0U;
  1626. }
  1627. static uint32_t PlsrFlashFindErasedAfter(uint8_t sectorIndex,
  1628. uint32_t address)
  1629. {
  1630. uint32_t sectorAddress = (sectorIndex == 0U)
  1631. ? PLSR_FLASH_SLOT_A_ADDRESS
  1632. : PLSR_FLASH_SLOT_B_ADDRESS;
  1633. uint32_t firstIndex = ((address - sectorAddress)
  1634. / PLSR_FLASH_RECORD_STRIDE) + 1UL;
  1635. uint32_t index;
  1636. for (index = firstIndex; index < PLSR_FLASH_SLOT_COUNT; index++)
  1637. {
  1638. uint32_t slotAddress = sectorAddress
  1639. + index * PLSR_FLASH_RECORD_STRIDE;
  1640. if (PlsrFlashSlotIsErased(slotAddress) != 0U)
  1641. {
  1642. return slotAddress;
  1643. }
  1644. }
  1645. return 0UL;
  1646. }
  1647. static uint8_t PlsrFlashEraseReserve(uint8_t sectorIndex)
  1648. {
  1649. FLASH_EraseInitTypeDef erase;
  1650. uint32_t sectorError;
  1651. HAL_StatusTypeDef status;
  1652. if (HAL_FLASH_Unlock() != HAL_OK)
  1653. {
  1654. (void)HAL_FLASH_Lock();
  1655. return 0U;
  1656. }
  1657. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
  1658. | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  1659. | FLASH_FLAG_PGSERR);
  1660. erase.TypeErase = FLASH_TYPEERASE_SECTORS;
  1661. erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  1662. erase.Sector = (sectorIndex == 0U) ? FLASH_SECTOR_10 : FLASH_SECTOR_11;
  1663. erase.NbSectors = 1U;
  1664. status = HAL_FLASHEx_Erase(&erase, &sectorError);
  1665. if ((status == HAL_OK)
  1666. && (PlsrFlashSectorIsErased((sectorIndex == 0U)
  1667. ? PLSR_FLASH_SLOT_A_ADDRESS
  1668. : PLSR_FLASH_SLOT_B_ADDRESS) == 0U))
  1669. {
  1670. status = HAL_ERROR;
  1671. }
  1672. if (HAL_FLASH_Lock() != HAL_OK)
  1673. {
  1674. (void)HAL_FLASH_Lock();
  1675. status = HAL_ERROR;
  1676. }
  1677. return (status == HAL_OK) ? 1U : 0U;
  1678. }
  1679. static uint8_t PlsrBackupConfigVersion(const void *address)
  1680. {
  1681. const PLSR_BACKUP_CONFIG_RECORD *record =
  1682. (const PLSR_BACKUP_CONFIG_RECORD *)address;
  1683. if (record->magic != PLSR_BACKUP_CONFIG_MAGIC)
  1684. {
  1685. return 0U;
  1686. }
  1687. if (record->crc32 == PlsrCrc32(&record->config, sizeof(record->config)))
  1688. {
  1689. return PLSR_FLASH_VERSION;
  1690. }
  1691. {
  1692. const PLSR_BACKUP_CONFIG_RECORD_V2 *oldRecord =
  1693. (const PLSR_BACKUP_CONFIG_RECORD_V2 *)address;
  1694. return (oldRecord->crc32
  1695. == PlsrCrc32(oldRecord->config, sizeof(oldRecord->config)))
  1696. ? PLSR_FLASH_VERSION_V2 : 0U;
  1697. }
  1698. }
  1699. static void PlsrLoadV2Payload(PLSR_PERSIST_PAYLOAD *destination,
  1700. const PLSR_PERSIST_PAYLOAD_V2 *source)
  1701. {
  1702. (void)memset(destination, 0, sizeof(*destination));
  1703. (void)memcpy(&destination->config, source->config,
  1704. sizeof(source->config));
  1705. destination->config.outputMode = PLSR_OUTPUT_PULSE_DIR;
  1706. destination->position = source->position;
  1707. destination->positionValid = source->positionValid;
  1708. destination->wasBusy = source->wasBusy;
  1709. }
  1710. static uint8_t PlsrBackupPositionIsValid(
  1711. const PLSR_BACKUP_POSITION_RECORD *record)
  1712. {
  1713. uint32_t crc = PlsrCrc32(&record->generation,
  1714. sizeof(record->generation)
  1715. + sizeof(record->position)
  1716. + sizeof(record->positionValid)
  1717. + sizeof(record->wasBusy)
  1718. + sizeof(record->reserved));
  1719. return ((record->magic == PLSR_BACKUP_POSITION_MAGIC)
  1720. && (record->crc32 == crc)) ? 1U : 0U;
  1721. }
  1722. static const PLSR_BACKUP_POSITION_RECORD *PlsrNewestBackupPosition(void)
  1723. {
  1724. const PLSR_BACKUP_POSITION_RECORD *slots =
  1725. (const PLSR_BACKUP_POSITION_RECORD *)PLSR_BACKUP_POSITION_ADDRESS;
  1726. uint8_t validA = PlsrBackupPositionIsValid(&slots[0]);
  1727. uint8_t validB = PlsrBackupPositionIsValid(&slots[1]);
  1728. if ((validA == 0U) && (validB == 0U))
  1729. {
  1730. return NULL;
  1731. }
  1732. if (validA == 0U)
  1733. {
  1734. return &slots[1];
  1735. }
  1736. if (validB == 0U)
  1737. {
  1738. return &slots[0];
  1739. }
  1740. return (PlsrGenerationIsNewer(slots[1].generation,
  1741. slots[0].generation) != 0U)
  1742. ? &slots[1] : &slots[0];
  1743. }
  1744. static void PlsrTimerStop(TIM_TypeDef *timer)
  1745. {
  1746. timer->DIER &= ~(TIM_DIER_UIE | TIM_DIER_CC1IE);
  1747. timer->CR1 &= ~TIM_CR1_CEN;
  1748. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  1749. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  1750. }
  1751. static void PlsrTimerInitialize(TIM_TypeDef *timer)
  1752. {
  1753. timer->CR1 = TIM_CR1_ARPE | TIM_CR1_URS;
  1754. timer->CR2 = 0UL;
  1755. timer->SMCR = 0UL;
  1756. timer->DIER = 0UL;
  1757. timer->CCMR1 = TIM_CCMR1_OC1PE | (6UL << TIM_CCMR1_OC1M_Pos);
  1758. timer->CCER = 0UL;
  1759. timer->PSC = 0UL;
  1760. timer->ARR = 999UL;
  1761. timer->CCR1 = 500UL;
  1762. timer->CNT = 0UL;
  1763. timer->EGR = TIM_EGR_UG;
  1764. timer->SR = 0UL;
  1765. }
  1766. static void PlsrPulsePinHoldIdle(uint8_t pulseOutput)
  1767. {
  1768. const PLSR_TIMER_MAP *map = &PlsrTimerMap[pulseOutput];
  1769. GPIO_InitTypeDef gpio;
  1770. HAL_GPIO_WritePin(map->port, map->pin, GPIO_PIN_SET);
  1771. gpio.Pin = map->pin;
  1772. gpio.Mode = GPIO_MODE_OUTPUT_PP;
  1773. gpio.Pull = GPIO_NOPULL;
  1774. gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1775. gpio.Alternate = 0U;
  1776. HAL_GPIO_Init(map->port, &gpio);
  1777. }
  1778. static void PlsrPulsePinCaptureIdle(uint8_t pulseOutput)
  1779. {
  1780. const PLSR_TIMER_MAP *map = &PlsrTimerMap[pulseOutput];
  1781. uint32_t shift = (uint32_t)map->pinIndex * 2UL;
  1782. uint32_t mode = map->port->MODER;
  1783. /* The update IRQ occurs while PWM is high; switch to GPIO high first. */
  1784. map->port->BSRR = map->pin;
  1785. mode &= ~(3UL << shift);
  1786. mode |= 1UL << shift;
  1787. map->port->MODER = mode;
  1788. __DSB();
  1789. }
  1790. static void PlsrPulsePinRelease(uint8_t pulseOutput)
  1791. {
  1792. const PLSR_TIMER_MAP *map = &PlsrTimerMap[pulseOutput];
  1793. GPIO_InitTypeDef gpio;
  1794. gpio.Pin = map->pin;
  1795. gpio.Mode = GPIO_MODE_AF_PP;
  1796. gpio.Pull = GPIO_NOPULL;
  1797. gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1798. gpio.Alternate = map->alternate;
  1799. HAL_GPIO_Init(map->port, &gpio);
  1800. __DSB();
  1801. }
  1802. static uint8_t PlsrTimerCalculate(uint8_t pulseOutput,
  1803. uint32_t frequencyHz,
  1804. PLSR_TIMER_SETTING *setting)
  1805. {
  1806. const PLSR_TIMER_MAP *map;
  1807. uint32_t prescalerDivider;
  1808. uint32_t denominator;
  1809. uint32_t periodCounts;
  1810. if ((pulseOutput > 3U) || (frequencyHz == 0UL)
  1811. || (frequencyHz > PLSR_FREQUENCY_MAX_HZ)
  1812. || (setting == NULL))
  1813. {
  1814. return 0U;
  1815. }
  1816. map = &PlsrTimerMap[pulseOutput];
  1817. prescalerDivider = (((map->timerClockHz - 1UL) / frequencyHz) >> 16U)
  1818. + 1UL;
  1819. if (prescalerDivider > 65536UL)
  1820. {
  1821. return 0U;
  1822. }
  1823. denominator = prescalerDivider * frequencyHz;
  1824. periodCounts = (map->timerClockHz + denominator / 2UL) / denominator;
  1825. if (periodCounts < 2UL)
  1826. {
  1827. periodCounts = 2UL;
  1828. }
  1829. if (periodCounts > 65536UL)
  1830. {
  1831. periodCounts = 65536UL;
  1832. }
  1833. setting->prescaler = prescalerDivider - 1UL;
  1834. setting->period = periodCounts - 1UL;
  1835. setting->compare = periodCounts / 2UL;
  1836. denominator = prescalerDivider * periodCounts;
  1837. setting->actualFrequencyHz =
  1838. (map->timerClockHz + denominator / 2UL) / denominator;
  1839. return 1U;
  1840. }
  1841. static void PlsrTimerWriteSetting(TIM_TypeDef *timer,
  1842. const PLSR_TIMER_SETTING *setting)
  1843. {
  1844. timer->PSC = setting->prescaler;
  1845. timer->ARR = setting->period;
  1846. timer->CCR1 = setting->compare;
  1847. }
  1848. static void PlsrTimerSnapshot(TIM_TypeDef *timer,
  1849. PLSR_TIMER_SNAPSHOT *snapshot)
  1850. {
  1851. snapshot->cr1 = timer->CR1;
  1852. snapshot->ccmr1 = timer->CCMR1;
  1853. snapshot->ccer = timer->CCER;
  1854. snapshot->psc = timer->PSC;
  1855. snapshot->arr = timer->ARR;
  1856. snapshot->ccr1 = timer->CCR1;
  1857. }
  1858. static uint8_t PlsrAbCalculate(uint8_t pulseOutput,
  1859. uint32_t frequencyHz,
  1860. PLSR_AB_SETTING *setting)
  1861. {
  1862. const PLSR_TIMER_MAP *baseMap;
  1863. const PLSR_TIMER_MAP *pairMap;
  1864. uint64_t ratio;
  1865. uint64_t pairDivider;
  1866. uint64_t baseDivider;
  1867. uint64_t periodCounts;
  1868. if (((pulseOutput != 0U) && (pulseOutput != 2U))
  1869. || (frequencyHz == 0UL)
  1870. || (frequencyHz > PLSR_FREQUENCY_MAX_HZ)
  1871. || (setting == NULL))
  1872. {
  1873. return 0U;
  1874. }
  1875. baseMap = &PlsrTimerMap[pulseOutput];
  1876. pairMap = &PlsrTimerMap[pulseOutput + 1U];
  1877. if ((pairMap->timerClockHz == 0UL)
  1878. || ((baseMap->timerClockHz % pairMap->timerClockHz) != 0UL))
  1879. {
  1880. return 0U;
  1881. }
  1882. ratio = baseMap->timerClockHz / pairMap->timerClockHz;
  1883. pairDivider = ((uint64_t)pairMap->timerClockHz
  1884. + (uint64_t)frequencyHz * 65536UL - 1UL)
  1885. / ((uint64_t)frequencyHz * 65536UL);
  1886. if (pairDivider == 0UL)
  1887. {
  1888. pairDivider = 1UL;
  1889. }
  1890. baseDivider = pairDivider * ratio;
  1891. if ((pairDivider > 65536UL) || (baseDivider > 65536UL))
  1892. {
  1893. return 0U;
  1894. }
  1895. periodCounts = ((uint64_t)pairMap->timerClockHz
  1896. + ((uint64_t)frequencyHz * pairDivider) / 2UL)
  1897. / ((uint64_t)frequencyHz * pairDivider);
  1898. if ((periodCounts < 4UL) || (periodCounts > 65536UL))
  1899. {
  1900. return 0U;
  1901. }
  1902. setting->basePrescaler = (uint32_t)(baseDivider - 1UL);
  1903. setting->pairPrescaler = (uint32_t)(pairDivider - 1UL);
  1904. setting->period = (uint32_t)(periodCounts - 1UL);
  1905. setting->compare = (uint32_t)(periodCounts / 2UL);
  1906. setting->actualFrequencyHz =
  1907. (uint32_t)(((uint64_t)pairMap->timerClockHz
  1908. + (pairDivider * periodCounts) / 2UL)
  1909. / (pairDivider * periodCounts));
  1910. return 1U;
  1911. }
  1912. static void PlsrAbHoldPairIdle(uint8_t pulseOutput)
  1913. {
  1914. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1915. GPIO_TypeDef *port = PlsrTimerMap[pulseOutput].port;
  1916. uint32_t firstShift = (uint32_t)PlsrTimerMap[pulseOutput].pinIndex * 2UL;
  1917. uint32_t secondShift = (uint32_t)PlsrTimerMap[pairOutput].pinIndex * 2UL;
  1918. uint32_t mode = port->MODER;
  1919. port->BSRR = (uint32_t)PlsrTimerMap[pulseOutput].pin
  1920. | (uint32_t)PlsrTimerMap[pairOutput].pin;
  1921. mode &= ~((3UL << firstShift) | (3UL << secondShift));
  1922. mode |= (1UL << firstShift) | (1UL << secondShift);
  1923. port->MODER = mode;
  1924. __DSB();
  1925. }
  1926. static void PlsrAbReleasePair(uint8_t pulseOutput)
  1927. {
  1928. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1929. GPIO_TypeDef *port = PlsrTimerMap[pulseOutput].port;
  1930. uint32_t firstAfrIndex =
  1931. (uint32_t)PlsrTimerMap[pulseOutput].pinIndex >> 3U;
  1932. uint32_t secondAfrIndex =
  1933. (uint32_t)PlsrTimerMap[pairOutput].pinIndex >> 3U;
  1934. uint32_t firstAfrShift =
  1935. ((uint32_t)PlsrTimerMap[pulseOutput].pinIndex & 7UL) * 4UL;
  1936. uint32_t secondAfrShift =
  1937. ((uint32_t)PlsrTimerMap[pairOutput].pinIndex & 7UL) * 4UL;
  1938. uint32_t firstShift = (uint32_t)PlsrTimerMap[pulseOutput].pinIndex * 2UL;
  1939. uint32_t secondShift = (uint32_t)PlsrTimerMap[pairOutput].pinIndex * 2UL;
  1940. uint32_t alternate;
  1941. uint32_t mode = port->MODER;
  1942. alternate = port->AFR[firstAfrIndex];
  1943. alternate &= ~(0xFUL << firstAfrShift);
  1944. alternate |= (uint32_t)PlsrTimerMap[pulseOutput].alternate
  1945. << firstAfrShift;
  1946. port->AFR[firstAfrIndex] = alternate;
  1947. alternate = port->AFR[secondAfrIndex];
  1948. alternate &= ~(0xFUL << secondAfrShift);
  1949. alternate |= (uint32_t)PlsrTimerMap[pairOutput].alternate
  1950. << secondAfrShift;
  1951. port->AFR[secondAfrIndex] = alternate;
  1952. mode &= ~((3UL << firstShift) | (3UL << secondShift));
  1953. mode |= (2UL << firstShift) | (2UL << secondShift);
  1954. port->MODER = mode;
  1955. __DSB();
  1956. }
  1957. static uint8_t PlsrAbStructureIsRunnable(
  1958. uint8_t pulseOutput,
  1959. const PLSR_TIMER_SNAPSHOT *base,
  1960. const PLSR_TIMER_SNAPSHOT *pair)
  1961. {
  1962. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1963. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  1964. return ((((base->cr1 & TIM_CR1_CEN) != 0UL)
  1965. && ((pair->cr1 & TIM_CR1_CEN) != 0UL)
  1966. && ((base->ccer & TIM_CCER_CC1E) != 0UL)
  1967. && ((pair->ccer & TIM_CCER_CC1E) != 0UL)
  1968. && ((base->ccer & TIM_CCER_CC1P) != 0UL)
  1969. && ((pair->ccer & TIM_CCER_CC1P) != 0UL)
  1970. && ((base->ccmr1 & PLSR_TIMER_OC1_MODE_MASK)
  1971. == PLSR_TIMER_PWM1_MODE)
  1972. && ((pair->ccmr1 & PLSR_TIMER_OC1_MODE_MASK)
  1973. == PLSR_TIMER_PWM1_MODE)
  1974. && (base->arr == pair->arr)
  1975. && (base->ccr1 == pair->ccr1)
  1976. && (base->ccr1 == ((base->arr + 1UL) / 2UL))
  1977. && ((base->psc + 1UL) == 2UL * (pair->psc + 1UL))
  1978. && ((lagOutput == pulseOutput) || (lagOutput == pairOutput)))
  1979. ? 1U : 0U);
  1980. }
  1981. static uint8_t PlsrAbTimersAreRunnable(uint8_t pulseOutput,
  1982. TIM_TypeDef *baseTimer,
  1983. TIM_TypeDef *pairTimer)
  1984. {
  1985. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1986. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  1987. uint32_t baseCr1 = baseTimer->CR1;
  1988. uint32_t pairCr1 = pairTimer->CR1;
  1989. uint32_t baseCcer = baseTimer->CCER;
  1990. uint32_t pairCcer = pairTimer->CCER;
  1991. uint32_t baseCcmr1 = baseTimer->CCMR1;
  1992. uint32_t pairCcmr1 = pairTimer->CCMR1;
  1993. uint32_t basePsc = baseTimer->PSC;
  1994. uint32_t pairPsc = pairTimer->PSC;
  1995. uint32_t baseCcr1 = baseTimer->CCR1;
  1996. uint32_t pairCcr1 = pairTimer->CCR1;
  1997. uint32_t baseArr = baseTimer->ARR;
  1998. uint32_t pairArr = pairTimer->ARR;
  1999. return (((((baseCr1 & TIM_CR1_CEN) != 0UL)
  2000. && ((pairCr1 & TIM_CR1_CEN) != 0UL)
  2001. && ((baseCcer & (TIM_CCER_CC1E | TIM_CCER_CC1P))
  2002. == (TIM_CCER_CC1E | TIM_CCER_CC1P))
  2003. && ((pairCcer & (TIM_CCER_CC1E | TIM_CCER_CC1P))
  2004. == (TIM_CCER_CC1E | TIM_CCER_CC1P))
  2005. && ((baseCcmr1 & PLSR_TIMER_OC1_MODE_MASK)
  2006. == PLSR_TIMER_PWM1_MODE)
  2007. && ((pairCcmr1 & PLSR_TIMER_OC1_MODE_MASK)
  2008. == PLSR_TIMER_PWM1_MODE)
  2009. && (baseArr == pairArr)
  2010. && (baseCcr1 == pairCcr1)
  2011. && (baseCcr1 == ((baseArr + 1UL) / 2UL))
  2012. && ((basePsc + 1UL) == 2UL * (pairPsc + 1UL))
  2013. && ((lagOutput == pulseOutput)
  2014. || (lagOutput == pairOutput))) ? 1U : 0U));
  2015. }
  2016. static uint8_t PlsrAbStopBoundaryIsReachable(
  2017. uint8_t pulseOutput,
  2018. TIM_TypeDef *baseTimer,
  2019. TIM_TypeDef *pairTimer)
  2020. {
  2021. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2022. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  2023. uint32_t baseCr1 = baseTimer->CR1;
  2024. uint32_t pairCr1 = pairTimer->CR1;
  2025. uint32_t baseCcer = baseTimer->CCER;
  2026. uint32_t pairCcer = pairTimer->CCER;
  2027. uint32_t baseCcr1 = baseTimer->CCR1;
  2028. uint32_t pairCcr1 = pairTimer->CCR1;
  2029. uint32_t baseArr = baseTimer->ARR;
  2030. uint32_t pairArr = pairTimer->ARR;
  2031. return (((((baseCr1 & TIM_CR1_CEN) != 0UL)
  2032. && ((pairCr1 & TIM_CR1_CEN) != 0UL)
  2033. && ((baseCcer & TIM_CCER_CC1E) != 0UL)
  2034. && ((pairCcer & TIM_CCER_CC1E) != 0UL)
  2035. && (baseCcr1 <= baseArr)
  2036. && (pairCcr1 <= pairArr)
  2037. && ((lagOutput == pulseOutput)
  2038. || (lagOutput == pairOutput))) ? 1U : 0U));
  2039. }
  2040. static void PlsrAbFastGate(uint8_t pulseOutput)
  2041. {
  2042. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2043. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2044. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2045. PlsrAbStructureVerified[pulseOutput] = 0U;
  2046. baseTimer->CR1 &= ~TIM_CR1_CEN;
  2047. pairTimer->CR1 &= ~TIM_CR1_CEN;
  2048. PlsrCounterSuspend(pulseOutput);
  2049. __DMB();
  2050. }
  2051. #if defined(__ICCARM__)
  2052. #pragma inline=never
  2053. #endif
  2054. static void PlsrAbEnableTimerPair(TIM_TypeDef *firstTimer,
  2055. uint32_t firstCr1,
  2056. TIM_TypeDef *secondTimer,
  2057. uint32_t secondCr1)
  2058. {
  2059. firstTimer->CR1 = firstCr1;
  2060. secondTimer->CR1 = secondCr1;
  2061. }
  2062. static uint8_t PlsrAbCanFastGateAtZero(uint8_t pulseOutput)
  2063. {
  2064. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2065. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2066. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2067. uint32_t baseCr1 = baseTimer->CR1;
  2068. uint32_t pairCr1 = pairTimer->CR1;
  2069. uint32_t baseCcer = baseTimer->CCER;
  2070. uint32_t pairCcer = pairTimer->CCER;
  2071. uint32_t baseCcr = baseTimer->CCR1;
  2072. uint32_t pairCcr = pairTimer->CCR1;
  2073. uint32_t baseCnt = baseTimer->CNT;
  2074. uint32_t pairCnt = pairTimer->CNT;
  2075. return ((((baseCr1 & TIM_CR1_CEN) != 0UL)
  2076. && ((pairCr1 & TIM_CR1_CEN) != 0UL)
  2077. && ((baseCcer & TIM_CCER_CC1E) != 0UL)
  2078. && ((pairCcer & TIM_CCER_CC1E) != 0UL)
  2079. && (baseCnt >= baseCcr)
  2080. && (pairCnt >= pairCcr)) ? 1U : 0U);
  2081. }
  2082. static uint8_t PlsrCounterIndex(uint8_t pulseOutput, uint8_t outputMode)
  2083. {
  2084. return (outputMode == PLSR_OUTPUT_AB)
  2085. ? (uint8_t)(pulseOutput >> 1U)
  2086. : (uint8_t)(pulseOutput & 1U);
  2087. }
  2088. static uint64_t PlsrCounterCurrentRaw(uint8_t pulseOutput)
  2089. {
  2090. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  2091. TIM_TypeDef *counter;
  2092. uint64_t overflowBefore;
  2093. uint64_t overflowAfter;
  2094. uint32_t statusBefore;
  2095. uint32_t statusAfter;
  2096. uint32_t count;
  2097. if (index >= PLSR_COUNTER_COUNT)
  2098. {
  2099. return 0UL;
  2100. }
  2101. counter = PlsrCounters[index];
  2102. for (;;)
  2103. {
  2104. overflowBefore = PlsrCounterOverflowPulses[index];
  2105. statusBefore = counter->SR & TIM_SR_UIF;
  2106. count = (uint16_t)counter->CNT;
  2107. statusAfter = counter->SR & TIM_SR_UIF;
  2108. overflowAfter = PlsrCounterOverflowPulses[index];
  2109. if ((overflowBefore == overflowAfter)
  2110. && (statusBefore == statusAfter))
  2111. {
  2112. if (statusAfter != 0UL)
  2113. {
  2114. overflowAfter += PLSR_COUNTER_BLOCK_PULSES;
  2115. }
  2116. return overflowAfter + count;
  2117. }
  2118. }
  2119. }
  2120. static uint64_t PlsrCounterSnapshot(uint8_t pulseOutput)
  2121. {
  2122. uint64_t current = PlsrCounterCurrentRaw(pulseOutput);
  2123. uint64_t observed;
  2124. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2125. && (current > 0UL))
  2126. {
  2127. uint8_t sourceAxis = PlsrAbCounterSourceAxis[pulseOutput];
  2128. uint8_t attempt;
  2129. for (attempt = 0U; attempt < 2U; attempt++)
  2130. {
  2131. uint64_t verified;
  2132. current = PlsrCounterCurrentRaw(pulseOutput);
  2133. if (sourceAxis <= 3U)
  2134. {
  2135. uint32_t sourceCount = PlsrTimerMap[sourceAxis].timer->CNT;
  2136. verified = PlsrCounterCurrentRaw(pulseOutput);
  2137. if (current == verified)
  2138. {
  2139. if ((sourceCount
  2140. < PlsrAbCounterBoundary[pulseOutput])
  2141. && (current > 0UL))
  2142. {
  2143. current--;
  2144. }
  2145. break;
  2146. }
  2147. current = verified;
  2148. }
  2149. }
  2150. }
  2151. observed = PlsrObservedPulseBase[pulseOutput] + current;
  2152. if (observed < PlsrObservedPulsePublished[pulseOutput])
  2153. {
  2154. observed = PlsrObservedPulsePublished[pulseOutput];
  2155. }
  2156. else
  2157. {
  2158. PlsrObservedPulsePublished[pulseOutput] = observed;
  2159. }
  2160. return observed;
  2161. }
  2162. static uint64_t PlsrCounterSnapshotStopped(uint8_t pulseOutput)
  2163. {
  2164. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  2165. TIM_TypeDef *counter = PlsrCounters[index];
  2166. uint64_t current = PlsrCounterOverflowPulses[index];
  2167. uint64_t observed;
  2168. current += (uint16_t)counter->CNT;
  2169. if ((counter->SR & TIM_SR_UIF) != 0UL)
  2170. {
  2171. current += PLSR_COUNTER_BLOCK_PULSES;
  2172. }
  2173. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2174. && (current > 0UL))
  2175. {
  2176. uint8_t sourceAxis = PlsrAbCounterSourceAxis[pulseOutput];
  2177. if ((sourceAxis <= 3U)
  2178. && (PlsrTimerMap[sourceAxis].timer->CNT
  2179. < PlsrAbCounterBoundary[pulseOutput]))
  2180. {
  2181. current--;
  2182. }
  2183. }
  2184. observed = PlsrObservedPulseBase[pulseOutput] + current;
  2185. if (observed < PlsrObservedPulsePublished[pulseOutput])
  2186. {
  2187. observed = PlsrObservedPulsePublished[pulseOutput];
  2188. }
  2189. else
  2190. {
  2191. PlsrObservedPulsePublished[pulseOutput] = observed;
  2192. }
  2193. return observed;
  2194. }
  2195. static void PlsrCounterStop(uint8_t pulseOutput)
  2196. {
  2197. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  2198. if (index < PLSR_COUNTER_COUNT)
  2199. {
  2200. TIM_TypeDef *counter = PlsrCounters[index];
  2201. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2202. && (PlsrAbFastGated[pulseOutput] != 0U))
  2203. {
  2204. PlsrObservedPulseBase[pulseOutput] =
  2205. PlsrCounterSnapshotStopped(pulseOutput);
  2206. }
  2207. else
  2208. {
  2209. PlsrCounterSuspend(pulseOutput);
  2210. PlsrObservedPulseBase[pulseOutput] =
  2211. PlsrCounterSnapshot(pulseOutput);
  2212. }
  2213. PlsrObservedPulsePublished[pulseOutput] =
  2214. PlsrObservedPulseBase[pulseOutput];
  2215. counter->CR1 = 0UL;
  2216. counter->DIER = 0UL;
  2217. counter->SMCR = 0UL;
  2218. counter->SR = 0UL;
  2219. if (PlsrCounterOwner[index] == pulseOutput)
  2220. {
  2221. PlsrCounterOwner[index] = PLSR_COUNTER_NONE;
  2222. }
  2223. }
  2224. PlsrCounterIndexByOutput[pulseOutput] = PLSR_COUNTER_NONE;
  2225. }
  2226. static uint8_t PlsrCounterConfigure(uint8_t pulseOutput, uint8_t outputMode)
  2227. {
  2228. uint8_t index = PlsrCounterIndex(pulseOutput, outputMode);
  2229. TIM_TypeDef *counter = PlsrCounters[index];
  2230. uint32_t triggerSelection = ((pulseOutput & 2U) == 0U)
  2231. ? TIM_SMCR_TS_1
  2232. : (TIM_SMCR_TS_1 | TIM_SMCR_TS_0);
  2233. PlsrCounterStop(pulseOutput);
  2234. if ((PlsrCounterOwner[index] != PLSR_COUNTER_NONE)
  2235. && (PlsrCounterOwner[index] != pulseOutput))
  2236. {
  2237. return 0U;
  2238. }
  2239. PlsrCounterOwner[index] = pulseOutput;
  2240. PlsrCounterIndexByOutput[pulseOutput] = index;
  2241. PlsrCounterOverflowPulses[index] = 0UL;
  2242. counter->CR1 = 0UL;
  2243. counter->DIER = 0UL;
  2244. counter->SMCR = 0UL;
  2245. counter->PSC = 0UL;
  2246. counter->ARR = 0xFFFFUL;
  2247. counter->CNT = 0UL;
  2248. counter->EGR = TIM_EGR_UG;
  2249. counter->SR = 0UL;
  2250. /* RM0090 table 101 routes TIM10/11/13/14 OC directly to ITR2/3. */
  2251. counter->SMCR = triggerSelection;
  2252. counter->DIER = TIM_DIER_UIE;
  2253. return 1U;
  2254. }
  2255. static void PlsrCounterBegin(uint8_t pulseOutput)
  2256. {
  2257. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  2258. if (index < PLSR_COUNTER_COUNT)
  2259. {
  2260. TIM_TypeDef *counter = PlsrCounters[index];
  2261. counter->SMCR |= TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0;
  2262. counter->CR1 |= TIM_CR1_CEN;
  2263. }
  2264. }
  2265. static void PlsrCounterSuspend(uint8_t pulseOutput)
  2266. {
  2267. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  2268. if (index < PLSR_COUNTER_COUNT)
  2269. {
  2270. PlsrCounters[index]->CR1 &= ~TIM_CR1_CEN;
  2271. PlsrCounters[index]->SMCR &=
  2272. ~(TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0);
  2273. }
  2274. }
  2275. static void PlsrAbLoadAndStart(uint8_t pulseOutput,
  2276. const PLSR_AB_SETTING *setting)
  2277. {
  2278. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2279. uint8_t leadOutput =
  2280. (PlsrTimerDirectionPositive[pulseOutput] != 0U)
  2281. ? pulseOutput : pairOutput;
  2282. uint8_t lagOutput = (leadOutput == pulseOutput)
  2283. ? pairOutput : pulseOutput;
  2284. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2285. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2286. TIM_TypeDef *counterSourceTimer;
  2287. TIM_TypeDef *otherTimer;
  2288. uint32_t periodCounts = setting->period + 1UL;
  2289. uint32_t leadStart = (periodCounts * 3UL) / 4UL + 1UL;
  2290. uint32_t lagStart = periodCounts / 2UL + 1UL;
  2291. uint32_t counterSourceCr1;
  2292. uint32_t otherCr1;
  2293. #if PLSR_DEBUG_TIMING
  2294. uint8_t debugCounterIndex = PlsrCounterIndexByOutput[pulseOutput];
  2295. uint8_t debugReload = PlsrTimerRunning[pulseOutput];
  2296. if ((debugReload != 0U) && (debugCounterIndex < PLSR_COUNTER_COUNT))
  2297. {
  2298. PlsrAbReloadCounterBefore[pulseOutput] =
  2299. (uint16_t)PlsrCounters[debugCounterIndex]->CNT;
  2300. PlsrAbReloadCount[pulseOutput]++;
  2301. }
  2302. #endif
  2303. if (leadStart >= periodCounts)
  2304. {
  2305. leadStart = periodCounts - 1UL;
  2306. }
  2307. if (lagStart >= periodCounts)
  2308. {
  2309. lagStart = periodCounts - 1UL;
  2310. }
  2311. PlsrAbStructureVerified[pulseOutput] = 0U;
  2312. PlsrAbLagAxis[pulseOutput] = lagOutput;
  2313. PlsrAbCounterSourceAxis[pulseOutput] =
  2314. (pulseOutput == 0U) ? pulseOutput : pairOutput;
  2315. PlsrAbCounterBoundary[pulseOutput] =
  2316. (PlsrAbCounterSourceAxis[pulseOutput] == leadOutput)
  2317. ? (leadStart - 1UL) : (periodCounts / 2UL);
  2318. PlsrCounterSuspend(pulseOutput);
  2319. baseTimer->CR1 &= ~TIM_CR1_CEN;
  2320. pairTimer->CR1 &= ~TIM_CR1_CEN;
  2321. PlsrAbHoldPairIdle(pulseOutput);
  2322. baseTimer->CCER &= ~TIM_CCER_CC1E;
  2323. pairTimer->CCER &= ~TIM_CCER_CC1E;
  2324. baseTimer->DIER = 0UL;
  2325. pairTimer->DIER = 0UL;
  2326. baseTimer->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE;
  2327. pairTimer->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE;
  2328. baseTimer->PSC = setting->basePrescaler;
  2329. pairTimer->PSC = setting->pairPrescaler;
  2330. baseTimer->ARR = setting->period;
  2331. pairTimer->ARR = setting->period;
  2332. baseTimer->CCR1 = setting->compare;
  2333. pairTimer->CCR1 = setting->compare;
  2334. baseTimer->CR1 = TIM_CR1_ARPE | TIM_CR1_URS;
  2335. pairTimer->CR1 = TIM_CR1_ARPE | TIM_CR1_URS;
  2336. baseTimer->EGR = TIM_EGR_UG;
  2337. pairTimer->EGR = TIM_EGR_UG;
  2338. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2339. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2340. PlsrTimerMap[leadOutput].timer->CNT = leadStart;
  2341. PlsrTimerMap[lagOutput].timer->CNT = lagStart;
  2342. baseTimer->CCER = (baseTimer->CCER
  2343. & ~(TIM_CCER_CC1P | TIM_CCER_CC1E))
  2344. | TIM_CCER_CC1P | TIM_CCER_CC1E;
  2345. pairTimer->CCER = (pairTimer->CCER
  2346. & ~(TIM_CCER_CC1P | TIM_CCER_CC1E))
  2347. | TIM_CCER_CC1P | TIM_CCER_CC1E;
  2348. PlsrAbReleasePair(pulseOutput);
  2349. baseTimer->CCMR1 = TIM_CCMR1_OC1PE
  2350. | (6UL << TIM_CCMR1_OC1M_Pos);
  2351. pairTimer->CCMR1 = TIM_CCMR1_OC1PE
  2352. | (6UL << TIM_CCMR1_OC1M_Pos);
  2353. PlsrCounterBegin(pulseOutput);
  2354. #if PLSR_DEBUG_TIMING
  2355. if ((debugReload != 0U) && (debugCounterIndex < PLSR_COUNTER_COUNT))
  2356. {
  2357. PlsrAbReloadCounterArmed[pulseOutput] =
  2358. (uint16_t)PlsrCounters[debugCounterIndex]->CNT;
  2359. }
  2360. #endif
  2361. counterSourceTimer =
  2362. PlsrTimerMap[PlsrAbCounterSourceAxis[pulseOutput]].timer;
  2363. otherTimer =
  2364. PlsrTimerMap[(PlsrAbCounterSourceAxis[pulseOutput] == pulseOutput)
  2365. ? pairOutput : pulseOutput].timer;
  2366. counterSourceCr1 = counterSourceTimer->CR1 | TIM_CR1_CEN;
  2367. otherCr1 = otherTimer->CR1 | TIM_CR1_CEN;
  2368. PlsrAbEnableTimerPair(counterSourceTimer, counterSourceCr1,
  2369. otherTimer, otherCr1);
  2370. #if PLSR_DEBUG_TIMING
  2371. if ((debugReload != 0U) && (debugCounterIndex < PLSR_COUNTER_COUNT))
  2372. {
  2373. __DSB();
  2374. PlsrAbReloadCounterStarted[pulseOutput] =
  2375. (uint16_t)PlsrCounters[debugCounterIndex]->CNT;
  2376. }
  2377. #endif
  2378. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2379. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2380. PlsrTimerMap[lagOutput].timer->DIER |= TIM_DIER_CC1IE;
  2381. __DMB();
  2382. }
  2383. static void PlsrAbScheduleFrequencyVerify(uint8_t pulseOutput)
  2384. {
  2385. uint8_t verifyOutput =
  2386. (PlsrAbLagAxis[pulseOutput] == pulseOutput)
  2387. ? (uint8_t)(pulseOutput + 1U) : pulseOutput;
  2388. TIM_TypeDef *verifyTimer = PlsrTimerMap[verifyOutput].timer;
  2389. PlsrFrequencyVerifyPending[pulseOutput] =
  2390. PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ;
  2391. PlsrAbVerifyOwner[verifyOutput] = pulseOutput;
  2392. verifyTimer->SR = ~TIM_SR_UIF;
  2393. verifyTimer->DIER |= TIM_DIER_UIE;
  2394. if (PlsrDeferredPulsePending[pulseOutput] != 0U)
  2395. {
  2396. verifyTimer->SR = ~TIM_SR_CC1IF;
  2397. verifyTimer->DIER |= TIM_DIER_CC1IE;
  2398. }
  2399. __DMB();
  2400. }
  2401. static uint32_t PlsrFrequencyFromSnapshot(
  2402. uint8_t pulseOutput,
  2403. const PLSR_TIMER_SNAPSHOT *snapshot)
  2404. {
  2405. uint64_t divider = ((uint64_t)snapshot->psc + 1UL)
  2406. * ((uint64_t)snapshot->arr + 1UL);
  2407. if (divider == 0UL)
  2408. {
  2409. return 0UL;
  2410. }
  2411. return (uint32_t)(((uint64_t)PlsrTimerMap[pulseOutput].timerClockHz
  2412. + divider / 2UL)
  2413. / divider);
  2414. }
  2415. static uint32_t PlsrVerifyActiveFrequency(uint8_t pulseOutput)
  2416. {
  2417. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2418. PLSR_TIMER_SNAPSHOT baseSnapshot;
  2419. uint32_t activeFrequency = PlsrTimerActiveFrequencyHz[pulseOutput];
  2420. PlsrTimerSnapshot(baseTimer, &baseSnapshot);
  2421. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2422. {
  2423. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2424. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2425. PLSR_TIMER_SNAPSHOT pairSnapshot;
  2426. uint8_t structureValid;
  2427. PlsrTimerSnapshot(pairTimer, &pairSnapshot);
  2428. structureValid = PlsrAbStructureIsRunnable(
  2429. pulseOutput, &baseSnapshot, &pairSnapshot);
  2430. PlsrAbStructureVerified[pulseOutput] = structureValid;
  2431. if (structureValid == 0U)
  2432. {
  2433. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_CURVE;
  2434. }
  2435. if ((baseSnapshot.psc
  2436. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  2437. || (pairSnapshot.psc
  2438. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  2439. || (baseSnapshot.arr
  2440. != PlsrAbActiveSetting[pulseOutput].period)
  2441. || (pairSnapshot.arr
  2442. != PlsrAbActiveSetting[pulseOutput].period)
  2443. || (baseSnapshot.ccr1
  2444. != PlsrAbActiveSetting[pulseOutput].compare)
  2445. || (pairSnapshot.ccr1
  2446. != PlsrAbActiveSetting[pulseOutput].compare))
  2447. {
  2448. activeFrequency = PlsrFrequencyFromSnapshot(pairOutput,
  2449. &pairSnapshot);
  2450. if (PlsrPlatformFaultPending != PLSR_PLATFORM_FAULT_CURVE)
  2451. {
  2452. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_FREQUENCY;
  2453. }
  2454. }
  2455. }
  2456. else
  2457. {
  2458. uint8_t structureValid =
  2459. ((((baseSnapshot.cr1 & TIM_CR1_CEN) != 0UL)
  2460. && ((baseSnapshot.ccer & TIM_CCER_CC1E) != 0UL)
  2461. && ((baseSnapshot.ccer & TIM_CCER_CC1P) == 0UL)
  2462. && ((baseSnapshot.ccmr1 & PLSR_TIMER_OC1_MODE_MASK)
  2463. == PLSR_TIMER_PWM1_MODE)
  2464. && (baseSnapshot.ccr1
  2465. == ((baseSnapshot.arr + 1UL) / 2UL)))
  2466. ? 1U : 0U);
  2467. if (structureValid == 0U)
  2468. {
  2469. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_CURVE;
  2470. }
  2471. if ((baseSnapshot.psc
  2472. != PlsrTimerQueuedSetting[pulseOutput].prescaler)
  2473. || (baseSnapshot.arr
  2474. != PlsrTimerQueuedSetting[pulseOutput].period)
  2475. || (baseSnapshot.ccr1
  2476. != PlsrTimerQueuedSetting[pulseOutput].compare))
  2477. {
  2478. activeFrequency = PlsrFrequencyFromSnapshot(pulseOutput,
  2479. &baseSnapshot);
  2480. if (PlsrPlatformFaultPending != PLSR_PLATFORM_FAULT_CURVE)
  2481. {
  2482. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_FREQUENCY;
  2483. }
  2484. }
  2485. }
  2486. if (activeFrequency == 0UL)
  2487. {
  2488. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_FREQUENCY;
  2489. }
  2490. return activeFrequency;
  2491. }
  2492. uint8_t PlsrPlatformInit(void)
  2493. {
  2494. GPIO_InitTypeDef gpio;
  2495. uint8_t index;
  2496. const PLSR_BACKUP_POSITION_RECORD *positionRecord;
  2497. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_NONE;
  2498. __HAL_RCC_GPIOB_CLK_ENABLE();
  2499. __HAL_RCC_GPIOF_CLK_ENABLE();
  2500. __HAL_RCC_GPIOG_CLK_ENABLE();
  2501. __HAL_RCC_GPIOH_CLK_ENABLE();
  2502. __HAL_RCC_TIM10_CLK_ENABLE();
  2503. __HAL_RCC_TIM11_CLK_ENABLE();
  2504. __HAL_RCC_TIM13_CLK_ENABLE();
  2505. __HAL_RCC_TIM14_CLK_ENABLE();
  2506. __HAL_RCC_TIM9_CLK_ENABLE();
  2507. __HAL_RCC_TIM12_CLK_ENABLE();
  2508. __HAL_RCC_PWR_CLK_ENABLE();
  2509. HAL_PWR_EnableBkUpAccess();
  2510. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  2511. if (HAL_PWREx_EnableBkUpReg() != HAL_OK)
  2512. {
  2513. return 0U;
  2514. }
  2515. #if PLSR_DEBUG_TIMING
  2516. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  2517. DWT->CYCCNT = 0UL;
  2518. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  2519. (void)memset((void *)PlsrIrqCount, 0, sizeof(PlsrIrqCount));
  2520. (void)memset((void *)PlsrIrqLastCycles, 0, sizeof(PlsrIrqLastCycles));
  2521. (void)memset((void *)PlsrIrqMaxCycles, 0, sizeof(PlsrIrqMaxCycles));
  2522. (void)memset((void *)PlsrFinalArmQueueCount, 0,
  2523. sizeof(PlsrFinalArmQueueCount));
  2524. (void)memset((void *)PlsrFinalArmJobLastCycles, 0,
  2525. sizeof(PlsrFinalArmJobLastCycles));
  2526. (void)memset((void *)PlsrFinalArmJobMaxCycles, 0,
  2527. sizeof(PlsrFinalArmJobMaxCycles));
  2528. (void)memset((void *)PlsrFinalArmQueueToStopLastCycles, 0,
  2529. sizeof(PlsrFinalArmQueueToStopLastCycles));
  2530. (void)memset((void *)PlsrFinalArmQueueToStopMaxCycles, 0,
  2531. sizeof(PlsrFinalArmQueueToStopMaxCycles));
  2532. (void)memset((void *)PlsrFinalArmQueuedAt, 0,
  2533. sizeof(PlsrFinalArmQueuedAt));
  2534. (void)memset((void *)PlsrFinalArmQueueTimingPending, 0,
  2535. sizeof(PlsrFinalArmQueueTimingPending));
  2536. #endif
  2537. HAL_GPIO_WritePin(GPIOH, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8
  2538. | GPIO_PIN_9, GPIO_PIN_SET);
  2539. gpio.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  2540. gpio.Mode = GPIO_MODE_OUTPUT_PP;
  2541. gpio.Pull = GPIO_NOPULL;
  2542. gpio.Speed = GPIO_SPEED_FREQ_HIGH;
  2543. gpio.Alternate = 0U;
  2544. HAL_GPIO_Init(GPIOH, &gpio);
  2545. gpio.Mode = GPIO_MODE_INPUT;
  2546. gpio.Pull = GPIO_NOPULL;
  2547. gpio.Speed = GPIO_SPEED_FREQ_LOW;
  2548. gpio.Alternate = 0U;
  2549. gpio.Pin = GPIO_PIN_5;
  2550. HAL_GPIO_Init(GPIOB, &gpio);
  2551. gpio.Pin = GPIO_PIN_12;
  2552. HAL_GPIO_Init(GPIOG, &gpio);
  2553. for (index = 0U; index < 4U; index++)
  2554. {
  2555. PlsrTimerActiveFrequencyHz[index] = 0UL;
  2556. PlsrTimerQueuedFrequencyHz[index] = 0UL;
  2557. (void)memset(&PlsrTimerActiveSetting[index], 0,
  2558. sizeof(PlsrTimerActiveSetting[index]));
  2559. (void)memset(&PlsrTimerQueuedSetting[index], 0,
  2560. sizeof(PlsrTimerQueuedSetting[index]));
  2561. PlsrTimerQueueGeneration[index] = 0UL;
  2562. PlsrTimerOutputMode[index] = PLSR_OUTPUT_PULSE_DIR;
  2563. PlsrTimerDirectionPositive[index] = 1U;
  2564. PlsrTimerRunning[index] = 0U;
  2565. PlsrFrequencyVerifyPending[index] = 0U;
  2566. PlsrFrequencyVerifyPulseCount[index] = 0U;
  2567. PlsrDeferredPulsePending[index] = 0U;
  2568. PlsrAbVerifyOwner[index] = PLSR_COUNTER_NONE;
  2569. PlsrAbFinalArmJobOwner[index] = PLSR_COUNTER_NONE;
  2570. PlsrTimerIrqActive[index] = 0U;
  2571. PlsrAbFrequencyPending[index] = 0U;
  2572. PlsrAbLagAxis[index] = PLSR_COUNTER_NONE;
  2573. PlsrAbStructureVerified[index] = 0U;
  2574. PlsrAbCounterSourceAxis[index] = PLSR_COUNTER_NONE;
  2575. PlsrAbCounterBoundary[index] = 0UL;
  2576. PlsrAbStopPending[index] = 0U;
  2577. PlsrAbFastGated[index] = 0U;
  2578. PlsrCounterIndexByOutput[index] = PLSR_COUNTER_NONE;
  2579. PlsrObservedPulseBase[index] = 0UL;
  2580. PlsrObservedPulsePublished[index] = 0UL;
  2581. PlsrFiniteActive[index] = 0U;
  2582. PlsrFiniteCompletionPending[index] = 0U;
  2583. PlsrFiniteFrequencyPending[index] = 0U;
  2584. PlsrFiniteRetargetPending[index] = 0U;
  2585. PlsrFiniteTailStopPending[index] = 0U;
  2586. PlsrFiniteRetargetDrainPulses[index] = 0UL;
  2587. PlsrFiniteTargetPulses[index] = 0UL;
  2588. PlsrFiniteRemainingPulses[index] = 0UL;
  2589. PlsrFiniteCounterPreload[index] = 0U;
  2590. PlsrFiniteStepCount[index] = 0U;
  2591. PlsrFiniteStepIndex[index] = 0U;
  2592. PlsrFiniteBoundaryReadIndex[index] = 0U;
  2593. PlsrFiniteCompletedStepCount[index] = 0U;
  2594. PlsrTimerInitialize(PlsrTimerMap[index].timer);
  2595. PlsrPulsePinHoldIdle(index);
  2596. HAL_NVIC_SetPriority(PlsrTimerMap[index].irq, 0U, 0U);
  2597. HAL_NVIC_EnableIRQ(PlsrTimerMap[index].irq);
  2598. }
  2599. for (index = 0U; index < PLSR_COUNTER_COUNT; index++)
  2600. {
  2601. PlsrCounterOwner[index] = PLSR_COUNTER_NONE;
  2602. PlsrCounterOverflowPulses[index] = 0UL;
  2603. PlsrCounters[index]->CR1 = 0UL;
  2604. PlsrCounters[index]->DIER = 0UL;
  2605. PlsrCounters[index]->SMCR = 0UL;
  2606. PlsrCounters[index]->SR = 0UL;
  2607. }
  2608. PlsrPlatformFaultPending = 0U;
  2609. HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 0U, 0U);
  2610. HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
  2611. HAL_NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 0U, 0U);
  2612. HAL_NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn);
  2613. positionRecord = PlsrNewestBackupPosition();
  2614. PlsrBackupPositionGeneration =
  2615. (positionRecord == NULL) ? 0UL : positionRecord->generation;
  2616. return 1U;
  2617. }
  2618. uint8_t PlsrPlatformPrepare(uint8_t pulseOutput,
  2619. uint8_t directionOutput,
  2620. uint8_t directionLevel,
  2621. uint8_t outputMode,
  2622. uint8_t directionPositive)
  2623. {
  2624. uint8_t index;
  2625. if ((pulseOutput > 3U) || (directionOutput > 3U)
  2626. || (outputMode > PLSR_OUTPUT_AB)
  2627. || ((outputMode == PLSR_OUTPUT_AB)
  2628. && (pulseOutput != 0U) && (pulseOutput != 2U)))
  2629. {
  2630. return 0U;
  2631. }
  2632. if ((PlsrAbStopPending[0] != 0U) || (PlsrAbStopPending[2] != 0U))
  2633. {
  2634. return 0U;
  2635. }
  2636. for (index = 0U; index < 4U; index++)
  2637. {
  2638. PlsrAbFinalArmJobOwner[index] = PLSR_COUNTER_NONE;
  2639. NVIC_ClearPendingIRQ(PlsrTimerMap[index].irq);
  2640. PlsrCounterStop(index);
  2641. PlsrPulsePinHoldIdle(index);
  2642. PlsrTimerStop(PlsrTimerMap[index].timer);
  2643. HAL_GPIO_WritePin(PlsrDirectionMap[index].port,
  2644. PlsrDirectionMap[index].pin,
  2645. ((outputMode == PLSR_OUTPUT_PULSE_DIR)
  2646. && (index == directionOutput)
  2647. && (directionLevel != 0U))
  2648. ? GPIO_PIN_RESET : GPIO_PIN_SET);
  2649. PlsrTimerRunning[index] = 0U;
  2650. PlsrFrequencyVerifyPending[index] = 0U;
  2651. PlsrFrequencyVerifyPulseCount[index] = 0U;
  2652. PlsrDeferredPulsePending[index] = 0U;
  2653. PlsrAbVerifyOwner[index] = PLSR_COUNTER_NONE;
  2654. PlsrAbFrequencyPending[index] = 0U;
  2655. PlsrAbStructureVerified[index] = 0U;
  2656. PlsrAbFastGated[index] = 0U;
  2657. }
  2658. PlsrTimerOutputMode[pulseOutput] = outputMode;
  2659. PlsrTimerDirectionPositive[pulseOutput] =
  2660. (directionPositive != 0U) ? 1U : 0U;
  2661. PlsrPlatformFaultPending = 0U;
  2662. return 1U;
  2663. }
  2664. uint8_t PlsrPlatformStartPulse(uint8_t pulseOutput,
  2665. uint32_t firstFrequencyHz,
  2666. uint32_t queuedFrequencyHz,
  2667. uint32_t *actualFirstFrequencyHz,
  2668. uint32_t *actualQueuedFrequencyHz)
  2669. {
  2670. PLSR_PLATFORM_TIMER_SETTING firstSetting;
  2671. PLSR_PLATFORM_TIMER_SETTING queuedSetting;
  2672. if ((pulseOutput > 3U)
  2673. || (PlsrPlatformBuildTimerSetting(
  2674. pulseOutput, PlsrTimerOutputMode[pulseOutput],
  2675. firstFrequencyHz, &firstSetting) == 0U)
  2676. || (PlsrPlatformBuildTimerSetting(
  2677. pulseOutput, PlsrTimerOutputMode[pulseOutput],
  2678. queuedFrequencyHz, &queuedSetting) == 0U))
  2679. {
  2680. return 0U;
  2681. }
  2682. return PlsrPlatformStartPrepared(pulseOutput, &firstSetting,
  2683. &queuedSetting,
  2684. actualFirstFrequencyHz,
  2685. actualQueuedFrequencyHz);
  2686. }
  2687. static uint8_t PlsrPreparedSettingIsValid(
  2688. uint8_t pulseOutput,
  2689. uint8_t outputMode,
  2690. const PLSR_PLATFORM_TIMER_SETTING *setting)
  2691. {
  2692. uint32_t periodCounts;
  2693. if ((pulseOutput > 3U) || (setting == NULL)
  2694. || (setting->actualFrequencyHz == 0UL)
  2695. || (setting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ))
  2696. {
  2697. return 0U;
  2698. }
  2699. periodCounts = (uint32_t)setting->period + 1UL;
  2700. if (setting->compare != (uint16_t)(periodCounts / 2UL))
  2701. {
  2702. return 0U;
  2703. }
  2704. if (outputMode == PLSR_OUTPUT_PULSE_DIR)
  2705. {
  2706. return ((periodCounts >= 2UL)
  2707. && (setting->pairPrescaler == 0U)) ? 1U : 0U;
  2708. }
  2709. if ((outputMode != PLSR_OUTPUT_AB) || ((pulseOutput & 1U) != 0U)
  2710. || (periodCounts < 4UL))
  2711. {
  2712. return 0U;
  2713. }
  2714. return ((((uint32_t)setting->prescaler + 1UL)
  2715. == 2UL * ((uint32_t)setting->pairPrescaler + 1UL))
  2716. ? 1U : 0U);
  2717. }
  2718. static void PlsrPlatformToTimerSetting(
  2719. const PLSR_PLATFORM_TIMER_SETTING *source,
  2720. PLSR_TIMER_SETTING *destination)
  2721. {
  2722. destination->prescaler = source->prescaler;
  2723. destination->period = source->period;
  2724. destination->compare = source->compare;
  2725. destination->actualFrequencyHz = source->actualFrequencyHz;
  2726. }
  2727. static void PlsrPlatformToAbSetting(
  2728. const PLSR_PLATFORM_TIMER_SETTING *source,
  2729. PLSR_AB_SETTING *destination)
  2730. {
  2731. destination->basePrescaler = source->prescaler;
  2732. destination->pairPrescaler = source->pairPrescaler;
  2733. destination->period = source->period;
  2734. destination->compare = source->compare;
  2735. destination->actualFrequencyHz = source->actualFrequencyHz;
  2736. }
  2737. static uint8_t PlsrPlatformSettingsDiffer(
  2738. const PLSR_PLATFORM_TIMER_SETTING *first,
  2739. const PLSR_PLATFORM_TIMER_SETTING *second)
  2740. {
  2741. return (((first->actualFrequencyHz != second->actualFrequencyHz)
  2742. || (first->prescaler != second->prescaler)
  2743. || (first->pairPrescaler != second->pairPrescaler)
  2744. || (first->period != second->period)
  2745. || (first->compare != second->compare)) ? 1U : 0U);
  2746. }
  2747. uint8_t PlsrPlatformBuildTimerSetting(
  2748. uint8_t pulseOutput,
  2749. uint8_t outputMode,
  2750. uint32_t requestedFrequencyHz,
  2751. PLSR_PLATFORM_TIMER_SETTING *setting)
  2752. {
  2753. PLSR_TIMER_SETTING timerSetting;
  2754. PLSR_AB_SETTING abSetting;
  2755. if (setting == NULL)
  2756. {
  2757. return 0U;
  2758. }
  2759. if (outputMode == PLSR_OUTPUT_AB)
  2760. {
  2761. if (PlsrAbCalculate(pulseOutput, requestedFrequencyHz,
  2762. &abSetting) == 0U)
  2763. {
  2764. return 0U;
  2765. }
  2766. setting->actualFrequencyHz = abSetting.actualFrequencyHz;
  2767. setting->prescaler = (uint16_t)abSetting.basePrescaler;
  2768. setting->pairPrescaler = (uint16_t)abSetting.pairPrescaler;
  2769. setting->period = (uint16_t)abSetting.period;
  2770. setting->compare = (uint16_t)abSetting.compare;
  2771. return 1U;
  2772. }
  2773. if ((outputMode != PLSR_OUTPUT_PULSE_DIR)
  2774. || (PlsrTimerCalculate(pulseOutput, requestedFrequencyHz,
  2775. &timerSetting) == 0U))
  2776. {
  2777. return 0U;
  2778. }
  2779. setting->actualFrequencyHz = timerSetting.actualFrequencyHz;
  2780. setting->prescaler = (uint16_t)timerSetting.prescaler;
  2781. setting->pairPrescaler = 0U;
  2782. setting->period = (uint16_t)timerSetting.period;
  2783. setting->compare = (uint16_t)timerSetting.compare;
  2784. return 1U;
  2785. }
  2786. uint8_t PlsrPlatformStartPrepared(
  2787. uint8_t pulseOutput,
  2788. const PLSR_PLATFORM_TIMER_SETTING *firstSetting,
  2789. const PLSR_PLATFORM_TIMER_SETTING *queuedSetting,
  2790. uint32_t *actualFirstFrequencyHz,
  2791. uint32_t *actualQueuedFrequencyHz)
  2792. {
  2793. uint8_t outputMode;
  2794. if ((pulseOutput > 3U) || (actualFirstFrequencyHz == NULL)
  2795. || (actualQueuedFrequencyHz == NULL))
  2796. {
  2797. return 0U;
  2798. }
  2799. outputMode = PlsrTimerOutputMode[pulseOutput];
  2800. if ((PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2801. firstSetting) == 0U)
  2802. || (PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2803. queuedSetting) == 0U))
  2804. {
  2805. return 0U;
  2806. }
  2807. if (outputMode == PLSR_OUTPUT_AB)
  2808. {
  2809. PLSR_AB_SETTING firstAbSetting;
  2810. PLSR_AB_SETTING queuedAbSetting;
  2811. if ((PlsrAbStopPending[pulseOutput] != 0U)
  2812. || (PlsrAbFastGated[pulseOutput] != 0U)
  2813. || (PlsrCounterConfigure(pulseOutput, PLSR_OUTPUT_AB) == 0U))
  2814. {
  2815. return 0U;
  2816. }
  2817. PlsrPlatformToAbSetting(firstSetting, &firstAbSetting);
  2818. PlsrPlatformToAbSetting(queuedSetting, &queuedAbSetting);
  2819. PlsrAbLoadAndStart(pulseOutput, &firstAbSetting);
  2820. PlsrAbActiveSetting[pulseOutput] = firstAbSetting;
  2821. PlsrAbPendingSetting[pulseOutput] = queuedAbSetting;
  2822. PlsrAbFrequencyPending[pulseOutput] =
  2823. ((firstSetting->prescaler != queuedSetting->prescaler)
  2824. || (firstSetting->pairPrescaler
  2825. != queuedSetting->pairPrescaler)
  2826. || (firstSetting->period != queuedSetting->period)) ? 1U : 0U;
  2827. }
  2828. else
  2829. {
  2830. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  2831. PLSR_TIMER_SETTING firstTimerSetting;
  2832. PLSR_TIMER_SETTING queuedTimerSetting;
  2833. if (PlsrCounterConfigure(pulseOutput,
  2834. PLSR_OUTPUT_PULSE_DIR) == 0U)
  2835. {
  2836. return 0U;
  2837. }
  2838. PlsrPlatformToTimerSetting(firstSetting, &firstTimerSetting);
  2839. PlsrPlatformToTimerSetting(queuedSetting, &queuedTimerSetting);
  2840. timer->DIER &= ~TIM_DIER_UIE;
  2841. timer->CR1 &= ~TIM_CR1_CEN;
  2842. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2843. timer->CNT = 0UL;
  2844. PlsrTimerWriteSetting(timer, &firstTimerSetting);
  2845. timer->EGR = TIM_EGR_UG;
  2846. PlsrTimerWriteSetting(timer, &queuedTimerSetting);
  2847. timer->CNT = firstTimerSetting.compare;
  2848. timer->SR = 0UL;
  2849. timer->CCER = (timer->CCER
  2850. & ~(TIM_CCER_CC1E | TIM_CCER_CC1P))
  2851. | TIM_CCER_CC1E;
  2852. __DSB();
  2853. timer->DIER |= TIM_DIER_UIE;
  2854. PlsrPulsePinRelease(pulseOutput);
  2855. PlsrCounterBegin(pulseOutput);
  2856. timer->CR1 |= TIM_CR1_CEN;
  2857. }
  2858. PlsrTimerActiveSetting[pulseOutput] = *firstSetting;
  2859. PlsrTimerQueuedSetting[pulseOutput] = *queuedSetting;
  2860. PlsrTimerActiveFrequencyHz[pulseOutput] =
  2861. firstSetting->actualFrequencyHz;
  2862. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  2863. queuedSetting->actualFrequencyHz;
  2864. PlsrTimerQueueGeneration[pulseOutput]++;
  2865. PlsrTimerRunning[pulseOutput] = 1U;
  2866. if (outputMode == PLSR_OUTPUT_AB)
  2867. {
  2868. PlsrAbScheduleFrequencyVerify(pulseOutput);
  2869. }
  2870. else
  2871. {
  2872. PlsrFrequencyVerifyPending[pulseOutput] =
  2873. PLSR_FREQUENCY_VERIFY_NOW;
  2874. }
  2875. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2876. *actualFirstFrequencyHz = firstSetting->actualFrequencyHz;
  2877. *actualQueuedFrequencyHz = queuedSetting->actualFrequencyHz;
  2878. return 1U;
  2879. }
  2880. uint8_t PlsrPlatformSupportsFinitePulseTrain(void)
  2881. {
  2882. return 1U;
  2883. }
  2884. uint8_t PlsrPlatformStartFinitePrepared(
  2885. uint8_t pulseOutput,
  2886. const PLSR_PLATFORM_TIMER_SETTING *setting,
  2887. uint32_t pulseCount,
  2888. uint32_t *actualFrequencyHz)
  2889. {
  2890. TIM_TypeDef *timer;
  2891. TIM_TypeDef *counter;
  2892. PLSR_TIMER_SETTING timerSetting;
  2893. uint32_t firstBlock;
  2894. uint8_t counterIndex;
  2895. if ((pulseOutput > 3U) || (setting == NULL)
  2896. || (pulseCount == 0UL) || (actualFrequencyHz == NULL)
  2897. || (PlsrTimerOutputMode[pulseOutput] != PLSR_OUTPUT_PULSE_DIR)
  2898. || (PlsrPreparedSettingIsValid(pulseOutput,
  2899. PLSR_OUTPUT_PULSE_DIR,
  2900. setting) == 0U)
  2901. || (PlsrCounterConfigure(pulseOutput,
  2902. PLSR_OUTPUT_PULSE_DIR) == 0U))
  2903. {
  2904. return 0U;
  2905. }
  2906. timer = PlsrTimerMap[pulseOutput].timer;
  2907. counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  2908. counter = PlsrCounters[counterIndex];
  2909. firstBlock = (pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  2910. ? PLSR_COUNTER_BLOCK_PULSES : pulseCount;
  2911. PlsrPlatformToTimerSetting(setting, &timerSetting);
  2912. counter->CR1 &= ~TIM_CR1_CEN;
  2913. counter->SMCR &= ~(TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0);
  2914. counter->ARR = (firstBlock == 1UL) ? 1UL : (firstBlock - 1UL);
  2915. counter->CNT = 0UL;
  2916. counter->EGR = TIM_EGR_UG;
  2917. PlsrFiniteCounterPreload[pulseOutput] =
  2918. (firstBlock == 1UL) ? 1U : 0U;
  2919. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  2920. counter->SR = 0UL;
  2921. counter->DIER = TIM_DIER_UIE;
  2922. timer->DIER = 0UL;
  2923. timer->CR1 &= ~TIM_CR1_CEN;
  2924. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2925. timer->CNT = 0UL;
  2926. PlsrTimerWriteSetting(timer, &timerSetting);
  2927. timer->EGR = TIM_EGR_UG;
  2928. /*
  2929. * The board's high-speed output stage is active low. Keep OC1 high
  2930. * while the pin changes from GPIO idle to the timer alternate function,
  2931. * then let the first terminal pulse start at CCR1. Starting at CCR1
  2932. * would expose an uncounted terminal pulse before the first OC rising
  2933. * edge reaches TIM9/TIM12.
  2934. */
  2935. timer->CNT = 0UL;
  2936. timer->SR = 0UL;
  2937. timer->CCER = (timer->CCER
  2938. & ~(TIM_CCER_CC1E | TIM_CCER_CC1P))
  2939. | TIM_CCER_CC1E;
  2940. PlsrFiniteTargetPulses[pulseOutput] = pulseCount;
  2941. PlsrFiniteRemainingPulses[pulseOutput] = pulseCount;
  2942. PlsrFiniteCompletionPending[pulseOutput] = 0U;
  2943. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  2944. PlsrFiniteStreamActive[pulseOutput] = 0U;
  2945. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  2946. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  2947. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  2948. PlsrFiniteActive[pulseOutput] = 1U;
  2949. PlsrFiniteStepCount[pulseOutput] = 0U;
  2950. PlsrFiniteStepIndex[pulseOutput] = 0U;
  2951. PlsrFiniteBoundaryReadIndex[pulseOutput] = 0U;
  2952. PlsrFiniteCompletedStepCount[pulseOutput] = 0U;
  2953. PlsrTimerActiveSetting[pulseOutput] = *setting;
  2954. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  2955. PlsrTimerActiveFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  2956. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  2957. PlsrTimerRunning[pulseOutput] = 1U;
  2958. *actualFrequencyHz = setting->actualFrequencyHz;
  2959. __DSB();
  2960. PlsrPulsePinRelease(pulseOutput);
  2961. PlsrCounterBegin(pulseOutput);
  2962. timer->CR1 |= TIM_CR1_CEN;
  2963. return 1U;
  2964. }
  2965. uint8_t PlsrPlatformStartCountedStreamPrepared(
  2966. uint8_t pulseOutput,
  2967. const PLSR_PLATFORM_TIMER_SETTING *setting,
  2968. uint32_t pulseCount,
  2969. uint32_t *actualFrequencyHz)
  2970. {
  2971. TIM_TypeDef *counter;
  2972. uint32_t firstBlock;
  2973. if (PlsrPlatformStartFinitePrepared(pulseOutput, setting, pulseCount,
  2974. actualFrequencyHz) == 0U)
  2975. {
  2976. return 0U;
  2977. }
  2978. PlsrFiniteStreamActive[pulseOutput] = 1U;
  2979. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  2980. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  2981. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  2982. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  2983. firstBlock = (pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  2984. ? PLSR_COUNTER_BLOCK_PULSES : pulseCount;
  2985. counter = PlsrCounters[PlsrCounterIndexByOutput[pulseOutput]];
  2986. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, firstBlock);
  2987. return 1U;
  2988. }
  2989. uint8_t PlsrPlatformStartFiniteSequencePrepared(
  2990. uint8_t pulseOutput,
  2991. PLSR_PLATFORM_FINITE_STEP *steps,
  2992. uint16_t stepCount,
  2993. uint32_t *actualFrequencyHz)
  2994. {
  2995. TIM_TypeDef *counter;
  2996. uint16_t index;
  2997. if ((steps == NULL) || (stepCount == 0U)
  2998. || (stepCount > PLSR_PLATFORM_FINITE_STEP_MAX))
  2999. {
  3000. return 0U;
  3001. }
  3002. for (index = 0U; index < stepCount; index++)
  3003. {
  3004. if ((steps[index].pulseCount == 0UL)
  3005. || (steps[index].segmentNumber == 0U)
  3006. || (PlsrPreparedSettingIsValid(
  3007. pulseOutput, PLSR_OUTPUT_PULSE_DIR,
  3008. &steps[index].setting) == 0U))
  3009. {
  3010. return 0U;
  3011. }
  3012. }
  3013. if (PlsrPlatformStartFinitePrepared(
  3014. pulseOutput, &steps[0].setting, steps[0].pulseCount,
  3015. actualFrequencyHz) == 0U)
  3016. {
  3017. return 0U;
  3018. }
  3019. PlsrFiniteSteps[pulseOutput] = steps;
  3020. PlsrFiniteStepCount[pulseOutput] = stepCount;
  3021. PlsrFiniteStepIndex[pulseOutput] = 0U;
  3022. PlsrFiniteBoundaryReadIndex[pulseOutput] = 0U;
  3023. PlsrFiniteCompletedStepCount[pulseOutput] = 0U;
  3024. if (stepCount > 1U)
  3025. {
  3026. counter = PlsrCounters[PlsrCounterIndexByOutput[pulseOutput]];
  3027. PlsrFiniteArmNextStepPrepare(
  3028. pulseOutput, counter,
  3029. (steps[0].pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  3030. ? PLSR_COUNTER_BLOCK_PULSES : steps[0].pulseCount);
  3031. }
  3032. return 1U;
  3033. }
  3034. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformUpdateFinitePrepared(
  3035. uint8_t pulseOutput,
  3036. const PLSR_PLATFORM_TIMER_SETTING *setting,
  3037. uint32_t *actualFrequencyHz)
  3038. {
  3039. TIM_TypeDef *timer;
  3040. uint32_t activePeriod;
  3041. uint32_t counter;
  3042. uint32_t criticalState;
  3043. if ((pulseOutput > 3U) || (setting == NULL)
  3044. || (actualFrequencyHz == NULL)
  3045. || (PlsrFiniteActive[pulseOutput] == 0U)
  3046. || (PlsrPreparedSettingIsValid(pulseOutput,
  3047. PLSR_OUTPUT_PULSE_DIR,
  3048. setting) == 0U))
  3049. {
  3050. return PLSR_PLATFORM_QUEUE_STALE;
  3051. }
  3052. timer = PlsrTimerMap[pulseOutput].timer;
  3053. criticalState = PlsrPlatformEnterCritical();
  3054. if ((PlsrFiniteActive[pulseOutput] == 0U)
  3055. || ((timer->CR1 & TIM_CR1_CEN) == 0UL))
  3056. {
  3057. PlsrPlatformExitCritical(criticalState);
  3058. return PLSR_PLATFORM_QUEUE_STALE;
  3059. }
  3060. if (PlsrFiniteFrequencyPending[pulseOutput] != 0U)
  3061. {
  3062. if ((timer->SR & TIM_SR_UIF) == 0UL)
  3063. {
  3064. PlsrPlatformExitCritical(criticalState);
  3065. return PLSR_PLATFORM_QUEUE_STALE;
  3066. }
  3067. /* The update IRQ normally commits this preload. Also consume a
  3068. latched update here so a delayed/shared IRQ cannot stall a ramp. */
  3069. timer->SR = ~TIM_SR_UIF;
  3070. timer->DIER &= ~TIM_DIER_UIE;
  3071. PlsrTimerActiveFrequencyHz[pulseOutput] =
  3072. PlsrTimerQueuedFrequencyHz[pulseOutput];
  3073. PlsrTimerActiveSetting[pulseOutput] =
  3074. PlsrTimerQueuedSetting[pulseOutput];
  3075. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  3076. }
  3077. activePeriod = PlsrTimerActiveSetting[pulseOutput].period;
  3078. counter = timer->CNT;
  3079. if ((counter > activePeriod)
  3080. || ((activePeriod - counter) < PLSR_FINITE_WRITE_GUARD_COUNTS))
  3081. {
  3082. PlsrPlatformExitCritical(criticalState);
  3083. return PLSR_PLATFORM_QUEUE_STALE;
  3084. }
  3085. timer->PSC = setting->prescaler;
  3086. timer->ARR = setting->period;
  3087. timer->CCR1 = setting->compare;
  3088. __DMB();
  3089. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  3090. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  3091. PlsrTimerQueueGeneration[pulseOutput]++;
  3092. PlsrFiniteFrequencyPending[pulseOutput] = 1U;
  3093. timer->SR = ~TIM_SR_UIF;
  3094. timer->DIER |= TIM_DIER_UIE;
  3095. *actualFrequencyHz = setting->actualFrequencyHz;
  3096. PlsrPlatformExitCritical(criticalState);
  3097. return PLSR_PLATFORM_QUEUE_APPLIED;
  3098. }
  3099. uint8_t PlsrPlatformRetargetFiniteStop(uint8_t pulseOutput,
  3100. uint32_t drainPulses)
  3101. {
  3102. TIM_TypeDef *timer;
  3103. if ((pulseOutput > 3U) || (drainPulses == 0UL)
  3104. || (PlsrFiniteActive[pulseOutput] == 0U)
  3105. || (PlsrCounterIndexByOutput[pulseOutput] >= PLSR_COUNTER_COUNT)
  3106. || (PlsrFiniteRetargetPending[pulseOutput] != 0U)
  3107. || (PlsrFiniteTailStopPending[pulseOutput] != 0U))
  3108. {
  3109. return 0U;
  3110. }
  3111. timer = PlsrTimerMap[pulseOutput].timer;
  3112. PlsrFiniteRetargetDrainPulses[pulseOutput] = drainPulses;
  3113. PlsrFiniteRetargetPending[pulseOutput] = 1U;
  3114. timer->SR = ~TIM_SR_CC1IF;
  3115. timer->DIER |= TIM_DIER_CC1IE;
  3116. __DMB();
  3117. return 1U;
  3118. }
  3119. uint8_t PlsrPlatformFiniteRetargetReady(uint8_t pulseOutput,
  3120. uint32_t *activeFrequencyHz)
  3121. {
  3122. uint32_t criticalState;
  3123. uint8_t ready;
  3124. if ((pulseOutput > 3U) || (activeFrequencyHz == NULL))
  3125. {
  3126. return 0U;
  3127. }
  3128. criticalState = PlsrPlatformEnterCritical();
  3129. ready = ((PlsrFiniteActive[pulseOutput] != 0U)
  3130. && (PlsrFiniteRetargetPending[pulseOutput] == 0U)
  3131. && (PlsrFiniteStepCount[pulseOutput] == 0U)) ? 1U : 0U;
  3132. *activeFrequencyHz = PlsrTimerActiveFrequencyHz[pulseOutput];
  3133. PlsrPlatformExitCritical(criticalState);
  3134. return ready;
  3135. }
  3136. uint8_t PlsrPlatformFiniteProgress(uint8_t pulseOutput,
  3137. uint32_t *completedPulses)
  3138. {
  3139. uint8_t counterIndex;
  3140. TIM_TypeDef *counter;
  3141. uint32_t criticalState;
  3142. uint32_t completed;
  3143. if ((pulseOutput > 3U) || (completedPulses == NULL)
  3144. || ((PlsrFiniteActive[pulseOutput] == 0U)
  3145. && (PlsrFiniteCompletionPending[pulseOutput] == 0U)))
  3146. {
  3147. return 0U;
  3148. }
  3149. criticalState = PlsrPlatformEnterCritical();
  3150. counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  3151. if (PlsrFiniteCompletionPending[pulseOutput] != 0U)
  3152. {
  3153. completed = PlsrFiniteTargetPulses[pulseOutput];
  3154. }
  3155. else if (counterIndex < PLSR_COUNTER_COUNT)
  3156. {
  3157. counter = PlsrCounters[counterIndex];
  3158. uint32_t blockCount = (uint16_t)counter->CNT;
  3159. if (blockCount >= PlsrFiniteCounterPreload[pulseOutput])
  3160. {
  3161. blockCount -= PlsrFiniteCounterPreload[pulseOutput];
  3162. }
  3163. completed = PlsrFiniteTargetPulses[pulseOutput]
  3164. - PlsrFiniteRemainingPulses[pulseOutput]
  3165. + blockCount;
  3166. if (completed > PlsrFiniteTargetPulses[pulseOutput])
  3167. {
  3168. completed = PlsrFiniteTargetPulses[pulseOutput];
  3169. }
  3170. }
  3171. else
  3172. {
  3173. PlsrPlatformExitCritical(criticalState);
  3174. return 0U;
  3175. }
  3176. if (PlsrFiniteStepCount[pulseOutput] != 0U)
  3177. {
  3178. completed += PlsrFiniteSteps[pulseOutput][
  3179. PlsrFiniteStepIndex[pulseOutput]].segmentPulseOffset;
  3180. }
  3181. *completedPulses = completed;
  3182. PlsrPlatformExitCritical(criticalState);
  3183. return 1U;
  3184. }
  3185. uint8_t PlsrPlatformTakeFiniteCompletion(uint8_t pulseOutput,
  3186. uint32_t *completedPulses)
  3187. {
  3188. uint8_t counterIndex;
  3189. TIM_TypeDef *counter;
  3190. if ((pulseOutput > 3U) || (completedPulses == NULL)
  3191. || (PlsrFiniteCompletionPending[pulseOutput] == 0U))
  3192. {
  3193. return 0U;
  3194. }
  3195. counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  3196. if (PlsrFiniteStepCount[pulseOutput] != 0U)
  3197. {
  3198. const PLSR_PLATFORM_FINITE_STEP *step =
  3199. &PlsrFiniteSteps[pulseOutput][
  3200. PlsrFiniteStepCount[pulseOutput] - 1U];
  3201. *completedPulses = step->segmentPulseOffset + step->pulseCount;
  3202. }
  3203. else
  3204. {
  3205. *completedPulses = PlsrFiniteTargetPulses[pulseOutput];
  3206. }
  3207. if ((PlsrFiniteStepCount[pulseOutput] == 0U)
  3208. && (PlsrFiniteStreamActive[pulseOutput] == 0U))
  3209. {
  3210. PlsrObservedPulseBase[pulseOutput] += *completedPulses;
  3211. }
  3212. PlsrObservedPulsePublished[pulseOutput] =
  3213. PlsrObservedPulseBase[pulseOutput];
  3214. PlsrFiniteCompletionPending[pulseOutput] = 0U;
  3215. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  3216. PlsrFiniteTargetPulses[pulseOutput] = 0UL;
  3217. PlsrFiniteRemainingPulses[pulseOutput] = 0UL;
  3218. PlsrFiniteCounterPreload[pulseOutput] = 0U;
  3219. PlsrFiniteStepCount[pulseOutput] = 0U;
  3220. PlsrFiniteStepIndex[pulseOutput] = 0U;
  3221. PlsrFiniteStreamActive[pulseOutput] = 0U;
  3222. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  3223. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  3224. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  3225. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  3226. PlsrTimerActiveFrequencyHz[pulseOutput] = 0UL;
  3227. PlsrTimerQueuedFrequencyHz[pulseOutput] = 0UL;
  3228. PlsrTimerRunning[pulseOutput] = 0U;
  3229. if (counterIndex < PLSR_COUNTER_COUNT)
  3230. {
  3231. counter = PlsrCounters[counterIndex];
  3232. counter->CR1 = 0UL;
  3233. counter->DIER = 0UL;
  3234. counter->SMCR = 0UL;
  3235. counter->SR = 0UL;
  3236. PlsrCounterOverflowPulses[counterIndex] = 0UL;
  3237. PlsrCounterOwner[counterIndex] = PLSR_COUNTER_NONE;
  3238. }
  3239. PlsrCounterIndexByOutput[pulseOutput] = PLSR_COUNTER_NONE;
  3240. return 1U;
  3241. }
  3242. uint8_t PlsrPlatformTakeFiniteBoundary(uint8_t pulseOutput,
  3243. uint8_t *segmentNumber,
  3244. uint32_t *completedPulses,
  3245. uint8_t *sequenceContinues,
  3246. uint32_t *activeFrequencyHz)
  3247. {
  3248. uint32_t criticalState;
  3249. uint16_t readIndex;
  3250. uint16_t completedCount;
  3251. if ((pulseOutput > 3U) || (segmentNumber == NULL)
  3252. || (completedPulses == NULL) || (sequenceContinues == NULL)
  3253. || (activeFrequencyHz == NULL))
  3254. {
  3255. return 0U;
  3256. }
  3257. criticalState = PlsrPlatformEnterCritical();
  3258. readIndex = PlsrFiniteBoundaryReadIndex[pulseOutput];
  3259. completedCount = PlsrFiniteCompletedStepCount[pulseOutput];
  3260. while (readIndex < completedCount)
  3261. {
  3262. uint16_t index = readIndex++;
  3263. const PLSR_PLATFORM_FINITE_STEP *step =
  3264. &PlsrFiniteSteps[pulseOutput][index];
  3265. PlsrFiniteBoundaryReadIndex[pulseOutput] = readIndex;
  3266. if (step->completesSegment != 0U)
  3267. {
  3268. *segmentNumber = step->segmentNumber;
  3269. *completedPulses = step->segmentPulseOffset + step->pulseCount;
  3270. *sequenceContinues =
  3271. (index + 1U < PlsrFiniteStepCount[pulseOutput]) ? 1U : 0U;
  3272. *activeFrequencyHz = (*sequenceContinues != 0U)
  3273. ? PlsrFiniteSteps[pulseOutput][index + 1U]
  3274. .setting.actualFrequencyHz
  3275. : step->setting.actualFrequencyHz;
  3276. PlsrPlatformExitCritical(criticalState);
  3277. return 1U;
  3278. }
  3279. }
  3280. PlsrPlatformExitCritical(criticalState);
  3281. return 0U;
  3282. }
  3283. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformLoadPreparedFromIrq(
  3284. uint8_t pulseOutput,
  3285. const PLSR_PLATFORM_TIMER_SETTING *setting,
  3286. uint32_t *actualFrequencyHz)
  3287. {
  3288. uint8_t outputMode;
  3289. if ((pulseOutput > 3U) || (actualFrequencyHz == NULL))
  3290. {
  3291. return PLSR_PLATFORM_QUEUE_FAILED;
  3292. }
  3293. outputMode = PlsrTimerOutputMode[pulseOutput];
  3294. if (PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  3295. setting) == 0U)
  3296. {
  3297. return PLSR_PLATFORM_QUEUE_FAILED;
  3298. }
  3299. if ((PlsrTimerRunning[pulseOutput] == 0U)
  3300. || (PlsrAbStopPending[pulseOutput] != 0U)
  3301. || (PlsrAbFastGated[pulseOutput] != 0U))
  3302. {
  3303. return PLSR_PLATFORM_QUEUE_STALE;
  3304. }
  3305. if (outputMode == PLSR_OUTPUT_AB)
  3306. {
  3307. PLSR_AB_SETTING pending;
  3308. PlsrPlatformToAbSetting(setting, &pending);
  3309. PlsrAbPendingSetting[pulseOutput] = pending;
  3310. PlsrAbFrequencyPending[pulseOutput] =
  3311. ((setting->prescaler
  3312. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  3313. || (setting->pairPrescaler
  3314. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  3315. || (setting->period
  3316. != PlsrAbActiveSetting[pulseOutput].period)) ? 1U : 0U;
  3317. }
  3318. else
  3319. {
  3320. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  3321. if ((timer->CR1 & TIM_CR1_CEN) == 0UL)
  3322. {
  3323. return PLSR_PLATFORM_QUEUE_STALE;
  3324. }
  3325. timer->PSC = setting->prescaler;
  3326. timer->ARR = setting->period;
  3327. timer->CCR1 = setting->compare;
  3328. __DMB();
  3329. }
  3330. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  3331. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  3332. PlsrTimerQueueGeneration[pulseOutput]++;
  3333. *actualFrequencyHz = setting->actualFrequencyHz;
  3334. return PLSR_PLATFORM_QUEUE_APPLIED;
  3335. }
  3336. void PlsrPlatformGateFromIrq(uint8_t pulseOutput)
  3337. {
  3338. if ((pulseOutput <= 3U)
  3339. && (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3340. && (PlsrAbFastGated[pulseOutput] != 0U))
  3341. {
  3342. return;
  3343. }
  3344. PlsrPlatformStopPulse(pulseOutput);
  3345. }
  3346. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformQueueFrequency(
  3347. uint8_t pulseOutput,
  3348. uint32_t frequencyHz,
  3349. uint32_t *actualFrequencyHz)
  3350. {
  3351. TIM_TypeDef *timer;
  3352. PLSR_PLATFORM_TIMER_SETTING setting;
  3353. uint32_t activePeriod;
  3354. uint32_t counter;
  3355. uint32_t criticalState;
  3356. uint32_t ownGeneration;
  3357. uint8_t updatePending;
  3358. if ((pulseOutput > 3U) || (actualFrequencyHz == NULL)
  3359. || (PlsrPlatformBuildTimerSetting(
  3360. pulseOutput, PlsrTimerOutputMode[pulseOutput], frequencyHz,
  3361. &setting) == 0U))
  3362. {
  3363. return PLSR_PLATFORM_QUEUE_FAILED;
  3364. }
  3365. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3366. {
  3367. PLSR_AB_SETTING pending;
  3368. uint32_t criticalState;
  3369. if ((PlsrTimerRunning[pulseOutput] == 0U)
  3370. || (PlsrAbStopPending[pulseOutput] != 0U)
  3371. || (PlsrAbFastGated[pulseOutput] != 0U))
  3372. {
  3373. return PLSR_PLATFORM_QUEUE_STALE;
  3374. }
  3375. criticalState = PlsrPlatformEnterCritical();
  3376. if ((PlsrTimerRunning[pulseOutput] == 0U)
  3377. || (PlsrAbStopPending[pulseOutput] != 0U)
  3378. || (PlsrAbFastGated[pulseOutput] != 0U))
  3379. {
  3380. PlsrPlatformExitCritical(criticalState);
  3381. return PLSR_PLATFORM_QUEUE_STALE;
  3382. }
  3383. PlsrPlatformToAbSetting(&setting, &pending);
  3384. PlsrAbPendingSetting[pulseOutput] = pending;
  3385. PlsrAbFrequencyPending[pulseOutput] =
  3386. ((pending.basePrescaler
  3387. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  3388. || (pending.pairPrescaler
  3389. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  3390. || (pending.period
  3391. != PlsrAbActiveSetting[pulseOutput].period)) ? 1U : 0U;
  3392. PlsrTimerQueuedSetting[pulseOutput] = setting;
  3393. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting.actualFrequencyHz;
  3394. PlsrTimerQueueGeneration[pulseOutput]++;
  3395. *actualFrequencyHz = setting.actualFrequencyHz;
  3396. PlsrPlatformExitCritical(criticalState);
  3397. return PLSR_PLATFORM_QUEUE_APPLIED;
  3398. }
  3399. timer = PlsrTimerMap[pulseOutput].timer;
  3400. if ((timer->CR1 & TIM_CR1_CEN) == 0UL)
  3401. {
  3402. return PLSR_PLATFORM_QUEUE_STALE;
  3403. }
  3404. criticalState = PlsrPlatformEnterCritical();
  3405. if ((timer->SR & TIM_SR_UIF) != 0UL)
  3406. {
  3407. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  3408. PlsrPlatformExitCritical(criticalState);
  3409. return PLSR_PLATFORM_QUEUE_STALE;
  3410. }
  3411. activePeriod = PlsrTimerActiveSetting[pulseOutput].period;
  3412. counter = timer->CNT;
  3413. if ((counter > activePeriod)
  3414. || ((activePeriod - counter) < PLSR_QUEUE_WRITE_GUARD_COUNTS))
  3415. {
  3416. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  3417. PlsrPlatformExitCritical(criticalState);
  3418. return PLSR_PLATFORM_QUEUE_STALE;
  3419. }
  3420. /* Preserve real update events and their TRGO pulse while replacing the
  3421. three preload registers. The near-wrap guard bounds the write window. */
  3422. timer->PSC = setting.prescaler;
  3423. timer->ARR = setting.period;
  3424. timer->CCR1 = setting.compare;
  3425. __DMB();
  3426. updatePending = ((timer->SR & TIM_SR_UIF) != 0UL) ? 1U : 0U;
  3427. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting.actualFrequencyHz;
  3428. PlsrTimerQueuedSetting[pulseOutput] = setting;
  3429. PlsrTimerQueueGeneration[pulseOutput]++;
  3430. ownGeneration = PlsrTimerQueueGeneration[pulseOutput];
  3431. if (updatePending != 0U)
  3432. {
  3433. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  3434. PlsrPlatformExitCritical(criticalState);
  3435. return PLSR_PLATFORM_QUEUE_STALE;
  3436. }
  3437. *actualFrequencyHz =
  3438. (PlsrTimerQueueGeneration[pulseOutput] == ownGeneration)
  3439. ? setting.actualFrequencyHz
  3440. : PlsrTimerQueuedFrequencyHz[pulseOutput];
  3441. if (PlsrTimerQueueGeneration[pulseOutput] != ownGeneration)
  3442. {
  3443. PlsrPlatformExitCritical(criticalState);
  3444. return PLSR_PLATFORM_QUEUE_STALE;
  3445. }
  3446. PlsrPlatformExitCritical(criticalState);
  3447. return PLSR_PLATFORM_QUEUE_APPLIED;
  3448. }
  3449. void PlsrPlatformDrainPendingPulse(uint8_t pulseOutput)
  3450. {
  3451. if (pulseOutput <= 3U)
  3452. {
  3453. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3454. && (PlsrDeferredPulsePending[pulseOutput] != 0U))
  3455. {
  3456. uint8_t verifyOutput =
  3457. (PlsrAbLagAxis[pulseOutput] == pulseOutput)
  3458. ? (uint8_t)(pulseOutput + 1U) : pulseOutput;
  3459. TIM_TypeDef *verifyTimer = PlsrTimerMap[verifyOutput].timer;
  3460. verifyTimer->DIER &= ~(TIM_DIER_UIE | TIM_DIER_CC1IE);
  3461. verifyTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3462. PlsrAbVerifyOwner[verifyOutput] = PLSR_COUNTER_NONE;
  3463. if (PlsrFrequencyVerifyPending[pulseOutput]
  3464. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  3465. {
  3466. PlsrFrequencyVerifyPending[pulseOutput] =
  3467. PLSR_FREQUENCY_VERIFY_NONE;
  3468. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3469. (void)PlsrVerifyActiveFrequency(pulseOutput);
  3470. }
  3471. PlsrDeferredPulsePending[pulseOutput] = 0U;
  3472. PlsrPulseTimerIrq(pulseOutput);
  3473. return;
  3474. }
  3475. PlsrHandleTimerIrq(pulseOutput);
  3476. }
  3477. }
  3478. uint32_t PlsrPlatformActiveFrequency(uint8_t pulseOutput)
  3479. {
  3480. if (pulseOutput > 3U)
  3481. {
  3482. return 0UL;
  3483. }
  3484. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3485. && (PlsrAbFastGated[pulseOutput] != 0U))
  3486. {
  3487. PlsrFrequencyVerifyPending[pulseOutput] =
  3488. PLSR_FREQUENCY_VERIFY_NONE;
  3489. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3490. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3491. }
  3492. if (PlsrFrequencyVerifyPending[pulseOutput]
  3493. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  3494. {
  3495. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3496. }
  3497. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3498. {
  3499. PlsrFrequencyVerifyPulseCount[pulseOutput]++;
  3500. if (PlsrFrequencyVerifyPulseCount[pulseOutput]
  3501. >= PLSR_STRUCTURE_VERIFY_INTERVAL)
  3502. {
  3503. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3504. PlsrAbScheduleFrequencyVerify(pulseOutput);
  3505. }
  3506. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3507. }
  3508. if (PlsrFrequencyVerifyPending[pulseOutput]
  3509. == PLSR_FREQUENCY_VERIFY_NOW)
  3510. {
  3511. PlsrFrequencyVerifyPending[pulseOutput] =
  3512. PLSR_FREQUENCY_VERIFY_NONE;
  3513. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3514. return PlsrVerifyActiveFrequency(pulseOutput);
  3515. }
  3516. PlsrFrequencyVerifyPulseCount[pulseOutput]++;
  3517. if (PlsrFrequencyVerifyPulseCount[pulseOutput]
  3518. >= PLSR_STRUCTURE_VERIFY_INTERVAL)
  3519. {
  3520. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3521. return PlsrVerifyActiveFrequency(pulseOutput);
  3522. }
  3523. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3524. }
  3525. uint8_t PlsrPlatformExpectedFrequency(uint8_t pulseOutput,
  3526. uint8_t outputMode,
  3527. uint32_t requestedFrequencyHz,
  3528. uint32_t *actualFrequencyHz)
  3529. {
  3530. PLSR_PLATFORM_TIMER_SETTING setting;
  3531. if (actualFrequencyHz == NULL)
  3532. {
  3533. return 0U;
  3534. }
  3535. if (PlsrPlatformBuildTimerSetting(pulseOutput, outputMode,
  3536. requestedFrequencyHz,
  3537. &setting) == 0U)
  3538. {
  3539. return 0U;
  3540. }
  3541. *actualFrequencyHz = setting.actualFrequencyHz;
  3542. return 1U;
  3543. }
  3544. uint64_t PlsrPlatformObservedPulses(uint8_t pulseOutput)
  3545. {
  3546. uint32_t criticalState;
  3547. uint64_t observed;
  3548. if (pulseOutput > 3U)
  3549. {
  3550. return 0UL;
  3551. }
  3552. criticalState = PlsrPlatformEnterCritical();
  3553. if (PlsrCounterIndexByOutput[pulseOutput] < PLSR_COUNTER_COUNT)
  3554. {
  3555. observed = ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3556. && (PlsrAbFastGated[pulseOutput] != 0U))
  3557. ? PlsrCounterSnapshotStopped(pulseOutput)
  3558. : PlsrCounterSnapshot(pulseOutput);
  3559. }
  3560. else
  3561. {
  3562. observed = PlsrObservedPulseBase[pulseOutput];
  3563. }
  3564. PlsrPlatformExitCritical(criticalState);
  3565. return observed;
  3566. }
  3567. uint16_t PlsrPlatformDiagnosticFault(void)
  3568. {
  3569. uint16_t fault = PlsrPlatformFaultPending;
  3570. PlsrPlatformFaultPending = 0U;
  3571. return fault;
  3572. }
  3573. PLSR_PLATFORM_STOP_RESULT PlsrPlatformRequestStopLocked(
  3574. uint8_t pulseOutput,
  3575. uint8_t requireZeroBoundary)
  3576. {
  3577. if (pulseOutput > 3U)
  3578. {
  3579. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  3580. }
  3581. if ((requireZeroBoundary != 0U)
  3582. && (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3583. && (PlsrTimerRunning[pulseOutput] != 0U)
  3584. && (PlsrAbFastGated[pulseOutput] == 0U))
  3585. {
  3586. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  3587. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  3588. uint32_t activeFrequencyHz;
  3589. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  3590. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  3591. uint8_t structureRunnable =
  3592. ((PlsrAbStructureVerified[pulseOutput] != 0U)
  3593. && (PlsrFrequencyVerifyPending[pulseOutput]
  3594. == PLSR_FREQUENCY_VERIFY_NONE))
  3595. ? PlsrAbStopBoundaryIsReachable(pulseOutput, baseTimer,
  3596. pairTimer)
  3597. : PlsrAbTimersAreRunnable(pulseOutput, baseTimer,
  3598. pairTimer);
  3599. if (structureRunnable == 0U)
  3600. {
  3601. PlsrCounterSuspend(pulseOutput);
  3602. PlsrAbHoldPairIdle(pulseOutput);
  3603. baseTimer->CR1 &= ~TIM_CR1_CEN;
  3604. pairTimer->CR1 &= ~TIM_CR1_CEN;
  3605. __DMB();
  3606. PlsrPlatformStopPulse(pulseOutput);
  3607. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  3608. }
  3609. if (PlsrAbStopPending[pulseOutput] != 0U)
  3610. {
  3611. return PLSR_PLATFORM_STOP_PENDING;
  3612. }
  3613. PlsrAbFrequencyPending[pulseOutput] = 0U;
  3614. baseTimer->DIER &= ~TIM_DIER_UIE;
  3615. pairTimer->DIER &= ~TIM_DIER_UIE;
  3616. PlsrFrequencyVerifyPending[pulseOutput] =
  3617. PLSR_FREQUENCY_VERIFY_NONE;
  3618. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3619. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  3620. PlsrAbVerifyOwner[pairOutput] = PLSR_COUNTER_NONE;
  3621. activeFrequencyHz = PlsrTimerActiveFrequencyHz[pulseOutput];
  3622. PlsrTimerQueuedFrequencyHz[pulseOutput] = activeFrequencyHz;
  3623. PlsrTimerQueueGeneration[pulseOutput]++;
  3624. PlsrAbStopPending[pulseOutput] = 1U;
  3625. #if PLSR_DEBUG_TIMING
  3626. if (PlsrFinalArmQueueTimingPending[pulseOutput] != 0U)
  3627. {
  3628. uint32_t queuedAt = PlsrFinalArmQueuedAt[pulseOutput];
  3629. uint32_t stoppedAt = DWT->CYCCNT;
  3630. uint32_t latency = stoppedAt - queuedAt;
  3631. PlsrFinalArmQueueTimingPending[pulseOutput] = 0U;
  3632. PlsrFinalArmQueueToStopLastCycles[pulseOutput] = latency;
  3633. if (latency > PlsrFinalArmQueueToStopMaxCycles[pulseOutput])
  3634. {
  3635. PlsrFinalArmQueueToStopMaxCycles[pulseOutput] = latency;
  3636. }
  3637. }
  3638. #endif
  3639. if (lagOutput == pulseOutput)
  3640. {
  3641. pairTimer->SR = ~TIM_SR_CC1IF;
  3642. }
  3643. else
  3644. {
  3645. baseTimer->SR = ~TIM_SR_CC1IF;
  3646. }
  3647. baseTimer->DIER |= TIM_DIER_CC1IE;
  3648. pairTimer->DIER |= TIM_DIER_CC1IE;
  3649. __DMB();
  3650. return PLSR_PLATFORM_STOP_PENDING;
  3651. }
  3652. PlsrPlatformStopPulse(pulseOutput);
  3653. return PLSR_PLATFORM_STOP_COMPLETE;
  3654. }
  3655. uint8_t PlsrPlatformQueueFinalArmFromIrq(uint8_t pulseOutput)
  3656. {
  3657. uint8_t jobOutput;
  3658. if ((pulseOutput > 2U) || ((pulseOutput & 1U) != 0U)
  3659. || (PlsrTimerOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  3660. || (PlsrTimerRunning[pulseOutput] == 0U)
  3661. || (PlsrAbFastGated[pulseOutput] != 0U))
  3662. {
  3663. return 0U;
  3664. }
  3665. jobOutput = PlsrFinalArmJobOutput(pulseOutput);
  3666. if (PlsrAbFinalArmJobOwner[jobOutput] != PLSR_COUNTER_NONE)
  3667. {
  3668. return 0U;
  3669. }
  3670. PlsrAbFinalArmJobOwner[jobOutput] = pulseOutput;
  3671. #if PLSR_DEBUG_TIMING
  3672. PlsrFinalArmQueuedAt[pulseOutput] = DWT->CYCCNT;
  3673. PlsrFinalArmQueueTimingPending[pulseOutput] = 1U;
  3674. PlsrFinalArmQueueCount[pulseOutput]++;
  3675. #endif
  3676. __DMB();
  3677. NVIC_SetPendingIRQ(PlsrTimerMap[jobOutput].irq);
  3678. return 1U;
  3679. }
  3680. void PlsrPlatformStopPulse(uint8_t pulseOutput)
  3681. {
  3682. if (pulseOutput <= 3U)
  3683. {
  3684. PlsrFiniteActive[pulseOutput] = 0U;
  3685. PlsrFiniteCompletionPending[pulseOutput] = 0U;
  3686. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  3687. PlsrFiniteRetargetPending[pulseOutput] = 0U;
  3688. PlsrFiniteTailStopPending[pulseOutput] = 0U;
  3689. PlsrFiniteRetargetDrainPulses[pulseOutput] = 0UL;
  3690. PlsrFiniteTargetPulses[pulseOutput] = 0UL;
  3691. PlsrFiniteRemainingPulses[pulseOutput] = 0UL;
  3692. PlsrFiniteStreamActive[pulseOutput] = 0U;
  3693. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  3694. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  3695. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  3696. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  3697. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3698. {
  3699. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  3700. uint8_t jobOutput = PlsrFinalArmJobOutput(pulseOutput);
  3701. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  3702. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  3703. PlsrAbStructureVerified[pulseOutput] = 0U;
  3704. PlsrAbFinalArmJobOwner[jobOutput] = PLSR_COUNTER_NONE;
  3705. #if PLSR_DEBUG_TIMING
  3706. PlsrFinalArmQueueTimingPending[pulseOutput] = 0U;
  3707. #endif
  3708. NVIC_ClearPendingIRQ(PlsrTimerMap[jobOutput].irq);
  3709. if (PlsrAbFastGated[pulseOutput] != 0U)
  3710. {
  3711. PlsrAbHoldPairIdle(pulseOutput);
  3712. baseTimer->DIER = 0UL;
  3713. pairTimer->DIER = 0UL;
  3714. baseTimer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3715. pairTimer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3716. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3717. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3718. }
  3719. else
  3720. {
  3721. PlsrCounterSuspend(pulseOutput);
  3722. PlsrAbHoldPairIdle(pulseOutput);
  3723. PlsrTimerStop(baseTimer);
  3724. PlsrTimerStop(pairTimer);
  3725. }
  3726. }
  3727. else
  3728. {
  3729. PlsrPulsePinCaptureIdle(pulseOutput);
  3730. PlsrTimerStop(PlsrTimerMap[pulseOutput].timer);
  3731. }
  3732. PlsrCounterStop(pulseOutput);
  3733. PlsrTimerActiveFrequencyHz[pulseOutput] = 0UL;
  3734. PlsrTimerQueuedFrequencyHz[pulseOutput] = 0UL;
  3735. PlsrTimerQueueGeneration[pulseOutput]++;
  3736. PlsrTimerRunning[pulseOutput] = 0U;
  3737. PlsrFrequencyVerifyPending[pulseOutput] = 0U;
  3738. PlsrDeferredPulsePending[pulseOutput] = 0U;
  3739. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  3740. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3741. {
  3742. PlsrAbVerifyOwner[pulseOutput + 1U] = PLSR_COUNTER_NONE;
  3743. }
  3744. PlsrAbFrequencyPending[pulseOutput] = 0U;
  3745. PlsrAbStopPending[pulseOutput] = 0U;
  3746. PlsrAbFastGated[pulseOutput] = 0U;
  3747. }
  3748. }
  3749. void PlsrPlatformForceSafeOutputsFromFault(void)
  3750. {
  3751. uint32_t mode;
  3752. const uint32_t outputPins = GPIO_PIN_6 | GPIO_PIN_7
  3753. | GPIO_PIN_8 | GPIO_PIN_9;
  3754. const uint32_t outputModeMask = (3UL << (6U * 2U))
  3755. | (3UL << (7U * 2U))
  3756. | (3UL << (8U * 2U))
  3757. | (3UL << (9U * 2U));
  3758. __disable_irq();
  3759. TIM10->DIER = 0UL;
  3760. TIM10->CR1 &= ~TIM_CR1_CEN;
  3761. TIM10->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3762. TIM10->SR = 0UL;
  3763. TIM11->DIER = 0UL;
  3764. TIM11->CR1 &= ~TIM_CR1_CEN;
  3765. TIM11->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3766. TIM11->SR = 0UL;
  3767. TIM13->DIER = 0UL;
  3768. TIM13->CR1 &= ~TIM_CR1_CEN;
  3769. TIM13->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3770. TIM13->SR = 0UL;
  3771. TIM14->DIER = 0UL;
  3772. TIM14->CR1 &= ~TIM_CR1_CEN;
  3773. TIM14->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3774. TIM14->SR = 0UL;
  3775. TIM9->DIER = 0UL;
  3776. TIM9->CR1 &= ~TIM_CR1_CEN;
  3777. TIM9->SMCR = 0UL;
  3778. TIM9->SR = 0UL;
  3779. TIM12->DIER = 0UL;
  3780. TIM12->CR1 &= ~TIM_CR1_CEN;
  3781. TIM12->SMCR = 0UL;
  3782. TIM12->SR = 0UL;
  3783. RCC->AHB1ENR |= RCC_AHB1ENR_GPIOFEN | RCC_AHB1ENR_GPIOHEN;
  3784. __DSB();
  3785. GPIOF->BSRR = outputPins;
  3786. GPIOH->BSRR = outputPins;
  3787. GPIOF->OTYPER &= ~outputPins;
  3788. GPIOH->OTYPER &= ~outputPins;
  3789. GPIOF->PUPDR &= ~outputModeMask;
  3790. GPIOH->PUPDR &= ~outputModeMask;
  3791. mode = GPIOF->MODER;
  3792. mode &= ~outputModeMask;
  3793. mode |= (1UL << (6U * 2U)) | (1UL << (7U * 2U))
  3794. | (1UL << (8U * 2U)) | (1UL << (9U * 2U));
  3795. GPIOF->MODER = mode;
  3796. mode = GPIOH->MODER;
  3797. mode &= ~outputModeMask;
  3798. mode |= (1UL << (6U * 2U)) | (1UL << (7U * 2U))
  3799. | (1UL << (8U * 2U)) | (1UL << (9U * 2U));
  3800. GPIOH->MODER = mode;
  3801. __DSB();
  3802. }
  3803. uint8_t PlsrPlatformReadInput(uint8_t inputSelection)
  3804. {
  3805. if (inputSelection == 0U)
  3806. {
  3807. return (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET) ? 1U : 0U;
  3808. }
  3809. if (inputSelection == 1U)
  3810. {
  3811. return (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_12) == GPIO_PIN_SET) ? 1U : 0U;
  3812. }
  3813. return 0U;
  3814. }
  3815. uint8_t PlsrPlatformLoad(PLSR_PERSIST_PAYLOAD *payload)
  3816. {
  3817. PLSR_FLASH_SECTOR_SCAN scanA;
  3818. PLSR_FLASH_SECTOR_SCAN scanB;
  3819. const PLSR_FLASH_HEADER *selected = NULL;
  3820. const void *backupConfig = (const void *)PLSR_BACKUP_CONFIG_ADDRESS;
  3821. const PLSR_BACKUP_POSITION_RECORD *backupPosition;
  3822. uint8_t selectedVersion = 0U;
  3823. uint8_t backupVersion;
  3824. uint8_t haveConfig = 0U;
  3825. uint32_t selectedSectorAddress;
  3826. if (payload == NULL)
  3827. {
  3828. return 0U;
  3829. }
  3830. selected = PlsrFlashInitializeJournal(&scanA, &scanB, &selectedVersion,
  3831. &selectedSectorAddress);
  3832. if (selected != NULL)
  3833. {
  3834. if (selectedVersion == PLSR_FLASH_VERSION)
  3835. {
  3836. *payload = ((const PLSR_FLASH_RECORD *)selected)->payload;
  3837. }
  3838. else
  3839. {
  3840. PlsrLoadV2Payload(
  3841. payload,
  3842. &((const PLSR_FLASH_RECORD_V2 *)selected)->payload);
  3843. }
  3844. haveConfig = 1U;
  3845. }
  3846. else
  3847. {
  3848. (void)memset(payload, 0, sizeof(*payload));
  3849. }
  3850. backupVersion = PlsrBackupConfigVersion(backupConfig);
  3851. if (backupVersion == PLSR_FLASH_VERSION)
  3852. {
  3853. payload->config =
  3854. ((const PLSR_BACKUP_CONFIG_RECORD *)backupConfig)->config;
  3855. haveConfig = 1U;
  3856. }
  3857. else if (backupVersion == PLSR_FLASH_VERSION_V2)
  3858. {
  3859. const PLSR_BACKUP_CONFIG_RECORD_V2 *oldConfig =
  3860. (const PLSR_BACKUP_CONFIG_RECORD_V2 *)backupConfig;
  3861. (void)memcpy(&payload->config, oldConfig->config,
  3862. sizeof(oldConfig->config));
  3863. payload->config.outputMode = PLSR_OUTPUT_PULSE_DIR;
  3864. haveConfig = 1U;
  3865. }
  3866. backupPosition = PlsrNewestBackupPosition();
  3867. if (backupPosition != NULL)
  3868. {
  3869. payload->position = backupPosition->position;
  3870. payload->positionValid = backupPosition->positionValid;
  3871. payload->wasBusy = backupPosition->wasBusy;
  3872. }
  3873. if (selected != NULL)
  3874. {
  3875. uint8_t reserveIndex =
  3876. (selectedSectorAddress == PLSR_FLASH_SLOT_A_ADDRESS) ? 1U : 0U;
  3877. const PLSR_FLASH_SECTOR_SCAN *reserveScan =
  3878. (reserveIndex == 0U) ? &scanA : &scanB;
  3879. if (reserveScan->hasProgrammedSlot != 0U)
  3880. {
  3881. PlsrFlashReserveEraseState = (uint8_t)(reserveIndex + 1U);
  3882. }
  3883. }
  3884. else if (PlsrFlashNeedsStartupRecovery(
  3885. 0U,
  3886. scanA.hasProgrammedSlot,
  3887. scanA.firstErasedAddress,
  3888. scanB.hasProgrammedSlot,
  3889. scanB.firstErasedAddress) != 0U)
  3890. {
  3891. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_SECTOR_A;
  3892. }
  3893. return haveConfig;
  3894. }
  3895. PLSR_PLATFORM_SERVICE_RESULT PlsrPlatformServicePersistence(void)
  3896. {
  3897. uint32_t criticalState;
  3898. uint8_t eraseState;
  3899. uint8_t sectorIndex;
  3900. uint8_t index;
  3901. criticalState = PlsrPlatformEnterCritical();
  3902. eraseState = PlsrFlashReserveEraseState;
  3903. if (eraseState == PLSR_FLASH_ERASE_FAILED)
  3904. {
  3905. PlsrPlatformExitCritical(criticalState);
  3906. return PLSR_PLATFORM_SERVICE_FAILED;
  3907. }
  3908. if (eraseState == PLSR_FLASH_ERASE_NONE)
  3909. {
  3910. PlsrPlatformExitCritical(criticalState);
  3911. return PLSR_PLATFORM_SERVICE_READY;
  3912. }
  3913. if ((eraseState < PLSR_FLASH_ERASE_SECTOR_A)
  3914. || (eraseState > PLSR_FLASH_ERASE_SECTOR_B))
  3915. {
  3916. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_FAILED;
  3917. PlsrPlatformExitCritical(criticalState);
  3918. return PLSR_PLATFORM_SERVICE_FAILED;
  3919. }
  3920. for (index = 0U; index < 4U; index++)
  3921. {
  3922. if ((PlsrTimerRunning[index] != 0U)
  3923. || (PlsrTimerIrqActive[index] != 0U))
  3924. {
  3925. PlsrPlatformExitCritical(criticalState);
  3926. return PLSR_PLATFORM_SERVICE_DEFERRED;
  3927. }
  3928. }
  3929. sectorIndex = (uint8_t)(eraseState - PLSR_FLASH_ERASE_SECTOR_A);
  3930. PlsrPlatformExitCritical(criticalState);
  3931. if (PlsrFlashEraseReserve(sectorIndex) == 0U)
  3932. {
  3933. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_FAILED;
  3934. return PLSR_PLATFORM_SERVICE_FAILED;
  3935. }
  3936. PlsrFlashNextErasedAddress[sectorIndex] =
  3937. (sectorIndex == 0U) ? PLSR_FLASH_SLOT_A_ADDRESS
  3938. : PLSR_FLASH_SLOT_B_ADDRESS;
  3939. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_NONE;
  3940. return PLSR_PLATFORM_SERVICE_READY;
  3941. }
  3942. uint8_t PlsrPlatformSave(const PLSR_PERSIST_PAYLOAD *payload)
  3943. {
  3944. PLSR_FLASH_SECTOR_SCAN scanA;
  3945. PLSR_FLASH_SECTOR_SCAN scanB;
  3946. uint8_t newestVersion;
  3947. uint32_t newestSectorAddress;
  3948. uint8_t targetIndex;
  3949. uint32_t targetAddress;
  3950. uint32_t index;
  3951. uint32_t wordCount;
  3952. const uint32_t *words;
  3953. HAL_StatusTypeDef status = HAL_OK;
  3954. if (payload == NULL)
  3955. {
  3956. return 0U;
  3957. }
  3958. if (PlsrFlashJournalInitialized == 0U)
  3959. {
  3960. (void)PlsrFlashInitializeJournal(&scanA, &scanB, &newestVersion,
  3961. &newestSectorAddress);
  3962. (void)newestVersion;
  3963. (void)newestSectorAddress;
  3964. }
  3965. if (PlsrFlashNewestAddress != 0UL)
  3966. {
  3967. targetIndex = PlsrFlashSectorIndex(PlsrFlashNewestAddress);
  3968. if (PlsrFlashNextErasedAddress[targetIndex] == 0UL)
  3969. {
  3970. targetIndex ^= 1U;
  3971. }
  3972. }
  3973. else if (PlsrFlashNextErasedAddress[0] != 0UL)
  3974. {
  3975. targetIndex = 0U;
  3976. }
  3977. else
  3978. {
  3979. targetIndex = 1U;
  3980. }
  3981. targetAddress = PlsrFlashNextErasedAddress[targetIndex];
  3982. if ((targetAddress != 0UL)
  3983. && ((PlsrFlashAddressIsJournalSlot(targetIndex, targetAddress) == 0U)
  3984. || (PlsrFlashSlotIsErased(targetAddress) == 0U)))
  3985. {
  3986. targetAddress = 0UL;
  3987. PlsrFlashNextErasedAddress[targetIndex] = 0UL;
  3988. }
  3989. if (targetAddress == 0UL)
  3990. {
  3991. uint8_t newestIndex = (PlsrFlashNewestAddress != 0UL)
  3992. ? PlsrFlashSectorIndex(
  3993. PlsrFlashNewestAddress)
  3994. : 0xFFU;
  3995. uint8_t alternateIndex = targetIndex ^ 1U;
  3996. if ((targetIndex == newestIndex)
  3997. || (PlsrFlashNextErasedAddress[alternateIndex] != 0UL))
  3998. {
  3999. targetIndex = alternateIndex;
  4000. targetAddress = PlsrFlashNextErasedAddress[targetIndex];
  4001. if ((targetAddress != 0UL)
  4002. && ((PlsrFlashAddressIsJournalSlot(targetIndex,
  4003. targetAddress) == 0U)
  4004. || (PlsrFlashSlotIsErased(targetAddress) == 0U)))
  4005. {
  4006. targetAddress = 0UL;
  4007. PlsrFlashNextErasedAddress[targetIndex] = 0UL;
  4008. }
  4009. }
  4010. if (targetAddress == 0UL)
  4011. {
  4012. return 0U;
  4013. }
  4014. }
  4015. PlsrFlashNextErasedAddress[targetIndex] =
  4016. PlsrFlashFindErasedAfter(targetIndex, targetAddress);
  4017. (void)memset(&PlsrFlashRecordBuffer, 0, sizeof(PlsrFlashRecordBuffer));
  4018. PlsrFlashRecordBuffer.magic = PLSR_FLASH_MAGIC;
  4019. PlsrFlashRecordBuffer.version = PLSR_FLASH_VERSION;
  4020. PlsrFlashRecordBuffer.payloadSize = sizeof(PLSR_PERSIST_PAYLOAD);
  4021. PlsrFlashRecordBuffer.generation = PlsrFlashNewestGeneration + 1UL;
  4022. PlsrFlashRecordBuffer.payload = *payload;
  4023. PlsrFlashRecordBuffer.crc32 =
  4024. PlsrFlashRecordCrc(&PlsrFlashRecordBuffer,
  4025. sizeof(PlsrFlashRecordBuffer.payload));
  4026. if (HAL_FLASH_Unlock() != HAL_OK)
  4027. {
  4028. (void)HAL_FLASH_Lock();
  4029. return 0U;
  4030. }
  4031. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
  4032. | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  4033. | FLASH_FLAG_PGSERR);
  4034. words = (const uint32_t *)&PlsrFlashRecordBuffer;
  4035. wordCount = sizeof(PlsrFlashRecordBuffer) / sizeof(uint32_t);
  4036. if (status == HAL_OK)
  4037. {
  4038. for (index = 1UL; index < wordCount; index++)
  4039. {
  4040. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
  4041. targetAddress + index * 4UL,
  4042. words[index]) != HAL_OK)
  4043. {
  4044. status = HAL_ERROR;
  4045. break;
  4046. }
  4047. }
  4048. }
  4049. if ((status == HAL_OK)
  4050. && (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, targetAddress,
  4051. PLSR_FLASH_MAGIC) != HAL_OK))
  4052. {
  4053. status = HAL_ERROR;
  4054. }
  4055. if (HAL_FLASH_Lock() != HAL_OK)
  4056. {
  4057. (void)HAL_FLASH_Lock();
  4058. status = HAL_ERROR;
  4059. }
  4060. if (PlsrFlashRecordVersion((const void *)targetAddress)
  4061. == PLSR_FLASH_VERSION)
  4062. {
  4063. PlsrFlashNewestAddress = targetAddress;
  4064. PlsrFlashNewestGeneration = PlsrFlashRecordBuffer.generation;
  4065. return (status == HAL_OK) ? 1U : 0U;
  4066. }
  4067. return 0U;
  4068. }
  4069. void PlsrPlatformCheckpointConfig(const PLSR_CONFIG *config)
  4070. {
  4071. PLSR_BACKUP_CONFIG_RECORD *record =
  4072. (PLSR_BACKUP_CONFIG_RECORD *)PLSR_BACKUP_CONFIG_ADDRESS;
  4073. if (config == NULL)
  4074. {
  4075. return;
  4076. }
  4077. record->magic = 0UL;
  4078. record->config = *config;
  4079. record->crc32 = PlsrCrc32(&record->config, sizeof(record->config));
  4080. __DMB();
  4081. record->magic = PLSR_BACKUP_CONFIG_MAGIC;
  4082. __DMB();
  4083. }
  4084. void PlsrPlatformCheckpointPosition(int32_t position,
  4085. uint8_t positionValid,
  4086. uint8_t wasBusy)
  4087. {
  4088. PLSR_BACKUP_POSITION_RECORD *slots =
  4089. (PLSR_BACKUP_POSITION_RECORD *)PLSR_BACKUP_POSITION_ADDRESS;
  4090. PLSR_BACKUP_POSITION_RECORD *record;
  4091. PlsrBackupPositionGeneration++;
  4092. record = &slots[PlsrBackupPositionGeneration & 1UL];
  4093. record->magic = 0UL;
  4094. record->generation = PlsrBackupPositionGeneration;
  4095. record->position = position;
  4096. record->positionValid = (positionValid != 0U) ? 1U : 0U;
  4097. record->wasBusy = (wasBusy != 0U) ? 1U : 0U;
  4098. record->reserved = 0U;
  4099. record->crc32 = PlsrCrc32(&record->generation,
  4100. sizeof(record->generation)
  4101. + sizeof(record->position)
  4102. + sizeof(record->positionValid)
  4103. + sizeof(record->wasBusy)
  4104. + sizeof(record->reserved));
  4105. __DMB();
  4106. record->magic = PLSR_BACKUP_POSITION_MAGIC;
  4107. __DMB();
  4108. }
  4109. uint32_t PlsrPlatformEnterCritical(void)
  4110. {
  4111. uint32_t state = __get_PRIMASK();
  4112. __disable_irq();
  4113. __DMB();
  4114. return state;
  4115. }
  4116. void PlsrPlatformExitCritical(uint32_t state)
  4117. {
  4118. __DMB();
  4119. if (state == 0UL)
  4120. {
  4121. __enable_irq();
  4122. }
  4123. }
  4124. static void PlsrHandleTimerIrq(uint8_t pulseOutput)
  4125. {
  4126. TIM_TypeDef *timer;
  4127. #if PLSR_DEBUG_TIMING
  4128. uint32_t startedAt;
  4129. uint32_t elapsedCycles;
  4130. uint8_t timingOutput = pulseOutput;
  4131. startedAt = DWT->CYCCNT;
  4132. #endif
  4133. timer = PlsrTimerMap[pulseOutput].timer;
  4134. if (PlsrTimerIrqActive[pulseOutput] != 0U)
  4135. {
  4136. #if PLSR_DEBUG_TIMING
  4137. goto irq_record;
  4138. #else
  4139. return;
  4140. #endif
  4141. }
  4142. PlsrTimerIrqActive[pulseOutput] = 1U;
  4143. if (((timer->SR & TIM_SR_CC1IF) != 0UL)
  4144. && ((timer->DIER & TIM_DIER_CC1IE) != 0UL))
  4145. {
  4146. uint8_t owner = ((pulseOutput & 2U) == 0U) ? 0U : 2U;
  4147. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_PULSE_DIR)
  4148. && (PlsrFiniteRetargetPending[pulseOutput] != 0U))
  4149. {
  4150. timer->SR = ~TIM_SR_CC1IF;
  4151. PlsrFiniteRetargetAtFallingEdge(pulseOutput);
  4152. goto irq_done;
  4153. }
  4154. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_PULSE_DIR)
  4155. && (PlsrFiniteTailStopPending[pulseOutput] != 0U))
  4156. {
  4157. timer->SR = ~TIM_SR_CC1IF;
  4158. PlsrFiniteStopAtFallingEdge(pulseOutput);
  4159. goto irq_done;
  4160. }
  4161. #if PLSR_DEBUG_TIMING
  4162. timingOutput = owner;
  4163. #endif
  4164. timer->SR = ~TIM_SR_CC1IF;
  4165. if ((owner <= 2U)
  4166. && (PlsrTimerRunning[owner] != 0U)
  4167. && (PlsrTimerOutputMode[owner] == PLSR_OUTPUT_AB))
  4168. {
  4169. uint8_t pairOutput = (uint8_t)(owner + 1U);
  4170. TIM_TypeDef *baseTimer = PlsrTimerMap[owner].timer;
  4171. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  4172. PLSR_AB_SETTING pending;
  4173. if (PlsrAbStopPending[owner] != 0U)
  4174. {
  4175. if (PlsrAbCanFastGateAtZero(owner) == 0U)
  4176. {
  4177. goto irq_done;
  4178. }
  4179. baseTimer->DIER &= ~(TIM_DIER_CC1IE | TIM_DIER_UIE);
  4180. pairTimer->DIER &= ~(TIM_DIER_CC1IE | TIM_DIER_UIE);
  4181. baseTimer->SR = ~TIM_SR_CC1IF;
  4182. pairTimer->SR = ~TIM_SR_CC1IF;
  4183. PlsrFrequencyVerifyPending[owner] =
  4184. PLSR_FREQUENCY_VERIFY_NONE;
  4185. PlsrAbVerifyOwner[owner] = PLSR_COUNTER_NONE;
  4186. PlsrAbVerifyOwner[pairOutput] = PLSR_COUNTER_NONE;
  4187. PlsrAbFastGate(owner);
  4188. PlsrAbFastGated[owner] = 1U;
  4189. PlsrDeferredPulsePending[owner] = 0U;
  4190. PlsrPulseTimerIrq(owner);
  4191. goto irq_done;
  4192. }
  4193. if (pulseOutput != PlsrAbLagAxis[owner])
  4194. {
  4195. if (PlsrDeferredPulsePending[owner] != 0U)
  4196. {
  4197. PlsrDeferredPulsePending[owner] = 0U;
  4198. timer->DIER &= ~TIM_DIER_CC1IE;
  4199. PlsrPulseTimerIrq(owner);
  4200. }
  4201. goto irq_done;
  4202. }
  4203. if (PlsrDeferredPulsePending[owner] != 0U)
  4204. {
  4205. goto irq_done;
  4206. }
  4207. if (PlsrAbFrequencyPending[owner] != 0U)
  4208. {
  4209. pending = PlsrAbPendingSetting[owner];
  4210. PlsrAbFrequencyPending[owner] = 0U;
  4211. PlsrAbLoadAndStart(owner, &pending);
  4212. PlsrAbActiveSetting[owner] = pending;
  4213. PlsrTimerActiveSetting[owner] =
  4214. PlsrTimerQueuedSetting[owner];
  4215. PlsrTimerActiveFrequencyHz[owner] =
  4216. pending.actualFrequencyHz;
  4217. PlsrDeferredPulsePending[owner] = 1U;
  4218. PlsrAbScheduleFrequencyVerify(owner);
  4219. goto irq_done;
  4220. }
  4221. PlsrPulseTimerIrq(owner);
  4222. }
  4223. goto irq_done;
  4224. }
  4225. if (((timer->SR & TIM_SR_UIF) != 0UL)
  4226. && ((timer->DIER & TIM_DIER_UIE) != 0UL))
  4227. {
  4228. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  4229. if ((PlsrFiniteActive[pulseOutput] != 0U)
  4230. && (PlsrFiniteFrequencyPending[pulseOutput] != 0U))
  4231. {
  4232. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4233. PlsrTimerQueuedFrequencyHz[pulseOutput];
  4234. PlsrTimerActiveSetting[pulseOutput] =
  4235. PlsrTimerQueuedSetting[pulseOutput];
  4236. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  4237. timer->DIER &= ~TIM_DIER_UIE;
  4238. goto irq_done;
  4239. }
  4240. if (PlsrPlatformSettingsDiffer(
  4241. &PlsrTimerActiveSetting[pulseOutput],
  4242. &PlsrTimerQueuedSetting[pulseOutput]) != 0U)
  4243. {
  4244. PlsrFrequencyVerifyPending[pulseOutput] = 1U;
  4245. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4246. PlsrTimerQueuedFrequencyHz[pulseOutput];
  4247. PlsrTimerActiveSetting[pulseOutput] =
  4248. PlsrTimerQueuedSetting[pulseOutput];
  4249. }
  4250. PlsrPulseTimerIrq(pulseOutput);
  4251. }
  4252. irq_done:
  4253. PlsrTimerIrqActive[pulseOutput] = 0U;
  4254. #if PLSR_DEBUG_TIMING
  4255. irq_record:
  4256. elapsedCycles = DWT->CYCCNT - startedAt;
  4257. PlsrIrqCount[timingOutput]++;
  4258. PlsrIrqLastCycles[timingOutput] = elapsedCycles;
  4259. if (elapsedCycles > PlsrIrqMaxCycles[timingOutput])
  4260. {
  4261. PlsrIrqMaxCycles[timingOutput] = elapsedCycles;
  4262. }
  4263. #endif
  4264. }
  4265. static uint8_t PlsrHandleScheduledAbVerify(uint8_t pulseOutput)
  4266. {
  4267. uint8_t verifyOwner = PlsrAbVerifyOwner[pulseOutput];
  4268. TIM_TypeDef *timer;
  4269. #if PLSR_DEBUG_TIMING
  4270. uint32_t startedAt;
  4271. uint32_t elapsedCycles;
  4272. #endif
  4273. if (verifyOwner > 2U)
  4274. {
  4275. return 0U;
  4276. }
  4277. timer = PlsrTimerMap[pulseOutput].timer;
  4278. if (((timer->SR & TIM_SR_UIF) == 0UL)
  4279. || ((timer->DIER & TIM_DIER_UIE) == 0UL))
  4280. {
  4281. return 0U;
  4282. }
  4283. #if PLSR_DEBUG_TIMING
  4284. startedAt = DWT->CYCCNT;
  4285. #endif
  4286. timer->SR = ~TIM_SR_UIF;
  4287. timer->DIER &= ~TIM_DIER_UIE;
  4288. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  4289. if (PlsrFrequencyVerifyPending[verifyOwner]
  4290. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  4291. {
  4292. PlsrFrequencyVerifyPending[verifyOwner] =
  4293. PLSR_FREQUENCY_VERIFY_NONE;
  4294. PlsrFrequencyVerifyPulseCount[verifyOwner] = 0U;
  4295. (void)PlsrVerifyActiveFrequency(verifyOwner);
  4296. }
  4297. #if PLSR_DEBUG_TIMING
  4298. elapsedCycles = DWT->CYCCNT - startedAt;
  4299. PlsrIrqCount[verifyOwner]++;
  4300. PlsrIrqLastCycles[verifyOwner] = elapsedCycles;
  4301. if (elapsedCycles > PlsrIrqMaxCycles[verifyOwner])
  4302. {
  4303. PlsrIrqMaxCycles[verifyOwner] = elapsedCycles;
  4304. }
  4305. #endif
  4306. return 1U;
  4307. }
  4308. static uint8_t PlsrFinalStopIrqIsPending(uint8_t pulseOutput)
  4309. {
  4310. uint8_t owner = ((pulseOutput & 2U) == 0U) ? 0U : 2U;
  4311. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  4312. return ((PlsrAbStopPending[owner] != 0U)
  4313. && ((timer->SR & TIM_SR_CC1IF) != 0UL)
  4314. && ((timer->DIER & TIM_DIER_CC1IE) != 0UL)) ? 1U : 0U;
  4315. }
  4316. static uint8_t PlsrHandleFinalArmJob(uint8_t pulseOutput)
  4317. {
  4318. uint8_t owner = PlsrAbFinalArmJobOwner[pulseOutput];
  4319. #if PLSR_DEBUG_TIMING
  4320. uint32_t startedAt;
  4321. uint32_t elapsedCycles;
  4322. #endif
  4323. if (owner > 2U)
  4324. {
  4325. return 0U;
  4326. }
  4327. PlsrAbFinalArmJobOwner[pulseOutput] = PLSR_COUNTER_NONE;
  4328. #if PLSR_DEBUG_TIMING
  4329. startedAt = DWT->CYCCNT;
  4330. #endif
  4331. PlsrFinalArmJobIrq(owner);
  4332. #if PLSR_DEBUG_TIMING
  4333. elapsedCycles = DWT->CYCCNT - startedAt;
  4334. PlsrFinalArmJobLastCycles[owner] = elapsedCycles;
  4335. if (elapsedCycles > PlsrFinalArmJobMaxCycles[owner])
  4336. {
  4337. PlsrFinalArmJobMaxCycles[owner] = elapsedCycles;
  4338. }
  4339. #endif
  4340. return 1U;
  4341. }
  4342. static void PlsrDispatchTimerIrq(uint8_t pulseOutput)
  4343. {
  4344. if (PlsrFinalStopIrqIsPending(pulseOutput) != 0U)
  4345. {
  4346. PlsrHandleTimerIrq(pulseOutput);
  4347. return;
  4348. }
  4349. if (PlsrHandleFinalArmJob(pulseOutput) != 0U)
  4350. {
  4351. return;
  4352. }
  4353. if (PlsrHandleScheduledAbVerify(pulseOutput) == 0U)
  4354. {
  4355. PlsrHandleTimerIrq(pulseOutput);
  4356. }
  4357. }
  4358. void TIM1_UP_TIM10_IRQHandler(void)
  4359. {
  4360. PlsrDispatchTimerIrq(0U);
  4361. }
  4362. void TIM8_UP_TIM13_IRQHandler(void)
  4363. {
  4364. PlsrDispatchTimerIrq(1U);
  4365. }
  4366. void TIM1_TRG_COM_TIM11_IRQHandler(void)
  4367. {
  4368. PlsrDispatchTimerIrq(2U);
  4369. }
  4370. void TIM8_TRG_COM_TIM14_IRQHandler(void)
  4371. {
  4372. PlsrDispatchTimerIrq(3U);
  4373. }
  4374. static void PlsrHandleCounterIrq(uint8_t counterIndex)
  4375. {
  4376. TIM_TypeDef *counter;
  4377. #if PLSR_DEBUG_TIMING
  4378. uint32_t startedAt = DWT->CYCCNT;
  4379. #endif
  4380. if (counterIndex >= PLSR_COUNTER_COUNT)
  4381. {
  4382. return;
  4383. }
  4384. counter = PlsrCounters[counterIndex];
  4385. if (((counter->SR & TIM_SR_CC1IF) != 0UL)
  4386. && ((counter->DIER & TIM_DIER_CC1IE) != 0UL))
  4387. {
  4388. uint8_t owner = PlsrCounterOwner[counterIndex];
  4389. counter->SR = ~TIM_SR_CC1IF;
  4390. if ((owner <= 3U) && (PlsrFiniteActive[owner] != 0U))
  4391. {
  4392. PlsrFinitePrepareNextStepIrq(owner, counter);
  4393. }
  4394. }
  4395. if (((counter->SR & TIM_SR_UIF) != 0UL)
  4396. && ((counter->DIER & TIM_DIER_UIE) != 0UL))
  4397. {
  4398. uint8_t owner = PlsrCounterOwner[counterIndex];
  4399. counter->SR = ~TIM_SR_UIF;
  4400. if ((owner <= 3U) && (PlsrFiniteActive[owner] != 0U))
  4401. {
  4402. PlsrFiniteCounterIrq(owner, counter);
  4403. #if PLSR_DEBUG_TIMING
  4404. {
  4405. uint32_t elapsedCycles = DWT->CYCCNT - startedAt;
  4406. if (PlsrFiniteCompletionPending[owner] != 0U)
  4407. {
  4408. PlsrFiniteFinalIrqLastCycles[owner] = elapsedCycles;
  4409. if (elapsedCycles > PlsrFiniteFinalIrqMaxCycles[owner])
  4410. {
  4411. PlsrFiniteFinalIrqMaxCycles[owner] = elapsedCycles;
  4412. }
  4413. }
  4414. else
  4415. {
  4416. PlsrFiniteBlockIrqCount[owner]++;
  4417. if (elapsedCycles > PlsrFiniteBlockIrqMaxCycles[owner])
  4418. {
  4419. PlsrFiniteBlockIrqMaxCycles[owner] = elapsedCycles;
  4420. }
  4421. }
  4422. }
  4423. #endif
  4424. }
  4425. else
  4426. {
  4427. PlsrCounterOverflowPulses[counterIndex] +=
  4428. PLSR_COUNTER_BLOCK_PULSES;
  4429. }
  4430. }
  4431. }
  4432. static void PlsrFiniteArmNextStepPrepare(uint8_t pulseOutput,
  4433. TIM_TypeDef *counter,
  4434. uint32_t blockPulses)
  4435. {
  4436. uint16_t nextIndex =
  4437. (uint16_t)(PlsrFiniteStepIndex[pulseOutput] + 1U);
  4438. counter->DIER &= ~TIM_DIER_CC1IE;
  4439. counter->SR = ~TIM_SR_CC1IF;
  4440. if (PlsrFiniteRemainingPulses[pulseOutput] > blockPulses)
  4441. {
  4442. return;
  4443. }
  4444. if ((PlsrFiniteStreamActive[pulseOutput] == 0U)
  4445. && (nextIndex >= PlsrFiniteStepCount[pulseOutput]))
  4446. {
  4447. return;
  4448. }
  4449. if (blockPulses > 1UL)
  4450. {
  4451. counter->CCR1 = blockPulses - 1UL;
  4452. counter->DIER |= TIM_DIER_CC1IE;
  4453. }
  4454. else
  4455. {
  4456. PlsrFinitePrepareNextStepIrq(pulseOutput, counter);
  4457. }
  4458. }
  4459. static void PlsrFinitePrepareNextStepIrq(uint8_t pulseOutput,
  4460. TIM_TypeDef *counter)
  4461. {
  4462. uint16_t nextIndex =
  4463. (uint16_t)(PlsrFiniteStepIndex[pulseOutput] + 1U);
  4464. TIM_TypeDef *timer;
  4465. const PLSR_PLATFORM_TIMER_SETTING *next;
  4466. counter->DIER &= ~TIM_DIER_CC1IE;
  4467. if (PlsrFiniteStreamActive[pulseOutput] != 0U)
  4468. {
  4469. PLSR_PLATFORM_TIMER_SETTING streamSetting;
  4470. uint32_t streamPulses = 0UL;
  4471. uint8_t startsNextSegment = 0U;
  4472. uint8_t takeResult;
  4473. if ((PlsrFiniteStreamNextValid[pulseOutput] != 0U)
  4474. || (PlsrFiniteStreamSourceDone[pulseOutput] != 0U)
  4475. || (PlsrFiniteStreamSourceFault[pulseOutput] != 0U))
  4476. {
  4477. return;
  4478. }
  4479. takeResult = PlsrExecTakeCountedRunIrq(
  4480. pulseOutput, &streamSetting, &streamPulses,
  4481. &startsNextSegment);
  4482. if (takeResult == PLSR_EXEC_RUN_DONE)
  4483. {
  4484. PlsrFiniteStreamSourceDone[pulseOutput] = 1U;
  4485. return;
  4486. }
  4487. if ((takeResult != PLSR_EXEC_RUN_READY)
  4488. || (streamPulses == 0UL)
  4489. || (PlsrPreparedSettingIsValid(
  4490. pulseOutput, PLSR_OUTPUT_PULSE_DIR,
  4491. &streamSetting) == 0U))
  4492. {
  4493. PlsrFiniteStreamSourceFault[pulseOutput] = 1U;
  4494. PlsrExecCountedStreamFaultIrq(pulseOutput);
  4495. return;
  4496. }
  4497. timer = PlsrTimerMap[pulseOutput].timer;
  4498. timer->PSC = streamSetting.prescaler;
  4499. timer->ARR = streamSetting.period;
  4500. timer->CCR1 = streamSetting.compare;
  4501. PlsrTimerQueuedSetting[pulseOutput] = streamSetting;
  4502. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  4503. streamSetting.actualFrequencyHz;
  4504. PlsrFiniteStreamNextSetting[pulseOutput] = streamSetting;
  4505. PlsrFiniteStreamNextPulses[pulseOutput] = streamPulses;
  4506. PlsrFiniteStreamNextStartsSegment[pulseOutput] =
  4507. startsNextSegment;
  4508. PlsrFiniteStreamNextValid[pulseOutput] = 1U;
  4509. return;
  4510. }
  4511. if (nextIndex >= PlsrFiniteStepCount[pulseOutput])
  4512. {
  4513. return;
  4514. }
  4515. timer = PlsrTimerMap[pulseOutput].timer;
  4516. next = &PlsrFiniteSteps[pulseOutput][nextIndex].setting;
  4517. timer->PSC = next->prescaler;
  4518. timer->ARR = next->period;
  4519. timer->CCR1 = next->compare;
  4520. PlsrTimerQueuedSetting[pulseOutput] = *next;
  4521. PlsrTimerQueuedFrequencyHz[pulseOutput] = next->actualFrequencyHz;
  4522. }
  4523. static void PlsrFiniteCounterIrq(uint8_t pulseOutput,
  4524. TIM_TypeDef *counter)
  4525. {
  4526. uint32_t completedBlock =
  4527. (PlsrFiniteRemainingPulses[pulseOutput]
  4528. > PLSR_COUNTER_BLOCK_PULSES)
  4529. ? PLSR_COUNTER_BLOCK_PULSES
  4530. : PlsrFiniteRemainingPulses[pulseOutput];
  4531. PlsrFiniteRemainingPulses[pulseOutput] -= completedBlock;
  4532. if (PlsrFiniteRemainingPulses[pulseOutput] != 0UL)
  4533. {
  4534. uint32_t nextBlock =
  4535. (PlsrFiniteRemainingPulses[pulseOutput]
  4536. > PLSR_COUNTER_BLOCK_PULSES)
  4537. ? PLSR_COUNTER_BLOCK_PULSES
  4538. : PlsrFiniteRemainingPulses[pulseOutput];
  4539. PlsrCounterOverflowPulses[PlsrCounterIndexByOutput[pulseOutput]] +=
  4540. completedBlock;
  4541. counter->ARR = (nextBlock == 1UL) ? 1UL : (nextBlock - 1UL);
  4542. PlsrFiniteCounterPreload[pulseOutput] =
  4543. (nextBlock == 1UL) ? 1U : 0U;
  4544. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4545. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, nextBlock);
  4546. return;
  4547. }
  4548. if (PlsrFiniteStreamActive[pulseOutput] != 0U)
  4549. {
  4550. PlsrObservedPulseBase[pulseOutput] +=
  4551. PlsrFiniteTargetPulses[pulseOutput];
  4552. PlsrObservedPulsePublished[pulseOutput] =
  4553. PlsrObservedPulseBase[pulseOutput];
  4554. if (PlsrFiniteStreamNextValid[pulseOutput] != 0U)
  4555. {
  4556. uint32_t nextPulses =
  4557. PlsrFiniteStreamNextPulses[pulseOutput];
  4558. uint32_t firstBlock =
  4559. (nextPulses > PLSR_COUNTER_BLOCK_PULSES)
  4560. ? PLSR_COUNTER_BLOCK_PULSES : nextPulses;
  4561. if (PlsrFiniteStreamNextStartsSegment[pulseOutput] != 0U)
  4562. {
  4563. /* 段边界回调只传"到当前 run 完成为止的累计计数"(IRQ 内
  4564. 已累加完成的 base),执行器只置事件,不做簿记。 */
  4565. PlsrExecCountedSegmentBoundaryIrq(
  4566. pulseOutput, PlsrObservedPulseBase[pulseOutput]);
  4567. }
  4568. PlsrFiniteTargetPulses[pulseOutput] = nextPulses;
  4569. PlsrFiniteRemainingPulses[pulseOutput] = nextPulses;
  4570. PlsrCounterOverflowPulses[
  4571. PlsrCounterIndexByOutput[pulseOutput]] = 0UL;
  4572. counter->ARR = (firstBlock == 1UL)
  4573. ? 1UL : (firstBlock - 1UL);
  4574. PlsrFiniteCounterPreload[pulseOutput] =
  4575. (firstBlock == 1UL) ? 1U : 0U;
  4576. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4577. PlsrTimerActiveSetting[pulseOutput] =
  4578. PlsrFiniteStreamNextSetting[pulseOutput];
  4579. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4580. PlsrFiniteStreamNextSetting[pulseOutput].actualFrequencyHz;
  4581. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  4582. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  4583. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, firstBlock);
  4584. return;
  4585. }
  4586. }
  4587. else if (PlsrFiniteStepCount[pulseOutput] != 0U)
  4588. {
  4589. uint16_t completedIndex = PlsrFiniteStepIndex[pulseOutput];
  4590. uint16_t nextIndex = (uint16_t)(completedIndex + 1U);
  4591. uint8_t hasNext = (nextIndex
  4592. < PlsrFiniteStepCount[pulseOutput]) ? 1U : 0U;
  4593. PlsrObservedPulseBase[pulseOutput] +=
  4594. PlsrFiniteTargetPulses[pulseOutput];
  4595. PlsrObservedPulsePublished[pulseOutput] =
  4596. PlsrObservedPulseBase[pulseOutput];
  4597. PlsrFiniteCompletedStepCount[pulseOutput] =
  4598. (uint16_t)(completedIndex + 1U);
  4599. if (hasNext != 0U)
  4600. {
  4601. const PLSR_PLATFORM_FINITE_STEP *next =
  4602. &PlsrFiniteSteps[pulseOutput][nextIndex];
  4603. uint32_t firstBlock =
  4604. (next->pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  4605. ? PLSR_COUNTER_BLOCK_PULSES : next->pulseCount;
  4606. PlsrFiniteStepIndex[pulseOutput] = nextIndex;
  4607. PlsrFiniteTargetPulses[pulseOutput] = next->pulseCount;
  4608. PlsrFiniteRemainingPulses[pulseOutput] = next->pulseCount;
  4609. PlsrCounterOverflowPulses[
  4610. PlsrCounterIndexByOutput[pulseOutput]] = 0UL;
  4611. counter->ARR = (firstBlock == 1UL)
  4612. ? 1UL : (firstBlock - 1UL);
  4613. PlsrFiniteCounterPreload[pulseOutput] =
  4614. (firstBlock == 1UL) ? 1U : 0U;
  4615. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4616. PlsrTimerActiveSetting[pulseOutput] = next->setting;
  4617. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4618. next->setting.actualFrequencyHz;
  4619. PlsrTimerQueuedSetting[pulseOutput] = next->setting;
  4620. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  4621. next->setting.actualFrequencyHz;
  4622. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, firstBlock);
  4623. return;
  4624. }
  4625. }
  4626. counter->DIER = 0UL;
  4627. counter->CR1 &= ~TIM_CR1_CEN;
  4628. counter->SMCR &= ~(TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0);
  4629. /*
  4630. * An OC rising edge is the terminal pulse's falling edge because the
  4631. * output stage is active low. The target-count update IRQ therefore is
  4632. * already the safe idle boundary: stop here before CCR1 can start an
  4633. * extra terminal pulse.
  4634. */
  4635. PlsrFiniteStopAtFallingEdge(pulseOutput);
  4636. }
  4637. static void PlsrFiniteRetargetAtFallingEdge(uint8_t pulseOutput)
  4638. {
  4639. uint8_t counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  4640. TIM_TypeDef *counter = PlsrCounters[counterIndex];
  4641. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  4642. uint32_t drainPulses = PlsrFiniteRetargetDrainPulses[pulseOutput];
  4643. uint32_t blockCount;
  4644. uint32_t completed;
  4645. PlsrFiniteRetargetPending[pulseOutput] = 0U;
  4646. PlsrFiniteRetargetDrainPulses[pulseOutput] = 0UL;
  4647. timer->DIER &= ~TIM_DIER_CC1IE;
  4648. PlsrCounterSuspend(pulseOutput);
  4649. blockCount = (uint16_t)counter->CNT;
  4650. if (blockCount >= PlsrFiniteCounterPreload[pulseOutput])
  4651. {
  4652. blockCount -= PlsrFiniteCounterPreload[pulseOutput];
  4653. }
  4654. completed = PlsrFiniteTargetPulses[pulseOutput]
  4655. - PlsrFiniteRemainingPulses[pulseOutput] + blockCount;
  4656. if (PlsrFiniteStepCount[pulseOutput] != 0U)
  4657. {
  4658. completed += PlsrFiniteSteps[pulseOutput][
  4659. PlsrFiniteStepIndex[pulseOutput]].segmentPulseOffset;
  4660. }
  4661. if (completed > PlsrFiniteTargetPulses[pulseOutput])
  4662. {
  4663. completed = PlsrFiniteTargetPulses[pulseOutput];
  4664. }
  4665. PlsrFiniteTargetPulses[pulseOutput] = completed + drainPulses;
  4666. PlsrFiniteRemainingPulses[pulseOutput] = drainPulses;
  4667. PlsrFiniteStepCount[pulseOutput] = 0U;
  4668. PlsrFiniteStepIndex[pulseOutput] = 0U;
  4669. PlsrFiniteBoundaryReadIndex[pulseOutput] = 0U;
  4670. PlsrFiniteCompletedStepCount[pulseOutput] = 0U;
  4671. counter->ARR = (drainPulses == 1UL) ? 1UL : (drainPulses - 1UL);
  4672. PlsrFiniteCounterPreload[pulseOutput] =
  4673. (drainPulses == 1UL) ? 1U : 0U;
  4674. counter->CNT = 0UL;
  4675. counter->EGR = TIM_EGR_UG;
  4676. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4677. counter->SR = 0UL;
  4678. counter->DIER = TIM_DIER_UIE;
  4679. PlsrCounterBegin(pulseOutput);
  4680. }
  4681. static void PlsrFiniteStopAtFallingEdge(uint8_t pulseOutput)
  4682. {
  4683. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  4684. /* OC1 and GPIO idle are both high here; hand off without a pin glitch. */
  4685. PlsrPulsePinCaptureIdle(pulseOutput);
  4686. timer->DIER = 0UL;
  4687. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  4688. timer->CR1 &= ~TIM_CR1_CEN;
  4689. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  4690. PlsrFiniteTailStopPending[pulseOutput] = 0U;
  4691. PlsrFiniteActive[pulseOutput] = 0U;
  4692. PlsrFiniteCompletionPending[pulseOutput] = 1U;
  4693. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  4694. PlsrFiniteCounterPreload[pulseOutput] = 0U;
  4695. __DSB();
  4696. }
  4697. void TIM1_BRK_TIM9_IRQHandler(void)
  4698. {
  4699. PlsrHandleCounterIrq(0U);
  4700. }
  4701. void TIM8_BRK_TIM12_IRQHandler(void)
  4702. {
  4703. PlsrHandleCounterIrq(1U);
  4704. }
  4705. #endif /* PLSR_HOST_TEST */