Skip to content

Commit

Permalink
break esp32 code into several files
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Jan 31, 2024
1 parent b54d7b3 commit 278b094
Show file tree
Hide file tree
Showing 7 changed files with 1,088 additions and 5 deletions.
File renamed without changes.
167 changes: 167 additions & 0 deletions uCNC/src/hal/mcus/esp32/esp32_bluetooth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Name: esp32_arduino.cpp
Description: Contains all Arduino ESP32 C++ to C functions used by µCNC.
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 27-07-2022
µCNC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#ifdef ESP32
#include <Arduino.h>
#include "esp_task_wdt.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <BluetoothSerial.h>

extern "C"
{
#include "../../../cnc.h"

#ifdef MCU_HAS_BLUETOOTH

BluetoothSerial SerialBT;

uint8_t bt_on;
uint16_t bt_settings_offset;

uint8_t esp32_bt_cmd(uint8_t *grbl_cmd_str, uint8_t grbl_cmd_len, uint8_t next_char)
{
#ifdef BOARD_HAS_CUSTOM_SYSTEM_COMMANDS
if (!strncmp((const char *)grbl_cmd_str, "BTH", 3))
{
if (!strcmp((const char *)&grbl_cmd_str[3], "ON"))
{
SerialBT.begin(BOARD_NAME);
protocol_send_feedback("Bluetooth enabled");
bt_on = 1;
settings_save(bt_settings_offset, &bt_on, 1);

return STATUS_OK;
}

if (!strcmp((const char *)&grbl_cmd_str[3], "OFF"))
{
SerialBT.end();
protocol_send_feedback("Bluetooth disabled");
bt_on = 0;
settings_save(bt_settings_offset, &bt_on, 1);

return STATUS_OK;
}
}
#endif
return STATUS_INVALID_STATEMENT;
}

void esp32_bt_init(void)
{
bt_settings_offset = settings_register_external_setting(1);
if (settings_load(bt_settings_offset, &bt_on, 1))
{
settings_erase(bt_settings_offset, (uint8_t *)&bt_on, 1);
}

if (bt_on)
{
SerialBT.begin(BOARD_NAME);
}
}

#ifndef BLUETOOTH_TX_BUFFER_SIZE
#define BLUETOOTH_TX_BUFFER_SIZE 64
#endif
DECL_BUFFER(uint8_t, bt_rx, RX_BUFFER_SIZE);
DECL_BUFFER(uint8_t, bt_tx, BLUETOOTH_TX_BUFFER_SIZE);

uint8_t mcu_bt_getc(void)
{
uint8_t c = 0;
BUFFER_DEQUEUE(bt_rx, &c);
return c;
}

uint8_t mcu_bt_available(void)
{
return BUFFER_READ_AVAILABLE(bt_rx);
}

void mcu_bt_clear(void)
{
BUFFER_CLEAR(bt_rx);
}

void mcu_bt_putc(uint8_t c)
{
while (BUFFER_FULL(bt_tx))
{
mcu_bt_flush();
}
BUFFER_ENQUEUE(bt_tx, &c);
}

void mcu_bt_flush(void)
{
if (SerialBT.hasClient())
{
while (!BUFFER_EMPTY(bt_tx))
{
uint8_t tmp[BLUETOOTH_TX_BUFFER_SIZE + 1];
memset(tmp, 0, sizeof(tmp));
uint8_t r;

BUFFER_READ(bt_tx, tmp, BLUETOOTH_TX_BUFFER_SIZE, r);
SerialBT.write(tmp, r);
SerialBT.flush();
}
}
else
{
// no client (discard)
BUFFER_CLEAR(bt_tx);
}
}

void esp32_bt_process(void)
{
if (SerialBT.hasClient())
{
while (SerialBT.available() > 0)
{
esp_task_wdt_reset();
#ifndef DETACH_BLUETOOTH_FROM_MAIN_PROTOCOL
uint8_t c = SerialBT.read();
if (mcu_com_rx_cb(c))
{
if (BUFFER_FULL(bt_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(bt_rx)) = c;
BUFFER_STORE(bt_rx);
}
#else
mcu_bt_rx_cb((uint8_t)SerialBT.read());
#endif
}
}
}
#else
uint8_t esp32_bt_cmd(uint8_t *grbl_cmd_str, uint8_t grbl_cmd_len, uint8_t next_char) { return STATUS_INVALID_STATEMENT; }
void esp32_bt_init(void) {}
void esp32_bt_process(void) {}
#endif
}

#endif
64 changes: 64 additions & 0 deletions uCNC/src/hal/mcus/esp32/esp32_eeprom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Name: esp32_arduino.cpp
Description: Contains all Arduino ESP32 C++ to C functions used by µCNC.
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 27-07-2022
µCNC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#ifdef ESP32
#include <Arduino.h>
#include "esp_task_wdt.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <EEPROM.h>

/**
*
* This handles EEPROM simulation on flash memory
*
* **/

extern "C"
{
#include "../../../cnc.h"

#if !defined(RAM_ONLY_SETTINGS) && defined(USE_ARDUINO_EEPROM_LIBRARY)
void esp32_eeprom_init(int size)
{
EEPROM.begin(size);
}

uint8_t mcu_eeprom_getc(uint16_t address)
{
return EEPROM.read(address);
}

void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
EEPROM.write(address, value);
}

void mcu_eeprom_flush(void)
{
if (!EEPROM.commit())
{
protocol_send_feedback(" EEPROM write error");
}
}

#endif
}

