First working usart Example. | pin.c corected the pinSetAlternate(); |

interrupts
key 4 years ago
parent d444dfa383
commit f96351197f

@ -73,7 +73,7 @@ extern "C" {
/*! Enum of possible Pin Modes */ /*! Enum of possible Pin Modes */
typedef enum typedef enum
{ {
undefined, /*!< Is the **default** mode */ def_mode, /*!< Is the **default** mode */
input, /*!< Set pin as **Input** */ input, /*!< Set pin as **Input** */
output, /*!< Set pin as **Output** */ output, /*!< Set pin as **Output** */
analog, /*!< Set pin as **Analog** */ analog, /*!< Set pin as **Analog** */
@ -83,14 +83,16 @@ typedef enum
/*! Enum of possible Outpout Stages */ /*! Enum of possible Outpout Stages */
typedef enum typedef enum
{ {
floating, /*!< Set ouput stage to **Floating** */ def_stage, /*!< Set ouput stage to **Default** */
pushPull, /*!< Set ouput stage to **Push Pull** */ floating, /*!< Set ouput stage to **Floating** */
openDrain /*!< Set ouput stage to **Open Drain** */ pushPull, /*!< Set ouput stage to **Push Pull** */
openDrain /*!< Set ouput stage to **Open Drain** */
}stage_t; }stage_t;
/*! Enum for the internal Pull-Up/Down resistors */ /*! Enum for the internal Pull-Up/Down resistors */
typedef enum typedef enum
{ {
def_res, /*!< **Default** internal resistance */
none, /*!< **Disbales** internal resistance */ none, /*!< **Disbales** internal resistance */
pullUp, /*!< Set internal resistance as **Pull-Up** */ pullUp, /*!< Set internal resistance as **Pull-Up** */
pullDown /*!< Set internal resistance as **Pull-Down** */ pullDown /*!< Set internal resistance as **Pull-Down** */
@ -99,6 +101,7 @@ typedef enum
/*! Enum to set the pin's speed*/ /*! Enum to set the pin's speed*/
typedef enum typedef enum
{ {
def_speed, /*!< set pin's spped to **Default** */
slow, /*!< set pin's speed to **Slow** */ slow, /*!< set pin's speed to **Slow** */
normal, /*!< set pin's speed to **Normal** */ normal, /*!< set pin's speed to **Normal** */
fast, /*!< set pin's speed to **Fast** */ fast, /*!< set pin's speed to **Fast** */

@ -2,8 +2,8 @@
************************************************************************************************** **************************************************************************************************
* @file pin.c * @file pin.c
* @author Kerem Yollu & Edwin Koch * @author Kerem Yollu & Edwin Koch
* @date 02.11.2021 * @date 03.11.2021
* @version 1.0 * @version 1.01
************************************************************************************************** **************************************************************************************************
* @brief Implementation of pin.h for the STM32F042K6 MCU * @brief Implementation of pin.h for the STM32F042K6 MCU
* *
@ -62,7 +62,8 @@ const uint32_t pinPort[3] = {
/** /**
* Table for binding the Pin pullUpDown_t enum index to the values that the PUPDR register needs * Table for binding the Pin pullUpDown_t enum index to the values that the PUPDR register needs
*/ */
const uint32_t pinPullUpDown[3] = { const uint32_t pinPullUpDown[4] = {
PUPDR_NO_PULL,
PUPDR_NO_PULL, PUPDR_NO_PULL,
PUPDR_PULL_UP, PUPDR_PULL_UP,
PUPDR_PULL_DOWN PUPDR_PULL_DOWN
@ -71,7 +72,8 @@ const uint32_t pinPullUpDown[3] = {
/** /**
* Table for binding the Pin speed_t enum index to the values that the OSPEEDR register needs * Table for binding the Pin speed_t enum index to the values that the OSPEEDR register needs
*/ */
const uint32_t speedList[4] = { const uint32_t speedList[5] = {
OSPEEDR_MEDIUM,
OSPEEDR_LOW, OSPEEDR_LOW,
OSPEEDR_LOW, OSPEEDR_LOW,
OSPEEDR_MEDIUM, OSPEEDR_MEDIUM,
@ -82,8 +84,9 @@ const uint32_t speedList[4] = {
/** /**
* Table for binding the Pin stage_t enum index to the values that the OTYPER register needs * Table for binding the Pin stage_t enum index to the values that the OTYPER register needs
*/ */
const uint32_t outputStgeList[3] = const uint32_t outputStgeList[4] =
{ {
OTYPER_PUSH_PULL,
OTYPER_OPEN_DRAIN, OTYPER_OPEN_DRAIN,
OTYPER_PUSH_PULL, OTYPER_PUSH_PULL,
OTYPER_OPEN_DRAIN OTYPER_OPEN_DRAIN
@ -117,14 +120,14 @@ void pinSetSpeed(pinNo_t pinNo, speed_t speed)
void pinSetAlternate(pinNo_t pinNo, uint16_t alternate) void pinSetAlternate(pinNo_t pinNo, uint16_t alternate)
{ {
if(PIN_NO > 7) if(PIN_NO < 7)
{ {
PIN_BASE->AFR[0] &= ~(0x0F << (PIN_NO * 2)); PIN_BASE->AFR[0] &= ~(0x0F << (PIN_NO * 4));
PIN_BASE->AFR[0] |= ((alternate & 0x0f) << (PIN_NO * 2)); PIN_BASE->AFR[0] |= ((alternate & 0x0F) << (PIN_NO * 4));
return;
} }
PIN_BASE->AFR[1] &= ~(0x0F << (PIN_NO * 4));
PIN_BASE->AFR[1] &= ~(0x0F << (PIN_NO * 2)); PIN_BASE->AFR[1] |= ((alternate & 0x0F) << (PIN_NO * 4));
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) void pinConfig(pinNo_t pinNo, mode_t mode, stage_t stage, pullUpDown_t resistance, speed_t speed)

@ -1,8 +1,26 @@
/**
**************************************************************************************************
* @file usart.c
* @author Kerem Yollu & Edwin Koch
* @date 03.11.2021
* @version 0.4 Unstable
**************************************************************************************************
* @brief Implementation of usart.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.
*
*
**************************************************************************************************
*/
#include"usart.h" #include"usart.h"
#include"pin.h" #include"pin.h"
#define USART2_EN (1U << 17) #define USART2_EN (1U << 17)
#define CR1_TE_EN (1U << 3) #define CR1_TE_EN (1U << 3)
#define CR1_UE_EN (1U << 0) #define CR1_UE_EN (1U << 0)
@ -11,26 +29,18 @@
#define UART_BAUD 115200 #define UART_BAUD 115200
#define SYST_CLK 8000000 #define SYST_CLK 8000000
#define AF1 0x01
void usartSetBaudRate(uint32_t baud); void usartSetBaudRate(uint32_t baud);
void usartInit() void usartInit()
{ {
/* COnfiguring teh pins for the uart*/ /* COnfiguring teh pins for the uart*/
//pinConfig(pinA2, alternate, none, none, normal); pinConfig(pinA2, alternate, def_stage, def_res, def_speed);
//pinConfig(pinA15, alternate, none, none, normal); pinConfig(pinA15, alternate, def_stage, def_res, def_speed);
//Sets pina2 to alternate mode as UART_TX //Sets pina2 to alternate mode as UART_TX
//pinSetAlternate(pinA2, 1); pinSetAlternate(pinA2, AF1);
GPIOA->MODER &= ~(1u <<4);
GPIOA->MODER |= (1u <<5);
GPIOA->AFR[0] &= ~(1u<<11);
GPIOA->AFR[0] &= ~(1u<<10);
GPIOA->AFR[0] &= ~(1u<<9);
GPIOA->AFR[0] |= (1u<<8);
//Enable the priferas bus and UART //Enable the priferas bus and UART
RCC->APB1ENR |= USART2_EN; RCC->APB1ENR |= USART2_EN;
@ -38,8 +48,7 @@ void usartInit()
usartSetBaudRate(UART_BAUD); usartSetBaudRate(UART_BAUD);
//enables the transmit and sets all the other values to 0 = Default //enables the transmit and sets all the other values to 0 = Default
USART2->CR1 = 0; // The = 0 is on purpose to set uart to default mode. USART2->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
USART2->CR1 |= CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
//UART Enable //UART Enable
USART2->CR1 |= CR1_UE_EN; USART2->CR1 |= CR1_UE_EN;
@ -47,12 +56,13 @@ void usartInit()
static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud) static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
{ {
return((clk + (baud/2U))/baud); //return((clk + (baud/2U))/baud);
return(clk/baud);
} }
void usartSetBaudRate(uint32_t baud) void usartSetBaudRate(uint32_t baud)
{ {
USART2->BRR = usartComputeBaudRate(baud, SYST_CLK); USART2->BRR = usartComputeBaudRate(SYST_CLK,baud);
} }
void usartSendChar(int ch) void usartSendChar(int ch)

@ -26,7 +26,6 @@ int main(int argc, char *argv[])
while(1) while(1)
{ {
if(pinRead(pinA0)) if(pinRead(pinA0))
{ {
pinWrite(pinB3,1); pinWrite(pinB3,1);
@ -35,7 +34,6 @@ int main(int argc, char *argv[])
{ {
pinWrite(pinB3,0); pinWrite(pinB3,0);
} }
} }
return 1; return 1;
} }

Loading…
Cancel
Save