Skip to content

Commit

Permalink
Initial work on SPI support.
Browse files Browse the repository at this point in the history
  • Loading branch information
slav-at-attachix committed Aug 7, 2020
1 parent 756ff4b commit 9e06b04
Show file tree
Hide file tree
Showing 14 changed files with 534 additions and 21 deletions.
71 changes: 71 additions & 0 deletions Sming/Components/Hosted-Lib/src/SpiHosted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <SPI.h>
#include <HostedClient.h>
#include <Data/Stream/MemoryDataStream.h>

extern HostedClient* hostedClient;

// define the static singleton
SPIClass SPI;

void SPIClass::begin()
{
NEW_HD_COMMAND(message, SpiBeginTransaction, {
command->has_settings = 0;
});

hostedClient->send(&message);
}

void SPIClass::beginTransaction(SPISettings mySettings)
{
NEW_HD_COMMAND(message, SpiBeginTransaction, {
command->has_settings = 1;
command->settings.speed = mySettings.speed;
command->settings.byteOrder = (SpiSettings_ByteOrder)mySettings.byteOrder;
command->settings.dataMode = (SpiSettings_DataMode)mySettings.dataMode;
});

hostedClient->send(&message);
}

/** @brief transfer(uint8_t *buffer, size_t numberBytes)
* @param buffer in/out
* @param numberBytes length of buffer
*
* SPI transfer is based on a simultaneous send and receive:
* The buffered transfers does split up the conversation internaly into 64 byte blocks.
* The received data is stored in the buffer passed by reference.
* (the data past in is replaced with the data received).
*
* SPI.transfer(buffer, size) : memory buffer of length size
*/
void SPIClass::transfer(uint8_t* buffer, size_t numberBytes)
{
PbData data;
data.value = buffer;
data.length = numberBytes;

NEW_HD_COMMAND(message, SpiTransfer, {
command->data.arg = (void*)&data;
command->data.funcs.encode = &pbEncodeData;
});

hostedClient->send(&message);
HostedCommand response = hostedClient->wait();

MemoryDataStream* resultData = (MemoryDataStream*)response.payload.responseSpiTransfer.data.arg;
if(resultData == nullptr) {
memset(buffer, 0, numberBytes);
return;
}

resultData->readBytes((char *)buffer, numberBytes);
delete resultData;
}


uint32_t SPIClass::transfer32(uint32_t val, uint8_t bits)
{
// TODO:
return 0;
}
29 changes: 11 additions & 18 deletions Sming/Components/Hosted/app/app/application.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <SmingCore.h>
#include <HostedServer.h>
#include <Digital.h>

// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
Expand All @@ -11,6 +10,15 @@
HostedServer hostedServer;
TcpServer* tcpServer;

namespace Hosted {
namespace Digital {
void registerCommands(HostedServer& server);
}
namespace Spi {
void registerCommands(HostedServer& server);
}
}

// Will be called when WiFi station was connected to AP
void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway)
{
Expand Down Expand Up @@ -39,24 +47,9 @@ void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway)

void init()
{
// Register Command Handlers
hostedServer.registerCommand(HostedCommand_requestPinMode_tag, [](HostedCommand *request, HostedCommand *response)-> int {
pinMode((uint16_t)request->payload.requestPinMode.pin, (uint8_t)request->payload.requestPinMode.mode);
return 0;
});
Hosted::Digital::registerCommands(hostedServer);
Hosted::Spi::registerCommands(hostedServer);

hostedServer.registerCommand(HostedCommand_requestDigitalWrite_tag, [](HostedCommand *request, HostedCommand *response)-> int {
digitalWrite((uint16_t)request->payload.requestDigitalWrite.pin, (uint8_t)request->payload.requestDigitalWrite.value);
return 0;
});

hostedServer.registerCommand(HostedCommand_requestDigitalRead_tag, [](HostedCommand *request, HostedCommand *response)-> int {
uint8_t result = digitalRead((uint16_t)request->payload.requestDigitalRead.pin);
response->which_payload = HostedCommand_responseDigitalRead_tag;
response->payload.responseDigitalRead.value = result;

return 0;
});

// Connect to same AP as the client application
WifiStation.enable(true);
Expand Down
3 changes: 3 additions & 0 deletions Sming/Components/Hosted/app/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ ENABLE_HOSTED :=
## SPIFFS options
DISABLE_SPIFFS := 1
# SPIFF_FILES = files

COMPONENT_APPCODE := src
COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS)
27 changes: 27 additions & 0 deletions Sming/Components/Hosted/app/src/DigitalHosted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "DigitalHosted.h"

