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
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index e8104f7..af0eb8e 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -108,7 +108,8 @@
 #endif

 #if defined(SANSA_E200) || defined(SANSA_C200) || defined(PHILIPS_SA9200) \
-    || defined(SANSA_CLIP) || defined(SANSA_FUZE) || defined(SANSA_C200V2)
+    || (CONFIG_CPU == AS3525v2) \
+    || (CONFIG_CPU == AS3525 && !defined(SANSA_M200V4))
 #include "ascodec.h"
 #include "as3514.h"
 #endif
@@ -1656,8 +1657,9 @@ static bool view_battery(void)
                     lcd_puts(0, line++, "T Battery: ?");
                 }

-#elif defined(SANSA_E200) || defined(SANSA_C200) || defined(SANSA_CLIP) || \
-      defined(SANSA_FUZE) || defined (SANSA_C200V2)
+#elif defined(SANSA_E200) || defined(SANSA_C200) \
+    || (CONFIG_CPU == AS3525v2) \
+    || (CONFIG_CPU == AS3525 && !defined(SANSA_M200V4))
                 const int first = CHARGE_STATE_DISABLED;
                 static const char * const chrgstate_strings[] =
                 {
diff --git a/firmware/target/arm/as3525/ascodec-as3525.c b/firmware/target/arm/as3525/ascodec-as3525.c
index 87a1447..43ef0a5 100644
--- a/firmware/target/arm/as3525/ascodec-as3525.c
+++ b/firmware/target/arm/as3525/ascodec-as3525.c
@@ -82,6 +82,7 @@
 #define REQ_FINISHED   1
 #define REQ_RETRY      2

+#define DEBUG
 #ifdef DEBUG
 #define IFDEBUG(x) x
 #else
@@ -109,9 +110,24 @@ static int int_usb_insert = 0;
 static int int_usb_remove = 0;
 static int int_rtc = 0;
 static int int_adc = 0;
+
+void dbg_i2c(int* x)
+{
+    *x++ = int_audio_ctr;
+    *x++ = int_chg_finished;
+    *x++ = int_chg_insert;
+    *x++ = int_chg_remove;
+    *x++ = int_usb_insert;
+    *x++ = int_usb_remove;
+    *x++ = int_rtc;
+    *x++ = int_adc;
+}
 #endif /* DEBUG */

 static void ascodec_read_cb(unsigned const char *data, unsigned int len);
+
+static bool chg_status;     /* charger present */
+static bool chg_endofch;    /* battery end of charge */
 #endif /* CONFIG_CPU == AS3525 */

 static void ascodec_start_req(struct ascodec_request *req);
@@ -188,7 +204,7 @@ void ascodec_init(void)
     VIC_INT_ENABLE = INTERRUPT_I2C_AUDIO | INTERRUPT_AUDIO;

     /* Generate irq for usb+charge status change */
-    ascodec_write(AS3514_IRQ_ENRD0, /*IRQ_CHGSTAT |*/ IRQ_USBSTAT);
+    ascodec_write(AS3514_IRQ_ENRD0, IRQ_CHGSTAT | IRQ_USBSTAT);
     /* Generate irq for push-pull, active high, irq on rtc+adc change */
     ascodec_write(AS3514_IRQ_ENRD2, IRQ_PUSHPULL | IRQ_HIGHACTIVE |
                                     /*IRQ_RTC |*/ IRQ_ADC);
@@ -436,9 +452,11 @@ static void ascodec_read_cb(unsigned const char *data, unsigned int len)
         return;

     if (data[0] & CHG_ENDOFCH) { /* chg finished */
+        chg_endofch = true;
         IFDEBUG(int_chg_finished++);
     }
     if (data[0] & CHG_CHANGED) { /* chg status changed */
+        chg_status = data[0] & CHG_STATUS;
         if (data[0] & CHG_STATUS) {
             IFDEBUG(int_chg_insert++);
         } else {
@@ -474,7 +492,6 @@ void ascodec_wait_adc_finished(void)
 }
 #endif /* CONFIG_CPU == AS3525 */

-
 void ascodec_enable_endofch_irq(void)
 {
     ascodec_write(AS3514_IRQ_ENRD0, ascodec_enrd0_shadow | CHG_ENDOFCH);
@@ -485,6 +502,26 @@ void ascodec_disable_endofch_irq(void)
     ascodec_write(AS3514_IRQ_ENRD0, ascodec_enrd0_shadow & ~CHG_ENDOFCH);
 }

+bool ascodec_get_chg_status(void)
+{
+#if CONFIG_CPU == AS3525
+    return chg_status;
+#else
+    /* not using AFE interrupts */
+    return ascodec_read(AS3514_IRQ_ENRD0) & CHG_STATUS;
+#endif
+}
+
+bool ascodec_get_chg_endofch(void)
+{
+#if CONFIG_CPU == AS3525
+    return chg_endofch;
+#else
+    /* not using AFE interrupts */
+    return ascodec_read(AS3514_IRQ_ENRD0) & CHG_ENDOFCH;
+#endif
+}
+
 /*
  * NOTE:
  * After the conversion to interrupts, ascodec_(lock|unlock) are only used by
diff --git a/firmware/target/arm/as3525/ascodec-target.h b/firmware/target/arm/as3525/ascodec-target.h
index 3794754..eaf3997 100644
--- a/firmware/target/arm/as3525/ascodec-target.h
+++ b/firmware/target/arm/as3525/ascodec-target.h
@@ -132,6 +132,10 @@ void ascodec_enable_endofch_irq(void);

 void ascodec_disable_endofch_irq(void);

+bool ascodec_get_chg_status(void);
+
+bool ascodec_get_chg_endofch(void);
+
 #endif /* !SIMULATOR */

 #endif /* !_ASCODEC_TARGET_H */
diff --git a/firmware/target/arm/as3525/debug-as3525.c b/firmware/target/arm/as3525/debug-as3525.c
index 75cce72..9d614b2 100644
--- a/firmware/target/arm/as3525/debug-as3525.c
+++ b/firmware/target/arm/as3525/debug-as3525.c
@@ -283,6 +283,7 @@ bool __dbg_hw_info(void)
 #endif
         lcd_clear_display();
         line = 0;
+#if 0
         lcd_puts(0, line++, "[Clock Frequencies:]");
         lcd_puts(0, line++, "     SET       ACTUAL");
 #if CONFIG_CPU == AS3525
@@ -370,7 +371,12 @@ bool __dbg_hw_info(void)
         lcd_putsf(0, line++, "Icache:%s Dcache:%s",
                                       (read_cp15() & CP15_IC)  ? " on" : "off",
                                       (read_cp15() & CP15_DC)  ? " on" : "off");
-
+#else
+        /*int x[8],i;
+        dbg_i2c(x);
+        for(i=0; i<8; i++)
+            lcd_putsf(0, line++, "%d : %d", i, x[i]);*/
+#endif
         lcd_update();
         int btn = button_get_w_tmo(HZ/10);
         if(btn == (DEBUG_CANCEL|BUTTON_REL))
diff --git a/firmware/target/arm/as3525/power-as3525.c b/firmware/target/arm/as3525/power-as3525.c
index 3570d7c..a1468a4 100644
--- a/firmware/target/arm/as3525/power-as3525.c
+++ b/firmware/target/arm/as3525/power-as3525.c
@@ -41,7 +41,7 @@ void power_init(void)
 #if CONFIG_CHARGING
 unsigned int power_input_status(void)
 {
-    return (ascodec_read(AS3514_IRQ_ENRD0) & (1<<5)) ?
+    return ascodec_get_chg_status() ?
         POWER_INPUT_MAIN_CHARGER : POWER_INPUT_NONE;

     /* TODO: Handle USB and other sources properly */
diff --git a/firmware/target/arm/ascodec-target.h b/firmware/target/arm/ascodec-target.h
index c87d869..10f0249 100644
--- a/firmware/target/arm/ascodec-target.h
+++ b/firmware/target/arm/ascodec-target.h
@@ -59,6 +59,16 @@ static inline void ascodec_unlock(void)
     i2c_unlock();
 }

+static inline bool ascodec_get_chg_status(void)
+{
+    return ascodec_read(AS3514_IRQ_ENRD0) & CHG_STATUS;
+}
+
+static inline bool ascodec_get_chg_endofch(void)
+{
+    return ascodec_read(AS3514_IRQ_ENRD0) & CHG_ENDOFCH;
+}
+
 static inline void ascodec_enable_endofch_irq(void)
 {
     ascodec_write(AS3514_IRQ_ENRD0, IRQ_ENDOFCH);
diff --git a/firmware/target/arm/powermgmt-ascodec.c b/firmware/target/arm/powermgmt-ascodec.c
index b463486..e3f4347 100644
--- a/firmware/target/arm/powermgmt-ascodec.c
+++ b/firmware/target/arm/powermgmt-ascodec.c
@@ -109,13 +109,12 @@ static void disable_charger(void)
 static void enable_charger(void)
 {
     ascodec_write(AS3514_CHARGER, BATT_CHG_I | BATT_CHG_V);
-    /* Watch for end of charge. Temperature supervision is handled in
-     * hardware. Charger status can be read and has no interrupt enable. */
-    ascodec_enable_endofch_irq();

     sleep(HZ/10); /* Allow charger turn-on time (it could be gradual). */

-    ascodec_disable_endofch_irq();
+    /* Watch for end of charge. Temperature supervision is handled in
+     * hardware. Charger status can be read and has no interrupt enable. */
+    ascodec_enable_endofch_irq();

     charge_state = CHARGING;
     charger_total_timer = CHARGER_TOTAL_TIMER;
@@ -148,7 +147,7 @@ static inline void charger_control(void)
         if (BATT_FULL_VOLTAGE == thresh)
         {
             /* Wait for CHG_status to be indicated. */
-            if ((ascodec_read(AS3514_IRQ_ENRD0) & CHG_STATUS) == 0)
+            if (!ascodec_get_chg_status())
                 break;

             batt_threshold = BATT_VAUTO_RECHARGE;
@@ -163,7 +162,7 @@ static inline void charger_control(void)

     case CHARGING:
     {
-        if ((ascodec_read(AS3514_IRQ_ENRD0) & CHG_ENDOFCH) == 0)
+        if (!ascodec_get_chg_endofch())
         {
             if (--charger_total_timer > 0)
                 break;