No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

5422 líneas
176 KiB

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