[ create a new paste ] login | about

Link: http://codepad.org/4H4eQWJW    [ raw code | fork ]

C++, pasted on Jul 28:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#include "USART.h"

#define CR1_UE_Set                ((uint16_t)0x2000)  /*!< USART Enable Mask */
#define CR1_UE_Reset              ((uint16_t)0xDFFF)  /*!< USART Disable Mask */

#define CR1_WAKE_Mask             ((uint16_t)0xF7FF)  /*!< USART WakeUp Method Mask */

#define CR1_RWU_Set               ((uint16_t)0x0002)  /*!< USART mute mode Enable Mask */
#define CR1_RWU_Reset             ((uint16_t)0xFFFD)  /*!< USART mute mode Enable Mask */
#define CR1_SBK_Set               ((uint16_t)0x0001)  /*!< USART Break Character send Mask */
#define CR1_CLEAR_Mask            ((uint16_t)0xE9F3)  /*!< USART CR1 Mask */
#define CR2_Address_Mask          ((uint16_t)0xFFF0)  /*!< USART address Mask */

#define CR2_LINEN_Set              ((uint16_t)0x4000)  /*!< USART LIN Enable Mask */
#define CR2_LINEN_Reset            ((uint16_t)0xBFFF)  /*!< USART LIN Disable Mask */

#define CR2_LBDL_Mask             ((uint16_t)0xFFDF)  /*!< USART LIN Break detection Mask */
#define CR2_STOP_CLEAR_Mask       ((uint16_t)0xCFFF)  /*!< USART CR2 STOP Bits Mask */
#define CR2_CLOCK_CLEAR_Mask      ((uint16_t)0xF0FF)  /*!< USART CR2 Clock Mask */

#define CR3_SCEN_Set              ((uint16_t)0x0020)  /*!< USART SC Enable Mask */
#define CR3_SCEN_Reset            ((uint16_t)0xFFDF)  /*!< USART SC Disable Mask */

#define CR3_NACK_Set              ((uint16_t)0x0010)  /*!< USART SC NACK Enable Mask */
#define CR3_NACK_Reset            ((uint16_t)0xFFEF)  /*!< USART SC NACK Disable Mask */

#define CR3_HDSEL_Set             ((uint16_t)0x0008)  /*!< USART Half-Duplex Enable Mask */
#define CR3_HDSEL_Reset           ((uint16_t)0xFFF7)  /*!< USART Half-Duplex Disable Mask */

#define CR3_IRLP_Mask             ((uint16_t)0xFFFB)  /*!< USART IrDA LowPower mode Mask */
#define CR3_CLEAR_Mask            ((uint16_t)0xFCFF)  /*!< USART CR3 Mask */

#define CR3_IREN_Set              ((uint16_t)0x0002)  /*!< USART IrDA Enable Mask */
#define CR3_IREN_Reset            ((uint16_t)0xFFFD)  /*!< USART IrDA Disable Mask */
#define GTPR_LSB_Mask             ((uint16_t)0x00FF)  /*!< Guard Time Register LSB Mask */
#define GTPR_MSB_Mask             ((uint16_t)0xFF00)  /*!< Guard Time Register MSB Mask */
#define IT_Mask                   ((uint16_t)0x001F)  /*!< USART Interrupt Mask */

/* USART OverSampling-8 Mask */
#define CR1_OVER8_Set             ((u16)0x8000)  /* USART OVER8 mode Enable Mask */
#define CR1_OVER8_Reset           ((u16)0x7FFF)  /* USART OVER8 mode Disable Mask */

/* USART One Bit Sampling Mask */
#define CR3_ONEBITE_Set           ((u16)0x0800)  /* USART ONEBITE mode Enable Mask */
#define CR3_ONEBITE_Reset         ((u16)0xF7FF)  /* USART ONEBITE mode Disable Mask */

/**
  * @brief  Initializes the USARTx peripheral according to the specified
  *   parameters in the USART_InitStruct .
  * @param  error: An instance of ErrorStruct which contains the error information, 
  * @retval None
  */
