annotate uart_addon.c @ 332:05c1249da994

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