AMTS/Mieke/SW/MT/interface_uart.c

190 lines
5.8 KiB
C
Raw Normal View History

2018-04-03 15:32:24 +00:00
/*
Manufacturing tests for the new cortex minimal system
Copyright (C) 2018 Andreas Mieke
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 <https://www.gnu.org/licenses/>.
*/
2018-03-18 22:43:59 +00:00
#include "interface_uart.h"
void USART_SendString(USART_TypeDef *USARTx, char *str)
{
2018-04-03 15:18:28 +00:00
// Sends a string, character for character, over the specified UART
2018-03-18 22:43:59 +00:00
while (*str) {
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *str++);
}
}
void init_all(void)
{
2018-04-03 15:18:28 +00:00
// Enable all GPIO and USART clocks needed for USART1, 2 and 3
2018-03-18 22:43:59 +00:00
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
2018-04-03 15:18:28 +00:00
// Create gpio struct and fill it with defaults
2018-03-18 22:43:59 +00:00
GPIO_InitTypeDef gpio;
GPIO_StructInit(&gpio);
2018-04-03 15:18:28 +00:00
// Set PA9 to alternate function push pull (Tx)
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA, &gpio);
2018-04-03 15:18:28 +00:00
// Same for PA2
2018-03-18 22:43:59 +00:00
gpio.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &gpio);
2018-04-03 15:18:28 +00:00
// Same for PB10
2018-03-18 22:43:59 +00:00
gpio.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &gpio);
2018-04-03 15:18:28 +00:00
// Set PA10 to input floating (Rx)
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &gpio);
2018-04-03 15:18:28 +00:00
// Same for PA3
2018-03-18 22:43:59 +00:00
gpio.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &gpio);
2018-04-03 15:18:28 +00:00
// Same for PB11
2018-03-18 22:43:59 +00:00
gpio.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &gpio);
2018-04-03 15:18:28 +00:00
// Create usart struct and set baud rate to 115 200, init all three USARTs with this baud rate
2018-03-18 22:43:59 +00:00
USART_InitTypeDef usart;
USART_StructInit(&usart);
usart.USART_BaudRate = 115200;
USART_Init(USART1, &usart);
USART_Init(USART2, &usart);
USART_Init(USART3, &usart);
2018-04-03 15:18:28 +00:00
// Enable USART clocks
2018-03-18 22:43:59 +00:00
USART_ClockInitTypeDef usartclock;
USART_ClockStructInit(&usartclock);
USART_ClockInit(USART1, &usartclock);
USART_ClockInit(USART2, &usartclock);
USART_ClockInit(USART3, &usartclock);
2018-04-03 15:18:28 +00:00
// Enable the USARTs
2018-03-18 22:43:59 +00:00
USART_Cmd(USART1, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_Cmd(USART3, ENABLE);
2018-04-03 15:18:28 +00:00
// Enable the SysTick with T = 1 ms
2018-03-18 22:43:59 +00:00
RCC_ClocksTypeDef clocks;
RCC_GetClocksFreq(&clocks);
SysTick_Config(clocks.HCLK_Frequency/1000 - 1); // SysTick T=1ms
}
void send_welcome(void)
{
2018-04-03 15:18:28 +00:00
// Send a welcome message to all three USARTs
2018-03-18 22:43:59 +00:00
USART_SendString(USART1, "\x1B[2J\x1B[0;0HManufacturing test software, press space...\r\n");
USART_SendString(USART2, "\x1B[2J\x1B[0;0HManufacturing test software, press space...\r\n");
USART_SendString(USART3, "\x1B[2J\x1B[0;0HManufacturing test software, press space...\r\n");
}
unsigned int wait_for_start(void)
{
uint8_t data;
2018-04-03 15:18:28 +00:00
// Runs until space has been pressed on one UART
2018-03-18 22:43:59 +00:00
for(;;) {
2018-04-03 15:18:28 +00:00
// Checks if USART1 receive register is not empty
2018-03-18 22:43:59 +00:00
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) {
data = USART_ReceiveData(USART1);
2018-04-03 15:18:28 +00:00
// Checks if space has been pressed
2018-03-18 22:43:59 +00:00
if (data == ' ') {
2018-04-03 15:18:28 +00:00
// Disable the other UARTs and return 1 as this is the used UART
2018-03-18 22:43:59 +00:00
USART_Cmd(USART2, DISABLE);
USART_Cmd(USART3, DISABLE);
USART_DeInit(USART2);
USART_DeInit(USART3);
return 1;
} else {
USART_Cmd(USART1, DISABLE);
USART_DeInit(USART1);
}
}
2018-04-03 15:18:28 +00:00
// Checks if USART2 receive register is not empty
2018-03-18 22:43:59 +00:00
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET) {
data = USART_ReceiveData(USART2);
2018-04-03 15:18:28 +00:00
// Checks if space has been pressed
2018-03-18 22:43:59 +00:00
if (data == ' ') {
2018-04-03 15:18:28 +00:00
// Disable the other UARTs and return 2 as this is the used UART
2018-03-18 22:43:59 +00:00
USART_Cmd(USART1, DISABLE);
USART_Cmd(USART3, DISABLE);
USART_DeInit(USART1);
USART_DeInit(USART3);
return 2;
} else {
USART_Cmd(USART2, DISABLE);
USART_DeInit(USART2);
}
}
2018-04-03 15:18:28 +00:00
// Checks if USART3 receive register is not empty
2018-03-18 22:43:59 +00:00
if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == SET) {
data = USART_ReceiveData(USART3);
2018-04-03 15:18:28 +00:00
// Checks if space has been pressed
2018-03-18 22:43:59 +00:00
if (data == ' ') {
2018-04-03 15:18:28 +00:00
// Disable the other UARTs and return 3 as this is the used UART
2018-03-18 22:43:59 +00:00
USART_Cmd(USART1, DISABLE);
USART_Cmd(USART2, DISABLE);
USART_DeInit(USART1);
USART_DeInit(USART2);
return 3;
} else {
USART_Cmd(USART3, DISABLE);
USART_DeInit(USART3);
}
}
}
}
unsigned int wait_for_test(USART_TypeDef *USARTx)
{
int data;
2018-04-03 15:18:28 +00:00
// Blocks until something is pressed
2018-03-18 22:43:59 +00:00
for(;;)
{
2018-04-03 15:18:28 +00:00
// Checks if receive register is not empty
2018-03-18 22:43:59 +00:00
if (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == SET) {
data = (int)USART_ReceiveData(USARTx);
2018-04-03 15:18:28 +00:00
// If data is out of range return 1 (no test)
if ((data > 0x09 && data <= 0x30) || data > 0x39) return 1;
// Else return the correct test id
2018-03-18 22:43:59 +00:00
return (data >= 0x30) ? data - 0x30 : data;
}
}
}
unsigned int get_test(USART_TypeDef *USARTx)
{
int data;
2018-04-03 15:18:28 +00:00
// Checks if receive register is not empty
2018-03-18 22:43:59 +00:00
if (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == SET) {
data = (int)USART_ReceiveData(USARTx);
2018-04-03 15:18:28 +00:00
// If data is out of range return 1 (no test)
if ((data > 0x09 && data <= 0x30) || data > 0x39) return 1;
// Else return the correct test id
2018-03-18 22:43:59 +00:00
return (data >= 0x30) ? data - 0x30 : data;
}
2018-04-03 15:18:28 +00:00
// If nothing has been sent to the UART, return 0 (test not changed)
2018-03-18 22:43:59 +00:00
return 0;
}