Skip to content

Conversation

@ardnew
Copy link
Contributor

@ardnew ardnew commented Oct 23, 2020

This PR is baselined on top of the Teensy 4.0 external interrupt support PR (#1484)

This PR adds analog input support (ADC1/ADC2) for Teensy 4.0 on 14 GPIO pins (A0-A13).

I mirrored the API used in machine feather-m4, assuming it is probably a good example for TinyGo convention.

This has been tested using a cheap potentiometer with src/examples/adc/adc.go. Note you must also change the pin from ADC0 to one of this machine's analog pins, e.g. A0. All other code can be left as-is (although the call to machine.InitADC() is unnecessary - it is implemented as an empty stub for backwards compatibility with existing code such as this example).

The following describes each analog pin, its equivalent digital pin, its IOMUX pad, and finally which ADC channel(s) it is connected to. It is defined in src/machine/board_teensy40.go:

// Analog pins
const (
	//  = Pin  // Dig | [Pad]      {ADC1/ADC2}
	A0  = PA18 // D14 | [AD_B1_02] {  7 / 7  }
	A1  = PA19 // D15 | [AD_B1_03] {  8 / 8  }
	A2  = PA23 // D16 | [AD_B1_07] { 12 / 12 }
	A3  = PA22 // D17 | [AD_B1_06] { 11 / 11 }
	A4  = PA17 // D18 | [AD_B1_01] {  6 / 6  }
	A5  = PA16 // D19 | [AD_B1_00] {  5 / 5  }
	A6  = PA26 // D20 | [AD_B1_10] { 15 / 15 }
	A7  = PA27 // D21 | [AD_B1_11] {  0 / 0  }
	A8  = PA24 // D22 | [AD_B1_08] { 13 / 13 }
	A9  = PA25 // D23 | [AD_B1_09] { 14 / 14 }
	A10 = PA12 // D24 | [AD_B0_12] {  1 / -  }
	A11 = PA13 // D25 | [AD_B0_13] {  2 / -  }
	A12 = PA30 // D26 | [AD_B1_14] {  - / 3  }
	A13 = PA31 // D27 | [AD_B1_15] {  - / 4  }
)

@ardnew ardnew changed the title teensy40: add ADC support board/teensy40: add ADC support Oct 23, 2020
@ardnew ardnew changed the title board/teensy40: add ADC support board/teensy40: Add ADC support Oct 23, 2020
@ardnew ardnew force-pushed the feature/teensy40-adc branch 3 times, most recently from 45b25ed to d1e521f Compare November 6, 2020 16:03
@ardnew ardnew force-pushed the feature/teensy40-adc branch from d1e521f to 4f4d73d Compare November 11, 2020 05:47
@deadprogram
Copy link
Member

Please rebase this branch against dev to resolve merge conflicts. Thanks.

@ardnew ardnew force-pushed the feature/teensy40-adc branch from 4f4d73d to c4a7f26 Compare November 11, 2020 20:24
@ardnew
Copy link
Contributor Author

ardnew commented Nov 11, 2020

rebased against dev

@ardnew ardnew force-pushed the feature/teensy40-adc branch from 9567c1c to e8068db Compare November 12, 2020 20:15

// Configure initializes the receiver ADC's Pin and the ADC1/ADC2 instances for
// immediate usage with default settings.
func (a ADC) Configure() { a.ConfigureMode(ADCConfig{}) }
Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to change the API of ADC.Configure (for all chips) to the API you're using here. The reason is that an ADC actually has a number of important configurations for different tradeoffs:

  • Reference voltage (often VCC, but sometimes 1.1V or some other value).
  • Accuracy vs time. I know that the nrf chips have this tradeoff (where more accurate sampling takes a lot longer but is necessary if there is a big resistor on the input) and I would guess other ADCs have something similar.

Of course, it's not necessary to implement them all right away but with this I think it makes sense to add an ADCConfig to the peripheral.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. However, unless you are wanting to maintain backwards compatibility, I would think the ADC peripheral should be like all other peripherals and receive its ADCConfig as argument to Configure, and completely ditch the ConfigureMode that I'm using here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aykevl would you rather I keep this code as is (i.e., keep it API-compatible with other machine package implementations)?

or should I introduce a new API-incompatible func (*ADC) Configure(ADCConfig), and remove my current func (*ADC) ConfigureMode(ADCConfig), under the presumption that you are planning to change those others to match this new API-incompatible function

Copy link
Member

Choose a reason for hiding this comment

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

I think it would actually be better to (temporarily) break compatibility and meanwhile also update the signature of the other ADC implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to use the more conventional API: func (*ADC) Configure(ADCConfig)

@ardnew ardnew force-pushed the feature/teensy40-adc branch from 0495df1 to 51c407b Compare November 13, 2020 19:39
@ardnew ardnew force-pushed the feature/teensy40-adc branch from 51c407b to b192826 Compare December 12, 2020 17:09
@ardnew
Copy link
Contributor Author

ardnew commented Dec 12, 2020

rebased against dev

@ardnew ardnew force-pushed the feature/teensy40-adc branch from 682ec29 to d2343d8 Compare September 12, 2021 02:24
@ardnew
Copy link
Contributor Author

ardnew commented Sep 12, 2021

rebased against dev (3eb9dca)

@deadprogram
Copy link
Member

Looks like this PR is ready to go @ardnew

Do you want to squash commits together so I can merge, or would you prefer that I do so, or something else entirely?

@deadprogram
Copy link
Member

I am going to squash/merge in the interest of getting this unstuck (which is entirely my fault for not doing sooner).

Thanks @ardnew

@deadprogram deadprogram merged commit 2dc46a8 into tinygo-org:dev Apr 13, 2022
deadprogram pushed a commit that referenced this pull request Apr 16, 2022
machine/teensy40: add ADC support
ardnew added a commit to ardnew/tinygo that referenced this pull request Jun 21, 2022
machine/teensy40: add ADC support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants