mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-06-06 16:58:48 +08:00
more stm32 porting
This commit is contained in:
@@ -55,22 +55,122 @@ test_uart.CFLAGS += -DUSE_UART3 -DUART3_BAUD=B57600
|
||||
test_uart.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
|
||||
|
||||
|
||||
#
|
||||
# test telemetry
|
||||
# test telemetry1
|
||||
#
|
||||
test_telemetry.ARCHDIR = $(ARCHI)
|
||||
test_telemetry.TARGET = test_telemetry
|
||||
test_telemetry.TARGETDIR = test_telemetry
|
||||
test_telemetry.CFLAGS = -I$(SRC_FYA) -I$(ARCHI) -DPERIPHERALS_AUTO_INIT
|
||||
test_telemetry.CFLAGS += -DCONFIG=$(BOARD_CFG)
|
||||
test_telemetry.srcs = $(SRC_FYA)/test_telemetry.c \
|
||||
$(SRC_FYA)/exceptions.c \
|
||||
test_telemetry1.ARCHDIR = $(ARCHI)
|
||||
test_telemetry1.TARGET = test_telemetry1
|
||||
test_telemetry1.TARGETDIR = test_telemetry1
|
||||
test_telemetry1.CFLAGS = -I$(SRC_FYA) -I$(ARCHI) -DPERIPHERALS_AUTO_INIT
|
||||
test_telemetry1.CFLAGS += -DCONFIG=$(BOARD_CFG)
|
||||
test_telemetry1.srcs = $(SRC_FYA)/test_telemetry.c \
|
||||
$(SRC_FYA)/exceptions.c \
|
||||
$(SRC_FYA)/vector_table.c
|
||||
test_telemetry.CFLAGS += -DUSE_LED
|
||||
test_telemetry.CFLAGS += -DUSE_SYS_TIME -DSYS_TIME_LED=1
|
||||
test_telemetry.CFLAGS += -DPERIODIC_TASK_PERIOD='SYS_TICS_OF_SEC(1./512.)'
|
||||
test_telemetry.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c
|
||||
test_telemetry.CFLAGS += -DUSE_UART3 -DUART3_BAUD=B57600
|
||||
test_telemetry.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
test_telemetry.CFLAGS += -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_DEVICE=Uart3
|
||||
test_telemetry.srcs += downlink.c pprz_transport.c
|
||||
test_telemetry1.CFLAGS += -DUSE_LED
|
||||
test_telemetry1.CFLAGS += -DUSE_SYS_TIME -DSYS_TIME_LED=1
|
||||
test_telemetry1.CFLAGS += -DPERIODIC_TASK_PERIOD='SYS_TICS_OF_SEC(1./512.)'
|
||||
test_telemetry1.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c
|
||||
test_telemetry1.CFLAGS += -DUSE_UART1 -DUART1_BAUD=B57600
|
||||
test_telemetry1.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
test_telemetry1.CFLAGS += -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_DEVICE=Uart1
|
||||
test_telemetry1.srcs += downlink.c pprz_transport.c
|
||||
|
||||
|
||||
#
|
||||
# test telemetry2
|
||||
#
|
||||
test_telemetry2.ARCHDIR = $(ARCHI)
|
||||
test_telemetry2.TARGET = test_telemetry2
|
||||
test_telemetry2.TARGETDIR = test_telemetry2
|
||||
test_telemetry2.CFLAGS = -I$(SRC_FYA) -I$(ARCHI) -DPERIPHERALS_AUTO_INIT
|
||||
test_telemetry2.CFLAGS += -DCONFIG=$(BOARD_CFG)
|
||||
test_telemetry2.srcs = $(SRC_FYA)/test_telemetry.c \
|
||||
$(SRC_FYA)/exceptions.c \
|
||||
$(SRC_FYA)/vector_table.c
|
||||
test_telemetry2.CFLAGS += -DUSE_LED
|
||||
test_telemetry2.CFLAGS += -DUSE_SYS_TIME -DSYS_TIME_LED=1
|
||||
test_telemetry2.CFLAGS += -DPERIODIC_TASK_PERIOD='SYS_TICS_OF_SEC(1./512.)'
|
||||
test_telemetry2.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c
|
||||
test_telemetry2.CFLAGS += -DUSE_UART2 -DUART2_BAUD=B57600
|
||||
test_telemetry2.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
test_telemetry2.CFLAGS += -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_DEVICE=Uart2
|
||||
test_telemetry2.srcs += downlink.c pprz_transport.c
|
||||
|
||||
|
||||
#
|
||||
# test telemetry3
|
||||
#
|
||||
test_telemetry3.ARCHDIR = $(ARCHI)
|
||||
test_telemetry3.TARGET = test_telemetry3
|
||||
test_telemetry3.TARGETDIR = test_telemetry3
|
||||
test_telemetry3.CFLAGS = -I$(SRC_FYA) -I$(ARCHI) -DPERIPHERALS_AUTO_INIT
|
||||
test_telemetry3.CFLAGS += -DCONFIG=$(BOARD_CFG)
|
||||
test_telemetry3.srcs = $(SRC_FYA)/test_telemetry.c \
|
||||
$(SRC_FYA)/exceptions.c \
|
||||
$(SRC_FYA)/vector_table.c
|
||||
test_telemetry3.CFLAGS += -DUSE_LED
|
||||
test_telemetry3.CFLAGS += -DUSE_SYS_TIME -DSYS_TIME_LED=1
|
||||
test_telemetry3.CFLAGS += -DPERIODIC_TASK_PERIOD='SYS_TICS_OF_SEC(1./512.)'
|
||||
test_telemetry3.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c
|
||||
test_telemetry3.CFLAGS += -DUSE_UART3 -DUART3_BAUD=B57600
|
||||
test_telemetry3.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
test_telemetry3.CFLAGS += -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_DEVICE=Uart3
|
||||
test_telemetry3.srcs += downlink.c pprz_transport.c
|
||||
|
||||
#
|
||||
# test datalink
|
||||
#
|
||||
test_datalink.ARCHDIR = $(ARCHI)
|
||||
test_datalink.TARGET = test_datalink
|
||||
test_datalink.TARGETDIR = test_datalink
|
||||
test_datalink.CFLAGS = -I$(SRC_FYA) -I$(ARCHI) -DPERIPHERALS_AUTO_INIT
|
||||
test_datalink.CFLAGS += -DCONFIG=$(BOARD_CFG)
|
||||
test_datalink.srcs = $(SRC_FYA)/test_datalink.c \
|
||||
$(SRC_FYA)/exceptions.c \
|
||||
$(SRC_FYA)/vector_table.c
|
||||
test_datalink.CFLAGS += -DUSE_LED
|
||||
test_datalink.CFLAGS += -DUSE_SYS_TIME -DSYS_TIME_LED=1
|
||||
test_datalink.CFLAGS += -DPERIODIC_TASK_PERIOD='SYS_TICS_OF_SEC(1./512.)'
|
||||
test_datalink.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c
|
||||
test_datalink.CFLAGS += -DUSE_UART1 -DUART1_BAUD=B57600
|
||||
test_datalink.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
test_datalink.CFLAGS += -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_DEVICE=Uart1
|
||||
test_datalink.srcs += downlink.c pprz_transport.c
|
||||
|
||||
test_datalink.CFLAGS += -DDATALINK=PPRZ -DPPRZ_UART=Uart1
|
||||
|
||||
|
||||
#
|
||||
# test rc 2.4
|
||||
#
|
||||
|
||||
SRC_BOOZ = booz
|
||||
SRC_BOOZ_TEST = $(SRC_BOOZ)/test
|
||||
|
||||
test_rc_24.ARCHDIR = $(ARCHI)
|
||||
test_rc_24.TARGET = test_rc_24
|
||||
test_rc_24.TARGETDIR = test_rc_24
|
||||
test_rc_24.CFLAGS += -I$(SRC_FYA) -I$(ARCHI) -I$(SRC_BOOZ) -DPERIPHERALS_AUTO_INIT
|
||||
test_rc_24.CFLAGS += -DCONFIG=$(BOARD_CFG)
|
||||
test_rc_24.srcs += $(SRC_BOOZ_TEST)/booz2_test_radio_control.c \
|
||||
$(SRC_FYA)/exceptions.c \
|
||||
$(SRC_FYA)/vector_table.c
|
||||
test_rc_24.CFLAGS += -DUSE_LED
|
||||
test_rc_24.CFLAGS += -DUSE_SYS_TIME -DTIME_LED=1
|
||||
test_rc_24.CFLAGS += -DPERIODIC_TASK_PERIOD='SYS_TICS_OF_SEC((1./512.))'
|
||||
test_rc_24.srcs += sys_time.c $(SRC_ARCH)/sys_time_hw.c
|
||||
|
||||
test_rc_24.CFLAGS += -DUSE_UART1 -DUART1_BAUD=B57600
|
||||
test_rc_24.srcs += $(SRC_ARCH)/uart_hw.c
|
||||
test_rc_24.CFLAGS += -DDOWNLINK -DDOWNLINK_TRANSPORT=PprzTransport -DDOWNLINK_DEVICE=Uart1
|
||||
test_rc_24.srcs += downlink.c pprz_transport.c
|
||||
|
||||
test_rc_24.CFLAGS += -DUSE_RADIO_CONTROL -DRADIO_CONTROL_LED=1
|
||||
test_rc_24.CFLAGS += -DRADIO_CONTROL_TYPE_H=\"booz_radio_control_spektrum.h\"
|
||||
test_rc_24.CFLAGS += -DRADIO_CONTROL_SPEKTRUM_MODEL_H=\"booz_radio_control_spektrum_dx7se.h\"
|
||||
test_rc_24.CFLAGS += -DUSE_UART2 -DUART2_BAUD=B115200
|
||||
test_rc_24.CFLAGS += -DRADIO_CONTROL_LINK=Uart2
|
||||
test_rc_24.srcs += $(SRC_BOOZ)/booz_radio_control.c \
|
||||
$(SRC_BOOZ)/booz_radio_control_spektrum.c \
|
||||
# $(SRC_ARCH)/uart_hw.c
|
||||
|
||||
@@ -28,6 +28,9 @@
|
||||
struct RadioControl radio_control;
|
||||
|
||||
void radio_control_init(void) {
|
||||
uint8_t i;
|
||||
for (i=0; i<RADIO_CONTROL_NB_CHANNEL; i++)
|
||||
radio_control.values[i] = 0;
|
||||
radio_control.status = RADIO_CONTROL_REALLY_LOST;
|
||||
radio_control.time_since_last_frame = RADIO_CONTROL_REALLY_LOST_TIME;
|
||||
radio_control.frame_rate = 0;
|
||||
|
||||
@@ -59,7 +59,7 @@ static inline void main_init( void ) {
|
||||
|
||||
static inline void main_periodic_task( void ) {
|
||||
|
||||
RunOnceEvery(51, {LED_TOGGLE(2); DOWNLINK_SEND_TIME(&cpu_time_sec);});
|
||||
RunOnceEvery(51, {/*LED_TOGGLE(2);*/ DOWNLINK_SEND_TIME(&cpu_time_sec);});
|
||||
|
||||
RunOnceEvery(10, {radio_control_periodic();});
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ EXTERN uint16_t datalink_time;
|
||||
#define MSG_SIZE 128
|
||||
EXTERN uint8_t dl_buffer[MSG_SIZE] __attribute__ ((aligned));
|
||||
|
||||
void dl_parse_msg(void);
|
||||
EXTERN void dl_parse_msg(void);
|
||||
/** Should be called when chars are available in dl_buffer */
|
||||
|
||||
#if DATALINK == PPRZ
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
|
||||
*
|
||||
* This file is part of paparazzi.
|
||||
*
|
||||
* paparazzi 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* paparazzi 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 paparazzi; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <stm32/rcc.h>
|
||||
#include <stm32/gpio.h>
|
||||
|
||||
#include <stm32/flash.h>
|
||||
#include <stm32/misc.h>
|
||||
|
||||
#define DATALINK_C
|
||||
|
||||
#include CONFIG
|
||||
#include "init_hw.h"
|
||||
#include "sys_time.h"
|
||||
#include "downlink.h"
|
||||
|
||||
#include "datalink.h"
|
||||
|
||||
static inline void main_init( void );
|
||||
static inline void main_periodic( void );
|
||||
static inline void main_event( void );
|
||||
|
||||
int main(void) {
|
||||
|
||||
main_init();
|
||||
|
||||
while (1) {
|
||||
if (sys_time_periodic())
|
||||
main_periodic();
|
||||
main_event();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void main_init( void ) {
|
||||
hw_init();
|
||||
sys_time_init();
|
||||
}
|
||||
|
||||
static inline void main_periodic( void ) {
|
||||
RunOnceEvery(10, {DOWNLINK_SEND_BOOT(&cpu_time_sec);});
|
||||
}
|
||||
|
||||
static inline void main_event( void ) {
|
||||
// DatalinkEvent();
|
||||
}
|
||||
|
||||
void dl_parse_msg(void) {
|
||||
uint8_t msg_id = dl_buffer[1];
|
||||
switch (msg_id) {
|
||||
|
||||
case DL_PING:
|
||||
{
|
||||
DOWNLINK_SEND_PONG();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ static inline void main_init( void ) {
|
||||
}
|
||||
|
||||
static inline void main_periodic( void ) {
|
||||
RunOnceEvery(10, {DOWNLINK_SEND_TIME(&cpu_time_sec);});
|
||||
RunOnceEvery(10, {DOWNLINK_SEND_BOOT(&cpu_time_sec);});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,6 +30,20 @@
|
||||
#define SYS_TICK_IRQ_HANDLER sys_tick_irq_handler
|
||||
#endif
|
||||
|
||||
#ifndef USE_UART1
|
||||
#define USART1_IRQ_HANDLER null_handler
|
||||
#else
|
||||
#include "uart.h"
|
||||
#define USART1_IRQ_HANDLER usart1_irq_handler
|
||||
#endif
|
||||
|
||||
#ifndef USE_UART2
|
||||
#define USART2_IRQ_HANDLER null_handler
|
||||
#else
|
||||
#include "uart.h"
|
||||
#define USART2_IRQ_HANDLER usart2_irq_handler
|
||||
#endif
|
||||
|
||||
#ifndef USE_UART3
|
||||
#define USART3_IRQ_HANDLER null_handler
|
||||
#else
|
||||
@@ -39,7 +53,6 @@
|
||||
|
||||
|
||||
|
||||
|
||||
/* addresses defined in the linker script */
|
||||
extern unsigned long _etext; /* end addr of .text section */
|
||||
extern unsigned long _sidata; /* init values for .data section */
|
||||
@@ -106,8 +119,8 @@ void (* const vector_table[])(void) = {
|
||||
null_handler, /* i2c2_er_irq_handler */
|
||||
null_handler, /* spi1_irq_handler */
|
||||
null_handler, /* spi2_irq_handler */
|
||||
null_handler, /* usart1_irq_handler */
|
||||
null_handler, /* usart2_irq_handler */
|
||||
USART1_IRQ_HANDLER, /* usart1_irq_handler */
|
||||
USART2_IRQ_HANDLER, /* usart2_irq_handler */
|
||||
USART3_IRQ_HANDLER, /* usart3_irq_handler */
|
||||
null_handler, /* exti15_10_irq_handler */
|
||||
null_handler, /* rtc_alarm_irq_handler */
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* $Id: interrupt_hw.h 859 2006-05-03 21:32:23Z poine $
|
||||
*
|
||||
* Copyright (C) 2005 Pascal Brisset, Antoine Drouin
|
||||
*
|
||||
* This file is part of paparazzi.
|
||||
*
|
||||
* paparazzi 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* paparazzi 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 paparazzi; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
/** \file interrupt_hw.h
|
||||
* \brief STM32 Low level interrupt handling
|
||||
* Empty for now, just to match what we have on LPC
|
||||
*/
|
||||
|
||||
#ifndef INTERRUPT_HW_H
|
||||
#define INTERRUPT_HW_H
|
||||
|
||||
#define int_enable() {}
|
||||
#define int_disable() {}
|
||||
|
||||
#endif /* INTERRUPT_HW_H */
|
||||
|
||||
@@ -34,6 +34,8 @@ void sys_time_init( void ) {
|
||||
/* Set SysTick handler */
|
||||
NVIC_SetPriority(SysTick_IRQn, 0x0);
|
||||
sys_time_period_elapsed = FALSE;
|
||||
|
||||
cpu_time_sec = 0;
|
||||
cpu_time_ticks = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,253 @@
|
||||
#include <stm32/gpio.h>
|
||||
#include "std.h"
|
||||
|
||||
|
||||
|
||||
#ifdef USE_UART1
|
||||
|
||||
uint16_t uart1_rx_insert_idx, uart1_rx_extract_idx;
|
||||
uint8_t uart1_rx_buffer[UART1_RX_BUFFER_SIZE];
|
||||
uint8_t uart1_tx_buffer[UART1_TX_BUFFER_SIZE];
|
||||
volatile uint16_t uart1_tx_insert_idx, uart1_tx_extract_idx;
|
||||
volatile bool_t uart1_tx_running;
|
||||
|
||||
|
||||
void uart1_init( void ) {
|
||||
/* init RCC */
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
|
||||
/* Enable USART1 interrupts */
|
||||
NVIC_InitTypeDef nvic;
|
||||
nvic.NVIC_IRQChannel = USART1_IRQn;
|
||||
nvic.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic);
|
||||
|
||||
/* Init GPIOS */
|
||||
GPIO_InitTypeDef gpio;
|
||||
/* GPIOA: USART1 Tx push-pull */
|
||||
gpio.GPIO_Pin = GPIO_Pin_9;
|
||||
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
gpio.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &gpio);
|
||||
/* GPIOA: USART1 Rx pin as floating input */
|
||||
gpio.GPIO_Pin = GPIO_Pin_10;
|
||||
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
GPIO_Init(GPIOA, &gpio);
|
||||
|
||||
/* Configure USART1 */
|
||||
USART_InitTypeDef usart;
|
||||
usart.USART_BaudRate = UART1_BAUD;
|
||||
usart.USART_WordLength = USART_WordLength_8b;
|
||||
usart.USART_StopBits = USART_StopBits_1;
|
||||
usart.USART_Parity = USART_Parity_No;
|
||||
usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
||||
USART_Init(USART1, &usart);
|
||||
/* Enable USART1 Receive interrupts */
|
||||
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
|
||||
/* Enable the USART1 */
|
||||
USART_Cmd(USART1, ENABLE);
|
||||
|
||||
// initialize the transmit data queue
|
||||
uart1_tx_extract_idx = 0;
|
||||
uart1_tx_insert_idx = 0;
|
||||
uart1_tx_running = FALSE;
|
||||
|
||||
// initialize the receive data queue
|
||||
uart1_rx_extract_idx = 0;
|
||||
uart1_rx_insert_idx = 0;
|
||||
|
||||
}
|
||||
|
||||
void uart1_transmit( uint8_t data ) {
|
||||
|
||||
uint16_t temp = (uart1_tx_insert_idx + 1) % UART1_TX_BUFFER_SIZE;
|
||||
|
||||
if (temp == uart1_tx_extract_idx)
|
||||
return; // no room
|
||||
|
||||
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
|
||||
|
||||
// check if in process of sending data
|
||||
if (uart1_tx_running) { // yes, add to queue
|
||||
uart1_tx_buffer[uart1_tx_insert_idx] = data;
|
||||
uart1_tx_insert_idx = temp;
|
||||
}
|
||||
else { // no, set running flag and write to output register
|
||||
uart1_tx_running = TRUE;
|
||||
USART_SendData(USART1, data);
|
||||
}
|
||||
|
||||
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
|
||||
|
||||
}
|
||||
|
||||
bool_t uart1_check_free_space( uint8_t len) {
|
||||
int16_t space = uart1_tx_extract_idx - uart1_tx_insert_idx;
|
||||
if (space <= 0)
|
||||
space += UART1_TX_BUFFER_SIZE;
|
||||
return (uint16_t)(space - 1) >= len;
|
||||
}
|
||||
|
||||
|
||||
void usart1_irq_handler(void) {
|
||||
|
||||
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET){
|
||||
// check if more data to send
|
||||
if (uart1_tx_insert_idx != uart1_tx_extract_idx) {
|
||||
USART_SendData(USART1,uart1_tx_buffer[uart1_tx_extract_idx]);
|
||||
uart1_tx_extract_idx++;
|
||||
uart1_tx_extract_idx %= UART1_TX_BUFFER_SIZE;
|
||||
}
|
||||
else {
|
||||
uart1_tx_running = 0; // clear running flag
|
||||
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
|
||||
}
|
||||
}
|
||||
|
||||
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
|
||||
uint16_t temp = (uart1_rx_insert_idx + 1) % UART1_RX_BUFFER_SIZE;;
|
||||
uart1_rx_buffer[uart1_rx_insert_idx] = USART_ReceiveData(USART1);
|
||||
// check for more room in queue
|
||||
if (temp != uart1_rx_extract_idx)
|
||||
uart1_rx_insert_idx = temp; // update insert index
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif /* USE_UART1 */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef USE_UART2
|
||||
|
||||
uint16_t uart2_rx_insert_idx, uart2_rx_extract_idx;
|
||||
uint8_t uart2_rx_buffer[UART2_RX_BUFFER_SIZE];
|
||||
uint8_t uart2_tx_buffer[UART2_TX_BUFFER_SIZE];
|
||||
volatile uint16_t uart2_tx_insert_idx, uart2_tx_extract_idx;
|
||||
volatile bool_t uart2_tx_running;
|
||||
|
||||
|
||||
void uart2_init( void ) {
|
||||
/* init RCC */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
|
||||
/* Enable USART2 interrupts */
|
||||
NVIC_InitTypeDef nvic;
|
||||
nvic.NVIC_IRQChannel = USART2_IRQn;
|
||||
nvic.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic);
|
||||
|
||||
/* Init GPIOS */
|
||||
GPIO_InitTypeDef gpio;
|
||||
/* GPIOA: USART2 Tx push-pull */
|
||||
gpio.GPIO_Pin = GPIO_Pin_2;
|
||||
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
gpio.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &gpio);
|
||||
/* GPIOA: USART2 Rx pin as floating input */
|
||||
gpio.GPIO_Pin = GPIO_Pin_3;
|
||||
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
GPIO_Init(GPIOA, &gpio);
|
||||
|
||||
/* Configure USART2 */
|
||||
USART_InitTypeDef usart;
|
||||
usart.USART_BaudRate = UART2_BAUD;
|
||||
usart.USART_WordLength = USART_WordLength_8b;
|
||||
usart.USART_StopBits = USART_StopBits_1;
|
||||
usart.USART_Parity = USART_Parity_No;
|
||||
usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
||||
USART_Init(USART2, &usart);
|
||||
/* Enable USART2 Receive interrupts */
|
||||
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
||||
/* Enable the USART2 */
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
|
||||
// initialize the transmit data queue
|
||||
uart2_tx_extract_idx = 0;
|
||||
uart2_tx_insert_idx = 0;
|
||||
uart2_tx_running = FALSE;
|
||||
|
||||
// initialize the receive data queue
|
||||
uart2_rx_extract_idx = 0;
|
||||
uart2_rx_insert_idx = 0;
|
||||
|
||||
}
|
||||
|
||||
void uart2_transmit( uint8_t data ) {
|
||||
|
||||
uint16_t temp = (uart2_tx_insert_idx + 1) % UART2_TX_BUFFER_SIZE;
|
||||
|
||||
if (temp == uart2_tx_extract_idx)
|
||||
return; // no room
|
||||
|
||||
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
|
||||
|
||||
// check if in process of sending data
|
||||
if (uart2_tx_running) { // yes, add to queue
|
||||
uart2_tx_buffer[uart2_tx_insert_idx] = data;
|
||||
uart2_tx_insert_idx = temp;
|
||||
}
|
||||
else { // no, set running flag and write to output register
|
||||
uart2_tx_running = TRUE;
|
||||
USART_SendData(USART2, data);
|
||||
}
|
||||
|
||||
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
|
||||
|
||||
}
|
||||
|
||||
bool_t uart2_check_free_space( uint8_t len) {
|
||||
int16_t space = uart2_tx_extract_idx - uart2_tx_insert_idx;
|
||||
if (space <= 0)
|
||||
space += UART2_TX_BUFFER_SIZE;
|
||||
return (uint16_t)(space - 1) >= len;
|
||||
}
|
||||
|
||||
|
||||
void usart2_irq_handler(void) {
|
||||
|
||||
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET){
|
||||
// check if more data to send
|
||||
if (uart2_tx_insert_idx != uart2_tx_extract_idx) {
|
||||
USART_SendData(USART2,uart2_tx_buffer[uart2_tx_extract_idx]);
|
||||
uart2_tx_extract_idx++;
|
||||
uart2_tx_extract_idx %= UART2_TX_BUFFER_SIZE;
|
||||
}
|
||||
else {
|
||||
uart2_tx_running = 0; // clear running flag
|
||||
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
|
||||
}
|
||||
}
|
||||
|
||||
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){
|
||||
uint16_t temp = (uart2_rx_insert_idx + 1) % UART2_RX_BUFFER_SIZE;;
|
||||
uart2_rx_buffer[uart2_rx_insert_idx] = USART_ReceiveData(USART2);
|
||||
// check for more room in queue
|
||||
if (temp != uart2_rx_extract_idx)
|
||||
uart2_rx_insert_idx = temp; // update insert index
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif /* USE_UART2 */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef USE_UART3
|
||||
|
||||
uint16_t uart3_rx_insert_idx, uart3_rx_extract_idx;
|
||||
|
||||
@@ -31,10 +31,59 @@
|
||||
|
||||
#include "std.h"
|
||||
|
||||
#define B38400 38400
|
||||
#define B57600 57600
|
||||
#define B38400 38400
|
||||
#define B57600 57600
|
||||
#define B115200 115200
|
||||
|
||||
|
||||
#ifdef USE_UART1
|
||||
|
||||
#define UART1_RX_BUFFER_SIZE 128
|
||||
#define UART1_TX_BUFFER_SIZE 128
|
||||
|
||||
extern uint16_t uart1_rx_insert_idx, uart1_rx_extract_idx;
|
||||
extern uint8_t uart1_rx_buffer[UART1_RX_BUFFER_SIZE];
|
||||
extern uint8_t uart1_tx_buffer[UART1_TX_BUFFER_SIZE];
|
||||
extern volatile uint16_t uart1_tx_insert_idx, uart1_tx_extract_idx;
|
||||
extern volatile bool_t uart1_tx_running;
|
||||
|
||||
extern void usart1_irq_handler(void);
|
||||
|
||||
#define Uart1ChAvailable() (uart1_rx_insert_idx != uart1_rx_extract_idx)
|
||||
#define Uart1Getch() ({ \
|
||||
uint8_t ret = uart1_rx_buffer[uart1_rx_extract_idx]; \
|
||||
uart1_rx_extract_idx = (uart1_rx_extract_idx + 1)%UART1_RX_BUFFER_SIZE; \
|
||||
ret; \
|
||||
})
|
||||
|
||||
#endif /* USE_UART1 */
|
||||
|
||||
|
||||
#ifdef USE_UART2
|
||||
|
||||
#define UART2_RX_BUFFER_SIZE 128
|
||||
#define UART2_TX_BUFFER_SIZE 128
|
||||
|
||||
extern uint16_t uart2_rx_insert_idx, uart2_rx_extract_idx;
|
||||
extern uint8_t uart2_rx_buffer[UART2_RX_BUFFER_SIZE];
|
||||
extern uint8_t uart2_tx_buffer[UART2_TX_BUFFER_SIZE];
|
||||
extern volatile uint16_t uart2_tx_insert_idx, uart2_tx_extract_idx;
|
||||
extern volatile bool_t uart2_tx_running;
|
||||
|
||||
extern void usart2_irq_handler(void);
|
||||
|
||||
#define Uart2ChAvailable() (uart2_rx_insert_idx != uart2_rx_extract_idx)
|
||||
#define Uart2Getch() ({ \
|
||||
uint8_t ret = uart2_rx_buffer[uart2_rx_extract_idx]; \
|
||||
uart2_rx_extract_idx = (uart2_rx_extract_idx + 1)%UART2_RX_BUFFER_SIZE; \
|
||||
ret; \
|
||||
})
|
||||
|
||||
#endif /* USE_UART2 */
|
||||
|
||||
|
||||
#ifdef USE_UART3
|
||||
|
||||
#define UART3_RX_BUFFER_SIZE 128
|
||||
#define UART3_TX_BUFFER_SIZE 128
|
||||
|
||||
@@ -45,6 +94,7 @@ extern volatile uint16_t uart3_tx_insert_idx, uart3_tx_extract_idx;
|
||||
extern volatile bool_t uart3_tx_running;
|
||||
|
||||
extern void usart3_irq_handler(void);
|
||||
|
||||
#endif /* USE_UART3 */
|
||||
|
||||
#endif /* UART_HW_H */
|
||||
|
||||
Reference in New Issue
Block a user