-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from tropicsquare/37-add-hal-support-for-raspb…
…erry-pi 37 add hal support for raspberry pi
- Loading branch information
Showing
10 changed files
with
341 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
Oops, something went wrong.