bool System::USARTConfig::U_IsInitialized()
{
	//if both instances are initialized then return false
	return this->U_isInitialized && this->IsInitialized() ? true : false;
}

/**
  * @brief  Initializes the USARTx peripheral according to the specified
  *   parameters in the USART_InitStruct .
  * @param  error: An instance of ErrorStruct which contains the error information, 
  * @retval None
  */
void System::USART::ErrorHandler(SysError::ErrorStruct error)
{
	globalHal->ErrorHandle(&error);
}

/**
	* @brief  Resets the registers of USART
	*	@param  none 
	* @retval none
	*/
void System::USART::ModuleResetMode(HAL_StateTypeDef newState)
{
	if(newState == HAL_Enable)
		this->DeInit();
	else
	{
			//checks to make sure that the USARTConfig is not null and initaliazed
			if(this->initializedUsartConfig != 0 && this->initializedUsartConfig->U_IsInitialized())
			{
				this->Init(this->initializedUsartConfig);
			}
	}
}

/**
	* @brief  Resets the registers of USART and initalizes the USART Device 
	*	@param  none 
	* @retval none
	*/
void System::USART::RestartModule(void)
{
	if(this->IsInitialized)
		{
			this->DeInit();
			
			//checks to make sure that the USARTConfig is not null and initaliazed
			if(this->initializedUsartConfig != 0 && this->initializedUsartConfig->U_IsInitialized())
			{
				//checks to that no error was returned
				if(this->Init(this->initializedUsartConfig) != HAL_ERROR)
					this->IsInitialized = true; 
				
				else
					this->IsInitialized = false;
			}
			else /* !< this->initializedUsartConfig is null or its not initialized*/
				this->IsInitialized = false;
		}
}

/**
  * @brief  Initializes the USARTx peripheral according to the specified
  *   parameters in the USART_InitStruct .
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTx: where x can be 1, 2, 3 to select the USART peripheral.
  * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure
  *   that contains the configuration information for the specified USART peripheral.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::Init(System::USARTConfig *config)
{	
	uint32_t tempreg = 0x00, apbClock = 0x00;
	uint32_t integerdivider = 0x00;
  uint32_t fractionaldivider = 0x00;
  uint32_t usartxbase = 0;
	
	RCC_ClocksTypeDef RCC_ClocksStatus;	
	System::ResetClockControl *rcc = new System::ResetClockControl;
	
	usartxbase = (uint32_t)config->USARTx;
	
	/*---------------------------- USART CR2 Configuration -----------------------*/
	tempreg = config->USARTx->CR1;
	/* Clear M, PCE, PS, TE and RE bits */
	tempreg &= CR1_CLEAR_Mask;
	/* Configure the USART Word Length, Parity and mode ----------------------- */
  /* Set the M bits according to USART_WordLength value */
  /* Set PCE and PS bits according to USART_Parity value */
  /* Set TE and RE bits according to USART_Mode value */
  tempreg |= (uint32_t)config->WordLength | config->Parity |
            config->Mode;
  /* Write to USART CR1 */
	config->USARTx->CR1 = (uint16_t)tempreg;

	/*---------------------------- USART CR3 Configuration -----------------------*/  
	tempreg = config->USARTx->CR3;
  /* Clear CTSE and RTSE bits */
	tempreg &= CR3_CLEAR_Mask;
  /* Configure the USART HFC -------------------------------------------------*/
  /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
	tempreg |= config->HardwareFlowControl;
	/* Write to USART CR3 */
	config->USARTx->CR3 = (uint16_t)tempreg;
	
	/*---------------------------- USART BRR Configuration -----------------------*/
	/* Configure the USART Baud Rate -------------------------------------------*/
	rcc->GetClocksFreq(&RCC_ClocksStatus);
	if(usartxbase == USART1_BASE)
		apbClock = RCC_ClocksStatus.PCLK2_Frequency;
	else 
		apbClock = RCC_ClocksStatus.PCLK1_Frequency;
	
  /* Determine the integer part */
	if((config->USARTx->CR1 & CR1_OVER8_Set) != 0)
    /* Integer part computing in case Oversampling mode is 8 Samples */
    integerdivider = ((25 * apbClock) / (2 * (config->BuadRate)));    
	else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
    /* Integer part computing in case Oversampling mode is 16 Samples */
    integerdivider = ((25 * apbClock) / (4 * (config->BuadRate)));    
		
	tempreg = (integerdivider / 100) << 4;
	
	/* Determine the fractional part */
  fractionaldivider = integerdivider - (100 * (tempreg >> 4));
	
	/* Implement the fractional part in the register */
	if((config->USARTx->CR1 & CR1_OVER8_Set) != 0)
		tempreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
	else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
		tempreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
	
	/* Write to USART BRR */
	config->USARTx->BRR = (uint16_t)tempreg;
	
	this->IsInitialized = true;
	//set initialize instance
	this->initializedUsartConfig = config;
	return HAL_OK;
}

