summaryrefslogtreecommitdiff
path: root/openpicc/application/tc_cdiv_sync.c
blob: 695095d1f05c63e956391a5522663b644b452867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Synchronize TC_CDIV divided sample clock with the SOF of the packet */

#include <lib_AT91SAM7.h>
#include <AT91SAM7.h>
#include "tc_cdiv_sync.h"
#include "dbgu.h"
#include "pio_irq.h"
#include "openpicc.h"
#include "led.h"
#include "tc_cdiv.h"

#define USE_IRQ

static u_int8_t enabled;

static void pio_data_change(u_int32_t pio)
{
	(void)pio;
	DEBUGP("PIO_FRAME_IRQ: ");
	/* we get one interrupt for each change. If now, after the
	 * change the level is high, then it must have been a rising
	 * edge */
	if (*AT91C_PIOA_PDSR & OPENPICC_PIO_FRAME) {
		vLedSetGreen(1);
		/* This code is now replaced by hard-coded assembler code in os/boot/boot.s my_fiq_handler */
		/* *AT91C_TC0_CCR = AT91C_TC_SWTRG;*/
		DEBUGPCR("CDIV_SYNC_FLIP SWTRG CV=0x%08x",
			  *AT91C_TC0_CV);
		vLedSetGreen(0);
	} else
		DEBUGPCR("");
	//vLedSetGreen(0);
}

void tc_cdiv_sync_reset(void)
{
	if (enabled) {
		u_int32_t tmp = *AT91C_PIOA_ISR;
		(void)tmp;
		volatile int i;
		DEBUGPCRF("CDIV_SYNC_FLOP");
		//vLedSetGreen(1);

		/* reset the hardware flipflop, this clears FRAME */
		AT91F_PIO_ClearOutput(AT91C_BASE_PIOA,
				      OPENPICC_PIO_SSC_DATA_CONTROL);
		for (i = 0; i < 0xff; i++) ;
		AT91F_PIO_SetOutput(AT91C_BASE_PIOA,
				    OPENPICC_PIO_SSC_DATA_CONTROL);
		
		if(OPENPICC->features.clock_gating) {
			/* reset tc_cdiv counter, the cleared frame signal stopped the tc_cdiv clock */
			tc_cdiv_reset();
		}
	}
}

void tc_cdiv_sync_disable(void)
{
	enabled = 0;	
	*AT91C_PIOA_IDR = OPENPICC_PIO_FRAME;
}

void tc_cdiv_sync_enable(void)
{
	enabled = 1;

	DEBUGPCRF("CDIV_SYNC_ENABLE ");
	tc_cdiv_sync_reset();
	*AT91C_PIOA_IER = OPENPICC_PIO_FRAME;
}

void tc_cdiv_sync_init()
{
	pio_irq_init_once();
	DEBUGPCRF("initializing");

	enabled = 0;

	AT91F_PIOA_CfgPMC();
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, OPENPICC_PIO_SSC_DATA_CONTROL);
	
	/* This code is now replaced by hard-coded assembler code in os/boot/boot.s my_fiq_handler */
	/*pio_irq_register(OPENPICC_PIO_FRAME, &pio_data_change);*/
	(void)pio_data_change;
	
	//vLedSetGreen(0);
	tc_cdiv_sync_disable();
}
personal git repositories of Harald Welte. Your mileage may vary