diff --git a/bsl/csl/interfaces/usart.h b/bsl/csl/interfaces/usart.h index 7b3dfce..2b1285f 100644 --- a/bsl/csl/interfaces/usart.h +++ b/bsl/csl/interfaces/usart.h @@ -16,7 +16,7 @@ * is implmented. * * @todo - * - 02.11.2021 : ALL + * - 03.11.2021 : MAybe find a way to link the awalabe uart channnels with the awailable pins for that USART channel ************************************************************************************************** */ #ifndef _USART_H_ @@ -27,8 +27,9 @@ extern "C" { #endif -#include "../stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h" +#include "pin.h" +#include "../stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h" /*! Enum of USART Word Lenght Options */ typedef enum @@ -36,36 +37,63 @@ typedef enum seven, /*!< 7 bits word Lenght */ eight, /*!< 8 bits word Lenght */ nine /*!< 9 bits word Lenght */ -}wordLength_t; +}usartWordLength_t; /*! Enum of USART Word Lenght Options */ typedef enum { - usart1, /*!< USART Channel 1 */ - usart2 /*!< USART Channel 2 */ + usart1 = USART1_BASE , /*!< USART Channel 1 */ + usart2 = USART2_BASE /*!< USART Channel 2 */ }usartNo_t; +typedef enum +{ + even, + odd +}usartParity_t; +/** + * \brief Enable the USART with the most commment settings + * \param usartNo_t channel Uart **Channel** to be used + * \param pinNo_t pinTx Pin to be used for **Transmit** + * \param pinNo_t pinRx Pin to be used for **Recieve** + * \retval none + */ +void usartInit( usartNo_t channel, + pinNo_t pinTx, + pinNo_t pinRx, + uint32_t baud, + usartWordLength_t lenght, + uint8_t parity, + uint8_t hwFlowControl); + +/** + * \brief Enable the USART Transmission pin, be carefull this function will not link the right pin to the right UART channel. + * \param pinNo_t pinTx Pin to be used for **Transmit** + * \retval none + */ +void usartInitTx(pinNo_t pinTx); + /** - * \brief Enable the USART block idependent of he RX & TX channels. - * \param USART_TypeDef *usart + * \brief Enable the USART Recieve pin, be carefull this function will not link the right pin to the right UART channel. + * \param pinNo_t pinRx Pin to be used for **Transmit** * \retval none */ -//void usartInit(USART_TypeDef *usart); -void usartInit(); +void usartInitRx(pinNo_t pinRx); /** * \brief Sets the word lenght of the Usart channel. - * \param * USART_TypeDef usart + * \param usartNo_t channel, + * \param usartWordLength_t lenght * \retval none */ -void usartSetWordLenght(wordLength_t lenght); +void usartSetWordLenght(usartNo_t channel, usartWordLength_t lenght); /** * \brief Sets the Baud Rate of the Usart channel. * \param uint32_t baud * \retval none */ -void usartSetBaudRate(uint32_t baud); +void usartSetBaudRate(usartNo_t channel, uint32_t baud); /** * \brief Sets the word lenght of the Usart channel. diff --git a/bsl/csl/stm32f042/Src/usart.c b/bsl/csl/stm32f042/Src/usart.c index 20804d6..58d0ee0 100644 --- a/bsl/csl/stm32f042/Src/usart.c +++ b/bsl/csl/stm32f042/Src/usart.c @@ -21,7 +21,6 @@ #include"pin.h" -#define USART2_EN (1U << 17) #define CR1_TE_EN (1U << 3) #define CR1_UE_EN (1U << 0) @@ -30,8 +29,7 @@ #define UART_BAUD 115200 #define SYST_CLK 8000000 #define AF1 0x01 - - +#define USART_CHANNEL ((USART_TypeDef *)channel) void print_Usart(char *ptr) { uint16_t len = 0; @@ -43,25 +41,52 @@ void print_Usart(char *ptr) } } -void usartInit() +void usartInit( usartNo_t channel, + pinNo_t pinTx, + pinNo_t pinRx, + uint32_t baud, + usartWordLength_t lenght, + uint8_t parity, + uint8_t hwFlowControl) { /* COnfiguring teh pins for the uart*/ - pinConfig(pinA2, alternate, def_stage, def_res, def_speed); - pinConfig(pinA15, alternate, def_stage, def_res, def_speed); + usartInitTx(pinTx); + usartInitRx(pinRx); - //Sets pina2 to alternate mode as UART_TX - pinSetAlternate(pinA2, AF1); - - //Enable the priferas bus and UART - RCC->APB1ENR |= USART2_EN; + //Enable the UART Module on the periferal bus this must be done before setting any regiter. + if(channel == usart2) + { + RCC->APB1ENR |= RCC_APB1ENR_USART2EN; + } + else + { + RCC->APB2ENR |= RCC_APB2ENR_USART1EN; + } - usartSetBaudRate(UART_BAUD); + usartSetBaudRate(channel,baud); //enables the transmit and sets all the other values to 0 = Default - USART2->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode. + USART_CHANNEL->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode. //UART Enable - USART2->CR1 |= CR1_UE_EN; + USART_CHANNEL->CR1 |= CR1_UE_EN; +} + +void usartInitTx(pinNo_t pinTx) +{ + pinConfig(pinTx, alternate, def_stage, def_res, def_speed); + pinSetAlternate(pinTx, AF1); +} + +void usartInitRx(pinNo_t pinRx) +{ + pinConfig(pinRx, alternate, def_stage, def_res, def_speed); + pinSetAlternate(pinRx, AF1); +} + +void usartSetWordLenght(usartNo_t channel, usartWordLength_t lenght) +{ + } static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud) @@ -70,9 +95,9 @@ static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud) return(clk/baud); } -void usartSetBaudRate(uint32_t baud) +void usartSetBaudRate(usartNo_t channel, uint32_t baud) { - USART2->BRR = usartComputeBaudRate(SYST_CLK,baud); + USART_CHANNEL->BRR = usartComputeBaudRate(SYST_CLK,baud); } void usartSendChar(int ch) diff --git a/main.cpp b/main.cpp index 44df8e3..eeb41d9 100644 --- a/main.cpp +++ b/main.cpp @@ -9,10 +9,15 @@ int main(int argc, char *argv[]) stmStart(); - pinConfig(pinB3, output, pushPull, none, normal); - pinConfig(pinA0, input, pushPull, pullDown, normal); - - usartInit(); + pinConfig(pinB3, output, pushPull, def_res, def_speed); + pinConfig(pinA0, input, def_stage, pullDown, def_speed); + usartInit( usart2, + pinA2, + pinA15, + 115200, + eight, + 0, + 0); print_Usart("Wellcome to our KED project\n\r");