/**
  * @brief  Deinitializes the USARTx peripheral registers to their default reset values.
	* @param  config : an instance of USARTConfig which is valid and populated
	*   USARTx: This parameter can be one of the following values: USART1, USART2, USART3, UART4 or UART5.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::DeInit(System::USARTConfig *config)
{
	
	System::ResetClockControl *rcc = new System::ResetClockControl;
	/*Enable then disable periph*/
	if(config->USARTx == USART1)
	{
		rcc->APB2PeriphClock(System::APB2Peripheral::Usart1, HAL_Enable);
		rcc->APB2PeriphClock(System::APB2Peripheral::Usart1, HAL_Disable);
	}
	else if(config->USARTx == USART2)
	{
		rcc->APB1PeriphClock(System::APB1Peripheral::Usart2, HAL_Enable);
		rcc->APB1PeriphClock(System::APB1Peripheral::Usart2, HAL_Disable);
	}
	else if(config->USARTx == USART3)
	{
		rcc->APB1PeriphClock(System::APB1Peripheral::Usart3, HAL_Enable);
		rcc->APB1PeriphClock(System::APB1Peripheral::Usart3, HAL_Disable);
	}
	else if(config->USARTx == UART4)
	{
		rcc->APB1PeriphClock(System::APB1Peripheral::Uart4, HAL_Enable);
		rcc->APB1PeriphClock(System::APB1Peripheral::Uart4, HAL_Enable);
	}
	else if(config->USARTx == UART5)
	{
		rcc->APB1PeriphClock(System::APB1Peripheral::Uart5, HAL_Enable);
		rcc->APB1PeriphClock(System::APB1Peripheral::Uart5, HAL_Enable);
	}
	else{
		const char errorMessage[100] = "Peripharel passed want NOT valid";
		
		/*Error Struct*/
		SysError::ErrorStruct error;
		error.isCalledBack = false; 											/*<!-- Errored is not called back */
		error.errorSeverity = SysError::NoActionRequired;
		error.errorType = SysError::USART_InvalidPeriph;
		
		/*Set the error message char bits*/
		for(int i = 0; i <= 100; i++)
		{
			error.errorMessage[i] = errorMessage[i];
		}
		
		//log error
		this->ErrorHandler(error);
		
		return HAL_ERROR;
	}
	
	//instance is no longer initalized
	this->IsInitialized = false;
	
	return HAL_OK;
}

/**
	* @brief  see @ref System::USART::DeInit(System::USARTConfig *config)
	* @param  none
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::DeInit()
{
	//checks if initializedUsartConfig is points nothing 
	return this->initializedUsartConfig ? this->DeInit(this->initializedUsartConfig) : HAL_ERROR;
}


/**
	* @brief  see @ref System::USART::ClockInit(System::USARTConfig *config)
	* @param  none
  * @retval None
  */
