@@ -174,6 +174,9 @@ struct STM32F4XXADCState {
174
174
175
175
uint8_t adc_sequence_position , adc_next_seq_pos ;
176
176
177
+ // Pre-computed conversion times
178
+ uint64_t adc_conv_times_ns [ADC_NUM_REG_CHANNELS ];
179
+
177
180
QEMUTimer * next_eoc ;
178
181
};
179
182
@@ -194,6 +197,8 @@ typedef union {
194
197
} QEMU_PACKED ;
195
198
} adc_smpr_t ;
196
199
200
+ static const uint16_t adc_smpr_map [] = { 3 , 15 , 28 , 56 , 84 , 112 , 144 , 480 };
201
+
197
202
static void stm32f4xx_adc_reset (DeviceState * dev )
198
203
{
199
204
STM32F4XXADCState * s = STM32F4xx_ADC (dev );
@@ -210,29 +215,6 @@ static void stm32f4xx_adc_reset(DeviceState *dev)
210
215
timer_del (s -> next_eoc );
211
216
}
212
217
213
- static uint16_t adc_lookup_smpr (uint8_t value ) {
214
- switch (value ) {
215
- case 0 :
216
- return 3 ;
217
- case 1 :
218
- return 15 ;
219
- case 2 :
220
- return 28 ;
221
- case 3 :
222
- return 56 ;
223
- case 4 :
224
- return 84 ;
225
- case 5 :
226
- return 112 ;
227
- case 6 :
228
- return 144 ;
229
- case 7 :
230
- return 480 ;
231
- default :
232
- assert (false);
233
- return 0 ;
234
- }
235
- }
236
218
237
219
static uint32_t stm32f4xx_adc_get_value (STM32F4XXADCState * s )
238
220
{
@@ -241,7 +223,8 @@ static uint32_t stm32f4xx_adc_get_value(STM32F4XXADCState *s)
241
223
// I'm not sure why this is yet - some sort of built in oversampling
242
224
// that is enabled in non-DMA mode?
243
225
if (!s -> defs .CR2 .DMA ) {
244
- s -> defs .DR *=(adc_lookup_smpr (s -> adc_smprs [channel ])+ 1 );
226
+ assert (s -> adc_smprs [channel ] < ARRAY_SIZE (adc_smpr_map ));
227
+ s -> defs .DR *=(adc_smpr_map [s -> adc_smprs [channel ]]+ 1 );
245
228
}
246
229
247
230
// Mask: RES 0..3 == 12..6 bit mask.
@@ -261,32 +244,33 @@ static void stm32f4xx_adc_data_in(void *opaque, int n, int level){
261
244
// printf("ADC: Ch %d new data: %d\n",n, level);
262
245
}
263
246
264
- static void stm32f4xx_adc_schedule_next (STM32F4XXADCState * s ) {
265
- if (!s -> defs .CR2 .ADON )
266
- return ;
267
- s -> defs .CR2 .SWSTART = 0 ;
268
- // Calculate the clock rate
269
- uint64_t clock = stm32_rcc_if_get_periph_freq (& s -> parent );
270
-
247
+ static void stm32f4xx_adc_recalc_times (STM32F4XXADCState * s ) {
248
+ // Get the clock rate
249
+ uint64_t clock = s -> parent .clock_freq ;
271
250
clock /= stm32f4xx_adcc_get_adcpre (s -> common );
272
-
273
- // #bits:
274
251
uint32_t conv_cycles = (12U - (s -> defs .CR1 .RES <<1U ));
275
- uint8_t channel = s -> adc_sequence [s -> adc_next_seq_pos ];
276
- conv_cycles += adc_lookup_smpr (s -> adc_smprs [channel ]);
277
-
278
- uint64_t delay_ns = 1000000000000U / (clock /conv_cycles );
279
- if (s -> parent .periph == STM32_P_ADC3 )
280
- {
252
+ for (int i = 0 ; i < ADC_NUM_REG_CHANNELS ; i ++ )
253
+ {
254
+ assert (s -> adc_smprs [i ] < ARRAY_SIZE (adc_smpr_map ));
255
+ uint32_t ch_conv_cycles = conv_cycles + adc_smpr_map [s -> adc_smprs [i ]];
256
+ s -> adc_conv_times_ns [i ] = 1000000000000U / (clock /ch_conv_cycles );
257
+ if (s -> parent .periph == STM32_P_ADC3 )
258
+ {
281
259
// Yes, this is an ugly-ass hack. The above calc is off by 1000 and I still need to determine
282
260
// how to deal with the other channels bogging down the simulation when they run at the "real" specified rate.
283
- delay_ns /= 1000 ;
261
+ s -> adc_conv_times_ns [ i ] /= 1000 ;
284
262
//printf("ADC conversion: %u cycles @ %"PRIu64" Hz (%lu nSec)\n", conv_cycles, clock, delay_ns);
285
- }
286
- timer_mod_ns (s -> next_eoc , qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL )+ delay_ns );
287
-
263
+ }
264
+ }
288
265
}
289
266
267
+ static void stm32f4xx_adc_schedule_next (STM32F4XXADCState * s ) {
268
+ if (!s -> defs .CR2 .ADON )
269
+ return ;
270
+ s -> defs .CR2 .SWSTART = 0 ;
271
+ uint8_t channel = s -> adc_sequence [s -> adc_next_seq_pos ];
272
+ timer_mod_ns (s -> next_eoc , qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL )+ s -> adc_conv_times_ns [channel ]);
273
+ }
290
274
291
275
static uint64_t stm32f4xx_adc_read (void * opaque , hwaddr addr ,
292
276
unsigned int size )
@@ -359,7 +343,7 @@ static void stm32f4xx_adc_update_sequence(STM32F4XXADCState *s)
359
343
static void stm32f4xx_adc_convert (STM32F4XXADCState * s )
360
344
{
361
345
uint8_t channel = s -> adc_sequence [s -> adc_sequence_position ];
362
- qemu_irq_pulse (s -> irq_read [channel ]); // Toggle the data read request IRQ. The receiver can opt to send a new value (or do nothing)
346
+ qemu_irq_raise (s -> irq_read [channel ]); // Toggle the data read request IRQ. The receiver can opt to send a new value (or do nothing)
363
347
}
364
348
365
349
static void stm32f4xx_adc_update_irqs (STM32F4XXADCState * s , int level ) {
@@ -424,8 +408,17 @@ static void stm32f4xx_adc_write(void *opaque, hwaddr addr,
424
408
}
425
409
426
410
switch (addr ) {
427
- case RI_SR :
428
411
case RI_CR1 :
412
+ {
413
+ uint8_t old_res = s -> defs .CR1 .RES ;
414
+ s -> regs [addr ] = value ;
415
+ if (s -> defs .CR1 .RES != old_res )
416
+ {
417
+ stm32f4xx_adc_recalc_times (s );
418
+ }
419
+ }
420
+ break ;
421
+ case RI_SR :
429
422
case RI_HTR :
430
423
case RI_LTR :
431
424
s -> regs [addr ] = value ;
@@ -450,6 +443,7 @@ static void stm32f4xx_adc_write(void *opaque, hwaddr addr,
450
443
{
451
444
s -> adc_smprs [10 + i ] = (value >> (3 * i )) & 0x7 ;
452
445
}
446
+ stm32f4xx_adc_recalc_times (s );
453
447
break ;
454
448
case RI_SMPR2 :
455
449
// if (value!=0) printf("FIXME: Nonzero sample time\n");
@@ -458,7 +452,7 @@ static void stm32f4xx_adc_write(void *opaque, hwaddr addr,
458
452
{
459
453
s -> adc_smprs [i ] = (value >> (3 * i )) & 0x7 ;
460
454
}
461
- break ;
455
+ stm32f4xx_adc_recalc_times ( s ) ;
462
456
break ;
463
457
case RI_JOFR1 ... RI_JOFR4 :
464
458
s -> regs [addr ] = (value & 0xFFF );
0 commit comments