Compare commits
No commits in common. "4666f79dd32d4581c2da4c88b17813e7076d8418" and "ae1c50ee09f2a64f93c0e17cbeefae3c338c1ea7" have entirely different histories.
4666f79dd3
...
ae1c50ee09
@ -3,7 +3,15 @@
|
|||||||
// The start address for the controller data
|
// The start address for the controller data
|
||||||
uint16_t EEMEM start_address_controller = 0x1400;
|
uint16_t EEMEM start_address_controller = 0x1400;
|
||||||
|
|
||||||
// Checks if the EEPROM memory is ready to be written in and waits until it is.
|
// Where the writing of the fans points start
|
||||||
|
uint16_t EEMEM start_address_fan1 = 0x1400 + 0x30;
|
||||||
|
uint16_t EEMEM start_address_fan2 = 0x1400 + 0x60;
|
||||||
|
|
||||||
|
// The placement for the next datapoint form the fans.
|
||||||
|
uint16_t EEMEM current_address_fan1 = 0x1400 + 0x30;
|
||||||
|
uint16_t EEMEM current_address_fan2 = 0x1400 + 0x60;
|
||||||
|
|
||||||
|
// Checks if the EEPROM memory is ready to be written in.
|
||||||
void check_eeprom_is_ready(){
|
void check_eeprom_is_ready(){
|
||||||
while(1){
|
while(1){
|
||||||
if (eeprom_is_ready()){
|
if (eeprom_is_ready()){
|
||||||
@ -15,28 +23,91 @@ void check_eeprom_is_ready(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Takes inn a struct by the form of config_t
|
||||||
|
// Checks if the eeprom is ready to be written in
|
||||||
|
// Checks if it has been written information at the address
|
||||||
|
// If true, the infromation is replaced with the intaken struct
|
||||||
|
// else the intaken struct is written at the address.
|
||||||
void write_struct_from_EEPROM(config_t write_struct){
|
void write_struct_from_EEPROM(config_t write_struct){
|
||||||
// Calculate the required storage size
|
|
||||||
uint8_t struct_size = sizeof(write_struct);
|
uint8_t struct_size = sizeof(write_struct);
|
||||||
|
|
||||||
// Wait for the EEPROM to be ready
|
|
||||||
check_eeprom_is_ready();
|
check_eeprom_is_ready();
|
||||||
|
|
||||||
// Update the stored config stuct
|
|
||||||
eeprom_update_block((void*) &write_struct,(void*) &start_address_controller, struct_size);
|
eeprom_update_block((void*) &write_struct,(void*) &start_address_controller, struct_size);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads the memory block at the address start_address_controller
|
||||||
|
// returns a struct in form of config_t
|
||||||
config_t read_struct_from_EEPROM(){
|
config_t read_struct_from_EEPROM(){
|
||||||
// Create a config struct to hold the received data
|
//is eeprom ready??
|
||||||
config_t read_struct = { 0 };
|
config_t read_struct;
|
||||||
uint8_t struct_size = sizeof(read_struct);
|
uint8_t struct_size = sizeof(read_struct);
|
||||||
|
|
||||||
// Wait for the EEPROM to be ready
|
|
||||||
check_eeprom_is_ready();
|
check_eeprom_is_ready();
|
||||||
|
|
||||||
// Read the data from the EEPROM
|
|
||||||
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
|
eeprom_read_block((void *) &read_struct,(void*) &start_address_controller, struct_size);
|
||||||
|
|
||||||
// Return the data
|
|
||||||
return read_struct;
|
return read_struct;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Takes inn a dataPoint and what data set it belongs to
|
||||||
|
// checks if EEPROM is ready
|
||||||
|
// If the dataset is 1, the datapoint is written at the address.
|
||||||
|
// If the dataset is 2 its written at another point.
|
||||||
|
int write_data_point_in_EEPROM(uint8_t byte, uint8_t fan_num){
|
||||||
|
|
||||||
|
check_eeprom_is_ready();
|
||||||
|
if (fan_num == 1){
|
||||||
|
eeprom_write_byte(0x30, byte);
|
||||||
|
current_address_fan1++;
|
||||||
|
return 1;
|
||||||
|
} else if (fan_num == 2){
|
||||||
|
eeprom_update_byte(current_address_fan2, byte);
|
||||||
|
current_address_fan2++;
|
||||||
|
return 1;
|
||||||
|
} else{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads all the datapoints to the choosen data.
|
||||||
|
// it writes the data points in the USART stream.
|
||||||
|
uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array){
|
||||||
|
uint8_t byte = 0;
|
||||||
|
|
||||||
|
if (fan_num == 1){
|
||||||
|
uint8_t len = current_address_fan1 - start_address_fan1;
|
||||||
|
|
||||||
|
check_eeprom_is_ready();
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i <len; i++){
|
||||||
|
byte = eeprom_read_byte(i + start_address_fan1);
|
||||||
|
array[i] = byte;
|
||||||
|
printf("Fanspeed nr %u : %u", i, byte);
|
||||||
|
}
|
||||||
|
} else if (fan_num == 2){
|
||||||
|
uint8_t len = current_address_fan2 - start_address_fan2;
|
||||||
|
check_eeprom_is_ready();
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i <len; i++){
|
||||||
|
byte = eeprom_read_byte(i + start_address_fan2);
|
||||||
|
array[i] = byte;
|
||||||
|
printf("Fanspeed nr %u : %u", i, byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sizeof(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads all the datapoints to the choosen data.
|
||||||
|
// it writes the data points in the USART stream.
|
||||||
|
uint8_t read_single_data_point_speed_info(uint8_t fan_num, uint8_t pos){
|
||||||
|
uint8_t byte = 0;
|
||||||
|
|
||||||
|
if (fan_num == 1){
|
||||||
|
check_eeprom_is_ready();
|
||||||
|
return eeprom_read_byte(0x30 + pos);
|
||||||
|
} else if (fan_num == 2){
|
||||||
|
check_eeprom_is_ready();
|
||||||
|
return eeprom_read_byte(start_address_fan2 + pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* File: eeprom.h
|
* File: eeprom.h
|
||||||
* Author: Helle Augland Grasmo, Sebastian H. Gabrielli
|
* Author: Helle Augland Grasmo
|
||||||
*
|
*
|
||||||
* Created on 06 March 2024, 15:30
|
* Created on 06 March 2024, 15:30
|
||||||
*/
|
*/
|
||||||
@ -19,20 +19,30 @@ extern "C" {
|
|||||||
#include <avr/eeprom.h>
|
#include <avr/eeprom.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Struct for information on the controller.
|
// Struct for information on the controller.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t ms_fanspeed_sample_rate;
|
uint16_t ms_fanspeed_sample_rate;
|
||||||
} config_t;
|
} config_t;
|
||||||
|
|
||||||
// Check if EEPROM is ready to be written to
|
// Check if EEPROM is ready to be written in
|
||||||
void check_eeprom_is_ready();
|
void check_eeprom_is_ready();
|
||||||
|
|
||||||
// Writes a config_t struct to the EEPROM
|
// Writes a struct in EEPROM
|
||||||
void write_struct_from_EEPROM(config_t write_struct);
|
void write_struct_from_EEPROM(config_t write_struct);
|
||||||
|
|
||||||
// Read data from EEPROM and return it as a config_t struct
|
// Read data from EEPROM and return it as a controller struct
|
||||||
config_t read_struct_from_EEPROM();
|
config_t read_struct_from_EEPROM();
|
||||||
|
|
||||||
|
// Writes a datapoint in EEPROM
|
||||||
|
int write_data_point_in_EEPROM(uint8_t byte, uint8_t fan_num);
|
||||||
|
|
||||||
|
// Reads all the dataPoints form EEPROM
|
||||||
|
uint8_t read_data_point_speed_info(uint8_t fan_num, uint8_t *array);
|
||||||
|
|
||||||
|
uint8_t read_single_data_point_speed_info(uint8_t fan_num, uint8_t pos);
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@
|
|||||||
// read request
|
// read request
|
||||||
volatile bool last_action_write = false;
|
volatile bool last_action_write = false;
|
||||||
|
|
||||||
// Buffer to hold the received data
|
volatile uint8_t i2c_recv[I2C_RECV_BUF_SIZE] = {
|
||||||
volatile uint8_t i2c_recv_buf[I2C_RECV_BUF_SIZE] = {0};
|
0}; // Arbitrary length array to hold the received
|
||||||
// Counter to know which datapoint we're on
|
// data, longer than max expected command
|
||||||
volatile uint8_t i2c_recv_len = 0;
|
volatile uint8_t i2c_recv_len = 0;
|
||||||
|
|
||||||
void init_i2c(void) {
|
void init_i2c(void) {
|
||||||
@ -47,18 +47,13 @@ void init_i2c(void) {
|
|||||||
void i2c_reset_recv() {
|
void i2c_reset_recv() {
|
||||||
i2c_recv_len = 0;
|
i2c_recv_len = 0;
|
||||||
for (int i = 0; i < I2C_RECV_BUF_SIZE; i++) {
|
for (int i = 0; i < I2C_RECV_BUF_SIZE; i++) {
|
||||||
i2c_recv_buf[i] = 0;
|
i2c_recv[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_write_handler(uint8_t data) {
|
void i2c_write_handler(uint8_t data) {
|
||||||
last_action_write = true;
|
last_action_write = true;
|
||||||
|
i2c_recv[i2c_recv_len] = data;
|
||||||
// Validate that we are not overflowing the buffer
|
|
||||||
if (i2c_recv_len >= I2C_RECV_BUF_SIZE) { return; }
|
|
||||||
|
|
||||||
// Write the data to the receive buffer
|
|
||||||
i2c_recv_buf[i2c_recv_len] = data;
|
|
||||||
i2c_recv_len++;
|
i2c_recv_len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,14 +66,24 @@ void i2c_read_handler() {
|
|||||||
void i2c_stop_handler() {
|
void i2c_stop_handler() {
|
||||||
if (last_action_write) {
|
if (last_action_write) {
|
||||||
// Parse the received command data
|
// Parse the received command data
|
||||||
parse_command(i2c_recv_buf, i2c_recv_len);
|
parse_command(i2c_recv, i2c_recv_len);
|
||||||
|
|
||||||
// If the received command is a write only command we want to route it now.
|
if (i2c_recv[0] == CLEAR_BULK_FAN_SPEED || i2c_recv[0] == WRITE_CONFIG) {
|
||||||
if (i2c_recv_buf[0] == CLEAR_BULK_FAN_SPEED || i2c_recv_buf[0] == WRITE_CONFIG) {
|
|
||||||
route_command(0);
|
route_command(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Write only commands need to be routed now
|
||||||
|
switch (i2c_recv[0]) {
|
||||||
|
WRITE_CONFIG:
|
||||||
|
CLEAR_BULK_FAN_SPEED:
|
||||||
|
route_command(0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Reset the buffer for future transmissions
|
// Reset the buffer for future transmissions
|
||||||
i2c_reset_recv();
|
i2c_reset_recv();
|
||||||
}
|
}
|
||||||
@ -93,14 +98,16 @@ ISR(TWI0_TWIS_vect) {
|
|||||||
uint8_t data = 0;
|
uint8_t data = 0;
|
||||||
|
|
||||||
if (((TWI0.SSTATUS & TWI_DIR_bm) >> TWI_DIR_bp) == 0) {
|
if (((TWI0.SSTATUS & TWI_DIR_bm) >> TWI_DIR_bp) == 0) {
|
||||||
// Data write Controller -> Target
|
// Data write Master -> Slave
|
||||||
data = TWI0.SDATA;
|
data = TWI0.SDATA;
|
||||||
|
|
||||||
// Send the data to the write handler
|
// Send the data to the write handler
|
||||||
i2c_write_handler(data);
|
i2c_write_handler(data);
|
||||||
} else {
|
} else {
|
||||||
// Data read Controller <- Target
|
// Data read Master <- Slave
|
||||||
i2c_read_handler();
|
i2c_read_handler();
|
||||||
|
// data = TWI0.SDATA;
|
||||||
|
// TWI0.SDATA = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acknowledge having received
|
// Acknowledge having received
|
||||||
|
|||||||
@ -20,9 +20,8 @@ extern "C" {
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <util/twi.h>
|
#include <util/twi.h>
|
||||||
|
|
||||||
// Received data buffer size
|
// Received data info
|
||||||
// The size is larger than any expected command length
|
#define I2C_RECV_BUF_SIZE 64
|
||||||
#define I2C_RECV_BUF_SIZE 16
|
|
||||||
|
|
||||||
// Reset recv to initial state
|
// Reset recv to initial state
|
||||||
void i2c_reset_recv();
|
void i2c_reset_recv();
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* File: main.c
|
* File: main.c
|
||||||
* Author: Sebastian H. Gabrielli, Helle Augland Grasmo, Ina Min Rørnes
|
* Author: Sebastian H. Gabrielli, Helle Augland Grasmo
|
||||||
*
|
*
|
||||||
* Created on March 6, 2024, 12:34 PM
|
* Created on March 6, 2024, 12:34 PM
|
||||||
*/
|
*/
|
||||||
@ -24,8 +24,8 @@
|
|||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
|
||||||
// Fan history variables
|
// Fan history variables
|
||||||
volatile uint16_t fan1_history[512] = { 0 };
|
volatile uint16_t fan1_history[512] = {1, 2, 3, 4};
|
||||||
volatile uint16_t fan2_history[512] = { 0 };
|
volatile uint16_t fan2_history[512] = {2, 3, 4, 5};
|
||||||
|
|
||||||
// Default config is 500ms sample rate
|
// Default config is 500ms sample rate
|
||||||
volatile config_t config = { 500 };
|
volatile config_t config = { 500 };
|
||||||
|
|||||||
@ -42,7 +42,7 @@
|
|||||||
<targetPluginBoard></targetPluginBoard>
|
<targetPluginBoard></targetPluginBoard>
|
||||||
<platformTool>nEdbgTool</platformTool>
|
<platformTool>nEdbgTool</platformTool>
|
||||||
<languageToolchain>XC8</languageToolchain>
|
<languageToolchain>XC8</languageToolchain>
|
||||||
<languageToolchainVersion>2.46</languageToolchainVersion>
|
<languageToolchainVersion>2.45</languageToolchainVersion>
|
||||||
<platform>2</platform>
|
<platform>2</platform>
|
||||||
</toolsSet>
|
</toolsSet>
|
||||||
<packs>
|
<packs>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user