Mercurial > avr_jtag
annotate src/avriotools.c @ 1:f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 21 Feb 2009 20:20:06 +0800 |
parents | a0ce8ebf2f18 |
children |
rev | line source |
---|---|
0
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 #include "avriotools.h" |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
2 #include <avr/io.h> |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 typedef struct _port_regs { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 volatile uint8_t *port, *ddr, *pin; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
6 } port_regs_t; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
8 static port_regs_t regs[] = { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 { &PORTB, &DDRB, &PINB }, |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
10 { &PORTD, &DDRD, &PIND } |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 }; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 #define NPORTS (sizeof(regs) / sizeof(*regs)) |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 #define ERR -1 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 #define OK 0 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
17 int pin_mode(volatile uint8_t * port, int pin, pin_mode_t mode) { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 port_regs_t *preg; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 int i; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 for(i = 0; i < NPORTS; i++) { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 preg = regs + i; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
23 if(preg->port == port) |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 break; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
25 } |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 if(i == NPORTS) |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 return ERR; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 switch(mode) { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
30 case PM_INPUT: |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
31 *preg->ddr &= ~_BV(pin); |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 *preg->port |= _BV(pin); |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 break; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 case PM_OUTPUT: |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 *preg->ddr |= _BV(pin); |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 break; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 default: |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 return ERR; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 } |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 return OK; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 } |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
45 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 /* To make setbaud.h no more complaint */ |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
47 #define BAUD 9600 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
48 #include <util/setbaud.h> |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 int uart_init(uint32_t baud) { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 unsigned long ubrr_v; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 long err; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 if(baud > (F_CPU / 8)) |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 return ERR; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 #define F_CPUx2 (F_CPU * 2UL) |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
59 ubrr_v = (F_CPU + baud * 8UL) / (baud * 16UL) - 1UL; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 /* Allow bias of baud rate in 2%, or use U2X clock. |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
61 * ATmega168 divides baud rate generator clock by 2, 8, or 16. |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
62 * Setting U2X bit makes the clock divided by 8 instead of 16. |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
63 * It makes more accurately output and higher max baud rate. |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 */ |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 err = 16L * 100L * (ubrr_v + 1) * baud - F_CPU * 100L; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 if(err > F_CPUx2 || err < -F_CPUx2 || ubrr_v < 2) { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
67 ubrr_v = (F_CPU + baud * 4UL) / (baud * 8UL) - 1UL; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 UCSRA |= (1 << U2X); |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
69 } else { |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
70 UCSRA &= ~(1 << U2X); |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
71 } |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
72 |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
73 UBRRH = ubrr_v >> 8; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
74 UBRRL = ubrr_v & 0xff; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
75 return OK; |
a0ce8ebf2f18
LED on PINB0 & PINB1 and UART testing.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
76 } |