Skip to content

Commit

Permalink
Merge pull request #38 from tropicsquare/37-add-hal-support-for-raspb…
Browse files Browse the repository at this point in the history
…erry-pi

37 add hal support for raspberry pi
  • Loading branch information
pavelpolach321 authored Feb 14, 2025
2 parents 0a5d9d9 + 22408df commit 71c83d0
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- ECC_SLOT1-32 numbering chabged to ECC_SLOT_0-31
- lt_handshake() function renamed to lt_session_start()
- Unit tests organization was updated
- Unit tests and platform tests organization was updated
- Included lt_l2_api_structs.h and lt_l3_api_structs.h are automatically generated
- SH0 pairing keypair now reflects first batch of TROPIC01 devices
- Changed logging format in examples.
Expand All @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- libtropic API: lt_update_mode()
- Example folder and test folder is reworked
- option LT_ADD_EXAMPLES controls if example's folder code is a part of compilation
- port/ support and tests/platform/ example for Raspberry Pi

### Fixed

Expand Down
7 changes: 4 additions & 3 deletions examples/lt_ex_test_reversible.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @license For the license see file LICENSE.txt file in the root directory of this source tree.
*/

#include "inttypes.h"
#include "string.h"

#include "libtropic.h"
Expand Down Expand Up @@ -190,7 +191,7 @@ void lt_ex_test_reversible(void)
LT_ASSERT(LT_OK, read_whole_R_config(&h, &r_config_read));
// Print r config
for (int i=0; i<27;i++) {
LT_LOG(" %s, %08lX", get_conf_desc(i), r_config_read.obj[i]);
LT_LOG(" %s, %08" PRIX32, get_conf_desc(i), r_config_read.obj[i]);
}

LT_LOG("%s", "write_whole_R_config()");
Expand All @@ -206,7 +207,7 @@ void lt_ex_test_reversible(void)
LT_ASSERT(LT_OK, read_whole_R_config(&h, &r_config_read));
// Print r config
for (int i=0; i<27;i++) {
LT_LOG(" %s, %08lX", get_conf_desc(i), r_config_read.obj[i]);
LT_LOG(" %s, %08" PRIX32, get_conf_desc(i), r_config_read.obj[i]);
}

LT_LOG("%s", "lt_ping() ");
Expand Down Expand Up @@ -298,7 +299,7 @@ void lt_ex_test_reversible(void)
LT_ASSERT(LT_OK, read_whole_R_config(&h, &r_config_read));
// Print r config
for (int i=0; i<27;i++) {
LT_LOG(" %s, %08lX", get_conf_desc(i), r_config_read.obj[i]);
LT_LOG(" %s, %08" PRIX32, get_conf_desc(i), r_config_read.obj[i]);
}

LT_LOG("lt_session_abort()");
Expand Down
122 changes: 122 additions & 0 deletions hal/port/unix/lt_port_raspberrypi_wiringpi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* @file lt_port_raspberrypi_spi.c
* @author Tropic Square s.r.o.
* @brief L1 implementation for raspberrypi. Expects wiringPi library to be
* installed on Raspberry Pi, for more info about this library check https://github.com/WiringPi/WiringPi/tree/master
*
* @license For the license see file LICENSE.txt file in the root directory of this source tree.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <wiringPi.h> /// for GPIO control
#include <wiringPiSPI.h> /// For SPI control

#include "libtropic_common.h"
#include "libtropic_port.h"

// CS is controlled separately
#define GPIO_CS 14
#define SPI_SPEED_HZ 5000000

// File descriptor, used in init and deinit
int fd = 0;

//#define NDEBUG
#ifdef NDEBUG
# define LOG_OUT(f_, ...) printf("[TCP] "f_, ##__VA_ARGS__)
# define LOG_ERR(f_, ...) fprintf(stderr, "ERROR: " f_, ##__VA_ARGS__)
# define LOG_U8_ARRAY(arr, len) \
for (int i = 0; i < len; i++) \
{ \
printf("%02x ", arr[i]); \
} \
printf("\r\n");
#else
# define LOG_OUT(...)
# define LOG_ERR(...)
# define LOG_U8_ARRAY(...)
#endif

