Skip to content

Commit

Permalink
[#110] Channel ID mandatory for channel commands; info and setup comp…
Browse files Browse the repository at this point in the history
…lete
  • Loading branch information
susanw1 committed Nov 18, 2023
1 parent 3be8a0e commit 0801033
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: I2C
name: I2c
id: 5
version: 0.0.1
description: I2C Connectivity

commands:
- name: capabilities
command: 0
command: 0x0
description: I2C capabilities
operation: get
requestFields: [ ]
Expand Down Expand Up @@ -58,7 +58,7 @@ commands:
status: [ ]

- name: i2cSetup
command: 1
command: 0x1
description: configures the I2C subsystem
operation: put
requestFields:
Expand Down Expand Up @@ -104,7 +104,7 @@ commands:
description: port or frequency out of range

- name: i2cSend
command: 2
command: 0x2
description: Sends data to a specified I2C device
operation: post
requestFields:
Expand Down Expand Up @@ -142,7 +142,7 @@ commands:
- key: I
name: resultInfo
description: detailed status info for I2C activity
typeDefinition:
typeDefinition: &result_status_bits
'@type': enum
values:
- Ok
Expand All @@ -164,7 +164,7 @@ commands:
description: write failed in bad way - serious error

- name: i2cReceive
command: 3
command: 0x3
description: Receives data from a specified I2C device
operation: post
requestFields:
Expand Down Expand Up @@ -202,14 +202,7 @@ commands:
- key: I
name: resultInfo
description: detailed status info for I2C activity
typeDefinition:
'@type': enum
values:
- Ok
- BufferOverrun
- AddrNack
- DataNack
- Other
typeDefinition: *result_status_bits
required: yes
- key: R
name: attempts
Expand All @@ -232,7 +225,7 @@ commands:
description: write failed in bad way - serious error

- name: i2cSendReceive
command: 4
command: 0x4
description: Sends a message to a specified I2C device, then performs a receive without an intervening stop bit
operation: post
requestFields:
Expand Down Expand Up @@ -276,14 +269,7 @@ commands:
- key: I
name: resultInfo
description: detailed status info for I2C activity
typeDefinition:
'@type': enum
values:
- Ok
- BufferOverrun
- AddrNack
- DataNack
- Other
typeDefinition: *result_status_bits
required: yes
- key: R
name: attempts
Expand All @@ -305,29 +291,23 @@ commands:


- name: channelInfo
command: 12
command: 0xc
description: gets detailed information about an I2C channel
operation: get
requestFields:
- key: C
name: channel
description: 'which channel to fetch info about (default: 0)'
description: 'which channel to fetch info about'
typeDefinition:
'@type': number
required: no
required: yes
responseFields:
- key: N
name: channelCount
description: the number of I2C channels supported by the device
typeDefinition:
'@type': number
required: yes
- key: C
name: channel
description: the global channel index of the requested channel
typeDefinition:
'@type': number
required: yes
- key: A
name: address
description: which address the channel operates on
Expand All @@ -343,7 +323,7 @@ commands:
status: [ ]

- name: channelSetup
command: 13
command: 0xd
description: sets detailed control variables on an I2C channel
operation: put
requestFields:
Expand All @@ -366,7 +346,7 @@ commands:

notifications:
- name: smBusAlert
notification: 0
notification: 0x0
description: a notification caused by an SMBus alert line being pulled low
condition: on an SMBus style alert
sections:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ commands:
typeDefinition:
'@type': bitset
bits:
- name: parityOn
description: enables sending parity bits
- name: parity
description: whether parity bits are supported
- name: doubleStop
description: whether the device supports choosing single or double stop bits
- name: autoBaud
Expand Down Expand Up @@ -108,13 +108,13 @@ commands:
- key: B
name: options
description: a set of more specific operational choices about the device's operation
typeDefinition:
typeDefinition: &setup_options
'@type': bitset
bits:
- name: parityOn
description: enables sending parity bits
- name: parityOdd
description: if parityOn, then is odd parity enabled
description: if parityOn, then enable odd parity
- name: doubleStop
description: selects double stop bits rather than single
- name: autoBaud
Expand Down Expand Up @@ -193,10 +193,10 @@ commands:
'@type': number
required: yes
responseFields:
- key: I
- key: M
name: serialMarker
description: the type of terminator ending the read
typeDefinition:
typeDefinition: &receive_markers
'@type': enum
values:
- Nothing
Expand All @@ -214,7 +214,7 @@ commands:
status: [ ]

- name: skip
command: 4
command: 0x4
description: Serial skip
operation: read
requestFields:
Expand All @@ -231,17 +231,10 @@ commands:
'@type': number
required: yes
responseFields:
- key: I
- key: M
name: serialMarker
description: the type of terminator ending the skip
typeDefinition:
'@type': enum
values:
- Nothing
- Break
- ParityError
- NoiseError
- BufferOverflow
typeDefinition: *receive_markers
required: yes
- key: L
name: length
Expand All @@ -261,7 +254,7 @@ commands:
description: 'which channel to fetch info about (default: 0)'
typeDefinition:
'@type': number
required: no
required: yes
- key: F
name: frequencySelection
description: 'which frequency option should be investigated (default: maximum)'
Expand All @@ -275,12 +268,6 @@ commands:
typeDefinition:
'@type': number
required: yes
- key: C
name: channel
description: the global channel index of the requested channel
typeDefinition:
'@type': number
required: yes
- key: I
name: interface
description: which interface the channel operates on
Expand All @@ -301,6 +288,8 @@ commands:
bits:
- name: parityOn
description: enables sending parity bits
- name: parityOdd
description: if parityOn, then odd parity enabled
- name: doubleStop
description: whether the device supports choosing single or double stop bits
- name: autoBaud
Expand Down Expand Up @@ -336,17 +325,7 @@ commands:
- key: B
name: options
description: a set of more specific operational choices about the device's operation
typeDefinition:
'@type': bitset
bits:
- name: parityOn
description: enables sending parity bits
- name: parityOdd
description: if parityOn, then is odd parity enabled (
- name: doubleStop
description: selects double stop bits rather than single
- name: autoBaud
description: enables automatic baud rate detection
typeDefinition: *setup_options
required: no
- key: +
name: baudRate
Expand All @@ -366,7 +345,7 @@ commands:

notifications:
- name: serialDataAvailable
notification: 0
notification: 0x0
description: a serial interface has data available
condition: on data exceeding some minimum level, or after data received and some timeout passed - not sent until after some kind of read received
sections:
Expand All @@ -389,7 +368,7 @@ notifications:
logicalTermination: end

- name: serialReceiveNearCapacity
notification: 1
notification: 0x1
description: a serial interface is almost overflowing its data capacity
condition: on data exceeding some proportion of the buffer (e.g. 75%) - not sent again until after the fullness drops below this level
sections:
Expand All @@ -400,14 +379,7 @@ notifications:
- key: M
name: serialMarker
description: the type of terminator ending the data
typeDefinition:
'@type': enum
values:
- Nothing
- Break
- ParityError
- NoiseError
- BufferOverflow
typeDefinition: *receive_markers
required: yes
- key: +
name: data
Expand All @@ -418,7 +390,7 @@ notifications:
logicalTermination: end

- name: serialReceiveOverflow
notification: 2
notification: 0x2
description: a serial interface has overflowed its receive buffer
condition: on data overflowing the buffer - not sent again until after the fullness drops below some level (e.g. 50%)
sections:
Expand All @@ -435,7 +407,7 @@ notifications:
logicalTermination: end

- name: serialReceiveData
notification: 3
notification: 0x3
description: a serial interface has data available, and this notification contains that data
condition: 'on any of: X bytes of data having arrived, Y milliseconds after the first data in the buffer arrived, or when a serial marker arrives, for reasonable chosen values of X,Y'
sections:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void shouldLoadAllBaseModules() {
ZscriptModel model = ZscriptModel.standardModel();
ModuleBank mb = model.getModuleBank(MODULEBANK_BASE_NAME).orElseThrow();
assertThat(mb.modules()).extracting(ModuleModel::getModuleName)
.containsExactlyInAnyOrder("Core", "OuterCore", "ScriptSpace", "Pins", "I2C", "Uart", "Servo");
.containsExactlyInAnyOrder("Core", "OuterCore", "ScriptSpace", "Pins", "I2c", "Uart", "Servo");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
#define SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_ZSCRIPTI2CCHANNELINFOCOMMAND_HPP_

#include <zscript/modules/ZscriptCommand.hpp>
#include <net/zscript/model/modules/base/I2cModule.hpp>
#include <Wire.h>

#define COMMAND_EXISTS_005c EXISTENCE_MARKER_UTIL

namespace Zscript {

template<class ZP>
class I2cModule;

template<class ZP>
class I2cChannel;

template<class ZP>
class I2cModule;
class ZscriptChannel;

template<class ZP>
class ZscriptI2cChannelInfoCommand {
public:
static constexpr uint8_t CODE = 0x0c;

static constexpr char ParamChannel__C = 'C';
static constexpr char ReqChannel__C = 'C';

static constexpr char RespChannelCount__N = 'N';
static constexpr char RespChannelGlobal__C = 'C';
Expand All @@ -34,17 +40,19 @@ class ZscriptI2cChannelInfoCommand {

static void execute(ZscriptCommandContext<ZP> ctx) {
uint16_t channelIndex;
if (ctx.getField(ParamChannel__C, &channelIndex)) {
if (channelIndex != 0) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}
if (!ctx.getReqdFieldCheckLimit(ReqChannel__C, Zscript<ZP>::zscript.getChannelCount(), &channelIndex)) {
return;
}
ZscriptChannel<ZP> *selectedChannel = Zscript<ZP>::zscript.getChannels()[channelIndex];
if (selectedChannel->getAssociatedModule() != i2c_module::MODULE_FULL_ID) {
ctx.status(ResponseStatus::VALUE_UNSUPPORTED);
return;
}

CommandOutStream<ZP> out = ctx.getOutStream();
// Just one I2C channel supported currently, always on port #0
out.writeField(RespChannelCount__N, 1);
out.writeField(RespChannelGlobal__C, I2cModule<ZP>::channel.getParser()->getChannelIndex());
out.writeField(RespAddress__A, I2cModule<ZP>::channel.getAddress());
out.writeField(RespInterface__I, 0);
}

};
Expand Down
Loading

0 comments on commit 0801033

Please sign in to comment.