source: rtems/c/src/lib/libbsp/arm/lpc32xx/misc/nand-mlc-write-blocks.c @ b2ed712

5
Last change on this file since b2ed712 was b2ed712, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:58:58

Include missing <string.h>

Update #2133.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup lpc32xx_nand_mlc
5 *
6 * @brief lpc32xx_mlc_write_blocks() implementation.
7 */
8
9/*
10 * Copyright (c) 2010-2011 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#include <bsp/nand-mlc.h>
24
25#include <string.h>
26
27static const uint32_t ones_spare [MLC_LARGE_SPARE_WORD_COUNT] = {
28  0xffffffff,
29  0xffffffff,
30  0xffffffff,
31  0xffffffff,
32  0xffffffff,
33  0xffffffff,
34  0xffffffff,
35  0xffffffff,
36  0xffffffff,
37  0xffffffff,
38  0xffffffff,
39  0xffffffff,
40  0xffffffff,
41  0xffffffff,
42  0xffffffff,
43  0xffffffff
44};
45
46rtems_status_code lpc32xx_mlc_write_blocks(
47  uint32_t block_begin,
48  uint32_t block_end,
49  const void *src,
50  size_t src_size,
51  uint32_t *page_data_buffer
52)
53{
54  rtems_status_code sc = RTEMS_SUCCESSFUL;
55  uint32_t pages_per_block = lpc32xx_mlc_pages_per_block();
56  uint32_t block_count = lpc32xx_mlc_block_count();
57  uint32_t page_size = lpc32xx_mlc_page_size();
58  uint32_t block = 0;
59  const uint8_t *current = src;
60  const uint8_t *last = current;
61  const uint8_t *end = current + src_size;
62
63  if (block_begin > block_end || block_end > block_count) {
64    return RTEMS_INVALID_ID;
65  }
66
67  for (block = block_begin; block != block_end; ++block) {
68    uint32_t page_begin = block * pages_per_block;
69    uint32_t page_end = page_begin + pages_per_block;
70    uint32_t page = 0;
71
72    sc = lpc32xx_mlc_erase_block_safe_3(block, page_begin, page_end);
73    if (sc != RTEMS_SUCCESSFUL) {
74      continue;
75    }
76
77    for (page = page_begin; page < page_end; ++page) {
78      uintptr_t remainder = (uintptr_t) end - (uintptr_t) current;
79      size_t delta = remainder < page_size ? remainder : page_size;
80
81      if (remainder > 0) {
82        memcpy(page_data_buffer, current, delta);
83        sc = lpc32xx_mlc_write_page_with_ecc(
84          page,
85          page_data_buffer,
86          ones_spare
87        );
88        if (sc != RTEMS_SUCCESSFUL) {
89          lpc32xx_mlc_erase_block(block);
90          lpc32xx_mlc_zero_pages(page_begin, page_end);
91          current = last;
92          continue;
93        }
94
95        current += delta;
96      } else {
97        goto done;
98      }
99    }
100
101    last = current;
102  }
103
104done:
105
106  if (current != end) {
107    return RTEMS_IO_ERROR;
108  }
109
110  return RTEMS_SUCCESSFUL;
111}
Note: See TracBrowser for help on using the repository browser.