[Cryptech-Commits] [sw/stm32] 01/11: rewrite and unify uart_send_<numbertype>
git at cryptech.is
git at cryptech.is
Mon Nov 16 21:43:03 UTC 2015
This is an automated email from the git hooks/post-receive script.
paul at psgd.org pushed a commit to branch master
in repository sw/stm32.
commit 0306ce3add02dbe28826f687f86c2c2fae53354c
Author: Paul Selkirk <paul at psgd.org>
Date: Mon Nov 2 18:46:16 2015 -0500
rewrite and unify uart_send_<numbertype>
---
include/stm-uart.h | 7 ++--
self-test/fmc-perf.c | 2 +-
self-test/fmc-test.c | 4 +--
src/stm-uart.c | 96 +++++++++++++++++-----------------------------------
4 files changed, 38 insertions(+), 71 deletions(-)
diff --git a/include/stm-uart.h b/include/stm-uart.h
index 62b2efd..c57afd5 100644
--- a/include/stm-uart.h
+++ b/include/stm-uart.h
@@ -9,8 +9,9 @@ extern void MX_USART2_UART_Init(void);
extern void uart_send_char(uint8_t ch);
extern void uart_send_string(char *s);
-extern void uart_send_binary(uint32_t num, uint8_t bits);
-extern void uart_send_integer(uint32_t data, uint32_t mag);
-extern void uart_send_hex(uint32_t num, uint8_t digits);
+extern void uart_send_number(uint32_t num, uint8_t digits, uint8_t radix);
+#define uart_send_binary(num, bits) uart_send_number(num, bits, 2)
+#define uart_send_integer(num, digits) uart_send_number(num, digits, 10)
+#define uart_send_hex(num, digits) uart_send_number(num, digits, 16)
#endif /* __STM32_DEV_BRIDGE_UART_H */
diff --git a/self-test/fmc-perf.c b/self-test/fmc-perf.c
index 9dd623e..cf6fe19 100644
--- a/self-test/fmc-perf.c
+++ b/self-test/fmc-perf.c
@@ -58,7 +58,7 @@ static void _time_check(char *label, const uint32_t t0)
uart_send_string(label);
uart_send_integer(t / 1000, 0);
uart_send_char('.');
- uart_send_integer(t % 1000, 100);
+ uart_send_integer(t % 1000, 3);
uart_send_string(" seconds, ");
uart_send_integer(((1000 * TEST_NUM_ROUNDS) / t), 0);
uart_send_string("/sec\r\n");
diff --git a/self-test/fmc-test.c b/self-test/fmc-test.c
index 0e8ff39..cf17087 100644
--- a/self-test/fmc-test.c
+++ b/self-test/fmc-test.c
@@ -91,9 +91,9 @@ int main(void)
addr_test_ok = test_fpga_address_bus();
uart_send_string("Data: ");
- uart_send_integer(data_test_ok, 100000);
+ uart_send_integer(data_test_ok, 6);
uart_send_string(", addr: ");
- uart_send_integer(addr_test_ok, 100000);
+ uart_send_integer(addr_test_ok, 6);
uart_send_string("\r\n");
if (data_test_ok == TEST_NUM_ROUNDS &&
diff --git a/src/stm-uart.c b/src/stm-uart.c
index 61f3e5b..7676645 100644
--- a/src/stm-uart.c
+++ b/src/stm-uart.c
@@ -41,70 +41,36 @@ void uart_send_string(char *s)
HAL_UART_Transmit(&huart2, (uint8_t *) s, strlen(s), 0x1);
}
-void uart_send_binary(uint32_t num, uint8_t bits)
+/* Generalized routine to send binary, decimal, and hex integers.
+ * This code is adapted from Chris Giese's printf.c
+ */
+void uart_send_number(uint32_t num, uint8_t digits, uint8_t radix)
{
- uint32_t i;
- uint8_t ch;
-
- bits--; /* bits 4 should give i = 1000, not 10000 */
-
- i = 1 << bits;
- while (i) {
- ch = '0';
- if (num & i) {
- ch = '1';
- }
- uart_send_char(ch);
- i = i >> 1;
- }
-}
-
-/* XXX this takes a mask, not a number of digits */
-void uart_send_integer(uint32_t data, uint32_t mag) {
- uint32_t i, t;
- uint8_t ch;
-
- if (! mag) {
- /* Find magnitude */
- if (data < 10) {
- ch = '0' + data;
- uart_send_char(ch);
- return;
- }
-
- for (mag = 10; mag < data; mag = i) {
- i = mag * 10;
- if (i > data || i < mag)
- break;
- }
- }
- /* mag is now 10 if data is 45, and 1000 if data is 1009 */
- for (i = mag; i; i /= 10) {
- t = (data / i);
- ch = '0' + t;
- uart_send_char(ch);
- data -= (t * i);
- }
-}
-
-void uart_send_hex(uint32_t num, uint8_t digits)
-{
- uint8_t ch;
- uint32_t mask;
-
- mask = 0x0FL << ((digits - 1) * 4);
-
- if (digits == 0 || digits > 8)
- digits = 8;
-
- while (digits) {
- ch = (num & mask) >> ((digits - 1) * 4);
- if (ch < 10)
- ch += '0';
- else
- ch += 'A' - 10;
- uart_send_char(ch);
- --digits;
- mask >>= 4;
- }
+ #define BUFSIZE 32
+ char buf[BUFSIZE];
+ char *where = buf + BUFSIZE;
+
+ /* initialize buf so we can add leading 0 by adjusting the pointer */
+ memset(buf, '0', BUFSIZE);
+
+ /* build the string backwards, starting with the least significant digit */
+ do {
+ uint32_t temp;
+ temp = num % radix;
+ where--;
+ if (temp < 10)
+ *where = temp + '0';
+ else
+ *where = temp - 10 + 'A';
+ num = num / radix;
+ } while (num != 0);
+
+ if (where > buf + BUFSIZE - digits)
+ /* pad with leading 0 */
+ where = buf + BUFSIZE - digits;
+ else
+ /* number is larger than the specified number of digits */
+ digits = buf + BUFSIZE - where;
+
+ HAL_UART_Transmit(&huart2, (uint8_t *) where, digits, 0x1);
}
More information about the Commits
mailing list