Coder Social home page Coder Social logo

Comments (2)

TheMindVirus avatar TheMindVirus commented on June 25, 2024 1

I have Serial working between the K210 and ESP32 using a custom class I wrote based on UARTClass:
(Please note, to update the ESP32 firmware, the reset button must be held down during flashing)

Maix_UART.h

#ifndef MAIX_UART_H
#define MAIX_UART_H

class UART
{
public:
  UART(uint8_t rx = 6, uint8_t tx = 7,
       fpioa_function_t rx_func = FUNC_UART1_RX,
       fpioa_function_t tx_func = FUNC_UART1_TX,
       uart_bitwidth_t width = UART_BITWIDTH_8BIT,
       uart_stopbit_t stopbit = UART_STOP_1,
       uart_parity_t parity = UART_PARITY_NONE)
  {
    this->rx = rx;
    this->tx = tx;
    this->rx_func = rx_func;
    this->tx_func = tx_func;
    this->width = width;
    this->stopbit = stopbit;
    this->parity = parity;
  }
  
  void begin(uint32_t baud = 115200)
  {
    this->baud = baud;
    fpioa_set_function(rx, this->rx_func);
    fpioa_set_function(tx, this->tx_func);
    uart_init(this->handle);
    uart_configure(this->handle, this->baud, this->width, this->stopbit, this->parity);
    this->buffer = new RingBuffer();
    uart_set_receive_trigger(this->handle, UART_RECEIVE_FIFO_1);
    uart_irq_register(this->handle, UART_RECEIVE, this->receiveCB, this, 5);
    sysctl_enable_irq();
  }

  static int receiveCB(void* ctx)
  {
    char tmp = '\0';
    UART* uart = (UART*)ctx;
    while(uart_receive_data(uart->handle, &tmp, 1)) { uart->buffer->store_char(tmp); }
    return 0;
  }

  int available()
  {
    return this->buffer->available();
  }

  char read()
  {
    if(this->available()) { return this->buffer->read_char(); }
    return -1;
  }
  
  void print(char* data, int len = -1)
  {
    if(len < 0) len = strlen(data);
    for(int i = 0; i < len; ++i)
    {
      while(uart[this->handle]->LSR & (1u << 5)) { }
      uart[this->handle]->THR = data[i];
    }
  }

  void println(char* data, int len = -1)
  {
    char newline[] = "\n";
    this->print(data, len);
    this->print(newline, strlen(newline));
  }
  
private:
  uint32_t baud;
  uint8_t rx;
  uint8_t tx;
  fpioa_function_t rx_func;
  fpioa_function_t tx_func;
  uart_bitwidth_t width;
  uart_stopbit_t stopbit;
  uart_parity_t parity;
  uart_device_number_t handle;
  RingBuffer* buffer;
};

#endif//MAIX_UART_H

K210_Usage.ino

UART ESP32; //ESP32(6, 7);

void setup()
{
  ESP32.begin(115200);

  int nBytes = 0;
  char buffer[255] = "";
  char message[] = "Hello ESP32";
  while(1)
  {
    ESP32.println(message);
    nBytes = ESP32.available();
    if(nBytes > 0)
    {
      for(int i = 0; i < nBytes; ++i) { buffer[i] = (char)ESP32.read(); }
      buffer[nBytes] = '\0';
      Serial.println(buffer);
    }
    delay(1000);
  }
}

void loop() { }

ESP32_Usage.ino

void setup()
{
  Serial.begin(115200);

  int nBytes = 0;
  char buffer[255] = "";
  char message[] = "Hello ESP32";
  char response[] = "Hello K210";
  while(1)
  {
    nBytes = Serial.available();
    if(nBytes > 0)
    {
      for(int i = 0; i < nBytes; ++i) { buffer[i] = (char)Serial.read(); }
      buffer[nBytes] = '\0';
      Serial.print(buffer);
      if(strncmp(buffer, message, strlen(message)) == 0) { Serial.println(response); }
    }
    delay(500);
  }
}

void loop() { }

from maixduino.

TheMindVirus avatar TheMindVirus commented on June 25, 2024

For printing floats, the print() method above may need to be modified to use sprintf() using preprocessor macros. This will make it more like the Arduino standard printer.

from maixduino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.