MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/hal/stm32/serial_stm32.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file    serial_stm32.hpp
00034  *
00035  * \author  MAV'RIC Team
00036  *
00037  * \brief   Implementation of serial peripheral for STM32
00038  *
00039  ******************************************************************************/
00040 
00041 #ifndef SERIAL_STM32_HPP_
00042 #define SERIAL_STM32_HPP_
00043 
00044 #include <libopencm3/stm32/usart.h>
00045 
00046 #include "hal/common/serial.hpp"
00047 #include "hal/stm32/gpio_stm32.hpp"
00048 #include "util/buffer.hpp"
00049 
00050 
00054 typedef enum
00055 {
00056     SERIAL_STM32_1          = USART1,
00057     SERIAL_STM32_2          = USART2,
00058     SERIAL_STM32_3          = USART3,
00059     SERIAL_STM32_4          = UART4,
00060     SERIAL_STM32_5          = UART5,
00061     SERIAL_STM32_6          = USART6,
00062     SERIAL_STM32_7          = UART7,
00063     SERIAL_STM32_8          = UART8,
00064     SERIAL_STM32_MAX_NUMBER = 8
00065 } serial_stm32_devices_t;
00066 
00067 
00071 typedef enum
00072 {
00073     SERIAL_STM32_DATABITS_8 = 8,
00074     SERIAL_STM32_DATABITS_9 = 9,
00075 } serial_stm32_databits_t;
00076 
00077 
00081 typedef enum
00082 {
00083     SERIAL_STM32_STOPBITS_1     = USART_STOPBITS_1,
00084     SERIAL_STM32_STOPBITS_0_5   = USART_STOPBITS_0_5,
00085     SERIAL_STM32_STOPBITS_2     = USART_STOPBITS_2,
00086     SERIAL_STM32_STOPBITS_1_5   = USART_STOPBITS_1_5,
00087 } serial_stm32_stopbits_t;
00088 
00089 
00093 typedef enum
00094 {
00095     SERIAL_STM32_PARITY_NONE    = USART_PARITY_NONE,
00096     SERIAL_STM32_PARITY_EVEN    = USART_PARITY_EVEN,
00097     SERIAL_STM32_PARITY_ODD     = USART_PARITY_ODD,
00098 } serial_stm32_parity_t;
00099 
00100 
00104 typedef enum
00105 {
00106     SERIAL_STM32_MODE_RX    = USART_MODE_RX,
00107     SERIAL_STM32_MODE_TX    = USART_MODE_TX,
00108     SERIAL_STM32_MODE_TX_RX = USART_MODE_TX_RX,
00109 } serial_stm32_mode_t;
00110 
00111 
00115 typedef enum
00116 {
00117     SERIAL_STM32_FLOWCONTROL_NONE       = USART_FLOWCONTROL_NONE,
00118     SERIAL_STM32_FLOWCONTROL_RTS        = USART_FLOWCONTROL_RTS,
00119     SERIAL_STM32_FLOWCONTROL_CTS        = USART_FLOWCONTROL_CTS,
00120     SERIAL_STM32_FLOWCONTROL_RTS_CTS    = USART_FLOWCONTROL_RTS_CTS,
00121 } serial_stm32_flowcontrol_t;
00122 
00123 
00127 typedef struct
00128 {
00129     uint8_t  pin;                       
00130     uint8_t  function;                  
00131 } serial_avr32_gpio_map_t;
00132 
00133 
00137 typedef struct
00138 {
00139     serial_stm32_devices_t      device;
00140     uint32_t                    baudrate;
00141     serial_stm32_databits_t     databits;
00142     serial_stm32_stopbits_t     stopbits;
00143     serial_stm32_parity_t       parity;
00144     serial_stm32_mode_t         mode;
00145     serial_stm32_flowcontrol_t  flow_control;
00146     gpio_stm32_port_t           rx_port;
00147     gpio_stm32_pin_t            rx_pin;
00148     gpio_stm32_alt_function_t   rx_af;
00149     gpio_stm32_port_t           tx_port;
00150     gpio_stm32_pin_t            tx_pin;
00151     gpio_stm32_alt_function_t   tx_af;
00152 } serial_stm32_conf_t;
00153 
00154 
00160 static inline serial_stm32_conf_t serial_stm32_default_config();
00161 
00162 
00166 class Serial_stm32: public Serial
00167 {
00168 public:
00169 
00175     Serial_stm32(serial_stm32_conf_t config = serial_stm32_default_config());
00176 
00177 
00184     bool init(void);
00185 
00186 
00192     uint32_t readable(void);
00193 
00194 
00200     uint32_t writeable(void);
00201 
00202 
00208     void flush(void);
00209 
00210 
00224     bool attach(serial_interrupt_callback_t func);
00225 
00226 
00236     bool write(const uint8_t* bytes, const uint32_t size = 1);
00237 
00238 
00248     bool read(uint8_t* bytes, const uint32_t size = 1);
00249 
00250 
00257     void irq_handler(void);
00258 
00259 private:
00260     serial_stm32_conf_t         config_;            
00261 
00262     Buffer_T<1024>            tx_buffer_;         
00263     Buffer_T<1024>            rx_buffer_;         
00264 
00270     serial_interrupt_callback_t irq_callback;
00271 };
00272 
00273 
00274 static inline serial_stm32_conf_t serial_stm32_default_config()
00275 {
00276     serial_stm32_conf_t conf = {};
00277 
00278     conf.device         = SERIAL_STM32_2;
00279     conf.baudrate       = 38400;
00280     conf.databits       = SERIAL_STM32_DATABITS_8;
00281     conf.stopbits       = SERIAL_STM32_STOPBITS_1;
00282     conf.parity         = SERIAL_STM32_PARITY_NONE;
00283     conf.mode           = SERIAL_STM32_MODE_TX_RX;
00284     conf.flow_control   = SERIAL_STM32_FLOWCONTROL_NONE;
00285     conf.rx_port        = GPIO_STM32_PORT_A;
00286     conf.rx_pin         = GPIO_STM32_PIN_3;
00287     conf.rx_af          = GPIO_STM32_AF_7;
00288     conf.tx_port        = GPIO_STM32_PORT_A;
00289     conf.tx_pin         = GPIO_STM32_PIN_2;
00290     conf.tx_af          = GPIO_STM32_AF_7;
00291 
00292     return conf;
00293 }
00294 
00295 
00296 #endif /* SERIAL_STM32_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines