aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/lib/complex/serial_nor/hal_serial_nor.c
blob: 0fd5b7dc107281be4220ca085a2e27ab9f6accd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    hal_serial_nor.c
 * @brief   Serial NOR serial flash driver code.
 *
 * @addtogroup HAL_SERIAL_NOR
 * @{
 */

#include "hal.h"
#include "hal_serial_nor.h"

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

static const flash_descriptor_t *snor_get_descriptor(void *instance);
static flash_error_t snor_read(void *instance, flash_offset_t offset,
                               size_t n, uint8_t *rp);
static flash_error_t snor_program(void *instance, flash_offset_t offset,
                                  size_t n, const uint8_t *pp);
static flash_error_t snor_start_erase_all(void *instance);
static flash_error_t snor_start_erase_sector(void *instance,
                                             flash_sector_t sector);
static flash_error_t snor_verify_erase(void *instance,
                                       flash_sector_t sector);
static flash_error_t snor_query_erase(void *instance, uint32_t *msec);
static flash_error_t snor_read_sfdp(void *instance, flash_offset_t offset,
                                    size_t n, uint8_t *rp);

/**
 * @brief   Virtual methods table.
 */
static const struct SNORDriverVMT snor_vmt = {
  (size_t)0,
  snor_get_descriptor, snor_read, snor_program,
  snor_start_erase_all, snor_start_erase_sector,
  snor_query_erase, snor_verify_erase,
  snor_read_sfdp
};

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

#if ((SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) &&                           \
     (SNOR_SHARED_BUS == TRUE)) || defined(__DOXYGEN__)
/**
 * @brief   Bus acquisition and lock.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] config    bus configuration
 *
 * @notapi
 */
static void bus_acquire(BUSDriver *busp, const BUSConfig *config) {

  (void)config;

  wspiAcquireBus(busp);
  if (busp->config != config) {
    wspiStart(busp, config);
  }
}

/**
 * @brief   Bus release.
 *
 * @param[in] busp      pointer to the bus driver
 *
 * @notapi
 */
static void bus_release(BUSDriver *busp) {

  wspiReleaseBus(busp);
}

#elif (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_SPI) &&                           \
      (SNOR_SHARED_BUS == TRUE)
void bus_acquire(BUSDriver *busp, const BUSConfig *config) {

  spiAcquireBus(busp);
  if (busp->config != config) {
    spiStart(busp, config);
  }
}

void bus_release(BUSDriver *busp) {

  spiReleaseBus(busp);
}

#else
/* No bus sharing, empty macros.*/
#define bus_acquire(busp)
#define bus_release(busp)
#endif

/**
 * @brief   Returns a pointer to the device descriptor.
 *
 * @param[in] instance  instance pointer
 */
static const flash_descriptor_t *snor_get_descriptor(void *instance) {
  SNORDriver *devp = (SNORDriver *)instance;

  osalDbgCheck(instance != NULL);
  osalDbgAssert((devp->state != FLASH_UNINIT) && (devp->state != FLASH_STOP),
                "invalid state");

  return &snor_descriptor;
}

static flash_error_t snor_read(void *instance, flash_offset_t offset,
                               size_t n, uint8_t *rp) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck((instance != NULL) && (rp != NULL) && (n > 0U));
  osalDbgCheck((size_t)offset + n <= (size_t)snor_descriptor.sectors_count *
                                     (size_t)snor_descriptor.sectors_size);
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  if (devp->state == FLASH_ERASE) {
    return FLASH_BUSY_ERASING;
  }

  /* Bus acquired.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* FLASH_READY state while the operation is performed.*/
  devp->state = FLASH_READ;

  /* Actual read implementation.*/
  err = snor_device_read(devp, offset, n, rp);

  /* Ready state again.*/
  devp->state = FLASH_READY;

  /* Bus released.*/
  bus_release(devp->config->busp);

  return err;
}

static flash_error_t snor_program(void *instance, flash_offset_t offset,
                                  size_t n, const uint8_t *pp) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck((instance != NULL) && (pp != NULL) && (n > 0U));
  osalDbgCheck((size_t)offset + n <= (size_t)snor_descriptor.sectors_count *
                                     (size_t)snor_descriptor.sectors_size);
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  if (devp->state == FLASH_ERASE) {
    return FLASH_BUSY_ERASING;
  }

  /* Bus acquired.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* FLASH_PGM state while the operation is performed.*/
  devp->state = FLASH_PGM;

  /* Actual program implementation.*/
  err = snor_device_program(devp, offset, n, pp);

  /* Ready state again.*/
  devp->state = FLASH_READY;

  /* Bus released.*/
  bus_release(devp->config->busp);

  return err;
}

