| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,201 @@ |
| 1 |
+/* |
|
| 2 |
+ * Author: Pawel Jablonski |
|
| 3 |
+ * E-mail: pj@xirx.net |
|
| 4 |
+ * WWW: xirx.net |
|
| 5 |
+ * GIT: git.xirx.net |
|
| 6 |
+ * |
|
| 7 |
+ * License: You can use this code however you like |
|
| 8 |
+ * but leave information about the original author. |
|
| 9 |
+ * Code is free for non-commercial and commercial use. |
|
| 10 |
+ */ |
|
| 11 |
+ |
|
| 12 |
+#include <stm32f0xx.h> |
|
| 13 |
+#include <stm32f0xx_hal.h> |
|
| 14 |
+#include <stm32f0xx_hal_gpio.h> |
|
| 15 |
+#include <stm32f0xx_hal_rcc.h> |
|
| 16 |
+#include <stm32f0xx_hal_uart.h> |
|
| 17 |
+#include <stm32f0xx_hal_usart.h> |
|
| 18 |
+#include <system_stm32f0xx.h> |
|
| 19 |
+ |
|
| 20 |
+#include "type.h" |
|
| 21 |
+ |
|
| 22 |
+#include "rs232.h" |
|
| 23 |
+ |
|
| 24 |
+#define RS232_BUFFER_RX_SIZE 64 //!< Receive buffer size |
|
| 25 |
+#define RS232_BUFFER_TX_SIZE 64 //!< Transmit buffer size |
|
| 26 |
+ |
|
| 27 |
+static unsigned char bufferRx[RS232_BUFFER_RX_SIZE]; //!< Receive circular buffer |
|
| 28 |
+static volatile unsigned int bufferRxStart = 0; //!< Receive data start position |
|
| 29 |
+static volatile unsigned int bufferRxEnd = 0; //!< Receive data end position |
|
| 30 |
+ |
|
| 31 |
+static unsigned char bufferTx[RS232_BUFFER_TX_SIZE]; //!< Transmit circular buffer |
|
| 32 |
+static volatile unsigned int bufferTxStart = 0; //!< Transmit data start position |
|
| 33 |
+static volatile unsigned int bufferTxEnd = 0; //!< Transmit data end position |
|
| 34 |
+ |
|
| 35 |
+static UART_HandleTypeDef uartHandle; //!< UART HAL handle |
|
| 36 |
+ |
|
| 37 |
+//! Init rs232 and set up UART |
|
| 38 |
+void rs232Init(void) |
|
| 39 |
+{
|
|
| 40 |
+ __HAL_RCC_GPIOA_CLK_ENABLE(); |
|
| 41 |
+ |
|
| 42 |
+ GPIO_InitTypeDef gpioInit = {0};
|
|
| 43 |
+ |
|
| 44 |
+ gpioInit.Pin = (GPIO_PIN_9 | GPIO_PIN_10); |
|
| 45 |
+ gpioInit.Mode = GPIO_MODE_AF_PP; |
|
| 46 |
+ gpioInit.Pull = GPIO_PULLUP; |
|
| 47 |
+ gpioInit.Speed = GPIO_SPEED_FREQ_HIGH; |
|
| 48 |
+ gpioInit.Alternate = GPIO_AF1_USART1; |
|
| 49 |
+ |
|
| 50 |
+ HAL_GPIO_Init(GPIOA, &gpioInit); |
|
| 51 |
+ |
|
| 52 |
+ __HAL_RCC_USART1_CLK_ENABLE(); |
|
| 53 |
+ |
|
| 54 |
+ uartHandle.Instance = USART1; |
|
| 55 |
+ uartHandle.Init.BaudRate = 1200; |
|
| 56 |
+ uartHandle.Init.WordLength = UART_WORDLENGTH_8B; |
|
| 57 |
+ uartHandle.Init.StopBits = UART_STOPBITS_1; |
|
| 58 |
+ uartHandle.Init.Parity = UART_PARITY_NONE; |
|
| 59 |
+ uartHandle.Init.Mode = UART_MODE_TX_RX; |
|
| 60 |
+ uartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
|
| 61 |
+ uartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; |
|
| 62 |
+ uartHandle.Init.OverSampling = UART_OVERSAMPLING_16; |
|
| 63 |
+ |
|
| 64 |
+ HAL_UART_Init(&uartHandle); |
|
| 65 |
+ __HAL_UART_ENABLE(&uartHandle); |
|
| 66 |
+ |
|
| 67 |
+ HAL_NVIC_SetPriority(USART1_IRQn, 3, 0); |
|
| 68 |
+ HAL_NVIC_EnableIRQ(USART1_IRQn); |
|
| 69 |
+ |
|
| 70 |
+ HAL_UART_Receive_IT(&uartHandle, bufferRx, 1); |
|
| 71 |
+} |
|
| 72 |
+ |
|
| 73 |
+/** |
|
| 74 |
+ * Send char |
|
| 75 |
+ * |
|
| 76 |
+ * @param byte Char to send |
|
| 77 |
+ */ |
|
| 78 |
+void rs232Send(unsigned char byte) |
|
| 79 |
+{
|
|
| 80 |
+ __disable_irq(); |
|
| 81 |
+ {
|
|
| 82 |
+ if(bufferTxStart == bufferTxEnd) |
|
| 83 |
+ {
|
|
| 84 |
+ bufferTx[bufferTxEnd] = byte; |
|
| 85 |
+ bufferTxEnd = ((bufferTxEnd + 1) % RS232_BUFFER_TX_SIZE); |
|
| 86 |
+ |
|
| 87 |
+ HAL_UART_Transmit_IT(&uartHandle, (bufferTx + bufferTxStart), 1); |
|
| 88 |
+ } |
|
| 89 |
+ else |
|
| 90 |
+ {
|
|
| 91 |
+ if(bufferTxStart == ((bufferTxEnd + 1) % RS232_BUFFER_TX_SIZE)) |
|
| 92 |
+ {
|
|
| 93 |
+ __enable_irq(); |
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ bufferTx[bufferTxEnd] = byte; |
|
| 97 |
+ bufferTxEnd = ((bufferTxEnd + 1) % RS232_BUFFER_TX_SIZE); |
|
| 98 |
+ } |
|
| 99 |
+ } |
|
| 100 |
+ __enable_irq(); |
|
| 101 |
+} |
|
| 102 |
+ |
|
| 103 |
+/** |
|
| 104 |
+ * Read a next char from receive buffer |
|
| 105 |
+ * |
|
| 106 |
+ * @return Next char from the receive buffer |
|
| 107 |
+ */ |
|
| 108 |
+unsigned char rs232Receive(void) |
|
| 109 |
+{
|
|
| 110 |
+ __disable_irq(); |
|
| 111 |
+ {
|
|
| 112 |
+ if(bufferRxStart == bufferRxEnd) |
|
| 113 |
+ {
|
|
| 114 |
+ __enable_irq(); |
|
| 115 |
+ return(0); |
|
| 116 |
+ } |
|
| 117 |
+ else |
|
| 118 |
+ {
|
|
| 119 |
+ unsigned char byte = bufferRx[bufferRxStart]; |
|
| 120 |
+ |
|
| 121 |
+ bufferRxStart = ((bufferRxStart + 1) % RS232_BUFFER_RX_SIZE); |
|
| 122 |
+ |
|
| 123 |
+ __enable_irq(); |
|
| 124 |
+ return(byte); |
|
| 125 |
+ } |
|
| 126 |
+ } |
|
| 127 |
+} |
|
| 128 |
+ |
|
| 129 |
+/** |
|
| 130 |
+ * Check if is a new data in the buffer |
|
| 131 |
+ * |
|
| 132 |
+ * @return Status of waiting data in receive buffer |
|
| 133 |
+ */ |
|
| 134 |
+bool rs232RxIsData(void) |
|
| 135 |
+{
|
|
| 136 |
+ __disable_irq(); |
|
| 137 |
+ if(bufferRxStart == bufferRxEnd) |
|
| 138 |
+ {
|
|
| 139 |
+ __enable_irq(); |
|
| 140 |
+ return(FALSE); |
|
| 141 |
+ } |
|
| 142 |
+ else |
|
| 143 |
+ {
|
|
| 144 |
+ __enable_irq(); |
|
| 145 |
+ return(TRUE); |
|
| 146 |
+ } |
|
| 147 |
+} |
|
| 148 |
+ |
|
| 149 |
+//! Take care of the UART IRQ event |
|
| 150 |
+void USART1_IRQHandler(void) |
|
| 151 |
+{
|
|
| 152 |
+ HAL_UART_IRQHandler(&uartHandle); |
|
| 153 |
+} |
|
| 154 |
+ |
|
| 155 |
+/** |
|
| 156 |
+ * Callback of the receive UART HAL event. Read a next char from the UART. |
|
| 157 |
+ * |
|
| 158 |
+ * @param handler UART HAL handle |
|
| 159 |
+ */ |
|
| 160 |
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handler) |
|
| 161 |
+{
|
|
| 162 |
+ if(handler->Instance == USART1) |
|
| 163 |
+ {
|
|
| 164 |
+ __disable_irq(); |
|
| 165 |
+ {
|
|
| 166 |
+ if(bufferRxStart == ((bufferRxEnd + 1) % RS232_BUFFER_RX_SIZE)) |
|
| 167 |
+ {
|
|
| 168 |
+ bufferRxStart = ((bufferRxStart + 1) % RS232_BUFFER_RX_SIZE); |
|
| 169 |
+ } |
|
| 170 |
+ |
|
| 171 |
+ bufferRxEnd = ((bufferRxEnd + 1) % RS232_BUFFER_RX_SIZE); |
|
| 172 |
+ HAL_UART_Receive_IT(&uartHandle, (bufferRx + bufferRxEnd), 1); |
|
| 173 |
+ } |
|
| 174 |
+ __enable_irq(); |
|
| 175 |
+ } |
|
| 176 |
+} |
|
| 177 |
+ |
|
| 178 |
+/** |
|
| 179 |
+ * Callback of the of the transmit UART HAL event. Write a next char to the UART. |
|
| 180 |
+ * |
|
| 181 |
+ * @param handler UART HAL handle |
|
| 182 |
+ */ |
|
| 183 |
+void HAL_UART_TxCpltCallback(UART_HandleTypeDef *handler) |
|
| 184 |
+{
|
|
| 185 |
+ if(handler->Instance == USART1) |
|
| 186 |
+ {
|
|
| 187 |
+ __disable_irq(); |
|
| 188 |
+ {
|
|
| 189 |
+ bufferTxStart = ((bufferTxStart + 1) % RS232_BUFFER_TX_SIZE); |
|
| 190 |
+ |
|
| 191 |
+ if(bufferTxStart == bufferTxEnd) |
|
| 192 |
+ {
|
|
| 193 |
+ __enable_irq(); |
|
| 194 |
+ return; |
|
| 195 |
+ } |
|
| 196 |
+ |
|
| 197 |
+ HAL_UART_Transmit_IT(&uartHandle, (bufferTx + bufferTxStart), 1); |
|
| 198 |
+ } |
|
| 199 |
+ __enable_irq(); |
|
| 200 |
+ } |
|
| 201 |
+} |