Using chatgpt to write code for the nrf52840....


  • Hero Member

    I haven't confirmed whether the offered solution will work or not, but it took only one second to generate, so on that basis alone I'm impressed.

    Anyone here using chatgpt for actual coding of any kind?

    By way of example, I gave this prompt:

    Without using zephyr, write a program for the nrf52840 to send a "Happy Birthday!" text message using the proprietary radio mode.
    

    and chatgpt's response was:

    To send a "Happy Birthday!" text message using the nRF52840 in proprietary radio mode without using the Zephyr RTOS, you can use Nordic Semiconductor's nRF5 SDK. Here's a basic example of how to create a custom radio communication program for the nRF52840. Please note that this code is a simplified example, and in a real-world scenario, you would implement error handling and more robust communication protocols.

    Transmitter (TX) Code:

    #include <stdint.h>
    #include <stdbool.h>
    #include "nrf.h"
    #include "nrf_gpio.h"
    
    #define RADIO_CHANNEL 76  // Choose a custom channel
    
    void radio_init(void) {
        NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm;
        NRF_RADIO->FREQUENCY = RADIO_CHANNEL;
        NRF_RADIO->MODE = RADIO_MODE_MODE_Nrf_2Mbit;
        NRF_RADIO->SHORTS = 0;
        NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_LFLEN_Pos);
        NRF_RADIO->PCNF1 = (1 << RADIO_PCNF1_WHITEEN_Pos) | (3 << RADIO_PCNF1_BALEN_Pos);
        NRF_RADIO->BASE0 = 0xE7E7E7E7;
        NRF_RADIO->BASE1 = 0x43434343;
        NRF_RADIO->PREFIX0 = 0x55443322;
        NRF_RADIO->TXADDRESS = 0x00;
        NRF_RADIO->RXADDRESSES = 0x01;
    }
    
    void radio_send(const uint8_t *data, uint8_t length) {
        NRF_RADIO->PACKETPTR = (uint32_t)data;
        NRF_RADIO->EVENTS_READY = 0;
        NRF_RADIO->EVENTS_END = 0;
    
        NRF_RADIO->TASKS_TXEN = 1;
        while (NRF_RADIO->EVENTS_READY == 0);
    
        NRF_RADIO->TASKS_START = 1;
    
        while (NRF_RADIO->EVENTS_END == 0);
        NRF_RADIO->EVENTS_END = 0;
    
        NRF_RADIO->TASKS_DISABLE = 1;
        while (NRF_RADIO->EVENTS_DISABLED == 0);
    }
    
    int main(void) {
        nrf_gpio_cfg_output(20); // LED on pin 20
    
        radio_init();
    
        while (1) {
            // Message to send
            uint8_t message[] = "Happy Birthday!";
            uint8_t message_len = sizeof(message) - 1; // Exclude null-terminator
    
            // Send the message
            radio_send(message, message_len);
    
            // Toggle an LED to indicate a transmission
            nrf_gpio_pin_toggle(20);
    
            // Delay between transmissions
            for (int i = 0; i < 1000000; i++) {
                __NOP();
            }
        }
    }
    

    Receiver (RX) Code:

    #include <stdint.h>
    #include <stdbool.h>
    #include "nrf.h"
    #include "nrf_gpio.h"
    
    #define RADIO_CHANNEL 76  // Should match the transmitter's channel
    
    void radio_init(void) {
        NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm;
        NRF_RADIO->FREQUENCY = RADIO_CHANNEL;
        NRF_RADIO->MODE = RADIO_MODE_MODE_Nrf_2Mbit;
        NRF_RADIO->SHORTS = 0;
        NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_LFLEN_Pos);
        NRF_RADIO->PCNF1 = (1 << RADIO_PCNF1_WHITEEN_Pos) | (3 << RADIO_PCNF1_BALEN_Pos);
        NRF_RADIO->BASE0 = 0xE7E7E7E7;
        NRF_RADIO->BASE1 = 0x43434343;
        NRF_RADIO->PREFIX0 = 0x55443322;
        NRF_RADIO->TXADDRESS = 0x00;
        NRF_RADIO->RXADDRESSES = 0x01;
    }
    
    void radio_receive(uint8_t *data, uint8_t *length) {
        NRF_RADIO->PACKETPTR = (uint32_t)data;
        NRF_RADIO->EVENTS_READY = 0;
        NRF_RADIO->EVENTS_END = 0;
    
        NRF_RADIO->TASKS_RXEN = 1;
        while (NRF_RADIO->EVENTS_READY == 0);
    
        NRF_RADIO->TASKS_START = 1;
    
        while (NRF_RADIO->EVENTS_END == 0);
        NRF_RADIO->EVENTS_END = 0;
    
        NRF_RADIO->TASKS_DISABLE = 1;
        while (NRF_RADIO->EVENTS_DISABLED == 0);
    
        *length = NRF_RADIO->RXMATCH;
    }
    
    int main(void) {
        nrf_gpio_cfg_output(20); // LED on pin 20
    
        radio_init();
    
        while (1) {
            uint8_t message[16]; // Adjust the buffer size as needed
            uint8_t message_len = 0;
    
            // Receive a message
            radio_receive(message, &message_len);
    
            // Toggle an LED to indicate a reception
            nrf_gpio_pin_toggle(20);
    
            // Process the received message (e.g., print it)
            if (message_len > 0) {
                message[message_len] = '\0'; // Null-terminate the received data
                printf("Received: %s\n", message);
            }
        }
    }
    

    This code sets up a transmitter (TX) and receiver (RX) using the nRF52840's radio peripheral. The TX repeatedly sends the "Happy Birthday!" message, and the RX receives and prints any incoming messages. Make sure both devices are configured to use the same radio channel and have matching radio settings.

    Remember to adapt this code to your specific hardware setup and application requirements, and consider adding more robust error handling for a production-quality solution.


Log in to reply
 

Suggested Topics

  • 4
  • 15
  • 17
  • 9
  • 8
  • 4

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts