Very generic suggestion. Read poetry, pull apart the rhyme schemes. Listen to rap and learn the rhythms. Understand "freestyle" is almost always a very practiced thing. Comedians dealing with hecklers aren't freestyling they are hitting them with practiced lines. Freestyling rappers are mostly using practiced lines, they just adapt them to the situation.
Learn to write vs learn to rap. I can't see why they couldn't be done together. Pick any object and just work on saying and writing good line about it as quick as you can. Then the next one till you dry up. Move on rinse repeat. Its like any kind of training, in this case brain to mouth,