HAL_StatusTypeDef System::USART::ClockInit()
{
	return this->initializedUsartConfig ? this->ClockInit(this->initializedUsartConfig) : HAL_ERROR;
}

/**
  * @brief  Initializes the USARTx peripheral Clock according to the 
  *   specified parameters in the USARTClockConfig.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTx: where x can be 1, 2, 3 to select the USART peripheral.
	* @note The Smart Card mode is not available for UART4 and UART5.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::ClockInit(System::USARTConfig *config)
{
		
	uint32_t tempreg = 0x00;
/*---------------------------- USART CR2 Configuration -----------------------*/
	tempreg = config->USARTx->CR2;
/* Clear CLKEN, CPOL, CPHA and LBCL bits */
	tempreg &= CR2_CLOCK_CLEAR_Mask;
	/* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
  /* Set CLKEN bit according to USART_Clock value */
  /* Set CPOL bit according to USART_CPOL value */
  /* Set CPHA bit according to USART_CPHA value */
  /* Set LBCL bit according to USART_LastBit value */
	tempreg |= (uint32_t)config->ClockConfig->Clock | config->ClockConfig->CPOL | 
						 config->ClockConfig->CPHA | config->ClockConfig->LastBit;
	/*Write to USART CR2*/
	config->USARTx->CR2 = (uint16_t)tempreg;
	
	return HAL_OK;
}

/**
	* @brief  see @ref System::USART::UsartCmd(System::USARTConfig *config)
	* @param  none
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::UsartCmd(HAL_StateTypeDef newState)
{
		return this->initializedUsartConfig ? this->UsartCmd(this->initializedUsartConfig, newState) : HAL_ERROR;
}


/**
  * @brief  Enables or disables the specified USART peripheral.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  NewState: new state of the USARTx peripheral.
  *   This parameter can be: HAL_Enable or HAL_Disable.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::UsartCmd(System::USARTConfig *config, HAL_StateTypeDef newState)
{
	
	if(newState == HAL_Enable)
	{
		/* Enable the selected USART by setting the UE bit in the CR1 register */
		config->USARTx->CR1 |= CR1_UE_Set;
	}
	else 
	{
		/* Disable the selected USART by clearing the UE bit in the CR1 register */
		config->USARTx->CR1 &= CR1_UE_Reset;
	}
	
	return HAL_OK;
}

/**
  * @brief  Enables or disables the specified USART interrupts.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  USART_IT: specifies the USART interrupt sources to be enabled or disabled.
  *   This parameter can be one of the following values:
  *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)
  *     @arg USART_IT_LBD:  LIN Break detection interrupt
  *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt
  *     @arg USART_IT_TC:   Transmission complete interrupt
  *     @arg USART_IT_RXNE: Receive Data register not empty interrupt
  *     @arg USART_IT_IDLE: Idle line detection interrupt
  *     @arg USART_IT_PE:   Parity Error interrupt
  *     @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
  * @param  NewState: new state of the specified USARTx interrupts.
  *   This parameter can be: HAL_Enable or HAL_Disable.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::InteruptConfig(System::USARTConfig *config, uint16_t USART_IT, HAL_StateTypeDef newState)
{
	
	uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
  uint32_t usartxbase = 0x00;
	
  usartxbase = (uint32_t)config->USARTx;

  /* Get the USART register index */
  usartreg = (((uint8_t)USART_IT) >> 0x05);

  /* Get the interrupt position */
  itpos = USART_IT & IT_Mask;
  itmask = (((uint32_t)0x01) << itpos);
    
  if (usartreg == 0x01) /* The IT is in CR1 register */
  {
    usartxbase += 0x0C;
  }
  else if (usartreg == 0x02) /* The IT is in CR2 register */
  {
    usartxbase += 0x10;
  }
  else /* The IT is in CR3 register */
  {
    usartxbase += 0x14; 
  }
  if (newState != HAL_Disable)
  {
    *(__IO uint32_t*)usartxbase  |= itmask;
  }
  else
  {
    *(__IO uint32_t*)usartxbase &= ~itmask;
  }	
	
	return HAL_OK;
}

