Hi, i've got a 24bit digital input into my maple from pin 38 to pin 14, how can I read this as one big ass 24bit value? e.g. 100001010000010100010100 meaning 38, 33, 31, 25, 23, 19 and 17 are high.
Thanks, Harris
Hi, i've got a 24bit digital input into my maple from pin 38 to pin 14, how can I read this as one big ass 24bit value? e.g. 100001010000010100010100 meaning 38, 33, 31, 25, 23, 19 and 17 are high.
Thanks, Harris
pyrohaz - The I/O ports on Maple (STM32) are 16bits wide, so it is not possible to read 24 bits simultaneously.
If it is critical that all 24 signals are read on the same clock cycle, then you are going to need some external electronics to latch and hold the 24 signals' values.
If it is okay to read them close together in time, but not at the same clock cycle, you could read 16bits at a time from the Maples ports. Each port maps to several locations in memory, and you need to read the 32 bit word which corresponds to the Input Data Register (IDR). Though you will read 32bits, only the lower 16 bits are connected to pins.
Each port has a name e.g. pins connected to port A are called PAn, where n is the bit position, from 0 to 15.
Ports addresses are defined already using names GPIOA_BASE, GPIOB_BASE, GPIOC_BASE, GPIOD_BASE, and the input data register is called IDR, so to read the values of port C pins, use the expression:
GPIOC_BASE->IDR
For example:
unsigned int cbits = GPIOC_BASE->IDR;
You'll need to ensure the pins are set up for input, and you can use the ordinary pinMode() function to do that. Bits from pins that aren't set as inputs should be ignored.
For more information, see http://leaflabs.com/docs/libmaple/api/gpio.html
The silkscreened pin numbers printed on the Maple do not refer to the port or bit position within ports.
There is a table for each Maple which does explain the correspondence between a Maple pin number and the port http://leaflabs.com/docs/hardware/maple.html#maple-gpios
A quick glance indicates those pin numbers are on the three ports PB (Port B), PC (Port C) and PD (Port D). Using the information in this table you might rearrange the signals so that they use only two ports.
You might also need to rearrange the bits read from the port to correspond with the order you'd like.
Hey,
Thanks for the reply. I'm used to using direct port manipulation on an arduino (using DDR, PIN and PORT commands) so I assume that its quite the same. How fast is the maple equivalent of DigitalRead? On an arduino, it can take up to 8uS which isn't great for time critical applications! If I could read all 24 inputs within a 50uS timeframe, that would be great and direct port manip. wouldn't be really required.
Cheers,
pyrohaz - Yes, reading or writing port pins on Maple's STM32 is similar to Arduino. The STM32F ports are a bit more complex because there are more modes for pins than the Arduino's ATmega, and there are special mechanisms to set and/or clear any combination of the 16 port bits, but the basic approach is similar; GPIOC_BASE->IDR
will read all 16 bits from port C.
The actual read (load) instruction takes 2 cycles (of 72MHz), so theoretically the pins can be read at 36MHz using direct port access, but in reality there are other things happening like loading the addresses and loop control.
I've had carefully crafted code writing at 12MHz, and 8MHz with a bit less care, to a single bit. Reading all 16 bits of a port is the same speed for 1 bit. So if the pins you want to read are spread across three ports, then all 24 signals (three ports) at 2MHz (0.5us) may be perfectly feasible. (i.e. about 100x faster than your 50us goal.)
The biggest consumer of time may be re-arranging the bits, which represent the signals, into a specific order, if the 24 signals are not independent bits.
I really can't remember how fast Maple's digitalRead()
is. There are threads from a couple of years ago where people did some measurements (including me!). It's likely to be between 2 and 4 times faster as the clock is 4x faster than Arduino, but I may be a bit out. So reading all 24 pins with digitalRead in 50us might be feasible.
(Full disclosure: I am not a member of LeafLabs staff.)
Thats brilliant, You've answered all of my questions, thanks a lot :)
You must log in to post.