25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

4946 lines
160 KiB

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