Skip to content

Commit 34fb39b

Browse files
authored
Merge pull request #8 from asimgunes/NonSVD
An alternative way to load peripherals information
2 parents 4a80637 + 6d814f8 commit 34fb39b

18 files changed

Lines changed: 279 additions & 96 deletions

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ Once you have the SVD file, specify the location of it in your `launch.json` usi
7171
}
7272
```
7373

74+
### Extending Peripheral Inspector
75+
76+
It is possible to extend the Peripheral Inspector with new file extension providers in your VSCode extension. This method will provide reading new file formats and load the peripherals information into the Peripheral Inspector.
77+
78+
```json
79+
{
80+
...
81+
"definitionPath": "${workspaceFolder}/STM32F103.<customFileExtension>"
82+
...
83+
}
84+
```
85+
86+
For more details about the implementation, please check the [Extending Peripheral Inspector](./docs/extending-peripheral-inspector.md) document.
87+
7488
## Settings
7589

7690
All variable key names used to extract data from debug launch configurations can be modified. This allows variable name clashes to be avoided as well as the need to duplicate configuration entries.

api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src/api-types';
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Extending Peripheral Inspector
2+
3+
It is possible to extend the Peripheral Inspector with new file extension providers in your VSCode extension. This method will provide reading new file formats and load the peripherals information into the Peripheral Inspector.
4+
5+
## Building your VSCode Extension to extend Peripheral Inspector
6+
7+
This is a guide about how you can register new peripheral providers to Peripheral Inspector in your VSCode extension. Please refer to [VSCode Extension API](https://code.visualstudio.com/api) for more information about developing VSCode extensions.
8+
9+
### Adding Peripheral Inspector to your VSCode extension
10+
11+
You need to install eclipse-cdt-cloud/vscode-peripheral-inspector to access the types information. You can use `npm` or `yarn` with the following arguments described below:
12+
13+
Using with npm:
14+
```bash
15+
npm install github:eclipse-cdt-cloud/vscode-peripheral-inspector
16+
```
17+
Using with yarn:
18+
```bash
19+
yarn add github:eclipse-cdt-cloud/vscode-peripheral-inspector
20+
```
21+
22+
### Developing your extension
23+
24+
To provide the peripherals information to Peripheral Inspector on debug session time, you need register your command which is going to construct the peripherals information. The command will receive `DebugSession` object as an input parameter and expects to return array of type `PeripheralOptions[]`.
25+
26+
You can find the example command implementation below:
27+
28+
```js
29+
import { ExtensionContext } from 'vscode';
30+
import type * as api from "peripheral-inspector/api";
31+
32+
33+
class MyExtensionProvider implements api.IPeripheralsProvider {
34+
public async getPeripherals (data: string, options: api.IGetPeripheralsArguments): Promise<api.PeripheralOptions[]> {
35+
// Load your peripherals data
36+
const peripherals: api.PeripheralOptions[] = ...
37+
return peripherals;
38+
}
39+
}
40+
41+
export async function activate(context: ExtensionContext) {
42+
...
43+
// Get the eclipse-cdt.peripheral-inspector extension
44+
const peripheralInspectorExtention = extensions.getExtension<api.IPeripheralInspectorAPI>('eclipse-cdt.peripheral-inspector');
45+
46+
// Check if the eclipse-cdt.peripheral-inspector extension is installed
47+
if (peripheralInspectorExtention) {
48+
const peripheralInspectorAPI = await peripheralInspectorExtention.activate();
49+
50+
// Invoke registerPeripheralsProvider method in eclipse-cdt.peripheral-inspector extension api
51+
// Register 'MyExtensionProvider' for files *.myext
52+
peripheralInspectorAPI.registerPeripheralsProvider('myext', new MyExtensionProvider());
53+
}
54+
...
55+
}
56+
```
57+
58+
For further information about the api definitions, review the [Peripheral Inspector API Definitions](../src/api-types.ts).
59+
60+
### Modifying your package.json
61+
62+
In `package.json` of your VSCode extension project, you need to define the dependency between Peripheral Inspector and your extension.
63+
64+
You need to define Peripheral Inspector in the `extensionDependencies` as shown below:
65+
66+
```json
67+
{
68+
...
69+
"extensionDependencies": [
70+
"eclipse-cdt.peripheral-inspector"
71+
],
72+
...
73+
}
74+
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"license": "MIT",
1919
"main": "dist/desktop/extension.js",
2020
"browser": "dist/browser/extension.js",
21+
"types": "dist/desktop/extension.d.ts",
2122
"repository": "https://github.com/eclipse-cdt-cloud/vscode-peripheral-inspector",
2223
"qna": "https://github.com/eclipse-cdt-cloud/vscode-peripheral-inspector/issues",
2324
"icon": "media/cdtcloud.png",

