spi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /******************************************************************************
  2. * @brief providing APIs for configuring SPI module (SPI).
  3. *
  4. *******************************************************************************
  5. *
  6. * provide APIs for configuring SPI module (SPI).
  7. ******************************************************************************/
  8. #include "common.h"
  9. #include "spi.h"
  10. /******************************************************************************
  11. * Local variables
  12. ******************************************************************************/
  13. SPI_CallbackType SPI_Callback[MAX_SPI_NO] = {(SPI_CallbackType)NULL};
  14. /******************************************************************************
  15. * Local function prototypes
  16. ******************************************************************************/
  17. /******************************************************************************
  18. * Local functions
  19. *****************************************************************************/
  20. /******************************************************************************
  21. * Global functions
  22. ******************************************************************************/
  23. /******************************************************************************
  24. * define SPI APIs
  25. *
  26. *//*! @addtogroup spi_api_list
  27. * @{
  28. *******************************************************************************/
  29. /*****************************************************************************//*!
  30. *
  31. * @brief initialize SPI as per params.
  32. *
  33. * @param[in] pSPI point to SPI module type.
  34. * @param[in] pConfig point to configuration parameters.
  35. *
  36. * @return none.
  37. *
  38. * @ Pass/ Fail criteria: none.
  39. *****************************************************************************/
  40. void SPI_Init(SPI_Type *pSPI, SPI_ConfigType *pConfig)
  41. {
  42. #if defined(CPU_NV32M3)
  43. /* sanity check */
  44. ASSERT((pSPI == SPI0));
  45. SIM->SCGC |= SIM_SCGC_SPI0_MASK;
  46. #else
  47. /* sanity check */
  48. ASSERT((pSPI == SPI0) || (pSPI == SPI1));
  49. /* enable SPI clock gating on */
  50. if( pSPI == SPI0)
  51. {
  52. SIM->SCGC |= SIM_SCGC_SPI0_MASK;
  53. }
  54. else
  55. {
  56. SIM->SCGC |= SIM_SCGC_SPI1_MASK;
  57. }
  58. #endif
  59. /* configure other control bits */
  60. if( pConfig->sSettings.bIntEn)
  61. {
  62. SPI_IntEnable(pSPI);
  63. #if defined(CPU_NV32M3)
  64. NVIC_EnableIRQ(SPI0_IRQn);
  65. #else
  66. if( pSPI == SPI0 )
  67. {
  68. NVIC_EnableIRQ(SPI0_IRQn);
  69. }
  70. else
  71. {
  72. NVIC_EnableIRQ(SPI1_IRQn);
  73. }
  74. #endif
  75. }
  76. if( pConfig->sSettings.bTxIntEn)
  77. {
  78. SPI_TxIntEnable(pSPI);
  79. #if defined(CPU_NV32M3)
  80. NVIC_EnableIRQ(SPI0_IRQn);
  81. #else
  82. if( pSPI == SPI0 )
  83. {
  84. NVIC_EnableIRQ(SPI0_IRQn);
  85. }
  86. else
  87. {
  88. NVIC_EnableIRQ(SPI1_IRQn);
  89. }
  90. #endif
  91. }
  92. if( pConfig->sSettings.bMasterMode)
  93. {
  94. SPI_SetMasterMode(pSPI);
  95. }
  96. else
  97. {
  98. SPI_SetSlaveMode(pSPI);
  99. }
  100. if( pConfig->sSettings.bClkPolarityLow)
  101. {
  102. SPI_SetClockPol(pSPI,1);
  103. }
  104. if( pConfig->sSettings.bClkPhase1)
  105. {
  106. SPI_SetClockPhase(pSPI,1);
  107. }else
  108. {
  109. SPI_SetClockPhase(pSPI,0);
  110. }
  111. if( pConfig->sSettings.bShiftLSBFirst)
  112. {
  113. SPI_SetLSBFirst(pSPI);
  114. }
  115. if( pConfig->sSettings.bMatchIntEn)
  116. {
  117. SPI_MatchIntEnable(pSPI);
  118. }
  119. if( pConfig->sSettings.bModeFaultEn)
  120. {
  121. SPI_ModfEnable(pSPI);
  122. }
  123. if( pConfig->sSettings.bMasterAutoDriveSS)
  124. {
  125. /* set both SSOE and MODFEN bits when auto drive slave SS is enabled */
  126. SPI_SSOutputEnable(pSPI);
  127. SPI_ModfEnable(pSPI);
  128. }
  129. if( pConfig->sSettings.bPinAsOuput)
  130. {
  131. SPI_BidirPinEnable(pSPI);
  132. }
  133. if( pConfig->sSettings.bBidirectionModeEn)
  134. {
  135. SPI_BidirOutEnable(pSPI);
  136. }
  137. if( pConfig->sSettings.bStopInWaitMode)
  138. {
  139. SPI_ClockStopEnable(pSPI);
  140. }
  141. if(pConfig->sSettings.bMasterMode)
  142. {
  143. SPI_SetBaudRate(pSPI,pConfig->u32BusClkHz,pConfig->u32BitRate);
  144. }
  145. /* enable SPI module */
  146. if( pConfig->sSettings.bModuleEn)
  147. {
  148. SPI_Enable(pSPI);
  149. }
  150. }
  151. /*****************************************************************************//*!
  152. *
  153. * @brief SPI set band rate.
  154. *
  155. * @param[in] pSPI point to SPI module type.
  156. * @param[in] u32BusClock Bus clock.
  157. * @param[in] u32Bps set spi's baudrate.
  158. *
  159. * @return none.
  160. *
  161. * @ Pass/ Fail criteria: none.
  162. *****************************************************************************/
  163. void SPI_SetBaudRate(SPI_Type *pSPI,uint32_t u32BusClock,uint32_t u32Bps)
  164. {
  165. uint32_t u32BitRateDivisor;
  166. uint8_t u8Sppr;
  167. uint8_t u8Spr;
  168. uint8_t u8ReadFlag;
  169. u32BitRateDivisor = u32BusClock/u32Bps; /* calculate bit rate divisor */
  170. u8ReadFlag = 0;
  171. /* find best fit SPPR and SPR */
  172. for (u8Spr = 0; u8Spr <= 8; u8Spr++)
  173. {
  174. for(u8Sppr = 0; u8Sppr <= 7; u8Sppr++)
  175. {
  176. if((u32BitRateDivisor>>(u8Spr+1))<=(u8Sppr+1))
  177. {
  178. u8ReadFlag = 1;
  179. break;
  180. }
  181. }
  182. if(u8ReadFlag)
  183. {
  184. break;
  185. }
  186. }
  187. if(u8Sppr >=8)
  188. {
  189. u8Sppr = 7;
  190. }
  191. if(u8Spr >8)
  192. {
  193. u8Spr = 8;
  194. }
  195. /* set bit rate */
  196. pSPI->BR = SPI_BR_SPPR(u8Sppr) | SPI_BR_SPR(u8Spr);
  197. }
  198. /*****************************************************************************//*!
  199. *
  200. * @brief implement write data to SPI.
  201. *
  202. * @param[in] pSPI pointer to SPI module type.
  203. * @param[in] pWrBuff -- write data buffer pointer.
  204. * @param[in] uiLength -- read/write data length.
  205. * @param[out] pRdBuff -- read data buffer pointer.
  206. *
  207. * @return if <0, means error, 0: success.
  208. *
  209. * @ Pass/ Fail criteria: none.
  210. *****************************************************************************/
  211. ResultType SPI_TransferWait(SPI_Type *pSPI, SPI_WidthType* pRdBuff, SPI_WidthType *pWrBuff,uint32 uiLength)
  212. {
  213. ResultType err = SPI_ERR_SUCCESS;
  214. uint32_t i;
  215. if(!uiLength)
  216. {
  217. return (err);
  218. }
  219. for(i = 0; i < uiLength; i++)
  220. {
  221. while(!SPI_IsSPTEF(pSPI));
  222. SPI_WriteDataReg(pSPI,pWrBuff[i]);
  223. while(!SPI_IsSPRF(pSPI));
  224. pRdBuff[i] = SPI_ReadDataReg(pSPI);
  225. }
  226. return (err);
  227. }
  228. /*****************************************************************************//*!
  229. *
  230. * @brief Deinitialize SPI to the default state (reset value).
  231. *
  232. * @param[in] pSPI pointer to SPI module type.
  233. *
  234. * @return none.
  235. *
  236. * @ Pass/ Fail criteria: none.
  237. *****************************************************************************/
  238. void SPI_DeInit(SPI_Type *pSPI)
  239. {
  240. int16 i;
  241. pSPI->C1 = SPI_C1_DEFAULT;
  242. pSPI->C2 = SPI_C2_DEFAULT;
  243. pSPI->BR = SPI_BR_DEFAULT;
  244. pSPI->M = SPI_M_DEFAULT;
  245. for(i = 0; i<100; i++); /* wait for some cycles for the ISR exit */
  246. }
  247. /*****************************************************************************//*!
  248. *
  249. * @brief set up SPI callback routines to be called by interrupt service routine.
  250. *
  251. * @param[in] pSPI pointer to SPI module type.
  252. * @param[in] pfnCallback callback routine.
  253. *
  254. * @return none.
  255. *
  256. * @ Pass/ Fail criteria: none.
  257. *****************************************************************************/
  258. void SPI_SetCallback(SPI_Type *pSPI,SPI_CallbackType pfnCallback)
  259. {
  260. uint32_t u32Port = ((uint32_t)pSPI-(uint32_t)SPI0)>>12;
  261. ASSERT(u32Port <2);
  262. SPI_Callback[u32Port] = pfnCallback;
  263. }
  264. /*! @} End of spi_api_list */
  265. /*****************************************************************************//*!
  266. *
  267. * @brief SPI0 interrupt service routine.
  268. *
  269. * @param none.
  270. * @return none.
  271. *
  272. * @ Pass/ Fail criteria: none.
  273. *****************************************************************************/
  274. void SPI0_Isr(void)
  275. {
  276. if( SPI_Callback[0] )
  277. {
  278. SPI_Callback[0]();
  279. }
  280. }
  281. #ifndef CPU_NV32M3
  282. /*****************************************************************************//*!
  283. *
  284. * @brief SPI1 interrupt service routine.
  285. *
  286. * @param none.
  287. * @return none.
  288. *
  289. * @ Pass/ Fail criteria: none
  290. *****************************************************************************/
  291. void SPI1_Isr(void)
  292. {
  293. if( SPI_Callback[1] )
  294. {
  295. SPI_Callback[1]();
  296. }
  297. }
  298. #endif