You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

5202 lines
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_SYSCFG_CLK_ENABLE();
  2522. __HAL_RCC_TIM10_CLK_ENABLE();
  2523. __HAL_RCC_TIM11_CLK_ENABLE();
  2524. __HAL_RCC_TIM13_CLK_ENABLE();
  2525. __HAL_RCC_TIM14_CLK_ENABLE();
  2526. __HAL_RCC_TIM9_CLK_ENABLE();
  2527. __HAL_RCC_TIM12_CLK_ENABLE();
  2528. __HAL_RCC_PWR_CLK_ENABLE();
  2529. HAL_PWR_EnableBkUpAccess();
  2530. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  2531. if (HAL_PWREx_EnableBkUpReg() != HAL_OK)
  2532. {
  2533. return 0U;
  2534. }
  2535. #if PLSR_DEBUG_TIMING
  2536. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  2537. DWT->CYCCNT = 0UL;
  2538. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  2539. (void)memset((void *)PlsrIrqCount, 0, sizeof(PlsrIrqCount));
  2540. (void)memset((void *)PlsrIrqLastCycles, 0, sizeof(PlsrIrqLastCycles));
  2541. (void)memset((void *)PlsrIrqMaxCycles, 0, sizeof(PlsrIrqMaxCycles));
  2542. (void)memset((void *)PlsrFinalArmQueueCount, 0,
  2543. sizeof(PlsrFinalArmQueueCount));
  2544. (void)memset((void *)PlsrFinalArmJobLastCycles, 0,
  2545. sizeof(PlsrFinalArmJobLastCycles));
  2546. (void)memset((void *)PlsrFinalArmJobMaxCycles, 0,
  2547. sizeof(PlsrFinalArmJobMaxCycles));
  2548. (void)memset((void *)PlsrFinalArmQueueToStopLastCycles, 0,
  2549. sizeof(PlsrFinalArmQueueToStopLastCycles));
  2550. (void)memset((void *)PlsrFinalArmQueueToStopMaxCycles, 0,
  2551. sizeof(PlsrFinalArmQueueToStopMaxCycles));
  2552. (void)memset((void *)PlsrFinalArmQueuedAt, 0,
  2553. sizeof(PlsrFinalArmQueuedAt));
  2554. (void)memset((void *)PlsrFinalArmQueueTimingPending, 0,
  2555. sizeof(PlsrFinalArmQueueTimingPending));
  2556. #endif
  2557. HAL_GPIO_WritePin(GPIOH, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8
  2558. | GPIO_PIN_9, GPIO_PIN_SET);
  2559. gpio.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  2560. gpio.Mode = GPIO_MODE_OUTPUT_PP;
  2561. gpio.Pull = GPIO_NOPULL;
  2562. gpio.Speed = GPIO_SPEED_FREQ_HIGH;
  2563. gpio.Alternate = 0U;
  2564. HAL_GPIO_Init(GPIOH, &gpio);
  2565. gpio.Mode = GPIO_MODE_IT_RISING;
  2566. gpio.Pull = GPIO_NOPULL;
  2567. gpio.Speed = GPIO_SPEED_FREQ_LOW;
  2568. gpio.Alternate = 0U;
  2569. gpio.Pin = GPIO_PIN_5;
  2570. HAL_GPIO_Init(GPIOB, &gpio);
  2571. gpio.Pin = GPIO_PIN_12;
  2572. HAL_GPIO_Init(GPIOG, &gpio);
  2573. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5 | GPIO_PIN_12);
  2574. HAL_NVIC_SetPriority(EXTI9_5_IRQn, 2U, 0U);
  2575. HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
  2576. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2U, 0U);
  2577. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  2578. for (index = 0U; index < 4U; index++)
  2579. {
  2580. PlsrTimerActiveFrequencyHz[index] = 0UL;
  2581. PlsrTimerQueuedFrequencyHz[index] = 0UL;
  2582. (void)memset(&PlsrTimerActiveSetting[index], 0,
  2583. sizeof(PlsrTimerActiveSetting[index]));
  2584. (void)memset(&PlsrTimerQueuedSetting[index], 0,
  2585. sizeof(PlsrTimerQueuedSetting[index]));
  2586. PlsrTimerQueueGeneration[index] = 0UL;
  2587. PlsrTimerOutputMode[index] = PLSR_OUTPUT_PULSE_DIR;
  2588. PlsrTimerDirectionPositive[index] = 1U;
  2589. PlsrTimerRunning[index] = 0U;
  2590. PlsrFrequencyVerifyPending[index] = 0U;
  2591. PlsrFrequencyVerifyPulseCount[index] = 0U;
  2592. PlsrDeferredPulsePending[index] = 0U;
  2593. PlsrAbVerifyOwner[index] = PLSR_COUNTER_NONE;
  2594. PlsrAbFinalArmJobOwner[index] = PLSR_COUNTER_NONE;
  2595. PlsrTimerIrqActive[index] = 0U;
  2596. PlsrAbFrequencyPending[index] = 0U;
  2597. PlsrAbLagAxis[index] = PLSR_COUNTER_NONE;
  2598. PlsrAbStructureVerified[index] = 0U;
  2599. PlsrAbCounterSourceAxis[index] = PLSR_COUNTER_NONE;
  2600. PlsrAbCounterBoundary[index] = 0UL;
  2601. PlsrAbStopPending[index] = 0U;
  2602. PlsrAbFastGated[index] = 0U;
  2603. PlsrCounterIndexByOutput[index] = PLSR_COUNTER_NONE;
  2604. PlsrObservedPulseBase[index] = 0UL;
  2605. PlsrObservedPulsePublished[index] = 0UL;
  2606. PlsrFiniteActive[index] = 0U;
  2607. PlsrFiniteCompletionPending[index] = 0U;
  2608. PlsrFiniteFrequencyPending[index] = 0U;
  2609. PlsrFiniteRetargetPending[index] = 0U;
  2610. PlsrFiniteTailStopPending[index] = 0U;
  2611. PlsrFiniteRetargetDrainPulses[index] = 0UL;
  2612. PlsrFiniteTargetPulses[index] = 0UL;
  2613. PlsrFiniteRemainingPulses[index] = 0UL;
  2614. PlsrFiniteCounterPreload[index] = 0U;
  2615. PlsrFiniteStepCount[index] = 0U;
  2616. PlsrFiniteStepIndex[index] = 0U;
  2617. PlsrFiniteBoundaryReadIndex[index] = 0U;
  2618. PlsrFiniteCompletedStepCount[index] = 0U;
  2619. PlsrTimerInitialize(PlsrTimerMap[index].timer);
  2620. PlsrPulsePinHoldIdle(index);
  2621. HAL_NVIC_SetPriority(PlsrTimerMap[index].irq, 0U, 0U);
  2622. HAL_NVIC_EnableIRQ(PlsrTimerMap[index].irq);
  2623. }
  2624. for (index = 0U; index < PLSR_COUNTER_COUNT; index++)
  2625. {
  2626. PlsrCounterOwner[index] = PLSR_COUNTER_NONE;
  2627. PlsrCounterOverflowPulses[index] = 0UL;
  2628. PlsrCounters[index]->CR1 = 0UL;
  2629. PlsrCounters[index]->DIER = 0UL;
  2630. PlsrCounters[index]->SMCR = 0UL;
  2631. PlsrCounters[index]->SR = 0UL;
  2632. }
  2633. PlsrPlatformFaultPending = 0U;
  2634. HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 0U, 0U);
  2635. HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
  2636. HAL_NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 0U, 0U);
  2637. HAL_NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn);
  2638. positionRecord = PlsrNewestBackupPosition();
  2639. PlsrBackupPositionGeneration =
  2640. (positionRecord == NULL) ? 0UL : positionRecord->generation;
  2641. return 1U;
  2642. }
  2643. uint8_t PlsrPlatformPrepare(uint8_t pulseOutput,
  2644. uint8_t directionOutput,
  2645. uint8_t directionLevel,
  2646. uint8_t outputMode,
  2647. uint8_t directionPositive)
  2648. {
  2649. uint8_t index;
  2650. if ((pulseOutput > 3U) || (directionOutput > 3U)
  2651. || (outputMode > PLSR_OUTPUT_AB)
  2652. || ((outputMode == PLSR_OUTPUT_AB)
  2653. && (pulseOutput != 0U) && (pulseOutput != 2U)))
  2654. {
  2655. return 0U;
  2656. }
  2657. if ((PlsrAbStopPending[0] != 0U) || (PlsrAbStopPending[2] != 0U))
  2658. {
  2659. return 0U;
  2660. }
  2661. for (index = 0U; index < 4U; index++)
  2662. {
  2663. PlsrAbFinalArmJobOwner[index] = PLSR_COUNTER_NONE;
  2664. NVIC_ClearPendingIRQ(PlsrTimerMap[index].irq);
  2665. PlsrCounterStop(index);
  2666. PlsrPulsePinHoldIdle(index);
  2667. PlsrTimerStop(PlsrTimerMap[index].timer);
  2668. HAL_GPIO_WritePin(PlsrDirectionMap[index].port,
  2669. PlsrDirectionMap[index].pin,
  2670. ((outputMode == PLSR_OUTPUT_PULSE_DIR)
  2671. && (index == directionOutput)
  2672. && (directionLevel != 0U))
  2673. ? GPIO_PIN_RESET : GPIO_PIN_SET);
  2674. PlsrTimerRunning[index] = 0U;
  2675. PlsrFrequencyVerifyPending[index] = 0U;
  2676. PlsrFrequencyVerifyPulseCount[index] = 0U;
  2677. PlsrDeferredPulsePending[index] = 0U;
  2678. PlsrAbVerifyOwner[index] = PLSR_COUNTER_NONE;
  2679. PlsrAbFrequencyPending[index] = 0U;
  2680. PlsrAbStructureVerified[index] = 0U;
  2681. PlsrAbFastGated[index] = 0U;
  2682. }
  2683. PlsrTimerOutputMode[pulseOutput] = outputMode;
  2684. PlsrTimerDirectionPositive[pulseOutput] =
  2685. (directionPositive != 0U) ? 1U : 0U;
  2686. PlsrPlatformFaultPending = 0U;
  2687. return 1U;
  2688. }
  2689. uint8_t PlsrPlatformStartPulse(uint8_t pulseOutput,
  2690. uint32_t firstFrequencyHz,
  2691. uint32_t queuedFrequencyHz,
  2692. uint32_t *actualFirstFrequencyHz,
  2693. uint32_t *actualQueuedFrequencyHz)
  2694. {
  2695. PLSR_PLATFORM_TIMER_SETTING firstSetting;
  2696. PLSR_PLATFORM_TIMER_SETTING queuedSetting;
  2697. if ((pulseOutput > 3U)
  2698. || (PlsrPlatformBuildTimerSetting(
  2699. pulseOutput, PlsrTimerOutputMode[pulseOutput],
  2700. firstFrequencyHz, &firstSetting) == 0U)
  2701. || (PlsrPlatformBuildTimerSetting(
  2702. pulseOutput, PlsrTimerOutputMode[pulseOutput],
  2703. queuedFrequencyHz, &queuedSetting) == 0U))
  2704. {
  2705. return 0U;
  2706. }
  2707. return PlsrPlatformStartPrepared(pulseOutput, &firstSetting,
  2708. &queuedSetting,
  2709. actualFirstFrequencyHz,
  2710. actualQueuedFrequencyHz);
  2711. }
  2712. static uint8_t PlsrPreparedSettingIsValid(
  2713. uint8_t pulseOutput,
  2714. uint8_t outputMode,
  2715. const PLSR_PLATFORM_TIMER_SETTING *setting)
  2716. {
  2717. uint32_t periodCounts;
  2718. if ((pulseOutput > 3U) || (setting == NULL)
  2719. || (setting->actualFrequencyHz == 0UL)
  2720. || (setting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ))
  2721. {
  2722. return 0U;
  2723. }
  2724. periodCounts = (uint32_t)setting->period + 1UL;
  2725. if (setting->compare != (uint16_t)(periodCounts / 2UL))
  2726. {
  2727. return 0U;
  2728. }
  2729. if (outputMode == PLSR_OUTPUT_PULSE_DIR)
  2730. {
  2731. return ((periodCounts >= 2UL)
  2732. && (setting->pairPrescaler == 0U)) ? 1U : 0U;
  2733. }
  2734. if ((outputMode != PLSR_OUTPUT_AB) || ((pulseOutput & 1U) != 0U)
  2735. || (periodCounts < 4UL))
  2736. {
  2737. return 0U;
  2738. }
  2739. return ((((uint32_t)setting->prescaler + 1UL)
  2740. == 2UL * ((uint32_t)setting->pairPrescaler + 1UL))
  2741. ? 1U : 0U);
  2742. }
  2743. static void PlsrPlatformToTimerSetting(
  2744. const PLSR_PLATFORM_TIMER_SETTING *source,
  2745. PLSR_TIMER_SETTING *destination)
  2746. {
  2747. destination->prescaler = source->prescaler;
  2748. destination->period = source->period;
  2749. destination->compare = source->compare;
  2750. destination->actualFrequencyHz = source->actualFrequencyHz;
  2751. }
  2752. static void PlsrPlatformToAbSetting(
  2753. const PLSR_PLATFORM_TIMER_SETTING *source,
  2754. PLSR_AB_SETTING *destination)
  2755. {
  2756. destination->basePrescaler = source->prescaler;
  2757. destination->pairPrescaler = source->pairPrescaler;
  2758. destination->period = source->period;
  2759. destination->compare = source->compare;
  2760. destination->actualFrequencyHz = source->actualFrequencyHz;
  2761. }
  2762. static uint8_t PlsrPlatformSettingsDiffer(
  2763. const PLSR_PLATFORM_TIMER_SETTING *first,
  2764. const PLSR_PLATFORM_TIMER_SETTING *second)
  2765. {
  2766. return (((first->actualFrequencyHz != second->actualFrequencyHz)
  2767. || (first->prescaler != second->prescaler)
  2768. || (first->pairPrescaler != second->pairPrescaler)
  2769. || (first->period != second->period)
  2770. || (first->compare != second->compare)) ? 1U : 0U);
  2771. }
  2772. uint8_t PlsrPlatformBuildTimerSetting(
  2773. uint8_t pulseOutput,
  2774. uint8_t outputMode,
  2775. uint32_t requestedFrequencyHz,
  2776. PLSR_PLATFORM_TIMER_SETTING *setting)
  2777. {
  2778. PLSR_TIMER_SETTING timerSetting;
  2779. PLSR_AB_SETTING abSetting;
  2780. if (setting == NULL)
  2781. {
  2782. return 0U;
  2783. }
  2784. if (outputMode == PLSR_OUTPUT_AB)
  2785. {
  2786. if (PlsrAbCalculate(pulseOutput, requestedFrequencyHz,
  2787. &abSetting) == 0U)
  2788. {
  2789. return 0U;
  2790. }
  2791. setting->actualFrequencyHz = abSetting.actualFrequencyHz;
  2792. setting->prescaler = (uint16_t)abSetting.basePrescaler;
  2793. setting->pairPrescaler = (uint16_t)abSetting.pairPrescaler;
  2794. setting->period = (uint16_t)abSetting.period;
  2795. setting->compare = (uint16_t)abSetting.compare;
  2796. return 1U;
  2797. }
  2798. if ((outputMode != PLSR_OUTPUT_PULSE_DIR)
  2799. || (PlsrTimerCalculate(pulseOutput, requestedFrequencyHz,
  2800. &timerSetting) == 0U))
  2801. {
  2802. return 0U;
  2803. }
  2804. setting->actualFrequencyHz = timerSetting.actualFrequencyHz;
  2805. setting->prescaler = (uint16_t)timerSetting.prescaler;
  2806. setting->pairPrescaler = 0U;
  2807. setting->period = (uint16_t)timerSetting.period;
  2808. setting->compare = (uint16_t)timerSetting.compare;
  2809. return 1U;
  2810. }
  2811. uint8_t PlsrPlatformStartPrepared(
  2812. uint8_t pulseOutput,
  2813. const PLSR_PLATFORM_TIMER_SETTING *firstSetting,
  2814. const PLSR_PLATFORM_TIMER_SETTING *queuedSetting,
  2815. uint32_t *actualFirstFrequencyHz,
  2816. uint32_t *actualQueuedFrequencyHz)
  2817. {
  2818. uint8_t outputMode;
  2819. if ((pulseOutput > 3U) || (actualFirstFrequencyHz == NULL)
  2820. || (actualQueuedFrequencyHz == NULL))
  2821. {
  2822. return 0U;
  2823. }
  2824. outputMode = PlsrTimerOutputMode[pulseOutput];
  2825. if ((PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2826. firstSetting) == 0U)
  2827. || (PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2828. queuedSetting) == 0U))
  2829. {
  2830. return 0U;
  2831. }
  2832. if (outputMode == PLSR_OUTPUT_AB)
  2833. {
  2834. PLSR_AB_SETTING firstAbSetting;
  2835. PLSR_AB_SETTING queuedAbSetting;
  2836. if ((PlsrAbStopPending[pulseOutput] != 0U)
  2837. || (PlsrAbFastGated[pulseOutput] != 0U)
  2838. || (PlsrCounterConfigure(pulseOutput, PLSR_OUTPUT_AB) == 0U))
  2839. {
  2840. return 0U;
  2841. }
  2842. PlsrPlatformToAbSetting(firstSetting, &firstAbSetting);
  2843. PlsrPlatformToAbSetting(queuedSetting, &queuedAbSetting);
  2844. PlsrAbLoadAndStart(pulseOutput, &firstAbSetting);
  2845. PlsrAbActiveSetting[pulseOutput] = firstAbSetting;
  2846. PlsrAbPendingSetting[pulseOutput] = queuedAbSetting;
  2847. PlsrAbFrequencyPending[pulseOutput] =
  2848. ((firstSetting->prescaler != queuedSetting->prescaler)
  2849. || (firstSetting->pairPrescaler
  2850. != queuedSetting->pairPrescaler)
  2851. || (firstSetting->period != queuedSetting->period)) ? 1U : 0U;
  2852. }
  2853. else
  2854. {
  2855. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  2856. PLSR_TIMER_SETTING firstTimerSetting;
  2857. PLSR_TIMER_SETTING queuedTimerSetting;
  2858. if (PlsrCounterConfigure(pulseOutput,
  2859. PLSR_OUTPUT_PULSE_DIR) == 0U)
  2860. {
  2861. return 0U;
  2862. }
  2863. PlsrPlatformToTimerSetting(firstSetting, &firstTimerSetting);
  2864. PlsrPlatformToTimerSetting(queuedSetting, &queuedTimerSetting);
  2865. timer->DIER &= ~TIM_DIER_UIE;
  2866. timer->CR1 &= ~TIM_CR1_CEN;
  2867. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2868. timer->CNT = 0UL;
  2869. PlsrTimerWriteSetting(timer, &firstTimerSetting);
  2870. timer->EGR = TIM_EGR_UG;
  2871. PlsrTimerWriteSetting(timer, &queuedTimerSetting);
  2872. timer->CNT = firstTimerSetting.compare;
  2873. timer->SR = 0UL;
  2874. timer->CCER = (timer->CCER
  2875. & ~(TIM_CCER_CC1E | TIM_CCER_CC1P))
  2876. | TIM_CCER_CC1E;
  2877. __DSB();
  2878. timer->DIER |= TIM_DIER_UIE;
  2879. PlsrPulsePinRelease(pulseOutput);
  2880. PlsrCounterBegin(pulseOutput);
  2881. timer->CR1 |= TIM_CR1_CEN;
  2882. }
  2883. PlsrTimerActiveSetting[pulseOutput] = *firstSetting;
  2884. PlsrTimerQueuedSetting[pulseOutput] = *queuedSetting;
  2885. PlsrTimerActiveFrequencyHz[pulseOutput] =
  2886. firstSetting->actualFrequencyHz;
  2887. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  2888. queuedSetting->actualFrequencyHz;
  2889. PlsrTimerQueueGeneration[pulseOutput]++;
  2890. PlsrTimerRunning[pulseOutput] = 1U;
  2891. if (outputMode == PLSR_OUTPUT_AB)
  2892. {
  2893. PlsrAbScheduleFrequencyVerify(pulseOutput);
  2894. }
  2895. else
  2896. {
  2897. PlsrFrequencyVerifyPending[pulseOutput] =
  2898. PLSR_FREQUENCY_VERIFY_NOW;
  2899. }
  2900. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2901. *actualFirstFrequencyHz = firstSetting->actualFrequencyHz;
  2902. *actualQueuedFrequencyHz = queuedSetting->actualFrequencyHz;
  2903. return 1U;
  2904. }
  2905. uint8_t PlsrPlatformSupportsFinitePulseTrain(void)
  2906. {
  2907. return 1U;
  2908. }
  2909. uint8_t PlsrPlatformStartFinitePrepared(
  2910. uint8_t pulseOutput,
  2911. const PLSR_PLATFORM_TIMER_SETTING *setting,
  2912. uint32_t pulseCount,
  2913. uint32_t *actualFrequencyHz)
  2914. {
  2915. TIM_TypeDef *timer;
  2916. TIM_TypeDef *counter;
  2917. PLSR_TIMER_SETTING timerSetting;
  2918. uint32_t firstBlock;
  2919. uint8_t counterIndex;
  2920. if ((pulseOutput > 3U) || (setting == NULL)
  2921. || (pulseCount == 0UL) || (actualFrequencyHz == NULL)
  2922. || (PlsrTimerOutputMode[pulseOutput] != PLSR_OUTPUT_PULSE_DIR)
  2923. || (PlsrPreparedSettingIsValid(pulseOutput,
  2924. PLSR_OUTPUT_PULSE_DIR,
  2925. setting) == 0U)
  2926. || (PlsrCounterConfigure(pulseOutput,
  2927. PLSR_OUTPUT_PULSE_DIR) == 0U))
  2928. {
  2929. return 0U;
  2930. }
  2931. timer = PlsrTimerMap[pulseOutput].timer;
  2932. counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  2933. counter = PlsrCounters[counterIndex];
  2934. firstBlock = (pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  2935. ? PLSR_COUNTER_BLOCK_PULSES : pulseCount;
  2936. PlsrPlatformToTimerSetting(setting, &timerSetting);
  2937. counter->CR1 &= ~TIM_CR1_CEN;
  2938. counter->SMCR &= ~(TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0);
  2939. counter->ARR = (firstBlock == 1UL) ? 1UL : (firstBlock - 1UL);
  2940. counter->CNT = 0UL;
  2941. counter->EGR = TIM_EGR_UG;
  2942. PlsrFiniteCounterPreload[pulseOutput] =
  2943. (firstBlock == 1UL) ? 1U : 0U;
  2944. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  2945. counter->SR = 0UL;
  2946. counter->DIER = TIM_DIER_UIE;
  2947. timer->DIER = 0UL;
  2948. timer->CR1 &= ~TIM_CR1_CEN;
  2949. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2950. timer->CNT = 0UL;
  2951. PlsrTimerWriteSetting(timer, &timerSetting);
  2952. timer->EGR = TIM_EGR_UG;
  2953. /*
  2954. * The board's high-speed output stage is active low. Keep OC1 high
  2955. * while the pin changes from GPIO idle to the timer alternate function,
  2956. * then let the first terminal pulse start at CCR1. Starting at CCR1
  2957. * would expose an uncounted terminal pulse before the first OC rising
  2958. * edge reaches TIM9/TIM12.
  2959. */
  2960. timer->CNT = 0UL;
  2961. timer->SR = 0UL;
  2962. timer->CCER = (timer->CCER
  2963. & ~(TIM_CCER_CC1E | TIM_CCER_CC1P))
  2964. | TIM_CCER_CC1E;
  2965. PlsrFiniteTargetPulses[pulseOutput] = pulseCount;
  2966. PlsrFiniteRemainingPulses[pulseOutput] = pulseCount;
  2967. PlsrFiniteCompletionPending[pulseOutput] = 0U;
  2968. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  2969. PlsrFiniteStreamActive[pulseOutput] = 0U;
  2970. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  2971. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  2972. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  2973. PlsrFiniteActive[pulseOutput] = 1U;
  2974. PlsrFiniteStepCount[pulseOutput] = 0U;
  2975. PlsrFiniteStepIndex[pulseOutput] = 0U;
  2976. PlsrFiniteBoundaryReadIndex[pulseOutput] = 0U;
  2977. PlsrFiniteCompletedStepCount[pulseOutput] = 0U;
  2978. PlsrTimerActiveSetting[pulseOutput] = *setting;
  2979. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  2980. PlsrTimerActiveFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  2981. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  2982. PlsrTimerRunning[pulseOutput] = 1U;
  2983. *actualFrequencyHz = setting->actualFrequencyHz;
  2984. __DSB();
  2985. PlsrPulsePinRelease(pulseOutput);
  2986. PlsrCounterBegin(pulseOutput);
  2987. timer->CR1 |= TIM_CR1_CEN;
  2988. return 1U;
  2989. }
  2990. uint8_t PlsrPlatformStartCountedStreamPrepared(
  2991. uint8_t pulseOutput,
  2992. const PLSR_PLATFORM_TIMER_SETTING *setting,
  2993. uint32_t pulseCount,
  2994. uint32_t *actualFrequencyHz)
  2995. {
  2996. TIM_TypeDef *counter;
  2997. uint32_t firstBlock;
  2998. if (PlsrPlatformStartFinitePrepared(pulseOutput, setting, pulseCount,
  2999. actualFrequencyHz) == 0U)
  3000. {
  3001. return 0U;
  3002. }
  3003. PlsrFiniteStreamActive[pulseOutput] = 1U;
  3004. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  3005. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  3006. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  3007. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  3008. firstBlock = (pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  3009. ? PLSR_COUNTER_BLOCK_PULSES : pulseCount;
  3010. counter = PlsrCounters[PlsrCounterIndexByOutput[pulseOutput]];
  3011. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, firstBlock);
  3012. return 1U;
  3013. }
  3014. uint8_t PlsrPlatformStartFiniteSequencePrepared(
  3015. uint8_t pulseOutput,
  3016. PLSR_PLATFORM_FINITE_STEP *steps,
  3017. uint16_t stepCount,
  3018. uint32_t *actualFrequencyHz)
  3019. {
  3020. TIM_TypeDef *counter;
  3021. uint16_t index;
  3022. if ((steps == NULL) || (stepCount == 0U)
  3023. || (stepCount > PLSR_PLATFORM_FINITE_STEP_MAX))
  3024. {
  3025. return 0U;
  3026. }
  3027. for (index = 0U; index < stepCount; index++)
  3028. {
  3029. if ((steps[index].pulseCount == 0UL)
  3030. || (steps[index].segmentNumber == 0U)
  3031. || (PlsrPreparedSettingIsValid(
  3032. pulseOutput, PLSR_OUTPUT_PULSE_DIR,
  3033. &steps[index].setting) == 0U))
  3034. {
  3035. return 0U;
  3036. }
  3037. }
  3038. if (PlsrPlatformStartFinitePrepared(
  3039. pulseOutput, &steps[0].setting, steps[0].pulseCount,
  3040. actualFrequencyHz) == 0U)
  3041. {
  3042. return 0U;
  3043. }
  3044. PlsrFiniteSteps[pulseOutput] = steps;
  3045. PlsrFiniteStepCount[pulseOutput] = stepCount;
  3046. PlsrFiniteStepIndex[pulseOutput] = 0U;
  3047. PlsrFiniteBoundaryReadIndex[pulseOutput] = 0U;
  3048. PlsrFiniteCompletedStepCount[pulseOutput] = 0U;
  3049. if (stepCount > 1U)
  3050. {
  3051. counter = PlsrCounters[PlsrCounterIndexByOutput[pulseOutput]];
  3052. PlsrFiniteArmNextStepPrepare(
  3053. pulseOutput, counter,
  3054. (steps[0].pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  3055. ? PLSR_COUNTER_BLOCK_PULSES : steps[0].pulseCount);
  3056. }
  3057. return 1U;
  3058. }
  3059. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformUpdateFinitePrepared(
  3060. uint8_t pulseOutput,
  3061. const PLSR_PLATFORM_TIMER_SETTING *setting,
  3062. uint32_t *actualFrequencyHz)
  3063. {
  3064. TIM_TypeDef *timer;
  3065. uint32_t activePeriod;
  3066. uint32_t counter;
  3067. uint32_t criticalState;
  3068. if ((pulseOutput > 3U) || (setting == NULL)
  3069. || (actualFrequencyHz == NULL)
  3070. || (PlsrFiniteActive[pulseOutput] == 0U)
  3071. || (PlsrPreparedSettingIsValid(pulseOutput,
  3072. PLSR_OUTPUT_PULSE_DIR,
  3073. setting) == 0U))
  3074. {
  3075. return PLSR_PLATFORM_QUEUE_STALE;
  3076. }
  3077. timer = PlsrTimerMap[pulseOutput].timer;
  3078. criticalState = PlsrPlatformEnterCritical();
  3079. if ((PlsrFiniteActive[pulseOutput] == 0U)
  3080. || ((timer->CR1 & TIM_CR1_CEN) == 0UL))
  3081. {
  3082. PlsrPlatformExitCritical(criticalState);
  3083. return PLSR_PLATFORM_QUEUE_STALE;
  3084. }
  3085. if (PlsrFiniteFrequencyPending[pulseOutput] != 0U)
  3086. {
  3087. if ((timer->SR & TIM_SR_UIF) == 0UL)
  3088. {
  3089. PlsrPlatformExitCritical(criticalState);
  3090. return PLSR_PLATFORM_QUEUE_STALE;
  3091. }
  3092. /* The update IRQ normally commits this preload. Also consume a
  3093. latched update here so a delayed/shared IRQ cannot stall a ramp. */
  3094. timer->SR = ~TIM_SR_UIF;
  3095. timer->DIER &= ~TIM_DIER_UIE;
  3096. PlsrTimerActiveFrequencyHz[pulseOutput] =
  3097. PlsrTimerQueuedFrequencyHz[pulseOutput];
  3098. PlsrTimerActiveSetting[pulseOutput] =
  3099. PlsrTimerQueuedSetting[pulseOutput];
  3100. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  3101. }
  3102. activePeriod = PlsrTimerActiveSetting[pulseOutput].period;
  3103. counter = timer->CNT;
  3104. if ((counter > activePeriod)
  3105. || ((activePeriod - counter) < PLSR_FINITE_WRITE_GUARD_COUNTS))
  3106. {
  3107. PlsrPlatformExitCritical(criticalState);
  3108. return PLSR_PLATFORM_QUEUE_STALE;
  3109. }
  3110. timer->PSC = setting->prescaler;
  3111. timer->ARR = setting->period;
  3112. timer->CCR1 = setting->compare;
  3113. __DMB();
  3114. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  3115. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  3116. PlsrTimerQueueGeneration[pulseOutput]++;
  3117. PlsrFiniteFrequencyPending[pulseOutput] = 1U;
  3118. timer->SR = ~TIM_SR_UIF;
  3119. timer->DIER |= TIM_DIER_UIE;
  3120. *actualFrequencyHz = setting->actualFrequencyHz;
  3121. PlsrPlatformExitCritical(criticalState);
  3122. return PLSR_PLATFORM_QUEUE_APPLIED;
  3123. }
  3124. uint8_t PlsrPlatformRetargetFiniteStop(uint8_t pulseOutput,
  3125. uint32_t drainPulses)
  3126. {
  3127. TIM_TypeDef *timer;
  3128. if ((pulseOutput > 3U) || (drainPulses == 0UL)
  3129. || (PlsrFiniteActive[pulseOutput] == 0U)
  3130. || (PlsrCounterIndexByOutput[pulseOutput] >= PLSR_COUNTER_COUNT)
  3131. || (PlsrFiniteRetargetPending[pulseOutput] != 0U)
  3132. || (PlsrFiniteTailStopPending[pulseOutput] != 0U))
  3133. {
  3134. return 0U;
  3135. }
  3136. timer = PlsrTimerMap[pulseOutput].timer;
  3137. PlsrFiniteRetargetDrainPulses[pulseOutput] = drainPulses;
  3138. PlsrFiniteRetargetPending[pulseOutput] = 1U;
  3139. timer->SR = ~TIM_SR_CC1IF;
  3140. timer->DIER |= TIM_DIER_CC1IE;
  3141. __DMB();
  3142. return 1U;
  3143. }
  3144. uint8_t PlsrPlatformFiniteRetargetReady(uint8_t pulseOutput,
  3145. uint32_t *activeFrequencyHz)
  3146. {
  3147. uint32_t criticalState;
  3148. uint8_t ready;
  3149. if ((pulseOutput > 3U) || (activeFrequencyHz == NULL))
  3150. {
  3151. return 0U;
  3152. }
  3153. criticalState = PlsrPlatformEnterCritical();
  3154. ready = ((PlsrFiniteActive[pulseOutput] != 0U)
  3155. && (PlsrFiniteRetargetPending[pulseOutput] == 0U)
  3156. && (PlsrFiniteStepCount[pulseOutput] == 0U)) ? 1U : 0U;
  3157. *activeFrequencyHz = PlsrTimerActiveFrequencyHz[pulseOutput];
  3158. PlsrPlatformExitCritical(criticalState);
  3159. return ready;
  3160. }
  3161. uint8_t PlsrPlatformFinitePipelineSnapshot(uint8_t pulseOutput,
  3162. uint32_t *committedPulses,
  3163. uint32_t *tailFrequencyHz,
  3164. uint8_t *startsNextSegment)
  3165. {
  3166. TIM_TypeDef *counter;
  3167. uint32_t criticalState;
  3168. uint32_t blockCount;
  3169. uint32_t remaining;
  3170. if ((pulseOutput > 3U) || (committedPulses == NULL)
  3171. || (tailFrequencyHz == NULL) || (startsNextSegment == NULL))
  3172. {
  3173. return 0U;
  3174. }
  3175. criticalState = PlsrPlatformEnterCritical();
  3176. if ((PlsrFiniteActive[pulseOutput] == 0U)
  3177. || (PlsrFiniteStreamActive[pulseOutput] == 0U)
  3178. || (PlsrCounterIndexByOutput[pulseOutput] >= PLSR_COUNTER_COUNT))
  3179. {
  3180. PlsrPlatformExitCritical(criticalState);
  3181. return 0U;
  3182. }
  3183. counter = PlsrCounters[PlsrCounterIndexByOutput[pulseOutput]];
  3184. blockCount = (uint16_t)counter->CNT;
  3185. if (blockCount >= PlsrFiniteCounterPreload[pulseOutput])
  3186. {
  3187. blockCount -= PlsrFiniteCounterPreload[pulseOutput];
  3188. }
  3189. else
  3190. {
  3191. blockCount = 0UL;
  3192. }
  3193. remaining = PlsrFiniteRemainingPulses[pulseOutput];
  3194. if (blockCount > remaining)
  3195. {
  3196. blockCount = remaining;
  3197. }
  3198. remaining -= blockCount;
  3199. *tailFrequencyHz = PlsrTimerActiveFrequencyHz[pulseOutput];
  3200. *startsNextSegment = 0U;
  3201. if (PlsrFiniteStreamNextValid[pulseOutput] != 0U)
  3202. {
  3203. uint32_t nextPulses = PlsrFiniteStreamNextPulses[pulseOutput];
  3204. remaining = (nextPulses > (0xFFFFFFFFUL - remaining))
  3205. ? 0xFFFFFFFFUL : remaining + nextPulses;
  3206. *tailFrequencyHz =
  3207. PlsrFiniteStreamNextSetting[pulseOutput].actualFrequencyHz;
  3208. *startsNextSegment =
  3209. PlsrFiniteStreamNextStartsSegment[pulseOutput];
  3210. }
  3211. *committedPulses = remaining;
  3212. PlsrPlatformExitCritical(criticalState);
  3213. return 1U;
  3214. }
  3215. uint8_t PlsrPlatformFiniteProgress(uint8_t pulseOutput,
  3216. uint32_t *completedPulses)
  3217. {
  3218. uint8_t counterIndex;
  3219. TIM_TypeDef *counter;
  3220. uint32_t criticalState;
  3221. uint32_t completed;
  3222. if ((pulseOutput > 3U) || (completedPulses == NULL)
  3223. || ((PlsrFiniteActive[pulseOutput] == 0U)
  3224. && (PlsrFiniteCompletionPending[pulseOutput] == 0U)))
  3225. {
  3226. return 0U;
  3227. }
  3228. criticalState = PlsrPlatformEnterCritical();
  3229. counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  3230. if (PlsrFiniteCompletionPending[pulseOutput] != 0U)
  3231. {
  3232. completed = PlsrFiniteTargetPulses[pulseOutput];
  3233. }
  3234. else if (counterIndex < PLSR_COUNTER_COUNT)
  3235. {
  3236. counter = PlsrCounters[counterIndex];
  3237. uint32_t blockCount = (uint16_t)counter->CNT;
  3238. if (blockCount >= PlsrFiniteCounterPreload[pulseOutput])
  3239. {
  3240. blockCount -= PlsrFiniteCounterPreload[pulseOutput];
  3241. }
  3242. completed = PlsrFiniteTargetPulses[pulseOutput]
  3243. - PlsrFiniteRemainingPulses[pulseOutput]
  3244. + blockCount;
  3245. if (completed > PlsrFiniteTargetPulses[pulseOutput])
  3246. {
  3247. completed = PlsrFiniteTargetPulses[pulseOutput];
  3248. }
  3249. }
  3250. else
  3251. {
  3252. PlsrPlatformExitCritical(criticalState);
  3253. return 0U;
  3254. }
  3255. if (PlsrFiniteStepCount[pulseOutput] != 0U)
  3256. {
  3257. completed += PlsrFiniteSteps[pulseOutput][
  3258. PlsrFiniteStepIndex[pulseOutput]].segmentPulseOffset;
  3259. }
  3260. *completedPulses = completed;
  3261. PlsrPlatformExitCritical(criticalState);
  3262. return 1U;
  3263. }
  3264. uint8_t PlsrPlatformTakeFiniteCompletion(uint8_t pulseOutput,
  3265. uint32_t *completedPulses)
  3266. {
  3267. uint8_t counterIndex;
  3268. TIM_TypeDef *counter;
  3269. if ((pulseOutput > 3U) || (completedPulses == NULL)
  3270. || (PlsrFiniteCompletionPending[pulseOutput] == 0U))
  3271. {
  3272. return 0U;
  3273. }
  3274. counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  3275. if (PlsrFiniteStepCount[pulseOutput] != 0U)
  3276. {
  3277. const PLSR_PLATFORM_FINITE_STEP *step =
  3278. &PlsrFiniteSteps[pulseOutput][
  3279. PlsrFiniteStepCount[pulseOutput] - 1U];
  3280. *completedPulses = step->segmentPulseOffset + step->pulseCount;
  3281. }
  3282. else
  3283. {
  3284. *completedPulses = PlsrFiniteTargetPulses[pulseOutput];
  3285. }
  3286. if ((PlsrFiniteStepCount[pulseOutput] == 0U)
  3287. && (PlsrFiniteStreamActive[pulseOutput] == 0U))
  3288. {
  3289. PlsrObservedPulseBase[pulseOutput] += *completedPulses;
  3290. }
  3291. PlsrObservedPulsePublished[pulseOutput] =
  3292. PlsrObservedPulseBase[pulseOutput];
  3293. PlsrFiniteCompletionPending[pulseOutput] = 0U;
  3294. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  3295. PlsrFiniteTargetPulses[pulseOutput] = 0UL;
  3296. PlsrFiniteRemainingPulses[pulseOutput] = 0UL;
  3297. PlsrFiniteCounterPreload[pulseOutput] = 0U;
  3298. PlsrFiniteStepCount[pulseOutput] = 0U;
  3299. PlsrFiniteStepIndex[pulseOutput] = 0U;
  3300. PlsrFiniteStreamActive[pulseOutput] = 0U;
  3301. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  3302. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  3303. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  3304. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  3305. PlsrTimerActiveFrequencyHz[pulseOutput] = 0UL;
  3306. PlsrTimerQueuedFrequencyHz[pulseOutput] = 0UL;
  3307. PlsrTimerRunning[pulseOutput] = 0U;
  3308. if (counterIndex < PLSR_COUNTER_COUNT)
  3309. {
  3310. counter = PlsrCounters[counterIndex];
  3311. counter->CR1 = 0UL;
  3312. counter->DIER = 0UL;
  3313. counter->SMCR = 0UL;
  3314. counter->SR = 0UL;
  3315. PlsrCounterOverflowPulses[counterIndex] = 0UL;
  3316. PlsrCounterOwner[counterIndex] = PLSR_COUNTER_NONE;
  3317. }
  3318. PlsrCounterIndexByOutput[pulseOutput] = PLSR_COUNTER_NONE;
  3319. return 1U;
  3320. }
  3321. uint8_t PlsrPlatformTakeFiniteBoundary(uint8_t pulseOutput,
  3322. uint8_t *segmentNumber,
  3323. uint32_t *completedPulses,
  3324. uint8_t *sequenceContinues,
  3325. uint32_t *activeFrequencyHz)
  3326. {
  3327. uint32_t criticalState;
  3328. uint16_t readIndex;
  3329. uint16_t completedCount;
  3330. if ((pulseOutput > 3U) || (segmentNumber == NULL)
  3331. || (completedPulses == NULL) || (sequenceContinues == NULL)
  3332. || (activeFrequencyHz == NULL))
  3333. {
  3334. return 0U;
  3335. }
  3336. criticalState = PlsrPlatformEnterCritical();
  3337. readIndex = PlsrFiniteBoundaryReadIndex[pulseOutput];
  3338. completedCount = PlsrFiniteCompletedStepCount[pulseOutput];
  3339. while (readIndex < completedCount)
  3340. {
  3341. uint16_t index = readIndex++;
  3342. const PLSR_PLATFORM_FINITE_STEP *step =
  3343. &PlsrFiniteSteps[pulseOutput][index];
  3344. PlsrFiniteBoundaryReadIndex[pulseOutput] = readIndex;
  3345. if (step->completesSegment != 0U)
  3346. {
  3347. *segmentNumber = step->segmentNumber;
  3348. *completedPulses = step->segmentPulseOffset + step->pulseCount;
  3349. *sequenceContinues =
  3350. (index + 1U < PlsrFiniteStepCount[pulseOutput]) ? 1U : 0U;
  3351. *activeFrequencyHz = (*sequenceContinues != 0U)
  3352. ? PlsrFiniteSteps[pulseOutput][index + 1U]
  3353. .setting.actualFrequencyHz
  3354. : step->setting.actualFrequencyHz;
  3355. PlsrPlatformExitCritical(criticalState);
  3356. return 1U;
  3357. }
  3358. }
  3359. PlsrPlatformExitCritical(criticalState);
  3360. return 0U;
  3361. }
  3362. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformLoadPreparedFromIrq(
  3363. uint8_t pulseOutput,
  3364. const PLSR_PLATFORM_TIMER_SETTING *setting,
  3365. uint32_t *actualFrequencyHz)
  3366. {
  3367. uint8_t outputMode;
  3368. if ((pulseOutput > 3U) || (actualFrequencyHz == NULL))
  3369. {
  3370. return PLSR_PLATFORM_QUEUE_FAILED;
  3371. }
  3372. outputMode = PlsrTimerOutputMode[pulseOutput];
  3373. if (PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  3374. setting) == 0U)
  3375. {
  3376. return PLSR_PLATFORM_QUEUE_FAILED;
  3377. }
  3378. if ((PlsrTimerRunning[pulseOutput] == 0U)
  3379. || (PlsrAbStopPending[pulseOutput] != 0U)
  3380. || (PlsrAbFastGated[pulseOutput] != 0U))
  3381. {
  3382. return PLSR_PLATFORM_QUEUE_STALE;
  3383. }
  3384. if (outputMode == PLSR_OUTPUT_AB)
  3385. {
  3386. PLSR_AB_SETTING pending;
  3387. PlsrPlatformToAbSetting(setting, &pending);
  3388. PlsrAbPendingSetting[pulseOutput] = pending;
  3389. PlsrAbFrequencyPending[pulseOutput] =
  3390. ((setting->prescaler
  3391. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  3392. || (setting->pairPrescaler
  3393. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  3394. || (setting->period
  3395. != PlsrAbActiveSetting[pulseOutput].period)) ? 1U : 0U;
  3396. }
  3397. else
  3398. {
  3399. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  3400. if ((timer->CR1 & TIM_CR1_CEN) == 0UL)
  3401. {
  3402. return PLSR_PLATFORM_QUEUE_STALE;
  3403. }
  3404. timer->PSC = setting->prescaler;
  3405. timer->ARR = setting->period;
  3406. timer->CCR1 = setting->compare;
  3407. __DMB();
  3408. }
  3409. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  3410. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  3411. PlsrTimerQueueGeneration[pulseOutput]++;
  3412. *actualFrequencyHz = setting->actualFrequencyHz;
  3413. return PLSR_PLATFORM_QUEUE_APPLIED;
  3414. }
  3415. void PlsrPlatformGateFromIrq(uint8_t pulseOutput)
  3416. {
  3417. if ((pulseOutput <= 3U)
  3418. && (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3419. && (PlsrAbFastGated[pulseOutput] != 0U))
  3420. {
  3421. return;
  3422. }
  3423. PlsrPlatformStopPulse(pulseOutput);
  3424. }
  3425. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformQueueFrequency(
  3426. uint8_t pulseOutput,
  3427. uint32_t frequencyHz,
  3428. uint32_t *actualFrequencyHz)
  3429. {
  3430. TIM_TypeDef *timer;
  3431. PLSR_PLATFORM_TIMER_SETTING setting;
  3432. uint32_t activePeriod;
  3433. uint32_t counter;
  3434. uint32_t criticalState;
  3435. uint32_t ownGeneration;
  3436. uint8_t updatePending;
  3437. if ((pulseOutput > 3U) || (actualFrequencyHz == NULL)
  3438. || (PlsrPlatformBuildTimerSetting(
  3439. pulseOutput, PlsrTimerOutputMode[pulseOutput], frequencyHz,
  3440. &setting) == 0U))
  3441. {
  3442. return PLSR_PLATFORM_QUEUE_FAILED;
  3443. }
  3444. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3445. {
  3446. PLSR_AB_SETTING pending;
  3447. uint32_t criticalState;
  3448. if ((PlsrTimerRunning[pulseOutput] == 0U)
  3449. || (PlsrAbStopPending[pulseOutput] != 0U)
  3450. || (PlsrAbFastGated[pulseOutput] != 0U))
  3451. {
  3452. return PLSR_PLATFORM_QUEUE_STALE;
  3453. }
  3454. criticalState = PlsrPlatformEnterCritical();
  3455. if ((PlsrTimerRunning[pulseOutput] == 0U)
  3456. || (PlsrAbStopPending[pulseOutput] != 0U)
  3457. || (PlsrAbFastGated[pulseOutput] != 0U))
  3458. {
  3459. PlsrPlatformExitCritical(criticalState);
  3460. return PLSR_PLATFORM_QUEUE_STALE;
  3461. }
  3462. PlsrPlatformToAbSetting(&setting, &pending);
  3463. PlsrAbPendingSetting[pulseOutput] = pending;
  3464. PlsrAbFrequencyPending[pulseOutput] =
  3465. ((pending.basePrescaler
  3466. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  3467. || (pending.pairPrescaler
  3468. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  3469. || (pending.period
  3470. != PlsrAbActiveSetting[pulseOutput].period)) ? 1U : 0U;
  3471. PlsrTimerQueuedSetting[pulseOutput] = setting;
  3472. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting.actualFrequencyHz;
  3473. PlsrTimerQueueGeneration[pulseOutput]++;
  3474. *actualFrequencyHz = setting.actualFrequencyHz;
  3475. PlsrPlatformExitCritical(criticalState);
  3476. return PLSR_PLATFORM_QUEUE_APPLIED;
  3477. }
  3478. timer = PlsrTimerMap[pulseOutput].timer;
  3479. if ((timer->CR1 & TIM_CR1_CEN) == 0UL)
  3480. {
  3481. return PLSR_PLATFORM_QUEUE_STALE;
  3482. }
  3483. criticalState = PlsrPlatformEnterCritical();
  3484. if ((timer->SR & TIM_SR_UIF) != 0UL)
  3485. {
  3486. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  3487. PlsrPlatformExitCritical(criticalState);
  3488. return PLSR_PLATFORM_QUEUE_STALE;
  3489. }
  3490. activePeriod = PlsrTimerActiveSetting[pulseOutput].period;
  3491. counter = timer->CNT;
  3492. if ((counter > activePeriod)
  3493. || ((activePeriod - counter) < PLSR_QUEUE_WRITE_GUARD_COUNTS))
  3494. {
  3495. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  3496. PlsrPlatformExitCritical(criticalState);
  3497. return PLSR_PLATFORM_QUEUE_STALE;
  3498. }
  3499. /* Preserve real update events and their TRGO pulse while replacing the
  3500. three preload registers. The near-wrap guard bounds the write window. */
  3501. timer->PSC = setting.prescaler;
  3502. timer->ARR = setting.period;
  3503. timer->CCR1 = setting.compare;
  3504. __DMB();
  3505. updatePending = ((timer->SR & TIM_SR_UIF) != 0UL) ? 1U : 0U;
  3506. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting.actualFrequencyHz;
  3507. PlsrTimerQueuedSetting[pulseOutput] = setting;
  3508. PlsrTimerQueueGeneration[pulseOutput]++;
  3509. ownGeneration = PlsrTimerQueueGeneration[pulseOutput];
  3510. if (updatePending != 0U)
  3511. {
  3512. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  3513. PlsrPlatformExitCritical(criticalState);
  3514. return PLSR_PLATFORM_QUEUE_STALE;
  3515. }
  3516. *actualFrequencyHz =
  3517. (PlsrTimerQueueGeneration[pulseOutput] == ownGeneration)
  3518. ? setting.actualFrequencyHz
  3519. : PlsrTimerQueuedFrequencyHz[pulseOutput];
  3520. if (PlsrTimerQueueGeneration[pulseOutput] != ownGeneration)
  3521. {
  3522. PlsrPlatformExitCritical(criticalState);
  3523. return PLSR_PLATFORM_QUEUE_STALE;
  3524. }
  3525. PlsrPlatformExitCritical(criticalState);
  3526. return PLSR_PLATFORM_QUEUE_APPLIED;
  3527. }
  3528. void PlsrPlatformDrainPendingPulse(uint8_t pulseOutput)
  3529. {
  3530. if (pulseOutput <= 3U)
  3531. {
  3532. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3533. && (PlsrDeferredPulsePending[pulseOutput] != 0U))
  3534. {
  3535. uint8_t verifyOutput =
  3536. (PlsrAbLagAxis[pulseOutput] == pulseOutput)
  3537. ? (uint8_t)(pulseOutput + 1U) : pulseOutput;
  3538. TIM_TypeDef *verifyTimer = PlsrTimerMap[verifyOutput].timer;
  3539. verifyTimer->DIER &= ~(TIM_DIER_UIE | TIM_DIER_CC1IE);
  3540. verifyTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3541. PlsrAbVerifyOwner[verifyOutput] = PLSR_COUNTER_NONE;
  3542. if (PlsrFrequencyVerifyPending[pulseOutput]
  3543. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  3544. {
  3545. PlsrFrequencyVerifyPending[pulseOutput] =
  3546. PLSR_FREQUENCY_VERIFY_NONE;
  3547. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3548. (void)PlsrVerifyActiveFrequency(pulseOutput);
  3549. }
  3550. PlsrDeferredPulsePending[pulseOutput] = 0U;
  3551. PlsrPulseTimerIrq(pulseOutput);
  3552. return;
  3553. }
  3554. PlsrHandleTimerIrq(pulseOutput);
  3555. }
  3556. }
  3557. uint32_t PlsrPlatformActiveFrequency(uint8_t pulseOutput)
  3558. {
  3559. if (pulseOutput > 3U)
  3560. {
  3561. return 0UL;
  3562. }
  3563. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3564. && (PlsrAbFastGated[pulseOutput] != 0U))
  3565. {
  3566. PlsrFrequencyVerifyPending[pulseOutput] =
  3567. PLSR_FREQUENCY_VERIFY_NONE;
  3568. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3569. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3570. }
  3571. if (PlsrFrequencyVerifyPending[pulseOutput]
  3572. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  3573. {
  3574. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3575. }
  3576. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3577. {
  3578. PlsrFrequencyVerifyPulseCount[pulseOutput]++;
  3579. if (PlsrFrequencyVerifyPulseCount[pulseOutput]
  3580. >= PLSR_STRUCTURE_VERIFY_INTERVAL)
  3581. {
  3582. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3583. PlsrAbScheduleFrequencyVerify(pulseOutput);
  3584. }
  3585. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3586. }
  3587. if (PlsrFrequencyVerifyPending[pulseOutput]
  3588. == PLSR_FREQUENCY_VERIFY_NOW)
  3589. {
  3590. PlsrFrequencyVerifyPending[pulseOutput] =
  3591. PLSR_FREQUENCY_VERIFY_NONE;
  3592. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3593. return PlsrVerifyActiveFrequency(pulseOutput);
  3594. }
  3595. PlsrFrequencyVerifyPulseCount[pulseOutput]++;
  3596. if (PlsrFrequencyVerifyPulseCount[pulseOutput]
  3597. >= PLSR_STRUCTURE_VERIFY_INTERVAL)
  3598. {
  3599. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3600. return PlsrVerifyActiveFrequency(pulseOutput);
  3601. }
  3602. return PlsrTimerActiveFrequencyHz[pulseOutput];
  3603. }
  3604. uint8_t PlsrPlatformExpectedFrequency(uint8_t pulseOutput,
  3605. uint8_t outputMode,
  3606. uint32_t requestedFrequencyHz,
  3607. uint32_t *actualFrequencyHz)
  3608. {
  3609. PLSR_PLATFORM_TIMER_SETTING setting;
  3610. if (actualFrequencyHz == NULL)
  3611. {
  3612. return 0U;
  3613. }
  3614. if (PlsrPlatformBuildTimerSetting(pulseOutput, outputMode,
  3615. requestedFrequencyHz,
  3616. &setting) == 0U)
  3617. {
  3618. return 0U;
  3619. }
  3620. *actualFrequencyHz = setting.actualFrequencyHz;
  3621. return 1U;
  3622. }
  3623. uint64_t PlsrPlatformObservedPulses(uint8_t pulseOutput)
  3624. {
  3625. uint32_t criticalState;
  3626. uint64_t observed;
  3627. if (pulseOutput > 3U)
  3628. {
  3629. return 0UL;
  3630. }
  3631. criticalState = PlsrPlatformEnterCritical();
  3632. if (PlsrCounterIndexByOutput[pulseOutput] < PLSR_COUNTER_COUNT)
  3633. {
  3634. observed = ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3635. && (PlsrAbFastGated[pulseOutput] != 0U))
  3636. ? PlsrCounterSnapshotStopped(pulseOutput)
  3637. : PlsrCounterSnapshot(pulseOutput);
  3638. }
  3639. else
  3640. {
  3641. observed = PlsrObservedPulseBase[pulseOutput];
  3642. }
  3643. PlsrPlatformExitCritical(criticalState);
  3644. return observed;
  3645. }
  3646. uint16_t PlsrPlatformDiagnosticFault(void)
  3647. {
  3648. uint16_t fault = PlsrPlatformFaultPending;
  3649. PlsrPlatformFaultPending = 0U;
  3650. return fault;
  3651. }
  3652. PLSR_PLATFORM_STOP_RESULT PlsrPlatformRequestStopLocked(
  3653. uint8_t pulseOutput,
  3654. uint8_t requireZeroBoundary)
  3655. {
  3656. if (pulseOutput > 3U)
  3657. {
  3658. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  3659. }
  3660. if ((requireZeroBoundary != 0U)
  3661. && (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3662. && (PlsrTimerRunning[pulseOutput] != 0U)
  3663. && (PlsrAbFastGated[pulseOutput] == 0U))
  3664. {
  3665. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  3666. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  3667. uint32_t activeFrequencyHz;
  3668. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  3669. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  3670. uint8_t structureRunnable =
  3671. ((PlsrAbStructureVerified[pulseOutput] != 0U)
  3672. && (PlsrFrequencyVerifyPending[pulseOutput]
  3673. == PLSR_FREQUENCY_VERIFY_NONE))
  3674. ? PlsrAbStopBoundaryIsReachable(pulseOutput, baseTimer,
  3675. pairTimer)
  3676. : PlsrAbTimersAreRunnable(pulseOutput, baseTimer,
  3677. pairTimer);
  3678. if (structureRunnable == 0U)
  3679. {
  3680. PlsrCounterSuspend(pulseOutput);
  3681. PlsrAbHoldPairIdle(pulseOutput);
  3682. baseTimer->CR1 &= ~TIM_CR1_CEN;
  3683. pairTimer->CR1 &= ~TIM_CR1_CEN;
  3684. __DMB();
  3685. PlsrPlatformStopPulse(pulseOutput);
  3686. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  3687. }
  3688. if (PlsrAbStopPending[pulseOutput] != 0U)
  3689. {
  3690. return PLSR_PLATFORM_STOP_PENDING;
  3691. }
  3692. PlsrAbFrequencyPending[pulseOutput] = 0U;
  3693. baseTimer->DIER &= ~TIM_DIER_UIE;
  3694. pairTimer->DIER &= ~TIM_DIER_UIE;
  3695. PlsrFrequencyVerifyPending[pulseOutput] =
  3696. PLSR_FREQUENCY_VERIFY_NONE;
  3697. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  3698. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  3699. PlsrAbVerifyOwner[pairOutput] = PLSR_COUNTER_NONE;
  3700. activeFrequencyHz = PlsrTimerActiveFrequencyHz[pulseOutput];
  3701. PlsrTimerQueuedFrequencyHz[pulseOutput] = activeFrequencyHz;
  3702. PlsrTimerQueueGeneration[pulseOutput]++;
  3703. PlsrAbStopPending[pulseOutput] = 1U;
  3704. #if PLSR_DEBUG_TIMING
  3705. if (PlsrFinalArmQueueTimingPending[pulseOutput] != 0U)
  3706. {
  3707. uint32_t queuedAt = PlsrFinalArmQueuedAt[pulseOutput];
  3708. uint32_t stoppedAt = DWT->CYCCNT;
  3709. uint32_t latency = stoppedAt - queuedAt;
  3710. PlsrFinalArmQueueTimingPending[pulseOutput] = 0U;
  3711. PlsrFinalArmQueueToStopLastCycles[pulseOutput] = latency;
  3712. if (latency > PlsrFinalArmQueueToStopMaxCycles[pulseOutput])
  3713. {
  3714. PlsrFinalArmQueueToStopMaxCycles[pulseOutput] = latency;
  3715. }
  3716. }
  3717. #endif
  3718. if (lagOutput == pulseOutput)
  3719. {
  3720. pairTimer->SR = ~TIM_SR_CC1IF;
  3721. }
  3722. else
  3723. {
  3724. baseTimer->SR = ~TIM_SR_CC1IF;
  3725. }
  3726. baseTimer->DIER |= TIM_DIER_CC1IE;
  3727. pairTimer->DIER |= TIM_DIER_CC1IE;
  3728. __DMB();
  3729. return PLSR_PLATFORM_STOP_PENDING;
  3730. }
  3731. PlsrPlatformStopPulse(pulseOutput);
  3732. return PLSR_PLATFORM_STOP_COMPLETE;
  3733. }
  3734. uint8_t PlsrPlatformQueueFinalArmFromIrq(uint8_t pulseOutput)
  3735. {
  3736. uint8_t jobOutput;
  3737. if ((pulseOutput > 2U) || ((pulseOutput & 1U) != 0U)
  3738. || (PlsrTimerOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  3739. || (PlsrTimerRunning[pulseOutput] == 0U)
  3740. || (PlsrAbFastGated[pulseOutput] != 0U))
  3741. {
  3742. return 0U;
  3743. }
  3744. jobOutput = PlsrFinalArmJobOutput(pulseOutput);
  3745. if (PlsrAbFinalArmJobOwner[jobOutput] != PLSR_COUNTER_NONE)
  3746. {
  3747. return 0U;
  3748. }
  3749. PlsrAbFinalArmJobOwner[jobOutput] = pulseOutput;
  3750. #if PLSR_DEBUG_TIMING
  3751. PlsrFinalArmQueuedAt[pulseOutput] = DWT->CYCCNT;
  3752. PlsrFinalArmQueueTimingPending[pulseOutput] = 1U;
  3753. PlsrFinalArmQueueCount[pulseOutput]++;
  3754. #endif
  3755. __DMB();
  3756. NVIC_SetPendingIRQ(PlsrTimerMap[jobOutput].irq);
  3757. return 1U;
  3758. }
  3759. void PlsrPlatformStopPulse(uint8_t pulseOutput)
  3760. {
  3761. if (pulseOutput <= 3U)
  3762. {
  3763. PlsrFiniteActive[pulseOutput] = 0U;
  3764. PlsrFiniteCompletionPending[pulseOutput] = 0U;
  3765. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  3766. PlsrFiniteRetargetPending[pulseOutput] = 0U;
  3767. PlsrFiniteTailStopPending[pulseOutput] = 0U;
  3768. PlsrFiniteRetargetDrainPulses[pulseOutput] = 0UL;
  3769. PlsrFiniteTargetPulses[pulseOutput] = 0UL;
  3770. PlsrFiniteRemainingPulses[pulseOutput] = 0UL;
  3771. PlsrFiniteStreamActive[pulseOutput] = 0U;
  3772. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  3773. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  3774. PlsrFiniteStreamSourceDone[pulseOutput] = 0U;
  3775. PlsrFiniteStreamSourceFault[pulseOutput] = 0U;
  3776. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3777. {
  3778. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  3779. uint8_t jobOutput = PlsrFinalArmJobOutput(pulseOutput);
  3780. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  3781. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  3782. PlsrAbStructureVerified[pulseOutput] = 0U;
  3783. PlsrAbFinalArmJobOwner[jobOutput] = PLSR_COUNTER_NONE;
  3784. #if PLSR_DEBUG_TIMING
  3785. PlsrFinalArmQueueTimingPending[pulseOutput] = 0U;
  3786. #endif
  3787. NVIC_ClearPendingIRQ(PlsrTimerMap[jobOutput].irq);
  3788. if (PlsrAbFastGated[pulseOutput] != 0U)
  3789. {
  3790. PlsrAbHoldPairIdle(pulseOutput);
  3791. baseTimer->DIER = 0UL;
  3792. pairTimer->DIER = 0UL;
  3793. baseTimer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3794. pairTimer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3795. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3796. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3797. }
  3798. else
  3799. {
  3800. PlsrCounterSuspend(pulseOutput);
  3801. PlsrAbHoldPairIdle(pulseOutput);
  3802. PlsrTimerStop(baseTimer);
  3803. PlsrTimerStop(pairTimer);
  3804. }
  3805. }
  3806. else
  3807. {
  3808. PlsrPulsePinCaptureIdle(pulseOutput);
  3809. PlsrTimerStop(PlsrTimerMap[pulseOutput].timer);
  3810. }
  3811. PlsrCounterStop(pulseOutput);
  3812. PlsrTimerActiveFrequencyHz[pulseOutput] = 0UL;
  3813. PlsrTimerQueuedFrequencyHz[pulseOutput] = 0UL;
  3814. PlsrTimerQueueGeneration[pulseOutput]++;
  3815. PlsrTimerRunning[pulseOutput] = 0U;
  3816. PlsrFrequencyVerifyPending[pulseOutput] = 0U;
  3817. PlsrDeferredPulsePending[pulseOutput] = 0U;
  3818. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  3819. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  3820. {
  3821. PlsrAbVerifyOwner[pulseOutput + 1U] = PLSR_COUNTER_NONE;
  3822. }
  3823. PlsrAbFrequencyPending[pulseOutput] = 0U;
  3824. PlsrAbStopPending[pulseOutput] = 0U;
  3825. PlsrAbFastGated[pulseOutput] = 0U;
  3826. }
  3827. }
  3828. void PlsrPlatformForceSafeOutputsFromFault(void)
  3829. {
  3830. uint32_t mode;
  3831. const uint32_t outputPins = GPIO_PIN_6 | GPIO_PIN_7
  3832. | GPIO_PIN_8 | GPIO_PIN_9;
  3833. const uint32_t outputModeMask = (3UL << (6U * 2U))
  3834. | (3UL << (7U * 2U))
  3835. | (3UL << (8U * 2U))
  3836. | (3UL << (9U * 2U));
  3837. __disable_irq();
  3838. TIM10->DIER = 0UL;
  3839. TIM10->CR1 &= ~TIM_CR1_CEN;
  3840. TIM10->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3841. TIM10->SR = 0UL;
  3842. TIM11->DIER = 0UL;
  3843. TIM11->CR1 &= ~TIM_CR1_CEN;
  3844. TIM11->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3845. TIM11->SR = 0UL;
  3846. TIM13->DIER = 0UL;
  3847. TIM13->CR1 &= ~TIM_CR1_CEN;
  3848. TIM13->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3849. TIM13->SR = 0UL;
  3850. TIM14->DIER = 0UL;
  3851. TIM14->CR1 &= ~TIM_CR1_CEN;
  3852. TIM14->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  3853. TIM14->SR = 0UL;
  3854. TIM9->DIER = 0UL;
  3855. TIM9->CR1 &= ~TIM_CR1_CEN;
  3856. TIM9->SMCR = 0UL;
  3857. TIM9->SR = 0UL;
  3858. TIM12->DIER = 0UL;
  3859. TIM12->CR1 &= ~TIM_CR1_CEN;
  3860. TIM12->SMCR = 0UL;
  3861. TIM12->SR = 0UL;
  3862. RCC->AHB1ENR |= RCC_AHB1ENR_GPIOFEN | RCC_AHB1ENR_GPIOHEN;
  3863. __DSB();
  3864. GPIOF->BSRR = outputPins;
  3865. GPIOH->BSRR = outputPins;
  3866. GPIOF->OTYPER &= ~outputPins;
  3867. GPIOH->OTYPER &= ~outputPins;
  3868. GPIOF->PUPDR &= ~outputModeMask;
  3869. GPIOH->PUPDR &= ~outputModeMask;
  3870. mode = GPIOF->MODER;
  3871. mode &= ~outputModeMask;
  3872. mode |= (1UL << (6U * 2U)) | (1UL << (7U * 2U))
  3873. | (1UL << (8U * 2U)) | (1UL << (9U * 2U));
  3874. GPIOF->MODER = mode;
  3875. mode = GPIOH->MODER;
  3876. mode &= ~outputModeMask;
  3877. mode |= (1UL << (6U * 2U)) | (1UL << (7U * 2U))
  3878. | (1UL << (8U * 2U)) | (1UL << (9U * 2U));
  3879. GPIOH->MODER = mode;
  3880. __DSB();
  3881. }
  3882. uint8_t PlsrPlatformReadInput(uint8_t inputSelection)
  3883. {
  3884. if (inputSelection == 0U)
  3885. {
  3886. return (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET) ? 1U : 0U;
  3887. }
  3888. if (inputSelection == 1U)
  3889. {
  3890. return (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_12) == GPIO_PIN_SET) ? 1U : 0U;
  3891. }
  3892. return 0U;
  3893. }
  3894. uint8_t PlsrPlatformLoad(PLSR_PERSIST_PAYLOAD *payload)
  3895. {
  3896. PLSR_FLASH_SECTOR_SCAN scanA;
  3897. PLSR_FLASH_SECTOR_SCAN scanB;
  3898. const PLSR_FLASH_HEADER *selected = NULL;
  3899. const void *backupConfig = (const void *)PLSR_BACKUP_CONFIG_ADDRESS;
  3900. const PLSR_BACKUP_POSITION_RECORD *backupPosition;
  3901. uint8_t selectedVersion = 0U;
  3902. uint8_t backupVersion;
  3903. uint8_t haveConfig = 0U;
  3904. uint32_t selectedSectorAddress;
  3905. if (payload == NULL)
  3906. {
  3907. return 0U;
  3908. }
  3909. selected = PlsrFlashInitializeJournal(&scanA, &scanB, &selectedVersion,
  3910. &selectedSectorAddress);
  3911. if (selected != NULL)
  3912. {
  3913. if (selectedVersion == PLSR_FLASH_VERSION)
  3914. {
  3915. *payload = ((const PLSR_FLASH_RECORD *)selected)->payload;
  3916. }
  3917. else
  3918. {
  3919. PlsrLoadV2Payload(
  3920. payload,
  3921. &((const PLSR_FLASH_RECORD_V2 *)selected)->payload);
  3922. }
  3923. haveConfig = 1U;
  3924. }
  3925. else
  3926. {
  3927. (void)memset(payload, 0, sizeof(*payload));
  3928. }
  3929. backupVersion = PlsrBackupConfigVersion(backupConfig);
  3930. if (backupVersion == PLSR_FLASH_VERSION)
  3931. {
  3932. payload->config =
  3933. ((const PLSR_BACKUP_CONFIG_RECORD *)backupConfig)->config;
  3934. haveConfig = 1U;
  3935. }
  3936. else if (backupVersion == PLSR_FLASH_VERSION_V2)
  3937. {
  3938. const PLSR_BACKUP_CONFIG_RECORD_V2 *oldConfig =
  3939. (const PLSR_BACKUP_CONFIG_RECORD_V2 *)backupConfig;
  3940. (void)memcpy(&payload->config, oldConfig->config,
  3941. sizeof(oldConfig->config));
  3942. payload->config.outputMode = PLSR_OUTPUT_PULSE_DIR;
  3943. haveConfig = 1U;
  3944. }
  3945. backupPosition = PlsrNewestBackupPosition();
  3946. if (backupPosition != NULL)
  3947. {
  3948. payload->position = backupPosition->position;
  3949. payload->positionValid = backupPosition->positionValid;
  3950. payload->wasBusy = backupPosition->wasBusy;
  3951. }
  3952. if (selected != NULL)
  3953. {
  3954. uint8_t reserveIndex =
  3955. (selectedSectorAddress == PLSR_FLASH_SLOT_A_ADDRESS) ? 1U : 0U;
  3956. const PLSR_FLASH_SECTOR_SCAN *reserveScan =
  3957. (reserveIndex == 0U) ? &scanA : &scanB;
  3958. if (reserveScan->hasProgrammedSlot != 0U)
  3959. {
  3960. PlsrFlashReserveEraseState = (uint8_t)(reserveIndex + 1U);
  3961. }
  3962. }
  3963. else if (PlsrFlashNeedsStartupRecovery(
  3964. 0U,
  3965. scanA.hasProgrammedSlot,
  3966. scanA.firstErasedAddress,
  3967. scanB.hasProgrammedSlot,
  3968. scanB.firstErasedAddress) != 0U)
  3969. {
  3970. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_SECTOR_A;
  3971. }
  3972. return haveConfig;
  3973. }
  3974. PLSR_PLATFORM_SERVICE_RESULT PlsrPlatformServicePersistence(void)
  3975. {
  3976. uint32_t criticalState;
  3977. uint8_t eraseState;
  3978. uint8_t sectorIndex;
  3979. uint8_t index;
  3980. criticalState = PlsrPlatformEnterCritical();
  3981. eraseState = PlsrFlashReserveEraseState;
  3982. if (eraseState == PLSR_FLASH_ERASE_FAILED)
  3983. {
  3984. PlsrPlatformExitCritical(criticalState);
  3985. return PLSR_PLATFORM_SERVICE_FAILED;
  3986. }
  3987. if (eraseState == PLSR_FLASH_ERASE_NONE)
  3988. {
  3989. PlsrPlatformExitCritical(criticalState);
  3990. return PLSR_PLATFORM_SERVICE_READY;
  3991. }
  3992. if ((eraseState < PLSR_FLASH_ERASE_SECTOR_A)
  3993. || (eraseState > PLSR_FLASH_ERASE_SECTOR_B))
  3994. {
  3995. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_FAILED;
  3996. PlsrPlatformExitCritical(criticalState);
  3997. return PLSR_PLATFORM_SERVICE_FAILED;
  3998. }
  3999. for (index = 0U; index < 4U; index++)
  4000. {
  4001. if ((PlsrTimerRunning[index] != 0U)
  4002. || (PlsrTimerIrqActive[index] != 0U))
  4003. {
  4004. PlsrPlatformExitCritical(criticalState);
  4005. return PLSR_PLATFORM_SERVICE_DEFERRED;
  4006. }
  4007. }
  4008. sectorIndex = (uint8_t)(eraseState - PLSR_FLASH_ERASE_SECTOR_A);
  4009. PlsrPlatformExitCritical(criticalState);
  4010. if (PlsrFlashEraseReserve(sectorIndex) == 0U)
  4011. {
  4012. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_FAILED;
  4013. return PLSR_PLATFORM_SERVICE_FAILED;
  4014. }
  4015. PlsrFlashNextErasedAddress[sectorIndex] =
  4016. (sectorIndex == 0U) ? PLSR_FLASH_SLOT_A_ADDRESS
  4017. : PLSR_FLASH_SLOT_B_ADDRESS;
  4018. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_NONE;
  4019. return PLSR_PLATFORM_SERVICE_READY;
  4020. }
  4021. uint8_t PlsrPlatformSave(const PLSR_PERSIST_PAYLOAD *payload)
  4022. {
  4023. PLSR_FLASH_SECTOR_SCAN scanA;
  4024. PLSR_FLASH_SECTOR_SCAN scanB;
  4025. uint8_t newestVersion;
  4026. uint32_t newestSectorAddress;
  4027. uint8_t targetIndex;
  4028. uint32_t targetAddress;
  4029. uint32_t index;
  4030. uint32_t wordCount;
  4031. const uint32_t *words;
  4032. HAL_StatusTypeDef status = HAL_OK;
  4033. if (payload == NULL)
  4034. {
  4035. return 0U;
  4036. }
  4037. if (PlsrFlashJournalInitialized == 0U)
  4038. {
  4039. (void)PlsrFlashInitializeJournal(&scanA, &scanB, &newestVersion,
  4040. &newestSectorAddress);
  4041. (void)newestVersion;
  4042. (void)newestSectorAddress;
  4043. }
  4044. if (PlsrFlashNewestAddress != 0UL)
  4045. {
  4046. targetIndex = PlsrFlashSectorIndex(PlsrFlashNewestAddress);
  4047. if (PlsrFlashNextErasedAddress[targetIndex] == 0UL)
  4048. {
  4049. targetIndex ^= 1U;
  4050. }
  4051. }
  4052. else if (PlsrFlashNextErasedAddress[0] != 0UL)
  4053. {
  4054. targetIndex = 0U;
  4055. }
  4056. else
  4057. {
  4058. targetIndex = 1U;
  4059. }
  4060. targetAddress = PlsrFlashNextErasedAddress[targetIndex];
  4061. if ((targetAddress != 0UL)
  4062. && ((PlsrFlashAddressIsJournalSlot(targetIndex, targetAddress) == 0U)
  4063. || (PlsrFlashSlotIsErased(targetAddress) == 0U)))
  4064. {
  4065. targetAddress = 0UL;
  4066. PlsrFlashNextErasedAddress[targetIndex] = 0UL;
  4067. }
  4068. if (targetAddress == 0UL)
  4069. {
  4070. uint8_t newestIndex = (PlsrFlashNewestAddress != 0UL)
  4071. ? PlsrFlashSectorIndex(
  4072. PlsrFlashNewestAddress)
  4073. : 0xFFU;
  4074. uint8_t alternateIndex = targetIndex ^ 1U;
  4075. if ((targetIndex == newestIndex)
  4076. || (PlsrFlashNextErasedAddress[alternateIndex] != 0UL))
  4077. {
  4078. targetIndex = alternateIndex;
  4079. targetAddress = PlsrFlashNextErasedAddress[targetIndex];
  4080. if ((targetAddress != 0UL)
  4081. && ((PlsrFlashAddressIsJournalSlot(targetIndex,
  4082. targetAddress) == 0U)
  4083. || (PlsrFlashSlotIsErased(targetAddress) == 0U)))
  4084. {
  4085. targetAddress = 0UL;
  4086. PlsrFlashNextErasedAddress[targetIndex] = 0UL;
  4087. }
  4088. }
  4089. if (targetAddress == 0UL)
  4090. {
  4091. return 0U;
  4092. }
  4093. }
  4094. PlsrFlashNextErasedAddress[targetIndex] =
  4095. PlsrFlashFindErasedAfter(targetIndex, targetAddress);
  4096. (void)memset(&PlsrFlashRecordBuffer, 0, sizeof(PlsrFlashRecordBuffer));
  4097. PlsrFlashRecordBuffer.magic = PLSR_FLASH_MAGIC;
  4098. PlsrFlashRecordBuffer.version = PLSR_FLASH_VERSION;
  4099. PlsrFlashRecordBuffer.payloadSize = sizeof(PLSR_PERSIST_PAYLOAD);
  4100. PlsrFlashRecordBuffer.generation = PlsrFlashNewestGeneration + 1UL;
  4101. PlsrFlashRecordBuffer.payload = *payload;
  4102. PlsrFlashRecordBuffer.crc32 =
  4103. PlsrFlashRecordCrc(&PlsrFlashRecordBuffer,
  4104. sizeof(PlsrFlashRecordBuffer.payload));
  4105. if (HAL_FLASH_Unlock() != HAL_OK)
  4106. {
  4107. (void)HAL_FLASH_Lock();
  4108. return 0U;
  4109. }
  4110. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
  4111. | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  4112. | FLASH_FLAG_PGSERR);
  4113. words = (const uint32_t *)&PlsrFlashRecordBuffer;
  4114. wordCount = sizeof(PlsrFlashRecordBuffer) / sizeof(uint32_t);
  4115. if (status == HAL_OK)
  4116. {
  4117. for (index = 1UL; index < wordCount; index++)
  4118. {
  4119. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
  4120. targetAddress + index * 4UL,
  4121. words[index]) != HAL_OK)
  4122. {
  4123. status = HAL_ERROR;
  4124. break;
  4125. }
  4126. }
  4127. }
  4128. if ((status == HAL_OK)
  4129. && (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, targetAddress,
  4130. PLSR_FLASH_MAGIC) != HAL_OK))
  4131. {
  4132. status = HAL_ERROR;
  4133. }
  4134. if (HAL_FLASH_Lock() != HAL_OK)
  4135. {
  4136. (void)HAL_FLASH_Lock();
  4137. status = HAL_ERROR;
  4138. }
  4139. if (PlsrFlashRecordVersion((const void *)targetAddress)
  4140. == PLSR_FLASH_VERSION)
  4141. {
  4142. PlsrFlashNewestAddress = targetAddress;
  4143. PlsrFlashNewestGeneration = PlsrFlashRecordBuffer.generation;
  4144. return (status == HAL_OK) ? 1U : 0U;
  4145. }
  4146. return 0U;
  4147. }
  4148. void PlsrPlatformCheckpointConfig(const PLSR_CONFIG *config)
  4149. {
  4150. PLSR_BACKUP_CONFIG_RECORD *record =
  4151. (PLSR_BACKUP_CONFIG_RECORD *)PLSR_BACKUP_CONFIG_ADDRESS;
  4152. if (config == NULL)
  4153. {
  4154. return;
  4155. }
  4156. record->magic = 0UL;
  4157. record->config = *config;
  4158. record->crc32 = PlsrCrc32(&record->config, sizeof(record->config));
  4159. __DMB();
  4160. record->magic = PLSR_BACKUP_CONFIG_MAGIC;
  4161. __DMB();
  4162. }
  4163. void PlsrPlatformCheckpointPosition(int32_t position,
  4164. uint8_t positionValid,
  4165. uint8_t wasBusy)
  4166. {
  4167. PLSR_BACKUP_POSITION_RECORD *slots =
  4168. (PLSR_BACKUP_POSITION_RECORD *)PLSR_BACKUP_POSITION_ADDRESS;
  4169. PLSR_BACKUP_POSITION_RECORD *record;
  4170. PlsrBackupPositionGeneration++;
  4171. record = &slots[PlsrBackupPositionGeneration & 1UL];
  4172. record->magic = 0UL;
  4173. record->generation = PlsrBackupPositionGeneration;
  4174. record->position = position;
  4175. record->positionValid = (positionValid != 0U) ? 1U : 0U;
  4176. record->wasBusy = (wasBusy != 0U) ? 1U : 0U;
  4177. record->reserved = 0U;
  4178. record->crc32 = PlsrCrc32(&record->generation,
  4179. sizeof(record->generation)
  4180. + sizeof(record->position)
  4181. + sizeof(record->positionValid)
  4182. + sizeof(record->wasBusy)
  4183. + sizeof(record->reserved));
  4184. __DMB();
  4185. record->magic = PLSR_BACKUP_POSITION_MAGIC;
  4186. __DMB();
  4187. }
  4188. uint32_t PlsrPlatformEnterCritical(void)
  4189. {
  4190. uint32_t state = __get_PRIMASK();
  4191. __disable_irq();
  4192. __DMB();
  4193. return state;
  4194. }
  4195. void PlsrPlatformExitCritical(uint32_t state)
  4196. {
  4197. __DMB();
  4198. if (state == 0UL)
  4199. {
  4200. __enable_irq();
  4201. }
  4202. }
  4203. static void PlsrHandleTimerIrq(uint8_t pulseOutput)
  4204. {
  4205. TIM_TypeDef *timer;
  4206. #if PLSR_DEBUG_TIMING
  4207. uint32_t startedAt;
  4208. uint32_t elapsedCycles;
  4209. uint8_t timingOutput = pulseOutput;
  4210. startedAt = DWT->CYCCNT;
  4211. #endif
  4212. timer = PlsrTimerMap[pulseOutput].timer;
  4213. if (PlsrTimerIrqActive[pulseOutput] != 0U)
  4214. {
  4215. #if PLSR_DEBUG_TIMING
  4216. goto irq_record;
  4217. #else
  4218. return;
  4219. #endif
  4220. }
  4221. PlsrTimerIrqActive[pulseOutput] = 1U;
  4222. if (((timer->SR & TIM_SR_CC1IF) != 0UL)
  4223. && ((timer->DIER & TIM_DIER_CC1IE) != 0UL))
  4224. {
  4225. uint8_t owner = ((pulseOutput & 2U) == 0U) ? 0U : 2U;
  4226. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_PULSE_DIR)
  4227. && (PlsrFiniteRetargetPending[pulseOutput] != 0U))
  4228. {
  4229. timer->SR = ~TIM_SR_CC1IF;
  4230. PlsrFiniteRetargetAtFallingEdge(pulseOutput);
  4231. goto irq_done;
  4232. }
  4233. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_PULSE_DIR)
  4234. && (PlsrFiniteTailStopPending[pulseOutput] != 0U))
  4235. {
  4236. timer->SR = ~TIM_SR_CC1IF;
  4237. PlsrFiniteStopAtFallingEdge(pulseOutput);
  4238. goto irq_done;
  4239. }
  4240. #if PLSR_DEBUG_TIMING
  4241. timingOutput = owner;
  4242. #endif
  4243. timer->SR = ~TIM_SR_CC1IF;
  4244. if ((owner <= 2U)
  4245. && (PlsrTimerRunning[owner] != 0U)
  4246. && (PlsrTimerOutputMode[owner] == PLSR_OUTPUT_AB))
  4247. {
  4248. uint8_t pairOutput = (uint8_t)(owner + 1U);
  4249. TIM_TypeDef *baseTimer = PlsrTimerMap[owner].timer;
  4250. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  4251. PLSR_AB_SETTING pending;
  4252. if (PlsrAbStopPending[owner] != 0U)
  4253. {
  4254. if (PlsrAbCanFastGateAtZero(owner) == 0U)
  4255. {
  4256. goto irq_done;
  4257. }
  4258. baseTimer->DIER &= ~(TIM_DIER_CC1IE | TIM_DIER_UIE);
  4259. pairTimer->DIER &= ~(TIM_DIER_CC1IE | TIM_DIER_UIE);
  4260. baseTimer->SR = ~TIM_SR_CC1IF;
  4261. pairTimer->SR = ~TIM_SR_CC1IF;
  4262. PlsrFrequencyVerifyPending[owner] =
  4263. PLSR_FREQUENCY_VERIFY_NONE;
  4264. PlsrAbVerifyOwner[owner] = PLSR_COUNTER_NONE;
  4265. PlsrAbVerifyOwner[pairOutput] = PLSR_COUNTER_NONE;
  4266. PlsrAbFastGate(owner);
  4267. PlsrAbFastGated[owner] = 1U;
  4268. PlsrDeferredPulsePending[owner] = 0U;
  4269. PlsrPulseTimerIrq(owner);
  4270. goto irq_done;
  4271. }
  4272. if (pulseOutput != PlsrAbLagAxis[owner])
  4273. {
  4274. if (PlsrDeferredPulsePending[owner] != 0U)
  4275. {
  4276. PlsrDeferredPulsePending[owner] = 0U;
  4277. timer->DIER &= ~TIM_DIER_CC1IE;
  4278. PlsrPulseTimerIrq(owner);
  4279. }
  4280. goto irq_done;
  4281. }
  4282. if (PlsrDeferredPulsePending[owner] != 0U)
  4283. {
  4284. goto irq_done;
  4285. }
  4286. if (PlsrAbFrequencyPending[owner] != 0U)
  4287. {
  4288. pending = PlsrAbPendingSetting[owner];
  4289. PlsrAbFrequencyPending[owner] = 0U;
  4290. PlsrAbLoadAndStart(owner, &pending);
  4291. PlsrAbActiveSetting[owner] = pending;
  4292. PlsrTimerActiveSetting[owner] =
  4293. PlsrTimerQueuedSetting[owner];
  4294. PlsrTimerActiveFrequencyHz[owner] =
  4295. pending.actualFrequencyHz;
  4296. PlsrDeferredPulsePending[owner] = 1U;
  4297. PlsrAbScheduleFrequencyVerify(owner);
  4298. goto irq_done;
  4299. }
  4300. PlsrPulseTimerIrq(owner);
  4301. }
  4302. goto irq_done;
  4303. }
  4304. if (((timer->SR & TIM_SR_UIF) != 0UL)
  4305. && ((timer->DIER & TIM_DIER_UIE) != 0UL))
  4306. {
  4307. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  4308. if ((PlsrFiniteActive[pulseOutput] != 0U)
  4309. && (PlsrFiniteFrequencyPending[pulseOutput] != 0U))
  4310. {
  4311. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4312. PlsrTimerQueuedFrequencyHz[pulseOutput];
  4313. PlsrTimerActiveSetting[pulseOutput] =
  4314. PlsrTimerQueuedSetting[pulseOutput];
  4315. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  4316. timer->DIER &= ~TIM_DIER_UIE;
  4317. goto irq_done;
  4318. }
  4319. if (PlsrPlatformSettingsDiffer(
  4320. &PlsrTimerActiveSetting[pulseOutput],
  4321. &PlsrTimerQueuedSetting[pulseOutput]) != 0U)
  4322. {
  4323. PlsrFrequencyVerifyPending[pulseOutput] = 1U;
  4324. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4325. PlsrTimerQueuedFrequencyHz[pulseOutput];
  4326. PlsrTimerActiveSetting[pulseOutput] =
  4327. PlsrTimerQueuedSetting[pulseOutput];
  4328. }
  4329. PlsrPulseTimerIrq(pulseOutput);
  4330. }
  4331. irq_done:
  4332. PlsrTimerIrqActive[pulseOutput] = 0U;
  4333. #if PLSR_DEBUG_TIMING
  4334. irq_record:
  4335. elapsedCycles = DWT->CYCCNT - startedAt;
  4336. PlsrIrqCount[timingOutput]++;
  4337. PlsrIrqLastCycles[timingOutput] = elapsedCycles;
  4338. if (elapsedCycles > PlsrIrqMaxCycles[timingOutput])
  4339. {
  4340. PlsrIrqMaxCycles[timingOutput] = elapsedCycles;
  4341. }
  4342. #endif
  4343. }
  4344. static uint8_t PlsrHandleScheduledAbVerify(uint8_t pulseOutput)
  4345. {
  4346. uint8_t verifyOwner = PlsrAbVerifyOwner[pulseOutput];
  4347. TIM_TypeDef *timer;
  4348. #if PLSR_DEBUG_TIMING
  4349. uint32_t startedAt;
  4350. uint32_t elapsedCycles;
  4351. #endif
  4352. if (verifyOwner > 2U)
  4353. {
  4354. return 0U;
  4355. }
  4356. timer = PlsrTimerMap[pulseOutput].timer;
  4357. if (((timer->SR & TIM_SR_UIF) == 0UL)
  4358. || ((timer->DIER & TIM_DIER_UIE) == 0UL))
  4359. {
  4360. return 0U;
  4361. }
  4362. #if PLSR_DEBUG_TIMING
  4363. startedAt = DWT->CYCCNT;
  4364. #endif
  4365. timer->SR = ~TIM_SR_UIF;
  4366. timer->DIER &= ~TIM_DIER_UIE;
  4367. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  4368. if (PlsrFrequencyVerifyPending[verifyOwner]
  4369. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  4370. {
  4371. PlsrFrequencyVerifyPending[verifyOwner] =
  4372. PLSR_FREQUENCY_VERIFY_NONE;
  4373. PlsrFrequencyVerifyPulseCount[verifyOwner] = 0U;
  4374. (void)PlsrVerifyActiveFrequency(verifyOwner);
  4375. }
  4376. #if PLSR_DEBUG_TIMING
  4377. elapsedCycles = DWT->CYCCNT - startedAt;
  4378. PlsrIrqCount[verifyOwner]++;
  4379. PlsrIrqLastCycles[verifyOwner] = elapsedCycles;
  4380. if (elapsedCycles > PlsrIrqMaxCycles[verifyOwner])
  4381. {
  4382. PlsrIrqMaxCycles[verifyOwner] = elapsedCycles;
  4383. }
  4384. #endif
  4385. return 1U;
  4386. }
  4387. static uint8_t PlsrFinalStopIrqIsPending(uint8_t pulseOutput)
  4388. {
  4389. uint8_t owner = ((pulseOutput & 2U) == 0U) ? 0U : 2U;
  4390. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  4391. return ((PlsrAbStopPending[owner] != 0U)
  4392. && ((timer->SR & TIM_SR_CC1IF) != 0UL)
  4393. && ((timer->DIER & TIM_DIER_CC1IE) != 0UL)) ? 1U : 0U;
  4394. }
  4395. static uint8_t PlsrHandleFinalArmJob(uint8_t pulseOutput)
  4396. {
  4397. uint8_t owner = PlsrAbFinalArmJobOwner[pulseOutput];
  4398. #if PLSR_DEBUG_TIMING
  4399. uint32_t startedAt;
  4400. uint32_t elapsedCycles;
  4401. #endif
  4402. if (owner > 2U)
  4403. {
  4404. return 0U;
  4405. }
  4406. PlsrAbFinalArmJobOwner[pulseOutput] = PLSR_COUNTER_NONE;
  4407. #if PLSR_DEBUG_TIMING
  4408. startedAt = DWT->CYCCNT;
  4409. #endif
  4410. PlsrFinalArmJobIrq(owner);
  4411. #if PLSR_DEBUG_TIMING
  4412. elapsedCycles = DWT->CYCCNT - startedAt;
  4413. PlsrFinalArmJobLastCycles[owner] = elapsedCycles;
  4414. if (elapsedCycles > PlsrFinalArmJobMaxCycles[owner])
  4415. {
  4416. PlsrFinalArmJobMaxCycles[owner] = elapsedCycles;
  4417. }
  4418. #endif
  4419. return 1U;
  4420. }
  4421. static void PlsrDispatchTimerIrq(uint8_t pulseOutput)
  4422. {
  4423. if (PlsrFinalStopIrqIsPending(pulseOutput) != 0U)
  4424. {
  4425. PlsrHandleTimerIrq(pulseOutput);
  4426. return;
  4427. }
  4428. if (PlsrHandleFinalArmJob(pulseOutput) != 0U)
  4429. {
  4430. return;
  4431. }
  4432. if (PlsrHandleScheduledAbVerify(pulseOutput) == 0U)
  4433. {
  4434. PlsrHandleTimerIrq(pulseOutput);
  4435. }
  4436. }
  4437. void TIM1_UP_TIM10_IRQHandler(void)
  4438. {
  4439. PlsrDispatchTimerIrq(0U);
  4440. }
  4441. void TIM8_UP_TIM13_IRQHandler(void)
  4442. {
  4443. PlsrDispatchTimerIrq(1U);
  4444. }
  4445. void TIM1_TRG_COM_TIM11_IRQHandler(void)
  4446. {
  4447. PlsrDispatchTimerIrq(2U);
  4448. }
  4449. void TIM8_TRG_COM_TIM14_IRQHandler(void)
  4450. {
  4451. PlsrDispatchTimerIrq(3U);
  4452. }
  4453. void EXTI9_5_IRQHandler(void)
  4454. {
  4455. if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_5) != RESET)
  4456. {
  4457. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5);
  4458. PlsrWaitInputExtiIrq(0U);
  4459. }
  4460. }
  4461. void EXTI15_10_IRQHandler(void)
  4462. {
  4463. if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_12) != RESET)
  4464. {
  4465. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_12);
  4466. PlsrWaitInputExtiIrq(1U);
  4467. }
  4468. }
  4469. static void PlsrHandleCounterIrq(uint8_t counterIndex)
  4470. {
  4471. TIM_TypeDef *counter;
  4472. #if PLSR_DEBUG_TIMING
  4473. uint32_t startedAt = DWT->CYCCNT;
  4474. #endif
  4475. if (counterIndex >= PLSR_COUNTER_COUNT)
  4476. {
  4477. return;
  4478. }
  4479. counter = PlsrCounters[counterIndex];
  4480. if (((counter->SR & TIM_SR_CC1IF) != 0UL)
  4481. && ((counter->DIER & TIM_DIER_CC1IE) != 0UL))
  4482. {
  4483. uint8_t owner = PlsrCounterOwner[counterIndex];
  4484. counter->SR = ~TIM_SR_CC1IF;
  4485. if ((owner <= 3U) && (PlsrFiniteActive[owner] != 0U))
  4486. {
  4487. PlsrFinitePrepareNextStepIrq(owner, counter);
  4488. }
  4489. }
  4490. if (((counter->SR & TIM_SR_UIF) != 0UL)
  4491. && ((counter->DIER & TIM_DIER_UIE) != 0UL))
  4492. {
  4493. uint8_t owner = PlsrCounterOwner[counterIndex];
  4494. counter->SR = ~TIM_SR_UIF;
  4495. if ((owner <= 3U) && (PlsrFiniteActive[owner] != 0U))
  4496. {
  4497. PlsrFiniteCounterIrq(owner, counter);
  4498. #if PLSR_DEBUG_TIMING
  4499. {
  4500. uint32_t elapsedCycles = DWT->CYCCNT - startedAt;
  4501. if (PlsrFiniteCompletionPending[owner] != 0U)
  4502. {
  4503. PlsrFiniteFinalIrqLastCycles[owner] = elapsedCycles;
  4504. if (elapsedCycles > PlsrFiniteFinalIrqMaxCycles[owner])
  4505. {
  4506. PlsrFiniteFinalIrqMaxCycles[owner] = elapsedCycles;
  4507. }
  4508. }
  4509. else
  4510. {
  4511. PlsrFiniteBlockIrqCount[owner]++;
  4512. if (elapsedCycles > PlsrFiniteBlockIrqMaxCycles[owner])
  4513. {
  4514. PlsrFiniteBlockIrqMaxCycles[owner] = elapsedCycles;
  4515. }
  4516. }
  4517. }
  4518. #endif
  4519. }
  4520. else
  4521. {
  4522. PlsrCounterOverflowPulses[counterIndex] +=
  4523. PLSR_COUNTER_BLOCK_PULSES;
  4524. }
  4525. }
  4526. }
  4527. static void PlsrFiniteArmNextStepPrepare(uint8_t pulseOutput,
  4528. TIM_TypeDef *counter,
  4529. uint32_t blockPulses)
  4530. {
  4531. uint16_t nextIndex =
  4532. (uint16_t)(PlsrFiniteStepIndex[pulseOutput] + 1U);
  4533. counter->DIER &= ~TIM_DIER_CC1IE;
  4534. counter->SR = ~TIM_SR_CC1IF;
  4535. if (PlsrFiniteRemainingPulses[pulseOutput] > blockPulses)
  4536. {
  4537. return;
  4538. }
  4539. if ((PlsrFiniteStreamActive[pulseOutput] == 0U)
  4540. && (nextIndex >= PlsrFiniteStepCount[pulseOutput]))
  4541. {
  4542. return;
  4543. }
  4544. if (blockPulses > 1UL)
  4545. {
  4546. counter->CCR1 = blockPulses - 1UL;
  4547. counter->DIER |= TIM_DIER_CC1IE;
  4548. }
  4549. else
  4550. {
  4551. PlsrFinitePrepareNextStepIrq(pulseOutput, counter);
  4552. }
  4553. }
  4554. static void PlsrFinitePrepareNextStepIrq(uint8_t pulseOutput,
  4555. TIM_TypeDef *counter)
  4556. {
  4557. uint16_t nextIndex =
  4558. (uint16_t)(PlsrFiniteStepIndex[pulseOutput] + 1U);
  4559. TIM_TypeDef *timer;
  4560. const PLSR_PLATFORM_TIMER_SETTING *next;
  4561. counter->DIER &= ~TIM_DIER_CC1IE;
  4562. if (PlsrFiniteStreamActive[pulseOutput] != 0U)
  4563. {
  4564. PLSR_PLATFORM_TIMER_SETTING streamSetting;
  4565. uint32_t streamPulses = 0UL;
  4566. uint8_t startsNextSegment = 0U;
  4567. uint8_t takeResult;
  4568. if ((PlsrFiniteStreamNextValid[pulseOutput] != 0U)
  4569. || (PlsrFiniteStreamSourceDone[pulseOutput] != 0U)
  4570. || (PlsrFiniteStreamSourceFault[pulseOutput] != 0U))
  4571. {
  4572. return;
  4573. }
  4574. takeResult = PlsrExecTakeCountedRunIrq(
  4575. pulseOutput, &streamSetting, &streamPulses,
  4576. &startsNextSegment);
  4577. if (takeResult == PLSR_EXEC_RUN_DONE)
  4578. {
  4579. PlsrFiniteStreamSourceDone[pulseOutput] = 1U;
  4580. return;
  4581. }
  4582. if ((takeResult != PLSR_EXEC_RUN_READY)
  4583. || (streamPulses == 0UL)
  4584. || (PlsrPreparedSettingIsValid(
  4585. pulseOutput, PLSR_OUTPUT_PULSE_DIR,
  4586. &streamSetting) == 0U))
  4587. {
  4588. PlsrFiniteStreamSourceFault[pulseOutput] = 1U;
  4589. PlsrExecCountedStreamFaultIrq(pulseOutput);
  4590. return;
  4591. }
  4592. timer = PlsrTimerMap[pulseOutput].timer;
  4593. timer->PSC = streamSetting.prescaler;
  4594. timer->ARR = streamSetting.period;
  4595. timer->CCR1 = streamSetting.compare;
  4596. PlsrTimerQueuedSetting[pulseOutput] = streamSetting;
  4597. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  4598. streamSetting.actualFrequencyHz;
  4599. PlsrFiniteStreamNextSetting[pulseOutput] = streamSetting;
  4600. PlsrFiniteStreamNextPulses[pulseOutput] = streamPulses;
  4601. PlsrFiniteStreamNextStartsSegment[pulseOutput] =
  4602. startsNextSegment;
  4603. PlsrFiniteStreamNextValid[pulseOutput] = 1U;
  4604. return;
  4605. }
  4606. if (nextIndex >= PlsrFiniteStepCount[pulseOutput])
  4607. {
  4608. return;
  4609. }
  4610. timer = PlsrTimerMap[pulseOutput].timer;
  4611. next = &PlsrFiniteSteps[pulseOutput][nextIndex].setting;
  4612. timer->PSC = next->prescaler;
  4613. timer->ARR = next->period;
  4614. timer->CCR1 = next->compare;
  4615. PlsrTimerQueuedSetting[pulseOutput] = *next;
  4616. PlsrTimerQueuedFrequencyHz[pulseOutput] = next->actualFrequencyHz;
  4617. }
  4618. static void PlsrFiniteCounterIrq(uint8_t pulseOutput,
  4619. TIM_TypeDef *counter)
  4620. {
  4621. uint32_t completedBlock =
  4622. (PlsrFiniteRemainingPulses[pulseOutput]
  4623. > PLSR_COUNTER_BLOCK_PULSES)
  4624. ? PLSR_COUNTER_BLOCK_PULSES
  4625. : PlsrFiniteRemainingPulses[pulseOutput];
  4626. PlsrFiniteRemainingPulses[pulseOutput] -= completedBlock;
  4627. if (PlsrFiniteRemainingPulses[pulseOutput] != 0UL)
  4628. {
  4629. uint32_t nextBlock =
  4630. (PlsrFiniteRemainingPulses[pulseOutput]
  4631. > PLSR_COUNTER_BLOCK_PULSES)
  4632. ? PLSR_COUNTER_BLOCK_PULSES
  4633. : PlsrFiniteRemainingPulses[pulseOutput];
  4634. PlsrCounterOverflowPulses[PlsrCounterIndexByOutput[pulseOutput]] +=
  4635. completedBlock;
  4636. counter->ARR = (nextBlock == 1UL) ? 1UL : (nextBlock - 1UL);
  4637. PlsrFiniteCounterPreload[pulseOutput] =
  4638. (nextBlock == 1UL) ? 1U : 0U;
  4639. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4640. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, nextBlock);
  4641. return;
  4642. }
  4643. if (PlsrFiniteStreamActive[pulseOutput] != 0U)
  4644. {
  4645. PlsrObservedPulseBase[pulseOutput] +=
  4646. PlsrFiniteTargetPulses[pulseOutput];
  4647. PlsrObservedPulsePublished[pulseOutput] =
  4648. PlsrObservedPulseBase[pulseOutput];
  4649. if (PlsrFiniteStreamNextValid[pulseOutput] != 0U)
  4650. {
  4651. uint32_t nextPulses =
  4652. PlsrFiniteStreamNextPulses[pulseOutput];
  4653. uint32_t firstBlock =
  4654. (nextPulses > PLSR_COUNTER_BLOCK_PULSES)
  4655. ? PLSR_COUNTER_BLOCK_PULSES : nextPulses;
  4656. if (PlsrFiniteStreamNextStartsSegment[pulseOutput] != 0U)
  4657. {
  4658. /* 段边界回调只传"到当前 run 完成为止的累计计数"(IRQ 内
  4659. 已累加完成的 base),执行器只置事件,不做簿记。 */
  4660. PlsrExecCountedSegmentBoundaryIrq(
  4661. pulseOutput, PlsrObservedPulseBase[pulseOutput]);
  4662. }
  4663. PlsrFiniteTargetPulses[pulseOutput] = nextPulses;
  4664. PlsrFiniteRemainingPulses[pulseOutput] = nextPulses;
  4665. PlsrCounterOverflowPulses[
  4666. PlsrCounterIndexByOutput[pulseOutput]] = 0UL;
  4667. counter->ARR = (firstBlock == 1UL)
  4668. ? 1UL : (firstBlock - 1UL);
  4669. PlsrFiniteCounterPreload[pulseOutput] =
  4670. (firstBlock == 1UL) ? 1U : 0U;
  4671. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4672. PlsrTimerActiveSetting[pulseOutput] =
  4673. PlsrFiniteStreamNextSetting[pulseOutput];
  4674. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4675. PlsrFiniteStreamNextSetting[pulseOutput].actualFrequencyHz;
  4676. PlsrFiniteStreamNextValid[pulseOutput] = 0U;
  4677. PlsrFiniteStreamNextStartsSegment[pulseOutput] = 0U;
  4678. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, firstBlock);
  4679. return;
  4680. }
  4681. }
  4682. else if (PlsrFiniteStepCount[pulseOutput] != 0U)
  4683. {
  4684. uint16_t completedIndex = PlsrFiniteStepIndex[pulseOutput];
  4685. uint16_t nextIndex = (uint16_t)(completedIndex + 1U);
  4686. uint8_t hasNext = (nextIndex
  4687. < PlsrFiniteStepCount[pulseOutput]) ? 1U : 0U;
  4688. PlsrObservedPulseBase[pulseOutput] +=
  4689. PlsrFiniteTargetPulses[pulseOutput];
  4690. PlsrObservedPulsePublished[pulseOutput] =
  4691. PlsrObservedPulseBase[pulseOutput];
  4692. PlsrFiniteCompletedStepCount[pulseOutput] =
  4693. (uint16_t)(completedIndex + 1U);
  4694. if (hasNext != 0U)
  4695. {
  4696. const PLSR_PLATFORM_FINITE_STEP *next =
  4697. &PlsrFiniteSteps[pulseOutput][nextIndex];
  4698. uint32_t firstBlock =
  4699. (next->pulseCount > PLSR_COUNTER_BLOCK_PULSES)
  4700. ? PLSR_COUNTER_BLOCK_PULSES : next->pulseCount;
  4701. PlsrFiniteStepIndex[pulseOutput] = nextIndex;
  4702. PlsrFiniteTargetPulses[pulseOutput] = next->pulseCount;
  4703. PlsrFiniteRemainingPulses[pulseOutput] = next->pulseCount;
  4704. PlsrCounterOverflowPulses[
  4705. PlsrCounterIndexByOutput[pulseOutput]] = 0UL;
  4706. counter->ARR = (firstBlock == 1UL)
  4707. ? 1UL : (firstBlock - 1UL);
  4708. PlsrFiniteCounterPreload[pulseOutput] =
  4709. (firstBlock == 1UL) ? 1U : 0U;
  4710. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4711. PlsrTimerActiveSetting[pulseOutput] = next->setting;
  4712. PlsrTimerActiveFrequencyHz[pulseOutput] =
  4713. next->setting.actualFrequencyHz;
  4714. PlsrTimerQueuedSetting[pulseOutput] = next->setting;
  4715. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  4716. next->setting.actualFrequencyHz;
  4717. PlsrFiniteArmNextStepPrepare(pulseOutput, counter, firstBlock);
  4718. return;
  4719. }
  4720. }
  4721. counter->DIER = 0UL;
  4722. counter->CR1 &= ~TIM_CR1_CEN;
  4723. counter->SMCR &= ~(TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0);
  4724. /*
  4725. * An OC rising edge is the terminal pulse's falling edge because the
  4726. * output stage is active low. The target-count update IRQ therefore is
  4727. * already the safe idle boundary: stop here before CCR1 can start an
  4728. * extra terminal pulse.
  4729. */
  4730. PlsrFiniteStopAtFallingEdge(pulseOutput);
  4731. }
  4732. static void PlsrFiniteRetargetAtFallingEdge(uint8_t pulseOutput)
  4733. {
  4734. uint8_t counterIndex = PlsrCounterIndexByOutput[pulseOutput];
  4735. TIM_TypeDef *counter = PlsrCounters[counterIndex];
  4736. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  4737. uint32_t drainPulses = PlsrFiniteRetargetDrainPulses[pulseOutput];
  4738. uint32_t blockCount;
  4739. uint32_t completed;
  4740. PlsrFiniteRetargetPending[pulseOutput] = 0U;
  4741. PlsrFiniteRetargetDrainPulses[pulseOutput] = 0UL;
  4742. timer->DIER &= ~TIM_DIER_CC1IE;
  4743. PlsrCounterSuspend(pulseOutput);
  4744. blockCount = (uint16_t)counter->CNT;
  4745. if (blockCount >= PlsrFiniteCounterPreload[pulseOutput])
  4746. {
  4747. blockCount -= PlsrFiniteCounterPreload[pulseOutput];
  4748. }
  4749. completed = PlsrFiniteTargetPulses[pulseOutput]
  4750. - PlsrFiniteRemainingPulses[pulseOutput] + blockCount;
  4751. if (PlsrFiniteStepCount[pulseOutput] != 0U)
  4752. {
  4753. completed += PlsrFiniteSteps[pulseOutput][
  4754. PlsrFiniteStepIndex[pulseOutput]].segmentPulseOffset;
  4755. }
  4756. if (completed > PlsrFiniteTargetPulses[pulseOutput])
  4757. {
  4758. completed = PlsrFiniteTargetPulses[pulseOutput];
  4759. }
  4760. PlsrFiniteTargetPulses[pulseOutput] = completed + drainPulses;
  4761. PlsrFiniteRemainingPulses[pulseOutput] = drainPulses;
  4762. PlsrFiniteStepCount[pulseOutput] = 0U;
  4763. PlsrFiniteStepIndex[pulseOutput] = 0U;
  4764. PlsrFiniteBoundaryReadIndex[pulseOutput] = 0U;
  4765. PlsrFiniteCompletedStepCount[pulseOutput] = 0U;
  4766. counter->ARR = (drainPulses == 1UL) ? 1UL : (drainPulses - 1UL);
  4767. PlsrFiniteCounterPreload[pulseOutput] =
  4768. (drainPulses == 1UL) ? 1U : 0U;
  4769. counter->CNT = 0UL;
  4770. counter->EGR = TIM_EGR_UG;
  4771. counter->CNT = PlsrFiniteCounterPreload[pulseOutput];
  4772. counter->SR = 0UL;
  4773. counter->DIER = TIM_DIER_UIE;
  4774. PlsrCounterBegin(pulseOutput);
  4775. }
  4776. static void PlsrFiniteStopAtFallingEdge(uint8_t pulseOutput)
  4777. {
  4778. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  4779. /* OC1 and GPIO idle are both high here; hand off without a pin glitch. */
  4780. PlsrPulsePinCaptureIdle(pulseOutput);
  4781. timer->DIER = 0UL;
  4782. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  4783. timer->CR1 &= ~TIM_CR1_CEN;
  4784. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  4785. PlsrFiniteTailStopPending[pulseOutput] = 0U;
  4786. PlsrFiniteActive[pulseOutput] = 0U;
  4787. PlsrFiniteCompletionPending[pulseOutput] = 1U;
  4788. PlsrFiniteFrequencyPending[pulseOutput] = 0U;
  4789. PlsrFiniteCounterPreload[pulseOutput] = 0U;
  4790. __DSB();
  4791. }
  4792. void TIM1_BRK_TIM9_IRQHandler(void)
  4793. {
  4794. PlsrHandleCounterIrq(0U);
  4795. }
  4796. void TIM8_BRK_TIM12_IRQHandler(void)
  4797. {
  4798. PlsrHandleCounterIrq(1U);
  4799. }
  4800. #endif /* PLSR_HOST_TEST */