static flash_error_t snor_start_erase_all(void *instance) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck(instance != NULL);
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  if (devp->state == FLASH_ERASE) {
    return FLASH_BUSY_ERASING;
  }

  /* Bus acquired.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* FLASH_ERASE state while the operation is performed.*/
  devp->state = FLASH_ERASE;

  /* Actual erase implementation.*/
  err = snor_device_start_erase_all(devp);

  /* Ready state again.*/
  devp->state = FLASH_READY;

  /* Bus released.*/
  bus_release(devp->config->busp);

  return err;
}

static flash_error_t snor_start_erase_sector(void *instance,
                                             flash_sector_t sector) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck(instance != NULL);
  osalDbgCheck(sector < snor_descriptor.sectors_count);
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  if (devp->state == FLASH_ERASE) {
    return FLASH_BUSY_ERASING;
  }

  /* Bus acquired.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* FLASH_ERASE state while the operation is performed.*/
  devp->state = FLASH_ERASE;

  /* Actual erase implementation.*/
  err = snor_device_start_erase_sector(devp, sector);

  /* Bus released.*/
  bus_release(devp->config->busp);

  return err;
}

static flash_error_t snor_verify_erase(void *instance,
                                       flash_sector_t sector) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck(instance != NULL);
  osalDbgCheck(sector < snor_descriptor.sectors_count);
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  if (devp->state == FLASH_ERASE) {
    return FLASH_BUSY_ERASING;
  }

  /* Bus acquired.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* FLASH_READY state while the operation is performed.*/
  devp->state = FLASH_READ;

  /* Actual verify erase implementation.*/
  err = snor_device_verify_erase(devp, sector);

  /* Ready state again.*/
  devp->state = FLASH_READY;

  /* Bus released.*/
  bus_release(devp->config->busp);

  return err;
}

static flash_error_t snor_query_erase(void *instance, uint32_t *msec) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck(instance != NULL);
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  /* If there is an erase in progress then the device must be checked.*/
  if (devp->state == FLASH_ERASE) {

    /* Bus acquired.*/
    bus_acquire(devp->config->busp, devp->config->buscfg);

    /* Actual query erase implementation.*/
    err = snor_device_query_erase(devp, msec);

    /* The device is ready to accept commands.*/
    if (err == FLASH_NO_ERROR) {
      devp->state = FLASH_READY;
    }

    /* Bus released.*/
    bus_release(devp->config->busp);
  }
  else {
    err = FLASH_NO_ERROR;
  }

  return err;
}

static flash_error_t snor_read_sfdp(void *instance, flash_offset_t offset,
                                    size_t n, uint8_t *rp) {
  SNORDriver *devp = (SNORDriver *)instance;
  flash_error_t err;

  osalDbgCheck((instance != NULL) && (rp != NULL) && (n > 0U));
  osalDbgAssert((devp->state == FLASH_READY) || (devp->state == FLASH_ERASE),
                "invalid state");

  if (devp->state == FLASH_ERASE) {
    return FLASH_BUSY_ERASING;
  }

  /* Bus acquired.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* Actual read SFDP implementation.*/
  err = snor_device_read_sfdp(devp, offset, n, rp);

  /* The device is ready to accept commands.*/
  if (err == FLASH_NO_ERROR) {
    devp->state = FLASH_READY;
  }

  /* Bus released.*/
  bus_release(devp->config->busp);

  return err;
}

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Stops the underlying bus driver.
 *
 * @param[in] busp      pointer to the bus driver
 *
 * @notapi
 */
void bus_stop(BUSDriver *busp) {

#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspiStop(busp);
#else
  spiStop(busp);
#endif
}

/**
 * @brief   Sends a naked command.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 *
 * @notapi
 */
void bus_cmd(BUSDriver *busp, uint32_t cmd) {
#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD;
  mode.addr  = 0U;
  mode.alt   = 0U;
  mode.dummy = 0U;
  wspiCommand(busp, &mode);
#else
  uint8_t buf[1];

  spiSelect(busp);
  buf[0] = cmd;
  spiSend(busp, 1, buf);
  spiUnselect(busp);
#endif
}

/**
 * @brief   Sends a command followed by a data transmit phase.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] n         number of bytes to receive
 * @param[in] p         data buffer
 *
 * @notapi
 */
