30 #include <oc_uart_lld.h> 31 #include <oc_stdlib.h> 44 #define _________________________________________DRIVER_DEFINITIONS_SECTION_________________________________________________________________ 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 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 66 #undef _________________________________________DRIVER_DEFINITIONS_SECTION_________________________________________________________________ 76 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 81 oC_UART_Channel_t Channel;
86 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 94 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 97 static bool InterruptsConfigured=
false;
104 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 111 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 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 );
119 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 127 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 131 oC_ErrorCode_t oC_UART_TurnOn(
void )
133 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
135 if(oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
false , oC_ErrorCode_ModuleIsTurnedOn))
137 errorCode = oC_UART_LLD_TurnOnDriver();
139 if(errorCode == oC_ErrorCode_None || errorCode == oC_ErrorCode_ModuleIsTurnedOn)
141 errorCode = oC_UART_LLD_SetDriverInterruptHandlers(RxNotEmptyInterruptHandler,TxNotFullInterruptHandler);
143 if(oC_ErrorOccur(errorCode))
145 oC_SaveError(
"UART: Cannot set driver interrupt handler: " , errorCode);
146 errorCode = oC_ErrorCode_None;
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);
165 oC_ErrorCode_t oC_UART_TurnOff(
void )
167 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
169 if(oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet))
172 errorCode = oC_UART_LLD_TurnOffDriver();
174 if(errorCode == oC_ErrorCode_None || errorCode == oC_ErrorCode_ModuleNotStartedYet)
176 if(InterruptsConfigured && !oC_Event_Delete(&RxNotEmptyEvent , AllocationFlags_CanWaitForever))
178 errorCode = oC_ErrorCode_ReleaseError;
182 errorCode = oC_ErrorCode_None;
192 bool oC_UART_IsTurnedOn(
void )
201 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
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 )
210 oC_UART_Channel_t channel = 0;
213 oC_AssignErrorCode(&errorCode,FindFreeChannelForPins(Config->Tx,Config->Rx,&channel))
216 struct Context_t * context = ksmartalloc(
sizeof(
struct Context_t),&ModuleAllocator,AllocationFlags_CanWait1Second);
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))
233 context->Channel = channel;
234 context->Dma = Config->Dma;
236 *outContext = context;
238 errorCode = oC_ErrorCode_None;
242 oC_UART_LLD_RestoreDefaultStateOnChannel(channel);
243 ksmartfree(context,
sizeof(
struct Context_t),AllocationFlags_CanWaitForever);
255 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
258 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
259 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(*outContext) , oC_ErrorCode_ContextNotCorrect )
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 )
270 errorCode = oC_ErrorCode_None;
280 oC_ErrorCode_t oC_UART_Read(
oC_UART_Context_t Context ,
char * outBuffer , uint32_t * Size , oC_Time_t Timeout )
282 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
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 )
292 if(Context->Dma != oC_UART_Dma_DontUse)
294 errorCode = oC_UART_LLD_ReadWithDma(Context->Channel,outBuffer,*Size);
297 if(oC_ErrorOccur(errorCode))
299 oC_Event_SetState(RxNotEmptyEvent,oC_Event_State_Inactive);
301 oC_Time_t timeStart = oC_KTime_GetTimestamp();
302 oC_Time_t timeEnd = timeStart + Timeout;
303 uint32_t readBytes = 0;
305 if(oC_UART_LLD_IsRxFifoEmpty(Context->Channel) && !
oC_Event_WaitForState(RxNotEmptyEvent , Context->Channel , oC_Event_StateMask_Full , Timeout ))
307 errorCode = oC_ErrorCode_Timeout;
311 errorCode = oC_ErrorCode_NoneElementReceived;
313 for(readBytes = 0 ; readBytes < (*Size) ; readBytes++)
315 if(oC_UART_LLD_IsRxFifoEmpty(Context->Channel))
319 errorCode = oC_ErrorCode_NoAllBytesRead;
321 outBuffer[readBytes] = oC_UART_LLD_GetData(Context->Channel);
322 Timeout = timeEnd - oC_KTime_GetTimestamp();
325 if((*Size) == readBytes)
327 errorCode = oC_ErrorCode_None;
342 oC_ErrorCode_t oC_UART_Write(
oC_UART_Context_t Context ,
const char * Buffer , uint32_t * Size , oC_Time_t Timeout )
344 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
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 )
354 if(Context->Dma != oC_UART_Dma_DontUse)
356 errorCode = oC_UART_LLD_WriteWithDma(Context->Channel,Buffer,*Size);
359 if(oC_ErrorOccur(errorCode) && (Context->Dma != oC_UART_Dma_AlwaysUse))
361 uint32_t sentBytes = 0;
363 oC_Event_SetState(TxNotFullEvent,oC_Event_State_Inactive);
365 if(oC_UART_LLD_IsTxFifoFull(Context->Channel) && !
oC_Event_WaitForState(TxNotFullEvent , Context->Channel , oC_Event_StateMask_Full , Timeout ))
367 errorCode = oC_ErrorCode_Timeout;
371 errorCode = oC_ErrorCode_NotAllSent;
373 for(sentBytes = 0 ; sentBytes < (*Size) ; sentBytes++)
375 if(oC_UART_LLD_IsTxFifoFull(Context->Channel))
379 oC_UART_LLD_PutData(Context->Channel,Buffer[sentBytes]);
381 if((*Size) == sentBytes)
383 errorCode = oC_ErrorCode_None;
398 oC_ErrorCode_t oC_UART_Ioctl(
oC_UART_Context_t Context , oC_Ioctl_Command_t Command ,
void * Data )
400 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
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 )
415 case oC_IoCtl_SpecialCommand_GetDriverVersion:
418 *((uint32_t*)Data) = UART.Version;
419 errorCode = oC_ErrorCode_None;
423 errorCode = oC_ErrorCode_OutputAddressNotInRAM;
431 case oC_Ioctl_NormalCommand_Enable:
432 errorCode = oC_UART_Enable(Context);
439 case oC_Ioctl_NormalCommand_Disable:
440 errorCode = oC_UART_Disable(Context);
447 case oC_IoCtl_SpecialCommand_ClearRxFifo:
448 errorCode = oC_UART_LLD_ClearRxFifo(Context->Channel);
456 errorCode = oC_ErrorCode_CommandNotHandled;
468 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
471 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
472 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect )
475 errorCode = oC_UART_LLD_DisableOperations(Context->Channel);
485 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
488 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
489 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect )
492 errorCode = oC_UART_LLD_EnableOperations(Context->Channel);
499 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 506 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 512 return isaddresscorrect(Config);
524 static oC_ErrorCode_t FindFreeChannelForPins( oC_Pins_t Tx , oC_Pins_t Rx , oC_UART_Channel_t * outChannel )
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;
536 oC_IntMan_EnterCriticalSection();
541 if(Tx != 0 && Rx != 0)
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)
550 txPinsArray = ksmartalloc(
sizeof(oC_UART_Pin_t)*txArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
551 rxPinsArray = ksmartalloc(
sizeof(oC_UART_Pin_t)*rxArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
553 if(ErrorCondition(txPinsArray !=
NULL && rxPinsArray !=
NULL , oC_ErrorCode_AllocationError))
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))
561 oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(txPinsArray,txArraySize,txPin)
563 oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(rxPinsArray,rxArraySize,rxPin)
565 oC_UART_Channel_t txChannel = oC_UART_LLD_GetChannelOfModulePin(*txPin);
566 oC_UART_Channel_t rxChannel = oC_UART_LLD_GetChannelOfModulePin(*rxPin);
569 if(ErrorCondition(txChannel == rxChannel , oC_ErrorCode_WrongModulePinChannel) &&
570 ErrorCondition(!oC_UART_LLD_IsChannelUsed(txChannel), oC_ErrorCode_ChannelIsUsed)
573 *outChannel = txChannel;
574 txModulePin = *txPin;
575 rxModulePin = *rxPin;
576 txModulePinFound =
true;
577 rxModulePinFound =
true;
592 if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Tx,
NULL,&txArraySize,oC_UART_PinFunction_Tx)) &&
593 ErrorCondition(txArraySize > 0 , oC_ErrorCode_NoneOfModulePinAssigned)
597 txPinsArray = ksmartalloc(
sizeof(oC_UART_Pin_t)*txArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
599 if(ErrorCondition(txPinsArray !=
NULL , oC_ErrorCode_AllocationError))
602 if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Tx,txPinsArray,&txArraySize,oC_UART_PinFunction_Tx)))
605 oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(txPinsArray,txArraySize,txPin)
607 oC_UART_Channel_t txChannel = oC_UART_LLD_GetChannelOfModulePin(*txPin);
609 if(ErrorCondition(!oC_UART_LLD_IsChannelUsed(txChannel) , oC_ErrorCode_ChannelIsUsed))
611 *outChannel = txChannel;
612 txModulePin = *txPin;
613 txModulePinFound =
true;
627 if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Rx,
NULL,&rxArraySize,oC_UART_PinFunction_Rx)) &&
628 ErrorCondition(rxArraySize > 0 , oC_ErrorCode_NoneOfModulePinAssigned)
632 rxPinsArray = ksmartalloc(
sizeof(oC_UART_Pin_t)*rxArraySize,&ModuleAllocator,AllocationFlags_CanWait1Second);
634 if(ErrorCondition(rxPinsArray !=
NULL , oC_ErrorCode_AllocationError))
637 if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ReadModulePinsOfPin(Rx,rxPinsArray,&rxArraySize,oC_UART_PinFunction_Rx)))
640 oC_ARRAY_FOREACH_IN_ARRAY_WITH_SIZE(rxPinsArray,rxArraySize,rxPin)
642 oC_UART_Channel_t rxChannel = oC_UART_LLD_GetChannelOfModulePin(*rxPin);
644 if(ErrorCondition(!oC_UART_LLD_IsChannelUsed(rxChannel) , oC_ErrorCode_ChannelIsUsed))
646 *outChannel = rxChannel;
647 rxModulePin = *rxPin;
648 rxModulePinFound =
true;
661 errorCode = oC_ErrorCode_ModulePinIsNotGiven;
667 if(txModulePinFound && rxModulePinFound)
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))
674 errorCode = oC_ErrorCode_None;
678 oC_UART_LLD_DisconnectModulePin(txModulePin);
679 oC_UART_LLD_DisconnectModulePin(rxModulePin);
687 if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ConnectModulePin(txModulePin)) &&
688 oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetChannelUsed(*outChannel))
691 errorCode = oC_ErrorCode_None;
695 oC_UART_LLD_DisconnectModulePin(txModulePin);
703 if(oC_AssignErrorCode(&errorCode , oC_UART_LLD_ConnectModulePin(rxModulePin)) &&
704 oC_AssignErrorCode(&errorCode , oC_UART_LLD_SetChannelUsed(*outChannel))
707 errorCode = oC_ErrorCode_None;
711 oC_UART_LLD_DisconnectModulePin(rxModulePin);
722 oC_IntMan_ExitCriticalSection();
727 if(txPinsArray !=
NULL)
729 if( !ksmartfree(txPinsArray,
sizeof(oC_UART_Pin_t)*txArraySize,AllocationFlags_CanWait1Second) )
731 oC_SaveError(
"UART:FindFreeChannelForPins" , oC_ErrorCode_ReleaseError);
734 if(rxPinsArray !=
NULL)
736 if( !ksmartfree(rxPinsArray,
sizeof(oC_UART_Pin_t)*rxArraySize,AllocationFlags_CanWait1Second) )
738 oC_SaveError(
"UART:FindFreeChannelForPins" , oC_ErrorCode_ReleaseError);
747 static void RxNotEmptyInterruptHandler( oC_UART_Channel_t Channel )
749 oC_Event_SetState(RxNotEmptyEvent,Channel);
754 static void TxNotFullInterruptHandler( oC_UART_Channel_t Channel )
756 oC_Event_SetState(TxNotFullEvent,Channel);
759 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
static bool ModuleEnabledFlag
The file with interface for the GPIO driver.
identifier for allocations
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
uint32_t oC_ObjectControl_t
stores object control value
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
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
The file with interface for mutex managing.
oC_ObjectControl_t ObjectControl
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