Using the Tiny RTC to wake from a deep sleep

wiring

The DS1307 on the Tiny RTC can generate a square wave every second.  This can be used as a periodic interrupt to wake the LPC1114 from a deep sleep.  GPIO0 pins 0 to 11 and GPIO 1 bit 0 can trigger a wake event; in this case, GPIO0 bit 8 (GPIO0_8) was used.  This involves a little bit of interrupt configuration and some confusion as GPIO0_8 can be used as a general purpose external interrupt as well as a wake-up interrupt.  These are not programmed in the same way.  Anyway, during deep sleep, current consumption drops to 67 micro amps, and runs up to about 12mA during full speed operation.  No doubt more tweaking code be done to reduce these figures further.  Code is available over here on github

The code contains a function called lp_ports which configures all unused GPIO’s as outputs and turns off pull up resistors.  I didn’t find this made much difference so the function remains but is not called.

Using the LPC1114 with the TinyRTC

I bought a TinyRTC module on dx.com for a couple of euro.  It contains an at24c32 32k flash memory IC and a ds1307 real time clock IC.  The pictures below show how it was interfaced (via I2C) to an LPC1114FN28.  The LPC1114 was programmed using ISP/IAP over a USB to serial interface.  GCC 5.2.1 was used to compile code and LPC21ISP was used to download it.

IMG_20160323_102852230

 Top view of TinyRTC module on breadboard

IMG_20160323_102502616

 Bottom view of Tiny RTC on breadboard.

The code to drive all of this is available on this  github page

The code consists of the following modules:

init : This contains the interrupt vector table and a startup routine which initializes global and static variables, bumps the clock speed up to 48MHz and finally calls on main.

serial : This module manages the UART interface and is used extensively to output run time and debugging information