namespace Hosted {
namespace Digital {

void registerCommands(HostedServer& server)
{
// Register Command Handlers
server.registerCommand(HostedCommand_requestPinMode_tag, [](HostedCommand *request, HostedCommand *response)-> int {
pinMode((uint16_t)request->payload.requestPinMode.pin, (uint8_t)request->payload.requestPinMode.mode);
return 0;
});

server.registerCommand(HostedCommand_requestDigitalWrite_tag, [](HostedCommand *request, HostedCommand *response)-> int {
digitalWrite((uint16_t)request->payload.requestDigitalWrite.pin, (uint8_t)request->payload.requestDigitalWrite.value);
return 0;
});

server.registerCommand(HostedCommand_requestDigitalRead_tag, [](HostedCommand *request, HostedCommand *response)-> int {
uint8_t result = digitalRead((uint16_t)request->payload.requestDigitalRead.pin);
response->which_payload = HostedCommand_responseDigitalRead_tag;
response->payload.responseDigitalRead.value = result;

return 0;
});
}
}}
12 changes: 12 additions & 0 deletions Sming/Components/Hosted/app/src/DigitalHosted.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <SmingCore.h>
#include <HostedServer.h>
#include <Digital.h>

namespace Hosted {
namespace Digital {

void registerCommands(HostedServer& server);
}
}
46 changes: 46 additions & 0 deletions Sming/Components/Hosted/app/src/SpiHosted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "SpiHosted.h"
#include <Data/Stream/MemoryDataStream.h>

namespace Hosted {
namespace Spi {

void registerCommands(HostedServer& server)
{
// Register Command Handlers
server.registerCommand(HostedCommand_requestSpiBeginTransaction_tag, [](HostedCommand *request, HostedCommand *response)-> int {
if(request->payload.requestSpiBeginTransaction.has_settings) {
SPI.begin();
}
else {
SPISettings settings(request->payload.requestSpiBeginTransaction.settings.speed,
request->payload.requestSpiBeginTransaction.settings.byteOrder,
request->payload.requestSpiBeginTransaction.settings.dataMode);
SPI.beginTransaction(settings);
}

return 0;
});

server.registerCommand(HostedCommand_requestSpiTransfer_tag, [](HostedCommand *request, HostedCommand *response)-> int {
PbData* responseData = new PbData;
responseData->length = 0;

MemoryDataStream* data = (MemoryDataStream*)request->payload.requestSpiTransfer.data.arg;
if(data != nullptr) {
size_t available = data->available();
uint8_t* buffer = new uint8_t[available];
size_t length = data->readBytes((char *)&buffer, available);
SPI.transfer(buffer, length);
responseData->value = buffer;
responseData->length = length;

delete data;
}

response->payload.responseSpiTransfer.data.funcs.encode = &pbEncodeData;
response->payload.responseSpiTransfer.data.arg = (void* )responseData;

return 0;
});
}
}}
12 changes: 12 additions & 0 deletions Sming/Components/Hosted/app/src/SpiHosted.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <SmingCore.h>
#include <HostedServer.h>
#include <SPI.h>

namespace Hosted {
namespace Spi {

void registerCommands(HostedServer& server);
}
}
29 changes: 29 additions & 0 deletions Sming/Components/Hosted/proto/hosted.pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,38 @@ PB_BIND(RequestDigitalRead, RequestDigitalRead, AUTO)
PB_BIND(ResponseDigitalRead, ResponseDigitalRead, AUTO)


PB_BIND(SpiSettings, SpiSettings, AUTO)


PB_BIND(RequestSpiBeginTransaction, RequestSpiBeginTransaction, AUTO)


PB_BIND(RequestSpiEndTransaction, RequestSpiEndTransaction, AUTO)


PB_BIND(RequestSpiTransfer, RequestSpiTransfer, AUTO)


PB_BIND(ResponseSpiTransfer, ResponseSpiTransfer, AUTO)


PB_BIND(RequestSpiWrite, RequestSpiWrite, AUTO)


PB_BIND(ResponseSpiWrite, ResponseSpiWrite, AUTO)


PB_BIND(RequestSpiRead, RequestSpiRead, AUTO)


PB_BIND(ResponseSpiRead, ResponseSpiRead, AUTO)


PB_BIND(HostedCommand, HostedCommand, AUTO)







Loading

0 comments on commit 9e06b04

Please sign in to comment.