flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
ptp_port_stm32f407_etherlib.c
Go to the documentation of this file.
2
3#include <stdint.h>
4#include <stdbool.h>
5#include <stdlib.h>
6#include <math.h>
7
8#include <cmsis_os2.h>
9
10#include "flexptp_options.h"
11
12extern ETH_HandleTypeDef EthHandle;
13
14static unsigned sFreq = 1;
15
16#ifdef CLI_REG_CMD
17
18static int CB_pps(const CliToken_Type *ppArgs, uint8_t argc) {
19 if (argc >= 1) {
20 float freq = atof(ppArgs[0]);
21
22 int fc_exp;
23
24 if (freq > 0) {
25 fc_exp = round(log2f(freq)) + 1;
26
27 // lower limit
28 if (fc_exp < ETHHW_PTP_PPS_1Hz) {
29 fc_exp = ETHHW_PTP_PPS_1Hz;
30 }
31
32 // upper limit
33 if (fc_exp > ETHHW_PTP_PPS_16384Hz) {
34 fc_exp = ETHHW_PTP_PPS_16384Hz;
35 }
36
37 sFreq = exp2(fc_exp - 1);
38 } else {
39 sFreq = 0;
40 fc_exp = 0;
41 }
42
43 ETHHW_SetPTPPPSFreq(ETH, fc_exp);
44
45// // parse frequency [Hz] (integer only!)
46// uint32_t freq_Hz = atoi(ppArgs[0]);
47//
48// if (freq_Hz == 0) {
49// // stop pulse train generation
50// ETH_StopPTPPPSPulseTrain(&EthHandle);
51// } else {
52// // compute period [ns]
53// uint32_t period_ns = NANO_PREFIX / freq_Hz;
54//
55// // display warning if frequency is not integer divisor of 1E+09
56// if ((NANO_PREFIX % freq_Hz) != 0) {
57// MSG("Warning! PPS frequency is not totally accurate, "
58// "choose frequency values corresponding to periods "
59// "being integer divisors of 1E+09!\n");
60// }
61//
62// // set duty cycle (try 50%) by specifying positive pulse length
63// uint32_t high_ns = period_ns / 2;
64//
65// // start pulse train generation
66// ETH_StartPTPPPSPulseTrain(&EthHandle, high_ns, period_ns);
67// }
68//
69// // store frequency setting
70// sFreq = freq_Hz;
71 }
72
73 if (sFreq > 0) {
74 MSG("PPS frequency: %u Hz\n", sFreq);
75 } else {
76 MSG("PPS output is turned off.\n");
77 }
78
79 return 0;
80}
81
82static void ptphw_register_cli_commands() {
83 CLI_REG_CMD("ptp pps {freq} \t\t\tSet or query PPS signal frequency [Hz]", 2, 0, CB_pps);
84}
85
86#endif // CLI_REG_CMD
87
88void ptphw_init(uint32_t increment, uint32_t addend) {
89 //MSG("Turning PTP on!");
90
91 // enable PTP timestamping
92 ETHHW_EnablePTPTimeStamping(ETH);
93
94 osDelay(10);
95
96 //ETH_EnablePTPTimeStamping(&EthHandle);
97
98 // initialize PTP time
99 ETHHW_InitPTPTime(ETH, 0, 0);
100
101 // enable fine correction
102 ETHHW_EnablePTPFineCorr(ETH, true);
103
104 // set addend register value
105 ETHHW_SetPTPAddend(ETH, addend);
106
107 // set increment register value
108 ETHHW_SetPTPSubsecondIncrement(ETH, increment);
109
110 //ETH_StartPTPPPSPulseTrain(&EthHandle, 500E+06, 1E+09);
111 ETHHW_SetPTPPPSFreq(ETH, ETHHW_PTP_PPS_1Hz);
112 sFreq = 1;
113
114 __HAL_RCC_GPIOG_CLK_ENABLE();
115
116 // setup PPS-pin
117 GPIO_InitTypeDef GPIO_InitStructure;
118 GPIO_InitStructure.Pin = GPIO_PIN_8;
119 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
120 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
121 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
122 GPIO_InitStructure.Pull = GPIO_NOPULL;
123 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
124
125#ifdef CLI_REG_CMD
126 // register cli commands
127 ptphw_register_cli_commands();
128#endif // CLI_REG_CMD
129}
130
132 uint32_t sec;
133 uint32_t nsec;
134 ETHHW_GetPTPTime(ETH, &sec, &nsec);
135 pTime->sec = sec;
136 pTime->nanosec = nsec;
137}
#define CLI_REG_CMD(cmd_hintline, n_cmd, n_min_arg, cb)
ETH_HandleTypeDef EthHandle
static unsigned sFreq
void ptphw_gettime(TimestampU *pTime)
void ptphw_init(uint32_t increment, uint32_t addend)
Timestamp (unsigned)
Definition: timeutils.h:20
uint32_t nanosec
nanoseconds
Definition: timeutils.h:22
uint64_t sec
seconds
Definition: timeutils.h:21