Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* |
2 | * serial.h |
||
3 | * |
||
4 | * Created on: 4 Jan 2016 |
||
5 | * Author: Mike |
||
6 | */ |
||
7 | |||
10 | mjames | 8 | #pragma once |
2 | mjames | 9 | |
10 | #include "main.h" |
||
12 | mjames | 11 | |
12 | |||
10 | mjames | 13 | typedef struct |
14 | { |
||
13 | mjames | 15 | /// @brief UART handle |
10 | mjames | 16 | UART_HandleTypeDef *Handle; |
13 | mjames | 17 | /// @brief Locally maintained transmit buffer |
2 | mjames | 18 | volatile uint8_t tx_usart_buff[TX_USART_BUFF_SIZ]; |
19 | volatile unsigned int tx_usart_in_Ptr; |
||
20 | volatile unsigned int tx_usart_out_Ptr; |
||
10 | mjames | 21 | volatile unsigned int tx_usart_overruns; |
13 | mjames | 22 | /// @brief Set when the usart is transmitting |
2 | mjames | 23 | volatile uint8_t tx_usart_running; |
10 | mjames | 24 | |
13 | mjames | 25 | /// @brief Locally maintained receive buffer |
2 | mjames | 26 | volatile uint8_t rx_usart_buff[RX_USART_BUFF_SIZ]; |
27 | volatile unsigned int rx_usart_in_Ptr; |
||
28 | volatile unsigned int rx_usart_out_Ptr; |
||
13 | mjames | 29 | /// @brief Set when the receiver buffer is full |
2 | mjames | 30 | volatile uint8_t rx_usart_buffer_full; |
31 | } usart_ctl; |
||
32 | |||
10 | mjames | 33 | |
34 | |||
2 | mjames | 35 | #if defined SERIAL_UART1 |
36 | extern usart_ctl uc1; |
||
37 | #endif |
||
38 | #if defined SERIAL_UART2 |
||
39 | extern usart_ctl uc2; |
||
40 | #endif |
||
41 | #if defined SERIAL_UART3 |
||
42 | extern usart_ctl uc3; |
||
43 | #endif |
||
7 | mjames | 44 | #if defined SERIAL_UART4 |
45 | extern usart_ctl uc4; |
||
46 | #endif |
||
47 | #if defined SERIAL_UART5 |
||
48 | extern usart_ctl uc5; |
||
49 | #endif |
||
2 | mjames | 50 | |
7 | mjames | 51 | ///@brief returns the number of characters in the recieve buffer |
13 | mjames | 52 | ///@param instance Pointer to usart_ctl structure |
53 | ///@return Number of characters |
||
10 | mjames | 54 | extern uint16_t SerialCharsReceived(usart_ctl *instance); |
13 | mjames | 55 | |
10 | mjames | 56 | ///@brief Get the amount of free space in the transmit buffer. |
13 | mjames | 57 | ///@param instance Pointer to usart_ctl structure |
58 | ///@return Free space at time of checking |
||
10 | mjames | 59 | extern uint16_t SerialTransmitSpace(usart_ctl *instance); |
2 | mjames | 60 | |
12 | mjames | 61 | /// @brief Return 1 if there are any characters in the receive buffer |
62 | /// @param instance Pointer to usart_ctl structure |
||
63 | /// @return 1 if any characters |
||
10 | mjames | 64 | extern uint8_t PollSerial(usart_ctl *instance); |
12 | mjames | 65 | |
13 | mjames | 66 | /// @brief return the next character in the Serial input buffer |
67 | /// This function will wait until a character arrives. |
||
68 | /// Use PollSerial() or SerialCharsReceived() if you want |
||
69 | /// to ensure you do not block here. |
||
70 | /// @param instance Pointer to usart_ctl structure |
||
71 | /// @return next character |
||
10 | mjames | 72 | extern uint8_t GetCharSerial(usart_ctl *instance); |
12 | mjames | 73 | |
74 | /// @brief Enable the receive interrupt |
||
75 | /// @param instance |
||
10 | mjames | 76 | extern void EnableSerialRxInterrupt(usart_ctl *instance); |
7 | mjames | 77 | |
13 | mjames | 78 | /// @brief Send a character |
79 | /// @param instance Pointer to usart_ctl structure |
||
80 | /// @param c character to send |
||
81 | extern void PutCharSerial(usart_ctl *instance, uint8_t c); |
||
2 | mjames | 82 | |
13 | mjames | 83 | /// @brief Reset transmit buffer |
84 | /// @param instance Pointer to usart_ctl structure |
||
10 | mjames | 85 | extern void ResetTxBuffer(usart_ctl *instance); |
13 | mjames | 86 | |
87 | /// @brief Reset receive buffer |
||
88 | /// @param instance Pointer to usart_ctl structure |
||
10 | mjames | 89 | extern void ResetRxBuffer(usart_ctl *instance); |
13 | mjames | 90 | |
91 | /// @brief Reset both transmit and receive buffers |
||
92 | /// @param instance Pointer to usart_ctl structure |
||
10 | mjames | 93 | extern void FlushSerial(usart_ctl *instance); |
13 | mjames | 94 | |
95 | /// @brief Check if the transmitter buffer is empty |
||
96 | /// @param instance Pointer to usart_ctl structure |
||
97 | /// @return 1 if the buffer is empty |
||
10 | mjames | 98 | extern uint8_t TxBufferEmpty(usart_ctl *instance); |
13 | mjames | 99 | |
4 | mjames | 100 | ///@brief wait until the USART buffer is empty and all characters are sent |
13 | mjames | 101 | /// @param instance Pointer to usart_ctl structure |
4 | mjames | 102 | extern void TxWaitEmpty(usart_ctl *instance); |
2 | mjames | 103 | |
13 | mjames | 104 | /// @brief Establish this instance as being in control of a USART |
105 | /// @param instance Pointer to usart_ctl structure |
||
106 | /// @param usart Handle to usart maintained by HAL |
||
10 | mjames | 107 | extern void init_usart_ctl(usart_ctl *instance, |
108 | UART_HandleTypeDef *usart); |
||
2 | mjames | 109 | |
13 | mjames | 110 | /// @brief Set the baud rate on this instance |
111 | /// @param instance Pointer to usart_ctl structure |
||
112 | /// @param baud Baud rate in bits per second |
||
10 | mjames | 113 | extern void setBaud(usart_ctl *instance, uint32_t baud); |
13 | mjames | 114 | |
115 | |||
116 | /// @brief Interrupt handler generic wrapper |
||
117 | /// @param instance Pointer to usart_ctl structure |
||
118 | extern void UART_IRQHandler(usart_ctl *instance); |