diff --git a/bsl/csl/interfaces/usart.h b/bsl/csl/interfaces/usart.h index 2b1285f..b4dd8d2 100644 --- a/bsl/csl/interfaces/usart.h +++ b/bsl/csl/interfaces/usart.h @@ -28,29 +28,43 @@ extern "C" { #include "pin.h" - #include "../stm32f042/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h" +#define NO_PARITY_CTRL 0 +#define PARITY_CTRL 1 + /*! Enum of USART Word Lenght Options */ typedef enum { - seven, /*!< 7 bits word Lenght */ - eight, /*!< 8 bits word Lenght */ - nine /*!< 9 bits word Lenght */ + seven, /*!< **7 bits** word Lenght */ + eight, /*!< **8 bits** word Lenght */ + nine /*!< **9 bits** word Lenght */ }usartWordLength_t; /*! Enum of USART Word Lenght Options */ typedef enum { usart1 = USART1_BASE , /*!< USART Channel 1 */ - usart2 = USART2_BASE /*!< USART Channel 2 */ + usart2 = USART2_BASE /*!< USART Channel 2 */ }usartNo_t; +/*! Enum of USART Parity Even or Odd */ typedef enum { - even, - odd + even, /*!< **Even** Parity */ + odd /*!< **Odd** Parity */ }usartParity_t; + +/*! Enum of USART Hardware flow control options */ +typedef enum +{ + noFlowControl, /*!< **Disabeled** */ + cts, /*!< **CTS** Only */ + rts, /*!< **RTS** Only */ + ctsRts /*!< **CTS & RTS** Enabled */ +}usartHwFlowCtrl_t; + + /** * \brief Enable the USART with the most commment settings * \param usartNo_t channel Uart **Channel** to be used @@ -64,7 +78,7 @@ void usartInit( usartNo_t channel, uint32_t baud, usartWordLength_t lenght, uint8_t parity, - uint8_t hwFlowControl); + usartHwFlowCtrl_t flowCtrl); /** * \brief Enable the USART Transmission pin, be carefull this function will not link the right pin to the right UART channel. @@ -90,20 +104,35 @@ void usartSetWordLenght(usartNo_t channel, usartWordLength_t lenght); /** * \brief Sets the Baud Rate of the Usart channel. + * \param usartNo_t channel, * \param uint32_t baud * \retval none */ void usartSetBaudRate(usartNo_t channel, uint32_t baud); /** - * \brief Sets the word lenght of the Usart channel. - * \param * USART_TypeDef usart + * \brief Stups Hardware Flow control + * \param usartNo_t channel, + * \param usartHwFlowCtrl_t flowCtrl * \retval none */ -void usartSendChar(int ch); +void usartSetHwFlowCtrl(usartNo_t channel, usartHwFlowCtrl_t flowCtrl); +/** + * \brief Sends a char array until \0 is detected. + * \param *ptr pointer to char array + * \retval none + */ void print_Usart(char *ptr); +/** + * \brief Cheks id send buffer is empty an than Sends one Char + * \param uint8_t ch one Character. + * \retval none + */ +void usartSendChar(uint8_t ch); + + #ifdef __cplusplus } #endif diff --git a/bsl/csl/stm32f042/Src/usart.c b/bsl/csl/stm32f042/Src/usart.c index 58d0ee0..0c900d6 100644 --- a/bsl/csl/stm32f042/Src/usart.c +++ b/bsl/csl/stm32f042/Src/usart.c @@ -21,25 +21,11 @@ #include"pin.h" -#define CR1_TE_EN (1U << 3) -#define CR1_UE_EN (1U << 0) - -#define ISR_TXE (1U << 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; - - while(ptr[len] != '\0') - { - usartSendChar(ptr[len]); - len++; - } -} + void usartInit( usartNo_t channel, pinNo_t pinTx, @@ -47,7 +33,7 @@ void usartInit( usartNo_t channel, uint32_t baud, usartWordLength_t lenght, uint8_t parity, - uint8_t hwFlowControl) + usartHwFlowCtrl_t flowCtrl) { /* COnfiguring teh pins for the uart*/ usartInitTx(pinTx); @@ -63,13 +49,20 @@ void usartInit( usartNo_t channel, RCC->APB2ENR |= RCC_APB2ENR_USART1EN; } - usartSetBaudRate(channel,baud); - - //enables the transmit and sets all the other values to 0 = Default - USART_CHANNEL->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode. + + USART_CHANNEL->CR1 = 0; // The = 0 is on purpose to set uart to default mode. + USART_CHANNEL->CR2 = 0; // The = 0 is on purpose to set uart to default mode. + USART_CHANNEL->CR3 = 0; // The = 0 is on purpose to set uart to default mode. + + USART_CHANNEL->CR1 |= USART_CR1_TE; //Enbale trasnmit + USART_CHANNEL->CR1 |= USART_CR1_RE; //Enable recieve - //UART Enable - USART_CHANNEL->CR1 |= CR1_UE_EN; + usartSetBaudRate(channel,baud); + usartSetWordLenght(channel, lenght); + usartSetHwFlowCtrl(channel, flowCtrl); + + //UART Enable shall be allwas date a th ene of configuration. + USART_CHANNEL->CR1 |= USART_CR1_UE; } void usartInitTx(pinNo_t pinTx) @@ -84,9 +77,24 @@ void usartInitRx(pinNo_t pinRx) pinSetAlternate(pinRx, AF1); } +//this one is special as the register bist's don't follow eachother. void usartSetWordLenght(usartNo_t channel, usartWordLength_t lenght) { - + if(lenght == seven) + { + USART_CHANNEL->CR1 |= USART_CR1_M1; + USART_CHANNEL->CR1 &= ~USART_CR1_M0; + } + else if (lenght == eight) + { + USART_CHANNEL->CR1 &= ~USART_CR1_M0; + USART_CHANNEL->CR1 &= ~USART_CR1_M1; + } + else if(lenght == nine) + { + USART_CHANNEL->CR1 &= ~USART_CR1_M1; + USART_CHANNEL->CR1 |= USART_CR1_M0; + } } static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud) @@ -100,9 +108,42 @@ void usartSetBaudRate(usartNo_t channel, uint32_t baud) USART_CHANNEL->BRR = usartComputeBaudRate(SYST_CLK,baud); } -void usartSendChar(int ch) +void usartSetHwFlowCtrl(usartNo_t channel, usartHwFlowCtrl_t flowCtrl) +{ + if(flowCtrl == noFlowControl) + { + USART_CHANNEL->CR3 &= ~USART_CR3_CTSE; + USART_CHANNEL->CR3 &= ~USART_CR3_RTSE; + } + else if(flowCtrl == cts) + { + USART_CHANNEL->CR3 |= USART_CR3_CTSE; + } + else if(flowCtrl == rts) + { + USART_CHANNEL->CR3 |= USART_CR3_RTSE; + } + else if(flowCtrl == ctsRts) + { + USART_CHANNEL->CR3 |= USART_CR3_CTSE; + USART_CHANNEL->CR3 |= USART_CR3_RTSE; + } +} + +void print_Usart(char *ptr) +{ + uint16_t len = 0; + + while(ptr[len] != '\0') + { + usartSendChar(ptr[len]); + len++; + } +} + +void usartSendChar(uint8_t ch) { // Make sure that TX buffer is empty - while(!(USART2->ISR & ISR_TXE)){} - USART2->TDR = (ch & 0xFF); + while(!(USART2->ISR & USART_ISR_TXE)){} + USART2->TDR = ch; } diff --git a/main.cpp b/main.cpp index eeb41d9..de51789 100644 --- a/main.cpp +++ b/main.cpp @@ -16,8 +16,8 @@ int main(int argc, char *argv[]) pinA15, 115200, eight, - 0, - 0); + NO_PARITY_CTRL, + noFlowControl); print_Usart("Wellcome to our KED project\n\r");