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 
64  // HAL_UART_Transmit_IT requires a pointer to the data, so
65  // have CB return the data to a local variable and pass that
66  // address to the HAL_UART_Transmit_IT. The ESOS CB don't
67  // have a routine that returns a pointer to data, and I
68  // don't trust the HAL enough to pass an address into my CB.
69  u8_uartTXbuf = __esos_CB_ReadUINT8( __pst_CB_Tx );
70 
71  usart_send(USART_CONSOLE, u8_uartTXbuf);
72  usart_enable_tx_interrupt(USART_CONSOLE);
73  //HAL_UART_Transmit_IT(&st_huart2, &u8_uartTXbuf, 1);
74  }
75 }
76 
77 inline void __esos_hw_signal_stop_tx(void) {
78  __esos_ClearSystemFlag(__ESOS_SYS_COMM_TX_ONGOING);
79 }
80 
95 void usart2_isr(void)
96 {
97  /* If this function is called, one of the UART interrupt flags
98  * has been fired. We will need to determine which one it is
99  * and handle it appropriately.
100  */
101  if(((USART_CR1(USART_CONSOLE) & USART_CR1_RXNEIE) != 0) &&
102  ((USART_ISR(USART_CONSOLE) & USART_ISR_RXNE) != 0 ))
103  {
104  /* If the code reaches here, we have received a
105  * byte into the UART.
106  */
107 
108  // call CB overwrite.... we can't block here in the ISR so
109  // if data comes in too fast, we will just overwrite existing
110  // data in the CBs
111  u8_uartRXbuf = usart_recv(USART_CONSOLE);
112  __esos_CB_OverwriteUINT8( __pst_CB_Rx, u8_uartRXbuf );
113  // request HAL UART handler to get ONE more byte
114  //enable the error condition flags (overrun, noise, framing)
115  //usart_enable_error_interrupt(USART_CONSOLE);
116  //usart_enable_rx_interrupt(USART_CONSOLE);
117  }
118  else if(((USART_CR1(USART_CONSOLE) & USART_CR1_TXEIE) != 0) &&
119  ((USART_ISR(USART_CONSOLE) & USART_ISR_TXE) != 0 ))
120  {
121  /* If the code reaches here, the HW transmit buffer is empty
122  */
123  if (__ESOS_CB_IS_EMPTY( __pst_CB_Tx)) {
124  //empty TX buffer, disable the interrupt, do not clear the flag
125  usart_disable_tx_interrupt(USART_CONSOLE);
126  __esos_hw_signal_stop_tx();
127  }
128  else
129  {
130  // HAL_UART_Transmit_IT requires a pointer to the data, so
131  // have CB return the data to a local variable and pass that
132  // address to the HAL_UART_Transmit_IT. The ESOS CB don't
133  // have a routine that returns a pointer to data, and I
134  // don't trust the HAL enough to pass an address into my CB.
135  u8_uartTXbuf = __esos_CB_ReadUINT8( __pst_CB_Tx );
136  usart_send(USART_CONSOLE, u8_uartTXbuf);
137  usart_enable_tx_interrupt(USART_CONSOLE);
138  }
139  }
140  //else if(((USART_CR1(USART_CONSOLE) & USART_CR3_EIE) != 0) &&
141  // ((USART_ISR(USART_CONSOLE) & USART_ISR_ORE) != 0 ))
142  //{
143  // // Error handler for the UART
144  //usart_enable_error_interrupt(USART_CONSOLE);
145  // u8_uartRXbuf = usart_recv(USART_CONSOLE);
146  //}*/
147 }
148 
155 void __esos_configUART1(uint32_t u32_baudRate) {
156  /************************* UART config ********************/
157  nvic_enable_irq(NVIC_USART2_IRQ);
158 
159 
160 #if (HARDWARE_PLATFORM == NUCLEO64)
161 # warning Building configUART1() for Nucleo 64 development board
162  rcc_periph_clock_enable(RCC_USART2);
163 
164  // we are using USART2. On the STM32L452RE chip, this means
165  // that TX is PA2, and RX is PA3.
166  gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
167  gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3);
168  gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3);
169 
170  /* Setup USART2 TX pin as alternate function. */
171  gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
172  gpio_set_af(GPIOA, GPIO_AF7, GPIO3);
173 
174  usart_set_baudrate(USART_CONSOLE, u32_baudRate);
175  usart_set_databits(USART_CONSOLE, 8);
176  usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1);
177  usart_set_mode(USART_CONSOLE, USART_MODE_TX_RX);
178  usart_set_parity(USART_CONSOLE, USART_PARITY_NONE);
179  usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE);
180 
181  // This interrupt will happen if the transmit buffer is empty
182  usart_disable_tx_interrupt(USART_CONSOLE);
183  //usart_enable_error_interrupt(USART_CONSOLE);
184  usart_enable_rx_interrupt(USART_CONSOLE);
185  // usart_disable_rx_interrupt(USART_CONSOLE);
186 
187  /* Finally enable the USART. */
188  usart_enable(USART_CONSOLE);
189 #else
190 #error Can not configUART1() since target hardware has not been defined.
191 #endif
192 
193  // Now that UART is configured, let's enable the USART2 interrupts
194 
195 
196  // Kick off RX on UART via call to HAL. Upon receipt of data
197  // the call back should kick off another one. In the current
198  // specificaiton of ESOS, we always are "receiving", so RX is
199  // never disabled.
200  //u8_uartRXbuf = usart_recv(USART_CONSOLE);
201 }
202 
203 /* ########################################################################### */
204 
205 
206 /******************************************************************************
207  * Function: void _esos_hw_InitSerialUart( void )
208  *
209  * PreCondition: None
210  *
211  * Input: None
212  *
213  * Output: ptr to ESOS_COMM_BUFF_DSC structure with initialized ptrs
214  *
215  * Side Effects: Turns on USART hardware
216  *
217  *****************************************************************************/
218 void __esos_hw_InitCommSystem(void) {
219  // use the HAL-inspired code to init the RS232 comm subsystem
220  // 8N1 @ 56k7 baud (DEFAULT_BAUDRATE) for now
221  __esos_configUART1(DEFAULT_BAUDRATE) ;
222 
223 } // end __esos_hw_InitCommSystem()
224 
225 
226 /******************************************************************************
227  * Function: uint8_t esos_GetCommSystemMaxInDataLen(void)
228  *
229  * PreCondition: None.
230  *
231  * Input: None
232  *
233  * Output: the maximum number of uint8_ts that the comm system will
234  * receive in a single buffer transfer from the host -- OR --
235  * in the case of single uint8_t xfers (like RS232), the maximum
236  * number of uint8_ts that can be RX-ed before the buffers
237  * overflow
238  *
239  * Side Effects: None
240  *
241  * Overview: A way for a run-time determination of the maximum buffer
242  * size that the user can can expect. This number is
243  * actually hard-coded in the USB CDC header file, but this
244  * method will allow the user code to be more generic, if
245  * it chooses to be.
246  *
247  *****************************************************************************/
248 uint8_t esos_GetCommSystemMaxInDataLen(void) {
249  return ESOS_SERIAL_OUT_EP_SIZE;
250 } //end esos_GetCommSystemMaxInDataLen()
251 
252 /******************************************************************************
253  * Function: uint8_t esos_GetCommSystemMaxOutDataLen(void)
254  *
255  * PreCondition: None.
256  *
257  * Input: None
258  *
259  * Output: the maximum number of uint8_ts that the comm system will
260  * transfer back to the host in a single buffer -- OR --
261  * in the case of singe uint8_t xfers (like RS232), the maximum
262  * number of uint8_ts in the output buffer before overflow
263  *
264  * Side Effects: None
265  *
266  * Overview: A way for a run-time determination of the maximum buffer
267  * size that the user can can send efficiently. The USB system
268  * will send a bigger buffer than getUSBCdcTxMax() size, but
269  * will do so in several smaller getUSBCdcTxMax()-sized chunks.
270  *
271  * This number is actually hard-coded in the USB CDC header file,
272  * but this method will allow the user code to be more generic,
273  * if it chooses to be.
274  *
275  *****************************************************************************/
276 uint8_t esos_GetCommSystemMaxOutDataLen(void) {
277  return ESOS_SERIAL_IN_EP_SIZE;
278 } //end esos_GetCommSystemMaxOutDataLen()
279 
280 /******************************************************************************
281  * Function: uint8_t _esos_hw_GetUartVersion(void)
282  *
283  * PreCondition: None.
284  *
285  * Input: None
286  *
287  * Output: Return the version number of the MSU Bulk CDC driver firmware
288  * currently running.
289  * The most-significant bit denotes we're running USB
290  * The most-significant nibble is the major revision number
291  * The least-significant nibble is the minor revision number
292  *
293  * Side Effects: None
294  *
295  *****************************************************************************/
296 uint8_t _esos_hw_GetSerialUartVersion(void) {
297  return ESOS_COMM_SYS_SERIAL_REV;
298 } //end _esos_hw_GetUartVersion()
299 
usart2_isr
void usart2_isr(void)
Definition: esos_stm32l4_rs232.c:95
esos_GetCommSystemMaxOutDataLen
uint8_t esos_GetCommSystemMaxOutDataLen(void)
Definition: esos_hwxxxx_rs232.c:222
USART_CONSOLE
#define USART_CONSOLE
Definition: esos_stm32l4_rs232.h:80
__esos_configUART1
void __esos_configUART1(uint32_t u32_baudRate)
Definition: esos_stm32l4_rs232.c:145
esos_GetCommSystemMaxInDataLen
uint8_t esos_GetCommSystemMaxInDataLen(void)
Definition: esos_hwxxxx_rs232.c:194