Skip to content

Commit

Permalink
Merge pull request #198 from markusju/feature-sfp-i2c
Browse files Browse the repository at this point in the history
Added Feature to read EEPROM of SFP Modules using I2C / Bugfix
  • Loading branch information
RogueMaster authored Aug 7, 2022
2 parents 548ef5c + f047c9e commit 6ba7c0c
Show file tree
Hide file tree
Showing 10 changed files with 593 additions and 0 deletions.
8 changes: 8 additions & 0 deletions applications/gpio/gpio_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ GpioApp* gpio_app_alloc() {
GpioAppViewI2CScanner,
gpio_i2c_scanner_get_view(app->gpio_i2c_scanner));

app->gpio_i2c_sfp = gpio_i2c_sfp_alloc();
view_dispatcher_add_view(
app->view_dispatcher,
GpioAppViewI2CSfp,
gpio_i2c_sfp_get_view(app->gpio_i2c_sfp));

app->widget = widget_alloc();
view_dispatcher_add_view(
app->view_dispatcher, GpioAppViewUsbUartCloseRpc, widget_get_view(app->widget));
Expand All @@ -82,6 +88,7 @@ void gpio_app_free(GpioApp* app) {
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewVarItemList);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewGpioTest);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewI2CScanner);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewI2CSfp);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUart);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
view_dispatcher_remove_view(app->view_dispatcher, GpioAppViewUsbUartCloseRpc);
Expand All @@ -90,6 +97,7 @@ void gpio_app_free(GpioApp* app) {
gpio_test_free(app->gpio_test);
gpio_usb_uart_free(app->gpio_usb_uart);
gpio_i2c_scanner_free(app->gpio_i2c_scanner);
gpio_i2c_sfp_free(app->gpio_i2c_sfp);

// View dispatcher
view_dispatcher_free(app->view_dispatcher);
Expand Down
3 changes: 3 additions & 0 deletions applications/gpio/gpio_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "views/gpio_test.h"
#include "views/gpio_usb_uart.h"
#include "views/gpio_i2c_scanner.h"
#include "views/gpio_i2c_sfp.h"

struct GpioApp {
Gui* gui;
Expand All @@ -29,6 +30,7 @@ struct GpioApp {
GpioUsbUart* gpio_usb_uart;
UsbUartBridge* usb_uart_bridge;
GpioI2CScanner* gpio_i2c_scanner;
GpioI2CSfp* gpio_i2c_sfp;
};

typedef enum {
Expand All @@ -38,4 +40,5 @@ typedef enum {
GpioAppViewUsbUartCfg,
GpioAppViewUsbUartCloseRpc,
GpioAppViewI2CScanner,
GpioAppViewI2CSfp
} GpioAppView;
1 change: 1 addition & 0 deletions applications/gpio/gpio_custom_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ typedef enum {
GpioStartEventManualControl,
GpioStartEventUsbUart,
GpioStartEventI2CScanner,
GpioStartEventI2CSfp,

GpioCustomEventErrorBack,

Expand Down
338 changes: 338 additions & 0 deletions applications/gpio/gpio_i2c_sfp_control.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,338 @@
#include "gpio_i2c_sfp_control.h"
#include <furi_hal.h>
#include <string.h>

// This is map mapping the connector type to the appropriate name. (see SFF-8024 Rev. 4.9, Table 4-3)
const char *sfp_connector_map[256] = {
"Unknown or unspecified",
"SC",
"Fibre Channel Style 1",
"Fibre Channel Style 2",
"BNC/TNC",
"Fibre Channel Coax",
"Fiber Jack",
"LC",
"MT-RJ",
"MU",
"SG",
"Optical Pigtail",
"MP0 1x12",
"MP 2x16",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"HSSDC II",
"Copper pigtail",
"RJ45",
"No seperable connector",
"MXC 2x16",
"CS optical connector",
"SN (prev. Mini CS)",
"MPO 2x12",
"MPO 1x16",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};

void str_part(uint8_t *data, char* buffer, int start, int len){
int i;
for (i=start; i<start+len; i++){
buffer[i-start] = data[i];
}
buffer[i-start] = '\0';
}

/*
void print_part(uint8_t *data, char *ptype, char *prefix, int start, int len){
printf("%s: ", prefix);
for (int i=start; i<start+len; i++){
printf(ptype, data[i]);
}
printf("\n");
}
*/

void gpio_i2c_sfp_run_once(I2CSfpState* i2c_sfp_state) {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
// Allocate buffer for SFP Data
uint8_t sfp_data[255];
uint32_t response_timeout_ticks = furi_ms_to_ticks(5.f);

// Check for SFP Module on I2C Bus
if(furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, SFP_I2C_ADDRESS << 1, response_timeout_ticks)) {
for (uint8_t i = 0; i <= 254; i++) {
uint8_t data = 0;
// Read data from register
furi_hal_i2c_read_reg_8(
&furi_hal_i2c_handle_external,
SFP_I2C_ADDRESS << 1,
i,
&data,
response_timeout_ticks
);
// Save data
sfp_data[i] = data;
}


/*
print_part(sfp_data, "%c", "Vendor", 20, 16);
print_part(sfp_data, "%02X", "OUI", 37, 3);
print_part(sfp_data, "%c", "Rev", 56, 4);
print_part(sfp_data, "%c", "PN", 40, 16);
print_part(sfp_data, "%c", "SN", 68, 16);
print_part(sfp_data, "%c", "DC", 84, 6);
printf("Typ: 0x%02X\r\n", sfp_data[0]);
printf("Connector: 0x%02X\r\n", sfp_data[2]);
printf("Bitrate: %u MBd\r\n", sfp_data[12]*100);
printf("Wavelength: %u nm\r\n", sfp_data[60] * 256 + sfp_data[61]);
printf(" %-6s %-6s %-6s %-6s %-6s\r\n", "SM", "OM1", "OM2", "OM3", "OM4");
printf("Max length: %3u km %4u m %4u m %4u m %4u m\r\n", sfp_data[14], sfp_data[17]*10, sfp_data[16]*10, sfp_data[19]*10, sfp_data[18]*10);
*/

str_part(sfp_data, i2c_sfp_state->vendor, 20, 16);
str_part(sfp_data, i2c_sfp_state->rev, 56,4);
str_part(sfp_data, i2c_sfp_state->pn, 40, 16);
str_part(sfp_data, i2c_sfp_state->sn, 68, 16);
str_part(sfp_data, i2c_sfp_state->dc, 84, 6);

//Look up connector in table and copy to struct.
strcpy(i2c_sfp_state->connector, sfp_connector_map[sfp_data[2]]);
i2c_sfp_state->bitrate = sfp_data[12]*100;
i2c_sfp_state->wavelength = sfp_data[60] * 256 + sfp_data[61];
i2c_sfp_state->sm_reach = sfp_data[14];
i2c_sfp_state->mm_reach_om3 = sfp_data[19]*10;


}
furi_hal_i2c_release(&furi_hal_i2c_handle_external);
}
Loading

0 comments on commit 6ba7c0c

Please sign in to comment.