#include <cstdint>
#include "hardware/5400tp194.h"
#include "owi_test.h"

void owi_test() {
	//clocking on
	uint32_t clken = CMM_APB0CLKEN_OWI0;
	CMM->apb0clken |= clken;

	//enabling alternative functions GPIOA
	uint32_t altfen = GPIOA->altf0 | GPIOA_ALTF0_P27_OWI0_DATA | GPIOA_ALTF0_P28_OWI0_STP
				| GPIOA_ALTF0_P29_OWI1_DATA | GPIOA_ALTF0_P30_OWI1_STP;
	GPIOA->altf0 |= altfen;

	//setting OWI0
	OWI0->prcr = 0xC0;
	OWI0->ctrl = 0;
	OWI0->cfg = 1;

	while(!(OWI0->st & 0b1));

	OWI0->buf = 0x99;

	while((OWI0->st & 8) != 8);

	OWI0->buf = 0;
	while((OWI0->st & 8) != 8);

	OWI0->buf = 0x99;
	while((OWI0->st & 8) != 8);

	OWI0->buf = 0xFF;
	while((OWI0->st & 8) != 8);

	//clocking off
	CMM->apb0clken &= ~clken;

	//disabling alternative GPIOA functions
	GPIOA->altf0 &= ~altfen;
}
