add software usart library (incomplete)

This commit is contained in:
King Kévin 2016-08-16 11:56:10 +02:00
parent 2f9dcd0ff7
commit abb59970bd
2 changed files with 92 additions and 0 deletions

63
lib/usart_soft.c Normal file
View File

@ -0,0 +1,63 @@
/* This program 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.
*
* This program 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/>.
*
*/
/** library to control multiple software USARTs (code)
* @file usart_soft.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016
* @note peripherals used: GPIO @ref usart_soft_gpio, timer @ref usart_soft_timer
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // general utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/timer.h> // timer library
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include "usart_soft.h" // software USART library API
#include "global.h" // common methods
/** @defgroup usart_soft_gpio GPIO used for the software USART ports
* @{
*/
const static uint32_t usart_soft_rx_ports[USART_SOFT_PORTS] = {}; /**< GPIO ports for RX signals */
const static uint32_t usart_soft_rx_pins[USART_SOFT_PORTS] = {}; /**< GPIO pins for RX signals */
/** @} */
/** @defgroup usart_soft_config USART configurations
* @note this implementation is designed for 8N1 configuration since this is the most common case
* @{
*/
const static uint32_t usart_soft_baudrate[USART_SOFT_PORTS] = {}; /**< baudrate for USART communication */
/** @} */
/** @defgroup usart_soft_timer timer used to same USART signals
* @{
*/
/** @} */
void usart_soft_setup(void)
{
// setup GPIOs
for (uint8_t i = 0; i < USART_SOFT_PORTS; i++) {
rcc_periph_clock_enable(RCC_GPIO(usart_soft_rx_ports[i])); // enable clock for GPIO peripheral
gpio_set_mode(usart_soft_rx_ports[i], GPIO_MODE_INPUT, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LED_WS2812B_CLK_PIN); // set pin as output
gpio_set_mode(usart_soft_rx_ports[i], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, usart_soft_rx_pins[i]); // setup GPIO pin USART receive
gpio_set(usart_soft_rx_ports[i], usart_soft_rx_pins[i]); // pull up to avoid noise when not connected
}
}

29
lib/usart_soft.h Normal file
View File

@ -0,0 +1,29 @@
/* This program 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.
*
* This program 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/>.
*
*/
/** library to control multiple software USARTs (API)
* @file usart_soft.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016
* @note peripherals used: GPIO @ref usart_soft_gpio, timer @ref usart_soft_timer
*/
/** number of software USART ports
* @note the corresponding GPIOs need to be configured in @p usart_soft_gpio
*/
#define USART_SOFT_PORTS 1
/** setup software USART ports */
void usart_soft_setup(void);