Two Button function coverage problem

Hey
I've got this project that requires two buttons. In normal mode, these buttons react to the Ready pin switch signals. when it goes into sleep mode (where it controls other MCUs going to sleep), the UART Rx changes to act as a button to wake up based on High_Low signals of UART communication.

When it wakes up, the UART Rx Button switches back to being UART Rx pin for communication.

But here's the snag: when I change UART Rx to button, the original Ready pin detection button stops working, it just doesn't respond anymore.

If I try to add the initialization declaration for the Ready pin detection button after initializing UART Rx as a button, the UART Rx button loses its functionality.

Here's the code where I declare the buttons. Could you help me figure out where I need to make changes?

By the way, I'm using NCS v2.5.2. and Chip is nrf52840

The following is the code for the Ready Pin initialization and interrupt subroutine.

void CapSwReadyPin(const struct device *dev, struct gpio_callback *cb, uint32_t pins){
	int ret;
	int err = 0;	
	switch(u8CapSwStatus){
		case deCapSwNull:
		break;
		
		//have other case can change u8CapSwStatus...
		
		default:
		break;
}

void CapSwReadyPinInit(void){
	int ret;


	if (!gpio_is_ready_dt(&button)) {
		printk("Error: button device %s is not ready\n",
		       button.port->name);
		return 0;
	}
	
	ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
	if (ret != 0) {
		printk("Error %d: failed to configure %s pin %d\n",
		       ret, button.port->name, button.pin);
		return 0;
	}
		ret = gpio_pin_interrupt_configure_dt(&button,GPIO_INT_EDGE_TO_ACTIVE );
	if (ret != 0) {
		printk("Error %d: failed to configure interrupt on %s pin %d\n",
			ret, button.port->name, button.pin);
		return 0;
	}

	gpio_init_callback(&button_cb_data, CapSwReadyPin, BIT(button.pin));
	gpio_add_callback(button.port, &button_cb_data);

}

The following is the code for initializing UART Rx as a button and setting up its interrupt subroutine.

void RxButtonInterrupt(const struct device *dev, struct gpio_callback *cb, uint32_t pins){
	u8SleepFlag = u8SleepFlag | deSleepSignal;
#if deSleepTest
	if(u8SleepTestFlag == deDisable){
		u8SleepTestFlag = deEnable;
		de_RLED_ON;
	}
	else{
		u8SleepTestFlag = deDisable;
		de_RLED_OFF;
	}
#endif	
}
void SleepButtoninit(void){
	int ret;
	if (!gpio_is_ready_dt(&RxButton)) {
		printk("Error: button device %s is not ready\n",
		       button.port->name);
		return 0;
	}
	
	ret = gpio_pin_configure_dt(&RxButton, GPIO_INPUT);
	if (ret != 0) {
		printk("Error %d: failed to configure %s pin %d\n",
		       ret, RxButton.port->name, RxButton.pin);
		return 0;
	}
		ret = gpio_pin_interrupt_configure_dt(&RxButton,GPIO_INT_EDGE_TO_ACTIVE );
	if (ret != 0) {
		printk("Error %d: failed to configure interrupt on %s pin %d\n",
			ret, RxButton.port->name, RxButton.pin);
		return 0;
	}

	gpio_init_callback(&button_cb_data, RxButtonInterrupt, BIT(RxButton.pin));
	gpio_add_callback(RxButton.port, &button_cb_data);
	
}
void SleepButtonUninit(void){
	int ret;
	if (!gpio_is_ready_dt(&RxButton)) {
		printk("Error: button device %s is not ready\n",
		       button.port->name);
		return 0;
	}
	
	ret = gpio_pin_configure_dt(&RxButton, GPIO_INPUT);
	if (ret != 0) {
		printk("Error %d: failed to configure %s pin %d\n",
		       ret, RxButton.port->name, RxButton.pin);
		return 0;
	}
		ret = gpio_pin_interrupt_configure_dt(&RxButton,GPIO_INT_MASK );
	if (ret != 0) {
		printk("Error %d: failed to configure interrupt on %s pin %d\n",
			ret, RxButton.port->name, RxButton.pin);
		return 0;
	}
}

The following is the code for the main program.

int main(void){
	int blink_status = 0;
	int err = 0;
	configure_gpio();
	de_GLED_ON;
	deSleepPinOFF;
	CapSwReadyPinInit();
#if deSempleCodeUR	
	err = uart_init();
	if (err) {
		error();
	}
#endif
#if deUartFunction
#if deMCU_URFunc
	MCU_UartInit();
#endif
#if	deRFID_Function
	RFID_UartInit();
#endif		
#endif
#if deI2C_Function
	I2C_Init();
#endif
	Timer_Init();
#if deMCU_TypeChackFunc
	u8TPSetTimNum = deTCRSMaxTime;
	u8TypeSetFlag = deEnable;
	while(u8TypeSetFlag==deEnable){
#if deMCU_URFunc
		MCU_UartDataProcess();
#endif	
#if deRFID_Function
		RFID_ModSwitch(deRFID_NormalMod);
#endif		
	}
	
#endif	
#if deBLE_Function
	BLE_FunctionSetting();
#endif	
#if deCapSwFunction
	de_CapSw_ON;
	deCapSwSetting;
#else
	de_CapSw_OFF;
#endif
	de_GLED_OFF;
	for (;;) {
#if deCapSwFunction		
		CapSwDataSend();
#endif	
#if deRFID_Function
		RFID_DataProcess();
#endif
#if deMCU_URFunc
		MCU_UartDataProcess();
#endif	
#if deBLE_Function
		BLE_ActFunction();
#endif	
#if deSlepButtonFunc
		SleepFunction();
#endif
	}
}

void SleepFunction(void){
	if(((u8SleepFlag & deSleepSignal)>0) && ((u8SleepLock & deSleepOn)== 0)){
		deMCU_UR_Off;
		SleepButtoninit();
		deSleepPinON;
		u8SleepFlag = u8SleepFlag & deSleepNormal;
		u8SleepLock = u8SleepLock | deSleepOn;
		// CapSwReadyPinInit();
	}
	else if(((u8SleepFlag & deSleepSignal)>0) && (u8SleepLock & deSleepOn)>0){
		SleepButtonUninit();
		MCU_UartInit();
		deSleepPinOFF;
		u8SleepFlag = u8SleepFlag & deSleepNormal;
		u8SleepLock = u8SleepLock & deSleepOff;
		// CapSwReadyPinInit();
	}
	
}

Parents Reply Children
  • Hi there! Sorry for the delayed response; work's been keeping me busy. After seeing your reply, I made several attempts to create a new project and use NCS 2.5.2 to present the issues I mentioned. However, after selecting "create a new application" -> "Copy a sample" -> "BLE UART service," the project I created couldn't include three files: "uart_async_adapter.h," "zephyr/usb/usb_device.h," and "<soc.h>." I tried to troubleshoot this on my own, but the problem persisted.

    So, I decided to upgrade to NCS version 2.6.1 to see if I could resolve the issues. However, I encountered a new problem. After burning the code into the MCU, it kept resetting itself continuously, and I received a UR message:

    1: *** Booting nRF Connect SDK 3758bcbfa5cd ***\r\n
    
    ASSERTION FAIL [(cfg->port_pin_mask & (gpio_port_pins_t)(1UL << (pin))) != 0U] @ WEST_TOPDIR/zephyr/include/zephyr/drivers/gpio.h:1001\r\n
    
    Unsupported pin\r\n [16:17:54.2484378] 

    Since I need to switch between Rx and Button modes to complete the project, I'm in need of assistance.

    Here's the complete project for NCS 2.6.1. If you still need the project for NCS 2.5.2, just let me know.

  • it's been a while, and I haven't received a reply yet. Can you give me an update on the current status? Is there any additional information or materials you need from me?

  • Hi,

    Apologies for the delayed response.

    The issue is coming from configure_gpio() function that calls dk_buttons_init()

    This function would initialize buttons as on the DK.

    As you can see that on the DK (nrf52840dk), pins 11, 12, 24 and 25 are used for the four buttons.

    When you are calling the configure_gpio() function, three of the DK buttons are initialized while initializing the 4th one which you have defined gives error; of course this is not as on the DK, this is your definition.

    Error is also "Unsupported pin" that is pin 8.

    Hope if provides you direction in your debugging.

    Regards,

    Naeem

  • Thank you for your response. The issue with the chip reset has been resolved. Unfortunately, when testing the required functionality with version 2.6.1, I’m still encountering the same problems as with 2.5.2.

    Using the nrf52840 development board, after powering it on and pressing Button1, LED4 lights up. After sending the command 80 03 02 35 23 81, LED2 lights up, and when Button2 is pressed, LED3 lights up. However, Button1 stops working and doesn’t respond. In the project, both buttons are supposed to work simultaneously.

    Could you please assist with this? I’ve updated and uploaded the code.

    Here’s the link:https://github.com/ItekRD/TwoButtonProblem

  • Hi,

    I have tried to go through your code, but I cannot review your whole code, it is not a short code.

    Also, when I flashed, I see different behavior than what you have described:

    When I reset the DK,

    LED4 is ON

    LED1 starts blinking

    if I press Button1 then LED4 toggles

    (meaning Button1 is working and in the callback we are toggling LED4)

    if I press Button2 in this state, it does nothing,

    next,

    when I connect with the device, LED2 turns ON,

    I can still use BUTTON1 to toggle LED4

    Button2 still does nothing,

    next,

    when I send that data "80 03 02 35 23 81" through RX characteristic, the device resets.

    You should probably debug your code by seeing how things go on.

    Also, maybe your Button2 is not configured properly.

    Can you first just test your buttons. Can you just show the sample code for buttons that produce that issue?

Related