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();
	}
	
}

  • I didn't make any changes to the BLE-related subroutines, except for turning off Uarte in the original example program. After receiving your message, I tried copying a new "BLE central UART" example without making any modifications, and after flashing it , I couldn't see the Bluetooth using nRF Connect.

    Could you please test it for me to see if there's an issue?

    Here's the link to the example program: [insert link]

  • Hello,

    Apologies for the delayed response.

    Is there any update at your end?

    ITEK said:
    I tried copying a new "BLE central UART" example without making any modifications, and after flashing it , I couldn't see the Bluetooth using nRF Connect

    Please follow the instruction given on the Testing section of the sample. You would require a peripheral (step 6) to connect  with. 

Related