DDRX function on STM32

Post here first, or if you can't find a relevant section!
Post Reply
HIDDEN
Posts: 4
Joined: Fri Dec 20, 2019 5:04 am

DDRX function on STM32

Post by HIDDEN »

I don't know if I'm posting this in the right section but there goes my question. Is there any function similar to DDRX in Arduino AVR to work with a full port in STM32?
I have been searching and I haven't been able to find anything clarifying about it. In some places they refer to the GPIO concept but I don't know how it works or can be used.
I hope someone can help me. Thank you.

ag123
Posts: 474
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: DDRX function on STM32

Post by ag123 »

not too familiar with avr's too but DDRx registers basically gives the direction of a pin being input or output?
http://maxembedded.com/2011/06/port-operations-in-avr/

i'd guess the arduino api

Code: Select all

pinMode(pin,mode)
gives similar functionality, e.g.

Code: Select all

pinMode(pin, INPUT) 
and

Code: Select all

pinMode(pin, OUTPUT) 
, probably is an equivalent

User avatar
fpiSTM
Posts: 728
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 27
Location: Le Mans
Contact:

Re: DDRX function on STM32

Post by fpiSTM »

This application note could be a good starting point to understand STM32 GPIO:
https://www.st.com/content/ccc/resource ... 315319.pdf

DDRx as far as I remember is the Register access. You can do the same with STM32 with the correct naming, you can find them in the STM32 MCU datasheet (ex: MODER, BSRR, IDR, ODR,...).

Like ag123 told in the previous post, you can simply use Arduino API.
Or if you use the STM32 Core, you can also use HAL or LL access. Ex for the STM32F1:
https://www.st.com/content/ccc/resource ... 154093.pdf

MGeo
Posts: 19
Joined: Thu Dec 19, 2019 10:29 am

Re: DDRX function on STM32

Post by MGeo »

STM32 GPIO is a bit more flexible and complicated than AVR. You should definitely scan through the AN4899 application note above to get a feel for the differences.

There are ways to write to whole port registers once you understand what you want to do. You can use the C/C++ arrow operator -> to access port registers direct, something like GPIOC->ODR = 0xF0FE for example. This tends to be pretty cryptic and non-portable, the HAL/LL functions can help a bit there.

There is a writeup here on accessing STM32 GPIO ports https://www.engineersgarage.com/stm32/a ... ontroller/

George

HIDDEN
Posts: 4
Joined: Fri Dec 20, 2019 5:04 am

Re: DDRX function on STM32

Post by HIDDEN »

Thanks for the answers. I already found what I was looking for.
Exactly the functions to work directly with ports are GPIOA->regs->REG syntax format for Libmaple Core.

Code: Select all

GPIOA->regs->CRL and GPIOA->regs->CRH  for configure output, inputo or alternate function.

GPIOA->regs->IDR for Input Data register

GPIOA->regs->ODR  for Output Daa register

GPIOA->regs->BRR and GPIOA->regs->BSRR for set/reset/mask some pins
It's very useful and allows you to work very quickly directly with the port information for applications that require a quick read / write avoiding the desperate delays of the Arduino functions.

synths_with_stm
Posts: 3
Joined: Sun Dec 22, 2019 2:05 am

Re: DDRX function on STM32

Post by synths_with_stm »

Hi, I know I´m kinda late in here but I just wanted to help you out. I have been playing with the STM32 for quite a bit of time and if you are interested here is a simple code that blinks the built in LED (PC13) using nothing but registers.

Code: Select all

#include "stm32f10x.h"
void TIM1_UP_IRQHandler(void)
{
if(TIM1->SR & TIM_SR_UIF) // if UIF flag is set
  {
      TIM1->SR &= ~TIM_SR_UIF;
	  GPIOC ->ODR ^= (GPIO_ODR_ODR13);
	  }

}
int main(void)
{
	RCC->APB2ENR |= RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_TIM1EN;
	GPIOC ->CRH |= GPIO_CRH_CNF13_0 | GPIO_CRH_MODE13_0;
	RCC->APB2ENR |= RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN;
	GPIOA ->CRL |= GPIO_CRL_CNF0_1;
	GPIOA ->CRL &= ~(GPIO_CRL_CNF0_0);
	GPIOA ->BSRR |= GPIO_BSRR_BS0;
        TIM1->CR1 |= TIM_CR1_CEN;
   	TIM1->PSC=3999;
  	TIM1->ARR=17999;
   	GPIOC ->ODR |= GPIO_ODR_ODR13;
 	TIM1->DIER |= TIM_DIER_UIE;
 	NVIC_EnableIRQ(TIM1_UP_IRQn);
	while (1){}
}

Andy2No
Posts: 15
Joined: Mon Dec 30, 2019 8:34 pm

Re: DDRX function on STM32

Post by Andy2No »

I hadn't seen this thread before. I was effectively asking the same thing, yesterday, but I was thinking more of parallel I/O. This thread has a lot more of the details than I managed to find, so I've linked to it, from there:

viewtopic.php?f=7&t=59&p=297#p297

Short version - it seems you can just use pinMode(), and name the port bits "properly" (e.g. pinMode(PA5,..) instead of pinMode(D13,..)), then do direct I/O via the port registers, e.g.

GPIOA->ODR= a; // upto 16 bits output, but PA2 and PA3 may not get affected

data= GPIOB->IDR; // upto 16 bits input, but not all of them have physical pins

ag123
Posts: 474
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: DDRX function on STM32

Post by ag123 »

slightly off topic but there is this app note AN4666 Parallel synchronous transmission using GPIO and DMA
https://www.st.com/content/ccc/resource ... 169730.pdf
i think it'd work for the general stm32s, the dma is driven by timer or timer input
i've not really tried it out, but one of the use case is to interface it with ov7670 camera
https://www.voti.nl/docs/OV7670.pdf
with that you'd have a stm32duino usb web cam

Post Reply

Return to “General discussion”