AMTS/Mieke/SW/ODD/io.c

133 lines
3.2 KiB
C
Raw Normal View History

2018-03-18 22:43:59 +00:00
#include "io.h"
uint8_t i2c1_inited = 0;
uint8_t usart1_inited = 0;
uint8_t usart3_inited = 0;
void i2c1_init(void)
{
2018-04-03 15:18:28 +00:00
// If I2C1 is already inited, do nothing
2018-03-18 22:43:59 +00:00
if (i2c1_inited == 1) {
return;
}
2018-04-03 15:18:28 +00:00
// Enable GPIOB and I2C1 clocks
2018-03-18 22:43:59 +00:00
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
2018-04-03 15:18:28 +00:00
// Create gpio struct and fill default values
2018-03-18 22:43:59 +00:00
GPIO_InitTypeDef gpio;
GPIO_StructInit(&gpio);
2018-04-03 15:18:28 +00:00
// Set PB6 to alternate function push pull (SCL)
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &gpio);
2018-04-03 15:18:28 +00:00
// Set PB7 to alternate function open drain (SDA)
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_AF_OD;
gpio.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &gpio);
2018-04-03 15:18:28 +00:00
// Set I2C1 clock to 400 kHz
2018-03-18 22:43:59 +00:00
I2C_InitTypeDef i2c;
I2C_StructInit(&i2c);
i2c.I2C_ClockSpeed = 400000;
I2C_Init(I2C1, &i2c);
2018-04-03 15:18:28 +00:00
// Enable I2C1
2018-03-18 22:43:59 +00:00
I2C_Cmd(I2C1, ENABLE);
i2c1_inited = 1;
}
void usart1_init(void)
{
2018-04-03 15:18:28 +00:00
// If USART1 is inited, do nothing
2018-03-18 22:43:59 +00:00
if (usart1_inited == 1) {
return;
}
2018-04-03 15:18:28 +00:00
// Enable GPIOA and USART1 clocks
2018-03-18 22:43:59 +00:00
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
2018-04-03 15:18:28 +00:00
// Create gpio struct and fill default values
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 (TxD)
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
// Set PA10 to input floating (RxD)
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
// Set USART1 clock to 115 200 baud
2018-03-18 22:43:59 +00:00
USART_InitTypeDef usart;
USART_StructInit(&usart);
usart.USART_BaudRate = 115200;
USART_Init(USART1, &usart);
2018-04-03 15:18:28 +00:00
// Init USART1 clocks
2018-03-18 22:43:59 +00:00
USART_ClockInitTypeDef usartclock;
USART_ClockStructInit(&usartclock);
USART_ClockInit(USART1, &usartclock);
2018-04-03 15:18:28 +00:00
// Enable USART1
2018-03-18 22:43:59 +00:00
USART_Cmd(USART1, ENABLE);
usart1_inited = 1;
}
void usart3_init(void)
{
2018-04-03 15:18:28 +00:00
// If USART3 is inited, do nothing
2018-03-18 22:43:59 +00:00
if (usart3_inited == 1) {
return;
}
2018-04-03 15:18:28 +00:00
// Enable GPIOB and USART3 clocks
2018-03-18 22:43:59 +00:00
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
2018-04-03 15:18:28 +00:00
// Create gpio struct and fill default values
2018-03-18 22:43:59 +00:00
GPIO_InitTypeDef gpio;
GPIO_StructInit(&gpio);
2018-04-03 15:18:28 +00:00
// Set PB10 to alternate function push pull (TxD)
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &gpio);
2018-04-03 15:18:28 +00:00
// Set PB11 to input floating (RxD)
2018-03-18 22:43:59 +00:00
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &gpio);
2018-04-03 15:18:28 +00:00
// Set USART3 clock to 115 200 baud
2018-03-18 22:43:59 +00:00
USART_InitTypeDef usart;
USART_StructInit(&usart);
usart.USART_BaudRate = 115200;
USART_Init(USART3, &usart);
2018-04-03 15:18:28 +00:00
// Init USART3 clocks
2018-03-18 22:43:59 +00:00
USART_ClockInitTypeDef usartclock;
USART_ClockStructInit(&usartclock);
USART_ClockInit(USART3, &usartclock);
2018-04-03 15:18:28 +00:00
// Enable USART3
2018-03-18 22:43:59 +00:00
USART_Cmd(USART3, ENABLE);
usart3_inited = 1;
}
void USART_SendString(USART_TypeDef *USARTx, char *str)
{
2018-04-03 15:18:28 +00:00
// Send a string, byte by byte over the specified USART
2018-03-18 22:43:59 +00:00
while (*str) {
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *str++);
}
}