PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/src/stm32f0l0g0/stm32_lowputc_v1.c

https://bitbucket.org/zapparello/nuttx
C | 406 lines | 230 code | 79 blank | 97 comment | 28 complexity | 8fd6be5759602cf34681a38a09f43b6f MD5 | raw file
Possible License(s): Apache-2.0
  1. /****************************************************************************
  2. * arch/arm/src/stm32f0l0g0/stm32_lowputc_v1.c
  3. *
  4. * Copyright (C) 2009, 2012 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. * Alan Carvalho de Assis <acassis@gmail.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdint.h>
  41. #include <arch/board/board.h>
  42. #include "up_internal.h"
  43. #include "up_arch.h"
  44. #include "chip.h"
  45. #include "stm32_rcc.h"
  46. #include "stm32_gpio.h"
  47. #include "stm32_uart.h"
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. /* Select USART parameters for the selected console */
  52. #ifdef HAVE_CONSOLE
  53. # if defined(CONFIG_USART1_SERIAL_CONSOLE)
  54. # define STM32_CONSOLE_BASE STM32_USART1_BASE
  55. # define STM32_APBCLOCK STM32_PCLK1_FREQUENCY
  56. # define STM32_CONSOLE_BAUD CONFIG_USART1_BAUD
  57. # define STM32_CONSOLE_BITS CONFIG_USART1_BITS
  58. # define STM32_CONSOLE_PARITY CONFIG_USART1_PARITY
  59. # define STM32_CONSOLE_2STOP CONFIG_USART1_2STOP
  60. # ifdef CONFIG_USART1_RS485
  61. # define STM32_CONSOLE_RS485_DIR GPIO_USART1_RS485_DIR
  62. # if (CONFIG_USART1_RS485_DIR_POLARITY == 0)
  63. # define STM32_CONSOLE_RS485_DIR_POLARITY false
  64. # else
  65. # define STM32_CONSOLE_RS485_DIR_POLARITY true
  66. # endif
  67. # endif
  68. # elif defined(CONFIG_USART2_SERIAL_CONSOLE)
  69. # define STM32_CONSOLE_BASE STM32_USART2_BASE
  70. # define STM32_APBCLOCK STM32_PCLK1_FREQUENCY
  71. # define STM32_CONSOLE_BAUD CONFIG_USART2_BAUD
  72. # define STM32_CONSOLE_BITS CONFIG_USART2_BITS
  73. # define STM32_CONSOLE_PARITY CONFIG_USART2_PARITY
  74. # define STM32_CONSOLE_2STOP CONFIG_USART2_2STOP
  75. # ifdef CONFIG_USART2_RS485
  76. # define STM32_CONSOLE_RS485_DIR GPIO_USART2_RS485_DIR
  77. # if (CONFIG_USART2_RS485_DIR_POLARITY == 0)
  78. # define STM32_CONSOLE_RS485_DIR_POLARITY false
  79. # else
  80. # define STM32_CONSOLE_RS485_DIR_POLARITY true
  81. # endif
  82. # endif
  83. # elif defined(CONFIG_USART3_SERIAL_CONSOLE)
  84. # define STM32_CONSOLE_BASE STM32_USART3_BASE
  85. # define STM32_APBCLOCK STM32_PCLK1_FREQUENCY
  86. # define STM32_CONSOLE_BAUD CONFIG_USART3_BAUD
  87. # define STM32_CONSOLE_BITS CONFIG_USART3_BITS
  88. # define STM32_CONSOLE_PARITY CONFIG_USART3_PARITY
  89. # define STM32_CONSOLE_2STOP CONFIG_USART3_2STOP
  90. # ifdef CONFIG_USART3_RS485
  91. # define STM32_CONSOLE_RS485_DIR GPIO_USART3_RS485_DIR
  92. # if (CONFIG_USART3_RS485_DIR_POLARITY == 0)
  93. # define STM32_CONSOLE_RS485_DIR_POLARITY false
  94. # else
  95. # define STM32_CONSOLE_RS485_DIR_POLARITY true
  96. # endif
  97. # endif
  98. # elif defined(CONFIG_USART4_SERIAL_CONSOLE)
  99. # define STM32_CONSOLE_BASE STM32_USART4_BASE
  100. # define STM32_APBCLOCK STM32_PCLK1_FREQUENCY
  101. # define STM32_CONSOLE_BAUD CONFIG_USART4_BAUD
  102. # define STM32_CONSOLE_BITS CONFIG_USART4_BITS
  103. # define STM32_CONSOLE_PARITY CONFIG_USART4_PARITY
  104. # define STM32_CONSOLE_2STOP CONFIG_USART4_2STOP
  105. # ifdef CONFIG_USART4_RS485
  106. # define STM32_CONSOLE_RS485_DIR GPIO_USART4_RS485_DIR
  107. # if (CONFIG_USART4_RS485_DIR_POLARITY == 0)
  108. # define STM32_CONSOLE_RS485_DIR_POLARITY false
  109. # else
  110. # define STM32_CONSOLE_RS485_DIR_POLARITY true
  111. # endif
  112. # endif
  113. # elif defined(CONFIG_USART5_SERIAL_CONSOLE)
  114. # define STM32_CONSOLE_BASE STM32_USART5_BASE
  115. # define STM32_APBCLOCK STM32_PCLK1_FREQUENCY
  116. # define STM32_CONSOLE_BAUD CONFIG_USART5_BAUD
  117. # define STM32_CONSOLE_BITS CONFIG_USART5_BITS
  118. # define STM32_CONSOLE_PARITY CONFIG_USART5_PARITY
  119. # define STM32_CONSOLE_2STOP CONFIG_USART5_2STOP
  120. # ifdef CONFIG_USART5_RS485
  121. # define STM32_CONSOLE_RS485_DIR GPIO_USART5_RS485_DIR
  122. # if (CONFIG_USART5_RS485_DIR_POLARITY == 0)
  123. # define STM32_CONSOLE_RS485_DIR_POLARITY false
  124. # else
  125. # define STM32_CONSOLE_RS485_DIR_POLARITY true
  126. # endif
  127. # endif
  128. # endif
  129. /* CR1 settings */
  130. # if STM32_CONSOLE_BITS == 9
  131. # define USART_CR1_M0_VALUE USART_CR1_M0
  132. # define USART_CR1_M1_VALUE 0
  133. # elif STM32_CONSOLE_BITS == 7
  134. # define USART_CR1_M0_VALUE 0
  135. # define USART_CR1_M1_VALUE USART_CR1_M1
  136. # else /* 8 bits */
  137. # define USART_CR1_M0_VALUE 0
  138. # define USART_CR1_M1_VALUE 0
  139. # endif
  140. # if STM32_CONSOLE_PARITY == 1 /* odd parity */
  141. # define USART_CR1_PARITY_VALUE (USART_CR1_PCE|USART_CR1_PS)
  142. # elif STM32_CONSOLE_PARITY == 2 /* even parity */
  143. # define USART_CR1_PARITY_VALUE USART_CR1_PCE
  144. # else /* no parity */
  145. # define USART_CR1_PARITY_VALUE 0
  146. # endif
  147. # define USART_CR1_CLRBITS \
  148. (USART_CR1_UESM | USART_CR1_RE | USART_CR1_TE | USART_CR1_PS | \
  149. USART_CR1_PCE | USART_CR1_WAKE | USART_CR1_M0 | USART_CR1_M1 | \
  150. USART_CR1_MME | USART_CR1_OVER8 | USART_CR1_DEDT_MASK | \
  151. USART_CR1_DEAT_MASK | USART_CR1_ALLINTS)
  152. # define USART_CR1_SETBITS (USART_CR1_M0_VALUE|USART_CR1_M1_VALUE|USART_CR1_PARITY_VALUE)
  153. /* CR2 settings */
  154. # if STM32_CONSOLE_2STOP != 0
  155. # define USART_CR2_STOP2_VALUE USART_CR2_STOP2
  156. # else
  157. # define USART_CR2_STOP2_VALUE 0
  158. # endif
  159. # define USART_CR2_CLRBITS \
  160. (USART_CR2_ADDM7 | USART_CR2_LBDL | USART_CR2_LBDIE | USART_CR2_LBCL | \
  161. USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_STOP_MASK | \
  162. USART_CR2_LINEN | USART_CR2_SWAP | USART_CR2_RXINV | USART_CR2_TXINV | \
  163. USART_CR2_DATAINV | USART_CR2_MSBFIRST | USART_CR2_ABREN | \
  164. USART_CR2_ABRMOD_MASK | USART_CR2_RTOEN | USART_CR2_ADD_MASK)
  165. # define USART_CR2_SETBITS USART_CR2_STOP2_VALUE
  166. /* CR3 settings */
  167. # define USART_CR3_CLRBITS \
  168. (USART_CR3_EIE | USART_CR3_IREN | USART_CR3_IRLP | USART_CR3_HDSEL | \
  169. USART_CR3_NACK | USART_CR3_SCEN | USART_CR3_DMAR | USART_CR3_DMAT | \
  170. USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_CTSIE | USART_CR3_ONEBIT | \
  171. USART_CR3_OVRDIS | USART_CR3_DDRE | USART_CR3_DEM | USART_CR3_DEP | \
  172. USART_CR3_SCARCNT_MASK | USART_CR3_WUS_MASK | USART_CR3_WUFIE)
  173. # define USART_CR3_SETBITS 0
  174. # undef USE_OVER8
  175. /* Calculate USART BAUD rate divider */
  176. /* Baud rate for standard USART (SPI mode included):
  177. *
  178. * In case of oversampling by 16, the equation is:
  179. * baud = fCK / UARTDIV
  180. * UARTDIV = fCK / baud
  181. *
  182. * In case of oversampling by 8, the equation is:
  183. *
  184. * baud = 2 * fCK / UARTDIV
  185. * UARTDIV = 2 * fCK / baud
  186. */
  187. # define STM32_USARTDIV8 \
  188. (((STM32_APBCLOCK << 1) + (STM32_CONSOLE_BAUD >> 1)) / STM32_CONSOLE_BAUD)
  189. # define STM32_USARTDIV16 \
  190. ((STM32_APBCLOCK + (STM32_CONSOLE_BAUD >> 1)) / STM32_CONSOLE_BAUD)
  191. /* Use oversamply by 8 only if the divisor is small. But what is small? */
  192. # if STM32_USARTDIV8 > 100
  193. # define STM32_BRR_VALUE STM32_USARTDIV16
  194. # else
  195. # define USE_OVER8 1
  196. # define STM32_BRR_VALUE \
  197. ((STM32_USARTDIV8 & 0xfff0) | ((STM32_USARTDIV8 & 0x000f) >> 1))
  198. # endif
  199. #endif /* HAVE_CONSOLE */
  200. /****************************************************************************
  201. * Public Functions
  202. ****************************************************************************/
  203. /****************************************************************************
  204. * Name: up_lowputc
  205. *
  206. * Description:
  207. * Output one byte on the serial console
  208. *
  209. ****************************************************************************/
  210. void up_lowputc(char ch)
  211. {
  212. #ifdef HAVE_CONSOLE
  213. /* Wait until the TX data register is empty */
  214. while ((getreg32(STM32_CONSOLE_BASE + STM32_USART_ISR_OFFSET) & USART_ISR_TXE) == 0);
  215. #ifdef STM32_CONSOLE_RS485_DIR
  216. stm32_gpiowrite(STM32_CONSOLE_RS485_DIR, STM32_CONSOLE_RS485_DIR_POLARITY);
  217. #endif
  218. /* Then send the character */
  219. putreg32((uint32_t)ch, STM32_CONSOLE_BASE + STM32_USART_TDR_OFFSET);
  220. #ifdef STM32_CONSOLE_RS485_DIR
  221. while ((getreg32(STM32_CONSOLE_BASE + STM32_USART_ISR_OFFSET) & USART_ISR_TC) == 0);
  222. stm32_gpiowrite(STM32_CONSOLE_RS485_DIR, !STM32_CONSOLE_RS485_DIR_POLARITY);
  223. #endif
  224. #endif /* HAVE_CONSOLE */
  225. }
  226. /****************************************************************************
  227. * Name: stm32_lowsetup
  228. *
  229. * Description:
  230. * This performs basic initialization of the USART used for the serial
  231. * console. Its purpose is to get the console output available as soon
  232. * as possible.
  233. *
  234. ****************************************************************************/
  235. void stm32_lowsetup(void)
  236. {
  237. #if defined(HAVE_USART)
  238. #if defined(HAVE_CONSOLE) && !defined(CONFIG_SUPPRESS_UART_CONFIG)
  239. uint32_t cr;
  240. #endif
  241. /* Setup clocking and GPIO pins for all configured USARTs */
  242. #ifdef CONFIG_STM32F0L0G0_USART1
  243. /* Enable USART APB2 clock */
  244. modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_USART1EN);
  245. /* Configure RX/TX pins */
  246. stm32_configgpio(GPIO_USART1_TX);
  247. stm32_configgpio(GPIO_USART1_RX);
  248. #ifdef CONFIG_USART1_RS485
  249. stm32_configgpio(GPIO_USART1_RS485_DIR);
  250. stm32_gpiowrite(GPIO_USART1_RS485_DIR, !CONFIG_USART1_RS485_DIR_POLARITY);
  251. #endif
  252. #endif
  253. #ifdef CONFIG_STM32F0L0G0_USART2
  254. /* Enable USART APB1 clock */
  255. modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_USART2EN);
  256. /* Configure RX/TX pins */
  257. stm32_configgpio(GPIO_USART2_TX);
  258. stm32_configgpio(GPIO_USART2_RX);
  259. #ifdef CONFIG_USART2_RS485
  260. stm32_configgpio(GPIO_USART2_RS485_DIR);
  261. stm32_gpiowrite(GPIO_USART2_RS485_DIR, !CONFIG_USART2_RS485_DIR_POLARITY);
  262. #endif
  263. #endif
  264. #ifdef CONFIG_STM32F0L0G0_USART3
  265. /* Enable USART APB1 clock */
  266. modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_USART3EN);
  267. /* Configure RX/TX pins */
  268. stm32_configgpio(GPIO_USART3_TX);
  269. stm32_configgpio(GPIO_USART3_RX);
  270. #ifdef CONFIG_USART3_RS485
  271. stm32_configgpio(GPIO_USART3_RS485_DIR);
  272. stm32_gpiowrite(GPIO_USART3_RS485_DIR, !CONFIG_USART3_RS485_DIR_POLARITY);
  273. #endif
  274. #endif
  275. #ifdef CONFIG_STM32F0L0G0_USART4
  276. /* Enable USART APB1 clock */
  277. modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_USART4EN);
  278. /* Configure RX/TX pins */
  279. stm32_configgpio(GPIO_USART4_TX);
  280. stm32_configgpio(GPIO_USART4_RX);
  281. #ifdef CONFIG_USART4_RS485
  282. stm32_configgpio(GPIO_USART4_RS485_DIR);
  283. stm32_gpiowrite(GPIO_USART4_RS485_DIR, !CONFIG_USART4_RS485_DIR_POLARITY);
  284. #endif
  285. #endif
  286. #ifdef CONFIG_STM32F0L0G0_USART5
  287. /* Enable USART APB1 clock */
  288. modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_USART5EN);
  289. /* Configure RX/TX pins */
  290. stm32_configgpio(GPIO_USART5_TX);
  291. stm32_configgpio(GPIO_USART5_RX);
  292. #ifdef CONFIG_USART5_RS485
  293. stm32_configgpio(GPIO_USART5_RS485_DIR);
  294. stm32_gpiowrite(GPIO_USART5_RS485_DIR, !CONFIG_USART5_RS485_DIR_POLARITY);
  295. #endif
  296. #endif
  297. /* Enable and configure the selected console device */
  298. #if defined(HAVE_CONSOLE) && !defined(CONFIG_SUPPRESS_UART_CONFIG)
  299. /* Configure CR2 */
  300. cr = getreg32(STM32_CONSOLE_BASE + STM32_USART_CR2_OFFSET);
  301. cr &= ~USART_CR2_CLRBITS;
  302. cr |= USART_CR2_SETBITS;
  303. putreg32(cr, STM32_CONSOLE_BASE + STM32_USART_CR2_OFFSET);
  304. /* Configure CR1 */
  305. cr = getreg32(STM32_CONSOLE_BASE + STM32_USART_CR1_OFFSET);
  306. cr &= ~USART_CR1_CLRBITS;
  307. cr |= USART_CR1_SETBITS;
  308. putreg32(cr, STM32_CONSOLE_BASE + STM32_USART_CR1_OFFSET);
  309. /* Configure CR3 */
  310. cr = getreg32(STM32_CONSOLE_BASE + STM32_USART_CR3_OFFSET);
  311. cr &= ~USART_CR3_CLRBITS;
  312. cr |= USART_CR3_SETBITS;
  313. putreg32(cr, STM32_CONSOLE_BASE + STM32_USART_CR3_OFFSET);
  314. /* Configure the USART Baud Rate */
  315. putreg32(STM32_BRR_VALUE, STM32_CONSOLE_BASE + STM32_USART_BRR_OFFSET);
  316. /* Select oversampling by 8 */
  317. cr = getreg32(STM32_CONSOLE_BASE + STM32_USART_CR1_OFFSET);
  318. #ifdef USE_OVER8
  319. cr |= USART_CR1_OVER8;
  320. putreg32(cr, STM32_CONSOLE_BASE + STM32_USART_CR1_OFFSET);
  321. #endif
  322. /* Enable Rx, Tx, and the USART */
  323. cr |= (USART_CR1_UE | USART_CR1_TE | USART_CR1_RE);
  324. putreg32(cr, STM32_CONSOLE_BASE + STM32_USART_CR1_OFFSET);
  325. #endif /* HAVE_CONSOLE && !CONFIG_SUPPRESS_UART_CONFIG */
  326. #endif /* HAVE_USART */
  327. }