Skip to content

Commit

Permalink
[refactoring] Clean-up for stlink-lib
Browse files Browse the repository at this point in the history
- Ensure proper function declaration
- Checked & revised header includes
  • Loading branch information
Nightwalker-87 committed Jun 9, 2023
1 parent be2e7e3 commit fc99064
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 72 deletions.
2 changes: 0 additions & 2 deletions src/stlink-lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#ifndef CALCULATE_H
#define CALCULATE_H

#include <stdint.h>

uint32_t calculate_F4_sectornum(uint32_t);
uint32_t calculate_F7_sectornum(uint32_t);
uint32_t calculate_H7_sectornum(stlink_t *, uint32_t, unsigned);
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/chipid.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ void init_chipids(char *dir_to_scan) {
return;
}
}

#endif // STLINK_HAVE_DIRENT_H

#if defined(_WIN32) && !defined(STLINK_HAVE_DIRENT_H)
Expand Down
7 changes: 4 additions & 3 deletions src/stlink-lib/chipid.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#ifndef CHIPID_H
#define CHIPID_H

#include <stdint.h>

/* Chipid parametres */
struct stlink_chipid_params {
char *dev_type;
Expand All @@ -27,6 +25,9 @@ struct stlink_chipid_params {
};

struct stlink_chipid_params *stlink_chipid_get_params(uint32_t chipid);
void init_chipids(char *dir_to_scan);

void dump_a_chip(struct stlink_chipid_params *dev);
void process_chipfile(char *fname);
void init_chipids(char *dir_to_scan);

#endif // CHIPID_H
2 changes: 0 additions & 2 deletions src/stlink-lib/common_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#ifndef COMMON_FLASH_H
#define COMMON_FLASH_H

#include <stdint.h>

#define BANK_1 0
#define BANK_2 1

Expand Down
2 changes: 0 additions & 2 deletions src/stlink-lib/flash_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#ifndef FLASH_LOADER_H
#define FLASH_LOADER_H

#include <stdint.h>

int32_t stlink_flash_loader_init(stlink_t *sl, flash_loader_t* fl);
// static int32_t loader_v_dependent_assignment(stlink_t *sl,
// const uint8_t **loader_code, size_t *loader_size,
Expand Down
5 changes: 2 additions & 3 deletions src/stlink-lib/helper.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */

/*
* File: helper.c
*
* General helper functions
*/

#ifdef STLINK_HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <sys_time.h>
#endif
#endif // STLINK_HAVE_SYS_TIME_H

#include <stdint.h>
#include <stddef.h>
Expand Down
5 changes: 0 additions & 5 deletions src/stlink-lib/helper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */

/*
* File: helper.h
*
Expand All @@ -9,10 +7,7 @@
#ifndef HELPER_H
#define HELPER_H

#include <stdint.h>

uint32_t time_ms();

int32_t arg_parse_freq(const char *str);

#endif // HELPER_H
1 change: 1 addition & 0 deletions src/stlink-lib/lib_md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <stdint.h>
#include <stdio.h>
#include <memory.h>

#include "lib_md5.h"
Expand Down
3 changes: 0 additions & 3 deletions src/stlink-lib/lib_md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

#pragma once

#include <stdint.h>
#include <stdio.h>

/* TYPES */

/* Md5Context
Expand Down
10 changes: 4 additions & 6 deletions src/stlink-lib/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
#ifndef LOGGING_H
#define LOGGING_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif
#endif // __cplusplus

enum ugly_loglevel {
UDEBUG = 90,
Expand All @@ -25,7 +23,7 @@ enum ugly_loglevel {
#define PRINTF_ARRT __attribute__ ((format (printf, 3, 4)))
#else
#define PRINTF_ARRT
#endif
#endif // __GNUC__

int32_t ugly_init(int32_t maximum_threshold);
int32_t ugly_log(int32_t level, const char *tag, const char *format, ...) PRINTF_ARRT;
Expand All @@ -48,6 +46,6 @@ int32_t ugly_libusb_log_level(enum ugly_loglevel v);

#ifdef __cplusplus
}
#endif
#endif // __cplusplus

#endif // LOGGING_H
#endif // LOGGING_H
6 changes: 2 additions & 4 deletions src/stlink-lib/map_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
#ifndef MAP_FILE_H
#define MAP_FILE_H

#include <stdint.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif
#endif // O_BINARY

#ifdef STLINK_HAVE_SYS_MMAN_H
#include <sys/mman.h>
#else
#include <mmap.h>
#endif
#endif // STLINK_HAVE_SYS_MMAN_H

/* Memory mapped file */
typedef struct mapped_file {
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <stdint.h>
#include <stdio.h>

#include <stlink.h>
#include "md5.h"
Expand Down
2 changes: 0 additions & 2 deletions src/stlink-lib/md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#ifndef MD5_H
#define MD5_H

#include <stdint.h>

#include "map_file.h"

void md5_calculate(mapped_file_t *);
Expand Down
3 changes: 1 addition & 2 deletions src/stlink-lib/option_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ int32_t stlink_read_option_bytes_boot_add_f7(stlink_t *sl, uint32_t *option_byte
* @param option_byte
* @return 0 on success, -ve on failure.
*/
static int
stlink_write_option_bytes_boot_add_f7(stlink_t *sl, uint32_t option_byte_boot_add) {
static int32_t stlink_write_option_bytes_boot_add_f7(stlink_t *sl, uint32_t option_byte_boot_add) {
ILOG("Asked to write option byte boot add %#010x.\n", option_byte_boot_add);
return stlink_write_option_control_register1_f7(sl, option_byte_boot_add);
}
Expand Down
44 changes: 33 additions & 11 deletions src/stlink-lib/option_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,41 @@
#ifndef OPTION_BYTES_H
#define OPTION_BYTES_H

#include <stdint.h>

int32_t stlink_read_option_control_register_f0(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_bytes_f0(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t len);
// static int32_t stlink_write_option_control_register_f0(stlink_t *sl, uint32_t option_cr);
int32_t stlink_read_option_control_register_f2(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_read_option_bytes_f2(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_read_option_control_register_f4(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_read_option_bytes_f4(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_bytes_f4(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
int32_t stlink_read_option_bytes_f7(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_bytes_f7(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
int32_t stlink_read_option_control_register_f7(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_control_register_f7(stlink_t *sl, uint32_t option_cr);
int32_t stlink_read_option_control_register1_f7(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_control_register1_f7(stlink_t *sl, uint32_t option_cr1);
int32_t stlink_read_option_bytes_boot_add_f7(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_bytes_boot_add_f7(stlink_t *sl, uint32_t option_byte_boot_add);
int32_t stlink_read_option_control_register_gx(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_read_option_bytes_gx(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_bytes_gx(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
// static int32_t stlink_write_option_bytes_h7(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
// static int32_t stlink_write_option_bytes_l0(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
// static int32_t stlink_write_option_bytes_l4(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
// static int32_t stlink_write_option_bytes_wb(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
int32_t stlink_read_option_control_register_wb(stlink_t *sl, uint32_t *option_byte);
// static int32_t stlink_write_option_control_register_wb(stlink_t *sl, uint32_t option_cr);
int32_t stlink_read_option_bytes_generic(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_write_option_bytes(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len);
int32_t stlink_fwrite_option_bytes(stlink_t *sl, const char *path, stm32_addr_t addr);
int32_t stlink_read_option_control_register32(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_write_option_control_register32(stlink_t *sl, uint32_t option_cr);
int32_t stlink_read_option_control_register1_32(stlink_t *sl, uint32_t *option_byte);
int32_t stlink_write_option_control_register1_32(stlink_t *sl, uint32_t option_cr1);
int32_t stlink_read_option_bytes32(stlink_t *sl, uint32_t* option_byte);
int32_t stlink_read_option_bytes_boot_add32(stlink_t *sl, uint32_t* option_byte);
int32_t stlink_read_option_control_register32(stlink_t *sl, uint32_t* option_byte);
int32_t stlink_read_option_control_register1_32(stlink_t *sl, uint32_t* option_byte);

int32_t stlink_write_option_bytes32(stlink_t *sl, uint32_t option_byte);
int32_t stlink_read_option_bytes_boot_add32(stlink_t *sl, uint32_t* option_byte);
int32_t stlink_write_option_bytes_boot_add32(stlink_t *sl, uint32_t option_bytes_boot_add);
int32_t stlink_write_option_control_register32(stlink_t *sl, uint32_t option_cr);
int32_t stlink_write_option_control_register1_32(stlink_t *sl, uint32_t option_cr1);

int32_t stlink_write_option_bytes(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t len);
int32_t stlink_fwrite_option_bytes(stlink_t *sl, const char* path, stm32_addr_t addr);

#endif // OPTION_BYTES_H
2 changes: 1 addition & 1 deletion src/stlink-lib/register.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@
#define STLINK_REG_CM7_ICIALLU 0xE000EF50
#define STLINK_REG_CM7_CCSIDR 0xE000ED80

#endif // REG_H
#endif // REGISTER_H
12 changes: 7 additions & 5 deletions src/stlink-lib/sg.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */

/*
* Copyright (c) 2010 "Capt'ns Missing Link" Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
Expand Down Expand Up @@ -87,15 +89,15 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// #include <sys/types.h>
// #include <sys/types.h> // TODO: Check use

#include "sg.h"

#include "commands.h"
#include "logging.h"
#include "register.h"
#include "usb.h"
// #include <stlink.h>
// #include <stlink.h> // TODO: Check use

#define STLINK_OK 0x80
#define STLINK_FALSE 0x81
Expand Down Expand Up @@ -150,6 +152,7 @@ static int32_t get_usb_mass_storage_status(libusb_device_handle *handle, uint8_t
uint32_t rsig = read_uint32(csw, 0);
uint32_t rtag = read_uint32(csw, 4);
/* uint32_t residue = read_uint32(csw, 8); */

#define USB_CSW_SIGNATURE 0x53425355 // 'U' 'S' 'B' 'S' (reversed)

if (rsig != USB_CSW_SIGNATURE) {
Expand Down Expand Up @@ -187,9 +190,8 @@ static int32_t dump_CDB_command(uint8_t *cdb, uint8_t cdb_len) {
* @param expected_rx_size
* @return
*/
int32_t send_usb_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint_out,
uint8_t *cdb, uint8_t cdb_length,
uint8_t lun, uint8_t flags, uint32_t expected_rx_size) {
int32_t send_usb_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint_out, uint8_t *cdb, uint8_t cdb_length,
uint8_t lun, uint8_t flags, uint32_t expected_rx_size) {
DLOG("Sending usb m-s cmd: cdblen:%d, rxsize=%d\n", cdb_length, expected_rx_size);
dump_CDB_command(cdb, cdb_length);

Expand Down
42 changes: 42 additions & 0 deletions src/stlink-lib/sg.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */

/*
* File: sg.h
*
Expand Down Expand Up @@ -58,6 +60,46 @@ struct stlink_libsg {
struct stlink_reg reg;
};

// static void clear_cdb(struct stlink_libsg *sl);
void _stlink_sg_close(stlink_t *sl);
// static int32_t get_usb_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t *tag);
// static int32_t dump_CDB_command(uint8_t *cdb, uint8_t cdb_len);
int32_t send_usb_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint_out, uint8_t *cdb, uint8_t cdb_length,
uint8_t lun, uint8_t flags, uint32_t expected_rx_size);
// static void get_sense(libusb_device_handle *handle, uint8_t endpoint_in, uint8_t endpoint_out);
int32_t send_usb_data_only(libusb_device_handle *handle, unsigned char endpoint_out,
unsigned char endpoint_in, unsigned char *cbuf, uint32_t length);
int32_t stlink_q(stlink_t *sl);
void stlink_stat(stlink_t *stl, char *txt);
int32_t _stlink_sg_version(stlink_t *stl);
int32_t _stlink_sg_current_mode(stlink_t *stl);
int32_t _stlink_sg_enter_swd_mode(stlink_t *sl);
int32_t _stlink_sg_enter_jtag_mode(stlink_t *sl);
int32_t _stlink_sg_exit_dfu_mode(stlink_t *sl);
int32_t _stlink_sg_core_id(stlink_t *sl);
int32_t _stlink_sg_reset(stlink_t *sl);
int32_t _stlink_sg_jtag_reset(stlink_t *sl, int32_t value);
int32_t _stlink_sg_status(stlink_t *sl);
int32_t _stlink_sg_force_debug(stlink_t *sl);
int32_t _stlink_sg_read_all_regs(stlink_t *sl, struct stlink_reg *regp);
int32_t _stlink_sg_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp);
int32_t _stlink_sg_write_reg(stlink_t *sl, uint32_t reg, int32_t idx);
void stlink_write_dreg(stlink_t *sl, uint32_t reg, uint32_t addr);
int32_t _stlink_sg_run(stlink_t *sl, enum run_type type);
int32_t _stlink_sg_step(stlink_t *sl);
void stlink_set_hw_bp(stlink_t *sl, int32_t fp_nr, uint32_t addr, int32_t fp);
void stlink_clr_hw_bp(stlink_t *sl, int32_t fp_nr);
int32_t _stlink_sg_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
int32_t _stlink_sg_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len);
int32_t _stlink_sg_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
int32_t _stlink_sg_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data);
int32_t _stlink_sg_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data);
int32_t _stlink_sg_exit_debug_mode(stlink_t *stl);

// static stlink_backend_t _stlink_sg_backend = { };

// static stlink_t* stlink_open(const int32_t verbose);
stlink_t* stlink_v1_open_inner(const int32_t verbose);
stlink_t* stlink_v1_open(const int32_t verbose, int32_t reset);

#endif // SG_H

0 comments on commit fc99064

Please sign in to comment.