lt_ret_t lt_port_delay (lt_handle_t *h, uint32_t wait_time_msecs)
{
UNUSED(h);
LOG_OUT("-- Waiting for the target.\n");

usleep(wait_time_msecs*1000);

return LT_OK;
}

lt_ret_t lt_port_random_bytes(uint32_t *buff, uint16_t len) {

for(int i=0; i<len; i++) {
buff[i] = (uint16_t)rand();
}

return LT_OK;
}

lt_ret_t lt_port_init(lt_handle_t *h)
{
UNUSED(h);
memset(h, 0, sizeof(lt_handle_t));

// Pseudo RNG init
srand(time(NULL));

// Setup CS pin
wiringPiSetupGpio();
pinMode(14, OUTPUT);
digitalWrite(GPIO_CS, HIGH);

// Setup SPI, returns fd, error if -1 is returned
fd = wiringPiSPISetup(0, SPI_SPEED_HZ);
if(fd < 0) {
return LT_FAIL;
}

return LT_OK;
}

lt_ret_t lt_port_deinit(lt_handle_t *h)
{
UNUSED(h);

close(fd);

return LT_OK;
}

lt_ret_t lt_port_spi_transfer (lt_handle_t *h, uint8_t offset, uint16_t tx_data_length, uint32_t timeout)
{
UNUSED(timeout);
LOG_OUT("-- Transfer of %d B\n", tx_data_length);

int ret = wiringPiSPIxDataRW (0, 0, h->l2_buff + offset, tx_data_length);

return LT_OK;
}

lt_ret_t lt_port_spi_csn_low (lt_handle_t *h)
{
UNUSED(h);
LOG_OUT("-- Driving Chip Select to Low.\n");

digitalWrite(GPIO_CS, LOW);

return LT_OK;
}

lt_ret_t lt_port_spi_csn_high (lt_handle_t *h)
{
UNUSED(h);
LOG_OUT("-- Driving Chip Select to High.\n");

digitalWrite(GPIO_CS, HIGH);

return LT_OK;
}
File renamed without changes.
93 changes: 93 additions & 0 deletions tests/platforms/libtropic-raspberrypi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
cmake_minimum_required(VERSION 3.21.0)


###########################################################################
# #
# Paths and setup #
# #
###########################################################################

#set(CMAKE_BUILD_TYPE Debug)

set(PATH_TREZOR_CRYPTO "../../vendor/trezor_crypto/")
set(PATH_LIBTROPIC "../../../")


###########################################################################
# #
# Define project's name #
# #
###########################################################################

project(libtropic_unix_spi_example
VERSION 0.1.0
DESCRIPTION "Example of libtropic's usage in Unix environment"
LANGUAGES C)


###########################################################################
# #
# Sources defined by this project #
# #
###########################################################################

add_executable(run main.c
${CMAKE_CURRENT_SOURCE_DIR}/${PATH_LIBTROPIC}hal/port/unix/lt_port_raspberrypi_wiringpi.c
)


###########################################################################
# #
# Define what examples will be compiled into main(). #
# If any of examples is enabled, libtropic is compiled with #
# examples exposed. #
# #
###########################################################################

# -DLT_EX_HW_WALLET=1
if(LT_EX_HW_WALLET)
set(LT_ADD_EXAMPLES ON)
target_compile_definitions(run PRIVATE LT_EX_HW_WALLET)
endif()
# -DLT_EX_HELLO_WORLD=1
if(LT_EX_HELLO_WORLD)
set(LT_ADD_EXAMPLES ON)
target_compile_definitions(run PRIVATE LT_EX_HELLO_WORLD)
endif()
# -DLT_EX_TEST_REVERSIBLE=1
if(LT_EX_TEST_REVERSIBLE)
set(LT_ADD_EXAMPLES ON)
target_compile_definitions(run PRIVATE LT_EX_TEST_REVERSIBLE)
endif()
# --DLT_EX_TEST_IREVERSIBLE=1
if(LT_EX_TEST_IREVERSIBLE)
set(LT_ADD_EXAMPLES ON)
target_compile_definitions(run PRIVATE LT_EX_TEST_IREVERSIBLE)
endif()

