Skip to content

Commit

Permalink
#63 - I2C mocked.
Browse files Browse the repository at this point in the history
  • Loading branch information
PerMalmberg committed Aug 31, 2019
1 parent 7ea3964 commit 3d5af1e
Show file tree
Hide file tree
Showing 11 changed files with 873 additions and 174 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ An application built with Smooth is entirely event driven and thread-safe*. Smoo

Traditionally, embedded systems require a fully static memory footprint after start-up. Smooth takes
a somewhat more pragmatic view on this; it utilizes the standard library (which is not memory static) to provide cleaner code,
at the cost of some extra used bytes of RAM. However, where it is appropriate, such as with the *Queue*, things are designed so
that the result is a memory static instance, i.e. a *smooth::ipc::Queue* will _not_ behave like an *std::vector*.
at the cost of some extra used bytes of RAM. However, where it is appropriate, such as with the queues, things are designed so
that the result is a memory static instance, i.e. a `smooth::ipc::Queue` will have a memory static footprint once initialized.

Apart from hardware/IDF-specific classes, applications written using Smooth can be compiled and run on POSIX systems (e.g. Linux)
without any special considerations.
[mock-idf](mock-idf/README.md) provides the ability to compile even applications that uses ESP-32 hardware for Linux
with the only consideration that the mocks do not actually simulate the hardware.

*) To certain limits, of course.

Expand Down
16 changes: 12 additions & 4 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ add_definitions(-DCONFIG_SMOOTH_MAX_MQTT_MESSAGE_SIZE=512)
add_definitions(-DCONFIG_SMOOTH_MAX_MQTT_OUTGOING_MESSAGES=10)
add_definitions(-DSMOOTH_MQTT_LOGGING_LEVEL=1)


add_library(${PROJECT_NAME} ${SMOOTH_SOURCES})
target_link_libraries(${PROJECT_NAME} mbedtls mbedx509 mbedcrypto sodium mock-idf)
set_compile_options(${PROJECT_NAME})

target_include_directories(${PROJECT_NAME}
PUBLIC
${SMOOTH_LIB_ROOT}/smooth/include
$ENV{IDF_PATH}/components/json/cJSON)
$ENV{IDF_PATH}/components/json/cJSON
)

target_link_libraries(${PROJECT_NAME} mbedtls mbedx509 mbedcrypto sodium mock-idf)
set_compile_options(${PROJECT_NAME})
# When building for Linux, mock-idf include paths must be make available.
file(GLOB mock_components LIST_DIRECTORIES true
${CMAKE_CURRENT_LIST_DIR}/../mock-idf/components/* )

foreach(mock ${mock_components})
target_include_directories(${PROJECT_NAME} PUBLIC ${mock}/include)
endforeach()
319 changes: 160 additions & 159 deletions lib/files.cmake

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/smooth/include/smooth/core/filesystem/SPIFlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

#pragma once

#include "esp_vfs.h"
#include <esp_vfs.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"

#include "esp_vfs_fat.h"
#include <esp_vfs_fat.h>

#pragma GCC diagnostic pop

Expand Down
6 changes: 3 additions & 3 deletions lib/smooth/include/smooth/core/util/FixedBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace smooth::core::util
: public FixedBufferBase<T>
{
public:
size_t size() const override
[[nodiscard]] size_t size() const override
{
return buff.size();
}
Expand All @@ -43,7 +43,7 @@ namespace smooth::core::util
return buff.end();
}

T * data() override
T* data() override
{
return &buff[0];
}
Expand All @@ -57,7 +57,7 @@ namespace smooth::core::util
T& operator[](size_t ix)
{
// Prevent going outside buffer
return buff[std::max(0u, std::min(size() - 1, ix))];
return buff[std::max(static_cast<size_t>(0), std::min(size() - 1, ix))];
}

private:
Expand Down
6 changes: 6 additions & 0 deletions mock-idf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# mock-idf

Mock-idf is a minimal mock of Espressif's ESP-IDF framework that allows Smooth-based applications to build on Linux
without while still referencing IDF headers and functions etc. Only the bare minimum is mocked.

Note: This is a **mock**, not a simulator, so don't expect things like I2C to actually give you back data.
10 changes: 10 additions & 0 deletions mock-idf/components/driver/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ esp_err_t gpio_config(const gpio_config_t* /*pGPIOConfig*/)
return ESP_OK;
}

esp_err_t gpio_set_level(gpio_num_t /*gpio_num*/, uint32_t /*level*/)
{
return ESP_OK;
}

int gpio_get_level(gpio_num_t /*gpio_num*/)
{
return 0;
}

esp_err_t gpio_set_pull_mode(gpio_num_t /*gpio_num*/, gpio_pull_mode_t /*pull*/)
{
return ESP_OK;
}

esp_err_t gpio_pullup_en(gpio_num_t /*gpio_num*/)
{
return ESP_OK;
}
74 changes: 74 additions & 0 deletions mock-idf/components/driver/i2c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <driver/i2c.h>

esp_err_t i2c_driver_install(i2c_port_t /*i2c_num*/,
i2c_mode_t /*mode*/,
std::size_t /*slv_rx_buf_len*/,
std::size_t /*slv_tx_buf_len*/,
int /*intr_alloc_flags*/)
{
return ESP_OK;
}

esp_err_t i2c_driver_delete(i2c_port_t /*i2c_num*/)
{
return ESP_OK;
}

esp_err_t i2c_param_config(i2c_port_t /*i2c_num*/, const i2c_config_t* /*i2c_conf*/)
{
return ESP_OK;
}

esp_err_t i2c_master_write(i2c_cmd_handle_t /*cmd_handle*/, uint8_t* /*data*/, std::size_t /*data_len*/, bool /*ack_en*/)
{
return ESP_OK;
}

esp_err_t i2c_master_write_byte(i2c_cmd_handle_t /*cmd_handle*/, uint8_t /*data*/, bool /*ack_en*/)
{
return ESP_OK;
}

esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack)
{
return ESP_OK;
}

esp_err_t i2c_master_read(i2c_cmd_handle_t cmd_handle, uint8_t* data, std::size_t data_len, i2c_ack_type_t ack)
{
return ESP_OK;
}

esp_err_t i2c_master_start(i2c_cmd_handle_t /*cmd_handle*/)
{
return ESP_OK;
}

esp_err_t i2c_master_stop(i2c_cmd_handle_t /*cmd_handle*/)
{
return ESP_OK;
}

esp_err_t i2c_master_cmd_begin(i2c_port_t /*i2c_num*/, i2c_cmd_handle_t /*cmd_handle*/, TickType_t /*ticks_to_wait*/)
{
return ESP_OK;
}

esp_err_t i2c_reset_tx_fifo(i2c_port_t /*i2c_num*/)
{
return ESP_OK;
}

esp_err_t i2c_reset_rx_fifo(i2c_port_t /*i2c_num*/)
{
return ESP_OK;
}

i2c_cmd_handle_t i2c_cmd_link_create()
{
return nullptr;
}

void i2c_cmd_link_delete(i2c_cmd_handle_t /*cmd_handle*/)
{
}
Loading

0 comments on commit 3d5af1e

Please sign in to comment.