Appiko
SDK_EVAL_EEPROM.c
1 
23 /* Includes ------------------------------------------------------------------*/
24 #include "SDK_EVAL_Config.h"
25 #include "SDK_EVAL_EEPROM.h"
26 
43 static SPI_HandleTypeDef EepromSpiHandle;
44 static uint8_t s_eeprom = 0;
45 
46 /*-----------------To Manage Board Selection-----------------------*/
47 static uint8_t BoardType = 0; //0 for SDK (default) 1 for XNUCLEO
48 /* To Store the state of the main CS reg */
49 static uint32_t MainCs_ModerGpioReg;
50 static uint32_t MainCs_OtyperGpioReg;
51 static uint32_t MainCs_OspeedrGpioReg;
52 static uint32_t MainCs_PupdrGpioReg;
53 static uint32_t MainCs_OdrGpioReg;
54 static uint32_t MainCs_AfrGpioReg[2];
55 
56 
57 void EepromSPICSLow()
58 {
59  if (BoardType==1) HAL_GPIO_WritePin(EEPROM_SPI_PERIPH_XNUCLEO_CS_PORT, EEPROM_SPI_PERIPH_XNUCLEO_CS_PIN, GPIO_PIN_RESET);
60  else HAL_GPIO_WritePin(EEPROM_SPI_PERIPH_CS_PORT, EEPROM_SPI_PERIPH_CS_PIN, GPIO_PIN_RESET);
61 }
62 
63 void EepromSPICSHigh()
64 {
65  if (BoardType==1) HAL_GPIO_WritePin(EEPROM_SPI_PERIPH_XNUCLEO_CS_PORT, EEPROM_SPI_PERIPH_XNUCLEO_CS_PIN, GPIO_PIN_SET);
66  else HAL_GPIO_WritePin(EEPROM_SPI_PERIPH_CS_PORT, EEPROM_SPI_PERIPH_CS_PIN, GPIO_PIN_SET);
67 }
77 {
78 
79  /*----------------------- GPIO Mode Configuration --------------------*/
80 
81  GPIO_InitTypeDef GPIO_InitStructure;
82 
83  /* Enable SPI periph and SCLK, MOSI, MISO and CS GPIO clocks */
84  EEPROM_SPI_PERIPH_RCC();
85  EEPROM_SPI_PERIPH_MOSI_RCC();
86  EEPROM_SPI_PERIPH_MISO_RCC();
87  EEPROM_SPI_PERIPH_SCLK_RCC();
88  EEPROM_SPI_PERIPH_CS_RCC();
89 
90 
91  /* Configure the AF for MOSI, MISO and SCLK GPIO pins*/
92  GPIO_InitStructure.Pin = EEPROM_SPI_PERIPH_SCLK_PIN;
93  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
94  GPIO_InitStructure.Pull = GPIO_PULLUP;
95  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
96  GPIO_InitStructure.Alternate = EEPROM_SPI_PERIPH_SCLK_AF;
97  HAL_GPIO_Init(EEPROM_SPI_PERIPH_SCLK_PORT, &GPIO_InitStructure);
98 
99  GPIO_InitStructure.Pin = EEPROM_SPI_PERIPH_MISO_PIN;
100  GPIO_InitStructure.Alternate = EEPROM_SPI_PERIPH_MISO_AF;
101  HAL_GPIO_Init(EEPROM_SPI_PERIPH_MISO_PORT, &GPIO_InitStructure);
102 
103  GPIO_InitStructure.Pin = EEPROM_SPI_PERIPH_MOSI_PIN;
104  GPIO_InitStructure.Alternate = EEPROM_SPI_PERIPH_MOSI_AF;
105  HAL_GPIO_Init(EEPROM_SPI_PERIPH_MOSI_PORT, &GPIO_InitStructure);
106 
107 
108  /*- Freeze the previous status of Main CS (Maybe it is used by the user) -*/
109 
110  GPIO_TypeDef *MainCsEepromPort;
111  MainCsEepromPort = EEPROM_SPI_PERIPH_CS_PORT;
112 
113  /* Store IO Direction in Input Floting Mode */
114  MainCs_ModerGpioReg = MainCsEepromPort->MODER;
115 
116  /* Store the previous Alternate Function in current IO */
117  MainCs_AfrGpioReg[0] = MainCsEepromPort->AFR[0] ;
118  MainCs_AfrGpioReg[1] = MainCsEepromPort->AFR[1] ;
119 
120  /* Store the previous value for IO Speed */
121  MainCs_OspeedrGpioReg = MainCsEepromPort->OSPEEDR;
122 
123  /* Store the previous value IO Output Type */
124  MainCs_OtyperGpioReg = MainCsEepromPort->OTYPER ;
125 
126  /* Store the previous Output Value */
127  MainCs_OdrGpioReg = MainCsEepromPort->ODR ;
128 
129  /* Store the previous Pull-up oand Pull-down value */
130  MainCs_PupdrGpioReg = MainCsEepromPort->PUPDR;
131  /*--------------------------- GPIO Mode Configuration --------------------*/
132 
133  //Chip Select (CS) GPIO Conf
134  GPIO_InitStructure.Pin = EEPROM_SPI_PERIPH_CS_PIN;
135  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
136  HAL_GPIO_Init(EEPROM_SPI_PERIPH_CS_PORT, &GPIO_InitStructure);
137 
138 
139  /* Configure SPI peripheral */
140  if(HAL_SPI_GetState(&EepromSpiHandle) == HAL_SPI_STATE_RESET)
141  {
142  /* Set the SPI parameters */
143  EepromSpiHandle.Instance = EEPROM_SPI_PERIPH_NB;
144  EepromSpiHandle.Init.Mode = SPI_MODE_MASTER;
145  EepromSpiHandle.Init.BaudRatePrescaler = SDK_EVAL_SPI_PRESCALER;
146 
147  EepromSpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
148  EepromSpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
149  EepromSpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
150  EepromSpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
151  EepromSpiHandle.Init.CRCPolynomial = 7;
152  EepromSpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
153  EepromSpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
154  EepromSpiHandle.Init.NSS = SPI_NSS_SOFT;
155  EepromSpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
156 
157  if(HAL_SPI_Init(&EepromSpiHandle) != HAL_OK) {
158  return;
159  }
160  __HAL_SPI_ENABLE(&EepromSpiHandle);
161  }
162 
163  EepromSPICSHigh();
164 }
165 
174 {
175 
176 
177  BoardType = 0;
178 
179  GPIO_InitTypeDef GPIO_InitStructure;
180  /* Configure SPI pin: CS */
181  GPIO_InitStructure.Pin = EEPROM_SPI_PERIPH_CS_PIN;
182  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
183  GPIO_InitStructure.Pull = GPIO_PULLUP;
184  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
185  HAL_GPIO_Init(EEPROM_SPI_PERIPH_CS_PORT, &GPIO_InitStructure);
186 
187  /* Enable CS GPIO clock */
188  EEPROM_SPI_PERIPH_CS_RCC();
189 
190  /* Put the SPI chip select high to end the transaction */
191  EepromSPICSHigh();
192 }
193 
202 {
203  /*--------------- To Restore the previous statut of CS ----------------*/
204  GPIO_TypeDef *MainCsEepromPort;
205  MainCsEepromPort = EEPROM_SPI_PERIPH_CS_PORT;
206 
207  /* Store IO Direction in Input Floting Mode */
208  MainCsEepromPort->MODER = MainCs_ModerGpioReg;
209 
210  /* Store the previous Alternate Function in current IO */
211  MainCsEepromPort->AFR[0] = MainCs_AfrGpioReg[0];
212  MainCsEepromPort->AFR[1]= MainCs_AfrGpioReg[1];
213 
214  /* Store the previous value for IO Speed */
215  MainCsEepromPort->OSPEEDR = MainCs_OspeedrGpioReg;
216 
217  /* Store the previous value IO Output Type */
218  MainCsEepromPort->OTYPER = MainCs_OtyperGpioReg;
219 
220  /* Store the previous Output Value */
221  MainCsEepromPort->ODR = MainCs_OdrGpioReg;
222 
223  /* Store the previous Pull-up oand Pull-down value */
224  MainCsEepromPort->PUPDR = MainCs_PupdrGpioReg;
225 
226 
228  BoardType = 1;
229 
230  GPIO_InitTypeDef GPIO_InitStructure;
232  GPIO_InitStructure.Pin = EEPROM_SPI_PERIPH_XNUCLEO_CS_PIN;
233  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
234  GPIO_InitStructure.Pull = GPIO_PULLUP;
235  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
236  HAL_GPIO_Init(EEPROM_SPI_PERIPH_XNUCLEO_CS_PORT, &GPIO_InitStructure);
237 
238  EEPROM_SPI_PERIPH_XNUCLEO_CS_RCC();
239 
240  //Put the SPI chip select high to end the transaction
241  EepromSPICSHigh();
242 }
243 
251 {
252  uint8_t cmd = EEPROM_CMD_RDSR;
253  uint8_t dummy = 0xFF;
254  uint8_t status;
255 
256  /* Put the SPI chip select low to start the transaction */
257  EepromSPICSLow();
258 
259  /* Send command */
260  HAL_SPI_TransmitReceive(&EepromSpiHandle, &cmd, &status, 1, 1000);
261 
262  /* Polling on status register */
263  do{
264  HAL_SPI_TransmitReceive(&EepromSpiHandle, &dummy, &status, 1, 1000);
265  }while(status&EEPROM_STATUS_WIP);
266 
267  /* Put the SPI chip select high to end the transaction */
268  EepromSPICSHigh();
269 
270 }
271 
272 
278 uint8_t EepromStatus(void)
279 {
280  uint8_t status[2];
281  uint8_t cmd[] = {EEPROM_CMD_RDSR, 0xFF};
282 
283  /* Put the SPI chip select low to start the transaction */
284  EepromSPICSLow();
285 
286  /* Send command */
287  HAL_SPI_TransmitReceive(&EepromSpiHandle, cmd, status, 2, 1000);
288 
289  /* Put the SPI chip select high to end the transaction */
290  EepromSPICSHigh();
291 
292  return status[1];
293 
294 }
295 
301 uint8_t EepromSetSrwd(void)
302 {
303  uint8_t status[2];
304  uint8_t cmd[] = {EEPROM_CMD_WRSR, EEPROM_STATUS_SRWD};
305 
306  /* Put the SPI chip select low to start the transaction */
307  EepromSPICSLow();
308 
309  /* Send command */
310  HAL_SPI_TransmitReceive(&EepromSpiHandle, cmd, status, 2, 1000);
311 
312  /* Put the SPI chip select high to end the transaction */
313  EepromSPICSHigh();
314 
315 
316  return status[1];
317 }
318 
324 uint8_t EepromResetSrwd(void)
325 {
326  uint8_t status[2];
327  uint8_t cmd[] = {EEPROM_CMD_WRSR, 0};//EEPROM_STATUS_SRWD};
328 
331  /* Put the SPI chip select low to start the transaction */
332  EepromSPICSLow();
333 
334  /* Send command */
335  HAL_SPI_TransmitReceive(&EepromSpiHandle, cmd, status, 2, 1000);
336 
337  /* Put the SPI chip select high to end the transaction */
338  EepromSPICSHigh();
340 
341  return status[1];
342 }
343 
350 {
351  uint8_t cmd = EEPROM_CMD_WREN;
352  uint8_t status;
353 
354  /* Put the SPI chip select low to start the transaction */
355  EepromSPICSLow();
356  /* Send command */
357  HAL_SPI_TransmitReceive(&EepromSpiHandle, &cmd, &status, 1, 1000);
358 
359  /* Put the SPI chip select high to end the transaction */
360  EepromSPICSHigh();
361 
362 }
363 
364 
376 void EepromRead(uint16_t nAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
377 {
378  uint8_t cmd[3];
379  uint8_t dummy[255];
380  cmd[0] = EEPROM_CMD_READ;
381 
382  for(uint8_t k=0; k<2; k++) {
383  cmd[k+1] = (uint8_t)(nAddress>>((1-k)*8));
384  }
385 
386  /* Wait the end of a previous write operation */
388 
389  /* Put the SPI chip select low to start the transaction */
390  EepromSPICSLow();
391 
392  /* Write the header bytes and read the status bytes */
393  HAL_SPI_TransmitReceive(&EepromSpiHandle, cmd, dummy, 3, 1000);
394 
395  /* Read the registers according to the number of bytes */
396  HAL_SPI_TransmitReceive(&EepromSpiHandle, dummy, pcBuffer, cNbBytes, 1000);
397 
398  /* Put the SPI chip select high to end the transaction */
399  EepromSPICSHigh();
400 
401 }
402 
403 
418 void EepromWrite(uint16_t nAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
419 {
420  uint8_t cmd = EEPROM_CMD_WRITE, tmp[255];
421  uint8_t address[2];
422 
423  /* Wait the end of a previous write operation */
425 
426  /* SET the WREN flag */
428 
429  for(uint8_t k=0; k<2; k++) {
430  address[k] = (uint8_t)(nAddress>>((1-k)*8));
431  }
432  //EepromWaitEndWriteOperation();
433 
434  /* Put the SPI chip select low to start the transaction */
435  EepromSPICSLow();
436 
437  /* Write the header bytes and read the SPIRIT status bytes */
438  HAL_SPI_TransmitReceive(&EepromSpiHandle, &cmd, tmp, 1, 1000);
439 
440  HAL_SPI_TransmitReceive(&EepromSpiHandle, address, tmp, 2, 1000);
441 
442  HAL_SPI_TransmitReceive(&EepromSpiHandle, pcBuffer, tmp, cNbBytes, 1000);
443 
444  /* Put the SPI chip select high to end the transaction */
445  EepromSPICSHigh();
446 
447 }
448 
454 uint8_t SdkEvalGetHasEeprom(void)
455 {
456  return s_eeprom;
457 }
458 
459 
465 void SdkEvalSetHasEeprom(uint8_t eeprom)
466 {
467  s_eeprom = eeprom;
468 }
469 
470 
480 /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
This file contains SDK EVAL configuration and useful defines.
void SdkEvalSetHasEeprom(uint8_t eeprom)
This function is to set if EEPROM is present or not.
void EepromWriteEnable(void)
Set the internal WEL flag to allow write operation.
uint8_t EepromResetSrwd(void)
Reset the ERSR status bit.
void EepromWrite(uint16_t nAddress, uint8_t cNbBytes, uint8_t *pcBuffer)
Write a page of the EEPROM. A page size is 32 bytes. The pages are 256. Page 0 address: 0x0000 Page 1...
void EepromCsXnucleoPinInitialization(void)
Initialization of the CSn pin of the EEPROM for XNUCLEO boards.
void EepromSpiInitialization(void)
Initializes the SPI for the EEPROM. SPI, MISO, MOSI and SCLK are the same used for the SPIRIT1....
uint8_t EepromSetSrwd(void)
Set the ERSR status bit.
uint8_t SdkEvalGetHasEeprom(void)
This function is to query if EEPROM is present or not.
void EepromCsPinInitialization(void)
Initialization of the CSn pin of the EEPROM. This function is called internally by EepromCsPinInitial...
void EepromRead(uint16_t nAddress, uint8_t cNbBytes, uint8_t *pcBuffer)
Read a page of the EEPROM. A page size is 32 bytes. The pages are 256. Page 0 address: 0x0000 Page 1 ...
void EepromWaitEndWriteOperation(void)
Wait polling the SPI until the internal WIP flag is RESET. The flag is SET when a write operation is ...
uint8_t EepromStatus(void)
Read the status register.