Period of a quasiperiodic digital signal
You can locate each rising edge and use that as the start time of each new cycle. Then compute the time differences between their locations and then take reciprocal of the average (or median, for more robustness against outliers) of these time differences to estimate frequency.

Locating the rising edges can be achieved by checking if the difference in consecutive data points exceeds a predetermined threshold. This threshold can be set adaptively from data. For instance, if you make a scatter plot of all your data samples it should look bimodal: the higher values correspond to the peaks of the square wave and the lower values to the valleys.