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.
 
 
 
 
 
 

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