aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2017-10-17 00:27:11 -0400
committerPaul Selkirk <paul@psgd.org>2017-10-17 00:27:11 -0400
commit03b14b4cd4a214a92e3968ff33a13c2086896864 (patch)
tree4b391205211d55a0cba4750682784f5d1799fb44
parent5af4a915edf3e77705fa2625081200b61f8dda26 (diff)
Overhaul UART API
MGMT is the default UART, and no one should have to explicitly refer to the UART unless they need USER (hsm.c:hal_serial_send_char). The default UART is now exposed in the header file, so that the default-using functions can be macros, which saves a few bytes in code space, and a few microseconds in function call overhead.
-rw-r--r--projects/board-test/fmc-perf.c4
-rw-r--r--projects/board-test/fmc-test.c5
-rw-r--r--projects/board-test/keystore-perf.c21
-rw-r--r--projects/board-test/rtc-test.c7
-rw-r--r--projects/board-test/spiflash-perf.c25
-rw-r--r--projects/bootloader/bootloader.c12
-rw-r--r--projects/bootloader/dfu.c54
-rw-r--r--projects/bootloader/log.c4
-rw-r--r--projects/cli-test/cli-test.c1
-rw-r--r--projects/cli-test/mgmt-cli.c7
-rw-r--r--projects/cli-test/mgmt-dfu.c4
-rw-r--r--projects/cli-test/mgmt-keystore.c4
-rw-r--r--projects/cli-test/mgmt-masterkey.c8
-rw-r--r--projects/cli-test/mgmt-misc.c8
-rw-r--r--projects/cli-test/mgmt-show.c4
-rw-r--r--projects/cli-test/test-fmc.c12
-rw-r--r--projects/hsm/hsm.c1
-rw-r--r--projects/hsm/log.c4
-rw-r--r--projects/hsm/mgmt-cli.c6
-rw-r--r--projects/hsm/mgmt-masterkey.c4
-rw-r--r--projects/hsm/mgmt-misc.c8
-rw-r--r--stm-uart.c105
-rw-r--r--stm-uart.h57
23 files changed, 159 insertions, 206 deletions
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 ed5891a..2002f57 100644
--- a/projects/board-test/fmc-test.c
+++ b/projects/board-test/fmc-test.c
@@ -85,7 +85,6 @@ int main(void)
int i;
stm_init();
- uart_set_default(STM_UART_MGMT);
uart_send_string("Keep calm for FPGA bitstream loading...\r\n");
@@ -142,9 +141,9 @@ 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_integer(failed_runs, 1);
uart_send_string("\r\n");
if (VERBOSE)
uart_send_string("\r\n");
diff --git a/projects/board-test/keystore-perf.c b/projects/board-test/keystore-perf.c
index 3552d30..c2aa4fb 100644
--- a/projects/board-test/keystore-perf.c
+++ b/projects/board-test/keystore-perf.c
@@ -22,7 +22,7 @@ static void test_read_data(void)
err = keystore_read_data(i * KEYSTORE_SUBSECTOR_SIZE, read_buf, KEYSTORE_SUBSECTOR_SIZE);
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;
}
@@ -42,13 +42,13 @@ static void _read_verify(uint8_t *vrfy_buf)
err = keystore_read_data(i * KEYSTORE_SUBSECTOR_SIZE, read_buf, KEYSTORE_SUBSECTOR_SIZE);
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;
}
@@ -67,7 +67,7 @@ static void test_erase_sector(void)
err = keystore_erase_sector(i);
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;
}
@@ -86,7 +86,7 @@ static void test_erase_subsector(void)
err = keystore_erase_subsector(i);
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;
}
@@ -123,9 +123,9 @@ static void test_write_data(void)
err = keystore_write_data(i * KEYSTORE_SUBSECTOR_SIZE, write_buf, KEYSTORE_SUBSECTOR_SIZE);
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,7 +177,6 @@ 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() != HAL_OK) {
uart_send_string("ERROR: keystore_check_id failed\r\n");
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 2691504..36c6131 100644
--- a/projects/board-test/spiflash-perf.c
+++ b/projects/board-test/spiflash-perf.c
@@ -35,7 +35,7 @@ static void test_read_page(void)
err = n25q128_read_page(ctx, i, read_buf);
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;
}
@@ -55,7 +55,7 @@ static void test_read_subsector(void)
err = n25q128_read_subsector(ctx, i, read_buf);
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;
}
@@ -77,13 +77,13 @@ static void _read_verify(uint8_t *vrfy_buf)
err = n25q128_read_page(ctx, i, read_buf);
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;
}
@@ -102,7 +102,7 @@ static void test_erase_sector(void)
err = n25q128_erase_sector(ctx, i);
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;
}
@@ -121,7 +121,7 @@ static void test_erase_subsector(void)
err = n25q128_erase_subsector(ctx, i);
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;
}
@@ -138,7 +138,7 @@ static void test_erase_bulk(void)
err = n25q128_erase_bulk(ctx);
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");
}
}
@@ -173,9 +173,9 @@ static void test_write_page(void)
err = n25q128_write_page(ctx, i, write_buf);
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,7 +227,6 @@ 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) != HAL_OK) {
uart_send_string("ERROR: n25q128_check_id failed\r\n");
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 6910c4d..d7f7383 100644
--- a/projects/cli-test/mgmt-cli.c
+++ b/projects/cli-test/mgmt-cli.c
@@ -104,9 +104,8 @@ 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("\r\n");
}
static ssize_t uart_cli_read(struct cli_def *cli __attribute__ ((unused)), void *buf, size_t count)
@@ -120,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 7b78d38..c7273f4 100644
--- a/projects/cli-test/mgmt-dfu.c
+++ b/projects/cli-test/mgmt-dfu.c
@@ -64,8 +64,8 @@ static int cmd_dfu_dump(struct cli_def *cli, const char *command, char *argv[],
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;
}
diff --git a/projects/cli-test/mgmt-keystore.c b/projects/cli-test/mgmt-keystore.c
index 8c16f35..6d0d38d 100644
--- a/projects/cli-test/mgmt-keystore.c
+++ b/projects/cli-test/mgmt-keystore.c
@@ -239,8 +239,8 @@ static int cmd_keystore_show_data(struct cli_def *cli, const char *command, char
}
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;
}
diff --git a/projects/cli-test/mgmt-masterkey.c b/projects/cli-test/mgmt-masterkey.c
index 0f6e352..811e15b 100644
--- a/projects/cli-test/mgmt-masterkey.c
+++ b/projects/cli-test/mgmt-masterkey.c
@@ -102,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));
@@ -111,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));
@@ -134,7 +134,7 @@ static int cmd_masterkey_set(struct cli_def *cli, const char *command, char *arg
}
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) {
@@ -175,7 +175,7 @@ static int cmd_masterkey_unsecure_set(struct cli_def *cli, const char *command,
}
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) {
diff --git a/projects/cli-test/mgmt-misc.c b/projects/cli-test/mgmt-misc.c
index 1216bd5..db8dbd2 100644
--- a/projects/cli-test/mgmt-misc.c
+++ b/projects/cli-test/mgmt-misc.c
@@ -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;
}
@@ -98,12 +98,12 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal
}
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");
diff --git a/projects/cli-test/mgmt-show.c b/projects/cli-test/mgmt-show.c
index e276b15..4338dcd 100644
--- a/projects/cli-test/mgmt-show.c
+++ b/projects/cli-test/mgmt-show.c
@@ -130,8 +130,8 @@ static int cmd_show_keystore_data(struct cli_def *cli, const char *command, char
}
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 */
diff --git a/projects/cli-test/test-fmc.c b/projects/cli-test/test-fmc.c
index a1d6eea..87f80ce 100644
--- a/projects/cli-test/test-fmc.c
+++ b/projects/cli-test/test-fmc.c
@@ -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;
}
@@ -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/hsm/hsm.c b/projects/hsm/hsm.c
index a36c64b..7fc7410 100644
--- a/projects/hsm/hsm.c
+++ b/projects/hsm/hsm.c
@@ -409,7 +409,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)
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-cli.c b/projects/hsm/mgmt-cli.c
index 23a0a40..2b5be1f 100644
--- a/projects/hsm/mgmt-cli.c
+++ b/projects/hsm/mgmt-cli.c
@@ -112,8 +112,8 @@ 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)
@@ -127,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-masterkey.c b/projects/hsm/mgmt-masterkey.c
index 8675637..765cb10 100644
--- a/projects/hsm/mgmt-masterkey.c
+++ b/projects/hsm/mgmt-masterkey.c
@@ -111,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");
}
@@ -122,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");
}
diff --git a/projects/hsm/mgmt-misc.c b/projects/hsm/mgmt-misc.c
index 65da7ff..b06003f 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;
}
@@ -91,12 +91,12 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal
}
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");
diff --git a/stm-uart.c b/stm-uart.c
index 59089fe..f64c90c 100644
--- a/stm-uart.c
+++ b/stm-uart.c
@@ -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 = 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;
diff --git a/stm-uart.h b/stm-uart.h
index 4d008c7..8ea13d8 100644
--- a/stm-uart.h
+++ b/stm-uart.h
@@ -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)