my implementation of mbed-like classes using the LPC1768 register access.

Dependents:   registers-example RedWireBridge

This is just to satisfy my curiosity on how the mbed libraries work. I put it here just in case others are too. Every time I learn how another internal register works, I'll keep it here to save myself from future coding headaches.

working

  • DigitalIn
  • DigitalOut
  • wait()

mostly working

  • Serial
  • Timer
  • Ticker
  • Timeout

Serial doesn't have all the methods that mbed had, but it works for me. (only UART0, so only over USB to the pc for now) Timer has the same limitations of mbed for default resolution (30 min limit), and if you start at the end of resolution and stop after it rolls back to 0, it doesn't take that into account. But I added the option to change resolution, so I can have longer timers.

For Ticker, I used a 100 microsecond timer instead of a 1 microsecond Timer, so the smallest interval in between function calls is 100 microseconds. (10KHz) However, this means that the maximum interval in between function calls is 59 hours. (untested)

The Timeout class, simply uses a Ticker, but then marks it as nonactive after the first function call. Automatically calls the detach() function when attaching it again, so no don't need to worry about it.

Committer:
elevatorguy
Date:
Thu Jan 03 05:25:18 2013 +0000
Revision:
2:276fb0fe230c
Parent:
0:d2d9baa1a6d8
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elevatorguy 0:d2d9baa1a6d8 1 //this file is for all the global functions that I need to use
elevatorguy 0:d2d9baa1a6d8 2 //but are here, so they are not cluttering up main.cpp
elevatorguy 0:d2d9baa1a6d8 3 #include "functions.h"
elevatorguy 0:d2d9baa1a6d8 4
elevatorguy 0:d2d9baa1a6d8 5 //this uses Timer 0, but we could just as easily change it to Timer 1, 2 or 3
elevatorguy 0:d2d9baa1a6d8 6 void wait(float time) //seconds
elevatorguy 0:d2d9baa1a6d8 7 {
elevatorguy 0:d2d9baa1a6d8 8 uint32_t us = time * 1000000; //microseconds
elevatorguy 0:d2d9baa1a6d8 9
elevatorguy 0:d2d9baa1a6d8 10 uint8_t pclk;
elevatorguy 0:d2d9baa1a6d8 11 uint32_t pclkdiv = (LPC_SC->PCLKSEL0 >> 2) & 0x03;
elevatorguy 0:d2d9baa1a6d8 12
elevatorguy 0:d2d9baa1a6d8 13 switch ( pclkdiv ) // table 42 (page 57 in user manual)
elevatorguy 0:d2d9baa1a6d8 14 {
elevatorguy 0:d2d9baa1a6d8 15 case 0x00:
elevatorguy 0:d2d9baa1a6d8 16 default:
elevatorguy 0:d2d9baa1a6d8 17 pclk = 4;
elevatorguy 0:d2d9baa1a6d8 18 break;
elevatorguy 0:d2d9baa1a6d8 19 case 0x01:
elevatorguy 0:d2d9baa1a6d8 20 pclk = 1;
elevatorguy 0:d2d9baa1a6d8 21 break;
elevatorguy 0:d2d9baa1a6d8 22 case 0x02:
elevatorguy 0:d2d9baa1a6d8 23 pclk = 2;
elevatorguy 0:d2d9baa1a6d8 24 break;
elevatorguy 0:d2d9baa1a6d8 25 case 0x03:
elevatorguy 0:d2d9baa1a6d8 26 pclk = 8;
elevatorguy 0:d2d9baa1a6d8 27 break;
elevatorguy 0:d2d9baa1a6d8 28 }
elevatorguy 0:d2d9baa1a6d8 29
elevatorguy 0:d2d9baa1a6d8 30 LPC_TIM0->TCR = 0x02; /* reset timer */
elevatorguy 0:d2d9baa1a6d8 31 LPC_TIM0->PR = (SystemCoreClock / (pclk * 1000000));
elevatorguy 0:d2d9baa1a6d8 32 LPC_TIM0->MR0 = us; /* enter delay time */
elevatorguy 0:d2d9baa1a6d8 33 LPC_TIM0->IR = 0xff; /* reset all interrrupts */
elevatorguy 0:d2d9baa1a6d8 34 LPC_TIM0->MCR = 0x04; /* stop timer on match */
elevatorguy 0:d2d9baa1a6d8 35 LPC_TIM0->TCR = 0x01; /* start timer */
elevatorguy 0:d2d9baa1a6d8 36
elevatorguy 0:d2d9baa1a6d8 37 /* wait until delay time has elapsed */
elevatorguy 0:d2d9baa1a6d8 38 while (LPC_TIM0->TCR & 0x01);
elevatorguy 0:d2d9baa1a6d8 39 }