void bus_cmd_send(BUSDriver *busp, uint32_t cmd, size_t n, const uint8_t *p) {
#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_DATA;
  mode.addr  = 0U;
  mode.alt   = 0U;
  mode.dummy = 0U;
  wspiSend(busp, &mode, n, p);
#else
  uint8_t buf[1];

  spiSelect(busp);
  buf[0] = cmd;
  spiSend(busp, 1, buf);
  spiSend(busp, n, p);
  spiUnselect(busp);
#endif
}

/**
 * @brief   Sends a command followed by a data receive phase.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] n         number of bytes to receive
 * @param[out] p        data buffer
 *
 * @notapi
 */
void bus_cmd_receive(BUSDriver *busp,
                     uint32_t cmd,
                     size_t n,
                     uint8_t *p) {
#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_DATA;
  mode.addr  = 0U;
  mode.alt   = 0U;
  mode.dummy = 0U;
  wspiReceive(busp, &mode, n, p);
#else
  uint8_t buf[1];

  spiSelect(busp);
  buf[0] = cmd;
  spiSend(busp, 1, buf);
  spiReceive(busp, n, p);
  spiUnselect(busp);
#endif
}

/**
 * @brief   Sends a command followed by a flash address.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] offset    flash offset
 *
 * @notapi
 */
void bus_cmd_addr(BUSDriver *busp, uint32_t cmd, flash_offset_t offset) {
#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_ADDR;
  mode.addr  = offset;
  mode.alt   = 0U;
  mode.dummy = 0U;
  wspiCommand(busp, &mode);
#else
  uint8_t buf[4];

  spiSelect(busp);
  buf[0] = cmd;
  buf[1] = (uint8_t)(offset >> 16);
  buf[2] = (uint8_t)(offset >> 8);
  buf[3] = (uint8_t)(offset >> 0);
  spiSend(busp, 4, buf);
  spiUnselect(busp);
#endif
}

/**
 * @brief   Sends a command followed by a flash address and a data transmit
 *          phase.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] offset    flash offset
 * @param[in] n         number of bytes to receive
 * @param[in] p         data buffer
 *
 * @notapi
 */
void bus_cmd_addr_send(BUSDriver *busp,
                       uint32_t cmd,
                       flash_offset_t offset,
                       size_t n,
                       const uint8_t *p) {
#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_ADDR_DATA;
  mode.addr  = offset;
  mode.alt   = 0U;
  mode.dummy = 0U;
  wspiSend(busp, &mode, n, p);
#else
  uint8_t buf[4];

  spiSelect(busp);
  buf[0] = cmd;
  buf[1] = (uint8_t)(offset >> 16);
  buf[2] = (uint8_t)(offset >> 8);
  buf[3] = (uint8_t)(offset >> 0);
  spiSend(busp, 4, buf);
  spiSend(busp, n, p);
  spiUnselect(busp);
#endif
}

/**
 * @brief   Sends a command followed by a flash address and a data receive
 *          phase.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] offset    flash offset
 * @param[in] n         number of bytes to receive
 * @param[out] p        data buffer
 *
 * @notapi
 */
void bus_cmd_addr_receive(BUSDriver *busp,
                          uint32_t cmd,
                          flash_offset_t offset,
                          size_t n,
                          uint8_t *p) {
#if SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_ADDR_DATA;
  mode.addr  = offset;
  mode.alt   = 0U;
  mode.dummy = 0U;
  wspiReceive(busp, &mode, n, p);
#else
  uint8_t buf[4];

  spiSelect(busp);
  buf[0] = cmd;
  buf[1] = (uint8_t)(offset >> 16);
  buf[2] = (uint8_t)(offset >> 8);
  buf[3] = (uint8_t)(offset >> 0);
  spiSend(busp, 4, buf);
  spiReceive(busp, n, p);
  spiUnselect(busp);
#endif
}

#if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) || defined(__DOXYGEN__)
/**
 * @brief   Sends a command followed by dummy cycles and a
 *          data receive phase.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] dummy     number of dummy cycles
 * @param[in] n         number of bytes to receive
 * @param[out] p        data buffer
 *
 * @notapi
 */
void bus_cmd_dummy_receive(BUSDriver *busp,
                           uint32_t cmd,
                           uint32_t dummy,
                           size_t n,
                           uint8_t *p) {
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_DATA;
  mode.addr  = 0U;
  mode.alt   = 0U;
  mode.dummy = dummy;
  wspiReceive(busp, &mode, n, p);
}

