diff options
author | Bertrik Sikken <bertrik@sikken.nl> | 2011-04-23 09:12:28 +0000 |
---|---|---|
committer | Bertrik Sikken <bertrik@sikken.nl> | 2011-04-23 09:12:28 +0000 |
commit | 69e1647d2dfb30adea96533eace02c658d36d66f (patch) | |
tree | 62d7a3f80ddf613e2c196a1638eeabfef424bf27 | |
parent | 55a7a31ce3a742feea2ff47f6ec35356ebb0ba06 (diff) | |
download | rockbox-69e1647d2dfb30adea96533eace02c658d36d66f.tar.gz rockbox-69e1647d2dfb30adea96533eace02c658d36d66f.zip |
iap: reduce code duplication in iap autobaud code
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29769 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | firmware/drivers/serial.c | 62 |
1 files changed, 26 insertions, 36 deletions
diff --git a/firmware/drivers/serial.c b/firmware/drivers/serial.c index fb8851daf2..af0f3abe68 100644 --- a/firmware/drivers/serial.c +++ b/firmware/drivers/serial.c | |||
@@ -32,6 +32,17 @@ | |||
32 | 32 | ||
33 | #if defined(IPOD_ACCESSORY_PROTOCOL) | 33 | #if defined(IPOD_ACCESSORY_PROTOCOL) |
34 | static int autobaud = 0; | 34 | static int autobaud = 0; |
35 | |||
36 | static void set_bitrate(unsigned int rate) | ||
37 | { | ||
38 | unsigned int divisor; | ||
39 | |||
40 | divisor = 24000000L / rate / 16; | ||
41 | SER0_LCR = 0x80; /* Divisor latch enable */ | ||
42 | SER0_DLL = (divisor >> 0) & 0xFF; | ||
43 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
44 | } | ||
45 | |||
35 | void serial_setup (void) | 46 | void serial_setup (void) |
36 | { | 47 | { |
37 | int tmp; | 48 | int tmp; |
@@ -71,16 +82,13 @@ void serial_bitrate(int rate) | |||
71 | if(rate == 0) | 82 | if(rate == 0) |
72 | { | 83 | { |
73 | autobaud = 2; | 84 | autobaud = 2; |
74 | SER0_LCR = 0x80; /* Divisor latch enable */ | 85 | set_bitrate(115200); |
75 | SER0_DLL = 0x0D; /* 24000000/13/16 = 115384 baud */ | 86 | } |
76 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | 87 | else |
77 | return; | 88 | { |
89 | autobaud = 0; | ||
90 | set_bitrate(rate); | ||
78 | } | 91 | } |
79 | |||
80 | autobaud = 0; | ||
81 | SER0_LCR = 0x80; /* Divisor latch enable */ | ||
82 | SER0_DLL = 24000000L / rate / 16; | ||
83 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
84 | } | 92 | } |
85 | 93 | ||
86 | int tx_rdy(void) | 94 | int tx_rdy(void) |
@@ -128,15 +136,11 @@ void SERIAL0(void) | |||
128 | case 0x55: | 136 | case 0x55: |
129 | break; | 137 | break; |
130 | case 0xFC: | 138 | case 0xFC: |
131 | SER0_LCR = 0x80; /* Divisor latch enable */ | 139 | set_bitrate(19200); |
132 | SER0_DLL = 0x4E; /* 24000000/78/16 = 19230 baud */ | ||
133 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
134 | temp = 0xFF; | 140 | temp = 0xFF; |
135 | break; | 141 | break; |
136 | case 0xE0: | 142 | case 0xE0: |
137 | SER0_LCR = 0x80; /* Divisor latch enable */ | 143 | set_bitrate(9600); |
138 | SER0_DLL = 0x9C; /* 24000000/156/16 = 9615 baud */ | ||
139 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
140 | temp = 0xFF; | 144 | temp = 0xFF; |
141 | break; | 145 | break; |
142 | default: | 146 | default: |
@@ -144,14 +148,10 @@ void SERIAL0(void) | |||
144 | if (badbaud >= 6) /* Switch baud detection mode */ | 148 | if (badbaud >= 6) /* Switch baud detection mode */ |
145 | { | 149 | { |
146 | autobaud = 2; | 150 | autobaud = 2; |
147 | SER0_LCR = 0x80; /* Divisor latch enable */ | 151 | set_bitrate(115200); |
148 | SER0_DLL = 0x0D; /* 24000000/13/16 = 115384 baud */ | ||
149 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
150 | badbaud = 0; | 152 | badbaud = 0; |
151 | } else { | 153 | } else { |
152 | SER0_LCR = 0x80; /* Divisor latch enable */ | 154 | set_bitrate(57600); |
153 | SER0_DLL = 0x1A; /* 24000000/26/16 = 57692 baud */ | ||
154 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
155 | } | 155 | } |
156 | continue; | 156 | continue; |
157 | } | 157 | } |
@@ -162,21 +162,15 @@ void SERIAL0(void) | |||
162 | case 0x55: | 162 | case 0x55: |
163 | break; | 163 | break; |
164 | case 0xFE: | 164 | case 0xFE: |
165 | SER0_LCR = 0x80; /* Divisor latch enable */ | 165 | set_bitrate(57600); |
166 | SER0_DLL = 0x1A; /* 24000000/26/16 = 57692 baud */ | ||
167 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
168 | temp = 0xFF; | 166 | temp = 0xFF; |
169 | break; | 167 | break; |
170 | case 0xFC: | 168 | case 0xFC: |
171 | SER0_LCR = 0x80; /* Divisor latch enable */ | 169 | set_bitrate(38400); |
172 | SER0_DLL = 0x27; /* 24000000/39/16 = 38461 baud */ | ||
173 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
174 | temp = 0xFF; | 170 | temp = 0xFF; |
175 | break; | 171 | break; |
176 | case 0xE0: | 172 | case 0xE0: |
177 | SER0_LCR = 0x80; /* Divisor latch enable */ | 173 | set_bitrate(19200); |
178 | SER0_DLL = 0x4E; /* 24000000/78/16 = 19230 baud */ | ||
179 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
180 | temp = 0xFF; | 174 | temp = 0xFF; |
181 | break; | 175 | break; |
182 | default: | 176 | default: |
@@ -184,14 +178,10 @@ void SERIAL0(void) | |||
184 | if (badbaud >= 6) /* Switch baud detection */ | 178 | if (badbaud >= 6) /* Switch baud detection */ |
185 | { | 179 | { |
186 | autobaud = 1; | 180 | autobaud = 1; |
187 | SER0_LCR = 0x80; /* Divisor latch enable */ | 181 | set_bitrate(57600); |
188 | SER0_DLL = 0x1A; /* 24000000/26/16 = 57692 baud */ | ||
189 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
190 | badbaud = 0; | 182 | badbaud = 0; |
191 | } else { | 183 | } else { |
192 | SER0_LCR = 0x80; /* Divisor latch enable */ | 184 | set_bitrate(115200); |
193 | SER0_DLL = 0x0D; /* 24000000/13/16 = 115384 baud */ | ||
194 | SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */ | ||
195 | } | 185 | } |
196 | continue; | 186 | continue; |
197 | } | 187 | } |