Skip to content

Commit

Permalink
Radio interrupt code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-MB committed Nov 19, 2024
1 parent c7825e9 commit 691e610
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/drivers/rfm9x.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ void rfm9x_transmit(rfm9x_t *r)
// we do not have an LNA
rfm9x_set_mode(r, TX_MODE);
uint8_t dioValue = rfm9x_get8(r, _RH_RF95_REG_40_DIO_MAPPING1);
dioValue = bits_set(dioValue, 6, 7, 0b01);
dioValue = bits_set(dioValue, 6, 7, 0b00);
rfm9x_put8(r, _RH_RF95_REG_40_DIO_MAPPING1, dioValue);
}

Expand Down Expand Up @@ -764,7 +764,7 @@ uint8_t rfm9x_receive_packet(rfm9x_t *r, uint8_t node, char *buf)
printf("FIFO length: %d\r\n", fifo_length);

// keep listening, no ack, yes debug, no view
rfm9x_receive(r, buf, node, 1, 0);
rfm9x_receive(r, buf, node, 1, 0, 1);

if (fifo_length > 0)
{
Expand All @@ -782,13 +782,15 @@ uint8_t rfm9x_receive_packet(rfm9x_t *r, uint8_t node, char *buf)

// my pointer stuff with packet is almost definitely wrong!
uint8_t rfm9x_receive(rfm9x_t *r, char *packet, uint8_t node,
uint8_t keep_listening, uint8_t with_ack)
uint8_t keep_listening, uint8_t with_ack, bool blocking_wait_for_packet)
{
uint8_t n_bytes = 0;

// Spin until we have a packet
// while(!rfm9x_rx_done(r));
rfm9x_await_rx(r);
if (blocking_wait_for_packet) {
rfm9x_await_rx(r);
}

// enter idle mode so that we don't receive other packets
rfm9x_set_mode(r, STANDBY_MODE);
Expand Down Expand Up @@ -982,7 +984,7 @@ uint8_t rfm9x_send_ack(rfm9x_t *r, char *data, uint32_t l, uint8_t destination,
else
{
// Wait for ack
uint16_t l = rfm9x_receive(r, ack_buffer, node, 0, 0);
uint16_t l = rfm9x_receive(r, ack_buffer, node, 0, 0, 1);
if (l > 0)
{ /* Received something */
if (ack_buffer[3] & _RH_FLAGS_ACK)
Expand Down
21 changes: 20 additions & 1 deletion src/drivers/rfm9x.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ uint32_t rfm9x_version(rfm9x_t *r);

/*
* Send a raw transmission from the RFM9X.
*
* r: the radio
* data: the data to send
* l: the length of the data. Must be less than `PAYLOAD_SIZE`
* keep_listening: 0 to stop listening after sending, 1 to keep blocking
* destination: radio to send it to. 255 is broadcast.
* node: our address
* identifier: Sequence number — if sending multiple packets, increment by one
* per packet.
* flags:
*/
uint8_t rfm9x_send(rfm9x_t *r, char *data, uint32_t l, uint8_t keep_listening,
uint8_t destination, uint8_t node, uint8_t identifier,
Expand All @@ -71,7 +81,7 @@ uint8_t rfm9x_send_ack(rfm9x_t *r, char *data, uint32_t l, uint8_t destination,
* Receive a transmission.
*/
uint8_t rfm9x_receive(rfm9x_t *r, char *packet, uint8_t node,
uint8_t keep_listening, uint8_t with_ack);
uint8_t keep_listening, uint8_t with_ack, bool blocking_wait_for_packet);

typedef enum
{
Expand Down Expand Up @@ -111,6 +121,15 @@ typedef enum
_RH_RF95_REG_25_FIFO_RX_BYTE_ADDR = 0x25,
_RH_RF95_REG_26_MODEM_CONFIG3 = 0x26,

/**
* In this register:
* Bits 7-6: Dio0Mapping
* Bits 5-4: Dio1Mapping
* Bits 3-2: Dio2Mapping
* Bits 1-0: Dio3Mapping
*
* (p. 100 of documentation)
*/
_RH_RF95_REG_40_DIO_MAPPING1 = 0x40,
_RH_RF95_REG_41_DIO_MAPPING2 = 0x41,
_RH_RF95_REG_42_VERSION = 0x42,
Expand Down
41 changes: 36 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ slate_t slate;

int send();

void interrupt_recieved(uint gpio, uint32_t events);

int receive();

int check_version();

const uint RADIO_INTERRUPT_PIN = 28;

rfm9x_t radio_module;
/**
* Main code entry point.
*
Expand Down Expand Up @@ -55,6 +61,17 @@ int main()
*/
LOG_INFO("main: Dispatching the state machine...");

gpio_init(RADIO_INTERRUPT_PIN);
gpio_set_dir(RADIO_INTERRUPT_PIN, GPIO_IN);
gpio_pull_down(RADIO_INTERRUPT_PIN);

// Set interrupt handler for the radio
gpio_set_irq_enabled_with_callback(RADIO_INTERRUPT_PIN, GPIO_IRQ_EDGE_RISE,
true, &interrupt_recieved);

bool interruptPin = gpio_get(RADIO_INTERRUPT_PIN);
printf("Interrupt pin (before): %d\n", interruptPin);

/*
while (true)
{
Expand All @@ -70,7 +87,7 @@ int main()
uint tx = 19;
uint rx = 16;
uint clk = 18;
rfm9x_t radio_module = rfm9x_mk(spi0, reset, cs, tx, rx, clk);
radio_module = rfm9x_mk(spi0, reset, cs, tx, rx, clk);

radio_module.debug = 1;

Expand All @@ -79,9 +96,9 @@ int main()
printf("Version: %d\r\n", rfm9x_version(&radio_module));

while(1) {
//send(radio_module);
receive(radio_module);
sleep_ms(1000);
// send(radio_module);
receive(radio_module);
sleep_ms(1000);
}

// rfm9x_init(&radio_module);
Expand All @@ -97,6 +114,7 @@ int check_version(rfm9x_t radio_module)
{
LOG_INFO("%d\n", rfm9x_version(&radio_module));
}

int send(rfm9x_t radio_module)
{
char data[4];
Expand All @@ -109,11 +127,24 @@ int send(rfm9x_t radio_module)
rfm9x_send(&radio_module, &data[0], 4, 0, 255, 0, 0, 0);
}

void interrupt_recieved(uint gpio, uint32_t events)
{
printf("Interrupt received on pin %d\n", gpio);
if (gpio == RADIO_INTERRUPT_PIN)
{
printf("Radio interrupt received\n");
receive(radio_module);
}
}

int receive(rfm9x_t radio_module)
{
char data[256];
uint8_t n = rfm9x_receive(&radio_module, &data[0], 1, 0, 0);
uint8_t n = rfm9x_receive(&radio_module, &data[0], 1, 0, 0, 1);
printf("Received %d\n", n);

bool interruptPin = gpio_get(RADIO_INTERRUPT_PIN);
printf("Interrupt pin: %d\n", interruptPin);
}

/*
Expand Down
4 changes: 4 additions & 0 deletions src/state_machine/tasks/radio_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ void radio_task_init(slate_t *slate)
printf("Brought up RFM9X v%d", rfm9x_version(&slate->radio));
}

// When it sees something in the transmit queue, switches into recieve mode and
// send a packet. Otherwise, be in recieve mode. When it recieves a packet, it
// inturrupts the CPU to immediately recieve.
void radio_task_dispatch(slate_t *slate)
{

}

sched_task_t radio_task = {.name = "radio",
Expand Down

1 comment on commit 691e610

@Sid-MB
Copy link
Contributor Author

@Sid-MB Sid-MB commented on 691e610 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shetaye please take a look

Please sign in to comment.