Started with the USART implmentation. Not finished nor Tested

interrupts
key 4 years ago
parent acee0e1eb3
commit aa6afaccce

@ -1,14 +1,24 @@
/************************************************************************************************
* Authors : Kerem Yollu & Edwin Koch
* Date : 31.11.2021
* Version : 1.0
*
* Description :
*
* TODO:
*
* LICENCE : Please check the end of this file
************************************************************************************************/
/**
**************************************************************************************************
* @file usart.h
* @author Kerem Yollu & Edwin Koch
* @date 02.11.2021
* @version 1.0
**************************************************************************************************
* @brief USART functionalities description and implementation template
*
* **Detailed Description :**
*
* This header file for pin control is based on the most common configuation options
* curenty awailable for modern hardware.
* Depending of the used Chip, some function may vary or be unawailable.
* Please take a minute to go and explore the according Chips pin.c file to see woh each function
* is implmented.
*
* @todo
* - 02.11.2021 : ALL
**************************************************************************************************
*/
#ifndef _USART_H_
#define _USART_H_
@ -17,8 +27,52 @@ extern "C" {
#endif
void usartTxInit();
void usartRxInit();
#include "../stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h"
/*! Enum of USART Word Lenght Options */
typedef enum
{
seven, /*!< 7 bits word Lenght */
eight, /*!< 8 bits word Lenght */
nine /*!< 9 bits word Lenght */
}wordLength_t;
/*! Enum of USART Word Lenght Options */
typedef enum
{
usart1, /*!< USART Channel 1 */
usart2 /*!< USART Channel 2 */
}usartNo_t;
/**
* \brief Enable the USART block idependent of he RX & TX channels.
* \param USART_TypeDef *usart
* \retval none
*/
//void usartInit(USART_TypeDef *usart);
void usartInit();
/**
* \brief Sets the word lenght of the Usart channel.
* \param * USART_TypeDef usart
* \retval none
*/
void usartSetWordLenght(wordLength_t lenght);
/**
* \brief Sets the Baud Rate of the Usart channel.
* \param uint32_t baud
* \retval none
*/
void usartSetBaudRate(uint32_t baud);
/**
* \brief Sets the word lenght of the Usart channel.
* \param * USART_TypeDef usart
* \retval none
*/
void usartSendChar(uint8_t ch);
#ifdef __cplusplus
}

@ -26,3 +26,9 @@ target_compile_options(stmPin PRIVATE ${C_FLAGS})
target_compile_definitions(stmPin PRIVATE ${C_DEFS})
target_include_directories(stmPin PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES})
add_library(sub::pin ALIAS stmPin)
add_library(stmUsart usart.c)
target_compile_options(stmUsart PRIVATE ${C_FLAGS})
target_compile_definitions(stmUsart PRIVATE ${C_DEFS})
target_include_directories(stmUsart PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES})
add_library(sub::usart ALIAS stmUsart)

