Idle Package, Broadcast Package, small improvements

This commit is contained in:
GabrielKoppenstein committed 2021-08-30 00:31:01 +02:00
1 parent 710692f40d
commit 2d50676a5e
2 files changed
+74 -79

No files matched your search

+36 -41
View File
@@ -12,49 +12,50 @@
#include "multicore.h"
#define SIZE_BYTE_ARRAY 5
#define SIZE_ACTIVE_FUNCTIONS 69
#define DCC_INPUT_PIN 17
bool active_functions[SIZE_ACTIVE_FUNCTIONS] = {false};
bool direction = 1;
void set_speed(uint8_t speed) {
if (speed == 255) //Emergency Break
{
// printf("EMERGENCY BREAK");
printf("EMERGENCY BREAK");
} else //normal speed control ranges from 0 to 126
{
// printf("Speed: %u - Direction: %u\n", speed, direction);
printf("Speed: %u - Direction: %u\n", speed, direction);
}
}
void set_outputs() {
uint32_t outputs_to_be_set = 0;
uint32_t GPIO_to_be_set = 0;
uint32_t filter_forbidden_GPIO = 0b00000000000111111111111111111110; //ensures that GPIO's that are used for inputs or PWM (motor) cannot be set HIGH
for (uint8_t i = 0; i < SIZE_ACTIVE_FUNCTIONS; i++) {
if (active_functions[i]) {
// printf("F%u == 1\n",i);
printf("F%u == 1\n",i);
uint8_t func_cv_0 = CV_FUNCTION_ARRAY[4 + i * 8 - 4 * direction];
uint8_t func_cv_1 = CV_FUNCTION_ARRAY[5 + i * 8 - 4 * direction];
uint8_t func_cv_2 = CV_FUNCTION_ARRAY[6 + i * 8 - 4 * direction];
uint8_t func_cv_3 = CV_FUNCTION_ARRAY[7 + i * 8 - 4 * direction];
uint32_t func_cv = (func_cv_0) + (func_cv_1 << 8) + (func_cv_2 << 16) + (func_cv_3 << 24);
outputs_to_be_set = outputs_to_be_set | func_cv;
/*uint32_t mask = 1;
or (uint8_t j = 0; j < 32; j++)
GPIO_to_be_set = (GPIO_to_be_set | func_cv) & filter_forbidden_GPIO;
uint32_t mask = 1;
for (uint8_t j = 0; j < 32; j++)
{
uint32_t bit_value = (func_cv&mask)>>j;
uint32_t bit_value = (GPIO_to_be_set&mask)>>j;
printf("GPIO: %u, set to: %d\n",j,bit_value);
//gpio_put(j,bit_value);
mask = mask<<1;
}*/
}
// printf("func_cv: %u\n",func_cv);
// printf("func: F%u, direction: %u\n",func,direction);
//printf("func_cv_0_index: %d func_cv_1_index: %d func_cv_2_index: %d func_cv_3_index: %d \n",4+257+i*8-4*direction,4+258+i*8-4*direction,4+259+i*8-4*direction,4+260+i*8-4*direction);
}
}
// printf("outputs_to_bet_set: %u\n",outputs_to_be_set);
}
bool readBit() {
busy_wait_us_32(87);
return !gpio_get(17);
return !gpio_get(DCC_INPUT_PIN);
}
void update_active_functions(uint8_t function_number, uint8_t input_byte, uint8_t count) {
@@ -76,32 +77,36 @@ bool error_detection(int8_t number_of_bytes, const uint8_t byte_array[]) {
// Returns true for long address
bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]) {
if (byte_array[number_of_bytes - 1] >> 7) //most significant bit of first received byte
{
if (byte_array[number_of_bytes - 1] >> 6) //second most significant bit of first received byte
{
return true;
}
}
if ((byte_array[number_of_bytes - 1]>>6) == 0b00000011) return true;
return false;
}
bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) {
uint16_t read_address;
if (is_long_address(number_of_bytes, byte_array)) {
if (byte_array[number_of_bytes - 1] == 255) //Check for Idle Package
{
printf("Idle-Package found!\n");
return false;
}
if (byte_array[number_of_bytes-1] == 0) //Check for Broadcast Package
{
printf("Broadcast-Package found!\n");
return true;
}
if (is_long_address(number_of_bytes, byte_array)) //Long Address Package
{
//start of transmission -> address_byte_1 -> address_byte_0 -> ... -> end of transmission
uint8_t address_byte_1 = (byte_array[number_of_bytes - 1]) - 192; //remove long address identifier bits
uint8_t address_byte_0 = (byte_array[number_of_bytes - 2]);
uint16_t address_byte_1 = (byte_array[number_of_bytes - 1]) - 192; //remove long address identifier bits
uint16_t address_byte_0 = (byte_array[number_of_bytes - 2]);
read_address = (address_byte_1 << 8) + address_byte_0;
// printf("long address: %u, was read. \n", read_address);
} else {
printf("long address: %u, was read. \n", read_address);
} else //Short Address Package
{
//start of transmission -> address_byte_0 -> ... -> end of transmission
read_address = (byte_array[number_of_bytes - 1]);
// printf("short address: %u, was read. \n",read_address);
printf("short address: %u, was read. \n",read_address);
}
if (CV_1 == read_address)
return true;
return false;
return (CV_1 == read_address); //returns true if Address Matches
}
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) {
@@ -226,7 +231,7 @@ void writeLastBit(bool bit) {
last_bits |= bit;
}
//start of transmission -> byte_n(addressbyte) -> ... -> byte_0(error detection byte) -> end of transmission
//start of transmission -> byte_n(address byte) -> ... -> byte_0(error detection byte) -> end of transmission
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) {
for (uint8_t i = 0; i < number_of_bytes; i++) {
byte_array[i] = last_bits >> (i * 9 + 1);
@@ -236,37 +241,27 @@ void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) {
void gpio_callback_rise(unsigned int gpio, long unsigned int events) {
writeLastBit(readBit());
absolute_time_t from = get_absolute_time();
int8_t number_of_bytes = check_for_package();
if (number_of_bytes != -1) {
uint8_t byte_array[SIZE_BYTE_ARRAY] = {0};
bits_to_byte_array(number_of_bytes,byte_array);
// convert_array(number_of_bytes); //converts bit_array to byte_array for easier handling
if (error_detection(number_of_bytes,byte_array)) { //Returns true if the XOR verification was error-free
// printf("no error :-)\n");
if (address_evaluation(number_of_bytes,byte_array)) { //Returns true if address matches with CV
// printf("address matches :-)\n");
instruction_evaluation(number_of_bytes,byte_array);
}
// else printf("ADDRESS DOES NOT MATCH!\n");
}
// else printf("ERROR DETECTED!\n");
absolute_time_t to = get_absolute_time();
int64_t diff = absolute_time_diff_us(from, to);
printf("%"PRId64"\n", diff);
for (int i = 0; i < number_of_bytes; i++) {
printf("BYTE_%u:" BYTE_TO_BINARY_PATTERN "\n", i, BYTE_TO_BINARY(byte_array[i]));
}
printf("Paketgroesse: %u\n", number_of_bytes);
printf("\n");
// printf(BYTE_TO_BINARY_PATTERN"\n",BYTE_TO_BINARY(error_detection(number_of_bytes, index_new)));
}
}
int main() {
stdio_init_all();
gpio_init(17);
gpio_set_dir(17, GPIO_IN);
gpio_set_irq_enabled_with_callback(17, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise);
gpio_init(DCC_INPUT_PIN);
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise);
while (1);
}