/**
 * @brief   Sends a command followed by a flash address, dummy cycles and a
 *          data receive phase.
 *
 * @param[in] busp      pointer to the bus driver
 * @param[in] cmd       instruction code
 * @param[in] offset    flash offset
 * @param[in] dummy     number of dummy cycles
 * @param[in] n         number of bytes to receive
 * @param[out] p        data buffer
 *
 * @notapi
 */
void bus_cmd_addr_dummy_receive(BUSDriver *busp,
                                uint32_t cmd,
                                flash_offset_t offset,
                                uint32_t dummy,
                                size_t n,
                                uint8_t *p) {
  wspi_command_t mode;

  mode.cmd   = cmd;
  mode.cfg   = SNOR_WSPI_CFG_CMD_ADDR_DATA;
  mode.addr  = offset;
  mode.alt   = 0U;
  mode.dummy = dummy;
  wspiReceive(busp, &mode, n, p);
}
#endif /* SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI */

/**
 * @brief   Initializes an instance.
 *
 * @param[out] devp     pointer to the @p SNORDriver object
 *
 * @init
 */
void snorObjectInit(SNORDriver *devp) {

  osalDbgCheck(devp != NULL);

  devp->vmt         = &snor_vmt;
  devp->state       = FLASH_STOP;
  devp->config      = NULL;
}

/**
 * @brief   Configures and activates SNOR driver.
 *
 * @param[in] devp      pointer to the @p SNORDriver object
 * @param[in] config    pointer to the configuration
 *
 * @api
 */
void snorStart(SNORDriver *devp, const SNORConfig *config) {

  osalDbgCheck((devp != NULL) && (config != NULL));
  osalDbgAssert(devp->state != FLASH_UNINIT, "invalid state");

  devp->config = config;

  if (devp->state == FLASH_STOP) {

    /* Bus acquisition.*/
    bus_acquire(devp->config->busp, devp->config->buscfg);

    /* Device identification and initialization.*/
    snor_device_init(devp);

    /* Driver in ready state.*/
    devp->state = FLASH_READY;

    /* Bus release.*/
    bus_release(devp->config->busp);
  }
} 

/**
 * @brief   Deactivates the SNOR driver.
 *
 * @param[in] devp      pointer to the @p SNORDriver object
 *
 * @api
 */
void snorStop(SNORDriver *devp) {

  osalDbgCheck(devp != NULL);
  osalDbgAssert(devp->state != FLASH_UNINIT, "invalid state");

  if (devp->state != FLASH_STOP) {

    /* Bus acquisition.*/
    bus_acquire(devp->config->busp, devp->config->buscfg);

    /* Stopping bus device.*/
    bus_stop(devp->config->busp);

    /* Driver stopped.*/
    devp->state = FLASH_STOP;

    /* Bus release.*/
    bus_release(devp->config->busp);

    /* Deleting current configuration.*/
    devp->config = NULL;
  }
}

#if (SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI) || defined(__DOXYGEN__)
#if (WSPI_SUPPORTS_MEMMAP == TRUE) || defined(__DOXYGEN__)
/**
 * @brief   Enters the memory Mapping mode.
 * @details The memory mapping mode is only available when the WSPI mode
 *          is selected and the underlying WSPI controller supports the
 *          feature.
 *
 * @param[in] devp      pointer to the @p SNORDriver object
 * @param[out] addrp    pointer to the memory start address of the mapped
 *                      flash or @p NULL
 *
 * @api
 */
void snorMemoryMap(SNORDriver *devp, uint8_t **addrp) {

  /* Bus acquisition.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

#if SNOR_DEVICE_SUPPORTS_XIP == TRUE
  /* Activating XIP mode in the device.*/
  snor_activate_xip(devp);
#endif

  /* Starting WSPI memory mapped mode.*/
  wspiMapFlash(devp->config->busp, &snor_memmap_read, addrp);

  /* Bus release.*/
  bus_release(devp->config->busp);
}

/**
 * @brief   Leaves the memory Mapping mode.
 *
 * @param[in] devp      pointer to the @p SNORDriver object
 *
 * @api
 */
void snorMemoryUnmap(SNORDriver *devp) {

  /* Bus acquisition.*/
  bus_acquire(devp->config->busp, devp->config->buscfg);

  /* Stopping WSPI memory mapped mode.*/
  wspiUnmapFlash(devp->config->busp);

#if SNOR_DEVICE_SUPPORTS_XIP == TRUE
  snor_reset_xip(devp);
#endif

  /* Bus release.*/
  bus_release(devp->config->busp);
}
#endif /* WSPI_SUPPORTS_MEMMAP == TRUE */
#endif /* SNOR_BUS_DRIVER == SNOR_BUS_DRIVER_WSPI */

/** @} */