diff options
author | Paul Selkirk <paul@psgd.org> | 2015-10-29 15:42:29 -0400 |
---|---|---|
committer | Paul Selkirk <paul@psgd.org> | 2015-10-29 15:42:29 -0400 |
commit | 9278e9bafd96105b64f9946eb94f5618f01649d3 (patch) | |
tree | 2aca37b1edebc9eb717456179f85927929c98cbb /src | |
parent | fb73e2d75b4e99a29c28bbbaf754222010c273b7 (diff) |
add libhal tests, some cleanup (some mess-making)
Diffstat (limited to 'src')
-rw-r--r-- | src/stm-fmc.c | 1 | ||||
-rw-r--r-- | src/stm-init.c | 21 | ||||
-rw-r--r-- | src/stm-uart.c | 53 |
3 files changed, 40 insertions, 35 deletions
diff --git a/src/stm-fmc.c b/src/stm-fmc.c index bc214d8..19b7fdc 100644 --- a/src/stm-fmc.c +++ b/src/stm-fmc.c @@ -7,6 +7,7 @@ // Headers //------------------------------------------------------------------------------ #include "stm-fmc.h" +#include "stm32f4xx_hal.h" //------------------------------------------------------------------------------ diff --git a/src/stm-init.c b/src/stm-init.c index 01a68e4..f8610cd 100644 --- a/src/stm-init.c +++ b/src/stm-init.c @@ -39,15 +39,12 @@ #include "stm-fmc.h" #include "stm-uart.h" -UART_HandleTypeDef huart2; - /* Private variables ---------------------------------------------------------*/ static GPIO_InitTypeDef GPIO_InitStruct; /* Private function prototypes -----------------------------------------------*/ static void SystemClock_Config(void); static void MX_GPIO_Init(void); -static void MX_USART2_UART_Init(void); void stm_init(void) { @@ -130,24 +127,6 @@ static void old_SystemClock_Config(void) } #endif -/* USART2 init function */ -static void MX_USART2_UART_Init(void) -{ - huart2.Instance = USART2; - huart2.Init.BaudRate = USART2_BAUD_RATE; - huart2.Init.WordLength = UART_WORDLENGTH_8B; - huart2.Init.StopBits = UART_STOPBITS_1; - huart2.Init.Parity = UART_PARITY_NONE; - huart2.Init.Mode = UART_MODE_TX_RX; - huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart2.Init.OverSampling = UART_OVERSAMPLING_16; - - if (HAL_UART_Init(&huart2) != HAL_OK) { - /* Initialization Error */ - Error_Handler(); - } -} - /** Configure pins as * Analog * Input diff --git a/src/stm-uart.c b/src/stm-uart.c index 61ba910..61f3e5b 100644 --- a/src/stm-uart.c +++ b/src/stm-uart.c @@ -4,7 +4,7 @@ #include <string.h> -UART_HandleTypeDef huart2; +static UART_HandleTypeDef huart2; extern void Error_Handler(); @@ -13,7 +13,6 @@ extern void Error_Handler(); /* Private function prototypes -----------------------------------------------*/ -#if 0 /* XXX moved [back] to stm-init.c */ /* USART2 init function */ void MX_USART2_UART_Init(void) { @@ -31,12 +30,21 @@ void MX_USART2_UART_Init(void) Error_Handler(); } } -#endif + +void uart_send_char(uint8_t ch) +{ + HAL_UART_Transmit(&huart2, &ch, 1, 0x1); +} + +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) { uint32_t i; - unsigned char ch; + uint8_t ch; bits--; /* bits 4 should give i = 1000, not 10000 */ @@ -46,26 +54,21 @@ void uart_send_binary(uint32_t num, uint8_t bits) if (num & i) { ch = '1'; } - - HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1); + uart_send_char(ch); i = i >> 1; } } -void uart_send_string(char *s) -{ - HAL_UART_Transmit(&huart2, (uint8_t *) s, strlen(s), 0x1); -} - +/* XXX this takes a mask, not a number of digits */ void uart_send_integer(uint32_t data, uint32_t mag) { uint32_t i, t; - unsigned char ch; + uint8_t ch; if (! mag) { /* Find magnitude */ if (data < 10) { ch = '0' + data; - HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1); + uart_send_char(ch); return; } @@ -79,7 +82,29 @@ void uart_send_integer(uint32_t data, uint32_t mag) { for (i = mag; i; i /= 10) { t = (data / i); ch = '0' + t; - HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1); + 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; + } +} |