annotate uart_addon.c @ 11:06bedbe8540d

all these optimisations make it 30% smaller
author Matt Johnston <matt@ucc.asn.au>
date Sat, 19 May 2012 17:15:50 +0800
parents 7da9a3f23592
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 Title: UART addon-library
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 Author: Martin Thomas <[email protected]>
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 http://www.siwawi.arubi.uni-kl.de/avr_projects
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 Software: AVR-GCC 3.3/3.4, Peter Fleury's UART-Library
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 DESCRIPTION:
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 USAGE:
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 Refere to the header file uart_addon.h for a description of the routines.
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 *************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #include <stdlib.h>
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #include <avr/io.h>
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 #include "uart.h"
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 Function: uart_put_int()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 Purpose: transmit integer as ASCII to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 Input: integer value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 void uart_put_int( const int val )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 char buffer[10];
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 uart_puts( itoa( val, buffer, 10 ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 } /* uart_puti */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 Function: uart_put_longint()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 Purpose: transmit long integer as ASCII to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 Input: integer value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 void uart_put_longint( const long int val )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 char buffer[15];
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 uart_puts( ltoa( val, buffer, 10 ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 } /* uart_puti */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 Function: uart_put_ulongint()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 Purpose: transmit long integer as ASCII to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 Input: integer value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 void uart_put_ulongint( const unsigned long int val )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 char buffer[15];
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 uart_puts( utoa( val, buffer, 10 ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 } /* uart_puti */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 Function: uart_puthex_nibble()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 Purpose: transmit lower nibble as ASCII-hex to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 Input: byte value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 void uart_puthex_nibble(const unsigned char b)
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 unsigned char c = b & 0x0f;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 if ( c > 9 ) {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 c += 'A'-10;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 else {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 c += '0';
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 uart_putc(c);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 } /* uart_puthex_nibble */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 Function: uart_puthex_byte()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 Purpose: transmit upper and lower nibble as ASCII-hex to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 Input: byte value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 void uart_puthex_byte( const unsigned char b )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 uart_puthex_nibble( b >> 4 );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 uart_puthex_nibble( b );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 } /* uart_puthex_byte */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 Function: uart_puthex_long()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 Purpose: transmit unsigned long as ASCII-hex to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 Input: uint32_t value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 void uart_puthex_long( const unsigned long l )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 uart_puthex_byte( (unsigned char)( l >> 24 ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 uart_puthex_byte( (unsigned char)( l >> 16 ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 uart_puthex_byte( (unsigned char)( l >> 8 ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 uart_puthex_byte( (unsigned char)( l ) );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 } /* uart_puthex_byte */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 /*************************************************************************
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 Function: uart_putbin_byte()
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 Purpose: transmit byte as ASCII-bin to UART
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 Input: byte value
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 Returns: none
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 **************************************************************************/
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 void uart_putbin_byte( const unsigned char b )
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 signed char i;
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 for ( i= 7;i >= 0;i-- ) {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 if ( b & ( 1 << i ) ) {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 uart_putc( '1' );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 else {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 uart_putc( '0' );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 } /* uart_putbin_byte */