diff options
55 files changed, 657 insertions, 518 deletions
@@ -27,6 +27,16 @@ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# A couple features that can be enabled at build time, but are not turned on +# by default: +# DO_PROFILING: Enable gmon profiling. See libraries/libprof/README.md for +# more details. +# DO_TASK_METRICS: Enable task metrics - average/max time between yields. This +# can be helpful when experimentally adding yields to improve responsiveness. +# +# To enable, run `make DO_PROFILING=1 DO_TASK_METRICS=1` +# (or DO_PROFILING=xyzzy - `make` just cares that the symbol is defined) + # export all variables to child processes by default .EXPORT_ALL_VARIABLES: @@ -102,7 +112,7 @@ SIZE=$(PREFIX)size STM32_CFLAGS_OPTIMIZATION ?= -ggdb -Og # whew, that's a lot of cflags -CFLAGS = $(STM32_CFLAGS_OPTIMIZATION) -Wall -Warray-bounds #-Wextra +CFLAGS = $(STM32_CFLAGS_OPTIMIZATION) -Wall -Warray-bounds -Wextra CFLAGS += -mcpu=cortex-m4 -mthumb -mlittle-endian -mthumb-interwork CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16 CFLAGS += -DUSE_STDPERIPH_DRIVER -DSTM32F4XX -DSTM32F429xx diff --git a/libraries/libcli/Makefile b/libraries/libcli/Makefile index ec79b37..cd1d3b6 100644 --- a/libraries/libcli/Makefile +++ b/libraries/libcli/Makefile @@ -12,6 +12,7 @@ CFLAGS += \ -DDO_SOCKET=0 \ -DDO_TAB_COMPLETION=1 \ -DDO_TELNET=0 +CFLAGS += -Wno-unused-parameter all: libcli.a diff --git a/libraries/libhal/Makefile b/libraries/libhal/Makefile index bf870b9..72225e1 100644 --- a/libraries/libhal/Makefile +++ b/libraries/libhal/Makefile @@ -1,4 +1,6 @@ vpath %.c ${LIBHAL_SRC} vpath %.h ${LIBHAL_SRC} +CFLAGS += -Wno-missing-field-initializers -Wno-unused-parameter -Wno-sign-compare + include ${LIBHAL_SRC}/Makefile diff --git a/libraries/libprof/README.txt b/libraries/libprof/README.md index f0bacc7..d464644 100644 --- a/libraries/libprof/README.txt +++ b/libraries/libprof/README.md @@ -5,7 +5,7 @@ Origin ------ This code was copied from https://github.com/ErichStyger/mcuoneclipse.git, -directory Examples/KDS/FRDM-K64F120M/FRDM-K64F_Profiling/Profiling, commit +directory `Examples/KDS/FRDM-K64F120M/FRDM-K64F_Profiling/Profiling`, commit 9b7eedddd8b24968128582aedc63be95b61f782c, dated Mon Jan 9 16:56:17 2017 +0100. References @@ -13,11 +13,9 @@ References I recommend reading both of these to understand how the profiling code works. -[1]: https://mcuoneclipse.com/2015/08/23/tutorial-using-gnu-profiling-gprof-with-arm-cortex-m/ -"Tutorial: Using GNU Profiling (gprof) with ARM Cortex-M" +1. [Tutorial: Using GNU Profiling (gprof) with ARM Cortex-M](https://mcuoneclipse.com/2015/08/23/tutorial-using-gnu-profiling-gprof-with-arm-cortex-m/) -[2]: http://bgamari.github.io/posts/2014-10-31-semihosting.html -"Semihosting with ARM, GCC, and OpenOCD" +2. [Semihosting with ARM, GCC, and OpenOCD](http://bgamari.github.io/posts/2014-10-31-semihosting.html) How to build ------------ @@ -35,8 +33,8 @@ How to run You need to start OpenOCD on the host, and enable semihosting, at least before you try to use it as a remote file system. -I recommend executing the following in the projects/hsm directory, so that -gmon.out ends up in the same directory as hsm.elf. +I recommend executing the following in the `projects/hsm` directory, so that +`gmon.out` ends up in the same directory as `hsm.elf`. Start the debugger: @@ -60,8 +58,8 @@ whatever will be exercising the hsm. Afterwards, in the console, type `profile stop`. After invoking `profile stop`, it can take several minutes to write -gmon.out over OpenOCD to the host. +`gmon.out` over OpenOCD to the host. -In the projects/hsm directory, run gprof to analyse the gmon.out file: +In the `projects/hsm` directory, run `gprof` to analyse the `gmon.out` file: $ gprof hsm.elf >gprof.txt diff --git a/libraries/libtfm/Makefile b/libraries/libtfm/Makefile index 5be45f4..b50421c 100644 --- a/libraries/libtfm/Makefile +++ b/libraries/libtfm/Makefile @@ -41,7 +41,7 @@ CFLAGS := $(subst ${STM32_CFLAGS_OPTIMIZATION},${STM32_LIBTFM_CFLAGS_OPTIMIZATIO CFLAGS += -DTFM_ARM -Dasm=__asm__ -Wa,-mimplicit-it=thumb CFLAGS += -I${LIBTFM_SRC}/tomsfastmath/src/headers CFLAGS += -DFP_MAX_SIZE="(${BITS}*2+(8*DIGIT_BIT))" -CFLAGS += -Wall -W -Wshadow +CFLAGS += -Wall -W -Wshadow -Wno-uninitialized TARGETS := $(notdir ${HDR} ${LIB}) diff --git a/libraries/mbed/Makefile b/libraries/mbed/Makefile index 9d68e92..873359d 100644 --- a/libraries/mbed/Makefile +++ b/libraries/mbed/Makefile @@ -1,6 +1,8 @@ CC=arm-none-eabi-gcc AR=arm-none-eabi-ar +CFLAGS += -Wno-unused-parameter + ########################################### vpath %.c targets/cmsis/TARGET_STM/TARGET_STM32F4 targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_DEV_BRIDGE diff --git a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c index fa08bb5..f64e6ce 100644 --- a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c +++ b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c @@ -87,19 +87,23 @@ void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) void HAL_SRAM_MspInit(SRAM_HandleTypeDef* hsram) { + hsram = hsram; } void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef* hsram) { + hsram = hsram; } void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef* hsdram) { + hsdram = hsdram; } void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef* hsdram) { + hsdram = hsdram; } void HAL_UART_MspInit(UART_HandleTypeDef* huart) diff --git a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c index b8b6fce..81b27cb 100644 --- a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c +++ b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c @@ -150,6 +150,7 @@ __weak void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART1_RxCpltCallback could be implemented in the user file */ + huart = huart; } __weak void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) @@ -157,6 +158,7 @@ __weak void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART2_RxCpltCallback could be implemented in the user file */ + huart = huart; } /** @@ -182,6 +184,7 @@ __weak void HAL_UART1_RxHalfCpltCallback(UART_HandleTypeDef *huart) /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART1_RxHalfCpltCallback could be implemented in the user file */ + huart = huart; } __weak void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart) @@ -189,10 +192,13 @@ __weak void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart) /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART2_RxHalfCpltCallback could be implemented in the user file */ + huart = huart; } void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { + huart = huart; + /* I dunno, just trap it for now */ Error_Handler(); } diff --git a/projects/board-test/fmc-perf.c b/projects/board-test/fmc-perf.c index 5ed47ec..e87f282 100644 --- a/projects/board-test/fmc-perf.c +++ b/projects/board-test/fmc-perf.c @@ -56,11 +56,11 @@ static void _time_check(char *label, const uint32_t t0) uint32_t t = HAL_GetTick() - t0; uart_send_string(label); - uart_send_integer(t / 1000, 0); + uart_send_integer(t / 1000, 1); uart_send_char('.'); uart_send_integer(t % 1000, 3); uart_send_string(" seconds, "); - uart_send_integer(((1000 * TEST_NUM_ROUNDS) / t), 0); + uart_send_integer(((1000 * TEST_NUM_ROUNDS) / t), 1); uart_send_string("/sec\r\n"); } diff --git a/projects/board-test/fmc-test.c b/projects/board-test/fmc-test.c index c6a37e6..2002f57 100644 --- a/projects/board-test/fmc-test.c +++ b/projects/board-test/fmc-test.c @@ -4,8 +4,7 @@ /* This requires a special bitstream with a special test register. - See core/platform/novena/fmc/rtl/novena_fmc_top.v, sections marked - `ifdef test: + See core/platform/alpha/rtl/alpha_fmc_test.v: //---------------------------------------------------------------- // Dummy Register // @@ -76,6 +75,7 @@ int test_fpga_address_bus(void); // Defines //------------------------------------------------------------------------------ #define TEST_NUM_ROUNDS 100000 +#define VERBOSE 0 //------------------------------------------------------------------------------ @@ -117,11 +117,15 @@ int main(void) // test address bus addr_test_ok = test_fpga_address_bus(); - uart_send_string("Data: "); - uart_send_integer(data_test_ok, 6); - uart_send_string(", addr: "); - uart_send_integer(addr_test_ok, 6); - uart_send_string("\r\n"); + if (VERBOSE || + (data_test_ok != TEST_NUM_ROUNDS || + addr_test_ok != TEST_NUM_ROUNDS)) { + uart_send_string("Data: "); + uart_send_integer(data_test_ok, 6); + uart_send_string(", addr: "); + uart_send_integer(addr_test_ok, 6); + uart_send_string("\r\n"); + } if (data_test_ok == TEST_NUM_ROUNDS && addr_test_ok == TEST_NUM_ROUNDS) { @@ -137,11 +141,12 @@ int main(void) } uart_send_string("Success "); - uart_send_integer(successful_runs, 0); + uart_send_integer(successful_runs, 1); uart_send_string(", fail "); - uart_send_integer(failed_runs, 0); - uart_send_string("\r\n\r\n"); - + uart_send_integer(failed_runs, 1); + uart_send_string("\r\n"); + if (VERBOSE) + uart_send_string("\r\n"); HAL_Delay(sleep); } @@ -194,13 +199,16 @@ int test_fpga_data_bus(void) data_diff = buf; data_diff ^= rnd; - uart_send_string("Sample of data bus test data: expected "); - uart_send_binary(rnd, 32); - uart_send_string(", got "); - uart_send_binary(buf, 32); - uart_send_string(", diff "); - uart_send_binary(data_diff, 32); - uart_send_string("\r\n"); + if (VERBOSE || data_diff) { + uart_send_string("Sample of data bus test data: expected "); + uart_send_binary(rnd, 32); + uart_send_string(", got "); + uart_send_binary(buf, 32); + uart_send_string(", diff "); + uart_send_binary(data_diff, 32); + uart_send_string("\r\n"); + } + // return number of successful tests return c; } @@ -263,13 +271,15 @@ int test_fpga_address_bus(void) addr_diff = buf; addr_diff ^= rnd; - uart_send_string("Sample of addr bus test data: expected "); - uart_send_binary(rnd, 32); - uart_send_string(", got "); - uart_send_binary(buf, 32); - uart_send_string(", diff "); - uart_send_binary(addr_diff, 32); - uart_send_string("\r\n"); + if (VERBOSE || addr_diff) { + uart_send_string("Sample of addr bus test data: expected "); + uart_send_binary(rnd, 32); + uart_send_string(", got "); + uart_send_binary(buf, 32); + uart_send_string(", diff "); + uart_send_binary(addr_diff, 32); + uart_send_string("\r\n"); + } return c; } diff --git a/projects/board-test/keystore-perf.c b/projects/board-test/keystore-perf.c index 09528a2..c2aa4fb 100644 --- a/projects/board-test/keystore-perf.c +++ b/projects/board-test/keystore-perf.c @@ -16,13 +16,13 @@ static void test_read_data(void) { uint8_t read_buf[KEYSTORE_SUBSECTOR_SIZE]; uint32_t i; - int err; + HAL_StatusTypeDef err; for (i = 0; i < KEYSTORE_NUM_SUBSECTORS; ++i) { err = keystore_read_data(i * KEYSTORE_SUBSECTOR_SIZE, read_buf, KEYSTORE_SUBSECTOR_SIZE); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: keystore_read_data returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -36,19 +36,19 @@ static void _read_verify(uint8_t *vrfy_buf) { uint8_t read_buf[KEYSTORE_SUBSECTOR_SIZE]; uint32_t i; - int err; + HAL_StatusTypeDef err; for (i = 0; i < KEYSTORE_NUM_SUBSECTORS; ++i) { err = keystore_read_data(i * KEYSTORE_SUBSECTOR_SIZE, read_buf, KEYSTORE_SUBSECTOR_SIZE); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: keystore_read_data returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } if (memcmp(read_buf, vrfy_buf, KEYSTORE_SUBSECTOR_SIZE) != 0) { uart_send_string("ERROR: verify failed in subsector "); - uart_send_integer(i, 0); + uart_send_integer(i, 1); uart_send_string("\r\n"); break; } @@ -61,13 +61,13 @@ static void _read_verify(uint8_t *vrfy_buf) static void test_erase_sector(void) { uint32_t i; - int err; + HAL_StatusTypeDef err; for (i = 0; i < KEYSTORE_NUM_SECTORS; ++i) { err = keystore_erase_sector(i); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: keystore_erase_sector returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -80,13 +80,13 @@ static void test_erase_sector(void) static void test_erase_subsector(void) { uint32_t i; - int err; + HAL_StatusTypeDef err; for (i = 0; i < KEYSTORE_NUM_SUBSECTORS; ++i) { err = keystore_erase_subsector(i); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: keystore_erase_subsector returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -114,18 +114,18 @@ static void test_write_data(void) { uint8_t write_buf[KEYSTORE_SUBSECTOR_SIZE]; uint32_t i; - int err; + HAL_StatusTypeDef err; for (i = 0; i < sizeof(write_buf); ++i) write_buf[i] = i & 0xFF; for (i = 0; i < KEYSTORE_NUM_SUBSECTORS; ++i) { err = keystore_write_data(i * KEYSTORE_SUBSECTOR_SIZE, write_buf, KEYSTORE_SUBSECTOR_SIZE); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: keystore_write_data returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string(" for subsector "); - uart_send_integer(i, 0); + uart_send_integer(i, 1); uart_send_string("\r\n"); break; } @@ -151,15 +151,15 @@ static void _time_check(char *label, const uint32_t t0, uint32_t n_rounds) uint32_t t = HAL_GetTick() - t0; uart_send_string(label); - uart_send_integer(t / 1000, 0); + uart_send_integer(t / 1000, 1); uart_send_char('.'); uart_send_integer(t % 1000, 3); uart_send_string(" sec"); if (n_rounds > 1) { uart_send_string(" for "); - uart_send_integer(n_rounds, 0); + uart_send_integer(n_rounds, 1); uart_send_string(" rounds, "); - uart_send_integer(t / n_rounds, 0); + uart_send_integer(t / n_rounds, 1); uart_send_char('.'); uart_send_integer(((t % n_rounds) * 100) / n_rounds, 2); uart_send_string(" ms each"); @@ -177,9 +177,8 @@ static void _time_check(char *label, const uint32_t t0, uint32_t n_rounds) int main(void) { stm_init(); - uart_set_default(STM_UART_MGMT); - if (keystore_check_id() != 1) { + if (keystore_check_id() != HAL_OK) { uart_send_string("ERROR: keystore_check_id failed\r\n"); return 0; } diff --git a/projects/board-test/rtc-test.c b/projects/board-test/rtc-test.c index f1c2db1..bbb297a 100644 --- a/projects/board-test/rtc-test.c +++ b/projects/board-test/rtc-test.c @@ -94,7 +94,7 @@ void dump_sram() request_data(buf, RTC_RTC_ADDR, 0x0, RTC_SRAM_TOTAL_BYTES); uart_send_string("SRAM contents:\r\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, RTC_SRAM_TOTAL_BYTES); + uart_send_hexdump(buf, 0, RTC_SRAM_TOTAL_BYTES); uart_send_string("\r\n"); } @@ -104,12 +104,12 @@ void dump_eeprom() request_data(buf, RTC_EEPROM_ADDR, 0x0, RTC_EEPROM_TOTAL_BYTES); uart_send_string("EEPROM contents:\r\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, RTC_EEPROM_TOTAL_BYTES); + uart_send_hexdump(buf, 0, RTC_EEPROM_TOTAL_BYTES); uart_send_string("\r\n"); request_data(buf, RTC_EEPROM_ADDR, RTC_EEPROM_EUI48_OFFSET, RTC_EEPROM_EUI48_BYTES); uart_send_string("EEPROM EUI-48:\r\n"); - uart_send_hexdump(STM_UART_MGMT, buf, RTC_EEPROM_EUI48_OFFSET, RTC_EEPROM_EUI48_BYTES); + uart_send_hexdump(buf, RTC_EEPROM_EUI48_OFFSET, RTC_EEPROM_EUI48_BYTES); uart_send_string("\r\n"); } @@ -131,7 +131,6 @@ int main() { stm_init(); - uart_set_default(STM_UART_MGMT); uart_send_string("\r\n\r\n*** Init done\r\n"); dump_sram(); diff --git a/projects/board-test/spiflash-perf.c b/projects/board-test/spiflash-perf.c index 53f29cb..36c6131 100644 --- a/projects/board-test/spiflash-perf.c +++ b/projects/board-test/spiflash-perf.c @@ -33,9 +33,9 @@ static void test_read_page(void) for (i = 0; i < N25Q128_NUM_PAGES; ++i) { err = n25q128_read_page(ctx, i, read_buf); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_read_page returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -53,9 +53,9 @@ static void test_read_subsector(void) for (i = 0; i < N25Q128_NUM_SUBSECTORS; ++i) { err = n25q128_read_subsector(ctx, i, read_buf); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_read_subsector returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -75,15 +75,15 @@ static void _read_verify(uint8_t *vrfy_buf) for (i = 0; i < N25Q128_NUM_PAGES; ++i) { err = n25q128_read_page(ctx, i, read_buf); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_read_page returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } if (memcmp(read_buf, vrfy_buf, N25Q128_PAGE_SIZE) != 0) { uart_send_string("ERROR: verify failed in page "); - uart_send_integer(i, 0); + uart_send_integer(i, 1); uart_send_string("\r\n"); break; } @@ -100,9 +100,9 @@ static void test_erase_sector(void) for (i = 0; i < N25Q128_NUM_SECTORS; ++i) { err = n25q128_erase_sector(ctx, i); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_erase_sector returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -119,9 +119,9 @@ static void test_erase_subsector(void) for (i = 0; i < N25Q128_NUM_SUBSECTORS; ++i) { err = n25q128_erase_subsector(ctx, i); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_erase_subsector returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); break; } @@ -136,9 +136,9 @@ static void test_erase_bulk(void) int err; err = n25q128_erase_bulk(ctx); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_erase_bulk returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string("\r\n"); } } @@ -171,11 +171,11 @@ static void test_write_page(void) for (i = 0; i < N25Q128_NUM_PAGES; ++i) { err = n25q128_write_page(ctx, i, write_buf); - if (err != 1) { + if (err != HAL_OK) { uart_send_string("ERROR: n25q128_write_page returned "); - uart_send_integer(err, 0); + uart_send_integer(err, 1); uart_send_string(" for page "); - uart_send_integer(i, 0); + uart_send_integer(i, 1); uart_send_string("\r\n"); break; } @@ -201,15 +201,15 @@ static void _time_check(char *label, const uint32_t t0, uint32_t n_rounds) uint32_t t = HAL_GetTick() - t0; uart_send_string(label); - uart_send_integer(t / 1000, 0); + uart_send_integer(t / 1000, 1); uart_send_char('.'); uart_send_integer(t % 1000, 3); uart_send_string(" sec"); if (n_rounds > 1) { uart_send_string(" for "); - uart_send_integer(n_rounds, 0); + uart_send_integer(n_rounds, 1); uart_send_string(" rounds, "); - uart_send_integer(t / n_rounds, 0); + uart_send_integer(t / n_rounds, 1); uart_send_char('.'); uart_send_integer(((t % n_rounds) * 100) / n_rounds, 2); uart_send_string(" ms each"); @@ -227,9 +227,8 @@ static void _time_check(char *label, const uint32_t t0, uint32_t n_rounds) int main(void) { stm_init(); - uart_set_default(STM_UART_MGMT); - if (n25q128_check_id(ctx) != 1) { + if (n25q128_check_id(ctx) != HAL_OK) { uart_send_string("ERROR: n25q128_check_id failed\r\n"); return 0; } diff --git a/projects/bootloader/bootloader.c b/projects/bootloader/bootloader.c index ead87d0..c62df12 100644 --- a/projects/bootloader/bootloader.c +++ b/projects/bootloader/bootloader.c @@ -84,7 +84,7 @@ int should_dfu() for (i = 0; i < 50; i++) { HAL_Delay(100); led_toggle(LED_BLUE); - if (uart_recv_char2(STM_UART_MGMT, &rx, 0) == HAL_OK) { + if (uart_recv_char(&rx, 0) == HAL_OK) { if (rx == 13) return 1; } } @@ -100,7 +100,7 @@ int main(void) stm_init(); - uart_send_string2(STM_UART_MGMT, (char *) "\r\n\r\nThis is the bootloader speaking..."); + uart_send_string("\r\n\r\nThis is the bootloader speaking..."); if (should_dfu()) { led_off(LED_BLUE); @@ -110,9 +110,9 @@ int main(void) */ led_off(LED_BLUE); led_on(LED_RED); - uart_send_string2(STM_UART_MGMT, (char *) "dfu_receive_firmware failed: "); - uart_send_number2(STM_UART_MGMT, status, 3, 16); - uart_send_string2(STM_UART_MGMT, (char *) "\r\n\r\nRebooting in three seconds\r\n"); + uart_send_string("dfu_receive_firmware failed: "); + uart_send_hex(status, 2); + uart_send_string("\r\n\r\nRebooting in three seconds\r\n"); HAL_Delay(3000); HAL_NVIC_SystemReset(); while (1) {}; @@ -124,7 +124,7 @@ int main(void) */ *dfu_control = HARDWARE_EARLY_DFU_JUMP; - uart_send_string2(STM_UART_MGMT, (char *) "loading firmware\r\n\r\n"); + uart_send_string("loading firmware\r\n\r\n"); /* De-initialize hardware by rebooting */ HAL_NVIC_SystemReset(); diff --git a/projects/bootloader/dfu.c b/projects/bootloader/dfu.c index a0ff372..83aef20 100644 --- a/projects/bootloader/dfu.c +++ b/projects/bootloader/dfu.c @@ -53,7 +53,7 @@ static int getline(char *buf, int len) uint8_t c; for (i = 0; i < len; ++i) { - if (uart_recv_char2(STM_UART_MGMT, &c, HAL_MAX_DELAY) != CMSIS_HAL_OK) + if (uart_recv_char(&c, HAL_MAX_DELAY) != CMSIS_HAL_OK) return -1; if (c == '\r') { buf[i] = '\0'; @@ -67,7 +67,7 @@ static int getline(char *buf, int len) static void uart_flush(void) { uint8_t c; - while (uart_recv_char2(STM_UART_MGMT, &c, 0) == CMSIS_HAL_OK) { ; } + while (uart_recv_char(&c, 0) == CMSIS_HAL_OK) { ; } } static int do_login(void) @@ -79,7 +79,7 @@ static int do_login(void) int n; uart_flush(); - uart_send_string2(STM_UART_MGMT, "\r\nUsername: "); + uart_send_string("\r\nUsername: "); if (getline(username, sizeof(username)) <= 0) return -1; if (strcmp(username, "wheel") == 0) @@ -92,7 +92,7 @@ static int do_login(void) user = HAL_USER_NONE; uart_flush(); - uart_send_string2(STM_UART_MGMT, "\r\nPassword: "); + uart_send_string("\r\nPassword: "); if ((n = getline(pin, sizeof(pin))) <= 0) return -1; @@ -101,7 +101,7 @@ static int do_login(void) hal_ks_init_read_only_pins_only(); if (hal_rpc_login(client, user, pin, n) != LIBHAL_OK) { - uart_send_string2(STM_UART_MGMT, "\r\nAccess denied\r\n"); + uart_send_string("\r\nAccess denied\r\n"); return -1; } return 0; @@ -118,33 +118,33 @@ int dfu_receive_firmware(void) return -1; /* Fake the CLI */ - uart_send_string2(STM_UART_MGMT, "\r\ncryptech> "); + uart_send_string("\r\ncryptech> "); char cmd[64]; if (getline(cmd, sizeof(cmd)) <= 0) return -1; if (strcmp(cmd, "firmware upload") != 0) { - uart_send_string2(STM_UART_MGMT, "\r\nInvalid command \""); - uart_send_string2(STM_UART_MGMT, cmd); - uart_send_string2(STM_UART_MGMT, "\"\r\n"); + uart_send_string("\r\nInvalid command \""); + uart_send_string(cmd); + uart_send_string("\"\r\n"); return -1; } - uart_send_string2(STM_UART_MGMT, "OK, write size (4 bytes), data in 4096 byte chunks, CRC-32 (4 bytes)\r\n"); + uart_send_string("OK, write size (4 bytes), data in 4096 byte chunks, CRC-32 (4 bytes)\r\n"); /* Read file size (4 bytes) */ - uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, sizeof(filesize), 10000); + uart_receive_bytes((void *) &filesize, sizeof(filesize), 10000); if (filesize < 512 || filesize > DFU_FIRMWARE_END_ADDR - DFU_FIRMWARE_ADDR) { - uart_send_string2(STM_UART_MGMT, "Invalid filesize "); - uart_send_number2(STM_UART_MGMT, filesize, 1, 10); - uart_send_string2(STM_UART_MGMT, "\r\n"); + uart_send_string("Invalid filesize "); + uart_send_integer(filesize, 1); + uart_send_string("\r\n"); return -1; } HAL_FLASH_Unlock(); - uart_send_string2(STM_UART_MGMT, "Send "); - uart_send_number2(STM_UART_MGMT, filesize, 1, 10); - uart_send_string2(STM_UART_MGMT, " bytes of data\r\n"); + uart_send_string("Send "); + uart_send_integer(filesize, 1); + uart_send_string(" bytes of data\r\n"); while (filesize) { /* By initializing buf to the same value that erased flash has (0xff), we don't @@ -156,7 +156,7 @@ int dfu_receive_firmware(void) n = filesize; } - if (uart_receive_bytes(STM_UART_MGMT, (void *) buf, n, 10000) != CMSIS_HAL_OK) { + if (uart_receive_bytes((void *) buf, n, 10000) != CMSIS_HAL_OK) { return -2; } filesize -= n; @@ -170,7 +170,7 @@ int dfu_receive_firmware(void) /* ACK this chunk by sending the current chunk counter (4 bytes) */ counter++; - uart_send_bytes(STM_UART_MGMT, (void *) &counter, 4); + uart_send_bytes((void *) &counter, 4); led_toggle(LED_BLUE); } @@ -178,20 +178,20 @@ int dfu_receive_firmware(void) HAL_FLASH_Lock(); - uart_send_string2(STM_UART_MGMT, "Send CRC-32\r\n"); + uart_send_string("Send CRC-32\r\n"); /* The sending side will now send its calculated CRC-32 */ - uart_receive_bytes(STM_UART_MGMT, (void *) &crc, sizeof(crc), 10000); + uart_receive_bytes((void *) &crc, sizeof(crc), 10000); - uart_send_string2(STM_UART_MGMT, "CRC-32 0x"); - uart_send_number2(STM_UART_MGMT, crc, 1, 16); - uart_send_string2(STM_UART_MGMT, ", calculated CRC 0x"); - uart_send_number2(STM_UART_MGMT, my_crc, 1, 16); + uart_send_string("CRC-32 0x"); + uart_send_hex(crc, 1); + uart_send_string(", calculated CRC 0x"); + uart_send_hex(my_crc, 1); if (crc == my_crc) { - uart_send_string2(STM_UART_MGMT, "CRC checksum MATCHED\r\n"); + uart_send_string("CRC checksum MATCHED\r\n"); return 0; } else { - uart_send_string2(STM_UART_MGMT, "CRC checksum did NOT match\r\n"); + uart_send_string("CRC checksum did NOT match\r\n"); } led_on(LED_RED); diff --git a/projects/bootloader/log.c b/projects/bootloader/log.c index c0d9df4..fbc0e73 100644 --- a/projects/bootloader/log.c +++ b/projects/bootloader/log.c @@ -63,6 +63,6 @@ void hal_log(const hal_log_level_t level, const char *format, ...) vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); - uart_send_string2(STM_UART_MGMT, buffer); - uart_send_string2(STM_UART_MGMT, "\r\n"); + uart_send_string(buffer); + uart_send_string("\r\n"); } diff --git a/projects/cli-test/cli-test.c b/projects/cli-test/cli-test.c index c288257..82946fa 100644 --- a/projects/cli-test/cli-test.c +++ b/projects/cli-test/cli-test.c @@ -57,7 +57,6 @@ int main() { stm_init(); - uart_set_default(STM_UART_MGMT); led_on(LED_GREEN); diff --git a/projects/cli-test/mgmt-cli.c b/projects/cli-test/mgmt-cli.c index eacb944..d7f7383 100644 --- a/projects/cli-test/mgmt-cli.c +++ b/projects/cli-test/mgmt-cli.c @@ -58,8 +58,8 @@ #endif typedef struct { - int ridx; - volatile int widx; + unsigned ridx; + unsigned widx; mgmt_cli_dma_state_t rx_state; uint8_t buf[CLI_UART_RECVBUF_SIZE]; } ringbuf_t; @@ -97,19 +97,20 @@ static uint8_t uart_rx; */ void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) { + huart = huart; + ringbuf_write_char(&uart_ringbuf, uart_rx); } static void uart_cli_print(struct cli_def *cli __attribute__ ((unused)), const char *buf) { - char crlf[] = "\r\n"; - uart_send_string2(STM_UART_MGMT, buf); - uart_send_string2(STM_UART_MGMT, crlf); + uart_send_string(buf); + uart_send_string("\r\n"); } static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count) { - for (int i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) { while (ringbuf_read_char(&uart_ringbuf, (uint8_t *)(buf + i)) == 0) { } } @@ -118,7 +119,7 @@ static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void static ssize_t uart_cli_write(struct cli_def *cli __attribute__ ((unused)), const void *buf, size_t count) { - uart_send_bytes(STM_UART_MGMT, (uint8_t *) buf, count); + uart_send_bytes((uint8_t *) buf, count); return (ssize_t)count; } diff --git a/projects/cli-test/mgmt-dfu.c b/projects/cli-test/mgmt-dfu.c index 5c9b4b7..c7273f4 100644 --- a/projects/cli-test/mgmt-dfu.c +++ b/projects/cli-test/mgmt-dfu.c @@ -58,10 +58,14 @@ __IO uint32_t *dfu_code_ptr = &CRYPTECH_FIRMWARE_START + 1; static int cmd_dfu_dump(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + cli_print(cli, "First 256 bytes from DFU application address %p:\r\n", dfu_firmware); - uart_send_hexdump(STM_UART_MGMT, (uint8_t *) dfu_firmware, 0, 0xff); - uart_send_string2(STM_UART_MGMT, (char *) "\r\n\r\n"); + uart_send_hexdump((uint8_t *) dfu_firmware, 0, 0xff); + cli_print(cli, "\n"); return CLI_OK; } @@ -70,6 +74,10 @@ static int cmd_dfu_erase(struct cli_def *cli, const char *command, char *argv[], { int status; + command = command; + argv = argv; + argc = argc; + cli_print(cli, "Erasing flash address %p to %p - expect the CLI to crash now", dfu_firmware, dfu_firmware_end); @@ -84,6 +92,11 @@ static int cmd_dfu_erase(struct cli_def *cli, const char *command, char *argv[], static int cmd_dfu_jump(struct cli_def *cli, const char *command, char *argv[], int argc) { uint32_t i; + + command = command; + argv = argv; + argc = argc; + /* Load first byte from the DFU_FIRMWARE_PTR to verify it contains an IVT before * jumping there. */ diff --git a/projects/cli-test/mgmt-fpga.c b/projects/cli-test/mgmt-fpga.c index b1b0973..b913316 100644 --- a/projects/cli-test/mgmt-fpga.c +++ b/projects/cli-test/mgmt-fpga.c @@ -3,7 +3,7 @@ * ----------- * CLI code to manage the FPGA configuration etc. * - * Copyright (c) 2016, NORDUnet A/S All rights reserved. + * Copyright (c) 2016-2017, NORDUnet A/S All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -47,14 +47,21 @@ static volatile uint32_t dfu_offset = 0; -static int _flash_write_callback(uint8_t *buf, size_t len) +static HAL_StatusTypeDef _flash_write_callback(uint8_t *buf, size_t len) { + HAL_StatusTypeDef res; + if ((dfu_offset % FPGACFG_SECTOR_SIZE) == 0) /* first page in sector, need to erase sector */ - if (fpgacfg_erase_sector(dfu_offset / FPGACFG_SECTOR_SIZE) != 1) - return CLI_ERROR; + if ((res = fpgacfg_erase_sector(dfu_offset / FPGACFG_SECTOR_SIZE)) != HAL_OK) + return res; - int res = fpgacfg_write_data(dfu_offset, buf, BITSTREAM_UPLOAD_CHUNK_SIZE) == 1; + /* fpgacfg_write_data (a thin wrapper around n25q128_write_data) + * requires the offset and length to be page-aligned. The last chunk + * will be short, so we pad it out to the full chunk size. + */ + len = len; + res = fpgacfg_write_data(dfu_offset, buf, BITSTREAM_UPLOAD_CHUNK_SIZE); dfu_offset += BITSTREAM_UPLOAD_CHUNK_SIZE; return res; } @@ -63,12 +70,16 @@ static int cmd_fpga_bitstream_upload(struct cli_def *cli, const char *command, c { uint8_t buf[BITSTREAM_UPLOAD_CHUNK_SIZE]; + command = command; + argv = argv; + argc = argc; + dfu_offset = 0; fpgacfg_access_control(ALLOW_ARM); cli_print(cli, "Checking if FPGA config memory is accessible"); - if (fpgacfg_check_id() != 1) { + if (fpgacfg_check_id() != HAL_OK) { cli_print(cli, "ERROR: FPGA config memory not accessible. Check that jumpers JP7 and JP8 are installed."); return CLI_ERROR; } @@ -83,10 +94,14 @@ static int cmd_fpga_bitstream_upload(struct cli_def *cli, const char *command, c static int cmd_fpga_bitstream_erase(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + fpgacfg_access_control(ALLOW_ARM); cli_print(cli, "Checking if FPGA config memory is accessible"); - if (fpgacfg_check_id() != 1) { + if (fpgacfg_check_id() != HAL_OK) { cli_print(cli, "ERROR: FPGA config memory not accessible. Check that jumpers JP7 and JP8 are installed."); return CLI_ERROR; } @@ -97,7 +112,7 @@ static int cmd_fpga_bitstream_erase(struct cli_def *cli, const char *command, ch * * This command could be made to accept an argument indicating the whole memory should be erased. */ - if (fpgacfg_erase_sector(0) != 0) { + if (fpgacfg_erase_sector(0) != HAL_OK) { cli_print(cli, "Erasing first sector in FPGA config memory failed"); return CLI_ERROR; } @@ -110,6 +125,10 @@ static int cmd_fpga_bitstream_erase(struct cli_def *cli, const char *command, ch static int cmd_fpga_reset(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + fpgacfg_access_control(ALLOW_FPGA); fpgacfg_reset_fpga(RESET_FULL); cli_print(cli, "FPGA has been reset"); @@ -119,6 +138,10 @@ static int cmd_fpga_reset(struct cli_def *cli, const char *command, char *argv[] static int cmd_fpga_reset_registers(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + fpgacfg_access_control(ALLOW_FPGA); fpgacfg_reset_fpga(RESET_REGISTERS); cli_print(cli, "FPGA registers have been reset"); diff --git a/projects/cli-test/mgmt-keystore.c b/projects/cli-test/mgmt-keystore.c index 18447c8..6d0d38d 100644 --- a/projects/cli-test/mgmt-keystore.c +++ b/projects/cli-test/mgmt-keystore.c @@ -59,6 +59,8 @@ static int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char * hal_error_t status; hal_client_handle_t client = { -1 }; + command = command; + if (argc != 2) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore set pin <user|so|wheel> <pin>"); @@ -91,6 +93,8 @@ static int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char hal_error_t status; hal_client_handle_t client = { -1 }; + command = command; + if (argc != 1) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore clear pin <user|so|wheel>"); @@ -122,6 +126,8 @@ static int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *comm hal_error_t status; hal_client_handle_t client = { -1 }; + command = command; + if (argc != 1) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore set pin iterations <number>"); @@ -156,6 +162,8 @@ static int cmd_keystore_set_key(struct cli_def *cli, const char *command, char * hal_error_t status; int hint = 0; + command = command; + if (argc != 2) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore set key <name> <der>"); @@ -187,6 +195,8 @@ static int cmd_keystore_delete_key(struct cli_def *cli, const char *command, cha hal_error_t status; hal_uuid_t name; + command = command; + if (argc != 1) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore delete key <name>"); @@ -214,19 +224,23 @@ static int cmd_keystore_show_data(struct cli_def *cli, const char *command, char uint8_t buf[KEYSTORE_PAGE_SIZE]; uint32_t i; - if (keystore_check_id() != 1) { + command = command; + argv = argv; + argc = argc; + + if (keystore_check_id() != CMSIS_HAL_OK) { cli_print(cli, "ERROR: The keystore memory is not accessible."); } memset(buf, 0, sizeof(buf)); - if ((i = keystore_read_data(0, buf, sizeof(buf))) != 1) { + if ((i = keystore_read_data(0, buf, sizeof(buf))) != CMSIS_HAL_OK) { cli_print(cli, "Failed reading first page from keystore memory: %li", i); return CLI_ERROR; } cli_print(cli, "First page from keystore memory:\r\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); - uart_send_string2(STM_UART_MGMT, (char *) "\r\n\r\n"); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); + cli_print(cli, "\n"); return CLI_OK; } @@ -263,7 +277,7 @@ static int show_keys(struct cli_def *cli, const char *title) if (!done) previous_uuid = uuids[sizeof(uuids)/sizeof(*uuids) - 1]; - for (int i = 0; i < n; i++) { + for (unsigned i = 0; i < n; i++) { if ((status = hal_uuid_format(&uuids[i], key_name, sizeof(key_name))) != LIBHAL_OK) { cli_print(cli, "Could not convert key name: %s", @@ -318,6 +332,10 @@ static int show_keys(struct cli_def *cli, const char *title) static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + if (show_keys(cli, "Keystore:")) return CLI_OK; else @@ -329,13 +347,15 @@ static int cmd_keystore_erase(struct cli_def *cli, const char *command, char *ar hal_error_t err; int status; + command = command; + if (argc != 1 || strcmp(argv[0], "YesIAmSure") != 0) { cli_print(cli, "Syntax: keystore erase YesIAmSure"); return CLI_ERROR; } cli_print(cli, "OK, erasing keystore, this might take a while..."); - if ((status = keystore_erase_bulk()) != 1) { + if ((status = keystore_erase_bulk()) != CMSIS_HAL_OK) { cli_print(cli, "Failed erasing token keystore: %i", status); return CLI_ERROR; } diff --git a/projects/cli-test/mgmt-masterkey.c b/projects/cli-test/mgmt-masterkey.c index 623d19b..811e15b 100644 --- a/projects/cli-test/mgmt-masterkey.c +++ b/projects/cli-test/mgmt-masterkey.c @@ -83,6 +83,10 @@ static int cmd_masterkey_status(struct cli_def *cli, const char *command, char * hal_error_t status; uint8_t buf[KEK_LENGTH] = {0}; + command = command; + argv = argv; + argc = argc; + cli_print(cli, "Status of master key:\n"); status = hal_mkm_volatile_read(NULL, 0); @@ -98,7 +102,7 @@ static int cmd_masterkey_status(struct cli_def *cli, const char *command, char * status = hal_mkm_volatile_read(&buf[0], sizeof(buf)); if (status == LIBHAL_OK || status == HAL_ERROR_MASTERKEY_NOT_SET) { cli_print(cli, "\nVolatile read-out:\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); cli_print(cli, "\n"); } else { cli_print(cli, "Failed reading from volatile memory: %s", hal_error_string(status)); @@ -107,7 +111,7 @@ static int cmd_masterkey_status(struct cli_def *cli, const char *command, char * status = hal_mkm_flash_read(&buf[0], sizeof(buf)); if (status == LIBHAL_OK || status == HAL_ERROR_MASTERKEY_NOT_SET) { cli_print(cli, "\nFlash read-out:\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); cli_print(cli, "\n"); } else { cli_print(cli, "Failed reading from flash: %s", hal_error_string(status)); @@ -122,13 +126,15 @@ static int cmd_masterkey_set(struct cli_def *cli, const char *command, char *arg hal_error_t err; int i; + command = command; + if ((i = _parse_hex_groups(&buf[0], sizeof(buf), argv, argc)) != 1) { cli_print(cli, "Failed parsing master key (%i)", i); return CLI_OK; } cli_print(cli, "Parsed key:\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); cli_print(cli, "\n"); if ((err = hal_mkm_volatile_write(buf, sizeof(buf))) == LIBHAL_OK) { @@ -143,6 +149,10 @@ static int cmd_masterkey_erase(struct cli_def *cli, const char *command, char *a { hal_error_t err; + command = command; + argv = argv; + argc = argc; + if ((err = hal_mkm_volatile_erase(KEK_LENGTH)) == LIBHAL_OK) { cli_print(cli, "Erased master key from volatile memory"); } else { @@ -157,13 +167,15 @@ static int cmd_masterkey_unsecure_set(struct cli_def *cli, const char *command, hal_error_t err; int i; + command = command; + if ((i = _parse_hex_groups(&buf[0], sizeof(buf), argv, argc)) != 1) { cli_print(cli, "Failed parsing master key (%i)", i); return CLI_OK; } cli_print(cli, "Parsed key:\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); cli_print(cli, "\n"); if ((err = hal_mkm_flash_write(buf, sizeof(buf))) == LIBHAL_OK) { @@ -178,6 +190,10 @@ static int cmd_masterkey_unsecure_erase(struct cli_def *cli, const char *command { hal_error_t err; + command = command; + argv = argv; + argc = argc; + if ((err = hal_mkm_flash_erase(KEK_LENGTH)) == LIBHAL_OK) { cli_print(cli, "Erased unsecure master key from flash"); } else { diff --git a/projects/cli-test/mgmt-misc.c b/projects/cli-test/mgmt-misc.c index 7db08f2..db8dbd2 100644 --- a/projects/cli-test/mgmt-misc.c +++ b/projects/cli-test/mgmt-misc.c @@ -49,9 +49,9 @@ static volatile hal_crc32_t demo_crc; -static int _count_bytes_callback(uint8_t *buf, size_t len) { +static HAL_StatusTypeDef _count_bytes_callback(uint8_t *buf, size_t len) { demo_crc = hal_crc32_update(demo_crc, buf, len); - return 1; + return CMSIS_HAL_OK; } int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_callback data_callback) @@ -67,7 +67,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal cli_print(cli, "OK, write size (4 bytes), data in %li byte chunks, CRC-32 (4 bytes)", (uint32_t) n); - if (uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, sizeof(filesize), 1000) != CMSIS_HAL_OK) { + if (uart_receive_bytes((void *) &filesize, sizeof(filesize), 1000) != CMSIS_HAL_OK) { cli_print(cli, "Receive timed out"); goto fail; } @@ -82,7 +82,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal if (filesize < n) n = filesize; - if (uart_receive_bytes(STM_UART_MGMT, (void *) buf, n, 1000) != CMSIS_HAL_OK) { + if (uart_receive_bytes((void *) buf, n, 1000) != CMSIS_HAL_OK) { cli_print(cli, "Receive timed out"); goto fail; } @@ -92,18 +92,18 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal /* After reception of a chunk but before ACKing we have "all" the time in the world to * calculate CRC and invoke the data_callback. */ - if (data_callback != NULL && ! data_callback(buf, (size_t) n)) { + if (data_callback != NULL && data_callback(buf, n) != CMSIS_HAL_OK) { cli_print(cli, "Data processing failed"); goto fail; } counter++; - uart_send_bytes(STM_UART_MGMT, (void *) &counter, 4); + uart_send_bytes((void *) &counter, 4); } my_crc = hal_crc32_finalize(my_crc); cli_print(cli, "Send CRC-32"); - uart_receive_bytes(STM_UART_MGMT, (void *) &crc, sizeof(crc), 1000); + uart_receive_bytes((void *) &crc, sizeof(crc), 1000); cli_print(cli, "CRC-32 0x%x, calculated CRC 0x%x", (unsigned int) crc, (unsigned int) my_crc); if (crc == my_crc) { cli_print(cli, "CRC checksum MATCHED"); @@ -120,6 +120,10 @@ static int cmd_filetransfer(struct cli_def *cli, const char *command, char *argv { uint8_t buf[FILETRANSFER_UPLOAD_CHUNK_SIZE]; + command = command; + argv = argv; + argc = argc; + demo_crc = hal_crc32_init(); cli_receive_data(cli, &buf[0], sizeof(buf), _count_bytes_callback); demo_crc = hal_crc32_finalize(demo_crc); @@ -129,6 +133,10 @@ static int cmd_filetransfer(struct cli_def *cli, const char *command, char *argv static int cmd_reboot(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + cli_print(cli, "\n\n\nRebooting\n\n\n"); HAL_NVIC_SystemReset(); diff --git a/projects/cli-test/mgmt-misc.h b/projects/cli-test/mgmt-misc.h index c549f63..c0581c9 100644 --- a/projects/cli-test/mgmt-misc.h +++ b/projects/cli-test/mgmt-misc.h @@ -39,7 +39,7 @@ #define FILETRANSFER_UPLOAD_CHUNK_SIZE 256 -typedef int (*cli_data_callback)(uint8_t *, size_t); +typedef HAL_StatusTypeDef (*cli_data_callback)(uint8_t *, size_t); extern int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_callback data_callback); diff --git a/projects/cli-test/mgmt-show.c b/projects/cli-test/mgmt-show.c index f124830..4338dcd 100644 --- a/projects/cli-test/mgmt-show.c +++ b/projects/cli-test/mgmt-show.c @@ -56,6 +56,10 @@ static int cmd_show_cpuspeed(struct cli_def *cli, const char *command, char *arg { volatile uint32_t hclk; + command = command; + argv = argv; + argc = argc; + hclk = HAL_RCC_GetHCLKFreq(); cli_print(cli, "HSE_VALUE: %li", HSE_VALUE); cli_print(cli, "HCLK: %li (%i MHz)", hclk, (int) hclk / 1000 / 1000); @@ -65,7 +69,11 @@ static int cmd_show_cpuspeed(struct cli_def *cli, const char *command, char *arg static int cmd_show_fpga_status(struct cli_def *cli, const char *command, char *argv[], int argc) { - cli_print(cli, "FPGA has %sloaded a bitstream", fpgacfg_check_done() ? "":"NOT "); + command = command; + argv = argv; + argc = argc; + + cli_print(cli, "FPGA has %sloaded a bitstream", (fpgacfg_check_done() == CMSIS_HAL_OK) ? "":"NOT "); return CLI_OK; } @@ -74,7 +82,11 @@ static int cmd_show_fpga_cores(struct cli_def *cli, const char *command, char *a hal_core_t *core; const hal_core_info_t *info; - if (! fpgacfg_check_done()) { + command = command; + argv = argv; + argc = argc; + + if (fpgacfg_check_done() != CMSIS_HAL_OK) { cli_print(cli, "FPGA has not loaded a bitstream"); return CLI_OK; } @@ -90,7 +102,11 @@ static int cmd_show_fpga_cores(struct cli_def *cli, const char *command, char *a static int cmd_show_keystore_status(struct cli_def *cli, const char *command, char *argv[], int argc) { - cli_print(cli, "Keystore memory is %sonline", (keystore_check_id() != 1) ? "NOT ":""); + command = command; + argv = argv; + argc = argc; + + cli_print(cli, "Keystore memory is %sonline", (keystore_check_id() == CMSIS_HAL_OK) ? "":"NOT "); return CLI_OK; } @@ -99,19 +115,23 @@ static int cmd_show_keystore_data(struct cli_def *cli, const char *command, char uint8_t buf[KEYSTORE_PAGE_SIZE]; uint32_t i; - if (keystore_check_id() != 1) { + command = command; + argv = argv; + argc = argc; + + if (keystore_check_id() != CMSIS_HAL_OK) { cli_print(cli, "ERROR: The keystore memory is not accessible."); } memset(buf, 0, sizeof(buf)); - if ((i = keystore_read_data(0, buf, sizeof(buf))) != 1) { + if ((i = keystore_read_data(0, buf, sizeof(buf))) != CMSIS_HAL_OK) { cli_print(cli, "Failed reading first page from keystore memory: %li", i); return CLI_ERROR; } cli_print(cli, "First page from keystore memory:\r\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); - uart_send_string2(STM_UART_MGMT, (char *) "\r\n\r\n"); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); + cli_print(cli, "\n"); for (i = 0; i < 8; i++) { if (buf[i] == 0xff) break; /* never written */ @@ -125,14 +145,14 @@ static int cmd_show_keystore_data(struct cli_def *cli, const char *command, char if (buf[i] == 0xff) { cli_print(cli, "Tombstoning byte %li", i); buf[i] = 0x55; - if ((i = keystore_write_data(0, buf, sizeof(buf))) != 1) { + if ((i = keystore_write_data(0, buf, sizeof(buf))) != CMSIS_HAL_OK) { cli_print(cli, "Failed writing data at offset 0: %li", i); return CLI_ERROR; } } } else { cli_print(cli, "Erasing first sector since all the first 8 bytes are tombstones"); - if ((i = keystore_erase_sector(0)) != 1) { + if ((i = keystore_erase_sector(0)) != CMSIS_HAL_OK) { cli_print(cli, "Failed erasing the first sector: %li", i); return CLI_ERROR; } diff --git a/projects/cli-test/mgmt-test.c b/projects/cli-test/mgmt-test.c index 1a22996..9b9972d 100644 --- a/projects/cli-test/mgmt-test.c +++ b/projects/cli-test/mgmt-test.c @@ -52,6 +52,8 @@ static int cmd_test_sdram(struct cli_def *cli, const char *command, char *argv[] // run external memory initialization sequence int ok, num_cycles = 1, i, test_completed; + command = command; + if (argc == 1) { num_cycles = strtol(argv[0], NULL, 0); if (num_cycles > 100) num_cycles = 100; @@ -106,6 +108,8 @@ static int cmd_test_fmc(struct cli_def *cli, const char *command, char *argv[], { int i, num_cycles = 1, num_rounds = 100000; + command = command; + if (argc >= 1) { num_cycles = strtol(argv[0], NULL, 0); if (num_cycles > 100000) num_cycles = 100000; diff --git a/projects/cli-test/test-fmc.c b/projects/cli-test/test-fmc.c index b393dac..87f80ce 100644 --- a/projects/cli-test/test-fmc.c +++ b/projects/cli-test/test-fmc.c @@ -105,7 +105,7 @@ int test_fpga_data_bus(struct cli_def *cli, uint32_t test_rounds) HAL_RNG_Init(&rng_inst); /* run some rounds of data bus test */ - for (c = 0; c < test_rounds; c++) { + for (c = 0; c < (int)test_rounds; c++) { data_diff = 0; /* try to generate "random" number */ hal_result = HAL_RNG_GenerateRandomNumber(&rng_inst, &rnd); @@ -121,9 +121,9 @@ int test_fpga_data_bus(struct cli_def *cli, uint32_t test_rounds) data_diff = buf ^ rnd; if (data_diff) { cli_print(cli, "Data bus FAIL: expected %lx got %lx", rnd, buf); - uart_send_string2(STM_UART_MGMT, (char *) "Binary diff: "); - uart_send_number2(STM_UART_MGMT, data_diff, 32, 2); - uart_send_string2(STM_UART_MGMT, "\r\n"); + uart_send_string((char *) "Binary diff: "); + uart_send_binary(data_diff, 32); + uart_send_string("\r\n"); break; } @@ -163,7 +163,7 @@ int test_fpga_address_bus(struct cli_def *cli, uint32_t test_rounds) HAL_RNG_Init(&rng_inst); /* run some rounds of address bus test */ - for (c = 0; c < test_rounds; c++) { + for (c = 0; c < (int)test_rounds; c++) { addr_diff = 0; /* try to generate "random" number */ hal_result = HAL_RNG_GenerateRandomNumber(&rng_inst, &addr); @@ -192,9 +192,9 @@ int test_fpga_address_bus(struct cli_def *cli, uint32_t test_rounds) addr_diff = buf ^ addr; if (addr_diff) { cli_print(cli, "Address bus FAIL: expected 0x%lx got 0x%lx", addr, buf); - uart_send_string2(STM_UART_MGMT, (char *) "Binary diff: "); - uart_send_number2(STM_UART_MGMT, addr_diff, 32, 2); - uart_send_string2(STM_UART_MGMT, "\r\n"); + uart_send_string((char *) "Binary diff: "); + uart_send_binary(addr_diff, 32); + uart_send_string("\r\n"); break; } diff --git a/projects/cli-test/test-mkmif.c b/projects/cli-test/test-mkmif.c index bb41b4d..cd71040 100644 --- a/projects/cli-test/test-mkmif.c +++ b/projects/cli-test/test-mkmif.c @@ -32,18 +32,18 @@ static hal_error_t sclk_test(struct cli_def *cli, hal_core_t *core, const uint32 uint32_t readback; hal_error_t err; - cli_print(cli, "Trying to adjust the clockspeed (divisor %x).\n", (unsigned int) divisor); + cli_print(cli, "Trying to adjust the clockspeed (divisor %x).", (unsigned int) divisor); if ((err = hal_mkmif_set_clockspeed(core, divisor)) != LIBHAL_OK) { - cli_print(cli, "hal_mkmif_set_clockspeed: %s\n", hal_error_string(err)); + cli_print(cli, "hal_mkmif_set_clockspeed: %s", hal_error_string(err)); return err; } if ((err = hal_mkmif_get_clockspeed(core, &readback)) != LIBHAL_OK) { - cli_print(cli, "hal_mkmif_get_clockspeed: %s\n", hal_error_string(err)); + cli_print(cli, "hal_mkmif_get_clockspeed: %s", hal_error_string(err)); return err; } if (readback != divisor) { - cli_print(cli, "expected %x, got %x\n", (unsigned int)divisor, (unsigned int)readback); + cli_print(cli, "expected %x, got %x", (unsigned int)divisor, (unsigned int)readback); return HAL_ERROR_IO_UNEXPECTED; } return LIBHAL_OK; @@ -53,10 +53,10 @@ static hal_error_t init_test(struct cli_def *cli, hal_core_t *core) { hal_error_t err; - cli_print(cli, "Trying to init to the memory in continuous mode.\n"); + cli_print(cli, "Trying to init to the memory in continuous mode."); if ((err = hal_mkmif_init(core)) != LIBHAL_OK) { - cli_print(cli, "hal_mkmif_init: %s\n", hal_error_string(err)); + cli_print(cli, "hal_mkmif_init: %s", hal_error_string(err)); return err; } @@ -74,11 +74,11 @@ static hal_error_t write_test(struct cli_def *cli, hal_core_t *core) i < 0x10; write_data += 0x01010101, write_address += 4, ++i) { - cli_print(cli, "Trying to write 0x%08x to memory address 0x%08x.\n", + cli_print(cli, "Trying to write 0x%08x to memory address 0x%08x.", (unsigned int)write_data, (unsigned int)write_address); if ((err = hal_mkmif_write_word(core, write_address, write_data)) != LIBHAL_OK) { - cli_print(cli, "hal_mkmif_write: %s\n", hal_error_string(err)); + cli_print(cli, "hal_mkmif_write: %s", hal_error_string(err)); return err; } } @@ -97,13 +97,13 @@ static hal_error_t read_test(struct cli_def *cli, hal_core_t *core) i < 0x10; read_address += 4, ++i) { - cli_print(cli, "Trying to read from memory address 0x%08x.\n", (unsigned int)read_address); + cli_print(cli, "Trying to read from memory address 0x%08x.", (unsigned int)read_address); if ((err = hal_mkmif_read_word(core, read_address, &read_data)) != LIBHAL_OK) { - cli_print(cli, "hal_mkmif_read: %s\n", hal_error_string(err)); + cli_print(cli, "hal_mkmif_read: %s", hal_error_string(err)); return err; } - cli_print(cli, "Data read: 0x%08x\n", (unsigned int)read_data); + cli_print(cli, "Data read: 0x%08x", (unsigned int)read_data); } return LIBHAL_OK; @@ -115,22 +115,22 @@ static hal_error_t write_read_test(struct cli_def *cli, hal_core_t *core) uint32_t readback; hal_error_t err; - cli_print(cli, "Trying to write 0xdeadbeef to the memory and then read back.\n"); + cli_print(cli, "Trying to write 0xdeadbeef to the memory and then read back."); data = 0xdeadbeef; if ((err = hal_mkmif_write_word(core, 0x00000000, data)) != LIBHAL_OK) { - cli_print(cli, "write error: %s\n", hal_error_string(err)); + cli_print(cli, "write error: %s", hal_error_string(err)); return err; } if ((err = hal_mkmif_read_word(core, 0x00000000, &readback)) != LIBHAL_OK) { - cli_print(cli, "read error: %s\n", hal_error_string(err)); + cli_print(cli, "read error: %s", hal_error_string(err)); return err; } if (readback != data) { - cli_print(cli, "read %08x, expected %08x\n", (unsigned int)readback, (unsigned int)data); + cli_print(cli, "read %08x, expected %08x", (unsigned int)readback, (unsigned int)data); return HAL_ERROR_IO_UNEXPECTED; } @@ -142,8 +142,12 @@ int cmd_test_mkmif(struct cli_def *cli, const char *command, char *argv[], int a hal_core_t *core = hal_core_find(MKMIF_NAME, NULL); hal_error_t res; + command = command; + argv = argv; + argc = argc; + if (core == NULL) { - cli_print(cli, "MKMIF core not present, not testing.\n"); + cli_print(cli, "MKMIF core not present, not testing."); return HAL_ERROR_CORE_NOT_FOUND; } diff --git a/projects/hsm/hsm.c b/projects/hsm/hsm.c index 5ef2ccc..f20ee64 100644 --- a/projects/hsm/hsm.c +++ b/projects/hsm/hsm.c @@ -419,7 +419,6 @@ void hal_sleep(const unsigned seconds) { task_delay(seconds * 1000); } int main(void) { stm_init(); - uart_set_default(STM_UART_MGMT); led_on(LED_GREEN); if (hal_rpc_server_init() != LIBHAL_OK) @@ -428,7 +427,7 @@ int main(void) /* Initialize the ibuf queues. */ memset(&ibuf_waiting, 0, sizeof(ibuf_waiting)); memset(&ibuf_ready, 0, sizeof(ibuf_ready)); - for (int i = 0; i < sizeof(ibufs)/sizeof(ibufs[0]); ++i) + for (size_t i = 0; i < sizeof(ibufs)/sizeof(ibufs[0]); ++i) ibuf_put(&ibuf_waiting, &ibufs[i]); /* Create the rpc dispatch worker tasks. */ diff --git a/projects/hsm/log.c b/projects/hsm/log.c index c0d9df4..fbc0e73 100644 --- a/projects/hsm/log.c +++ b/projects/hsm/log.c @@ -63,6 +63,6 @@ void hal_log(const hal_log_level_t level, const char *format, ...) vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); - uart_send_string2(STM_UART_MGMT, buffer); - uart_send_string2(STM_UART_MGMT, "\r\n"); + uart_send_string(buffer); + uart_send_string("\r\n"); } diff --git a/projects/hsm/mgmt-bootloader.c b/projects/hsm/mgmt-bootloader.c index 738686e..1d8b8ad 100644 --- a/projects/hsm/mgmt-bootloader.c +++ b/projects/hsm/mgmt-bootloader.c @@ -50,16 +50,19 @@ extern hal_user_t user; static uint32_t dfu_offset; -static int _flash_write_callback(uint8_t *buf, size_t len) +static HAL_StatusTypeDef _flash_write_callback(uint8_t *buf, size_t len) { - if (stm_flash_write32(dfu_offset, (uint32_t *)buf, (uint32_t)len/4) != 1) - return 0; + HAL_StatusTypeDef status = stm_flash_write32(dfu_offset, (uint32_t *)buf, len/4); dfu_offset += DFU_UPLOAD_CHUNK_SIZE; - return 1; + return status; } static int cmd_bootloader_upload(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + if (user < HAL_USER_SO) { cli_print(cli, "Permission denied."); return CLI_ERROR; diff --git a/projects/hsm/mgmt-cli.c b/projects/hsm/mgmt-cli.c index ec9bf8f..2b5be1f 100644 --- a/projects/hsm/mgmt-cli.c +++ b/projects/hsm/mgmt-cli.c @@ -64,8 +64,8 @@ static tcb_t *cli_task; #endif typedef struct { - int ridx; - volatile int widx; + unsigned ridx; + unsigned widx; mgmt_cli_dma_state_t rx_state; uint8_t buf[CLI_UART_RECVBUF_SIZE]; } ringbuf_t; @@ -103,6 +103,8 @@ static uint8_t uart_rx; */ void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) { + huart = huart; + ringbuf_write_char(&uart_ringbuf, uart_rx); task_wake(cli_task); } @@ -110,13 +112,13 @@ void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) static void uart_cli_print(struct cli_def *cli __attribute__ ((unused)), const char *buf) { char crlf[] = "\r\n"; - uart_send_string2(STM_UART_MGMT, buf); - uart_send_string2(STM_UART_MGMT, crlf); + uart_send_string(buf); + uart_send_string(crlf); } static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count) { - for (int i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) { while (ringbuf_read_char(&uart_ringbuf, (uint8_t *)(buf + i)) == 0) task_sleep(); } @@ -125,7 +127,7 @@ static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void static ssize_t uart_cli_write(struct cli_def *cli __attribute__ ((unused)), const void *buf, size_t count) { - uart_send_bytes(STM_UART_MGMT, (uint8_t *) buf, count); + uart_send_bytes((uint8_t *) buf, count); return (ssize_t)count; } diff --git a/projects/hsm/mgmt-firmware.c b/projects/hsm/mgmt-firmware.c index ec8a69d..b6b3321 100644 --- a/projects/hsm/mgmt-firmware.c +++ b/projects/hsm/mgmt-firmware.c @@ -36,7 +36,6 @@ #define HAL_OK CMSIS_HAL_OK #include "stm-init.h" #include "stm-uart.h" -#include "stm-flash.h" #include "mgmt-cli.h" @@ -49,6 +48,10 @@ extern hal_user_t user; static int cmd_firmware_upload(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + if (user < HAL_USER_SO) { cli_print(cli, "Permission denied."); return CLI_ERROR; diff --git a/projects/hsm/mgmt-fpga.c b/projects/hsm/mgmt-fpga.c index 06f2a26..af7ba11 100644 --- a/projects/hsm/mgmt-fpga.c +++ b/projects/hsm/mgmt-fpga.c @@ -55,20 +55,31 @@ extern hal_user_t user; static volatile uint32_t dfu_offset = 0; -static int _flash_write_callback(uint8_t *buf, size_t len) +static HAL_StatusTypeDef _flash_write_callback(uint8_t *buf, size_t len) { + HAL_StatusTypeDef res; + if ((dfu_offset % FPGACFG_SECTOR_SIZE) == 0) /* first page in sector, need to erase sector */ - if (fpgacfg_erase_sector(dfu_offset / FPGACFG_SECTOR_SIZE) != 1) - return CLI_ERROR; + if ((res = fpgacfg_erase_sector(dfu_offset / FPGACFG_SECTOR_SIZE)) != CMSIS_HAL_OK) + return res; - int res = fpgacfg_write_data(dfu_offset, buf, BITSTREAM_UPLOAD_CHUNK_SIZE) == 1; + /* fpgacfg_write_data (a thin wrapper around n25q128_write_data) + * requires the offset and length to be page-aligned. The last chunk + * will be short, so we pad it out to the full chunk size. + */ + len = len; + res = fpgacfg_write_data(dfu_offset, buf, BITSTREAM_UPLOAD_CHUNK_SIZE); dfu_offset += BITSTREAM_UPLOAD_CHUNK_SIZE; return res; } static int cmd_fpga_bitstream_upload(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + if (user < HAL_USER_SO) { cli_print(cli, "Permission denied."); return CLI_ERROR; @@ -81,7 +92,7 @@ static int cmd_fpga_bitstream_upload(struct cli_def *cli, const char *command, c fpgacfg_access_control(ALLOW_ARM); cli_print(cli, "Checking if FPGA config memory is accessible"); - if (fpgacfg_check_id() != 1) { + if (fpgacfg_check_id() != CMSIS_HAL_OK) { cli_print(cli, "ERROR: FPGA config memory not accessible. Check that jumpers JP7 and JP8 are installed."); return CLI_ERROR; } @@ -96,10 +107,14 @@ static int cmd_fpga_bitstream_upload(struct cli_def *cli, const char *command, c static int cmd_fpga_bitstream_erase(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + fpgacfg_access_control(ALLOW_ARM); cli_print(cli, "Checking if FPGA config memory is accessible"); - if (fpgacfg_check_id() != 1) { + if (fpgacfg_check_id() != CMSIS_HAL_OK) { cli_print(cli, "ERROR: FPGA config memory not accessible. Check that jumpers JP7 and JP8 are installed."); return CLI_ERROR; } @@ -110,7 +125,7 @@ static int cmd_fpga_bitstream_erase(struct cli_def *cli, const char *command, ch * * This command could be made to accept an argument indicating the whole memory should be erased. */ - if (fpgacfg_erase_sector(0) != 0) { + if (fpgacfg_erase_sector(0) != CMSIS_HAL_OK) { cli_print(cli, "Erasing first sector in FPGA config memory failed"); return CLI_ERROR; } @@ -123,6 +138,10 @@ static int cmd_fpga_bitstream_erase(struct cli_def *cli, const char *command, ch static int cmd_fpga_reset(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + fpgacfg_access_control(ALLOW_FPGA); fpgacfg_reset_fpga(RESET_FULL); hal_core_reset_table(); @@ -136,7 +155,11 @@ static int cmd_fpga_show_cores(struct cli_def *cli, const char *command, char *a hal_core_t *core; const hal_core_info_t *info; - if (! fpgacfg_check_done()) { + command = command; + argv = argv; + argc = argc; + + if (fpgacfg_check_done() != CMSIS_HAL_OK) { cli_print(cli, "FPGA has not loaded a bitstream"); return CLI_OK; } diff --git a/projects/hsm/mgmt-keystore.c b/projects/hsm/mgmt-keystore.c index 0104265..b79a5fe 100644 --- a/projects/hsm/mgmt-keystore.c +++ b/projects/hsm/mgmt-keystore.c @@ -59,6 +59,8 @@ static int cmd_keystore_set_pin(struct cli_def *cli, const char *command, char * hal_error_t status; hal_client_handle_t client = { -1 }; + command = command; + if (argc != 2) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore set pin <user|so|wheel> <pin>"); @@ -91,6 +93,8 @@ static int cmd_keystore_clear_pin(struct cli_def *cli, const char *command, char hal_error_t status; hal_client_handle_t client = { -1 }; + command = command; + if (argc != 1) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore clear pin <user|so|wheel>"); @@ -122,6 +126,8 @@ static int cmd_keystore_set_pin_iterations(struct cli_def *cli, const char *comm hal_error_t status; hal_client_handle_t client = { -1 }; + command = command; + if (argc != 1) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore set pin iterations <number>"); @@ -145,6 +151,8 @@ static int cmd_keystore_delete_key(struct cli_def *cli, const char *command, cha hal_error_t status; hal_uuid_t name; + command = command; + if (argc != 1) { cli_print(cli, "Wrong number of arguments (%i).", argc); cli_print(cli, "Syntax: keystore delete key <name>"); @@ -205,7 +213,7 @@ static int show_keys(struct cli_def *cli, const char *title) if (!done) previous_uuid = uuids[sizeof(uuids)/sizeof(*uuids) - 1]; - for (int i = 0; i < n; i++) { + for (unsigned i = 0; i < n; i++) { if ((status = hal_uuid_format(&uuids[i], key_name, sizeof(key_name))) != LIBHAL_OK) { cli_print(cli, "Could not convert key name, skipping: %s", @@ -276,6 +284,10 @@ static int show_pin(struct cli_def *cli, char *label, hal_user_t user) static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + int err = 0; err |= show_keys(cli, "Keystore:"); @@ -291,7 +303,9 @@ static int cmd_keystore_show_keys(struct cli_def *cli, const char *command, char static int cmd_keystore_erase(struct cli_def *cli, const char *command, char *argv[], int argc) { hal_error_t err; - int status; + HAL_StatusTypeDef status; + + command = command; if (argc != 1 || strcmp(argv[0], "YesIAmSure") != 0) { cli_print(cli, "Syntax: keystore erase YesIAmSure"); @@ -299,7 +313,7 @@ static int cmd_keystore_erase(struct cli_def *cli, const char *command, char *ar } cli_print(cli, "OK, erasing keystore, this will take about 45 seconds..."); - if ((status = keystore_erase_bulk()) != 1) { + if ((status = keystore_erase_bulk()) != CMSIS_HAL_OK) { cli_print(cli, "Failed erasing token keystore: %i", status); return CLI_ERROR; } diff --git a/projects/hsm/mgmt-masterkey.c b/projects/hsm/mgmt-masterkey.c index 9f5e4d0..765cb10 100644 --- a/projects/hsm/mgmt-masterkey.c +++ b/projects/hsm/mgmt-masterkey.c @@ -82,6 +82,10 @@ static int cmd_masterkey_status(struct cli_def *cli, const char *command, char * { hal_error_t status; + command = command; + argv = argv; + argc = argc; + cli_print(cli, "Status of master key:\n"); status = hal_mkm_volatile_read(NULL, 0); @@ -107,7 +111,7 @@ static int _masterkey_set(struct cli_def *cli, char *argv[], int argc, return CLI_ERROR; } cli_print(cli, "Random key:\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); cli_print(cli, "\n"); } @@ -118,7 +122,7 @@ static int _masterkey_set(struct cli_def *cli, char *argv[], int argc, } cli_print(cli, "Parsed key:\n"); - uart_send_hexdump(STM_UART_MGMT, buf, 0, sizeof(buf) - 1); + uart_send_hexdump(buf, 0, sizeof(buf) - 1); cli_print(cli, "\n"); } @@ -132,6 +136,8 @@ static int _masterkey_set(struct cli_def *cli, char *argv[], int argc, static int cmd_masterkey_set(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + return _masterkey_set(cli, argv, argc, "volatile", hal_mkm_volatile_write); } @@ -139,6 +145,10 @@ static int cmd_masterkey_erase(struct cli_def *cli, const char *command, char *a { hal_error_t err; + command = command; + argv = argv; + argc = argc; + if ((err = hal_mkm_volatile_erase(KEK_LENGTH)) == LIBHAL_OK) { cli_print(cli, "Erased master key from volatile memory"); } else { @@ -149,6 +159,8 @@ static int cmd_masterkey_erase(struct cli_def *cli, const char *command, char *a static int cmd_masterkey_unsecure_set(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + return _masterkey_set(cli, argv, argc, "flash", hal_mkm_flash_write); } @@ -156,6 +168,10 @@ static int cmd_masterkey_unsecure_erase(struct cli_def *cli, const char *command { hal_error_t err; + command = command; + argv = argv; + argc = argc; + if ((err = hal_mkm_flash_erase(KEK_LENGTH)) == LIBHAL_OK) { cli_print(cli, "Erased unsecure master key from flash"); } else { diff --git a/projects/hsm/mgmt-misc.c b/projects/hsm/mgmt-misc.c index 016d7cb..86f1be8 100644 --- a/projects/hsm/mgmt-misc.c +++ b/projects/hsm/mgmt-misc.c @@ -60,7 +60,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal cli_print(cli, "OK, write size (4 bytes), data in %li byte chunks, CRC-32 (4 bytes)", (uint32_t) n); - if (uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, sizeof(filesize), 2000) != CMSIS_HAL_OK) { + if (uart_receive_bytes((void *) &filesize, sizeof(filesize), 2000) != CMSIS_HAL_OK) { cli_print(cli, "Receive timed out"); goto fail; } @@ -75,7 +75,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal if (filesize < n) n = filesize; - if (uart_receive_bytes(STM_UART_MGMT, (void *) buf, n, 2000) != CMSIS_HAL_OK) { + if (uart_receive_bytes((void *) buf, n, 2000) != CMSIS_HAL_OK) { cli_print(cli, "Receive timed out"); goto fail; } @@ -85,18 +85,18 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal /* After reception of a chunk but before ACKing we have "all" the time in the world to * calculate CRC and invoke the data_callback. */ - if (data_callback != NULL && ! data_callback(buf, (size_t) n)) { + if (data_callback != NULL && data_callback(buf, n) != CMSIS_HAL_OK) { cli_print(cli, "Data processing failed"); goto okay; } counter++; - uart_send_bytes(STM_UART_MGMT, (void *) &counter, 4); + uart_send_bytes((void *) &counter, 4); } my_crc = hal_crc32_finalize(my_crc); cli_print(cli, "Send CRC-32"); - uart_receive_bytes(STM_UART_MGMT, (void *) &crc, sizeof(crc), 2000); + uart_receive_bytes((void *) &crc, sizeof(crc), 2000); cli_print(cli, "CRC-32 0x%x, calculated CRC 0x%x", (unsigned int) crc, (unsigned int) my_crc); if (crc == my_crc) { cli_print(cli, "CRC checksum MATCHED"); @@ -116,6 +116,11 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal #ifdef DO_PROFILING static int cmd_profile_start(struct cli_def *cli, const char *command, char *argv[], int argc) { + cli = cli; + command = command; + argv = argv; + argc = argc; + extern uint32_t CRYPTECH_FIRMWARE_START; extern char __etext; /* end of text/code symbol, defined by linker */ extern void monstartup (size_t lowpc, size_t highpc); @@ -125,6 +130,11 @@ static int cmd_profile_start(struct cli_def *cli, const char *command, char *arg static int cmd_profile_stop(struct cli_def *cli, const char *command, char *argv[], int argc) { + cli = cli; + command = command; + argv = argv; + argc = argc; + extern void _mcleanup(void); _mcleanup(); return CLI_OK; @@ -134,6 +144,10 @@ static int cmd_profile_stop(struct cli_def *cli, const char *command, char *argv static int cmd_reboot(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + cli_print(cli, "\n\n\nRebooting\n\n\n"); HAL_NVIC_SystemReset(); diff --git a/projects/hsm/mgmt-misc.h b/projects/hsm/mgmt-misc.h index 862ca0c..ef63a9e 100644 --- a/projects/hsm/mgmt-misc.h +++ b/projects/hsm/mgmt-misc.h @@ -37,7 +37,8 @@ #include <libcli.h> -typedef int (*cli_data_callback)(uint8_t *, size_t); +/* Write a chunk of received data to flash. */ +typedef HAL_StatusTypeDef (*cli_data_callback)(uint8_t *, size_t); extern int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_callback data_callback); diff --git a/projects/hsm/mgmt-task.c b/projects/hsm/mgmt-task.c index 4668585..c2a3d3f 100644 --- a/projects/hsm/mgmt-task.c +++ b/projects/hsm/mgmt-task.c @@ -52,6 +52,10 @@ extern size_t request_queue_max(void); static int cmd_task_show(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + cli_print(cli, "name state stack high water"); cli_print(cli, "-------- -------- ----------------"); @@ -76,6 +80,10 @@ static int cmd_task_show(struct cli_def *cli, const char *command, char *argv[], #ifdef DO_TASK_METRICS static int cmd_task_show_metrics(struct cli_def *cli, const char *command, char *argv[], int argc) { + command = command; + argv = argv; + argc = argc; + struct task_metrics tm; task_get_metrics(&tm); @@ -88,6 +96,11 @@ static int cmd_task_show_metrics(struct cli_def *cli, const char *command, char static int cmd_task_reset_metrics(struct cli_def *cli, const char *command, char *argv[], int argc) { + cli = cli; + command = command; + argv = argv; + argc = argc; + task_reset_metrics(); return CLI_OK; diff --git a/projects/libhal-test/gettimeofday.c b/projects/libhal-test/gettimeofday.c index b13485d..f7a87f1 100644 --- a/projects/libhal-test/gettimeofday.c +++ b/projects/libhal-test/gettimeofday.c @@ -56,6 +56,8 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) { uint32_t tick = HAL_GetTick(); /* uptime in ms */ + tz = tz; + tv->tv_sec = tick / 1000; tv->tv_usec = (tick % 1000) * 1000; diff --git a/projects/libhal-test/printf.c b/projects/libhal-test/printf.c index 5a15d12..85af09b 100644 --- a/projects/libhal-test/printf.c +++ b/projects/libhal-test/printf.c @@ -244,7 +244,7 @@ OK, I found my mistake. The math here is _always_ unsigned */ if (precision != 0)
{
width = max(width, precision);
- if (precision > strlen(where))
+ if (precision > strlen((const char *)where))
flags |= PR_LZ;
precision = 0;
}
@@ -295,7 +295,7 @@ EMIT2: if((flags & PR_LJ) == 0) count++;
}
/* emit string/char/converted number */
- for(int i = (flags & PR_WS) ? 1 : 0;
+ for(unsigned i = (flags & PR_WS) ? 1 : 0;
i < length; ++i)
{
fn(*where++, &ptr);
@@ -362,6 +362,8 @@ You must write your own putchar() *****************************************************************************/
int vprintf_help(unsigned c, void **ptr)
{
+ ptr = ptr;
+
putchar(c);
return 0 ;
}
diff --git a/spiflash_n25q128.c b/spiflash_n25q128.c index 5c4a3b2..5e10185 100644 --- a/spiflash_n25q128.c +++ b/spiflash_n25q128.c @@ -62,9 +62,6 @@ static inline int _n25q128_get_status_bit(struct spiflash_ctx *ctx, unsigned bit uint8_t spi_tx[2]; uint8_t spi_rx[2]; - // result - HAL_StatusTypeDef ok; - //assert(bitnum < sizeof(uint8_t)); // send READ STATUS command @@ -72,11 +69,12 @@ static inline int _n25q128_get_status_bit(struct spiflash_ctx *ctx, unsigned bit // send command, read response, deselect _n25q128_select(ctx); - ok = HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 2, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 2, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return -1; + if (!ok) return -1; // done return ((spi_rx[1] >> bitnum) & 1); @@ -95,89 +93,85 @@ static inline int _n25q128_get_wip_flag(struct spiflash_ctx *ctx) } /* Wait until the flash memory is done writing */ -static int _n25q128_wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) +static HAL_StatusTypeDef _n25q128_wait_while_wip(struct spiflash_ctx *ctx, uint32_t timeout) { uint32_t tick_end = HAL_GetTick() + timeout; - int i; do { - i = _n25q128_get_wip_flag(ctx); - if (i < 0) return 0; - if (! i) return 1; + switch (_n25q128_get_wip_flag(ctx)) { + case 0: + return HAL_OK; + case -1: + return HAL_ERROR; + default: + /* try again */ + continue; + } } while (HAL_GetTick() < tick_end); - return 0; + return HAL_TIMEOUT; } /* Send the Write Enable command */ -static int _n25q128_write_enable(struct spiflash_ctx *ctx) +static HAL_StatusTypeDef _n25q128_write_enable(struct spiflash_ctx *ctx) { // tx buffer uint8_t spi_tx[1]; - // result - HAL_StatusTypeDef ok; - // enable writing spi_tx[0] = N25Q128_COMMAND_WRITE_ENABLE; // activate, send command, deselect _n25q128_select(ctx); - ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return -1; + if (!ok) return HAL_ERROR; // make sure, that write enable did the job - return _n25q128_get_wel_flag(ctx); + return _n25q128_get_wel_flag(ctx) ? HAL_OK : HAL_ERROR; } -int n25q128_check_id(struct spiflash_ctx *ctx) +HAL_StatusTypeDef n25q128_check_id(struct spiflash_ctx *ctx) { // tx, rx buffers uint8_t spi_tx[4]; uint8_t spi_rx[4]; - // result - HAL_StatusTypeDef ok; - // send READ ID command spi_tx[0] = N25Q128_COMMAND_READ_ID; // select, send command & read response, deselect _n25q128_select(ctx); - ok = HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 4, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_TransmitReceive(ctx->hspi, spi_tx, spi_rx, 4, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return 0; + if (!ok) return HAL_ERROR; // parse response (note, that the very first byte was received during the // transfer of the command byte, so it contains garbage and should // be ignored here) - if (spi_rx[1] != N25Q128_ID_MANUFACTURER) return 0; - if (spi_rx[2] != N25Q128_ID_DEVICE_TYPE) return 0; - if (spi_rx[3] != N25Q128_ID_DEVICE_CAPACITY) return 0; - - // done - return 1; + return + (spi_rx[1] == N25Q128_ID_MANUFACTURER && + spi_rx[2] == N25Q128_ID_DEVICE_TYPE && + spi_rx[3] == N25Q128_ID_DEVICE_CAPACITY) ? HAL_OK : HAL_ERROR; } -int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uint8_t *page_buffer) +HAL_StatusTypeDef n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uint8_t *page_buffer) { // tx buffer uint8_t spi_tx[4]; - // result - HAL_StatusTypeDef ok; - // check offset - if (page_offset >= N25Q128_NUM_PAGES) return 0; + if (page_offset >= N25Q128_NUM_PAGES) return HAL_ERROR; // enable writing - if (_n25q128_write_enable(ctx) != 1) return 0; + if (_n25q128_write_enable(ctx) != 0) return HAL_ERROR; // calculate byte address uint32_t byte_offset = page_offset * N25Q128_PAGE_SIZE; @@ -188,44 +182,31 @@ int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uin spi_tx[2] = (uint8_t)(byte_offset >> 8); spi_tx[3] = (uint8_t)(byte_offset >> 0); - // activate, send command + // activate, send command, send data, deselect _n25q128_select(ctx); - ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT); - - // check - if (ok != HAL_OK) { - _n25q128_deselect(ctx); - return 0; - } - - // send data, deselect - ok = HAL_SPI_Transmit(ctx->hspi, (uint8_t *) page_buffer, N25Q128_PAGE_SIZE, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT) == HAL_OK && + HAL_SPI_Transmit(ctx->hspi, (uint8_t *) page_buffer, N25Q128_PAGE_SIZE, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return 0; + if (!ok) return HAL_ERROR; // wait until write finishes - if (! _n25q128_wait_while_wip(ctx, 1000)) return 0; - - // done - return 1; + return _n25q128_wait_while_wip(ctx, 1000); } static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, uint32_t byte_offset) { // check offset - if (byte_offset >= N25Q128_NUM_BYTES) return 0; + if (byte_offset >= N25Q128_NUM_BYTES) return HAL_ERROR; // tx buffer uint8_t spi_tx[4]; - // result - HAL_StatusTypeDef ok; - // enable writing - if (_n25q128_write_enable(ctx) != 1) return 0; + if (_n25q128_write_enable(ctx) != 0) return HAL_ERROR; // send command (ERASE SECTOR or ERASE SUBSECTOR) spi_tx[0] = command; @@ -235,70 +216,61 @@ static int n25q128_erase_something(struct spiflash_ctx *ctx, uint8_t command, ui // activate, send command, deselect _n25q128_select(ctx); - ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return 0; + if (!ok) return HAL_ERROR; // wait for erase to finish - - if (! _n25q128_wait_while_wip(ctx, 1000)) return 0; - - // done - return 1; + return _n25q128_wait_while_wip(ctx, 1000); } -int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset) +HAL_StatusTypeDef n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset) { return n25q128_erase_something(ctx, N25Q128_COMMAND_ERASE_SECTOR, sector_offset * N25Q128_SECTOR_SIZE); } -int n25q128_erase_subsector(struct spiflash_ctx *ctx, uint32_t subsector_offset) +HAL_StatusTypeDef n25q128_erase_subsector(struct spiflash_ctx *ctx, uint32_t subsector_offset) { return n25q128_erase_something(ctx, N25Q128_COMMAND_ERASE_SUBSECTOR, subsector_offset * N25Q128_SUBSECTOR_SIZE); } -int n25q128_erase_bulk(struct spiflash_ctx *ctx) +HAL_StatusTypeDef n25q128_erase_bulk(struct spiflash_ctx *ctx) { // tx buffer uint8_t spi_tx[1]; - // result - HAL_StatusTypeDef ok; - // enable writing - if (_n25q128_write_enable(ctx) != 1) return 0; + if (_n25q128_write_enable(ctx) != 0) return HAL_ERROR; // send command spi_tx[0] = N25Q128_COMMAND_ERASE_BULK; // activate, send command, deselect _n25q128_select(ctx); - ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_Transmit(ctx->hspi, spi_tx, 1, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return 0; + if (!ok) return HAL_ERROR; // wait for erase to finish - - if (! _n25q128_wait_while_wip(ctx, 60000)) return 0; - - // done - return 1; + return _n25q128_wait_while_wip(ctx, 60000); } /* This function writes of a number of pages to the flash memory. * The caller is responsible for ensuring that the pages have been erased. */ -int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len) +HAL_StatusTypeDef n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len) { uint32_t page; @@ -320,29 +292,25 @@ int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t * it anyway. */ - if ((offset % N25Q128_PAGE_SIZE) != 0) return -1; - if ((len % N25Q128_PAGE_SIZE) != 0) return -2; + if (offset % N25Q128_PAGE_SIZE != 0 || len % N25Q128_PAGE_SIZE != 0) return HAL_ERROR; for (page = 0; page < len / N25Q128_PAGE_SIZE; page++) { - if (! n25q128_write_page(ctx, offset / N25Q128_PAGE_SIZE, buf)) { - return -6; + if (n25q128_write_page(ctx, offset / N25Q128_PAGE_SIZE, buf) != 0) { + return HAL_ERROR; } buf += N25Q128_PAGE_SIZE; offset += N25Q128_PAGE_SIZE; } - return 1; + return HAL_OK; } /* This function reads zero or more pages from the SPI flash. */ -int n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, const uint32_t len) +HAL_StatusTypeDef n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, const uint32_t len) { // tx buffer uint8_t spi_tx[4]; - // result - HAL_StatusTypeDef ok; - /* * The data sheet says: * The addressed byte can be at any location, and the address @@ -356,7 +324,7 @@ int n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, c */ // avoid overflow - if (offset + len > N25Q128_NUM_BYTES) return -3; + if (offset + len > N25Q128_NUM_BYTES) return HAL_ERROR; // prepare READ command spi_tx[0] = N25Q128_COMMAND_READ; @@ -364,23 +332,13 @@ int n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, c spi_tx[2] = (uint8_t)(offset >> 8); spi_tx[3] = (uint8_t)(offset >> 0); - // activate, send command + // activate, send command, read response, deselect _n25q128_select(ctx); - ok = HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT); - - // check - if (ok != HAL_OK) { - _n25q128_deselect(ctx); - return 0; - } - - // read response, deselect - ok = HAL_SPI_Receive(ctx->hspi, buf, len, N25Q128_SPI_TIMEOUT); + int ok = + HAL_SPI_Transmit(ctx->hspi, spi_tx, 4, N25Q128_SPI_TIMEOUT) == HAL_OK && + HAL_SPI_Receive(ctx->hspi, buf, len, N25Q128_SPI_TIMEOUT) == HAL_OK; _n25q128_deselect(ctx); // check - if (ok != HAL_OK) return 0; - - // done - return 1; + return ok ? HAL_OK : HAL_ERROR; } diff --git a/spiflash_n25q128.h b/spiflash_n25q128.h index 50243bd..926be56 100644 --- a/spiflash_n25q128.h +++ b/spiflash_n25q128.h @@ -70,13 +70,13 @@ struct spiflash_ctx { uint16_t cs_n_pin; }; -extern int n25q128_check_id(struct spiflash_ctx *ctx); -extern int n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, const uint32_t len); -extern int n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uint8_t *page_buffer); -extern int n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len); -extern int n25q128_erase_subsector(struct spiflash_ctx *ctx, uint32_t subsector_offset); -extern int n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset); -extern int n25q128_erase_bulk(struct spiflash_ctx *ctx); +extern HAL_StatusTypeDef n25q128_check_id(struct spiflash_ctx *ctx); +extern HAL_StatusTypeDef n25q128_read_data(struct spiflash_ctx *ctx, uint32_t offset, uint8_t *buf, const uint32_t len); +extern HAL_StatusTypeDef n25q128_write_page(struct spiflash_ctx *ctx, uint32_t page_offset, const uint8_t *page_buffer); +extern HAL_StatusTypeDef n25q128_write_data(struct spiflash_ctx *ctx, uint32_t offset, const uint8_t *buf, const uint32_t len); +extern HAL_StatusTypeDef n25q128_erase_subsector(struct spiflash_ctx *ctx, uint32_t subsector_offset); +extern HAL_StatusTypeDef n25q128_erase_sector(struct spiflash_ctx *ctx, uint32_t sector_offset); +extern HAL_StatusTypeDef n25q128_erase_bulk(struct spiflash_ctx *ctx); #define n25q128_read_page(ctx, page_offset, page_buffer) \ n25q128_read_data(ctx, page_offset * N25Q128_PAGE_SIZE, page_buffer, N25Q128_PAGE_SIZE) diff --git a/stm-flash.c b/stm-flash.c index 99e09ee..952543f 100644 --- a/stm-flash.c +++ b/stm-flash.c @@ -69,29 +69,30 @@ uint32_t flash_sector_offsets[FLASH_NUM_SECTORS + 1] = { 0x08200000 /* first address *after* flash */ }; -static int stm_flash_sector_num(const uint32_t offset) +static uint32_t stm_flash_sector_num(const uint32_t offset) { - int i; + uint32_t i; if (offset < flash_sector_offsets[0]) - return -1; + return 0xFFFFFFFF; for (i = 0; i < FLASH_NUM_SECTORS; ++i) if (offset < flash_sector_offsets[i + 1]) return i; - return -1; + return 0xFFFFFFFF; } -int stm_flash_erase_sectors(const uint32_t start_offset, const uint32_t end_offset) +HAL_StatusTypeDef stm_flash_erase_sectors(const uint32_t start_offset, const uint32_t end_offset) { uint32_t start_sector = stm_flash_sector_num(start_offset); uint32_t end_sector = stm_flash_sector_num(end_offset); FLASH_EraseInitTypeDef FLASH_EraseInitStruct; uint32_t SectorError = 0; + HAL_StatusTypeDef err; - if (start_sector > end_sector) return -1; - if (start_sector < 0 || end_sector > FLASH_NUM_SECTORS) return -2; + if (start_sector > end_sector || end_sector > FLASH_NUM_SECTORS) + return HAL_ERROR; FLASH_EraseInitStruct.Sector = start_sector; FLASH_EraseInitStruct.NbSectors = (end_sector - start_sector) + 1; @@ -99,40 +100,38 @@ int stm_flash_erase_sectors(const uint32_t start_offset, const uint32_t end_offs FLASH_EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; HAL_FLASH_Unlock(); - - if (HAL_FLASHEx_Erase(&FLASH_EraseInitStruct, &SectorError) != HAL_OK) { - return -3; - } - + err = HAL_FLASHEx_Erase(&FLASH_EraseInitStruct, &SectorError); HAL_FLASH_Lock(); - if (SectorError == 0xFFFFFFFF) return 0; + if (err != HAL_OK || SectorError != 0xFFFFFFFF) + return HAL_ERROR; - return -3; + return HAL_OK; } -int stm_flash_write32(uint32_t offset, const uint32_t *buf, const uint32_t elements) +HAL_StatusTypeDef stm_flash_write32(uint32_t offset, const uint32_t *buf, const size_t elements) { uint32_t sector = stm_flash_sector_num(offset); - uint32_t i, j; + size_t i; + HAL_StatusTypeDef err = HAL_OK; if (offset == flash_sector_offsets[sector]) { /* Request to write to beginning of a flash sector, erase it first. */ if (stm_flash_erase_sectors(offset, offset) != 0) { - return -1; + return HAL_ERROR; } } HAL_FLASH_Unlock(); for (i = 0; i < elements; i++) { - if ((j = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, offset, buf[i])) != HAL_OK) { - return -2; + if ((err = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, offset, buf[i])) != HAL_OK) { + break; } offset += 4; } HAL_FLASH_Lock(); - return 1; + return err; } diff --git a/stm-flash.h b/stm-flash.h index aecb87f..db7d327 100644 --- a/stm-flash.h +++ b/stm-flash.h @@ -1,7 +1,8 @@ /* * stm-flash.h * ----------- - * Functions and defines for accessing the flash memory. + * Functions for writing/erasing the STM32 internal flash memory. + * The flash is memory mapped, so no code is needed here to read it. * * Copyright (c) 2016, NORDUnet A/S All rights reserved. * @@ -35,7 +36,7 @@ #ifndef __STM32_FLASH_H #define __STM32_FLASH_H -extern int stm_flash_erase_sectors(const uint32_t start_offset, const uint32_t end_offset); -extern int stm_flash_write32(const uint32_t offset, const uint32_t *buf, const uint32_t elements); +extern HAL_StatusTypeDef stm_flash_erase_sectors(const uint32_t start_offset, const uint32_t end_offset); +extern HAL_StatusTypeDef stm_flash_write32(const uint32_t offset, const uint32_t *buf, const size_t elements); #endif /* __STM32_FLASH_H */ @@ -166,7 +166,7 @@ void fmc_init(void) } -static int _fmc_nwait_idle(void) +static HAL_StatusTypeDef _fmc_nwait_idle(void) { int cnt; @@ -175,41 +175,44 @@ static int _fmc_nwait_idle(void) { // read pin state if (HAL_GPIO_ReadPin(FMC_GPIO_PORT_NWAIT, FMC_GPIO_PIN_NWAIT) == FMC_NWAIT_IDLE) - return 0; + return HAL_OK; } - return -1; + return HAL_ERROR; } -int fmc_write_32(uint32_t addr, uint32_t *data) +HAL_StatusTypeDef fmc_write_32(uint32_t addr, uint32_t *data) { // calculate target fpga address uint32_t ptr = FMC_FPGA_BASE_ADDR + (addr & FMC_FPGA_ADDR_MASK); __disable_irq(); - int status = + HAL_StatusTypeDef status = // write data to fpga - (HAL_SRAM_Write_32b(&_fmc_fpga_inst, (uint32_t *)ptr, data, 1) != HAL_OK) || + HAL_SRAM_Write_32b(&_fmc_fpga_inst, (uint32_t *)ptr, data, 1); + if (status == HAL_OK) // wait for transaction to complete - _fmc_nwait_idle(); + status = _fmc_nwait_idle(); __enable_irq(); return status; } -static inline int _fmc_read_32(uint32_t *ptr, uint32_t *data) +static inline HAL_StatusTypeDef _fmc_read_32(uint32_t *ptr, uint32_t *data) { - return + HAL_StatusTypeDef status = // read data from fpga - (HAL_SRAM_Read_32b(&_fmc_fpga_inst, (uint32_t *)ptr, data, 1) != HAL_OK) || + HAL_SRAM_Read_32b(&_fmc_fpga_inst, (uint32_t *)ptr, data, 1); + if (status == HAL_OK) // wait for transaction to complete - _fmc_nwait_idle(); + status = _fmc_nwait_idle(); + return status; } -int fmc_read_32(uint32_t addr, uint32_t *data) +HAL_StatusTypeDef fmc_read_32(uint32_t addr, uint32_t *data) { // calculate target fpga address uint32_t ptr = FMC_FPGA_BASE_ADDR + (addr & FMC_FPGA_ADDR_MASK); @@ -232,9 +235,10 @@ int fmc_read_32(uint32_t addr, uint32_t *data) */ __disable_irq(); - int status = - _fmc_read_32((uint32_t *)ptr, data) || + HAL_StatusTypeDef status = _fmc_read_32((uint32_t *)ptr, data); + if (status == HAL_OK) + status = _fmc_read_32((uint32_t *)ptr, data); __enable_irq(); @@ -57,7 +57,7 @@ extern void fmc_init(void); -extern int fmc_write_32(uint32_t addr, uint32_t *data); -extern int fmc_read_32(uint32_t addr, uint32_t *data); +extern HAL_StatusTypeDef fmc_write_32(uint32_t addr, uint32_t *data); +extern HAL_StatusTypeDef fmc_read_32(uint32_t addr, uint32_t *data); #endif /* __STM_FMC_H */ diff --git a/stm-fpgacfg.c b/stm-fpgacfg.c index b2f09d0..54342bf 100644 --- a/stm-fpgacfg.c +++ b/stm-fpgacfg.c @@ -68,12 +68,12 @@ void fpgacfg_init(void) HAL_SPI_Init(&hspi_fpgacfg); } -int fpgacfg_check_id(void) +HAL_StatusTypeDef fpgacfg_check_id(void) { return n25q128_check_id(&fpgacfg_ctx); } -int fpgacfg_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) +HAL_StatusTypeDef fpgacfg_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) { return n25q128_write_data(&fpgacfg_ctx, offset, buf, len); } @@ -113,13 +113,12 @@ void fpgacfg_reset_fpga(enum fpgacfg_reset reset) } } -int fpgacfg_check_done(void) +HAL_StatusTypeDef fpgacfg_check_done(void) { - GPIO_PinState status = HAL_GPIO_ReadPin(FPGA_DONE_Port, FPGA_DONE_Pin); - return (status == GPIO_PIN_SET); + return (HAL_GPIO_ReadPin(FPGA_DONE_Port, FPGA_DONE_Pin) == GPIO_PIN_SET) ? HAL_OK : HAL_ERROR; } -int fpgacfg_erase_sector(uint32_t sector_offset) +HAL_StatusTypeDef fpgacfg_erase_sector(uint32_t sector_offset) { return n25q128_erase_sector(&fpgacfg_ctx, sector_offset); } diff --git a/stm-fpgacfg.h b/stm-fpgacfg.h index b31dee3..74cc683 100644 --- a/stm-fpgacfg.h +++ b/stm-fpgacfg.h @@ -86,13 +86,13 @@ enum fpgacfg_reset { }; extern void fpgacfg_init(void); -extern int fpgacfg_check_id(void); -extern int fpgacfg_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len); -extern int fpgacfg_erase_sector(uint32_t sector_offset); +extern HAL_StatusTypeDef fpgacfg_check_id(void); +extern HAL_StatusTypeDef fpgacfg_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len); +extern HAL_StatusTypeDef fpgacfg_erase_sector(uint32_t sector_offset); extern void fpgacfg_access_control(enum fpgacfg_access_ctrl access); /* Reset the FPGA */ extern void fpgacfg_reset_fpga(enum fpgacfg_reset reset); /* Check status of FPGA bitstream loading */ -extern int fpgacfg_check_done(void); +extern HAL_StatusTypeDef fpgacfg_check_done(void); #endif /* __STM32_FPGACFG_H */ diff --git a/stm-keystore.c b/stm-keystore.c index 6bec008..7683f40 100644 --- a/stm-keystore.c +++ b/stm-keystore.c @@ -62,46 +62,32 @@ void keystore_init(void) HAL_SPI_Init(&hspi_keystore); } -int keystore_check_id(void) +HAL_StatusTypeDef keystore_check_id(void) { return n25q128_check_id(&keystore_ctx); } -int keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len) +HAL_StatusTypeDef keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len) { return n25q128_read_data(&keystore_ctx, offset, buf, len); } -int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) +HAL_StatusTypeDef keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len) { return n25q128_write_data(&keystore_ctx, offset, buf, len); } -int keystore_erase_subsector(uint32_t subsector_offset) +HAL_StatusTypeDef keystore_erase_subsector(uint32_t subsector_offset) { return n25q128_erase_subsector(&keystore_ctx, subsector_offset); } -int keystore_erase_sector(uint32_t sector_offset) +HAL_StatusTypeDef keystore_erase_sector(uint32_t sector_offset) { return n25q128_erase_sector(&keystore_ctx, sector_offset); } -int keystore_erase_bulk(void) +HAL_StatusTypeDef keystore_erase_bulk(void) { return n25q128_erase_bulk(&keystore_ctx); } - -/* - * Deprecated, will be removed when we fix libhal/ks_flash.c to use the - * new function. I love inter-dependent repos. - */ - -int keystore_erase_subsectors(uint32_t start, uint32_t stop) -{ - for (int i = start; i <= stop; ++i) { - if (keystore_erase_subsector(i) != 1) - return 0; - } - return 1; -} diff --git a/stm-keystore.h b/stm-keystore.h index f660790..9aa740c 100644 --- a/stm-keystore.h +++ b/stm-keystore.h @@ -56,14 +56,11 @@ extern void keystore_init(void); -extern int keystore_check_id(void); -extern int keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len); -extern int keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len); -extern int keystore_erase_subsector(uint32_t subsector_offset); -extern int keystore_erase_sector(uint32_t sector_offset); -extern int keystore_erase_bulk(void); - -/* Deprecated, will be removed. */ -extern int keystore_erase_subsectors(uint32_t start, uint32_t stop); +extern HAL_StatusTypeDef keystore_check_id(void); +extern HAL_StatusTypeDef keystore_read_data(uint32_t offset, uint8_t *buf, const uint32_t len); +extern HAL_StatusTypeDef keystore_write_data(uint32_t offset, const uint8_t *buf, const uint32_t len); +extern HAL_StatusTypeDef keystore_erase_subsector(uint32_t subsector_offset); +extern HAL_StatusTypeDef keystore_erase_sector(uint32_t sector_offset); +extern HAL_StatusTypeDef keystore_erase_bulk(void); #endif /* __STM32_KEYSTORE_H */ @@ -60,7 +60,7 @@ HAL_StatusTypeDef rtc_device_ready(uint16_t i2c_addr) return HAL_I2C_IsDeviceReady (&hi2c_rtc, i2c_addr, 10, 1000); } -HAL_StatusTypeDef rtc_enable_oscillator() +HAL_StatusTypeDef rtc_enable_oscillator(void) { uint8_t buf[2]; HAL_StatusTypeDef res; @@ -96,7 +96,7 @@ HAL_StatusTypeDef rtc_read_bytes (const uint16_t i2c_addr, uint8_t *buf, const u { HAL_StatusTypeDef res; - while (HAL_I2C_Master_Receive (&hi2c_rtc, i2c_addr, buf, len, 1000) != HAL_OK) { + while (HAL_I2C_Master_Receive (&hi2c_rtc, i2c_addr, buf, len, timeout) != HAL_OK) { res = HAL_I2C_GetError (&hi2c_rtc); if (res != HAL_I2C_ERROR_AF) { return res; @@ -43,7 +43,7 @@ UART_HandleTypeDef huart_user; /* USART2 */ DMA_HandleTypeDef hdma_usart_mgmt_rx; DMA_HandleTypeDef hdma_usart_user_rx; -static stm_uart_port_t default_uart = STM_UART_USER; +UART_HandleTypeDef* default_uart = STM_UART_MGMT; #ifdef HAL_DMA_MODULE_ENABLED /** @@ -118,100 +118,52 @@ void uart_init(void) MX_USART2_UART_Init(); } -void uart_set_default(stm_uart_port_t port) +void uart_set_default(UART_HandleTypeDef *uart) { - if (port == STM_UART_USER || port == STM_UART_MGMT) - default_uart = port; -} - -static inline UART_HandleTypeDef *_which_uart(stm_uart_port_t port) -{ - if (port == STM_UART_USER) { - return &huart_user; - } else if (port == STM_UART_MGMT) { - return &huart_mgmt; - } - - return NULL; + default_uart = uart; } /* send a single character */ -HAL_StatusTypeDef uart_send_char(uint8_t ch) -{ - return uart_send_char2(default_uart, ch); -} - -HAL_StatusTypeDef uart_send_char2(stm_uart_port_t port, uint8_t ch) +HAL_StatusTypeDef uart_send_char2(UART_HandleTypeDef *uart, uint8_t ch) { - return uart_send_bytes(port, &ch, 1); + return uart_send_bytes2(uart, &ch, 1); } /* receive a single character */ -HAL_StatusTypeDef uart_recv_char(uint8_t *cp) +HAL_StatusTypeDef uart_recv_char2(UART_HandleTypeDef *uart, uint8_t *cp, uint32_t timeout) { - return uart_recv_char2(default_uart, cp, HAL_MAX_DELAY); -} - -/* receive a single character */ -HAL_StatusTypeDef uart_recv_char2(stm_uart_port_t port, uint8_t *cp, uint32_t timeout) -{ - UART_HandleTypeDef *uart = _which_uart(port); - - if (uart) - return HAL_UART_Receive(uart, cp, 1, timeout); - - return HAL_ERROR; -} - -/* send a string */ -HAL_StatusTypeDef uart_send_string(char *s) -{ - return uart_send_string2(default_uart, s); + return HAL_UART_Receive(uart, cp, 1, timeout); } /* send a string */ -HAL_StatusTypeDef uart_send_string2(stm_uart_port_t port, const char *s) +HAL_StatusTypeDef uart_send_string2(UART_HandleTypeDef *uart, const char *s) { - return uart_send_bytes(port, (uint8_t *) s, strlen(s)); + return uart_send_bytes2(uart, (uint8_t *) s, strlen(s)); } /* send raw bytes */ -HAL_StatusTypeDef uart_send_bytes(stm_uart_port_t port, uint8_t *buf, size_t len) +HAL_StatusTypeDef uart_send_bytes2(UART_HandleTypeDef *uart, uint8_t *buf, size_t len) { - UART_HandleTypeDef *uart = _which_uart(port); - - if (uart) { - for (int timeout = 0; timeout < 100; ++timeout) { - HAL_UART_StateTypeDef status = HAL_UART_GetState(uart); - if (status == HAL_UART_STATE_READY || - status == HAL_UART_STATE_BUSY_RX) - return HAL_UART_Transmit(uart, (uint8_t *) buf, (uint32_t) len, 0x1); - } + for (int timeout = 0; timeout < 100; ++timeout) { + HAL_UART_StateTypeDef status = HAL_UART_GetState(uart); + if (status == HAL_UART_STATE_READY || + status == HAL_UART_STATE_BUSY_RX) + return HAL_UART_Transmit(uart, (uint8_t *) buf, (uint32_t) len, 0x1); } - return HAL_ERROR; + return HAL_TIMEOUT; } /* receive raw bytes */ -HAL_StatusTypeDef uart_receive_bytes(stm_uart_port_t port, uint8_t *buf, size_t len, uint32_t timeout) +HAL_StatusTypeDef uart_receive_bytes2(UART_HandleTypeDef *uart, uint8_t *buf, size_t len, uint32_t timeout) { - UART_HandleTypeDef *uart = _which_uart(port); - - if (uart) - return HAL_UART_Receive(uart, (uint8_t *) buf, (uint32_t) len, timeout); - - return HAL_ERROR; + return HAL_UART_Receive(uart, (uint8_t *) buf, (uint32_t) len, timeout); } /* Generalized routine to send binary, decimal, and hex integers. * This code is adapted from Chris Giese's printf.c */ -HAL_StatusTypeDef uart_send_number(uint32_t num, uint8_t digits, uint8_t radix) -{ - return uart_send_number2(default_uart, num, digits, radix); -} - -HAL_StatusTypeDef uart_send_number2(stm_uart_port_t port, uint32_t num, uint8_t digits, uint8_t radix) +HAL_StatusTypeDef uart_send_number2(UART_HandleTypeDef *uart, uint32_t num, uint8_t digits, uint8_t radix) { #define BUFSIZE 32 char buf[BUFSIZE]; @@ -239,29 +191,30 @@ HAL_StatusTypeDef uart_send_number2(stm_uart_port_t port, uint32_t num, uint8_t /* number is larger than the specified number of digits */ digits = buf + BUFSIZE - where; - return uart_send_bytes(port, (uint8_t *) where, digits); + return uart_send_bytes2(uart, (uint8_t *) where, digits); } -HAL_StatusTypeDef uart_send_hexdump(stm_uart_port_t port, const uint8_t *buf, - const uint8_t start_offset, const uint8_t end_offset) +HAL_StatusTypeDef uart_send_hexdump2(UART_HandleTypeDef *uart, const uint8_t *buf, + const uint8_t start_offset, const uint8_t end_offset) { uint32_t i; - uart_send_string2(port, "00 -- "); + uart_send_number2(uart, start_offset, 2, 16); + uart_send_string2(uart, " -- "); - for (i = 0; i <= end_offset; i++) { + for (i = start_offset; i <= end_offset; i++) { if (i && (! (i % 16))) { - uart_send_string2(port, "\r\n"); + uart_send_string2(uart, "\r\n"); if (i != end_offset) { /* Output new offset unless the last byte is reached */ - uart_send_number2(port, i, 2, 16); - uart_send_string2(port, " -- "); + uart_send_number2(uart, i, 2, 16); + uart_send_string2(uart, " -- "); } } - uart_send_number2(port, *(buf + i), 2, 16); - uart_send_string2(port, " "); + uart_send_number2(uart, *(buf + i), 2, 16); + uart_send_string2(uart, " "); } return HAL_OK; @@ -37,41 +37,48 @@ #include "stm32f4xx_hal.h" -#define USART_MGMT_BAUD_RATE 921600 -#define USART_USER_BAUD_RATE 921600 - -typedef enum { - STM_UART_USER, - STM_UART_MGMT -} stm_uart_port_t; +#define USART_MGMT_BAUD_RATE 921600 +#define USART_USER_BAUD_RATE 921600 extern UART_HandleTypeDef huart_mgmt; extern UART_HandleTypeDef huart_user; +#define STM_UART_USER &huart_user +#define STM_UART_MGMT &huart_mgmt + +/* These are only exposed because they're used in the DMA IRQ handler code. + * Pretend you never saw them. + */ extern DMA_HandleTypeDef hdma_usart_mgmt_rx; extern DMA_HandleTypeDef hdma_usart_user_rx; extern void uart_init(void); -extern void uart_set_default(stm_uart_port_t port); - -extern HAL_StatusTypeDef uart_send_char(uint8_t ch); -extern HAL_StatusTypeDef uart_recv_char(uint8_t *cp); - -extern HAL_StatusTypeDef uart_send_string(char *s); -extern HAL_StatusTypeDef uart_send_number(uint32_t num, uint8_t digits, uint8_t radix); - -extern HAL_StatusTypeDef uart_send_char2(stm_uart_port_t port, uint8_t ch); -extern HAL_StatusTypeDef uart_recv_char2(stm_uart_port_t port, uint8_t *cp, uint32_t timeout); - -extern HAL_StatusTypeDef uart_send_string2(stm_uart_port_t port, const char *s); -extern HAL_StatusTypeDef uart_send_number2(stm_uart_port_t port, uint32_t num, uint8_t digits, uint8_t radix); - -extern HAL_StatusTypeDef uart_send_bytes(stm_uart_port_t port, uint8_t *buf, size_t len); -extern HAL_StatusTypeDef uart_receive_bytes(stm_uart_port_t port, uint8_t *buf, size_t len, uint32_t timeout); +/* Default UART is MGMT; don't change it unless you need to. + */ +extern UART_HandleTypeDef* default_uart; +extern void uart_set_default(UART_HandleTypeDef *uart); -extern HAL_StatusTypeDef uart_send_hexdump(stm_uart_port_t port, const uint8_t *buf, - const uint8_t start_offset, const uint8_t end_offset); +/* Send and receive to/from an explicit UART. For the most part, you + * shouldn't need to call these directly, but can use the default_uart + * macros below. + */ +extern HAL_StatusTypeDef uart_send_char2(UART_HandleTypeDef *uart, uint8_t ch); +extern HAL_StatusTypeDef uart_recv_char2(UART_HandleTypeDef *uart, uint8_t *cp, uint32_t timeout); +extern HAL_StatusTypeDef uart_send_string2(UART_HandleTypeDef *uart, const char *s); +extern HAL_StatusTypeDef uart_send_number2(UART_HandleTypeDef *uart, uint32_t num, uint8_t digits, uint8_t radix); +extern HAL_StatusTypeDef uart_send_bytes2(UART_HandleTypeDef *uart, uint8_t *buf, size_t len); +extern HAL_StatusTypeDef uart_receive_bytes2(UART_HandleTypeDef *uart, uint8_t *buf, size_t len, uint32_t timeout); +extern HAL_StatusTypeDef uart_send_hexdump2(UART_HandleTypeDef *uart, const uint8_t *buf, + const uint8_t start_offset, const uint8_t end_offset); + +#define uart_send_char(c) uart_send_char2(default_uart, c) +#define uart_recv_char(cp, t) uart_recv_char2(default_uart, cp, t) +#define uart_send_string(s) uart_send_string2(default_uart, s) +#define uart_send_bytes(b, l) uart_send_bytes2(default_uart, b, l) +#define uart_receive_bytes(b, l, t) uart_receive_bytes2(default_uart, b, l, t) +#define uart_send_number(n, d, r) uart_send_number2(default_uart, n, d, r) +#define uart_send_hexdump(b, s, e) uart_send_hexdump2(default_uart, b, s, e) #define uart_send_binary(num, bits) uart_send_number(num, bits, 2) #define uart_send_integer(num, digits) uart_send_number(num, digits, 10) @@ -102,6 +102,9 @@ int _write_r (struct _reent *r, int file, char * ptr, int len) int _close_r (struct _reent *r, int file) { + r = r; + file = file; + return 0; } #endif @@ -117,6 +120,8 @@ caddr_t _sbrk_r (struct _reent *r, int incr) static char * heap_end; char * prev_heap_end; + r = r; + if (heap_end == NULL) heap_end = & end; |