···2828{
2929 unsigned short data = 0;
30303131- if ((unsigned)channel < NUM_ADC_CHANNELS)
3232- {
3333- ascodec_lock();
3131+ if ((unsigned)channel >= NUM_ADC_CHANNELS)
3232+ return 0;
34333535- /* Select channel */
3636- if (ascodec_write(AS3514_ADC_0, (channel << 4)) >= 0)
3737- {
3838- unsigned char buf[2];
3434+ ascodec_lock();
39354040- /*
4141- * The AS3514 ADC will trigger an interrupt when the conversion
4242- * is finished, if the corresponding enable bit in IRQ_ENRD2
4343- * is set.
4444- * Previously the code did not wait and this apparently did
4545- * not pose any problems, but this should be more correct.
4646- * Without the wait the data read back may be completely or
4747- * partially (first one of the two bytes) stale.
4848- */
4949- ascodec_wait_adc_finished();
3636+ /* Select channel */
3737+ if (ascodec_write(AS3514_ADC_0, (channel << 4)) >= 0)
3838+ {
3939+ unsigned char buf[2];
50404141+ /*
4242+ * The AS3514 ADC will trigger an interrupt when the conversion
4343+ * is finished, if the corresponding enable bit in IRQ_ENRD2
4444+ * is set.
4545+ * Previously the code did not wait and this apparently did
4646+ * not pose any problems, but this should be more correct.
4747+ * Without the wait the data read back may be completely or
4848+ * partially (first one of the two bytes) stale.
4949+ */
5050+ ascodec_wait_adc_finished();
51515252- /* Read data */
5353- if (ascodec_readbytes(AS3514_ADC_0, 2, buf) >= 0)
5454- {
5555- data = (((buf[0] & 0x3) << 8) | buf[1]);
5656- }
5757- }
58525959- ascodec_unlock();
5353+ /* Read data */
5454+ if (ascodec_readbytes(AS3514_ADC_0, 2, buf) >= 0)
5555+ {
5656+ data = (((buf[0] & 0x3) << 8) | buf[1]);
5757+ }
6058 }
5959+6060+ ascodec_unlock();
61616262 return data;
6363}