Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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