mfxstm32l152.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /**
  2. ******************************************************************************
  3. * @file mfxstm32l152.c
  4. * @author MCD Application Team
  5. * @brief This file provides a set of functions needed to manage the MFXSTM32L152
  6. * IO Expander devices.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under BSD 3-Clause license,
  14. * the "License"; You may not use this file except in compliance with the
  15. * License. You may obtain a copy of the License at:
  16. * opensource.org/licenses/BSD-3-Clause
  17. *
  18. ******************************************************************************
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "mfxstm32l152.h"
  22. /** @addtogroup BSP
  23. * @{
  24. */
  25. /** @addtogroup Component
  26. * @{
  27. */
  28. /** @defgroup MFXSTM32L152
  29. * @{
  30. */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /** @defgroup MFXSTM32L152_Private_Types_Definitions
  33. * @{
  34. */
  35. /* Private define ------------------------------------------------------------*/
  36. /** @defgroup MFXSTM32L152_Private_Defines
  37. * @{
  38. */
  39. #define MFXSTM32L152_MAX_INSTANCE 3
  40. /* Private macro -------------------------------------------------------------*/
  41. /** @defgroup MFXSTM32L152_Private_Macros
  42. * @{
  43. */
  44. /* Private variables ---------------------------------------------------------*/
  45. /** @defgroup MFXSTM32L152_Private_Variables
  46. * @{
  47. */
  48. /* Touch screen driver structure initialization */
  49. TS_DrvTypeDef mfxstm32l152_ts_drv =
  50. {
  51. mfxstm32l152_Init,
  52. mfxstm32l152_ReadID,
  53. mfxstm32l152_Reset,
  54. mfxstm32l152_TS_Start,
  55. mfxstm32l152_TS_DetectTouch,
  56. mfxstm32l152_TS_GetXY,
  57. mfxstm32l152_TS_EnableIT,
  58. mfxstm32l152_TS_ClearIT,
  59. mfxstm32l152_TS_ITStatus,
  60. mfxstm32l152_TS_DisableIT,
  61. };
  62. /* IO driver structure initialization */
  63. IO_DrvTypeDef mfxstm32l152_io_drv =
  64. {
  65. mfxstm32l152_Init,
  66. mfxstm32l152_ReadID,
  67. mfxstm32l152_Reset,
  68. mfxstm32l152_IO_Start,
  69. mfxstm32l152_IO_Config,
  70. mfxstm32l152_IO_WritePin,
  71. mfxstm32l152_IO_ReadPin,
  72. mfxstm32l152_IO_EnableIT,
  73. mfxstm32l152_IO_DisableIT,
  74. mfxstm32l152_IO_ITStatus,
  75. mfxstm32l152_IO_ClearIT,
  76. };
  77. /* IDD driver structure initialization */
  78. IDD_DrvTypeDef mfxstm32l152_idd_drv =
  79. {
  80. mfxstm32l152_Init,
  81. mfxstm32l152_DeInit,
  82. mfxstm32l152_ReadID,
  83. mfxstm32l152_Reset,
  84. mfxstm32l152_LowPower,
  85. mfxstm32l152_WakeUp,
  86. mfxstm32l152_IDD_Start,
  87. mfxstm32l152_IDD_Config,
  88. mfxstm32l152_IDD_GetValue,
  89. mfxstm32l152_IDD_EnableIT,
  90. mfxstm32l152_IDD_ClearIT,
  91. mfxstm32l152_IDD_GetITStatus,
  92. mfxstm32l152_IDD_DisableIT,
  93. mfxstm32l152_Error_EnableIT,
  94. mfxstm32l152_Error_ClearIT,
  95. mfxstm32l152_Error_GetITStatus,
  96. mfxstm32l152_Error_DisableIT,
  97. mfxstm32l152_Error_ReadSrc,
  98. mfxstm32l152_Error_ReadMsg
  99. };
  100. /* mfxstm32l152 instances by address */
  101. uint8_t mfxstm32l152[MFXSTM32L152_MAX_INSTANCE] = {0};
  102. /**
  103. * @}
  104. */
  105. /* Private function prototypes -----------------------------------------------*/
  106. /** @defgroup MFXSTM32L152_Private_Function_Prototypes
  107. * @{
  108. */
  109. static uint8_t mfxstm32l152_GetInstance(uint16_t DeviceAddr);
  110. static uint8_t mfxstm32l152_ReleaseInstance(uint16_t DeviceAddr);
  111. static void mfxstm32l152_reg24_setPinValue(uint16_t DeviceAddr, uint8_t RegisterAddr, uint32_t PinPosition, uint8_t PinValue );
  112. /* Private functions ---------------------------------------------------------*/
  113. /** @defgroup MFXSTM32L152_Private_Functions
  114. * @{
  115. */
  116. /**
  117. * @brief Initialize the mfxstm32l152 and configure the needed hardware resources
  118. * @param DeviceAddr: Device address on communication Bus.
  119. * @retval None
  120. */
  121. void mfxstm32l152_Init(uint16_t DeviceAddr)
  122. {
  123. uint8_t instance;
  124. uint8_t empty;
  125. /* Check if device instance already exists */
  126. instance = mfxstm32l152_GetInstance(DeviceAddr);
  127. /* To prevent double initialization */
  128. if(instance == 0xFF)
  129. {
  130. /* Look for empty instance */
  131. empty = mfxstm32l152_GetInstance(0);
  132. if(empty < MFXSTM32L152_MAX_INSTANCE)
  133. {
  134. /* Register the current device instance */
  135. mfxstm32l152[empty] = DeviceAddr;
  136. /* Initialize IO BUS layer */
  137. MFX_IO_Init();
  138. }
  139. }
  140. mfxstm32l152_SetIrqOutPinPolarity(DeviceAddr, MFXSTM32L152_OUT_PIN_POLARITY_HIGH);
  141. mfxstm32l152_SetIrqOutPinType(DeviceAddr, MFXSTM32L152_OUT_PIN_TYPE_PUSHPULL);
  142. }
  143. /**
  144. * @brief DeInitialize the mfxstm32l152 and unconfigure the needed hardware resources
  145. * @param DeviceAddr: Device address on communication Bus.
  146. * @retval None
  147. */
  148. void mfxstm32l152_DeInit(uint16_t DeviceAddr)
  149. {
  150. uint8_t instance;
  151. /* release existing instance */
  152. instance = mfxstm32l152_ReleaseInstance(DeviceAddr);
  153. /* De-Init only if instance was previously registered */
  154. if(instance != 0xFF)
  155. {
  156. /* De-Initialize IO BUS layer */
  157. MFX_IO_DeInit();
  158. }
  159. }
  160. /**
  161. * @brief Reset the mfxstm32l152 by Software.
  162. * @param DeviceAddr: Device address on communication Bus.
  163. * @retval None
  164. */
  165. void mfxstm32l152_Reset(uint16_t DeviceAddr)
  166. {
  167. /* Soft Reset */
  168. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, MFXSTM32L152_SWRST);
  169. /* Wait for a delay to ensure registers erasing */
  170. MFX_IO_Delay(10);
  171. }
  172. /**
  173. * @brief Put mfxstm32l152 Device in Low Power standby mode
  174. * @param DeviceAddr: Device address on communication Bus.
  175. * @retval None
  176. */
  177. void mfxstm32l152_LowPower(uint16_t DeviceAddr)
  178. {
  179. /* Enter standby mode */
  180. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, MFXSTM32L152_STANDBY);
  181. /* enable wakeup pin */
  182. MFX_IO_EnableWakeupPin();
  183. }
  184. /**
  185. * @brief WakeUp mfxstm32l152 from standby mode
  186. * @param DeviceAddr: Device address on communication Bus.
  187. * @retval None
  188. */
  189. void mfxstm32l152_WakeUp(uint16_t DeviceAddr)
  190. {
  191. uint8_t instance;
  192. /* Check if device instance already exists */
  193. instance = mfxstm32l152_GetInstance(DeviceAddr);
  194. /* if instance does not exist, first initialize pins*/
  195. if(instance == 0xFF)
  196. {
  197. /* enable wakeup pin */
  198. MFX_IO_EnableWakeupPin();
  199. }
  200. /* toggle wakeup pin */
  201. MFX_IO_Wakeup();
  202. }
  203. /**
  204. * @brief Read the MFXSTM32L152 IO Expander device ID.
  205. * @param DeviceAddr: Device address on communication Bus.
  206. * @retval The Device ID (two bytes).
  207. */
  208. uint16_t mfxstm32l152_ReadID(uint16_t DeviceAddr)
  209. {
  210. uint8_t id;
  211. /* Wait for a delay to ensure the state of registers */
  212. MFX_IO_Delay(1);
  213. /* Initialize IO BUS layer */
  214. MFX_IO_Init();
  215. id = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_ID);
  216. /* Return the device ID value */
  217. return (id);
  218. }
  219. /**
  220. * @brief Read the MFXSTM32L152 device firmware version.
  221. * @param DeviceAddr: Device address on communication Bus.
  222. * @retval The Device FW version (two bytes).
  223. */
  224. uint16_t mfxstm32l152_ReadFwVersion(uint16_t DeviceAddr)
  225. {
  226. uint8_t data[2];
  227. MFX_IO_ReadMultiple((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_FW_VERSION_MSB, data, sizeof(data)) ;
  228. /* Recompose MFX firmware value */
  229. return ((data[0] << 8) | data[1]);
  230. }
  231. /**
  232. * @brief Enable the interrupt mode for the selected IT source
  233. * @param DeviceAddr: Device address on communication Bus.
  234. * @param Source: The interrupt source to be configured, could be:
  235. * @arg MFXSTM32L152_IRQ_GPIO: IO interrupt
  236. * @arg MFXSTM32L152_IRQ_IDD : IDD interrupt
  237. * @arg MFXSTM32L152_IRQ_ERROR : Error interrupt
  238. * @arg MFXSTM32L152_IRQ_TS_DET : Touch Screen Controller Touch Detected interrupt
  239. * @arg MFXSTM32L152_IRQ_TS_NE : Touch Screen FIFO Not Empty
  240. * @arg MFXSTM32L152_IRQ_TS_TH : Touch Screen FIFO threshold triggered
  241. * @arg MFXSTM32L152_IRQ_TS_FULL : Touch Screen FIFO Full
  242. * @arg MFXSTM32L152_IRQ_TS_OVF : Touch Screen FIFO Overflow
  243. * @retval None
  244. */
  245. void mfxstm32l152_EnableITSource(uint16_t DeviceAddr, uint8_t Source)
  246. {
  247. uint8_t tmp = 0;
  248. /* Get the current value of the INT_EN register */
  249. tmp = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_SRC_EN);
  250. /* Set the interrupts to be Enabled */
  251. tmp |= Source;
  252. /* Set the register */
  253. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_SRC_EN, tmp);
  254. }
  255. /**
  256. * @brief Disable the interrupt mode for the selected IT source
  257. * @param DeviceAddr: Device address on communication Bus.
  258. * @param Source: The interrupt source to be configured, could be:
  259. * @arg MFXSTM32L152_IRQ_GPIO: IO interrupt
  260. * @arg MFXSTM32L152_IRQ_IDD : IDD interrupt
  261. * @arg MFXSTM32L152_IRQ_ERROR : Error interrupt
  262. * @arg MFXSTM32L152_IRQ_TS_DET : Touch Screen Controller Touch Detected interrupt
  263. * @arg MFXSTM32L152_IRQ_TS_NE : Touch Screen FIFO Not Empty
  264. * @arg MFXSTM32L152_IRQ_TS_TH : Touch Screen FIFO threshold triggered
  265. * @arg MFXSTM32L152_IRQ_TS_FULL : Touch Screen FIFO Full
  266. * @arg MFXSTM32L152_IRQ_TS_OVF : Touch Screen FIFO Overflow
  267. * @retval None
  268. */
  269. void mfxstm32l152_DisableITSource(uint16_t DeviceAddr, uint8_t Source)
  270. {
  271. uint8_t tmp = 0;
  272. /* Get the current value of the INT_EN register */
  273. tmp = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_SRC_EN);
  274. /* Set the interrupts to be Enabled */
  275. tmp &= ~Source;
  276. /* Set the register */
  277. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_SRC_EN, tmp);
  278. }
  279. /**
  280. * @brief Returns the selected Global interrupt source pending bit value
  281. * @param DeviceAddr: Device address on communication Bus.
  282. * @param Source: the Global interrupt source to be checked, could be:
  283. * @arg MFXSTM32L152_IRQ_GPIO: IO interrupt
  284. * @arg MFXSTM32L152_IRQ_IDD : IDD interrupt
  285. * @arg MFXSTM32L152_IRQ_ERROR : Error interrupt
  286. * @arg MFXSTM32L152_IRQ_TS_DET : Touch Screen Controller Touch Detected interrupt
  287. * @arg MFXSTM32L152_IRQ_TS_NE : Touch Screen FIFO Not Empty
  288. * @arg MFXSTM32L152_IRQ_TS_TH : Touch Screen FIFO threshold triggered
  289. * @arg MFXSTM32L152_IRQ_TS_FULL : Touch Screen FIFO Full
  290. * @arg MFXSTM32L152_IRQ_TS_OVF : Touch Screen FIFO Overflow
  291. * @retval The value of the checked Global interrupt source status.
  292. */
  293. uint8_t mfxstm32l152_GlobalITStatus(uint16_t DeviceAddr, uint8_t Source)
  294. {
  295. /* Return the global IT source status (pending or not)*/
  296. return((MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_PENDING) & Source));
  297. }
  298. /**
  299. * @brief Clear the selected Global interrupt pending bit(s)
  300. * @param DeviceAddr: Device address on communication Bus.
  301. * @param Source: the Global interrupt source to be cleared, could be any combination
  302. * of the below values. The acknowledge signal for MFXSTM32L152_GPIOs configured in input
  303. * with interrupt is not on this register but in IRQ_GPI_ACK1, IRQ_GPI_ACK2 registers.
  304. * @arg MFXSTM32L152_IRQ_IDD : IDD interrupt
  305. * @arg MFXSTM32L152_IRQ_ERROR : Error interrupt
  306. * @arg MFXSTM32L152_IRQ_TS_DET : Touch Screen Controller Touch Detected interrupt
  307. * @arg MFXSTM32L152_IRQ_TS_NE : Touch Screen FIFO Not Empty
  308. * @arg MFXSTM32L152_IRQ_TS_TH : Touch Screen FIFO threshold triggered
  309. * @arg MFXSTM32L152_IRQ_TS_FULL : Touch Screen FIFO Full
  310. * @arg MFXSTM32L152_IRQ_TS_OVF : Touch Screen FIFO Overflow
  311. * /\/\ IMPORTANT NOTE /\/\ must not use MFXSTM32L152_IRQ_GPIO as argument, see IRQ_GPI_ACK1 and IRQ_GPI_ACK2 registers
  312. * @retval None
  313. */
  314. void mfxstm32l152_ClearGlobalIT(uint16_t DeviceAddr, uint8_t Source)
  315. {
  316. /* Write 1 to the bits that have to be cleared */
  317. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_ACK, Source);
  318. }
  319. /**
  320. * @brief Set the global interrupt Polarity of IRQ_OUT_PIN.
  321. * @param DeviceAddr: Device address on communication Bus.
  322. * @param Polarity: the IT mode polarity, could be one of the following values:
  323. * @arg MFXSTM32L152_OUT_PIN_POLARITY_LOW: Interrupt output line is active Low edge
  324. * @arg MFXSTM32L152_OUT_PIN_POLARITY_HIGH: Interrupt line output is active High edge
  325. * @retval None
  326. */
  327. void mfxstm32l152_SetIrqOutPinPolarity(uint16_t DeviceAddr, uint8_t Polarity)
  328. {
  329. uint8_t tmp = 0;
  330. /* Get the current register value */
  331. tmp = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_MFX_IRQ_OUT);
  332. /* Mask the polarity bits */
  333. tmp &= ~(uint8_t)0x02;
  334. /* Modify the Interrupt Output line configuration */
  335. tmp |= Polarity;
  336. /* Set the new register value */
  337. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_MFX_IRQ_OUT, tmp);
  338. /* Wait for 1 ms for MFX to change IRQ_out pin config, before activate it */
  339. MFX_IO_Delay(1);
  340. }
  341. /**
  342. * @brief Set the global interrupt Type of IRQ_OUT_PIN.
  343. * @param DeviceAddr: Device address on communication Bus.
  344. * @param Type: Interrupt line activity type, could be one of the following values:
  345. * @arg MFXSTM32L152_OUT_PIN_TYPE_OPENDRAIN: Open Drain output Interrupt line
  346. * @arg MFXSTM32L152_OUT_PIN_TYPE_PUSHPULL: Push Pull output Interrupt line
  347. * @retval None
  348. */
  349. void mfxstm32l152_SetIrqOutPinType(uint16_t DeviceAddr, uint8_t Type)
  350. {
  351. uint8_t tmp = 0;
  352. /* Get the current register value */
  353. tmp = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_MFX_IRQ_OUT);
  354. /* Mask the type bits */
  355. tmp &= ~(uint8_t)0x01;
  356. /* Modify the Interrupt Output line configuration */
  357. tmp |= Type;
  358. /* Set the new register value */
  359. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_MFX_IRQ_OUT, tmp);
  360. /* Wait for 1 ms for MFX to change IRQ_out pin config, before activate it */
  361. MFX_IO_Delay(1);
  362. }
  363. /* ------------------------------------------------------------------ */
  364. /* ----------------------- GPIO ------------------------------------- */
  365. /* ------------------------------------------------------------------ */
  366. /**
  367. * @brief Start the IO functionality used and enable the AF for selected IO pin(s).
  368. * @param DeviceAddr: Device address on communication Bus.
  369. * @param AF_en: 0 to disable, else enabled.
  370. * @retval None
  371. */
  372. void mfxstm32l152_IO_Start(uint16_t DeviceAddr, uint32_t IO_Pin)
  373. {
  374. uint8_t mode;
  375. /* Get the current register value */
  376. mode = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL);
  377. /* Set the IO Functionalities to be Enabled */
  378. mode |= MFXSTM32L152_GPIO_EN;
  379. /* Enable ALTERNATE functions */
  380. /* AGPIO[0..3] can be either IDD or GPIO */
  381. /* AGPIO[4..7] can be either TS or GPIO */
  382. /* if IDD or TS are enabled no matter the value this bit GPIO are not available for those pins */
  383. /* however the MFX will waste some cycles to to handle these potential GPIO (pooling, etc) */
  384. /* so if IDD and TS are both active it is better to let ALTERNATE off (0) */
  385. /* if however IDD or TS are not connected then set it on gives more GPIOs availability */
  386. /* remind that AGPIO are less efficient then normal GPIO (They use pooling rather then EXTI */
  387. if (IO_Pin > 0xFFFF)
  388. {
  389. mode |= MFXSTM32L152_ALTERNATE_GPIO_EN;
  390. }
  391. else
  392. {
  393. mode &= ~MFXSTM32L152_ALTERNATE_GPIO_EN;
  394. }
  395. /* Write the new register value */
  396. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, mode);
  397. /* Wait for 1 ms for MFX to change IRQ_out pin config, before activate it */
  398. MFX_IO_Delay(1);
  399. }
  400. /**
  401. * @brief Configures the IO pin(s) according to IO mode structure value.
  402. * @param DeviceAddr: Device address on communication Bus.
  403. * @param IO_Pin: The output pin to be set or reset. This parameter can be one
  404. * of the following values:
  405. * @arg MFXSTM32L152_GPIO_PIN_x: where x can be from 0 to 23.
  406. * @param IO_Mode: The IO pin mode to configure, could be one of the following values:
  407. * @arg IO_MODE_INPUT
  408. * @arg IO_MODE_OUTPUT
  409. * @arg IO_MODE_IT_RISING_EDGE
  410. * @arg IO_MODE_IT_FALLING_EDGE
  411. * @arg IO_MODE_IT_LOW_LEVEL
  412. * @arg IO_MODE_IT_HIGH_LEVEL
  413. * @arg IO_MODE_INPUT_PU,
  414. * @arg IO_MODE_INPUT_PD,
  415. * @arg IO_MODE_OUTPUT_OD_PU,
  416. * @arg IO_MODE_OUTPUT_OD_PD,
  417. * @arg IO_MODE_OUTPUT_PP_PU,
  418. * @arg IO_MODE_OUTPUT_PP_PD,
  419. * @arg IO_MODE_IT_RISING_EDGE_PU
  420. * @arg IO_MODE_IT_FALLING_EDGE_PU
  421. * @arg IO_MODE_IT_LOW_LEVEL_PU
  422. * @arg IO_MODE_IT_HIGH_LEVEL_PU
  423. * @arg IO_MODE_IT_RISING_EDGE_PD
  424. * @arg IO_MODE_IT_FALLING_EDGE_PD
  425. * @arg IO_MODE_IT_LOW_LEVEL_PD
  426. * @arg IO_MODE_IT_HIGH_LEVEL_PD
  427. * @retval None
  428. */
  429. uint8_t mfxstm32l152_IO_Config(uint16_t DeviceAddr, uint32_t IO_Pin, IO_ModeTypedef IO_Mode)
  430. {
  431. uint8_t error_code = 0;
  432. /* Configure IO pin according to selected IO mode */
  433. switch(IO_Mode)
  434. {
  435. case IO_MODE_OFF: /* Off or analog mode */
  436. case IO_MODE_ANALOG: /* Off or analog mode */
  437. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  438. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  439. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITHOUT_PULL_RESISTOR);
  440. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  441. break;
  442. case IO_MODE_INPUT: /* Input mode */
  443. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  444. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  445. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITHOUT_PULL_RESISTOR);
  446. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  447. break;
  448. case IO_MODE_INPUT_PU: /* Input mode */
  449. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  450. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  451. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  452. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  453. break;
  454. case IO_MODE_INPUT_PD: /* Input mode */
  455. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  456. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  457. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  458. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  459. break;
  460. case IO_MODE_OUTPUT: /* Output mode */
  461. case IO_MODE_OUTPUT_PP_PD: /* Output mode */
  462. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  463. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_OUT);
  464. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPO_PUSH_PULL);
  465. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  466. break;
  467. case IO_MODE_OUTPUT_PP_PU: /* Output mode */
  468. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  469. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_OUT);
  470. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPO_PUSH_PULL);
  471. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  472. break;
  473. case IO_MODE_OUTPUT_OD_PD: /* Output mode */
  474. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  475. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_OUT);
  476. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPO_OPEN_DRAIN);
  477. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  478. break;
  479. case IO_MODE_OUTPUT_OD_PU: /* Output mode */
  480. mfxstm32l152_IO_DisablePinIT(DeviceAddr, IO_Pin); /* first disable IT */
  481. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_OUT);
  482. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPO_OPEN_DRAIN);
  483. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  484. break;
  485. case IO_MODE_IT_RISING_EDGE: /* Interrupt rising edge mode */
  486. mfxstm32l152_IO_EnableIT(DeviceAddr);
  487. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  488. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITHOUT_PULL_RESISTOR);
  489. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  490. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_EDGE);
  491. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_HLRE);
  492. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  493. break;
  494. case IO_MODE_IT_RISING_EDGE_PU: /* Interrupt rising edge mode */
  495. mfxstm32l152_IO_EnableIT(DeviceAddr);
  496. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  497. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  498. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  499. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_EDGE);
  500. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_HLRE);
  501. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  502. break;
  503. case IO_MODE_IT_RISING_EDGE_PD: /* Interrupt rising edge mode */
  504. mfxstm32l152_IO_EnableIT(DeviceAddr);
  505. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  506. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  507. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  508. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_EDGE);
  509. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_HLRE);
  510. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  511. break;
  512. case IO_MODE_IT_FALLING_EDGE: /* Interrupt falling edge mode */
  513. mfxstm32l152_IO_EnableIT(DeviceAddr);
  514. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  515. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITHOUT_PULL_RESISTOR);
  516. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  517. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_EDGE);
  518. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_LLFE);
  519. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  520. break;
  521. case IO_MODE_IT_FALLING_EDGE_PU: /* Interrupt falling edge mode */
  522. mfxstm32l152_IO_EnableIT(DeviceAddr);
  523. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  524. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  525. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  526. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_EDGE);
  527. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_LLFE);
  528. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  529. break;
  530. case IO_MODE_IT_FALLING_EDGE_PD: /* Interrupt falling edge mode */
  531. mfxstm32l152_IO_EnableIT(DeviceAddr);
  532. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  533. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  534. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  535. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_EDGE);
  536. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_LLFE);
  537. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  538. break;
  539. case IO_MODE_IT_LOW_LEVEL: /* Low level interrupt mode */
  540. mfxstm32l152_IO_EnableIT(DeviceAddr);
  541. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  542. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITHOUT_PULL_RESISTOR);
  543. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  544. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_LEVEL);
  545. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_LLFE);
  546. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  547. break;
  548. case IO_MODE_IT_LOW_LEVEL_PU: /* Low level interrupt mode */
  549. mfxstm32l152_IO_EnableIT(DeviceAddr);
  550. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  551. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  552. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  553. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_LEVEL);
  554. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_LLFE);
  555. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  556. break;
  557. case IO_MODE_IT_LOW_LEVEL_PD: /* Low level interrupt mode */
  558. mfxstm32l152_IO_EnableIT(DeviceAddr);
  559. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  560. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  561. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  562. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_LEVEL);
  563. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_LLFE);
  564. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  565. break;
  566. case IO_MODE_IT_HIGH_LEVEL: /* High level interrupt mode */
  567. mfxstm32l152_IO_EnableIT(DeviceAddr);
  568. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  569. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITHOUT_PULL_RESISTOR);
  570. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  571. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_LEVEL);
  572. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_HLRE);
  573. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  574. break;
  575. case IO_MODE_IT_HIGH_LEVEL_PU: /* High level interrupt mode */
  576. mfxstm32l152_IO_EnableIT(DeviceAddr);
  577. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  578. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  579. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_UP);
  580. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_LEVEL);
  581. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_HLRE);
  582. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  583. break;
  584. case IO_MODE_IT_HIGH_LEVEL_PD: /* High level interrupt mode */
  585. mfxstm32l152_IO_EnableIT(DeviceAddr);
  586. mfxstm32l152_IO_InitPin(DeviceAddr, IO_Pin, MFXSTM32L152_GPIO_DIR_IN);
  587. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_TYPE1, IO_Pin, MFXSTM32L152_GPI_WITH_PULL_RESISTOR);
  588. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_PUPD1, IO_Pin, MFXSTM32L152_GPIO_PULL_DOWN);
  589. mfxstm32l152_IO_SetIrqEvtMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_EVT_LEVEL);
  590. mfxstm32l152_IO_SetIrqTypeMode(DeviceAddr, IO_Pin, MFXSTM32L152_IRQ_GPI_TYPE_HLRE);
  591. mfxstm32l152_IO_EnablePinIT(DeviceAddr, IO_Pin); /* last to do: enable IT */
  592. break;
  593. default:
  594. error_code = (uint8_t) IO_Mode;
  595. break;
  596. }
  597. return error_code;
  598. }
  599. /**
  600. * @brief Initialize the selected IO pin direction.
  601. * @param DeviceAddr: Device address on communication Bus.
  602. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  603. * combination of the following values:
  604. * @arg MFXSTM32L152_GPIO_PIN_x: Where x can be from 0 to 23.
  605. * @param Direction: could be MFXSTM32L152_GPIO_DIR_IN or MFXSTM32L152_GPIO_DIR_OUT.
  606. * @retval None
  607. */
  608. void mfxstm32l152_IO_InitPin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Direction)
  609. {
  610. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_DIR1, IO_Pin, Direction);
  611. }
  612. /**
  613. * @brief Set the global interrupt Type.
  614. * @param DeviceAddr: Device address on communication Bus.
  615. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  616. * combination of the following values:
  617. * @arg MFXSTM32L152_GPIO_PIN_x: Where x can be from 0 to 23.
  618. * @param Evt: Interrupt line activity type, could be one of the following values:
  619. * @arg MFXSTM32L152_IRQ_GPI_EVT_LEVEL: Interrupt line is active in level model
  620. * @arg MFXSTM32L152_IRQ_GPI_EVT_EDGE: Interrupt line is active in edge model
  621. * @retval None
  622. */
  623. void mfxstm32l152_IO_SetIrqEvtMode(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Evt)
  624. {
  625. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_EVT1, IO_Pin, Evt);
  626. MFX_IO_Delay(1);
  627. }
  628. /**
  629. * @brief Configure the Edge for which a transition is detectable for the
  630. * selected pin.
  631. * @param DeviceAddr: Device address on communication Bus.
  632. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  633. * combination of the following values:
  634. * @arg MFXSTM32L152_GPIO_PIN_x: Where x can be from 0 to 23.
  635. * @param Evt: Interrupt line activity type, could be one of the following values:
  636. * @arg MFXSTM32L152_IRQ_GPI_TYPE_LLFE: Interrupt line is active in Low Level or Falling Edge
  637. * @arg MFXSTM32L152_IRQ_GPI_TYPE_HLRE: Interrupt line is active in High Level or Rising Edge
  638. * @retval None
  639. */
  640. void mfxstm32l152_IO_SetIrqTypeMode(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Type)
  641. {
  642. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_TYPE1, IO_Pin, Type);
  643. MFX_IO_Delay(1);
  644. }
  645. /**
  646. * @brief When GPIO is in output mode, puts the corresponding GPO in High (1) or Low (0) level.
  647. * @param DeviceAddr: Device address on communication Bus.
  648. * @param IO_Pin: The output pin to be set or reset. This parameter can be one
  649. * of the following values:
  650. * @arg MFXSTM32L152_GPIO_PIN_x: where x can be from 0 to 23.
  651. * @param PinState: The new IO pin state.
  652. * @retval None
  653. */
  654. void mfxstm32l152_IO_WritePin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t PinState)
  655. {
  656. /* Apply the bit value to the selected pin */
  657. if (PinState != 0)
  658. {
  659. /* Set the SET register */
  660. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPO_SET1, IO_Pin, 1);
  661. }
  662. else
  663. {
  664. /* Set the CLEAR register */
  665. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_GPO_CLR1, IO_Pin, 1);
  666. }
  667. }
  668. /**
  669. * @brief Return the state of the selected IO pin(s).
  670. * @param DeviceAddr: Device address on communication Bus.
  671. * @param IO_Pin: The output pin to be set or reset. This parameter can be one
  672. * of the following values:
  673. * @arg MFXSTM32L152_GPIO_PIN_x: where x can be from 0 to 23.
  674. * @retval IO pin(s) state.
  675. */
  676. uint32_t mfxstm32l152_IO_ReadPin(uint16_t DeviceAddr, uint32_t IO_Pin)
  677. {
  678. uint32_t tmp1 = 0;
  679. uint32_t tmp2 = 0;
  680. uint32_t tmp3 = 0;
  681. if(IO_Pin & 0x000000FF)
  682. {
  683. tmp1 = (uint32_t) MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_STATE1);
  684. }
  685. if(IO_Pin & 0x0000FF00)
  686. {
  687. tmp2 = (uint32_t) MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_STATE2);
  688. }
  689. if(IO_Pin & 0x00FF0000)
  690. {
  691. tmp3 = (uint32_t) MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_GPIO_STATE3);
  692. }
  693. tmp3 = tmp1 + (tmp2 << 8) + (tmp3 << 16);
  694. return(tmp3 & IO_Pin);
  695. }
  696. /**
  697. * @brief Enable the global IO interrupt source.
  698. * @param DeviceAddr: Device address on communication Bus.
  699. * @retval None
  700. */
  701. void mfxstm32l152_IO_EnableIT(uint16_t DeviceAddr)
  702. {
  703. MFX_IO_ITConfig();
  704. /* Enable global IO IT source */
  705. mfxstm32l152_EnableITSource(DeviceAddr, MFXSTM32L152_IRQ_GPIO);
  706. }
  707. /**
  708. * @brief Disable the global IO interrupt source.
  709. * @param DeviceAddr: Device address on communication Bus.
  710. * @retval None
  711. */
  712. void mfxstm32l152_IO_DisableIT(uint16_t DeviceAddr)
  713. {
  714. /* Disable global IO IT source */
  715. mfxstm32l152_DisableITSource(DeviceAddr, MFXSTM32L152_IRQ_GPIO);
  716. }
  717. /**
  718. * @brief Enable interrupt mode for the selected IO pin(s).
  719. * @param DeviceAddr: Device address on communication Bus.
  720. * @param IO_Pin: The IO interrupt to be enabled. This parameter could be any
  721. * combination of the following values:
  722. * @arg MFXSTM32L152_GPIO_PIN_x: where x can be from 0 to 23.
  723. * @retval None
  724. */
  725. void mfxstm32l152_IO_EnablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
  726. {
  727. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_SRC1, IO_Pin, 1);
  728. }
  729. /**
  730. * @brief Disable interrupt mode for the selected IO pin(s).
  731. * @param DeviceAddr: Device address on communication Bus.
  732. * @param IO_Pin: The IO interrupt to be disabled. This parameter could be any
  733. * combination of the following values:
  734. * @arg MFXSTM32L152_GPIO_PIN_x: where x can be from 0 to 23.
  735. * @retval None
  736. */
  737. void mfxstm32l152_IO_DisablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
  738. {
  739. mfxstm32l152_reg24_setPinValue(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_SRC1, IO_Pin, 0);
  740. }
  741. /**
  742. * @brief Check the status of the selected IO interrupt pending bit
  743. * @param DeviceAddr: Device address on communication Bus.
  744. * @param IO_Pin: The IO interrupt to be checked could be:
  745. * @arg MFXSTM32L152_GPIO_PIN_x Where x can be from 0 to 23.
  746. * @retval Status of the checked IO pin(s).
  747. */
  748. uint32_t mfxstm32l152_IO_ITStatus(uint16_t DeviceAddr, uint32_t IO_Pin)
  749. {
  750. /* Get the Interrupt status */
  751. uint8_t tmp1 = 0;
  752. uint16_t tmp2 = 0;
  753. uint32_t tmp3 = 0;
  754. if(IO_Pin & 0xFF)
  755. {
  756. tmp1 = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_PENDING1);
  757. }
  758. if(IO_Pin & 0xFFFF00)
  759. {
  760. tmp2 = (uint16_t) MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_PENDING2);
  761. }
  762. if(IO_Pin & 0xFFFF0000)
  763. {
  764. tmp3 = (uint32_t) MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_PENDING3);
  765. }
  766. tmp3 = tmp1 + (tmp2 << 8) + (tmp3 << 16);
  767. return(tmp3 & IO_Pin);
  768. }
  769. /**
  770. * @brief Clear the selected IO interrupt pending bit(s). It clear automatically also the general MFXSTM32L152_REG_ADR_IRQ_PENDING
  771. * @param DeviceAddr: Device address on communication Bus.
  772. * @param IO_Pin: the IO interrupt to be cleared, could be:
  773. * @arg MFXSTM32L152_GPIO_PIN_x: Where x can be from 0 to 23.
  774. * @retval None
  775. */
  776. void mfxstm32l152_IO_ClearIT(uint16_t DeviceAddr, uint32_t IO_Pin)
  777. {
  778. /* Clear the IO IT pending bit(s) by acknowledging */
  779. /* it cleans automatically also the Global IRQ_GPIO */
  780. /* normally this function is called under interrupt */
  781. uint8_t pin_0_7, pin_8_15, pin_16_23;
  782. pin_0_7 = IO_Pin & 0x0000ff;
  783. pin_8_15 = IO_Pin >> 8;
  784. pin_8_15 = pin_8_15 & 0x00ff;
  785. pin_16_23 = IO_Pin >> 16;
  786. if (pin_0_7)
  787. {
  788. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_ACK1, pin_0_7);
  789. }
  790. if (pin_8_15)
  791. {
  792. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_ACK2, pin_8_15);
  793. }
  794. if (pin_16_23)
  795. {
  796. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_IRQ_GPI_ACK3, pin_16_23);
  797. }
  798. }
  799. /**
  800. * @brief Enable the AF for aGPIO.
  801. * @param DeviceAddr: Device address on communication Bus.
  802. * @retval None
  803. */
  804. void mfxstm32l152_IO_EnableAF(uint16_t DeviceAddr)
  805. {
  806. uint8_t mode;
  807. /* Get the current register value */
  808. mode = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL);
  809. /* Enable ALTERNATE functions */
  810. /* AGPIO[0..3] can be either IDD or GPIO */
  811. /* AGPIO[4..7] can be either TS or GPIO */
  812. /* if IDD or TS are enabled no matter the value this bit GPIO are not available for those pins */
  813. /* however the MFX will waste some cycles to to handle these potential GPIO (pooling, etc) */
  814. /* so if IDD and TS are both active it is better to let ALTERNATE disabled (0) */
  815. /* if however IDD or TS are not connected then set it on gives more GPIOs availability */
  816. /* remind that AGPIO are less efficient then normal GPIO (they use pooling rather then EXTI) */
  817. mode |= MFXSTM32L152_ALTERNATE_GPIO_EN;
  818. /* Write the new register value */
  819. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, mode);
  820. }
  821. /**
  822. * @brief Disable the AF for aGPIO.
  823. * @param DeviceAddr: Device address on communication Bus.
  824. * @retval None
  825. */
  826. void mfxstm32l152_IO_DisableAF(uint16_t DeviceAddr)
  827. {
  828. uint8_t mode;
  829. /* Get the current register value */
  830. mode = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL);
  831. /* Enable ALTERNATE functions */
  832. /* AGPIO[0..3] can be either IDD or GPIO */
  833. /* AGPIO[4..7] can be either TS or GPIO */
  834. /* if IDD or TS are enabled no matter the value this bit GPIO are not available for those pins */
  835. /* however the MFX will waste some cycles to to handle these potential GPIO (pooling, etc) */
  836. /* so if IDD and TS are both active it is better to let ALTERNATE disabled (0) */
  837. /* if however IDD or TS are not connected then set it on gives more GPIOs availability */
  838. /* remind that AGPIO are less efficient then normal GPIO (they use pooling rather then EXTI) */
  839. mode &= ~MFXSTM32L152_ALTERNATE_GPIO_EN;
  840. /* Write the new register value */
  841. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, mode);
  842. }
  843. /* ------------------------------------------------------------------ */
  844. /* --------------------- TOUCH SCREEN ------------------------------- */
  845. /* ------------------------------------------------------------------ */
  846. /**
  847. * @brief Configures the touch Screen Controller (Single point detection)
  848. * @param DeviceAddr: Device address on communication Bus.
  849. * @retval None.
  850. */
  851. void mfxstm32l152_TS_Start(uint16_t DeviceAddr)
  852. {
  853. uint8_t mode;
  854. /* Get the current register value */
  855. mode = MFX_IO_Read(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL);
  856. /* Set the Functionalities to be Enabled */
  857. mode |= MFXSTM32L152_TS_EN;
  858. /* Set the new register value */
  859. MFX_IO_Write(DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, mode);
  860. /* Wait for 2 ms */
  861. MFX_IO_Delay(2);
  862. /* Select 2 nF filter capacitor */
  863. /* Configuration:
  864. - Touch average control : 4 samples
  865. - Touch delay time : 500 uS
  866. - Panel driver setting time: 500 uS
  867. */
  868. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_SETTLING, 0x32);
  869. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_TOUCH_DET_DELAY, 0x5);
  870. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_AVE, 0x04);
  871. /* Configure the Touch FIFO threshold: single point reading */
  872. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_FIFO_TH, 0x01);
  873. /* Clear the FIFO memory content. */
  874. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_FIFO_TH, MFXSTM32L152_TS_CLEAR_FIFO);
  875. /* Touch screen control configuration :
  876. - No window tracking index
  877. */
  878. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_TRACK, 0x00);
  879. /* Clear all the IT status pending bits if any */
  880. mfxstm32l152_IO_ClearIT(DeviceAddr, 0xFFFFFF);
  881. /* Wait for 1 ms delay */
  882. MFX_IO_Delay(1);
  883. }
  884. /**
  885. * @brief Return if there is touch detected or not.
  886. * @param DeviceAddr: Device address on communication Bus.
  887. * @retval Touch detected state.
  888. */
  889. uint8_t mfxstm32l152_TS_DetectTouch(uint16_t DeviceAddr)
  890. {
  891. uint8_t state;
  892. uint8_t ret = 0;
  893. state = MFX_IO_Read(DeviceAddr, MFXSTM32L152_TS_FIFO_STA);
  894. state = ((state & (uint8_t)MFXSTM32L152_TS_CTRL_STATUS) == (uint8_t)MFXSTM32L152_TS_CTRL_STATUS);
  895. if(state > 0)
  896. {
  897. if(MFX_IO_Read(DeviceAddr, MFXSTM32L152_TS_FIFO_LEVEL) > 0)
  898. {
  899. ret = 1;
  900. }
  901. }
  902. return ret;
  903. }
  904. /**
  905. * @brief Get the touch screen X and Y positions values
  906. * @param DeviceAddr: Device address on communication Bus.
  907. * @param X: Pointer to X position value
  908. * @param Y: Pointer to Y position value
  909. * @retval None.
  910. */
  911. void mfxstm32l152_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
  912. {
  913. uint8_t data_xy[3];
  914. MFX_IO_ReadMultiple(DeviceAddr, MFXSTM32L152_TS_XY_DATA, data_xy, sizeof(data_xy)) ;
  915. /* Calculate positions values */
  916. *X = (data_xy[1]<<4) + (data_xy[0]>>4);
  917. *Y = (data_xy[2]<<4) + (data_xy[0]&4);
  918. /* Reset the FIFO memory content. */
  919. MFX_IO_Write(DeviceAddr, MFXSTM32L152_TS_FIFO_TH, MFXSTM32L152_TS_CLEAR_FIFO);
  920. }
  921. /**
  922. * @brief Configure the selected source to generate a global interrupt or not
  923. * @param DeviceAddr: Device address on communication Bus.
  924. * @retval None
  925. */
  926. void mfxstm32l152_TS_EnableIT(uint16_t DeviceAddr)
  927. {
  928. MFX_IO_ITConfig();
  929. /* Enable global TS IT source */
  930. mfxstm32l152_EnableITSource(DeviceAddr, MFXSTM32L152_IRQ_TS_DET);
  931. }
  932. /**
  933. * @brief Configure the selected source to generate a global interrupt or not
  934. * @param DeviceAddr: Device address on communication Bus.
  935. * @retval None
  936. */
  937. void mfxstm32l152_TS_DisableIT(uint16_t DeviceAddr)
  938. {
  939. /* Disable global TS IT source */
  940. mfxstm32l152_DisableITSource(DeviceAddr, MFXSTM32L152_IRQ_TS_DET);
  941. }
  942. /**
  943. * @brief Configure the selected source to generate a global interrupt or not
  944. * @param DeviceAddr: Device address on communication Bus.
  945. * @retval TS interrupts status
  946. */
  947. uint8_t mfxstm32l152_TS_ITStatus(uint16_t DeviceAddr)
  948. {
  949. /* Return TS interrupts status */
  950. return(mfxstm32l152_GlobalITStatus(DeviceAddr, MFXSTM32L152_IRQ_TS));
  951. }
  952. /**
  953. * @brief Configure the selected source to generate a global interrupt or not
  954. * @param DeviceAddr: Device address on communication Bus.
  955. * @retval None
  956. */
  957. void mfxstm32l152_TS_ClearIT(uint16_t DeviceAddr)
  958. {
  959. /* Clear the global TS IT source */
  960. mfxstm32l152_ClearGlobalIT(DeviceAddr, MFXSTM32L152_IRQ_TS);
  961. }
  962. /* ------------------------------------------------------------------ */
  963. /* --------------------- IDD MEASUREMENT ---------------------------- */
  964. /* ------------------------------------------------------------------ */
  965. /**
  966. * @brief Launch IDD current measurement
  967. * @param DeviceAddr: Device address on communication Bus
  968. * @retval None.
  969. */
  970. void mfxstm32l152_IDD_Start(uint16_t DeviceAddr)
  971. {
  972. uint8_t mode = 0;
  973. /* Get the current register value */
  974. mode = MFX_IO_Read((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_CTRL);
  975. /* Set the Functionalities to be enabled */
  976. mode |= MFXSTM32L152_IDD_CTRL_REQ;
  977. /* Start measurement campaign */
  978. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_CTRL, mode);
  979. }
  980. /**
  981. * @brief Configures the IDD current measurement
  982. * @param DeviceAddr: Device address on communication Bus.
  983. * @param MfxIddConfig: Parameters depending on hardware config.
  984. * @retval None
  985. */
  986. void mfxstm32l152_IDD_Config(uint16_t DeviceAddr, IDD_ConfigTypeDef MfxIddConfig)
  987. {
  988. uint8_t value = 0;
  989. uint8_t mode = 0;
  990. /* Get the current register value */
  991. mode = MFX_IO_Read((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL);
  992. if((mode & MFXSTM32L152_IDD_EN) != MFXSTM32L152_IDD_EN)
  993. {
  994. /* Set the Functionalities to be enabled */
  995. mode |= MFXSTM32L152_IDD_EN;
  996. /* Set the new register value */
  997. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_SYS_CTRL, mode);
  998. }
  999. /* Control register setting: number of shunts */
  1000. value = ((MfxIddConfig.ShuntNbUsed << 1) & MFXSTM32L152_IDD_CTRL_SHUNT_NB);
  1001. value |= (MfxIddConfig.VrefMeasurement & MFXSTM32L152_IDD_CTRL_VREF_DIS);
  1002. value |= (MfxIddConfig.Calibration & MFXSTM32L152_IDD_CTRL_CAL_DIS);
  1003. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_CTRL, value);
  1004. /* Idd pre delay configuration: unit and value*/
  1005. value = (MfxIddConfig.PreDelayUnit & MFXSTM32L152_IDD_PREDELAY_UNIT) |
  1006. (MfxIddConfig.PreDelayValue & MFXSTM32L152_IDD_PREDELAY_VALUE);
  1007. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_PRE_DELAY, value);
  1008. /* Shunt 0 register value: MSB then LSB */
  1009. value = (uint8_t) (MfxIddConfig.Shunt0Value >> 8);
  1010. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT0_MSB, value);
  1011. value = (uint8_t) (MfxIddConfig.Shunt0Value);
  1012. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT0_LSB, value);
  1013. /* Shunt 1 register value: MSB then LSB */
  1014. value = (uint8_t) (MfxIddConfig.Shunt1Value >> 8);
  1015. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT1_MSB, value);
  1016. value = (uint8_t) (MfxIddConfig.Shunt1Value);
  1017. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT1_LSB, value);
  1018. /* Shunt 2 register value: MSB then LSB */
  1019. value = (uint8_t) (MfxIddConfig.Shunt2Value >> 8);
  1020. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT2_MSB, value);
  1021. value = (uint8_t) (MfxIddConfig.Shunt2Value);
  1022. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT2_LSB, value);
  1023. /* Shunt 3 register value: MSB then LSB */
  1024. value = (uint8_t) (MfxIddConfig.Shunt3Value >> 8);
  1025. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT3_MSB, value);
  1026. value = (uint8_t) (MfxIddConfig.Shunt3Value);
  1027. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT3_LSB, value);
  1028. /* Shunt 4 register value: MSB then LSB */
  1029. value = (uint8_t) (MfxIddConfig.Shunt4Value >> 8);
  1030. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT4_MSB, value);
  1031. value = (uint8_t) (MfxIddConfig.Shunt4Value);
  1032. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT4_LSB, value);
  1033. /* Shunt 0 stabilization delay */
  1034. value = MfxIddConfig.Shunt0StabDelay;
  1035. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SH0_STABILIZATION, value);
  1036. /* Shunt 1 stabilization delay */
  1037. value = MfxIddConfig.Shunt1StabDelay;
  1038. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SH1_STABILIZATION, value);
  1039. /* Shunt 2 stabilization delay */
  1040. value = MfxIddConfig.Shunt2StabDelay;
  1041. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SH2_STABILIZATION, value);
  1042. /* Shunt 3 stabilization delay */
  1043. value = MfxIddConfig.Shunt3StabDelay;
  1044. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SH3_STABILIZATION, value);
  1045. /* Shunt 4 stabilization delay */
  1046. value = MfxIddConfig.Shunt4StabDelay;
  1047. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SH4_STABILIZATION, value);
  1048. /* Idd ampli gain value: MSB then LSB */
  1049. value = (uint8_t) (MfxIddConfig.AmpliGain >> 8);
  1050. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_GAIN_MSB, value);
  1051. value = (uint8_t) (MfxIddConfig.AmpliGain);
  1052. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_GAIN_LSB, value);
  1053. /* Idd VDD min value: MSB then LSB */
  1054. value = (uint8_t) (MfxIddConfig.VddMin >> 8);
  1055. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_VDD_MIN_MSB, value);
  1056. value = (uint8_t) (MfxIddConfig.VddMin);
  1057. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_VDD_MIN_LSB, value);
  1058. /* Idd number of measurements */
  1059. value = MfxIddConfig.MeasureNb;
  1060. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_NBR_OF_MEAS, value);
  1061. /* Idd delta delay configuration: unit and value */
  1062. value = (MfxIddConfig.DeltaDelayUnit & MFXSTM32L152_IDD_DELTADELAY_UNIT) |
  1063. (MfxIddConfig.DeltaDelayValue & MFXSTM32L152_IDD_DELTADELAY_VALUE);
  1064. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_MEAS_DELTA_DELAY, value);
  1065. /* Idd number of shut on board */
  1066. value = MfxIddConfig.ShuntNbOnBoard;
  1067. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNTS_ON_BOARD, value);
  1068. }
  1069. /**
  1070. * @brief This function allows to modify number of shunt used for a measurement
  1071. * @param DeviceAddr: Device address on communication Bus
  1072. * @retval None.
  1073. */
  1074. void mfxstm32l152_IDD_ConfigShuntNbLimit(uint16_t DeviceAddr, uint8_t ShuntNbLimit)
  1075. {
  1076. uint8_t mode = 0;
  1077. /* Get the current register value */
  1078. mode = MFX_IO_Read((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_CTRL);
  1079. /* Clear number of shunt limit */
  1080. mode &= ~(MFXSTM32L152_IDD_CTRL_SHUNT_NB);
  1081. /* Clear number of shunt limit */
  1082. mode |= ((ShuntNbLimit << 1) & MFXSTM32L152_IDD_CTRL_SHUNT_NB);
  1083. /* Write noewx desired limit */
  1084. MFX_IO_Write((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_CTRL, mode);
  1085. }
  1086. /**
  1087. * @brief Get Idd current value
  1088. * @param DeviceAddr: Device address on communication Bus
  1089. * @param ReadValue: Pointer on value to be read
  1090. * @retval Idd value in 10 nA.
  1091. */
  1092. void mfxstm32l152_IDD_GetValue(uint16_t DeviceAddr, uint32_t *ReadValue)
  1093. {
  1094. uint8_t data[3];
  1095. MFX_IO_ReadMultiple((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_VALUE_MSB, data, sizeof(data)) ;
  1096. /* Recompose Idd current value */
  1097. *ReadValue = (data[0] << 16) | (data[1] << 8) | data[2];
  1098. }
  1099. /**
  1100. * @brief Get Last shunt used for measurement
  1101. * @param DeviceAddr: Device address on communication Bus
  1102. * @retval Last shunt used
  1103. */
  1104. uint8_t mfxstm32l152_IDD_GetShuntUsed(uint16_t DeviceAddr)
  1105. {
  1106. return(MFX_IO_Read((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_IDD_SHUNT_USED));
  1107. }
  1108. /**
  1109. * @brief Configure mfx to enable Idd interrupt
  1110. * @param DeviceAddr: Device address on communication Bus.
  1111. * @retval None
  1112. */
  1113. void mfxstm32l152_IDD_EnableIT(uint16_t DeviceAddr)
  1114. {
  1115. MFX_IO_ITConfig();
  1116. /* Enable global IDD interrupt source */
  1117. mfxstm32l152_EnableITSource(DeviceAddr, MFXSTM32L152_IRQ_IDD);
  1118. }
  1119. /**
  1120. * @brief Clear Idd global interrupt
  1121. * @param DeviceAddr: Device address on communication Bus.
  1122. * @retval None
  1123. */
  1124. void mfxstm32l152_IDD_ClearIT(uint16_t DeviceAddr)
  1125. {
  1126. /* Clear the global IDD interrupt source */
  1127. mfxstm32l152_ClearGlobalIT(DeviceAddr, MFXSTM32L152_IRQ_IDD);
  1128. }
  1129. /**
  1130. * @brief get Idd interrupt status
  1131. * @param DeviceAddr: Device address on communication Bus.
  1132. * @retval IDD interrupts status
  1133. */
  1134. uint8_t mfxstm32l152_IDD_GetITStatus(uint16_t DeviceAddr)
  1135. {
  1136. /* Return IDD interrupt status */
  1137. return(mfxstm32l152_GlobalITStatus(DeviceAddr, MFXSTM32L152_IRQ_IDD));
  1138. }
  1139. /**
  1140. * @brief disable Idd interrupt
  1141. * @param DeviceAddr: Device address on communication Bus.
  1142. * @retval None.
  1143. */
  1144. void mfxstm32l152_IDD_DisableIT(uint16_t DeviceAddr)
  1145. {
  1146. /* Disable global IDD interrupt source */
  1147. mfxstm32l152_DisableITSource(DeviceAddr, MFXSTM32L152_IRQ_IDD);
  1148. }
  1149. /* ------------------------------------------------------------------ */
  1150. /* --------------------- ERROR MANAGEMENT --------------------------- */
  1151. /* ------------------------------------------------------------------ */
  1152. /**
  1153. * @brief Read Error Source.
  1154. * @param DeviceAddr: Device address on communication Bus.
  1155. * @retval Error message code with error source
  1156. */
  1157. uint8_t mfxstm32l152_Error_ReadSrc(uint16_t DeviceAddr)
  1158. {
  1159. /* Get the current source register value */
  1160. return(MFX_IO_Read((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_ERROR_SRC));
  1161. }
  1162. /**
  1163. * @brief Read Error Message
  1164. * @param DeviceAddr: Device address on communication Bus.
  1165. * @retval Error message code with error source
  1166. */
  1167. uint8_t mfxstm32l152_Error_ReadMsg(uint16_t DeviceAddr)
  1168. {
  1169. /* Get the current message register value */
  1170. return(MFX_IO_Read((uint8_t) DeviceAddr, MFXSTM32L152_REG_ADR_ERROR_MSG));
  1171. }
  1172. /**
  1173. * @brief Enable Error global interrupt
  1174. * @param DeviceAddr: Device address on communication Bus.
  1175. * @retval None
  1176. */
  1177. void mfxstm32l152_Error_EnableIT(uint16_t DeviceAddr)
  1178. {
  1179. MFX_IO_ITConfig();
  1180. /* Enable global Error interrupt source */
  1181. mfxstm32l152_EnableITSource(DeviceAddr, MFXSTM32L152_IRQ_ERROR);
  1182. }
  1183. /**
  1184. * @brief Clear Error global interrupt
  1185. * @param DeviceAddr: Device address on communication Bus.
  1186. * @retval None
  1187. */
  1188. void mfxstm32l152_Error_ClearIT(uint16_t DeviceAddr)
  1189. {
  1190. /* Clear the global Error interrupt source */
  1191. mfxstm32l152_ClearGlobalIT(DeviceAddr, MFXSTM32L152_IRQ_ERROR);
  1192. }
  1193. /**
  1194. * @brief get Error interrupt status
  1195. * @param DeviceAddr: Device address on communication Bus.
  1196. * @retval Error interrupts status
  1197. */
  1198. uint8_t mfxstm32l152_Error_GetITStatus(uint16_t DeviceAddr)
  1199. {
  1200. /* Return Error interrupt status */
  1201. return(mfxstm32l152_GlobalITStatus(DeviceAddr, MFXSTM32L152_IRQ_ERROR));
  1202. }
  1203. /**
  1204. * @brief disable Error interrupt
  1205. * @param DeviceAddr: Device address on communication Bus.
  1206. * @retval None.
  1207. */
  1208. void mfxstm32l152_Error_DisableIT(uint16_t DeviceAddr)
  1209. {
  1210. /* Disable global Error interrupt source */
  1211. mfxstm32l152_DisableITSource(DeviceAddr, MFXSTM32L152_IRQ_ERROR);
  1212. }
  1213. /**
  1214. * @brief FOR DEBUG ONLY
  1215. */
  1216. uint8_t mfxstm32l152_ReadReg(uint16_t DeviceAddr, uint8_t RegAddr)
  1217. {
  1218. /* Get the current register value */
  1219. return(MFX_IO_Read((uint8_t) DeviceAddr, RegAddr));
  1220. }
  1221. void mfxstm32l152_WriteReg(uint16_t DeviceAddr, uint8_t RegAddr, uint8_t Value)
  1222. {
  1223. /* set the current register value */
  1224. MFX_IO_Write((uint8_t) DeviceAddr, RegAddr, Value);
  1225. }
  1226. /* ------------------------------------------------------------------ */
  1227. /* ----------------------- Private functions ------------------------ */
  1228. /* ------------------------------------------------------------------ */
  1229. /**
  1230. * @brief Check if the device instance of the selected address is already registered
  1231. * and return its index
  1232. * @param DeviceAddr: Device address on communication Bus.
  1233. * @retval Index of the device instance if registered, 0xFF if not.
  1234. */
  1235. static uint8_t mfxstm32l152_GetInstance(uint16_t DeviceAddr)
  1236. {
  1237. uint8_t idx = 0;
  1238. /* Check all the registered instances */
  1239. for(idx = 0; idx < MFXSTM32L152_MAX_INSTANCE ; idx ++)
  1240. {
  1241. if(mfxstm32l152[idx] == DeviceAddr)
  1242. {
  1243. return idx;
  1244. }
  1245. }
  1246. return 0xFF;
  1247. }
  1248. /**
  1249. * @brief Release registered device instance
  1250. * @param DeviceAddr: Device address on communication Bus.
  1251. * @retval Index of released device instance, 0xFF if not.
  1252. */
  1253. static uint8_t mfxstm32l152_ReleaseInstance(uint16_t DeviceAddr)
  1254. {
  1255. uint8_t idx = 0;
  1256. /* Check for all the registered instances */
  1257. for(idx = 0; idx < MFXSTM32L152_MAX_INSTANCE ; idx ++)
  1258. {
  1259. if(mfxstm32l152[idx] == DeviceAddr)
  1260. {
  1261. mfxstm32l152[idx] = 0;
  1262. return idx;
  1263. }
  1264. }
  1265. return 0xFF;
  1266. }
  1267. /**
  1268. * @brief Internal routine
  1269. * @param DeviceAddr: Device address on communication Bus.
  1270. * @param RegisterAddr: Register Address
  1271. * @param PinPosition: Pin [0:23]
  1272. * @param PinValue: 0/1
  1273. * @retval None
  1274. */
  1275. void mfxstm32l152_reg24_setPinValue(uint16_t DeviceAddr, uint8_t RegisterAddr, uint32_t PinPosition, uint8_t PinValue )
  1276. {
  1277. uint8_t tmp = 0;
  1278. uint8_t pin_0_7, pin_8_15, pin_16_23;
  1279. pin_0_7 = PinPosition & 0x0000ff;
  1280. pin_8_15 = PinPosition >> 8;
  1281. pin_8_15 = pin_8_15 & 0x00ff;
  1282. pin_16_23 = PinPosition >> 16;
  1283. if (pin_0_7)
  1284. {
  1285. /* Get the current register value */
  1286. tmp = MFX_IO_Read(DeviceAddr, RegisterAddr);
  1287. /* Set the selected pin direction */
  1288. if (PinValue != 0)
  1289. {
  1290. tmp |= (uint8_t)pin_0_7;
  1291. }
  1292. else
  1293. {
  1294. tmp &= ~(uint8_t)pin_0_7;
  1295. }
  1296. /* Set the new register value */
  1297. MFX_IO_Write(DeviceAddr, RegisterAddr, tmp);
  1298. }
  1299. if (pin_8_15)
  1300. {
  1301. /* Get the current register value */
  1302. tmp = MFX_IO_Read(DeviceAddr, RegisterAddr+1);
  1303. /* Set the selected pin direction */
  1304. if (PinValue != 0)
  1305. {
  1306. tmp |= (uint8_t)pin_8_15;
  1307. }
  1308. else
  1309. {
  1310. tmp &= ~(uint8_t)pin_8_15;
  1311. }
  1312. /* Set the new register value */
  1313. MFX_IO_Write(DeviceAddr, RegisterAddr+1, tmp);
  1314. }
  1315. if (pin_16_23)
  1316. {
  1317. /* Get the current register value */
  1318. tmp = MFX_IO_Read(DeviceAddr, RegisterAddr+2);
  1319. /* Set the selected pin direction */
  1320. if (PinValue != 0)
  1321. {
  1322. tmp |= (uint8_t)pin_16_23;
  1323. }
  1324. else
  1325. {
  1326. tmp &= ~(uint8_t)pin_16_23;
  1327. }
  1328. /* Set the new register value */
  1329. MFX_IO_Write(DeviceAddr, RegisterAddr+2, tmp);
  1330. }
  1331. }
  1332. /**
  1333. * @}
  1334. */
  1335. /**
  1336. * @}
  1337. */
  1338. /**
  1339. * @}
  1340. */
  1341. /**
  1342. * @}
  1343. */
  1344. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/