No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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