summaryrefslogtreecommitdiff
path: root/tests/usb-serial-rs485/main-stm32f4-disco.c
blob: f5c21bf4c8108765d193da38c6506f09c517a492 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * 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/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/usart.h>

#include <stdio.h>
#include "syscfg.h"
#include "usb_cdcacm.h"
#include "ringb.h"
#include "trace.h"

#define ER_DEBUG
#ifdef ER_DEBUG
#define ER_DPRINTF(fmt, ...) \
    do { printf(fmt, ## __VA_ARGS__); } while (0)
#else
#define ER_DPRINTF(fmt, ...) \
    do { } while (0)
#endif


extern struct ringb rx_ring, tx_ring;
static void usart_setup(void)
{
	/* Enable the USART2 interrupt. */
	nvic_enable_irq(NVIC_USART2_IRQ);

	/* USART2 pins are on port A */
	rcc_periph_clock_enable(RCC_GPIOA);
	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2 | GPIO3);
	gpio_set_af(GPIOA, GPIO_AF7, GPIO2 | GPIO3);

	/* Enable clocks for USART2. */
	rcc_periph_clock_enable(RCC_USART2);

	/* Setup USART2 parameters. */
	usart_set_baudrate(USART2, 115200);
	usart_set_databits(USART2, 8);
	usart_set_stopbits(USART2, USART_STOPBITS_1);
	usart_set_mode(USART2, USART_MODE_TX_RX);
	usart_set_parity(USART2, USART_PARITY_NONE);
	usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);

	/* Enable USART2 Receive interrupt. */
	usart_enable_rx_interrupt(USART2);

	/* Finally enable the USART. */
	usart_enable(USART2);
}

void usart2_isr(void)
{
	// usbser-rxne()
	/* Check if we were called because of RXNE. */
	if (usart_get_interrupt_source(USART2, USART_SR_RXNE)) {
		gpio_set(LED_RX_PORT, LED_RX_PIN);
		uint8_t c = usart_recv(USART2);
		if (ringb_put(&rx_ring, c)) {
			// good,
		} else {
			// fatal, you should always have drained by now.
			// (when you've got it all ironed out, _actually_
			// just drop and count drops), but not yet...
			ER_DPRINTF("rx buffer full\n");
			while(1);
		}
		gpio_clear(LED_RX_PORT, LED_RX_PIN);
	}
	// usbser-irq-txe()
	if (usart_get_interrupt_source(USART2, USART_SR_TXE)) {
		if (ringb_depth(&tx_ring) == 0) {
			// turn off tx empty interrupts, nothing left to send
			usart_disable_tx_interrupt(USART2);
			ER_DPRINTF("OFF\n");
			// Turn on tx complete interrupts, for rs485 de
//			USART_CR1(USART2) |= ~USART_CR1_TCIE;
		} else {
			int c = ringb_get(&tx_ring);
			usart_send(USART2, c);
		}
	}
	// usbser-irq-txc?  rs485 is auto on some devices, but can be emulated anyway
//	if (usart_get_interrupt_source(USART2, USART_SR_TC)) {
//		ER_DPRINTF("TC");
//		// turn off the complete irqs, we're done now.
//		USART_SR(USART2) &= ~USART_SR_TC;
//		USART_CR1(USART2) &= ~USART_CR1_TCIE;
//		gpio_clear(LED_TX_PORT, LED_TX_PIN);
//		gpio_clear(RS485DE_PORT, RS485DE_PIN);
//	}
}

void usb_cdcacm_setup_pre_arch(void)
{
	rcc_periph_clock_enable(RCC_GPIOA);
	rcc_periph_clock_enable(RCC_OTGFS);

	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,
		GPIO9 | GPIO11 | GPIO12);
	gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12);
	
}

void usb_cdcacm_setup_post_arch(usbd_device *dev)
{
}


int main(void)
{
	rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
	ER_DPRINTF("And we're alive!\n");
	/* Leds and rs485 are on port D */
	rcc_periph_clock_enable(RCC_GPIOD);
	gpio_mode_setup(LED_RX_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
		LED_RX_PIN | LED_TX_PIN);
	gpio_mode_setup(RS485DE_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
		RS485DE_PIN);

	

	usart_setup();
	usb_cdcacm_setup_pre_arch();
	usbd_device *usbd_dev = usb_cdcacm_init(&otgfs_usb_driver, "stm32f4-disco");
	usb_cdcacm_setup_post_arch(usbd_dev);

	ER_DPRINTF("Looping...\n");
	volatile int i = 0;
	while (1) {
		usbd_poll(usbd_dev);
		usb_cdcacm_poll(usbd_dev);
	}

}