i2c : This module manages the I2C interface and is implemented as recommended in the LPC1114 reference manual .  The I2C interface is implemented as a state machine in hardware and takes a little getting used to.  If you enable debugging (uncomment the line #define DEBUG 1 at the start of the i2c.c file) the module outputs debug trace data showing all of the states visited by the I2C state machine.

ds1307: This manages the interface to the RTC and has set/get date routines.

at24c32: This manages the interface with the AT24C32 memory IC and contains functions to read and write random data (be careful of the 1 million write cycles).

main: This is the top level application which exchanges data with the DS1307 and AT24C32.

The DS1307 is capable of outputting a 1Hz square wave which may be used to wake a slumbering MCU from a power saving deep sleep.  Hopefully I will get around to this in a future post.

Blinky in assembler on the LPC1114FN28

I’ve been struggling with this demonstrator of assembler on the LPC1114 for a while but finally cracked it (on a train ride across the country :). Hope this is instructive to somebody. I think I have stripped the assembler directives down as much as possible, if you find a mistake please comment below.

// Minimal assembler blinky example for LPC1114FN28
// This program flashes an LED on and off.  It is used as a 
// demonstrator for class work
// LED is on GPIO0 bit 2
		.syntax unified
		.cpu cortex-m0	// Which variant we are using
		.thumb  /* Cortex micros only understand thumb(2) code	*/
		.global Vector_Table, start	
/* Set up interrupt vector table */
		.text
/* It would seem that the LPC1114FN28 treats the area of flash used by
the first 16 vectors as special.  If you attempt to place code here
it doesn't work so you must either skip past this space or just place
dummy values here */
Vector_Table:    
	.word     0x10001000 	/* Top of Stack */
    .word     start     	/* Reset Handler */
    .word 	  0		/* NMI */
	.word 	  0		/* Hard Fault */
	.word 	  0     	/* Reserved */
	.word 	  0     	/* Reserved */
	.word 	  0   	    	/* Reserved */
	.word 	  0  	     	/* Reserved */
	.word 	  0   	     	/* Reserved */
	.word 	  0  	      	/* Reserved */
	.word 	  0   	     	/* Reserved */
	.word 	  0		/* SVC */
	.word 	  0    	    	/* Reserved */
	.word 	  0     	/* Reserved */
	.word 	  0 		/* PendSV */
	.word 	  0 		/* SysTick */	
    
   
/* the program starts here */	
	.thumb_func		// The LSB of the address of thumb instructions is 
				// required to be 1 - the LSB is used as a flag to 
				// indicate thumb instructions are being used.
				// The Cortex M series of processors only understand
				// thumb instructions so if PC is loaded with an
				// address with LSB = 0 then this is treated as an
				// an illegal instruction 
				// the .thumb_func directive forces the LSB of
				// 'start' to be 1

start:
// Do initial IO configuration
// Turn on the clocks for GPIO and IOCON
		ldr R1,SYSAHBCLKCTRL	// make pointer to register		
		ldr R0,[R1]		// read current value
		ldr R2,SYSAHBCLKMASK	// read desired bit pattern
		orrs R0,R0,R2		// combine with register contents
		str R0,[R1]		// write back to register
// Make sure pin 25 behaves as a GPIO
		ldr R1,IOCON_PIO0_2	// make pointer to register
		ldr R0,[R1]		// read current value
		ldr R2,IOCONMASK	// read desired bit pattern
		bics R0,R0,R2		// combine with register contents
		str R0,[R1]		// write back to register
// Make pin 25 behave as an output
		ldr R1,GPIO0DIR		// make pointer to register
		ldr R0,[R1]		// read current value
		ldr R2,GPIO0MASK	// read desired bit pattern
		orrs R0,R0,R2		// combine with register contents
		str R0,[R1]		// write back to register
		
// Now enter the main loop
main_loop:
// Set bit 2
		ldr R1,GPIO0DATA	// make pointer to port data register
		ldr R0,[R1]		// read current value
		ldr R2,GPIO0MASK	// read bit pattern
		orrs R0,R0,R2		// set bits
		str R0,[R1]		// write new value		
		
		bl delay		// Leave the LED on for a while

// clear bit 2
		ldr R1,GPIO0DATA	// make pointer to port data register
		ldr R0,[R1]		// read current value
		ldr R2,GPIO0MASK	// read mask
		bics R0,R0,R2		// clear bits
		str R0,[R1]		// write new value
		
		bl delay		// Leave the LED on for a while
		
		b	main_loop	/* branch back to start of loop */

delay:	ldr R3,DELAYLENGTH
delay_loop:
		subs R3,R3,#1
		bne delay_loop
		bx LR
// Constants go below here		
		.align 2		/* Must force realignment again here */
GPIO0DATA: 	.word 	0x50003ffc
GPIO0DIR: 	.word 	0x50008000
SYSAHBCLKCTRL: 	.word	0x40048080
IOCON_PIO0_2:	.word 	0x4004401c
SYSAHBCLKMASK:	.word   0x10040
IOCONMASK:	.word   0x3
GPIO0MASK:	.word 	0x04
NOTGPIO0MASK:	.word 	0xfffffffb
DELAYLENGTH:	.word 	0xfffff

The Linker script file is as follows:

MEMORY
{
    flash : org = 0x00000000, len = 32k
    ram : org = 0x10000000, len = 4k
}
  
SECTIONS
{
        
	. = ORIGIN(flash);
        .text : {
		  *(.vectors); /* The interrupt vectors */
		  *(.text);
        } >flash
	. = ORIGIN(ram);
        .data : {
	  INIT_DATA_VALUES = LOADADDR(.data);
	  INIT_DATA_START = .;
	    *(.data);
	  INIT_DATA_END = .;
        } >ram AT>flash
	BSS_START = .;
	.bss : {
	  
	    *(.bss);
	  
	} > ram
	BSS_END = .;
}

And finally the makefile is

# Specify the compiler to use
CC=arm-none-eabi-gcc
# Specify the assembler to use
AS=arm-none-eabi-as
# Specity the linker to use
LD=arm-none-eabi-ld

CCFLAGS=-mcpu=cortex-m0 -mthumb -g 
ASFLAGS=-mcpu=cortex-m0 -mthumb -g --warn
# List the object files involved in this project
OBJS=	blinky.o 
blinky.elf : $(OBJS)
	$(LD) $(OBJS) -T linker_script.ld --cref -Map blinky.map -nostartfiles -o main.elf
	 objcopy -O ihex main.elf main.hex
blinky.o: blinky.s	
	$(AS) $(ASFLAGS) blinky.s -asghl=blinky.lst -o blinky.o
# if someone types in 'make clean' then remove all object files and executables
# associated wit this project
clean: 
	rm $(OBJS) 
	rm blinky.elf