From: r-studio Date: Wed, 28 Oct 2009 07:19:17 +0000 (+0000) Subject: changes to support standart tos PrintfC X-Git-Tag: rc_6_tinyos_2_1_1~177 X-Git-Url: https://oss.titaniummirror.com/gitweb/?p=tinyos-2.x.git;a=commitdiff_plain;h=4bcbb8920bdbdf3830690f09984d97bd9bfa2b3e changes to support standart tos PrintfC --- diff --git a/tos/chips/m16c62p/control/M16c62pControl.nc b/tos/chips/m16c62p/control/M16c62pControl.nc index ba9b398c..c90c91c9 100755 --- a/tos/chips/m16c62p/control/M16c62pControl.nc +++ b/tos/chips/m16c62p/control/M16c62pControl.nc @@ -54,7 +54,7 @@ interface M16c62pControl * * @param def Default system speed ( != M16C62P_DONT_CARE) */ - async command error_t defaultSystemClock(M16c62pSystemClock def); + command error_t defaultSystemClock(M16c62pSystemClock def); /** * Put the mcu to sleep. diff --git a/tos/chips/m16c62p/control/M16c62pControlP.nc b/tos/chips/m16c62p/control/M16c62pControlP.nc index 1f1cceb1..bade4dab 100755 --- a/tos/chips/m16c62p/control/M16c62pControlP.nc +++ b/tos/chips/m16c62p/control/M16c62pControlP.nc @@ -166,10 +166,63 @@ implementation } } + void initPin(volatile uint8_t *port, volatile uint8_t *port_d, uint8_t pin, uint16_t state) + { + uint8_t inactive = (state >> (pin*2)) & 0x3; + // Turn off protection of PD9 + PRCR.BYTE = BIT2; + switch (inactive) + { + case M16C_PIN_INACTIVE_DONT_CARE: + break; + case M16C_PIN_INACTIVE_OUTPUT_LOW: + SET_BIT((*port_d), pin); + CLR_BIT((*port), pin); + break; + case M16C_PIN_INACTIVE_OUTPUT_HIGH: + SET_BIT((*port_d), pin); + SET_BIT((*port), pin); + break; + case M16C_PIN_INACTIVE_INPUT: + CLR_BIT((*port_d), pin); + CLR_BIT((*port), pin); + break; + } + PRCR.BYTE = 0; + } + + void initPort(volatile uint8_t *port, volatile uint8_t *port_d, uint16_t state) + { + initPin(port, port_d, 0, state); + initPin(port, port_d, 1, state); + initPin(port, port_d, 2, state); + initPin(port, port_d, 3, state); + initPin(port, port_d, 4, state); + initPin(port, port_d, 5, state); + initPin(port, port_d, 6, state); + initPin(port, port_d, 7, state); + } + + void initPins() + { + initPort(&P0.BYTE, &PD0.BYTE, PORT_P0_INACTIVE_STATE); + initPort(&P1.BYTE, &PD1.BYTE, PORT_P1_INACTIVE_STATE); + initPort(&P2.BYTE, &PD2.BYTE, PORT_P2_INACTIVE_STATE); + initPort(&P3.BYTE, &PD3.BYTE, PORT_P3_INACTIVE_STATE); + initPort(&P4.BYTE, &PD4.BYTE, PORT_P4_INACTIVE_STATE); + initPort(&P5.BYTE, &PD5.BYTE, PORT_P5_INACTIVE_STATE); + initPort(&P6.BYTE, &PD6.BYTE, PORT_P6_INACTIVE_STATE); + initPort(&P7.BYTE, &PD7.BYTE, PORT_P7_INACTIVE_STATE); + initPort(&P8.BYTE, &PD8.BYTE, PORT_P8_INACTIVE_STATE); + initPort(&P9.BYTE, &PD9.BYTE, PORT_P9_INACTIVE_STATE); + initPort(&P10.BYTE, &PD10.BYTE, PORT_P_10_INACTIVE_STATE); + } + command error_t M16c62pControl.init() { uint8_t i; uint8_t tmp; + initPins(); PRCR.BYTE = BIT1 | BIT0; // Turn off protection for cpu & clock register. PM0.BYTE = BIT7; // Single Chip mode. No BCLK output. @@ -194,7 +247,7 @@ implementation return setSystemClock(M16C62P_DONT_CARE); } - async command error_t M16c62pControl.defaultSystemClock( + command error_t M16c62pControl.defaultSystemClock( M16c62pSystemClock def) { if (def == M16C62P_DONT_CARE) @@ -234,25 +287,25 @@ implementation void stopMode() { - asm("nop"); - asm("nop"); - asm("nop"); - asm("nop"); - asm("nop"); + uint8_t cm0_tmp, cm1_tmp; __nesc_enable_interrupt(); PRCR.BYTE = 1; // Turn off protection of system clock control registers - CM1.BYTE &= ~16; // Xin low drive capacity - CM1.BYTE |= 1; // Enter stop mode - PRCR.BYTE = 0; - asm("nop"); + cm0_tmp = CM0.BYTE; + cm1_tmp = CM1.BYTE; + CM0.BYTE = 0b00001000; + asm("bset 0,0x0007"); // Enter stop mode + asm("jmp.b MAIN_A"); + asm("MAIN_A:"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); + PRCR.BYTE = 0; // Turn off protection of system clock control registers asm volatile ("" : : : "memory"); __nesc_disable_interrupt(); + CM0.BYTE = cm0_tmp; + CM1.BYTE = cm1_tmp; PRCR.BIT.PRC0 = 0; // Turn on protection of system clock control registers - atomic setSystemClock(system_clock); } async command void M16c62pControl.sleep() @@ -283,7 +336,7 @@ implementation } } - async command error_t SystemClockControl.minSpeed[uint8_t client]( + command error_t SystemClockControl.minSpeed[uint8_t client]( M16c62pSystemClock speed) { atomic client_system_clock[client] = speed; diff --git a/tos/chips/m16c62p/control/SystemClockControl.nc b/tos/chips/m16c62p/control/SystemClockControl.nc index 18dde2b5..330c75fa 100644 --- a/tos/chips/m16c62p/control/SystemClockControl.nc +++ b/tos/chips/m16c62p/control/SystemClockControl.nc @@ -46,5 +46,5 @@ interface SystemClockControl * * @param speed The allowed minimum speed. */ - async command error_t minSpeed(M16c62pSystemClock speed); + command error_t minSpeed(M16c62pSystemClock speed); } diff --git a/tos/chips/m16c62p/m16c62phardware.h b/tos/chips/m16c62p/m16c62phardware.h index 1603d661..d85c2de2 100755 --- a/tos/chips/m16c62p/m16c62phardware.h +++ b/tos/chips/m16c62p/m16c62phardware.h @@ -47,6 +47,8 @@ #include "interrupts.h" #include "iom16c62p.h" #include "bits.h" +#include "uart/M16c62pUart.h" +#include "pins/M16c62pPin.h" #define true 1 #define false 0 @@ -147,4 +149,49 @@ inline void __nesc_atomic_end(__nesc_atomic_t original_FLG) @spontaneous() #define PLL_MULTIPLIER M16C62P_PLL_2 #endif +// Default inactive pin states +#ifndef PORT_P0_INACTIVE_STATE +#define PORT_P0_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P1_INACTIVE_STATE +#define PORT_P1_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P2_INACTIVE_STATE +#define PORT_P2_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P3_INACTIVE_STATE +#define PORT_P3_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P4_INACTIVE_STATE +#define PORT_P4_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P5_INACTIVE_STATE +#define PORT_P5_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P6_INACTIVE_STATE +#define PORT_P6_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P7_INACTIVE_STATE +#define PORT_P7_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P8_INACTIVE_STATE +#define PORT_P8_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P9_INACTIVE_STATE +#define PORT_P9_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + +#ifndef PORT_P_10_INACTIVE_STATE +#define PORT_P_10_INACTIVE_STATE M16C_PIN_INACTIVE_DONT_CARE +#endif + #endif // __M16C62PHARDWARE_H__ diff --git a/tos/chips/m16c62p/pins/HplM16c62pGeneralIOC.nc b/tos/chips/m16c62p/pins/HplM16c62pGeneralIOC.nc index ec6a85e6..439143e8 100755 --- a/tos/chips/m16c62p/pins/HplM16c62pGeneralIOC.nc +++ b/tos/chips/m16c62p/pins/HplM16c62pGeneralIOC.nc @@ -149,8 +149,7 @@ configuration HplM16c62pGeneralIOC interface GeneralIO as PortP82; interface GeneralIO as PortP83; interface GeneralIO as PortP84; - // TODO(henrik) What should be done with PortP85? -// interface GeneralIO as PortP85; + interface GeneralIO as PortP85; interface GeneralIO as PortP86; interface GeneralIO as PortP87; @@ -266,17 +265,37 @@ implementation PortP82 = PortP8.Pin2; PortP83 = PortP8.Pin3; PortP84 = PortP8.Pin4; + PortP85 = PortP8.Pin5; PortP86 = PortP8.Pin6; PortP87 = PortP8.Pin7; + + components + new HplM16c62pGeneralIOPinPRC2P() as PortP90W, + new HplM16c62pGeneralIOPinPRC2P() as PortP91W, + new HplM16c62pGeneralIOPinPRC2P() as PortP92W, + new HplM16c62pGeneralIOPinPRC2P() as PortP93W, + new HplM16c62pGeneralIOPinPRC2P() as PortP94W, + new HplM16c62pGeneralIOPinPRC2P() as PortP95W, + new HplM16c62pGeneralIOPinPRC2P() as PortP96W, + new HplM16c62pGeneralIOPinPRC2P() as PortP97W; - PortP90 = PortP9.Pin0; - PortP91 = PortP9.Pin1; - PortP92 = PortP9.Pin2; - PortP93 = PortP9.Pin3; - PortP94 = PortP9.Pin4; - PortP95 = PortP9.Pin5; - PortP96 = PortP9.Pin6; - PortP97 = PortP9.Pin7; + PortP90W -> PortP9.Pin0; + PortP91W -> PortP9.Pin1; + PortP92W -> PortP9.Pin2; + PortP93W -> PortP9.Pin3; + PortP94W -> PortP9.Pin4; + PortP95W -> PortP9.Pin5; + PortP96W -> PortP9.Pin6; + PortP97W -> PortP9.Pin7; + + PortP90 = PortP90W; + PortP91 = PortP91W; + PortP92 = PortP92W; + PortP93 = PortP93W; + PortP94 = PortP94W; + PortP95 = PortP95W; + PortP96 = PortP96W; + PortP97 = PortP97W; PortP100 = PortP_10.Pin0; PortP101 = PortP_10.Pin1; diff --git a/tos/chips/m16c62p/pins/M16c62pPin.h b/tos/chips/m16c62p/pins/M16c62pPin.h new file mode 100644 index 00000000..e4bb8e10 --- /dev/null +++ b/tos/chips/m16c62p/pins/M16c62pPin.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2009 Communication Group and Eislab at + * Lulea University of Technology + * + * Contact: Laurynas Riliskis, LTU + * Mail: laurynas.riliskis@ltu.se + * All rights reserved. + * + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * - Neither the name of Communication Group at Lulea University of Technology + * nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD + * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * M16c62p pin defines and enums. + * + * @author Henrik Makitaavola + */ + + +#ifndef __M16C62P_PIN_H__ +#define __M16C62P_PIN_H__ + +enum +{ + M16C_PIN_INACTIVE_DONT_CARE = 0, + M16C_PIN_INACTIVE_OUTPUT_LOW = 1, + M16C_PIN_INACTIVE_OUTPUT_HIGH = 2, + M16C_PIN_INACTIVE_INPUT = 3 +}; + +#define M16C_PORT_INACTIVE_STATE(p0, p1, p2, p3, p4, p5, p6, p7) p0 | (p1 << 2) | \ + (p2 << 4) | (p3 << 6) | \ + (p4 << 8) | (p5 << 10) | \ + (p6 << 12) | (p7 << 14) + +#endif // __M16C62P_PIN_H__ diff --git a/tos/chips/m16c62p/printf/elib.c b/tos/chips/m16c62p/printf/elib.c new file mode 100644 index 00000000..7106e1cd --- /dev/null +++ b/tos/chips/m16c62p/printf/elib.c @@ -0,0 +1,491 @@ +/**************************************************************** + KPIT Cummins Infosystems Ltd, Pune, India. 1-April-2006. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + *****************************************************************/ + +/* + Written By: + Shrirang Khishti . + + This is a smaller version of printf + Positive points about this function + 1. Reduces code size considerably ,very useful in embedded applications + 2. No malloc calls are used + 3. Supports almost all the functionalities of GNU std printf routine. + 4. If user dont want float_support in this customized printf + just undef macro float_support + */ + +#include +#include + + +int left_val,right_val; + +#define condition *format!='f'&&*format!='d'&&*format!='c'&&*format!='s'&&*format!='l'&&*format!='u'&&*format!='\0'&&*format!=' '&&*format!='i'&&*format!='x'&&*format!='X'&&*format!='o'&&*format!='%'&&*format!='p' + +#define float_support + +long temp_arr[]={100000,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; + + +/** + * @fn void shutdown(void) + * @brief Shutsdown the MCU by placing it in an eternal loop + * + */ +void +shutdown(void) +{ + for(;;) + ; +} + + +/** + * @fn + * @brief + * + * @param c + */ +int +_putchar(int c) +{ + /* Convert CR to CR/LF */ + if (c == '\n') + lowlevel_putc('\r'); + lowlevel_putc(c); + + return c; +} + + +/** + * @fn void _puts(const char *tempStr) + * @brief Prints a NULL-erminated string on UART 1 + * + * @param s The string to output + * + */ +int +_puts(const char *s) +{ + while( *s != '\0' ) + _putchar(*s++); + + _putchar('\n'); + + return 0; +} + + +/** + * @fn void strrev(char *str) + * @brief Reverses a string + * + * @param str The string to reverse + */ +void +strrev(char *str) +{ + char *temp, c; + int len=strlen(str) ; + temp = str + len -1; + + while(str < temp ) { + + c = *str; + *str = *temp; + + *temp = c; + str++; + temp--; + } +} + +static void print_hex_oct( long int temp_var,int div,int corr_factor,int ret_val,int sign,int *cntr_val) +{ + unsigned long int i=1,i1,temp=temp_var; + int cntr=0,neg_flag=0; + char s1[40]; + + if(sign==1&&temp_var<0) + { + temp=-temp_var; + neg_flag=1; + } + if(temp==0) + s1[cntr++]='0'; + while(temp>0) + { + i1=temp%div; + temp=temp/div; + if(i1<=9) + s1[cntr]=i1+'0'; + else + s1[cntr]=i1+corr_factor-9; + cntr++; + } + + while((left_val-(right_val>cntr?right_val:cntr+neg_flag))>0) + { + _putchar(' '); + left_val--; + (*cntr_val)++; + } + + while(right_val-cntr>0) + { + s1[cntr++]='0'; + } + + if(neg_flag==1) + s1[cntr++]='-'; + + s1[cntr]='\0'; + strrev(s1); + _puts(s1); + (*cntr_val)+=strlen(s1); +} + + +#ifdef float_support +static void float_print(long double f1,long double f2,int multi,int *cntr_val) +{ + int i=1,temp,cntr=0,i1,neg_flag=0; + char s1[10]; + + if(f1<0) + { + f1=f1*-1; + neg_flag=1; + f2=f1; + } + temp=(int)f1; + + f1=f1-temp; + f1=f1*multi; + + temp=f1; + + if(temp==0) + s1[cntr++]='0'; + while(temp>0) + { + i1=temp%10; + temp=temp/10; + s1[cntr]=i1+0x30; + cntr++; + } + + while(right_val<9&&(right_val -cntr)>0) + s1[cntr++]='0'; + s1[cntr]='.'; + cntr++; + + temp=(int)f2; + if(temp==0) + s1[cntr++]='0'; + while(temp>0) + { + i1=temp%10; + temp=temp/10; + s1[cntr]=i1+0x30; + cntr++; + } + + while(left_val-- -cntr>0) + { + _putchar(' '); + (*cntr_val)++; + } + if(neg_flag==1) + s1[cntr++]='-'; + s1[cntr]='\0'; + cntr--; + strrev(s1); + _puts(s1); + (*cntr_val)+=strlen(s1); + neg_flag=0; +} + +#endif // float_support + +static int format_val(char *temp,long float_flag,int *cntr_val,int flag) +{ +left_val=0; +right_val=0; + if(*temp=='\0'&&flag==1) + { + right_val=3; + return 0; + } + while(*temp!='.'&&*temp!='\0') + { + if(*temp<'0'||*temp>'9') + { + while(*temp) + { + _putchar(*temp++); + (*cntr_val)++; + } + return -1; + } + else + left_val=left_val*10+*temp-'0'; + temp++; + } + if(*temp) + temp++; + else + return left_val; + while(*temp) + { + if(*temp<'0'||*temp>'9') + { + while(*temp) + { + _putchar(*temp++); + (*cntr_val)++; + } + return -1; + } + else + right_val=right_val*10+*temp-'0'; + temp++; + } + +return 0; +} + + +/** + * @fn int _printf(const char *format, ...) + * @brief Prints a formatted string on UART1 + * + * @param format The string + */ +int _printf(const char *format, ...) +{ + int format_cntr=0; + char temp_str[20]; + int return_flag=0,cntr_val; + long double f1,f2; + char *str_temp; + int *cntr=&cntr_val; + + va_list ap; + va_start(ap, format); + *cntr=0; + while(*format) { + temp_str[format_cntr]='\0'; + if(*format=='%') + { + *format++; + while(*format==' ') + { + format++; + _putchar(' '); + } + while(condition) + + { + temp_str[format_cntr++]=*format++; + } + temp_str[format_cntr]='\0'; + if(*format=='%') + { + _putchar('%'); + (*cntr)++; + format_cntr=0; + format++; + continue; + } + + /************** print unsigned ****************/ + else if(*format=='u') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) + + print_hex_oct(va_arg(ap,unsigned int),10,0,return_flag,0,cntr); + + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + } + /*********** Print Integer Values **************/ + else if(*format=='d'||*format=='i') + { + return_flag=format_val(temp_str,0,cntr,0); + + if(return_flag!=-1) + + print_hex_oct(va_arg(ap,int),10,0,return_flag,1,cntr); + + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + + } + + /*********** Print hex,Octal values ******************/ + else if(*format=='x'||*format=='X'||*format=='o'||*format=='p') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) { + + if(*format=='x'||*format=='p') + print_hex_oct(va_arg(ap,unsigned int),16,0x60,return_flag,0,cntr); + else if(*format=='X') + print_hex_oct(va_arg(ap,unsigned int),16,0x40,return_flag,0,cntr); + else + print_hex_oct(va_arg(ap,unsigned int),8,0,return_flag,0,cntr); + } + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + } + + /************ Character printing ****************88*/ + else if(*format=='c') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) + { + while(return_flag-->1) + { + _putchar(' '); + (*cntr)++; + } + _putchar(va_arg(ap,int)); + (*cntr)+=2; + } + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + } + + /*************** Print String *****************/ + else if(*format=='s') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) + { + str_temp=va_arg(ap,char*); + + while((return_flag-- -(int) strlen(str_temp))>0) + { + _putchar(' '); + (*cntr)++; + + } + _puts(str_temp); + (*cntr)+=strlen(str_temp); + } + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + + } + /*************** Print floating point number *****************/ + else if(*format=='f'||(*format=='l'&&*(format+1)=='f')) + { + + return_flag=format_val(temp_str,1,cntr,1); + if(return_flag!=-1) + { + if(*format=='l') + { + f1=va_arg(ap,long double); + format+=2; + } + else + { + f1=va_arg(ap,double); + format++; + } + f2=f1; +#ifdef float_support + right_val++; + float_print(f1,f2,temp_arr[right_val%10],cntr); +#endif + } + else + { + _putchar(*format++); + (*cntr)++; + } + format_cntr=0; + continue; + } + else if(*format=='l'&&((*(format+1)=='d')||(*(format+1)=='u'))) + { + return_flag=format_val(temp_str,0,cntr,0); + + if((return_flag=-1)&&(*(format+1)=='d')) + { + print_hex_oct(va_arg(ap,long int),10,0x00,return_flag,1,cntr); + } + + else if((return_flag=-1)&&(*(format+1)=='u')) + { + print_hex_oct(va_arg(ap,unsigned long int),10,0x00,return_flag,0,cntr); + } + + else + { + _putchar(*format); + _putchar(*(format+1)); + (*cntr)+=2; + } + format+=2; + format_cntr=0; + continue; + } + else + { + _puts(temp_str); + format_cntr=0; + continue; + } + } + _putchar(*format++); + + (*cntr)++; + } + va_end(ap); + return cntr_val; +} + + + diff --git a/tos/chips/m16c62p/printf/m16c62p_printf.h b/tos/chips/m16c62p/printf/m16c62p_printf.h new file mode 100644 index 00000000..96a8d9d6 --- /dev/null +++ b/tos/chips/m16c62p/printf/m16c62p_printf.h @@ -0,0 +1,481 @@ +/**************************************************************** + KPIT Cummins Infosystems Ltd, Pune, India. 1-April-2006. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + *****************************************************************/ + +/* + Written By: + Shrirang Khishti . + + This is a smaller version of printf + Positive points about this function + 1. Reduces code size considerably ,very useful in embedded applications + 2. No malloc calls are used + 3. Supports almost all the functionalities of GNU std printf routine. + 4. If user dont want float_support in this customized printf + just undef macro float_support + */ + +#ifndef __M16C62P_PRINTF_H__ +#define __M16C62P_PRINTF_H__ +#include +#include + +#define printf _printf + +int left_val,right_val; + +#define condition *format!='f'&&*format!='d'&&*format!='c'&&*format!='s'&&*format!='l'&&*format!='u'&&*format!='\0'&&*format!=' '&&*format!='i'&&*format!='x'&&*format!='X'&&*format!='o'&&*format!='%'&&*format!='p' + +#define float_support + +long temp_arr[]={100000,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; + + +/** + * @fn + * @brief + * + * @param c + */ +int +_putchar(int c) +{ + /* Convert CR to CR/LF */ + if (c == '\n') + lowlevel_putc('\r'); + lowlevel_putc(c); + + return c; +} + + +/** + * @fn void _puts(const char *tempStr) + * @brief Prints a NULL-erminated string on UART 1 + * + * @param s The string to output + * + */ +int +_puts(const char *s) +{ + while( *s != '\0' ) + _putchar(*s++); + + return 0; +} + + +/** + * @fn void strrev(char *str) + * @brief Reverses a string + * + * @param str The string to reverse + */ +void +strrev(char *str) +{ + char *temp, c; + int len=strlen(str) ; + temp = str + len -1; + + while(str < temp ) { + + c = *str; + *str = *temp; + + *temp = c; + str++; + temp--; + } +} + + +static void print_hex_oct( long int temp_var,int _div,int corr_factor,int ret_val,int sign,int *cntr_val) +{ + unsigned long int i=1,i1,temp=temp_var; + int cntr=0,neg_flag=0; + char s1[40]; + + if(sign==1&&temp_var<0) + { + temp=-temp_var; + neg_flag=1; + } + if(temp==0) + s1[cntr++]='0'; + while(temp>0) + { + i1=temp%_div; + temp=temp/_div; + if(i1<=9) + s1[cntr]=i1+'0'; + else + s1[cntr]=i1+corr_factor-9; + cntr++; + } + + while((left_val-(right_val>cntr?right_val:cntr+neg_flag))>0) + { + _putchar(' '); + left_val--; + (*cntr_val)++; + } + + while(right_val-cntr>0) + { + s1[cntr++]='0'; + } + + if(neg_flag==1) + s1[cntr++]='-'; + + s1[cntr]='\0'; + strrev(s1); + _puts(s1); + (*cntr_val)+=strlen(s1); +} + + +#ifdef float_support +static void float_print(long double f1,long double f2,int multi,int *cntr_val) +{ + int i=1,temp,cntr=0,i1,neg_flag=0; + char s1[10]; + + if(f1<0) + { + f1=f1*-1; + neg_flag=1; + f2=f1; + } + temp=(int)f1; + + f1=f1-temp; + f1=f1*multi; + + temp=f1; + + if(temp==0) + s1[cntr++]='0'; + while(temp>0) + { + i1=temp%10; + temp=temp/10; + s1[cntr]=i1+0x30; + cntr++; + } + + while(right_val<9&&(right_val -cntr)>0) + s1[cntr++]='0'; + s1[cntr]='.'; + cntr++; + + temp=(int)f2; + if(temp==0) + s1[cntr++]='0'; + while(temp>0) + { + i1=temp%10; + temp=temp/10; + s1[cntr]=i1+0x30; + cntr++; + } + + while(left_val-- -cntr>0) + { + _putchar(' '); + (*cntr_val)++; + } + if(neg_flag==1) + s1[cntr++]='-'; + s1[cntr]='\0'; + cntr--; + strrev(s1); + _puts(s1); + (*cntr_val)+=strlen(s1); + neg_flag=0; +} + +#endif // float_support + +static int format_val(char *temp,long float_flag,int *cntr_val,int flag) +{ +left_val=0; +right_val=0; + if(*temp=='\0'&&flag==1) + { + right_val=3; + return 0; + } + while(*temp!='.'&&*temp!='\0') + { + if(*temp<'0'||*temp>'9') + { + while(*temp) + { + _putchar(*temp++); + (*cntr_val)++; + } + return -1; + } + else + left_val=left_val*10+*temp-'0'; + temp++; + } + if(*temp) + temp++; + else + return left_val; + while(*temp) + { + if(*temp<'0'||*temp>'9') + { + while(*temp) + { + _putchar(*temp++); + (*cntr_val)++; + } + return -1; + } + else + right_val=right_val*10+*temp-'0'; + temp++; + } + +return 0; +} + + +/** + * @fn int _printf(const char *format, ...) + * @brief Prints a formatted string on UART1 + * + * @param format The string + */ +int _printf(const char *format, ...) +{ + int format_cntr=0; + char temp_str[20]; + int return_flag=0,cntr_val; + long double f1,f2; + char *str_temp; + int *cntr=&cntr_val; + + va_list ap; + va_start(ap, format); + *cntr=0; + while(*format) { + temp_str[format_cntr]='\0'; + if(*format=='%') + { + *format++; + while(*format==' ') + { + format++; + _putchar(' '); + } + while(condition) + + { + temp_str[format_cntr++]=*format++; + } + temp_str[format_cntr]='\0'; + if(*format=='%') + { + _putchar('%'); + (*cntr)++; + format_cntr=0; + format++; + continue; + } + + /************** print unsigned ****************/ + else if(*format=='u') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) + + print_hex_oct(va_arg(ap,unsigned int),10,0,return_flag,0,cntr); + + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + } + /*********** Print Integer Values **************/ + else if(*format=='d'||*format=='i') + { + return_flag=format_val(temp_str,0,cntr,0); + + if(return_flag!=-1) + + print_hex_oct(va_arg(ap,int),10,0,return_flag,1,cntr); + + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + + } + + /*********** Print hex,Octal values ******************/ + else if(*format=='x'||*format=='X'||*format=='o'||*format=='p') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) { + + if(*format=='x'||*format=='p') + print_hex_oct(va_arg(ap,unsigned int),16,0x60,return_flag,0,cntr); + else if(*format=='X') + print_hex_oct(va_arg(ap,unsigned int),16,0x40,return_flag,0,cntr); + else + print_hex_oct(va_arg(ap,unsigned int),8,0,return_flag,0,cntr); + } + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + } + + /************ Character printing ****************88*/ + else if(*format=='c') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) + { + while(return_flag-->1) + { + _putchar(' '); + (*cntr)++; + } + _putchar(va_arg(ap,int)); + (*cntr)+=2; + } + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + } + + /*************** Print String *****************/ + else if(*format=='s') + { + return_flag=format_val(temp_str,0,cntr,0); + if(return_flag!=-1) + { + str_temp=va_arg(ap,char*); + + while((return_flag-- -(int) strlen(str_temp))>0) + { + _putchar(' '); + (*cntr)++; + + } + _puts(str_temp); + (*cntr)+=strlen(str_temp); + } + else + { + _putchar(*format); + (*cntr)++; + } + format++; + format_cntr=0; + continue; + + } + /*************** Print floating point number *****************/ + else if(*format=='f'||(*format=='l'&&*(format+1)=='f')) + { + + return_flag=format_val(temp_str,1,cntr,1); + if(return_flag!=-1) + { + if(*format=='l') + { + f1=va_arg(ap,long double); + format+=2; + } + else + { + f1=va_arg(ap,double); + format++; + } + f2=f1; +#ifdef float_support + right_val++; + float_print(f1,f2,temp_arr[right_val%10],cntr); +#endif + } + else + { + _putchar(*format++); + (*cntr)++; + } + format_cntr=0; + continue; + } + else if(*format=='l'&&((*(format+1)=='d')||(*(format+1)=='u'))) + { + return_flag=format_val(temp_str,0,cntr,0); + + if((return_flag=-1)&&(*(format+1)=='d')) + { + print_hex_oct(va_arg(ap,long int),10,0x00,return_flag,1,cntr); + } + + else if((return_flag=-1)&&(*(format+1)=='u')) + { + print_hex_oct(va_arg(ap,unsigned long int),10,0x00,return_flag,0,cntr); + } + + else + { + _putchar(*format); + _putchar(*(format+1)); + (*cntr)+=2; + } + format+=2; + format_cntr=0; + continue; + } + else + { + _puts(temp_str); + format_cntr=0; + continue; + } + } + _putchar(*format++); + + (*cntr)++; + } + va_end(ap); + return cntr_val; +} + + +#endif // __M16C62P_PRINTF_H__ + diff --git a/tos/chips/m16c62p/uart/HplM16c62pUart.nc b/tos/chips/m16c62p/uart/HplM16c62pUart.nc index 40d9b0d1..5df085d5 100755 --- a/tos/chips/m16c62p/uart/HplM16c62pUart.nc +++ b/tos/chips/m16c62p/uart/HplM16c62pUart.nc @@ -72,8 +72,18 @@ * @author Alec Woo * @author Jonathan Hui */ + +#include "M16c62pUart.h" + interface HplM16c62pUart { - + async command void on(); + async command void off(); + async command error_t setSpeed(uart_speed_t speed); + async command uart_speed_t getSpeed(); + async command void setParity(uart_parity_t parity); + async command uart_parity_t getParity(); + async command void setStopBits(uart_stop_bits_t stop_bits); + async command uart_stop_bits_t getStopBits(); async command error_t enableTxInterrupt(); async command error_t disableTxInterrupt(); async command error_t enableRxInterrupt(); diff --git a/tos/chips/m16c62p/uart/HplM16c62pUartC.nc b/tos/chips/m16c62p/uart/HplM16c62pUartC.nc index 9e060670..c549e11f 100755 --- a/tos/chips/m16c62p/uart/HplM16c62pUartC.nc +++ b/tos/chips/m16c62p/uart/HplM16c62pUartC.nc @@ -68,16 +68,16 @@ configuration HplM16c62pUartC { provides { - interface StdControl as Uart0TxControl; - interface StdControl as Uart0RxControl; + interface AsyncStdControl as Uart0TxControl; + interface AsyncStdControl as Uart0RxControl; interface HplM16c62pUart as HplUart0; - interface StdControl as Uart1TxControl; - interface StdControl as Uart1RxControl; + interface AsyncStdControl as Uart1TxControl; + interface AsyncStdControl as Uart1RxControl; interface HplM16c62pUart as HplUart1; - interface StdControl as Uart2TxControl; - interface StdControl as Uart2RxControl; + interface AsyncStdControl as Uart2TxControl; + interface AsyncStdControl as Uart2RxControl; interface HplM16c62pUart as HplUart2; } } @@ -142,9 +142,4 @@ implementation HplUart2P.Irq -> Irqs.Uart2; HplUart2P.StopModeControl -> Uart2StopModeControl; - components MainC; - MainC.SoftwareInit -> HplUart0P.UartInit; - MainC.SoftwareInit -> HplUart1P.UartInit; - MainC.SoftwareInit -> HplUart2P.UartInit; - } diff --git a/tos/chips/m16c62p/uart/HplM16c62pUartP.nc b/tos/chips/m16c62p/uart/HplM16c62pUartP.nc index 77447c9d..3059a5fb 100755 --- a/tos/chips/m16c62p/uart/HplM16c62pUartP.nc +++ b/tos/chips/m16c62p/uart/HplM16c62pUartP.nc @@ -101,6 +101,9 @@ * * @author Henrik Makitaavola */ + +#include "M16c62pUart.h" + generic module HplM16c62pUartP(uint8_t uartNr, uint16_t tx_addr, uint16_t rx_addr, @@ -111,9 +114,8 @@ generic module HplM16c62pUartP(uint8_t uartNr, uint16_t txInterrupt_addr, uint16_t rxInterrupt_addr) { - provides interface Init as UartInit; - provides interface StdControl as UartTxControl; - provides interface StdControl as UartRxControl; + provides interface AsyncStdControl as UartTxControl; + provides interface AsyncStdControl as UartRxControl; provides interface HplM16c62pUart as HplUart; uses interface GeneralIO as TxIO; @@ -133,39 +135,167 @@ implementation #define ctrl0 (*TCAST(volatile uint8_t* ONE, ctrl0_addr)) #define ctrl1 (*TCAST(volatile uint8_t* ONE, ctrl1_addr)) - command error_t UartInit.init() + enum { + ON, + OFF + }; + + uint8_t state = OFF; + uart_speed_t current_speed = TOS_UART_57600; + + async command void HplUart.on() { - // Divide the mainclock get baudrate. - brg = (uint8_t)(((MAIN_CRYSTAL_SPEED * 1000000.0 / (16.0 * 57600.0))+ 0.5) - 1.0); - mode = BIT0 | BIT2; // Set 8 bit transfer length, 1 stop bit, no parity. - ctrl0 = BIT4; // set f1, no cts/rts. - + // Set 8 bit transfer + SET_BIT(mode, 0); + SET_BIT(mode, 2); + + //no cts/rts. + SET_BIT(ctrl0, 4); + atomic switch (current_speed) + { + case TOS_UART_1200: + SET_BIT(ctrl0, 0); + CLR_BIT(ctrl0, 1); + break; + case TOS_UART_9600: + case TOS_UART_57600: + CLR_BIT(ctrl0, 0); + CLR_BIT(ctrl0, 1); + break; + default: + break; + } + call StopModeControl.allowStopMode(false); + atomic state = ON; + } + + async command void HplUart.off() + { + CLR_BIT(mode, 0); + CLR_BIT(mode, 2); + call StopModeControl.allowStopMode(true); + atomic state = OFF; + } + + + async command error_t HplUart.setSpeed(uart_speed_t speed) + { + atomic if (state != OFF) + { + return FAIL; + } + + switch (speed) + { + // TODO(henrik) These values are based on a mcu that runs on MAIN_CRYSTAL_SPEED and doesn't + // consider if the PLL is on which they should. + case TOS_UART_1200: + brg = (uint8_t)(((MAIN_CRYSTAL_SPEED * 1000000.0 / (128.0 * 1200.0))+ 0.5) - 1.0); + break; + case TOS_UART_9600: + brg = (uint8_t)(((MAIN_CRYSTAL_SPEED * 1000000.0 / (16.0 * 9600.0))+ 0.5) - 1.0); + break; + case TOS_UART_57600: + brg = (uint8_t)(((MAIN_CRYSTAL_SPEED * 1000000.0 / (16.0 * 57600.0))+ 0.5) - 1.0); + break; + default: + break; + } + atomic current_speed = speed; return SUCCESS; } + + async command uart_speed_t HplUart.getSpeed() + { + atomic return current_speed; + } + + async command void HplUart.setParity(uart_parity_t parity) + { + switch (parity) + { + case TOS_UART_PARITY_NONE: + CLR_BIT(mode, 6); + break; + case TOS_UART_PARITY_EVEN: + SET_BIT(mode, 6); + SET_BIT(mode, 5); + break; + case TOS_UART_PARITY_ODD: + SET_BIT(mode, 6); + CLR_BIT(mode, 5); + break; + default: + break; + } + } + + async command uart_parity_t HplUart.getParity() + { + if (READ_BIT(mode, 6) && READ_BIT(mode, 5)) + { + return TOS_UART_PARITY_EVEN; + } + else if (READ_BIT(mode, 6)) + { + return TOS_UART_PARITY_ODD; + } + else + { + return TOS_UART_PARITY_NONE; + } + } + + async command void HplUart.setStopBits(uart_stop_bits_t stop_bits) + { + switch (stop_bits) + { + case TOS_UART_STOP_BITS_1: + CLR_BIT(mode, 4); + break; + case TOS_UART_STOP_BITS_2: + SET_BIT(mode, 4); + break; + default: + break; + } + } + + async command uart_stop_bits_t HplUart.getStopBits() + { + if (READ_BIT(mode, 4)) + { + return TOS_UART_STOP_BITS_2; + } + else + { + return TOS_UART_STOP_BITS_1; + } + } + - command error_t UartTxControl.start() + async command error_t UartTxControl.start() { call TxIO.makeOutput(); SET_BIT(ctrl1, 0); - call StopModeControl.allowStopMode(false); return SUCCESS; } - command error_t UartTxControl.stop() + async command error_t UartTxControl.stop() { call TxIO.makeInput(); CLR_BIT(ctrl1, 0); - call StopModeControl.allowStopMode(true); return SUCCESS; } - command error_t UartRxControl.start() + async command error_t UartRxControl.start() { + call RxIO.makeInput(); SET_BIT(ctrl1, 2); return SUCCESS; } - command error_t UartRxControl.stop() + async command error_t UartRxControl.stop() { CLR_BIT(ctrl1, 2); return SUCCESS; diff --git a/tos/chips/m16c62p/uart/M16c62pUart.h b/tos/chips/m16c62p/uart/M16c62pUart.h index 7edb9413..23fa284d 100644 --- a/tos/chips/m16c62p/uart/M16c62pUart.h +++ b/tos/chips/m16c62p/uart/M16c62pUart.h @@ -45,8 +45,9 @@ typedef enum { - TOS_UART_1200 = 0, - TOS_UART_57600 = 7, + TOS_UART_1200, + TOS_UART_9600, + TOS_UART_57600, } uart_speed_t; typedef enum { diff --git a/tos/chips/m16c62p/uart/M16c62pUartC.nc b/tos/chips/m16c62p/uart/M16c62pUartC.nc index 40013d62..6c2fb1c6 100755 --- a/tos/chips/m16c62p/uart/M16c62pUartC.nc +++ b/tos/chips/m16c62p/uart/M16c62pUartC.nc @@ -76,17 +76,17 @@ */ configuration M16c62pUartC { - provides interface StdControl as StdControl0; provides interface UartByte as Uart0Byte; provides interface UartStream as Uart0Stream; + provides interface UartControl as Uart0Control; - provides interface StdControl as StdControl1; provides interface UartByte as Uart1Byte; provides interface UartStream as Uart1Stream; + provides interface UartControl as Uart1Control; - provides interface StdControl as StdControl2; provides interface UartByte as Uart2Byte; provides interface UartStream as Uart2Stream; + provides interface UartControl as Uart2Control; uses interface Counter; } @@ -96,9 +96,9 @@ implementation components HplM16c62pUartC as HplUartC; components new M16c62pUartP() as Uart0P; - StdControl0 = Uart0P; Uart0Byte = Uart0P; Uart0Stream = Uart0P; + Uart0Control = Uart0P; Uart0P.Counter = Counter; Uart0P.HplUartTxControl -> HplUartC.Uart0TxControl; @@ -106,9 +106,9 @@ implementation Uart0P.HplUart -> HplUartC.HplUart0; components new M16c62pUartP() as Uart1P; - StdControl1 = Uart1P; Uart1Byte = Uart1P; Uart1Stream = Uart1P; + Uart1Control = Uart1P; Uart1P.Counter = Counter; Uart1P.HplUartTxControl -> HplUartC.Uart1TxControl; @@ -116,18 +116,13 @@ implementation Uart1P.HplUart -> HplUartC.HplUart1; components new M16c62pUartP() as Uart2P; - StdControl2 = Uart2P; Uart2Byte = Uart2P; Uart2Stream = Uart2P; + Uart2Control = Uart2P; Uart2P.Counter = Counter; Uart2P.HplUartTxControl -> HplUartC.Uart2TxControl; Uart2P.HplUartRxControl -> HplUartC.Uart2RxControl; Uart2P.HplUart -> HplUartC.HplUart2; - - components MainC; - MainC.SoftwareInit -> Uart0P; - MainC.SoftwareInit -> Uart1P; - MainC.SoftwareInit -> Uart2P; } diff --git a/tos/chips/m16c62p/uart/M16c62pUartP.nc b/tos/chips/m16c62p/uart/M16c62pUartP.nc index e966d6a5..9a630330 100755 --- a/tos/chips/m16c62p/uart/M16c62pUartP.nc +++ b/tos/chips/m16c62p/uart/M16c62pUartP.nc @@ -76,67 +76,37 @@ */ #include +#include "M16c62pUart.h" generic module M16c62pUartP() { - provides interface Init; - provides interface StdControl; provides interface UartByte; provides interface UartStream; + provides interface UartControl; - uses interface StdControl as HplUartTxControl; - uses interface StdControl as HplUartRxControl; + uses interface AsyncStdControl as HplUartTxControl; + uses interface AsyncStdControl as HplUartRxControl; uses interface HplM16c62pUart as HplUart; uses interface Counter; } implementation -{ - +{ norace uint16_t m_tx_len, m_rx_len; norace uint8_t *m_tx_buf, *m_rx_buf; norace uint16_t m_tx_pos, m_rx_pos; - norace uint16_t m_byte_time; + norace uint16_t m_byte_time = 68; norace uint8_t m_rx_intr; norace uint8_t m_tx_intr; - - command error_t Init.init() - { - // TODO(henrik) fix this for the different uarts. - /*if (PLATFORM_BAUDRATE == 19200UL) - m_byte_time = 200; // 1 TMicor ~= 2.12 us, one byte = 417us ~= 200 - else if (PLATFORM_BAUDRATE == 57600UL) - m_byte_time = 68; // 1 TMicor ~= 2.12 us, one byte = 138us ~= 65*/ - m_byte_time = 68; - return SUCCESS; - } - - command error_t StdControl.start() - { - /* make sure interrupts are off and set flags */ - call HplUart.disableTxInterrupt(); - call HplUart.disableRxInterrupt(); - m_rx_intr = 0; - m_tx_intr = 0; - - /* enable tx/rx */ - call HplUartTxControl.start(); - call HplUartRxControl.start(); - - // Bug fix: pal 11/26/07: RX interrupts should be enabled on start - call HplUart.enableRxInterrupt(); - return SUCCESS; - } - - command error_t StdControl.stop() - { - call HplUartTxControl.stop(); - call HplUartRxControl.stop(); - return SUCCESS; - } + uart_duplex_t mode = TOS_UART_OFF; async command error_t UartStream.enableReceiveInterrupt() { + if (mode == TOS_UART_TONLY) + { + return FAIL; + } + atomic { m_rx_intr = 3; @@ -147,6 +117,10 @@ implementation async command error_t UartStream.disableReceiveInterrupt() { + if (mode == TOS_UART_TONLY) + { + return FAIL; + } atomic { call HplUart.disableRxInterrupt(); @@ -157,6 +131,10 @@ implementation async command error_t UartStream.receive( uint8_t* buf, uint16_t len ) { + if (mode == TOS_UART_TONLY) + { + return FAIL; + } if ( len == 0 ) return FAIL; @@ -204,7 +182,10 @@ implementation async command error_t UartStream.send( uint8_t *buf, uint16_t len) { - + if (mode == TOS_UART_RONLY) + { + return FAIL; + } if ( len == 0 ) return FAIL; else if ( m_tx_buf ) @@ -241,6 +222,10 @@ implementation async command error_t UartByte.send( uint8_t byte ) { + if (mode == TOS_UART_RONLY) + { + return FAIL; + } if(m_tx_intr) return FAIL; @@ -251,10 +236,14 @@ implementation async command error_t UartByte.receive( uint8_t * byte, uint8_t timeout) { - uint16_t timeout_micro = m_byte_time * timeout + 1; uint16_t start; + if (mode == TOS_UART_TONLY) + { + return FAIL; + } + if(m_rx_intr) return FAIL; @@ -270,6 +259,115 @@ implementation } + async command error_t UartControl.setSpeed(uart_speed_t speed) + { + return call HplUart.setSpeed(speed); + } + + async command uart_speed_t UartControl.speed() + { + return call HplUart.getSpeed(); + } + + async command error_t UartControl.setDuplexMode(uart_duplex_t duplex) + { + if (mode == TOS_UART_OFF) + { + call HplUart.disableTxInterrupt(); + call HplUart.disableRxInterrupt(); + m_rx_intr = 0; + m_tx_intr = 0; + } + switch (duplex) + { + case TOS_UART_OFF: + call HplUart.disableTxInterrupt(); + call HplUart.disableRxInterrupt(); + call HplUartTxControl.stop(); + call HplUartRxControl.stop(); + call HplUart.off(); + return SUCCESS; + break; + case TOS_UART_RONLY: + call HplUart.disableTxInterrupt(); + call HplUartTxControl.stop(); + call HplUart.on(); + call HplUartRxControl.start(); + call HplUart.enableRxInterrupt(); + break; + case TOS_UART_TONLY: + call HplUart.disableRxInterrupt(); + call HplUartRxControl.stop(); + call HplUart.on(); + call HplUartTxControl.start(); + break; + case TOS_UART_DUPLEX: + call HplUart.on(); + call HplUartTxControl.start(); + call HplUartRxControl.start(); + call HplUart.enableRxInterrupt(); + break; + default: + break; + } + + return SUCCESS; + } + + async command uart_duplex_t UartControl.duplexMode() + { + atomic return mode; + } + + async command error_t UartControl.setParity(uart_parity_t parity) + { + if (mode != TOS_UART_OFF) + { + return FAIL; + } + call HplUart.setParity(parity); + return SUCCESS; + } + + async command uart_parity_t UartControl.parity() + { + return call HplUart.getParity(); + } + + async command error_t UartControl.setStop() + { + if (mode != TOS_UART_OFF) + { + return FAIL; + } + call HplUart.setStopBits(TOS_UART_STOP_BITS_2); + return SUCCESS; + } + + async command error_t UartControl.setNoStop() + { + if (mode != TOS_UART_OFF) + { + return FAIL; + } + call HplUart.setStopBits(TOS_UART_STOP_BITS_1); + return SUCCESS; + } + + async command bool UartControl.stopBits() + { + if (call HplUart.getStopBits() == TOS_UART_STOP_BITS_2) + { + return true; + } + else + { + return false; + } + } + + + async event void Counter.overflow() {} default async event void UartStream.sendDone( uint8_t* buf, uint16_t len, error_t error ){}