1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
diff --git a/firmware/target/arm/as3525/dbop-as3525.c b/firmware/target/arm/as3525/dbop-as3525.c
index 938eee2..7a60efc 100644
--- a/firmware/target/arm/as3525/dbop-as3525.c
+++ b/firmware/target/arm/as3525/dbop-as3525.c
@@ -20,6 +20,7 @@
  ****************************************************************************/

 #include "config.h"
+#include <inttypes.h>
 #include "as3525.h"
 #include "dbop-as3525.h"

@@ -75,3 +76,50 @@ unsigned short dbop_debug(void)
 {
     return dbop_input_value;
 }
+
+static inline void dbop_set_mode(int mode)
+{
+    int delay = 20;
+    if (mode == 32)
+        DBOP_CTRL |= (1<<13|1<<14);
+    else
+        DBOP_CTRL &= ~(1<<14|1<<13);
+    while(delay--) asm volatile("nop");
+}
+
+void dbop_write_data(const int16_t* p_bytes, int count)
+{
+    
+    const int32_t *data;
+    if ((intptr_t)p_bytes & 0x3 || count == 1)
+    {   /* need to do a single 16bit write beforehand if the address is
+         * not word aligned or count is 1, switch to 16bit mode if needed */
+        if (DBOP_CTRL & (1<<13|1<<14))
+            dbop_set_mode(16);
+        DBOP_DOUT16 = *p_bytes++;
+        if (!(--count))
+            return;
+    }
+    /* from here, 32bit transfers are save
+     * set it to transfer 4*(outputwidth) units at a time,
+     * if bit 12 is set it only does 2 halfwords though (we never set it)
+     * switch to 32bit output if needed */
+    if (!(DBOP_CTRL & (1<<13|1<<14)))
+        dbop_set_mode(32);
+    data = (int32_t*)p_bytes;
+    while (count > 1)
+    {
+        DBOP_DOUT32 = *data++;
+        count -= 2;
+
+        /* Wait if push fifo is full */
+        while ((DBOP_STAT & (1<<6)) != 0);
+    }
+    /* While push fifo is not empty */
+    while ((DBOP_STAT & (1<<10)) == 0);
+
+    /* due to the 32bit alignment requirement or uneven count,
+     * we possibly need to do a 16bit transfer at the end also */
+    if (count > 0)
+        dbop_write_data((int16_t*)data, 1);
+}
diff --git a/firmware/target/arm/as3525/dbop-as3525.h b/firmware/target/arm/as3525/dbop-as3525.h
index 6315603..3db62bd 100644
--- a/firmware/target/arm/as3525/dbop-as3525.h
+++ b/firmware/target/arm/as3525/dbop-as3525.h
@@ -19,5 +19,17 @@
  *
  ****************************************************************************/

+
+#ifndef __DBOP_AS3525_H__
+#define __DBOP_AS3525_H__
+#include <inttypes.h>
 unsigned short int  dbop_read_input(void);
 unsigned short      dbop_debug(void);
+
+/*
+ * Write any data to dbop
+ * switches to 32bit transfers if possible,
+ * for best performance pre-align count and data
+ **/
+void dbop_write_data(const int16_t *data, int count);
+#endif
diff --git a/firmware/target/arm/as3525/sansa-e200v2/lcd-e200v2.c b/firmware/target/arm/as3525/sansa-e200v2/lcd-e200v2.c
index f4d1a7c..54aa22e 100644
--- a/firmware/target/arm/as3525/sansa-e200v2/lcd-e200v2.c
+++ b/firmware/target/arm/as3525/sansa-e200v2/lcd-e200v2.c
@@ -120,19 +120,11 @@ static void as3525_dbop_init(void)
     /* TODO: The OF calls some other functions here, but maybe not important */
 }

