We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am using the HY mini STM32V eval. board and are trying to set up a simple USART transmission. The board has a UART-to-USB uC so I need to write to USART1. I use RealTerm terminal. I am new to ARM and this is my first ARM. I can't seem to get it to work. It seems that the break and error indicator in RealTerm are flashing sometimes. I do not wish to use the standard library provided (by STM). I will post my code here and if someone has the time to look at it, it would be much appreciated! Any feedback is welcome!
(I saw the tabs got a little off, but should still be readable).
#include "stm32f10x.h" //LED #define LED_1 GPIO_BSRR_BS0 #define LED_2 GPIO_BSRR_BS1 #define LEDPORT GPIOB //GPIO #define GPIO_CR_MODE_RESET 0 #define GPIO_CR_MODE_OUTPUT_2MHZ 2 #define GPIO_CR_MODE_OUTPUT_10MHZ 1 #define GPIO_CR_MODE_OUTPUT_50MHZ 3 #define GPIO_CR_CNF_OUTPUT_PUSHPULL 0 #define GPIO_CR_CNF_OUTPUT_OPENDRAIN 1 #define GPIO_CR_CNF_OUTPUT_AFIO_PUSHPULL 2 #define GPIO_CR_CNF_OUTPUT_AFIO_OPENDRAIN 3 #define GPIO_CR_CNF_INPUT_ANALOG 0 #define GPIO_CR_CNF_INPUT_FLOATING 1 #define GPIO_CR_CNF_INPUT_PULLUPDOWN 2 #define GPIO_setPinModeL(port, pin, mode) port->CRL |= (mode << 4*pin) #define GPIO_setPinModeH(port, pin, mode) port->CRH |= (mode << ((4*pin) - 32)) #define GPIO_setPinCnfL(port, pin, cnf) port->CRL |= (cnf << ((4*pin) + 2)) #define GPIO_setPinCnfH(port, pin, cnf) port->CRH |= (cnf << ((4*pin) - 32 + 2)) #define GPIO_initPort(port) port = 0x0000 //USART #define USARTPORT GPIOA //Delay fucntion void DELAY(int delay) { volatile int dly; for(dly = 0; dly < delay; dly++); } //LED functions void LED_init(void) { RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; //Enable port B clock GPIO_initPort(LEDPORT->CRL); //Have to set CRL to 0x0000 before we can bitwise or to the register. GPIO_setPinModeL(LEDPORT, 0, GPIO_CR_MODE_OUTPUT_2MHZ); GPIO_setPinModeL(LEDPORT, 1, GPIO_CR_MODE_OUTPUT_2MHZ); } void LED1_set(void) { LEDPORT->BSRR = LED_1; } void LED2_set(void) { LEDPORT->BSRR = LED_2; } void LED2_clear(void) { LEDPORT->BRR = LED_2; } //USART funtions void USART1_send_byte(int byte) { while( !(USART1->SR & USART_SR_TXE) ); USART1->DR = byte; } int USART1_get_byte(void) { while( (USART1->SR & USART_SR_RXNE) != 0 ); return ( (int) (USART1->DR & 0x000000FF) ); } void USART1_init(void) { RCC->APB2ENR |= RCC_APB2ENR_USART1EN; //Enable USART clock RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; //NULL registers USART1->CR1 = 0x0000; USART1->CR2 = 0x0000; USART1->CR3 = 0x0000; USART1->BRR = 0x0000; //Tx - PA9 GPIO_setPinModeH(USARTPORT, 9, GPIO_CR_MODE_OUTPUT_50MHZ); GPIO_setPinCnfH(USARTPORT, 9, GPIO_CR_CNF_OUTPUT_AFIO_PUSHPULL); USART1->CR1 |= USART_CR1_UE; USART1->BRR = ( (1 << 0) | (0x00000034 << 4) ); // 8MHz system clock and 9600 baud rate. USART1->CR1 |= USART_CR1_TE; //Rx //GPIO_setPinModeH(USARTPORT, 10, GPIO_CR_MODE_OUTPUT_50MHZ); //GPIO_setPinCnfH(USARTPORT, 10, GPIO_CR_CNF_INPUT_FLOATING); //USART1->CR1 |= USART_CR1_RE; } /********* MAIN *********/ int main(void) { LED_init(); USART1_init(); //USART1_init2(); USART1->DR = 0x000000AA; LED1_set(); while(1) { USART1_send_byte(0x000000BB); LED2_set(); DELAY(5000000); LED2_clear(); DELAY(5000000); } }
I rewrote and simplified the program. Still not able to see any data on the receiving end. I have used RealTerm and Terminal v1.9. I ran an example program and was able to receive data so it is not damaged in any way. There must be something I am missing. Here is the new code:
#include "stm32f10x.h" int main(void) { //Inits. RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN); GPIOA->CRH |= (GPIO_CRH_CNF9_1 | GPIO_CRH_MODE9_1 | GPIO_CRH_MODE9_0); USART1->CR2 &= (~USART_CR2_CPOL | ~USART_CR2_CPHA | ~USART_CR2_CLKEN | ~USART_CR2_LBCL | ~USART_CR2_STOP); USART1->CR1 &= (~USART_CR1_M | ~USART_CR1_PCE | ~USART_CR1_PS | ~USART_CR1_TE | ~USART_CR1_RE); USART1->CR1 |= USART_CR1_TE; USART1->CR3 &= (~USART_CR3_CTSE | ~USART_CR3_RTSE); USART1->BRR = ( (0x0034 << 4) | (0x0001) ); //9600 @8MHz USART1->CR1 |= USART_CR1_UE; while(1) { USART1->DR = (uint8_t) 0xAA; while(! (USART1->CR1 & USART_SR_TC) ); //while(! (USART1->CR1 & USART_SR_TXE) ); } }