@ -1,3 +1,25 @@
/**
**************************************************************************************************
* @file pin.c
* @author Kerem Yollu & Edwin Koch
* @date 02.11.2021
* @version 1.0
**************************************************************************************************
* @brief Implementation of pin.h for the STM32F042K6 MCU
*
* **Detailed Description :**
*
* This source code uses bit manipulation in order to minimise the footprint of pin initialisation
* and manipulation. It's based on the CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h Header file
* to get the obtain the right Registers.
*
*
* @todo
* - 01.11.2021 : Should we add a seprate header in the cls layer containing the pinNo_t ?
* - 01.11.2021 : Depending on request implment a pinLock() function
**************************************************************************************************
*/
#include "pin.h"
#define MODER_IN 0x0UL
@ -16,8 +38,10 @@
#define OTYPER_PUSH_PULL 0x0UL
#define OTYPER_OPEN_DRAIN 0x1UL
#define PIN_BASE ((GPIO_TypeDef *)(pinNo&~0xFF))
#define PIN_BASE ((GPIO_TypeDef *)(pinNo&~0xFF))
#define PIN_NO (pinNo & 0x0F)
#define PIN_PORT ((pinNo & 0xF0)>>4)
/*! Table for binding the Pin mode_t enum index to the values that the MODER register needs */
const uint32_t moderMode[5] = {
MODER_ANALOG,
MODER_IN,
@ -26,18 +50,27 @@ const uint32_t moderMode[5] = {
MODER_ALTERNATE
};
/**
* Table for binding the Pin pinNo_t's Port Offet to the PORT gesiter that needs to be activated
*/
const uint32_t pinPort[3] = {
RCC_AHBENR_GPIOAEN,
RCC_AHBENR_GPIOBEN,
RCC_AHBENR_GPIOFEN
};
/**
* Table for binding the Pin pullUpDown_t enum index to the values that the PUPDR register needs
*/
const uint32_t pinPullUpDown[3] = {
PUPDR_NO_PULL,
PUPDR_PULL_UP,
PUPDR_PULL_DOWN
};
/**
* Table for binding the Pin speed_t enum index to the values that the OSPEEDR register needs
*/
const uint32_t speedList[4] = {
OSPEEDR_LOW,
OSPEEDR_LOW,
@ -45,6 +78,10 @@ const uint32_t speedList[4] = {
OSPEEDR_HIGH
};
/**
* Table for binding the Pin stage_t enum index to the values that the OTYPER register needs
*/
const uint32_t outputStgeList[3] =
{
OTYPER_OPEN_DRAIN,
@ -55,29 +92,41 @@ const uint32_t outputStgeList[3] =
void pinSetMode(pinNo_t pinNo, mode_t mode)
{
//Clear entry.
PIN_BASE->MODER &=~ (0x3 << ((pinNo & 0x0F) * 2));
PIN_BASE->MODER &=~ (0x3 << (PIN_NO * 2));
//Set to corresponding mode.
PIN_BASE->MODER |= (moderMode[mode] << ((pinNo & 0x0F) * 2));
PIN_BASE->MODER |= (moderMode[mode] << (PIN_NO * 2));
}
void pinSetOutputStage(pinNo_t pinNo, stage_t stage)
{
PIN_BASE->OSPEEDR &= 1 << (pinNo & 0x0F);
PIN_BASE->OSPEEDR |= (outputStgeList[stage] << (pinNo & 0x0F));
PIN_BASE->OTYPER &= ~(1 << PIN_NO);
PIN_BASE->OTYPER |= (outputStgeList[stage] << PIN_NO);
}
void pinSetPullUpDonw(pinNo_t pinNo, pullUpDown_t resistance)
{
PIN_BASE->PUPDR &= ~(0x3 << ((pinNo & 0x0F) * 2));
PIN_BASE->PUPDR |= (pinPullUpDown[resistance] << ((pinNo & 0x0F) * 2));
PIN_BASE->PUPDR &= ~(0x3 << (PIN_NO * 2));
PIN_BASE->PUPDR |= (pinPullUpDown[resistance] << (PIN_NO * 2));
}
void pinSetSpeed(pinNo_t pinNo, speed_t speed)
{
PIN_BASE->OSPEEDR &= (0x3 << ((pinNo & 0x0F) * 2));
PIN_BASE->OSPEEDR |= (speedList[speed] << ((pinNo & 0x0F) * 2));
PIN_BASE->OSPEEDR &= (0x3 << (PIN_NO * 2));
PIN_BASE->OSPEEDR |= (speedList[speed] << (PIN_NO * 2));
}
void pinSetAlternate(pinNo_t pinNo, uint16_t alternate)
{
if(PIN_NO > 7)
{
PIN_BASE->AFR[0] &= ~(0x0F << (PIN_NO * 2));
PIN_BASE->AFR[0] |= ((alternate & 0x0f) << (PIN_NO * 2));
}
PIN_BASE->AFR[1] &= ~(0x0F << (PIN_NO * 2));
PIN_BASE->AFR[1] |= ((alternate & 0x0f) << (PIN_NO * 2));
}
void pinConfig(pinNo_t pinNo, mode_t mode, stage_t stage, pullUpDown_t resistance, speed_t speed)
{
pinInit(pinNo); //Very important to init first so that the corresponding bus gets his clock
@ -89,26 +138,26 @@ void pinConfig(pinNo_t pinNo, mode_t mode, stage_t stage, pullUpDown_t resistanc
uint8_t pinRead(pinNo_t pinNo)
{
return ((PIN_BASE->IDR & (1<<(pinNo & 0x0F))) >> (pinNo & 0x0F));
return ((PIN_BASE->IDR & (1<<PIN_NO)) >> PIN_NO);
}
void pinToggle(pinNo_t pinNo)
{
if(pinRead(pinNo))
{
PIN_BASE->BSRR |= (GPIO_BSRR_BR_0 << ((pinNo & 0xF)));
PIN_BASE->BSRR |= (GPIO_BSRR_BR_0 << (PIN_NO));
return;
}
PIN_BASE->BSRR |= (GPIO_BSRR_BS_0 << ((pinNo & 0xF)));
PIN_BASE->BSRR |= (GPIO_BSRR_BS_0 << (PIN_NO));
}
void pinWrite(pinNo_t pinNo, uint8_t state)
{
if(state) {
PIN_BASE->BSRR |= (GPIO_BSRR_BS_0 << ((pinNo & 0xF)));
PIN_BASE->BSRR |= (GPIO_BSRR_BS_0 << (PIN_NO));
return;
}
PIN_BASE->BSRR |= (GPIO_BSRR_BR_0 << ((pinNo & 0xF)));
PIN_BASE->BSRR |= (GPIO_BSRR_BR_0 << (PIN_NO));
}
//Enable the pin port's clock
@ -117,7 +166,7 @@ void pinWrite(pinNo_t pinNo, uint8_t state)
//DS Page : 121
void pinInit(pinNo_t pinNo)
{
RCC->AHBENR |= pinPort[((pinNo & 0xF0)>>4)];
RCC->AHBENR |= pinPort[PIN_PORT];
}
//in this family of MCU ell the cloks are on the same bus that is why we dion't need to
@ -125,10 +174,15 @@ void pinInit(pinNo_t pinNo)
//DS Page : 121
void pinDeInit(pinNo_t pinNo)
{
RCC->AHBENR &=~(pinPort[((pinNo & 0xF0)>>4)]);
RCC->AHBENR &=~(pinPort[PIN_PORT]);
}
void pinReset(pinNo_t pinNo)
{
}
void pinHardwareInfo(pinNo_t pinNo)
{
//TODO : define where to print anh woh to print

@ -0,0 +1,32 @@
#include"usart.h"
#include"pin.h"
#define USART2_EN (1U << 17)
USART_TypeDef usart;
void usartInit()
{
/* COnfiguring teh pins for the uart*/
pinConfig(pinA2, alternate, none, none, normal);
pinConfig(pinA15, alternate, none, none, normal);
pinSetAlternate(pinA2, 1);
RCC->APB1ENR |= USART2_EN;
}
static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
{
return((clk + (baud/2U))/baud);
}
void usartSetBaudRate(uint32_t baud)
{
usartComputeBaudRate(baud, 8000000);
}
void usartSendChar(uint8_t ch)
{
}

@ -75,5 +75,5 @@ list(APPEND EXTRA_LIBS sub::startup)
list(APPEND EXTRA_LIBS sub::translator)
list(APPEND EXTRA_LIBS sub::sources)
list(APPEND EXTRA_LIBS sub::delay)
list(APPEND EXTRA_LIBS sub::pin) # TODO : PLease add this again.
#list(APPEND EXTRA_LIBS sub::led)
list(APPEND EXTRA_LIBS sub::pin)
list(APPEND EXTRA_LIBS sub::usart)

@ -794,7 +794,7 @@ CITE_BIB_FILES =
# messages are off.
# The default value is: NO.
QUIET = NO
QUIET = Yes
# The WARNINGS tag can be used to turn on/off the warning messages that are
# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES
@ -803,14 +803,14 @@ QUIET = NO
# Tip: Turn warnings on while writing the documentation.
# The default value is: YES.
WARNINGS = YES
WARNINGS = NO
# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate
# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag
# will automatically be disabled.
# The default value is: YES.
WARN_IF_UNDOCUMENTED = YES
WARN_IF_UNDOCUMENTED = NO
# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for
# potential errors in the documentation, such as not documenting some parameters
@ -818,7 +818,7 @@ WARN_IF_UNDOCUMENTED = YES
# markup commands wrongly.
# The default value is: YES.
WARN_IF_DOC_ERROR = YES
WARN_IF_DOC_ERROR = NO
# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that
# are documented, but have no documentation for their parameters or return
@ -866,9 +866,8 @@ WARN_LOGFILE =
INPUT = @CMAKE_CURRENT_SOURCE_DIR@/ \
@CMAKE_CURRENT_SOURCE_DIR@/bsl/csl/interfaces/ \
@CMAKE_CURRENT_SOURCE_DIR@/ bsl/csl/stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/
#@CMAKE_CURRENT_SOURCE_DIR@/bsl/csl/stm32f042/Src/ \
#@CMAKE_CURRENT_SOURCE_DIR@/bsl/csl/stm32f042/startup/
@CMAKE_CURRENT_SOURCE_DIR@/bsl/csl/stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/ \
@CMAKE_CURRENT_SOURCE_DIR@/bsl/csl/stm32f042/Src/
# This tag can be used to specify the character encoding of the source files

Loading…
Cancel
Save