Writing tone(unit16_t frequency) function without using Arduino library

Could you please help me writing a “tone(uint16_t frequency)” function with Atmega328. The tone function takes in 1 parameter which is the frequency of the tone to be generated. This function must control the 16-bit timer to produce a square wave that has 50% duty cycle and the frequency of the wave should match the parameter.

https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Tone.cpp