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.
 
 
 
 
 
 

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