From 6013565e66cf5523d59cbbbac05785218da88ee3 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sun, 27 Sep 2026 20:51:07 +0200 Subject: [PATCH] Working communication. --- .gitignore | 2 - CMakeLists.txt | 20 +++++ Config.h | 14 ++++ Pico_I2C.c | 107 +++++++++++++++++++++++++++ Pico_I2C.h | 61 +++++++++++++++ README.md | 3 + master/.vscode/c_cpp_properties.json | 33 --------- master/.vscode/settings.json | 19 ----- master/.vscode/tasks.json | 25 ------- master/CMakeLists.txt | 44 ----------- master/Readme.md | 95 ------------------------ master/pico_sdk_import.cmake | 62 ---------------- master/src/i2c_master.c | 36 --------- master/src/include/i2c_master.h | 22 ------ master/src/main.c | 42 ----------- slave/.vscode/c_cpp_properties.json | 31 -------- slave/.vscode/settings.json | 13 ---- slave/.vscode/tasks.json | 25 ------- slave/CMakeLists.txt | 33 --------- slave/Readme.md | 79 -------------------- slave/pico_sdk_import.cmake | 62 ---------------- slave/src/i2c_slave.c | 72 ------------------ slave/src/include/i2c_slave.h | 28 ------- slave/src/main.c | 39 ---------- 24 files changed, 205 insertions(+), 762 deletions(-) delete mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Config.h create mode 100644 Pico_I2C.c create mode 100644 Pico_I2C.h create mode 100644 README.md delete mode 100644 master/.vscode/c_cpp_properties.json delete mode 100644 master/.vscode/settings.json delete mode 100644 master/.vscode/tasks.json delete mode 100644 master/CMakeLists.txt delete mode 100644 master/Readme.md delete mode 100644 master/pico_sdk_import.cmake delete mode 100644 master/src/i2c_master.c delete mode 100644 master/src/include/i2c_master.h delete mode 100644 master/src/main.c delete mode 100644 slave/.vscode/c_cpp_properties.json delete mode 100644 slave/.vscode/settings.json delete mode 100644 slave/.vscode/tasks.json delete mode 100644 slave/CMakeLists.txt delete mode 100644 slave/Readme.md delete mode 100644 slave/pico_sdk_import.cmake delete mode 100644 slave/src/i2c_slave.c delete mode 100644 slave/src/include/i2c_slave.h delete mode 100644 slave/src/main.c diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 25ce69c..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -master/build -slave/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ee23735 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +project(Pico_I2C C) + +include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake) + +# Sources +add_library(Pico_I2C + Pico_I2C.c +) + +# Includes +target_include_directories(Pico_I2C PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/. +) + +# Libs +target_link_libraries(Pico_I2C + pico_stdlib + hardware_i2c + pico_i2c_slave +) diff --git a/Config.h b/Config.h new file mode 100644 index 0000000..1896a3a --- /dev/null +++ b/Config.h @@ -0,0 +1,14 @@ +#ifndef _CONFIG_H +#define _CONFIG_H + +#define I2C_MASTER_SDA_PIN 0 +#define I2C_MASTER_SCL_PIN 1 + +#define I2C_MASTER_BAUD_RATE 100 * 1000 + +#define I2C_SLAVE_SDA_PIN 0 +#define I2C_SLAVE_SCL_PIN 1 + +#define I2C_SLAVE_BUFFER_SIZE 1 + +#endif // _CONFIG_H \ No newline at end of file diff --git a/Pico_I2C.c b/Pico_I2C.c new file mode 100644 index 0000000..7bcc221 --- /dev/null +++ b/Pico_I2C.c @@ -0,0 +1,107 @@ +#include "Pico_I2C.h" + +#include +#include +#include + +#include "Config.h" + +#include + +void init_i2c_master(i2c_master_t *i2c_master, i2c_inst_t *i2c_inst) +{ + i2c_master->i2c_inst = i2c_inst; + + // Init GPIO pins + gpio_init(I2C_MASTER_SDA_PIN); + gpio_init(I2C_MASTER_SCL_PIN); + gpio_set_function(I2C_MASTER_SDA_PIN, GPIO_FUNC_I2C); + gpio_set_function(I2C_MASTER_SCL_PIN, GPIO_FUNC_I2C); + gpio_pull_up(I2C_MASTER_SDA_PIN); + gpio_pull_up(I2C_MASTER_SCL_PIN); + + i2c_init(i2c_master->i2c_inst, I2C_MASTER_BAUD_RATE); +} + +void exit_i2c_master(i2c_master_t *i2c_master) +{ + // Reset GPIO pins + gpio_deinit(I2C_MASTER_SDA_PIN); + gpio_deinit(I2C_MASTER_SCL_PIN); + gpio_set_function(I2C_MASTER_SDA_PIN, GPIO_FUNC_NULL); + gpio_set_function(I2C_MASTER_SCL_PIN, GPIO_FUNC_NULL); + + i2c_deinit(i2c_master->i2c_inst); +} + +static i2c_slave_t *i2c_slaves[2]; + +static inline void i2c_slave_buffer_handler(i2c_inst_t *i2c, i2c_slave_event_t event) +{ + i2c_slave_t *i2c_slave = i2c_slaves[i2c_get_index(i2c)]; + + switch(event) + { + case I2C_SLAVE_RECEIVE: // master has written some data + if(!i2c_slave->reg_written) + { + // writes always start with the memory address + i2c_slave->reg = i2c_read_byte_raw(i2c); + i2c_slave->reg_written = true; + } + else + { + // save into memory + i2c_slave->buffer[i2c_slave->reg] = i2c_read_byte_raw(i2c); + i2c_slave->reg++; + } + + break; + + case I2C_SLAVE_REQUEST: // master is requesting data + // load from memory + i2c_write_byte_raw(i2c, i2c_slave->buffer[i2c_slave->reg]); + i2c_slave->reg++; + + break; + + case I2C_SLAVE_FINISH: // master has signalled Stop / Restart + i2c_slave->reg_written = false; + + break; + + default: + break; + } +} + +void init_i2c_slave(i2c_slave_t *i2c_slave, i2c_inst_t *i2c_inst, uint8_t address) +{ + i2c_slave->i2c_inst = i2c_inst; + + i2c_slaves[i2c_get_index(i2c_inst)] = i2c_slave; + + // Init GPIO pins + gpio_init(I2C_SLAVE_SDA_PIN); + gpio_init(I2C_SLAVE_SCL_PIN); + gpio_set_function(I2C_SLAVE_SDA_PIN, GPIO_FUNC_I2C); + gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_I2C); + + i2c_init(i2c_slave->i2c_inst, 0); + + // New SDK method to init i2c slave + i2c_slave_init(i2c_slave->i2c_inst, address, &i2c_slave_buffer_handler); +} + +void exit_i2c_slave(i2c_slave_t *i2c_slave) +{ + // Reset GPIO pins + gpio_deinit(I2C_SLAVE_SDA_PIN); + gpio_deinit(I2C_SLAVE_SCL_PIN); + gpio_set_function(I2C_SLAVE_SDA_PIN, GPIO_FUNC_NULL); + gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_NULL); + + // New SDK method to reset i2c slave + i2c_slave_deinit(i2c_slave->i2c_inst); + i2c_deinit(i2c_slave->i2c_inst); +} diff --git a/Pico_I2C.h b/Pico_I2C.h new file mode 100644 index 0000000..b431d92 --- /dev/null +++ b/Pico_I2C.h @@ -0,0 +1,61 @@ +#ifndef _I2C_SLAVE_H +#define _I2C_SLAVE_H + +#include + +/** + * @brief I2c master struct. + * + * @param i2c_inst Instance i2c master initialized on + */ +typedef struct i2c_master { + i2c_inst_t *i2c_inst; +} i2c_master_t; + +/** + * @brief I2c slave struct. + * + * @param i2c_inst Instance i2c slave initialized on + * @param reg Currently pointed register + * @param reg_written Communication option + * @param buffer Memory buffer for communication + */ +typedef struct i2c_slave { + i2c_inst_t *i2c_inst; + uint8_t reg; + bool reg_written; + uint8_t buffer[256]; +} i2c_slave_t; + +/** + * @brief Init an i2c instance as master. + * + * @param i2c_master Pointer to a i2c master struct + * @param i2c_inst Instance to initialise master on + */ +void init_i2c_master(i2c_master_t *i2c_master, i2c_inst_t *i2c_inst); + +/** + * @brief Reset an i2c master to default settings. + * + * @param i2c_master Pointer to the i2c master struct to exit + */ +void exit_i2c_master(i2c_master_t *i2c_master); + +/** + * @brief Init an i2c instance as slave. + * + * @param i2c_slave Pointer to an i2c slave struct + * @param i2c_inst Instance to initialise slave on + * @param address The address of the slave + */ +void init_i2c_slave(i2c_slave_t *i2c_slave, i2c_inst_t *i2c_inst, uint8_t address); + +/** + * @brief Reset an i2c slave to default settings. + * + * @param i2c_slave Pointer to the i2c slave struct to exit + */ +void exit_i2c_slave(i2c_slave_t *i2c_slave); + +#endif // _I2C_SLAVE_H \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5e95ce4 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Pico_I2C_Lib + +Librairy to abstract i2c communication for the RPI Pico and RPI Pico W. \ No newline at end of file diff --git a/master/.vscode/c_cpp_properties.json b/master/.vscode/c_cpp_properties.json deleted file mode 100644 index 643d42c..0000000 --- a/master/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "env": { - "myDefaultIncludePath": [ - "${workspaceFolder}/src/include/", - "${env:PICO_SDK_PATH}/src/**/include/", - "${env:PICO_SDK_PATH}/lib/**/include/", - "${workspaceFolder}/build/generated/pico_base/" - ], - "myCompilerPath": "/usr/bin/arm-none-eabi-gcc" - }, - - "configurations": [ - { - "name": "Linux", - "intelliSenseMode": "linux-gcc-arm", - "includePath": [ - "${myDefaultIncludePath}" - ], - "compilerPath": "/usr/bin/arm-none-eabi-gcc", - "cStandard": "c11", - "cppStandard": "c++17", - "browse": { - "path": [ - "${workspaceFolder}" - ], - "limitSymbolsToIncludedHeaders": true, - "databaseFilename": "" - }, - "configurationProvider": "ms-vscode.cmake-tools" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/master/.vscode/settings.json b/master/.vscode/settings.json deleted file mode 100644 index cb0aebb..0000000 --- a/master/.vscode/settings.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "files.associations": { - "*.md": "markdown", - "binary_info.h": "c", - "i2c.h": "c", - "stdlib.h": "c", - "stdint.h": "c", - "gyro.h": "c", - "motors.h": "c", - "motion_control.h": "c", - "i2c_master.h": "c", - "udp_client.h": "c", - "udp_payload.h": "c", - "udp.h": "c", - "opt.h": "c", - "cyw43_arch.h": "c", - "wifi_operator.h": "c" - } -} \ No newline at end of file diff --git a/master/.vscode/tasks.json b/master/.vscode/tasks.json deleted file mode 100644 index 1255bf5..0000000 --- a/master/.vscode/tasks.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "tasks": [ - { - "type": "shell", - "command": "cd build; cmake -DPICO_BOARD=pico_w ..; make", - "label": "CMake in build/", - "problemMatcher": [], - "group": { - "kind": "build", - "isDefault": false - } - }, - { - "type": "shell", - "command": "cd build; cmake -DPICO_BOARD=pico_w ..; make Flash", - "label": "CMake & Make & Flash", - "problemMatcher": [], - "group": { - "kind": "build", - "isDefault": true - } - } - ], - "version": "2.0.0" -} \ No newline at end of file diff --git a/master/CMakeLists.txt b/master/CMakeLists.txt deleted file mode 100644 index 2c2b173..0000000 --- a/master/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -cmake_minimum_required(VERSION 3.13) - -include(pico_sdk_import.cmake) - -project(main_controller C CXX ASM) -set(CMAKE_C_STANDARD 11) -set(CMAKE_CXX_STANDARD 17) - -set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR}) - -# Définir explicitement la carte comme Pico W -set(PICO_BOARD pico_w) -if(NOT DEFINED PICO_BOARD) - add_definitions(-DPICO_BOARD=${PICO_BOARD}) -endif() - -pico_sdk_init() - -add_executable(main_controller - src/main.c - src/i2c_master.c -) - -target_include_directories(main_controller PRIVATE - ${CMAKE_CURRENT_LIST_DIR}/src - ${CMAKE_CURRENT_LIST_DIR}/src/include -) - -target_link_libraries(main_controller - hardware_i2c - hardware_pwm - hardware_uart - pico_stdlib -) - -pico_enable_stdio_usb(main_controller 1) -pico_enable_stdio_uart(main_controller 1) - -pico_add_extra_outputs(main_controller) - -add_custom_target(Flash - DEPENDS main_controller - COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/main_controller.uf2 -) diff --git a/master/Readme.md b/master/Readme.md deleted file mode 100644 index 76447b8..0000000 --- a/master/Readme.md +++ /dev/null @@ -1,95 +0,0 @@ -Motion controler code for the RPI Pico (RP2040) -=============================================== - -This project is the main controller firmware for the RPI Pico (RP2040), designed for the Eurobot 2025 Cup. - - -I2C description ------------------------------------------------ - -The robot’s I2C communication works as follows: - * Send the device address + R/W bit (to select read or write mode). - * Send the target register address (to read from or write to). - * Read or write the register data. Multiple registers can be read/written sequentially, with the address auto-incrementing after each operation. - -This code is designed to be the master in the i2c communication. - -|Register |R/W|Description |Encoding | -|---------|:-:|-------------------------------|:-----------------:| -| 0x00 | W | Speed motor 1 |**-128** - **127** | -| 0x01 | W | Speed motor 2 |**-128** - **127** | -| 0x02 | W | Speed motor 3 |**-128** - **127** | -| 0x03 | W | Speed motor 4 |**-128** - **127** | -| 0x04 | W | Servo 1 position selection | **0** - **1** | -| 0x05 | W | Servo 2 position selection | **0** - **1** | -| 0x06 | W | Servo 3 position selection | **0** - **1** | -| 0x07 | W | Servo 4 position selection | **0** - **1** | - - -Motors communication description ------------------------------------------------ - -Motors are «connected» to their respective I2C buffer address. - -To control a motor you need to write data to its adress of the form : - ->```C ->int8_t speed; ->``` - -Value goes from **-128** to **127**. - - -Servo motors communication description ------------------------------------------------ - -Servo motors are «connected» to their respective I2C buffer address. - -To control a servo motor you need to write data to its adress of the form : - ->```C ->uint8_t close; ->``` - -Value is 0 or 1 for the open or the close pos. - - -Internet communication description ------------------------------------------------ - -The robot main_controller is a client connected to the wireless controller which is an udp server host. A buffer is used to store data received from host. - -Speed on X and Y axis are not depending of the robot orientation. - -Servo motors keep the same byte address in i2c buffer and udp payload. - -|Byte |Description |Encoding | -|---------|-------------------------------------------------|:-----------------:| -| 0x00-01 | Robot angle (0x00 is the last significant byte) |**-180** - **180** | -| 0x02 | Speed x axis |**-128** - **127** | -| 0x03 | Speed y axis |**-128** - **127** | -| 0x04 | Servo 1 position selection | **0** - **1** | -| 0x05 | Servo 2 position selection | **0** - **1** | -| 0x06 | Servo 3 position selection | **0** - **1** | -| 0x07 | Servo 4 position selection | **0** - **1** | - - -Pinout description ------------------------------------------------ - -|Pin |Description |GPIO Type | -|----|----------------------------------|-----------| -| 4 | I2C Bus SDA | I2C | -| 5 | I2C Bus SCL | I2C | - - -Motors placement ------------------------------------------------ - - ,-~***~-, - /1 2\ - | | - | | - | | - \3 4/ - `-.....-' diff --git a/master/pico_sdk_import.cmake b/master/pico_sdk_import.cmake deleted file mode 100644 index 28efe9e..0000000 --- a/master/pico_sdk_import.cmake +++ /dev/null @@ -1,62 +0,0 @@ -# This is a copy of /external/pico_sdk_import.cmake - -# This can be dropped into an external project to help locate this SDK -# It should be include()ed prior to project() - -if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) - set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) - message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) - set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) - message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) - set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) - message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") -endif () - -set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") -set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") -set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") - -if (NOT PICO_SDK_PATH) - if (PICO_SDK_FETCH_FROM_GIT) - include(FetchContent) - set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) - if (PICO_SDK_FETCH_FROM_GIT_PATH) - get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") - endif () - FetchContent_Declare( - pico_sdk - GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG master - ) - if (NOT pico_sdk) - message("Downloading Raspberry Pi Pico SDK") - FetchContent_Populate(pico_sdk) - set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) - endif () - set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) - else () - message(FATAL_ERROR - "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." - ) - endif () -endif () - -get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") -if (NOT EXISTS ${PICO_SDK_PATH}) - message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") -endif () - -set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) -if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) - message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") -endif () - -set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) - -include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/master/src/i2c_master.c b/master/src/i2c_master.c deleted file mode 100644 index 0f79ee4..0000000 --- a/master/src/i2c_master.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "include/i2c_master.h" - -#include -#include - -void i2c_master_init(void) -{ - gpio_set_function(I2C_MASTER_SDA_PIN, GPIO_FUNC_I2C); - gpio_set_function(I2C_MASTER_SCL_PIN, GPIO_FUNC_I2C); - - gpio_pull_up(I2C_MASTER_SDA_PIN); - gpio_pull_up(I2C_MASTER_SCL_PIN); - - i2c_init(I2C_MASTER_INSTANCE, I2C_MASTER_BAUD_RATE); -} - -void i2c_master_deinit(void) -{ - i2c_deinit(I2C_MASTER_INSTANCE); -} - -inline int i2c_master_write(uint8_t address, const uint8_t *src, size_t len) -{ - return i2c_write_blocking(I2C_MASTER_INSTANCE, address, src, len, false); -} - -inline void i2c_master_read(uint8_t address, uint8_t *dst, size_t len) -{ - i2c_read_blocking(I2C_MASTER_INSTANCE, address, dst, len, false); -} - -inline void i2c_master_read_reg(uint8_t address, uint8_t reg, uint8_t *dst, size_t len) -{ - i2c_master_write(address, ®, 1); - i2c_master_read(address, dst, len); -} \ No newline at end of file diff --git a/master/src/include/i2c_master.h b/master/src/include/i2c_master.h deleted file mode 100644 index 899c0bd..0000000 --- a/master/src/include/i2c_master.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef I2C_MASTER_H -#define I2C_MASTER_H - -#include - -#define I2C_MASTER_SDA_PIN 16 -#define I2C_MASTER_SCL_PIN 17 -#define I2C_MASTER_INSTANCE i2c0 -#define I2C_MASTER_BAUD_RATE 100 * 1000 - -// Init master i2c -void i2c_master_init(void); -// Deinit master i2c -void i2c_master_deinit(void); -// Send [src] of [len] to [address] and close communication -int i2c_master_write(uint8_t address, const uint8_t *src, size_t len); -// Receive [dst] of [len] from [address] and close communication -void i2c_master_read(uint8_t address, uint8_t *dst, size_t len); -// Send [reg] and receive data in [dst] and close communition -void i2c_master_read_reg(uint8_t address, uint8_t reg, uint8_t *dst, size_t len); - -#endif // I2C_MASTER_H \ No newline at end of file diff --git a/master/src/main.c b/master/src/main.c deleted file mode 100644 index 914085c..0000000 --- a/master/src/main.c +++ /dev/null @@ -1,42 +0,0 @@ -/* *\ - Copyrights 2025 - Riombotique -\* */ - -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ -* Code du RPI Pico principale gérant les differentes entrées-sorties. * -* Ce Pico est un maitre pilotant le gyroscope, l'internet et le motion controller.* -\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "include/i2c_master.h" -#include -#include - -void main(void) -{ - stdio_init_all(); - - sleep_ms(5000); - - puts("STDIO INIT DONE"); - - i2c_master_init(); - - puts("I2C MASTER INIT DONE"); - - bool led_state = false; - - while(true) - { - uint8_t data[] = {0x00, led_state}; - int ret = i2c_write_blocking(I2C_MASTER_INSTANCE, 0x09, data, 2, false); - - if(ret == PICO_ERROR_GENERIC) - puts("I2C ERROR GENERIC"); - else - printf("Written %d bytes\n", ret); - - led_state = !led_state; - sleep_ms(1000); - } -} diff --git a/slave/.vscode/c_cpp_properties.json b/slave/.vscode/c_cpp_properties.json deleted file mode 100644 index 8cc4ce6..0000000 --- a/slave/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "env": { - "myDefaultIncludePath": [ - "${env:PICO_SDK_PATH}/src/**/include/", - "${workspaceFolder}/build/generated/pico_base/" - ], - "myCompilerPath": "/usr/bin/arm-none-eabi-gcc" - }, - "configurations": [ - { - "name": "Linux", - "intelliSenseMode": "linux-gcc-arm", - "includePath": [ - "${myDefaultIncludePath}", - "${workspaceFolder}/build/" - ], - "compilerPath": "/usr/bin/arm-none-eabi-gcc", - "cStandard": "c11", - "cppStandard": "c++17", - "browse": { - "path": [ - "${workspaceFolder}" - ], - "limitSymbolsToIncludedHeaders": true, - "databaseFilename": "" - }, - "configurationProvider": "ms-vscode.cmake-tools" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/slave/.vscode/settings.json b/slave/.vscode/settings.json deleted file mode 100644 index 9fcb311..0000000 --- a/slave/.vscode/settings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "files.associations": { - "*.md": "markdown", - "binary_info.h": "c", - "i2c.h": "c", - "time.h": "c", - "stdlib.h": "c", - "robot.h": "c", - "stdio.h": "c", - "i2c_buffer.h": "c", - "motors.h": "c" - } -} \ No newline at end of file diff --git a/slave/.vscode/tasks.json b/slave/.vscode/tasks.json deleted file mode 100644 index c98bf9b..0000000 --- a/slave/.vscode/tasks.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "tasks": [ - { - "type": "shell", - "command": "cd build; cmake ../; make", - "label": "CMake in build/", - "problemMatcher": [], - "group": { - "kind": "build", - "isDefault": false - } - }, - { - "type": "shell", - "command": "cd build; cmake ../; make Flash", - "label": "CMake & Make & Flash", - "problemMatcher": [], - "group": { - "kind": "build", - "isDefault": true - } - } - ], - "version": "2.0.0" -} \ No newline at end of file diff --git a/slave/CMakeLists.txt b/slave/CMakeLists.txt deleted file mode 100644 index 1e09b35..0000000 --- a/slave/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -cmake_minimum_required(VERSION 3.13) - -include(pico_sdk_import.cmake) - -project(motion_controller C CXX ASM) -set(CMAKE_C_STNDARD 11) -set(CMAKE_CXX_STANDARD 17) - -set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR}) - -pico_sdk_init() - -add_executable(motion_controller - src/main.c - src/i2c_slave.c -) - -target_link_libraries(motion_controller - pico_stdlib - hardware_uart - hardware_i2c - pico_i2c_slave -) - -pico_enable_stdio_usb(motion_controller 1) -pico_enable_stdio_uart(motion_controller 1) - -pico_add_extra_outputs(motion_controller) - -add_custom_target(Flash - DEPENDS motion_controller - COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/motion_controller.uf2 -) diff --git a/slave/Readme.md b/slave/Readme.md deleted file mode 100644 index bb0c0ef..0000000 --- a/slave/Readme.md +++ /dev/null @@ -1,79 +0,0 @@ -Motion controler code for the RPI Pico (RP2040) -=============================================== - -This project is a motion controller firmware for the RPI Pico (RP2040), designed for plug-and-play use with modular code and a clear architecture. - - -I2C description ------------------------------------------------ - -The robot’s I2C communication works as follows: - * Send the device address + R/W bit (to select read or write mode). - * Send the target register address (to read from or write to). - * Read or write the register data. Multiple registers can be read/written sequentially, with the address auto-incrementing after each operation. - -This firmware is coded to be a slave when adressed. Its address is **0x09** which you can change if there is any conflict whit other hardware. - -|Adress |R/W|Description |Encoding | -|-------|:-:|-------------------------------|:-----------------:| -| 0x00 | W | Speed motor 1 |**-128** - **127** | -| 0x01 | W | Speed motor 2 |**-128** - **127** | -| 0x02 | W | Speed motor 3 |**-128** - **127** | -| 0x03 | W | Speed motor 4 |**-128** - **127** | -| 0x04 | W | Servo 1 position selection | **0** - **1** | -| 0x05 | W | Servo 2 position selection | **0** - **1** | -| 0x06 | W | Servo 3 position selection | **0** - **1** | -| 0x07 | W | Servo 4 position selection | **0** - **1** | - - -Motors communication description ------------------------------------------------ - -Motors are «connected» to their respective I2C buffer address. - -To control a motor you need to write data to its adress of the form : - ->```C ->int8_t speed; ->``` - -Value goes from **-128** to **127**. - - -Servo motors communication description ------------------------------------------------ - -Servo motors are «connected» to their respective I2C buffer address. - -To control a servo motor you need to write data to its adress of the form : - ->```C ->uint8_t close; ->``` - -Value is 0 or 1 for the open pos or the close pos. - - -Pinout description ------------------------------------------------ - -|Pin |Description |GPIO Type | -|----|----------------------------------|-----------| -| 0 | Motor1 Speed | PWM | -| 1 | Motor2 Speed | PWM | -| 2 | Motor3 Speed | PWM | -| 3 | Motor4 Speed | PWM | -| 4 | Motor1 Dir1 | OUTPUT | -| 5 | Motor1 Dir2 | OUTPUT | -| 6 | Motor2 Dir1 | OUTPUT | -| 7 | Motor2 Dir2 | OUTPUT | -| 8 | Motor3 Dir1 | OUTPUT | -| 9 | Motor3 Dir2 | OUTPUT | -| 10 | Motor4 Dir1 | OUTPUT | -| 11 | Motor5 Dir2 | OUTPUT | -| 12 | Servo1 Angle | PWM | -| 13 | Servo2 Angle | PWM | -| 14 | Servo3 Angle | PWM | -| 15 | Servo4 Angle | PWM | -| 21 | I2C Bus SDA | I2C | -| 20 | I2C Bus SCL | I2C | diff --git a/slave/pico_sdk_import.cmake b/slave/pico_sdk_import.cmake deleted file mode 100644 index 28efe9e..0000000 --- a/slave/pico_sdk_import.cmake +++ /dev/null @@ -1,62 +0,0 @@ -# This is a copy of /external/pico_sdk_import.cmake - -# This can be dropped into an external project to help locate this SDK -# It should be include()ed prior to project() - -if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) - set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) - message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) - set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) - message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) - set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) - message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") -endif () - -set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") -set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") -set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") - -if (NOT PICO_SDK_PATH) - if (PICO_SDK_FETCH_FROM_GIT) - include(FetchContent) - set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) - if (PICO_SDK_FETCH_FROM_GIT_PATH) - get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") - endif () - FetchContent_Declare( - pico_sdk - GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG master - ) - if (NOT pico_sdk) - message("Downloading Raspberry Pi Pico SDK") - FetchContent_Populate(pico_sdk) - set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) - endif () - set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) - else () - message(FATAL_ERROR - "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." - ) - endif () -endif () - -get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") -if (NOT EXISTS ${PICO_SDK_PATH}) - message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") -endif () - -set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) -if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) - message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") -endif () - -set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) - -include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/slave/src/i2c_slave.c b/slave/src/i2c_slave.c deleted file mode 100644 index b43da03..0000000 --- a/slave/src/i2c_slave.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2021 Valentin Milea - * - * SPDX-License-Identifier: MIT - */ - -#include "include/i2c_slave.h" - -#include -#include -#include "pico/i2c_slave.h" - -#include - -extern i2c_buffer_t i2c_buffer; - -void i2c_slave_buffer_handler(i2c_inst_t *i2c, i2c_slave_event_t event) -{ - switch(event) - { - case I2C_SLAVE_RECEIVE: // master has written some data - if(!i2c_buffer.buffer_reg_written) - { - // writes always start with the memory address - i2c_buffer.buffer_reg = i2c_read_byte_raw(I2C_SLAVE_INSTANCE); - i2c_buffer.buffer_reg_written = true; - } - else - { - // save into memory - i2c_buffer.buffer[i2c_buffer.buffer_reg] = i2c_read_byte_raw(I2C_SLAVE_INSTANCE); - i2c_buffer.buffer_reg++; - } - break; - - case I2C_SLAVE_REQUEST: // master is requesting data - // load from memory - i2c_write_byte_raw(I2C_SLAVE_INSTANCE, i2c_buffer.buffer[i2c_buffer.buffer_reg]); - i2c_buffer.buffer_reg++; - break; - - case I2C_SLAVE_FINISH: // master has signalled Stop / Restart - i2c_buffer.buffer_reg_written = false; - break; - - default: - break; - } -} - -void init_i2c_slave(void) -{ - // Init GPIO pins - gpio_init(I2C_SLAVE_SDA_PIN); - gpio_init(I2C_SLAVE_SCL_PIN); - gpio_set_function(I2C_SLAVE_SDA_PIN, GPIO_FUNC_I2C); - gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_I2C); - - i2c_init(I2C_SLAVE_INSTANCE, 0); - // New SDK method to init i2c slave - i2c_slave_init(I2C_SLAVE_INSTANCE, I2C_SLAVE_ADDRESS, &i2c_slave_buffer_handler); -} - -void deinit_i2c_slave(void) -{ - // Reset GPIO pins - gpio_set_function(I2C_SLAVE_SDA_PIN, GPIO_FUNC_NULL); - gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_NULL); - - // New SDK method to reset i2c slave - i2c_slave_deinit(I2C_SLAVE_INSTANCE); -} diff --git a/slave/src/include/i2c_slave.h b/slave/src/include/i2c_slave.h deleted file mode 100644 index 017d4bd..0000000 --- a/slave/src/include/i2c_slave.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2021 Valentin Milea - * - * SPDX-License-Identifier: MIT - */ - -#ifndef I2C_SLAVE_H -#define I2C_SLAVE_H - -#include - -#define I2C_SLAVE_SDA_PIN 16 -#define I2C_SLAVE_SCL_PIN 17 -#define I2C_SLAVE_INSTANCE i2c0 -#define I2C_SLAVE_ADDRESS 0x09 - -typedef struct i2c_buffer_t { - uint8_t buffer[256]; - uint8_t buffer_reg; - bool buffer_reg_written; -} i2c_buffer_t; - -// Init i2c slave with default parameters -void init_i2c_slave(void); -// Deinit i2c slave -void deinit_i2c_slave(void); - -#endif // I2C_SLAVE_H \ No newline at end of file diff --git a/slave/src/main.c b/slave/src/main.c deleted file mode 100644 index 0a37250..0000000 --- a/slave/src/main.c +++ /dev/null @@ -1,39 +0,0 @@ -/* *\ - Copyrights 2025 - Riombotique -\* */ - -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ -* Code du RPI Pico gérant les different Actionneurs. * -* Ce Pico est un esclave piloté par le Pico Principal. * -\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include -#include -#include "include/i2c_slave.h" - -i2c_buffer_t i2c_buffer; - -int main(void) -{ - stdio_init_all(); - - sleep_ms(1000); - - puts("STDIO INIT ALL DONE"); - - init_i2c_slave(); - - puts("SLAVE INIT DONE"); - - gpio_init(PICO_DEFAULT_LED_PIN); - gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); - - while(true) - { - gpio_put(PICO_DEFAULT_LED_PIN, i2c_buffer.buffer[2]); - sleep_ms(10); - } - - return 0; -}