You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3880 lines
122 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 void PlsrHostServiceFinalArmJob(void);
  62. static uint32_t PlsrHostOffsetFrequency(uint32_t frequencyHz)
  63. {
  64. int64_t adjusted = (int64_t)frequencyHz
  65. + (int64_t)PlsrHostFrequencyOffsetHz;
  66. if (adjusted < 1)
  67. {
  68. adjusted = 1;
  69. }
  70. if (adjusted > (int64_t)PLSR_FREQUENCY_MAX_HZ)
  71. {
  72. adjusted = (int64_t)PLSR_FREQUENCY_MAX_HZ;
  73. }
  74. return (uint32_t)adjusted;
  75. }
  76. static void PlsrHostLatchPulse(uint8_t pulseOutput)
  77. {
  78. if ((pulseOutput <= 3U)
  79. && (PlsrHostPulseActive[pulseOutput] != 0U))
  80. {
  81. if (PlsrHostCurveMismatchPending != 0U)
  82. {
  83. PlsrHostCurveMismatchPending = 0U;
  84. PlsrHostDiagnosticFault = 3U;
  85. }
  86. else if ((PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  87. || (PlsrHostAbStopPending[pulseOutput] == 0U))
  88. {
  89. PlsrHostFrequency[pulseOutput] =
  90. PlsrHostQueuedFrequency[pulseOutput];
  91. PlsrHostActiveSetting[pulseOutput] =
  92. PlsrHostQueuedSetting[pulseOutput];
  93. }
  94. PlsrHostObservedPulses[pulseOutput]++;
  95. PlsrHostUpdatePending[pulseOutput] = 1U;
  96. }
  97. }
  98. static void PlsrHostServicePendingPulse(uint8_t pulseOutput)
  99. {
  100. if ((pulseOutput <= 3U)
  101. && (PlsrHostUpdatePending[pulseOutput] != 0U))
  102. {
  103. PlsrHostUpdatePending[pulseOutput] = 0U;
  104. if ((PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  105. && (PlsrHostAbStopPending[pulseOutput] != 0U)
  106. && (PlsrHostAbFastGated[pulseOutput] == 0U))
  107. {
  108. PlsrHostAbFastGated[pulseOutput] = 1U;
  109. PlsrHostPulseActive[pulseOutput] = 0U;
  110. PlsrHostAbFastGateCount++;
  111. }
  112. PlsrPulseTimerIrq(pulseOutput);
  113. }
  114. if (PlsrHostDeferFinalArmJob == 0U)
  115. {
  116. PlsrHostServiceFinalArmJob();
  117. }
  118. }
  119. static void PlsrHostServiceFinalArmJob(void)
  120. {
  121. uint8_t owner;
  122. if (PlsrHostFinalArmJobPending == 0U)
  123. {
  124. return;
  125. }
  126. owner = PlsrHostFinalArmJobOwner;
  127. PlsrHostFinalArmJobPending = 0U;
  128. PlsrHostFinalArmJobOwner = 0xFFU;
  129. PlsrFinalArmJobIrq(owner);
  130. }
  131. static uint8_t PlsrHostAdvanceAbQuarter(uint8_t pulseOutput)
  132. {
  133. static const uint8_t positivePhase[4] = {0U, 2U, 3U, 1U};
  134. static const uint8_t negativePhase[4] = {0U, 1U, 3U, 2U};
  135. if ((pulseOutput > 3U)
  136. || (PlsrHostPulseActive[pulseOutput] == 0U)
  137. || (PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_AB))
  138. {
  139. return 0U;
  140. }
  141. PlsrHostAbQuarter[pulseOutput] =
  142. (uint8_t)((PlsrHostAbQuarter[pulseOutput] + 1U) & 3U);
  143. PlsrHostAbPhase[pulseOutput] =
  144. (PlsrHostDirectionPositive[pulseOutput] != 0U)
  145. ? positivePhase[PlsrHostAbQuarter[pulseOutput]]
  146. : negativePhase[PlsrHostAbQuarter[pulseOutput]];
  147. PlsrHostAbTransitions[pulseOutput]++;
  148. if (PlsrHostAbQuarter[pulseOutput] != 0U)
  149. {
  150. return 0U;
  151. }
  152. PlsrHostLatchPulse(pulseOutput);
  153. return 1U;
  154. }
  155. uint8_t PlsrPlatformInit(void)
  156. {
  157. uint8_t index;
  158. (void)memset(PlsrHostPulseActive, 0, sizeof(PlsrHostPulseActive));
  159. (void)memset(PlsrHostFrequency, 0, sizeof(PlsrHostFrequency));
  160. (void)memset(PlsrHostQueuedFrequency, 0,
  161. sizeof(PlsrHostQueuedFrequency));
  162. (void)memset(PlsrHostActiveSetting, 0,
  163. sizeof(PlsrHostActiveSetting));
  164. (void)memset(PlsrHostQueuedSetting, 0,
  165. sizeof(PlsrHostQueuedSetting));
  166. (void)memset(PlsrHostUpdatePending, 0,
  167. sizeof(PlsrHostUpdatePending));
  168. (void)memset(PlsrHostOutputMode, 0, sizeof(PlsrHostOutputMode));
  169. (void)memset(PlsrHostDirectionPositive, 0,
  170. sizeof(PlsrHostDirectionPositive));
  171. (void)memset(PlsrHostAbQuarter, 0, sizeof(PlsrHostAbQuarter));
  172. (void)memset(PlsrHostAbPhase, 0, sizeof(PlsrHostAbPhase));
  173. (void)memset(PlsrHostAbTransitions, 0,
  174. sizeof(PlsrHostAbTransitions));
  175. (void)memset(PlsrHostAbStopPending, 0,
  176. sizeof(PlsrHostAbStopPending));
  177. (void)memset(PlsrHostAbFastGated, 0,
  178. sizeof(PlsrHostAbFastGated));
  179. PlsrHostAbFastGateCount = 0UL;
  180. PlsrHostAbCleanupCount = 0UL;
  181. (void)memset(PlsrHostObservedPulses, 0,
  182. sizeof(PlsrHostObservedPulses));
  183. PlsrHostSelectedPulse = 0U;
  184. PlsrHostDirectionLevel = 0U;
  185. for (index = 0U; index < 4U; index++)
  186. {
  187. PlsrHostDirectionPinLevel[index] = 1U;
  188. PlsrHostDirectionWriteCount[index] = 0UL;
  189. PlsrHostDirectionTransitionCount[index] = 0UL;
  190. }
  191. PlsrHostEmitPulseOnCriticalEntry = 0U;
  192. PlsrHostEmitPulseOnCriticalExit = 0U;
  193. PlsrHostLatchPulseOnCriticalEntry = 0U;
  194. PlsrHostCriticalEntriesToSkip = 0U;
  195. PlsrHostLatchAbFinalQuarterOnStopArm = 0U;
  196. PlsrHostCompleteAbCycleOnQueueCommit = 0U;
  197. PlsrHostCriticalDepth = 0UL;
  198. PlsrHostFailNextStart = 0U;
  199. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  200. PlsrHostFailNextFrequencyAtUpdate = 0U;
  201. PlsrHostFailNextStopRequest = 0U;
  202. PlsrHostFinalArmJobPending = 0U;
  203. PlsrHostFinalArmJobOwner = 0xFFU;
  204. PlsrHostDeferFinalArmJob = 0U;
  205. PlsrHostCountOffset = 0L;
  206. PlsrHostFrequencyOffsetHz = 0L;
  207. PlsrHostCurveMismatchPending = 0U;
  208. PlsrHostDiagnosticFault = 0U;
  209. return 1U;
  210. }
  211. uint8_t PlsrPlatformPrepare(uint8_t pulseOutput,
  212. uint8_t directionOutput,
  213. uint8_t directionLevel,
  214. uint8_t outputMode,
  215. uint8_t directionPositive)
  216. {
  217. uint8_t index;
  218. uint8_t pinLevel;
  219. if (PlsrHostFailNextStart != 0U)
  220. {
  221. PlsrHostFailNextStart = 0U;
  222. return 0U;
  223. }
  224. if ((pulseOutput > 3U) || (directionOutput > 3U)
  225. || (outputMode > PLSR_OUTPUT_AB)
  226. || ((outputMode == PLSR_OUTPUT_AB)
  227. && ((pulseOutput & 1U) != 0U))
  228. || (PlsrHostAbStopPending[0] != 0U)
  229. || (PlsrHostAbStopPending[2] != 0U))
  230. {
  231. return 0U;
  232. }
  233. for (index = 0U; index < 4U; index++)
  234. {
  235. PlsrHostPulseActive[index] = 0U;
  236. PlsrHostFrequency[index] = 0UL;
  237. PlsrHostQueuedFrequency[index] = 0UL;
  238. PlsrHostUpdatePending[index] = 0U;
  239. pinLevel = ((outputMode == PLSR_OUTPUT_PULSE_DIR)
  240. && (index == directionOutput)
  241. && (directionLevel != 0U)) ? 0U : 1U;
  242. PlsrHostDirectionWriteCount[index]++;
  243. if (PlsrHostDirectionPinLevel[index] != pinLevel)
  244. {
  245. PlsrHostDirectionTransitionCount[index]++;
  246. }
  247. PlsrHostDirectionPinLevel[index] = pinLevel;
  248. }
  249. PlsrHostSelectedPulse = pulseOutput;
  250. PlsrHostDirectionLevel = (directionLevel != 0U) ? 1U : 0U;
  251. PlsrHostOutputMode[pulseOutput] = outputMode;
  252. PlsrHostDirectionPositive[pulseOutput] =
  253. (directionPositive != 0U) ? 1U : 0U;
  254. PlsrHostAbQuarter[pulseOutput] = 0U;
  255. PlsrHostAbPhase[pulseOutput] = 0U;
  256. PlsrHostAbFastGated[pulseOutput] = 0U;
  257. return 1U;
  258. }
  259. uint8_t PlsrPlatformStartPulse(uint8_t pulseOutput,
  260. uint32_t firstFrequencyHz,
  261. uint32_t queuedFrequencyHz,
  262. uint32_t *actualFirstFrequencyHz,
  263. uint32_t *actualQueuedFrequencyHz)
  264. {
  265. PLSR_PLATFORM_TIMER_SETTING firstSetting;
  266. PLSR_PLATFORM_TIMER_SETTING queuedSetting;
  267. if ((pulseOutput > 3U)
  268. || (PlsrPlatformBuildTimerSetting(
  269. pulseOutput, PlsrHostOutputMode[pulseOutput],
  270. firstFrequencyHz, &firstSetting) == 0U)
  271. || (PlsrPlatformBuildTimerSetting(
  272. pulseOutput, PlsrHostOutputMode[pulseOutput],
  273. queuedFrequencyHz, &queuedSetting) == 0U))
  274. {
  275. return 0U;
  276. }
  277. return PlsrPlatformStartPrepared(pulseOutput, &firstSetting,
  278. &queuedSetting,
  279. actualFirstFrequencyHz,
  280. actualQueuedFrequencyHz);
  281. }
  282. uint8_t PlsrPlatformBuildTimerSetting(
  283. uint8_t pulseOutput,
  284. uint8_t outputMode,
  285. uint32_t requestedFrequencyHz,
  286. PLSR_PLATFORM_TIMER_SETTING *setting)
  287. {
  288. if ((pulseOutput > 3U) || (outputMode > PLSR_OUTPUT_AB)
  289. || ((outputMode == PLSR_OUTPUT_AB)
  290. && ((pulseOutput & 1U) != 0U))
  291. || (requestedFrequencyHz == 0UL)
  292. || (requestedFrequencyHz > PLSR_FREQUENCY_MAX_HZ)
  293. || (setting == NULL))
  294. {
  295. return 0U;
  296. }
  297. setting->actualFrequencyHz = requestedFrequencyHz;
  298. setting->prescaler = (outputMode == PLSR_OUTPUT_AB) ? 1U : 0U;
  299. setting->pairPrescaler = 0U;
  300. setting->period = (outputMode == PLSR_OUTPUT_AB) ? 3U : 1U;
  301. setting->compare = (outputMode == PLSR_OUTPUT_AB) ? 2U : 1U;
  302. return 1U;
  303. }
  304. uint8_t PlsrPlatformStartPrepared(
  305. uint8_t pulseOutput,
  306. const PLSR_PLATFORM_TIMER_SETTING *firstSetting,
  307. const PLSR_PLATFORM_TIMER_SETTING *queuedSetting,
  308. uint32_t *actualFirstFrequencyHz,
  309. uint32_t *actualQueuedFrequencyHz)
  310. {
  311. if ((pulseOutput > 3U) || (firstSetting == NULL)
  312. || (queuedSetting == NULL) || (actualFirstFrequencyHz == NULL)
  313. || (actualQueuedFrequencyHz == NULL)
  314. || (firstSetting->actualFrequencyHz == 0UL)
  315. || (firstSetting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ)
  316. || (queuedSetting->actualFrequencyHz == 0UL)
  317. || (queuedSetting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ)
  318. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  319. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  320. {
  321. return 0U;
  322. }
  323. PlsrHostPulseActive[pulseOutput] = 1U;
  324. PlsrHostFrequency[pulseOutput] = firstSetting->actualFrequencyHz;
  325. PlsrHostQueuedFrequency[pulseOutput] = queuedSetting->actualFrequencyHz;
  326. PlsrHostActiveSetting[pulseOutput] = *firstSetting;
  327. PlsrHostQueuedSetting[pulseOutput] = *queuedSetting;
  328. PlsrHostUpdatePending[pulseOutput] = 0U;
  329. PlsrHostSelectedPulse = pulseOutput;
  330. *actualFirstFrequencyHz = firstSetting->actualFrequencyHz;
  331. *actualQueuedFrequencyHz = queuedSetting->actualFrequencyHz;
  332. return 1U;
  333. }
  334. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformLoadPreparedFromIrq(
  335. uint8_t pulseOutput,
  336. const PLSR_PLATFORM_TIMER_SETTING *setting,
  337. uint32_t *actualFrequencyHz)
  338. {
  339. if (PlsrHostFailNextFrequencyAtUpdate != 0U)
  340. {
  341. PlsrHostFailNextFrequencyAtUpdate = 0U;
  342. return PLSR_PLATFORM_QUEUE_FAILED;
  343. }
  344. if ((pulseOutput > 3U) || (setting == NULL)
  345. || (actualFrequencyHz == NULL)
  346. || (setting->actualFrequencyHz == 0UL)
  347. || (setting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ))
  348. {
  349. return PLSR_PLATFORM_QUEUE_FAILED;
  350. }
  351. if (PlsrHostStaleNextFrequencyAtUpdate != 0U)
  352. {
  353. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  354. return PLSR_PLATFORM_QUEUE_STALE;
  355. }
  356. if ((PlsrHostCompleteAbCycleOnQueueCommit != 0U)
  357. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB))
  358. {
  359. PlsrHostCompleteAbCycleOnQueueCommit = 0U;
  360. PlsrTestEmitAbQuarters(4UL);
  361. }
  362. if ((PlsrHostPulseActive[pulseOutput] == 0U)
  363. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  364. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  365. {
  366. return PLSR_PLATFORM_QUEUE_STALE;
  367. }
  368. PlsrHostQueuedSetting[pulseOutput] = *setting;
  369. PlsrHostQueuedFrequency[pulseOutput] = setting->actualFrequencyHz;
  370. *actualFrequencyHz = setting->actualFrequencyHz;
  371. return PLSR_PLATFORM_QUEUE_APPLIED;
  372. }
  373. void PlsrPlatformGateFromIrq(uint8_t pulseOutput)
  374. {
  375. if ((pulseOutput <= 3U)
  376. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  377. && (PlsrHostAbFastGated[pulseOutput] != 0U))
  378. {
  379. return;
  380. }
  381. PlsrPlatformStopPulse(pulseOutput);
  382. }
  383. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformQueueFrequency(
  384. uint8_t pulseOutput,
  385. uint32_t frequencyHz,
  386. uint32_t *actualFrequencyHz)
  387. {
  388. uint32_t criticalState;
  389. PLSR_PLATFORM_TIMER_SETTING setting;
  390. if (PlsrHostFailNextFrequencyAtUpdate != 0U)
  391. {
  392. PlsrHostFailNextFrequencyAtUpdate = 0U;
  393. return PLSR_PLATFORM_QUEUE_FAILED;
  394. }
  395. if ((pulseOutput > 3U)
  396. || (PlsrPlatformBuildTimerSetting(
  397. pulseOutput, PlsrHostOutputMode[pulseOutput], frequencyHz,
  398. &setting) == 0U)
  399. || (actualFrequencyHz == NULL)
  400. || (PlsrHostPulseActive[pulseOutput] == 0U)
  401. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  402. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  403. {
  404. return PLSR_PLATFORM_QUEUE_FAILED;
  405. }
  406. if (PlsrHostStaleNextFrequencyAtUpdate != 0U)
  407. {
  408. PlsrHostStaleNextFrequencyAtUpdate = 0U;
  409. return PLSR_PLATFORM_QUEUE_STALE;
  410. }
  411. if ((PlsrHostCompleteAbCycleOnQueueCommit != 0U)
  412. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB))
  413. {
  414. PlsrHostCompleteAbCycleOnQueueCommit = 0U;
  415. PlsrTestEmitAbQuarters(4UL);
  416. }
  417. criticalState = PlsrPlatformEnterCritical();
  418. if ((PlsrHostPulseActive[pulseOutput] == 0U)
  419. || (PlsrHostAbStopPending[pulseOutput] != 0U)
  420. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  421. {
  422. PlsrPlatformExitCritical(criticalState);
  423. return PLSR_PLATFORM_QUEUE_STALE;
  424. }
  425. PlsrHostQueuedFrequency[pulseOutput] = setting.actualFrequencyHz;
  426. PlsrHostQueuedSetting[pulseOutput] = setting;
  427. *actualFrequencyHz = setting.actualFrequencyHz;
  428. PlsrPlatformExitCritical(criticalState);
  429. return PLSR_PLATFORM_QUEUE_APPLIED;
  430. }
  431. void PlsrPlatformDrainPendingPulse(uint8_t pulseOutput)
  432. {
  433. PlsrHostServicePendingPulse(pulseOutput);
  434. }
  435. uint32_t PlsrPlatformActiveFrequency(uint8_t pulseOutput)
  436. {
  437. if (pulseOutput > 3U)
  438. {
  439. return 0UL;
  440. }
  441. return (PlsrHostFrequencyOffsetHz != 0L)
  442. ? PlsrHostOffsetFrequency(PlsrHostFrequency[pulseOutput])
  443. : PlsrHostFrequency[pulseOutput];
  444. }
  445. uint8_t PlsrPlatformExpectedFrequency(uint8_t pulseOutput,
  446. uint8_t outputMode,
  447. uint32_t requestedFrequencyHz,
  448. uint32_t *actualFrequencyHz)
  449. {
  450. PLSR_PLATFORM_TIMER_SETTING setting;
  451. if (PlsrPlatformBuildTimerSetting(pulseOutput, outputMode,
  452. requestedFrequencyHz,
  453. &setting) == 0U)
  454. {
  455. return 0U;
  456. }
  457. *actualFrequencyHz = setting.actualFrequencyHz;
  458. return 1U;
  459. }
  460. uint64_t PlsrPlatformObservedPulses(uint8_t pulseOutput)
  461. {
  462. int64_t observed;
  463. if (pulseOutput > 3U)
  464. {
  465. return 0UL;
  466. }
  467. observed = (int64_t)PlsrHostObservedPulses[pulseOutput]
  468. + (int64_t)PlsrHostCountOffset;
  469. return (observed > 0) ? (uint64_t)observed : 0UL;
  470. }
  471. uint16_t PlsrPlatformDiagnosticFault(void)
  472. {
  473. uint16_t fault = PlsrHostDiagnosticFault;
  474. PlsrHostDiagnosticFault = 0U;
  475. return fault;
  476. }
  477. PLSR_PLATFORM_STOP_RESULT PlsrPlatformRequestStopLocked(
  478. uint8_t pulseOutput,
  479. uint8_t requireZeroBoundary)
  480. {
  481. if (pulseOutput > 3U)
  482. {
  483. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  484. }
  485. if (PlsrHostFailNextStopRequest != 0U)
  486. {
  487. PlsrHostFailNextStopRequest = 0U;
  488. PlsrPlatformStopPulse(pulseOutput);
  489. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  490. }
  491. if ((requireZeroBoundary != 0U)
  492. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  493. && (PlsrHostPulseActive[pulseOutput] != 0U)
  494. && (PlsrHostAbFastGated[pulseOutput] == 0U))
  495. {
  496. if (PlsrHostLatchAbFinalQuarterOnStopArm != 0U)
  497. {
  498. PlsrHostLatchAbFinalQuarterOnStopArm = 0U;
  499. (void)PlsrHostAdvanceAbQuarter(pulseOutput);
  500. }
  501. PlsrHostQueuedFrequency[pulseOutput] =
  502. PlsrHostFrequency[pulseOutput];
  503. PlsrHostAbStopPending[pulseOutput] = 1U;
  504. return PLSR_PLATFORM_STOP_PENDING;
  505. }
  506. PlsrPlatformStopPulse(pulseOutput);
  507. return PLSR_PLATFORM_STOP_COMPLETE;
  508. }
  509. uint8_t PlsrPlatformQueueFinalArmFromIrq(uint8_t pulseOutput)
  510. {
  511. if ((pulseOutput > 2U) || ((pulseOutput & 1U) != 0U)
  512. || (PlsrHostOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  513. || (PlsrHostPulseActive[pulseOutput] == 0U)
  514. || (PlsrHostAbFastGated[pulseOutput] != 0U))
  515. {
  516. return 0U;
  517. }
  518. PlsrHostFinalArmJobOwner = pulseOutput;
  519. PlsrHostFinalArmJobPending = 1U;
  520. return 1U;
  521. }
  522. void PlsrPlatformStopPulse(uint8_t pulseOutput)
  523. {
  524. if (pulseOutput <= 3U)
  525. {
  526. if ((PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  527. && ((PlsrHostAbStopPending[pulseOutput] != 0U)
  528. || (PlsrHostAbFastGated[pulseOutput] != 0U)))
  529. {
  530. PlsrHostAbCleanupCount++;
  531. }
  532. PlsrHostPulseActive[pulseOutput] = 0U;
  533. PlsrHostFrequency[pulseOutput] = 0UL;
  534. PlsrHostQueuedFrequency[pulseOutput] = 0UL;
  535. PlsrHostUpdatePending[pulseOutput] = 0U;
  536. PlsrHostAbQuarter[pulseOutput] = 0U;
  537. PlsrHostAbPhase[pulseOutput] = 0U;
  538. PlsrHostAbStopPending[pulseOutput] = 0U;
  539. PlsrHostAbFastGated[pulseOutput] = 0U;
  540. PlsrHostFrequencyOffsetHz = 0L;
  541. if (PlsrHostFinalArmJobOwner == pulseOutput)
  542. {
  543. PlsrHostFinalArmJobPending = 0U;
  544. PlsrHostFinalArmJobOwner = 0xFFU;
  545. }
  546. }
  547. }
  548. uint8_t PlsrPlatformReadInput(uint8_t inputSelection)
  549. {
  550. return (inputSelection <= 1U) ? PlsrHostInputs[inputSelection] : 0U;
  551. }
  552. uint8_t PlsrPlatformLoad(PLSR_PERSIST_PAYLOAD *payload)
  553. {
  554. if ((payload == NULL) || (PlsrHostPersistentValid == 0U))
  555. {
  556. return 0U;
  557. }
  558. *payload = PlsrHostPersistentPayload;
  559. return 1U;
  560. }
  561. void PlsrPlatformForceSafeOutputsFromFault(void)
  562. {
  563. uint8_t index;
  564. for (index = 0U; index < 4U; index++)
  565. {
  566. PlsrPlatformStopPulse(index);
  567. }
  568. }
  569. PLSR_PLATFORM_SERVICE_RESULT PlsrPlatformServicePersistence(void)
  570. {
  571. return PLSR_PLATFORM_SERVICE_READY;
  572. }
  573. uint8_t PlsrPlatformSave(const PLSR_PERSIST_PAYLOAD *payload)
  574. {
  575. if (payload == NULL)
  576. {
  577. return 0U;
  578. }
  579. PlsrHostPersistentPayload = *payload;
  580. PlsrHostPersistentValid = 1U;
  581. PlsrHostSaveCount++;
  582. return 1U;
  583. }
  584. void PlsrPlatformCheckpointConfig(const PLSR_CONFIG *config)
  585. {
  586. if (config != NULL)
  587. {
  588. PlsrHostPersistentPayload.config = *config;
  589. PlsrHostPersistentValid = 1U;
  590. }
  591. }
  592. void PlsrPlatformCheckpointPosition(int32_t position,
  593. uint8_t positionValid,
  594. uint8_t wasBusy)
  595. {
  596. PlsrHostPersistentPayload.position = position;
  597. PlsrHostPersistentPayload.positionValid = positionValid;
  598. PlsrHostPersistentPayload.wasBusy = wasBusy;
  599. PlsrHostPersistentPayload.reserved = 0U;
  600. }
  601. uint32_t PlsrPlatformEnterCritical(void)
  602. {
  603. uint32_t previousDepth = PlsrHostCriticalDepth;
  604. if (PlsrHostEmitPulseOnCriticalEntry != 0U)
  605. {
  606. if (PlsrHostCriticalEntriesToSkip != 0U)
  607. {
  608. PlsrHostCriticalEntriesToSkip--;
  609. }
  610. else
  611. {
  612. PlsrHostEmitPulseOnCriticalEntry = 0U;
  613. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  614. if (previousDepth == 0UL)
  615. {
  616. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  617. }
  618. }
  619. }
  620. if (PlsrHostLatchPulseOnCriticalEntry != 0U)
  621. {
  622. PlsrHostLatchPulseOnCriticalEntry = 0U;
  623. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  624. }
  625. PlsrHostCriticalDepth++;
  626. return previousDepth;
  627. }
  628. void PlsrPlatformExitCritical(uint32_t state)
  629. {
  630. (void)state;
  631. if (PlsrHostCriticalDepth != 0UL)
  632. {
  633. PlsrHostCriticalDepth--;
  634. }
  635. if (PlsrHostCriticalDepth != 0UL)
  636. {
  637. return;
  638. }
  639. if (PlsrHostEmitPulseOnCriticalExit != 0U)
  640. {
  641. PlsrHostEmitPulseOnCriticalExit = 0U;
  642. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  643. }
  644. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  645. }
  646. void PlsrTestSetInput(uint8_t inputSelection, uint8_t level)
  647. {
  648. if (inputSelection <= 1U)
  649. {
  650. PlsrHostInputs[inputSelection] = (level != 0U) ? 1U : 0U;
  651. }
  652. }
  653. void PlsrTestEmitPulses(uint32_t pulseCount)
  654. {
  655. if (PlsrHostOutputMode[PlsrHostSelectedPulse] == PLSR_OUTPUT_AB)
  656. {
  657. while ((pulseCount != 0UL)
  658. && (PlsrHostPulseActive[PlsrHostSelectedPulse] != 0U))
  659. {
  660. PlsrTestEmitAbQuarters(4UL);
  661. pulseCount--;
  662. }
  663. return;
  664. }
  665. while ((pulseCount != 0UL)
  666. && (PlsrHostPulseActive[PlsrHostSelectedPulse] != 0U))
  667. {
  668. PlsrHostLatchPulse(PlsrHostSelectedPulse);
  669. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  670. pulseCount--;
  671. }
  672. }
  673. void PlsrTestEmitPulseOnCriticalEntry(void)
  674. {
  675. PlsrHostCriticalEntriesToSkip = 0U;
  676. PlsrHostEmitPulseOnCriticalEntry = 1U;
  677. }
  678. void PlsrTestEmitPulseAfterCriticalEntries(uint8_t entriesToSkip)
  679. {
  680. PlsrHostCriticalEntriesToSkip = entriesToSkip;
  681. PlsrHostEmitPulseOnCriticalEntry = 1U;
  682. }
  683. void PlsrTestEmitPulseOnCriticalExit(void)
  684. {
  685. PlsrHostEmitPulseOnCriticalExit = 1U;
  686. }
  687. void PlsrTestLatchPulseOnCriticalEntry(void)
  688. {
  689. PlsrHostLatchPulseOnCriticalEntry = 1U;
  690. }
  691. void PlsrTestLatchAbFinalQuarterOnNextStopArm(void)
  692. {
  693. PlsrHostLatchAbFinalQuarterOnStopArm = 1U;
  694. }
  695. void PlsrTestCompleteAbCycleOnNextQueueCommit(void)
  696. {
  697. PlsrHostCompleteAbCycleOnQueueCommit = 1U;
  698. }
  699. void PlsrTestServicePendingPulse(void)
  700. {
  701. PlsrHostServicePendingPulse(PlsrHostSelectedPulse);
  702. }
  703. void PlsrTestFailNextStart(void)
  704. {
  705. PlsrHostFailNextStart = 1U;
  706. }
  707. void PlsrTestServiceFinalArmJob(void)
  708. {
  709. PlsrHostServiceFinalArmJob();
  710. }
  711. void PlsrTestDeferFinalArmJob(uint8_t defer)
  712. {
  713. PlsrHostDeferFinalArmJob = (defer != 0U) ? 1U : 0U;
  714. if (PlsrHostDeferFinalArmJob == 0U)
  715. {
  716. PlsrHostServiceFinalArmJob();
  717. }
  718. }
  719. void PlsrTestStaleNextFrequencyAtUpdate(void)
  720. {
  721. PlsrHostStaleNextFrequencyAtUpdate = 1U;
  722. }
  723. void PlsrTestFailNextFrequencyAtUpdate(void)
  724. {
  725. PlsrHostFailNextFrequencyAtUpdate = 1U;
  726. }
  727. void PlsrTestFailNextStopRequest(void)
  728. {
  729. PlsrHostFailNextStopRequest = 1U;
  730. }
  731. void PlsrTestEmitAbQuarters(uint32_t quarterCount)
  732. {
  733. uint8_t pulseOutput = PlsrHostSelectedPulse;
  734. while ((quarterCount != 0UL)
  735. && (PlsrHostPulseActive[pulseOutput] != 0U)
  736. && (PlsrHostOutputMode[pulseOutput] == PLSR_OUTPUT_AB))
  737. {
  738. if (PlsrHostAdvanceAbQuarter(pulseOutput) != 0U)
  739. {
  740. PlsrHostServicePendingPulse(pulseOutput);
  741. }
  742. quarterCount--;
  743. }
  744. }
  745. uint8_t PlsrTestPulseIsActive(void)
  746. {
  747. return PlsrHostPulseActive[PlsrHostSelectedPulse];
  748. }
  749. uint32_t PlsrTestOutputFrequency(void)
  750. {
  751. return PlsrHostFrequency[PlsrHostSelectedPulse];
  752. }
  753. uint32_t PlsrTestQueuedFrequency(void)
  754. {
  755. return PlsrHostQueuedFrequency[PlsrHostSelectedPulse];
  756. }
  757. uint8_t PlsrTestDirectionLevel(void)
  758. {
  759. return PlsrHostDirectionLevel;
  760. }
  761. uint8_t PlsrTestDirectionPinLevel(uint8_t directionOutput)
  762. {
  763. return (directionOutput < 4U)
  764. ? PlsrHostDirectionPinLevel[directionOutput] : 0U;
  765. }
  766. uint32_t PlsrTestDirectionWriteCount(uint8_t directionOutput)
  767. {
  768. return (directionOutput < 4U)
  769. ? PlsrHostDirectionWriteCount[directionOutput] : 0UL;
  770. }
  771. uint32_t PlsrTestDirectionTransitionCount(uint8_t directionOutput)
  772. {
  773. return (directionOutput < 4U)
  774. ? PlsrHostDirectionTransitionCount[directionOutput] : 0UL;
  775. }
  776. uint8_t PlsrTestAbPhase(void)
  777. {
  778. return PlsrHostAbPhase[PlsrHostSelectedPulse];
  779. }
  780. uint32_t PlsrTestAbTransitionCount(void)
  781. {
  782. return PlsrHostAbTransitions[PlsrHostSelectedPulse];
  783. }
  784. uint32_t PlsrTestAbFastGateCount(void)
  785. {
  786. return PlsrHostAbFastGateCount;
  787. }
  788. uint32_t PlsrTestAbCleanupCount(void)
  789. {
  790. return PlsrHostAbCleanupCount;
  791. }
  792. void PlsrTestInjectCountOffset(int32_t offset)
  793. {
  794. PlsrHostCountOffset = offset;
  795. }
  796. void PlsrTestInjectActiveFrequencyOffset(int32_t offsetHz)
  797. {
  798. PlsrHostFrequencyOffsetHz = offsetHz;
  799. PlsrHostDiagnosticFault = 2U;
  800. }
  801. void PlsrTestInjectCurveMismatch(void)
  802. {
  803. PlsrHostCurveMismatchPending = 1U;
  804. }
  805. void PlsrTestClearPersistentStorage(void)
  806. {
  807. (void)memset(&PlsrHostPersistentPayload, 0,
  808. sizeof(PlsrHostPersistentPayload));
  809. (void)memset(PlsrHostInputs, 0, sizeof(PlsrHostInputs));
  810. PlsrHostPersistentValid = 0U;
  811. PlsrHostSaveCount = 0UL;
  812. PlsrHostCountOffset = 0L;
  813. PlsrHostFrequencyOffsetHz = 0L;
  814. PlsrHostCurveMismatchPending = 0U;
  815. PlsrHostDiagnosticFault = 0U;
  816. }
  817. void PlsrTestResetSaveCount(void)
  818. {
  819. PlsrHostSaveCount = 0UL;
  820. }
  821. uint32_t PlsrTestSaveCount(void)
  822. {
  823. return PlsrHostSaveCount;
  824. }
  825. uint8_t PlsrTestFlashNeedsStartupRecovery(
  826. uint8_t haveValidRecord,
  827. uint8_t sectorAHasProgrammedSlot,
  828. uint32_t sectorAFirstErasedAddress,
  829. uint8_t sectorBHasProgrammedSlot,
  830. uint32_t sectorBFirstErasedAddress)
  831. {
  832. return PlsrFlashNeedsStartupRecovery(
  833. haveValidRecord,
  834. sectorAHasProgrammedSlot,
  835. sectorAFirstErasedAddress,
  836. sectorBHasProgrammedSlot,
  837. sectorBFirstErasedAddress);
  838. }
  839. #else
  840. #include "stm32f4xx_hal.h"
  841. #include <stddef.h>
  842. #include <string.h>
  843. #ifndef PLSR_DEBUG_TIMING
  844. #define PLSR_DEBUG_TIMING (0U)
  845. #endif
  846. #define PLSR_FLASH_SLOT_A_ADDRESS (0x080C0000UL)
  847. #define PLSR_FLASH_SLOT_B_ADDRESS (0x080E0000UL)
  848. #define PLSR_FLASH_SECTOR_SIZE (0x00020000UL)
  849. #define PLSR_FLASH_MAGIC (0x50534C52UL)
  850. #define PLSR_FLASH_VERSION_V2 (2U)
  851. #define PLSR_FLASH_VERSION (3U)
  852. #define PLSR_BACKUP_CONFIG_ADDRESS (BKPSRAM_BASE + 0x0100UL)
  853. #define PLSR_BACKUP_POSITION_ADDRESS (BKPSRAM_BASE + 0x0200UL)
  854. #define PLSR_BACKUP_CONFIG_MAGIC (0x50434647UL)
  855. #define PLSR_BACKUP_POSITION_MAGIC (0x50504F53UL)
  856. #define PLSR_CONFIG_V2_SIZE (offsetof(PLSR_CONFIG, outputMode))
  857. #define PLSR_COUNTER_COUNT (2U)
  858. #define PLSR_COUNTER_NONE (0xFFU)
  859. #define PLSR_COUNTER_BLOCK_PULSES (65536UL)
  860. #define PLSR_PLATFORM_FAULT_FREQUENCY (2U)
  861. #define PLSR_PLATFORM_FAULT_CURVE (3U)
  862. #define PLSR_TIMER_OC1_MODE_MASK (7UL << TIM_CCMR1_OC1M_Pos)
  863. #define PLSR_TIMER_PWM1_MODE (6UL << TIM_CCMR1_OC1M_Pos)
  864. #define PLSR_STRUCTURE_VERIFY_INTERVAL (64U)
  865. #define PLSR_FREQUENCY_VERIFY_NONE (0U)
  866. #define PLSR_FREQUENCY_VERIFY_NOW (1U)
  867. #define PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ (2U)
  868. #define PLSR_QUEUE_WRITE_GUARD_COUNTS (64UL)
  869. #define PLSR_FLASH_ERASE_NONE (0U)
  870. #define PLSR_FLASH_ERASE_SECTOR_A (1U)
  871. #define PLSR_FLASH_ERASE_SECTOR_B (2U)
  872. #define PLSR_FLASH_ERASE_FAILED (3U)
  873. typedef struct
  874. {
  875. TIM_TypeDef *timer;
  876. GPIO_TypeDef *port;
  877. uint16_t pin;
  878. uint8_t pinIndex;
  879. uint8_t alternate;
  880. IRQn_Type irq;
  881. uint32_t timerClockHz;
  882. } PLSR_TIMER_MAP;
  883. typedef struct
  884. {
  885. GPIO_TypeDef *port;
  886. uint16_t pin;
  887. } PLSR_GPIO_MAP;
  888. typedef struct
  889. {
  890. uint32_t prescaler;
  891. uint32_t period;
  892. uint32_t compare;
  893. uint32_t actualFrequencyHz;
  894. } PLSR_TIMER_SETTING;
  895. typedef struct
  896. {
  897. uint32_t basePrescaler;
  898. uint32_t pairPrescaler;
  899. uint32_t period;
  900. uint32_t compare;
  901. uint32_t actualFrequencyHz;
  902. } PLSR_AB_SETTING;
  903. typedef struct
  904. {
  905. uint32_t cr1;
  906. uint32_t ccmr1;
  907. uint32_t ccer;
  908. uint32_t psc;
  909. uint32_t arr;
  910. uint32_t ccr1;
  911. } PLSR_TIMER_SNAPSHOT;
  912. typedef struct
  913. {
  914. uint32_t magic;
  915. uint16_t version;
  916. uint16_t payloadSize;
  917. uint32_t generation;
  918. } PLSR_FLASH_HEADER;
  919. typedef struct
  920. {
  921. uint8_t config[PLSR_CONFIG_V2_SIZE];
  922. int32_t position;
  923. uint8_t positionValid;
  924. uint8_t wasBusy;
  925. uint16_t reserved;
  926. } PLSR_PERSIST_PAYLOAD_V2;
  927. typedef struct
  928. {
  929. uint32_t magic;
  930. uint16_t version;
  931. uint16_t payloadSize;
  932. uint32_t generation;
  933. PLSR_PERSIST_PAYLOAD payload;
  934. uint32_t crc32;
  935. } PLSR_FLASH_RECORD;
  936. typedef struct
  937. {
  938. uint32_t magic;
  939. uint16_t version;
  940. uint16_t payloadSize;
  941. uint32_t generation;
  942. PLSR_PERSIST_PAYLOAD_V2 payload;
  943. uint32_t crc32;
  944. } PLSR_FLASH_RECORD_V2;
  945. #define PLSR_FLASH_RECORD_STRIDE \
  946. ((uint32_t)sizeof(PLSR_FLASH_RECORD))
  947. #define PLSR_FLASH_SLOT_COUNT \
  948. (PLSR_FLASH_SECTOR_SIZE / PLSR_FLASH_RECORD_STRIDE)
  949. typedef char PLSR_FLASH_RECORD_SIZE_MUST_BE_228[
  950. (sizeof(PLSR_FLASH_RECORD) == 228U) ? 1 : -1];
  951. typedef struct
  952. {
  953. const PLSR_FLASH_HEADER *newest;
  954. uint32_t firstErasedAddress;
  955. uint8_t newestVersion;
  956. uint8_t hasProgrammedSlot;
  957. } PLSR_FLASH_SECTOR_SCAN;
  958. typedef struct
  959. {
  960. uint32_t magic;
  961. PLSR_CONFIG config;
  962. uint32_t crc32;
  963. } PLSR_BACKUP_CONFIG_RECORD;
  964. typedef struct
  965. {
  966. uint32_t magic;
  967. uint8_t config[PLSR_CONFIG_V2_SIZE];
  968. uint32_t crc32;
  969. } PLSR_BACKUP_CONFIG_RECORD_V2;
  970. typedef struct
  971. {
  972. uint32_t magic;
  973. uint32_t generation;
  974. int32_t position;
  975. uint8_t positionValid;
  976. uint8_t wasBusy;
  977. uint16_t reserved;
  978. uint32_t crc32;
  979. } PLSR_BACKUP_POSITION_RECORD;
  980. static const PLSR_TIMER_MAP PlsrTimerMap[4] =
  981. {
  982. {TIM10, GPIOF, GPIO_PIN_6, 6U, GPIO_AF3_TIM10,
  983. TIM1_UP_TIM10_IRQn, 168000000UL},
  984. {TIM13, GPIOF, GPIO_PIN_8, 8U, GPIO_AF9_TIM13,
  985. TIM8_UP_TIM13_IRQn, 84000000UL},
  986. {TIM11, GPIOF, GPIO_PIN_7, 7U, GPIO_AF3_TIM11,
  987. TIM1_TRG_COM_TIM11_IRQn, 168000000UL},
  988. {TIM14, GPIOF, GPIO_PIN_9, 9U, GPIO_AF9_TIM14,
  989. TIM8_TRG_COM_TIM14_IRQn, 84000000UL}
  990. };
  991. static const PLSR_GPIO_MAP PlsrDirectionMap[4] =
  992. {
  993. {GPIOH, GPIO_PIN_9},
  994. {GPIOH, GPIO_PIN_8},
  995. {GPIOH, GPIO_PIN_7},
  996. {GPIOH, GPIO_PIN_6}
  997. };
  998. static PLSR_FLASH_RECORD PlsrFlashRecordBuffer;
  999. static uint32_t PlsrFlashNewestAddress;
  1000. static uint32_t PlsrFlashNewestGeneration;
  1001. static uint32_t PlsrFlashNextErasedAddress[2];
  1002. static uint8_t PlsrFlashJournalInitialized;
  1003. static uint8_t PlsrFlashReserveEraseState;
  1004. static uint32_t PlsrBackupPositionGeneration;
  1005. static uint32_t PlsrTimerActiveFrequencyHz[4];
  1006. static uint32_t PlsrTimerQueuedFrequencyHz[4];
  1007. static PLSR_PLATFORM_TIMER_SETTING PlsrTimerActiveSetting[4];
  1008. static PLSR_PLATFORM_TIMER_SETTING PlsrTimerQueuedSetting[4];
  1009. static uint32_t PlsrTimerQueueGeneration[4];
  1010. static uint8_t PlsrTimerOutputMode[4];
  1011. static uint8_t PlsrTimerDirectionPositive[4];
  1012. static uint8_t PlsrTimerRunning[4];
  1013. static uint8_t PlsrFrequencyVerifyPending[4];
  1014. static uint8_t PlsrFrequencyVerifyPulseCount[4];
  1015. static volatile uint8_t PlsrDeferredPulsePending[4];
  1016. static volatile uint8_t PlsrAbVerifyOwner[4];
  1017. static volatile uint8_t PlsrAbFinalArmJobOwner[4];
  1018. static uint8_t PlsrTimerIrqActive[4];
  1019. static PLSR_AB_SETTING PlsrAbActiveSetting[4];
  1020. static PLSR_AB_SETTING PlsrAbPendingSetting[4];
  1021. static uint8_t PlsrAbFrequencyPending[4];
  1022. static uint8_t PlsrAbLagAxis[4];
  1023. static uint8_t PlsrAbStructureVerified[4];
  1024. static uint8_t PlsrAbCounterSourceAxis[4];
  1025. static uint32_t PlsrAbCounterBoundary[4];
  1026. static volatile uint8_t PlsrAbStopPending[4];
  1027. static volatile uint8_t PlsrAbFastGated[4];
  1028. static TIM_TypeDef * const PlsrCounters[PLSR_COUNTER_COUNT] =
  1029. {
  1030. TIM9, TIM12
  1031. };
  1032. static uint8_t PlsrCounterOwner[PLSR_COUNTER_COUNT];
  1033. static uint8_t PlsrCounterTriggerSourceAxis[PLSR_COUNTER_COUNT];
  1034. static volatile uint64_t PlsrCounterOverflowPulses[PLSR_COUNTER_COUNT];
  1035. static uint8_t PlsrCounterIndexByOutput[4];
  1036. static uint64_t PlsrObservedPulseBase[4];
  1037. static uint64_t PlsrObservedPulsePublished[4];
  1038. static volatile uint16_t PlsrPlatformFaultPending;
  1039. static void PlsrHandleTimerIrq(uint8_t pulseOutput);
  1040. static void PlsrCounterSuspend(uint8_t pulseOutput);
  1041. static void PlsrAbFastGate(uint8_t pulseOutput);
  1042. static uint8_t PlsrFinalArmJobOutput(uint8_t pulseOutput)
  1043. {
  1044. return (pulseOutput == 0U) ? 2U : 0U;
  1045. }
  1046. #if PLSR_DEBUG_TIMING
  1047. volatile uint32_t PlsrIrqCount[4];
  1048. volatile uint32_t PlsrIrqLastCycles[4];
  1049. volatile uint32_t PlsrIrqMaxCycles[4];
  1050. volatile uint32_t PlsrFinalArmQueueCount[4];
  1051. volatile uint32_t PlsrFinalArmJobLastCycles[4];
  1052. volatile uint32_t PlsrFinalArmJobMaxCycles[4];
  1053. volatile uint32_t PlsrFinalArmQueueToStopLastCycles[4];
  1054. volatile uint32_t PlsrFinalArmQueueToStopMaxCycles[4];
  1055. static volatile uint32_t PlsrFinalArmQueuedAt[4];
  1056. static volatile uint8_t PlsrFinalArmQueueTimingPending[4];
  1057. #endif
  1058. static uint32_t PlsrCrc32(const void *data, uint32_t length)
  1059. {
  1060. const uint8_t *bytes = (const uint8_t *)data;
  1061. uint32_t crc = 0xFFFFFFFFUL;
  1062. uint32_t index;
  1063. uint8_t bit;
  1064. for (index = 0UL; index < length; index++)
  1065. {
  1066. crc ^= bytes[index];
  1067. for (bit = 0U; bit < 8U; bit++)
  1068. {
  1069. crc = ((crc & 1UL) != 0UL) ? ((crc >> 1U) ^ 0xEDB88320UL)
  1070. : (crc >> 1U);
  1071. }
  1072. }
  1073. return ~crc;
  1074. }
  1075. static uint8_t PlsrGenerationIsNewer(uint32_t first, uint32_t second)
  1076. {
  1077. return ((int32_t)(first - second) > 0) ? 1U : 0U;
  1078. }
  1079. static uint32_t PlsrFlashRecordCrc(const void *record,
  1080. uint32_t payloadSize)
  1081. {
  1082. const PLSR_FLASH_HEADER *header = (const PLSR_FLASH_HEADER *)record;
  1083. const uint8_t *start = (const uint8_t *)&header->version;
  1084. uint32_t length = (uint32_t)(sizeof(header->version)
  1085. + sizeof(header->payloadSize)
  1086. + sizeof(header->generation))
  1087. + payloadSize;
  1088. return PlsrCrc32(start, length);
  1089. }
  1090. static uint8_t PlsrFlashRecordVersion(const void *address)
  1091. {
  1092. const PLSR_FLASH_HEADER *header = (const PLSR_FLASH_HEADER *)address;
  1093. if (header->magic != PLSR_FLASH_MAGIC)
  1094. {
  1095. return 0U;
  1096. }
  1097. if ((header->version == PLSR_FLASH_VERSION)
  1098. && (header->payloadSize == sizeof(PLSR_PERSIST_PAYLOAD)))
  1099. {
  1100. const PLSR_FLASH_RECORD *record =
  1101. (const PLSR_FLASH_RECORD *)address;
  1102. return (record->crc32
  1103. == PlsrFlashRecordCrc(record, sizeof(record->payload)))
  1104. ? PLSR_FLASH_VERSION : 0U;
  1105. }
  1106. if ((header->version == PLSR_FLASH_VERSION_V2)
  1107. && (header->payloadSize == sizeof(PLSR_PERSIST_PAYLOAD_V2)))
  1108. {
  1109. const PLSR_FLASH_RECORD_V2 *record =
  1110. (const PLSR_FLASH_RECORD_V2 *)address;
  1111. return (record->crc32
  1112. == PlsrFlashRecordCrc(record, sizeof(record->payload)))
  1113. ? PLSR_FLASH_VERSION_V2 : 0U;
  1114. }
  1115. return 0U;
  1116. }
  1117. static uint8_t PlsrFlashSlotIsErased(uint32_t address)
  1118. {
  1119. const uint32_t *words = (const uint32_t *)address;
  1120. uint32_t index;
  1121. for (index = 0UL;
  1122. index < (PLSR_FLASH_RECORD_STRIDE / sizeof(uint32_t));
  1123. index++)
  1124. {
  1125. if (words[index] != 0xFFFFFFFFUL)
  1126. {
  1127. return 0U;
  1128. }
  1129. }
  1130. return 1U;
  1131. }
  1132. static uint8_t PlsrFlashSectorIsErased(uint32_t address)
  1133. {
  1134. const uint32_t *words = (const uint32_t *)address;
  1135. uint32_t index;
  1136. for (index = 0UL;
  1137. index < (PLSR_FLASH_SECTOR_SIZE / sizeof(uint32_t)); index++)
  1138. {
  1139. if (words[index] != 0xFFFFFFFFUL)
  1140. {
  1141. return 0U;
  1142. }
  1143. }
  1144. return 1U;
  1145. }
  1146. static void PlsrFlashScanSector(uint32_t sectorAddress,
  1147. PLSR_FLASH_SECTOR_SCAN *scan)
  1148. {
  1149. uint32_t index;
  1150. (void)memset(scan, 0, sizeof(*scan));
  1151. for (index = 0UL; index < PLSR_FLASH_SLOT_COUNT; index++)
  1152. {
  1153. uint32_t slotAddress = sectorAddress
  1154. + index * PLSR_FLASH_RECORD_STRIDE;
  1155. const PLSR_FLASH_HEADER *header =
  1156. (const PLSR_FLASH_HEADER *)slotAddress;
  1157. uint8_t version = PlsrFlashRecordVersion(header);
  1158. uint8_t erased = PlsrFlashSlotIsErased(slotAddress);
  1159. if ((version != 0U)
  1160. && ((scan->newest == NULL)
  1161. || (PlsrGenerationIsNewer(header->generation,
  1162. scan->newest->generation) != 0U)))
  1163. {
  1164. scan->newest = header;
  1165. scan->newestVersion = version;
  1166. }
  1167. if ((scan->firstErasedAddress == 0UL) && (erased != 0U))
  1168. {
  1169. scan->firstErasedAddress = slotAddress;
  1170. }
  1171. if (erased == 0U)
  1172. {
  1173. scan->hasProgrammedSlot = 1U;
  1174. }
  1175. }
  1176. for (index = PLSR_FLASH_SLOT_COUNT * PLSR_FLASH_RECORD_STRIDE;
  1177. index < PLSR_FLASH_SECTOR_SIZE; index += sizeof(uint32_t))
  1178. {
  1179. if (*(const uint32_t *)(sectorAddress + index) != 0xFFFFFFFFUL)
  1180. {
  1181. scan->hasProgrammedSlot = 1U;
  1182. }
  1183. }
  1184. }
  1185. static const PLSR_FLASH_HEADER *PlsrFlashSelectNewest(
  1186. const PLSR_FLASH_SECTOR_SCAN *scanA,
  1187. const PLSR_FLASH_SECTOR_SCAN *scanB,
  1188. uint8_t *version,
  1189. uint32_t *sectorAddress)
  1190. {
  1191. const PLSR_FLASH_SECTOR_SCAN *selectedScan;
  1192. if (scanA->newest == NULL)
  1193. {
  1194. selectedScan = (scanB->newest != NULL) ? scanB : NULL;
  1195. }
  1196. else if ((scanB->newest != NULL)
  1197. && (PlsrGenerationIsNewer(scanB->newest->generation,
  1198. scanA->newest->generation) != 0U))
  1199. {
  1200. selectedScan = scanB;
  1201. }
  1202. else
  1203. {
  1204. selectedScan = scanA;
  1205. }
  1206. if (selectedScan == NULL)
  1207. {
  1208. *version = 0U;
  1209. *sectorAddress = 0UL;
  1210. return NULL;
  1211. }
  1212. *version = selectedScan->newestVersion;
  1213. *sectorAddress = (selectedScan == scanA)
  1214. ? PLSR_FLASH_SLOT_A_ADDRESS
  1215. : PLSR_FLASH_SLOT_B_ADDRESS;
  1216. return selectedScan->newest;
  1217. }
  1218. static const PLSR_FLASH_HEADER *PlsrFlashInitializeJournal(
  1219. PLSR_FLASH_SECTOR_SCAN *scanA,
  1220. PLSR_FLASH_SECTOR_SCAN *scanB,
  1221. uint8_t *version,
  1222. uint32_t *sectorAddress)
  1223. {
  1224. const PLSR_FLASH_HEADER *newest;
  1225. PlsrFlashScanSector(PLSR_FLASH_SLOT_A_ADDRESS, scanA);
  1226. PlsrFlashScanSector(PLSR_FLASH_SLOT_B_ADDRESS, scanB);
  1227. newest = PlsrFlashSelectNewest(scanA, scanB, version, sectorAddress);
  1228. PlsrFlashNextErasedAddress[0] = scanA->firstErasedAddress;
  1229. PlsrFlashNextErasedAddress[1] = scanB->firstErasedAddress;
  1230. PlsrFlashNewestAddress = (uint32_t)newest;
  1231. PlsrFlashNewestGeneration = (newest != NULL)
  1232. ? newest->generation : 0UL;
  1233. PlsrFlashJournalInitialized = 1U;
  1234. return newest;
  1235. }
  1236. static uint8_t PlsrFlashSectorIndex(uint32_t address)
  1237. {
  1238. return (address >= PLSR_FLASH_SLOT_B_ADDRESS) ? 1U : 0U;
  1239. }
  1240. static uint8_t PlsrFlashAddressIsJournalSlot(uint8_t sectorIndex,
  1241. uint32_t address)
  1242. {
  1243. uint32_t sectorAddress = (sectorIndex == 0U)
  1244. ? PLSR_FLASH_SLOT_A_ADDRESS
  1245. : PLSR_FLASH_SLOT_B_ADDRESS;
  1246. uint32_t offset;
  1247. if ((address < sectorAddress)
  1248. || (address >= sectorAddress + PLSR_FLASH_SECTOR_SIZE))
  1249. {
  1250. return 0U;
  1251. }
  1252. offset = address - sectorAddress;
  1253. return ((offset % PLSR_FLASH_RECORD_STRIDE) == 0UL)
  1254. && ((offset / PLSR_FLASH_RECORD_STRIDE)
  1255. < PLSR_FLASH_SLOT_COUNT) ? 1U : 0U;
  1256. }
  1257. static uint32_t PlsrFlashFindErasedAfter(uint8_t sectorIndex,
  1258. uint32_t address)
  1259. {
  1260. uint32_t sectorAddress = (sectorIndex == 0U)
  1261. ? PLSR_FLASH_SLOT_A_ADDRESS
  1262. : PLSR_FLASH_SLOT_B_ADDRESS;
  1263. uint32_t firstIndex = ((address - sectorAddress)
  1264. / PLSR_FLASH_RECORD_STRIDE) + 1UL;
  1265. uint32_t index;
  1266. for (index = firstIndex; index < PLSR_FLASH_SLOT_COUNT; index++)
  1267. {
  1268. uint32_t slotAddress = sectorAddress
  1269. + index * PLSR_FLASH_RECORD_STRIDE;
  1270. if (PlsrFlashSlotIsErased(slotAddress) != 0U)
  1271. {
  1272. return slotAddress;
  1273. }
  1274. }
  1275. return 0UL;
  1276. }
  1277. static uint8_t PlsrFlashEraseReserve(uint8_t sectorIndex)
  1278. {
  1279. FLASH_EraseInitTypeDef erase;
  1280. uint32_t sectorError;
  1281. HAL_StatusTypeDef status;
  1282. if (HAL_FLASH_Unlock() != HAL_OK)
  1283. {
  1284. (void)HAL_FLASH_Lock();
  1285. return 0U;
  1286. }
  1287. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
  1288. | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  1289. | FLASH_FLAG_PGSERR);
  1290. erase.TypeErase = FLASH_TYPEERASE_SECTORS;
  1291. erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  1292. erase.Sector = (sectorIndex == 0U) ? FLASH_SECTOR_10 : FLASH_SECTOR_11;
  1293. erase.NbSectors = 1U;
  1294. status = HAL_FLASHEx_Erase(&erase, &sectorError);
  1295. if ((status == HAL_OK)
  1296. && (PlsrFlashSectorIsErased((sectorIndex == 0U)
  1297. ? PLSR_FLASH_SLOT_A_ADDRESS
  1298. : PLSR_FLASH_SLOT_B_ADDRESS) == 0U))
  1299. {
  1300. status = HAL_ERROR;
  1301. }
  1302. if (HAL_FLASH_Lock() != HAL_OK)
  1303. {
  1304. (void)HAL_FLASH_Lock();
  1305. status = HAL_ERROR;
  1306. }
  1307. return (status == HAL_OK) ? 1U : 0U;
  1308. }
  1309. static uint8_t PlsrBackupConfigVersion(const void *address)
  1310. {
  1311. const PLSR_BACKUP_CONFIG_RECORD *record =
  1312. (const PLSR_BACKUP_CONFIG_RECORD *)address;
  1313. if (record->magic != PLSR_BACKUP_CONFIG_MAGIC)
  1314. {
  1315. return 0U;
  1316. }
  1317. if (record->crc32 == PlsrCrc32(&record->config, sizeof(record->config)))
  1318. {
  1319. return PLSR_FLASH_VERSION;
  1320. }
  1321. {
  1322. const PLSR_BACKUP_CONFIG_RECORD_V2 *oldRecord =
  1323. (const PLSR_BACKUP_CONFIG_RECORD_V2 *)address;
  1324. return (oldRecord->crc32
  1325. == PlsrCrc32(oldRecord->config, sizeof(oldRecord->config)))
  1326. ? PLSR_FLASH_VERSION_V2 : 0U;
  1327. }
  1328. }
  1329. static void PlsrLoadV2Payload(PLSR_PERSIST_PAYLOAD *destination,
  1330. const PLSR_PERSIST_PAYLOAD_V2 *source)
  1331. {
  1332. (void)memset(destination, 0, sizeof(*destination));
  1333. (void)memcpy(&destination->config, source->config,
  1334. sizeof(source->config));
  1335. destination->config.outputMode = PLSR_OUTPUT_PULSE_DIR;
  1336. destination->position = source->position;
  1337. destination->positionValid = source->positionValid;
  1338. destination->wasBusy = source->wasBusy;
  1339. }
  1340. static uint8_t PlsrBackupPositionIsValid(
  1341. const PLSR_BACKUP_POSITION_RECORD *record)
  1342. {
  1343. uint32_t crc = PlsrCrc32(&record->generation,
  1344. sizeof(record->generation)
  1345. + sizeof(record->position)
  1346. + sizeof(record->positionValid)
  1347. + sizeof(record->wasBusy)
  1348. + sizeof(record->reserved));
  1349. return ((record->magic == PLSR_BACKUP_POSITION_MAGIC)
  1350. && (record->crc32 == crc)) ? 1U : 0U;
  1351. }
  1352. static const PLSR_BACKUP_POSITION_RECORD *PlsrNewestBackupPosition(void)
  1353. {
  1354. const PLSR_BACKUP_POSITION_RECORD *slots =
  1355. (const PLSR_BACKUP_POSITION_RECORD *)PLSR_BACKUP_POSITION_ADDRESS;
  1356. uint8_t validA = PlsrBackupPositionIsValid(&slots[0]);
  1357. uint8_t validB = PlsrBackupPositionIsValid(&slots[1]);
  1358. if ((validA == 0U) && (validB == 0U))
  1359. {
  1360. return NULL;
  1361. }
  1362. if (validA == 0U)
  1363. {
  1364. return &slots[1];
  1365. }
  1366. if (validB == 0U)
  1367. {
  1368. return &slots[0];
  1369. }
  1370. return (PlsrGenerationIsNewer(slots[1].generation,
  1371. slots[0].generation) != 0U)
  1372. ? &slots[1] : &slots[0];
  1373. }
  1374. static void PlsrTimerStop(TIM_TypeDef *timer)
  1375. {
  1376. timer->DIER &= ~(TIM_DIER_UIE | TIM_DIER_CC1IE);
  1377. timer->CR1 &= ~TIM_CR1_CEN;
  1378. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  1379. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  1380. }
  1381. static void PlsrTimerInitialize(TIM_TypeDef *timer)
  1382. {
  1383. timer->CR1 = TIM_CR1_ARPE | TIM_CR1_URS;
  1384. timer->CR2 = 0UL;
  1385. timer->SMCR = 0UL;
  1386. timer->DIER = 0UL;
  1387. timer->CCMR1 = TIM_CCMR1_OC1PE | (6UL << TIM_CCMR1_OC1M_Pos);
  1388. timer->CCER = 0UL;
  1389. timer->PSC = 0UL;
  1390. timer->ARR = 999UL;
  1391. timer->CCR1 = 500UL;
  1392. timer->CNT = 0UL;
  1393. timer->EGR = TIM_EGR_UG;
  1394. timer->SR = 0UL;
  1395. }
  1396. static void PlsrPulsePinHoldIdle(uint8_t pulseOutput)
  1397. {
  1398. const PLSR_TIMER_MAP *map = &PlsrTimerMap[pulseOutput];
  1399. GPIO_InitTypeDef gpio;
  1400. HAL_GPIO_WritePin(map->port, map->pin, GPIO_PIN_SET);
  1401. gpio.Pin = map->pin;
  1402. gpio.Mode = GPIO_MODE_OUTPUT_PP;
  1403. gpio.Pull = GPIO_NOPULL;
  1404. gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1405. gpio.Alternate = 0U;
  1406. HAL_GPIO_Init(map->port, &gpio);
  1407. }
  1408. static void PlsrPulsePinCaptureIdle(uint8_t pulseOutput)
  1409. {
  1410. const PLSR_TIMER_MAP *map = &PlsrTimerMap[pulseOutput];
  1411. uint32_t shift = (uint32_t)map->pinIndex * 2UL;
  1412. uint32_t mode = map->port->MODER;
  1413. /* The update IRQ occurs while PWM is high; switch to GPIO high first. */
  1414. map->port->BSRR = map->pin;
  1415. mode &= ~(3UL << shift);
  1416. mode |= 1UL << shift;
  1417. map->port->MODER = mode;
  1418. __DSB();
  1419. }
  1420. static void PlsrPulsePinRelease(uint8_t pulseOutput)
  1421. {
  1422. const PLSR_TIMER_MAP *map = &PlsrTimerMap[pulseOutput];
  1423. GPIO_InitTypeDef gpio;
  1424. gpio.Pin = map->pin;
  1425. gpio.Mode = GPIO_MODE_AF_PP;
  1426. gpio.Pull = GPIO_NOPULL;
  1427. gpio.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1428. gpio.Alternate = map->alternate;
  1429. HAL_GPIO_Init(map->port, &gpio);
  1430. __DSB();
  1431. }
  1432. static uint8_t PlsrTimerCalculate(uint8_t pulseOutput,
  1433. uint32_t frequencyHz,
  1434. PLSR_TIMER_SETTING *setting)
  1435. {
  1436. const PLSR_TIMER_MAP *map;
  1437. uint32_t prescalerDivider;
  1438. uint32_t denominator;
  1439. uint32_t periodCounts;
  1440. if ((pulseOutput > 3U) || (frequencyHz == 0UL)
  1441. || (frequencyHz > PLSR_FREQUENCY_MAX_HZ)
  1442. || (setting == NULL))
  1443. {
  1444. return 0U;
  1445. }
  1446. map = &PlsrTimerMap[pulseOutput];
  1447. prescalerDivider = (((map->timerClockHz - 1UL) / frequencyHz) >> 16U)
  1448. + 1UL;
  1449. if (prescalerDivider > 65536UL)
  1450. {
  1451. return 0U;
  1452. }
  1453. denominator = prescalerDivider * frequencyHz;
  1454. periodCounts = (map->timerClockHz + denominator / 2UL) / denominator;
  1455. if (periodCounts < 2UL)
  1456. {
  1457. periodCounts = 2UL;
  1458. }
  1459. if (periodCounts > 65536UL)
  1460. {
  1461. periodCounts = 65536UL;
  1462. }
  1463. setting->prescaler = prescalerDivider - 1UL;
  1464. setting->period = periodCounts - 1UL;
  1465. setting->compare = periodCounts / 2UL;
  1466. denominator = prescalerDivider * periodCounts;
  1467. setting->actualFrequencyHz =
  1468. (map->timerClockHz + denominator / 2UL) / denominator;
  1469. return 1U;
  1470. }
  1471. static void PlsrTimerWriteSetting(TIM_TypeDef *timer,
  1472. const PLSR_TIMER_SETTING *setting)
  1473. {
  1474. timer->PSC = setting->prescaler;
  1475. timer->ARR = setting->period;
  1476. timer->CCR1 = setting->compare;
  1477. }
  1478. static void PlsrTimerSnapshot(TIM_TypeDef *timer,
  1479. PLSR_TIMER_SNAPSHOT *snapshot)
  1480. {
  1481. snapshot->cr1 = timer->CR1;
  1482. snapshot->ccmr1 = timer->CCMR1;
  1483. snapshot->ccer = timer->CCER;
  1484. snapshot->psc = timer->PSC;
  1485. snapshot->arr = timer->ARR;
  1486. snapshot->ccr1 = timer->CCR1;
  1487. }
  1488. static uint8_t PlsrAbCalculate(uint8_t pulseOutput,
  1489. uint32_t frequencyHz,
  1490. PLSR_AB_SETTING *setting)
  1491. {
  1492. const PLSR_TIMER_MAP *baseMap;
  1493. const PLSR_TIMER_MAP *pairMap;
  1494. uint64_t ratio;
  1495. uint64_t pairDivider;
  1496. uint64_t baseDivider;
  1497. uint64_t periodCounts;
  1498. if (((pulseOutput != 0U) && (pulseOutput != 2U))
  1499. || (frequencyHz == 0UL)
  1500. || (frequencyHz > PLSR_FREQUENCY_MAX_HZ)
  1501. || (setting == NULL))
  1502. {
  1503. return 0U;
  1504. }
  1505. baseMap = &PlsrTimerMap[pulseOutput];
  1506. pairMap = &PlsrTimerMap[pulseOutput + 1U];
  1507. if ((pairMap->timerClockHz == 0UL)
  1508. || ((baseMap->timerClockHz % pairMap->timerClockHz) != 0UL))
  1509. {
  1510. return 0U;
  1511. }
  1512. ratio = baseMap->timerClockHz / pairMap->timerClockHz;
  1513. pairDivider = ((uint64_t)pairMap->timerClockHz
  1514. + (uint64_t)frequencyHz * 65536UL - 1UL)
  1515. / ((uint64_t)frequencyHz * 65536UL);
  1516. if (pairDivider == 0UL)
  1517. {
  1518. pairDivider = 1UL;
  1519. }
  1520. baseDivider = pairDivider * ratio;
  1521. if ((pairDivider > 65536UL) || (baseDivider > 65536UL))
  1522. {
  1523. return 0U;
  1524. }
  1525. periodCounts = ((uint64_t)pairMap->timerClockHz
  1526. + ((uint64_t)frequencyHz * pairDivider) / 2UL)
  1527. / ((uint64_t)frequencyHz * pairDivider);
  1528. if ((periodCounts < 4UL) || (periodCounts > 65536UL))
  1529. {
  1530. return 0U;
  1531. }
  1532. setting->basePrescaler = (uint32_t)(baseDivider - 1UL);
  1533. setting->pairPrescaler = (uint32_t)(pairDivider - 1UL);
  1534. setting->period = (uint32_t)(periodCounts - 1UL);
  1535. setting->compare = (uint32_t)(periodCounts / 2UL);
  1536. setting->actualFrequencyHz =
  1537. (uint32_t)(((uint64_t)pairMap->timerClockHz
  1538. + (pairDivider * periodCounts) / 2UL)
  1539. / (pairDivider * periodCounts));
  1540. return 1U;
  1541. }
  1542. static void PlsrAbHoldPairIdle(uint8_t pulseOutput)
  1543. {
  1544. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1545. GPIO_TypeDef *port = PlsrTimerMap[pulseOutput].port;
  1546. uint32_t firstShift = (uint32_t)PlsrTimerMap[pulseOutput].pinIndex * 2UL;
  1547. uint32_t secondShift = (uint32_t)PlsrTimerMap[pairOutput].pinIndex * 2UL;
  1548. uint32_t mode = port->MODER;
  1549. port->BSRR = (uint32_t)PlsrTimerMap[pulseOutput].pin
  1550. | (uint32_t)PlsrTimerMap[pairOutput].pin;
  1551. mode &= ~((3UL << firstShift) | (3UL << secondShift));
  1552. mode |= (1UL << firstShift) | (1UL << secondShift);
  1553. port->MODER = mode;
  1554. __DSB();
  1555. }
  1556. static void PlsrAbReleasePair(uint8_t pulseOutput)
  1557. {
  1558. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1559. GPIO_TypeDef *port = PlsrTimerMap[pulseOutput].port;
  1560. uint32_t firstAfrIndex =
  1561. (uint32_t)PlsrTimerMap[pulseOutput].pinIndex >> 3U;
  1562. uint32_t secondAfrIndex =
  1563. (uint32_t)PlsrTimerMap[pairOutput].pinIndex >> 3U;
  1564. uint32_t firstAfrShift =
  1565. ((uint32_t)PlsrTimerMap[pulseOutput].pinIndex & 7UL) * 4UL;
  1566. uint32_t secondAfrShift =
  1567. ((uint32_t)PlsrTimerMap[pairOutput].pinIndex & 7UL) * 4UL;
  1568. uint32_t firstShift = (uint32_t)PlsrTimerMap[pulseOutput].pinIndex * 2UL;
  1569. uint32_t secondShift = (uint32_t)PlsrTimerMap[pairOutput].pinIndex * 2UL;
  1570. uint32_t alternate;
  1571. uint32_t mode = port->MODER;
  1572. alternate = port->AFR[firstAfrIndex];
  1573. alternate &= ~(0xFUL << firstAfrShift);
  1574. alternate |= (uint32_t)PlsrTimerMap[pulseOutput].alternate
  1575. << firstAfrShift;
  1576. port->AFR[firstAfrIndex] = alternate;
  1577. alternate = port->AFR[secondAfrIndex];
  1578. alternate &= ~(0xFUL << secondAfrShift);
  1579. alternate |= (uint32_t)PlsrTimerMap[pairOutput].alternate
  1580. << secondAfrShift;
  1581. port->AFR[secondAfrIndex] = alternate;
  1582. mode &= ~((3UL << firstShift) | (3UL << secondShift));
  1583. mode |= (2UL << firstShift) | (2UL << secondShift);
  1584. port->MODER = mode;
  1585. __DSB();
  1586. }
  1587. static uint8_t PlsrAbStructureIsRunnable(
  1588. uint8_t pulseOutput,
  1589. const PLSR_TIMER_SNAPSHOT *base,
  1590. const PLSR_TIMER_SNAPSHOT *pair)
  1591. {
  1592. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1593. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  1594. return ((((base->cr1 & TIM_CR1_CEN) != 0UL)
  1595. && ((pair->cr1 & TIM_CR1_CEN) != 0UL)
  1596. && ((base->ccer & TIM_CCER_CC1E) != 0UL)
  1597. && ((pair->ccer & TIM_CCER_CC1E) != 0UL)
  1598. && ((base->ccer & TIM_CCER_CC1P) != 0UL)
  1599. && ((pair->ccer & TIM_CCER_CC1P) != 0UL)
  1600. && ((base->ccmr1 & PLSR_TIMER_OC1_MODE_MASK)
  1601. == PLSR_TIMER_PWM1_MODE)
  1602. && ((pair->ccmr1 & PLSR_TIMER_OC1_MODE_MASK)
  1603. == PLSR_TIMER_PWM1_MODE)
  1604. && (base->arr == pair->arr)
  1605. && (base->ccr1 == pair->ccr1)
  1606. && (base->ccr1 == ((base->arr + 1UL) / 2UL))
  1607. && ((base->psc + 1UL) == 2UL * (pair->psc + 1UL))
  1608. && ((lagOutput == pulseOutput) || (lagOutput == pairOutput)))
  1609. ? 1U : 0U);
  1610. }
  1611. static uint8_t PlsrAbTimersAreRunnable(uint8_t pulseOutput,
  1612. TIM_TypeDef *baseTimer,
  1613. TIM_TypeDef *pairTimer)
  1614. {
  1615. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1616. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  1617. uint32_t baseCr1 = baseTimer->CR1;
  1618. uint32_t pairCr1 = pairTimer->CR1;
  1619. uint32_t baseCcer = baseTimer->CCER;
  1620. uint32_t pairCcer = pairTimer->CCER;
  1621. uint32_t baseCcmr1 = baseTimer->CCMR1;
  1622. uint32_t pairCcmr1 = pairTimer->CCMR1;
  1623. uint32_t basePsc = baseTimer->PSC;
  1624. uint32_t pairPsc = pairTimer->PSC;
  1625. uint32_t baseCcr1 = baseTimer->CCR1;
  1626. uint32_t pairCcr1 = pairTimer->CCR1;
  1627. uint32_t baseArr = baseTimer->ARR;
  1628. uint32_t pairArr = pairTimer->ARR;
  1629. return (((((baseCr1 & TIM_CR1_CEN) != 0UL)
  1630. && ((pairCr1 & TIM_CR1_CEN) != 0UL)
  1631. && ((baseCcer & (TIM_CCER_CC1E | TIM_CCER_CC1P))
  1632. == (TIM_CCER_CC1E | TIM_CCER_CC1P))
  1633. && ((pairCcer & (TIM_CCER_CC1E | TIM_CCER_CC1P))
  1634. == (TIM_CCER_CC1E | TIM_CCER_CC1P))
  1635. && ((baseCcmr1 & PLSR_TIMER_OC1_MODE_MASK)
  1636. == PLSR_TIMER_PWM1_MODE)
  1637. && ((pairCcmr1 & PLSR_TIMER_OC1_MODE_MASK)
  1638. == PLSR_TIMER_PWM1_MODE)
  1639. && (baseArr == pairArr)
  1640. && (baseCcr1 == pairCcr1)
  1641. && (baseCcr1 == ((baseArr + 1UL) / 2UL))
  1642. && ((basePsc + 1UL) == 2UL * (pairPsc + 1UL))
  1643. && ((lagOutput == pulseOutput)
  1644. || (lagOutput == pairOutput))) ? 1U : 0U));
  1645. }
  1646. static uint8_t PlsrAbStopBoundaryIsReachable(
  1647. uint8_t pulseOutput,
  1648. TIM_TypeDef *baseTimer,
  1649. TIM_TypeDef *pairTimer)
  1650. {
  1651. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1652. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  1653. uint32_t baseCr1 = baseTimer->CR1;
  1654. uint32_t pairCr1 = pairTimer->CR1;
  1655. uint32_t baseCcer = baseTimer->CCER;
  1656. uint32_t pairCcer = pairTimer->CCER;
  1657. uint32_t baseCcr1 = baseTimer->CCR1;
  1658. uint32_t pairCcr1 = pairTimer->CCR1;
  1659. uint32_t baseArr = baseTimer->ARR;
  1660. uint32_t pairArr = pairTimer->ARR;
  1661. return (((((baseCr1 & TIM_CR1_CEN) != 0UL)
  1662. && ((pairCr1 & TIM_CR1_CEN) != 0UL)
  1663. && ((baseCcer & TIM_CCER_CC1E) != 0UL)
  1664. && ((pairCcer & TIM_CCER_CC1E) != 0UL)
  1665. && (baseCcr1 <= baseArr)
  1666. && (pairCcr1 <= pairArr)
  1667. && ((lagOutput == pulseOutput)
  1668. || (lagOutput == pairOutput))) ? 1U : 0U));
  1669. }
  1670. static void PlsrAbFastGate(uint8_t pulseOutput)
  1671. {
  1672. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1673. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  1674. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  1675. PlsrAbStructureVerified[pulseOutput] = 0U;
  1676. baseTimer->CR1 &= ~TIM_CR1_CEN;
  1677. pairTimer->CR1 &= ~TIM_CR1_CEN;
  1678. PlsrCounterSuspend(pulseOutput);
  1679. __DMB();
  1680. }
  1681. #if defined(__ICCARM__)
  1682. #pragma inline=never
  1683. #endif
  1684. static void PlsrAbEnableTimerPair(TIM_TypeDef *firstTimer,
  1685. uint32_t firstCr1,
  1686. TIM_TypeDef *secondTimer,
  1687. uint32_t secondCr1)
  1688. {
  1689. firstTimer->CR1 = firstCr1;
  1690. secondTimer->CR1 = secondCr1;
  1691. }
  1692. static uint8_t PlsrAbCanFastGateAtZero(uint8_t pulseOutput)
  1693. {
  1694. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1695. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  1696. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  1697. uint32_t baseCr1 = baseTimer->CR1;
  1698. uint32_t pairCr1 = pairTimer->CR1;
  1699. uint32_t baseCcer = baseTimer->CCER;
  1700. uint32_t pairCcer = pairTimer->CCER;
  1701. uint32_t baseCcr = baseTimer->CCR1;
  1702. uint32_t pairCcr = pairTimer->CCR1;
  1703. uint32_t baseCnt = baseTimer->CNT;
  1704. uint32_t pairCnt = pairTimer->CNT;
  1705. return ((((baseCr1 & TIM_CR1_CEN) != 0UL)
  1706. && ((pairCr1 & TIM_CR1_CEN) != 0UL)
  1707. && ((baseCcer & TIM_CCER_CC1E) != 0UL)
  1708. && ((pairCcer & TIM_CCER_CC1E) != 0UL)
  1709. && (baseCnt >= baseCcr)
  1710. && (pairCnt >= pairCcr)) ? 1U : 0U);
  1711. }
  1712. static uint8_t PlsrCounterIndex(uint8_t pulseOutput, uint8_t outputMode)
  1713. {
  1714. return (outputMode == PLSR_OUTPUT_AB)
  1715. ? (uint8_t)(pulseOutput >> 1U)
  1716. : (uint8_t)(pulseOutput & 1U);
  1717. }
  1718. static uint64_t PlsrCounterCurrentRaw(uint8_t pulseOutput)
  1719. {
  1720. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  1721. TIM_TypeDef *counter;
  1722. uint64_t overflowBefore;
  1723. uint64_t overflowAfter;
  1724. uint32_t statusBefore;
  1725. uint32_t statusAfter;
  1726. uint32_t count;
  1727. if (index >= PLSR_COUNTER_COUNT)
  1728. {
  1729. return 0UL;
  1730. }
  1731. counter = PlsrCounters[index];
  1732. for (;;)
  1733. {
  1734. overflowBefore = PlsrCounterOverflowPulses[index];
  1735. statusBefore = counter->SR & TIM_SR_UIF;
  1736. count = (uint16_t)counter->CNT;
  1737. statusAfter = counter->SR & TIM_SR_UIF;
  1738. overflowAfter = PlsrCounterOverflowPulses[index];
  1739. if ((overflowBefore == overflowAfter)
  1740. && (statusBefore == statusAfter))
  1741. {
  1742. if (statusAfter != 0UL)
  1743. {
  1744. overflowAfter += PLSR_COUNTER_BLOCK_PULSES;
  1745. }
  1746. return overflowAfter + count;
  1747. }
  1748. }
  1749. }
  1750. static uint64_t PlsrCounterSnapshot(uint8_t pulseOutput)
  1751. {
  1752. uint64_t current = PlsrCounterCurrentRaw(pulseOutput);
  1753. uint64_t observed;
  1754. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  1755. && (current > 0UL))
  1756. {
  1757. uint8_t sourceAxis = PlsrAbCounterSourceAxis[pulseOutput];
  1758. uint8_t attempt;
  1759. for (attempt = 0U; attempt < 2U; attempt++)
  1760. {
  1761. uint64_t verified;
  1762. current = PlsrCounterCurrentRaw(pulseOutput);
  1763. if (sourceAxis <= 3U)
  1764. {
  1765. uint32_t sourceCount = PlsrTimerMap[sourceAxis].timer->CNT;
  1766. verified = PlsrCounterCurrentRaw(pulseOutput);
  1767. if (current == verified)
  1768. {
  1769. if ((sourceCount
  1770. < PlsrAbCounterBoundary[pulseOutput])
  1771. && (current > 0UL))
  1772. {
  1773. current--;
  1774. }
  1775. break;
  1776. }
  1777. current = verified;
  1778. }
  1779. }
  1780. }
  1781. observed = PlsrObservedPulseBase[pulseOutput] + current;
  1782. if (observed < PlsrObservedPulsePublished[pulseOutput])
  1783. {
  1784. observed = PlsrObservedPulsePublished[pulseOutput];
  1785. }
  1786. else
  1787. {
  1788. PlsrObservedPulsePublished[pulseOutput] = observed;
  1789. }
  1790. return observed;
  1791. }
  1792. static uint64_t PlsrCounterSnapshotStopped(uint8_t pulseOutput)
  1793. {
  1794. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  1795. TIM_TypeDef *counter = PlsrCounters[index];
  1796. uint64_t current = PlsrCounterOverflowPulses[index];
  1797. uint64_t observed;
  1798. current += (uint16_t)counter->CNT;
  1799. if ((counter->SR & TIM_SR_UIF) != 0UL)
  1800. {
  1801. current += PLSR_COUNTER_BLOCK_PULSES;
  1802. }
  1803. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  1804. && (current > 0UL))
  1805. {
  1806. uint8_t sourceAxis = PlsrAbCounterSourceAxis[pulseOutput];
  1807. if ((sourceAxis <= 3U)
  1808. && (PlsrTimerMap[sourceAxis].timer->CNT
  1809. < PlsrAbCounterBoundary[pulseOutput]))
  1810. {
  1811. current--;
  1812. }
  1813. }
  1814. observed = PlsrObservedPulseBase[pulseOutput] + current;
  1815. if (observed < PlsrObservedPulsePublished[pulseOutput])
  1816. {
  1817. observed = PlsrObservedPulsePublished[pulseOutput];
  1818. }
  1819. else
  1820. {
  1821. PlsrObservedPulsePublished[pulseOutput] = observed;
  1822. }
  1823. return observed;
  1824. }
  1825. static void PlsrCounterStop(uint8_t pulseOutput)
  1826. {
  1827. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  1828. if (index < PLSR_COUNTER_COUNT)
  1829. {
  1830. TIM_TypeDef *counter = PlsrCounters[index];
  1831. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  1832. && (PlsrAbFastGated[pulseOutput] != 0U))
  1833. {
  1834. PlsrObservedPulseBase[pulseOutput] =
  1835. PlsrCounterSnapshotStopped(pulseOutput);
  1836. }
  1837. else
  1838. {
  1839. PlsrCounterSuspend(pulseOutput);
  1840. PlsrObservedPulseBase[pulseOutput] =
  1841. PlsrCounterSnapshot(pulseOutput);
  1842. }
  1843. PlsrObservedPulsePublished[pulseOutput] =
  1844. PlsrObservedPulseBase[pulseOutput];
  1845. counter->CR1 = 0UL;
  1846. counter->DIER = 0UL;
  1847. counter->SMCR = 0UL;
  1848. counter->SR = 0UL;
  1849. if (PlsrCounterOwner[index] == pulseOutput)
  1850. {
  1851. uint8_t sourceAxis = PlsrCounterTriggerSourceAxis[index];
  1852. if (sourceAxis <= 3U)
  1853. {
  1854. PlsrTimerMap[sourceAxis].timer->CR2 &= ~TIM_CR2_MMS;
  1855. }
  1856. PlsrCounterTriggerSourceAxis[index] = PLSR_COUNTER_NONE;
  1857. PlsrCounterOwner[index] = PLSR_COUNTER_NONE;
  1858. }
  1859. }
  1860. PlsrCounterIndexByOutput[pulseOutput] = PLSR_COUNTER_NONE;
  1861. }
  1862. static uint8_t PlsrCounterConfigure(uint8_t pulseOutput, uint8_t outputMode)
  1863. {
  1864. uint8_t index = PlsrCounterIndex(pulseOutput, outputMode);
  1865. uint8_t sourceAxis = (outputMode == PLSR_OUTPUT_AB)
  1866. ? ((pulseOutput == 0U) ? 0U : 3U)
  1867. : pulseOutput;
  1868. TIM_TypeDef *counter = PlsrCounters[index];
  1869. TIM_TypeDef *sourceTimer = PlsrTimerMap[sourceAxis].timer;
  1870. uint32_t triggerSelection = ((pulseOutput & 2U) == 0U)
  1871. ? TIM_SMCR_TS_1
  1872. : (TIM_SMCR_TS_1 | TIM_SMCR_TS_0);
  1873. PlsrCounterStop(pulseOutput);
  1874. if ((PlsrCounterOwner[index] != PLSR_COUNTER_NONE)
  1875. && (PlsrCounterOwner[index] != pulseOutput))
  1876. {
  1877. return 0U;
  1878. }
  1879. PlsrCounterOwner[index] = pulseOutput;
  1880. PlsrCounterTriggerSourceAxis[index] = sourceAxis;
  1881. PlsrCounterIndexByOutput[pulseOutput] = index;
  1882. PlsrCounterOverflowPulses[index] = 0UL;
  1883. counter->CR1 = 0UL;
  1884. counter->DIER = 0UL;
  1885. counter->SMCR = 0UL;
  1886. counter->PSC = 0UL;
  1887. counter->ARR = 0xFFFFUL;
  1888. counter->CNT = 0UL;
  1889. counter->EGR = TIM_EGR_UG;
  1890. counter->SR = 0UL;
  1891. sourceTimer->CR2 = (sourceTimer->CR2 & ~TIM_CR2_MMS) | TIM_CR2_MMS_1;
  1892. counter->SMCR = triggerSelection;
  1893. counter->DIER = TIM_DIER_UIE;
  1894. return 1U;
  1895. }
  1896. static void PlsrCounterBegin(uint8_t pulseOutput)
  1897. {
  1898. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  1899. if (index < PLSR_COUNTER_COUNT)
  1900. {
  1901. TIM_TypeDef *counter = PlsrCounters[index];
  1902. counter->SMCR |= TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0;
  1903. counter->CR1 |= TIM_CR1_CEN;
  1904. }
  1905. }
  1906. static void PlsrCounterSuspend(uint8_t pulseOutput)
  1907. {
  1908. uint8_t index = PlsrCounterIndexByOutput[pulseOutput];
  1909. if (index < PLSR_COUNTER_COUNT)
  1910. {
  1911. PlsrCounters[index]->CR1 &= ~TIM_CR1_CEN;
  1912. PlsrCounters[index]->SMCR &=
  1913. ~(TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0);
  1914. }
  1915. }
  1916. static void PlsrAbLoadAndStart(uint8_t pulseOutput,
  1917. const PLSR_AB_SETTING *setting)
  1918. {
  1919. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  1920. uint8_t leadOutput =
  1921. (PlsrTimerDirectionPositive[pulseOutput] != 0U)
  1922. ? pulseOutput : pairOutput;
  1923. uint8_t lagOutput = (leadOutput == pulseOutput)
  1924. ? pairOutput : pulseOutput;
  1925. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  1926. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  1927. TIM_TypeDef *counterSourceTimer;
  1928. TIM_TypeDef *otherTimer;
  1929. uint32_t periodCounts = setting->period + 1UL;
  1930. uint32_t leadStart = (periodCounts * 3UL) / 4UL + 1UL;
  1931. uint32_t lagStart = periodCounts / 2UL + 1UL;
  1932. uint32_t counterSourceCr1;
  1933. uint32_t otherCr1;
  1934. if (leadStart >= periodCounts)
  1935. {
  1936. leadStart = periodCounts - 1UL;
  1937. }
  1938. if (lagStart >= periodCounts)
  1939. {
  1940. lagStart = periodCounts - 1UL;
  1941. }
  1942. PlsrAbStructureVerified[pulseOutput] = 0U;
  1943. PlsrAbLagAxis[pulseOutput] = lagOutput;
  1944. PlsrAbCounterSourceAxis[pulseOutput] =
  1945. (pulseOutput == 0U) ? pulseOutput : pairOutput;
  1946. PlsrAbCounterBoundary[pulseOutput] =
  1947. (PlsrAbCounterSourceAxis[pulseOutput] == leadOutput)
  1948. ? (leadStart - 1UL) : (periodCounts / 2UL);
  1949. PlsrCounterSuspend(pulseOutput);
  1950. baseTimer->CR1 &= ~TIM_CR1_CEN;
  1951. pairTimer->CR1 &= ~TIM_CR1_CEN;
  1952. PlsrAbHoldPairIdle(pulseOutput);
  1953. baseTimer->CCER &= ~TIM_CCER_CC1E;
  1954. pairTimer->CCER &= ~TIM_CCER_CC1E;
  1955. baseTimer->DIER = 0UL;
  1956. pairTimer->DIER = 0UL;
  1957. baseTimer->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE;
  1958. pairTimer->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE;
  1959. baseTimer->PSC = setting->basePrescaler;
  1960. pairTimer->PSC = setting->pairPrescaler;
  1961. baseTimer->ARR = setting->period;
  1962. pairTimer->ARR = setting->period;
  1963. baseTimer->CCR1 = setting->compare;
  1964. pairTimer->CCR1 = setting->compare;
  1965. baseTimer->CR1 = TIM_CR1_ARPE | TIM_CR1_URS;
  1966. pairTimer->CR1 = TIM_CR1_ARPE | TIM_CR1_URS;
  1967. baseTimer->EGR = TIM_EGR_UG;
  1968. pairTimer->EGR = TIM_EGR_UG;
  1969. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  1970. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  1971. PlsrTimerMap[leadOutput].timer->CNT = leadStart;
  1972. PlsrTimerMap[lagOutput].timer->CNT = lagStart;
  1973. baseTimer->CCER = (baseTimer->CCER
  1974. & ~(TIM_CCER_CC1P | TIM_CCER_CC1E))
  1975. | TIM_CCER_CC1P | TIM_CCER_CC1E;
  1976. pairTimer->CCER = (pairTimer->CCER
  1977. & ~(TIM_CCER_CC1P | TIM_CCER_CC1E))
  1978. | TIM_CCER_CC1P | TIM_CCER_CC1E;
  1979. PlsrAbReleasePair(pulseOutput);
  1980. baseTimer->CCMR1 = TIM_CCMR1_OC1PE
  1981. | (6UL << TIM_CCMR1_OC1M_Pos);
  1982. pairTimer->CCMR1 = TIM_CCMR1_OC1PE
  1983. | (6UL << TIM_CCMR1_OC1M_Pos);
  1984. PlsrCounterBegin(pulseOutput);
  1985. counterSourceTimer =
  1986. PlsrTimerMap[PlsrAbCounterSourceAxis[pulseOutput]].timer;
  1987. otherTimer =
  1988. PlsrTimerMap[(PlsrAbCounterSourceAxis[pulseOutput] == pulseOutput)
  1989. ? pairOutput : pulseOutput].timer;
  1990. counterSourceCr1 = counterSourceTimer->CR1 | TIM_CR1_CEN;
  1991. otherCr1 = otherTimer->CR1 | TIM_CR1_CEN;
  1992. PlsrAbEnableTimerPair(counterSourceTimer, counterSourceCr1,
  1993. otherTimer, otherCr1);
  1994. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  1995. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  1996. PlsrTimerMap[lagOutput].timer->DIER |= TIM_DIER_CC1IE;
  1997. __DMB();
  1998. }
  1999. static void PlsrAbScheduleFrequencyVerify(uint8_t pulseOutput)
  2000. {
  2001. uint8_t verifyOutput =
  2002. (PlsrAbLagAxis[pulseOutput] == pulseOutput)
  2003. ? (uint8_t)(pulseOutput + 1U) : pulseOutput;
  2004. TIM_TypeDef *verifyTimer = PlsrTimerMap[verifyOutput].timer;
  2005. PlsrFrequencyVerifyPending[pulseOutput] =
  2006. PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ;
  2007. PlsrAbVerifyOwner[verifyOutput] = pulseOutput;
  2008. verifyTimer->SR = ~TIM_SR_UIF;
  2009. verifyTimer->DIER |= TIM_DIER_UIE;
  2010. if (PlsrDeferredPulsePending[pulseOutput] != 0U)
  2011. {
  2012. verifyTimer->SR = ~TIM_SR_CC1IF;
  2013. verifyTimer->DIER |= TIM_DIER_CC1IE;
  2014. }
  2015. __DMB();
  2016. }
  2017. static uint32_t PlsrFrequencyFromSnapshot(
  2018. uint8_t pulseOutput,
  2019. const PLSR_TIMER_SNAPSHOT *snapshot)
  2020. {
  2021. uint64_t divider = ((uint64_t)snapshot->psc + 1UL)
  2022. * ((uint64_t)snapshot->arr + 1UL);
  2023. if (divider == 0UL)
  2024. {
  2025. return 0UL;
  2026. }
  2027. return (uint32_t)(((uint64_t)PlsrTimerMap[pulseOutput].timerClockHz
  2028. + divider / 2UL)
  2029. / divider);
  2030. }
  2031. static uint32_t PlsrVerifyActiveFrequency(uint8_t pulseOutput)
  2032. {
  2033. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2034. PLSR_TIMER_SNAPSHOT baseSnapshot;
  2035. uint32_t activeFrequency = PlsrTimerActiveFrequencyHz[pulseOutput];
  2036. PlsrTimerSnapshot(baseTimer, &baseSnapshot);
  2037. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2038. {
  2039. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2040. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2041. PLSR_TIMER_SNAPSHOT pairSnapshot;
  2042. uint8_t structureValid;
  2043. PlsrTimerSnapshot(pairTimer, &pairSnapshot);
  2044. structureValid = PlsrAbStructureIsRunnable(
  2045. pulseOutput, &baseSnapshot, &pairSnapshot);
  2046. PlsrAbStructureVerified[pulseOutput] = structureValid;
  2047. if (structureValid == 0U)
  2048. {
  2049. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_CURVE;
  2050. }
  2051. if ((baseSnapshot.psc
  2052. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  2053. || (pairSnapshot.psc
  2054. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  2055. || (baseSnapshot.arr
  2056. != PlsrAbActiveSetting[pulseOutput].period)
  2057. || (pairSnapshot.arr
  2058. != PlsrAbActiveSetting[pulseOutput].period)
  2059. || (baseSnapshot.ccr1
  2060. != PlsrAbActiveSetting[pulseOutput].compare)
  2061. || (pairSnapshot.ccr1
  2062. != PlsrAbActiveSetting[pulseOutput].compare))
  2063. {
  2064. activeFrequency = PlsrFrequencyFromSnapshot(pairOutput,
  2065. &pairSnapshot);
  2066. if (PlsrPlatformFaultPending != PLSR_PLATFORM_FAULT_CURVE)
  2067. {
  2068. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_FREQUENCY;
  2069. }
  2070. }
  2071. }
  2072. else
  2073. {
  2074. uint8_t structureValid =
  2075. ((((baseSnapshot.cr1 & TIM_CR1_CEN) != 0UL)
  2076. && ((baseSnapshot.ccer & TIM_CCER_CC1E) != 0UL)
  2077. && ((baseSnapshot.ccer & TIM_CCER_CC1P) == 0UL)
  2078. && ((baseSnapshot.ccmr1 & PLSR_TIMER_OC1_MODE_MASK)
  2079. == PLSR_TIMER_PWM1_MODE)
  2080. && (baseSnapshot.ccr1
  2081. == ((baseSnapshot.arr + 1UL) / 2UL)))
  2082. ? 1U : 0U);
  2083. if (structureValid == 0U)
  2084. {
  2085. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_CURVE;
  2086. }
  2087. if ((baseSnapshot.psc
  2088. != PlsrTimerQueuedSetting[pulseOutput].prescaler)
  2089. || (baseSnapshot.arr
  2090. != PlsrTimerQueuedSetting[pulseOutput].period)
  2091. || (baseSnapshot.ccr1
  2092. != PlsrTimerQueuedSetting[pulseOutput].compare))
  2093. {
  2094. activeFrequency = PlsrFrequencyFromSnapshot(pulseOutput,
  2095. &baseSnapshot);
  2096. if (PlsrPlatformFaultPending != PLSR_PLATFORM_FAULT_CURVE)
  2097. {
  2098. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_FREQUENCY;
  2099. }
  2100. }
  2101. }
  2102. if (activeFrequency == 0UL)
  2103. {
  2104. PlsrPlatformFaultPending = PLSR_PLATFORM_FAULT_FREQUENCY;
  2105. }
  2106. return activeFrequency;
  2107. }
  2108. uint8_t PlsrPlatformInit(void)
  2109. {
  2110. GPIO_InitTypeDef gpio;
  2111. uint8_t index;
  2112. const PLSR_BACKUP_POSITION_RECORD *positionRecord;
  2113. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_NONE;
  2114. __HAL_RCC_GPIOB_CLK_ENABLE();
  2115. __HAL_RCC_GPIOF_CLK_ENABLE();
  2116. __HAL_RCC_GPIOG_CLK_ENABLE();
  2117. __HAL_RCC_GPIOH_CLK_ENABLE();
  2118. __HAL_RCC_TIM10_CLK_ENABLE();
  2119. __HAL_RCC_TIM11_CLK_ENABLE();
  2120. __HAL_RCC_TIM13_CLK_ENABLE();
  2121. __HAL_RCC_TIM14_CLK_ENABLE();
  2122. __HAL_RCC_TIM9_CLK_ENABLE();
  2123. __HAL_RCC_TIM12_CLK_ENABLE();
  2124. __HAL_RCC_PWR_CLK_ENABLE();
  2125. HAL_PWR_EnableBkUpAccess();
  2126. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  2127. if (HAL_PWREx_EnableBkUpReg() != HAL_OK)
  2128. {
  2129. return 0U;
  2130. }
  2131. #if PLSR_DEBUG_TIMING
  2132. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  2133. DWT->CYCCNT = 0UL;
  2134. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  2135. (void)memset((void *)PlsrIrqCount, 0, sizeof(PlsrIrqCount));
  2136. (void)memset((void *)PlsrIrqLastCycles, 0, sizeof(PlsrIrqLastCycles));
  2137. (void)memset((void *)PlsrIrqMaxCycles, 0, sizeof(PlsrIrqMaxCycles));
  2138. (void)memset((void *)PlsrFinalArmQueueCount, 0,
  2139. sizeof(PlsrFinalArmQueueCount));
  2140. (void)memset((void *)PlsrFinalArmJobLastCycles, 0,
  2141. sizeof(PlsrFinalArmJobLastCycles));
  2142. (void)memset((void *)PlsrFinalArmJobMaxCycles, 0,
  2143. sizeof(PlsrFinalArmJobMaxCycles));
  2144. (void)memset((void *)PlsrFinalArmQueueToStopLastCycles, 0,
  2145. sizeof(PlsrFinalArmQueueToStopLastCycles));
  2146. (void)memset((void *)PlsrFinalArmQueueToStopMaxCycles, 0,
  2147. sizeof(PlsrFinalArmQueueToStopMaxCycles));
  2148. (void)memset((void *)PlsrFinalArmQueuedAt, 0,
  2149. sizeof(PlsrFinalArmQueuedAt));
  2150. (void)memset((void *)PlsrFinalArmQueueTimingPending, 0,
  2151. sizeof(PlsrFinalArmQueueTimingPending));
  2152. #endif
  2153. HAL_GPIO_WritePin(GPIOH, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8
  2154. | GPIO_PIN_9, GPIO_PIN_SET);
  2155. gpio.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
  2156. gpio.Mode = GPIO_MODE_OUTPUT_PP;
  2157. gpio.Pull = GPIO_NOPULL;
  2158. gpio.Speed = GPIO_SPEED_FREQ_HIGH;
  2159. gpio.Alternate = 0U;
  2160. HAL_GPIO_Init(GPIOH, &gpio);
  2161. gpio.Mode = GPIO_MODE_INPUT;
  2162. gpio.Pull = GPIO_NOPULL;
  2163. gpio.Speed = GPIO_SPEED_FREQ_LOW;
  2164. gpio.Alternate = 0U;
  2165. gpio.Pin = GPIO_PIN_5;
  2166. HAL_GPIO_Init(GPIOB, &gpio);
  2167. gpio.Pin = GPIO_PIN_12;
  2168. HAL_GPIO_Init(GPIOG, &gpio);
  2169. for (index = 0U; index < 4U; index++)
  2170. {
  2171. PlsrTimerActiveFrequencyHz[index] = 0UL;
  2172. PlsrTimerQueuedFrequencyHz[index] = 0UL;
  2173. (void)memset(&PlsrTimerActiveSetting[index], 0,
  2174. sizeof(PlsrTimerActiveSetting[index]));
  2175. (void)memset(&PlsrTimerQueuedSetting[index], 0,
  2176. sizeof(PlsrTimerQueuedSetting[index]));
  2177. PlsrTimerQueueGeneration[index] = 0UL;
  2178. PlsrTimerOutputMode[index] = PLSR_OUTPUT_PULSE_DIR;
  2179. PlsrTimerDirectionPositive[index] = 1U;
  2180. PlsrTimerRunning[index] = 0U;
  2181. PlsrFrequencyVerifyPending[index] = 0U;
  2182. PlsrFrequencyVerifyPulseCount[index] = 0U;
  2183. PlsrDeferredPulsePending[index] = 0U;
  2184. PlsrAbVerifyOwner[index] = PLSR_COUNTER_NONE;
  2185. PlsrAbFinalArmJobOwner[index] = PLSR_COUNTER_NONE;
  2186. PlsrTimerIrqActive[index] = 0U;
  2187. PlsrAbFrequencyPending[index] = 0U;
  2188. PlsrAbLagAxis[index] = PLSR_COUNTER_NONE;
  2189. PlsrAbStructureVerified[index] = 0U;
  2190. PlsrAbCounterSourceAxis[index] = PLSR_COUNTER_NONE;
  2191. PlsrAbCounterBoundary[index] = 0UL;
  2192. PlsrAbStopPending[index] = 0U;
  2193. PlsrAbFastGated[index] = 0U;
  2194. PlsrCounterIndexByOutput[index] = PLSR_COUNTER_NONE;
  2195. PlsrObservedPulseBase[index] = 0UL;
  2196. PlsrObservedPulsePublished[index] = 0UL;
  2197. PlsrTimerInitialize(PlsrTimerMap[index].timer);
  2198. PlsrPulsePinHoldIdle(index);
  2199. HAL_NVIC_SetPriority(PlsrTimerMap[index].irq, 1U, 0U);
  2200. HAL_NVIC_EnableIRQ(PlsrTimerMap[index].irq);
  2201. }
  2202. for (index = 0U; index < PLSR_COUNTER_COUNT; index++)
  2203. {
  2204. PlsrCounterOwner[index] = PLSR_COUNTER_NONE;
  2205. PlsrCounterTriggerSourceAxis[index] = PLSR_COUNTER_NONE;
  2206. PlsrCounterOverflowPulses[index] = 0UL;
  2207. PlsrCounters[index]->CR1 = 0UL;
  2208. PlsrCounters[index]->DIER = 0UL;
  2209. PlsrCounters[index]->SMCR = 0UL;
  2210. PlsrCounters[index]->SR = 0UL;
  2211. }
  2212. PlsrPlatformFaultPending = 0U;
  2213. HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 1U, 0U);
  2214. HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
  2215. HAL_NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 1U, 0U);
  2216. HAL_NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn);
  2217. positionRecord = PlsrNewestBackupPosition();
  2218. PlsrBackupPositionGeneration =
  2219. (positionRecord == NULL) ? 0UL : positionRecord->generation;
  2220. return 1U;
  2221. }
  2222. uint8_t PlsrPlatformPrepare(uint8_t pulseOutput,
  2223. uint8_t directionOutput,
  2224. uint8_t directionLevel,
  2225. uint8_t outputMode,
  2226. uint8_t directionPositive)
  2227. {
  2228. uint8_t index;
  2229. if ((pulseOutput > 3U) || (directionOutput > 3U)
  2230. || (outputMode > PLSR_OUTPUT_AB)
  2231. || ((outputMode == PLSR_OUTPUT_AB)
  2232. && (pulseOutput != 0U) && (pulseOutput != 2U)))
  2233. {
  2234. return 0U;
  2235. }
  2236. if ((PlsrAbStopPending[0] != 0U) || (PlsrAbStopPending[2] != 0U))
  2237. {
  2238. return 0U;
  2239. }
  2240. for (index = 0U; index < 4U; index++)
  2241. {
  2242. PlsrAbFinalArmJobOwner[index] = PLSR_COUNTER_NONE;
  2243. NVIC_ClearPendingIRQ(PlsrTimerMap[index].irq);
  2244. PlsrCounterStop(index);
  2245. PlsrPulsePinHoldIdle(index);
  2246. PlsrTimerStop(PlsrTimerMap[index].timer);
  2247. HAL_GPIO_WritePin(PlsrDirectionMap[index].port,
  2248. PlsrDirectionMap[index].pin,
  2249. ((outputMode == PLSR_OUTPUT_PULSE_DIR)
  2250. && (index == directionOutput)
  2251. && (directionLevel != 0U))
  2252. ? GPIO_PIN_RESET : GPIO_PIN_SET);
  2253. PlsrTimerRunning[index] = 0U;
  2254. PlsrFrequencyVerifyPending[index] = 0U;
  2255. PlsrFrequencyVerifyPulseCount[index] = 0U;
  2256. PlsrDeferredPulsePending[index] = 0U;
  2257. PlsrAbVerifyOwner[index] = PLSR_COUNTER_NONE;
  2258. PlsrAbFrequencyPending[index] = 0U;
  2259. PlsrAbStructureVerified[index] = 0U;
  2260. PlsrAbFastGated[index] = 0U;
  2261. }
  2262. PlsrTimerOutputMode[pulseOutput] = outputMode;
  2263. PlsrTimerDirectionPositive[pulseOutput] =
  2264. (directionPositive != 0U) ? 1U : 0U;
  2265. PlsrPlatformFaultPending = 0U;
  2266. return 1U;
  2267. }
  2268. uint8_t PlsrPlatformStartPulse(uint8_t pulseOutput,
  2269. uint32_t firstFrequencyHz,
  2270. uint32_t queuedFrequencyHz,
  2271. uint32_t *actualFirstFrequencyHz,
  2272. uint32_t *actualQueuedFrequencyHz)
  2273. {
  2274. PLSR_PLATFORM_TIMER_SETTING firstSetting;
  2275. PLSR_PLATFORM_TIMER_SETTING queuedSetting;
  2276. if ((pulseOutput > 3U)
  2277. || (PlsrPlatformBuildTimerSetting(
  2278. pulseOutput, PlsrTimerOutputMode[pulseOutput],
  2279. firstFrequencyHz, &firstSetting) == 0U)
  2280. || (PlsrPlatformBuildTimerSetting(
  2281. pulseOutput, PlsrTimerOutputMode[pulseOutput],
  2282. queuedFrequencyHz, &queuedSetting) == 0U))
  2283. {
  2284. return 0U;
  2285. }
  2286. return PlsrPlatformStartPrepared(pulseOutput, &firstSetting,
  2287. &queuedSetting,
  2288. actualFirstFrequencyHz,
  2289. actualQueuedFrequencyHz);
  2290. }
  2291. static uint8_t PlsrPreparedSettingIsValid(
  2292. uint8_t pulseOutput,
  2293. uint8_t outputMode,
  2294. const PLSR_PLATFORM_TIMER_SETTING *setting)
  2295. {
  2296. uint32_t periodCounts;
  2297. if ((pulseOutput > 3U) || (setting == NULL)
  2298. || (setting->actualFrequencyHz == 0UL)
  2299. || (setting->actualFrequencyHz > PLSR_FREQUENCY_MAX_HZ))
  2300. {
  2301. return 0U;
  2302. }
  2303. periodCounts = (uint32_t)setting->period + 1UL;
  2304. if (setting->compare != (uint16_t)(periodCounts / 2UL))
  2305. {
  2306. return 0U;
  2307. }
  2308. if (outputMode == PLSR_OUTPUT_PULSE_DIR)
  2309. {
  2310. return ((periodCounts >= 2UL)
  2311. && (setting->pairPrescaler == 0U)) ? 1U : 0U;
  2312. }
  2313. if ((outputMode != PLSR_OUTPUT_AB) || ((pulseOutput & 1U) != 0U)
  2314. || (periodCounts < 4UL))
  2315. {
  2316. return 0U;
  2317. }
  2318. return ((((uint32_t)setting->prescaler + 1UL)
  2319. == 2UL * ((uint32_t)setting->pairPrescaler + 1UL))
  2320. ? 1U : 0U);
  2321. }
  2322. static void PlsrPlatformToTimerSetting(
  2323. const PLSR_PLATFORM_TIMER_SETTING *source,
  2324. PLSR_TIMER_SETTING *destination)
  2325. {
  2326. destination->prescaler = source->prescaler;
  2327. destination->period = source->period;
  2328. destination->compare = source->compare;
  2329. destination->actualFrequencyHz = source->actualFrequencyHz;
  2330. }
  2331. static void PlsrPlatformToAbSetting(
  2332. const PLSR_PLATFORM_TIMER_SETTING *source,
  2333. PLSR_AB_SETTING *destination)
  2334. {
  2335. destination->basePrescaler = source->prescaler;
  2336. destination->pairPrescaler = source->pairPrescaler;
  2337. destination->period = source->period;
  2338. destination->compare = source->compare;
  2339. destination->actualFrequencyHz = source->actualFrequencyHz;
  2340. }
  2341. static uint8_t PlsrPlatformSettingsDiffer(
  2342. const PLSR_PLATFORM_TIMER_SETTING *first,
  2343. const PLSR_PLATFORM_TIMER_SETTING *second)
  2344. {
  2345. return (((first->actualFrequencyHz != second->actualFrequencyHz)
  2346. || (first->prescaler != second->prescaler)
  2347. || (first->pairPrescaler != second->pairPrescaler)
  2348. || (first->period != second->period)
  2349. || (first->compare != second->compare)) ? 1U : 0U);
  2350. }
  2351. uint8_t PlsrPlatformBuildTimerSetting(
  2352. uint8_t pulseOutput,
  2353. uint8_t outputMode,
  2354. uint32_t requestedFrequencyHz,
  2355. PLSR_PLATFORM_TIMER_SETTING *setting)
  2356. {
  2357. PLSR_TIMER_SETTING timerSetting;
  2358. PLSR_AB_SETTING abSetting;
  2359. if (setting == NULL)
  2360. {
  2361. return 0U;
  2362. }
  2363. if (outputMode == PLSR_OUTPUT_AB)
  2364. {
  2365. if (PlsrAbCalculate(pulseOutput, requestedFrequencyHz,
  2366. &abSetting) == 0U)
  2367. {
  2368. return 0U;
  2369. }
  2370. setting->actualFrequencyHz = abSetting.actualFrequencyHz;
  2371. setting->prescaler = (uint16_t)abSetting.basePrescaler;
  2372. setting->pairPrescaler = (uint16_t)abSetting.pairPrescaler;
  2373. setting->period = (uint16_t)abSetting.period;
  2374. setting->compare = (uint16_t)abSetting.compare;
  2375. return 1U;
  2376. }
  2377. if ((outputMode != PLSR_OUTPUT_PULSE_DIR)
  2378. || (PlsrTimerCalculate(pulseOutput, requestedFrequencyHz,
  2379. &timerSetting) == 0U))
  2380. {
  2381. return 0U;
  2382. }
  2383. setting->actualFrequencyHz = timerSetting.actualFrequencyHz;
  2384. setting->prescaler = (uint16_t)timerSetting.prescaler;
  2385. setting->pairPrescaler = 0U;
  2386. setting->period = (uint16_t)timerSetting.period;
  2387. setting->compare = (uint16_t)timerSetting.compare;
  2388. return 1U;
  2389. }
  2390. uint8_t PlsrPlatformStartPrepared(
  2391. uint8_t pulseOutput,
  2392. const PLSR_PLATFORM_TIMER_SETTING *firstSetting,
  2393. const PLSR_PLATFORM_TIMER_SETTING *queuedSetting,
  2394. uint32_t *actualFirstFrequencyHz,
  2395. uint32_t *actualQueuedFrequencyHz)
  2396. {
  2397. uint8_t outputMode;
  2398. if ((pulseOutput > 3U) || (actualFirstFrequencyHz == NULL)
  2399. || (actualQueuedFrequencyHz == NULL))
  2400. {
  2401. return 0U;
  2402. }
  2403. outputMode = PlsrTimerOutputMode[pulseOutput];
  2404. if ((PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2405. firstSetting) == 0U)
  2406. || (PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2407. queuedSetting) == 0U))
  2408. {
  2409. return 0U;
  2410. }
  2411. if (outputMode == PLSR_OUTPUT_AB)
  2412. {
  2413. PLSR_AB_SETTING firstAbSetting;
  2414. PLSR_AB_SETTING queuedAbSetting;
  2415. if ((PlsrAbStopPending[pulseOutput] != 0U)
  2416. || (PlsrAbFastGated[pulseOutput] != 0U)
  2417. || (PlsrCounterConfigure(pulseOutput, PLSR_OUTPUT_AB) == 0U))
  2418. {
  2419. return 0U;
  2420. }
  2421. PlsrPlatformToAbSetting(firstSetting, &firstAbSetting);
  2422. PlsrPlatformToAbSetting(queuedSetting, &queuedAbSetting);
  2423. PlsrAbLoadAndStart(pulseOutput, &firstAbSetting);
  2424. PlsrAbActiveSetting[pulseOutput] = firstAbSetting;
  2425. PlsrAbPendingSetting[pulseOutput] = queuedAbSetting;
  2426. PlsrAbFrequencyPending[pulseOutput] =
  2427. ((firstSetting->prescaler != queuedSetting->prescaler)
  2428. || (firstSetting->pairPrescaler
  2429. != queuedSetting->pairPrescaler)
  2430. || (firstSetting->period != queuedSetting->period)) ? 1U : 0U;
  2431. }
  2432. else
  2433. {
  2434. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  2435. PLSR_TIMER_SETTING firstTimerSetting;
  2436. PLSR_TIMER_SETTING queuedTimerSetting;
  2437. if (PlsrCounterConfigure(pulseOutput,
  2438. PLSR_OUTPUT_PULSE_DIR) == 0U)
  2439. {
  2440. return 0U;
  2441. }
  2442. PlsrPlatformToTimerSetting(firstSetting, &firstTimerSetting);
  2443. PlsrPlatformToTimerSetting(queuedSetting, &queuedTimerSetting);
  2444. timer->DIER &= ~TIM_DIER_UIE;
  2445. timer->CR1 &= ~TIM_CR1_CEN;
  2446. timer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2447. timer->CNT = 0UL;
  2448. PlsrTimerWriteSetting(timer, &firstTimerSetting);
  2449. timer->EGR = TIM_EGR_UG;
  2450. PlsrTimerWriteSetting(timer, &queuedTimerSetting);
  2451. timer->CNT = firstTimerSetting.compare;
  2452. timer->SR = 0UL;
  2453. timer->CCER = (timer->CCER
  2454. & ~(TIM_CCER_CC1E | TIM_CCER_CC1P))
  2455. | TIM_CCER_CC1E;
  2456. __DSB();
  2457. timer->DIER |= TIM_DIER_UIE;
  2458. PlsrPulsePinRelease(pulseOutput);
  2459. PlsrCounterBegin(pulseOutput);
  2460. timer->CR1 |= TIM_CR1_CEN;
  2461. }
  2462. PlsrTimerActiveSetting[pulseOutput] = *firstSetting;
  2463. PlsrTimerQueuedSetting[pulseOutput] = *queuedSetting;
  2464. PlsrTimerActiveFrequencyHz[pulseOutput] =
  2465. firstSetting->actualFrequencyHz;
  2466. PlsrTimerQueuedFrequencyHz[pulseOutput] =
  2467. queuedSetting->actualFrequencyHz;
  2468. PlsrTimerQueueGeneration[pulseOutput]++;
  2469. PlsrTimerRunning[pulseOutput] = 1U;
  2470. if (outputMode == PLSR_OUTPUT_AB)
  2471. {
  2472. PlsrAbScheduleFrequencyVerify(pulseOutput);
  2473. }
  2474. else
  2475. {
  2476. PlsrFrequencyVerifyPending[pulseOutput] =
  2477. PLSR_FREQUENCY_VERIFY_NOW;
  2478. }
  2479. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2480. *actualFirstFrequencyHz = firstSetting->actualFrequencyHz;
  2481. *actualQueuedFrequencyHz = queuedSetting->actualFrequencyHz;
  2482. return 1U;
  2483. }
  2484. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformLoadPreparedFromIrq(
  2485. uint8_t pulseOutput,
  2486. const PLSR_PLATFORM_TIMER_SETTING *setting,
  2487. uint32_t *actualFrequencyHz)
  2488. {
  2489. uint8_t outputMode;
  2490. if ((pulseOutput > 3U) || (actualFrequencyHz == NULL))
  2491. {
  2492. return PLSR_PLATFORM_QUEUE_FAILED;
  2493. }
  2494. outputMode = PlsrTimerOutputMode[pulseOutput];
  2495. if (PlsrPreparedSettingIsValid(pulseOutput, outputMode,
  2496. setting) == 0U)
  2497. {
  2498. return PLSR_PLATFORM_QUEUE_FAILED;
  2499. }
  2500. if ((PlsrTimerRunning[pulseOutput] == 0U)
  2501. || (PlsrAbStopPending[pulseOutput] != 0U)
  2502. || (PlsrAbFastGated[pulseOutput] != 0U))
  2503. {
  2504. return PLSR_PLATFORM_QUEUE_STALE;
  2505. }
  2506. if (outputMode == PLSR_OUTPUT_AB)
  2507. {
  2508. PLSR_AB_SETTING pending;
  2509. PlsrPlatformToAbSetting(setting, &pending);
  2510. PlsrAbPendingSetting[pulseOutput] = pending;
  2511. PlsrAbFrequencyPending[pulseOutput] =
  2512. ((setting->prescaler
  2513. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  2514. || (setting->pairPrescaler
  2515. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  2516. || (setting->period
  2517. != PlsrAbActiveSetting[pulseOutput].period)) ? 1U : 0U;
  2518. }
  2519. else
  2520. {
  2521. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  2522. if ((timer->CR1 & TIM_CR1_CEN) == 0UL)
  2523. {
  2524. return PLSR_PLATFORM_QUEUE_STALE;
  2525. }
  2526. timer->PSC = setting->prescaler;
  2527. timer->ARR = setting->period;
  2528. timer->CCR1 = setting->compare;
  2529. __DMB();
  2530. }
  2531. PlsrTimerQueuedSetting[pulseOutput] = *setting;
  2532. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting->actualFrequencyHz;
  2533. PlsrTimerQueueGeneration[pulseOutput]++;
  2534. *actualFrequencyHz = setting->actualFrequencyHz;
  2535. return PLSR_PLATFORM_QUEUE_APPLIED;
  2536. }
  2537. void PlsrPlatformGateFromIrq(uint8_t pulseOutput)
  2538. {
  2539. if ((pulseOutput <= 3U)
  2540. && (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2541. && (PlsrAbFastGated[pulseOutput] != 0U))
  2542. {
  2543. return;
  2544. }
  2545. PlsrPlatformStopPulse(pulseOutput);
  2546. }
  2547. PLSR_PLATFORM_QUEUE_RESULT PlsrPlatformQueueFrequency(
  2548. uint8_t pulseOutput,
  2549. uint32_t frequencyHz,
  2550. uint32_t *actualFrequencyHz)
  2551. {
  2552. TIM_TypeDef *timer;
  2553. PLSR_PLATFORM_TIMER_SETTING setting;
  2554. uint32_t activePeriod;
  2555. uint32_t counter;
  2556. uint32_t criticalState;
  2557. uint32_t ownGeneration;
  2558. uint8_t updatePending;
  2559. if ((pulseOutput > 3U) || (actualFrequencyHz == NULL)
  2560. || (PlsrPlatformBuildTimerSetting(
  2561. pulseOutput, PlsrTimerOutputMode[pulseOutput], frequencyHz,
  2562. &setting) == 0U))
  2563. {
  2564. return PLSR_PLATFORM_QUEUE_FAILED;
  2565. }
  2566. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2567. {
  2568. PLSR_AB_SETTING pending;
  2569. uint32_t criticalState;
  2570. if ((PlsrTimerRunning[pulseOutput] == 0U)
  2571. || (PlsrAbStopPending[pulseOutput] != 0U)
  2572. || (PlsrAbFastGated[pulseOutput] != 0U))
  2573. {
  2574. return PLSR_PLATFORM_QUEUE_STALE;
  2575. }
  2576. criticalState = PlsrPlatformEnterCritical();
  2577. if ((PlsrTimerRunning[pulseOutput] == 0U)
  2578. || (PlsrAbStopPending[pulseOutput] != 0U)
  2579. || (PlsrAbFastGated[pulseOutput] != 0U))
  2580. {
  2581. PlsrPlatformExitCritical(criticalState);
  2582. return PLSR_PLATFORM_QUEUE_STALE;
  2583. }
  2584. PlsrPlatformToAbSetting(&setting, &pending);
  2585. PlsrAbPendingSetting[pulseOutput] = pending;
  2586. PlsrAbFrequencyPending[pulseOutput] =
  2587. ((pending.basePrescaler
  2588. != PlsrAbActiveSetting[pulseOutput].basePrescaler)
  2589. || (pending.pairPrescaler
  2590. != PlsrAbActiveSetting[pulseOutput].pairPrescaler)
  2591. || (pending.period
  2592. != PlsrAbActiveSetting[pulseOutput].period)) ? 1U : 0U;
  2593. PlsrTimerQueuedSetting[pulseOutput] = setting;
  2594. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting.actualFrequencyHz;
  2595. PlsrTimerQueueGeneration[pulseOutput]++;
  2596. *actualFrequencyHz = setting.actualFrequencyHz;
  2597. PlsrPlatformExitCritical(criticalState);
  2598. return PLSR_PLATFORM_QUEUE_APPLIED;
  2599. }
  2600. timer = PlsrTimerMap[pulseOutput].timer;
  2601. if ((timer->CR1 & TIM_CR1_CEN) == 0UL)
  2602. {
  2603. return PLSR_PLATFORM_QUEUE_STALE;
  2604. }
  2605. criticalState = PlsrPlatformEnterCritical();
  2606. if ((timer->SR & TIM_SR_UIF) != 0UL)
  2607. {
  2608. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  2609. PlsrPlatformExitCritical(criticalState);
  2610. return PLSR_PLATFORM_QUEUE_STALE;
  2611. }
  2612. activePeriod = PlsrTimerActiveSetting[pulseOutput].period;
  2613. counter = timer->CNT;
  2614. if ((counter > activePeriod)
  2615. || ((activePeriod - counter) < PLSR_QUEUE_WRITE_GUARD_COUNTS))
  2616. {
  2617. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  2618. PlsrPlatformExitCritical(criticalState);
  2619. return PLSR_PLATFORM_QUEUE_STALE;
  2620. }
  2621. /* Preserve real update events and their TRGO pulse while replacing the
  2622. three preload registers. The near-wrap guard bounds the write window. */
  2623. timer->PSC = setting.prescaler;
  2624. timer->ARR = setting.period;
  2625. timer->CCR1 = setting.compare;
  2626. __DMB();
  2627. updatePending = ((timer->SR & TIM_SR_UIF) != 0UL) ? 1U : 0U;
  2628. PlsrTimerQueuedFrequencyHz[pulseOutput] = setting.actualFrequencyHz;
  2629. PlsrTimerQueuedSetting[pulseOutput] = setting;
  2630. PlsrTimerQueueGeneration[pulseOutput]++;
  2631. ownGeneration = PlsrTimerQueueGeneration[pulseOutput];
  2632. if (updatePending != 0U)
  2633. {
  2634. *actualFrequencyHz = PlsrTimerQueuedFrequencyHz[pulseOutput];
  2635. PlsrPlatformExitCritical(criticalState);
  2636. return PLSR_PLATFORM_QUEUE_STALE;
  2637. }
  2638. *actualFrequencyHz =
  2639. (PlsrTimerQueueGeneration[pulseOutput] == ownGeneration)
  2640. ? setting.actualFrequencyHz
  2641. : PlsrTimerQueuedFrequencyHz[pulseOutput];
  2642. if (PlsrTimerQueueGeneration[pulseOutput] != ownGeneration)
  2643. {
  2644. PlsrPlatformExitCritical(criticalState);
  2645. return PLSR_PLATFORM_QUEUE_STALE;
  2646. }
  2647. PlsrPlatformExitCritical(criticalState);
  2648. return PLSR_PLATFORM_QUEUE_APPLIED;
  2649. }
  2650. void PlsrPlatformDrainPendingPulse(uint8_t pulseOutput)
  2651. {
  2652. if (pulseOutput <= 3U)
  2653. {
  2654. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2655. && (PlsrDeferredPulsePending[pulseOutput] != 0U))
  2656. {
  2657. uint8_t verifyOutput =
  2658. (PlsrAbLagAxis[pulseOutput] == pulseOutput)
  2659. ? (uint8_t)(pulseOutput + 1U) : pulseOutput;
  2660. TIM_TypeDef *verifyTimer = PlsrTimerMap[verifyOutput].timer;
  2661. verifyTimer->DIER &= ~(TIM_DIER_UIE | TIM_DIER_CC1IE);
  2662. verifyTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2663. PlsrAbVerifyOwner[verifyOutput] = PLSR_COUNTER_NONE;
  2664. if (PlsrFrequencyVerifyPending[pulseOutput]
  2665. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  2666. {
  2667. PlsrFrequencyVerifyPending[pulseOutput] =
  2668. PLSR_FREQUENCY_VERIFY_NONE;
  2669. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2670. (void)PlsrVerifyActiveFrequency(pulseOutput);
  2671. }
  2672. PlsrDeferredPulsePending[pulseOutput] = 0U;
  2673. PlsrPulseTimerIrq(pulseOutput);
  2674. return;
  2675. }
  2676. PlsrHandleTimerIrq(pulseOutput);
  2677. }
  2678. }
  2679. uint32_t PlsrPlatformActiveFrequency(uint8_t pulseOutput)
  2680. {
  2681. if (pulseOutput > 3U)
  2682. {
  2683. return 0UL;
  2684. }
  2685. if ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2686. && (PlsrAbFastGated[pulseOutput] != 0U))
  2687. {
  2688. PlsrFrequencyVerifyPending[pulseOutput] =
  2689. PLSR_FREQUENCY_VERIFY_NONE;
  2690. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2691. return PlsrTimerActiveFrequencyHz[pulseOutput];
  2692. }
  2693. if (PlsrFrequencyVerifyPending[pulseOutput]
  2694. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  2695. {
  2696. return PlsrTimerActiveFrequencyHz[pulseOutput];
  2697. }
  2698. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2699. {
  2700. PlsrFrequencyVerifyPulseCount[pulseOutput]++;
  2701. if (PlsrFrequencyVerifyPulseCount[pulseOutput]
  2702. >= PLSR_STRUCTURE_VERIFY_INTERVAL)
  2703. {
  2704. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2705. PlsrAbScheduleFrequencyVerify(pulseOutput);
  2706. }
  2707. return PlsrTimerActiveFrequencyHz[pulseOutput];
  2708. }
  2709. if (PlsrFrequencyVerifyPending[pulseOutput]
  2710. == PLSR_FREQUENCY_VERIFY_NOW)
  2711. {
  2712. PlsrFrequencyVerifyPending[pulseOutput] =
  2713. PLSR_FREQUENCY_VERIFY_NONE;
  2714. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2715. return PlsrVerifyActiveFrequency(pulseOutput);
  2716. }
  2717. PlsrFrequencyVerifyPulseCount[pulseOutput]++;
  2718. if (PlsrFrequencyVerifyPulseCount[pulseOutput]
  2719. >= PLSR_STRUCTURE_VERIFY_INTERVAL)
  2720. {
  2721. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2722. return PlsrVerifyActiveFrequency(pulseOutput);
  2723. }
  2724. return PlsrTimerActiveFrequencyHz[pulseOutput];
  2725. }
  2726. uint8_t PlsrPlatformExpectedFrequency(uint8_t pulseOutput,
  2727. uint8_t outputMode,
  2728. uint32_t requestedFrequencyHz,
  2729. uint32_t *actualFrequencyHz)
  2730. {
  2731. PLSR_PLATFORM_TIMER_SETTING setting;
  2732. if (actualFrequencyHz == NULL)
  2733. {
  2734. return 0U;
  2735. }
  2736. if (PlsrPlatformBuildTimerSetting(pulseOutput, outputMode,
  2737. requestedFrequencyHz,
  2738. &setting) == 0U)
  2739. {
  2740. return 0U;
  2741. }
  2742. *actualFrequencyHz = setting.actualFrequencyHz;
  2743. return 1U;
  2744. }
  2745. uint64_t PlsrPlatformObservedPulses(uint8_t pulseOutput)
  2746. {
  2747. uint32_t criticalState;
  2748. uint64_t observed;
  2749. if (pulseOutput > 3U)
  2750. {
  2751. return 0UL;
  2752. }
  2753. criticalState = PlsrPlatformEnterCritical();
  2754. if (PlsrCounterIndexByOutput[pulseOutput] < PLSR_COUNTER_COUNT)
  2755. {
  2756. observed = ((PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2757. && (PlsrAbFastGated[pulseOutput] != 0U))
  2758. ? PlsrCounterSnapshotStopped(pulseOutput)
  2759. : PlsrCounterSnapshot(pulseOutput);
  2760. }
  2761. else
  2762. {
  2763. observed = PlsrObservedPulseBase[pulseOutput];
  2764. }
  2765. PlsrPlatformExitCritical(criticalState);
  2766. return observed;
  2767. }
  2768. uint16_t PlsrPlatformDiagnosticFault(void)
  2769. {
  2770. uint16_t fault = PlsrPlatformFaultPending;
  2771. PlsrPlatformFaultPending = 0U;
  2772. return fault;
  2773. }
  2774. PLSR_PLATFORM_STOP_RESULT PlsrPlatformRequestStopLocked(
  2775. uint8_t pulseOutput,
  2776. uint8_t requireZeroBoundary)
  2777. {
  2778. if (pulseOutput > 3U)
  2779. {
  2780. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  2781. }
  2782. if ((requireZeroBoundary != 0U)
  2783. && (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2784. && (PlsrTimerRunning[pulseOutput] != 0U)
  2785. && (PlsrAbFastGated[pulseOutput] == 0U))
  2786. {
  2787. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2788. uint8_t lagOutput = PlsrAbLagAxis[pulseOutput];
  2789. uint32_t activeFrequencyHz;
  2790. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2791. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2792. uint8_t structureRunnable =
  2793. ((PlsrAbStructureVerified[pulseOutput] != 0U)
  2794. && (PlsrFrequencyVerifyPending[pulseOutput]
  2795. == PLSR_FREQUENCY_VERIFY_NONE))
  2796. ? PlsrAbStopBoundaryIsReachable(pulseOutput, baseTimer,
  2797. pairTimer)
  2798. : PlsrAbTimersAreRunnable(pulseOutput, baseTimer,
  2799. pairTimer);
  2800. if (structureRunnable == 0U)
  2801. {
  2802. PlsrCounterSuspend(pulseOutput);
  2803. PlsrAbHoldPairIdle(pulseOutput);
  2804. baseTimer->CR1 &= ~TIM_CR1_CEN;
  2805. pairTimer->CR1 &= ~TIM_CR1_CEN;
  2806. __DMB();
  2807. PlsrPlatformStopPulse(pulseOutput);
  2808. return PLSR_PLATFORM_STOP_FORCED_FAULT;
  2809. }
  2810. if (PlsrAbStopPending[pulseOutput] != 0U)
  2811. {
  2812. return PLSR_PLATFORM_STOP_PENDING;
  2813. }
  2814. PlsrAbFrequencyPending[pulseOutput] = 0U;
  2815. baseTimer->DIER &= ~TIM_DIER_UIE;
  2816. pairTimer->DIER &= ~TIM_DIER_UIE;
  2817. PlsrFrequencyVerifyPending[pulseOutput] =
  2818. PLSR_FREQUENCY_VERIFY_NONE;
  2819. PlsrFrequencyVerifyPulseCount[pulseOutput] = 0U;
  2820. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  2821. PlsrAbVerifyOwner[pairOutput] = PLSR_COUNTER_NONE;
  2822. activeFrequencyHz = PlsrTimerActiveFrequencyHz[pulseOutput];
  2823. PlsrTimerQueuedFrequencyHz[pulseOutput] = activeFrequencyHz;
  2824. PlsrTimerQueueGeneration[pulseOutput]++;
  2825. PlsrAbStopPending[pulseOutput] = 1U;
  2826. #if PLSR_DEBUG_TIMING
  2827. if (PlsrFinalArmQueueTimingPending[pulseOutput] != 0U)
  2828. {
  2829. uint32_t queuedAt = PlsrFinalArmQueuedAt[pulseOutput];
  2830. uint32_t stoppedAt = DWT->CYCCNT;
  2831. uint32_t latency = stoppedAt - queuedAt;
  2832. PlsrFinalArmQueueTimingPending[pulseOutput] = 0U;
  2833. PlsrFinalArmQueueToStopLastCycles[pulseOutput] = latency;
  2834. if (latency > PlsrFinalArmQueueToStopMaxCycles[pulseOutput])
  2835. {
  2836. PlsrFinalArmQueueToStopMaxCycles[pulseOutput] = latency;
  2837. }
  2838. }
  2839. #endif
  2840. if (lagOutput == pulseOutput)
  2841. {
  2842. pairTimer->SR = ~TIM_SR_CC1IF;
  2843. }
  2844. else
  2845. {
  2846. baseTimer->SR = ~TIM_SR_CC1IF;
  2847. }
  2848. baseTimer->DIER |= TIM_DIER_CC1IE;
  2849. pairTimer->DIER |= TIM_DIER_CC1IE;
  2850. __DMB();
  2851. return PLSR_PLATFORM_STOP_PENDING;
  2852. }
  2853. PlsrPlatformStopPulse(pulseOutput);
  2854. return PLSR_PLATFORM_STOP_COMPLETE;
  2855. }
  2856. uint8_t PlsrPlatformQueueFinalArmFromIrq(uint8_t pulseOutput)
  2857. {
  2858. uint8_t jobOutput;
  2859. if ((pulseOutput > 2U) || ((pulseOutput & 1U) != 0U)
  2860. || (PlsrTimerOutputMode[pulseOutput] != PLSR_OUTPUT_AB)
  2861. || (PlsrTimerRunning[pulseOutput] == 0U)
  2862. || (PlsrAbFastGated[pulseOutput] != 0U))
  2863. {
  2864. return 0U;
  2865. }
  2866. jobOutput = PlsrFinalArmJobOutput(pulseOutput);
  2867. if (PlsrAbFinalArmJobOwner[jobOutput] != PLSR_COUNTER_NONE)
  2868. {
  2869. return 0U;
  2870. }
  2871. PlsrAbFinalArmJobOwner[jobOutput] = pulseOutput;
  2872. #if PLSR_DEBUG_TIMING
  2873. PlsrFinalArmQueuedAt[pulseOutput] = DWT->CYCCNT;
  2874. PlsrFinalArmQueueTimingPending[pulseOutput] = 1U;
  2875. PlsrFinalArmQueueCount[pulseOutput]++;
  2876. #endif
  2877. __DMB();
  2878. NVIC_SetPendingIRQ(PlsrTimerMap[jobOutput].irq);
  2879. return 1U;
  2880. }
  2881. void PlsrPlatformStopPulse(uint8_t pulseOutput)
  2882. {
  2883. if (pulseOutput <= 3U)
  2884. {
  2885. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2886. {
  2887. uint8_t pairOutput = (uint8_t)(pulseOutput + 1U);
  2888. uint8_t jobOutput = PlsrFinalArmJobOutput(pulseOutput);
  2889. TIM_TypeDef *baseTimer = PlsrTimerMap[pulseOutput].timer;
  2890. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  2891. PlsrAbStructureVerified[pulseOutput] = 0U;
  2892. PlsrAbFinalArmJobOwner[jobOutput] = PLSR_COUNTER_NONE;
  2893. #if PLSR_DEBUG_TIMING
  2894. PlsrFinalArmQueueTimingPending[pulseOutput] = 0U;
  2895. #endif
  2896. NVIC_ClearPendingIRQ(PlsrTimerMap[jobOutput].irq);
  2897. if (PlsrAbFastGated[pulseOutput] != 0U)
  2898. {
  2899. PlsrAbHoldPairIdle(pulseOutput);
  2900. baseTimer->DIER = 0UL;
  2901. pairTimer->DIER = 0UL;
  2902. baseTimer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2903. pairTimer->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2904. baseTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2905. pairTimer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  2906. }
  2907. else
  2908. {
  2909. PlsrCounterSuspend(pulseOutput);
  2910. PlsrAbHoldPairIdle(pulseOutput);
  2911. PlsrTimerStop(baseTimer);
  2912. PlsrTimerStop(pairTimer);
  2913. }
  2914. }
  2915. else
  2916. {
  2917. PlsrPulsePinCaptureIdle(pulseOutput);
  2918. PlsrTimerStop(PlsrTimerMap[pulseOutput].timer);
  2919. }
  2920. PlsrCounterStop(pulseOutput);
  2921. PlsrTimerActiveFrequencyHz[pulseOutput] = 0UL;
  2922. PlsrTimerQueuedFrequencyHz[pulseOutput] = 0UL;
  2923. PlsrTimerQueueGeneration[pulseOutput]++;
  2924. PlsrTimerRunning[pulseOutput] = 0U;
  2925. PlsrFrequencyVerifyPending[pulseOutput] = 0U;
  2926. PlsrDeferredPulsePending[pulseOutput] = 0U;
  2927. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  2928. if (PlsrTimerOutputMode[pulseOutput] == PLSR_OUTPUT_AB)
  2929. {
  2930. PlsrAbVerifyOwner[pulseOutput + 1U] = PLSR_COUNTER_NONE;
  2931. }
  2932. PlsrAbFrequencyPending[pulseOutput] = 0U;
  2933. PlsrAbStopPending[pulseOutput] = 0U;
  2934. PlsrAbFastGated[pulseOutput] = 0U;
  2935. }
  2936. }
  2937. void PlsrPlatformForceSafeOutputsFromFault(void)
  2938. {
  2939. uint32_t mode;
  2940. const uint32_t outputPins = GPIO_PIN_6 | GPIO_PIN_7
  2941. | GPIO_PIN_8 | GPIO_PIN_9;
  2942. const uint32_t outputModeMask = (3UL << (6U * 2U))
  2943. | (3UL << (7U * 2U))
  2944. | (3UL << (8U * 2U))
  2945. | (3UL << (9U * 2U));
  2946. __disable_irq();
  2947. TIM10->DIER = 0UL;
  2948. TIM10->CR1 &= ~TIM_CR1_CEN;
  2949. TIM10->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2950. TIM10->SR = 0UL;
  2951. TIM11->DIER = 0UL;
  2952. TIM11->CR1 &= ~TIM_CR1_CEN;
  2953. TIM11->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2954. TIM11->SR = 0UL;
  2955. TIM13->DIER = 0UL;
  2956. TIM13->CR1 &= ~TIM_CR1_CEN;
  2957. TIM13->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2958. TIM13->SR = 0UL;
  2959. TIM14->DIER = 0UL;
  2960. TIM14->CR1 &= ~TIM_CR1_CEN;
  2961. TIM14->CCER &= ~(TIM_CCER_CC1E | TIM_CCER_CC1P);
  2962. TIM14->SR = 0UL;
  2963. TIM9->DIER = 0UL;
  2964. TIM9->CR1 &= ~TIM_CR1_CEN;
  2965. TIM9->SMCR = 0UL;
  2966. TIM9->SR = 0UL;
  2967. TIM12->DIER = 0UL;
  2968. TIM12->CR1 &= ~TIM_CR1_CEN;
  2969. TIM12->SMCR = 0UL;
  2970. TIM12->SR = 0UL;
  2971. RCC->AHB1ENR |= RCC_AHB1ENR_GPIOFEN | RCC_AHB1ENR_GPIOHEN;
  2972. __DSB();
  2973. GPIOF->BSRR = outputPins;
  2974. GPIOH->BSRR = outputPins;
  2975. GPIOF->OTYPER &= ~outputPins;
  2976. GPIOH->OTYPER &= ~outputPins;
  2977. GPIOF->PUPDR &= ~outputModeMask;
  2978. GPIOH->PUPDR &= ~outputModeMask;
  2979. mode = GPIOF->MODER;
  2980. mode &= ~outputModeMask;
  2981. mode |= (1UL << (6U * 2U)) | (1UL << (7U * 2U))
  2982. | (1UL << (8U * 2U)) | (1UL << (9U * 2U));
  2983. GPIOF->MODER = mode;
  2984. mode = GPIOH->MODER;
  2985. mode &= ~outputModeMask;
  2986. mode |= (1UL << (6U * 2U)) | (1UL << (7U * 2U))
  2987. | (1UL << (8U * 2U)) | (1UL << (9U * 2U));
  2988. GPIOH->MODER = mode;
  2989. __DSB();
  2990. }
  2991. uint8_t PlsrPlatformReadInput(uint8_t inputSelection)
  2992. {
  2993. if (inputSelection == 0U)
  2994. {
  2995. return (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET) ? 1U : 0U;
  2996. }
  2997. if (inputSelection == 1U)
  2998. {
  2999. return (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_12) == GPIO_PIN_SET) ? 1U : 0U;
  3000. }
  3001. return 0U;
  3002. }
  3003. uint8_t PlsrPlatformLoad(PLSR_PERSIST_PAYLOAD *payload)
  3004. {
  3005. PLSR_FLASH_SECTOR_SCAN scanA;
  3006. PLSR_FLASH_SECTOR_SCAN scanB;
  3007. const PLSR_FLASH_HEADER *selected = NULL;
  3008. const void *backupConfig = (const void *)PLSR_BACKUP_CONFIG_ADDRESS;
  3009. const PLSR_BACKUP_POSITION_RECORD *backupPosition;
  3010. uint8_t selectedVersion = 0U;
  3011. uint8_t backupVersion;
  3012. uint8_t haveConfig = 0U;
  3013. uint32_t selectedSectorAddress;
  3014. if (payload == NULL)
  3015. {
  3016. return 0U;
  3017. }
  3018. selected = PlsrFlashInitializeJournal(&scanA, &scanB, &selectedVersion,
  3019. &selectedSectorAddress);
  3020. if (selected != NULL)
  3021. {
  3022. if (selectedVersion == PLSR_FLASH_VERSION)
  3023. {
  3024. *payload = ((const PLSR_FLASH_RECORD *)selected)->payload;
  3025. }
  3026. else
  3027. {
  3028. PlsrLoadV2Payload(
  3029. payload,
  3030. &((const PLSR_FLASH_RECORD_V2 *)selected)->payload);
  3031. }
  3032. haveConfig = 1U;
  3033. }
  3034. else
  3035. {
  3036. (void)memset(payload, 0, sizeof(*payload));
  3037. }
  3038. backupVersion = PlsrBackupConfigVersion(backupConfig);
  3039. if (backupVersion == PLSR_FLASH_VERSION)
  3040. {
  3041. payload->config =
  3042. ((const PLSR_BACKUP_CONFIG_RECORD *)backupConfig)->config;
  3043. haveConfig = 1U;
  3044. }
  3045. else if (backupVersion == PLSR_FLASH_VERSION_V2)
  3046. {
  3047. const PLSR_BACKUP_CONFIG_RECORD_V2 *oldConfig =
  3048. (const PLSR_BACKUP_CONFIG_RECORD_V2 *)backupConfig;
  3049. (void)memcpy(&payload->config, oldConfig->config,
  3050. sizeof(oldConfig->config));
  3051. payload->config.outputMode = PLSR_OUTPUT_PULSE_DIR;
  3052. haveConfig = 1U;
  3053. }
  3054. backupPosition = PlsrNewestBackupPosition();
  3055. if (backupPosition != NULL)
  3056. {
  3057. payload->position = backupPosition->position;
  3058. payload->positionValid = backupPosition->positionValid;
  3059. payload->wasBusy = backupPosition->wasBusy;
  3060. }
  3061. if (selected != NULL)
  3062. {
  3063. uint8_t reserveIndex =
  3064. (selectedSectorAddress == PLSR_FLASH_SLOT_A_ADDRESS) ? 1U : 0U;
  3065. const PLSR_FLASH_SECTOR_SCAN *reserveScan =
  3066. (reserveIndex == 0U) ? &scanA : &scanB;
  3067. if (reserveScan->hasProgrammedSlot != 0U)
  3068. {
  3069. PlsrFlashReserveEraseState = (uint8_t)(reserveIndex + 1U);
  3070. }
  3071. }
  3072. else if (PlsrFlashNeedsStartupRecovery(
  3073. 0U,
  3074. scanA.hasProgrammedSlot,
  3075. scanA.firstErasedAddress,
  3076. scanB.hasProgrammedSlot,
  3077. scanB.firstErasedAddress) != 0U)
  3078. {
  3079. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_SECTOR_A;
  3080. }
  3081. return haveConfig;
  3082. }
  3083. PLSR_PLATFORM_SERVICE_RESULT PlsrPlatformServicePersistence(void)
  3084. {
  3085. uint32_t criticalState;
  3086. uint8_t eraseState;
  3087. uint8_t sectorIndex;
  3088. uint8_t index;
  3089. criticalState = PlsrPlatformEnterCritical();
  3090. eraseState = PlsrFlashReserveEraseState;
  3091. if (eraseState == PLSR_FLASH_ERASE_FAILED)
  3092. {
  3093. PlsrPlatformExitCritical(criticalState);
  3094. return PLSR_PLATFORM_SERVICE_FAILED;
  3095. }
  3096. if (eraseState == PLSR_FLASH_ERASE_NONE)
  3097. {
  3098. PlsrPlatformExitCritical(criticalState);
  3099. return PLSR_PLATFORM_SERVICE_READY;
  3100. }
  3101. if ((eraseState < PLSR_FLASH_ERASE_SECTOR_A)
  3102. || (eraseState > PLSR_FLASH_ERASE_SECTOR_B))
  3103. {
  3104. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_FAILED;
  3105. PlsrPlatformExitCritical(criticalState);
  3106. return PLSR_PLATFORM_SERVICE_FAILED;
  3107. }
  3108. for (index = 0U; index < 4U; index++)
  3109. {
  3110. if ((PlsrTimerRunning[index] != 0U)
  3111. || (PlsrTimerIrqActive[index] != 0U))
  3112. {
  3113. PlsrPlatformExitCritical(criticalState);
  3114. return PLSR_PLATFORM_SERVICE_DEFERRED;
  3115. }
  3116. }
  3117. sectorIndex = (uint8_t)(eraseState - PLSR_FLASH_ERASE_SECTOR_A);
  3118. PlsrPlatformExitCritical(criticalState);
  3119. if (PlsrFlashEraseReserve(sectorIndex) == 0U)
  3120. {
  3121. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_FAILED;
  3122. return PLSR_PLATFORM_SERVICE_FAILED;
  3123. }
  3124. PlsrFlashNextErasedAddress[sectorIndex] =
  3125. (sectorIndex == 0U) ? PLSR_FLASH_SLOT_A_ADDRESS
  3126. : PLSR_FLASH_SLOT_B_ADDRESS;
  3127. PlsrFlashReserveEraseState = PLSR_FLASH_ERASE_NONE;
  3128. return PLSR_PLATFORM_SERVICE_READY;
  3129. }
  3130. uint8_t PlsrPlatformSave(const PLSR_PERSIST_PAYLOAD *payload)
  3131. {
  3132. PLSR_FLASH_SECTOR_SCAN scanA;
  3133. PLSR_FLASH_SECTOR_SCAN scanB;
  3134. uint8_t newestVersion;
  3135. uint32_t newestSectorAddress;
  3136. uint8_t targetIndex;
  3137. uint32_t targetAddress;
  3138. uint32_t index;
  3139. uint32_t wordCount;
  3140. const uint32_t *words;
  3141. HAL_StatusTypeDef status = HAL_OK;
  3142. if (payload == NULL)
  3143. {
  3144. return 0U;
  3145. }
  3146. if (PlsrFlashJournalInitialized == 0U)
  3147. {
  3148. (void)PlsrFlashInitializeJournal(&scanA, &scanB, &newestVersion,
  3149. &newestSectorAddress);
  3150. (void)newestVersion;
  3151. (void)newestSectorAddress;
  3152. }
  3153. if (PlsrFlashNewestAddress != 0UL)
  3154. {
  3155. targetIndex = PlsrFlashSectorIndex(PlsrFlashNewestAddress);
  3156. if (PlsrFlashNextErasedAddress[targetIndex] == 0UL)
  3157. {
  3158. targetIndex ^= 1U;
  3159. }
  3160. }
  3161. else if (PlsrFlashNextErasedAddress[0] != 0UL)
  3162. {
  3163. targetIndex = 0U;
  3164. }
  3165. else
  3166. {
  3167. targetIndex = 1U;
  3168. }
  3169. targetAddress = PlsrFlashNextErasedAddress[targetIndex];
  3170. if ((targetAddress != 0UL)
  3171. && ((PlsrFlashAddressIsJournalSlot(targetIndex, targetAddress) == 0U)
  3172. || (PlsrFlashSlotIsErased(targetAddress) == 0U)))
  3173. {
  3174. targetAddress = 0UL;
  3175. PlsrFlashNextErasedAddress[targetIndex] = 0UL;
  3176. }
  3177. if (targetAddress == 0UL)
  3178. {
  3179. uint8_t newestIndex = (PlsrFlashNewestAddress != 0UL)
  3180. ? PlsrFlashSectorIndex(
  3181. PlsrFlashNewestAddress)
  3182. : 0xFFU;
  3183. uint8_t alternateIndex = targetIndex ^ 1U;
  3184. if ((targetIndex == newestIndex)
  3185. || (PlsrFlashNextErasedAddress[alternateIndex] != 0UL))
  3186. {
  3187. targetIndex = alternateIndex;
  3188. targetAddress = PlsrFlashNextErasedAddress[targetIndex];
  3189. if ((targetAddress != 0UL)
  3190. && ((PlsrFlashAddressIsJournalSlot(targetIndex,
  3191. targetAddress) == 0U)
  3192. || (PlsrFlashSlotIsErased(targetAddress) == 0U)))
  3193. {
  3194. targetAddress = 0UL;
  3195. PlsrFlashNextErasedAddress[targetIndex] = 0UL;
  3196. }
  3197. }
  3198. if (targetAddress == 0UL)
  3199. {
  3200. return 0U;
  3201. }
  3202. }
  3203. PlsrFlashNextErasedAddress[targetIndex] =
  3204. PlsrFlashFindErasedAfter(targetIndex, targetAddress);
  3205. (void)memset(&PlsrFlashRecordBuffer, 0, sizeof(PlsrFlashRecordBuffer));
  3206. PlsrFlashRecordBuffer.magic = PLSR_FLASH_MAGIC;
  3207. PlsrFlashRecordBuffer.version = PLSR_FLASH_VERSION;
  3208. PlsrFlashRecordBuffer.payloadSize = sizeof(PLSR_PERSIST_PAYLOAD);
  3209. PlsrFlashRecordBuffer.generation = PlsrFlashNewestGeneration + 1UL;
  3210. PlsrFlashRecordBuffer.payload = *payload;
  3211. PlsrFlashRecordBuffer.crc32 =
  3212. PlsrFlashRecordCrc(&PlsrFlashRecordBuffer,
  3213. sizeof(PlsrFlashRecordBuffer.payload));
  3214. if (HAL_FLASH_Unlock() != HAL_OK)
  3215. {
  3216. (void)HAL_FLASH_Lock();
  3217. return 0U;
  3218. }
  3219. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
  3220. | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  3221. | FLASH_FLAG_PGSERR);
  3222. words = (const uint32_t *)&PlsrFlashRecordBuffer;
  3223. wordCount = sizeof(PlsrFlashRecordBuffer) / sizeof(uint32_t);
  3224. if (status == HAL_OK)
  3225. {
  3226. for (index = 1UL; index < wordCount; index++)
  3227. {
  3228. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
  3229. targetAddress + index * 4UL,
  3230. words[index]) != HAL_OK)
  3231. {
  3232. status = HAL_ERROR;
  3233. break;
  3234. }
  3235. }
  3236. }
  3237. if ((status == HAL_OK)
  3238. && (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, targetAddress,
  3239. PLSR_FLASH_MAGIC) != HAL_OK))
  3240. {
  3241. status = HAL_ERROR;
  3242. }
  3243. if (HAL_FLASH_Lock() != HAL_OK)
  3244. {
  3245. (void)HAL_FLASH_Lock();
  3246. status = HAL_ERROR;
  3247. }
  3248. if (PlsrFlashRecordVersion((const void *)targetAddress)
  3249. == PLSR_FLASH_VERSION)
  3250. {
  3251. PlsrFlashNewestAddress = targetAddress;
  3252. PlsrFlashNewestGeneration = PlsrFlashRecordBuffer.generation;
  3253. return (status == HAL_OK) ? 1U : 0U;
  3254. }
  3255. return 0U;
  3256. }
  3257. void PlsrPlatformCheckpointConfig(const PLSR_CONFIG *config)
  3258. {
  3259. PLSR_BACKUP_CONFIG_RECORD *record =
  3260. (PLSR_BACKUP_CONFIG_RECORD *)PLSR_BACKUP_CONFIG_ADDRESS;
  3261. if (config == NULL)
  3262. {
  3263. return;
  3264. }
  3265. record->magic = 0UL;
  3266. record->config = *config;
  3267. record->crc32 = PlsrCrc32(&record->config, sizeof(record->config));
  3268. __DMB();
  3269. record->magic = PLSR_BACKUP_CONFIG_MAGIC;
  3270. __DMB();
  3271. }
  3272. void PlsrPlatformCheckpointPosition(int32_t position,
  3273. uint8_t positionValid,
  3274. uint8_t wasBusy)
  3275. {
  3276. PLSR_BACKUP_POSITION_RECORD *slots =
  3277. (PLSR_BACKUP_POSITION_RECORD *)PLSR_BACKUP_POSITION_ADDRESS;
  3278. PLSR_BACKUP_POSITION_RECORD *record;
  3279. PlsrBackupPositionGeneration++;
  3280. record = &slots[PlsrBackupPositionGeneration & 1UL];
  3281. record->magic = 0UL;
  3282. record->generation = PlsrBackupPositionGeneration;
  3283. record->position = position;
  3284. record->positionValid = (positionValid != 0U) ? 1U : 0U;
  3285. record->wasBusy = (wasBusy != 0U) ? 1U : 0U;
  3286. record->reserved = 0U;
  3287. record->crc32 = PlsrCrc32(&record->generation,
  3288. sizeof(record->generation)
  3289. + sizeof(record->position)
  3290. + sizeof(record->positionValid)
  3291. + sizeof(record->wasBusy)
  3292. + sizeof(record->reserved));
  3293. __DMB();
  3294. record->magic = PLSR_BACKUP_POSITION_MAGIC;
  3295. __DMB();
  3296. }
  3297. uint32_t PlsrPlatformEnterCritical(void)
  3298. {
  3299. uint32_t state = __get_PRIMASK();
  3300. __disable_irq();
  3301. __DMB();
  3302. return state;
  3303. }
  3304. void PlsrPlatformExitCritical(uint32_t state)
  3305. {
  3306. __DMB();
  3307. if (state == 0UL)
  3308. {
  3309. __enable_irq();
  3310. }
  3311. }
  3312. static void PlsrHandleTimerIrq(uint8_t pulseOutput)
  3313. {
  3314. TIM_TypeDef *timer;
  3315. #if PLSR_DEBUG_TIMING
  3316. uint32_t startedAt;
  3317. uint32_t elapsedCycles;
  3318. uint8_t timingOutput = pulseOutput;
  3319. startedAt = DWT->CYCCNT;
  3320. #endif
  3321. timer = PlsrTimerMap[pulseOutput].timer;
  3322. if (PlsrTimerIrqActive[pulseOutput] != 0U)
  3323. {
  3324. #if PLSR_DEBUG_TIMING
  3325. goto irq_record;
  3326. #else
  3327. return;
  3328. #endif
  3329. }
  3330. PlsrTimerIrqActive[pulseOutput] = 1U;
  3331. if (((timer->SR & TIM_SR_CC1IF) != 0UL)
  3332. && ((timer->DIER & TIM_DIER_CC1IE) != 0UL))
  3333. {
  3334. uint8_t owner = ((pulseOutput & 2U) == 0U) ? 0U : 2U;
  3335. #if PLSR_DEBUG_TIMING
  3336. timingOutput = owner;
  3337. #endif
  3338. timer->SR = ~TIM_SR_CC1IF;
  3339. if ((owner <= 2U)
  3340. && (PlsrTimerRunning[owner] != 0U)
  3341. && (PlsrTimerOutputMode[owner] == PLSR_OUTPUT_AB))
  3342. {
  3343. uint8_t pairOutput = (uint8_t)(owner + 1U);
  3344. TIM_TypeDef *baseTimer = PlsrTimerMap[owner].timer;
  3345. TIM_TypeDef *pairTimer = PlsrTimerMap[pairOutput].timer;
  3346. PLSR_AB_SETTING pending;
  3347. if (PlsrAbStopPending[owner] != 0U)
  3348. {
  3349. if (PlsrAbCanFastGateAtZero(owner) == 0U)
  3350. {
  3351. goto irq_done;
  3352. }
  3353. baseTimer->DIER &= ~(TIM_DIER_CC1IE | TIM_DIER_UIE);
  3354. pairTimer->DIER &= ~(TIM_DIER_CC1IE | TIM_DIER_UIE);
  3355. baseTimer->SR = ~TIM_SR_CC1IF;
  3356. pairTimer->SR = ~TIM_SR_CC1IF;
  3357. PlsrFrequencyVerifyPending[owner] =
  3358. PLSR_FREQUENCY_VERIFY_NONE;
  3359. PlsrAbVerifyOwner[owner] = PLSR_COUNTER_NONE;
  3360. PlsrAbVerifyOwner[pairOutput] = PLSR_COUNTER_NONE;
  3361. PlsrAbFastGate(owner);
  3362. PlsrAbFastGated[owner] = 1U;
  3363. PlsrDeferredPulsePending[owner] = 0U;
  3364. PlsrPulseTimerIrq(owner);
  3365. goto irq_done;
  3366. }
  3367. if (pulseOutput != PlsrAbLagAxis[owner])
  3368. {
  3369. if (PlsrDeferredPulsePending[owner] != 0U)
  3370. {
  3371. PlsrDeferredPulsePending[owner] = 0U;
  3372. timer->DIER &= ~TIM_DIER_CC1IE;
  3373. PlsrPulseTimerIrq(owner);
  3374. }
  3375. goto irq_done;
  3376. }
  3377. if (PlsrDeferredPulsePending[owner] != 0U)
  3378. {
  3379. goto irq_done;
  3380. }
  3381. if (PlsrAbFrequencyPending[owner] != 0U)
  3382. {
  3383. pending = PlsrAbPendingSetting[owner];
  3384. PlsrAbFrequencyPending[owner] = 0U;
  3385. PlsrAbLoadAndStart(owner, &pending);
  3386. PlsrAbActiveSetting[owner] = pending;
  3387. PlsrTimerActiveSetting[owner] =
  3388. PlsrTimerQueuedSetting[owner];
  3389. PlsrTimerActiveFrequencyHz[owner] =
  3390. pending.actualFrequencyHz;
  3391. PlsrDeferredPulsePending[owner] = 1U;
  3392. PlsrAbScheduleFrequencyVerify(owner);
  3393. goto irq_done;
  3394. }
  3395. PlsrPulseTimerIrq(owner);
  3396. }
  3397. goto irq_done;
  3398. }
  3399. if (((timer->SR & TIM_SR_UIF) != 0UL)
  3400. && ((timer->DIER & TIM_DIER_UIE) != 0UL))
  3401. {
  3402. timer->SR = ~(TIM_SR_UIF | TIM_SR_CC1IF);
  3403. if (PlsrPlatformSettingsDiffer(
  3404. &PlsrTimerActiveSetting[pulseOutput],
  3405. &PlsrTimerQueuedSetting[pulseOutput]) != 0U)
  3406. {
  3407. PlsrFrequencyVerifyPending[pulseOutput] = 1U;
  3408. PlsrTimerActiveFrequencyHz[pulseOutput] =
  3409. PlsrTimerQueuedFrequencyHz[pulseOutput];
  3410. PlsrTimerActiveSetting[pulseOutput] =
  3411. PlsrTimerQueuedSetting[pulseOutput];
  3412. }
  3413. PlsrPulseTimerIrq(pulseOutput);
  3414. }
  3415. irq_done:
  3416. PlsrTimerIrqActive[pulseOutput] = 0U;
  3417. #if PLSR_DEBUG_TIMING
  3418. irq_record:
  3419. elapsedCycles = DWT->CYCCNT - startedAt;
  3420. PlsrIrqCount[timingOutput]++;
  3421. PlsrIrqLastCycles[timingOutput] = elapsedCycles;
  3422. if (elapsedCycles > PlsrIrqMaxCycles[timingOutput])
  3423. {
  3424. PlsrIrqMaxCycles[timingOutput] = elapsedCycles;
  3425. }
  3426. #endif
  3427. }
  3428. static uint8_t PlsrHandleScheduledAbVerify(uint8_t pulseOutput)
  3429. {
  3430. uint8_t verifyOwner = PlsrAbVerifyOwner[pulseOutput];
  3431. TIM_TypeDef *timer;
  3432. #if PLSR_DEBUG_TIMING
  3433. uint32_t startedAt;
  3434. uint32_t elapsedCycles;
  3435. #endif
  3436. if (verifyOwner > 2U)
  3437. {
  3438. return 0U;
  3439. }
  3440. timer = PlsrTimerMap[pulseOutput].timer;
  3441. if (((timer->SR & TIM_SR_UIF) == 0UL)
  3442. || ((timer->DIER & TIM_DIER_UIE) == 0UL))
  3443. {
  3444. return 0U;
  3445. }
  3446. #if PLSR_DEBUG_TIMING
  3447. startedAt = DWT->CYCCNT;
  3448. #endif
  3449. timer->SR = ~TIM_SR_UIF;
  3450. timer->DIER &= ~TIM_DIER_UIE;
  3451. PlsrAbVerifyOwner[pulseOutput] = PLSR_COUNTER_NONE;
  3452. if (PlsrFrequencyVerifyPending[verifyOwner]
  3453. == PLSR_FREQUENCY_VERIFY_AB_AUX_IRQ)
  3454. {
  3455. PlsrFrequencyVerifyPending[verifyOwner] =
  3456. PLSR_FREQUENCY_VERIFY_NONE;
  3457. PlsrFrequencyVerifyPulseCount[verifyOwner] = 0U;
  3458. (void)PlsrVerifyActiveFrequency(verifyOwner);
  3459. }
  3460. #if PLSR_DEBUG_TIMING
  3461. elapsedCycles = DWT->CYCCNT - startedAt;
  3462. PlsrIrqCount[verifyOwner]++;
  3463. PlsrIrqLastCycles[verifyOwner] = elapsedCycles;
  3464. if (elapsedCycles > PlsrIrqMaxCycles[verifyOwner])
  3465. {
  3466. PlsrIrqMaxCycles[verifyOwner] = elapsedCycles;
  3467. }
  3468. #endif
  3469. return 1U;
  3470. }
  3471. static uint8_t PlsrFinalStopIrqIsPending(uint8_t pulseOutput)
  3472. {
  3473. uint8_t owner = ((pulseOutput & 2U) == 0U) ? 0U : 2U;
  3474. TIM_TypeDef *timer = PlsrTimerMap[pulseOutput].timer;
  3475. return ((PlsrAbStopPending[owner] != 0U)
  3476. && ((timer->SR & TIM_SR_CC1IF) != 0UL)
  3477. && ((timer->DIER & TIM_DIER_CC1IE) != 0UL)) ? 1U : 0U;
  3478. }
  3479. static uint8_t PlsrHandleFinalArmJob(uint8_t pulseOutput)
  3480. {
  3481. uint8_t owner = PlsrAbFinalArmJobOwner[pulseOutput];
  3482. #if PLSR_DEBUG_TIMING
  3483. uint32_t startedAt;
  3484. uint32_t elapsedCycles;
  3485. #endif
  3486. if (owner > 2U)
  3487. {
  3488. return 0U;
  3489. }
  3490. PlsrAbFinalArmJobOwner[pulseOutput] = PLSR_COUNTER_NONE;
  3491. #if PLSR_DEBUG_TIMING
  3492. startedAt = DWT->CYCCNT;
  3493. #endif
  3494. PlsrFinalArmJobIrq(owner);
  3495. #if PLSR_DEBUG_TIMING
  3496. elapsedCycles = DWT->CYCCNT - startedAt;
  3497. PlsrFinalArmJobLastCycles[owner] = elapsedCycles;
  3498. if (elapsedCycles > PlsrFinalArmJobMaxCycles[owner])
  3499. {
  3500. PlsrFinalArmJobMaxCycles[owner] = elapsedCycles;
  3501. }
  3502. #endif
  3503. return 1U;
  3504. }
  3505. static void PlsrDispatchTimerIrq(uint8_t pulseOutput)
  3506. {
  3507. if (PlsrFinalStopIrqIsPending(pulseOutput) != 0U)
  3508. {
  3509. PlsrHandleTimerIrq(pulseOutput);
  3510. return;
  3511. }
  3512. if (PlsrHandleFinalArmJob(pulseOutput) != 0U)
  3513. {
  3514. return;
  3515. }
  3516. if (PlsrHandleScheduledAbVerify(pulseOutput) == 0U)
  3517. {
  3518. PlsrHandleTimerIrq(pulseOutput);
  3519. }
  3520. }
  3521. void TIM1_UP_TIM10_IRQHandler(void)
  3522. {
  3523. PlsrDispatchTimerIrq(0U);
  3524. }
  3525. void TIM8_UP_TIM13_IRQHandler(void)
  3526. {
  3527. PlsrDispatchTimerIrq(1U);
  3528. }
  3529. void TIM1_TRG_COM_TIM11_IRQHandler(void)
  3530. {
  3531. PlsrDispatchTimerIrq(2U);
  3532. }
  3533. void TIM8_TRG_COM_TIM14_IRQHandler(void)
  3534. {
  3535. PlsrDispatchTimerIrq(3U);
  3536. }
  3537. static void PlsrHandleCounterIrq(uint8_t counterIndex)
  3538. {
  3539. TIM_TypeDef *counter;
  3540. if (counterIndex >= PLSR_COUNTER_COUNT)
  3541. {
  3542. return;
  3543. }
  3544. counter = PlsrCounters[counterIndex];
  3545. if (((counter->SR & TIM_SR_UIF) != 0UL)
  3546. && ((counter->DIER & TIM_DIER_UIE) != 0UL))
  3547. {
  3548. counter->SR = ~TIM_SR_UIF;
  3549. PlsrCounterOverflowPulses[counterIndex] +=
  3550. PLSR_COUNTER_BLOCK_PULSES;
  3551. }
  3552. }
  3553. void TIM1_BRK_TIM9_IRQHandler(void)
  3554. {
  3555. PlsrHandleCounterIrq(0U);
  3556. }
  3557. void TIM8_BRK_TIM12_IRQHandler(void)
  3558. {
  3559. PlsrHandleCounterIrq(1U);
  3560. }
  3561. #endif /* PLSR_HOST_TEST */