summaryrefslogtreecommitdiff
path: root/usb-serial-rs485/usb_cdcacm-arch.c
blob: e868861ea1ca9ea7a1094b5e05b54aea0c29c724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2014 Karl Palsson <karlp@tweak.net.au>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/dma.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/usart.h>

#include "usb_cdcacm.h"
#include "syscfg.h"
#include "ringb.h"



int glue_set_line_coding_cb(uint32_t baud, uint8_t databits,
	enum usb_cdc_line_coding_bParityType cdc_parity,
	enum usb_cdc_line_coding_bCharFormat cdc_stopbits)
{
	int uart_parity;
	int uart_stopbits;

	if (databits < 8 || databits > 9) {
		return 0;
	}

	/* Be careful here, ST counts parity as a data bit */
	switch (cdc_parity) {
	case USB_CDC_NO_PARITY:
		uart_parity = USART_PARITY_NONE;
		break;
	case USB_CDC_ODD_PARITY:
		uart_parity = USART_PARITY_ODD;
		databits++;
		break;
	case USB_CDC_EVEN_PARITY:
		uart_parity = USART_PARITY_EVEN;
		databits++;
		break;
	default:
		return 0;
	}

	switch (cdc_stopbits) {
	case USB_CDC_1_STOP_BITS:
		uart_stopbits = USART_STOPBITS_1;
		break;
	case USB_CDC_2_STOP_BITS:
		uart_stopbits = USART_STOPBITS_2;
		break;
	default:
		return 0;
	}

	/* Disable the UART while we mess with its settings */
	usart_disable(USART2);
	/* Set communication parameters */
	usart_set_baudrate(USART2, baud);
	usart_set_databits(USART2, databits);
	usart_set_parity(USART2, uart_parity);
	usart_set_stopbits(USART2, uart_stopbits);
	/* Back to work. */
	usart_enable(USART2);

	return 1;
}