Skip to content

Commit 72be2c7

Browse files
committed
pico-sdk: adapt for the sha256 driver to Zephyr
Comment out unused DMA-related code and add wrappers to expose internal functions. Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 5a981c7 commit 72be2c7

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/rp2_common/pico_sha256/include/pico/sha256.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#ifndef _PICO_SHA256_H
88
#define _PICO_SHA256_H
99

10+
#if !defined(__ZEPHYR__)
1011
#include "pico/time.h"
1112
#include "hardware/dma.h"
13+
#endif
1214
#include "hardware/sha256.h"
1315

1416
/** \file pico/sha256.h
@@ -54,10 +56,13 @@ typedef struct pico_sha256_state {
5456
uint32_t word;
5557
uint8_t bytes[4];
5658
} cache;
59+
#if !defined(__ZEPHYR__)
5760
dma_channel_config config;
61+
#endif
5862
size_t total_data_size;
5963
} pico_sha256_state_t;
6064

65+
#if !defined(__ZEPHYR__)
6166
/*! \brief Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not available
6267
* \ingroup pico_sha256
6368
*
@@ -99,6 +104,7 @@ int pico_sha256_start_blocking_until(pico_sha256_state_t *state, enum sha256_end
99104
static inline int pico_sha256_start_blocking(pico_sha256_state_t *state, enum sha256_endianness endianness, bool use_dma) {
100105
return pico_sha256_start_blocking_until(state, endianness, use_dma, at_the_end_of_time);
101106
}
107+
#endif
102108

103109
/*! \brief Add byte data to be SHA-256 calculation
104110
* \ingroup pico_sha256
@@ -117,6 +123,7 @@ static inline int pico_sha256_start_blocking(pico_sha256_state_t *state, enum sh
117123
*/
118124
void pico_sha256_update(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes);
119125

126+
#if !defined(__ZEPHYR__)
120127
/*! \brief Add byte data to be SHA-256 calculation
121128
* \ingroup pico_sha256
122129
*
@@ -142,6 +149,11 @@ void pico_sha256_update_blocking(pico_sha256_state_t *state, const uint8_t *data
142149
* @param out The SHA-256 checksum
143150
*/
144151
void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out);
152+
#endif
153+
154+
#if defined(__ZEPHYR__)
155+
void pico_sha256_write_padding(pico_sha256_state_t *state);
156+
#endif
145157

146158
#ifdef __cplusplus
147159
}

src/rp2_common/pico_sha256/sha256.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
#include "hardware/sha256.h"
1212
#include "pico/bootrom/lock.h"
1313
#include "pico/sha256.h"
14+
#if !defined(__ZEPHYR__)
1415
#include "pico/time.h"
16+
#endif
1517

1618
// We add one 0x80 byte, then 8 bytes for the size
1719
#define SHA256_PADDING_DATA_BYTES 9
1820
#define SHA256_BLOCK_SIZE_BYTES 64
1921

22+
#if !defined(__ZEPHYR__)
2023
bool __weak pico_sha256_lock(pico_sha256_state_t *state) {
2124
if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
2225
return false;
@@ -68,8 +71,10 @@ int pico_sha256_start_blocking_until(pico_sha256_state_t *state, enum sha256_end
6871
} while (true);
6972
return rc;
7073
}
74+
#endif
7175

7276
static void write_to_hardware(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes) {
77+
#if !defined(__ZEPHYR__)
7378
if (state->channel >= 0) {
7479
dma_channel_wait_for_finish_blocking(state->channel);
7580
assert(!sha256_err_not_ready());
@@ -83,6 +88,7 @@ static void write_to_hardware(pico_sha256_state_t *state, const uint8_t *data, s
8388
true
8489
);
8590
} else {
91+
#endif
8692
if (!state->cache_used && !(((uintptr_t)data)&3u)) {
8793
GCC_Like_Pragma("GCC diagnostic ignored \"-Wcast-align\"")
8894
const uint32_t *data32 = (const uint32_t *)data;
@@ -103,7 +109,9 @@ static void write_to_hardware(pico_sha256_state_t *state, const uint8_t *data, s
103109
sha256_put_word(state->cache.word);
104110
}
105111
}
112+
#if !defined(__ZEPHYR__)
106113
}
114+
#endif
107115
}
108116

109117
static void update_internal(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes) {
@@ -138,12 +146,14 @@ void pico_sha256_update(pico_sha256_state_t *state, const uint8_t *data, size_t
138146
update_internal(state, data, data_size_bytes);
139147
}
140148

149+
#if !defined(__ZEPHYR__)
141150
void pico_sha256_update_blocking(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes) {
142151
update_internal(state, data, data_size_bytes);
143152
if (state->channel >= 0) {
144153
dma_channel_wait_for_finish_blocking(state->channel);
145154
}
146155
}
156+
#endif
147157

148158
// write the SHA-256 padding to hardware
149159
static void write_padding(pico_sha256_state_t *state) {
@@ -164,6 +174,7 @@ static void write_padding(pico_sha256_state_t *state) {
164174
update_internal(state, (uint8_t*)&size, sizeof(uint64_t)); // last write
165175
}
166176

177+
#if !defined(__ZEPHYR__)
167178
void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out) {
168179
assert(state->locked);
169180
// pass NULL to abandon the current hash in case of an error
@@ -183,3 +194,10 @@ void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out) {
183194
}
184195
pico_sha256_unlock(state);
185196
}
197+
#endif
198+
199+
#if defined(__ZEPHYR__)
200+
void pico_sha256_write_padding(pico_sha256_state_t *state) {
201+
write_padding(state);
202+
}
203+
#endif

0 commit comments

Comments
 (0)