Choco OS  V.0.16.9.0
Join to the chocolate world
oc_uart.c
Go to the documentation of this file.
1 
27 #include <oc_uart.h>
28 #include <oc_gpio.h>
29 #include <oc_object.h>
30 #include <oc_uart_lld.h>
31 #include <oc_stdlib.h>
32 #include <oc_array.h>
33 #include <oc_intman.h>
34 #include <oc_mutex.h>
35 #include <oc_memman.h>
36 #include <oc_event.h>
37 #include <oc_ktime.h>
38 
44 #define _________________________________________DRIVER_DEFINITIONS_SECTION_________________________________________________________________
45 
46 #define DRIVER_SOURCE
47 
48 /* Driver basic definitions */
49 #define DRIVER_NAME UART
50 #define DRIVER_FILE_NAME "uart"
51 #define DRIVER_TYPE COMMUNICATION_DRIVER
52 #define DRIVER_VERSION oC_Driver_MakeVersion(1,0,0)
53 #define REQUIRED_DRIVERS &GPIO
54 #define REQUIRED_BOOT_LEVEL oC_Boot_Level_RequireClock | oC_Boot_Level_RequireMemoryManager | oC_Boot_Level_RequireDriversManager
55 
56 /* Driver interface definitions */
57 #define DRIVER_CONFIGURE oC_UART_Configure
58 #define DRIVER_UNCONFIGURE oC_UART_Unconfigure
59 #define DRIVER_TURN_ON oC_UART_TurnOn
60 #define DRIVER_TURN_OFF oC_UART_TurnOff
61 #define IS_TURNED_ON oC_UART_IsTurnedOn
62 #define READ_FROM_DRIVER oC_UART_Read
63 #define WRITE_TO_DRIVER oC_UART_Write
64 #define HANDLE_IOCTL oC_UART_Ioctl
65 
66 #undef _________________________________________DRIVER_DEFINITIONS_SECTION_________________________________________________________________
67 
68 /* This should be always at the end of includes list */
69 #include <oc_driver.h>
70 
76 #define _________________________________________TYPES_SECTION______________________________________________________________________________
77 
78 struct Context_t
79 {
81  oC_UART_Channel_t Channel;
82  oC_UART_Dma_t Dma;
83  oC_UART_Mode_t Mode;
84 };
85 
86 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
87 
88 
94 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
95 
96 static bool ModuleEnabledFlag = false;
97 static bool InterruptsConfigured= false;
98 static oC_Event_t RxNotEmptyEvent = NULL;
99 static oC_Event_t TxNotFullEvent = NULL;
100 static const oC_Allocator_t ModuleAllocator = {
101  .Name = "uart"
102 };
103 
104 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
105 
111 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
112 
113 static inline bool IsConfigCorrect( const oC_UART_Config_t * Config );
114 static inline bool IsContextCorrect( oC_UART_Context_t Context );
115 static oC_ErrorCode_t FindFreeChannelForPins( oC_Pins_t Tx , oC_Pins_t Rx , oC_UART_Channel_t * outChannel );
116 static void RxNotEmptyInterruptHandler( oC_UART_Channel_t Channel );
117 static void TxNotFullInterruptHandler( oC_UART_Channel_t Channel );
118 
119 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
120 
121 
127 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
128 
129 //==========================================================================================================================================
130 //==========================================================================================================================================
131 oC_ErrorCode_t oC_UART_TurnOn( void )
132 {
133  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
134 
135  if(oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == false , oC_ErrorCode_ModuleIsTurnedOn))
136  {
137  errorCode = oC_UART_LLD_TurnOnDriver();
138 
139  if(errorCode == oC_ErrorCode_None || errorCode == oC_ErrorCode_ModuleIsTurnedOn)
140  {
141  errorCode = oC_UART_LLD_SetDriverInterruptHandlers(RxNotEmptyInterruptHandler,TxNotFullInterruptHandler);
142 
143  if(oC_ErrorOccur(errorCode))
144  {
145  oC_SaveError("UART: Cannot set driver interrupt handler: " , errorCode);
146  errorCode = oC_ErrorCode_None;
147  }
148  else
149  {
150  RxNotEmptyEvent = oC_Event_New(oC_Event_State_Inactive , &ModuleAllocator , AllocationFlags_CanWait1Second);
151  TxNotFullEvent = oC_Event_New(oC_Event_State_Inactive , &ModuleAllocator , AllocationFlags_CanWait1Second);
152  InterruptsConfigured = oC_Event_IsCorrect(RxNotEmptyEvent);
153  }
154 
155  ModuleEnabledFlag = true;
156  }
157 
158  }
159 
160  return errorCode;
161 }
162 
163 //==========================================================================================================================================
164 //==========================================================================================================================================
165 oC_ErrorCode_t oC_UART_TurnOff( void )
166 {
167  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
168 
169  if(oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet))
170  {
171  ModuleEnabledFlag = false;
172  errorCode = oC_UART_LLD_TurnOffDriver();
173 
174  if(errorCode == oC_ErrorCode_None || errorCode == oC_ErrorCode_ModuleNotStartedYet)
175  {
176  if(InterruptsConfigured && !oC_Event_Delete(&RxNotEmptyEvent , AllocationFlags_CanWaitForever))
177  {
178  errorCode = oC_ErrorCode_ReleaseError;
179  }
180  else
181  {
182  errorCode = oC_ErrorCode_None;
183  }
184  }
185  }
186 
187  return errorCode;
188 }
189 
190 //==========================================================================================================================================
191 //==========================================================================================================================================
192 bool oC_UART_IsTurnedOn( void )
193 {
194  return ModuleEnabledFlag;
195 }
196 
197 //==========================================================================================================================================
198 //==========================================================================================================================================
199 oC_ErrorCode_t oC_UART_Configure( const oC_UART_Config_t * Config , oC_UART_Context_t * outContext )
200 {
201  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
202 
203  if(
204  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
205  oC_AssignErrorCodeIfFalse(&errorCode , IsConfigCorrect(Config) , oC_ErrorCode_WrongConfigAddress ) &&
206  oC_AssignErrorCodeIfFalse(&errorCode , isram(outContext) , oC_ErrorCode_OutputAddressNotInRAM ) &&
207  oC_AssignErrorCodeIfFalse(&errorCode , Config->Dma <= oC_UART_Dma_DontUse, oC_ErrorCode_DmaModeNotCorrect )
208  )
209  {
210  oC_UART_Channel_t channel = 0;
211 
212  if(
213  oC_AssignErrorCode(&errorCode,FindFreeChannelForPins(Config->Tx,Config->Rx,&channel))
214  )
215  {
216  struct Context_t * context = ksmartalloc(sizeof(struct Context_t),&ModuleAllocator,AllocationFlags_CanWait1Second);
217 
218  if(
219  oC_AssignErrorCodeIfFalse(&errorCode , oC_MemMan_IsDynamicAllocatedAddress(context) , oC_ErrorCode_AllocationError ) &&
220  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetPower(channel,oC_Power_On)) &&
221  oC_AssignErrorCode(&errorCode , oC_UART_LLD_DisableOperations(channel)) &&
222  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetBitRate(channel,Config->BitRate)) &&
223  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetWordLength(channel,Config->WordLength)) &&
224  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetParity(channel,Config->Parity)) &&
225  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetStopBit(channel,Config->StopBit)) &&
226  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetBitOrder(channel,Config->BitOrder)) &&
227  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetInvert(channel,Config->Invert)) &&
228  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetLoopback(channel,Config->Loopback)) &&
229  oC_AssignErrorCode(&errorCode , oC_UART_LLD_EnableOperations(channel))
230  )
231  {
232  context->ObjectControl = oC_CountObjectControl(context,oC_ObjectId_UartContext);
233  context->Channel = channel;
234  context->Dma = Config->Dma;
235 
236  *outContext = context;
237 
238  errorCode = oC_ErrorCode_None;
239  }
240  else
241  {
242  oC_UART_LLD_RestoreDefaultStateOnChannel(channel);
243  ksmartfree(context,sizeof(struct Context_t),AllocationFlags_CanWaitForever);
244  }
245  }
246  }
247 
248  return errorCode;
249 }
250 
251 //==========================================================================================================================================
252 //==========================================================================================================================================
253 oC_ErrorCode_t oC_UART_Unconfigure( const oC_UART_Config_t * Config , oC_UART_Context_t * outContext )
254 {
255  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
256 
257  if(
258  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
259  oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(*outContext) , oC_ErrorCode_ContextNotCorrect )
260  )
261  {
262  oC_UART_Context_t context = *outContext;
263 
264  if(
265  oC_AssignErrorCodeIfFalse(&errorCode , oC_UART_LLD_RestoreDefaultStateOnChannel(context->Channel) , oC_ErrorCode_CannotRestoreDefaultState ) &&
266  oC_AssignErrorCodeIfFalse(&errorCode , ksmartfree(context,sizeof(struct Context_t),AllocationFlags_CanWaitForever) , oC_ErrorCode_ReleaseError )
267  )
268  {
269  *outContext = NULL;
270  errorCode = oC_ErrorCode_None;
271  }
272 
273  }
274 
275  return errorCode;
276 }
277 
278 //==========================================================================================================================================
279 //==========================================================================================================================================
280 oC_ErrorCode_t oC_UART_Read( oC_UART_Context_t Context , char * outBuffer , uint32_t * Size , oC_Time_t Timeout )
281 {
282  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
283 
284  if(
285  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
286  oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
287  oC_AssignErrorCodeIfFalse(&errorCode , isram(outBuffer) , oC_ErrorCode_OutputAddressNotInRAM ) &&
288  oC_AssignErrorCodeIfFalse(&errorCode , isram(Size) , oC_ErrorCode_OutputAddressNotInRAM ) &&
289  oC_AssignErrorCodeIfFalse(&errorCode , (*Size) > 0 , oC_ErrorCode_SizeNotCorrect )
290  )
291  {
292  if(Context->Dma != oC_UART_Dma_DontUse)
293  {
294  errorCode = oC_UART_LLD_ReadWithDma(Context->Channel,outBuffer,*Size);
295  }
296 
297  if(oC_ErrorOccur(errorCode))
298  {
299  oC_Event_SetState(RxNotEmptyEvent,oC_Event_State_Inactive);
300 
301  oC_Time_t timeStart = oC_KTime_GetTimestamp();
302  oC_Time_t timeEnd = timeStart + Timeout;
303  uint32_t readBytes = 0;
304 
305  if(oC_UART_LLD_IsRxFifoEmpty(Context->Channel) && !oC_Event_WaitForState(RxNotEmptyEvent , Context->Channel , oC_Event_StateMask_Full , Timeout ))
306  {
307  errorCode = oC_ErrorCode_Timeout;
308  }
309  else
310  {
311  errorCode = oC_ErrorCode_NoneElementReceived;
312 
313  for(readBytes = 0 ; readBytes < (*Size) ; readBytes++)
314  {
315  if(oC_UART_LLD_IsRxFifoEmpty(Context->Channel))
316  {
317  break;
318  }
319  errorCode = oC_ErrorCode_NoAllBytesRead;
320 
321  outBuffer[readBytes] = oC_UART_LLD_GetData(Context->Channel);
322  Timeout = timeEnd - oC_KTime_GetTimestamp();
323  }
324 
325  if((*Size) == readBytes)
326  {
327  errorCode = oC_ErrorCode_None;
328  }
329  else
330  {
331  *Size = readBytes;
332  }
333  }
334  }
335  }
336 
337  return errorCode;
338 }
339 
340 //==========================================================================================================================================
341 //==========================================================================================================================================
342 oC_ErrorCode_t oC_UART_Write( oC_UART_Context_t Context , const char * Buffer , uint32_t * Size , oC_Time_t Timeout )
343 {
344  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
345 
346  if(
347  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
348  oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
349  oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Buffer) , oC_ErrorCode_WrongAddress) &&
350  oC_AssignErrorCodeIfFalse(&errorCode , isram(Size) , oC_ErrorCode_OutputAddressNotInRAM ) &&
351  oC_AssignErrorCodeIfFalse(&errorCode , (*Size) > 0 , oC_ErrorCode_SizeNotCorrect )
352  )
353  {
354  if(Context->Dma != oC_UART_Dma_DontUse)
355  {
356  errorCode = oC_UART_LLD_WriteWithDma(Context->Channel,Buffer,*Size);
357  }
358 
359  if(oC_ErrorOccur(errorCode) && (Context->Dma != oC_UART_Dma_AlwaysUse))
360  {
361  uint32_t sentBytes = 0;
362 
363  oC_Event_SetState(TxNotFullEvent,oC_Event_State_Inactive);
364 
365  if(oC_UART_LLD_IsTxFifoFull(Context->Channel) && !oC_Event_WaitForState(TxNotFullEvent , Context->Channel , oC_Event_StateMask_Full , Timeout ))
366  {
367  errorCode = oC_ErrorCode_Timeout;
368  }
369  else
370  {
371  errorCode = oC_ErrorCode_NotAllSent;
372 
373  for(sentBytes = 0 ; sentBytes < (*Size) ; sentBytes++)
374  {
375  if(oC_UART_LLD_IsTxFifoFull(Context->Channel))
376  {
377  break;
378  }
379  oC_UART_LLD_PutData(Context->Channel,Buffer[sentBytes]);
380  }
381  if((*Size) == sentBytes)
382  {
383  errorCode = oC_ErrorCode_None;
384  }
385  else
386  {
387  *Size = sentBytes;
388  }
389  }
390  }
391  }
392 
393  return errorCode;
394 }
395 
396 //==========================================================================================================================================
397 //==========================================================================================================================================
398 oC_ErrorCode_t oC_UART_Ioctl( oC_UART_Context_t Context , oC_Ioctl_Command_t Command , void * Data )
399 {
400  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
401 
402  if(
403  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
404  oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
405  oC_AssignErrorCodeIfFalse(&errorCode , oC_Ioctl_IsCorrectCommand(Command) , oC_ErrorCode_CommandNotCorrect )
406  )
407  {
408  switch(Command)
409  {
410  //==============================================================================================================================
411  /*
412  * Returns driver version
413  */
414  //==============================================================================================================================
415  case oC_IoCtl_SpecialCommand_GetDriverVersion:
416  if(isram(Data))
417  {
418  *((uint32_t*)Data) = UART.Version;
419  errorCode = oC_ErrorCode_None;
420  }
421  else
422  {
423  errorCode = oC_ErrorCode_OutputAddressNotInRAM;
424  }
425  break;
426  //==============================================================================================================================
427  /*
428  * Enables UART operations
429  */
430  //==============================================================================================================================
431  case oC_Ioctl_NormalCommand_Enable:
432  errorCode = oC_UART_Enable(Context);
433  break;
434  //==============================================================================================================================
435  /*
436  * Disables UART operations
437  */
438  //==============================================================================================================================
439  case oC_Ioctl_NormalCommand_Disable:
440  errorCode = oC_UART_Disable(Context);
441  break;
442  //==============================================================================================================================
443  /*
444  * Clears Rx FIFO
445  */
446  //==============================================================================================================================
447  case oC_IoCtl_SpecialCommand_ClearRxFifo:
448  errorCode = oC_UART_LLD_ClearRxFifo(Context->Channel);
449  break;
450  //==============================================================================================================================
451  /*
452  * Not handled commands
453  */
454  //==============================================================================================================================
455  default:
456  errorCode = oC_ErrorCode_CommandNotHandled;
457  break;
458  }
459  }
460 
461  return errorCode;
462 }
463 
464 //==========================================================================================================================================
465 //==========================================================================================================================================
466 oC_ErrorCode_t oC_UART_Disable( oC_UART_Context_t Context )
467 {
468  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
469 
470  if(
471  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
472  oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect )
473  )
474  {
475  errorCode = oC_UART_LLD_DisableOperations(Context->Channel);
476  }
477 
478  return errorCode;
479 }
480 
481 //==========================================================================================================================================
482 //==========================================================================================================================================
483 oC_ErrorCode_t oC_UART_Enable( oC_UART_Context_t Context )
484 {
485  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
486 
487  if(
488  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet ) &&
489  oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect )
490  )
491  {
492  errorCode = oC_UART_LLD_EnableOperations(Context->Channel);
493  }
494 
495  return errorCode;
496 }
497 
498 
499 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
500 
506 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
507 
508 //==========================================================================================================================================
509 //==========================================================================================================================================
510 static inline bool IsConfigCorrect( const oC_UART_Config_t * Config )
511 {
512  return isaddresscorrect(Config);
513 }
514 
515 //==========================================================================================================================================
516 //==========================================================================================================================================
517 static inline bool IsContextCorrect( oC_UART_Context_t Context )
518 {
519  return isram(Context) && oC_CheckObjectControl(Context,oC_ObjectId_UartContext,Context->ObjectControl);
520 }
521 
522 //==========================================================================================================================================
523 //==========================================================================================================================================
524 static oC_ErrorCode_t FindFreeChannelForPins( oC_Pins_t Tx , oC_Pins_t Rx , oC_UART_Channel_t * outChannel )
525 {
526  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
527  oC_UART_Pin_t * txPinsArray = NULL;
528  oC_UART_Pin_t * rxPinsArray = NULL;
529  uint32_t txArraySize = 0;
530  uint32_t rxArraySize = 0;
531  oC_UART_Pin_t txModulePin = 0;
532  oC_UART_Pin_t rxModulePin = 0;
533  bool txModulePinFound = false;
534  bool rxModulePinFound = false;
535 
536  oC_IntMan_EnterCriticalSection();
537 
538  /* *******************************************************************
539  * Transmit/Receive Mode
540  * ******************************************************************/
541  if(Tx != 0 && Rx != 0)
542  {
543  /* Searching for size of module pins arrays */
544  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Tx,NULL,&txArraySize,oC_UART_PinFunction_Tx)) &&
545  oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Rx,NULL,&rxArraySize,oC_UART_PinFunction_Rx)) &&
546  ErrorCondition(txArraySize > 0 && rxArraySize > 0 , oC_ErrorCode_NoneOfModulePinAssigned)
547  )
548  {
549  /* Allocation memory for module pins array */
550  txPinsArray = ksmartalloc(sizeof(oC_UART_Pin_t)*txArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
551  rxPinsArray = ksmartalloc(sizeof(oC_UART_Pin_t)*rxArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
552 
553  if(ErrorCondition(txPinsArray != NULL && rxPinsArray != NULL , oC_ErrorCode_AllocationError))
554  {
555  /* Searching for size of module pins arrays */
556  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Tx,txPinsArray,&txArraySize,oC_UART_PinFunction_Tx)) &&
557  oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Rx,rxPinsArray,&rxArraySize,oC_UART_PinFunction_Rx))
558  )
559  {
560  /* Searching for free Tx and Rx pins with the same channel */
561  oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(txPinsArray,txArraySize,txPin)
562  {
563  oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(rxPinsArray,rxArraySize,rxPin)
564  {
565  oC_UART_Channel_t txChannel = oC_UART_LLD_GetChannelOfModulePin(*txPin);
566  oC_UART_Channel_t rxChannel = oC_UART_LLD_GetChannelOfModulePin(*rxPin);
567 
568 
569  if(ErrorCondition(txChannel == rxChannel , oC_ErrorCode_WrongModulePinChannel) &&
570  ErrorCondition(!oC_UART_LLD_IsChannelUsed(txChannel), oC_ErrorCode_ChannelIsUsed)
571  )
572  {
573  *outChannel = txChannel;
574  txModulePin = *txPin;
575  rxModulePin = *rxPin;
576  txModulePinFound = true;
577  rxModulePinFound = true;
578  break;
579  }
580  }
581  }
582  }
583  }
584  }
585  }
586  /* *******************************************************************
587  * Transmit Only Mode
588  * ******************************************************************/
589  else if(Tx != 0)
590  {
591  /* Searching for size of module pins arrays */
592  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Tx,NULL,&txArraySize,oC_UART_PinFunction_Tx)) &&
593  ErrorCondition(txArraySize > 0 , oC_ErrorCode_NoneOfModulePinAssigned)
594  )
595  {
596  /* Allocation memory for module pins array */
597  txPinsArray = ksmartalloc(sizeof(oC_UART_Pin_t)*txArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
598 
599  if(ErrorCondition(txPinsArray != NULL , oC_ErrorCode_AllocationError))
600  {
601  /* Searching for size of module pins arrays */
602  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Tx,txPinsArray,&txArraySize,oC_UART_PinFunction_Tx)))
603  {
604  /* Searching for free Tx pins with the same channel */
605  oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(txPinsArray,txArraySize,txPin)
606  {
607  oC_UART_Channel_t txChannel = oC_UART_LLD_GetChannelOfModulePin(*txPin);
608 
609  if(ErrorCondition(!oC_UART_LLD_IsChannelUsed(txChannel) , oC_ErrorCode_ChannelIsUsed))
610  {
611  *outChannel = txChannel;
612  txModulePin = *txPin;
613  txModulePinFound = true;
614  break;
615  }
616  }
617  }
618  }
619  }
620  }
621  /* *******************************************************************
622  * Receive Only Mode
623  * ******************************************************************/
624  else if(Rx != 0)
625  {
626  /* Searching for size of module pins arrays */
627  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Rx,NULL,&rxArraySize,oC_UART_PinFunction_Rx)) &&
628  ErrorCondition(rxArraySize > 0 , oC_ErrorCode_NoneOfModulePinAssigned)
629  )
630  {
631  /* Allocation memory for module pins array */
632  rxPinsArray = ksmartalloc(sizeof(oC_UART_Pin_t)*rxArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
633 
634  if(ErrorCondition(rxPinsArray != NULL , oC_ErrorCode_AllocationError))
635  {
636  /* Searching for size of module pins arrays */
637  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Rx,rxPinsArray,&rxArraySize,oC_UART_PinFunction_Rx)))
638  {
639  /* Searching for free Rx pins with the same channel */
640  oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(rxPinsArray,rxArraySize,rxPin)
641  {
642  oC_UART_Channel_t rxChannel = oC_UART_LLD_GetChannelOfModulePin(*rxPin);
643 
644  if(ErrorCondition(!oC_UART_LLD_IsChannelUsed(rxChannel) , oC_ErrorCode_ChannelIsUsed))
645  {
646  *outChannel = rxChannel;
647  rxModulePin = *rxPin;
648  rxModulePinFound = true;
649  break;
650  }
651  }
652  }
653  }
654  }
655  }
656  /* *******************************************************************
657  * None pin is given
658  * ******************************************************************/
659  else
660  {
661  errorCode = oC_ErrorCode_ModulePinIsNotGiven;
662  }
663 
664  /* *******************************************************************
665  * Both pins found
666  * ******************************************************************/
667  if(txModulePinFound && rxModulePinFound)
668  {
669  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ConnectModulePin(txModulePin)) &&
670  oC_AssignErrorCode(&errorCode , oC_UART_LLD_ConnectModulePin(rxModulePin)) &&
671  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetChannelUsed(*outChannel))
672  )
673  {
674  errorCode = oC_ErrorCode_None;
675  }
676  else
677  {
678  oC_UART_LLD_DisconnectModulePin(txModulePin);
679  oC_UART_LLD_DisconnectModulePin(rxModulePin);
680  }
681  }
682  /* *******************************************************************
683  * Transmit pin found
684  * ******************************************************************/
685  else if(txModulePin)
686  {
687  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ConnectModulePin(txModulePin)) &&
688  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetChannelUsed(*outChannel))
689  )
690  {
691  errorCode = oC_ErrorCode_None;
692  }
693  else
694  {
695  oC_UART_LLD_DisconnectModulePin(txModulePin);
696  }
697  }
698  /* *******************************************************************
699  * Receive pin found
700  * ******************************************************************/
701  else if(rxModulePin)
702  {
703  if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ConnectModulePin(rxModulePin)) &&
704  oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetChannelUsed(*outChannel))
705  )
706  {
707  errorCode = oC_ErrorCode_None;
708  }
709  else
710  {
711  oC_UART_LLD_DisconnectModulePin(rxModulePin);
712  }
713  }
714  /* *******************************************************************
715  * None of pin found
716  * ******************************************************************/
717  else
718  {
719  /* Module pin not found */
720  }
721 
722  oC_IntMan_ExitCriticalSection();
723 
724  /* *******************************************************************
725  * Releasing memory
726  * ******************************************************************/
727  if(txPinsArray != NULL)
728  {
729  if( !ksmartfree(txPinsArray,sizeof(oC_UART_Pin_t)*txArraySize,AllocationFlags_CanWait1Second) )
730  {
731  oC_SaveError("UART:FindFreeChannelForPins" , oC_ErrorCode_ReleaseError);
732  }
733  }
734  if(rxPinsArray != NULL)
735  {
736  if( !ksmartfree(rxPinsArray,sizeof(oC_UART_Pin_t)*rxArraySize,AllocationFlags_CanWait1Second) )
737  {
738  oC_SaveError("UART:FindFreeChannelForPins" , oC_ErrorCode_ReleaseError);
739  }
740  }
741 
742  return errorCode;
743 }
744 
745 //==========================================================================================================================================
746 //==========================================================================================================================================
747 static void RxNotEmptyInterruptHandler( oC_UART_Channel_t Channel )
748 {
749  oC_Event_SetState(RxNotEmptyEvent,Channel);
750 }
751 
752 //==========================================================================================================================================
753 //==========================================================================================================================================
754 static void TxNotFullInterruptHandler( oC_UART_Channel_t Channel )
755 {
756  oC_Event_SetState(TxNotFullEvent,Channel);
757 }
758 
759 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
Something is powered on.
Definition: oc_stdtypes.h:252
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
Definition: oc_event.c:245
static bool ModuleEnabledFlag
Definition: oc_memman.c:211
const char * Name
Definition: oc_stdlib.h:161
The file with interface for the GPIO driver.
identifier for allocations
Definition: oc_stdlib.h:159
The file with helper macros for managing objects.
The file with interface for driver creating.
bool oC_MemMan_IsDynamicAllocatedAddress(const void *Address)
checks if address is in dynamic allocated section
Definition: oc_memman.c:1552
uint32_t oC_ObjectControl_t
stores object control value
Definition: oc_object.h:141
The file with interface for event module.
The file with interface for interrupt manager.
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
Definition: oc_object.h:168
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
Definition: oc_object.h:203
The file with interface for mutex managing.
oC_ObjectControl_t ObjectControl
Definition: oc_eth.c:100
stores ETH context
Definition: oc_eth.c:97
Static array definitions.
The file with memory manager interface.
The file with interface for UART driver.
The file with interface of kernel time module.
#define NULL
pointer to a zero
Definition: oc_null.h:37