src/api-types.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Peripheral Inspector API
2+
export interface IPeripheralInspectorAPI {
3+
registerSVDFile: (expression: RegExp | string, path: string) => void;
4+
getSVDFile: (device: string) => string | undefined;
5+
getSVDFileFromCortexDebug: (device: string) => Promise<string | undefined>;
6+
registerPeripheralsProvider: (fileExtension: string, provider: IPeripheralsProvider) => void;
7+
}
8+
9+
export interface IPeripheralsProvider {
10+
getPeripherals: (data: string, options: IGetPeripheralsArguments) => Promise<PeripheralOptions[]>;
11+
}
12+
13+
export interface IGetPeripheralsArguments {
14+
gapThreshold: number;
15+
}
16+
17+
export interface PeripheralOptions {
18+
name: string;
19+
baseAddress: number;
20+
totalLength: number;
21+
description: string;
22+
groupName?: string;
23+
accessType?: AccessType;
24+
size?: number;
25+
resetValue?: number;
26+
registers?: PeripheralRegisterOptions[];
27+
clusters?: ClusterOptions[];
28+
}
29+
30+
export interface PeripheralRegisterOptions {
31+
name: string;
32+
description?: string;
33+
addressOffset: number;
34+
accessType?: AccessType;
35+
size?: number;
36+
resetValue?: number;
37+
fields?: FieldOptions[];
38+
}
39+
40+
export interface ClusterOptions {
41+
name: string;
42+
description?: string;
43+
addressOffset: number;
44+
accessType?: AccessType;
45+
size?: number;
46+
resetValue?: number;
47+
registers?: PeripheralRegisterOptions[];
48+
clusters?: ClusterOptions[];
49+
}
50+
51+
export interface FieldOptions {
52+
name: string;
53+
description: string;
54+
offset: number;
55+
width: number;
56+
enumeration?: EnumerationMap;
57+
derivedFrom?: string; // Set this if unresolved
58+
accessType?: AccessType;
59+
}
60+
61+
export interface IGetPeripheralsArguments {
62+
gapThreshold: number;
63+
}
64+
65+
export interface IPeripheralsProvider {
66+
getPeripherals: (data: string, options: IGetPeripheralsArguments) => Promise<PeripheralOptions[]>;
67+
}
68+
69+
export enum AccessType {
70+
ReadOnly = 1,
71+
ReadWrite = 2,
72+
WriteOnly = 3
73+
}
74+
75+
export interface EnumerationMap {
76+
[value: number]: IEnumeratedValue;
77+
}
78+
79+
export interface IEnumeratedValue {
80+
name: string;
81+
description: string;
82+
value: number;
83+
}

src/browser/extension.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ import * as vscode from 'vscode';
99
import { PeripheralTreeProvider } from '../views/peripheral';
1010
import { Commands } from '../commands';
1111
import { DebugTracker } from '../debug-tracker';
12-
import { SvdRegistry } from '../svd-registry';
12+
import { PeripheralInspectorAPI } from '../peripheral-inspector-api';
1313
import { SvdResolver } from '../svd-resolver';
14+
import { IPeripheralInspectorAPI } from '../api-types';
15+
export * as api from '../api-types';
1416