#endif
83 changes: 83 additions & 0 deletions uCNC/src/hal/mcus/esp32/esp32_i2c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Name: esp32_arduino.cpp
Description: Contains all Arduino ESP32 C++ to C functions used by µCNC.
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 27-07-2022
µCNC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#ifdef ESP32
#include <Arduino.h>
#include "esp_task_wdt.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <Wire.h>

extern "C"
{
#include "../../../cnc.h"

#ifdef MCU_HAS_I2C


#if (I2C_ADDRESS != 0)
static uint8_t mcu_i2c_buffer_len;
static uint8_t mcu_i2c_buffer[I2C_SLAVE_BUFFER_SIZE];
void esp32_i2c_onreceive(int len)
{
uint8_t l = I2C_REG.readBytes(mcu_i2c_buffer, len);
mcu_i2c_slave_cb(mcu_i2c_buffer, &l);
mcu_i2c_buffer_len = l;
}

void esp32_i2c_onrequest(void)
{
I2C_REG.write(mcu_i2c_buffer, mcu_i2c_buffer_len);
}

#endif

void mcu_i2c_config(uint32_t frequency)
{
#if (I2C_ADDRESS == 0)
I2C_REG.begin(I2C_DATA_BIT, I2C_CLK_BIT, frequency);
#else
I2C_REG.onReceive(esp32_i2c_onreceive);
I2C_REG.onRequest(esp32_i2c_onrequest);
I2C_REG.begin(I2C_ADDRESS, I2C_DATA_BIT, I2C_CLK_BIT, frequency);
#endif
}

uint8_t mcu_i2c_send(uint8_t address, uint8_t *data, uint8_t datalen, bool release)
{
I2C_REG.beginTransmission(address);
I2C_REG.write(data, datalen);
return (I2C_REG.endTransmission(release) == 0) ? I2C_OK : I2C_NOTOK;
}

uint8_t mcu_i2c_receive(uint8_t address, uint8_t *data, uint8_t datalen, uint32_t ms_timeout)
{
I2C_REG.setTimeOut((uint16_t)ms_timeout);
if (I2C_REG.requestFrom(address, datalen) == datalen)
{
I2C_REG.readBytes(data, datalen);
return I2C_OK;
}

return I2C_NOTOK;
}
#endif
}

#endif
66 changes: 66 additions & 0 deletions uCNC/src/hal/mcus/esp32/esp32_spi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Name: esp32_arduino.cpp
Description: Contains all Arduino ESP32 C++ to C functions used by µCNC.
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 27-07-2022
µCNC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#ifdef ESP32
#include <Arduino.h>
#include "esp_task_wdt.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <SPI.h>

extern "C"
{
#include "../../../cnc.h"

#if defined(MCU_HAS_SPI) && defined(USE_ARDUINO_SPI_LIBRARY)

SPIClass *esp32spi = NULL;
uint32_t esp32spifreq = SPI_FREQ;
uint8_t esp32spimode = SPI_MODE0;

void mcu_spi_config(uint8_t mode, uint32_t freq)
{
if (esp32spi != NULL)
{
esp32spi->end();
esp32spi = NULL;
}

#if (SPI_CLK_BIT == 14 || SPI_CLK_BIT == 25)
esp32spi = new SPIClass(HSPI);
#else
esp32spi = new SPIClass(VSPI);
#endif
esp32spi->begin(SPI_CLK_BIT, SPI_SDI_BIT, SPI_SDO_BIT, SPI_CS_BIT);
esp32spifreq = freq;
esp32spimode = mode;
}

uint8_t mcu_spi_xmit(uint8_t data)
{

esp32spi->beginTransaction(SPISettings(esp32spifreq, MSBFIRST, esp32spimode));
data = esp32spi->transfer(data);
esp32spi->endTransaction();
return data;
}
#endif
}

#endif
Loading

0 comments on commit 278b094

Please sign in to comment.