OpenOCD
lattice_bit.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2022 by Daniel Anselmi *
5  * danselmi@gmx.ch *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 #include "lattice_bit.h"
12 #include "raw_bit.h"
13 #include "pld.h"
14 #include <helper/system.h>
15 #include <helper/log.h>
16 #include <helper/binarybuffer.h>
17 
24 };
25 
26 static int lattice_read_bit_file(struct lattice_bit_file *bit_file, const char *filename, enum lattice_family_e family)
27 {
28  int retval = cpld_read_raw_bit_file(&bit_file->raw_bit, filename);
29  if (retval != ERROR_OK)
30  return retval;
31 
32  bit_file->part = NULL;
33  bit_file->has_id = false;
35  for (size_t pos = 1; pos < bit_file->raw_bit.length && state != DONE; ++pos) {
36  switch (state) {
37  case SEEK_HEADER_START:
38  if (bit_file->raw_bit.data[pos] == 0 && bit_file->raw_bit.data[pos - 1] == 0xff)
40  break;
41  case SEEK_HEADER_END:
42  if (pos + 6 < bit_file->raw_bit.length &&
43  strncmp((const char *)(bit_file->raw_bit.data + pos), "Part: ", 6) == 0) {
44  bit_file->part = (const char *)bit_file->raw_bit.data + pos + 6;
45  LOG_INFO("part found: %s\n", bit_file->part);
46  } else if (bit_file->raw_bit.data[pos] == 0xff && bit_file->raw_bit.data[pos - 1] == 0) {
47  bit_file->offset = pos;
48  state = (family != LATTICE_ECP2 && family != LATTICE_ECP3) ? SEEK_PREAMBLE : DONE;
49  }
50  break;
51  case SEEK_PREAMBLE:
52  if (pos >= 4) {
53  uint32_t preamble = be_to_h_u32(bit_file->raw_bit.data + pos - 3);
54  switch (preamble) {
55  case 0xffffbdb3:
56  state = SEEK_ID;
57  break;
58  case 0xffffbfb3:
59  case 0xffffbeb3:
60  state = DONE;
61  break;
62  }
63  }
64  break;
65  case SEEK_ID:
66  if (pos + 7 < bit_file->raw_bit.length && bit_file->raw_bit.data[pos] == 0xe2) {
67  bit_file->idcode = be_to_h_u32(&bit_file->raw_bit.data[pos + 4]);
68  bit_file->has_id = true;
69  state = DONE;
70  }
71  break;
72  default:
73  break;
74  }
75  }
76 
77  if (state != DONE) {
78  LOG_ERROR("parsing bitstream failed");
80  }
81 
82  for (size_t i = bit_file->offset; i < bit_file->raw_bit.length; i++)
83  bit_file->raw_bit.data[i] = flip_u32(bit_file->raw_bit.data[i], 8);
84 
85  return ERROR_OK;
86 }
87 
88 int lattice_read_file(struct lattice_bit_file *bit_file, const char *filename, enum lattice_family_e family)
89 {
90  if (!filename || !bit_file)
92 
93  /* check if binary .bin or ascii .bit/.hex */
94  const char *file_suffix_pos = strrchr(filename, '.');
95  if (!file_suffix_pos) {
96  LOG_ERROR("Unable to detect filename suffix");
98  }
99 
100  if (strcasecmp(file_suffix_pos, ".bit") == 0)
101  return lattice_read_bit_file(bit_file, filename, family);
102 
103  LOG_ERROR("Filetype not supported");
105 }
uint32_t flip_u32(uint32_t value, unsigned int num)
Definition: binarybuffer.c:166
Support functions to access arbitrary bits in a byte array.
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
read_bit_state
Definition: lattice_bit.c:18
@ SEEK_HEADER_END
Definition: lattice_bit.c:20
@ DONE
Definition: lattice_bit.c:23
@ SEEK_HEADER_START
Definition: lattice_bit.c:19
@ SEEK_ID
Definition: lattice_bit.c:22
@ SEEK_PREAMBLE
Definition: lattice_bit.c:21
static int lattice_read_bit_file(struct lattice_bit_file *bit_file, const char *filename, enum lattice_family_e family)
Definition: lattice_bit.c:26
int lattice_read_file(struct lattice_bit_file *bit_file, const char *filename, enum lattice_family_e family)
Definition: lattice_bit.c:88
lattice_family_e
Definition: lattice_bit.h:23
@ LATTICE_ECP3
Definition: lattice_bit.h:25
@ LATTICE_ECP2
Definition: lattice_bit.h:24
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define ERROR_OK
Definition: log.h:164
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:62
int cpld_read_raw_bit_file(struct raw_bit_file *bit_file, const char *filename)
Definition: raw_bit.c:19
uint32_t idcode
Definition: lattice_bit.h:18
const char * part
Definition: lattice_bit.h:19
struct raw_bit_file raw_bit
Definition: lattice_bit.h:16
uint8_t * data
Definition: raw_bit.h:16
size_t length
Definition: raw_bit.h:15
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
#define NULL
Definition: usb.h:16
uint8_t state[4]
Definition: vdebug.c:21