cancel
Showing results for 
Search instead for 
Did you mean: 

directly read register status instead of HAL_GPIO_Readpin();

YTimm.1
Associate II

i am trying to bit bang 14 bits grey code. my output is not steady

uint16_t readbits(uint16_t dataPin, uint16_t clockPin) {

 uint16_t value =0;

  for (int i = 0; i <14; ++i) {

  value <<= 1;

  HAL_GPIO_WritePin(GPIOC, clockPin, GPIO_PIN_RESET);

  delay_us(20);

  HAL_GPIO_WritePin(GPIOC, clockPin, GPIO_PIN_SET);

  delay_us(20);

    value |= HAL_GPIO_ReadPin(GPIOC, dataPin);

  }

  return value;

 }

i think HAL_GPIO_ReadPin is not fast enough

in the while loop ()

DATA1 = readbits(DATA1_L_Pin, CLK_SSI_PORT_Pin);

how can change HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_2); for example to read register status?

12 REPLIES 12
YTimm.1
Associate II

with the line: GPIOC->BSRR = clockPin << 16; this is for specific GPIO_PIN_1 ?

Read about the BSRR register in the reference manual. The upper 16 bits reset the output (low), the 16 lower bits set the output (high).
clockPin should be some combination of GPIO_PIN_x values.
If you feel a post has answered your question, please click "Accept as Solution".

it worked ! i missed one falling edge, because ssi word starts when de clock goes low and then up. Than i need the 14 bit data burst.

with your answer and Ozone i made the following function: it reads 4 pins at the same clock

void CLK_and_reading(uint16_t clockPin) {

 DATA1 =0; DATA2 =0; DATA3 =0; DATA4 =0;

 GPIOC->BSRR = clockPin << 16; delay_us(4);

 GPIOC->BSRR = clockPin; delay_us(4);

 for (int i = 0; i <14; ++i) {

  DATA1 <<= 1; DATA2 <<= 1; DATA3 <<= 1; DATA4 <<= 1;

  GPIOC->BSRR = clockPin << 16; delay_us(4);

  GPIOC->BSRR = clockPin;

  if (GPIOC->IDR & DATA1_L_Pin) { DATA1 |= 1; }

  if (GPIOC->IDR & DATA2_L_Pin) { DATA2 |= 1; }

  if (GPIOC->IDR & DATA3_L_Pin) { DATA3 |= 1; }

  if (GPIOC->IDR & DATA4_L_Pin) { DATA4 |= 1; }

    delay_us(1);

  }

 }