13 changed files with 329 additions and 323 deletions
-
4remote/include/FrSkyD_cc2500.h
-
1remote/include/Multiprotocol.h
-
12remote/include/common.h
-
7remote/include/crc.h
-
92remote/include/eeprom.h
-
48remote/include/input.h
-
44remote/src/FrSkyD_cc2500.cpp
-
1remote/src/Multiprotocol.cpp
-
117remote/src/common.cpp
-
22remote/src/crc.cpp
-
102remote/src/eeprom.cpp
-
47remote/src/input.cpp
-
7remote/src/state.cpp
@ -0,0 +1,7 @@ |
|||
#ifndef __CRC_H_H__ |
|||
#define __CRC_H_H__ |
|||
#include <stdint.h> |
|||
|
|||
uint32_t crc_update (uint32_t crc, uint8_t data); |
|||
|
|||
#endif |
@ -1,81 +1,37 @@ |
|||
#ifndef _EEPROM_H_ |
|||
#define _EEPROM_H_ |
|||
#include <EEPROM.h> |
|||
|
|||
#include <Arduino.h> |
|||
#include <stdint.h> |
|||
#include "tx_def.h" |
|||
#include "input.h" |
|||
extern class Eeprom_config eeprom_config; |
|||
|
|||
#define CURRENT_VERSION 0x01 |
|||
struct eeprom_data_v1 { |
|||
uint8_t version; |
|||
uint32_t crc; |
|||
struct { |
|||
struct { |
|||
uint16_t max; |
|||
uint16_t min; |
|||
uint8_t inverted; |
|||
} throttle, yaw, roll, pitch, aux[5]; |
|||
uint32_t master_id; |
|||
} data; |
|||
class Eeprom_config { |
|||
public: |
|||
|
|||
}; |
|||
Eeprom_config(void); |
|||
~Eeprom_config(void); |
|||
|
|||
int validate(void); |
|||
int read(void); |
|||
int write(void); |
|||
|
|||
const static uint32_t crc_table[16] = { |
|||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, |
|||
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
|||
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, |
|||
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
|||
}; |
|||
int get_ch_config(struct Input::ch_config* config); |
|||
int set_ch_config(struct Input::ch_config* config); |
|||
|
|||
uint32_t crc_update (uint32_t crc, uint8_t data) |
|||
{ |
|||
uint8_t tbl_idx; |
|||
int get_master_id(uint32_t* master_id); |
|||
int set_master_id(uint32_t master_id); |
|||
|
|||
tbl_idx = crc ^ (data >> (0 * 4)); |
|||
crc = (crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4); |
|||
|
|||
tbl_idx = crc ^ (data >> (1 * 4)); |
|||
crc = (crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4); |
|||
|
|||
return crc; |
|||
private: |
|||
#define CURRENT_VERSION 0x01 |
|||
struct eeprom_data_v1 { |
|||
uint8_t version; |
|||
uint32_t data_crc; |
|||
struct { |
|||
uint32_t master_id; |
|||
struct Input::ch_config[CH_COUNT]; |
|||
} data; |
|||
} current_config; |
|||
bool sucessfull_read; |
|||
} |
|||
|
|||
|
|||
int read_eeprom(struct eeprom_data_v1 *eeprom_data) { |
|||
uint8_t *data = NULL; |
|||
uint8_t start_address = 0x10; |
|||
|
|||
EEPROM.PageBase0 = 0x801F000; |
|||
EEPROM.PageBase1 = 0x801F800; |
|||
EEPROM.PageSize = 0x400; |
|||
EEPROM.init(); |
|||
|
|||
data = (uint8_t*) eeprom_data; |
|||
|
|||
for (uint8_t i = 0; i < sizeof(eeprom_data) ; i = 0) { |
|||
EEPROM.read(0x10 + i , data); |
|||
data+=1; |
|||
} |
|||
|
|||
// validate version |
|||
if (eeprom_data->version != CURRENT_VERSION) { |
|||
return -1; |
|||
} |
|||
|
|||
|
|||
data = (uint8_t*) &(eeprom_data->data); |
|||
uint32_t crc = ~0L; |
|||
for (uint8_t i = 0; i < sizeof(eeprom_data->data) ; i = 0) { |
|||
crc = crc_update(crc, *data); |
|||
data+=1; |
|||
} |
|||
|
|||
// validate crc |
|||
if (eeprom_data->crc != crc) { |
|||
return -1; |
|||
} |
|||
|
|||
} |
|||
#endif |
@ -0,0 +1,22 @@ |
|||
#include "Arduino.h"
|
|||
|
|||
static uint32_t crc_table[16] = { |
|||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, |
|||
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
|||
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, |
|||
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
|||
}; |
|||
|
|||
uint32_t crc_update (uint32_t crc, uint8_t data) |
|||
{ |
|||
uint8_t tbl_idx; |
|||
uint8_t mask = 0x0f; |
|||
|
|||
tbl_idx = crc ^ (data >> (0 * 4)); |
|||
crc = (crc_table + (tbl_idx & mask)) ^ (crc >> 4); |
|||
|
|||
tbl_idx = crc ^ (data >> (1 * 4)); |
|||
crc = (crc_table + (tbl_idx & mask)) ^ (crc >> 4); |
|||
|
|||
return crc; |
|||
} |
@ -0,0 +1,102 @@ |
|||
#include <EEPROM.h>
|
|||
|
|||
#include <Arduino.h>
|
|||
#include <stdint.h>
|
|||
#include "tx_def.h"
|
|||
#include "eeprom.h"
|
|||
#include "crc.h"
|
|||
|
|||
class Eeprom_config eeprom_config; |
|||
|
|||
Eeprom_config::Eeprom_config() { |
|||
EEPROM.PageBase0 = 0x801F000; |
|||
EEPROM.PageBase1 = 0x801F800; |
|||
EEPROM.PageSize = 0x400; |
|||
EEPROM.init(); |
|||
memset(&this->current_config, 0, sizeof(this->current_config)); |
|||
this->sucessfull_read = false; |
|||
} |
|||
|
|||
Eeprom_config::~Eeprom_config() { |
|||
/* nope */ |
|||
} |
|||
|
|||
int Eeprom_config::validate() { |
|||
if (this->sucessfull_read == false) { |
|||
return -1; |
|||
} |
|||
|
|||
if (this->current_config.version != CURRENT_VERSION) { |
|||
return -1; |
|||
} |
|||
uint32_t crc_calc = 0; |
|||
|
|||
uint8_t * data = &(this->current_config.data); |
|||
for (uint8_t i = 0; i < sizeof(struct eeprom_data_v1) ; i = 0) { |
|||
crc_calc = crc_update(crc_calc, data[i]); |
|||
} |
|||
|
|||
if (crc_calc != this->current_config.data_crc) { |
|||
return -1; |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
int Eeprom_config::read(void) { |
|||
uint8_t *data = NULL; |
|||
data = (uint8_t *) &this->current_config; |
|||
|
|||
for (uint8_t i = 0; i < sizeof(struct eeprom_data_v1) ; i = 0) { |
|||
EEPROM.read(0x10 + i , data); |
|||
data+=1; |
|||
} |
|||
|
|||
this->sucessfull_read = true; |
|||
return 0; |
|||
} |
|||
|
|||
int Eeprom_config::write(void) { |
|||
uint8_t *data = NULL; |
|||
data = (uint8_t *) &this->current_config; |
|||
|
|||
for (uint8_t i = 0; i < sizeof(struct eeprom_data_v1) ; i = 0) { |
|||
EEPROM.write(0x10 + i , data); |
|||
data+=1; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
int Eeprom_config::get_ch_config(struct ch_config* config) { |
|||
if (this->sucessfull_read == false && config != NULL) { |
|||
return -1; |
|||
} |
|||
memcpy(config, this->current_config.data.ch, sizeof(struct ch_config) * CH_COUNT); |
|||
return 0; |
|||
} |
|||
|
|||
int Eeprom_config::set_ch_config(struct ch_config* config) { |
|||
if (this->sucessfull_read == false && config != NULL) { |
|||
return -1; |
|||
} |
|||
memcpy(this->current_config.data.ch, config, sizeof(struct ch_config) * CH_COUNT); |
|||
return 0; |
|||
} |
|||
|
|||
int Eeprom_config::get_master_id(uint32_t* master_id) |
|||
{ |
|||
if (this->sucessfull_read == false && master_id != NULL) { |
|||
return -1; |
|||
} |
|||
*master_id = this->current_config.data.master_id; |
|||
return 0; |
|||
} |
|||
|
|||
int Eeprom_config::set_master_id(uint32_t master_id) |
|||
{ |
|||
if (this->sucessfull_read == false) { |
|||
return -1; |
|||
} |
|||
this->current_config.data.master_id = master_id; |
|||
return 0; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue