Skip to content

Atmega328p adc support#6148

Closed
mali wants to merge 6 commits intoRIOT-OS:masterfrom
mali:atmega328p_adc_support
Closed

Atmega328p adc support#6148
mali wants to merge 6 commits intoRIOT-OS:masterfrom
mali:atmega328p_adc_support

Conversation

@mali
Copy link
Copy Markdown
Contributor

@mali mali commented Nov 19, 2016

This is a basic adc support for arduino atmega328p based boards.

  • Prescaler is fixed to /128 (125kHz)
  • Vref is fixed to 5V

A simple test program I've used to test with a potentiometer here

Comments needed :-)

@LudwigKnuepfer LudwigKnuepfer added Platform: AVR Platform: This PR/issue effects AVR-based platforms Type: new feature The issue requests / The PR implemements a new feature for RIOT labels Nov 19, 2016
@LudwigKnuepfer LudwigKnuepfer added the Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR label Nov 19, 2016

// Is it needed ...?
// DDRD = 0x00; // configure PORTA as input
// DIDR0 = 0x00; //Digital input disabled on all ADC ports
Copy link
Copy Markdown
Contributor

@plushvoxel plushvoxel Nov 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"For analog input pins, the digital input buffer should be disabled at all times." datasheet page 43. But they are also initialized with 0.
It's still recommended to set/unset the bits.
like

DDRD &= ~(1 << line);
DIDR0 &= ~(1 << line);

Copy link
Copy Markdown
Contributor Author

@mali mali Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Improved adc by setting DDRD & DIDR0 as pointed by @plushvoxel
  • replaced REG |= _BV(REGX) avr style by REG |= (1 << REGX) notation which is more widely used in RIOT.
  • added analogRead() arduino sketches support in sys/arduino

@mali mali force-pushed the atmega328p_adc_support branch from f07140d to 50a3edc Compare November 30, 2016 20:29
@mali
Copy link
Copy Markdown
Contributor Author

mali commented Dec 1, 2016

maybe @kYc0o or @aabadie can have a look too ?

@OlegHahm OlegHahm added the Area: drivers Area: Device drivers label Dec 8, 2016

int analogRead(int pin)
{
adc_init(pin);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK there needs to be a conversion from the Arduino pin number to the actual RIOT specific adc_t ADC line here...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/
#define ADC_NUMOF (6)
/* ADC Channels */
#define ADC_CHAN_0 0 // Maybe ADC_PIN_x is better ?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ADC_PIN_x is better ?

yes :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mali mali force-pushed the atmega328p_adc_support branch from e4ace5d to 15dda76 Compare January 12, 2017 20:54
@mali
Copy link
Copy Markdown
Contributor Author

mali commented Jan 12, 2017

Oops, I've made a force-push which brokes review history, sorry for that ...
Requested changes are adressed

@mali mali force-pushed the atmega328p_adc_support branch 2 times, most recently from 3684ccb to bcb37c2 Compare January 12, 2017 21:24
mali added 6 commits January 12, 2017 22:36
add adc support to atmega328p mcu.
make use of atmega328p adc capability.
impprove arduino sketches support by implementing analogRead function.
analogRead(pin) returns the value read from the specified analog pin.
cast literal to uint32_t in xtimer_usleep() to avoid
main.c:58:27: error: integer overflow in expression [-Werror=overflow]
         xtimer_usleep(500 * 1000);
                           ^
cc1: all warnings being treated as errors
@kYc0o kYc0o added this to the Release 2017.04 milestone Jan 31, 2017
@kYc0o
Copy link
Copy Markdown
Contributor

kYc0o commented Feb 2, 2017

Is this still WIP?

@mali mali changed the title [WIP] Atmega328p adc support Atmega328p adc support Feb 2, 2017
@mali
Copy link
Copy Markdown
Contributor Author

mali commented Feb 2, 2017

I was waiting the 2017.01 Release to remove the WIP tag :-)

@kYc0o
Copy link
Copy Markdown
Contributor

kYc0o commented Feb 2, 2017

Do you think it would be hard to add also the mega2560 and mega1281 cpus? Maybe adc.c can also be in arduino_common code?

xtimer_usleep((uint32_t)500 * 1000);
}
return 0;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change should be in a separated PR. BTW, you can use the constants on timex.h

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR send on #6581

@mali
Copy link
Copy Markdown
Contributor Author

mali commented Feb 2, 2017

@kYc0o I can have a look for mega2560 and mega1281 but I don't have the hardware to test it.

@kYc0o
Copy link
Copy Markdown
Contributor

kYc0o commented Feb 3, 2017

I have the hardware to test, I'd like just to know how much different it can be, just to avoid another PR factorising the code to atmega_common.

@mali
Copy link
Copy Markdown
Contributor Author

mali commented Feb 5, 2017

Seems quite similar, except 2560 has 16 adc while 1281 and 328p habe only 8.
Will push a new PR in a few days.

@kYc0o
Copy link
Copy Markdown
Contributor

kYc0o commented Feb 6, 2017

@mali Thanks!

@mali
Copy link
Copy Markdown
Contributor Author

mali commented Feb 16, 2017

I close this one, a better solution is proposed with PR #6616

@mali mali closed this Feb 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR Platform: AVR Platform: This PR/issue effects AVR-based platforms Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants