Skip to content

Commit

Permalink
Merge pull request #42 from UnifiedEngineering/irq-integration
Browse files Browse the repository at this point in the history
Bring in irq-integration to master
  • Loading branch information
xnk committed Jan 2, 2015
2 parents db599e0 + d6d5485 commit 8b9daad
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 34 deletions.
100 changes: 73 additions & 27 deletions src/cr_startup_lpc21.s
Original file line number Diff line number Diff line change
Expand Up @@ -129,34 +129,80 @@ _boot:
*/
Vectors:
B _start // reset
LDR pc,_undf // undefined
LDR pc,_swi // SWI/SVC
LDR pc,_pabt // program abort
LDR pc,_dabt // data abort
MOV pc,#__undf // undefined
MOV pc,#__swi // SWI/SVC
MOV pc,#__pabt // program abort
MOV pc,#__dabt // data abort
NOP // Reserved for the flash checksum
LDR pc,[pc,#-0xFF0] // IRQ - read the VIC register
// LDR pc,_irq // or go to default handler
LDR pc,_fiq // FIQ
// MOV pc,#__fiq // Do __fiq in-place instead
__fiq: MOV r0,#_fiqstr // FIQ
B _printexc
__undf: MOV r0,#_undfstr // undefined
B _printexc
__pabt: MOV r0,#_pabtstr // program abort
B _printexc
__dabt: MOV r0,#_dabtstr // data abort
// lr contains instruction that failed to access data +8 so adjust an additional 4 bytes
sub lr,lr,#4
B _printexc
__swi:
MOV r0,#_swistr
// B _printexc // Fall-thru
_printexc:
.set UART0, 0xe000c000
.set U0LSR_OFFS, 0x14
.set U0IER_OFFS, 0x04
.set U0THR_OFFS, 0x00
.set FIO0CLR, 0x3fffc01c
// lr contains address of failed instruction +4
SUB lr,lr,#4
LDR r1,=UART0
// MOV r2,#0
// STR r2,[r1,#U0IER_OFFS]

// Output exception text
_ploop:
LDRB r2,[r0],#1
_pwait:
LDR r3,[r1,#U0LSR_OFFS]
TST r3,#0x20
BEQ _pwait
CMP r2,#0
STRNE r2,[r1,#U0THR_OFFS]
BNE _ploop
_done:

// Output lr register in hex
MOV r5,#8
MOV r4,#_hexstr
_hloop:
LSR r0,lr,#0x1c
LDR r2,[r4,r0]
_pwait2:
LDR r3,[r1,#U0LSR_OFFS]
TST r3,#0x20
BEQ _pwait2
STR r2,[r1,#U0THR_OFFS]
LSL lr,lr,#4
SUBS r5,r5,#1
BNE _hloop

// Turn off backlight to indicate fault
// LDR r1,=FIO0CLR
// MOV r2,#(1<<11)
// STR r2,[r1]
_realdone:
B _realdone

_hexstr: .ASCII "0123456789abcdef"
_undfstr: .ASCIZ "UNDF@"
_pabtstr: .ASCIZ "PABT@"
_dabtstr: .ASCIZ "DABT@"
_fiqstr: .ASCIZ "FIQ?"
_swistr: .ASCIZ "SWI?"


_undf: .word __undf // undefined
_swi: .word _swi_handler // SWI
_pabt: .word __pabt // program abort
_dabt: .word __dabt // data abort
_irq: .word __irq
_fiq: .word __fiq // FIQ

/*
* Some simple default handlers
*/
__undf: B . // undefined
__pabt: B . // program abort
__dabt: B . // data abort
__irq: B . // IRQ
__fiq: B . // FIQ

_swi_handler:
B .
.endfunc
/*
* Setup the operating mode & stack.
Expand Down Expand Up @@ -440,10 +486,10 @@ post_data_bss_init:
* Call main program: main(0)
*/
MOV r0,#0 // no arguments (argc = 0)
MOV r1,r0
MOV r2,r0
// MOV r1,r0
// MOV r2,r0
MOV fp,r0 // null frame pointer
MOV r7,r0 // null frame pointer for thumb
// MOV r7,r0 // null frame pointer for thumb

// Change to system mode (IRQs enabled) before calling main application

Expand Down
3 changes: 3 additions & 0 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "t962.h"
#include "io.h"
#include "sched.h"
#include "vic.h"

void Set_Heater(uint8_t enable) {
if( enable < 0xff ) {
Expand Down Expand Up @@ -53,8 +54,10 @@ static int32_t Sleep_Work(void) {
flip ^= 1;

// If interrupts are used they must be disabled around the following two instructions!
uint32_t save = VIC_DisableIRQ();
WDFEED = 0xaa; // Feed watchdog
WDFEED = 0x55;
VIC_RestoreIRQ( save );
return TICKS_SECS(1);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "buzzer.h"
#include "nvstorage.h"
#include "version.h"
#include "vic.h"
#include "max31855.h"
#include "systemfan.h"

Expand Down Expand Up @@ -135,7 +136,8 @@ int main(void) {
VPBDIV = 0x01; // APB runs at the same frequency as the CPU (55.296MHz)
MAMTIM = 0x03; // 3 cycles flash access recommended >40MHz
MAMCR = 0x02; // Fully enable memory accelerator


VIC_Init();
Sched_Init();
IO_Init();
Set_Heater(0);
Expand Down
15 changes: 15 additions & 0 deletions src/onewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string.h>
#include "onewire.h"
#include "sched.h"
#include "vic.h"

static inline void setpin0() {
FIO0CLR = (1<<7);
Expand Down Expand Up @@ -280,6 +281,7 @@ static int32_t OneWire_Work( void ) {
int32_t retval = 0;

if( mystate == 0 ) {
uint32_t save = VIC_DisableIRQ();
if(resetbus()) {
xferbyte(OW_SKIP_ROM); // All devices on the bus are addressed here
xferbyte(OW_CONVERT_T);
Expand All @@ -288,14 +290,17 @@ static int32_t OneWire_Work( void ) {
retval = TICKS_MS(100); // TC interface needs max 100ms to be ready
mystate++;
}
VIC_RestoreIRQ( save );
} else if( mystate == 1 ) {
for( int i = 0; i < numowdevices; i++ ) {
uint32_t save = VIC_DisableIRQ();
selectdevbyidx(i);
xferbyte(OW_READ_SCRATCHPAD);
for(uint32_t iter=0; iter<4; iter++) { // Read four bytes
scratch[iter] = xferbyte(0xff);
//printf("%02x ",scratch[iter]);
}
VIC_RestoreIRQ( save );
int16_t tmp = scratch[1]<<8 | scratch[0];
devreadout[i] = tmp;
tmp = scratch[3]<<8 | scratch[2];
Expand All @@ -319,12 +324,17 @@ uint32_t OneWire_Init( void ) {
tcidmapping[i] = -1; // Assume we don't find any thermocouple interfaces
}

uint32_t save = VIC_DisableIRQ();
int rslt = OWFirst();
VIC_RestoreIRQ( save );

numowdevices = 0;
while (rslt && numowdevices < MAX_OW_DEVICES) {
memcpy(owdeviceids[numowdevices], ROM_NO, sizeof(ROM_NO));
numowdevices++;
save = VIC_DisableIRQ();
rslt = OWNext();
VIC_RestoreIRQ( save );
}
if(numowdevices) {
for( int iter = 0; iter < numowdevices; iter++ ) {
Expand All @@ -334,28 +344,33 @@ uint32_t OneWire_Init( void ) {
}
uint8_t family = owdeviceids[iter][0];
if(family == OW_FAMILY_TEMP) {
save = VIC_DisableIRQ();
selectdevbyidx(iter);
xferbyte(OW_WRITE_SCRATCHPAD);
xferbyte(0x00);
xferbyte(0x00);
xferbyte(0x1f); // Reduce resolution to 0.5C to keep conversion time reasonable
VIC_RestoreIRQ( save );
tempidx = iter; // Keep track of where we saw the last/only temperature sensor
printf(" [Temperature sensor]");
} else if(family == OW_FAMILY_TC) {
save = VIC_DisableIRQ();
selectdevbyidx(iter);
xferbyte(OW_READ_SCRATCHPAD);
xferbyte(0xff);
xferbyte(0xff);
xferbyte(0xff);
xferbyte(0xff);
uint8_t tcid = xferbyte(0xff) & 0x0f;
VIC_RestoreIRQ( save );
tcidmapping[tcid] = iter; // Keep track of the ID mapping
printf(" [Thermocouple interface, ID %x]",tcid);
}
}
} else {
printf(" No devices found!");
}

if( numowdevices ) {
Sched_SetState( ONEWIRE_WORK, 2, 0 ); // Enable OneWire task if there's at least one device
}
Expand Down
Loading

0 comments on commit 8b9daad

Please sign in to comment.