/**
  * @brief  Enables or disables the USART’s DMA interface.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  DMAReq: specifies the DMA request.
  *   This parameter can be any combination of the following values:
  *     @arg USART_DMAReq_Tx: USART DMA transmit request
  *     @arg USART_DMAReq_Rx: USART DMA receive request
  * @param  NewState: new state of the DMA Request sources.
  *   This parameter can be: HAL_Enable or HAL_Disable.
  * @note The DMA mode is not available for UART5.  
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::DMACmd(System::USARTConfig *config, uint16_t DMAReq, HAL_StateTypeDef newState)
{
	
	if(newState != HAL_Disable)
		/* Enable the DMA transfer for selected requests by setting the DMAT and/or
		 DMAR bits in the USART CR3 register */
		config->USARTx->CR3 |= DMAReq;
	else
		/* Disable the DMA transfer for selected requests by clearing the DMAT and/or
		 DMAR bits in the USART CR3 register */
		config->USARTx->CR3 &= (uint16_t)~DMAReq;
	
	return HAL_OK;
}


/**
  * @brief  Sets the address of the USART node.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  Address: Indicates the address of the USART node.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::SetAddress(System::USARTConfig *config, uint8_t Address)
{
	
  /* Clear the USART address */
	config->USARTx->CR2 &= CR2_Address_Mask;
	/* Set the USART address node */
	config->USARTx->CR2 |= Address;
	
	return HAL_OK;
}

/**
  * @brief  Selects the USART WakeUp method.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  WakeUp: specifies the USART wakeup method.
  *   This parameter can be one of the following values:
  *     @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
  *     @arg USART_WakeUp_AddressMark: WakeUp by an address mark
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::WakeUpConfig(System::USARTConfig *config, uint16_t WakeUp)
{
	
	config->USARTx->CR1 &= CR1_WAKE_Mask;
	config->USARTx->CR1 |= WakeUp;
	
	return HAL_OK;
}

/**
	* @brief  see @ref System::USART::ReceiverWakeUpCmd(System::USARTConfig *config)
	* @param  none
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::ReceiverWakeUpCmd(HAL_StateTypeDef newState)
{
		return this->initializedUsartConfig ? this->ReceiverWakeUpCmd(this->initializedUsartConfig, newState) : HAL_ERROR;
}

/**
  * @brief  Determines if the USART is in mute mode or not.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  newState: new state of the USART mute mode.
  *   This parameter can be: HAL_Enable or HAL_Disable.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::ReceiverWakeUpCmd(System::USARTConfig *config, HAL_StateTypeDef newState)
{
	
	if(newState != HAL_Disable)
		/* Enable the USART mute mode  by setting the RWU bit in the CR1 register */
		config->USARTx->CR1 |= CR1_RWU_Set;
	else
		config->USARTx->CR1 &= CR1_RWU_Reset;
	
	return HAL_OK;
}

/**
  * @brief  Sets the USART LIN Break detection length.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  LINBreakDetectLength: specifies the LIN break detection length.
  *   This parameter can be one of the following values:
  *     @arg USART_LINBreakDetectLength_10b: 10-bit break detection
  *     @arg USART_LINBreakDetectLength_11b: 11-bit break detection
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::LINBreakDetectLengthConfig(System::USARTConfig *config, uint16_t LINBreakDetectLength)
{
	
	config->USARTx->CR2 &= CR2_LBDL_Mask;
	config->USARTx->CR2 |= LINBreakDetectLength;
	
	return HAL_OK;
}

/**
  * @brief  Enables or disables the USART’s LIN mode.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  newState: new state of the USART LIN mode.
  *   This parameter can be: HAL_Enable or HAl_Disable.
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
HAL_StatusTypeDef System::USART::LINCmd(System::USARTConfig *config, HAL_StateTypeDef newState)
{
		
	if(newState != HAL_Disable)
    /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
		config->USARTx->CR2 |= CR2_LINEN_Set;
	else 
		/* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
		config->USARTx->CR2 &= CR2_LINEN_Reset;
	return HAL_OK;
}

HAL_StatusTypeDef System::USART::SendData(uint16_t Data)
{
		return this->initializedUsartConfig ? this->SendData(this->initializedUsartConfig, (uint16_t )Data) : HAL_ERROR;
}

HAL_StatusTypeDef System::USART::SendData(System::USARTConfig *config, uint16_t Data)
{
	config->USARTx->DR = (Data & (uint16_t)0x01FF);
	
	return HAL_OK;
}

/**
	* @brief  see @ref System::USART::ReceiveData(System::USARTConfig *config)
	* @param  none
	* @retval HAL_StatusTypeDef: Indicates the success of the operation. 
	*					@ref HAL_StatusTypeDef
  */
