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.
 
 
 
 
 
 

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