aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2016-01-21 21:31:23 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2016-01-21 21:31:23 +0100
commite787d8d252f612a259c697ad9d413ca63fb0b387 (patch)
treedcb01660121929e4055110a18ffe71eea948eca8 /src
parent9c7d2bf918da26d65a06f0c6b58f5626ee3d8136 (diff)
downloadglutte-o-matic-e787d8d252f612a259c697ad9d413ca63fb0b387.tar.gz
glutte-o-matic-e787d8d252f612a259c697ad9d413ca63fb0b387.tar.bz2
glutte-o-matic-e787d8d252f612a259c697ad9d413ca63fb0b387.zip
Initialise USART1 for PC debug
Diffstat (limited to 'src')
-rw-r--r--src/fsm/pio.txt5
-rw-r--r--src/fsm/usart.c83
-rw-r--r--src/fsm/usart.h17
3 files changed, 95 insertions, 10 deletions
diff --git a/src/fsm/pio.txt b/src/fsm/pio.txt
index eabc16f..a95fc67 100644
--- a/src/fsm/pio.txt
+++ b/src/fsm/pio.txt
@@ -10,6 +10,11 @@ u-blox NEO-M8N GPS module connection
Yellow out UART TX to GPS RX PD8
Orange in UART RX to GPS TX PD9
+Debug USART
+-----------
+- out UART TX to PC RX PA9
+- in UART RX to PC TX PA10
+
For the little test board with the 3 LEDs and switches
------------------------------------------------------
Blue in QRP_n PC1
diff --git a/src/fsm/usart.c b/src/fsm/usart.c
index c606814..98d49eb 100644
--- a/src/fsm/usart.c
+++ b/src/fsm/usart.c
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
- * Copyright (c) 2015 Matthias P. Braendli
+ * Copyright (c) 2016 Matthias P. Braendli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -22,6 +22,9 @@
* SOFTWARE.
*/
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
#include "common.h"
#include "usart.h"
#include <stm32f4xx.h>
@@ -36,6 +39,10 @@
const uint16_t GPIOD_PIN_USART3_TX = GPIO_Pin_8;
const uint16_t GPIOD_PIN_USART3_RX = GPIO_Pin_9;
+/* USART 3 on PA9 and PA10 */
+const uint16_t GPIOA_PIN_USART1_TX = GPIO_Pin_10;
+const uint16_t GPIOA_PIN_USART1_RX = GPIO_Pin_9;
+
// The ISR writes into this buffer
static char nmea_sentence[MAX_NMEA_SENTENCE_LEN];
static int nmea_sentence_last_written = 0;
@@ -51,11 +58,53 @@ void usart_init()
while(1); /* fatal error */
}
+ GPIO_InitTypeDef GPIO_InitStruct;
+ USART_InitTypeDef USART_InitStruct;
+ NVIC_InitTypeDef NVIC_InitStructure;
+
+ // ============== PC DEBUG USART ===========
+ RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
+
+ GPIO_InitStruct.GPIO_Pin = GPIOA_PIN_USART1_RX | GPIOA_PIN_USART1_TX;
+ GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
+ GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
+ GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
+ GPIO_Init(GPIOA, &GPIO_InitStruct);
+
+ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
+ GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
+
+ // Setup USART1 for 9600,8,N,1
+ USART_InitStruct.USART_BaudRate = 9600;
+ USART_InitStruct.USART_WordLength = USART_WordLength_8b;
+ USART_InitStruct.USART_StopBits = USART_StopBits_1;
+ USART_InitStruct.USART_Parity = USART_Parity_No;
+ USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
+ USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
+ USART_Init(USART1, &USART_InitStruct);
+
+
+ // enable the USART1 receive interrupt
+ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
+
+ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
+ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
+ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
+ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
+ NVIC_Init(&NVIC_InitStructure);
+
+ NVIC_SetPriority(USART1_IRQn, 6);
+
+ // finally this enables the complete USART1 peripheral
+ USART_Cmd(USART1, ENABLE);
+
+ // ============== GPS USART ===========
// Setup GPIO D and connect to USART 3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIOD_PIN_USART3_RX | GPIOD_PIN_USART3_TX;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
@@ -68,7 +117,6 @@ void usart_init()
// Setup USART3 for 9600,8,N,1
- USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
@@ -81,7 +129,6 @@ void usart_init()
// enable the USART3 receive interrupt
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
@@ -94,16 +141,38 @@ void usart_init()
USART_Cmd(USART3, ENABLE);
}
-void usart_puts(const char* str)
+static void usart_puts(USART_TypeDef* USART, const char* str)
{
while(*str) {
// wait until data register is empty
- while ( !(USART3->SR & 0x00000040) );
- USART_SendData(USART3, *str);
+ while ( !(USART->SR & 0x00000040) );
+ USART_SendData(USART, *str);
str++;
}
}
+void usart_gps_puts(const char* str)
+{
+ return usart_puts(USART3, str);
+}
+
+#define MAX_MSG_LEN 80
+static char usart_debug_message[MAX_MSG_LEN];
+
+void usart_debug(const char *format, ...)
+{
+ va_list list;
+ va_start(list, format);
+ vsnprintf(usart_debug_message, MAX_MSG_LEN-1, format, list);
+ usart_puts(USART1, usart_debug_message);
+ va_end(list);
+}
+
+void usart_debug_puts(const char* str)
+{
+ usart_puts(USART1, str);
+}
+
int usart_get_nmea_sentence(char* nmea)
{
return xQueueReceive(usart_nmea_queue, nmea, portMAX_DELAY);
diff --git a/src/fsm/usart.h b/src/fsm/usart.h
index 4ab8b22..0a8c12e 100644
--- a/src/fsm/usart.h
+++ b/src/fsm/usart.h
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
- * Copyright (c) 2015 Matthias P. Braendli
+ * Copyright (c) 2016 Matthias P. Braendli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -22,8 +22,10 @@
* SOFTWARE.
*/
-/* This handles the USART to the GPS receiver, and fills a queue of
+/* This handles the USART 3 to the GPS receiver, and fills a queue of
* NMEA messages.
+ *
+ * It also handles the debug USART 1 and allows sending messages to the PC.
*/
#ifndef __USART_H_
@@ -31,11 +33,20 @@
#define MAX_NMEA_SENTENCE_LEN 256
+// Initialise both USART1 for PC and USART3 for GPS
void usart_init(void);
-void usart_puts(const char* str);
+// Send the str to the GPS receiver
+void usart_gps_puts(const char* str);
+
+// a printf to send data to the PC
+void usart_debug(const char *format, ...);
+
+// Send a string to the PC
+void usart_debug_puts(const char* str);
// Get a MAX_NMEA_SENTENCE_LEN sized NMEA sentence
+// Return 1 on success
int usart_get_nmea_sentence(char* nmea);
#endif //__USART_H_