Skip to content

Commit

Permalink
NCD-343: Read configuration, merge with defaults, show in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
cybic committed Feb 6, 2024
1 parent f7b8657 commit 12cecb6
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 21 deletions.
23 changes: 23 additions & 0 deletions src/SidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ export default () => {
>
Write config
</Button>
<Button
disabled={!device}
variant="secondary"
className="w-100"
onClick={async () => {
console.dir(device);
if (!device) {
return;
}
const boardControllerConfig =
await NrfutilDeviceLib.getBoardControllerConfig(
device
);
console.dir(boardControllerConfig);
logger.info(
`Got boardControllerConfig: ${JSON.stringify(
boardControllerConfig
)}`
);
}}
>
Get BoardController config
</Button>
</CollapsibleGroup>
<CollapsibleGroup defaultCollapsed heading="Configuration data">
<ConfigDataPreview enabled />
Expand Down
24 changes: 22 additions & 2 deletions src/app/DeviceSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import {

import {
clearConfig,
clearHardwareConfig,
clearPmicConfig,
setHardwareConfig,
} from '../features/Configuration/boardControllerConfigSlice';
import { wrapHardwareConfig } from '../features/Configuration/hardwareConfiguration';
import {
clearBoardControllerFirmwareVersion,
clearBoardRevision,
Expand Down Expand Up @@ -59,9 +62,10 @@ const deviceListing: DeviceTraits = {
* Note that the callbacks releaseCurrentDevice and onDeviceIsReady
* are only invoked, if a deviceSetup is defined.
*/
const onDeviceSelected = (dispatch: AppDispatch) => (device: Device) => {
const onDeviceSelected = (dispatch: AppDispatch) => async (device: Device) => {
logger.info(`Selected device with s/n ${device.serialNumber}`);
getBoardControllerVersion(dispatch, device); // FIXME: Remove this when onDeviceIsReady() is called
await getBoardControllerVersion(dispatch, device); // FIXME: Remove this when onDeviceIsReady() is called
await getCurrentBoardControllerConfig(dispatch, device); // FIXME: Remove this when onDeviceIsReady() is called
};

const onDeviceIsReady = (dispatch: AppDispatch) => (device: Device) => {
Expand Down Expand Up @@ -91,12 +95,28 @@ const getBoardControllerVersion = async (
dispatch(setBoardControllerFirmwareVersion(bcVersion.bc_fw_ver));
};

const getCurrentBoardControllerConfig = async (
dispatch: AppDispatch,
device: Device
) => {
if (!device) {
return;
}
const currentConfig = await NrfutilDeviceLib.getBoardControllerConfig(
device
);
console.log('Read current config %o', currentConfig);
const wrappedConfig = wrapHardwareConfig(currentConfig);
dispatch(setHardwareConfig({ hardwareConfig: wrappedConfig }));
};

const onDeviceDeselected = (dispatch: AppDispatch) => () => {
logger.info('Deselected device');
dispatch(clearBoardRevision());
dispatch(clearBoardControllerFirmwareVersion());
dispatch(clearConfig());
dispatch(clearPmicConfig());
dispatch(clearHardwareConfig());
};

export default () => {
Expand Down
8 changes: 5 additions & 3 deletions src/features/ConfigDataPreview/ConfigDataPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import React from 'react';
import { useSelector } from 'react-redux';
import { logger } from '@nordicsemiconductor/pc-nrfconnect-shared';
import { HashType, logger } from '@nordicsemiconductor/pc-nrfconnect-shared';

import {
getConfigArray,
getConfigData,
getHardwareConfig,
getPmicConfigData,
} from '../Configuration/boardControllerConfigSlice';
import {
Expand All @@ -27,7 +28,6 @@ interface ConfigDataPreviewProps {
const ConfigDataPreview = ({ enabled = true }: ConfigDataPreviewProps) => {
logger.debug('Rendering ConfigDataPreview');

const configArray = useSelector(getConfigArray);
const configData = useSelector(getConfigData);
const pmicData = useSelector(getPmicConfigData);
const boardRevision = useSelector(getBoardRevisionSemver);
Expand All @@ -54,7 +54,9 @@ const ConfigDataPreview = ({ enabled = true }: ConfigDataPreviewProps) => {

{configData && configData.size > 0 && (
<>
<h2 className="heading">Toggles</h2>
<h2 className="heading">
Board Controller pin configuration
</h2>
<div className="config-block">
<table>{configList(configData)}</table>
</div>
Expand Down
69 changes: 53 additions & 16 deletions src/features/Configuration/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import ConfigSwitch from '../ConfigSwitch/ConfigSwitch';
import { getBoardRevisionSemver } from '../Device/deviceSlice';
import VCOMConfiguration from '../VCOMConfiguration/VCOMConfiguration';
import VoltageConfiguration from '../VoltageConfiguration/VoltageConfiguration';
import { setConfig, setPmicConfig } from './boardControllerConfigSlice';
import {
getHardwareConfig,
setConfig,
setPmicConfig,
} from './boardControllerConfigSlice';
import { BoardConfiguration } from './hardwareConfiguration';

interface BoardControllerProps {
active: boolean;
Expand Down Expand Up @@ -56,6 +61,7 @@ const BoardController = ({ active }: BoardControllerProps) => {

const device: Device | undefined = useSelector(selectedDevice);
const boardRevision = useSelector(getBoardRevisionSemver);
const hardwareConfig = useSelector(getHardwareConfig);

if (device) {
logDeviceVersion(device, boardRevision);
Expand All @@ -64,22 +70,38 @@ const BoardController = ({ active }: BoardControllerProps) => {
case 'PCA10156':
// nRF54L15
if (boardRevision === '0.3.0') {
setDefaultConfig(dispatch, typednrf54l15v030json);
setInitialConfig(
dispatch,
hardwareConfig,
typednrf54l15v030json
);
return BuildGui(typednrf54l15v030json);
}

// Default is revision 0.2.0 or 0.2.1
setDefaultConfig(dispatch, typednrf54l15v020json);
setInitialConfig(
dispatch,
hardwareConfig,
typednrf54l15v020json
);
return BuildGui(typednrf54l15v020json);

case 'PCA10153':
// nRF9161
if (boardRevision === '0.10.0') {
setDefaultConfig(dispatch, typednrf9161json);
setInitialConfig(
dispatch,
hardwareConfig,
typednrf9161json
);
return BuildGui(typednrf9161json);
}
if (boardRevision === '0.9.0' || boardRevision === '0.9.1') {
setDefaultConfig(dispatch, typednrf9161v091);
setInitialConfig(
dispatch,
hardwareConfig,
typednrf9161v091
);
return BuildGui(typednrf9161v091);
}
if (!boardRevision) {
Expand All @@ -90,7 +112,7 @@ const BoardController = ({ active }: BoardControllerProps) => {

case 'PCA10145':
// nRF54H20
setDefaultConfig(dispatch, typednrf54h20json);
setInitialConfig(dispatch, hardwareConfig, typednrf54h20json);
return BuildGui(typednrf54h20json);

default:
Expand Down Expand Up @@ -210,21 +232,36 @@ const BuildGui = (boardJson: BoardControllerConfigDefinition) => {
);
};

function setDefaultConfig(
function setInitialConfig(
dispatch: AppDispatch,
hardwareConfig: BoardConfiguration,
boardJson: BoardControllerConfigDefinition
) {
if (boardJson?.defaults) {
const { pins, pmicPorts } = boardJson.defaults;

const defaultConfig: Map<number, boolean> = new Map(pins);
const defaultPmicConfig: Map<number, number> = new Map(pmicPorts);

dispatch(setConfig({ boardControllerConfig: defaultConfig }));
dispatch(setPmicConfig({ pmicConfig: defaultPmicConfig }));
} else {
if (!boardJson?.defaults) {
logger.warn('No defaults found in board definition JSON');
}

// Create defaults map
const defaultConfig: Map<number, boolean> = new Map(
boardJson.defaults?.pins || []
);
const defaultPmicConfig: Map<number, number> = new Map(
boardJson.defaults?.pmicPorts || []
);

// Merge with currently read hardware config
const mergedPinConfig = new Map([
...defaultConfig,
...(hardwareConfig.pins || []),
]);

const mergedPmicConfig = new Map([
...defaultPmicConfig,
...(hardwareConfig.pmicPorts || []),
]);

dispatch(setConfig({ boardControllerConfig: mergedPinConfig }));
dispatch(setPmicConfig({ pmicConfig: mergedPmicConfig }));
}

const logDeviceVersion = (
Expand Down
21 changes: 21 additions & 0 deletions src/features/Configuration/boardControllerConfigSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
import { createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';

import type { RootState } from '../../app/appReducer';
import { BoardConfiguration } from './hardwareConfiguration';

interface ConfigState {
boardControllerConfigData: Map<number, boolean>;
pmicConfigData: Map<number, number>;
hardwareConfig: BoardConfiguration;
}

// nRF54H20 default config
const initialState: ConfigState = {
boardControllerConfigData: new Map([]),
// This might be needed pmicConfigData: new Map([[1, 1800]]),
pmicConfigData: new Map([]),
hardwareConfig: {},
};

const boardControllerConfigSlice = createSlice({
Expand Down Expand Up @@ -65,6 +68,19 @@ const boardControllerConfigSlice = createSlice({
clearPmicConfig(state) {
state.pmicConfigData.clear();
},

setHardwareConfig(
state,
{
payload: { hardwareConfig },
}: PayloadAction<{ hardwareConfig: BoardConfiguration }>
) {
state.hardwareConfig = hardwareConfig;
},

clearHardwareConfig(state) {
state.hardwareConfig = {};
},
},
});

Expand All @@ -86,6 +102,9 @@ export const getConfigData = (state: RootState) =>
export const getPmicConfigData = (state: RootState) =>
state.app.boardControllerConfig.pmicConfigData;

export const getHardwareConfig = (state: RootState) =>
state.app.boardControllerConfig.hardwareConfig;

export const getConfigArray = createSelector(
[getConfigData, getPmicConfigData],
(configData: Map<number, boolean>, pmicConfigData: Map<number, number>) => [
Expand Down Expand Up @@ -114,6 +133,8 @@ export const {
setPmicConfig,
setPmicConfigValue,
clearPmicConfig,
setHardwareConfig,
clearHardwareConfig,
} = boardControllerConfigSlice.actions;

export default boardControllerConfigSlice;
51 changes: 51 additions & 0 deletions src/features/Configuration/hardwareConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export interface BoardConfiguration {
pins?: Map<number, boolean>;
pmicPorts?: Map<number, number>;
}

export function wrapHardwareConfig(
hardwareConfig: unknown[][]
): BoardConfiguration {
if (hardwareConfig && hardwareConfig.length !== 0) {
const pins = wrapPinConfig(hardwareConfig[0]);

const pmicPorts = wrapPmicConfig(
hardwareConfig.length > 1 ? hardwareConfig[1] : undefined
);

return { pins, pmicPorts };
}
console.log('No current config to apply');
return {};
}

export function wrapPinConfig(hardwarePinConfig: unknown[]) {
const pins = new Map<number, boolean>();

// Zip the current config pins array - NB! step of 2
for (let i = 0; i < hardwarePinConfig.length; i += 2) {
pins.set(
Number(hardwarePinConfig[i]),
Boolean(hardwarePinConfig[i + 1])
);
}

return pins;
}

export function wrapPmicConfig(hardwarePmicConfig: unknown[] | undefined) {
const ports = new Map<number, number>();
if (hardwarePmicConfig === undefined) {
return ports;
}

// Zip the current PMIC config port array - NB! step of 2
for (let i = 0; i < hardwarePmicConfig.length; i += 2) {
ports.set(
Number(hardwarePmicConfig[i]),
Number(hardwarePmicConfig[i + 1])
);
}

return ports;
}

0 comments on commit 12cecb6

Please sign in to comment.