nmhw21: add support for UM network config

- get network configuration from UM via I2C
- provide method to retrieve IP and mask
This commit is contained in:
Rene Straub 2019-03-30 12:22:47 +01:00
parent db067fefdd
commit 3e938e9ab8
2 changed files with 55 additions and 7 deletions

View File

@ -22,6 +22,8 @@ static int module_present = 0;
static um_type_t module_type = UM_TYPE_RESERVED;
static int hw_version = 0;
static int hw_revision = 0;
static struct in_addr ipv4_addr;
static struct in_addr ipv4_mask;
static int switch_i2c_bus(int* old_bus)
@ -55,9 +57,6 @@ static void get_version(void)
int ret;
uint8_t reg[1];
/* TODO: Check why this delay is required. Is the UM software still reading board descriptor? */
udelay(50*1000); /* 50 ms delay (tested with 30 ms) */
ret = i2c_read(CONFIG_UM_I2C_ADDR, UM_REG_HW_VER, 1, reg, 1);
if (ret == 0) {
hw_version = (reg[0] >> 4) & 0x0F;
@ -67,6 +66,26 @@ static void get_version(void)
}
}
static void set_inaddr(const uint8_t* data, struct in_addr* in)
{
in->s_addr = htonl(data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3] << 0);
}
static void get_network_address(void)
{
int ret;
uint8_t data[8];
ret = i2c_read(CONFIG_UM_I2C_ADDR, UM_REG_IPV4_ADDR, 1, data, 8);
if (ret == 0) {
set_inaddr(&data[0], &ipv4_addr);
set_inaddr(&data[4], &ipv4_mask);
} else {
puts("error reading user module network configuration\n");
}
}
static void detect(void)
{
int ret;
@ -107,7 +126,13 @@ void um_init(int i2c_bus)
detect();
if (module_present) {
/* TODO: Check why this delay is required.
* Is the UM software still reading board descriptor?
*/
udelay(50*1000); /* 50 ms delay (tested with 30 ms) */
get_version();
get_network_address();
}
}
@ -122,7 +147,7 @@ int um_present(void)
const char* um_type_as_str(void)
{
switch (module_type) {
case UM_TYPE_VCUPRO: return "VCUpro";
case UM_TYPE_VCUPRO: return "VCU Pro";
default: return "<unknown";
}
}
@ -135,3 +160,12 @@ void um_version(uint8_t *version, uint8_t *revision)
if (revision != NULL)
*revision = hw_revision;
}
void um_network_address(struct in_addr* ip, struct in_addr* mask)
{
if (ip != NULL)
*ip = ipv4_addr;
if (mask != NULL)
*mask = ipv4_mask;
}

View File

@ -11,12 +11,18 @@
#ifndef UM_H
#define UM_H
#include <net.h>
#define CONFIG_UM_I2C_BUS 1
#define CONFIG_UM_I2C_ADDR 0x40
#define UM_REG_PRESENCE 0 /* Presence register, value = 0xC5 */
#define UM_REG_TYPE 1 /* See um_type_t */
#define UM_REG_HW_VER 4 /* Version.Revision, e.g. 0x21 = v2.1 */
#define UM_REG_PRESENCE 0x00 /* Presence register, value = 0xC5 */
#define UM_REG_TYPE 0x01 /* See um_type_t */
#define UM_REG_HW_VER 0x04 /* Version.Revision, e.g. 0x21 = v2.1 */
#define UM_REG_IPV4_ADDR 0x10 /* IPv4 address in network byte order, 4 bytes */
#define UM_REG_IPV4_MASK 0x14 /* IPv4 mask in network byte order, 4 bytes */
#define UM_PRESENCE_TOKEN 0xC5
@ -59,4 +65,12 @@ const char* um_type_as_str(void);
*/
void um_version(uint8_t *version, uint8_t *revision);
/**
* Returns user module IPv4 configuration
*
* @param ip Pointer to variable to receive IPv4 address
* @param mask Pointer to variable to receive IPv4 mask
*/
void um_network_address(struct in_addr* ip, struct in_addr* mask);
#endif /* UM_H */