-static void lcd_write_value16(unsigned short value)
-{
-    DBOP_CTRL &= ~(1<<14|1<<13);
-    lcd_delay(10);
-    DBOP_DOUT16 = value;
-    while ((DBOP_STAT & (1<<10)) == 0);
-}
-
-static void lcd_write_cmd(int cmd)
+static void lcd_write_cmd(short cmd)
 {
     /* Write register */
     DBOP_TIMPOL_23 = 0xa167006e;
-    lcd_write_value16(cmd);
+    dbop_write_data(&cmd, 1);

     /* Wait for fifo to empty */
     while ((DBOP_STAT & (1<<10)) == 0);
@@ -144,43 +136,13 @@ static void lcd_write_cmd(int cmd)
     DBOP_TIMPOL_23 = 0xa167e06f;
 }

-void lcd_write_data(const fb_data* p_bytes, int count)
-{
-    const long *data;
-    if ((int)p_bytes & 0x3)
-    {   /* need to do a single 16bit write beforehand if the address is
-         * not word aligned */
-        lcd_write_value16(*p_bytes);
-        count--;p_bytes++;
-    }
-    /* from here, 32bit transfers are save
-     * set it to transfer 4*(outputwidth) units at a time,
-     * if bit 12 is set it only does 2 halfwords though */
-    DBOP_CTRL |= (1<<13|1<<14);
-    data = (long*)p_bytes;
-    while (count > 1)
-    {
-        DBOP_DOUT32 = *data++;
-        count -= 2;
-
-        /* Wait if push fifo is full */
-        while ((DBOP_STAT & (1<<6)) != 0);
-    }
-    /* While push fifo is not empty */
-    while ((DBOP_STAT & (1<<10)) == 0);
-
-    /* due to the 32bit alignment requirement or uneven count,
-     * we possibly need to do a 16bit transfer at the end also */
-    if (count > 0)
-        lcd_write_value16(*(fb_data*)data);
-}

 static void lcd_write_reg(int reg, int value)
 {
-    fb_data data = value;
+    unsigned short data = value;

     lcd_write_cmd(reg);
-    lcd_write_value16(data);
+    dbop_write_data(&data, 1);
 }

 /*** hardware configuration ***/
@@ -486,7 +448,7 @@ void lcd_update(void)

     lcd_write_cmd(R_WRITE_DATA_2_GRAM);

-    lcd_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
+    dbop_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
 }

 /* Update a fraction of the display. */
@@ -526,7 +488,7 @@ void lcd_update_rect(int x, int y, int width, int height)

     do
     {
-        lcd_write_data(ptr, width);
+        dbop_write_data(ptr, width);
         ptr += LCD_WIDTH;
     }
     while (--height > 0);
diff --git a/firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c b/firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c
index 3332e0c..bdf1c70 100644
--- a/firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c
+++ b/firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c
@@ -124,66 +124,23 @@ static void as3525_dbop_init(void)
     /* TODO: The OF calls some other functions here, but maybe not important */
 }

-static void lcd_write_value16(unsigned short value)
-{
-    DBOP_CTRL &= ~(1<<14|1<<13);
-    lcd_delay(10);
-    DBOP_DOUT16 = value;
-    while ((DBOP_STAT & (1<<10)) == 0);
-}
-
-static void lcd_write_cmd(int cmd)
+static void lcd_write_cmd(short cmd)
 {
     /* Write register */
     DBOP_TIMPOL_23 = 0xa167006e;
-    lcd_write_value16(cmd);
-
-    /* Wait for fifo to empty */
-    while ((DBOP_STAT & (1<<10)) == 0);
+    dbop_write_data(&cmd, 1);

     lcd_delay(4);

     DBOP_TIMPOL_23 = 0xa167e06f;
 }