15-
export const activate = async (context: vscode.ExtensionContext): Promise<SvdRegistry> => {
17+
export const activate = async (context: vscode.ExtensionContext): Promise<IPeripheralInspectorAPI> => {
1618
const tracker = new DebugTracker();
17-
const registry = new SvdRegistry();
18-
const resolver = new SvdResolver(registry);
19-
const peripheralTree = new PeripheralTreeProvider(tracker, resolver, context);
19+
const api = new PeripheralInspectorAPI();
20+
const resolver = new SvdResolver(api);
21+
const peripheralTree = new PeripheralTreeProvider(tracker, resolver, api, context);
2022
const commands = new Commands(peripheralTree);
2123

2224
await tracker.activate(context);
2325
await peripheralTree.activate();
2426
await commands.activate(context);
2527

26-
return registry;
28+
return api;
2729
};
2830

2931
export const deactivate = async (): Promise<void> => {

src/desktop/extension.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ import * as vscode from 'vscode';
99
import { PeripheralTreeProvider } from '../views/peripheral';
1010
import { Commands } from '../commands';
1111
import { DebugTracker } from '../debug-tracker';
12-
import { SvdRegistry } from '../svd-registry';
12+
import { PeripheralInspectorAPI } from '../peripheral-inspector-api';
1313
import { SvdResolver } from '../svd-resolver';
14+
import { IPeripheralInspectorAPI } from '../api-types';
15+
export * as api from '../api-types';
1416

15-
export const activate = async (context: vscode.ExtensionContext): Promise<SvdRegistry> => {
17+
export const activate = async (context: vscode.ExtensionContext): Promise<IPeripheralInspectorAPI> => {
1618
const tracker = new DebugTracker();
17-
const registry = new SvdRegistry();
18-
const resolver = new SvdResolver(registry);
19-
const peripheralTree = new PeripheralTreeProvider(tracker, resolver, context);
19+
const api = new PeripheralInspectorAPI();
20+
const resolver = new SvdResolver(api);
21+
const peripheralTree = new PeripheralTreeProvider(tracker, resolver, api, context);
2022
const commands = new Commands(peripheralTree);
2123

2224
await tracker.activate(context);
2325
await peripheralTree.activate();
2426
await commands.activate(context);
2527

26-
return registry;
28+
return api;
2729
};
2830

2931
export const deactivate = async (): Promise<void> => {

src/enumerated-value.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class EnumeratedValue {
2+
constructor(public name: string, public description: string, public value: number) {}
3+
}

src/memreadutils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class MemUtils {
3838
storeTo[dst++] = byte;
3939
}
4040
}
41-
} catch (e: unknown) {
41+
} catch (e: any) {
4242
const err = e ? e.toString() : 'Unknown error';
4343
errors.push(new Error(`readMemory failed @ ${memoryReference} for ${request.count} bytes: ${err}, session=${session.id}`));
4444
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
********************************************************************************/
77

88
import * as vscode from 'vscode';
9+
import { IPeripheralsProvider, IPeripheralInspectorAPI } from './api-types';
910

1011
const CORTEX_EXTENSION = 'marus25.cortex-debug';
1112

@@ -14,8 +15,9 @@ interface SVDInfo {
1415
path: string;
1516
}
1617

17-
export class SvdRegistry {
18+
export class PeripheralInspectorAPI implements IPeripheralInspectorAPI {
1819
private SVDDirectory: SVDInfo[] = [];
20+
private PeripheralProviders: Record<string, IPeripheralsProvider> = {};
1921

2022
public registerSVDFile(expression: RegExp | string, path: string): void {
2123
if (typeof expression === 'string') {
@@ -38,7 +40,7 @@ export class SvdRegistry {
3840
public async getSVDFileFromCortexDebug(device: string): Promise<string | undefined> {
3941
try {
4042
// Try loading from device support pack registered with this extension
41-
const cortexDebug = vscode.extensions.getExtension<SvdRegistry>(CORTEX_EXTENSION);
43+
const cortexDebug = vscode.extensions.getExtension<IPeripheralInspectorAPI>(CORTEX_EXTENSION);
4244
if (cortexDebug) {
4345
const cdbg = await cortexDebug.activate();
4446
if (cdbg) {
@@ -51,4 +53,13 @@ export class SvdRegistry {
5153

5254
return undefined;
5355
}
56+
57+
public registerPeripheralsProvider(fileExtension: string, provider: IPeripheralsProvider) {
58+
this.PeripheralProviders[fileExtension] = provider;
59+
}
60+
61+
public getPeripheralsProvider(svdPath: string) : IPeripheralsProvider | undefined {
62+
const ext = Object.keys(this.PeripheralProviders).filter((extension) => svdPath.endsWith(`.${extension}`))[0];
63+
return ext ? this.PeripheralProviders[ext] : undefined;
64+
}
5465
}

0 commit comments

Comments
 (0)