--- Original-esb/esb.c	2023-03-08 15:43:54.000000000 +0530
+++ Philips-modified-esb/esb.c	2023-03-08 15:44:06.000000000 +0530
@@ -80,6 +80,12 @@
 #define ESB_SYS_TIMER_IRQn TIMER4_IRQn
 #endif
 
+
+int dbg_ready_counter;
+int dbg_disabled_counter;
+int dbg_end_counter;
+bool end_event = false;
+bool tx_start_event = false;
 /* Internal Enhanced ShockBurst module state. */
 enum esb_state {
 	ESB_STATE_IDLE,		/* Idle. */
@@ -143,7 +149,7 @@
 struct esb_address {
 	uint8_t base_addr_p0[4];	/* Base address for pipe 0, in big endian. */
 	uint8_t base_addr_p1[4];   /* Base address for pipe 1-7, in big endian. */
-	uint8_t pipe_prefixes[8];	/* Address prefix for pipe 0 to 7. */
+	uint8_t pipe_prefixes[8];	/* Address prefix for  0 to pipe7. */
 	uint8_t num_pipes;		/* Number of pipes available. */
 	uint8_t addr_length;	/* Length of the address plus the prefix. */
 	uint8_t rx_pipes_enabled;	/* Bitfield for enabled pipes. */
@@ -160,6 +166,7 @@
  * because more pipes are supported.
  */
 __ALIGN(4)
+/*
 static struct esb_address esb_addr = {
 	.base_addr_p0 = {0xE7, 0xE7, 0xE7, 0xE7},
 	.base_addr_p1 = {0xC2, 0xC2, 0xC2, 0xC2},
@@ -169,6 +176,17 @@
 	.rf_channel = 2,
 	.rx_pipes_enabled = 0xFF
 };
+*/
+static struct esb_address esb_addr = {
+	.base_addr_p0 = {0x99, 0x44, 0x99, 0x96},
+	.base_addr_p1 = {0x00, 0x00, 0x00, 0x00},
+	.pipe_prefixes = {0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+	.addr_length = 4,
+	.num_pipes = 1,
+	.rf_channel = 40,
+	.rx_pipes_enabled = 0xFF
+};
+/*Ajit change*/
 
 static esb_event_handler event_handler;
 static struct esb_payload *current_payload;
@@ -176,8 +194,15 @@
 /* FIFOs and buffers */
 static struct payload_tx_fifo tx_fifo;
 static struct payload_rx_fifo rx_fifo;
+
+#if 0
 static uint8_t tx_payload_buffer[CONFIG_ESB_MAX_PAYLOAD_LENGTH + 2];
 static uint8_t rx_payload_buffer[CONFIG_ESB_MAX_PAYLOAD_LENGTH + 2];
+#endif
+//Ajit change
+static uint8_t tx_payload_buffer[CONFIG_ESB_MAX_PAYLOAD_LENGTH];
+static uint8_t rx_payload_buffer[CONFIG_ESB_MAX_PAYLOAD_LENGTH];
+
 
 /* Random access buffer variables for ACK payload handling */
 struct payload_wrap ack_pl_wrap[CONFIG_ESB_TX_FIFO_SIZE];
@@ -204,6 +229,7 @@
 static ppi_channel_t ppi_ch_radio_address_timer_stop;
 static ppi_channel_t ppi_ch_timer_compare0_radio_disable;
 static ppi_channel_t ppi_ch_timer_compare1_radio_txen;
+static ppi_channel_t ppi_ch_timer_compare1_radio_rxen;
 
 static uint32_t ppi_all_channels_mask;
 
@@ -213,7 +239,8 @@
 static void (*on_radio_disabled)(void);
 static void (*on_radio_end)(void);
 static void (*update_rf_payload_format)(uint32_t payload_length);
-
+int esb_init_tx(const struct esb_config *config);
+int esb_init_rx(const struct esb_config *config);
 /*  The following functions are assigned to the function pointers above. */
 static void on_radio_disabled_tx_noack(void);
 static void on_radio_disabled_tx(void);
@@ -221,6 +248,8 @@
 static void on_radio_disabled_rx(void);
 static void on_radio_disabled_rx_ack(void);
 
+void mband_frequency_set(void);
+
 /*  Function to do bytewise bit-swap on an unsigned 32-bit value */
 static uint32_t bytewise_bit_swap(const uint8_t *input)
 {
@@ -284,32 +313,44 @@
 	}
 }
 
+void mband_frequency_set(void)
+{
+	NRF_RADIO->FREQUENCY = (RADIO_FREQUENCY_MAP_Low << RADIO_FREQUENCY_MAP_Pos);
+}
+
 static void update_rf_payload_format_esb_dpl(uint32_t payload_length)
 {
 #if (CONFIG_ESB_MAX_PAYLOAD_LENGTH <= 32)
 	/* Using 6 bits for length */
+	/*Ajit change*/
 	NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
-			   (6 << RADIO_PCNF0_LFLEN_Pos) |
-			   (3 << RADIO_PCNF0_S1LEN_Pos);
+			   (0 << RADIO_PCNF0_LFLEN_Pos) |
+			   (0 << RADIO_PCNF0_S1LEN_Pos);
 #else
 	/* Using 8 bits for length */
 	NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
 			   (8 << RADIO_PCNF0_LFLEN_Pos) |
 			   (3 << RADIO_PCNF0_S1LEN_Pos);
 #endif
-	NRF_RADIO->PCNF1 =
+
+
+//Ajit change 
+		NRF_RADIO->PCNF1 =
 		(RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
 		(RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
 		((esb_addr.addr_length - 1) << RADIO_PCNF1_BALEN_Pos) |
-		(0 << RADIO_PCNF1_STATLEN_Pos) |
+		(CONFIG_ESB_MAX_PAYLOAD_LENGTH << RADIO_PCNF1_STATLEN_Pos) |
 		(CONFIG_ESB_MAX_PAYLOAD_LENGTH << RADIO_PCNF1_MAXLEN_Pos);
 }
 
 static void update_rf_payload_format_esb(uint32_t payload_length)
 {
-	NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_S0LEN_Pos) |
+
+/*Ajit change*/
+	NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
 			   (0 << RADIO_PCNF0_LFLEN_Pos) |
-			   (1 << RADIO_PCNF0_S1LEN_Pos);
+			   (0 << RADIO_PCNF0_S1LEN_Pos);
+//Ajit Change
 
 	NRF_RADIO->PCNF1 =
 		(RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
@@ -317,6 +358,7 @@
 		((esb_addr.addr_length - 1) << RADIO_PCNF1_BALEN_Pos) |
 		(payload_length << RADIO_PCNF1_STATLEN_Pos) |
 		(payload_length << RADIO_PCNF1_MAXLEN_Pos);
+
 }
 
 static void update_radio_addresses(uint8_t update_mask)
@@ -407,8 +449,9 @@
 	switch (esb_cfg.crc) {
 	case ESB_CRC_16BIT:
 		NRF_RADIO->CRCINIT = 0xFFFFUL;  /* Initial value */
-		NRF_RADIO->CRCPOLY = 0x11021UL; /* CRC poly: x^16+x^12^x^5+1 */
+		NRF_RADIO->CRCPOLY = 0x1021UL; /* CRC poly: x^16+x^12^x^5+1 */ // Ajit change from 0x11021
 		NRF_RADIO->CRCCNF = ESB_CRC_16BIT << RADIO_CRCCNF_LEN_Pos;
+		//(" ** After setting CRC registers ** NRF_RADIO->CRCCNF: 0x%x \r\n",NRF_RADIO->CRCCNF);
 		break;
 
 	case ESB_CRC_8BIT:
@@ -528,7 +571,10 @@
 		rx_fifo.payload[rx_fifo.back]->length = esb_cfg.payload_length;
 	}
 
-	memcpy(rx_fifo.payload[rx_fifo.back]->data, &rx_payload_buffer[2],
+//	memcpy(rx_fifo.payload[rx_fifo.back]->data, &rx_payload_buffer[2],
+//	       rx_fifo.payload[rx_fifo.back]->length);
+
+	memcpy(rx_fifo.payload[rx_fifo.back]->data, &rx_payload_buffer[0],
 	       rx_fifo.payload[rx_fifo.back]->length);
 
 	rx_fifo.payload[rx_fifo.back]->pipe = pipe;
@@ -574,6 +620,8 @@
 	nrfx_ppi_channel_alloc(&ppi_ch_radio_address_timer_stop);
 	nrfx_ppi_channel_alloc(&ppi_ch_timer_compare0_radio_disable);
 	nrfx_ppi_channel_alloc(&ppi_ch_timer_compare1_radio_txen);
+	nrfx_ppi_channel_alloc(&ppi_ch_timer_compare1_radio_rxen);
+	
 
 	nrfx_ppi_channel_assign(ppi_ch_radio_ready_timer_start,
 		(uint32_t)&NRF_RADIO->EVENTS_READY, (uint32_t)&ESB_SYS_TIMER->TASKS_START);
@@ -583,9 +631,11 @@
 		(uint32_t)&ESB_SYS_TIMER->EVENTS_COMPARE[0], (uint32_t)&NRF_RADIO->TASKS_DISABLE);
 	nrfx_ppi_channel_assign(ppi_ch_timer_compare1_radio_txen,
 		(uint32_t)&ESB_SYS_TIMER->EVENTS_COMPARE[1], (uint32_t)&NRF_RADIO->TASKS_TXEN);
+	nrfx_ppi_channel_assign(ppi_ch_timer_compare1_radio_rxen,
+		(uint32_t)&ESB_SYS_TIMER->EVENTS_COMPARE[2], (uint32_t)&NRF_RADIO->TASKS_RXEN);	
 #endif
 	ppi_all_channels_mask = (1 << ppi_ch_radio_ready_timer_start) | (1 << ppi_ch_radio_address_timer_stop) |
-							(1 << ppi_ch_timer_compare0_radio_disable) | (1 << ppi_ch_timer_compare1_radio_txen);
+							(1 << ppi_ch_timer_compare0_radio_disable) | (1 << ppi_ch_timer_compare1_radio_txen) | (1 << ppi_ch_timer_compare1_radio_rxen);
 }
 
 static void start_tx_transaction(void)
@@ -599,11 +649,16 @@
 	switch (esb_cfg.protocol) {
 	case ESB_PROTOCOL_ESB:
 		update_rf_payload_format(current_payload->length);
+#if 0
 		tx_payload_buffer[0] = current_payload->pid;
 		tx_payload_buffer[1] = 0;
 		memcpy(&tx_payload_buffer[2], current_payload->data,
 		       current_payload->length);
-
+#endif
+//Ajit change
+		memcpy(&tx_payload_buffer, current_payload->data,
+		       current_payload->length);	   
+#if 0
 		NRF_RADIO->SHORTS = radio_shorts_common |
 				    RADIO_SHORTS_DISABLED_RXEN_Msk;
 		NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk |
@@ -613,15 +668,44 @@
 		retransmits_remaining = esb_cfg.retransmit_count;
 		on_radio_disabled = on_radio_disabled_tx;
 		esb_state = ESB_STATE_PTX_TX_ACK;
+#endif 
+#if 0
+		printk("Printing tx data before transmitting...\r\n");
+		int i = 0;
+		for(i=0; i < current_payload->length; i++)
+		{
+			printk("tx_payload_buffer[%d] : %x \r\n",i,tx_payload_buffer[i]);
+		}
+#endif
+#if 1
+		// Ajit change for tx to work
+		//NRF_RADIO->SHORTS = radio_shorts_common;
+
+
+		NRF_RADIO->SHORTS = radio_shorts_common | RADIO_SHORTS_DISABLED_RXEN_Msk;
+		//NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk | RADIO_INTENSET_READY_Msk;
+		//NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
+		NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
+		nrfx_gppi_channels_disable(1 << ppi_ch_timer_compare1_radio_rxen);
+		nrfx_gppi_channels_enable(1 << ppi_ch_timer_compare1_radio_txen);
+		on_radio_disabled = on_radio_disabled_tx_noack;
+		esb_state = ESB_STATE_PTX_TX;
+
+#endif
 		break;
 
 	case ESB_PROTOCOL_ESB_DPL:
 		ack = !current_payload->noack || !esb_cfg.selective_auto_ack;
+#if 0
 		tx_payload_buffer[0] = current_payload->length;
 		tx_payload_buffer[1] = current_payload->pid << 1;
 		tx_payload_buffer[1] |= current_payload->noack ? 0x00 : 0x01;
 		memcpy(&tx_payload_buffer[2], current_payload->data,
 		       current_payload->length);
+#endif
+//Ajit change
+		memcpy(&tx_payload_buffer, current_payload->data,
+		       current_payload->length);	   
 
 		/* Handling ack if noack is set to false or if
 		 * selective auto ack is turned off
@@ -651,7 +735,9 @@
 
 	NRF_RADIO->TXADDRESS = current_payload->pipe;
 	NRF_RADIO->RXADDRESSES = 1 << current_payload->pipe;
-	NRF_RADIO->FREQUENCY = esb_addr.rf_channel;
+	//NRF_RADIO->FREQUENCY = esb_addr.rf_channel;
+	NRF_RADIO->FREQUENCY &=  (~RADIO_FREQUENCY_FREQUENCY_Msk);
+	NRF_RADIO->FREQUENCY |= (esb_addr.rf_channel & RADIO_FREQUENCY_FREQUENCY_Msk);
 
 	NRF_RADIO->PACKETPTR = (uint32_t)tx_payload_buffer;
 
@@ -661,15 +747,14 @@
 	NRF_RADIO->EVENTS_ADDRESS = 0;
 	NRF_RADIO->EVENTS_PAYLOAD = 0;
 	NRF_RADIO->EVENTS_DISABLED = 0;
-
 	NRF_RADIO->TASKS_TXEN = 1;
+	tx_start_event = true;
 }
 
 static void on_radio_disabled_tx_noack(void)
 {
 	interrupt_flags |= INT_TX_SUCCESS_MSK;
 	tx_fifo_remove_last();
-
 	if (tx_fifo.count == 0) {
 		esb_state = ESB_STATE_IDLE;
 		NVIC_SetPendingIRQ(ESB_EVT_IRQ);
@@ -681,6 +766,7 @@
 
 static void on_radio_disabled_tx(void)
 {
+	printk("Inside on_radio_disabled_tx \r\n");
 	/* Remove the DISABLED -> RXEN shortcut, to make sure the radio stays
 	 * disabled after the RX window
 	 */
@@ -702,6 +788,7 @@
 	nrfx_gppi_channels_enable(ppi_all_channels_mask);
 	nrfx_gppi_channels_disable(1 << ppi_ch_timer_compare1_radio_txen);
 
+
 	NRF_RADIO->EVENTS_END = 0;
 
 	if (esb_cfg.protocol == ESB_PROTOCOL_ESB) {
@@ -740,7 +827,6 @@
 					INT_RX_DATA_RECEIVED_MSK;
 			}
 		}
-
 		if ((tx_fifo.count == 0) ||
 		    (esb_cfg.tx_mode == ESB_TXMODE_MANUAL)) {
 			esb_state = ESB_STATE_IDLE;
@@ -872,6 +958,12 @@
 	pipe_info->pid = rx_payload_buffer[1] >> 1;
 	pipe_info->crc = NRF_RADIO->RXCRC;
 
+	//Ajit change//
+	nrfx_gppi_channels_enable(ppi_all_channels_mask);
+	nrfx_gppi_channels_disable(1 << ppi_ch_timer_compare1_radio_rxen);
+
+	//Ajit change
+
 	/* Check if an ack should be sent */
 	if ((esb_cfg.selective_auto_ack == false) ||
 	    ((rx_payload_buffer[1] & 0x01) == 1)) {
@@ -941,18 +1033,41 @@
 
 static void radio_irq_handler(void)
 {
+
+#if 0
+	if (NRF_RADIO->EVENTS_TXREADY) {
+
+			NRF_RADIO->EVENTS_TXREADY = 0;
+			//printk("Inside EVENTS_TXREADY \r\n");
+
+	}
+
+	if (NRF_RADIO->EVENTS_RXREADY) {
+
+			NRF_RADIO->EVENTS_RXREADY = 0;
+			//printk("Inside EVENTS_RXREADY \r\n");
+	}
+#endif
+
 	if (NRF_RADIO->EVENTS_READY &&
 	    (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk)) {
+
+		dbg_ready_counter++;
+
 		NRF_RADIO->EVENTS_READY = 0;
 		ESB_SYS_TIMER->TASKS_START;
 	}
 
 	if (NRF_RADIO->EVENTS_END &&
 	    (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk)) {
+			
+		dbg_end_counter++;
 		NRF_RADIO->EVENTS_END = 0;
 		/* Call the correct on_radio_end function, depending on the
 		 * current protocol state.
 		 */
+		end_event = true;
+
 		if (on_radio_end) {
 			on_radio_end();
 		}
@@ -960,10 +1075,16 @@
 
 	if (NRF_RADIO->EVENTS_DISABLED &&
 	    (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk)) {
+
+			
+		dbg_disabled_counter++;
 		NRF_RADIO->EVENTS_DISABLED = 0;
+
 		/* Call the correct on_radio_disable function, depending on the
 		 * current protocol state.
 		 */
+
+
 		if (on_radio_disabled) {
 			on_radio_disabled();
 		}
@@ -976,7 +1097,6 @@
 	struct esb_evt event;
 
 	event.tx_attempts = last_tx_attempts;
-
 	get_and_clear_irqs(&interrupts);
 	if (event_handler != NULL) {
 		if (interrupts & INT_TX_SUCCESS_MSK) {
@@ -1030,16 +1150,16 @@
 		esb_disable();
 	}
 
-	event_handler = config->event_handler;
+	event_handler = config->event_handler; 
 
-	memcpy(&esb_cfg, config, sizeof(esb_cfg));
+	memcpy(&esb_cfg, config, sizeof(esb_cfg)); //separate
 
 	interrupt_flags = 0;
 
 	memset(rx_pipe_info, 0, sizeof(rx_pipe_info));
 	memset(pids, 0, sizeof(pids));
 
-	update_radio_parameters();
+	update_radio_parameters(); //separate
 
 	/* Configure radio address registers according to ESB default values */
 	NRF_RADIO->BASE0 = 0xE7E7E7E7;
@@ -1062,8 +1182,8 @@
 	irq_enable(ESB_EVT_IRQ);
 	irq_enable(ESB_SYS_TIMER_IRQn);
 
-	esb_state = ESB_STATE_IDLE;
-	esb_initialized = true;
+	esb_state = ESB_STATE_IDLE; //separate
+	esb_initialized = true; //separate
 
 #ifdef CONFIG_SOC_NRF52832
 	if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004500) {
@@ -1076,6 +1196,41 @@
 	return 0;
 }
 
+
+
+int esb_init_tx(const struct esb_config *config)
+{
+	if (config == NULL) {
+		return -EINVAL;
+	}
+
+	memcpy(&esb_cfg, config, sizeof(esb_cfg)); //separate
+
+	update_radio_parameters(); //separate
+
+	esb_state = ESB_STATE_IDLE; //separate
+
+	return 0;
+}
+
+
+
+int esb_init_rx(const struct esb_config *config)
+{
+	if (config == NULL) {
+		return -EINVAL;
+	}
+
+	memcpy(&esb_cfg, config, sizeof(esb_cfg)); //separate
+
+	update_radio_parameters(); //separate
+
+	esb_state = ESB_STATE_IDLE; //separate
+
+	return 0;
+}
+
+
 int esb_suspend(void)
 {
 	if (esb_state != ESB_STATE_IDLE) {
@@ -1160,7 +1315,6 @@
 		if (++tx_fifo.back >= CONFIG_ESB_TX_FIFO_SIZE) {
 			tx_fifo.back = 0;
 		}
-
 		tx_fifo.count++;
 	} else {
 		struct payload_wrap *new_ack_payload = find_free_payload_cont();
@@ -1188,7 +1342,6 @@
 	}
 
 	irq_unlock(key);
-
 	if (esb_cfg.mode == ESB_MODE_PTX &&
 	    esb_cfg.tx_mode == ESB_TXMODE_AUTO &&
 	    esb_state == ESB_STATE_IDLE) {
@@ -1235,13 +1388,13 @@
 int esb_start_tx(void)
 {
 	if (esb_state != ESB_STATE_IDLE) {
+		printk("inside esb_start_tx , esb_state != ESB_STATE_IDLE \r\n");
 		return -EBUSY;
 	}
 
 	if (tx_fifo.count == 0) {
 		return -ENODATA;
 	}
-
 	start_tx_transaction();
 
 	return 0;
@@ -1250,20 +1403,28 @@
 int esb_start_rx(void)
 {
 	if (esb_state != ESB_STATE_IDLE) {
+		printk("inside esb_start_rx , esb_state != ESB_STATE_IDLE \r\n");
 		return -EBUSY;
 	}
 
+	nrfx_gppi_channels_disable(1 << ppi_ch_timer_compare1_radio_txen);
+	nrfx_gppi_channels_enable(1 << ppi_ch_timer_compare1_radio_rxen);
+
 	NRF_RADIO->INTENCLR = 0xFFFFFFFF;
 	NRF_RADIO->EVENTS_DISABLED = 0;
 	on_radio_disabled = on_radio_disabled_rx;
 
+
 	NRF_RADIO->SHORTS = radio_shorts_common |
 			    RADIO_SHORTS_DISABLED_TXEN_Msk;
 	NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
 	esb_state = ESB_STATE_PRX;
 
+
 	NRF_RADIO->RXADDRESSES = esb_addr.rx_pipes_enabled;
-	NRF_RADIO->FREQUENCY = esb_addr.rf_channel;
+	//NRF_RADIO->FREQUENCY = esb_addr.rf_channel;
+	NRF_RADIO->FREQUENCY &=  (~RADIO_FREQUENCY_FREQUENCY_Msk);
+	NRF_RADIO->FREQUENCY |= (esb_addr.rf_channel & RADIO_FREQUENCY_FREQUENCY_Msk);
 	NRF_RADIO->PACKETPTR = (uint32_t)rx_payload_buffer;
 
 	NVIC_ClearPendingIRQ(RADIO_IRQn);
@@ -1274,13 +1435,33 @@
 	NRF_RADIO->EVENTS_DISABLED = 0;
 
 	NRF_RADIO->TASKS_RXEN = 1;
+	
+	return 0;
+}
+
+int esb_stop_tx(void)
+{
+//	NRF_RADIO->SHORTS = 0;
+//	NRF_RADIO->EVENTS_DISABLED = 0;
+//Ajit
+	NRF_RADIO->SHORTS = 0;
+	NRF_RADIO->INTENCLR = 0xFFFFFFFF;
+//	on_radio_disabled = NULL;
+	NRF_RADIO->EVENTS_DISABLED = 0;
+//ajit
 
+	NRF_RADIO->TASKS_DISABLE = 1;
+	while (NRF_RADIO->EVENTS_DISABLED == 0) {
+		/* wait for register to settle */
+	}
+	esb_state = ESB_STATE_IDLE;
 	return 0;
 }
 
 int esb_stop_rx(void)
 {
 	if (esb_state != ESB_STATE_PRX && esb_state != ESB_STATE_PRX_SEND_ACK) {
+		printk("return invalid from esb_stop_rx \r\n");
 		return -EINVAL;
 	}
 
