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.
 
 
 
 
 
 

5459 linhas
177 KiB

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