[quote=“Hooligan0”]It is not wrong, the mask is used to define which bit(s) you want to modify. So, to change bits 0,1,6, 7 you create a mask with this bits to ‘1’ (bits 0 and 1 = 0x03 … bits 7 and 6 = 0xC0) ; then after you write a 8 bits value but only the bits set to 1 into the mask are used … it more or less like network mask used to compute network adresse from ip address.
MASK = [color=#FF0000]11[/color]00 00[color=#FF0000]11[/color]
VALUE = [color=#0000FF]00[/color]00 00[color=#0000FF]11[/color]
new GPIO port value = 00xx xx11 (where x values are unchanged)
The mask is not really a register but an array of address. The GPIO peripheral will use a part of the adress to compute the mask. So, to define a mask, you shift the mask value two bits left and you add the array offset.
Example to set the blue led of wizwiki board, connected on GPIO port-C bit 5 :
GPIO-C is at 0x44000000
Lower bits array (bit0 to bit7) is at 0x400 offset
To change only bit5, you create a mask : 0010 0000 (or 0x20)
You shift two bits to get mask address : 1000 0000 (0x80)
Then to set gpio to ‘1’ (led OFF) you write value 00[color=#0000FF]1[/color]0 0000 at adresse 0x44000000 + 0x400 + 0x80 (=0x44000480)
*(volatile unsigned long *)0x44000480 = 0x20;
Then to set gpio to ‘0’ (led ON) you write value 00[color=#0000FF]0[/color]0 0000 at adresse 0x44000000 + 0x400 + 0x80 (=0x44000480)
*(volatile unsigned long *)0x44000480 = 0x00;
Is it clear ?[/quote]
Thank you, Hooligan0, I am very appreciate with your kindly explain.And I have test it with a little more and made it successfully.This time I want to set the Green led, connected on GPIO port-C bit 9:
GPIO-C is at 0x44000000
[color=#FF4000]Higher[/color] bits array (bit15 to bit8) is at 0x800 offset
To change only bit9, I create a mask : 0000 0010 0000 0000(or 0x0200)
shift two bits to get mask address :0000 1000 0000 0000(or 0x0800)([color=#FFBF00]Here I still don’t know why two bits, can you give me some reference[/color]?)
Then to set gpio to ‘1’ (led OFF) I write value 0000 00[color=#0000FF]1[/color]0 0000 0000 at adresse 0x44000000 + 0x800 + 0x0800 (=0x44001000)
*(volatile unsigned long *)0x44001000 = 0x0200;
Then to set gpio to ‘0’ (led ON) I write value 0000 00[color=#0000FF]0[/color]0 0000 0000 at adresse 0x44000000 + 0x800 + 0x0800 (=0x44001000)
*(volatile unsigned long *)0x44001000 = 0x0000;
#include "W7500x_gpio.h"
GPIO_InitTypeDef GPIO_InitDef;
void delay_ms(__IO uint32_t nCount);
int main()
{
SystemInit();
/* GPIO LED(G) Set */
GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (LED(G))
GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
GPIO_Init(GPIOC, &GPIO_InitDef);
PAD_AFConfig(PAD_PC,GPIO_Pin_9, PAD_AF1); // PAD Config - LED used 2nd Function
GPIO_SetBits(GPIOC, GPIO_Pin_9); // LED(G) Off
while(1)
{
// LED(G) On
delay_ms(500);
// GPIO_ResetBits(GPIOC, GPIO_Pin_9);
*(volatile unsigned long *)0x44001000 = 0x0000;
delay_ms(500);
// GPIO_SetBits(GPIOC, GPIO_Pin_9);
*(volatile unsigned long *)0x44001000 = 0x0200;
}
}
void delay_ms(__IO uint32_t nCount)
{
volatile uint32_t delay = nCount * 2500; // approximate loops per ms at 24 MHz, Debug config
for(; delay != 0; delay--)
__NOP();
}