PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
mfs_hl.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
55 /******************************************************************************/
56 /* Include files */
57 /******************************************************************************/
58 #include "mfs_hl.h"
59 
60 #if (defined(PDL_PERIPHERAL_MFS_ACTIVE))
61 
62 #if (PDL_MFS_USE_HL == PDL_ON)
63 
68 
69 /******************************************************************************/
70 /* Local pre-processor symbols/macros ('#define') */
71 /******************************************************************************/
72 /* for BGR */
73 #define MFS_HL_RELOADVAL_MIN4UART (4u)
74 #define MFS_HL_RELOADVAL_MIN4CSIO (3u)
75 #define MFS_HL_RELOADVAL_MIN4LIN (3u)
76 #define MFS_HL_BGR_MASK (0x7FFFu)
77 
78 /* Number definition */
79 #define MFS_HL_NUM_1 (1u)
80 #define MFS_HL_NUM_2 (2u)
81 #define MFS_HL_NUM_3 (3u)
82 #define MFS_HL_NUM_4 (4u)
83 #define MFS_HL_NUM_5 (5u)
84 #define MFS_HL_NUM_6 (6u)
85 #define MFS_HL_NUM_7 (7u)
86 #define MFS_HL_NUM_8 (8u)
87 #define MFS_HL_NUM_9 (9u)
88 #define MFS_HL_NUM_10 (10u)
89 
90 /* Mask data */
91 #define MFS_HL_MASK_0 (0x01u)
92 #define MFS_HL_MASK_1 (0x02u)
93 #define MFS_HL_MASK_2 (0x04u)
94 #define MFS_HL_MASK_3 (0x08u)
95 #define MFS_HL_MASK_4 (0x10u)
96 #define MFS_HL_MASK_5 (0x20u)
97 #define MFS_HL_MASK_6 (0x40u)
98 #define MFS_HL_MASK_7 (0x80u)
99 #define MFS_HL_MASK_0_2 (0x07u)
100 #define MFS_HL_MASK_0_3 (0x0Fu)
101 #define MFS_HL_MASK_0_6 (0x7Fu)
102 #define MFS_HL_MASK_4_7 (0xF0u)
103 
104 #define MFS_HL_MASK_0_7 (0x000000FFu)
105 #define MFS_HL_MASK_8_15 (0x0000FF00u)
106 #define MFS_HL_MASK_16_23 (0x00FF0000u)
107 #define MFS_HL_MASK_24_31 (0xFF000000u)
108 #define MFS_HL_MASK_0_15 (0x0000FFFFu)
109 #define MFS_HL_MASK_16_31 (0xFFFF0000u)
110 
111 /******************************************************************************/
112 /* Global variable definitions (declared in header file with 'extern') */
113 /******************************************************************************/
114 
117 {
118 #if (PDL_PERIPHERAL_ENABLE_MFS0 == PDL_ON)
119  {
120  &MFS0, /* pstcInstance */
121  {NULL} /* stcInternData (not initialized yet) */
122  },
123 #endif
124 #if (PDL_PERIPHERAL_ENABLE_MFS1 == PDL_ON)
125  {
126  &MFS1, /* pstcInstance */
127  {NULL} /* stcInternData (not initialized yet) */
128  },
129 #endif
130 #if (PDL_PERIPHERAL_ENABLE_MFS2 == PDL_ON)
131  {
132  &MFS2, /* pstcInstance */
133  {NULL} /* stcInternData (not initialized yet) */
134  },
135 #endif
136 #if (PDL_PERIPHERAL_ENABLE_MFS3 == PDL_ON)
137  {
138  &MFS3, /* pstcInstance */
139  {NULL} /* stcInternData (not initialized yet) */
140  },
141 #endif
142 #if (PDL_PERIPHERAL_ENABLE_MFS4 == PDL_ON)
143  {
144  &MFS4, /* pstcInstance */
145  {NULL} /* stcInternData (not initialized yet) */
146  },
147 #endif
148 #if (PDL_PERIPHERAL_ENABLE_MFS5 == PDL_ON)
149  {
150  &MFS5, /* pstcInstance */
151  {NULL} /* stcInternData (not initialized yet) */
152  },
153 #endif
154 #if (PDL_PERIPHERAL_ENABLE_MFS6 == PDL_ON)
155  {
156  &MFS6, /* pstcInstance */
157  {NULL} /* stcInternData (not initialized yet) */
158  },
159 #endif
160 #if (PDL_PERIPHERAL_ENABLE_MFS7 == PDL_ON)
161  {
162  &MFS7, /* pstcInstance */
163  {NULL} /* stcInternData (not initialized yet) */
164  },
165 #endif
166 #if (PDL_PERIPHERAL_ENABLE_MFS8 == PDL_ON)
167  {
168  &MFS8, /* pstcInstance */
169  {NULL} /* stcInternData (not initialized yet) */
170  },
171 #endif
172 #if (PDL_PERIPHERAL_ENABLE_MFS9 == PDL_ON)
173  {
174  &MFS9, /* pstcInstance */
175  {NULL} /* stcInternData (not initialized yet) */
176  },
177 #endif
178 #if (PDL_PERIPHERAL_ENABLE_MFS10 == PDL_ON)
179  {
180  &MFS10, /* pstcInstance */
181  {NULL} /* stcInternData (not initialized yet) */
182  },
183 #endif
184 #if (PDL_PERIPHERAL_ENABLE_MFS11 == PDL_ON)
185  {
186  &MFS11, /* pstcInstance */
187  {NULL} /* stcInternData (not initialized yet) */
188  },
189 #endif
190 #if (PDL_PERIPHERAL_ENABLE_MFS12 == PDL_ON)
191  {
192  &MFS12, /* pstcInstance */
193  {NULL} /* stcInternData (not initialized yet) */
194  },
195 #endif
196 #if (PDL_PERIPHERAL_ENABLE_MFS13 == PDL_ON)
197  {
198  &MFS13, /* pstcInstance */
199  {NULL} /* stcInternData (not initialized yet) */
200  },
201 #endif
202 #if (PDL_PERIPHERAL_ENABLE_MFS14 == PDL_ON)
203  {
204  &MFS14, /* pstcInstance */
205  {NULL} /* stcInternData (not initialized yet) */
206  },
207 #endif
208 #if (PDL_PERIPHERAL_ENABLE_MFS15 == PDL_ON)
209  {
210  &MFS15, /* pstcInstance */
211  {NULL} /* stcInternData (not initialized yet) */
212  },
213 #endif
214 };
215 
216 /******************************************************************************/
217 /* Local type definitions ('typedef') */
218 /******************************************************************************/
219 
220 /******************************************************************************/
221 /* Local function prototypes ('static') */
222 /******************************************************************************/
224 
225 static uint16_t MfsHlGetMin(uint16_t u16Num1,
226  uint16_t u16Num2
227  );
228 
229 static void MfsHlWriteBuf(volatile stc_mfsn_t* pstcMfs,
230  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
231  );
232 
233 static void MfsHlIrqHandlerTx(volatile stc_mfsn_t* pstcMfs,
234  void* pvHandle
235  );
236 
237 static void MfsHlReadBuf(volatile stc_mfsn_t* pstcMfs,
238  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
239  );
240 
241 static void MfsHlIrqHandlerRx(volatile stc_mfsn_t* pstcMfs,
242  void* pvHandle
243  );
244 
245 static void MfsHlIrqHandlerSts(volatile stc_mfsn_t* pstcMfs,
246  void* pvHandle
247  );
248 
249 static void MfsHlFifoUseInit(volatile stc_mfsn_t* pstcMfs,
250  uint16_t u16BufFillLvl
251  );
252 
253 static void MfsHlI2cDataTxMaster(volatile stc_mfsn_t* pstcI2c,
254  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
255  );
256 
257 static void MfsHlI2cDataRxMaster(volatile stc_mfsn_t* pstcI2c,
258  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
259  );
260 
261 static void MfsHlI2cPreStartSlave(volatile stc_mfsn_t* pstcI2c,
262  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
263  );
264 
265 static void MfsHlI2cDataRxSlave(volatile stc_mfsn_t* pstcI2c,
266  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
267  );
268 
269 static void MfsHlI2cDataTxSlave(volatile stc_mfsn_t* pstcI2c,
270  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
271  );
272 
273 static void MfsHlI2cIrqHandlerTx(volatile stc_mfsn_t* pstcMfs,
274  void* pvHandle
275  );
276 
277 static void MfsHlI2cIrqHandlerRx(volatile stc_mfsn_t* pstcMfs,
278  void* pvHandle
279  );
280 
281 static void MfsHlI2cIrqHandlerSts(volatile stc_mfsn_t* pstcMfs,
282  void* pvHandle
283  );
284 
285 static en_result_t MfsHlI2cStartSlave(volatile stc_mfsn_t* pstcI2c,
286  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
287  );
288 
289 static en_result_t MfsHlI2cWaitIntState(volatile stc_mfs_hl_intern_data_t* pstcMfsHlInternData);
290 
292  uint32_t u32MaxCnt);
293 
294 /******************************************************************************/
295 /* Local variable definitions ('static') */
296 /******************************************************************************/
297 
298 /******************************************************************************/
299 /* Function implementation - global ('extern') and local ('static') */
300 /******************************************************************************/
301 
312 {
313  stc_mfs_hl_intern_data_t* pstcMfsInternDataPtr = NULL;
314  uint32_t u32Instance;
315 
316  if (NULL != pstcMfs)
317  {
318  for (u32Instance = 0u; u32Instance < (uint32_t)MfsInstanceIndexMax; u32Instance++)
319  {
320  if (pstcMfs == m_astcMfsHlInstanceDataLut[u32Instance].pstcInstance)
321  {
322  pstcMfsInternDataPtr = &m_astcMfsHlInstanceDataLut[u32Instance].stcInternData;
323  break;
324  }
325  }
326  }
327 
328  return (pstcMfsInternDataPtr);
329 } /* MfsHlGetInternDataPtr */
330 
341 static uint16_t MfsHlGetMin(uint16_t u16Num1,
342  uint16_t u16Num2
343  )
344 {
345  uint16_t u16Min;
346 
347  /* Return the smaller number as the result of comparing u16Num1 with u16Num2 */
348  u16Min = u16Num2;
349  if (u16Num1 < u16Num2)
350  {
351  u16Min = u16Num1;
352  }
353 
354  return (u16Min);
355 } /* MfsHlGetMin */
356 
369 static void MfsHlWriteBuf(volatile stc_mfsn_t* pstcMfs,
370  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
371  )
372 {
373  stc_mfs_hl_buffer_t* pstcBuffer;
374  uint32_t u32Data;
375  uint_fast16_t fu16Loop;
376  uint_fast16_t fu16Cnt;
377 
378  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
379  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
380  {
381  /* Get # of Data to put into HW FIFO, whichever space is less */
382  /* Temporary var only used to define the order of volatile accesses to */
383  /* Get rid of the compiler warning */
384  fu16Loop = (uint_fast16_t)MfsHlGetMin( pstcBuffer->u16FillCount,
385  (uint16_t)(MFS_FIFO_MAX_VAL - (pstcMfs->FBYTE1))
386  );
387  }
388  else
389  {
390  fu16Loop = MFS_HL_NUM_1;
391  }
392 
393  while (0u != fu16Loop)
394  {
395  u32Data = 0u;
396  if (MfsEightBits >= pstcMfsHlInternData->u8DataWidth)
397  {
398  fu16Cnt = MFS_HL_NUM_1;
399  }
400  else if (MfsSixteenBits >= pstcMfsHlInternData->u8DataWidth)
401  {
402  fu16Cnt = MFS_HL_NUM_2;
403  }
404  else if (MfsTwentyFourBits >= pstcMfsHlInternData->u8DataWidth)
405  {
406  fu16Cnt = MFS_HL_NUM_3;
407  }
408  else
409  {
410  fu16Cnt = MFS_HL_NUM_4;
411  }
412  while ((0u != fu16Cnt) && (0u != fu16Loop))
413  {
414  u32Data <<= MFS_HL_NUM_8;
415  u32Data |= (uint32_t)(pstcBuffer->pu8Buffer[pstcBuffer->u16OutIndex]) & MFS_HL_MASK_0_7;
416  /* update tail */
417  pstcBuffer->u16OutIndex = ((pstcBuffer->u16OutIndex + (uint16_t)MFS_HL_NUM_1) % (pstcBuffer->u16BufferSize));
418  pstcBuffer->u16FillCount-- ;
419  pstcBuffer->u16TxCount-- ;
420  pstcBuffer->u16TxCounter++ ;
421  fu16Loop-- ;
422  fu16Cnt--;
423  }
424  if (MfsSixteenBits >= pstcMfsHlInternData->u8DataWidth)
425  {
426  /* Put data from Buffer into Transmit Data Register */
427  pstcMfs->TDR = (uint16_t)u32Data;
428  }
429  else
430  {
431  /* Put data from Buffer into Transmit Data Register (upper 16 bits width) */
432  pstcMfs->TDR32 = u32Data;
433  }
434  } /* while ( fu16Loop != 0 ) */
435 } /* MfsHlWriteBuf */
436 
454 static void MfsHlIrqHandlerTx(volatile stc_mfsn_t* pstcMfs,
455  void* pvHandle
456  )
457 {
458  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
459  stc_mfs_hl_buffer_t* pstcBuffer;
460  uint8_t u8End = FALSE;
461  volatile uint16_t u16DummyData;
462 
463  pstcMfsHlInternData = (stc_mfs_hl_intern_data_t *)pvHandle;
464  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
465 
466  /* Wait TX bus idle (for CSIO timer mode) */
467  if (MfsHlExecWaitTxBusIdle == pstcMfsHlInternData->u8Exec)
468  {
469  /* Disable TX Idle Interrupt */
470  pstcMfs->SCR_f.TBIE = FALSE;
471  /* Disable serial timer */
472  pstcMfs->SCR_f.TXE = FALSE;
473  pstcMfs->SACSR_f.TMRE = FALSE;
474  pstcMfs->SCR_f.TXE = TRUE;
475  if (NULL != pstcMfsHlInternData->pfnTransmitCbFunction)
476  {
477  /* Call transmit callback function. */
478  pstcMfsHlInternData->pfnTransmitCbFunction(pstcBuffer->u16TxCounter);
479  }
480  /* Transmit is standby */
481  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
482  }
483  else
484  {
485  /* Write the contents of the buffer to Transmit Data Register */
486  MfsHlWriteBuf(pstcMfs, pstcMfsHlInternData);
487  if (0u == pstcBuffer->u16FillCount)
488  {
489  u8End = TRUE;
490  }
491 
492  if (TRUE == u8End)
493  {
494  /* If no more bytes to sent: Disable transmission interrupt(s) */
495  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
496  {
497  /* Disable TX Fifo Interrupt */
498  pstcMfs->FCR1_f.FTIE = FALSE;
499  }
500  else
501  {
502  /* Disable TX Interrupt */
503  pstcMfs->SCR_f.TIE = FALSE;
504  }
505 
506  if (0u == pstcBuffer->u16TxCount)
507  {
508  /* CSIO timer mode */
509  if (MfsHlModeCsioNormalMasterTimer == pstcMfsHlInternData->u8MfsMode)
510  {
511  /* Wait TX bus idle */
512  pstcMfsHlInternData->u8Exec = MfsHlExecWaitTxBusIdle;
513  /* Enable TX Idle Interrupt */
514  pstcMfs->SCR_f.TBIE = TRUE;
515  }
516  else
517  {
518  if (NULL != pstcMfsHlInternData->pfnTransmitCbFunction)
519  {
520  /* Call transmit callback function. */
521  pstcMfsHlInternData->pfnTransmitCbFunction(pstcBuffer->u16TxCounter);
522  }
523  /* Transmit is standby */
524  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
525  }
526  }
527  }
528  }
529 
530 } /* MfsHlIrqHandlerTx */
531 
543 static void MfsHlReadBuf(volatile stc_mfsn_t* pstcMfs,
544  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
545  )
546 {
547  stc_mfs_hl_buffer_t* pstcRxBuffer;
548  volatile uint32_t u32Data;
549  uint8_t u8Cnt;
550  uint8_t u8Shift;
551 
552  /* Pointer to the receive buffer */
553  pstcRxBuffer = &pstcMfsHlInternData->stcRxBuffer;
554 
555  if (MfsEightBits >= pstcMfsHlInternData->u8DataWidth)
556  {
557  u8Cnt = MFS_HL_NUM_1;
558  }
559  else if (MfsSixteenBits >= pstcMfsHlInternData->u8DataWidth)
560  {
561  u8Cnt = MFS_HL_NUM_2;
562  }
563  else if (MfsTwentyFourBits >= pstcMfsHlInternData->u8DataWidth)
564  {
565  u8Cnt = MFS_HL_NUM_3;
566  }
567  else
568  {
569  u8Cnt = MFS_HL_NUM_4;
570  }
571  u8Shift = 0u;
572  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
573  {
574  /* Now read all available bytes from hardware FIFO (if available) to buffer */
575  while (0u != pstcMfs->FBYTE2)
576  {
577  if (MfsSixteenBits >= pstcMfsHlInternData->u8DataWidth)
578  {
579  u32Data = (uint32_t)pstcMfs->RDR;
580  }
581  else
582  {
583  u32Data = pstcMfs->RDR32;
584  }
585  /* If there is empty space in RX buffer */
586  if (pstcRxBuffer->u16FillCount < pstcRxBuffer->u16BufferSize)
587  {
588  while ((pstcRxBuffer->u16FillCount < pstcRxBuffer->u16BufferSize) && (0u != u8Cnt))
589  {
590  u8Cnt--;
591  u32Data = u32Data >> (u8Shift * MFS_HL_NUM_8);
592  u8Shift++;
593  /* Read data from RX FIFO, store in RX buffer */
594  pstcRxBuffer->pu8Buffer[pstcRxBuffer->u16InIndex] = (uint8_t)u32Data;
595  /* Increment in index */
596  pstcRxBuffer->u16InIndex++;
597  if (pstcRxBuffer->u16InIndex == pstcRxBuffer->u16BufferSize)
598  {
599  pstcRxBuffer->u16InIndex = 0u;
600  }
601  /* Count bytes in RX-FIFO */
602  pstcRxBuffer->u16FillCount++;
603  }
604  }
605  else
606  {
607  /* We still need to do a dummy read to clear pending interrupts */
608  pstcRxBuffer->bOverflow = TRUE;
609  }
610  }
611  }
612  else
613  {
614  if (MfsSixteenBits >= pstcMfsHlInternData->u8DataWidth)
615  {
616  u32Data = (uint32_t)pstcMfs->RDR;
617  }
618  else
619  {
620  u32Data = pstcMfs->RDR32;
621  }
622  /* If there is empty space in RX buffer */
623  if (pstcRxBuffer->u16FillCount < pstcRxBuffer->u16BufferSize)
624  {
625  while ((pstcRxBuffer->u16FillCount < pstcRxBuffer->u16BufferSize) && (0u != u8Cnt))
626  {
627  u8Cnt--;
628  u32Data = u32Data >> (u8Shift * MFS_HL_NUM_8);
629  u8Shift++;
630  /* Read data from RX FIFO, store in RX buffer */
631  pstcRxBuffer->pu8Buffer[pstcRxBuffer->u16InIndex] = (uint8_t)u32Data;
632  /* Increment in index */
633  pstcRxBuffer->u16InIndex++;
634  if (pstcRxBuffer->u16InIndex == pstcRxBuffer->u16BufferSize)
635  {
636  pstcRxBuffer->u16InIndex = 0u;
637  }
638  /* Count bytes in RX-FIFO */
639  pstcRxBuffer->u16FillCount++;
640  }
641  }
642  else
643  {
644  /* We still need to do a dummy read to clear pending interrupts */
645  pstcRxBuffer->bOverflow = TRUE;
646  }
647  }
648 } /* MfsHlReadBuf */
649 
662 static void MfsHlIrqHandlerRx(volatile stc_mfsn_t* pstcMfs,
663  void* pvHandle
664  )
665 {
666  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
667  stc_mfs_hl_buffer_t* pstcRxBuffer;
668  volatile uint8_t u8Ssr;
669 
670  pstcMfsHlInternData = (stc_mfs_hl_intern_data_t *)pvHandle;
671 
672  u8Ssr = pstcMfs->SSR;
673  /* Error is different each mode */
674  switch (pstcMfsHlInternData->u8MfsMode & MFS_HL_MODE_GROUP_CHK)
675  {
676  /* UART */
678  /* Check Overrun/Framing/Parity error */
679  u8Ssr &= MFS_UART_SSR_ERR;
680  break;
681  /* CSIO */
683  /* Check Overrun error */
684  u8Ssr &= MFS_CSIO_SSR_ERR;
685  break;
686  /* LIN */
688  /* Check Overrun/Framing error */
689  u8Ssr &= MFS_LIN_SSR_ERR;
690  break;
691  default:
692  u8Ssr = 0u;
693  break;
694  }
695  /* Error */
696  if(0u != u8Ssr)
697  {
698  /* Clear possible reception errors */
699  pstcMfs->SSR_f.REC = TRUE;
700  }
701 
702  /* Store Received Data Register into buffer */
703  MfsHlReadBuf(pstcMfs, pstcMfsHlInternData);
704  /* Check if receive callback function is valid. */
705  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
706  {
707  /* Pointer to the receive buffer */
708  pstcRxBuffer = &pstcMfsHlInternData->stcRxBuffer;
709  if (pstcRxBuffer->u16FillCount >= pstcMfsHlInternData->u16RxCallbackBufFillLevel)
710  {
711  /* Call receive callback function. */
712  pstcMfsHlInternData->pfnReceiveCbFunction(pstcRxBuffer->u16FillCount);
713  }
714  }
715 } /* MfsHlIrqHandlerRx */
716 
723 static void MfsHlIrqHandlerSts(volatile stc_mfsn_t* pstcMfs,
724  void* pvHandle
725  )
726 {
727  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
728  volatile uint16_t u16DummyData;
729 
730  pstcMfsHlInternData = (stc_mfs_hl_intern_data_t *)pvHandle;
731 
732  /* LIN mode */
733  if ((pstcMfs->SMR & MFS_SMR_MODEBITS_MASK) == MFS_SMR_LIN_MODE)
734  {
735  /* LIN break detected? */
736  if (pstcMfs->SSR_f.LBD == 1)
737  {
738  u16DummyData = pstcMfs->RDR;
739  pstcMfs->SSR_f.LBD = FALSE; /* Clear LIN break detection */
740  pstcMfs->SSR_f.REC = TRUE; /* Clear possible reception errors */
741 
742  /* Break Callback if specified */
743  if (NULL != pstcMfsHlInternData->pfnLinBreakCbFunction)
744  {
745  pstcMfsHlInternData->pfnLinBreakCbFunction();
746  }
747  }
748  }
749 
750 } /* MfsHlIrqHandlerSts */
751 
760 static void MfsHlFifoUseInit(volatile stc_mfsn_t* pstcMfs,
761  uint16_t u16BufFillLvl
762  )
763 {
764  stc_mfs_fcr0_field_t stcFCR0 = { 0 }; /* Preset to zero */
765  stc_mfs_fcr1_field_t stcFCR1 = { 0 };
766 
767  /*
768  * Local Fifo Control Register 0 variable
769  */
770 
771  stcFCR0.FLD = FALSE; /* No reload (for transmission) */
772  stcFCR0.FSET = FALSE; /* FIFO2 read pointer not saved */
773  stcFCR0.FCL2 = TRUE; /* Reset FIFO2 */
774  stcFCR0.FCL1 = TRUE; /* Reset FIFO1 */
775  stcFCR0.FE2 = TRUE; /* Enable FIFO2 */
776  stcFCR0.FE1 = TRUE; /* Enable FIFO1 */
777 
778  /*
779  * Local Fifo Control Register 1 variable
780  */
781 
782  /* No FIFO test (also not used for I2C) */
783  stcFCR1.FTST0 = FALSE;
784  stcFCR1.FTST1 = FALSE;
785 
786  stcFCR1.FLSTE = FALSE; /* disable data lost detection */
787  stcFCR1.FRIIE = FALSE; /* disable FIFO idle detection */
788  stcFCR1.FDRQ = FALSE; /* no request for transmission FIFO data */
789  stcFCR1.FTIE = FALSE; /* disable transmission FIFO interrupt */
790  stcFCR1.FSEL = FALSE; /* FIFO1: transmission FIFO, FIFO2: reception FIFO */
791 
792  /* Setup hardware */
793  pstcMfs->FCR0_f = stcFCR0;
794  pstcMfs->FCR1_f = stcFCR1;
795  pstcMfs->FBYTE2 = (uint8_t)MfsHlGetMin(u16BufFillLvl, MFS_FIFO_MAX_VAL);
796 } /* MfsHlFifoUseInit */
797 
806 static void MfsHlI2cDataTxMaster(volatile stc_mfsn_t* pstcI2c,
807  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
808  )
809 {
810  stc_mfs_hl_buffer_t* pstcBuffer;
811  uint16_t u16Num;
812  uint16_t u16Cnt;
813  stc_mfs_smr_field_t stcSMR;
814  stc_mfs_fcr1_field_t stcFCR1 = { 0 };
815  stc_mfs_ssr_field_t stcSSR;
816  stc_mfs_i2c_ibsr_field_t stcIBSR;
817  stc_mfs_i2c_ibcr_field_t stcIBCR;
818  stc_mfs_i2c_ismk_field_t stcISMK;
819 
820  stcIBSR = pstcI2c->IBSR_f;
821  stcIBCR = pstcI2c->IBCR_f;
822 
823  /* Get ptr to internal transmit Buffer */
824  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
825 
826  /* stop condition */
827  if (TRUE == stcIBSR.SPC)
828  {
829  /* Clear stop condition interrupt */
830  stcIBSR.SPC = FALSE;
831  pstcI2c->IBSR_f = stcIBSR;
832 
833  /* stop condition interrupt disable */
834  stcIBCR.CNDE = FALSE;
835  stcIBCR.ACT_SCC = FALSE;
836  pstcI2c->IBCR_f = stcIBCR;
837 
838  /* Clear IBSR:RACK */
839  stcISMK = pstcI2c->ISMK_f;
840  stcISMK.EN = FALSE;
841  pstcI2c->ISMK_f = stcISMK;
842 
843  /* Restart */
844  stcISMK.EN = TRUE;
845  pstcI2c->ISMK_f = stcISMK;
846 
847  if (NULL != pstcMfsHlInternData->pfnTransmitCbFunction)
848  {
849  /* Call transmit callback function. */
850  pstcMfsHlInternData->pfnTransmitCbFunction(pstcBuffer->u16OutIndex);
851  }
852  /* Transmit is standby */
853  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
854  }
855  else if (((pstcBuffer->u16BufferSize == pstcBuffer->u16OutIndex)
856  && (TRUE == stcIBCR.INT))
857  || (TRUE == stcIBSR.RACK))
858  {
859  /* Disable master mode */
860  stcIBCR.MSS = FALSE;
861  stcIBCR.ACT_SCC = FALSE;
862  /* Disable interrupt */
863  stcIBCR.INTE = FALSE;
864  /* Clear interrupt */
865  stcIBCR.INT = FALSE;
866  /* Enable stop condition interrupt */
867  stcIBCR.CNDE = TRUE;
868  pstcI2c->IBCR_f = stcIBCR;
869  }
870  else
871  {
872  stcSSR = pstcI2c->SSR_f;
873  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
874  {
875  stcFCR1 = pstcI2c->FCR1_f;
876  }
877  if ((TRUE == stcSSR.TDRE) || (TRUE == stcFCR1.FDRQ))
878  {
879  /* FIFO is used */
880  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
881  {
882  u16Num = MfsHlGetMin(((pstcBuffer->u16BufferSize) - (pstcBuffer->u16OutIndex)),
883  (uint16_t)(MFS_FIFO_MAX_VAL - (pstcI2c->FBYTE1))
884  );
885  for (u16Cnt = 0u; u16Cnt < u16Num; u16Cnt++)
886  {
887  pstcI2c->TDR = pstcBuffer->pu8Buffer[pstcBuffer->u16OutIndex];
888  pstcBuffer->u16OutIndex += MFS_HL_NUM_1;
889  }
890  /* clear FIFO tx interrupt */
891  stcFCR1 = pstcI2c->FCR1_f;
892  stcFCR1.FDRQ = FALSE;
893  if (pstcBuffer->u16BufferSize == pstcBuffer->u16OutIndex)
894  {
895  /* tx FIFO interrupt disable */
896  stcFCR1.FTIE = FALSE;
897  }
898  pstcI2c->FCR1_f = stcFCR1;
899  }
900  else
901  {
902  /* tx data to register */
903  pstcI2c->TDR = pstcBuffer->pu8Buffer[pstcBuffer->u16OutIndex];
904  pstcBuffer->u16OutIndex += MFS_HL_NUM_1;
905  if (pstcBuffer->u16BufferSize == pstcBuffer->u16OutIndex)
906  {
907  /* tx interrupt disable */
908  stcSMR = pstcI2c->SMR_f;
909  stcSMR.TIE = FALSE;
910  pstcI2c->SMR_f = stcSMR;
911  }
912  }
913  }
914 
915  /* clear interrupt */
916  stcIBCR.ACT_SCC = FALSE;
917  stcIBCR.INT = FALSE;
918  pstcI2c->IBCR_f = stcIBCR;
919  }
920 } /* MfsHlI2cDataTxMaster */
921 
930 static void MfsHlI2cDataRxMaster(volatile stc_mfsn_t* pstcI2c,
931  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
932  )
933 {
934  stc_mfs_hl_buffer_t* pstcBuffer;
935  stc_mfs_ssr_field_t stcSSR;
936  stc_mfs_i2c_ibsr_field_t stcIBSR;
937  stc_mfs_i2c_ibcr_field_t stcIBCR;
938  stc_mfs_i2c_ismk_field_t stcISMK;
939 
940  stcIBSR = pstcI2c->IBSR_f;
941  stcIBCR = pstcI2c->IBCR_f;
942  stcSSR = pstcI2c->SSR_f;
943 
944  /* Get ptr to internal transmit Buffer */
945  pstcBuffer = &pstcMfsHlInternData->stcRxBuffer;
946 
947  /* stop condition */
948  if (TRUE == stcIBSR.SPC)
949  {
950  /* Clear stop condition interrupt */
951  stcIBSR.SPC = FALSE;
952  pstcI2c->IBSR_f = stcIBSR;
953 
954  /* Disable stop condition interrupt */
955  stcIBCR.CNDE = FALSE;
956  /* Disable interrupt */
957  stcIBCR.INTE = FALSE;
958 
959  /* Clear IBSR:RACK */
960  stcISMK = pstcI2c->ISMK_f;
961  stcISMK.EN = FALSE;
962  pstcI2c->ISMK_f = stcISMK;
963 
964  /* Restart */
965  stcISMK.EN = TRUE;
966  pstcI2c->ISMK_f = stcISMK;
967 
968  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
969  {
970  /* Call receive callback function. */
971  pstcMfsHlInternData->pfnReceiveCbFunction(pstcBuffer->u16InIndex);
972  }
973  /* Receiving is standby */
974  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
975  }
976  /* Received data after second byte */
977  else if ((TRUE == stcSSR.RDRF) && (FALSE == stcIBSR.FBT))
978  {
979  /* Continue until specified data length is received */
980  while (pstcBuffer->u16InIndex < pstcBuffer->u16BufferSize)
981  {
982  stcSSR = pstcI2c->SSR_f;
983  if (TRUE == stcSSR.RDRF)
984  {
985  pstcBuffer->pu8Buffer[pstcBuffer->u16InIndex] = (uint8_t)pstcI2c->RDR;
986  pstcBuffer->u16InIndex += MFS_HL_NUM_1;
987  }
988  else
989  {
990  /* No data */
991  break;
992  }
993  }
994  /* Complete to receive */
995  if (pstcBuffer->u16InIndex == pstcBuffer->u16BufferSize)
996  {
997  /* Stop condition */
998  stcIBCR.MSS = FALSE;
999  /* NACK */
1000  stcIBCR.ACKE = FALSE;
1001  /* stop condition interrupt enable */
1002  stcIBCR.CNDE = TRUE;
1003 
1004  /* If restart condition was detected */
1005  if (TRUE == stcIBSR.RSC)
1006  {
1007  /* Clear restart condition */
1008  stcIBSR.RSC = FALSE;
1009  pstcI2c->IBSR_f = stcIBSR;
1010  }
1011  }
1012  }
1013  /* Overrun error */
1014  else if (TRUE == stcSSR.ORE)
1015  {
1016  /* Clear RX error */
1017  stcSSR.REC = TRUE;
1018  pstcI2c->SSR_f = stcSSR;
1019  }
1020  else
1021  {
1022  /* There is no process that should be executed. */
1023  }
1024 
1025  /* Clear interrupt */
1026  stcIBCR.ACT_SCC = FALSE;
1027  stcIBCR.INT = FALSE;
1028  pstcI2c->IBCR_f = stcIBCR;
1029 } /* MfsHlI2cDataRxMaster */
1030 
1039 static void MfsHlI2cPreStartSlave(volatile stc_mfsn_t* pstcI2c,
1040  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
1041  )
1042 {
1043  stc_mfs_smr_field_t stcSMR;
1044  stc_mfs_i2c_ibcr_field_t stcIBCR;
1045  stc_mfs_i2c_ibsr_field_t stcIBSR;
1046  stc_mfs_i2c_ismk_field_t stcISMK;
1047  uint8_t u8Data;
1048 
1049  stcIBCR = pstcI2c->IBCR_f;
1050  stcIBSR = pstcI2c->IBSR_f;
1051  /* Chk Slave Address Received */
1052  if ((FALSE == stcIBCR.MSS) && (TRUE == stcIBCR.ACT_SCC) && (TRUE == stcIBSR.FBT))
1053  {
1054  stcSMR = pstcI2c->SMR_f;
1055  /* Check direction */
1056  /* TX */
1057  if (TRUE == stcIBSR.TRX)
1058  {
1059  /* Callback for the I2C slave starting(TX) and get 1st data to send */
1060  if (NULL != pstcMfsHlInternData->pfnI2cSlvStCbFunction)
1061  {
1062  u8Data = pstcMfsHlInternData->pfnI2cSlvStCbFunction(MfsI2cWrite);
1063  }
1064  else
1065  {
1066  u8Data = 0x00u;
1067  }
1068  /* Send 1st data */
1069  pstcI2c->TDR = (uint16_t)u8Data;
1070  /* Disable TX interrupt */
1071  stcSMR.TIE = FALSE;
1072  stcIBCR.WSEL = FALSE;
1073  }
1074  /* RX */
1075  else
1076  {
1077  stcIBCR.ACKE = TRUE;
1078  /* Disable RX interrupt */
1079  stcSMR.RIE = FALSE;
1080  stcIBCR.WSEL = TRUE;
1081  /* Callback for the I2C slave starting(RX) */
1082  if (NULL != pstcMfsHlInternData->pfnI2cSlvStCbFunction)
1083  {
1084  pstcMfsHlInternData->pfnI2cSlvStCbFunction(MfsI2cRead);
1085  }
1086  }
1087  pstcI2c->SMR_f = stcSMR;
1088  }
1089 
1090  /* Clear interrupt */
1091  stcIBCR.ACT_SCC = FALSE;
1092  stcIBCR.INT = FALSE;
1093  pstcI2c->IBCR_f = stcIBCR;
1094 } /* MfsHlI2cPreStartSlave */
1095 
1104 static void MfsHlI2cDataRxSlave(volatile stc_mfsn_t* pstcI2c,
1105  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
1106  )
1107 {
1108  stc_mfs_hl_buffer_t* pstcBuffer;
1109  stc_mfs_ssr_field_t stcSSR;
1110  stc_mfs_i2c_ibcr_field_t stcIBCR;
1111  stc_mfs_i2c_ibsr_field_t stcIBSR;
1112  stc_mfs_i2c_ismk_field_t stcISMK;
1113 
1114  stcIBSR = pstcI2c->IBSR_f;
1115  stcSSR = pstcI2c->SSR_f;
1116 
1117  /* Get ptr to internal transmit Buffer */
1118  pstcBuffer = &pstcMfsHlInternData->stcRxBuffer;
1119 
1120  /* Stop condition */
1121  if (TRUE == stcIBSR.SPC)
1122  {
1123  /* Clear stop condition interrupt */
1124  stcIBSR.SPC = FALSE;
1125  pstcI2c->IBSR_f = stcIBSR;
1126 
1127  /* Clear IBSR:RACK */
1128  stcISMK = pstcI2c->ISMK_f;
1129  stcISMK.EN = FALSE;
1130  pstcI2c->ISMK_f = stcISMK;
1131 
1132  /* Restart */
1133  stcISMK.EN = TRUE;
1134  pstcI2c->ISMK_f = stcISMK;
1135 
1136  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
1137  {
1138  /* Call receive callback function. */
1139  pstcMfsHlInternData->pfnReceiveCbFunction(pstcBuffer->u16InIndex);
1140  }
1141  /* Receiving is standby */
1142  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
1143  }
1144  /* Received NACK */
1145  else if (TRUE == stcIBSR.RACK)
1146  {
1147  /* Clear interrupt */
1148  stcIBCR = pstcI2c->IBCR_f;
1149  stcIBCR.ACT_SCC = FALSE;
1150  stcIBCR.INT = FALSE;
1151  pstcI2c->IBCR_f = stcIBCR;
1152  }
1153  /* Received data */
1154  else
1155  {
1156  stcIBCR = pstcI2c->IBCR_f;
1157 
1158  if ((TRUE == stcSSR.RDRF) && (FALSE == stcIBSR.FBT))
1159  {
1160  /* Continue until specified data length is received */
1161  while (pstcBuffer->u16InIndex < pstcBuffer->u16BufferSize)
1162  {
1163  stcSSR = pstcI2c->SSR_f;
1164  if (TRUE == stcSSR.RDRF)
1165  {
1166  pstcBuffer->pu8Buffer[pstcBuffer->u16InIndex] = (uint8_t)pstcI2c->RDR;
1167  pstcBuffer->u16InIndex += MFS_HL_NUM_1;
1168  }
1169  else
1170  {
1171  /* No data */
1172  break;
1173  }
1174  }
1175  /* Complete to receive */
1176  if (pstcBuffer->u16InIndex == pstcBuffer->u16BufferSize)
1177  {
1178  /* Send NACK */
1179  stcIBCR.ACKE = FALSE;
1180  }
1181  else
1182  {
1183  /* Send ACK */
1184  stcIBCR.ACKE = TRUE;
1185  }
1186  }
1187  /* Clear interrupt */
1188  stcIBCR.ACT_SCC = FALSE;
1189  stcIBCR.INT = FALSE;
1190  pstcI2c->IBCR_f = stcIBCR;
1191  }
1192  /* Overrun error */
1193  if (TRUE == stcSSR.ORE)
1194  {
1195  /* Clear RX error */
1196  stcSSR.REC = TRUE;
1197  pstcI2c->SSR_f = stcSSR;
1198  }
1199 } /* MfsHlI2cDataRxSlave */
1200 
1209 static void MfsHlI2cDataTxSlave(volatile stc_mfsn_t* pstcI2c,
1210  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
1211  )
1212 {
1213  stc_mfs_hl_buffer_t* pstcBuffer;
1214  stc_mfs_smr_field_t stcSMR;
1215  stc_mfs_fcr1_field_t stcFCR1 = { 0 };
1216  stc_mfs_ssr_field_t stcSSR;
1217  stc_mfs_i2c_ibcr_field_t stcIBCR;
1218  stc_mfs_i2c_ibsr_field_t stcIBSR;
1219  stc_mfs_i2c_ismk_field_t stcISMK;
1220  uint16_t u16Num;
1221  uint16_t u16Cnt;
1222 
1223  stcIBSR = pstcI2c->IBSR_f;
1224 
1225  /* Get ptr to internal transmit Buffer */
1226  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
1227 
1228  /* end of data or */
1229  if ((pstcBuffer->u16BufferSize == pstcBuffer->u16OutIndex)
1230  /* Stop condition */
1231  || (TRUE == stcIBSR.SPC))
1232  {
1233  /* FIFO is used */
1234  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
1235  {
1236  /* Disable TX FIFO interrupt */
1237  stcFCR1 = pstcI2c->FCR1_f;
1238  stcFCR1.FTIE = FALSE;
1239  pstcI2c->FCR1_f = stcFCR1;
1240  }
1241  /* FIFO is not used */
1242  else
1243  {
1244  /* Disable TX interrupt */
1245  stcSMR = pstcI2c->SMR_f;
1246  stcSMR.TIE = FALSE;
1247  pstcI2c->SMR_f = stcSMR;
1248  }
1249  /* Stop condition */
1250  if (TRUE == stcIBSR.SPC)
1251  {
1252  /* Clear IBSR:RACK */
1253  stcISMK = pstcI2c->ISMK_f;
1254  stcISMK.EN = FALSE;
1255  pstcI2c->ISMK_f = stcISMK;
1256 
1257  /* Restart */
1258  stcISMK.EN = TRUE;
1259  pstcI2c->ISMK_f = stcISMK;
1260 
1261  /* Clear stop condition interrupt */
1262  stcIBSR.SPC = FALSE;
1263  pstcI2c->IBSR_f = stcIBSR;
1264 
1265  if (NULL != pstcMfsHlInternData->pfnTransmitCbFunction)
1266  {
1267  /* Call transmit callback function. */
1268  pstcMfsHlInternData->pfnTransmitCbFunction(pstcBuffer->u16OutIndex);
1269  }
1270  /* Transmit is standby */
1271  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
1272  }
1273 
1274  /* Clear interrupt */
1275  stcIBCR = pstcI2c->IBCR_f;
1276  stcIBCR.ACT_SCC = FALSE;
1277  stcIBCR.INT = FALSE;
1278  pstcI2c->IBCR_f = stcIBCR;
1279 
1280  /* Clear RSC interrupt */
1281  stcIBSR.RSC = FALSE;
1282  pstcI2c->IBSR_f = stcIBSR;
1283  }
1284  else
1285  {
1286  stcSSR = pstcI2c->SSR_f;
1287  /* FIFO is used */
1288  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
1289  {
1290  stcFCR1 = pstcI2c->FCR1_f;
1291  }
1292  if ((TRUE == stcSSR.TDRE) || (TRUE == stcFCR1.FDRQ))
1293  {
1294  /* FIFO is used */
1295  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
1296  {
1297  u16Num = MfsHlGetMin(((pstcBuffer->u16BufferSize) - (pstcBuffer->u16OutIndex)),
1298  (uint16_t)(MFS_FIFO_MAX_VAL - (pstcI2c->FBYTE1))
1299  );
1300  for (u16Cnt = 0u; u16Cnt < u16Num; u16Cnt++)
1301  {
1302  /* Set data to TX FIFO */
1303  pstcI2c->TDR = pstcBuffer->pu8Buffer[pstcBuffer->u16OutIndex];
1304  pstcBuffer->u16OutIndex++;
1305  }
1306  /* clear FIFO tx interrupt */
1307  stcFCR1 = pstcI2c->FCR1_f;
1308  stcFCR1.FDRQ = FALSE;
1309  pstcI2c->FCR1_f = stcFCR1;
1310  }
1311  /* FIFO is not used */
1312  else
1313  {
1314  /* Set data to TX FIFO */
1315  pstcI2c->TDR = pstcBuffer->pu8Buffer[pstcBuffer->u16OutIndex];
1316  pstcBuffer->u16OutIndex++;
1317  }
1318  }
1319  /* Clear interrupt */
1320  stcIBCR = pstcI2c->IBCR_f;
1321  stcIBCR.ACT_SCC = FALSE;
1322  stcIBCR.INT = FALSE;
1323  pstcI2c->IBCR_f = stcIBCR;
1324  /* Received NACK */
1325  if (TRUE == stcIBSR.RACK)
1326  {
1327  /* Clear RSC interrupt */
1328  stcIBSR.RSC = FALSE;
1329  pstcI2c->IBSR_f = stcIBSR;
1330  }
1331  }
1332 } /* MfsHlI2cDataTxSlave */
1333 
1344 static void MfsHlI2cIrqHandlerTx(volatile stc_mfsn_t* pstcMfs,
1345  void* pvHandle
1346  )
1347 {
1348  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
1349 
1350  pstcMfsHlInternData = (stc_mfs_hl_intern_data_t *)pvHandle;
1351 
1352  /* I2C Master mode */
1353  if (MfsHlModeI2cMaster == pstcMfsHlInternData->u8MfsMode)
1354  {
1355  MfsHlI2cDataTxMaster(pstcMfs, pstcMfsHlInternData);
1356  }
1357  /* I2C Slave mode */
1358  else
1359  {
1360  MfsHlI2cDataTxSlave(pstcMfs, pstcMfsHlInternData);
1361  }
1362 } /* MfsHlI2cIrqHandlerTx */
1363 
1374 static void MfsHlI2cIrqHandlerRx(volatile stc_mfsn_t* pstcMfs,
1375  void* pvHandle
1376  )
1377 {
1378  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
1379 
1380  pstcMfsHlInternData = (stc_mfs_hl_intern_data_t *)pvHandle;
1381 
1382  /* I2C Master mode */
1383  if (MfsHlModeI2cMaster == pstcMfsHlInternData->u8MfsMode)
1384  {
1385  MfsHlI2cDataRxMaster(pstcMfs, pstcMfsHlInternData);
1386  }
1387  /* I2C Slave mode */
1388  else
1389  {
1390  /* If status is standby... */
1391  if (MfsHlExecStby == pstcMfsHlInternData->u8Exec)
1392  {
1393  MfsHlI2cPreStartSlave(pstcMfs, pstcMfsHlInternData);
1394  }
1395  else
1396  {
1397  MfsHlI2cDataRxSlave(pstcMfs, pstcMfsHlInternData);
1398  }
1399  }
1400 } /* MfsHlI2cIrqHandlerRx */
1401 
1412 static void MfsHlI2cIrqHandlerSts(volatile stc_mfsn_t* pstcMfs,
1413  void* pvHandle
1414  )
1415 {
1416  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
1417 
1418  pstcMfsHlInternData = (stc_mfs_hl_intern_data_t *)pvHandle;
1419 
1420  /* I2C Master mode */
1421  if (MfsHlModeI2cMaster == pstcMfsHlInternData->u8MfsMode)
1422  {
1423  /* Tramsmission */
1424  if (MfsHlExecTransmitting == pstcMfsHlInternData->u8Exec)
1425  {
1426  MfsHlI2cDataTxMaster(pstcMfs, pstcMfsHlInternData);
1427  }
1428  /* Reception */
1429  else
1430  {
1431  MfsHlI2cDataRxMaster(pstcMfs, pstcMfsHlInternData);
1432  }
1433  }
1434  /* I2C Slave mode */
1435  else
1436  {
1437  switch (pstcMfsHlInternData->u8Exec)
1438  {
1439  /* Standby */
1440  case MfsHlExecStby:
1441  MfsHlI2cPreStartSlave(pstcMfs, pstcMfsHlInternData);
1442  break;
1443  /* Receiving (after slave address was received) */
1444  case MfsHlExecReceiving:
1445  MfsHlI2cDataRxSlave(pstcMfs, pstcMfsHlInternData);
1446  break;
1447  default:
1448  MfsHlI2cDataTxSlave(pstcMfs, pstcMfsHlInternData);
1449  break;
1450  }
1451  }
1452 } /* MfsHlI2cIrqHandlerSts */
1453 
1476  stc_mfs_hl_uart_config_t* pstcConfig
1477  )
1478 {
1479  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
1480  uint32_t u32ReloadValue;
1481 
1482  /* Preset local register variables to zero */
1483  stc_mfs_smr_field_t stcSMR = { 0 };
1484  stc_mfs_scr_field_t stcSCR = { 0 };
1485  stc_mfs_escr_field_t stcESCR = { 0 };
1486 
1487  /* Check for valid pointers and get pointer to internal data struct ... */
1488  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcUart);
1489  /* ... and check */
1490  if ((NULL == pstcMfsHlInternData)
1491  || (NULL == pstcConfig)
1492  )
1493  {
1494  return (ErrorInvalidParameter);
1495  }
1496 
1497  /* RX Callback Level must be set more than zero */
1498  if (1u > pstcConfig->u16RxCbBufFillLvl)
1499  {
1500  return (ErrorInvalidParameter);
1501  }
1502 
1503  /* Get reload value */
1504  u32ReloadValue = Mfs_GetReloadValue(pstcConfig->u32DataRate);
1505  /* For asynchronous communication, the reload value must be greater than */
1506  /* or equal to 4 because five times over-sampling is performed internally */
1507  if (MFS_HL_NUM_4 > u32ReloadValue)
1508  {
1509  return (ErrorInvalidParameter);
1510  }
1511 
1512  /* Check buffer for UART */
1513  if (((NULL == pstcConfig->pu8TxBuf)
1514  || (0u == pstcConfig->u16TxBufSize))
1515  || ((NULL == pstcConfig->pu8RxBuf)
1516  || (0u == pstcConfig->u16RxBufSize))
1517  )
1518  {
1519  return (ErrorInvalidParameter);
1520  }
1521 
1522  /* First of all set MFS to Asynchronous mode 0 */
1523  pstcUart->SMR = 0u;
1524 
1525  /* Then we disable TX and RX for safe operation */
1526  pstcUart->SCR = 0u;
1527 
1528  /* Clear MFS by setting the Software Reset bit */
1529  pstcUart->SCR_f.UPCL = TRUE;
1530 
1531  /* Check the configration of FIFO usage */
1532  switch (pstcConfig->u8FifoUsage)
1533  {
1534  case MfsHlUseNoFifo:
1535  case MfsHlUseFifo:
1536  /* Set FIFO usage */
1537  pstcMfsHlInternData->u8FifoUsage = pstcConfig->u8FifoUsage;
1538  break;
1539  default:
1540  return (ErrorInvalidParameter);
1541  }
1542 
1543  /* ************ Register Settings ************ */
1544  /*
1545  * Local Serial Mode Register variable
1546  */
1547 
1548  /* Operation Mode Selection */
1549  switch (pstcConfig->u8UartMode)
1550  {
1551  /* Normal mode */
1552  case MfsUartNormal:
1553  stcSMR.MD = MFS_MD_UART_NORNAL;
1554  break;
1555  /* Multi-Processor mode */
1556  case MfsUartMulti:
1557  stcSMR.MD = MFS_MD_UART_MULTI;
1558  break;
1559  default:
1560  return (ErrorInvalidParameter); /* should never see the daylight */
1561  }
1562 
1563  /* Bit direction */
1564  if (TRUE == pstcConfig->bBitDirection)
1565  {
1566  stcSMR.BDS = TRUE;
1567  }
1568  else
1569  {
1570  stcSMR.BDS = FALSE;
1571  }
1572 
1573  /* now setup hardware with correct mode first and then go on with */
1574  /* bit settings */
1575  pstcUart->SMR_f = stcSMR;
1576 
1577  /* Stop bit length 1 */
1578  switch (pstcConfig->u8StopBit)
1579  {
1580  case MfsOneStopBit:
1581  case MfsThreeStopBits:
1582  stcSMR.SBL = FALSE;
1583  break;
1584  case MfsTwoStopBits:
1585  case MfsFourStopBits:
1586  stcSMR.SBL = TRUE;
1587  break;
1588  default:
1589  return (ErrorInvalidParameter);
1590  }
1591 
1592  /* Enable serial output */
1593  stcSMR.SOE = TRUE;
1594 
1595  /* now setup hardware (asynchronous) */
1596  pstcUart->SMR_f = stcSMR;
1597 
1598  /*
1599  * Local Serial/I2C Control Register variable
1600  */
1601  /* now setup hardware */
1602  pstcUart->SCR_f = stcSCR;
1603 
1604  /*
1605  * Local Extended Serial Control Register variable
1606  */
1607  /* Setup Parity 1 (Only in Normal mode available) */
1608  if (MfsUartNormal == pstcConfig->u8UartMode)
1609  {
1610  switch (pstcConfig->u8Parity)
1611  {
1612  case MfsParityNone:
1613  stcESCR.P = FALSE; /* Parity type selection (0-even/1-odd) */
1614  stcESCR.PEN = FALSE; /* Parity disable */
1615  break;
1616  case MfsParityEven:
1617  stcESCR.P = FALSE; /* Parity type selection (0-even/1-odd) */
1618  stcESCR.PEN = TRUE; /* Parity Enable */
1619  break;
1620  case MfsParityOdd:
1621  stcESCR.P = TRUE; /* Parity type selection (0-even/1-odd) */
1622  stcESCR.PEN = TRUE; /* Parity Enable */
1623  break;
1624  default:
1625  return (ErrorInvalidParameter); /* should never see the daylight */
1626  }
1627  } /* if (pstcConfig->enUartMode == MfsAsynchronous0) */
1628 
1629  /* Stop bits */
1630  switch (pstcConfig->u8StopBit)
1631  {
1632  case MfsOneStopBit:
1633  case MfsTwoStopBits:
1634  stcESCR.ESBL = FALSE;
1635  break;
1636  case MfsThreeStopBits:
1637  case MfsFourStopBits:
1638  stcESCR.ESBL = TRUE;
1639  break;
1640  default:
1641  return (ErrorInvalidParameter);
1642  }
1643 
1644  /* HW Flow */
1645  if (TRUE == pstcConfig->bHwFlow)
1646  {
1647  stcESCR.FLWEN = TRUE;
1648  }
1649  else
1650  {
1651  stcESCR.FLWEN = FALSE;
1652  }
1653 
1654  /* NRZ / inverted NRZI */
1655  if (TRUE == pstcConfig->bSignalSystem)
1656  {
1657  stcESCR.INV = TRUE;
1658  }
1659  else
1660  {
1661  stcESCR.INV = FALSE;
1662  }
1663 
1664  /* Caracter Length */
1665  switch (pstcConfig->u8CharLength)
1666  {
1667  case MfsFiveBits:
1668  stcESCR.L = MFS_DATA_LEN_5;
1669  break;
1670  case MfsSixBits:
1671  stcESCR.L = MFS_DATA_LEN_6;
1672  break;
1673  case MfsSevenBits:
1674  stcESCR.L = MFS_DATA_LEN_7;
1675  break;
1676  case MfsEightBits:
1677  stcESCR.L = MFS_DATA_LEN_8;
1678  break;
1679  case MfsNineBits:
1680  stcESCR.L = MFS_DATA_LEN_9;
1681  break;
1682  default:
1683  return (ErrorInvalidParameter); /* should never see the daylight */
1684  }
1685 
1686  /* now setup hardware */
1687  pstcUart->ESCR_f = stcESCR;
1688 
1689  /*
1690  * Local Serial Status Register variable (USART)
1691  */
1692  /* Clear possible reception errors */
1693  pstcUart->SSR_f.REC = TRUE;
1694 
1695  /*
1696  * Local Fifo Control Register 0 variable
1697  */
1698  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
1699  {
1700  /* Initialize FIFO to use */
1701  MfsHlFifoUseInit(pstcUart, pstcConfig->u16RxCbBufFillLvl);
1702  }
1703 
1704  /* Save RX Callback Buffer Fill Level (needed during servicing RX INT) */
1705  pstcMfsHlInternData->u16RxCallbackBufFillLevel = pstcConfig->u16RxCbBufFillLvl;
1706 
1707  /* Save character length */
1708  pstcMfsHlInternData->u8DataWidth = pstcConfig->u8CharLength;
1709 
1710  /* Set baud rate generation reload register */
1711  pstcUart->BGR = (uint16_t)(u32ReloadValue & MFS_HL_BGR_MASK);
1712 
1713  /* Set callback functions. */
1714  pstcMfsHlInternData->pfnTransmitCbFunction = pstcConfig->pfnTxCb;
1715  pstcMfsHlInternData->pfnReceiveCbFunction = pstcConfig->pfnRxCb;
1716 
1717  /* Initialise TX ring buffer */
1718  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = pstcConfig->pu8TxBuf;
1719  pstcMfsHlInternData->stcTxBuffer.u16BufferSize = pstcConfig->u16TxBufSize;
1720  pstcMfsHlInternData->stcTxBuffer.u16InIndex = 0u; /* Reset Index */
1721  pstcMfsHlInternData->stcTxBuffer.u16OutIndex = 0u; /* Reset Index */
1722  pstcMfsHlInternData->stcTxBuffer.u16FillCount = 0u; /* Reset Counter */
1723  pstcMfsHlInternData->stcTxBuffer.u16TxCounter = 0u; /* Reset Counter */
1724  pstcMfsHlInternData->stcTxBuffer.u16TxCount = 0u; /* Reset Counter */
1725  pstcMfsHlInternData->stcTxBuffer.bOverflow = 0u; /* Reset Flag */
1726 
1727  /* Initialise RX ring buffer */
1728  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = pstcConfig->pu8RxBuf;
1729  pstcMfsHlInternData->stcRxBuffer.u16BufferSize = pstcConfig->u16RxBufSize;
1730  pstcMfsHlInternData->stcRxBuffer.u16InIndex = 0u; /* Reset Index */
1731  pstcMfsHlInternData->stcRxBuffer.u16OutIndex = 0u; /* Reset Index */
1732  pstcMfsHlInternData->stcRxBuffer.u16FillCount = 0u; /* Reset Counter */
1733  pstcMfsHlInternData->stcRxBuffer.bOverflow = 0u; /* Reset Flag */
1734 
1735  /* MFS is standby */
1736  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
1737 
1738  /* Operation Mode Selection */
1739  if (MfsUartNormal == pstcConfig->u8UartMode)
1740  {
1741  /* Normal mode */
1742  pstcMfsHlInternData->u8MfsMode = MfsHlModeUartNormal;
1743  }
1744  else
1745  {
1746  /* Multi-Processor mode */
1747  pstcMfsHlInternData->u8MfsMode = MfsHlModeUartMulti;
1748  }
1749 
1750  /* Register interrupt handler and internal handle */
1751  (void)Mfs_SetTxIntCallBack(pstcUart, MfsHlIrqHandlerTx);
1752  (void)Mfs_SetRxIntCallBack(pstcUart, MfsHlIrqHandlerRx);
1753  (void)Mfs_SetUpperLayerHandle(pstcUart, pstcMfsHlInternData);
1754 
1755  /* Enable receiver, transmitter */
1756  pstcUart->SCR_f.TXE = TRUE;
1757  pstcUart->SCR_f.RXE = TRUE;
1758 
1759  /* Use interruption */
1760  if (NULL != pstcConfig->pfnRxCb)
1761  {
1762  /* Enable reception interrupt */
1763  pstcUart->SCR_f.RIE = TRUE;
1764  }
1765 
1766  /* Init transmission interrupt */
1767  (void)Mfs_InitTxIrq(pstcUart);
1768  /* Init reception interrupt */
1769  (void)Mfs_InitRxIrq(pstcUart);
1770 
1771  return (Ok);
1772 } /* Mfs_Hl_Uart_Init */
1773 
1792 {
1793  en_result_t enResult;
1794  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
1795 
1796  /* Check for valid pointer and get pointer to internal data struct ... */
1797  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcUart);
1798  /* ... and check */
1799  if (NULL == pstcMfsHlInternData)
1800  {
1801  enResult = ErrorInvalidParameter;
1802  }
1803  else
1804  {
1805  /* First of all disable receiver, transmitter and deinit interrupts */
1806  pstcUart->SCR_f.TXE = FALSE;
1807  pstcUart->SCR_f.RXE = FALSE;
1808 
1809  (void)Mfs_DeInitIrq(pstcUart); /* returns always en_result_t Ok */
1810 
1811  /* Baud Rate Generation Reload Reset */
1812  pstcUart->BGR = 0u;
1813 
1814  /* Serial Mode Register clear all bits (valid in any mode) */
1815  pstcUart->SMR = 0u;
1816 
1817  /* Reset Mfs receive and transmit bit to default */
1818  /* and clear all error flags */
1819  pstcUart->SCR = 0u;
1820 
1821  /* Clear reception Errors */
1822  pstcUart->SSR_f.REC = TRUE;
1823 
1824  /* Reset all other used register to default value */
1825  pstcUart->SSR = 0u;
1826  pstcUart->ESCR = 0u;
1827  pstcUart->FCR0 = 0u;
1828  pstcUart->FCR1 = 0u;
1829 
1830  /* Clear MFS by setting the Software Reset bit */
1831  pstcUart->SCR_f.UPCL = TRUE;
1832 
1833  /* Unregist interrupt handler and internal handle */
1834  (void)Mfs_SetTxIntCallBack(pstcUart, NULL);
1835  (void)Mfs_SetRxIntCallBack(pstcUart, NULL);
1836  (void)Mfs_SetStsIntCallBack(pstcUart, NULL);
1837  (void)Mfs_SetUpperLayerHandle(pstcUart, NULL);
1838 
1839  /* Reset Fifo buffer */
1840  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = NULL;
1841  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = NULL;
1842 
1843  /* Reset callback functions. */
1844  pstcMfsHlInternData->pfnTransmitCbFunction = NULL;
1845  pstcMfsHlInternData->pfnReceiveCbFunction = NULL;
1846 
1847  /* Reset LIN Break Callback used for Mfs_Lin_DeInit() */
1848  pstcMfsHlInternData->pfnLinBreakCbFunction = NULL;
1849  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
1850  pstcMfsHlInternData->u8MfsMode = MfsHlModeStby;
1851 
1852  enResult = Ok;
1853  }
1854 
1855  return (enResult);
1856 } /* Mfs_Hl_Uart_DeInit */
1857 
1881  stc_mfs_hl_csio_config_t* pstcConfig
1882  )
1883 {
1884  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
1885  uint32_t u32ReloadValue;
1886 
1887  /* Preset local register variables to zero */
1888  stc_mfs_smr_field_t stcSMR = { 0 };
1889  stc_mfs_scr_field_t stcSCR = { 0 };
1890  stc_mfs_escr_field_t stcESCR = { 0 };
1891  stc_mfs_ssr_field_t stcSSR = { 0 };
1892  stc_mfs_csio_sacsr_field_t stcSACSR = { 0 };
1893  stc_mfs_csio_scscr_field_t stcSCSCR = { 0 };
1894  uint16_t u16STMCR = 0u;
1895  uint16_t u16SCSTR32 = 0u;
1896  uint8_t u8SCSTR0 = 0u;
1897  uint8_t u8SCSTR1 = 0u;
1898  uint8_t u8TBYTE0 = 0u;
1899 
1900  /* Check for valid pointers and get pointer to internal data struct ... */
1901  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcCsio);
1902  /* ... and check */
1903  if ((NULL == pstcMfsHlInternData)
1904  || (NULL == pstcConfig)
1905  )
1906  {
1907  return (ErrorInvalidParameter);
1908  }
1909 
1910  /* RX Callback Level must be set somewhat ge zero (except I2C mode) */
1911  if (1 > pstcConfig->u16RxCbBufFillLvl)
1912  {
1913  return (ErrorInvalidParameter);
1914  }
1915 
1916  /* Check buffer for CSIO */
1917  if (((NULL == pstcConfig->pu8TxBuf)
1918  || (0u == pstcConfig->u16TxBufSize))
1919  || ((NULL == pstcConfig->pu8RxBuf)
1920  || (0u == pstcConfig->u16RxBufSize))
1921  )
1922  {
1923  return (ErrorInvalidParameter);
1924  }
1925 
1926  /* Get reload value */
1927  u32ReloadValue = Mfs_GetReloadValue(pstcConfig->u32DataRate);
1928  /* For synchronous communication, the reload value must be greater than */
1929  /* or equal to 3 because four times over-sampling is performed internally */
1930  if (MFS_HL_RELOADVAL_MIN4CSIO > u32ReloadValue)
1931  {
1932  return (ErrorInvalidParameter);
1933  }
1934 
1935  /* First of all set MFS to Asynchronous mode 0 */
1936  pstcCsio->SMR = 0u;
1937 
1938  /* Then we disable TX and RX for safe operation */
1939  pstcCsio->SCR = 0u;
1940 
1941  /* Clear MFS by setting the Software Reset bit */
1942  pstcCsio->SCR_f.UPCL = TRUE;
1943 
1944  /* Check the configration of FIFO usage */
1945  switch (pstcConfig->u8FifoUsage)
1946  {
1947  case MfsHlUseNoFifo:
1948  case MfsHlUseFifo:
1949  /* Set FIFO usage */
1950  pstcMfsHlInternData->u8FifoUsage = pstcConfig->u8FifoUsage;
1951  break;
1952  default:
1953  return (ErrorInvalidParameter);
1954  }
1955 
1956  /* ************ Register Settings ************ */
1957  /* Local Serial Mode Register variable */
1958 
1959  /* Operation Mode Selection */
1960  switch (pstcConfig->u8CsioMode)
1961  {
1962  /* Master mode */
1963  case MfsCsioMaster:
1964  stcSCR.MS = FALSE;
1965  break;
1966  /* Slave mode */
1967  case MfsCsioSlave:
1968  stcSCR.MS = TRUE;
1969  break;
1970  default:
1971  return (ErrorInvalidParameter); /* should never see the daylight */
1972  }
1973 
1974  /* Set mode (CSIO) */
1975  stcSMR.MD = MFS_MD_CSIO;
1976 
1977  /* Active Mode Selection */
1978  switch (pstcConfig->u8CsioActMode)
1979  {
1980  /* Normal mode */
1981  case MfsCsioActNormalMode:
1982  stcSCR.SPI = FALSE;
1983  break;
1984  /* SPI mode */
1985  case MfsCsioActSpiMode:
1986  stcSCR.SPI = TRUE;
1987  break;
1988  default:
1989  return (ErrorInvalidParameter); /* should never see the daylight */
1990  }
1991 
1992  /* Bit direction */
1993  if (TRUE == pstcConfig->bBitDirection)
1994  {
1995  stcSMR.BDS = TRUE;
1996  }
1997  else
1998  {
1999  stcSMR.BDS = FALSE;
2000  }
2001 
2002  /* now setup hardware with correct mode first and then go on with */
2003  /* bit settings */
2004  pstcCsio->SMR_f = stcSMR;
2005 
2006  /* Set synchronous clock input/output according master/slave mode */
2007  if (MfsCsioMaster == pstcConfig->u8CsioMode)
2008  {
2009  /* Master:Clock output */
2010  stcSMR.SCKE = TRUE;
2011  }
2012  else
2013  {
2014  /* Slave:Clock input */
2015  stcSMR.SCKE = FALSE;
2016  }
2017 
2018  /* Serial clock invert */
2019  if (TRUE == pstcConfig->bSignalSystem)
2020  {
2021  /* Mark level "LOW" */
2022  stcSMR.SCINV = TRUE;
2023  }
2024  else
2025  {
2026  /* Mark level "HIGH" */
2027  stcSMR.SCINV = FALSE;
2028  }
2029 
2030  /* Enable serial output */
2031  stcSMR.SOE = TRUE;
2032 
2033  /* Wait time insertion */
2034  switch (pstcConfig->u8SyncWaitTime)
2035  {
2036  case MfsSyncWaitZero:
2037  stcESCR.WT = MFS_WAIT_TIME_0BIT;
2038  break;
2039  case MfsSyncWaitOne:
2040  stcESCR.WT = MFS_WAIT_TIME_1BIT;
2041  break;
2042  case MfsSyncWaitTwo:
2043  stcESCR.WT = MFS_WAIT_TIME_2BITS;
2044  break;
2045  case MfsSyncWaitThree:
2046  stcESCR.WT = MFS_WAIT_TIME_3BITS;
2047  break;
2048  default:
2049  return (ErrorInvalidParameter);
2050  }
2051 
2052  stcESCR.SOP = FALSE;
2053 
2054  stcESCR.L3 = FALSE; /* less than 13 bits */
2055  stcSSR.AWC = FALSE; /* 16bit access for RDR/TDR (less than or equal to 16 bits) */
2056  /* Caracter Length */
2057  switch (pstcConfig->u8CharLength)
2058  {
2059  case MfsFiveBits:
2060  stcESCR.L = MFS_DATA_LEN_5;
2061  break;
2062  case MfsSixBits:
2063  stcESCR.L = MFS_DATA_LEN_6;
2064  break;
2065  case MfsSevenBits:
2066  stcESCR.L = MFS_DATA_LEN_7;
2067  break;
2068  case MfsEightBits:
2069  stcESCR.L = MFS_DATA_LEN_8;
2070  break;
2071  case MfsNineBits:
2072  stcESCR.L = MFS_DATA_LEN_9;
2073  break;
2074  case MfsTenBits:
2075  stcESCR.L = MFS_DATA_LEN_10;
2076  break;
2077  case MfsElevenBits:
2078  stcESCR.L = MFS_DATA_LEN_11;
2079  break;
2080  case MfsTwelveBits:
2081  stcESCR.L = MFS_DATA_LEN_12;
2082  break;
2083  case MfsThirteenBits:
2084  stcESCR.L = MFS_DATA_LEN_13;
2085  stcESCR.L3 = TRUE; /* More than 12 bits */
2086  break;
2087  case MfsFourteenBits:
2088  stcESCR.L = MFS_DATA_LEN_14;
2089  stcESCR.L3 = TRUE; /* More than 12 bits */
2090  break;
2091  case MfsFifteenBits:
2092  stcESCR.L = MFS_DATA_LEN_15;
2093  stcESCR.L3 = TRUE; /* More than 12 bits */
2094  break;
2095  case MfsSixteenBits:
2096  stcESCR.L = MFS_DATA_LEN_16;
2097  stcESCR.L3 = TRUE; /* More than 12 bits */
2098  break;
2099  case MfsTwentyBits:
2100  stcESCR.L = MFS_DATA_LEN_20;
2101  stcESCR.L3 = TRUE; /* More than 12 bits */
2102  stcSSR.AWC = TRUE; /* 32bit access for RDR/TDR (more than 16 bits) */
2103  break;
2104  case MfsTwentyFourBits:
2105  stcESCR.L = MFS_DATA_LEN_24;
2106  stcESCR.L3 = TRUE; /* More than 12 bits */
2107  stcSSR.AWC = TRUE; /* 32bit access for RDR/TDR (more than 16 bits) */
2108  break;
2109  case MfsThirtyTwoBits:
2110  stcESCR.L = MFS_DATA_LEN_32;
2111  stcESCR.L3 = TRUE; /* More than 12 bits */
2112  stcSSR.AWC = TRUE; /* 32bit access for RDR/TDR (more than 16 bits) */
2113  break;
2114  default:
2115  return (ErrorInvalidParameter);
2116  }
2117 
2118  /* now setup hardware (synchronous) */
2119  pstcCsio->SMR_f = stcSMR;
2120 
2121  /* now setup hardware */
2122  pstcCsio->SCR_f = stcSCR;
2123 
2124  /* now setup hardware */
2125  pstcCsio->ESCR_f = stcESCR;
2126  pstcCsio->SSR_f = stcSSR;
2127 
2128  /* Local Serial Status Register variable (USART) */
2129  /* Clear possible reception errors */
2130  pstcCsio->SSR_f.REC = TRUE;
2131 
2132  /* Master mode */
2133  if (MfsCsioMaster == pstcConfig->u8CsioMode)
2134  {
2135  /* Normal mode */
2136  if (MfsCsioActNormalMode == pstcConfig->u8CsioActMode)
2137  {
2138  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioNormalMaster;
2139  }
2140  /* SPI mode */
2141  else
2142  {
2143  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioSpiMaster;
2144  }
2145  }
2146  /* Slave mode */
2147  else
2148  {
2149  /* Normal mode */
2150  if (MfsCsioActNormalMode == pstcConfig->u8CsioActMode)
2151  {
2152  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioNormalSlave;
2153  }
2154  /* SPI mode */
2155  else
2156  {
2157  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioSpiSlave;
2158  }
2159  }
2160 
2161  /* SPI Chip select configration */
2162  if (NULL != pstcConfig->pstcMfsSpiCsConfig)
2163  {
2164  /* Chip select function sets when mode is master and SPI */
2165  if ((MfsCsioActSpiMode == pstcConfig->u8CsioActMode)
2166  /* Chip select is available only ch.6 or 7. */
2167  && ((pstcCsio == &MFS6) || (pstcCsio == &MFS7))
2168  )
2169  {
2170  /* Enable chip select */
2171  stcSCSCR.CSEN0 = TRUE;
2172  if (MfsCsioMaster == pstcConfig->u8CsioMode)
2173  {
2174  /* Enable chip select output */
2175  stcSCSCR.CSOE = TRUE;
2176  /* Set clock dividor for chip select activation */
2177  stcSCSCR.CDIV = pstcConfig->pstcMfsSpiCsConfig->u8CsDivision & MFS_HL_MASK_0_2;
2178  /* Set chip de-select bit */
2179  u16SCSTR32 = pstcConfig->pstcMfsSpiCsConfig->u16CsDeSelect;
2180  /* Set chip select setting delay */
2181  u8SCSTR0 = pstcConfig->pstcMfsSpiCsConfig->u8CsSetDelay;
2182  /* Set chip select holding delay */
2183  u8SCSTR1 = pstcConfig->pstcMfsSpiCsConfig->u8CsHoldDelay;
2184  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioSpiMasterWithCs;
2185  }
2186  else
2187  {
2188  /* Set chip select activation level */
2189  if (FALSE == pstcConfig->pstcMfsSpiCsConfig->bCsLevel)
2190  {
2191  /* In-active High (Active level low) */
2192  stcSCSCR.CSLVL = TRUE;
2193  }
2194  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioSpiSlaveWithCs;
2195  }
2196  /* now setup hardware */
2197  pstcCsio->SCSCR_f = stcSCSCR;
2198  pstcCsio->SCSTR32 = u16SCSTR32;
2199  pstcCsio->SCSTR0 = u8SCSTR0;
2200  pstcCsio->SCSTR1 = u8SCSTR1;
2201  }
2202  }
2203  /* Timer mode configration */
2204  else if (NULL != pstcConfig->pstcMfsTimerConfig)
2205  {
2206  /* Serial timer settings (master only) */
2207  if ((0u != pstcConfig->pstcMfsTimerConfig->u16SerialTimer)
2208  && (MfsCsioMaster == pstcConfig->u8CsioMode))
2209  {
2210  /* Set serial timer */
2211  u16STMCR = pstcConfig->pstcMfsTimerConfig->u16SerialTimer;
2212  /* Set serial timer dividor */
2213  stcSACSR.TDIV = pstcConfig->pstcMfsTimerConfig->u8TimerDivision & MFS_HL_MASK_0_3;
2214  /* Synchronous transfer with serial timer */
2215  if (TRUE == pstcConfig->pstcMfsTimerConfig->bTimerSyncEnable)
2216  {
2217  /* Enable synchronous transfer */
2218  stcSACSR.TSYNE = TRUE;
2219  u8TBYTE0 = pstcConfig->pstcMfsTimerConfig->u8TxByte;
2220  }
2221  /* now setup hardware */
2222  pstcCsio->SACSR_f = stcSACSR;
2223  pstcCsio->STMCR = u16STMCR;
2224  pstcCsio->TBYTE0 = u8TBYTE0;
2225  /* CSIO timer mode */
2226  pstcMfsHlInternData->u8MfsMode = MfsHlModeCsioNormalMasterTimer;
2227  }
2228  }
2229  else
2230  {
2231  /* There is no process that should be executed. */
2232  }
2233 
2234  /* Local Fifo Control Register 0 variable */
2235  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
2236  {
2237  /* Initialize FIFO to use */
2238  MfsHlFifoUseInit(pstcCsio, pstcConfig->u16RxCbBufFillLvl);
2239  }
2240 
2241  /* Save RX Callback Buffer Fill Level (needed during servicing RX INT) */
2242  pstcMfsHlInternData->u16RxCallbackBufFillLevel = pstcConfig->u16RxCbBufFillLvl;
2243 
2244  /* Save character length */
2245  pstcMfsHlInternData->u8DataWidth = pstcConfig->u8CharLength;
2246 
2247  /* Set baud rate generation reload register */
2248  pstcCsio->BGR = (uint16_t)(u32ReloadValue & MFS_HL_BGR_MASK);
2249 
2250  /* Set callback functions. */
2251  pstcMfsHlInternData->pfnTransmitCbFunction = pstcConfig->pfnTxCb;
2252  pstcMfsHlInternData->pfnReceiveCbFunction = pstcConfig->pfnRxCb;
2253 
2254  /* Initialise TX ring buffer */
2255  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = pstcConfig->pu8TxBuf;
2256  pstcMfsHlInternData->stcTxBuffer.u16BufferSize = pstcConfig->u16TxBufSize;
2257  pstcMfsHlInternData->stcTxBuffer.u16InIndex = 0u; /* Reset Index */
2258  pstcMfsHlInternData->stcTxBuffer.u16OutIndex = 0u; /* Reset Index */
2259  pstcMfsHlInternData->stcTxBuffer.u16FillCount = 0u; /* Reset Counter */
2260  pstcMfsHlInternData->stcTxBuffer.u16TxCounter = 0u; /* Reset Counter */
2261  pstcMfsHlInternData->stcTxBuffer.u16TxCount = 0u; /* Reset Counter */
2262  pstcMfsHlInternData->stcTxBuffer.bOverflow = 0u; /* Reset Flag */
2263 
2264  /* Initialise RX ring buffer */
2265  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = pstcConfig->pu8RxBuf;
2266  pstcMfsHlInternData->stcRxBuffer.u16BufferSize = pstcConfig->u16RxBufSize;
2267  pstcMfsHlInternData->stcRxBuffer.u16InIndex = 0u; /* Reset Index */
2268  pstcMfsHlInternData->stcRxBuffer.u16OutIndex = 0u; /* Reset Index */
2269  pstcMfsHlInternData->stcRxBuffer.u16FillCount = 0u; /* Reset Counter */
2270  pstcMfsHlInternData->stcRxBuffer.bOverflow = 0u; /* Reset Flag */
2271 
2272  /* MFS is standby */
2273  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
2274 
2275  /* Register interrupt handler and internal handle */
2276  (void)Mfs_SetTxIntCallBack(pstcCsio, MfsHlIrqHandlerTx);
2277  (void)Mfs_SetRxIntCallBack(pstcCsio, MfsHlIrqHandlerRx);
2278  (void)Mfs_SetStsIntCallBack(pstcCsio, MfsHlIrqHandlerSts);
2279  (void)Mfs_SetUpperLayerHandle(pstcCsio, pstcMfsHlInternData);
2280 
2281  /* Enable receiver, transmitter */
2282  pstcCsio->SCR_f.TXE = TRUE;
2283  pstcCsio->SCR_f.RXE = TRUE;
2284 
2285  /* Use interruption */
2286  if (NULL != pstcConfig->pfnRxCb)
2287  {
2288  /* Enable reception interrupt */
2289  pstcCsio->SCR_f.RIE = TRUE;
2290  }
2291 
2292  /* Init transmission interrupt */
2293  (void)Mfs_InitTxIrq(pstcCsio);
2294  /* Init reception interrupt */
2295  (void)Mfs_InitRxIrq(pstcCsio);
2296 
2297  return (Ok);
2298 } /* Mfs_Hl_Csio_Init */
2299 
2318 {
2319  en_result_t enResult;
2320  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
2321 
2322  /* Check for valid pointer and get pointer to internal data struct ... */
2323  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcCsio);
2324  /* ... and check */
2325  if (NULL == pstcMfsHlInternData)
2326  {
2327  enResult = ErrorInvalidParameter;
2328  }
2329  else
2330  {
2331  /* First of all disable receiver, transmitter and deinit interrupts */
2332  pstcCsio->SCR_f.TXE = FALSE;
2333  pstcCsio->SCR_f.RXE = FALSE;
2334 
2335  (void)Mfs_DeInitIrq(pstcCsio); /* returns always en_result_t Ok */
2336 
2337  /* Baud Rate Generation Reload Reset */
2338  pstcCsio->BGR = 0u;
2339 
2340  /* Serial Mode Register clear all bits (valid in any mode) */
2341  pstcCsio->SMR = 0u;
2342 
2343  /* Reset Mfs receive and transmit bit to default */
2344  /* and clear all error flags */
2345  pstcCsio->SCR = 0u;
2346 
2347  /* Clear reception Errors */
2348  pstcCsio->SSR_f.REC = TRUE;
2349 
2350  /* Reset all other used register to default value */
2351  pstcCsio->SSR = 0u;
2352  pstcCsio->ESCR = 0u;
2353  pstcCsio->FCR0 = 0u;
2354  pstcCsio->FCR1 = 0u;
2355  pstcCsio->SACSR = 0u;
2356  pstcCsio->SCSCR = 0u;
2357  pstcCsio->STMCR = 0u;
2358  pstcCsio->SCSTR10 = 0u;
2359  pstcCsio->SCSTR32 = 0u;
2360  pstcCsio->TBYTE0 = 0u;
2361 
2362  /* Clear MFS by setting the Software Reset bit */
2363  pstcCsio->SCR_f.UPCL = TRUE;
2364 
2365  /* Unregist interrupt handler and internal handle */
2366  (void)Mfs_SetTxIntCallBack(pstcCsio, NULL);
2367  (void)Mfs_SetRxIntCallBack(pstcCsio, NULL);
2368  (void)Mfs_SetStsIntCallBack(pstcCsio, NULL);
2369  (void)Mfs_SetUpperLayerHandle(pstcCsio, NULL);
2370 
2371  /* Reset Fifo buffer */
2372  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = NULL;
2373  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = NULL;
2374 
2375  /* Reset callback functions. */
2376  pstcMfsHlInternData->pfnTransmitCbFunction = NULL;
2377  pstcMfsHlInternData->pfnReceiveCbFunction = NULL;
2378 
2379  /* Reset LIN Break Callback used for Mfs_Lin_DeInit() */
2380  pstcMfsHlInternData->pfnLinBreakCbFunction = NULL;
2381  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
2382  pstcMfsHlInternData->u8MfsMode = MfsHlModeStby;
2383 
2384  enResult = Ok;
2385  }
2386 
2387  return (enResult);
2388 } /* Mfs_Hl_Csio_DeInit */
2389 
2451  const uint8_t* pu8TxData,
2452  uint8_t* pu8RxData,
2453  uint16_t u16TransferSize,
2454  boolean_t bCsHolding
2455  )
2456 {
2457  stc_mfs_hl_intern_data_t* pstcMfsHlInternData ;
2458  uint16_t u16DataToSendCounter;
2459  uint16_t u16DataReceivedCounter;
2460  uint8_t u8Data;
2461  volatile uint8_t u8Reg;
2462 
2463  /* Check for valid pointers and get pointer to internal data struct ... */
2464  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcCsio);
2465  /* ... and check */
2466  if ((NULL == pstcMfsHlInternData)
2467  || ((NULL == pu8TxData) && (NULL == pu8RxData))
2468  /* Check for 0 < transmission length */
2469  || (0u == u16TransferSize)
2470  )
2471  {
2472  return (ErrorInvalidParameter);
2473  }
2474 
2475  /* check for valid mode (only sync mode allowed) */
2476  if (MFS_MD_CSIO != pstcCsio->SMR_f.MD)
2477  {
2478  return (ErrorInvalidMode);
2479  }
2480 
2481  /* This function can use when character length is set less or equel to 8 bits. */
2482  if (MfsEightBits < pstcMfsHlInternData->u8DataWidth)
2483  {
2484  return (Error);
2485  }
2486 
2487  switch (pstcMfsHlInternData->u8MfsMode)
2488  {
2489  /* Set tx bytes when chip select is used on master mode */
2491  if (MFS_CSIO_TBYTE_MAX < u16TransferSize)
2492  {
2493  return (ErrorInvalidParameter);
2494  }
2495  pstcCsio->SCR_f.TXE = FALSE;
2496  /* Set size to active chip select */
2497  pstcCsio->TBYTE0 = (uint8_t)u16TransferSize;
2498  if (TRUE == bCsHolding)
2499  {
2500  /* Hold chip select */
2501  pstcCsio->SCSCR_f.SCAM = TRUE;
2502  }
2503  else
2504  {
2505  /* In-active chip select when transmit is end */
2506  pstcCsio->SCSCR_f.SCAM = FALSE;
2507  }
2508  pstcCsio->SCR_f.TXE = TRUE;
2509  break;
2510 
2511  /* CSIO timer mode */
2513  /* Enable serial timer */
2514  pstcCsio->SCR_f.TXE = FALSE;
2515  pstcCsio->SACSR_f.TMRE = TRUE;
2516  pstcCsio->SCR_f.TXE = TRUE;
2517  break;
2518 
2519  default:
2520  break;
2521  }
2522 
2523  /* Use interruption */
2524  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
2525  {
2526  /* Disable reception interrupt during sending and receiving data */
2527  pstcCsio->SCR_f.RIE = FALSE;
2528  }
2529 
2530  u16DataToSendCounter = 0u;
2531  u16DataReceivedCounter = 0u;
2532 
2533  while (u16DataReceivedCounter != u16TransferSize)
2534  {
2535  if (u16DataToSendCounter < u16TransferSize)
2536  {
2537  /* Wait for TDR empty */
2538  while(FALSE == pstcCsio->SSR_f.TDRE)
2539  {
2541  }
2542  /* If pu8TxData is NULL, dummy data is sent. */
2543  u8Data = 0u;
2544  if (NULL != pu8TxData)
2545  {
2546  u8Data = pu8TxData[u16DataToSendCounter];
2547  }
2548  /* Send data */
2549  pstcCsio->TDR = (uint16_t)u8Data;
2550  u16DataToSendCounter++;
2551  }
2552 
2553  /* To avoid inter-byte spikes on SOT, send out 2nd byte immediately, */
2554  /* if TDR is empty! */
2555  if ((MFS_HL_NUM_1 == u16DataToSendCounter) && (MFS_HL_NUM_1 < u16TransferSize))
2556  {
2557  /* Wait for TDR empty */
2558  while(FALSE == pstcCsio->SSR_f.TDRE)
2559  {
2561  }
2562  /* If pu8TxData is NULL, dummy data is sent. */
2563  u8Data = 0u;
2564  if (NULL != pu8TxData)
2565  {
2566  u8Data = pu8TxData[u16DataToSendCounter];
2567  }
2568  pstcCsio->TDR = (uint16_t)u8Data;
2569  u16DataToSendCounter++;
2570  }
2571 
2572  do
2573  {
2574  /* Check reception */
2575  u8Reg = pstcCsio->SSR;
2576  /* wait for reception finsihed */
2577  } while (0u == (u8Reg & MFS_SSR_RDRF));
2578  /* Check Overrun error */
2579  if (0u != (u8Reg & MFS_CSIO_SSR_ERR))
2580  {
2581  pstcCsio->SSR_f.REC = TRUE; /* Clear possible reception errors */
2582  }
2583  /* If pu8RxData is NULL, dummy data is received. */
2584  u8Data = (uint8_t)pstcCsio->RDR;
2585  if (NULL != pu8RxData)
2586  {
2587  pu8RxData[u16DataReceivedCounter] = u8Data;
2588  }
2589  u16DataReceivedCounter++;
2590  }
2591 
2592  /* CSIO timer mode */
2593  if (MfsHlModeCsioNormalMasterTimer == pstcMfsHlInternData->u8MfsMode)
2594  {
2595  /* Wait for TX bus idle */
2596  while (FALSE == pstcCsio->SSR_f.TBI)
2597  {
2598  /* Disable serial timer */
2599  pstcCsio->SCR_f.TXE = FALSE;
2600  pstcCsio->SACSR_f.TMRE = FALSE;
2601  pstcCsio->SCR_f.TXE = TRUE;
2602  }
2603  }
2604 
2605  /* Use interruption */
2606  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
2607  {
2608  /* Enable reception interrupt */
2609  pstcCsio->SCR_f.RIE = TRUE;
2610  }
2611 
2612  return (Ok);
2613 } /* Mfs_Hl_Csio_SynchronousTrans */
2614 
2638  stc_mfs_hl_lin_config_t* pstcConfig
2639  )
2640 {
2641  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
2642  uint32_t u32ReloadValue;
2643 
2644  /* Preset local register variables to zero */
2645  stc_mfs_smr_field_t stcSMR = { 0 };
2646  stc_mfs_scr_field_t stcSCR = { 0 };
2647  stc_mfs_escr_field_t stcESCR = { 0 };
2648 
2649  /* Check for valid pointers and get pointer to internal data struct ... */
2650  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcLin);
2651  /* ... and check */
2652  if ((NULL == pstcMfsHlInternData)
2653  || (NULL == pstcConfig)
2654  )
2655  {
2656  return (ErrorInvalidParameter);
2657  }
2658 
2659  /* Get reload value */
2660  u32ReloadValue = Mfs_GetReloadValue(pstcConfig->u32DataRate);
2661  /* For asynchronous communication, the reload value must be greater than */
2662  /* or equal to 3 because five times over-sampling is performed internally */
2663  if (MFS_HL_RELOADVAL_MIN4LIN > u32ReloadValue)
2664  {
2665  return (ErrorInvalidParameter);
2666  }
2667 
2668  /* Check buffer for LIN */
2669  if (((NULL == pstcConfig->pu8TxBuf)
2670  || (0u == pstcConfig->u16TxBufSize))
2671  || ((NULL == pstcConfig->pu8RxBuf)
2672  || (0u == pstcConfig->u16RxBufSize)))
2673  {
2674  return (ErrorInvalidParameter);
2675  }
2676 
2677  /* Check the configration of FIFO usage */
2678  switch (pstcConfig->u8FifoUsage)
2679  {
2680  case MfsHlUseNoFifo:
2681  case MfsHlUseFifo:
2682  /* Set FIFO usage */
2683  pstcMfsHlInternData->u8FifoUsage = pstcConfig->u8FifoUsage;
2684  break;
2685  default:
2686  return (ErrorInvalidParameter);
2687  }
2688 
2689  /* First of all set MFS to Asynchronous mode 0 */
2690  pstcLin->SMR = 0u;
2691 
2692  /* Then we disable TX and RX for safe operation */
2693  pstcLin->SCR = 0u;
2694 
2695  /* Clear MFS by setting the Software Reset bit */
2696  pstcLin->SCR_f.UPCL = TRUE;
2697 
2698  /* Set LIN mode to HW and preset variable */
2699  stcSMR.MD = MFS_MD_LIN;
2700 
2701  /* Serial output */
2702  stcSMR.SOE = TRUE;
2703 
2704  /* Operation Mode Selection */
2705  switch (pstcConfig->u8LinMode)
2706  {
2707  /* Master */
2708  case MfsLinMaster:
2709  stcSCR.MS = FALSE;
2710  break;
2711  /* Slave */
2712  case MfsLinSlave:
2713  stcSCR.MS = TRUE;
2714  break;
2715  default:
2716  return (ErrorInvalidParameter);
2717  }
2718 
2719  /* Set LIN master configuration */
2720  if (MfsLinMaster == pstcConfig->u8LinMode)
2721  {
2722  /* Stop bits configration */
2723  switch (pstcConfig->u8StopBits)
2724  {
2725  case MfsLinOneStopBit:
2726  stcSMR.SBL = FALSE;
2727  stcESCR.ESBL = FALSE;
2728  break;
2729  case MfsLinTwoStopBits:
2730  stcSMR.SBL = TRUE;
2731  stcESCR.ESBL = FALSE;
2732  break;
2733  case MfsLinThreeStopBits:
2734  stcSMR.SBL = FALSE;
2735  stcESCR.ESBL = TRUE;
2736  break;
2737  case MfsLinFourStopBits:
2738  stcSMR.SBL = TRUE;
2739  stcESCR.ESBL = TRUE;
2740  break;
2741  default:
2742  return (ErrorInvalidParameter);
2743  }
2744  /* Break length configration */
2745  switch (pstcConfig->u8BreakLength)
2746  {
2747  case MfsLinBreakLength13:
2748  stcESCR.LBL = MFS_LIN_BREAK_13BITS;
2749  break;
2750  case MfsLinBreakLength14:
2751  stcESCR.LBL = MFS_LIN_BREAK_14BITS;
2752  break;
2753  case MfsLinBreakLength15:
2754  stcESCR.LBL = MFS_LIN_BREAK_15BITS;
2755  break;
2756  case MfsLinBreakLength16:
2757  stcESCR.LBL = MFS_LIN_BREAK_16BITS;
2758  break;
2759  default:
2760  return (ErrorInvalidParameter);
2761  }
2762  /* Delimiter length configration */
2763  switch (pstcConfig->u8DelimiterLength)
2764  {
2766  stcESCR.DEL = MFS_LIN_DELIMITER_1BIT;
2767  break;
2769  stcESCR.DEL = MFS_LIN_DELIMITER_2BITS;
2770  break;
2772  stcESCR.DEL = MFS_LIN_DELIMITER_3BITS;
2773  break;
2775  stcESCR.DEL = MFS_LIN_DELIMITER_4BITS;
2776  break;
2777  default:
2778  return (ErrorInvalidParameter);
2779  }
2780  }
2781 
2782  /* Lin break interrupt */
2783  if (TRUE == pstcConfig->bLinBreakIrqEnable)
2784  {
2785  stcESCR.LBIE = TRUE;
2786  }
2787  else
2788  {
2789  stcESCR.LBIE = FALSE;
2790  }
2791 
2792  /* Local Fifo Control Register 0 variable */
2793  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
2794  {
2795  /* Initialize FIFO to use */
2797  }
2798 
2799  /* Save RX Callback Buffer Fill Level (needed during servicing RX INT) */
2800  pstcMfsHlInternData->u16RxCallbackBufFillLevel = MFS_RX_FIFO_MIN_VAL;
2801 
2802  /* Save character length (Only 8bits) */
2803  pstcMfsHlInternData->u8DataWidth = MfsEightBits;
2804 
2805  /* Set baud rate generation reload register */
2806  pstcLin->BGR = (uint16_t)(u32ReloadValue & MFS_HL_BGR_MASK);
2807 
2808  /* Set callback functions. */
2809  pstcMfsHlInternData->pfnTransmitCbFunction = pstcConfig->pfnTxCb;
2810  pstcMfsHlInternData->pfnReceiveCbFunction = pstcConfig->pfnRxCb;
2811  pstcMfsHlInternData->pfnLinBreakCbFunction = pstcConfig->pfnLinBrkCb;
2812 
2813  /* Initialise TX ring buffer */
2814  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = pstcConfig->pu8TxBuf;
2815  pstcMfsHlInternData->stcTxBuffer.u16BufferSize = pstcConfig->u16TxBufSize;
2816  pstcMfsHlInternData->stcTxBuffer.u16InIndex = 0u; /* Reset Index */
2817  pstcMfsHlInternData->stcTxBuffer.u16OutIndex = 0u; /* Reset Index */
2818  pstcMfsHlInternData->stcTxBuffer.u16FillCount = 0u; /* Reset Counter */
2819  pstcMfsHlInternData->stcTxBuffer.u16TxCounter = 0u; /* Reset Counter */
2820  pstcMfsHlInternData->stcTxBuffer.u16TxCount = 0u; /* Reset Counter */
2821  pstcMfsHlInternData->stcTxBuffer.bOverflow = 0u; /* Reset Flag */
2822 
2823  /* Initialise RX ring buffer */
2824  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = pstcConfig->pu8RxBuf;
2825  pstcMfsHlInternData->stcRxBuffer.u16BufferSize = pstcConfig->u16RxBufSize;
2826  pstcMfsHlInternData->stcRxBuffer.u16InIndex = 0u; /* Reset Index */
2827  pstcMfsHlInternData->stcRxBuffer.u16OutIndex = 0u; /* Reset Index */
2828  pstcMfsHlInternData->stcRxBuffer.u16FillCount = 0u; /* Reset Counter */
2829  pstcMfsHlInternData->stcRxBuffer.bOverflow = 0u; /* Reset Flag */
2830 
2831  /* MFS is standby */
2832  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
2833 
2834  /* Operation Mode Selection */
2835  if (MfsLinMaster == pstcConfig->u8LinMode)
2836  {
2837  /* Master mode */
2838  pstcMfsHlInternData->u8MfsMode = MfsHlModeLinMaster;
2839  }
2840  else
2841  {
2842  /* Slave mode */
2843  pstcMfsHlInternData->u8MfsMode = MfsHlModeLinSlave;
2844  }
2845 
2846 
2847  /* Register interrupt handler and internal handle */
2848  (void)Mfs_SetTxIntCallBack(pstcLin, MfsHlIrqHandlerTx);
2849  (void)Mfs_SetRxIntCallBack(pstcLin, MfsHlIrqHandlerRx);
2850  (void)Mfs_SetStsIntCallBack(pstcLin, MfsHlIrqHandlerSts);
2851  (void)Mfs_SetUpperLayerHandle(pstcLin, pstcMfsHlInternData);
2852 
2853  /* Setup hardware */
2854  pstcLin->SMR_f = stcSMR;
2855  pstcLin->SCR_f = stcSCR;
2856  pstcLin->ESCR_f = stcESCR;
2857 
2858  /* Enable receiver, transmitter */
2859  pstcLin->SCR_f.TXE = TRUE;
2860  pstcLin->SCR_f.RXE = TRUE;
2861 
2862  /* Use interruption */
2863  if (NULL != pstcConfig->pfnRxCb)
2864  {
2865  /* Enable reception interrupt */
2866  pstcLin->SCR_f.RIE = TRUE;
2867  }
2868 
2869  /* Init transmission interrupt */
2870  (void)Mfs_InitTxIrq(pstcLin);
2871  /* Init reception interrupt */
2872  (void)Mfs_InitRxIrq(pstcLin);
2873 
2874  return (Ok);
2875 } /* Mfs_Hl_Lin_Init */
2876 
2896 {
2897  en_result_t enResult;
2898 
2899  /* Check correct LIN master mode */
2900  if (MFS_SMR_LIN_MODE != (pstcLin->SMR & MFS_SMR_MODEBITS_MASK))
2901  {
2902  enResult = ErrorInvalidMode;
2903  }
2904  else
2905  {
2906  enResult = Mfs_Hl_Uart_DeInit(pstcLin);
2907  }
2908 
2909  return (enResult);
2910 } /* Mfs_Hl_Lin_DeInit */
2911 
2933 {
2934  en_result_t enResult;
2935  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
2936 
2937  /* Check for valid pointer and get pointer to internal data struct ... */
2938  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcLin);
2939  /* ... and check */
2940  if (NULL == pstcMfsHlInternData)
2941  {
2942  enResult = ErrorInvalidParameter;
2943  }
2944 
2945  /* Check correct LIN master mode */
2946  else if ((MFS_SMR_LIN_MODE != (pstcLin->SMR & MFS_SMR_MODEBITS_MASK))
2947  || (TRUE == pstcLin->SCR_f.MS) )
2948  {
2949  enResult = ErrorInvalidMode;
2950  }
2951 
2952  /* Check if transmission bus is free (no transmission ongoing) */
2953  else if (FALSE == pstcLin->SSR_f.TBI)
2954  {
2955  enResult = ErrorOperationInProgress;
2956  }
2957 
2958  else
2959  {
2960  /* Finally generate LIN break with configured length and delimiter */
2961  pstcLin->SCR_f.LBR = TRUE;
2962  enResult = Ok;
2963  }
2964 
2965  return (enResult);
2966 } /* Mfs_Hl_Lin_SetBreak */
2967 
2994  uint16_t u16BaudDivisor
2995  )
2996 {
2997  en_result_t enResult;
2998  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
2999 
3000  /* Check for valid pointer and get pointer to internal data struct ... */
3001  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcLin);
3002  /* ... and check */
3003  if (NULL == pstcMfsHlInternData)
3004  {
3005  enResult = ErrorInvalidParameter;
3006  }
3007 
3008  /* Check correct LIN slave mode */
3009  else if ((MFS_SMR_LIN_MODE != (pstcLin->SMR & MFS_SMR_MODEBITS_MASK))
3010  || (FALSE == pstcLin->SCR_f.MS) )
3011  {
3012  enResult = ErrorInvalidMode;
3013  }
3014 
3015  else
3016  {
3017  /* Update Baud Rate register */
3018  pstcLin->BGR = u16BaudDivisor;
3019  enResult = Ok;
3020  }
3021 
3022  return (enResult);
3023 } /* Mfs_Hl_Lin_SetNewBaudDivisor */
3024 
3042  uint8_t* pu8Data,
3043  uint16_t u16ReadCount)
3044 {
3045  en_result_t enResult;
3046  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
3047  uint16_t u16Count;
3048  int32_t i32BufferCount;
3049 
3050  /* Check for valid pointers and get pointer to internal data struct ... */
3051  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcLin);
3052  /* ... and check */
3053  if ((NULL == pstcMfsHlInternData)
3054  || (NULL == pu8Data)
3055  )
3056  {
3057  enResult = ErrorInvalidParameter;
3058  }
3059 
3060  else
3061  {
3062  enResult = Ok;
3063  /* Check if nothing to do */
3064  if (0u != u16ReadCount)
3065  {
3066  /* Calculate internal buffer index */
3067  i32BufferCount = (int32_t)(pstcMfsHlInternData->stcRxBuffer.u16InIndex) - (int32_t)(u16ReadCount);
3068 
3069  /* Adjust ring buffer index? */
3070  if (0 > i32BufferCount)
3071  {
3072  i32BufferCount += (int32_t)pstcMfsHlInternData->stcRxBuffer.u16BufferSize;
3073  }
3074 
3075  for (u16Count = 0u; u16Count < u16ReadCount; u16Count++)
3076  {
3077  pu8Data[u16Count] = pstcMfsHlInternData->stcRxBuffer.pu8Buffer[i32BufferCount];
3078  i32BufferCount++;
3079 
3080  if (i32BufferCount > (int32_t)pstcMfsHlInternData->stcRxBuffer.u16BufferSize)
3081  {
3082  i32BufferCount = 0;
3083  }
3084  }
3085  }
3086  }
3087 
3088  return (enResult);
3089 } /* Mfs_Hl_Lin_TransferRxBuffer */
3090 
3103 {
3104  en_result_t enResult;
3105  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
3106 
3107  /* Check for valid pointer and get pointer to internal data struct ... */
3108  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcLin);
3109  /* ... and check */
3110  if (NULL == pstcMfsHlInternData)
3111  {
3112  enResult = ErrorInvalidParameter;
3113  }
3114 
3115  else
3116  {
3117  pstcLin->SCR_f.RIE = FALSE;
3118  enResult = Ok;
3119  }
3120 
3121  return (enResult);
3122 } /* Mfs_Hl_Lin_DisableRxInterrupt */
3123 
3148  stc_mfs_hl_i2c_config_t* pstcConfig
3149  )
3150 {
3151  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
3152  uint32_t u32ReloadValue;
3153  uint32_t u32BusClock;
3154 
3155  /* Preset local register variables to zero */
3156  stc_mfs_smr_field_t stcSMR = { 0 };
3157  stc_mfs_i2c_ibcr_field_t stcIBCR = { 0 };
3158  stc_mfs_i2c_isba_field_t stcISBA = { 0 };
3159  stc_mfs_i2c_ismk_field_t stcISMK = { 0 };
3160  uint8_t u8NFCR = 0;
3161 
3162  /* Check for valid pointers and get pointer to internal data struct ... */
3163  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcI2c);
3164  /* ... and check */
3165  if ((NULL == pstcMfsHlInternData)
3166  || (NULL == pstcConfig)
3167  )
3168  {
3169  return (ErrorInvalidParameter);
3170  }
3171 
3172  /* Get reload value */
3173  u32ReloadValue = Mfs_I2c_GetReloadValue(pstcConfig->u32DataRate);
3174  /* For asynchronous communication, the reload value must be greater than 0 */
3175  /* because reload counter is stop */
3176  if (0u == u32ReloadValue)
3177  {
3178  return (ErrorInvalidParameter);
3179  }
3180 
3181  /* Check the configration of FIFO usage */
3182  switch (pstcConfig->u8FifoUsage)
3183  {
3184  case MfsHlUseNoFifo:
3185  case MfsHlUseFifo:
3186  /* Set FIFO usage */
3187  pstcMfsHlInternData->u8FifoUsage = pstcConfig->u8FifoUsage;
3188  break;
3189  default:
3190  return (ErrorInvalidParameter);
3191  }
3192 
3193  /* ************ Register Settings ************ */
3194  /* Local Serial Mode Register variable */
3195 
3196  /* Operation Mode Selection */
3197  switch (pstcConfig->u8I2cMode)
3198  {
3199  /* I2C master mode */
3200  case MfsI2cMaster:
3201  /* I2C slave mode */
3202  case MfsI2cSlave:
3203  stcSMR.MD = MFS_MD_I2C;
3204  break;
3205  default:
3206  return (ErrorInvalidParameter); /* should never see the daylight */
3207  }
3208 
3209  /* Noize filter settings according to the APB2 bus clock */
3210  u32BusClock = Mfs_GetBusClock();
3211  if (MFS_CLOCK_40M > u32BusClock)
3212  {
3213  u8NFCR = MfsI2cNoizeFilterLess40M;
3214  }
3215  else if (MFS_CLOCK_60M > u32BusClock)
3216  {
3217  u8NFCR = MfsI2cNoizeFilterLess60M;
3218  }
3219  else if (MFS_CLOCK_80M > u32BusClock)
3220  {
3221  u8NFCR = MfsI2cNoizeFilterLess80M;
3222  }
3223  else if (MFS_CLOCK_100M > u32BusClock)
3224  {
3225  u8NFCR = MfsI2cNoizeFilterLess100M;
3226  }
3227  else if (MFS_CLOCK_120M > u32BusClock)
3228  {
3229  u8NFCR = MfsI2cNoizeFilterLess120M;
3230  }
3231  else if (MFS_CLOCK_140M > u32BusClock)
3232  {
3233  u8NFCR = MfsI2cNoizeFilterLess140M;
3234  }
3235  else if (MFS_CLOCK_160M > u32BusClock)
3236  {
3237  u8NFCR = MfsI2cNoizeFilterLess160M;
3238  }
3239  else if (MFS_CLOCK_180M > u32BusClock)
3240  {
3241  u8NFCR = MfsI2cNoizeFilterLess180M;
3242  }
3243  else if (MFS_CLOCK_200M > u32BusClock)
3244  {
3245  u8NFCR = MfsI2cNoizeFilterLess200M;
3246  }
3247  else if (MFS_CLOCK_220M > u32BusClock)
3248  {
3249  u8NFCR = MfsI2cNoizeFilterLess220M;
3250  }
3251  else if (MFS_CLOCK_240M > u32BusClock)
3252  {
3253  u8NFCR = MfsI2cNoizeFilterLess240M;
3254  }
3255  else if (MFS_CLOCK_260M > u32BusClock)
3256  {
3257  u8NFCR = MfsI2cNoizeFilterLess260M;
3258  }
3259  else if (MFS_CLOCK_280M > u32BusClock)
3260  {
3261  u8NFCR = MfsI2cNoizeFilterLess280M;
3262  }
3263  else if (MFS_CLOCK_300M > u32BusClock)
3264  {
3265  u8NFCR = MfsI2cNoizeFilterLess300M;
3266  }
3267  else if (MFS_CLOCK_320M > u32BusClock)
3268  {
3269  u8NFCR = MfsI2cNoizeFilterLess320M;
3270  }
3271  else if (MFS_CLOCK_340M > u32BusClock)
3272  {
3273  u8NFCR = MfsI2cNoizeFilterLess340M;
3274  }
3275  else if (MFS_CLOCK_360M > u32BusClock)
3276  {
3277  u8NFCR = MfsI2cNoizeFilterLess360M;
3278  }
3279  else if (MFS_CLOCK_380M > u32BusClock)
3280  {
3281  u8NFCR = MfsI2cNoizeFilterLess380M;
3282  }
3283  else if (MFS_CLOCK_400M > u32BusClock)
3284  {
3285  u8NFCR = MfsI2cNoizeFilterLess400M;
3286  }
3287  else
3288  {
3289  return (ErrorUninitialized);
3290  }
3291 
3292  /* Fast mode selection */
3293  if ((pstcI2c == MFS_I2C_FAST_MODE_PLUS_CH_A)
3294  || (pstcI2c == MFS_I2C_FAST_MODE_PLUS_CH_B)
3295  )
3296  {
3297  switch (pstcConfig->u8FastMode)
3298  {
3299  /* Standard-mode */
3301  if (pstcI2c == MFS_I2C_FAST_MODE_PLUS_CH_A)
3302  {
3303  FM4_GPIO->EPFR16_f.SFMPAC = FALSE;
3304  }
3305  else
3306  {
3307  FM4_GPIO->EPFR16_f.SFMPBC = FALSE;
3308  }
3309  break;
3310  /* Fast-mode Plus */
3312  if (pstcI2c == MFS_I2C_FAST_MODE_PLUS_CH_A)
3313  {
3314  FM4_GPIO->EPFR16_f.SFMPAC = TRUE;
3315  }
3316  else
3317  {
3318  FM4_GPIO->EPFR16_f.SFMPBC = TRUE;
3319  }
3320  break;
3321  default:
3322  return (ErrorInvalidParameter); /* should never see the daylight */
3323  }
3324  }
3325 
3326  /* Ack enable */
3327  stcIBCR.ACKE = TRUE;
3328 
3329  /* Slave mode */
3330  if (MfsI2cSlave == pstcConfig->u8I2cMode)
3331  {
3332  /* Enable stop condition interrupt */
3333  stcIBCR.CNDE = TRUE;
3334  /* Enable interrupt */
3335  stcIBCR.INTE = TRUE;
3336  /* Set slave address */
3337  stcISBA.SA = pstcConfig->u8SlvAddr;
3338  /* Enable slave address detection */
3339  stcISBA.SAEN = TRUE;
3340  }
3341 
3342  /* Enable I2C, Enable bit comparing */
3343  stcISMK.SM = MFS_HL_MASK_0_6;
3344  stcISMK.EN = TRUE;
3345 
3346  /* now setup hardware with correct mode first and then go on with */
3347  /* bit settings */
3348  pstcI2c->SMR_f = stcSMR;
3349 
3350  /* I2C disable before other registers are set. */
3351  pstcI2c->ISMK = 0u;
3352 
3353  /* Local Fifo Control Register 0 variable */
3354  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
3355  {
3356  /* Initialize FIFO to use */
3358  }
3359 
3360  /* Save RX Callback Buffer Fill Level (needed during servicing RX INT) */
3361  pstcMfsHlInternData->u16RxCallbackBufFillLevel = 0u;
3362 
3363  /* Save character length */
3364  pstcMfsHlInternData->u8DataWidth = MfsEightBits;
3365 
3366  /* Clear callback functions. */
3367  pstcMfsHlInternData->pfnTransmitCbFunction = pstcConfig->pfnTxCb;
3368  pstcMfsHlInternData->pfnReceiveCbFunction = pstcConfig->pfnRxCb;
3369 
3370  /* Initialise TX ring buffer */
3371  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = NULL;
3372  pstcMfsHlInternData->stcTxBuffer.u16BufferSize = 0u;
3373  pstcMfsHlInternData->stcTxBuffer.u16InIndex = 0u; /* Reset Index */
3374  pstcMfsHlInternData->stcTxBuffer.u16OutIndex = 0u; /* Reset Index */
3375  pstcMfsHlInternData->stcTxBuffer.u16FillCount = 0u; /* Reset Counter */
3376  pstcMfsHlInternData->stcTxBuffer.u16TxCounter = 0u; /* Reset Counter */
3377  pstcMfsHlInternData->stcTxBuffer.u16TxCount = 0u; /* Reset Counter */
3378  pstcMfsHlInternData->stcTxBuffer.bOverflow = 0u; /* Reset Flag */
3379 
3380  /* Initialise RX ring buffer */
3381  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = NULL;
3382  pstcMfsHlInternData->stcRxBuffer.u16BufferSize = 0u;
3383  pstcMfsHlInternData->stcRxBuffer.u16InIndex = 0u; /* Reset Index */
3384  pstcMfsHlInternData->stcRxBuffer.u16OutIndex = 0u; /* Reset Index */
3385  pstcMfsHlInternData->stcRxBuffer.u16FillCount = 0u; /* Reset Counter */
3386  pstcMfsHlInternData->stcRxBuffer.bOverflow = 0u; /* Reset Flag */
3387 
3388  /* MFS is standby */
3389  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
3390 
3391  /* Operation Mode Selection */
3392  if (MfsI2cMaster == pstcConfig->u8I2cMode)
3393  {
3394  /* Master mode */
3395  pstcMfsHlInternData->u8MfsMode = MfsHlModeI2cMaster;
3396  /* Clear the starting slave callback function. */
3397  pstcMfsHlInternData->pfnI2cSlvStCbFunction = NULL;
3398  }
3399  else
3400  {
3401  /* Slave mode */
3402  pstcMfsHlInternData->u8MfsMode = MfsHlModeI2cSlave;
3403  /* Set the starting slave callback function. */
3404  pstcMfsHlInternData->pfnI2cSlvStCbFunction = pstcConfig->pfnI2cSlvStCb;
3405  }
3406 
3407  /* Register interrupt handler and internal handle */
3411  (void)Mfs_SetUpperLayerHandle(pstcI2c, pstcMfsHlInternData);
3412 
3413  /* Set baud rate generation reload register */
3414  pstcI2c->BGR = (uint16_t)(u32ReloadValue & MFS_HL_BGR_MASK);
3415 
3416  pstcI2c->NFCR = u8NFCR;
3417 
3418  pstcI2c->IBCR_f = stcIBCR;
3419  pstcI2c->ISBA_f = stcISBA;
3420  pstcI2c->ISMK_f = stcISMK;
3421 
3422  /* Init transmission interrupt */
3423  (void)Mfs_InitTxIrq(pstcI2c);
3424  /* Init reception interrupt */
3425  (void)Mfs_InitRxIrq(pstcI2c);
3426 
3427  return (Ok);
3428 } /* Mfs_Hl_I2c_Init */
3429 
3447 {
3448  en_result_t enResult;
3449  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
3450 
3451  /* Check for valid pointer and get pointer to internal data struct ... */
3452  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcI2c);
3453  /* ... and check */
3454  if (NULL == pstcMfsHlInternData)
3455  {
3456  enResult = ErrorInvalidParameter;
3457  }
3458  else
3459  {
3460  /* Disable I2C */
3461  pstcI2c->ISMK_f.EN = FALSE;
3462 
3463  (void)Mfs_DeInitIrq(pstcI2c); /* returns always en_result_t Ok */
3464 
3465  /* Serial Mode Register clear all bits (valid in any mode) */
3466  pstcI2c->SMR = 0u;
3467 
3468  /* Reset Mfs receive and transmit bit to default */
3469  /* and clear all error flags */
3470  pstcI2c->IBCR = 0u;
3471 
3472  /* Clear reception Errors */
3473  pstcI2c->SSR_f.REC = TRUE;
3474 
3475  /* Reset all other used register to default value */
3476  pstcI2c->SSR = 0u;
3477  pstcI2c->IBSR = 0u;
3478  pstcI2c->FCR0 = 0u;
3479  pstcI2c->FCR1 = 0u;
3480  pstcI2c->NFCR = 0u;
3481 
3482  /* Unregist interrupt handler and internal handle */
3483  (void)Mfs_SetTxIntCallBack(pstcI2c, NULL);
3484  (void)Mfs_SetRxIntCallBack(pstcI2c, NULL);
3485  (void)Mfs_SetStsIntCallBack(pstcI2c, NULL);
3486  (void)Mfs_SetUpperLayerHandle(pstcI2c, NULL);
3487 
3488  /* Reset Fifo buffer */
3489  pstcMfsHlInternData->stcTxBuffer.pu8Buffer = NULL;
3490  pstcMfsHlInternData->stcRxBuffer.pu8Buffer = NULL;
3491 
3492  /* Reset callback functions. */
3493  pstcMfsHlInternData->pfnTransmitCbFunction = NULL;
3494  pstcMfsHlInternData->pfnReceiveCbFunction = NULL;
3495  pstcMfsHlInternData->pfnI2cSlvStCbFunction = NULL;
3496 
3497  /* Reset LIN Break Callback used for Mfs_Lin_DeInit() */
3498  pstcMfsHlInternData->pfnLinBreakCbFunction = NULL;
3499  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
3500  pstcMfsHlInternData->u8MfsMode = MfsHlModeStby;
3501 
3502  enResult = Ok;
3503  }
3504 
3505  return (enResult);
3506 } /* Mfs_Hl_I2c_DeInit */
3507 
3519 static en_result_t MfsHlI2cStartSlave(volatile stc_mfsn_t* pstcI2c,
3520  stc_mfs_hl_intern_data_t* pstcMfsHlInternData
3521  )
3522 {
3523  en_result_t enResult;
3524  stc_mfs_smr_field_t stcSMR;
3525  stc_mfs_i2c_ibcr_field_t stcIBCR;
3526  stc_mfs_i2c_ibsr_field_t stcIBSR;
3527  stc_mfs_fcr1_field_t stcFCR1;
3528 
3529  stcIBCR = pstcI2c->IBCR_f;
3530  /* Slave is active */
3531  if ((TRUE == stcIBCR.ACT_SCC) && (FALSE == stcIBCR.MSS))
3532  {
3533  /* Set ACK */
3534  stcIBCR.ACKE = TRUE;
3535  stcIBCR.ACT_SCC = FALSE;
3536  pstcI2c->IBCR_f = stcIBCR;
3537 
3538  stcIBSR = pstcI2c->IBSR_f;
3539 
3540  /* Direction is TX */
3541  if (TRUE == stcIBSR.TRX)
3542  {
3543  /* FIFO is used */
3544  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
3545  {
3546  /* tx FIFO interrupt enable */
3547  stcFCR1 = pstcI2c->FCR1_f;
3548  stcFCR1.FTIE = TRUE;
3549  pstcI2c->FCR1_f = stcFCR1;
3550  }
3551  /* FIFO is not used */
3552  else
3553  {
3554  /* tx interrupt enable */
3555  stcSMR = pstcI2c->SMR_f;
3556  stcSMR.TIE = TRUE;
3557  pstcI2c->SMR_f = stcSMR;
3558  }
3559  }
3560  /* Direction is RX */
3561  else
3562  {
3563  /* rx interrupt enable */
3564  stcSMR = pstcI2c->SMR_f;
3565  stcSMR.RIE = TRUE;
3566  pstcI2c->SMR_f = stcSMR;
3567  }
3568  enResult = Ok;
3569  }
3570  else
3571  {
3572  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
3573  enResult = ErrorInvalidMode;
3574  }
3575 
3576  return (enResult);
3577 } /* MfsHlI2cStartSlave */
3578 
3591 static en_result_t MfsHlI2cWaitIntState(volatile stc_mfs_hl_intern_data_t* pstcMfsHlInternData)
3592 {
3593  en_result_t enResult = ErrorTimeout;
3594  volatile uint32_t u32Count;
3595  uint32_t u32MaxCnt;
3596 
3597  u32MaxCnt = Mfs_GetBusClock() / MFS_HL_NUM_10;
3598  u32Count = 0u;
3599  while (u32Count < u32MaxCnt)
3600  {
3601  /* Wait until tx or rx is completed */
3602  if (MfsHlExecStby == pstcMfsHlInternData->u8Exec)
3603  {
3604  enResult = Ok;
3605  break;
3606  }
3607  u32Count++;
3608  }
3609  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
3610 
3611  return (enResult);
3612 } /* MfsHlI2cWaitIntState */
3613 
3630  uint32_t u32MaxCnt)
3631 {
3632  en_result_t enResult;
3633 
3634  /* If tx or rx is completed, return OK */
3635  if (MfsHlExecStby == pstcMfsHlInternData->u8Exec)
3636  {
3637  enResult = Ok;
3638  }
3639  else
3640  {
3641  enResult = ErrorOperationInProgress;
3642  /* If tx or rx is proceeding, polling counter counts */
3643  pstcMfsHlInternData->u32I2cProcCnt++;
3644  if (u32MaxCnt <= pstcMfsHlInternData->u32I2cProcCnt)
3645  {
3646  enResult = ErrorTimeout;
3647  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
3648  }
3649  }
3650 
3651  return (enResult);
3652 }
3653 
3697  uint8_t u8SlaveAddr,
3698  uint8_t* pu8Data,
3699  uint16_t* pu16WriteCnt,
3700  boolean_t bBlocking
3701  )
3702 {
3703  en_result_t enResult;
3704  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
3705  stc_mfs_hl_buffer_t* pstcBuffer;
3706  stc_mfs_smr_field_t stcSMR;
3707  stc_mfs_i2c_ibcr_field_t stcIBCR;
3708  stc_mfs_fcr1_field_t stcFCR1;
3709 
3710  /* Check for valid pointers and get pointer to internal data struct ... */
3711  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcI2c);
3712  /* ... and check */
3713  if ((NULL == pstcMfsHlInternData)
3714  || (NULL == pu8Data)
3715  || (NULL == pu16WriteCnt)
3716  )
3717  {
3718  enResult = ErrorInvalidParameter;
3719  }
3720 
3721  /* Check the execution */
3722  else if (MfsHlExecStby != pstcMfsHlInternData->u8Exec)
3723  {
3724  enResult = ErrorOperationInProgress;
3725  }
3726 
3727  else
3728  {
3729  enResult = Ok;
3730  /* Check if nothing to do */
3731  if (0u != *pu16WriteCnt)
3732  {
3733  /* Get ptr to internal transmit Buffer */
3734  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
3735 
3736  /* Preset buffer */
3737  pstcBuffer->pu8Buffer = pu8Data;
3738  pstcBuffer->u16BufferSize = *pu16WriteCnt;
3739  pstcBuffer->u16OutIndex = 0u;
3740 
3741  /* Change state */
3742  pstcMfsHlInternData->u8Exec = MfsHlExecTransmitting;
3743 
3744  /* Master mode */
3745  if (MfsHlModeI2cMaster == pstcMfsHlInternData->u8MfsMode)
3746  {
3747  /* tx */
3748  /* write slave address, bit0 = 0 (tx) */
3749  u8SlaveAddr <<= MFS_HL_NUM_1;
3750  u8SlaveAddr |= (uint8_t)(MfsI2cWrite);
3751  pstcI2c->TDR = (uint16_t)(u8SlaveAddr);
3752 
3753  stcIBCR = pstcI2c->IBCR_f;
3754  /* Set master mode */
3755  stcIBCR.MSS = TRUE;
3756  /* Enable ACK */
3757  stcIBCR.ACKE = TRUE;
3758  /* Enable interrupt */
3759  stcIBCR.INTE = TRUE;
3760  stcIBCR.ACT_SCC = FALSE;
3761  /* wait select */
3762  stcIBCR.WSEL = FALSE;
3763  pstcI2c->IBCR_f = stcIBCR;
3764 
3765  /* FIFO is used */
3766  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
3767  {
3768  /* tx FIFO interrupt enable : interruption occur */
3769  stcFCR1 = pstcI2c->FCR1_f;
3770  stcFCR1.FTIE = TRUE;
3771  pstcI2c->FCR1_f = stcFCR1;
3772  }
3773  /* FIFO is not used */
3774  else
3775  {
3776  /* tx interrupt enable : interruption occur */
3777  stcSMR = pstcI2c->SMR_f;
3778  stcSMR.TIE = TRUE;
3779  pstcI2c->SMR_f = stcSMR;
3780  }
3781  enResult = Ok;
3782  }
3783  /* Slave mode */
3784  else
3785  {
3786  enResult = MfsHlI2cStartSlave(pstcI2c, pstcMfsHlInternData);
3787  }
3788 
3789  if (Ok == enResult)
3790  {
3791  /* Specified non-blocking call */
3792  if (FALSE == bBlocking)
3793  {
3794  /* Polling counter for check status is clear */
3795  pstcMfsHlInternData->u32I2cProcCnt = 0;
3796  }
3797  /* Specified blocking call */
3798  else
3799  {
3800  /* Wait until TX is completed or error occur */
3801  enResult = MfsHlI2cWaitIntState(pstcMfsHlInternData);
3802 
3803  if (0u == pstcBuffer->u16OutIndex)
3804  {
3805  enResult = ErrorTimeout;
3806  }
3807  *pu16WriteCnt = pstcBuffer->u16OutIndex;
3808  }
3809  }
3810  }
3811  }
3812 
3813  return (enResult);
3814 } /* Mfs_Hl_I2c_Write */
3815 
3857  uint8_t u8SlaveAddr,
3858  uint8_t* pu8Data,
3859  uint16_t* pu16ReadCnt,
3860  boolean_t bBlocking
3861  )
3862 {
3863  en_result_t enResult;
3864  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
3865  stc_mfs_hl_buffer_t* pstcBuffer;
3866  stc_mfs_i2c_ibcr_field_t stcIBCR;
3867  uint16_t u16Count;
3868  uint16_t u16Cnt;
3869 
3870  /* Check for valid pointers, and get pointer to internal data struct ... */
3871  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcI2c);
3872  /* ... and check */
3873  if ((NULL == pstcMfsHlInternData)
3874  || (NULL == pu8Data)
3875  || (NULL == pu16ReadCnt)
3876  )
3877  {
3878  enResult = ErrorInvalidParameter;
3879  }
3880 
3881  /* Check the execution */
3882  else if (MfsHlExecStby != pstcMfsHlInternData->u8Exec)
3883  {
3884  enResult = ErrorOperationInProgress;
3885  }
3886 
3887  else
3888  {
3889  /* Get ptr to internal reception Buffer */
3890  pstcBuffer = &pstcMfsHlInternData->stcRxBuffer;
3891 
3892  pstcBuffer->pu8Buffer = pu8Data;
3893  pstcBuffer->u16BufferSize = *pu16ReadCnt;
3894  pstcBuffer->u16InIndex = 0u;
3895 
3896  /* Change state */
3897  pstcMfsHlInternData->u8Exec = MfsHlExecReceiving;
3898 
3899  /* Master mode */
3900  if (MfsHlModeI2cMaster == pstcMfsHlInternData->u8MfsMode)
3901  {
3902  /* FIFO is used */
3903  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
3904  {
3905  /* -1 is for SlaveAddr */
3906  u16Count = MfsHlGetMin((pstcBuffer->u16BufferSize),
3908  );
3909  /* Set threshold value for RX FIFO */
3910  pstcI2c->FBYTE2 = (uint8_t)u16Count;
3911  }
3912  /* FIFO is not used */
3913  else
3914  {
3915  u16Count = 0u;
3916  }
3917  /* rx */
3918  /* write slave address, bit0 = 1 (rx) */
3919  u8SlaveAddr <<= MFS_HL_NUM_1;
3920  u8SlaveAddr |= (uint8_t)(MfsI2cRead);
3921  pstcI2c->TDR = (uint16_t)(u8SlaveAddr);
3922 
3923  /* Write dummy data (0x00) for FIFO */
3924  for (u16Cnt = 0u; u16Cnt < u16Count; u16Cnt++)
3925  {
3926  pstcI2c->TDR = 0x00u;
3927  }
3928 
3929  stcIBCR = pstcI2c->IBCR_f;
3930  /* Enable ACK */
3931  stcIBCR.ACKE = TRUE;
3932  /* wait select */
3933  stcIBCR.WSEL = TRUE;
3934  /* interrup enable */
3935  stcIBCR.INTE = TRUE;
3936  /* clear interrupt */
3937  stcIBCR.ACT_SCC = FALSE;
3938  stcIBCR.INT = FALSE;
3939  /* Set master mode */
3940  stcIBCR.MSS = TRUE;
3941  pstcI2c->IBCR_f = stcIBCR;
3942 
3943  enResult = Ok;
3944  }
3945  /* Slave mode */
3946  else
3947  {
3948  enResult = MfsHlI2cStartSlave(pstcI2c, pstcMfsHlInternData);
3949  }
3950 
3951  if (Ok == enResult)
3952  {
3953  /* Specified non-blocking call */
3954  if (FALSE == bBlocking)
3955  {
3956  /* Polling counter for check status is clear */
3957  pstcMfsHlInternData->u32I2cProcCnt = 0;
3958  }
3959  /* Specified blocking call */
3960  else
3961  {
3962  /* Wait until TX is completed or error occur */
3963  enResult = MfsHlI2cWaitIntState(pstcMfsHlInternData);
3964 
3965  if ((Ok != enResult)
3966  || (0u == pstcBuffer->u16InIndex))
3967  {
3968  *pu16ReadCnt = 0u;
3969  enResult = ErrorTimeout;
3970  }
3971  else
3972  {
3973  *pu16ReadCnt = pstcBuffer->u16InIndex;
3974  }
3975  }
3976  }
3977  }
3978 
3979  return (enResult);
3980 } /* Mfs_Hl_I2c_Read */
3981 
4005  uint32_t u32MaxCnt
4006  )
4007 {
4008  en_result_t enResult;
4009  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
4010  stc_mfs_hl_buffer_t* pstcBuffer;
4011 
4012  /* Check for valid pointers, and get pointer to internal data struct ... */
4013  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcI2c);
4014  /* ... and check */
4015  if (NULL == pstcMfsHlInternData)
4016  {
4017  enResult = ErrorInvalidParameter;
4018  }
4019 
4020  /* Check the execution */
4021  else if ((MfsHlExecTransmitting != pstcMfsHlInternData->u8Exec)
4022  && (MfsHlExecStby != pstcMfsHlInternData->u8Exec))
4023  {
4024  enResult = ErrorInvalidMode;
4025  }
4026 
4027  else
4028  {
4029  /* Get ptr to internal transmit Buffer */
4030  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
4031 
4032  enResult = Ok;
4033  if (MfsHlExecTransmitting == pstcMfsHlInternData->u8Exec)
4034  {
4035  /* Check tx or rx is completed or error occur */
4036  enResult = MfsHlI2cChkTxRxComplete(pstcMfsHlInternData, u32MaxCnt);
4037  if (ErrorOperationInProgress != enResult)
4038  {
4039  if (0u == pstcBuffer->u16OutIndex)
4040  {
4041  enResult = ErrorTimeout;
4042  }
4043  }
4044  }
4045  }
4046 
4047  return (enResult);
4048 } /* Mfs_Hl_I2c_WaitTxComplete */
4049 
4073  uint32_t u32MaxCnt
4074  )
4075 {
4076  en_result_t enResult;
4077  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
4078  stc_mfs_hl_buffer_t* pstcBuffer;
4079 
4080  /* Check for valid pointers, and get pointer to internal data struct ... */
4081  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcI2c);
4082  /* ... and check */
4083  if (NULL == pstcMfsHlInternData)
4084  {
4085  enResult = ErrorInvalidParameter;
4086  }
4087 
4088  /* Check the execution */
4089  else if ((MfsHlExecReceiving != pstcMfsHlInternData->u8Exec)
4090  && (MfsHlExecStby != pstcMfsHlInternData->u8Exec))
4091  {
4092  enResult = ErrorInvalidMode;
4093  }
4094 
4095  else
4096  {
4097  enResult = Ok;
4098  if (MfsHlExecReceiving == pstcMfsHlInternData->u8Exec)
4099  {
4100  /* Check tx or rx is completed or error occur */
4101  enResult = MfsHlI2cChkTxRxComplete(pstcMfsHlInternData, u32MaxCnt);
4102  if (ErrorOperationInProgress != enResult)
4103  {
4104  /* Get ptr to internal reception Buffer */
4105  pstcBuffer = &pstcMfsHlInternData->stcRxBuffer;
4106 
4107  if ((Ok != enResult)
4108  || (0u == pstcBuffer->u16InIndex))
4109  {
4110  enResult = ErrorTimeout;
4111  }
4112  }
4113  }
4114  }
4115 
4116  return (enResult);
4117 } /* Mfs_Hl_I2c_WaitRxComplete */
4118 
4173  uint8_t* pu8Data,
4174  uint16_t u16WriteCnt,
4175  boolean_t bBlocking,
4176  boolean_t bCsHolding
4177  )
4178 {
4179  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
4180  stc_mfs_hl_buffer_t* pstcBuffer;
4181  uint_fast16_t fu16DataSent;
4182  uint_fast16_t fu16Idx;
4183  boolean_t bTxInProgress;
4184 
4185  /* Check for valid pointers and get pointer to internal data struct ... */
4186  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcMfs);
4187  /* ... and check */
4188  if ((NULL == pstcMfsHlInternData)
4189  || (NULL == pu8Data)
4190  )
4191  {
4192  return (ErrorInvalidParameter);
4193  }
4194 
4195  /* Check if nothing to do */
4196  if (0u == u16WriteCnt)
4197  {
4198  return (Ok);
4199  }
4200 
4201  /* Check the execution */
4202  if (MfsHlExecStby != pstcMfsHlInternData->u8Exec)
4203  {
4204  return (ErrorOperationInProgress);
4205  }
4206 
4207  /* Get ptr to internal transmit Buffer */
4208  pstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
4209 
4210  /* Check if ring buffer can take all bytes (blocking only) */
4211  if ((FALSE == bBlocking) &&
4212  (u16WriteCnt > (pstcBuffer->u16BufferSize - pstcBuffer->u16FillCount))
4213  )
4214  {
4215  /* not enough space left if non-blocking mode is requested */
4216  return (ErrorBufferFull);
4217  }
4218 
4219  /* Change state */
4220  pstcMfsHlInternData->u8Exec = MfsHlExecTransmitting;
4221 
4222  /* Set total tx bytes (for CSIO timer mode) */
4223  pstcBuffer->u16TxCount = u16WriteCnt;
4224  /* Clear total tx bytes */
4225  pstcBuffer->u16TxCounter = 0u;
4226 
4227  /* CSIO timer mode */
4228  if (MfsHlModeCsioNormalMasterTimer == pstcMfsHlInternData->u8MfsMode)
4229  {
4230  /* Enable serial timer */
4231  pstcMfs->SCR_f.TXE = FALSE;
4232  pstcMfs->SACSR_f.TMRE = TRUE;
4233  pstcMfs->SCR_f.TXE = TRUE;
4234  }
4235  /* Loop until all data has been sent (blocking only) */
4236  /* If non-blocking mode is requested, it is guaranteed here that the */
4237  /* provided data will fit into the tx buffer */
4238  while (0u < u16WriteCnt)
4239  {
4240  /* Use interruption */
4241  if (NULL != pstcMfsHlInternData->pfnTransmitCbFunction)
4242  {
4243  /* Check for transmission ongoing */
4244  bTxInProgress = pstcMfs->SSR_f.TDRE;
4245 
4246  /* In case, a transmission is already pending */
4247  if (TRUE == bTxInProgress)
4248  {
4249  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
4250  {
4251  /* Disable transmission Fifo interrupt during copy */
4252  pstcMfs->FCR1_f.FTIE = FALSE;
4253  }
4254  else /* if (pstcMfsInternData->enFifoAvailable == MfsHasFifo) */
4255  {
4256  /* Disable transmission interrupt during copy */
4257  pstcMfs->SCR_f.TIE = FALSE;
4258  }
4259  }
4260  }
4261 
4262  /* Copy data to provided destinaton buffer and save # bytes been read */
4263  /* determine free size in TX buffer */
4264  fu16DataSent = (uint_fast16_t)MfsHlGetMin(((pstcBuffer->u16BufferSize) - (pstcBuffer->u16FillCount)),
4265  u16WriteCnt
4266  );
4267 
4268  if (MfsHlModeCsioSpiMasterWithCs == pstcMfsHlInternData->u8MfsMode)
4269  {
4270  if (MFS_CSIO_TBYTE_MAX < fu16DataSent)
4271  {
4272  fu16DataSent = MFS_CSIO_TBYTE_MAX;
4273  }
4274  }
4275 
4276  /* store bytes in TX buffer */
4277  for (fu16Idx = 0u; fu16Idx < fu16DataSent; fu16Idx++)
4278  {
4279  pstcBuffer->pu8Buffer[pstcBuffer->u16InIndex] = pu8Data[fu16Idx];
4280 
4281  /* Update in index */
4282  pstcBuffer->u16InIndex++;
4283  if (pstcBuffer->u16InIndex == pstcBuffer->u16BufferSize)
4284  {
4285  pstcBuffer->u16InIndex = 0u; /* wrapped around */
4286  }
4287  }
4288 
4289  pstcBuffer->u16FillCount += (uint16_t)fu16DataSent;
4290  u16WriteCnt -= (uint16_t)fu16DataSent;
4291 
4292  /* Set tx bytes when chip select is used on master mode */
4293  if (MfsHlModeCsioSpiMasterWithCs == pstcMfsHlInternData->u8MfsMode)
4294  {
4295  pstcMfs->SCR_f.TXE = FALSE;
4296  /* Set size to active chip select */
4297  pstcMfs->TBYTE0 = (uint8_t)fu16DataSent;
4298  if ((0u != u16WriteCnt) || (TRUE == bCsHolding))
4299  {
4300  /* Hold chip select */
4301  pstcMfs->SCSCR_f.SCAM = TRUE;
4302  }
4303  else
4304  {
4305  /* In-active chip select when transmit is end */
4306  pstcMfs->SCSCR_f.SCAM = FALSE;
4307  }
4308  pstcMfs->SCR_f.TXE = TRUE;
4309  }
4310  /* Use interruption */
4311  if (NULL != pstcMfsHlInternData->pfnTransmitCbFunction)
4312  {
4313  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
4314  {
4315  /* Now enable transmission Fifo interrupt to trigger send operation */
4316  pstcMfs->FCR1_f.FTIE = TRUE;
4317  }
4318  else /* if (MfsHlUseNoFifo == pstcMfsHlInternData->u8FifoUsage) */
4319  {
4320  /* Now enable transmission interrupt to trigger send operation */
4321  pstcMfs->SCR_f.TIE = TRUE;
4322  }
4323  }
4324  /* No use interruption */
4325  else
4326  {
4327  /* Wait until all data has been tranferred to the MFS HW FIFO */
4328  while (0u != pstcBuffer->u16FillCount)
4329  {
4330  /* If Transmit Data Register (TDR) is empty */
4331  if (TRUE == pstcMfs->SSR_f.TDRE)
4332  {
4333  /* Write the contents of the buffer to Transmit Data Register */
4334  MfsHlWriteBuf(pstcMfs, pstcMfsHlInternData);
4335  }
4336  }
4337  if (0u == u16WriteCnt)
4338  {
4339  /* CSIO timer mode */
4340  if (MfsHlModeCsioNormalMasterTimer == pstcMfsHlInternData->u8MfsMode)
4341  {
4342  /* Wait for TX bus idle */
4343  while (FALSE == pstcMfs->SSR_f.TBI)
4344  {
4345  /* There is no process that should be executed. */
4346  ;
4347  }
4348  /* Disable serial timer */
4349  pstcMfs->SCR_f.TXE = FALSE;
4350  pstcMfs->SACSR_f.TMRE = FALSE;
4351  pstcMfs->SCR_f.TXE = TRUE;
4352  }
4353  pstcMfsHlInternData->u8Exec = MfsHlExecStby;
4354  }
4355  }
4356  } /* while (u16WriteCnt) */
4357 
4358  /* Wait until all data has been tranferred to the MFS HW FIFO (when blocking) */
4359  if (TRUE == bBlocking)
4360  {
4361  while (MfsHlExecStby != pstcMfsHlInternData->u8Exec)
4362  {
4364  }
4365  }
4366 
4367  return (Ok);
4368 } /* Mfs_Hl_Write */
4369 
4420  uint8_t* pu8Data,
4421  uint16_t* pu16DataCnt,
4422  uint16_t u16ReadCnt,
4423  boolean_t bBlocking
4424  )
4425 {
4426  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
4427  stc_mfs_hl_buffer_t* pstcBuffer;
4428  uint_fast16_t fu16Idx;
4429  uint_fast16_t fu16Length;
4430  uint16_t u16BytesToReadLeft;
4431  volatile uint8_t u8Ssr;
4432  volatile uint8_t u8SsrErr;
4433 
4434  if (NULL != pu16DataCnt)
4435  {
4436  *pu16DataCnt = 0u; /* Preset to default */
4437  }
4438 
4439  /* Check for valid pointers and get pointer to internal data struct ... */
4440  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcMfs);
4441  /* ... and check */
4442  if ((NULL == pstcMfsHlInternData)
4443  || (NULL == pu8Data)
4444  || (NULL == pu16DataCnt)
4445  )
4446  {
4447  return (ErrorInvalidParameter);
4448  }
4449 
4450  /* Check the execution */
4451  if (MfsHlExecStby != pstcMfsHlInternData->u8Exec)
4452  {
4453  return (ErrorOperationInProgress);
4454  }
4455 
4456  /* Save Read Count for later use */
4457  u16BytesToReadLeft = u16ReadCnt;
4458 
4459  /* Check for nothing to do */
4460  if (0u == u16ReadCnt)
4461  {
4462  return (Ok);
4463  }
4464 
4465  /* Get ptr to internal receive Buffer */
4466  pstcBuffer = &pstcMfsHlInternData->stcRxBuffer;
4467 
4468  /* Adjust FIFO, if u16ReadCnt less than FIFO depth */
4469  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
4470  {
4471  if (MFS_FIFO_MAX_VAL > u16ReadCnt)
4472  {
4473  pstcMfs->FBYTE2 = (uint8_t)u16ReadCnt;
4474  }
4475  else
4476  {
4477  pstcMfs->FBYTE2 = MFS_FIFO_MAX_VAL;
4478  }
4479  }
4480 
4481  /* Use interruption */
4482  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
4483  {
4484  if (FALSE == pstcMfs->SCR_f.RIE)
4485  {
4486  /* Enable reception interrupt */
4487  pstcMfs->SCR_f.RIE = TRUE;
4488  }
4489  }
4490 
4491  /* Read all available bytes from ring buffer, blocking. */
4492  while (0u < u16BytesToReadLeft)
4493  {
4494  /* Blocking read */
4495  if (TRUE == bBlocking)
4496  {
4497  while (0u == pstcBuffer->u16FillCount)
4498  {
4499  /* No use interruption */
4500  if (NULL == pstcMfsHlInternData->pfnReceiveCbFunction)
4501  {
4502  /* Read status */
4503  u8Ssr = u8SsrErr = pstcMfs->SSR;
4504  /* Error is different each mode */
4505  switch (pstcMfsHlInternData->u8MfsMode & MFS_HL_MODE_GROUP_CHK)
4506  {
4507  /* UART */
4509  /* Check Overrun/Framing/Parity error */
4510  u8SsrErr &= MFS_UART_SSR_ERR;
4511  break;
4512  /* CSIO */
4514  /* Check Overrun error */
4515  u8SsrErr &= MFS_CSIO_SSR_ERR;
4516  break;
4517  /* LIN */
4518  case MFS_HL_MODE_GROUP_LIN:
4519  /* Check Overrun/Framing error */
4520  u8SsrErr &= MFS_LIN_SSR_ERR;
4521  break;
4522  default:
4523  u8SsrErr = 0u;
4524  break;
4525  }
4526  /* Error */
4527  if(0u != u8SsrErr)
4528  {
4529  /* Clear possible reception errors */
4530  pstcMfs->SSR_f.REC = TRUE;
4531 
4532  /* If error occurs when receiving data, return error. */
4533  return Error;
4534  }
4535  /* If received data is full... */
4536  if (0u != (u8Ssr & MFS_SSR_RDRF))
4537  {
4538  /* Store Received Data Register into buffer */
4539  MfsHlReadBuf(pstcMfs, pstcMfsHlInternData);
4540  }
4541  }
4542  else
4543  {
4544  /* Wait until at least one byte is available */
4546  }
4547  }
4548  }
4549  else
4550  {
4551  /* No use interruption */
4552  if (NULL == pstcMfsHlInternData->pfnReceiveCbFunction)
4553  {
4554  /* Read status */
4555  u8Ssr = u8SsrErr = pstcMfs->SSR;
4556  /* Error is different each mode */
4557  switch (pstcMfsHlInternData->u8MfsMode & MFS_HL_MODE_GROUP_CHK)
4558  {
4559  /* UART */
4561  /* Check Overrun/Framing/Parity error */
4562  u8SsrErr &= MFS_UART_SSR_ERR;
4563  break;
4564  /* CSIO */
4566  /* Check Overrun error */
4567  u8SsrErr &= MFS_CSIO_SSR_ERR;
4568  break;
4569  /* LIN */
4570  case MFS_HL_MODE_GROUP_LIN:
4571  /* Check Overrun/Framing error */
4572  u8SsrErr &= MFS_LIN_SSR_ERR;
4573  break;
4574  default:
4575  u8SsrErr = 0u;
4576  break;
4577  }
4578  /* Error */
4579  if(0u != u8SsrErr)
4580  {
4581  /* Clear possible reception errors */
4582  pstcMfs->SSR_f.REC = TRUE;
4583  /* If error occurs when receiving data, return error. */
4584  return Error;
4585  }
4586  /* If received data is full... */
4587  if (0u != (u8Ssr & MFS_SSR_RDRF))
4588  {
4589  /* Store Received Data Register into buffer */
4590  MfsHlReadBuf(pstcMfs, pstcMfsHlInternData);
4591  }
4592  }
4593  if (0u == pstcBuffer->u16FillCount)
4594  {
4595  return (Ok);
4596  }
4597  }
4598 
4599  /* Disable reception interrupt */
4600  pstcMfs->SCR_f.RIE = FALSE;
4601 
4602  /* Copy data to destinaton buffer and save no. of bytes been read */
4603  /* get number of bytes to read */
4604  fu16Length = (uint_fast16_t)MfsHlGetMin(pstcBuffer->u16FillCount, u16BytesToReadLeft);
4605 
4606  /* if there are any bytes left to read */
4607  if (0u != fu16Length)
4608  {
4609  /* read bytes out of RX buffer */
4610  for (fu16Idx = *pu16DataCnt; fu16Idx < (fu16Length + *pu16DataCnt); fu16Idx++)
4611  {
4612  pu8Data[fu16Idx] = pstcBuffer->pu8Buffer[pstcBuffer->u16OutIndex];
4613  /* Update out index */
4614  pstcBuffer->u16OutIndex++;
4615  if (pstcBuffer->u16OutIndex == pstcBuffer->u16BufferSize)
4616  {
4617  pstcBuffer->u16OutIndex = 0u;
4618  }
4619  }
4620  pstcBuffer->u16FillCount -= (uint16_t)fu16Length; /* Update fill counter */
4621  }
4622 
4623  *pu16DataCnt += (uint16_t)fu16Length; /* Provide no. of read to the caller */
4624  u16BytesToReadLeft -= (uint16_t)fu16Length; /* Some data processed */
4625 
4626  /* Adjust FIFO, if bytes to read left less than FIFO depth */
4627  if (MfsHlUseFifo == pstcMfsHlInternData->u8FifoUsage)
4628  {
4629  if (MFS_FIFO_MAX_VAL > u16BytesToReadLeft)
4630  {
4631  if (0u == u16BytesToReadLeft)
4632  {
4633  pstcMfs->FBYTE2 = MFS_RX_FIFO_MIN_VAL;
4634  }
4635  else
4636  {
4637  pstcMfs->FBYTE2 = (uint8_t)u16BytesToReadLeft;
4638  }
4639  }
4640  }
4641  /* Use interruption */
4642  if (NULL != pstcMfsHlInternData->pfnReceiveCbFunction)
4643  {
4644  /* Reenable reception interrupt */
4645  pstcMfs->SCR_f.RIE = TRUE;
4646  }
4647  }
4648 
4649  return (Ok);
4650 } /* Mfs_Hl_Read */
4651 
4652 
4672  en_mfs_hl_buffer_t enBufIndex,
4673  stc_mfs_hl_buffer_t** ppstcBuffer)
4674 {
4675  stc_mfs_hl_intern_data_t* pstcMfsHlInternData;
4676 
4677  /* Check for valid pointers and get pointer to internal data struct ... */
4678  pstcMfsHlInternData = MfsHlGetInternDataPtr(pstcMfs);
4679  /* ... and check */
4680  if (NULL == pstcMfsHlInternData)
4681  {
4682  return (ErrorInvalidParameter);
4683  }
4684 
4685  switch(enBufIndex)
4686  {
4687  case MfsHlRxBuffer:
4688  *ppstcBuffer = &pstcMfsHlInternData->stcRxBuffer;
4689  break;
4690  case MfsHlTxBuffer:
4691  *ppstcBuffer = &pstcMfsHlInternData->stcTxBuffer;
4692  break;
4693  default:
4694  return (ErrorInvalidParameter);
4695  }
4696 
4697  return Ok;
4698 }
4699 
4701 
4702 #endif /* #if (PDL_MFS_USE_HL == PDL_ON) */
4703 
4704 #endif /* #if (defined(PDL_PERIPHERAL_MFS_ACTIVE)) */
4705 /******************************************************************************/
4706 /* EOF (not truncated) */
4707 /******************************************************************************/