diff options
| author | Atsushi Saitoh <lynxeyed@xj9.so-net.ne.jp> | 2012-03-18 10:32:43 +0900 |
|---|---|---|
| committer | Atsushi Saitoh <lynxeyed@xj9.so-net.ne.jp> | 2012-03-18 10:32:43 +0900 |
| commit | 70026dd0fafb340dc6b9522324c880c9fc427b62 (patch) | |
| tree | 7f85bb0a9c9d81ce8c1111cdd2ba3445b2e65c77 | |
| parent | bbc1578f7b58b58b9299b8a46a2d363161cd0055 (diff) | |
| parent | 7f6b217b9f31953e40edbb6a9177d69f604a2a23 (diff) | |
Merge https://github.com/lynxeyed-atsu/eXodusino
| -rw-r--r-- | src/core/Print.cpp | 109 | ||||
| -rw-r--r-- | src/core/Print.h | 42 | ||||
| -rwxr-xr-x | src/core/uart.cpp | 172 | ||||
| -rwxr-xr-x | src/core/uart.h | 74 |
4 files changed, 213 insertions, 184 deletions
diff --git a/src/core/Print.cpp b/src/core/Print.cpp new file mode 100644 index 0000000..ab58b9f --- /dev/null +++ b/src/core/Print.cpp @@ -0,0 +1,109 @@ +#include "Print.h" + +void Print::print(const char *s) //uart_puts +{ + int n; + for (n = 0; *s; s++, n++) { + write(*s); + } + //return n; + return; +} + +void Print::print(int val, RADIX_FORMAT format) +{ + if(format == BYTE) + { + write((char)(val & 0xff)); + return; + } + char s[32]; + int i,j; + i=j=0; // header of strings + if (val<0) + { //minus value + s[i]='-'; + val=~val+1; + i++; + j++; + } + switch(format) + { + case BIN: + while (val>0) + { // do until zero + s[i++]=radix_bin[val%format]; + val/=format; + } + break; + + case OCT: + while (val>0) + { // do until zero + s[i++]=radix_oct[val%format]; + val/=format; + } + break; + + case DEC: //RADIX = Decimal + while (val>0) + { // do until zero + s[i++]=radix_dec[val%format]; + val/=format; + } + break; + + case HEX: //RADIX = Hexadecimal + while (val>0) + { // do until zero + s[i++]=radix_hex[val%format]; + val/=format; + } + break; + default: + break; + } + s[i--]='\0'; // EOF + while (j<i) + { // reverse strings array + char t; + t=s[j]; s[j]=s[i]; s[i]=t; + j++; i--; + } + print(s); + + return ; +} + +void Print::write(const char *s, int length) +{ + int n; + for(n=0;n<length;s++,n++) + { + write(*s); + } + return; +} + +void Print::println(const char *s) +{ + print(s); + println(); + return; +} + + +void Print::println(int val, RADIX_FORMAT format) +{ + print(val,format); + println(); + return; +} + +void Print::println(void) +{ + write ('\r'); + write ('\n'); + return; +} + diff --git a/src/core/Print.h b/src/core/Print.h new file mode 100644 index 0000000..3c83abe --- /dev/null +++ b/src/core/Print.h @@ -0,0 +1,42 @@ +/* + Print.cpp + Base class that provides print() and println() + Created on: 2012/03/17 + */ +#ifndef Print_h +#define Print_h + +#include <string.h> +#include "LPC11xx.h" + +const char radix_bin[] = "01"; +const char radix_oct[] = "01234567"; +const char radix_dec[] = "0123456789"; +const char radix_hex[] = "0123456789ABCDEF"; + +typedef enum { + BYTE = 0x01, /*!< 1バイトのアスキーコードが表示される */ + BIN = 0x02, /*!< dataで与えられた数値を2進数で表示する */ + OCT = 0x08, /*!< dataで与えられた数値を8進数で表示する */ + DEC = 0x0A, /*!< dataで与えられた数値を10進数で表示する */ + HEX = 0x10 +/*!< dataで与えられた数値を16進数で表示する */ +} RADIX_FORMAT; + + +class Print { +private: + // RADIX_FORMAT format; +public: + virtual void write(const char c) = 0; + void write(const char *s,int length); + void write(const char *s) { return write((const char *)s, strlen(s)); } + + virtual void println(void); + void print(const char *s); + void print(int val, RADIX_FORMAT format); + void println(const char *s); + void println(int val, RADIX_FORMAT format); +}; + +#endif diff --git a/src/core/uart.cpp b/src/core/uart.cpp index ea61c3c..7142723 100755 --- a/src/core/uart.cpp +++ b/src/core/uart.cpp @@ -1,8 +1,8 @@ /* * uart.cpp * - * Created on: 2011/07/30 - * Author: lynxeyed + * Created on: 2011/07/30 + * Author: lynxeyed */ #include <string.h> @@ -10,54 +10,53 @@ #include "uart.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif - // Flag mask for UART_LSR register. -const unsigned long LSR_TEMT = 0x40; -const unsigned long LSR_THRE = 0x20; -const unsigned long LSR_RDR = 0x01; -const unsigned long ACR_START = 0x01; -const unsigned long IER_RLS = 0x04; -const unsigned long IER_RBR = 0x01; +// Flag mask for UART_LSR register. +const unsigned long LSR_TEMT = 0x40; +const unsigned long LSR_THRE = 0x20; +const unsigned long LSR_RDR = 0x01; +const unsigned long ACR_START = 0x01; +const unsigned long IER_RLS = 0x04; +const unsigned long IER_RBR = 0x01; USerial Serial; -USerial::USerial()//USerial(SERIAL_PORT port) //define constructor +USerial::USerial()//USerial(SERIAL_PORT port) //define constructor { - this->port = marySerial1; //default = marySerial1 - format = OCT; - speed = 9600; //baudrate + this->port = marySerial1; //default = marySerial1 +// format = OCT; + speed = 9600; //baudrate //begin(speed); } -USerial::~USerial() -{ +USerial::~USerial() { } -void USerial::begin(int speed) -{ +void USerial::begin(int speed) { this->speed = speed; // Divisor ratio unsigned long Fdiv; // Configure P1[7] as TXD output. - LPC_IOCON->PIO1_7 &= ~0x07; // FUNC=000 (GPIO) - LPC_IOCON->PIO1_7 |= 0x01; // FUNC=001 (TXD) + LPC_IOCON->PIO1_7 &= ~0x07; // FUNC=000 (GPIO) + LPC_IOCON->PIO1_7 |= 0x01; // FUNC=001 (TXD) // Configure P1[6] as RXD input. - LPC_IOCON->PIO1_6 &= ~0x07; // FUNC=000 (GPIO) - LPC_IOCON->PIO1_6 |= 0x01; // FUNC=001 (RXD) + LPC_IOCON->PIO1_6 &= ~0x07; // FUNC=000 (GPIO) + LPC_IOCON->PIO1_6 |= 0x01; // FUNC=001 (RXD) // Enable UART clock - LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); // UART=1 + LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 12); // UART=1 // Enable UART peripheral clock - LPC_SYSCON->UARTCLKDIV = 0x01; // DIV=1 + LPC_SYSCON->UARTCLKDIV = 0x01; // DIV=1 // fix baud-rate - Fdiv = (SystemCoreClock * LPC_SYSCON->SYSAHBCLKDIV) / (LPC_SYSCON->UARTCLKDIV * 16 * speed); + Fdiv = (SystemCoreClock * LPC_SYSCON->SYSAHBCLKDIV) + / (LPC_SYSCON->UARTCLKDIV * 16 * speed); // Set the baud rate divisor value LPC_UART->LCR |= 0x80; @@ -66,141 +65,38 @@ void USerial::begin(int speed) LPC_UART->LCR &= ~0x80; // Configure UART module as - // 8 bit, 1 stop bit, no parity - // Enable and reset TX and RX FIFO. + // 8 bit, 1 stop bit, no parity + // Enable and reset TX and RX FIFO. LPC_UART->LCR = 0x03; LPC_UART->FCR = 0x07; - } -void USerial::end() -{ +void USerial::end() { return; } -void USerial::write(const char c) //uart_putc + +void USerial::write(const char c) //uart_putc { // Wait for TX buffer empty - while (!(LPC_UART->LSR & LSR_THRE)); + while (!(LPC_UART->LSR & LSR_THRE)) + ; // Put a character LPC_UART->THR = c; return; } -void USerial::print(const char *s) //uart_puts -{ - int n; - for (n = 0; *s; s++, n++) { - write(*s); - } - //return n; - return; -} - -void USerial::print(int val, RADIX_FORMAT format) -{ - if(format == BYTE) - { - write((char)(val & 0xff)); - return; - } - char s[32]; - int i,j; - i=j=0; // header of strings - if (val<0) - { //minus value - s[i]='-'; - val=~val+1; - i++; - j++; - } - switch(format) - { - case BIN: - while (val>0) - { // do until zero - s[i++]=radix_bin[val%format]; - val/=format; - } - break; - - case OCT: - while (val>0) - { // do until zero - s[i++]=radix_oct[val%format]; - val/=format; - } - break; - - case DEC: //RADIX = Decimal - while (val>0) - { // do until zero - s[i++]=radix_dec[val%format]; - val/=format; - } - break; - - case HEX: //RADIX = Hexadecimal - while (val>0) - { // do until zero - s[i++]=radix_hex[val%format]; - val/=format; - } - break; - default: - break; - } - s[i--]='\0'; // EOF - while (j<i) - { // reverse strings array - char t; - t=s[j]; s[j]=s[i]; s[i]=t; - j++; i--; - } - print(s); - - return ; -} - -void USerial::write(const char *s, int length) -{ - int n; - for(n=0;n<length;s++,n++) - { - write(*s); - } - return; -} - -void USerial::println(const char *s) -{ - print(s); - write ('\r'); - write ('\n'); - return; -} - - -void USerial::println(int val, RADIX_FORMAT format) -{ - print(val,format); - write ('\r'); - write ('\n'); - return; -} - -int USerial::available(void) -{ +int USerial::available(void) { return (LPC_UART->LSR & LSR_RDR); } -int USerial::read(void) -{ +int USerial::read(void) { // Wait for RX buffer valid //while (!(LPC_UART->LSR & LSR_RDR)); return LPC_UART->RBR; } + #ifdef __cplusplus } //extern "C" #endif diff --git a/src/core/uart.h b/src/core/uart.h index c7e5125..6e3a1a7 100755 --- a/src/core/uart.h +++ b/src/core/uart.h @@ -2,12 +2,12 @@ #define UART_H_ /**************************************************************************//** - * @file uart.h - * @brief Arduino-like library for MARY(LPC1114) - * @version v.0.50 - * @date 1. August 2011. - * @author @@lynxeyed_atsu - * @note Copyright (c) 2011 Lynx-EyED's Klavier and Craft-works. <BR> + * @file uart.h + * @brief Arduino-like library for MARY(LPC1114) + * @version v.0.50 + * @date 1. August 2011. + * @author @@lynxeyed_atsu + * @note Copyright (c) 2011 Lynx-EyED's Klavier and Craft-works. <BR> * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation @@ -26,65 +26,47 @@ ******************************************************************************/ #include "LPC11xx.h" +#include "Print.h" + #ifdef __cplusplus - extern "C" { +extern "C" { #endif -const char radix_bin[]="01"; -const char radix_oct[]="01234567"; -const char radix_dec[]="0123456789"; -const char radix_hex[]="0123456789ABCDEF"; -/*! @enum SERIAL_PORT - @brief COMポート(UART)のチャンネル選択。 - @note UARTが1ポートしかないMARYでは使用していないので特に影響はないが、2ポート以上UARTが存在するLPCXpressoシリーズや - mbedシリーズとの後方互換性を考慮し用意している -*/ +/*! @enum SERIAL_PORT + @brief COMポート(UART)のチャンネル選択。 + @note UARTが1ポートしかないMARYでは使用していないので特に影響はないが、2ポート以上UARTが存在するLPCXpressoシリーズや + mbedシリーズとの後方互換性を考慮し用意している + */ typedef enum { - marySerial1, /*!< MARYのUARTポート(デフォルト) */ - mbedSerial1, /*!< mbed(LPC1768/9)のUART1ポート(将来的に対応予定) */ - mbedSerial2 /*!< mbed(LPC1768/9)のUART2ポート(将来的に対応予定) */ + marySerial1, /*!< MARYのUARTポート(デフォルト) */ + mbedSerial1, /*!< mbed(LPC1768/9)のUART1ポート(将来的に対応予定) */ + mbedSerial2 +/*!< mbed(LPC1768/9)のUART2ポート(将来的に対応予定) */ } SERIAL_PORT; // Defaults:marySerial1 -/*! @enum SERIAL_FORMAT - @brief Arduinoのprint(data, format)の第二パラメータ(format)の基数。 -*/ -typedef enum { - BYTE = 0x01, /*!< 1バイトのアスキーコードが表示される */ - BIN = 0x02, /*!< dataで与えられた数値を2進数で表示する */ - OCT = 0x08, /*!< dataで与えられた数値を8進数で表示する */ - DEC = 0x0A, /*!< dataで与えられた数値を10進数で表示する */ - HEX = 0x10 /*!< dataで与えられた数値を16進数で表示する */ -} RADIX_FORMAT; - - -class USerial { +class USerial: public Print { private: SERIAL_PORT port; - RADIX_FORMAT format; + //RADIX_FORMAT format; int speed; public: - USerial();//USerial(SERIAL_PORT port); // announce the constructor to initialize + USerial();//USerial(SERIAL_PORT port); // announce the constructor to initialize ~USerial(); void begin(int speed); - void end() ; - void print(const char *s); - void print(int val, RADIX_FORMAT format); - void println(const char *s); - void println(int val, RADIX_FORMAT format) ; - int read(void); - int available(void); - void write(const char c); - void write(const char *s,int length); -}; + void end(); + int read(void); + int available(void); + virtual void write(const char c); + using Print::write; // pull in write(buf, size) from Print +}; extern USerial Serial; #ifdef __cplusplus - } +} #endif - #endif /* UART_H_ */ |
