+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Member level 4
    Points: 1,844, Level: 9
    Achievements:
    7 years registered

    Join Date
    Oct 2010
    Location
    Wyoming
    Posts
    68
    Helped
    0 / 0
    Points
    1,844
    Level
    9

    (stm32f100 Discovery) Can I read output GPIO ports?

    I'm interfacing a 4x4 keypad to an STM32F100 Discovery board - using PC0->3 as the 4 columns and PB12->15 as the rows. The rows are outpus and the columns are inputs. The 4 rows do a scan (each bit goes low one at a time) in one task and when a key is pressed one of the inputs causes an ISR to be triggered. I want to read the current state of the output port (GPIOB) within the ISR so I can do a lookup for the keypad.

    Here's my code:

    Code:
    void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
    {
    	UCHAR crow;
    	volatile long unsigned int *regb;
    	volatile long unsigned int regb3;
    	volatile long unsigned int regb3a;
    	unsigned int regb4;
    	uint8_t col;
    	uint8_t key_pressed;
    
    	regb = (long unsigned int*)GPIOB->ODR;
    	regb3 = *regb;
    	regb3a = regb3;
    
    	regb4 = (unsigned int)regb3;
    	crow = (UCHAR)regb4;
    
    	crow += 0x30;
    	HAL_UART_Transmit(&huart2, &crow, 1, 100);
    
    	regb4 = (unsigned int)regb3;
    	regb4 >>= 8;
    	crow = (UCHAR)regb4;
    
    	crow += 0x30;
    	HAL_UART_Transmit(&huart2, &crow, 1, 100);
    
    	regb4 = (unsigned int)regb3;
    	regb4 >>= 16;
    	crow = (UCHAR)regb4;
    	crow += 0x30;
    	HAL_UART_Transmit(&huart2, &crow, 1, 100);
    
    	regb4 >>= 8;
    	crow = (UCHAR)regb4;
    	crow += 0x30;
    	HAL_UART_Transmit(&huart2, &crow, 1, 100);
    
    /*
    	col = (UCHAR)GPIO_Pin;
    	switch(col)
    	{
    		case 1:
    		col = 0;
    		break;
    		case 2:
    		col = 1;
    		break;
    		case 4:
    		col = 2;
    		break;
    		case 8:
    		col = 3;
    		break;
    		default:
    		col = 0;
    		break;
    	}
     	key_pressed = (au8_keyTable[glrow][col]);
    	glrow += 0x30;
    	HAL_UART_Transmit(&huart2, &glrow, 1, 100);
    
    	crow = 0x20;
    	HAL_UART_Transmit(&huart2, &crow, 1, 100);
     */
    	
    }
    All I get is slashes. There's goto be an easier way to do this other than possible doing a HAL_GPIO_ReadPin(GPIOB, x);
    BTW, I'm compiling with Atollic TrueStudio and using FreeRTOS.

    •   AltAdvertisment

        
       

  2. #2
    Member level 4
    Points: 1,844, Level: 9
    Achievements:
    7 years registered

    Join Date
    Oct 2010
    Location
    Wyoming
    Posts
    68
    Helped
    0 / 0
    Points
    1,844
    Level
    9

    Re: (stm32f100 Discovery) Can I read output GPIO ports?

    OK - I've got it working now without using an ISR:

    Code:
    void StartKeyPressed(void const * argument)
    {
    	/* USER CODE BEGIN StartKeyPressed */
    //	unsigned long ulReceivedValue;
    	uint8_t u8_row;
    	uint8_t key_pressed;
    	GPIO_PinState state;
    
    	key_pressed = 0;
    
    	/* Infinite loop */
    	for(;;)
    	{
    		key_pressed = 0;
      		for (u8_row = 0; u8_row < NUM_ROWS; u8_row++)
    		{
    			setOneRowLow(u8_row);
    			vTaskDelay(1);
    			state = HAL_GPIO_ReadPin(GPIOC,col0);
    			if(state == GPIO_PIN_RESET)
    			{
    			 	key_pressed = (au8_keyTable[u8_row][0]);
    				HAL_UART_Transmit(&huart2, &key_pressed, 1, 100);
    				vTaskDelay(1);
    			}
    			state = HAL_GPIO_ReadPin(GPIOC,col1);
    			if(state == GPIO_PIN_RESET)
    			{
    			 	key_pressed = (au8_keyTable[u8_row][1]);
    				HAL_UART_Transmit(&huart2, &key_pressed, 1, 100);
    				vTaskDelay(1);
    			}
    			state = HAL_GPIO_ReadPin(GPIOC,col2);
    			if(state == GPIO_PIN_RESET)
    			{
    			 	key_pressed = (au8_keyTable[u8_row][2]);
    				HAL_UART_Transmit(&huart2, &key_pressed, 1, 100);
    				vTaskDelay(1);
    			}
    			state = HAL_GPIO_ReadPin(GPIOC,col3);
    			if(state == GPIO_PIN_RESET)
    			{
    			 	key_pressed = (au8_keyTable[u8_row][3]);
    				HAL_UART_Transmit(&huart2, &key_pressed, 1, 100);
    				vTaskDelay(1);
    			}
    			drive_row_high();
    		}
    	}
      /* USER CODE END StartKeyPressed */
    }



    •   AltAdvertisment

        
       

  3. #3
    Super Moderator
    Points: 73,677, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,947
    Helped
    3403 / 3403
    Points
    73,677
    Level
    66

    Re: (stm32f100 Discovery) Can I read output GPIO ports?

    Hi,

    What about setting up a timer interrupt every 5ms.
    In the according ISR:
    * read in ports
    * activate new row
    * decode the input

    With this... the ports have about 5ms to settle
    And all keys are processed within 4 x 5ms = 20ms.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--