Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NCD-747: Refactor definition loading, add "Reset to default" button #58

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/SidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import {
Button,
CollapsibleGroup,
Expand All @@ -16,15 +16,23 @@ import {
import { NrfutilDeviceLib } from '@nordicsemiconductor/pc-nrfconnect-shared/nrfutil/device';

import ConfigDataPreview from './features/ConfigDataPreview/ConfigDataPreview';
import { getConfigArray } from './features/Configuration/boardControllerConfigSlice';
import {
getConfigArray,
getDefaultConfig,
setConfig,
setPmicConfig,
} from './features/Configuration/boardControllerConfigSlice';

export default () => {
logger.debug('Rendering SidePanel');

const [isWriting, setWriting] = useState(false);

const dispatch = useDispatch();

const device = useSelector(selectedDevice);
const configData = useSelector(getConfigArray);
const defaultConfig = useSelector(getDefaultConfig);

return (
<SidePanel className="side-panel">
Expand All @@ -50,6 +58,31 @@ export default () => {
>
Write config
</Button>
<Button
disabled={!device || isWriting || !defaultConfig}
variant="secondary"
className="tw-w-full"
onClick={() => {
logger.info('Reset to default');
const { pins, pmicPorts } = defaultConfig;
if (pins) {
dispatch(
setConfig({
boardControllerConfig: pins,
})
);
}
if (pmicPorts) {
dispatch(
setPmicConfig({
pmicConfig: pmicPorts,
})
);
}
}}
>
Load default config
</Button>
</CollapsibleGroup>
<CollapsibleGroup defaultCollapsed heading="Configuration data">
<ConfigDataPreview enabled />
Expand Down
2 changes: 2 additions & 0 deletions src/app/DeviceSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {

import {
clearConfig,
clearDefaultConfig,
clearHardwareConfig,
clearPmicConfig,
setHardwareConfig,
Expand Down Expand Up @@ -114,6 +115,7 @@ const onDeviceDeselected = (dispatch: AppDispatch) => () => {
dispatch(clearConfig());
dispatch(clearPmicConfig());
dispatch(clearHardwareConfig());
dispatch(clearDefaultConfig());
};

export default () => {
Expand Down
135 changes: 55 additions & 80 deletions src/features/Configuration/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,26 @@ import {
} from '@nordicsemiconductor/pc-nrfconnect-shared';

import BoardControllerConfigDefinition from '../../common/boards/BoardControllerConfigDefinition';
import nrf54h20json from '../../common/boards/nrf_PCA10145_54H20.json';
import nrf9161v091json from '../../common/boards/nrf_PCA10153_0.9.1_9161.json';
import nrf9161v0100json from '../../common/boards/nrf_PCA10153_0.10.0_9161.json';
import nrf54l15v020json from '../../common/boards/nrf_PCA10156_0.2.0.json';
import nrf54l15v030json from '../../common/boards/nrf_PCA10156_0.3.0.json';
import nrf9151v020json from '../../common/boards/nrf_PCA10171_0.2.0_9151.json';
import ConfigSlideSelector from '../ConfigSlideSelector/ConfigSlideSelector';
import ConfigSwitch from '../ConfigSwitch/ConfigSwitch';
import { getBoardRevisionSemver } from '../Device/deviceSlice';
import VCOMConfiguration from '../VCOMConfiguration/VCOMConfiguration';
import VoltageConfiguration from '../VoltageConfiguration/VoltageConfiguration';
import {
getDefaultConfig,
getHardwareConfig,
setConfig,
setDefaultConfig,
setPmicConfig,
} from './boardControllerConfigSlice';
import { getBoardDefinition } from './boardDefinitions';
import { BoardConfiguration } from './hardwareConfiguration';

interface BoardControllerProps {
active: boolean;
}

const BoardController = ({ active }: BoardControllerProps) => {
const typednrf9161json =
nrf9161v0100json as BoardControllerConfigDefinition;
const typednrf9161v091 = nrf9161v091json as BoardControllerConfigDefinition;
const typednrf54l15v020json =
nrf54l15v020json as BoardControllerConfigDefinition;
const typednrf54l15v030json =
nrf54l15v030json as BoardControllerConfigDefinition;
const typednrf54h20json = nrf54h20json as BoardControllerConfigDefinition;
const typednrf9151v020json =
nrf9151v020json as BoardControllerConfigDefinition;

const dispatch = useDispatch();

useEffect(() => {
Expand All @@ -65,74 +51,36 @@ const BoardController = ({ active }: BoardControllerProps) => {
const device: Device | undefined = useSelector(selectedDevice);
const boardRevision = useSelector(getBoardRevisionSemver);
const hardwareConfig = useSelector(getHardwareConfig);
const defaultConfig = useSelector(getDefaultConfig);

if (device) {
logDeviceVersion(device, boardRevision);

switch (device?.devkit?.boardVersion) {
case 'PCA10156':
// nRF54L15
if (boardRevision === '0.3.0') {
setInitialConfig(
dispatch,
hardwareConfig,
typednrf54l15v030json
);
return BuildGui(typednrf54l15v030json);
}
const definition = getBoardDefinition(device, boardRevision);

// Default is revision 0.2.0 or 0.2.1
setInitialConfig(
if (definition.boardControllerConfigDefinition) {
if (!defaultConfig.pins && !defaultConfig.pmicPorts) {
loadDefaultConfig(
dispatch,
hardwareConfig,
typednrf54l15v020json
definition.boardControllerConfigDefinition
);
return BuildGui(typednrf54l15v020json);

case 'PCA10153':
// nRF9161
if (boardRevision === '0.10.0') {
setInitialConfig(
dispatch,
hardwareConfig,
typednrf9161json
);
return BuildGui(typednrf9161json);
}
if (boardRevision === '0.9.0' || boardRevision === '0.9.1') {
setInitialConfig(
dispatch,
hardwareConfig,
typednrf9161v091
);
return BuildGui(typednrf9161v091);
}
if (!boardRevision) {
return Spinner();
}

return UnrecognizedBoardRevision();

case 'PCA10145':
// nRF54H20
setInitialConfig(dispatch, hardwareConfig, typednrf54h20json);
return BuildGui(typednrf54h20json);
}
setInitialConfig(dispatch, hardwareConfig, defaultConfig);
return BuildGui(definition.boardControllerConfigDefinition);
}

case 'PCA10171':
// nRF9151
setInitialConfig(
dispatch,
hardwareConfig,
typednrf9151v020json
);
return BuildGui(typednrf9151v020json);
if (definition.controlFlag?.noRevision) {
return Spinner();
}

default:
return UnrecognizedBoard();
if (definition.controlFlag?.unknownRevision) {
return UnrecognizedBoardRevision();
}
} else {
return NoBoardSelected();

return UnrecognizedBoard();
}

return NoBoardSelected();
};

const NoBoardSelected = () => (
Expand All @@ -149,7 +97,16 @@ const NoBoardSelected = () => (
target="_blank"
rel="noreferrer"
>
nRF9161DK (Rev. 0.9.0 and later)
nRF9161-DK (Rev. 0.9.0 and later)
</a>
</li>
<li>
<a
href="https://www.nordicsemi.com/Products/nRF9151"
target="_blank"
rel="noreferrer"
>
nRF9151-DK
</a>
</li>
</ul>
Expand All @@ -172,7 +129,7 @@ const UnrecognizedBoardRevision = () => (
<div>
<p>
This revision of the development kit is not supported. Update to the
latest version of nRF Connect Board Configurator // FIXME
latest version of nRF Connect Board Configurator
</p>
</div>
);
Expand Down Expand Up @@ -247,13 +204,13 @@ const BuildGui = (boardJson: BoardControllerConfigDefinition) => {
);
};

function setInitialConfig(
function loadDefaultConfig(
dispatch: AppDispatch,
hardwareConfig: BoardConfiguration,
boardJson: BoardControllerConfigDefinition
) {
if (!boardJson?.defaults) {
logger.warn('No defaults found in board definition JSON');
return;
}

// Create defaults map
Expand All @@ -267,14 +224,32 @@ function setInitialConfig(
])
);

dispatch(
setDefaultConfig({
defaultConfig: {
pins: defaultConfig,
pmicPorts: defaultPmicConfig,
},
})
);
}

function setInitialConfig(
dispatch: AppDispatch,
hardwareConfig: BoardConfiguration,
defaultBoardConfig: BoardConfiguration
) {
const { pins: defaultPinConfig, pmicPorts: defaultPmicConfig } =
defaultBoardConfig;

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

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

Expand Down
19 changes: 19 additions & 0 deletions src/features/Configuration/boardControllerConfigSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface ConfigState {
boardControllerConfigData: Map<number, boolean>;
pmicConfigData: Map<number, number>;
hardwareConfig: BoardConfiguration;
defaultConfig: BoardConfiguration;
}

// nRF54H20 default config
Expand All @@ -21,6 +22,7 @@ const initialState: ConfigState = {
// This might be needed pmicConfigData: new Map([[1, 1800]]),
pmicConfigData: new Map([]),
hardwareConfig: {},
defaultConfig: {},
};

const boardControllerConfigSlice = createSlice({
Expand Down Expand Up @@ -81,6 +83,18 @@ const boardControllerConfigSlice = createSlice({
clearHardwareConfig(state) {
state.hardwareConfig = {};
},
setDefaultConfig(
state,
{
payload: { defaultConfig },
}: PayloadAction<{ defaultConfig: BoardConfiguration }>
) {
state.defaultConfig = defaultConfig;
},

clearDefaultConfig(state) {
state.defaultConfig = {};
},
},
});

Expand All @@ -105,6 +119,9 @@ export const getPmicConfigData = (state: RootState) =>
export const getHardwareConfig = (state: RootState) =>
state.app.boardControllerConfig.hardwareConfig;

export const getDefaultConfig = (state: RootState) =>
state.app.boardControllerConfig.defaultConfig;

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

export default boardControllerConfigSlice;
Loading