MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/boards/sparky_chibi/sparky_chibi.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    sparky_chibi.hpp
00034  *
00035  * \author  MAV'RIC Team
00036  * \author  Julien Lecoeur
00037  *
00038  * \brief   Autopilot for Sparky board based on STM32
00039  *
00040  ******************************************************************************/
00041 
00042 
00043 #ifndef SPARKY_CHIBI_HPP_
00044 #define SPARKY_CHIBI_HPP_
00045 
00046 
00047 #include "drivers/barometer_ms5611.hpp"
00048 #include "hal/common/led_gpio.hpp"
00049 #include "drivers/state_display_sparky_v2.hpp"
00050 #include "drivers/servo.hpp"
00051 
00052 #include "hal/dummy/gpio_dummy.hpp"
00053 #include "hal/dummy/pwm_dummy.hpp"
00054 #include "hal/dummy/serial_dummy.hpp"
00055 
00056 #include "hal/chibios/gpio_chibios.hpp"
00057 #include "hal/chibios/i2c_chibios.hpp"
00058 #include "hal/chibios/serial_chibios.hpp"
00059 #include "hal/chibios/pwm_chibios.hpp"
00060 #include "hal/chibios/spi_chibios.hpp"
00061 
00062 extern "C"
00063 {
00064 #include "util/streams.h"
00065 }
00066 
00067 
00068 // Preprocessor definitions
00069 
00070 /*
00071  * Should the ESC be calibrated?
00072  * 0 for false (normal flight)
00073  * 1 for true (calibration)
00074  * !!!IMPORTANT!!!
00075  * IF CALIBRATING, TAKE OFF PROPS
00076  */
00077 #define CALIBRATE_ESC 0
00078 
00079 
00083 // typedef struct
00084 // {
00085     // gpio_stm32_conf_t        dsm_receiver_gpio_config;
00086     // gpio_stm32_conf_t        dsm_power_gpio_config;
00087     // gpio_stm32_conf_t        led_err_gpio_config;
00088     // gpio_stm32_conf_t        led_stat_gpio_config;
00089     // gpio_stm32_conf_t        led_rf_gpio_config;
00090     // Pwm_stm32::config_t      pwm_config[8];
00091     // Serial_usb_stm32::conf_t serial_usb_config;
00092     // servo_conf_t             servo_config[8];
00093     // spi_stm32_conf_t         spi_config[3];
00094     // i2c_stm32_conf_t         i2c_config[2];
00095     //
00096     // Barometer_MS5611::conf_t barometer_config;
00097 // } sparky_v2_conf_t;
00098 
00099 
00105 // static inline sparky_v2_conf_t sparky_v2_default_config();
00106 
00107 
00112 class Sparky_chibi
00113 {
00114 public:
00115 
00116     static const uint8_t PWM_COUNT    = 10;
00117 
00121     struct conf_t
00122     {
00123         Gpio_chibios::conf_t      gpio_led_err;
00124         Gpio_chibios::conf_t      gpio_led_stat;
00125         Gpio_chibios::conf_t      gpio_led_rf;
00126         Serial_chibios::conf_t    serial;
00127         Pwm_chibios::conf_t       pwm[PWM_COUNT];
00128         servo_conf_t              servo[PWM_COUNT];
00129         I2c_chibios::conf_t       i2c1;
00130         Barometer_MS5611::conf_t  barometer;
00131     };
00132 
00133 
00134 
00140     Sparky_chibi(conf_t config = default_config());
00141 
00142 
00148     bool init(void);
00149 
00150 
00156     static inline conf_t default_config();
00157 
00158 
00162     // Gpio_stm32              led_err_gpio_;
00163     // Gpio_stm32              led_stat_gpio_;
00164     // Gpio_stm32              led_rf_gpio_;
00165     // Led_gpio                led_err_;
00166     // Led_gpio                led_stat_;
00167     // Led_gpio                led_rf_;
00168     // Pwm_stm32               pwm_0_;
00169     // Pwm_stm32               pwm_1_;
00170     // Pwm_stm32               pwm_2_;
00171     // Pwm_stm32               pwm_3_;
00172     // Pwm_stm32               pwm_4_;
00173     // Pwm_stm32               pwm_5_;
00174     // Pwm_dummy               pwm_6_;
00175     // Pwm_dummy               pwm_7_;
00176     // Serial_usb_stm32        serial_;
00177     // Servo                   servo_0_;
00178     // Servo                   servo_1_;
00179     // Servo                   servo_2_;
00180     // Servo                   servo_3_;
00181     // Servo                   servo_4_;
00182     // Servo                   servo_5_;
00183     // Servo                   servo_6_;
00184     // Servo                   servo_7_;
00185     // Spi_stm32               spi_1_;
00186     // Spi_stm32               spi_3_;
00187     // I2c_stm32               i2c_1_;
00188     // I2c_stm32               i2c_2_;
00189     // State_display_sparky_v2 state_display_sparky_v2_;
00190 
00191 
00192 
00193     Gpio_chibios            gpio_led_err_;
00194     Gpio_chibios            gpio_led_stat_;
00195     Gpio_chibios            gpio_led_rf_;
00196     Led_gpio                led_err_;
00197     Led_gpio                led_stat_;
00198     Led_gpio                led_rf_;
00199     Serial_chibios          serial_;
00200     Pwm_chibios             pwm_[PWM_COUNT];
00201     Servo                   servo_[PWM_COUNT];
00202 
00203     State_display_sparky_v2 state_display_;
00204 
00205     I2c_chibios             i2c1_;
00206 
00207     Barometer_MS5611        barometer_;
00208 
00209 private:
00215     static void pwmp8cb(PWMDriver *pwmp)
00216     {
00217       (void)pwmp;
00218       palSetPad(GPIOB, GPIOB_PIN15_PWM8);
00219     }
00220 
00226     static void pwmp9cb(PWMDriver *pwmp)
00227     {
00228       (void)pwmp;
00229       palSetPad(GPIOB, GPIOB_PIN14_PWM9);
00230     }
00231 
00237     static void pwmc9cb(PWMDriver *pwmp)
00238     {
00239       (void)pwmp;
00240       palClearPad(GPIOB, GPIOB_PIN14_PWM9);
00241     }
00242 
00248     static void pwmc8cb(PWMDriver *pwmp)
00249     {
00250       (void)pwmp;
00251       palClearPad(GPIOB, GPIOB_PIN15_PWM8);
00252     }
00253 
00254     // byte_stream_t   dbg_stream_;  ///< Temporary member to make print_util work TODO: remove
00255 };
00256 
00257 
00258 
00259 
00265 Sparky_chibi::conf_t Sparky_chibi::default_config()
00266 {
00267     conf_t conf = {};
00268 
00269     // -------------------------------------------------------------------------
00270     // GPIO config
00271     // -------------------------------------------------------------------------
00272     conf.gpio_led_err =
00273     {
00274         .port  = GPIOB,
00275         .pin   = GPIOB_PIN4
00276     };
00277 
00278     conf.gpio_led_stat =
00279     {
00280         .port  = GPIOB,
00281         .pin   = GPIOB_PIN5
00282     };
00283 
00284     conf.gpio_led_rf =
00285     {
00286         .port  = GPIOB,
00287         .pin   = GPIOB_PIN6
00288     };
00289 
00290     // -------------------------------------------------------------------------
00291     // Serial config
00292     // -------------------------------------------------------------------------
00293     conf.serial =
00294     {
00295         .id         = Serial_chibios::SERIAL_1,
00296         .device     = &UARTD1,
00297         .baudrate   = 38400
00298     };
00299 
00300     // -------------------------------------------------------------------------
00301     // PWM config
00302     // -------------------------------------------------------------------------
00303     // PWM12 is not directly supported by ChibiOS so we cannot use it for PWM8 and PWM9,
00304     // so we use
00305     // - PWM4 with callbacks pwmp8cb and pwmc8cb to toggle the pin GPIOB_PIN15_PWM8
00306     // - PWM1 with callbacks pwmp9cb and pwmc9cb to toggle the pin GPIOB_PIN14_PWM9
00307     //
00308     // Configuration of PWMs on the servo connectors:
00309     // ID Pin  Timer Channel
00310     // ---------------------
00311     // 0  PB0  TIM3  CH3
00312     // 1  PB1  TIM3  CH4
00313     // 2  PA3  TIM9  CH2
00314     // 3  PA2  TIM9  CH1
00315     // 4  PA1  TIM5  CH2
00316     // 5  PA0  TIM5  CH1
00317     //
00318     // Configuration of PWMs on the servo connectors:
00319     // ID Pin  Timer Channel
00320     // ---------------------
00321     // 6  PC9  TIM8  CH4
00322     // 7  PC8  TIM8  CH3
00323     // 8  PB15 TIM4  CH1 with callbacks
00324     // 9  PB14 TIM1  CH1 with callbacks
00325 
00326     for (size_t i = 0; i < PWM_COUNT; i++)
00327     {
00328         conf.pwm[i] = Pwm_chibios::default_config();
00329     }
00330 
00331     conf.pwm[0].driver  = &PWMD3;
00332     conf.pwm[0].channel = Pwm_chibios::CHANNEL_3;
00333     conf.pwm[1].driver  = &PWMD3;
00334     conf.pwm[1].channel = Pwm_chibios::CHANNEL_4;
00335     conf.pwm[2].driver  = &PWMD9;
00336     conf.pwm[2].channel = Pwm_chibios::CHANNEL_2;
00337     conf.pwm[3].driver  = &PWMD9;
00338     conf.pwm[3].channel = Pwm_chibios::CHANNEL_1;
00339     conf.pwm[4].driver  = &PWMD5;
00340     conf.pwm[4].channel = Pwm_chibios::CHANNEL_2;
00341     conf.pwm[5].driver  = &PWMD5;
00342     conf.pwm[5].channel = Pwm_chibios::CHANNEL_1;
00343     conf.pwm[6].driver  = &PWMD8;
00344     conf.pwm[6].channel = Pwm_chibios::CHANNEL_4;
00345     conf.pwm[7].driver  = &PWMD8;
00346     conf.pwm[7].channel = Pwm_chibios::CHANNEL_3;
00347     conf.pwm[8].driver  = &PWMD4;
00348     conf.pwm[8].channel = Pwm_chibios::CHANNEL_1;
00349     conf.pwm[8].config.callback = pwmp8cb;
00350     conf.pwm[8].config.channels[Pwm_chibios::CHANNEL_1].callback = &pwmc8cb;
00351     conf.pwm[9].driver  = &PWMD1;
00352     conf.pwm[9].channel = Pwm_chibios::CHANNEL_1;
00353     conf.pwm[9].config.callback = pwmp9cb;
00354     conf.pwm[9].config.channels[Pwm_chibios::CHANNEL_1].callback = &pwmc9cb;
00355 
00356 
00357     // -------------------------------------------------------------------------
00358     // Servos config
00359     // -------------------------------------------------------------------------
00360     for (size_t i = 0; i < PWM_COUNT; i++)
00361     {
00362         conf.servo[i] = servo_default_config_esc();
00363     }
00364 
00365     // -------------------------------------------------------------------------
00366     // I2C config
00367     // -------------------------------------------------------------------------
00368     conf.i2c1 =
00369     {
00370         .driver  = &I2CD1,
00371         .config  =
00372         {
00373             .op_mode     = OPMODE_I2C,
00374             .clock_speed = 400000,
00375             .duty_cycle  = FAST_DUTY_CYCLE_2
00376         },
00377         .timeout = 1000,
00378     };
00379 
00380     // -------------------------------------------------------------------------
00381     // Barometer config
00382     // -------------------------------------------------------------------------
00383     conf.barometer = Barometer_MS5611::default_config();
00384 
00385     // // -------------------------------------------------------------------------
00386     // // USB config
00387     // // -------------------------------------------------------------------------
00388     // conf.serial_usb_config = Serial_usb_stm32::default_config();
00389     //
00390     // // -------------------------------------------------------------------------
00391     // // SPI config
00392     // // -------------------------------------------------------------------------
00393     // conf.spi_config[0].spi_device       = STM32_SPI1;
00394     // conf.spi_config[0].mode             = STM32_SPI_IN_OUT;
00395     // conf.spi_config[0].clk_div          = SPI_CR1_BAUDRATE_FPCLK_DIV_128;
00396     //
00397     // conf.spi_config[0].miso_gpio_config.port    = GPIO_STM32_PORT_A;
00398     // conf.spi_config[0].miso_gpio_config.pin     = GPIO_STM32_PIN_6;
00399     // conf.spi_config[0].miso_gpio_config.dir     = GPIO_INPUT;
00400     // conf.spi_config[0].miso_gpio_config.pull    = GPIO_PULL_UPDOWN_DOWN;
00401     // conf.spi_config[0].miso_gpio_config.alt_fct = GPIO_STM32_AF_5;
00402     //
00403     // conf.spi_config[0].mosi_gpio_config.port    = GPIO_STM32_PORT_A;
00404     // conf.spi_config[0].mosi_gpio_config.pin     = GPIO_STM32_PIN_7;
00405     // conf.spi_config[0].mosi_gpio_config.dir     = GPIO_OUTPUT;
00406     // conf.spi_config[0].mosi_gpio_config.pull    = GPIO_PULL_UPDOWN_DOWN;
00407     // conf.spi_config[0].mosi_gpio_config.alt_fct = GPIO_STM32_AF_5;
00408     //
00409     // conf.spi_config[0].nss_gpio_config.port     = GPIO_STM32_PORT_A;
00410     // conf.spi_config[0].nss_gpio_config.pin      = GPIO_STM32_PIN_4;
00411     // conf.spi_config[0].nss_gpio_config.dir      = GPIO_OUTPUT;
00412     // conf.spi_config[0].nss_gpio_config.pull     = GPIO_PULL_UPDOWN_NONE;
00413     // conf.spi_config[0].nss_gpio_config.alt_fct  = GPIO_STM32_AF_5;
00414     //
00415     // conf.spi_config[0].sck_gpio_config.port     = GPIO_STM32_PORT_A;
00416     // conf.spi_config[0].sck_gpio_config.pin      = GPIO_STM32_PIN_5;
00417     // conf.spi_config[0].sck_gpio_config.dir      = GPIO_OUTPUT;
00418     // conf.spi_config[0].sck_gpio_config.pull     = GPIO_PULL_UPDOWN_DOWN;
00419     // conf.spi_config[0].sck_gpio_config.alt_fct  = GPIO_STM32_AF_5;
00420     //
00421     //
00422     // conf.spi_config[2].spi_device               = STM32_SPI3;
00423     // conf.spi_config[2].mode                     = STM32_SPI_IN_OUT;
00424     // conf.spi_config[2].clk_div                  = SPI_CR1_BAUDRATE_FPCLK_DIV_64;
00425     //
00426     // conf.spi_config[2].miso_gpio_config.port    = GPIO_STM32_PORT_C;
00427     // conf.spi_config[2].miso_gpio_config.pin     = GPIO_STM32_PIN_11;
00428     // conf.spi_config[2].miso_gpio_config.dir     = GPIO_INPUT;
00429     // conf.spi_config[2].miso_gpio_config.pull    = GPIO_PULL_UPDOWN_DOWN;
00430     // conf.spi_config[2].miso_gpio_config.alt_fct = GPIO_STM32_AF_6;
00431     //
00432     // conf.spi_config[2].mosi_gpio_config.port    = GPIO_STM32_PORT_C;
00433     // conf.spi_config[2].mosi_gpio_config.pin     = GPIO_STM32_PIN_12;
00434     // conf.spi_config[2].mosi_gpio_config.dir     = GPIO_OUTPUT;
00435     // conf.spi_config[2].mosi_gpio_config.pull    = GPIO_PULL_UPDOWN_DOWN;
00436     // conf.spi_config[2].mosi_gpio_config.alt_fct = GPIO_STM32_AF_6;
00437     //
00438     // conf.spi_config[2].nss_gpio_config.port     = GPIO_STM32_PORT_A;
00439     // conf.spi_config[2].nss_gpio_config.pin      = GPIO_STM32_PIN_15;
00440     // conf.spi_config[2].nss_gpio_config.dir      = GPIO_OUTPUT;
00441     // conf.spi_config[2].nss_gpio_config.pull     = GPIO_PULL_UPDOWN_NONE;
00442     // conf.spi_config[2].nss_gpio_config.alt_fct  = GPIO_STM32_AF_6;
00443     //
00444     // conf.spi_config[2].sck_gpio_config.port     = GPIO_STM32_PORT_C;
00445     // conf.spi_config[2].sck_gpio_config.pin      = GPIO_STM32_PIN_10;
00446     // conf.spi_config[2].sck_gpio_config.dir      = GPIO_OUTPUT;
00447     // conf.spi_config[2].sck_gpio_config.pull     = GPIO_PULL_UPDOWN_DOWN;
00448     // conf.spi_config[2].sck_gpio_config.alt_fct  = GPIO_STM32_AF_6;
00449     //
00450     // conf.i2c_config[0]                        = i2c_stm32_default_config();
00451     // conf.i2c_config[0].i2c_device_config      = STM32_I2C1;
00452     // conf.i2c_config[0].rcc_i2c_config         = RCC_I2C1;
00453     // conf.i2c_config[0].rcc_sda_port_config    = RCC_GPIOB;
00454     // conf.i2c_config[0].sda_config.port        = GPIO_STM32_PORT_B;
00455     // conf.i2c_config[0].sda_config.pin         = GPIO_STM32_PIN_9;
00456     // conf.i2c_config[0].sda_config.alt_fct     = GPIO_STM32_AF_4;
00457     // conf.i2c_config[0].rcc_clk_port_config    = RCC_GPIOB;
00458     // conf.i2c_config[0].clk_config.port        = GPIO_STM32_PORT_B;
00459     // conf.i2c_config[0].clk_config.pin         = GPIO_STM32_PIN_8;
00460     // conf.i2c_config[0].clk_config.alt_fct     = GPIO_STM32_AF_4;
00461     // conf.i2c_config[0].clk_speed              = 100000;
00462     // conf.i2c_config[0].tenbit_config          = false;  // currently only support 8 bits addressing
00463     // conf.i2c_config[0].timeout                = 20000;
00464     //
00465     // conf.i2c_config[1]                        = i2c_stm32_default_config();
00466     // conf.i2c_config[1].i2c_device_config      = STM32_I2C2;
00467     // conf.i2c_config[1].rcc_i2c_config         = RCC_I2C2;
00468     // conf.i2c_config[1].rcc_sda_port_config    = RCC_GPIOB;
00469     // conf.i2c_config[1].sda_config.port        = GPIO_STM32_PORT_B;
00470     // conf.i2c_config[1].sda_config.pin         = GPIO_STM32_PIN_11;
00471     // conf.i2c_config[1].sda_config.alt_fct     = GPIO_STM32_AF_4;
00472     // conf.i2c_config[1].rcc_clk_port_config    = RCC_GPIOB;
00473     // conf.i2c_config[1].clk_config.port        = GPIO_STM32_PORT_B;
00474     // conf.i2c_config[1].clk_config.pin         = GPIO_STM32_PIN_10;
00475     // conf.i2c_config[1].clk_config.alt_fct     = GPIO_STM32_AF_4;
00476     // conf.i2c_config[1].clk_speed              = 100000;
00477     // conf.i2c_config[1].tenbit_config          = false;  // currently only support 8 bits addressing
00478     // conf.i2c_config[1].timeout                = 20000;
00479 
00480     return conf;
00481 }
00482 
00483 
00484 #endif /* SPARKY_CHIBI_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines