2009-11-29 08:50:13 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011 Giovanni Di Sirio.
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @file STM32/serial_lld.c
|
|
|
|
* @brief STM32 low level serial driver code.
|
|
|
|
*
|
2010-10-25 18:48:13 +00:00
|
|
|
* @addtogroup SERIAL
|
2009-11-29 08:50:13 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#if HAL_USE_SERIAL || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2009-12-19 09:05:40 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver exported variables. */
|
2009-12-19 09:05:40 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/** @brief USART1 serial driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1 || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
SerialDriver SD1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @brief USART2 serial driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART2 || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
SerialDriver SD2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @brief USART3 serial driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART3 || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
SerialDriver SD3;
|
|
|
|
#endif
|
|
|
|
|
2010-01-23 18:32:02 +00:00
|
|
|
/** @brief UART4 serial driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART4 || defined(__DOXYGEN__)
|
2010-01-23 18:32:02 +00:00
|
|
|
SerialDriver SD4;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @brief UART5 serial driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART5 || defined(__DOXYGEN__)
|
2010-01-23 18:32:02 +00:00
|
|
|
SerialDriver SD5;
|
|
|
|
#endif
|
|
|
|
|
2009-12-19 09:05:40 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver local variables. */
|
2009-12-19 09:05:40 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/** @brief Driver default configuration.*/
|
2010-01-01 13:41:31 +00:00
|
|
|
static const SerialConfig default_config =
|
2009-11-29 08:50:13 +00:00
|
|
|
{
|
2010-01-02 10:59:48 +00:00
|
|
|
SERIAL_DEFAULT_BITRATE,
|
2009-11-29 08:50:13 +00:00
|
|
|
0,
|
2010-07-31 07:02:45 +00:00
|
|
|
USART_CR2_STOP1_BITS | USART_CR2_LINEN,
|
2009-11-29 08:50:13 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver local functions. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @brief USART initialization.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details This function must be invoked with interrupts disabled.
|
|
|
|
*
|
2010-04-20 15:39:07 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver object
|
|
|
|
* @param[in] config the architecture-dependent serial driver configuration
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-04-19 15:48:30 +00:00
|
|
|
static void usart_init(SerialDriver *sdp, const SerialConfig *config) {
|
2010-01-22 14:51:54 +00:00
|
|
|
USART_TypeDef *u = sdp->usart;
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Baud rate setting.
|
|
|
|
*/
|
2010-01-22 14:51:54 +00:00
|
|
|
if (sdp->usart == USART1)
|
2010-05-09 17:52:30 +00:00
|
|
|
u->BRR = STM32_PCLK2 / config->sc_speed;
|
2009-11-29 08:50:13 +00:00
|
|
|
else
|
2010-05-09 17:52:30 +00:00
|
|
|
u->BRR = STM32_PCLK1 / config->sc_speed;
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that some bits are enforced.
|
|
|
|
*/
|
2010-04-19 15:48:30 +00:00
|
|
|
u->CR1 = config->sc_cr1 | USART_CR1_UE | USART_CR1_PEIE |
|
2011-01-22 14:13:49 +00:00
|
|
|
USART_CR1_RXNEIE | USART_CR1_TE |
|
|
|
|
USART_CR1_RE;
|
2010-04-19 15:48:30 +00:00
|
|
|
u->CR2 = config->sc_cr2 | USART_CR2_LBDIE;
|
|
|
|
u->CR3 = config->sc_cr3 | USART_CR3_EIE;
|
2011-01-22 09:52:08 +00:00
|
|
|
u->SR = 0;
|
2010-01-01 13:41:31 +00:00
|
|
|
(void)u->SR; /* SR reset step 1.*/
|
|
|
|
(void)u->DR; /* SR reset step 2.*/
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @brief USART de-initialization.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details This function must be invoked with interrupts disabled.
|
|
|
|
*
|
2010-04-20 15:39:07 +00:00
|
|
|
* @param[in] u pointer to an USART I/O block
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
static void usart_deinit(USART_TypeDef *u) {
|
|
|
|
|
|
|
|
u->CR1 = 0;
|
|
|
|
u->CR2 = 0;
|
|
|
|
u->CR3 = 0;
|
|
|
|
}
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1 || STM32_SERIAL_USE_USART2 || \
|
|
|
|
STM32_SERIAL_USE_USART3 || STM32_SERIAL_USE_UART4 || \
|
|
|
|
USE_STM32_USART5
|
2009-11-29 08:50:13 +00:00
|
|
|
/**
|
2010-07-27 14:44:28 +00:00
|
|
|
* @brief Error handling routine.
|
2010-01-01 13:41:31 +00:00
|
|
|
*
|
2010-04-20 15:39:07 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver object
|
|
|
|
* @param[in] sr USART SR register value
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
static void set_error(SerialDriver *sdp, uint16_t sr) {
|
2011-01-09 10:10:39 +00:00
|
|
|
ioflags_t sts = 0;
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
if (sr & USART_SR_ORE)
|
|
|
|
sts |= SD_OVERRUN_ERROR;
|
|
|
|
if (sr & USART_SR_PE)
|
|
|
|
sts |= SD_PARITY_ERROR;
|
|
|
|
if (sr & USART_SR_FE)
|
|
|
|
sts |= SD_FRAMING_ERROR;
|
2010-01-01 13:41:31 +00:00
|
|
|
if (sr & USART_SR_NE)
|
|
|
|
sts |= SD_NOISE_ERROR;
|
2009-11-29 08:50:13 +00:00
|
|
|
chSysLockFromIsr();
|
2011-01-09 10:10:39 +00:00
|
|
|
chIOAddFlagsI(sdp, sts);
|
2009-11-29 08:50:13 +00:00
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @brief Common IRQ handler.
|
2010-01-01 13:41:31 +00:00
|
|
|
*
|
2010-04-20 15:39:07 +00:00
|
|
|
* @param[in] sdp communication channel associated to the USART
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
static void serve_interrupt(SerialDriver *sdp) {
|
2010-01-22 14:51:54 +00:00
|
|
|
USART_TypeDef *u = sdp->usart;
|
2010-01-01 13:41:31 +00:00
|
|
|
uint16_t cr1 = u->CR1;
|
|
|
|
uint16_t sr = u->SR; /* SR reset step 1.*/
|
|
|
|
uint16_t dr = u->DR; /* SR reset step 2.*/
|
|
|
|
|
2011-01-30 16:18:24 +00:00
|
|
|
/* Error condition detection.*/
|
|
|
|
if (sr & (USART_SR_ORE | USART_SR_NE | USART_SR_FE | USART_SR_PE))
|
2010-01-01 13:41:31 +00:00
|
|
|
set_error(sdp, sr);
|
2011-01-30 16:18:24 +00:00
|
|
|
/* Special case, LIN break detection.*/
|
|
|
|
if (sr & USART_SR_LBD) {
|
|
|
|
chSysLockFromIsr();
|
|
|
|
chIOAddFlagsI(sdp, SD_BREAK_DETECTED);
|
|
|
|
chSysUnlockFromIsr();
|
|
|
|
u->SR &= ~USART_SR_LBD;
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2011-01-30 16:18:24 +00:00
|
|
|
/* Data available.*/
|
2009-11-29 08:50:13 +00:00
|
|
|
if (sr & USART_SR_RXNE) {
|
|
|
|
chSysLockFromIsr();
|
2010-01-01 13:41:31 +00:00
|
|
|
sdIncomingDataI(sdp, (uint8_t)dr);
|
2009-11-29 08:50:13 +00:00
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
2011-01-30 16:18:24 +00:00
|
|
|
/* Transmission buffer empty.*/
|
2010-01-01 13:41:31 +00:00
|
|
|
if ((cr1 & USART_CR1_TXEIE) && (sr & USART_SR_TXE)) {
|
|
|
|
msg_t b;
|
2009-11-29 08:50:13 +00:00
|
|
|
chSysLockFromIsr();
|
2010-01-22 14:51:54 +00:00
|
|
|
b = chOQGetI(&sdp->oqueue);
|
2010-01-01 13:41:31 +00:00
|
|
|
if (b < Q_OK) {
|
2011-01-09 10:10:39 +00:00
|
|
|
chIOAddFlagsI(sdp, IO_OUTPUT_EMPTY);
|
2011-01-30 16:23:08 +00:00
|
|
|
u->CR1 = (cr1 & ~USART_CR1_TXEIE) | USART_CR1_TCIE;
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2011-01-22 14:13:49 +00:00
|
|
|
else
|
2009-11-29 08:50:13 +00:00
|
|
|
u->DR = b;
|
2010-01-01 13:41:31 +00:00
|
|
|
chSysUnlockFromIsr();
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
2011-01-30 16:18:24 +00:00
|
|
|
/* Physical transmission end.*/
|
|
|
|
if (sr & USART_SR_TC) {
|
|
|
|
chSysLockFromIsr();
|
|
|
|
chIOAddFlagsI(sdp, IO_TRANSMISSION_END);
|
|
|
|
chSysUnlockFromIsr();
|
|
|
|
u->CR1 = cr1 & ~USART_CR1_TCIE;
|
|
|
|
u->SR &= ~USART_SR_TC;
|
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
2010-07-31 06:46:17 +00:00
|
|
|
#endif
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1 || defined(__DOXYGEN__)
|
2011-01-02 14:58:51 +00:00
|
|
|
static void notify1(GenericQueue *qp) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2011-01-02 14:58:51 +00:00
|
|
|
(void)qp;
|
2011-01-30 16:23:08 +00:00
|
|
|
USART1->CR1 |= USART_CR1_TXEIE;
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART2 || defined(__DOXYGEN__)
|
2011-01-02 14:58:51 +00:00
|
|
|
static void notify2(GenericQueue *qp) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2011-01-02 14:58:51 +00:00
|
|
|
(void)qp;
|
2011-01-30 16:23:08 +00:00
|
|
|
USART2->CR1 |= USART_CR1_TXEIE;
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART3 || defined(__DOXYGEN__)
|
2011-01-02 14:58:51 +00:00
|
|
|
static void notify3(GenericQueue *qp) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2011-01-02 14:58:51 +00:00
|
|
|
(void)qp;
|
2009-11-29 08:50:13 +00:00
|
|
|
USART3->CR1 |= USART_CR1_TXEIE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART4 || defined(__DOXYGEN__)
|
2011-01-02 14:58:51 +00:00
|
|
|
static void notify4(GenericQueue *qp) {
|
2010-01-23 18:32:02 +00:00
|
|
|
|
2011-01-02 14:58:51 +00:00
|
|
|
(void)qp;
|
2010-01-23 18:32:02 +00:00
|
|
|
UART4->CR1 |= USART_CR1_TXEIE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART5 || defined(__DOXYGEN__)
|
2011-01-02 14:58:51 +00:00
|
|
|
static void notify5(GenericQueue *qp) {
|
2010-01-23 18:32:02 +00:00
|
|
|
|
2011-01-02 14:58:51 +00:00
|
|
|
(void)qp;
|
2010-01-23 18:32:02 +00:00
|
|
|
UART5->CR1 |= USART_CR1_TXEIE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver interrupt handlers. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1 || defined(__DOXYGEN__)
|
2010-10-04 17:16:18 +00:00
|
|
|
/**
|
|
|
|
* @brief USART1 interrupt handler.
|
|
|
|
*
|
2010-12-12 14:51:21 +00:00
|
|
|
* @isr
|
2010-10-04 17:16:18 +00:00
|
|
|
*/
|
2010-07-27 08:36:01 +00:00
|
|
|
CH_IRQ_HANDLER(USART1_IRQHandler) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
2010-01-01 13:41:31 +00:00
|
|
|
serve_interrupt(&SD1);
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART2 || defined(__DOXYGEN__)
|
2010-10-04 17:16:18 +00:00
|
|
|
/**
|
|
|
|
* @brief USART2 interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2010-07-27 08:36:01 +00:00
|
|
|
CH_IRQ_HANDLER(USART2_IRQHandler) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
2010-01-01 13:41:31 +00:00
|
|
|
serve_interrupt(&SD2);
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART3 || defined(__DOXYGEN__)
|
2010-10-04 17:16:18 +00:00
|
|
|
/**
|
|
|
|
* @brief USART3 interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2010-07-27 08:36:01 +00:00
|
|
|
CH_IRQ_HANDLER(USART3_IRQHandler) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
2010-01-01 13:41:31 +00:00
|
|
|
serve_interrupt(&SD3);
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART4 || defined(__DOXYGEN__)
|
2010-10-04 17:16:18 +00:00
|
|
|
/**
|
|
|
|
* @brief UART4 interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2010-07-27 08:36:01 +00:00
|
|
|
CH_IRQ_HANDLER(UART4_IRQHandler) {
|
2010-01-23 18:32:02 +00:00
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
|
|
|
serve_interrupt(&SD4);
|
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART5 || defined(__DOXYGEN__)
|
2010-10-04 17:16:18 +00:00
|
|
|
/**
|
|
|
|
* @brief UART5 interrupt handler.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2010-07-27 08:36:01 +00:00
|
|
|
CH_IRQ_HANDLER(UART5_IRQHandler) {
|
2010-01-23 18:32:02 +00:00
|
|
|
|
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
|
|
|
serve_interrupt(&SD5);
|
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver exported functions. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @brief Low level serial driver initialization.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void sd_lld_init(void) {
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1
|
2009-11-29 08:50:13 +00:00
|
|
|
sdObjectInit(&SD1, NULL, notify1);
|
2010-01-22 14:51:54 +00:00
|
|
|
SD1.usart = USART1;
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART2
|
2009-11-29 08:50:13 +00:00
|
|
|
sdObjectInit(&SD2, NULL, notify2);
|
2010-01-22 14:51:54 +00:00
|
|
|
SD2.usart = USART2;
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART3
|
2009-11-29 08:50:13 +00:00
|
|
|
sdObjectInit(&SD3, NULL, notify3);
|
2010-01-22 14:51:54 +00:00
|
|
|
SD3.usart = USART3;
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-01-23 18:32:02 +00:00
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART4
|
2010-01-23 18:32:02 +00:00
|
|
|
sdObjectInit(&SD4, NULL, notify4);
|
|
|
|
SD4.usart = UART4;
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART5
|
2010-01-23 18:32:02 +00:00
|
|
|
sdObjectInit(&SD5, NULL, notify5);
|
|
|
|
SD5.usart = UART5;
|
|
|
|
#endif
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @brief Low level serial driver configuration and (re)start.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
2010-04-20 15:39:07 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver object
|
|
|
|
* @param[in] config the architecture-dependent serial driver configuration.
|
|
|
|
* If this parameter is set to @p NULL then a default
|
|
|
|
* configuration is used.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-04-19 15:48:30 +00:00
|
|
|
void sd_lld_start(SerialDriver *sdp, const SerialConfig *config) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-04-19 15:48:30 +00:00
|
|
|
if (config == NULL)
|
|
|
|
config = &default_config;
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
if (sdp->state == SD_STOP) {
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1
|
2010-01-01 13:41:31 +00:00
|
|
|
if (&SD1 == sdp) {
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
2010-03-30 17:04:51 +00:00
|
|
|
NVICEnableVector(USART1_IRQn,
|
2010-08-08 07:57:28 +00:00
|
|
|
CORTEX_PRIORITY_MASK(STM32_SERIAL_USART1_PRIORITY));
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART2
|
2010-01-01 13:41:31 +00:00
|
|
|
if (&SD2 == sdp) {
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
|
2010-03-30 17:04:51 +00:00
|
|
|
NVICEnableVector(USART2_IRQn,
|
2010-08-08 07:57:28 +00:00
|
|
|
CORTEX_PRIORITY_MASK(STM32_SERIAL_USART2_PRIORITY));
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART3
|
2010-01-01 13:41:31 +00:00
|
|
|
if (&SD3 == sdp) {
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
|
2010-03-30 17:04:51 +00:00
|
|
|
NVICEnableVector(USART3_IRQn,
|
2010-08-08 07:57:28 +00:00
|
|
|
CORTEX_PRIORITY_MASK(STM32_SERIAL_USART3_PRIORITY));
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2010-01-23 18:32:02 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART4
|
2010-01-23 18:32:02 +00:00
|
|
|
if (&SD4 == sdp) {
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_UART4EN;
|
2010-03-30 17:04:51 +00:00
|
|
|
NVICEnableVector(UART4_IRQn,
|
2010-08-08 07:57:28 +00:00
|
|
|
CORTEX_PRIORITY_MASK(STM32_SERIAL_UART4_PRIORITY));
|
2010-01-23 18:32:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART5
|
2010-01-23 18:32:02 +00:00
|
|
|
if (&SD5 == sdp) {
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_UART5EN;
|
2010-03-30 17:04:51 +00:00
|
|
|
NVICEnableVector(UART5_IRQn,
|
2010-08-08 07:57:28 +00:00
|
|
|
CORTEX_PRIORITY_MASK(STM32_SERIAL_UART5_PRIORITY));
|
2010-01-23 18:32:02 +00:00
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2010-04-19 15:48:30 +00:00
|
|
|
usart_init(sdp, config);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-20 15:39:07 +00:00
|
|
|
* @brief Low level serial driver stop.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details De-initializes the USART, stops the associated clock, resets the
|
|
|
|
* interrupt vector.
|
|
|
|
*
|
2010-04-20 15:39:07 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void sd_lld_stop(SerialDriver *sdp) {
|
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
if (sdp->state == SD_READY) {
|
|
|
|
usart_deinit(sdp->usart);
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART1
|
2010-01-01 13:41:31 +00:00
|
|
|
if (&SD1 == sdp) {
|
|
|
|
RCC->APB2ENR &= ~RCC_APB2ENR_USART1EN;
|
|
|
|
NVICDisableVector(USART1_IRQn);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART2
|
2010-01-01 13:41:31 +00:00
|
|
|
if (&SD2 == sdp) {
|
|
|
|
RCC->APB1ENR &= ~RCC_APB1ENR_USART2EN;
|
|
|
|
NVICDisableVector(USART2_IRQn);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_USART3
|
2010-01-01 13:41:31 +00:00
|
|
|
if (&SD3 == sdp) {
|
|
|
|
RCC->APB1ENR &= ~RCC_APB1ENR_USART3EN;
|
|
|
|
NVICDisableVector(USART3_IRQn);
|
|
|
|
return;
|
|
|
|
}
|
2010-01-23 18:32:02 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART4
|
2010-01-23 18:32:02 +00:00
|
|
|
if (&SD4 == sdp) {
|
|
|
|
RCC->APB1ENR &= ~RCC_APB1ENR_UART4EN;
|
|
|
|
NVICDisableVector(UART4_IRQn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SERIAL_USE_UART5
|
2010-01-23 18:32:02 +00:00
|
|
|
if (&SD5 == sdp) {
|
|
|
|
RCC->APB1ENR &= ~RCC_APB1ENR_UART5EN;
|
|
|
|
NVICDisableVector(UART5_IRQn);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-01-01 13:41:31 +00:00
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#endif /* HAL_USE_SERIAL */
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/** @} */
|