uint16_t System::USART::ReceiveData()
{
	return this->initializedUsartConfig ? this->ReceiveData(this->initializedUsartConfig) : 0;
}


/**
  * @brief  Returns the most recent received data by the USARTx peripheral.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @retval The received data.
  */
uint16_t System::USART::ReceiveData(System::USARTConfig *config)
{
	
	/* Receive Data */
	return (uint16_t)(config->USARTx->DR & (uint16_t)0x01FF);
}

/**
  * @brief  Checks whether the specified USART flag is set or not.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  FLAG: specifies the flag to check.
  *   This parameter can be one of the following values:
  *     @arg USART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5)
  *     @arg USART_FLAG_LBD:  LIN Break detection flag
  *     @arg USART_FLAG_TXE:  Transmit data register empty flag
  *     @arg USART_FLAG_TC:   Transmission Complete flag
  *     @arg USART_FLAG_RXNE: Receive data register not empty flag
  *     @arg USART_FLAG_IDLE: Idle Line detection flag
  *     @arg USART_FLAG_ORE:  OverRun Error flag
  *     @arg USART_FLAG_NE:   Noise Error flag
  *     @arg USART_FLAG_FE:   Framing Error flag
  *     @arg USART_FLAG_PE:   Parity Error flag
  * @retval The new state of USART_FLAG (SET or RESET).
  */
FlagStatus System::USART::GetFlagStatus(System::USARTConfig *config, uint16_t FLAG)
{
	FlagStatus bitstatus = RESET;
	
  if ((config->USARTx->SR & FLAG) != (uint16_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
   bitstatus = RESET;
  }
	return bitstatus;
}

/**
  * @brief  Clears the USARTx's pending flags.
	* @param config: an instance of USARTConfig that has been inialized
  * @config->USARTxThis parameter can be one of the following values:
  *   USART1, USART2, USART3, UART4 or UART5.
  * @param  FLAG: specifies the flag to clear.
  *   This parameter can be any combination of the following values:
  *     @arg USART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5).
  *     @arg USART_FLAG_LBD:  LIN Break detection flag.
  *     @arg USART_FLAG_TC:   Transmission Complete flag.
  *     @arg USART_FLAG_RXNE: Receive data register not empty flag.
  *   
  * @note
  *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun 
  *     error) and IDLE (Idle line detected) flags are cleared by software 
  *     sequence: a read operation to USART_SR register (USART_GetFlagStatus()) 
  *     followed by a read operation to USART_DR register (USART_ReceiveData()).
  *   - RXNE flag can be also cleared by a read to the USART_DR register 
  *     (USART_ReceiveData()).
  *   - TC flag can be also cleared by software sequence: a read operation to 
  *     USART_SR register (USART_GetFlagStatus()) followed by a write operation
  *     to USART_DR register (USART_SendData()).
  *   - TXE flag is cleared only by a write to the USART_DR register 
  *     (USART_SendData()).
  * @retval None
  */
void System::USART::ClearFlag(System::USARTConfig *config, uint16_t FLAG)
{
	config->USARTx->SR = (uint16_t)~FLAG;
}


Create a new paste based on this one


Comments: