CARME-M4 BSP  V1.5
uart.c
Go to the documentation of this file.
1 
77 #ifdef __cplusplus
78 extern "C" {
79 #endif /* __cplusplus */
80 
81 /*----- Header-Files -------------------------------------------------------*/
82 #include <stm32f4xx.h> /* Processor STM32F407IG */
83 #include <carme.h> /* CARME Module */
84 #include <uart.h> /* CARME BSP UART port */
85 
86 /*----- Macros -------------------------------------------------------------*/
87 #define RCC_APB2Periph_CARME_UART0 RCC_APB2Periph_USART1
88 #define RCC_APB1Periph_CARME_UART1 RCC_APB1Periph_USART3
90 /*----- Data types ---------------------------------------------------------*/
91 
92 /*----- Function prototypes ------------------------------------------------*/
93 
94 /*----- Data ---------------------------------------------------------------*/
99  /* UART0 (FF) */
100  { GPIOA, GPIO_Pin_9, GPIO_Mode_AF, GPIO_AF_USART1 },
101  { GPIOA, GPIO_Pin_10, GPIO_Mode_AF, GPIO_AF_USART1 },
102  { GPIOA, GPIO_Pin_11, GPIO_Mode_AF, GPIO_AF_USART1 },
103  { GPIOA, GPIO_Pin_12, GPIO_Mode_AF, GPIO_AF_USART1 },
104  { GPIOH, GPIO_Pin_13, GPIO_Mode_OUT },
105  { GPIOH, GPIO_Pin_14, GPIO_Mode_IN },
106  { GPIOH, GPIO_Pin_15, GPIO_Mode_IN },
107  /* UART1 (BT) */
108  { GPIOB, GPIO_Pin_10, GPIO_Mode_AF, GPIO_AF_USART3 },
109  { GPIOB, GPIO_Pin_11, GPIO_Mode_AF, GPIO_AF_USART3 },
110  { GPIOH, GPIO_Pin_9, GPIO_Mode_IN },
111  { GPIOH, GPIO_Pin_6, GPIO_Mode_OUT }
112 };
113 
114 /*----- Implementation -----------------------------------------------------*/
125 void CARME_UART_Init(USART_TypeDef *UARTx,
126  USART_InitTypeDef *pUSART_InitStruct) {
127 
128  USART_ClockInitTypeDef USART_ClockInitStruct;
129  uint8_t i = 0xFF;
130 
131  CARME_UART_GPIO_Init(); /* Initialize the CARME UART Port GPIOs */
132 
133  if (UARTx == CARME_UART0) {
135  }
136  else {
138  }
139  USART_DeInit(UARTx);
140 
141  /* Initialize the USART clock */
142  USART_ClockStructInit(&USART_ClockInitStruct);
143  USART_ClockInit(UARTx, &USART_ClockInitStruct);
144 
145  /* Delete all content on the input buffer */
146  while (USART_GetFlagStatus(UARTx, USART_FLAG_RXNE ) == SET) {
147  USART_ReceiveData(UARTx);
148  }
149 
150  USART_Cmd(UARTx, DISABLE);
151  USART_Init(UARTx, pUSART_InitStruct);
152  USART_Cmd(UARTx, ENABLE);
153 
154  while (i--) {
155  /* Wait until hardware is running */
156  }
157 }
158 
167 
168  GPIO_InitTypeDef GPIO_InitStruct;
169 
170  /* Configure the GPIO */
171  GPIO_StructInit(&GPIO_InitStruct);
172  GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
173  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
174  CARME_GPIO_Init(CARME_UART_Port_Pin, &GPIO_InitStruct,
175  sizeof(CARME_UART_Port_Pin) / sizeof(CARME_Port_Pin_t));
176 }
177 
188 void CARME_UART_SendChar(USART_TypeDef *UARTx, char c) {
189 
190  while (USART_GetFlagStatus(UARTx, USART_FLAG_TC) == RESET) {
191  }
192  USART_SendData(UARTx, c);
193 }
194 
206 void CARME_UART_SendString(USART_TypeDef *UARTx, char *pStr) {
207 
208  while (*pStr) {
209  CARME_UART_SendChar(UARTx, *pStr);
210  pStr++;
211  }
212 }
213 
225 ERROR_CODES CARME_UART_ReceiveChar(USART_TypeDef *UARTx, char *pc) {
226 
227  uint16_t i;
229 
230  if (USART_GetFlagStatus(UARTx, USART_FLAG_RXNE ) == SET) {
231  i = USART_ReceiveData(UARTx);
232  if (i <= 0xFF) {
233  *pc = (uint8_t) (i & 0xFF);
234  res = CARME_NO_ERROR;
235  }
236  }
237  return res;
238 }
239 
253 ERROR_CODES CARME_UART_ReceiveString(USART_TypeDef *UARTx, char *pc,
254  uint8_t count) {
255 
256  ERROR_CODES res = 0;
257  uint8_t i;
258 
259  for (i = 0; i < count; i++) {
260 
261  res = CARME_UART_ReceiveChar(UARTx, pc);
262  if ((*pc == '\r') || (*pc == '\n')
263  || (res == CARME_ERROR_UART_NO_DATA)) {
264  break;
265  }
266  pc++;
267  }
268  return res;
269 }
270 
271 #ifdef __cplusplus
272 }
273 #endif /* __cplusplus */
274 
uint8_t ERROR_CODES
Error variable.
Definition: carme.h:255
void USART_Init(USART_TypeDef *USARTx, USART_InitTypeDef *USART_InitStruct)
Initializes the USARTx peripheral according to the specified parameters in the USART_InitStruct ...
The CARME UART Module provides a function to initialize the GPIOs for the CARME UART ports...
USART Clock Init Structure definition.
void CARME_UART_Init(USART_TypeDef *UARTx, USART_InitTypeDef *pUSART_InitStruct)
UART initialization.
Definition: uart.c:125
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
Enables or disables the High Speed APB (APB2) peripheral clock.
#define CARME_UART0
Definition: uart.h:81
#define RCC_APB2Periph_CARME_UART0
Definition: uart.c:87
#define CARME_ERROR_UART_NO_DATA
Definition: uart.h:85
void CARME_UART_SendString(USART_TypeDef *UARTx, char *pStr)
Send a string over the UART port. You can use printf() to send a formated String over the CARME_UART0...
Definition: uart.c:206
void CARME_UART_SendChar(USART_TypeDef *UARTx, char c)
Send a char over the UART port.
Definition: uart.c:188
GPIOSpeed_TypeDef GPIO_Speed
FlagStatus USART_GetFlagStatus(USART_TypeDef *USARTx, uint16_t USART_FLAG)
Checks whether the specified USART flag is set or not.
#define RCC_APB1Periph_CARME_UART1
Definition: uart.c:88
GPIOPuPd_TypeDef GPIO_PuPd
void CARME_GPIO_Init(CARME_Port_Pin_t *pPortPinAssociation, GPIO_InitTypeDef *pGPIO_InitStruct, uint8_t size)
Initialize GPIO ports with a CARME_Port_Pin_t table.
Definition: carme.c:540
void CARME_UART_GPIO_Init(void)
CARME UART port GPIO initialization.
Definition: uart.c:166
void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
Fills each GPIO_InitStruct member with its default value.
CARME port and pin association structure.
Definition: carme.h:245
static CARME_Port_Pin_t CARME_UART_Port_Pin[]
UART Port and Pin association.
Definition: uart.c:98
GPIO Init structure definition.
#define CARME_NO_ERROR
Definition: carme.h:135
void USART_Cmd(USART_TypeDef *USARTx, FunctionalState NewState)
Enables or disables the specified USART peripheral.
void USART_ClockStructInit(USART_ClockInitTypeDef *USART_ClockInitStruct)
Fills each USART_ClockInitStruct member with its default value.
USART Init Structure definition.
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
#define GPIO_AF_USART1
AF 7 selection.
void USART_ClockInit(USART_TypeDef *USARTx, USART_ClockInitTypeDef *USART_ClockInitStruct)
Initializes the USARTx peripheral Clock according to the specified parameters in the USART_ClockInitS...
void USART_DeInit(USART_TypeDef *USARTx)
Deinitializes the USARTx peripheral registers to their default reset values.
uint16_t USART_ReceiveData(USART_TypeDef *USARTx)
Returns the most recent received data by the USARTx peripheral.
void USART_SendData(USART_TypeDef *USARTx, uint16_t Data)
Transmits single data through the USARTx peripheral.
ERROR_CODES CARME_UART_ReceiveString(USART_TypeDef *UARTx, char *pc, uint8_t count)
Receive a string from the UART input buffer until new line or carriage return or there is no more dat...
Definition: uart.c:253
ERROR_CODES CARME_UART_ReceiveChar(USART_TypeDef *UARTx, char *pc)
Receive a char from the UART input buffer.
Definition: uart.c:225