@@ -198,12 +198,31 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
198198}
199199STATIC MP_DEFINE_CONST_FUN_OBJ_1 (adc_read_obj , adc_read );
200200
201- /// \method read_timed(buf, freq)
202- /// Read analog values into the given buffer at the given frequency. Buffer
203- /// can be bytearray or array.array for example. If a buffer with 8-bit elements
204- /// is used, sample resolution will be reduced to 8 bits.
201+ /// \method read_timed(buf, timer)
205202///
206- /// Example:
203+ /// Read analog values into `buf` at a rate set by the `timer` object.
204+ ///
205+ /// `buf` can be bytearray or array.array for example. The ADC values have
206+ /// 12-bit resolution and are stored directly into `buf` if its element size is
207+ /// 16 bits or greater. If `buf` has only 8-bit elements (eg a bytearray) then
208+ /// the sample resolution will be reduced to 8 bits.
209+ ///
210+ /// `timer` should be a Timer object, and a sample is read each time the timer
211+ /// triggers. The timer must already be initialised and running at the desired
212+ /// sampling frequency.
213+ ///
214+ /// To support previous behaviour of this function, `timer` can also be an
215+ /// integer which specifies the frequency (in Hz) to sample at. In this case
216+ /// Timer(6) will be automatically configured to run at the given frequency.
217+ ///
218+ /// Example using a Timer object (preferred way):
219+ ///
220+ /// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
221+ /// tim = pyb.Timer(6, freq=10) # create a timer running at 10Hz
222+ /// buf = bytearray(100) # creat a buffer to store the samples
223+ /// adc.read_timed(buf, tim) # sample 100 values, taking 10s
224+ ///
225+ /// Example using an integer for the frequency:
207226///
208227/// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
209228/// buf = bytearray(100) # create a buffer of 100 bytes
@@ -213,19 +232,25 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
213232/// print(val) # print the value out
214233///
215234/// This function does not allocate any memory.
216- #if defined(TIM6 )
217235STATIC mp_obj_t adc_read_timed (mp_obj_t self_in , mp_obj_t buf_in , mp_obj_t freq_in ) {
218236 pyb_obj_adc_t * self = self_in ;
219237
220238 mp_buffer_info_t bufinfo ;
221239 mp_get_buffer_raise (buf_in , & bufinfo , MP_BUFFER_WRITE );
222240 size_t typesize = mp_binary_get_size ('@' , bufinfo .typecode , NULL );
223241
224- // Init TIM6 at the required frequency (in Hz)
225- timer_tim6_init (mp_obj_get_int (freq_in ));
226-
227- // Start timer
228- HAL_TIM_Base_Start (& TIM6_Handle );
242+ TIM_HandleTypeDef * tim ;
243+ #if defined(TIM6 )
244+ if (mp_obj_is_integer (freq_in )) {
245+ // freq in Hz given so init TIM6 (legacy behaviour)
246+ tim = timer_tim6_init (mp_obj_get_int (freq_in ));
247+ HAL_TIM_Base_Start (tim );
248+ } else
249+ #endif
250+ {
251+ // use the supplied timer object as the sampling time base
252+ tim = pyb_timer_get_handle (freq_in );
253+ }
229254
230255 // configure the ADC channel
231256 adc_config_channel (self );
@@ -236,9 +261,9 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
236261 uint nelems = bufinfo .len / typesize ;
237262 for (uint index = 0 ; index < nelems ; index ++ ) {
238263 // Wait for the timer to trigger so we sample at the correct frequency
239- while (__HAL_TIM_GET_FLAG (& TIM6_Handle , TIM_FLAG_UPDATE ) == RESET ) {
264+ while (__HAL_TIM_GET_FLAG (tim , TIM_FLAG_UPDATE ) == RESET ) {
240265 }
241- __HAL_TIM_CLEAR_FLAG (& TIM6_Handle , TIM_FLAG_UPDATE );
266+ __HAL_TIM_CLEAR_FLAG (tim , TIM_FLAG_UPDATE );
242267
243268 if (index == 0 ) {
244269 // for the first sample we need to turn the ADC on
@@ -270,19 +295,20 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
270295 // turn the ADC off
271296 HAL_ADC_Stop (& self -> handle );
272297
273- // Stop timer
274- HAL_TIM_Base_Stop (& TIM6_Handle );
298+ #if defined(TIM6 )
299+ if (mp_obj_is_integer (freq_in )) {
300+ // stop timer if we initialised TIM6 in this function (legacy behaviour)
301+ HAL_TIM_Base_Stop (tim );
302+ }
303+ #endif
275304
276305 return mp_obj_new_int (bufinfo .len );
277306}
278307STATIC MP_DEFINE_CONST_FUN_OBJ_3 (adc_read_timed_obj , adc_read_timed );
279- #endif
280308
281309STATIC const mp_map_elem_t adc_locals_dict_table [] = {
282310 { MP_OBJ_NEW_QSTR (MP_QSTR_read ), (mp_obj_t )& adc_read_obj },
283- #if defined(TIM6 )
284311 { MP_OBJ_NEW_QSTR (MP_QSTR_read_timed ), (mp_obj_t )& adc_read_timed_obj },
285- #endif
286312};
287313
288314STATIC MP_DEFINE_CONST_DICT (adc_locals_dict , adc_locals_dict_table );
0 commit comments