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.
Serial.cpp
- Committer:
- elevatorguy
- Date:
- 2013-01-03
- Revision:
- 2:276fb0fe230c
- Parent:
- 0:d2d9baa1a6d8
File content as of revision 2:276fb0fe230c:
#include "Serial.h" void Serial::initUART0() { int pclk; unsigned long int Fdiv; // PCLK_UART0 is being set to 1/4 of SystemCoreClock pclk = SystemCoreClock / 4; // Turn on power to UART0 LPC_SC->PCONP |= (1 << 3); // Turn on UART0 peripheral clock LPC_SC->PCLKSEL0 &= ~(3 << 6); // mask LPC_SC->PCLKSEL0 |= (0 << 6); // PCLK_periph = CCLK/4 ,, 6 is pclk for uart0 // Set PINSEL0 so that P0.2 = TXD0, P0.3 = RXD0 LPC_PINCON->PINSEL0 &= ~0xf0; LPC_PINCON->PINSEL0 |= ((1 << 4) | (1 << 6)); LPC_UART0->LCR = 0x83; // 8 bits, no Parity, 1 Stop bit, DLAB=1 Fdiv = ( pclk / 16 ) / baudrate ; // Set baud rate LPC_UART0->DLM = Fdiv / 256; LPC_UART0->DLL = Fdiv % 256; LPC_UART0->LCR = 0x03; // 8 bits, no Parity, 1 Stop bit DLAB = 0 LPC_UART0->FCR = 0x07; // Enable and reset TX and RX FIFO } Serial::Serial() { baudrate = 9600; initUART0(); } Serial::Serial(int br) { baudrate = br; initUART0(); } void Serial::putc(char c) { while( !(LPC_UART0->LSR & 0x20) ); // Block until tx empty LPC_UART0->THR = c; } char Serial::getc() { char c; while( !(LPC_UART0->LSR & 0x01) ); // wait until something received c = LPC_UART0->RBR; // Read Receiver buffer register return c; } void Serial::printf(char* sendstring) { int i = 0; // loop through until reach string's zero terminator while (sendstring[i] != '\0') { putc(sendstring[i]); // print each character i++; } } void Serial::printf(double number) { int numberAsInt = number; //truncates right of decimal point printf(numberAsInt); putc('.'); int rightOfDecimal = (number-numberAsInt)*1000000000; //truncating smaller than 10^(-9) printf(rightOfDecimal); } void Serial::printf(double number, int res) { int numberAsInt = number; //truncates right of decimal point printf(numberAsInt); putc('.'); int rightOfDecimal = (number-numberAsInt)*res; printf(rightOfDecimal); } void Serial::printf(int number) { //we go from left to right int numofdigits = 0; int temp2 = number; while(temp2 > 0) { numofdigits = numofdigits + 1; temp2 = temp2 / 10; } while(numofdigits > 0) { int temp = numofdigits; int position = 1; while(temp > 1) { position = position * 10; temp = temp - 1; } int digit = (number / position) % 10; char character = tochar(digit); putc(character); numofdigits = numofdigits - 1; // } } // converts an individual digit into a character char Serial::tochar(int digit) { int character = '0'; switch (digit) { case 0 : character = '0'; break; case 1 : character = '1'; break; case 2 : character = '2'; break; case 3 : character = '3'; break; case 4 : character = '4'; break; case 5 : character = '5'; break; case 6 : character = '6'; break; case 7 : character = '7'; break; case 8 : character = '8'; break; case 9 : character = '9'; break; } return character; }