###########################################################################
# #
# Add libtropic and set it up #
# #
###########################################################################

# Use trezor crypto as a source of backend cryptography code
set(LT_USE_TREZOR_CRYPTO ON)

# Add path to libtropic's repository root folder
add_subdirectory(${PATH_LIBTROPIC} "libtropic")

# Customize libtropic's compilation
target_compile_options(tropic PRIVATE -Wall)
target_compile_options(tropic PRIVATE -ffunction-sections -fdata-sections)
target_compile_options(tropic PRIVATE -Wno-implicit-function-declaration)


###########################################################################
# #
# Link executable #
# #
###########################################################################

target_link_options(run PRIVATE -Wl,--gc-sections)
target_link_libraries(run PRIVATE tropic wiringPi)
78 changes: 78 additions & 0 deletions tests/platforms/libtropic-raspberrypi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Unix-spi example

Wire chip like this:

```
Connector J8 on Raspberry Pi:
3v3 --> 3V3 (1) (2) 5V
GPIO2 (3) (4) 5V
GPIO3 (5) (6) GND
GPIO4 (7) (8) GPIO14 <-- CS
GND --> GND (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND
GPIO22 (15) (16) GPIO23
3V3 (17) (18) GPIO24
MOSI --> GPIO10 (19) (20) GND
MISO --> GPIO9 (21) (22) GPIO25
CLK --> GPIO11 (23) (24) GPIO8
GND (25) (26) GPIO7
GPIO0 (27) (28) GPIO1
GPIO5 (29) (30) GND
GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
GND (39) (40) GPIO21
```

This example is ready to be compiled on Raspberry Pi with official raspbian image. Hardware spi0 is used, but chip select pin is controlled separately.

# Dependencies

On raspberry pi install wiringPi from released package

```bash
$ wget https://github.com/WiringPi/WiringPi/releases/download/3.14/wiringpi_3.14_arm64.deb

$ sudo apt install ./wiringpi_3.14_arm64.deb
```

Check out installed version:

```bash
$ gpio -v
gpio version: 3.14
Copyright (c) 2012-2025 Gordon Henderson and contributors
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Hardware details:
Type: Pi 3, Revision: 02, Memory: 1024MB, Maker: Sony UK

System details:
* Device tree present.
Model: Raspberry Pi 3 Model B Rev 1.2
* Supports full user-level GPIO access via memory.
* Supports basic user-level GPIO access via /dev/gpiomem.
* Supports basic user-level GPIO access via /dev/gpiochip (slow).

```

# Compile

Use switches as described here TODO to control what examples will be compiled in.

```bash
$ cd ./libtropic/tests/platforms/unix-spi

$ mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DLT_EX_TEST_REVERSIBLE=1 .. && make

```

# Execute

```bash
$ ./libtropic-test
```
34 changes: 34 additions & 0 deletions tests/platforms/libtropic-raspberrypi/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @file main.c
* @author Tropic Square s.r.o.
*
* @license For the license see file LICENSE.txt file in the root directory of this source tree.
*/

#include <stdio.h>
#include "string.h"
#include "libtropic_examples.h"

// rm -rf build/ && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DLT_EX_HELLO_WORLD=1 .. && make && cd ../
int main(void)
{
// Test routines
#ifdef LT_EX_TEST_REVERSIBLE
lt_ex_test_reversible();
#endif
#ifdef LT_EX_TEST_IREVERSIBLE
lt_ex_test_ireversible();
#endif

// Full examples
#ifdef LT_FEATURES_FWUPDATE
lt_ex_fwupdate();
#endif
#ifdef LT_EX_HELLO_WORLD
/*int*/ lt_ex_hello_world();
#endif
#ifdef LT_EX_HW_WALLET
/*int*/ lt_ex_hardware_wallet();
#endif

}
Loading

0 comments on commit 71c83d0

Please sign in to comment.