From 3a61f9f35efa05d8c35a9c636b92045afbbaaf4d Mon Sep 17 00:00:00 2001 From: key Date: Thu, 4 Nov 2021 13:33:44 +0100 Subject: [PATCH] USART Rx is wroking, fixed yet another bug in pin.c ^-^ --- bsl/csl/interfaces/ascii.h | 60 +++++++++++++++++++++++++++++++++++ bsl/csl/interfaces/usart.h | 16 ++++++++-- bsl/csl/stm32f042/Src/pin.c | 6 ++-- bsl/csl/stm32f042/Src/usart.c | 34 +++++++++++++------- main.cpp | 27 +++++++++++++--- 5 files changed, 121 insertions(+), 22 deletions(-) create mode 100644 bsl/csl/interfaces/ascii.h diff --git a/bsl/csl/interfaces/ascii.h b/bsl/csl/interfaces/ascii.h new file mode 100644 index 0000000..9a1a55a --- /dev/null +++ b/bsl/csl/interfaces/ascii.h @@ -0,0 +1,60 @@ +/** + ************************************************************************************************** + * @file ascii.h + * @author Kerem Yollu + * @date 04.11.2021 + * @version 1.0 + ************************************************************************************************** + * @brief Defines ofr ascii chars. + * + ************************************************************************************************** +*/ + +#ifndef _ASCII_H_ +#define _ASCII_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#define ASCII_NULL 0 // Null character +#define ASCII_SOH 1 // Start of Header +#define ASCII_STX 2 // Start of Text +#define ASCII_ETX 3 // End of Text, hearts card suit +#define ASCII_EOT 4 // End of Transmission, diamonds card suit +#define ASCII_ENQ 5 // Enquiry, clubs card suit +#define ASCII_ACK 6 // Acknowledgement, spade card suit +#define ASCII_BEL 7 // Bell +#define ASCII_BS 8 // Backspace +#define ASCII_HT 9 // Horizontal Tab +#define ASCII_LF 10 // Line feed +#define ASCII_VT 11 // Vertical Tab, male symbol, symbol for Mars +#define ASCII_FF 12 // Form feed, female symbol, symbol for Venus +#define ASCII_CR 13 // Carriage return +#define ASCII_SO 14 // Shift Out +#define ASCII_SI 15 // Shift In +#define ASCII_DLE 16 // Data link escape +#define ASCII_DC1 17 // Device control 1 +#define ASCII_DC2 18 // Device control 2 +#define ASCII_DC3 19 // Device control 3 +#define ASCII_DC4 20 // Device control 4 +#define ASCII_NAK 21 // NAK Negative-acknowledge +#define ASCII_SYN 22 // Synchronous idle +#define ASCII_ETB 23 // End of trans. block +#define ASCII_CAN 24 // Cancel +#define ASCII_EM 25 // End of medium +#define ASCII_SUB 26 // Substitute +#define ASCII_ESC 27 // Escape +#define ASCII_FS 28 // File separator +#define ASCII_GS 29 // Group separator +#define ASCII_RS 30 // Record separator +#define ASCII_US 31 // Unit separator +#define ASCII_DEL 127 // Delete +#define ASCII_space 32 // Space +#define ASCII_nbsp 255 // Non-breaking space or no-break space +#define ASCII_clear "\033[2J" // Clear screen command + +#ifdef __cplusplus +} +#endif + +#endif // _ASCII_H_ diff --git a/bsl/csl/interfaces/usart.h b/bsl/csl/interfaces/usart.h index b4dd8d2..aa5eb0e 100644 --- a/bsl/csl/interfaces/usart.h +++ b/bsl/csl/interfaces/usart.h @@ -120,19 +120,29 @@ void usartSetHwFlowCtrl(usartNo_t channel, usartHwFlowCtrl_t flowCtrl); /** * \brief Sends a char array until \0 is detected. + * \param usartNo_t channel, * \param *ptr pointer to char array * \retval none */ -void print_Usart(char *ptr); +void print_Usart(usartNo_t channel,char *ptr); /** - * \brief Cheks id send buffer is empty an than Sends one Char + * \brief Cheks if the send buffer is empty an than Sends one Char + * \param usartNo_t channel, * \param uint8_t ch one Character. * \retval none */ -void usartSendChar(uint8_t ch); +void usartSendChar(usartNo_t channel, uint8_t ch); +/** + * \brief Cheks if the recieve buffer is NOT empty an than Rreturns the recieved Char + * \param usartNo_t channel, + * \param uint8_t ch one Character. + * \retval none + */ +uint8_t usartGetChar(usartNo_t channel); + #ifdef __cplusplus } #endif diff --git a/bsl/csl/stm32f042/Src/pin.c b/bsl/csl/stm32f042/Src/pin.c index b209637..45eda0e 100644 --- a/bsl/csl/stm32f042/Src/pin.c +++ b/bsl/csl/stm32f042/Src/pin.c @@ -120,14 +120,14 @@ void pinSetSpeed(pinNo_t pinNo, pinSpeed_t speed) void pinSetAlternate(pinNo_t pinNo, uint16_t alternate) { - if(PIN_NO < 7) + if(PIN_NO < 8) { PIN_BASE->AFR[0] &= ~(0x0F << (PIN_NO * 4)); PIN_BASE->AFR[0] |= ((alternate & 0x0F) << (PIN_NO * 4)); return; } - PIN_BASE->AFR[1] &= ~(0x0F << (PIN_NO * 4)); - PIN_BASE->AFR[1] |= ((alternate & 0x0F) << (PIN_NO * 4)); + PIN_BASE->AFR[1] &= ~(0x0F << ((PIN_NO-8) * 4)); + PIN_BASE->AFR[1] |= ((alternate & 0x0F) << ((PIN_NO-8) * 4)); } void pinConfig(pinNo_t pinNo, pinMode_t mode, pinStage_t stage, pinPullUpDown_t resistance, pinSpeed_t speed) diff --git a/bsl/csl/stm32f042/Src/usart.c b/bsl/csl/stm32f042/Src/usart.c index 0c900d6..b449bed 100644 --- a/bsl/csl/stm32f042/Src/usart.c +++ b/bsl/csl/stm32f042/Src/usart.c @@ -35,11 +35,11 @@ void usartInit( usartNo_t channel, uint8_t parity, usartHwFlowCtrl_t flowCtrl) { - /* COnfiguring teh pins for the uart*/ + /* Configuring the pins for the uart*/ usartInitTx(pinTx); usartInitRx(pinRx); - //Enable the UART Module on the periferal bus this must be done before setting any regiter. + //Enable the UART Module on the periferal bus this must be done before setting any register. if(channel == usart2) { RCC->APB1ENR |= RCC_APB1ENR_USART2EN; @@ -54,26 +54,29 @@ void usartInit( usartNo_t channel, 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 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_TE; //Enbale trasnmit + USART_CHANNEL->CR1 |= USART_CR1_RE; //Enable recieve + + //UART Enable shall allwas be at the end of configuration. USART_CHANNEL->CR1 |= USART_CR1_UE; } void usartInitTx(pinNo_t pinTx) { - pinConfig(pinTx, alternate, def_stage, def_res, def_speed); + //pinConfig(pinTx, alternate, def_stage, def_res, def_speed); + pinSetMode(pinTx,alternate); pinSetAlternate(pinTx, AF1); } void usartInitRx(pinNo_t pinRx) { - pinConfig(pinRx, alternate, def_stage, def_res, def_speed); + //pinConfig(pinRx, alternate, def_stage, def_res, def_speed); + pinSetMode(pinRx,alternate); pinSetAlternate(pinRx, AF1); } @@ -130,20 +133,27 @@ void usartSetHwFlowCtrl(usartNo_t channel, usartHwFlowCtrl_t flowCtrl) } } -void print_Usart(char *ptr) +void print_Usart(usartNo_t channel,char *ptr) { uint16_t len = 0; while(ptr[len] != '\0') { - usartSendChar(ptr[len]); + usartSendChar(channel, ptr[len]); len++; } } -void usartSendChar(uint8_t ch) +void usartSendChar(usartNo_t channel, uint8_t ch) +{ + // Make sure that TX buffer is empty + while(!(USART_CHANNEL->ISR & USART_ISR_TXE)){} + USART_CHANNEL->TDR = ch; +} + +uint8_t usartGetChar(usartNo_t channel) { // Make sure that TX buffer is empty - while(!(USART2->ISR & USART_ISR_TXE)){} - USART2->TDR = ch; + while(!(USART_CHANNEL->ISR & USART_ISR_RXNE)){} + return USART_CHANNEL->RDR; } diff --git a/main.cpp b/main.cpp index de51789..7d06145 100644 --- a/main.cpp +++ b/main.cpp @@ -2,11 +2,12 @@ #include "delay.h" #include "stm32f0xx_csl.h" #include "usart.h" +#include "ascii.h" int main(int argc, char *argv[]) { uint8_t i = 0; - + uint8_t a = '0'; stmStart(); pinConfig(pinB3, output, pushPull, def_res, def_speed); @@ -19,7 +20,9 @@ int main(int argc, char *argv[]) NO_PARITY_CTRL, noFlowControl); - print_Usart("Wellcome to our KED project\n\r"); +// print_Usart(usart2, ASCII_clear); + print_Usart(usart2, "Wellcome to our KED project\n\r"); + for(i = 0 ; i < 10 ; i++) { @@ -29,10 +32,14 @@ int main(int argc, char *argv[]) } pinWrite(pinB3,0); - + + //usartSendChar(usart2, a); while(1) { - if(pinRead(pinA0)) + a = usartGetChar(usart2); + usartSendChar(usart2, a); + + if(a == '1') { pinWrite(pinB3,1); } @@ -40,6 +47,18 @@ int main(int argc, char *argv[]) { pinWrite(pinB3,0); } + +/* + if(pinRead(pinA0)) + { + pinWrite(pinB3,1); + } + else + { + pinWrite(pinB3,0); + } + */ + delayMs(100); } return 1; }