ESOS32
ESOSOn32-bitProcessors
esos_stm32l4_rs232.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
28 
35 // Documentation for this file. If the \file tag isn't present,
36 // this file won't be documented.
42 /*** I N C L U D E S *************************************************/
43 #include "esos_stm32l4_rs232.h"
44 
45 /*** G L O B A L S *************************************************/
46 uint8_t u8_uartRXbuf;
47 uint8_t u8_uartTXbuf;
48 UART_HandleTypeDef st_huart2;
49 
50 /*** T H E C O D E *************************************************/
51 // probably need to define a flag for this so ISRs can start/stop
52 // transfers
53 #define __ESOS_HW_SIGNAL_START_TX() __esos_SetSystemFlag(__ESOS_SYS_COMM_TX_ONGOING)
54 #define __ESOS_HW_SIGNAL_STOP_TX() __esos_ClearSystemFlag(__ESOS_SYS_COMM_TX_ONGOING)
55 
56 /*********************************************************
57  * Public functions intended to be called by other files *
58  *********************************************************/
59 inline void __esos_hw_signal_start_tx(void) {
60  if __esos_IsSystemFlagClear(__ESOS_SYS_COMM_TX_ONGOING) {
61  // no TX is currently ongoing, so kick it off.
62  __esos_SetSystemFlag(__ESOS_SYS_COMM_TX_ONGOING);
63  // HAL_UART_Transmit_IT requires a pointer to the data, so
64  // have CB return the data to a local variable and pass that
65  // address to the HAL_UART_Transmit_IT. The ESOS CB don't
66  // have a routine that returns a pointer to data, and I
67  // don't trust the HAL enough to pass an address into my CB.
68  u8_uartTXbuf = __esos_CB_ReadUINT8( __pst_CB_Tx );
69  HAL_UART_Transmit_IT(&st_huart2, &u8_uartTXbuf, 1);
70  //HAL_UART_Transmit_IT(&st_huart2, &__st_TxBuffer.pau8_Data[__st_TxBuffer.u16_Tail], 1);
71  }
72 }
73 
74 inline void __esos_hw_signal_stop_tx(void) {
75  __esos_ClearSystemFlag(__ESOS_SYS_COMM_TX_ONGOING);
76 }
77 
93 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) {
94  /* If this function is called, we have received a
95  * byte into the UART. The HAL UART handler has
96  * called us, and the received data is in u8_uartRXbuf.
97  * Do something with it (i.e. copy it over into our
98  * ciruclar buffers) and initiate a new transfer.
99  */
100 
101  //__st_RxBuffer.u16_Head++; //increment head pointer
102  //if (__st_RxBuffer.u16_Head == ESOS_SERIAL_OUT_EP_SIZE)
103  //__st_RxBuffer.u16_Head = 0; //wrap if needed
105  //__st_RxBuffer.pau8_Data[__st_RxBuffer.u16_Head] = u8_uartRXbuf;
106 
107  // call CB overwrite.... we can't block here in the ISR so
108  // if data comes in too fast, we will just overwrite existing
109  // data in the CBs
110  __esos_CB_OverwriteUINT8( __pst_CB_Rx, u8_uartRXbuf );
111  // request HAL UART handler to get ONE more byte
112  HAL_UART_Receive_IT(UartHandle, &u8_uartRXbuf, 1);
113 }
114 
115 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle) {
116  if (__ESOS_CB_IS_EMPTY( __pst_CB_Tx)) {
117  //empty TX buffer, disable the interrupt, do not clear the flag
118  __esos_hw_signal_stop_tx();
119  } else {
120  // HAL_UART_Transmit_IT requires a pointer to the data, so
121  // have CB return the data to a local variable and pass that
122  // address to the HAL_UART_Transmit_IT. The ESOS CB don't
123  // have a routine that returns a pointer to data, and I
124  // don't trust the HAL enough to pass an address into my CB.
125  //
126  // TODO: Once this gets working... change to have the HAL
127  // transmit all remining data in the circular buffer
128  // This will improve performance a little bit.
129  u8_uartTXbuf = __esos_CB_ReadUINT8( __pst_CB_Tx );
130  HAL_UART_Transmit_IT(&st_huart2, &u8_uartTXbuf, 1);
131  }
132 }
133 
134 void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) {
135  if(UartHandle->ErrorCode == HAL_UART_ERROR_ORE)
136  HAL_UART_Receive_IT(UartHandle, &u8_uartRXbuf, 1);
137 }
138 
145 void __esos_configUART1(uint32_t u32_baudRate) {
146  /************************* UART config ********************/
147 
148 
149 #if (HARDWARE_PLATFORM == NUCLEO64)
150 # warning Building configUART1() for Nucleo 64 development board
151  // we are using USART2. On the STM32L452RE chip, this means
152  // that TX is PA2, and RX is PA3.
153  st_huart2.Instance = USART2;
154  st_huart2.Init.BaudRate = u32_baudRate;
155  st_huart2.Init.WordLength = UART_WORDLENGTH_8B;
156  st_huart2.Init.StopBits = UART_STOPBITS_1;
157  st_huart2.Init.Parity = UART_PARITY_NONE;
158  st_huart2.Init.Mode = UART_MODE_TX_RX;
159  st_huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
160  st_huart2.Init.OverSampling = UART_OVERSAMPLING_16;
161  st_huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
162  st_huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
163  if (HAL_UART_Init(&st_huart2) != HAL_OK) {
164  Error_Handler();
165  }
166 #else
167 #error Can not configUART1() since target hardware has not been defined.
168 #endif
169 
170  // Now that UART is configured, let's enable the USART2 interrupts
171  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
172  HAL_NVIC_EnableIRQ(USART2_IRQn);
173 
174  // Kick off RX on UART via call to HAL. Upon receipt of data
175  // the call back should kick off another one. In the current
176  // specificaiton of ESOS, we always are "receiving", so RX is
177  // never disabled.
178  HAL_UART_Receive_IT(&st_huart2, &u8_uartRXbuf, 1);
179 
180 }
181 
182 /* ########################################################################### */
183 
184 
185 /******************************************************************************
186  * Function: void _esos_hw_InitSerialUart( void )
187  *
188  * PreCondition: None
189  *
190  * Input: None
191  *
192  * Output: ptr to ESOS_COMM_BUFF_DSC structure with initialized ptrs
193  *
194  * Side Effects: Turns on USART hardware
195  *
196  *****************************************************************************/
197 void __esos_hw_InitCommSystem(void) {
198  // use the HAL-inspired code to init the RS232 comm subsystem
199  // 8N1 @ 56k7 baud (DEFAULT_BAUDRATE) for now
200  __esos_configUART1(DEFAULT_BAUDRATE) ;
201 
202 } // end __esos_hw_InitCommSystem()
203 
204 
205 /******************************************************************************
206  * Function: uint8_t esos_GetCommSystemMaxInDataLen(void)
207  *
208  * PreCondition: None.
209  *
210  * Input: None
211  *
212  * Output: the maximum number of uint8_ts that the comm system will
213  * receive in a single buffer transfer from the host -- OR --
214  * in the case of single uint8_t xfers (like RS232), the maximum
215  * number of uint8_ts that can be RX-ed before the buffers
216  * overflow
217  *
218  * Side Effects: None
219  *
220  * Overview: A way for a run-time determination of the maximum buffer
221  * size that the user can can expect. This number is
222  * actually hard-coded in the USB CDC header file, but this
223  * method will allow the user code to be more generic, if
224  * it chooses to be.
225  *
226  *****************************************************************************/
227 uint8_t esos_GetCommSystemMaxInDataLen(void) {
228  return ESOS_SERIAL_OUT_EP_SIZE;
229 } //end esos_GetCommSystemMaxInDataLen()
230 
231 /******************************************************************************
232  * Function: uint8_t esos_GetCommSystemMaxOutDataLen(void)
233  *
234  * PreCondition: None.
235  *
236  * Input: None
237  *
238  * Output: the maximum number of uint8_ts that the comm system will
239  * transfer back to the host in a single buffer -- OR --
240  * in the case of singe uint8_t xfers (like RS232), the maximum
241  * number of uint8_ts in the output buffer before overflow
242  *
243  * Side Effects: None
244  *
245  * Overview: A way for a run-time determination of the maximum buffer
246  * size that the user can can send efficiently. The USB system
247  * will send a bigger buffer than getUSBCdcTxMax() size, but
248  * will do so in several smaller getUSBCdcTxMax()-sized chunks.
249  *
250  * This number is actually hard-coded in the USB CDC header file,
251  * but this method will allow the user code to be more generic,
252  * if it chooses to be.
253  *
254  *****************************************************************************/
255 uint8_t esos_GetCommSystemMaxOutDataLen(void) {
256  return ESOS_SERIAL_IN_EP_SIZE;
257 } //end esos_GetCommSystemMaxOutDataLen()
258 
259 /******************************************************************************
260  * Function: uint8_t _esos_hw_GetUartVersion(void)
261  *
262  * PreCondition: None.
263  *
264  * Input: None
265  *
266  * Output: Return the version number of the MSU Bulk CDC driver firmware
267  * currently running.
268  * The most-significant bit denotes we're running USB
269  * The most-significant nibble is the major revision number
270  * The least-significant nibble is the minor revision number
271  *
272  * Side Effects: None
273  *
274  *****************************************************************************/
275 uint8_t _esos_hw_GetSerialUartVersion(void) {
276  return ESOS_COMM_SYS_SERIAL_REV;
277 } //end _esos_hw_GetUartVersion()
278 
HAL_UART_RxCpltCallback
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
Definition: esos_stm32l4_rs232.c:93
esos_GetCommSystemMaxOutDataLen
uint8_t esos_GetCommSystemMaxOutDataLen(void)
Definition: esos_hwxxxx_rs232.c:222
__esos_configUART1
void __esos_configUART1(uint32_t u32_baudRate)
Definition: esos_stm32l4_rs232.c:145
Error_Handler
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition: esos_stm32l4_tick.c:170
esos_GetCommSystemMaxInDataLen
uint8_t esos_GetCommSystemMaxInDataLen(void)
Definition: esos_hwxxxx_rs232.c:194