' Test TIMER1 interrupt
' TIMER1 will interrupt every 2 seconds and print a dot
#if defined(LPC1756) || defined(LPC1751)
#include "LPC17xx.bas"
#elif defined(LPC1114)
#include "LPC11xx.bas"
#elif defined(LPC11U37)
#include "LPC11U3x.bas"
#elif defined(LPC54102)
#include "LPC54102.bas"
#elif defined(LPC54016) || defined(LPC54005)
#include "LPC540xx.bas"
#elif defined(LPC812)
#error -- use SCTIMER.bas for LPC812
#elif defined(LPC2103) || defined(LPC2138)
#include "LPC21xx.bas"
#else
#error -- your CPU does not support this feature
#endif
dim e3 as integer
dim s3 as integer
INTERRUPT SUB TIMER1IRQ
T1_IR = 1 ' Clear interrupt
#if defined(LPC2103) || defined(LPC2138)
VICVectAddr = 0 ' Acknowledge Interrupt
#endif
e3 = e3 + 1
ENDSUB
SUB ON_TIMER ( msec, dothis )
#if defined(LPC1756) || defined(LPC1751)
TIMER1_ISR = dothis + 1 'set function of VIC -- need the +1 for Thumb operation
T1_PR = 24999 '1 ms prescale
VICIntEnable or= (1<<2) 'Enable interrupt
#elif defined(LPC11U37)
TIMER1_ISR = dothis + 1 'set function of VIC -- need the +1 for Thumb operation
SYSCON_SYSAHBCLKCTRL OR= (1<<10) ' enable TIMER1
T1_PR = 24999 '1 ms prescale
VICIntEnable OR= (1<<TIMER1_IRQn) 'Enable interrupt
#elif defined(LPC1114)
TIMER1_ISR = dothis + 1 'set function of VIC -- need the +1 for Thumb operation
T1_PR = 49999 '1 ms prescale
VICIntEnable or= (1<<19) 'Enable interrupt REMEMBER for LPC11xx we swapped 0/1 with 2/3
#elif defined(LPC2103) || defined(LPC2138)
VICVectAddr7 = dothis ' set function of VIC 7
VICVectCntl7 = &H25 ' use it for TIMER1 Interrupt:
VICIntEnable = &H20 ' Enable TIMER1 Interrupt
#elif defined(LPC54102)
TIMER1_ISR = dothis + 1 'set function of VIC -- need the +1 for Thumb operation
ASYNCAPBCLKCTRLSET = SYSCON_CLOCK_TIMER1 ' enable TIMER1
T1_PR = 12499 '1 ms prescale
VICIntEnSet0 = (1<<TIMER1_IRQn) 'Enable interrupt
#elif defined(LPC54016) || defined(LPC54005)
SCB_VTOR = 0 ' early firmware version did not set this
TIMER1_ISR = dothis + 1 'set function of VIC -- need the +1 for Thumb operation
SYSCON_AHBCLKCTRLSET1 = SYSCON_CLOCK_TIMER1 ' enable TIMER1
T1_PR = 179999 '1 ms prescale
VICIntEnSet0 = (1<<TIMER1_IRQn) 'Enable interrupt
#else
#endif
T1_MR0 = msec-1 ' set up match number of ms
T1_MCR = 3 ' Interrupt and Reset on MR0
T1_IR = 1 ' clear interrupt
T1_TC = 0 ' clear timer counter
T1_TCR = 1 ' TIMER1 Enable
#if defined(LPC2103) || defined(LPC2138)
VICVectAddr = 0 ' Acknowledge Interrupt
#endif
ENDSUB
dim starttime as integer ' in 5 minute increments
dim startmonth as integer
dim startday as integer
dim starthour as integer
dim startminit as integer
sub printDays (atime)
dim month as integer ' not yet implimented
dim day as integer
dim hour as integer
dim minit as integer
day = atime / (24 * 60)
hour = (atime - (day * 24 * 60)) / 60
minit = atime - (day * 24 * 60) - (hour * 60)
print day, hour, minit
end sub
#include "ASMinlineBASIC.bas"
dim iap_command(4)
dim iap_result(4)
sub IAPcall
dim i, save_NVIC, save_NVIC1
save_NVIC = VICIntEnSet0 'save interrupt setting
VICIntEnClear = save_NVIC ' disable interrupts during IAP call
#ifdef VICIntEnSet1 ' this is for some other parts than 1114, but needs expansion
dim save_NVIC1
VICIntEnClear1 = save_NVIC1
#endif
i = addressof iap_command
ASM_MOV(r0,r7)
i = addressof iap_result
ASM_MOV(r1,r7)
CALL (&H1FFF1FF1)
VICIntEnSet0 = save_NVIC 're-Enable interrupts
#ifdef VICIntEnSet1
VICIntEnSet1 = save_NVIC1
#endif
#ifdef IAP_DEBUG
for i=0 to 3
print "0x";hex(iap_result(i)),
next
print
#endif
end Sub
dim logs (64)
sub writeLog2Flash (Faddress) ' writes into flash can only clear bits, so for a complete write the whole sector has to be 0xFFFFFFFF -- via erase
iap_command(0)= 50
iap_command(1)= Faddress >> 12
iap_command(2)= Faddress >> 12
iap_command(3)= 0
IAPcall() ' prep sector for write
iap_command(0)= 51
iap_command(1)= Faddress
iap_command(2)= addressof(logs)
iap_command(3)= 256
iap_command(4)= 50000
IAPcall() ' write into the sector
end sub
sub eraseFlash (Faddress)
iap_command(0)= 50
iap_command(1)= Faddress >> 12
iap_command(2)= Faddress >> 12
iap_command(3)= 0
IAPcall() ' prep sector for write
iap_command(0)= 52
iap_command(1)= Faddress >> 12
iap_command(2)= Faddress >> 12
iap_command(3)= 50000
iap_command(4)= 0
IAPcall() ' write 0xFFFFFFFF to the sector
end sub
dim FlashStart, LogCnt
sub initLog
dim i
FlashStart = 0x5000
LogCnt = 0
while LogCnt = 0
for i=0 to 63
logs(i) = * (FlashStart + (i<<2))
if logs(i) <> -1 then
LogCnt = i
printDays (logs(i)) ' this prints out the log date
endif
next
if LogCnt < 63 then return
LogCnt = 0
FlashStart += 0x100
loop
end sub
sub add2log (atime)
logs(LogCnt) = atime
writeLog2Flash(FlashStart)
LogCnt += 1
if LogCnt = 64 then
LogCnt = 0
FlashStart += 0x100
endif
end sub
#define SEC60 57650 '57650 ' adjusted by hand - or make small for testing
#define MIN_INTERVAL 5
' #define ERASE_FLASH
main:
#ifdef ERASE_FLASH
print "Erase Flash"
eraseFlash(0x5000)
end
#endif
dim nowtime
dim people
dim idle_cnt_dn
dim triggered
nowtime = starttime
print "TIMER1 will interrupt every ", MIN_INTERVAL; " minutes and print a dot"
ON_TIMER(SEC60 * MIN_INTERVAL, ADDRESSOF TIMER1IRQ)
s3 = 0
e3 = 0
print "start"
initLog()
idle_cnt_dn = 5
WHILE (1)
if in(6) then people = 1
if s3 <> e3 then
s3 = e3
nowtime += MIN_INTERVAL
if (people) then
print "*";
printDays(nowtime - starttime)
if idle_cnt_dn < 0 then
triggered = 1
io(1) = 0 ' turn on the triggered LED
add2log(nowtime - starttime)
endif
else
print ".";
idle_cnt_dn -= 1
endif
people = 0
endif
LOOP