Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By mcsa
#2196
mamalala wrote:
mcsa wrote:I dont Inderstand how to write in registers. For example in PIN_OUT
WRITE_PERI_REG(PIN_OUT,0x05) set gpio0 and gpio2 high. Right or no?


No. The purpose of defining tzhe registers directly is to allow direct access to them, instead of going through the WRITE_PERI_REG stuff. For example:

PIN_OUT = 0x00000005;

should do what you want. Of course the pin has to be set up as an output first.

Greetings,

Chris

Don't understand how we can one number assign another number...
User avatar
By martinayotte
#2202 From my understanding (since I didn't verify the PROVIDE macro), it is defining a register pointer, so having PROVIDE(PIN_OUT = 0x60000300); and then PIN_OUT = 0x05 is equivalent of doing *(0x60000300) = 0x05.
Again, as mentionned earlier, this will change the states of several GPIOs at the same time, so it is better to control individual GPIO using PIN_OUT_SET/PIN_OUT_CLEAR macros.
User avatar
By nicoverduin
#2203 I can understand the confusion a bit and I must admit I had to understand the "thinking as well". So if I understand. Setting pin 0 as output and toggeling it could be done as:

initalizing:

Code: Select all
#define GPIO    0     
PIN_DIR_OUTPUT = GPIO;


some loop:

Code: Select allvoid loop() {
   //
   // sets GPIO pin 0 HIGH
   //
   PIN_OUT_SET = GPIO;
   some_delay();
   //
   // clears GPIO pin 0
   //
   PIN_OUT_CLEAR = GPIO;
   some_delay();
}


And if I would like to change this to GPIO2 I just need to change the define in the setup
User avatar
By Dr X7FFF
#5961
tinhead wrote:this is actually described in "6-ESP8266 FAQ文档_v0.2.pdf" (under GPIO, shift register).

I tested it once, however with org. defines (here for GPIO4):

WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR+GPIO_OUT_W1TS_ADDRESS,BIT4);
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR+GPIO_OUT_W1TC_ADDRESS,BIT4);

one can of course use predefined macro (and that is described in the pdf above):

GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS,BIT4);
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS,BIT4)

(a quick look into eagle_soc.h, #define GPIO_REG_WRITE(reg, val) WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + reg, val))

Anyway, the result is of course exact the same.

When writing directly to the out register one can toggle a PIN with ~5714kHz instead of ~825kHz, which was the
case when using GPIO_OUTPUT_SET(GPIO_ID_PIN(4), 1) instead.

One need to know that only GPIO0 to GPIO15 can be accessed like that, to use GPIO16 one need to use the slow API.


I try this macro for driving multiple shift register but nothing. Is there some one how can explain how to direct write out pin ? Here a simple sample code :
Code: Select all#include <ets_sys.h>
#include <osapi.h>
#include <os_type.h>
#include <gpio.h>

// see eagle_soc.h for these definitions
#define DELAY 2000 /* milliseconds */
LOCAL os_timer_t blink_timer;
LOCAL uint8_t led_state=0;

LOCAL ICACHE_FLASH_ATTR blink_cb(void *arg){   
  if (led_state == 0){
    GPIO_REG_WRITE(GPIO_OUT_ADDRESS,(GPIO_REG_READ(GPIO_OUT_ADDRESS) & (uint32)(0xFFFFFFFB)) | (uint32)(0x4));
//    gpio_output_set(0,BIT2|BIT12, BIT2|BIT12,0);
    led_state = 1;
  }
  else{
    GPIO_REG_WRITE(GPIO_OUT_ADDRESS,(GPIO_REG_READ(GPIO_OUT_ADDRESS) & (uint32)(0xFFFFFFFB)));
//    gpio_output_set(BIT2|BIT12, 0, BIT2|BIT12,0);
    led_state = 0;
  }
}

void user_init(void){
  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U,FUNC_GPIO2);
//  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U,FUNC_GPIO4);
//  PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U,FUNC_GPIO12);

// "don't forget to set up the pin first" as output in this case
  gpio_output_set(0, 0, BIT2,0);


  os_timer_disarm(&blink_timer);
  os_timer_setfn(&blink_timer, (os_timer_func_t *)blink_cb, (void *)0);
  os_timer_arm(&blink_timer, DELAY, 1);
}


EDIT :
I don't delete this post for the code sample but I give the solution : " Don't forget to set up the pin first." grrrrr :oops:
besause "gpio_output_set" defined the type output or input but here you MUST do this in PIN set up. (I update the code sample)