-void lcd_write_data(const fb_data* p_bytes, int count)
-{
-    const long *data;
-    if ((int)p_bytes & 0x3)
-    {   /* need to do a single 16bit write beforehand if the address is
-         * not word aligned */
-        lcd_write_value16(*p_bytes);
-        count--;p_bytes++;
-    }
-    /* from here, 32bit transfers are save
-     * set it to transfer 4*(outputwidth) units at a time,
-     * if bit 12 is set it only does 2 halfwords though */
-    DBOP_CTRL |= (1<<13|1<<14);
-    lcd_delay(10);
-    data = (long*)p_bytes;
-    while (count > 1)
-    {
-        DBOP_DOUT32 = *data++;
-        count -= 2;
-
-        /* Wait if push fifo is full */
-        while ((DBOP_STAT & (1<<6)) != 0);
-    }
-    /* While push fifo is not empty */
-    while ((DBOP_STAT & (1<<10)) == 0);
-
-    /* due to the 32bit alignment requirement or uneven count,
-     * we possibly need to do a 16bit transfer at the end also */
-    if (count > 0)
-        lcd_write_value16(*(fb_data*)data);
-}
-
 static void lcd_write_reg(int reg, int value)
 {
-    fb_data data = value;
+    int16_t data = value;

     lcd_write_cmd(reg);
-    lcd_write_value16(data);
+    dbop_write_data(&data, 1);
 }

 /*** hardware configuration ***/
@@ -437,7 +394,7 @@ void lcd_update(void)

     lcd_write_cmd(R_WRITE_DATA_2_GRAM);

-    lcd_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
+    dbop_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
 }

 /* Update a fraction of the display. */
@@ -470,6 +427,10 @@ void lcd_update_rect(int x, int y, int width, int height)

     lcd_write_reg(R_ENTRY_MODE, r_entry_mode);

+    /* we need to make x and width even to enable 32bit transfers */
+    width = (width + (x & 1) + 1) & ~1;
+    x &= ~1;
+
     lcd_window_x(x, x + width - 1);
     lcd_window_y(y, y + height -1);

@@ -479,7 +440,7 @@ void lcd_update_rect(int x, int y, int width, int height)

     do
     {
-        lcd_write_data(ptr, width);
+        dbop_write_data(ptr, width);
         ptr += LCD_WIDTH;
     }
     while (--height > 0);
diff --git a/firmware/target/arm/lcd-c200_c200v2.c b/firmware/target/arm/lcd-c200_c200v2.c
index 45a37d9..9e6b44c 100644
--- a/firmware/target/arm/lcd-c200_c200v2.c
+++ b/firmware/target/arm/lcd-c200_c200v2.c
@@ -138,15 +138,10 @@ static inline void lcd_delay(int delay)
 void lcd_write_data(const fb_data *data, int width)
 {
     do {
-        DBOP_DOUT = *data << 8 | *data >> 8;
-        data++;
-
-        /* Wait if push fifo is full */
-        while ((DBOP_STAT & (1<<6)) != 0);
-    } while(--width);
-
-    /* While push fifo is not empty */
-    while ((DBOP_STAT & (1<<10)) == 0);
+        int data32 = swap32(*(int*)data);
+        dbop_write_data((int16_t*)&data32, 2);
+        width -= 2;
+    } while(width > 0);
 }

 /* send LCD command */
@@ -388,6 +383,12 @@ void lcd_update_rect(int x, int y, int width, int height)
     if ((width <= 0) || (height <= 0))
         return; /* Nothing left to do. */

+#ifdef SANSA_C200V2
+    /* we need to make x and width even to enable 32bit transfers */
+    width = (width + (x & 1) + 1) & ~1;
+    x &= ~1;
+#endif
+
     addr = &lcd_framebuffer[y][x];

     if (width <= 1) {
@@ -411,3 +412,18 @@ void lcd_update_rect(int x, int y, int width, int height)
         addr += LCD_WIDTH;
     } while (--height > 0);
 }
+
+void dbop_write_data_swapped(int16_t *data, int count)
+{
+    unsigned short data16_swapped;
+    unsigned data_swapped;
+
+    if ((intptr_t)data & 0x3 || count == 1)
+    {
+        data16_swapped = swap16(*data);
+        dbop_write_data(&data_swapped, 1);
+    }
+    while (count > 1)
+    {
+        data_swapped = swap32(*data);
+        dbop_write_data(data