; OC_ex_s12.asm		29 Mar 2012		Rev. 0
;
; Toggles red LED every 2 seconds.  Blue LED off 0.5s and on 1.5s,
; i.e., period = 2s and duty cycle = (1.5/2) % = 75%
; Uses ePWM1 system. Demo: Run emulator with no breakpoints. Then
; runbreakpoint at ISR and montior LEDs and ePWM1 TBCTR.
; ******************************
	.global		_c_int00
; ****** PROGRAM CONSTANTS ******

GPAMUX1  	.set		0x6f86
GPAMUX2 	.set		0x6f88
GPADIR		.set		0x6f8a
GPADAT		.set		0x6fc0
GPATOGGLE	.set		0x6FC6
WDCR		.set		0x7029
PCLKCR0		.set		0x701C
PCLKCR1		.set		0x701D
PIECTRL		.set		0x0CE0
PIEACK		.set		0x0CE1
PIEIER3		.set		0x0CE6

; **** ePWM1 registers ****
TBCTL1		.set		0x6800
TBSTS1		.set		0x6801
TBCTR1		.set		0x6804
TBPRD1		.set		0x6805

CMPCTL1		.set		0x6807
CMPAHR1		.set		0x6808
CMPA1		.set		0x6809
CMPB1		.set		0x680A

AQCTLA1		.set		0x680B
AQCTLB1		.set		0x680C
AQSFRC1		.set		0x680D
AQCSFRC1	.set		0x680E

ETSEL1		.set		0x6819
ETPS1		.set		0x681A
ETFLG1		.set		0x681B
ETCLR1		.set		0x681C
ETFRC1		.set		0x681D

; ****** PROGRAM SECTION ******
	.text

_c_int00:
	EALLOW
			
	mov		AR0,#WDCR
	mov		*AR0,#0x68
			
	mov		AR0,#GPADIR
	or		*AR0,#0x8000		;set gpio15 as an output
			
	lc		init_output_compare
			
loop
	b		loop,unc

; **** initialization ****
init_output_compare:	
	mov		AR0,#PCLKCR1
	or		*AR0,#0x1			; ePWM1 clock enable set

	mov		AR0,#PCLKCR0
	or		*AR0,#0x4			; set TBCLKSYNC to turn on TBCLK
	
	mov		AR0,#GPAMUX1
	mov		*AR0,#0x1			; enable ePWM1A pin at GPIO0, set GP7-1 to GPIO
			
	mov		AR0,#TBCTL1			; time base control register
	mov		*AR0,#0x9D38		; Free run mode, CLKDIV = 128*4, Disable sync, no shadow reg,
			 
	mov		AR0,#TBPRD1			; time base period register
	mov		*AR0,#29296			; ePWM clock = (2s)*7.5*10^6 Hz /(128 * 4) = 29296
			
	mov		AR0,#AQCTLA1		;Action-Qualifier Output A Control Register
	mov		*AR0,#0x0024		;When the counter equals CMPA, force pwm pin high
								;when the counter equals period, force pwm pin low
			
	mov 	AR0,#ETSEL1			;event trigger selection register
	mov		*AR0,#0x000A		;when the timer equals the period, trigger an event.
						
	mov		AR0,#ETPS1			;Event-Trigger Prescale Register
	mov		*AR0,#0x1			;trigger an interrupt after one event occurs
											
	mov		AR0,#CMPCTL1		; Counter-Compare Control Register
	mov		*AR0,#0x5F			;immediate mode, do not use shadow registers
			
	mov		AR0,#CMPA1			;Compare A register
	mov		*AR0,#7324			; 29296/4= 7324 = 0.5sec

	mov		AR0,#PIECTRL
	or		*AR0,#0x1			; enable the pie
						
	or		IER,#0x4			; enable interrupt group 3

	mov		AR0,#PIEIER3
	or		*AR0,#0x1			; enable interrupt 3.1 (EPWM1_INT)
			
	mov		AR0,#0x0D60			; 0xD60 is the ePWM1 interrupt vector location
	mov		*XAR0++,#OCA_ISR	; set ePWM1 interrupt vector
	mov		*XAR0,#OCA_ISR>>16
			
	mov		AR0,#PIEACK
	or		*AR0,#0x4			; acknowledge interrupt 3
			
	mov		AR0,#ETCLR1			; Event-Trigger Clear Register
	or		*AR0,#0x1			; clear epwm1 interrupt flags
			
	clrc	intm
			
	lret
; *************************
OCA_ISR:
	mov		AR0,#GPATOGGLE
	mov		*AR0,#0x8000

	mov		AR0,#ETCLR1			; Event-Trigger Clear Register
	or		*AR0,#0x1			; clear epwm1 interrupt flags

	mov		AR0,#PIEACK
	or		*AR0,#0x4			; acknowledge interrupt 3
			
	iret

