diff options
author | Paul Selkirk <paul@psgd.org> | 2015-10-26 15:18:58 -0400 |
---|---|---|
committer | Paul Selkirk <paul@psgd.org> | 2015-10-26 15:18:58 -0400 |
commit | 26f12903dab2fafeaaefb02349763618ce96d070 (patch) | |
tree | b37cceea7014770ea2ae039928e57b6f02c0aaa2 /src/stm-uart.c |
Based on user/ft/stm32-dev-bridge, without the project-specific build
directories (and duplicated code).
Diffstat (limited to 'src/stm-uart.c')
-rw-r--r-- | src/stm-uart.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/stm-uart.c b/src/stm-uart.c new file mode 100644 index 0000000..61ba910 --- /dev/null +++ b/src/stm-uart.c @@ -0,0 +1,85 @@ +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4xx_hal.h" +#include "stm-uart.h" + +#include <string.h> + +UART_HandleTypeDef huart2; + +extern void Error_Handler(); + + +/* Private variables ---------------------------------------------------------*/ + +/* Private function prototypes -----------------------------------------------*/ + +#if 0 /* XXX moved [back] to stm-init.c */ +/* USART2 init function */ +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(); + } +} +#endif + +void uart_send_binary(uint32_t num, uint8_t bits) +{ + uint32_t i; + unsigned char ch; + + bits--; /* bits 4 should give i = 1000, not 10000 */ + + i = 1 << bits; + while (i) { + ch = '0'; + if (num & i) { + ch = '1'; + } + + HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1); + i = i >> 1; + } +} + +void uart_send_string(char *s) +{ + HAL_UART_Transmit(&huart2, (uint8_t *) s, strlen(s), 0x1); +} + +void uart_send_integer(uint32_t data, uint32_t mag) { + uint32_t i, t; + unsigned char ch; + + if (! mag) { + /* Find magnitude */ + if (data < 10) { + ch = '0' + data; + HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1); + 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; + HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1); + data -= (t * i); + } +} |