From 3568065141279baa3dfff0e6a65fbf6ab8d14676 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 12 Feb 2016 09:33:38 +0200 Subject: [PATCH 001/109] Add readme --- readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000000..82bf8bfdfa --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +A visualization library for the TMK keyboard firmware From 4452f4965dae25bb3707e64858221784318c4f56 Mon Sep 17 00:00:00 2001 From: fredizzimo Date: Fri, 12 Feb 2016 09:59:39 +0200 Subject: [PATCH 002/109] Add MIT license --- LICENSE.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000000..d7cc3198cb --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From bb75446b0beb4db28b775a2e2db61d91f0f0324a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 16:44:14 +0200 Subject: [PATCH 003/109] Add uGFX submodule v 2.4 --- .gitmodules | 3 +++ ugfx | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 ugfx diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..2ab25f6883 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ugfx"] + path = ugfx + url = https://bitbucket.org/Tectu/ugfx.git diff --git a/ugfx b/ugfx new file mode 160000 index 0000000000..2b66ac524b --- /dev/null +++ b/ugfx @@ -0,0 +1 @@ +Subproject commit 2b66ac524bd56853ba97b917683971f3ebc0104c From fb681b5a60ae5b13a0413c815b4564d60fa200c5 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 17:47:39 +0200 Subject: [PATCH 004/109] Add makefile that includes ugfx --- visualizer.mk | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 visualizer.mk diff --git a/visualizer.mk b/visualizer.mk new file mode 100644 index 0000000000..c5ae0dfa95 --- /dev/null +++ b/visualizer.mk @@ -0,0 +1,26 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +GFXLIB = $(VISUALIZER_DIR)/ugfx +include $(GFXLIB)/gfx.mk +SRC += $(GFXSRC) +INC += $(GFXINC) From 01b955aa64766d51191a716d3a9d74d35f221b28 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 19:21:16 +0200 Subject: [PATCH 005/109] Add LCD backlight support Also possibility to disable the LCD support --- lcd_backlight.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ lcd_backlight.h | 42 ++++++++++++++++++++++++ visualizer.mk | 9 +++++- 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 lcd_backlight.c create mode 100644 lcd_backlight.h diff --git a/lcd_backlight.c b/lcd_backlight.c new file mode 100644 index 0000000000..70187d1e00 --- /dev/null +++ b/lcd_backlight.c @@ -0,0 +1,85 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "lcd_backlight.h" +#include + +static uint8_t current_hue = 0x00; +static uint8_t current_saturation = 0x00; +static uint8_t current_intensity = 0xFF; +static uint8_t current_brightness = 0x7F; + +void lcd_backlight_init(void) { + lcd_backlight_hal_init(); + lcd_backlight_color(current_hue, current_saturation, current_intensity); +} + +// This code is based on Brian Neltner's blogpost and example code +// "Why every LED light should be using HSI colorspace". +// http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi +static void hsi_to_rgb(float h, float s, float i, uint16_t* r_out, uint16_t* g_out, uint16_t* b_out) { + unsigned int r, g, b; + h = fmodf(h, 360.0f); // cycle h around to 0-360 degrees + h = 3.14159f * h / 180.0f; // Convert to radians. + s = s > 0.0f ? (s < 1.0f ? s : 1.0f) : 0.0f; // clamp s and i to interval [0,1] + i = i > 0.0f ? (i < 1.0f ? i : 1.0f) : 0.0f; + + // Math! Thanks in part to Kyle Miller. + if(h < 2.09439f) { + r = 65535.0f * i/3.0f *(1.0f + s * cos(h) / cosf(1.047196667f - h)); + g = 65535.0f * i/3.0f *(1.0f + s *(1.0f - cosf(h) / cos(1.047196667f - h))); + b = 65535.0f * i/3.0f *(1.0f - s); + } else if(h < 4.188787) { + h = h - 2.09439; + g = 65535.0f * i/3.0f *(1.0f + s * cosf(h) / cosf(1.047196667f - h)); + b = 65535.0f * i/3.0f *(1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h))); + r = 65535.0f * i/3.0f *(1.0f - s); + } else { + h = h - 4.188787; + b = 65535.0f*i/3.0f * (1.0f + s * cosf(h) / cosf(1.047196667f - h)); + r = 65535.0f*i/3.0f * (1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h))); + g = 65535.0f*i/3.0f * (1.0f - s); + } + *r_out = r > 65535 ? 65535 : r; + *g_out = g > 65535 ? 65535 : g; + *b_out = b > 65535 ? 65535 : b; +} + +void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity) { + uint16_t r, g, b; + float hue_f = 360.0f * (float)hue / 255.0f; + float saturation_f = (float)saturation / 255.0f; + float intensity_f = (float)intensity / 255.0f; + intensity_f *= (float)current_brightness / 255.0f; + hsi_to_rgb(hue_f, saturation_f, intensity_f, &r, &g, &b); + current_hue = hue; + current_saturation = saturation; + current_intensity = intensity; + lcd_backlight_hal_color(r, g, b); +} + +void lcd_backlight_brightness(uint8_t b) { + current_brightness = b; + lcd_backlight_color(current_hue, current_saturation, current_intensity); +} diff --git a/lcd_backlight.h b/lcd_backlight.h new file mode 100644 index 0000000000..dd3e37a064 --- /dev/null +++ b/lcd_backlight.h @@ -0,0 +1,42 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef LCD_BACKLIGHT_H_ +#define LCD_BACKLIGHT_H_ +#include "stdint.h" + +// Helper macros for storing hue, staturation and intensity as unsigned integers +#define LCD_COLOR(hue, saturation, intensity) (hue << 16 | saturation << 8 | intensity) +#define LCD_HUE(color) ((color >> 16) & 0xFF) +#define LCD_SAT(color) ((color >> 8) & 0xFF) +#define LCD_INT(color) (color & 0xFF) + +void lcd_backlight_init(void); +void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity); +void lcd_backlight_brightness(uint8_t b); + +void lcd_backlight_hal_init(void); +void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b); + +#endif /* LCD_BACKLIGHT_H_ */ diff --git a/visualizer.mk b/visualizer.mk index c5ae0dfa95..8ffc1e4aca 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -21,6 +21,13 @@ # SOFTWARE. GFXLIB = $(VISUALIZER_DIR)/ugfx +ifdef LCD_ENABLE include $(GFXLIB)/gfx.mk +endif SRC += $(GFXSRC) -INC += $(GFXINC) +INC += $(GFXINC) $(VISUALIZER_DIR) + +ifdef LCD_BACKLIGHT_ENABLE +SRC += $(VISUALIZER_DIR)/lcd_backlight.c +SRC += lcd_backlight_hal.c +endif From 9e58d022ba4320ce0917defe4b81c1c7b5de77bb Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 19:38:23 +0200 Subject: [PATCH 006/109] Add visualizer A generic visualizer that supports animations. There's a few predefined keyframe types included, and more can be added by the user. --- visualizer.c | 349 ++++++++++++++++++++++++++++++++++++++++++++++++++ visualizer.h | 120 +++++++++++++++++ visualizer.mk | 4 +- 3 files changed, 472 insertions(+), 1 deletion(-) create mode 100644 visualizer.c create mode 100644 visualizer.h diff --git a/visualizer.c b/visualizer.c new file mode 100644 index 0000000000..2a92524e25 --- /dev/null +++ b/visualizer.c @@ -0,0 +1,349 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "visualizer.h" +#include "ch.h" +#include + +#ifdef LCD_ENABLE +#include "gfx.h" +#endif + +#ifdef LCD_BACKLIGHT_ENABLE +#include "lcd_backlight.h" +#endif + +//#define DEBUG_VISUALIZER + +#ifdef DEBUG_VISUALIZER +#include "debug.h" +#else +#include "nodebug.h" +#endif + + +static visualizer_keyboard_status_t current_status = { + .layer = 0xFFFFFFFF, + .default_layer = 0xFFFFFFFF, + .leds = 0xFFFFFFFF, +}; + +static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) { + return memcmp(status1, status2, sizeof(visualizer_keyboard_status_t)) == 0; +} + +static event_source_t layer_changed_event; +static bool visualizer_enabled = false; + +#define MAX_SIMULTANEOUS_ANIMATIONS 4 +static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {}; + +void start_keyframe_animation(keyframe_animation_t* animation) { + animation->current_frame = -1; + animation->time_left_in_frame = 0; + animation->need_update = true; + int free_index = -1; + for (int i=0;icurrent_frame = animation->num_frames; + animation->time_left_in_frame = 0; + animation->need_update = true; + for (int i=0;icurrent_frame, + animation->time_left_in_frame, delta); + if (animation->current_frame == animation->num_frames) { + animation->need_update = false; + return false; + } + if (animation->current_frame == -1) { + animation->current_frame = 0; + animation->time_left_in_frame = animation->frame_lengths[0]; + animation->need_update = true; + } else { + animation->time_left_in_frame -= delta; + while (animation->time_left_in_frame <= 0) { + int left = animation->time_left_in_frame; + if (animation->need_update) { + animation->time_left_in_frame = 0; + (*animation->frame_functions[animation->current_frame])(animation, state); + } + animation->current_frame++; + animation->need_update = true; + if (animation->current_frame == animation->num_frames) { + if (animation->loop) { + animation->current_frame = 0; + } + else { + stop_keyframe_animation(animation); + return false; + } + } + delta = -left; + animation->time_left_in_frame = animation->frame_lengths[animation->current_frame]; + animation->time_left_in_frame -= delta; + } + } + if (animation->need_update) { + animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state); + } + + int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame; + if ((unsigned)wanted_sleep < *sleep_time) { + *sleep_time = wanted_sleep; + } + + return true; +} + +bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + (void)state; + return false; +} + +#ifdef LCD_BACKLIGHT_ENABLE +bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) { + int frame_length = animation->frame_lengths[1]; + int current_pos = frame_length - animation->time_left_in_frame; + uint8_t t_h = LCD_HUE(state->target_lcd_color); + uint8_t t_s = LCD_SAT(state->target_lcd_color); + uint8_t t_i = LCD_INT(state->target_lcd_color); + uint8_t p_h = LCD_HUE(state->prev_lcd_color); + uint8_t p_s = LCD_SAT(state->prev_lcd_color); + uint8_t p_i = LCD_INT(state->prev_lcd_color); + + uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around + int d_h2 = t_h - p_h; + // Chose the shortest way around + int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1; + int d_s = t_s - p_s; + int d_i = t_i - p_i; + + int hue = (d_h * current_pos) / frame_length; + int sat = (d_s * current_pos) / frame_length; + int intensity = (d_i * current_pos) / frame_length; + //dprintf("%X -> %X = %X\n", p_h, t_h, hue); + hue += p_h; + sat += p_s; + intensity += p_i; + state->current_lcd_color = LCD_COLOR(hue, sat, intensity); + lcd_backlight_color( + LCD_HUE(state->current_lcd_color), + LCD_SAT(state->current_lcd_color), + LCD_INT(state->current_lcd_color)); + + return true; +} + +bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + state->prev_lcd_color = state->target_lcd_color; + state->current_lcd_color = state->target_lcd_color; + lcd_backlight_color( + LCD_HUE(state->current_lcd_color), + LCD_SAT(state->current_lcd_color), + LCD_INT(state->current_lcd_color)); + return false; +} +#endif // LCD_BACKLIGHT_ENABLE + +#ifdef LCD_ENABLE +bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + gdispClear(White); + gdispDrawString(0, 10, state->layer_text, state->font_dejavusansbold12, Black); + gdispFlush(); + return false; +} + +static void format_layer_bitmap_string(uint16_t default_layer, uint16_t layer, char* buffer) { + for (int i=0; i<16;i++) + { + uint32_t mask = (1u << i); + if (default_layer & mask) { + if (layer & mask) { + *buffer = 'B'; + } else { + *buffer = 'D'; + } + } else if (layer & mask) { + *buffer = '1'; + } else { + *buffer = '0'; + } + ++buffer; + + if (i==3 || i==7 || i==11) { + *buffer = ' '; + ++buffer; + } + } + *buffer = 0; +} + +bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + const char* layer_help = "1=On D=Default B=Both"; + char layer_buffer[16 + 4]; // 3 spaces and one null terminator + gdispClear(White); + gdispDrawString(0, 0, layer_help, state->font_fixed5x8, Black); + format_layer_bitmap_string(state->status.default_layer, state->status.layer, layer_buffer); + gdispDrawString(0, 10, layer_buffer, state->font_fixed5x8, Black); + format_layer_bitmap_string(state->status.default_layer >> 16, state->status.layer >> 16, layer_buffer); + gdispDrawString(0, 20, layer_buffer, state->font_fixed5x8, Black); + gdispFlush(); + return false; +} +#endif // LCD_ENABLE + +bool user_visualizer_inited(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + (void)state; + dprint("User visualizer inited\n"); + visualizer_enabled = true; + return false; +} + +// TODO: Optimize the stack size, this is probably way too big +static THD_WORKING_AREA(visualizerThreadStack, 1024); +static THD_FUNCTION(visualizerThread, arg) { + (void)arg; + + event_listener_t event_listener; + chEvtRegister(&layer_changed_event, &event_listener, 0); + + visualizer_state_t state = { + .status = { + .default_layer = 0xFFFFFFFF, + .layer = 0xFFFFFFFF, + .leds = 0xFFFFFFFF, + }, + + .current_lcd_color = 0, +#ifdef LCD_ENABLE + .font_fixed5x8 = gdispOpenFont("fixed_5x8"), + .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12") +#endif + }; + initialize_user_visualizer(&state); + state.prev_lcd_color = state.current_lcd_color; + +#ifdef LCD_BACKLIGHT_ENABLE + lcd_backlight_color( + LCD_HUE(state.current_lcd_color), + LCD_SAT(state.current_lcd_color), + LCD_INT(state.current_lcd_color)); +#endif + + systime_t sleep_time = TIME_INFINITE; + systime_t current_time = chVTGetSystemTimeX(); + + while(true) { + systime_t new_time = chVTGetSystemTimeX(); + systime_t delta = new_time - current_time; + current_time = new_time; + bool enabled = visualizer_enabled; + if (!same_status(&state.status, ¤t_status)) { + if (visualizer_enabled) { + state.status = current_status; + update_user_visualizer_state(&state); + state.prev_lcd_color = state.current_lcd_color; + } + } + sleep_time = TIME_INFINITE; + for (int i=0;i update_delta) { + sleep_time -= update_delta; + } + else { + sleep_time = 0; + } + } + dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time); + chEvtWaitOneTimeout(EVENT_MASK(0), sleep_time); + } +#ifdef LCD_ENABLE + gdispCloseFont(state.font_fixed5x8); + gdispCloseFont(state.font_dejavusansbold12); +#endif +} + +void visualizer_init(void) { + // We are using a low priority thread, the idea is to have it run only + // when the main thread is sleeping during the matrix scanning + chEvtObjectInit(&layer_changed_event); + (void)chThdCreateStatic(visualizerThreadStack, sizeof(visualizerThreadStack), + LOWPRIO, visualizerThread, NULL); +} + +void visualizer_set_state(uint32_t default_state, uint32_t state, uint32_t leds) { + // Note that there's a small race condition here, the thread could read + // a state where one of these are set but not the other. But this should + // not really matter as it will be fixed during the next loop step. + // Alternatively a mutex could be used instead of the volatile variables + bool changed = false; + visualizer_keyboard_status_t new_status = { + .layer = state, + .default_layer = default_state, + .leds = leds, + }; + if (!same_status(¤t_status, &new_status)) { + changed = true; + } + current_status = new_status; + if (changed) { + chEvtBroadcast(&layer_changed_event); + } +} diff --git a/visualizer.h b/visualizer.h new file mode 100644 index 0000000000..b7b0a3a7d0 --- /dev/null +++ b/visualizer.h @@ -0,0 +1,120 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef VISUALIZER_H +#define VISUALIZER_H +#include +#include +#include + +#ifdef LCD_ENABLE +#include "gfx.h" +#endif + +#ifdef LCD_BACKLIGHT_ENABLE +#include "lcd_backlight.h" +#endif + +// This need to be called once at the start +void visualizer_init(void); +// This should be called before every matrix scan +void visualizer_set_state(uint32_t default_state, uint32_t state, uint32_t leds); + +// If you need support for more than 8 keyframes per animation, you can change this +#define MAX_VISUALIZER_KEY_FRAMES 8 + +struct keyframe_animation_t; + +typedef struct { + uint32_t layer; + uint32_t default_layer; + uint32_t leds; // See led.h for available statuses +} visualizer_keyboard_status_t; + +// The state struct is used by the various keyframe functions +// It's also used for setting the LCD color and layer text +// from the user customized code +typedef struct visualizer_state_t { + // The user code should primarily be modifying these + uint32_t target_lcd_color; + const char* layer_text; + + // The user visualizer(and animation functions) can read these + visualizer_keyboard_status_t status; + + // These are used by the animation functions + uint32_t current_lcd_color; + uint32_t prev_lcd_color; +#ifdef LCD_ENABLE + font_t font_fixed5x8; + font_t font_dejavusansbold12; +#endif +} visualizer_state_t; + +// Any custom keyframe function should have this signature +// return true to get continuous updates, otherwise you will only get one +// update per frame +typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*); + +// Represents a keyframe animation, so fields are internal to the system +// while others are meant to be initialized by the user code +typedef struct keyframe_animation_t { + // These should be initialized + int num_frames; + bool loop; + int frame_lengths[MAX_VISUALIZER_KEY_FRAMES]; + frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES]; + + // Used internally by the system, and can also be read by + // keyframe update functions + int current_frame; + int time_left_in_frame; + bool need_update; + +} keyframe_animation_t; + +void start_keyframe_animation(keyframe_animation_t* animation); +void stop_keyframe_animation(keyframe_animation_t* animation); + +// Some predefined keyframe functions that can be used by the user code +// Does nothing, useful for adding delays +bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state); +// Animates the LCD backlight color between the current color and the target color (of the state) +bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state); +// Sets the backlight color to the target color +bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state); +// Displays the layer text centered vertically on the screen +bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state); +// Displays a bitmap (0/1) of all the currently active layers +bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state); +// Call this once, when the initial animation has finished, alternatively you can call it +// directly from the initalize_user_visualizer function (the animation can be null) +bool user_visualizer_inited(keyframe_animation_t* animation, visualizer_state_t* state); + +// These two functions have to be implemented by the user +void initialize_user_visualizer(visualizer_state_t* state); +void update_user_visualizer_state(visualizer_state_t* state); + + +#endif /* VISUALIZER_H */ diff --git a/visualizer.mk b/visualizer.mk index 8ffc1e4aca..ff4b61f089 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -23,11 +23,13 @@ GFXLIB = $(VISUALIZER_DIR)/ugfx ifdef LCD_ENABLE include $(GFXLIB)/gfx.mk +OPT_DEFS += -DLCD_ENABLE endif -SRC += $(GFXSRC) +SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c INC += $(GFXINC) $(VISUALIZER_DIR) ifdef LCD_BACKLIGHT_ENABLE SRC += $(VISUALIZER_DIR)/lcd_backlight.c SRC += lcd_backlight_hal.c +OPT_DEFS += -DLCD_BACKLIGHT_ENABLE endif From 209167d4d6a7e65a728fab5d996904cc5fb33a3c Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 19:52:57 +0200 Subject: [PATCH 007/109] Compile the user visualizer --- visualizer.mk | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/visualizer.mk b/visualizer.mk index ff4b61f089..e6e0d63d03 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -33,3 +33,8 @@ SRC += $(VISUALIZER_DIR)/lcd_backlight.c SRC += lcd_backlight_hal.c OPT_DEFS += -DLCD_BACKLIGHT_ENABLE endif + +ifndef VISUALIZER_USER +VISUALIZER_USER = visualizer_user.c +endif +SRC += $(VISUALIZER_USER) \ No newline at end of file From 8ce60649c85f1ad5371d443675f91c8fc36c3d2e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 20:29:49 +0200 Subject: [PATCH 008/109] Initialize backlight and LCD during visualizer_init --- visualizer.c | 7 +++++++ visualizer.mk | 1 + 2 files changed, 8 insertions(+) diff --git a/visualizer.c b/visualizer.c index 2a92524e25..402bbd1517 100644 --- a/visualizer.c +++ b/visualizer.c @@ -321,6 +321,13 @@ static THD_FUNCTION(visualizerThread, arg) { } void visualizer_init(void) { +#ifdef LCD_ENABLE + gfxInit(); +#endif + +#ifdef LCD_BACKLIGHT_ENABLE + lcd_backlight_init(); +#endif // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning chEvtObjectInit(&layer_changed_event); diff --git a/visualizer.mk b/visualizer.mk index e6e0d63d03..eef2d5cc8a 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -24,6 +24,7 @@ GFXLIB = $(VISUALIZER_DIR)/ugfx ifdef LCD_ENABLE include $(GFXLIB)/gfx.mk OPT_DEFS += -DLCD_ENABLE +OPT_LIBS += -lm endif SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c INC += $(GFXINC) $(VISUALIZER_DIR) From 8479e6aa390bdd7711753d9fa1141bd1073e2b0a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 22:08:49 +0200 Subject: [PATCH 009/109] Update readme and license Also add integration examples --- LICENSE.md | 8 + example_integration/callbacks.c | 50 ++++ example_integration/gfxconf.h | 325 ++++++++++++++++++++++++ example_integration/lcd_backlight_hal.c | 90 +++++++ example_integration/visualizer_user.c | 135 ++++++++++ readme.md | 19 +- 6 files changed, 626 insertions(+), 1 deletion(-) create mode 100644 example_integration/callbacks.c create mode 100644 example_integration/gfxconf.h create mode 100644 example_integration/lcd_backlight_hal.c create mode 100644 example_integration/visualizer_user.c diff --git a/LICENSE.md b/LICENSE.md index d7cc3198cb..22d4c3f08b 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,3 +1,11 @@ +The files in this project are licensed under the MIT license +It uses the following libraries +uGFX - with it's own license, see the license.html file in the uGFX subfolder for more information +tmk_core - is indirectly used and not included in the repository. It's licensed under the GPLv2 license +Chibios - which is used by tmk_core is licensed under GPLv3. + +Therefore the effective license for any project using the library is GPLv3 + The MIT License (MIT) Copyright (c) 2016 Fred Sundvik diff --git a/example_integration/callbacks.c b/example_integration/callbacks.c new file mode 100644 index 0000000000..21b2d9f748 --- /dev/null +++ b/example_integration/callbacks.c @@ -0,0 +1,50 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "keyboard.h" +#include "action_layer.h" +#include "visualizer.h" +#include "host.h" + +void post_keyboard_init(void) { + visualizer_init(); +} + +void post_keyboard_task() { + visualizer_set_state(default_layer_state, layer_state, host_keyboard_leds()); +} diff --git a/example_integration/gfxconf.h b/example_integration/gfxconf.h new file mode 100644 index 0000000000..304c5d187a --- /dev/null +++ b/example_integration/gfxconf.h @@ -0,0 +1,325 @@ +/** + * This file has a different license to the rest of the uGFX system. + * You can copy, modify and distribute this file as you see fit. + * You do not need to publish your source modifications to this file. + * The only thing you are not permitted to do is to relicense it + * under a different license. + */ + +/** + * Copy this file into your project directory and rename it as gfxconf.h + * Edit your copy to turn on the uGFX features you want to use. + * The values below are the defaults. + * + * Only remove the comments from lines where you want to change the + * default value. This allows definitions to be included from + * driver makefiles when required and provides the best future + * compatibility for your project. + * + * Please use spaces instead of tabs in this file. + */ + +#ifndef _GFXCONF_H +#define _GFXCONF_H + + +/////////////////////////////////////////////////////////////////////////// +// GOS - One of these must be defined, preferably in your Makefile // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_OS_CHIBIOS TRUE +//#define GFX_USE_OS_FREERTOS FALSE +// #define GFX_FREERTOS_USE_TRACE FALSE +//#define GFX_USE_OS_WIN32 FALSE +//#define GFX_USE_OS_LINUX FALSE +//#define GFX_USE_OS_OSX FALSE +//#define GFX_USE_OS_ECOS FALSE +//#define GFX_USE_OS_RAWRTOS FALSE +//#define GFX_USE_OS_ARDUINO FALSE +//#define GFX_USE_OS_KEIL FALSE +//#define GFX_USE_OS_CMSIS FALSE +//#define GFX_USE_OS_RAW32 FALSE +// #define INTERRUPTS_OFF() optional_code +// #define INTERRUPTS_ON() optional_code +// These are not defined by default for some reason +#define GOS_NEED_X_THREADS FALSE +#define GOS_NEED_X_HEAP FALSE + +// Options that (should where relevant) apply to all operating systems + #define GFX_NO_INLINE FALSE +// #define GFX_COMPILER GFX_COMPILER_UNKNOWN +// #define GFX_CPU GFX_CPU_UNKNOWN +// #define GFX_OS_HEAP_SIZE 0 +// #define GFX_OS_NO_INIT FALSE +// #define GFX_OS_INIT_NO_WARNING FALSE +// #define GFX_OS_PRE_INIT_FUNCTION myHardwareInitRoutine +// #define GFX_OS_EXTRA_INIT_FUNCTION myOSInitRoutine +// #define GFX_OS_EXTRA_DEINIT_FUNCTION myOSDeInitRoutine + + +/////////////////////////////////////////////////////////////////////////// +// GDISP // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GDISP TRUE + +//#define GDISP_NEED_AUTOFLUSH FALSE +//#define GDISP_NEED_TIMERFLUSH FALSE +//#define GDISP_NEED_VALIDATION TRUE +//#define GDISP_NEED_CLIP TRUE +//#define GDISP_NEED_CIRCLE FALSE +//#define GDISP_NEED_ELLIPSE FALSE +//#define GDISP_NEED_ARC FALSE +//#define GDISP_NEED_ARCSECTORS FALSE +//#define GDISP_NEED_CONVEX_POLYGON FALSE +//#define GDISP_NEED_SCROLL FALSE +//#define GDISP_NEED_PIXELREAD FALSE +//#define GDISP_NEED_CONTROL FALSE +//#define GDISP_NEED_QUERY FALSE +//#define GDISP_NEED_MULTITHREAD FALSE +//#define GDISP_NEED_STREAMING FALSE +#define GDISP_NEED_TEXT TRUE +// #define GDISP_NEED_TEXT_WORDWRAP FALSE +// #define GDISP_NEED_ANTIALIAS FALSE +// #define GDISP_NEED_UTF8 FALSE + #define GDISP_NEED_TEXT_KERNING TRUE +// #define GDISP_INCLUDE_FONT_UI1 FALSE +// #define GDISP_INCLUDE_FONT_UI2 FALSE // The smallest preferred font. +// #define GDISP_INCLUDE_FONT_LARGENUMBERS FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS10 FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS12 FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS16 FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS20 FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS24 FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS32 FALSE + #define GDISP_INCLUDE_FONT_DEJAVUSANSBOLD12 TRUE +// #define GDISP_INCLUDE_FONT_FIXED_10X20 FALSE +// #define GDISP_INCLUDE_FONT_FIXED_7X14 FALSE + #define GDISP_INCLUDE_FONT_FIXED_5X8 TRUE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS12_AA FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS16_AA FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS20_AA FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS24_AA FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANS32_AA FALSE +// #define GDISP_INCLUDE_FONT_DEJAVUSANSBOLD12_AA FALSE +// #define GDISP_INCLUDE_USER_FONTS FALSE + +//#define GDISP_NEED_IMAGE FALSE +// #define GDISP_NEED_IMAGE_NATIVE FALSE +// #define GDISP_NEED_IMAGE_GIF FALSE +// #define GDISP_NEED_IMAGE_BMP FALSE +// #define GDISP_NEED_IMAGE_BMP_1 FALSE +// #define GDISP_NEED_IMAGE_BMP_4 FALSE +// #define GDISP_NEED_IMAGE_BMP_4_RLE FALSE +// #define GDISP_NEED_IMAGE_BMP_8 FALSE +// #define GDISP_NEED_IMAGE_BMP_8_RLE FALSE +// #define GDISP_NEED_IMAGE_BMP_16 FALSE +// #define GDISP_NEED_IMAGE_BMP_24 FALSE +// #define GDISP_NEED_IMAGE_BMP_32 FALSE +// #define GDISP_NEED_IMAGE_JPG FALSE +// #define GDISP_NEED_IMAGE_PNG FALSE +// #define GDISP_NEED_IMAGE_ACCOUNTING FALSE + +//#define GDISP_NEED_PIXMAP FALSE +// #define GDISP_NEED_PIXMAP_IMAGE FALSE + +//#define GDISP_DEFAULT_ORIENTATION GDISP_ROTATE_LANDSCAPE // If not defined the native hardware orientation is used. +//#define GDISP_LINEBUF_SIZE 128 +//#define GDISP_STARTUP_COLOR Black +#define GDISP_NEED_STARTUP_LOGO FALSE + +//#define GDISP_TOTAL_DISPLAYS 1 + +//#define GDISP_DRIVER_LIST GDISPVMT_Win32, GDISPVMT_Win32 +// #ifdef GDISP_DRIVER_LIST +// // For code and speed optimization define as TRUE or FALSE if all controllers have the same capability +// #define GDISP_HARDWARE_STREAM_WRITE FALSE +// #define GDISP_HARDWARE_STREAM_READ FALSE +// #define GDISP_HARDWARE_STREAM_POS FALSE +// #define GDISP_HARDWARE_DRAWPIXEL FALSE +// #define GDISP_HARDWARE_CLEARS FALSE +// #define GDISP_HARDWARE_FILLS FALSE +// #define GDISP_HARDWARE_BITFILLS FALSE +// #define GDISP_HARDWARE_SCROLL FALSE +// #define GDISP_HARDWARE_PIXELREAD FALSE +// #define GDISP_HARDWARE_CONTROL FALSE +// #define GDISP_HARDWARE_QUERY FALSE +// #define GDISP_HARDWARE_CLIP FALSE + + #define GDISP_PIXELFORMAT GDISP_PIXELFORMAT_RGB888 +// #endif + +// The custom format is not defined for some reason, so define it as error +// so we don't get compiler warnings +#define GDISP_PIXELFORMAT_CUSTOM GDISP_PIXELFORMAT_ERROR + +#define GDISP_USE_GFXNET FALSE +// #define GDISP_GFXNET_PORT 13001 +// #define GDISP_GFXNET_CUSTOM_LWIP_STARTUP FALSE +// #define GDISP_DONT_WAIT_FOR_NET_DISPLAY FALSE +// #define GDISP_GFXNET_UNSAFE_SOCKETS FALSE + + +/////////////////////////////////////////////////////////////////////////// +// GWIN // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GWIN FALSE + +//#define GWIN_NEED_WINDOWMANAGER FALSE +// #define GWIN_REDRAW_IMMEDIATE FALSE +// #define GWIN_REDRAW_SINGLEOP FALSE +// #define GWIN_NEED_FLASHING FALSE +// #define GWIN_FLASHING_PERIOD 250 + +//#define GWIN_NEED_CONSOLE FALSE +// #define GWIN_CONSOLE_USE_HISTORY FALSE +// #define GWIN_CONSOLE_HISTORY_AVERAGING FALSE +// #define GWIN_CONSOLE_HISTORY_ATCREATE FALSE +// #define GWIN_CONSOLE_ESCSEQ FALSE +// #define GWIN_CONSOLE_USE_BASESTREAM FALSE +// #define GWIN_CONSOLE_USE_FLOAT FALSE +//#define GWIN_NEED_GRAPH FALSE +//#define GWIN_NEED_GL3D FALSE + +//#define GWIN_NEED_WIDGET FALSE +//#define GWIN_FOCUS_HIGHLIGHT_WIDTH 1 +// #define GWIN_NEED_LABEL FALSE +// #define GWIN_LABEL_ATTRIBUTE FALSE +// #define GWIN_NEED_BUTTON FALSE +// #define GWIN_BUTTON_LAZY_RELEASE FALSE +// #define GWIN_NEED_SLIDER FALSE +// #define GWIN_SLIDER_NOSNAP FALSE +// #define GWIN_SLIDER_DEAD_BAND 5 +// #define GWIN_SLIDER_TOGGLE_INC 20 +// #define GWIN_NEED_CHECKBOX FALSE +// #define GWIN_NEED_IMAGE FALSE +// #define GWIN_NEED_IMAGE_ANIMATION FALSE +// #define GWIN_NEED_RADIO FALSE +// #define GWIN_NEED_LIST FALSE +// #define GWIN_NEED_LIST_IMAGES FALSE +// #define GWIN_NEED_PROGRESSBAR FALSE +// #define GWIN_PROGRESSBAR_AUTO FALSE +// #define GWIN_NEED_KEYBOARD FALSE +// #define GWIN_KEYBOARD_DEFAULT_LAYOUT VirtualKeyboard_English1 +// #define GWIN_NEED_KEYBOARD_ENGLISH1 TRUE +// #define GWIN_NEED_TEXTEDIT FALSE +// #define GWIN_FLAT_STYLING FALSE +// #define GWIN_WIDGET_TAGS FALSE + +//#define GWIN_NEED_CONTAINERS FALSE +// #define GWIN_NEED_CONTAINER FALSE +// #define GWIN_NEED_FRAME FALSE +// #define GWIN_NEED_TABSET FALSE +// #define GWIN_TABSET_TABHEIGHT 18 + + +/////////////////////////////////////////////////////////////////////////// +// GEVENT // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GEVENT FALSE + +//#define GEVENT_ASSERT_NO_RESOURCE FALSE +//#define GEVENT_MAXIMUM_SIZE 32 +//#define GEVENT_MAX_SOURCE_LISTENERS 32 + + +/////////////////////////////////////////////////////////////////////////// +// GTIMER // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GTIMER FALSE + +//#define GTIMER_THREAD_PRIORITY HIGH_PRIORITY +//#define GTIMER_THREAD_WORKAREA_SIZE 2048 + + +/////////////////////////////////////////////////////////////////////////// +// GQUEUE // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GQUEUE FALSE + +//#define GQUEUE_NEED_ASYNC FALSE +//#define GQUEUE_NEED_GSYNC FALSE +//#define GQUEUE_NEED_FSYNC FALSE +//#define GQUEUE_NEED_BUFFERS FALSE + +/////////////////////////////////////////////////////////////////////////// +// GINPUT // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GINPUT FALSE + +//#define GINPUT_NEED_MOUSE FALSE +// #define GINPUT_TOUCH_STARTRAW FALSE +// #define GINPUT_TOUCH_NOTOUCH FALSE +// #define GINPUT_TOUCH_NOCALIBRATE FALSE +// #define GINPUT_TOUCH_NOCALIBRATE_GUI FALSE +// #define GINPUT_MOUSE_POLL_PERIOD 25 +// #define GINPUT_MOUSE_CLICK_TIME 300 +// #define GINPUT_TOUCH_CXTCLICK_TIME 700 +// #define GINPUT_TOUCH_USER_CALIBRATION_LOAD FALSE +// #define GINPUT_TOUCH_USER_CALIBRATION_SAVE FALSE +// #define GMOUSE_DRIVER_LIST GMOUSEVMT_Win32, GMOUSEVMT_Win32 +//#define GINPUT_NEED_KEYBOARD FALSE +// #define GINPUT_KEYBOARD_POLL_PERIOD 200 +// #define GKEYBOARD_DRIVER_LIST GKEYBOARDVMT_Win32, GKEYBOARDVMT_Win32 +// #define GKEYBOARD_LAYOUT_OFF FALSE +// #define GKEYBOARD_LAYOUT_SCANCODE2_US FALSE +//#define GINPUT_NEED_TOGGLE FALSE +//#define GINPUT_NEED_DIAL FALSE + + +/////////////////////////////////////////////////////////////////////////// +// GFILE // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GFILE FALSE + +//#define GFILE_NEED_PRINTG FALSE +//#define GFILE_NEED_SCANG FALSE +//#define GFILE_NEED_STRINGS FALSE +//#define GFILE_NEED_FILELISTS FALSE +//#define GFILE_NEED_STDIO FALSE +//#define GFILE_NEED_NOAUTOMOUNT FALSE +//#define GFILE_NEED_NOAUTOSYNC FALSE + +//#define GFILE_NEED_MEMFS FALSE +//#define GFILE_NEED_ROMFS FALSE +//#define GFILE_NEED_RAMFS FALSE +//#define GFILE_NEED_FATFS FALSE +//#define GFILE_NEED_NATIVEFS FALSE +//#define GFILE_NEED_CHBIOSFS FALSE + +//#define GFILE_ALLOW_FLOATS FALSE +//#define GFILE_ALLOW_DEVICESPECIFIC FALSE +//#define GFILE_MAX_GFILES 3 + +/////////////////////////////////////////////////////////////////////////// +// GADC // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GADC FALSE + +//#define GADC_MAX_LOWSPEED_DEVICES 4 + + +/////////////////////////////////////////////////////////////////////////// +// GAUDIO // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GAUDIO FALSE +// There seems to be a bug in the ugfx code, the wrong define is used +// So define it in order to avoid warnings +#define GFX_USE_GAUDIN GFX_USE_GAUDIO +// #define GAUDIO_NEED_PLAY FALSE +// #define GAUDIO_NEED_RECORD FALSE + + +/////////////////////////////////////////////////////////////////////////// +// GMISC // +/////////////////////////////////////////////////////////////////////////// +#define GFX_USE_GMISC FALSE + +//#define GMISC_NEED_ARRAYOPS FALSE +//#define GMISC_NEED_FASTTRIG FALSE +//#define GMISC_NEED_FIXEDTRIG FALSE +//#define GMISC_NEED_INVSQRT FALSE +// #define GMISC_INVSQRT_MIXED_ENDIAN FALSE +// #define GMISC_INVSQRT_REAL_SLOW FALSE +//#define GMISC_NEED_MATRIXFLOAT2D FALSE +//#define GMISC_NEED_MATRIXFIXED2D FALSE + +#endif /* _GFXCONF_H */ diff --git a/example_integration/lcd_backlight_hal.c b/example_integration/lcd_backlight_hal.c new file mode 100644 index 0000000000..0f35cb6755 --- /dev/null +++ b/example_integration/lcd_backlight_hal.c @@ -0,0 +1,90 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#include "lcd_backlight.h" +#include "hal.h" + +#define RED_PIN 1 +#define GREEN_PIN 2 +#define BLUE_PIN 3 +#define CHANNEL_RED FTM0->CHANNEL[0] +#define CHANNEL_GREEN FTM0->CHANNEL[1] +#define CHANNEL_BLUE FTM0->CHANNEL[2] + +#define RGB_PORT PORTC +#define RGB_PORT_GPIO GPIOC + +// Base FTM clock selection (72 MHz system clock) +// @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period +// Higher pre-scalar will use the most power (also look the best) +// Pre-scalar calculations +// 0 - 72 MHz -> 549 Hz +// 1 - 36 MHz -> 275 Hz +// 2 - 18 MHz -> 137 Hz +// 3 - 9 MHz -> 69 Hz (Slightly visible flicker) +// 4 - 4 500 kHz -> 34 Hz (Visible flickering) +// 5 - 2 250 kHz -> 17 Hz +// 6 - 1 125 kHz -> 9 Hz +// 7 - 562 500 Hz -> 4 Hz +// Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced +// Which will reduce the brightness range +#define PRESCALAR_DEFINE 0 + +void lcd_backlight_hal_init(void) { + // Setup Backlight + SIM->SCGC6 |= SIM_SCGC6_FTM0; + FTM0->CNT = 0; // Reset counter + + // PWM Period + // 16-bit maximum + FTM0->MOD = 0xFFFF; + + // Set FTM to PWM output - Edge Aligned, Low-true pulses +#define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0) + CHANNEL_RED.CnSC = CNSC_MODE; + CHANNEL_GREEN.CnSC = CNSC_MODE; + CHANNEL_BLUE.CnSC = CNSC_MODE; + + // System clock, /w prescalar setting + FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE); + + CHANNEL_RED.CnV = 0; + CHANNEL_GREEN.CnV = 0; + CHANNEL_BLUE.CnV = 0; + + RGB_PORT_GPIO->PDDR |= (1 << RED_PIN); + RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN); + RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN); + +#define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4) + RGB_PORT->PCR[RED_PIN] = RGB_MODE; + RGB_PORT->PCR[GREEN_PIN] = RGB_MODE; + RGB_PORT->PCR[BLUE_PIN] = RGB_MODE; +} + +void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) { + CHANNEL_RED.CnV = r; + CHANNEL_GREEN.CnV = g; + CHANNEL_BLUE.CnV = b; +} + diff --git a/example_integration/visualizer_user.c b/example_integration/visualizer_user.c new file mode 100644 index 0000000000..a50ecc60da --- /dev/null +++ b/example_integration/visualizer_user.c @@ -0,0 +1,135 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +// Currently we are assuming that both the backlight and LCD are enabled +// But it's entirely possible to write a custom visualizer that use only +// one of them +#ifndef LCD_BACKLIGHT_ENABLE +#error This visualizer needs that LCD backlight is enabled +#endif + +#ifndef LCD_ENABLE +#error This visualizer needs that LCD is enabled +#endif + +#include "visualizer.h" + +static const char* welcome_text[] = {"TMK", "Infinity Ergodox"}; + +// Just an example how to write custom keyframe functions, we could have moved +// all this into the init function +bool display_welcome(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + // Read the uGFX documentation for information how to use the displays + // http://wiki.ugfx.org/index.php/Main_Page + gdispClear(White); + // You can use static variables for things that can't be found in the animation + // or state structs + gdispDrawString(0, 3, welcome_text[0], state->font_dejavusansbold12, Black); + gdispDrawString(0, 15, welcome_text[1], state->font_dejavusansbold12, Black); + // Always remember to flush the display + gdispFlush(); + // you could set the backlight color as well, but we won't do it here, since + // it's part of the following animation + // lcd_backlight_color(hue, saturation, intensity); + // We don't need constant updates, just drawing the screen once is enough + return false; +} + +// Feel free to modify the animations below, or even add new ones if needed + +// Don't worry, if the startup animation is long, you can use the keyboard like normal +// during that time +static keyframe_animation_t startup_animation = { + .num_frames = 4, + .loop = false, + .frame_lengths = {0, MS2ST(1000), MS2ST(5000), 0}, + .frame_functions = {display_welcome, keyframe_animate_backlight_color, keyframe_no_operation, user_visualizer_inited}, +}; + +// The color animation animates the LCD color when you change layers +static keyframe_animation_t color_animation = { + .num_frames = 2, + .loop = false, + // Note that there's a 200 ms no-operation frame, + // this prevents the color from changing when activating the layer + // momentarily + .frame_lengths = {MS2ST(200), MS2ST(500)}, + .frame_functions = {keyframe_no_operation, keyframe_animate_backlight_color}, +}; + +// The LCD animation alternates between the layer name display and a +// bitmap that displays all active layers +static keyframe_animation_t lcd_animation = { + .num_frames = 2, + .loop = true, + .frame_lengths = {MS2ST(2000), MS2ST(2000)}, + .frame_functions = {keyframe_display_layer_text, keyframe_display_layer_bitmap}, +}; + +void initialize_user_visualizer(visualizer_state_t* state) { + // The brightness will be dynamically adjustable in the future + // But for now, change it here. + lcd_backlight_brightness(0x50); + state->current_lcd_color = LCD_COLOR(0x00, 0x00, 0xFF); + state->target_lcd_color = LCD_COLOR(0x10, 0xFF, 0xFF); + start_keyframe_animation(&startup_animation); +} + +void update_user_visualizer_state(visualizer_state_t* state) { + // Add more tests, change the colors and layer texts here + // Usually you want to check the high bits (higher layers first) + // because that's the order layers are processed for keypresses + // You can for check for example: + // state->status.layer + // state->status.default_layer + // state->status.leds (see led.h for available statuses) + if (state->status.layer & 0x2) { + state->target_lcd_color = LCD_COLOR(0xA0, 0xB0, 0xFF); + state->layer_text = "Layer 2"; + } + else { + state->target_lcd_color = LCD_COLOR(0x50, 0xB0, 0xFF); + state->layer_text = "Layer 1"; + } + // You can also stop existing animations, and start your custom ones here + // remember that you should normally have only one animation for the LCD + // and one for the background. But you can also combine them if you want. + start_keyframe_animation(&lcd_animation); + start_keyframe_animation(&color_animation); +} diff --git a/readme.md b/readme.md index 82bf8bfdfa..545ba22707 100644 --- a/readme.md +++ b/readme.md @@ -1 +1,18 @@ -A visualization library for the TMK keyboard firmware +# A visualization library for the TMK keyboard firmware + +This library is designed to work together with the [TMK keyboard firmware](https://github.com/tmk/tmk_keyboard). Currently it only works for [Chibios](http://www.chibios.org/) + flavors, but it would be possible to add support for other configurations as well. The LCD display functionality is provided by the [uGFX library](http://www.ugfx.org/). + +## To use this library as a user +You can and should modify the visualizer\_user.c file. Check the comments in the file for more information. + +## To add this library to custom keyboard projects + +1. Add tmk_visualizer as a submodule to your project +1. Set VISUALIZER_DIR in the main keyboard project makefile to point to the submodule +1. Define LCD\_ENABLE and/or LCD\_BACKLIGHT\_ENABLE, to enable support +1. Include the visualizer.mk make file +1. Copy the files in the example\_integration folder to your keyboard project +1. All other files than the callback.c file are included automatically, so you will need to add callback.c to your makefile manually. If you already have a similar file in your project, you can just copy the functions instead of the whole file. +1. Edit the files to match your hardware. You might might want to read the Chibios and UGfx documentation, for more information. +1. If you enable LCD support you might also have to write a custom uGFX display driver, check the uGFX documentation for that. You probably also want to enable SPI support in your Chibios configuration. From c5db02925c6bf73ef1df1f718e09ff304266a535 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 13 Feb 2016 22:19:49 +0200 Subject: [PATCH 010/109] Clean up wrong license information --- example_integration/callbacks.c | 14 -------------- example_integration/lcd_backlight_hal.c | 1 + example_integration/visualizer_user.c | 14 -------------- 3 files changed, 1 insertion(+), 28 deletions(-) diff --git a/example_integration/callbacks.c b/example_integration/callbacks.c index 21b2d9f748..2539615d65 100644 --- a/example_integration/callbacks.c +++ b/example_integration/callbacks.c @@ -22,20 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - #include "keyboard.h" #include "action_layer.h" #include "visualizer.h" diff --git a/example_integration/lcd_backlight_hal.c b/example_integration/lcd_backlight_hal.c index 0f35cb6755..913131b169 100644 --- a/example_integration/lcd_backlight_hal.c +++ b/example_integration/lcd_backlight_hal.c @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + #include "lcd_backlight.h" #include "hal.h" diff --git a/example_integration/visualizer_user.c b/example_integration/visualizer_user.c index a50ecc60da..6c4619d954 100644 --- a/example_integration/visualizer_user.c +++ b/example_integration/visualizer_user.c @@ -22,20 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - // Currently we are assuming that both the backlight and LCD are enabled // But it's entirely possible to write a custom visualizer that use only // one of them From ffc425603dcbedf0da1d6aa1759f8f14f3a5e841 Mon Sep 17 00:00:00 2001 From: fredizzimo Date: Sat, 13 Feb 2016 23:26:39 +0200 Subject: [PATCH 011/109] Initial commit --- LICENSE | 21 +++++++++++++++++++++ README.md | 1 + 2 files changed, 22 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..d66406efdf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 fredizzimo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000000..94af9125ce --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# tmk_serial_link \ No newline at end of file From e70cefc12fb897f7221f7b1bc6fe0ce595eb2de1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 00:05:58 +0200 Subject: [PATCH 012/109] Add cgreen unit test library and makefile --- .gitmodules | 3 +++ cgreen/Makefile | 38 ++++++++++++++++++++++++++++++++++++++ cgreen/Makefile.build | 33 +++++++++++++++++++++++++++++++++ cgreen/cgreen | 1 + serial_link.mk | 11 +++++++++++ 5 files changed, 86 insertions(+) create mode 100644 .gitmodules create mode 100644 cgreen/Makefile create mode 100644 cgreen/Makefile.build create mode 160000 cgreen/cgreen create mode 100644 serial_link.mk diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..991cfe96d7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cgreen/cgreen"] + path = cgreen/cgreen + url = http://github.com/cgreen-devs/cgreen diff --git a/cgreen/Makefile b/cgreen/Makefile new file mode 100644 index 0000000000..6b31a3f923 --- /dev/null +++ b/cgreen/Makefile @@ -0,0 +1,38 @@ +# This Makefile ensures that the build is made out of source in a subdirectory called 'build' +# If it doesn't exist, it is created and a Makefile created there (from Makefile.build) +# +# This Makefile also contains delegation of the most common make commands +# +# If you have cmake installed you should be able to do: +# +# make +# make test +# make install +# make package +# +# That should build cgreen for C and C++, run some tests, install it locally and +# generate two distributable packages. + +all: build + cd $(CGREEN_BUILD_DIR); make all + +test: build + cd $(CGREEN_BUILD_DIR); make test + +clean: build + cd $(CGREEN_BUILD_DIR); make clean + +package: build + cd $(CGREEN_BUILD_DIR); make package + +install: + cd $(CGREEN_BUILD_DIR); make install + +############# Internal + +build: + mkdir -p $(CGREEN_BUILD_DIR) + cp Makefile.build $(CGREEN_BUILD_DIR)/Makefile + + +.SILENT: diff --git a/cgreen/Makefile.build b/cgreen/Makefile.build new file mode 100644 index 0000000000..f76165244c --- /dev/null +++ b/cgreen/Makefile.build @@ -0,0 +1,33 @@ +# This Makefile is copied from the cgreen top directory (where it is +# named Makefile.build) and put in a subdirectory called 'build' where +# builds are made This Makefile then automatically creates +# subdirectories for C and C++ builds configuring them using the cmake +# command. Once created you can always tweak the cmake setup as with +# any cmake build directory + +all: build-c build-c++ + for d in build-* ; do cd $$d; make ; cd .. ; done + +clean: + for d in build-* ; do cd $$d; make clean ; cd .. ; done + +check test: + for d in build-* ; do cd $$d; make check ; cd .. ; done + +package: + for d in build-* ; do cd $$d; make package ; cd .. ; done + +install: + for d in build-* ; do cd $$d; make install ; cd .. ; done + +############ Internal + +build-c: + mkdir build-c + cd build-c; cmake -G "Unix Makefiles" $(CGREEN_DIR) + +build-c++: + mkdir build-c++ + cd build-c++; cmake -G "Unix Makefiles" -DWITH_CXX:bool=ON $(CGREEN_DIR) + +.SILENT: diff --git a/cgreen/cgreen b/cgreen/cgreen new file mode 160000 index 0000000000..d4d438dda1 --- /dev/null +++ b/cgreen/cgreen @@ -0,0 +1 @@ +Subproject commit d4d438dda1b7131f0bd0530b2c258e9dea6a2a9f diff --git a/serial_link.mk b/serial_link.mk new file mode 100644 index 0000000000..51d33fcb41 --- /dev/null +++ b/serial_link.mk @@ -0,0 +1,11 @@ +CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a + +.phony testserial: +testserial: $(CGREEN_LIB) + +CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" +CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" +export CGREEN_DIR +export CGREEN_BUILD_DIR +$(CGREEN_LIB): + make -C $(SERIAL_DIR)/cgreen \ No newline at end of file From e9a1474bb6a92024c8d4e3095d6b871833db1cf4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 00:20:14 +0200 Subject: [PATCH 013/109] Add makefile for compiling and running unit tests --- serial_link.mk | 1 + tests/Makefile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/Makefile diff --git a/serial_link.mk b/serial_link.mk index 51d33fcb41..8e8de807f5 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -2,6 +2,7 @@ CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a .phony testserial: testserial: $(CGREEN_LIB) + make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000000..239e9f6093 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,49 @@ +CC = gcc +CFLAGS = +INCLUDES = -I. +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src +LDLIBS = -lcgreen +UNITOBJ = $(BUILDDIR)/testserial/unitobj +DEPDIR = $(BUILDDIR)/testserial/unit.d +UNITEXE = $(BUILDDIR)/testserial/unitexe +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +EXT = +UNAME := $(shell uname) +ifneq (, $(findstring mingw, $(UNAME))) + EXT += exe +endif +ifneq (, $(findstring cygwin, $(UNAME))) + EXT += exe +endif + +SRC = $(wildcard *.c) +EXE = $(patsubst %.c, $(UNITEXE)/%$(EXT), $(SRC)) +$(shell mkdir -p $(DEPDIR) >/dev/null) + +test: $(EXE) + @for f in $^; do \ + echo "++++++++++++++++++"; \ + echo "Running unit tests"; \ + echo $$(basename $$f); \ + echo "++++++++++++++++++"; \ + echo ""; \ + $$f || exit 1; \ + echo ""; \ + echo "******************"; \ + echo ""; \ + done + +$(UNITEXE)/%$(EXT): $(UNITOBJ)/%.o + mkdir -p $(UNITEXE) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +$(UNITOBJ)/%.o : %.c +$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d + mkdir -p $(UNITOBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ + mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(DEPDIR)/%.d: ; +.PRECIOUS: $(DEPDIR)/%.d + +-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC).c)) \ No newline at end of file From 3c6b93a491ab6ea280c30be26980e2ca91ba5c4c Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 01:31:01 +0200 Subject: [PATCH 014/109] Add separate makefile for tests --- serial_link.mk | 12 ------------ serial_link_tests.mk | 13 +++++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 serial_link_tests.mk diff --git a/serial_link.mk b/serial_link.mk index 8e8de807f5..e69de29bb2 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -1,12 +0,0 @@ -CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a - -.phony testserial: -testserial: $(CGREEN_LIB) - make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) - -CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" -CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" -export CGREEN_DIR -export CGREEN_BUILD_DIR -$(CGREEN_LIB): - make -C $(SERIAL_DIR)/cgreen \ No newline at end of file diff --git a/serial_link_tests.mk b/serial_link_tests.mk new file mode 100644 index 0000000000..36d7c09b3f --- /dev/null +++ b/serial_link_tests.mk @@ -0,0 +1,13 @@ +CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a + +CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" +CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" +export CGREEN_DIR +export CGREEN_BUILD_DIR +$(CGREEN_LIB): + make -C $(SERIAL_DIR)/cgreen + +.PHONY serialtest: +serialtest : $(CGREEN_LIB) + @echo $(CGREEN_LIB) + make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) \ No newline at end of file From 3eaddd65141f880ddc7d23fc05cafb01ca5477ac Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 01:58:26 +0200 Subject: [PATCH 015/109] Use cgreen test runner instead of executables --- tests/Makefile | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 239e9f6093..3f89cba471 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,40 +1,30 @@ CC = gcc CFLAGS = INCLUDES = -I. -LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared LDLIBS = -lcgreen -UNITOBJ = $(BUILDDIR)/testserial/unitobj -DEPDIR = $(BUILDDIR)/testserial/unit.d -UNITEXE = $(BUILDDIR)/testserial/unitexe +UNITOBJ = $(BUILDDIR)/serialtest/unitobj +DEPDIR = $(BUILDDIR)/serialtest/unit.d +UNITTESTS = $(BUILDDIR)/serialtest/unittests DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td -EXT = +EXT = .so UNAME := $(shell uname) -ifneq (, $(findstring mingw, $(UNAME))) - EXT += exe +ifneq (, $(findstring MINGW, $(UNAME))) + EXT = .dll endif -ifneq (, $(findstring cygwin, $(UNAME))) - EXT += exe +ifneq (, $(findstring CYGWIN, $(UNAME))) + EXT = .dll endif SRC = $(wildcard *.c) -EXE = $(patsubst %.c, $(UNITEXE)/%$(EXT), $(SRC)) +TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) $(shell mkdir -p $(DEPDIR) >/dev/null) -test: $(EXE) - @for f in $^; do \ - echo "++++++++++++++++++"; \ - echo "Running unit tests"; \ - echo $$(basename $$f); \ - echo "++++++++++++++++++"; \ - echo ""; \ - $$f || exit 1; \ - echo ""; \ - echo "******************"; \ - echo ""; \ - done +test: $(TESTFILES) + @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) -$(UNITEXE)/%$(EXT): $(UNITOBJ)/%.o - mkdir -p $(UNITEXE) +$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o + mkdir -p $(UNITTESTS) $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(UNITOBJ)/%.o : %.c From 1f1bc183dd1080e14a2d85010d5c7f6455f023f6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 12:45:12 +0200 Subject: [PATCH 016/109] Start implementing data_link --- LICENSE | 2 +- serial_link.mk | 23 +++++++++++ serial_link/protocol/data_link.c | 29 ++++++++++++++ serial_link/protocol/data_link.h | 25 ++++++++++++ serial_link/protocol/routing.h | 25 ++++++++++++ serial_link/tests/Makefile | 61 +++++++++++++++++++++++++++++ serial_link/tests/data_link_tests.c | 52 ++++++++++++++++++++++++ serial_link_tests.mk | 27 +++++++++++-- tests/Makefile | 39 ------------------ 9 files changed, 240 insertions(+), 43 deletions(-) create mode 100644 serial_link/protocol/data_link.c create mode 100644 serial_link/protocol/data_link.h create mode 100644 serial_link/protocol/routing.h create mode 100644 serial_link/tests/Makefile create mode 100644 serial_link/tests/data_link_tests.c delete mode 100644 tests/Makefile diff --git a/LICENSE b/LICENSE index d66406efdf..d7cc3198cb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 fredizzimo +Copyright (c) 2016 Fred Sundvik Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/serial_link.mk b/serial_link.mk index e69de29bb2..de2364108a 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -0,0 +1,23 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +INC += $(SERIAL_DIR) \ No newline at end of file diff --git a/serial_link/protocol/data_link.c b/serial_link/protocol/data_link.c new file mode 100644 index 0000000000..71d538470c --- /dev/null +++ b/serial_link/protocol/data_link.c @@ -0,0 +1,29 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/data_link.h" +#include "protocol/routing.h" + +void recv_byte(uint8_t data) { +} diff --git a/serial_link/protocol/data_link.h b/serial_link/protocol/data_link.h new file mode 100644 index 0000000000..3b9f9ea5d4 --- /dev/null +++ b/serial_link/protocol/data_link.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void recv_byte(uint8_t data); diff --git a/serial_link/protocol/routing.h b/serial_link/protocol/routing.h new file mode 100644 index 0000000000..ee1f9a78ae --- /dev/null +++ b/serial_link/protocol/routing.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void recv_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/tests/Makefile b/serial_link/tests/Makefile new file mode 100644 index 0000000000..0d8ba4b7b6 --- /dev/null +++ b/serial_link/tests/Makefile @@ -0,0 +1,61 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +CC = gcc +CFLAGS = +INCLUDES = -I. -I../ +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared +LDLIBS = -lcgreen +UNITOBJ = $(BUILDDIR)/serialtest/unitobj +DEPDIR = $(BUILDDIR)/serialtest/unit.d +UNITTESTS = $(BUILDDIR)/serialtest/unittests +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +EXT = .so +UNAME := $(shell uname) +ifneq (, $(findstring MINGW, $(UNAME))) + EXT = .dll +endif +ifneq (, $(findstring CYGWIN, $(UNAME))) + EXT = .dll +endif + +SRC = $(wildcard *.c) +TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) +$(shell mkdir -p $(DEPDIR) >/dev/null) + +test: $(TESTFILES) + @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) + +$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o + @mkdir -p $(UNITTESTS) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +$(UNITOBJ)/%.o : %.c +$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d + @mkdir -p $(UNITOBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@ + @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(DEPDIR)/%.d: ; +.PRECIOUS: $(DEPDIR)/%.d + +-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC))) \ No newline at end of file diff --git a/serial_link/tests/data_link_tests.c b/serial_link/tests/data_link_tests.c new file mode 100644 index 0000000000..1b7107e708 --- /dev/null +++ b/serial_link/tests/data_link_tests.c @@ -0,0 +1,52 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include "protocol/data_link.h" +#include "protocol/data_link.c" +#include "protocol/routing.h" + +Describe(DataLink); +BeforeEach(DataLink) {} +AfterEach(DataLink) {} + +void recv_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + +Ensure(DataLink, receives_no_frame_for_a_single_zero_byte) { + never_expect(recv_frame); + recv_byte(0); +} + +Ensure(DataLink, receives_no_frame_for_a_single_FF_byte) { + never_expect(recv_frame); + recv_byte(0xFF); +} + +Ensure(DataLink, receives_no_frame_for_a_single_random_byte) { + never_expect(recv_frame); + recv_byte(0x4A); +} diff --git a/serial_link_tests.mk b/serial_link_tests.mk index 36d7c09b3f..e292f582a9 100644 --- a/serial_link_tests.mk +++ b/serial_link_tests.mk @@ -1,3 +1,25 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" @@ -5,9 +27,8 @@ CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" export CGREEN_DIR export CGREEN_BUILD_DIR $(CGREEN_LIB): - make -C $(SERIAL_DIR)/cgreen + @make -C $(SERIAL_DIR)/cgreen .PHONY serialtest: serialtest : $(CGREEN_LIB) - @echo $(CGREEN_LIB) - make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) \ No newline at end of file + @make -C $(SERIAL_DIR)/serial_link/tests BUILDDIR=../../../$(BUILDDIR) \ No newline at end of file diff --git a/tests/Makefile b/tests/Makefile deleted file mode 100644 index 3f89cba471..0000000000 --- a/tests/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -CC = gcc -CFLAGS = -INCLUDES = -I. -LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared -LDLIBS = -lcgreen -UNITOBJ = $(BUILDDIR)/serialtest/unitobj -DEPDIR = $(BUILDDIR)/serialtest/unit.d -UNITTESTS = $(BUILDDIR)/serialtest/unittests -DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td -EXT = .so -UNAME := $(shell uname) -ifneq (, $(findstring MINGW, $(UNAME))) - EXT = .dll -endif -ifneq (, $(findstring CYGWIN, $(UNAME))) - EXT = .dll -endif - -SRC = $(wildcard *.c) -TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) -$(shell mkdir -p $(DEPDIR) >/dev/null) - -test: $(TESTFILES) - @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) - -$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o - mkdir -p $(UNITTESTS) - $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) - -$(UNITOBJ)/%.o : %.c -$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d - mkdir -p $(UNITOBJ) - $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ - mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d - -$(DEPDIR)/%.d: ; -.PRECIOUS: $(DEPDIR)/%.d - --include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC).c)) \ No newline at end of file From 8a991a266e7f8f9aff48049dffb17eccfa353ca1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 13:05:50 +0200 Subject: [PATCH 017/109] Rename files to match what they actually do Also add validator before routing. --- .../protocol/{data_link.c => byte_stuffer.c} | 4 ++-- .../protocol/{data_link.h => byte_stuffer.h} | 0 .../protocol/{routing.h => frame_validator.h} | 0 ...{data_link_tests.c => byte_stuffer_tests.c} | 18 +++++++++--------- 4 files changed, 11 insertions(+), 11 deletions(-) rename serial_link/protocol/{data_link.c => byte_stuffer.c} (93%) rename serial_link/protocol/{data_link.h => byte_stuffer.h} (100%) rename serial_link/protocol/{routing.h => frame_validator.h} (100%) rename serial_link/tests/{data_link_tests.c => byte_stuffer_tests.c} (78%) diff --git a/serial_link/protocol/data_link.c b/serial_link/protocol/byte_stuffer.c similarity index 93% rename from serial_link/protocol/data_link.c rename to serial_link/protocol/byte_stuffer.c index 71d538470c..95ce86524f 100644 --- a/serial_link/protocol/data_link.c +++ b/serial_link/protocol/byte_stuffer.c @@ -22,8 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/data_link.h" -#include "protocol/routing.h" +#include "protocol/byte_stuffer.h" +#include "protocol/frame_validator.h" void recv_byte(uint8_t data) { } diff --git a/serial_link/protocol/data_link.h b/serial_link/protocol/byte_stuffer.h similarity index 100% rename from serial_link/protocol/data_link.h rename to serial_link/protocol/byte_stuffer.h diff --git a/serial_link/protocol/routing.h b/serial_link/protocol/frame_validator.h similarity index 100% rename from serial_link/protocol/routing.h rename to serial_link/protocol/frame_validator.h diff --git a/serial_link/tests/data_link_tests.c b/serial_link/tests/byte_stuffer_tests.c similarity index 78% rename from serial_link/tests/data_link_tests.c rename to serial_link/tests/byte_stuffer_tests.c index 1b7107e708..418d48f6a4 100644 --- a/serial_link/tests/data_link_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -24,29 +24,29 @@ SOFTWARE. #include #include -#include "protocol/data_link.h" -#include "protocol/data_link.c" -#include "protocol/routing.h" +#include "protocol/byte_stuffer.h" +#include "protocol/byte_stuffer.c" +#include "protocol/frame_validator.h" -Describe(DataLink); -BeforeEach(DataLink) {} -AfterEach(DataLink) {} +Describe(ByteStuffer); +BeforeEach(ByteStuffer) {} +AfterEach(ByteStuffer) {} void recv_frame(uint8_t* data, uint16_t size) { mock(data, size); } -Ensure(DataLink, receives_no_frame_for_a_single_zero_byte) { +Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(recv_frame); recv_byte(0); } -Ensure(DataLink, receives_no_frame_for_a_single_FF_byte) { +Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(recv_frame); recv_byte(0xFF); } -Ensure(DataLink, receives_no_frame_for_a_single_random_byte) { +Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(recv_frame); recv_byte(0x4A); } From ce3a21cbeaa58cfa6b117a8be13be47a69f086ae Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 14:04:51 +0200 Subject: [PATCH 018/109] Implement receive of valid small byte stuffed frames --- serial_link/protocol/byte_stuffer.c | 36 ++++++++++++++- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/tests/byte_stuffer_tests.c | 61 ++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 95ce86524f..7ce01a96ab 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -25,5 +25,39 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" -void recv_byte(uint8_t data) { +// This implements the "Consistent overhead byte stuffing protocol" +// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing +// http://www.stuartcheshire.org/papers/COBSforToN.pdf + +typedef struct byte_stuffer_state { + uint16_t next_zero; + uint16_t data_pos; + uint8_t data[256]; +}byte_stuffer_state_t; + +void init_byte_stuffer_state(byte_stuffer_state_t* state) { + state->next_zero = 0; + state->data_pos = 0; +} + +void recv_byte(byte_stuffer_state_t* state, uint8_t data) { + if (state->next_zero == 0) { + state->next_zero = data; + state->data_pos = 0; + return; + } + + state->next_zero--; + if (data == 0) { + recv_frame(state->data, state->data_pos); + } + else { + if (state->next_zero == 0) { + state->next_zero = data; + state->data[state->data_pos++] = 0; + } + else { + state->data[state->data_pos++] = data; + } + } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 3b9f9ea5d4..9a5551fab5 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,4 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void recv_byte(uint8_t data); +typedef struct byte_stuffer_state byte_stuffer_state_t; +void init_byte_stuffer_state(byte_stuffer_state_t* state); +void recv_byte(byte_stuffer_state_t* state, uint8_t data); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 418d48f6a4..2fc7a0b26c 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -28,8 +28,12 @@ SOFTWARE. #include "protocol/byte_stuffer.c" #include "protocol/frame_validator.h" +byte_stuffer_state_t state; + Describe(ByteStuffer); -BeforeEach(ByteStuffer) {} +BeforeEach(ByteStuffer) { + init_byte_stuffer_state(&state); +} AfterEach(ByteStuffer) {} void recv_frame(uint8_t* data, uint16_t size) { @@ -38,15 +42,64 @@ void recv_frame(uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(recv_frame); - recv_byte(0); + recv_byte(&state, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(recv_frame); - recv_byte(0xFF); + recv_byte(&state, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(recv_frame); - recv_byte(0x4A); + recv_byte(&state, 0x4A); +} + +Ensure(ByteStuffer, receives_single_byte_valid_frame) { + uint8_t expected[] = {0x37}; + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected, 1)) + ); + recv_byte(&state, 2); + recv_byte(&state, 0x37); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_three_bytes_valid_frame) { + uint8_t expected[] = {0x37, 0x99, 0xFF}; + expect(recv_frame, + when(size, is_equal_to(3)), + when(data, is_equal_to_contents_of(expected, 3)) + ); + recv_byte(&state, 5); + recv_byte(&state, 0x37); + recv_byte(&state, 0x99); + recv_byte(&state, 0xFF); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_single_zero_valid_frame) { + uint8_t expected[] = {0}; + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected, 1)) + ); + recv_byte(&state, 1); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { + uint8_t expected[] = {5, 0, 3, 0}; + expect(recv_frame, + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(expected, 4)) + ); + recv_byte(&state, 2); + recv_byte(&state, 5); + recv_byte(&state, 2); + recv_byte(&state, 3); + recv_byte(&state, 1); + recv_byte(&state, 0); } From eefb5b5634e341396fb535f4eeac1323bf716ed0 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 14:25:54 +0200 Subject: [PATCH 019/109] Mutiple frame handling for byte stuffer Also handles unexpected data. --- serial_link/protocol/byte_stuffer.c | 12 +++++- serial_link/tests/byte_stuffer_tests.c | 53 +++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 7ce01a96ab..e578f88dc1 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -41,6 +41,7 @@ void init_byte_stuffer_state(byte_stuffer_state_t* state) { } void recv_byte(byte_stuffer_state_t* state, uint8_t data) { + // Start of a new frame if (state->next_zero == 0) { state->next_zero = data; state->data_pos = 0; @@ -49,10 +50,19 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { state->next_zero--; if (data == 0) { - recv_frame(state->data, state->data_pos); + if (state->next_zero == 0) { + // The frame is completed + recv_frame(state->data, state->data_pos); + } + else { + // The frame is invalid, so reset + state->next_zero = 0; + state->data_pos = 0; + } } else { if (state->next_zero == 0) { + // Special case for zeroes state->next_zero = data; state->data[state->data_pos++] = 0; } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 2fc7a0b26c..74a349b1f3 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -72,7 +72,7 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(&state, 5); + recv_byte(&state, 4); recv_byte(&state, 0x37); recv_byte(&state, 0x99); recv_byte(&state, 0xFF); @@ -103,3 +103,54 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { recv_byte(&state, 1); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_two_valid_frames) { + uint8_t expected1[] = {5, 0}; + uint8_t expected2[] = {3}; + expect(recv_frame, + when(size, is_equal_to(2)), + when(data, is_equal_to_contents_of(expected1, 2)) + ); + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected2, 1)) + ); + recv_byte(&state, 2); + recv_byte(&state, 5); + recv_byte(&state, 1); + recv_byte(&state, 0); + recv_byte(&state, 2); + recv_byte(&state, 3); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { + uint8_t expected[] = {5, 7}; + expect(recv_frame, + when(size, is_equal_to(2)), + when(data, is_equal_to_contents_of(expected, 2)) + ); + recv_byte(&state, 3); + recv_byte(&state, 1); + recv_byte(&state, 0); + recv_byte(&state, 3); + recv_byte(&state, 5); + recv_byte(&state, 7); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { + uint8_t expected[] = {5, 7}; + expect(recv_frame, + when(size, is_equal_to(2)), + when(data, is_equal_to_contents_of(expected, 2)) + ); + recv_byte(&state, 2); + recv_byte(&state, 9); + recv_byte(&state, 4); // This should have been zero + recv_byte(&state, 0); + recv_byte(&state, 3); + recv_byte(&state, 5); + recv_byte(&state, 7); + recv_byte(&state, 0); +} From e8cb6d8023cf2912a08dc1a0a1b108b6dbc429cc Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 15:28:57 +0200 Subject: [PATCH 020/109] Bytestuffer recv handling of long frames --- serial_link/protocol/byte_stuffer.c | 26 ++++++-- serial_link/tests/byte_stuffer_tests.c | 92 ++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index e578f88dc1..cc7afe97ae 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -24,26 +24,35 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" +#include // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing // http://www.stuartcheshire.org/papers/COBSforToN.pdf +#define MAX_FRAME_SIZE 1024 + typedef struct byte_stuffer_state { uint16_t next_zero; uint16_t data_pos; - uint8_t data[256]; + bool long_frame; + uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; void init_byte_stuffer_state(byte_stuffer_state_t* state) { state->next_zero = 0; state->data_pos = 0; + state->long_frame = false; +} + +static void start_frame(byte_stuffer_state_t* state, uint8_t data) { } void recv_byte(byte_stuffer_state_t* state, uint8_t data) { // Start of a new frame if (state->next_zero == 0) { state->next_zero = data; + state->long_frame = data == 0xFF; state->data_pos = 0; return; } @@ -56,15 +65,20 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } else { // The frame is invalid, so reset - state->next_zero = 0; - state->data_pos = 0; + init_byte_stuffer_state(state); } } else { if (state->next_zero == 0) { - // Special case for zeroes - state->next_zero = data; - state->data[state->data_pos++] = 0; + if (state->long_frame) { + state->next_zero = data; + state->long_frame = data == 0xFF; + } + else { + // Special case for zeroes + state->next_zero = data; + state->data[state->data_pos++] = 0; + } } else { state->data[state->data_pos++] = data; diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 74a349b1f3..a28c361938 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -154,3 +154,95 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { recv_byte(&state, 7); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { + uint8_t expected[254]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expect(recv_frame, + when(size, is_equal_to(254)), + when(data, is_equal_to_contents_of(expected, 254)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { + uint8_t expected[255]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expected[254] = 7; + expect(recv_frame, + when(size, is_equal_to(255)), + when(data, is_equal_to_contents_of(expected, 255)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 2); + recv_byte(&state, 7); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { + uint8_t expected[255]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expected[254] = 0; + expect(recv_frame, + when(size, is_equal_to(255)), + when(data, is_equal_to_contents_of(expected, 255)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 1); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { + uint8_t expected[515]; + int i; + int j; + for (j=0;j<2;j++) { + for (i=0;i<254;i++) { + expected[i+254*j] = i + 1; + } + } + for (i=0;i<7;i++) { + expected[254*2+i] = i + 1; + } + expect(recv_frame, + when(size, is_equal_to(515)), + when(data, is_equal_to_contents_of(expected, 510)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 8); + recv_byte(&state, 1); + recv_byte(&state, 2); + recv_byte(&state, 3); + recv_byte(&state, 4); + recv_byte(&state, 5); + recv_byte(&state, 6); + recv_byte(&state, 7); + recv_byte(&state, 0); +} From 26537474ae1d65cdf40276299d7e04648474357b Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 15:57:44 +0200 Subject: [PATCH 021/109] Add byte stuffer recv handling of too long frames --- .gitignore | 1 + serial_link/protocol/byte_stuffer.c | 17 ++++++--- serial_link/tests/byte_stuffer_tests.c | 48 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..2d68e206e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.stackdump diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index cc7afe97ae..f0071b1f77 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -45,9 +45,6 @@ void init_byte_stuffer_state(byte_stuffer_state_t* state) { state->long_frame = false; } -static void start_frame(byte_stuffer_state_t* state, uint8_t data) { -} - void recv_byte(byte_stuffer_state_t* state, uint8_t data) { // Start of a new frame if (state->next_zero == 0) { @@ -61,7 +58,9 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { if (data == 0) { if (state->next_zero == 0) { // The frame is completed - recv_frame(state->data, state->data_pos); + if (state->data_pos > 0) { + recv_frame(state->data, state->data_pos); + } } else { // The frame is invalid, so reset @@ -69,8 +68,16 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } else { - if (state->next_zero == 0) { + if (state->data_pos == MAX_FRAME_SIZE) { + // We exceeded our maximum frame size + // therefore there's nothing else to do than reset to a new frame + state->next_zero = data; + state->long_frame = data == 0xFF; + state->data_pos = 0; + } + else if (state->next_zero == 0) { if (state->long_frame) { + // This is part of a long frame, so continue state->next_zero = data; state->long_frame = data == 0xFF; } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index a28c361938..bfa0193861 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -55,6 +55,12 @@ Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { recv_byte(&state, 0x4A); } +Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { + never_expect(recv_frame); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + Ensure(ByteStuffer, receives_single_byte_valid_frame) { uint8_t expected[] = {0x37}; expect(recv_frame, @@ -246,3 +252,45 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { recv_byte(&state, 7); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { + uint8_t expected[MAX_FRAME_SIZE] = {}; + expect(recv_frame, + when(size, is_equal_to(MAX_FRAME_SIZE)), + when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) + ); + int i; + recv_byte(&state, 1); + for(i=0;i Date: Sun, 14 Feb 2016 17:45:25 +0200 Subject: [PATCH 022/109] Add sending of small frames with no zeroes --- serial_link/protocol/byte_stuffer.c | 12 +++++- serial_link/protocol/byte_stuffer.h | 1 + serial_link/protocol/physical.h | 25 +++++++++++ serial_link/tests/byte_stuffer_tests.c | 60 +++++++++++++++++++------- 4 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 serial_link/protocol/physical.h diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index f0071b1f77..dfd5942ebc 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -24,7 +24,7 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" -#include +#include "protocol/physical.h" // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing @@ -92,3 +92,13 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } } + +void send_frame(uint8_t* data, uint16_t size) { + if (size > 0) { + uint8_t numZeroes = size + 1; + const uint8_t zero = 0; + send_data(&numZeroes, 1); + send_data(data, size); + send_data(&zero, 1); + } +} diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 9a5551fab5..ea6b8451d4 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -25,3 +25,4 @@ SOFTWARE. typedef struct byte_stuffer_state byte_stuffer_state_t; void init_byte_stuffer_state(byte_stuffer_state_t* state); void recv_byte(byte_stuffer_state_t* state, uint8_t data); +void send_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h new file mode 100644 index 0000000000..73a7855dfb --- /dev/null +++ b/serial_link/protocol/physical.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void send_data(const uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index bfa0193861..6e44c9170c 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -27,12 +27,16 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/byte_stuffer.c" #include "protocol/frame_validator.h" +#include "protocol/physical.h" -byte_stuffer_state_t state; +static byte_stuffer_state_t state; +static uint8_t sent_data[MAX_FRAME_SIZE*2]; +static uint16_t sent_data_size; Describe(ByteStuffer); BeforeEach(ByteStuffer) { init_byte_stuffer_state(&state); + sent_data_size = 0; } AfterEach(ByteStuffer) {} @@ -40,6 +44,11 @@ void recv_frame(uint8_t* data, uint16_t size) { mock(data, size); } +void send_data(const uint8_t* data, uint16_t size) { + memcpy(sent_data + sent_data_size, data, size); + sent_data_size += size; +} + Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(recv_frame); recv_byte(&state, 0); @@ -66,7 +75,7 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { expect(recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 0x37); recv_byte(&state, 0); @@ -77,7 +86,7 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { expect(recv_frame, when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) - ); + ); recv_byte(&state, 4); recv_byte(&state, 0x37); recv_byte(&state, 0x99); @@ -90,7 +99,7 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { expect(recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) - ); + ); recv_byte(&state, 1); recv_byte(&state, 1); recv_byte(&state, 0); @@ -101,7 +110,7 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { expect(recv_frame, when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 5); recv_byte(&state, 2); @@ -116,11 +125,11 @@ Ensure(ByteStuffer, receives_two_valid_frames) { expect(recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected1, 2)) - ); + ); expect(recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 5); recv_byte(&state, 1); @@ -135,7 +144,7 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { expect(recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) - ); + ); recv_byte(&state, 3); recv_byte(&state, 1); recv_byte(&state, 0); @@ -150,7 +159,7 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { expect(recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 9); recv_byte(&state, 4); // This should have been zero @@ -170,7 +179,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ expect(recv_frame, when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -188,7 +197,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expect(recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -208,7 +217,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expect(recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -233,7 +242,7 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { expect(recv_frame, when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -258,7 +267,7 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { expect(recv_frame, when(size, is_equal_to(MAX_FRAME_SIZE)), when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) - ); + ); int i; recv_byte(&state, 1); for(i=0;i Date: Sun, 14 Feb 2016 18:59:01 +0200 Subject: [PATCH 023/109] Hanlde sending of zero bytes for small packets --- serial_link/protocol/byte_stuffer.c | 27 +++++++++++-- serial_link/tests/byte_stuffer_tests.c | 56 ++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index dfd5942ebc..6118557c1d 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -93,12 +93,31 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } +static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { + send_data(&num_non_zero, 1); + if (end > start) { + send_data(start, end-start); + } +} + void send_frame(uint8_t* data, uint16_t size) { + const uint8_t zero = 0; if (size > 0) { - uint8_t numZeroes = size + 1; - const uint8_t zero = 0; - send_data(&numZeroes, 1); - send_data(data, size); + uint8_t num_non_zero = 1; + uint8_t* end = data + size; + uint8_t* start = data; + while (data < end) { + if (*data == 0) { + send_block(start, data, num_non_zero); + start = data + 1; + num_non_zero = 1; + } + else { + num_non_zero++; + } + ++data; + } + send_block(start, data, num_non_zero); send_data(&zero, 1); } } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 6e44c9170c..0c324e9c48 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -312,15 +312,63 @@ Ensure(ByteStuffer, send_zero_size_frame_does_nothing) { Ensure(ByteStuffer, send_one_byte_frame) { uint8_t data[] = {5}; send_frame(data, 1); - assert_that(sent_data_size, is_equal_to(3)); uint8_t expected[] = {2, 5, 0}; - assert_that(sent_data, is_equal_to_contents_of(expected, 3)); + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } Ensure(ByteStuffer, send_two_byte_frame) { uint8_t data[] = {5, 0x77}; send_frame(data, 2); - assert_that(sent_data_size, is_equal_to(4)); uint8_t expected[] = {3, 5, 0x77, 0}; - assert_that(sent_data, is_equal_to_contents_of(expected, 4)); + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_one_byte_frame_with_zero) { + uint8_t data[] = {0}; + send_frame(data, 1); + uint8_t expected[] = {1, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_two_byte_frame_starting_with_zero) { + uint8_t data[] = {0, 9}; + send_frame(data, 2); + uint8_t expected[] = {1, 2, 9, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_two_byte_frame_starting_with_non_zero) { + uint8_t data[] = {9, 0}; + send_frame(data, 2); + uint8_t expected[] = {2, 9, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_three_byte_frame_zero_in_the_middle) { + uint8_t data[] = {9, 0, 0x68}; + send_frame(data, 3); + uint8_t expected[] = {2, 9, 2, 0x68, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_three_byte_frame_data_in_the_middle) { + uint8_t data[] = {0, 0x55, 0}; + send_frame(data, 3); + uint8_t expected[] = {1, 2, 0x55, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_three_byte_frame_all_zeroes) { + uint8_t data[] = {0, 0, 0}; + send_frame(data, 3); + uint8_t expected[] = {1, 1, 1, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } From 2f3ea76428276b871b454effd1164648f5e49f23 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 19:03:34 +0200 Subject: [PATCH 024/109] Fix name of some unit tests --- serial_link/tests/byte_stuffer_tests.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 0c324e9c48..1982421e6e 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -304,7 +304,7 @@ Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) { recv_byte(&state, 0); } -Ensure(ByteStuffer, send_zero_size_frame_does_nothing) { +Ensure(ByteStuffer, does_nothing_when_sending_zero_size_frame) { assert_that(sent_data_size, is_equal_to(0)); send_frame(NULL, 0); } @@ -317,7 +317,7 @@ Ensure(ByteStuffer, send_one_byte_frame) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_two_byte_frame) { +Ensure(ByteStuffer, sends_two_byte_frame) { uint8_t data[] = {5, 0x77}; send_frame(data, 2); uint8_t expected[] = {3, 5, 0x77, 0}; @@ -325,7 +325,7 @@ Ensure(ByteStuffer, send_two_byte_frame) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_one_byte_frame_with_zero) { +Ensure(ByteStuffer, sends_one_byte_frame_with_zero) { uint8_t data[] = {0}; send_frame(data, 1); uint8_t expected[] = {1, 1, 0}; @@ -333,7 +333,7 @@ Ensure(ByteStuffer, send_one_byte_frame_with_zero) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_two_byte_frame_starting_with_zero) { +Ensure(ByteStuffer, sends_two_byte_frame_starting_with_zero) { uint8_t data[] = {0, 9}; send_frame(data, 2); uint8_t expected[] = {1, 2, 9, 0}; @@ -341,7 +341,7 @@ Ensure(ByteStuffer, send_two_byte_frame_starting_with_zero) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_two_byte_frame_starting_with_non_zero) { +Ensure(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) { uint8_t data[] = {9, 0}; send_frame(data, 2); uint8_t expected[] = {2, 9, 1, 0}; @@ -349,7 +349,7 @@ Ensure(ByteStuffer, send_two_byte_frame_starting_with_non_zero) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_three_byte_frame_zero_in_the_middle) { +Ensure(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) { uint8_t data[] = {9, 0, 0x68}; send_frame(data, 3); uint8_t expected[] = {2, 9, 2, 0x68, 0}; @@ -357,7 +357,7 @@ Ensure(ByteStuffer, send_three_byte_frame_zero_in_the_middle) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_three_byte_frame_data_in_the_middle) { +Ensure(ByteStuffer, sends_three_byte_frame_data_in_the_middle) { uint8_t data[] = {0, 0x55, 0}; send_frame(data, 3); uint8_t expected[] = {1, 2, 0x55, 1, 0}; @@ -365,7 +365,7 @@ Ensure(ByteStuffer, send_three_byte_frame_data_in_the_middle) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_three_byte_frame_all_zeroes) { +Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { uint8_t data[] = {0, 0, 0}; send_frame(data, 3); uint8_t expected[] = {1, 1, 1, 1, 0}; From 6956c177da8737f999eef72a21f0fc3caea2ac3e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 19:56:22 +0200 Subject: [PATCH 025/109] Add byte stuffing send support for large frames --- serial_link/protocol/byte_stuffer.c | 21 +++++++--- serial_link/tests/byte_stuffer_tests.c | 56 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 6118557c1d..69cfca3594 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -25,6 +25,7 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" #include "protocol/physical.h" +#include // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing @@ -103,19 +104,29 @@ static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { void send_frame(uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { - uint8_t num_non_zero = 1; + uint16_t num_non_zero = 1; uint8_t* end = data + size; uint8_t* start = data; while (data < end) { - if (*data == 0) { + if (num_non_zero == 0xFF) { + // There's more data after big non-zero block + // So send it, and start a new block send_block(start, data, num_non_zero); - start = data + 1; + start = data; num_non_zero = 1; } else { - num_non_zero++; + if (*data == 0) { + // A zero encountered, so send the block + send_block(start, data, num_non_zero); + start = data + 1; + num_non_zero = 1; + } + else { + num_non_zero++; + } + ++data; } - ++data; } send_block(start, data, num_non_zero); send_data(&zero, 1); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 1982421e6e..8598cddd30 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -372,3 +372,59 @@ Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } + +Ensure(ByteStuffer, sends_frame_with_254_non_zeroes) { + uint8_t data[254]; + int i; + for(i=0;i<254;i++) { + data[i] = i + 1; + } + send_frame(data, 254); + uint8_t expected[256]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 0; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, sends_frame_with_255_non_zeroes) { + uint8_t data[255]; + int i; + for(i=0;i<255;i++) { + data[i] = i + 1; + } + send_frame(data, 255); + uint8_t expected[258]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 2; + expected[256] = 255; + expected[257] = 0; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) { + uint8_t data[255]; + int i; + for(i=0;i<254;i++) { + data[i] = i + 1; + } + data[255] = 0; + send_frame(data, 255); + uint8_t expected[258]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 1; + expected[256] = 1; + expected[257] = 0; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} From 713465fb416785c3a544e8626f85aca7e57dafa1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 20:07:51 +0200 Subject: [PATCH 026/109] Add some full round trip tests Mostly to ensure that the receive and send framing works the same way. Especially for the special case with 254 non-zeros. --- serial_link/tests/byte_stuffer_tests.c | 77 ++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 8598cddd30..fbdf39e1b2 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -428,3 +428,80 @@ Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) { assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } + +Ensure(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) { + uint8_t original_data[] = { 1, 2, 3}; + send_frame(original_data, sizeof(original_data)); + expect(recv_frame, + when(size, is_equal_to(sizeof(original_data))), + when(data, is_equal_to_contents_of(original_data, sizeof(original_data))) + ); + int i; + for(i=0;i Date: Sun, 14 Feb 2016 20:45:59 +0200 Subject: [PATCH 027/109] Start implement frame validator --- serial_link/protocol/frame_router.h | 25 ++++++ serial_link/protocol/frame_validator.c | 105 ++++++++++++++++++++++ serial_link/tests/frame_validator_tests.c | 44 +++++++++ 3 files changed, 174 insertions(+) create mode 100644 serial_link/protocol/frame_router.h create mode 100644 serial_link/protocol/frame_validator.c create mode 100644 serial_link/tests/frame_validator_tests.c diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h new file mode 100644 index 0000000000..b5beba13c3 --- /dev/null +++ b/serial_link/protocol/frame_router.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void route_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c new file mode 100644 index 0000000000..1ffd3aad85 --- /dev/null +++ b/serial_link/protocol/frame_validator.c @@ -0,0 +1,105 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/frame_validator.h" + +const uint32_t poly8_lookup[256] = +{ + 0, 0x77073096, 0xEE0E612C, 0x990951BA, + 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, + 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, + 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, + 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, + 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, + 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, + 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, + 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, + 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, + 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, + 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, + 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, + 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, + 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, + 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, + 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, + 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, + 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, + 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, + 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, + 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, + 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, + 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, + 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, + 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, + 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, + 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, + 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, + 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, + 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, + 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, + 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, + 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, + 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, + 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, + 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, + 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, + 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, + 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, + 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, + 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, + 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D +}; + +static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) +{ + uint32_t crc = 0xffffffff; + while (bytelength-- !=0) crc = poly8_lookup[((uint8_t) crc ^ *(p++))] ^ (crc >> 8); + // return (~crc); also works + return (crc ^ 0xffffffff); +} + +void recv_frame(uint8_t* data, uint16_t size) { + +} diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c new file mode 100644 index 0000000000..f6a4fcd842 --- /dev/null +++ b/serial_link/tests/frame_validator_tests.c @@ -0,0 +1,44 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include "protocol/frame_validator.c" + +void route_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + +Describe(FrameValidator); +BeforeEach(FrameValidator) {} +AfterEach(FrameValidator) {} + +Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { + never_expect(route_frame); + uint8_t data[] = {1, 2}; + recv_frame(0, 1); + recv_frame(data, 2); + recv_frame(data, 3); + recv_frame(data, 4); +} From 62058329ff9000589ddba6454ff8ef8a551b7243 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:13:16 +0200 Subject: [PATCH 028/109] Add crc32 validation of received frames --- serial_link/protocol/frame_validator.c | 9 +++++- serial_link/tests/frame_validator_tests.c | 35 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 1ffd3aad85..d3337f6e32 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -23,6 +23,7 @@ SOFTWARE. */ #include "protocol/frame_validator.h" +#include "protocol/frame_router.h" const uint32_t poly8_lookup[256] = { @@ -101,5 +102,11 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) } void recv_frame(uint8_t* data, uint16_t size) { - + if (size > 4) { + uint32_t frame_crc = *(uint32_t*)(data + size - 4); + uint32_t expected_crc = crc32_byte(data, size - 4); + if (frame_crc == expected_crc) { + route_frame(data, size-4); + } + } } diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index f6a4fcd842..1aca9f95fc 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -42,3 +42,38 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { recv_frame(data, 3); recv_frame(data, 4); } + +Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { + uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + expect(route_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(data, 1)) + ); + recv_frame(data, 5); +} + +Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { + uint8_t data[] = {0x44, 0, 0, 0, 0}; + never_expect(route_frame); + recv_frame(data, 5); +} + +Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { + uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; + expect(route_frame, + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(data, 4)) + ); + recv_frame(data, 8); +} + +Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { + //0xBA304E74 + //0x470B99F4 + uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + expect(route_frame, + when(size, is_equal_to(5)), + when(data, is_equal_to_contents_of(data, 5)) + ); + recv_frame(data, 9); +} From 2a6696bd3d0837c2e655a99000bcbcc759b40075 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:31:01 +0200 Subject: [PATCH 029/109] Add validator send frame --- serial_link/protocol/frame_validator.c | 7 ++++++ serial_link/tests/frame_validator_tests.c | 26 +++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index d3337f6e32..fdb3ef51c3 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -24,6 +24,7 @@ SOFTWARE. #include "protocol/frame_validator.h" #include "protocol/frame_router.h" +#include "protocol/byte_stuffer.h" const uint32_t poly8_lookup[256] = { @@ -110,3 +111,9 @@ void recv_frame(uint8_t* data, uint16_t size) { } } } + +void validator_send_frame(uint8_t* data, uint16_t size) { + uint32_t* crc = (uint32_t*)(data + size); + *crc = crc32_byte(data, size); + send_frame(data, size + 4); +} diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index 1aca9f95fc..816256f6b7 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -30,6 +30,10 @@ void route_frame(uint8_t* data, uint16_t size) { mock(data, size); } +void send_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + Describe(FrameValidator); BeforeEach(FrameValidator) {} AfterEach(FrameValidator) {} @@ -68,8 +72,6 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { } Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { - //0xBA304E74 - //0x470B99F4 uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; expect(route_frame, when(size, is_equal_to(5)), @@ -77,3 +79,23 @@ Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { ); recv_frame(data, 9); } + +Ensure(FrameValidator, sends_one_byte_with_correct_crc) { + uint8_t original[] = {0x44, 0, 0, 0, 0}; + uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + expect(send_frame, + when(size, is_equal_to(sizeof(expected))), + when(data, is_equal_to_contents_of(expected, sizeof(expected))) + ); + validator_send_frame(original, 1); +} + +Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { + uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0}; + uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + expect(send_frame, + when(size, is_equal_to(sizeof(expected))), + when(data, is_equal_to_contents_of(expected, sizeof(expected))) + ); + validator_send_frame(original, 5); +} From 0eb62c4ce61e19cf7629bb74cf01d812c042ac57 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:33:14 +0200 Subject: [PATCH 030/109] Rename recv_frame to validator_recv_frame --- serial_link/protocol/byte_stuffer.c | 2 +- serial_link/protocol/frame_validator.c | 2 +- serial_link/protocol/frame_validator.h | 4 +- serial_link/tests/byte_stuffer_tests.c | 50 +++++++++++------------ serial_link/tests/frame_validator_tests.c | 16 ++++---- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 69cfca3594..f9aa02a3f5 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -60,7 +60,7 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { if (state->next_zero == 0) { // The frame is completed if (state->data_pos > 0) { - recv_frame(state->data, state->data_pos); + validator_recv_frame(state->data, state->data_pos); } } else { diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index fdb3ef51c3..0ffcda0473 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -102,7 +102,7 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) return (crc ^ 0xffffffff); } -void recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t* data, uint16_t size) { if (size > 4) { uint32_t frame_crc = *(uint32_t*)(data + size - 4); uint32_t expected_crc = crc32_byte(data, size - 4); diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index ee1f9a78ae..2e518894fb 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,4 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void recv_frame(uint8_t* data, uint16_t size); +void validator_recv_frame(uint8_t* data, uint16_t size); +// The buffer pointed to by the data needs 4 additional bytes +void validator_send_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index fbdf39e1b2..78bcbb4734 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -40,7 +40,7 @@ BeforeEach(ByteStuffer) { } AfterEach(ByteStuffer) {} -void recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t* data, uint16_t size) { mock(data, size); } @@ -50,29 +50,29 @@ void send_data(const uint8_t* data, uint16_t size) { } Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 1); recv_byte(&state, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { uint8_t expected[] = {0x37}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); @@ -83,7 +83,7 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { Ensure(ByteStuffer, receives_three_bytes_valid_frame) { uint8_t expected[] = {0x37, 0x99, 0xFF}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); @@ -96,7 +96,7 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { Ensure(ByteStuffer, receives_single_zero_valid_frame) { uint8_t expected[] = {0}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); @@ -107,7 +107,7 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { uint8_t expected[] = {5, 0, 3, 0}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); @@ -122,11 +122,11 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { Ensure(ByteStuffer, receives_two_valid_frames) { uint8_t expected1[] = {5, 0}; uint8_t expected2[] = {3}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected1, 2)) ); - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); @@ -141,7 +141,7 @@ Ensure(ByteStuffer, receives_two_valid_frames) { Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { uint8_t expected[] = {5, 7}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); @@ -156,7 +156,7 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { uint8_t expected[] = {5, 7}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); @@ -176,7 +176,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ for (i=0;i<254;i++) { expected[i] = i + 1; } - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); @@ -194,7 +194,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expected[i] = i + 1; } expected[254] = 7; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); @@ -214,7 +214,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expected[i] = i + 1; } expected[254] = 0; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); @@ -239,7 +239,7 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { for (i=0;i<7;i++) { expected[254*2+i] = i + 1; } - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); @@ -264,7 +264,7 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { uint8_t expected[MAX_FRAME_SIZE] = {}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(MAX_FRAME_SIZE)), when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); @@ -278,7 +278,7 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) { uint8_t expected[1] = {0}; - never_expect(recv_frame); + never_expect(validator_recv_frame); int i; recv_byte(&state, 1); for(i=0;i Date: Sun, 14 Feb 2016 21:34:40 +0200 Subject: [PATCH 031/109] Rename route_frame to route_incoming_frame --- serial_link/protocol/frame_router.h | 2 +- serial_link/protocol/frame_validator.c | 2 +- serial_link/tests/frame_validator_tests.c | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index b5beba13c3..dd1bfb1230 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void route_frame(uint8_t* data, uint16_t size); +void route_incoming_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 0ffcda0473..0a5f05068e 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -107,7 +107,7 @@ void validator_recv_frame(uint8_t* data, uint16_t size) { uint32_t frame_crc = *(uint32_t*)(data + size - 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { - route_frame(data, size-4); + route_incoming_frame(data, size-4); } } } diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index 3938f3eaca..6f9b746f69 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -26,7 +26,7 @@ SOFTWARE. #include #include "protocol/frame_validator.c" -void route_frame(uint8_t* data, uint16_t size) { +void route_incoming_frame(uint8_t* data, uint16_t size) { mock(data, size); } @@ -39,7 +39,7 @@ BeforeEach(FrameValidator) {} AfterEach(FrameValidator) {} Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { - never_expect(route_frame); + never_expect(route_incoming_frame); uint8_t data[] = {1, 2}; validator_recv_frame(0, 1); validator_recv_frame(data, 2); @@ -49,7 +49,7 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; - expect(route_frame, + expect(route_incoming_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(data, 1)) ); @@ -58,13 +58,13 @@ Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { uint8_t data[] = {0x44, 0, 0, 0, 0}; - never_expect(route_frame); + never_expect(route_incoming_frame); validator_recv_frame(data, 5); } Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; - expect(route_frame, + expect(route_incoming_frame, when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(data, 4)) ); @@ -73,7 +73,7 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; - expect(route_frame, + expect(route_incoming_frame, when(size, is_equal_to(5)), when(data, is_equal_to_contents_of(data, 5)) ); From 8d50880686dfc2b06958fc3bd6212945ee9029c4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:49:31 +0200 Subject: [PATCH 032/109] Use memcpy instead of cast to avoid alignment issues --- serial_link/protocol/frame_validator.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 0a5f05068e..f326097ffa 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -104,7 +104,8 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) void validator_recv_frame(uint8_t* data, uint16_t size) { if (size > 4) { - uint32_t frame_crc = *(uint32_t*)(data + size - 4); + uint32_t frame_crc; + memcpy(&frame_crc, data + size -4, 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { route_incoming_frame(data, size-4); @@ -113,7 +114,7 @@ void validator_recv_frame(uint8_t* data, uint16_t size) { } void validator_send_frame(uint8_t* data, uint16_t size) { - uint32_t* crc = (uint32_t*)(data + size); - *crc = crc32_byte(data, size); + uint32_t crc = crc32_byte(data, size); + memcpy(data + size, &crc, 4); send_frame(data, size + 4); } From 419908e5ef15481bd22dcd4173a25562f6153a5a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 17 Feb 2016 09:07:38 +0200 Subject: [PATCH 033/109] Hide the byte stuffer state inside it --- serial_link/protocol/byte_stuffer.c | 52 ++++---- serial_link/protocol/byte_stuffer.h | 5 +- serial_link/tests/byte_stuffer_tests.c | 177 ++++++++++++------------- 3 files changed, 117 insertions(+), 117 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index f9aa02a3f5..e5a26c2c2e 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -40,56 +40,58 @@ typedef struct byte_stuffer_state { uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; -void init_byte_stuffer_state(byte_stuffer_state_t* state) { - state->next_zero = 0; - state->data_pos = 0; - state->long_frame = false; +static byte_stuffer_state_t state; + +void init_byte_stuffer(void) { + state.next_zero = 0; + state.data_pos = 0; + state.long_frame = false; } -void recv_byte(byte_stuffer_state_t* state, uint8_t data) { +void recv_byte(uint8_t data) { // Start of a new frame - if (state->next_zero == 0) { - state->next_zero = data; - state->long_frame = data == 0xFF; - state->data_pos = 0; + if (state.next_zero == 0) { + state.next_zero = data; + state.long_frame = data == 0xFF; + state.data_pos = 0; return; } - state->next_zero--; + state.next_zero--; if (data == 0) { - if (state->next_zero == 0) { + if (state.next_zero == 0) { // The frame is completed - if (state->data_pos > 0) { - validator_recv_frame(state->data, state->data_pos); + if (state.data_pos > 0) { + validator_recv_frame(state.data, state.data_pos); } } else { // The frame is invalid, so reset - init_byte_stuffer_state(state); + init_byte_stuffer(); } } else { - if (state->data_pos == MAX_FRAME_SIZE) { + if (state.data_pos == MAX_FRAME_SIZE) { // We exceeded our maximum frame size // therefore there's nothing else to do than reset to a new frame - state->next_zero = data; - state->long_frame = data == 0xFF; - state->data_pos = 0; + state.next_zero = data; + state.long_frame = data == 0xFF; + state.data_pos = 0; } - else if (state->next_zero == 0) { - if (state->long_frame) { + else if (state.next_zero == 0) { + if (state.long_frame) { // This is part of a long frame, so continue - state->next_zero = data; - state->long_frame = data == 0xFF; + state.next_zero = data; + state.long_frame = data == 0xFF; } else { // Special case for zeroes - state->next_zero = data; - state->data[state->data_pos++] = 0; + state.next_zero = data; + state.data[state.data_pos++] = 0; } } else { - state->data[state->data_pos++] = data; + state.data[state.data_pos++] = data; } } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index ea6b8451d4..4dcdd24c11 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,7 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -typedef struct byte_stuffer_state byte_stuffer_state_t; -void init_byte_stuffer_state(byte_stuffer_state_t* state); -void recv_byte(byte_stuffer_state_t* state, uint8_t data); +void init_byte_stuffer(void); +void recv_byte(uint8_t data); void send_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 78bcbb4734..f29cc12993 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -29,13 +29,12 @@ SOFTWARE. #include "protocol/frame_validator.h" #include "protocol/physical.h" -static byte_stuffer_state_t state; static uint8_t sent_data[MAX_FRAME_SIZE*2]; static uint16_t sent_data_size; Describe(ByteStuffer); BeforeEach(ByteStuffer) { - init_byte_stuffer_state(&state); + init_byte_stuffer(); sent_data_size = 0; } AfterEach(ByteStuffer) {} @@ -51,23 +50,23 @@ void send_data(const uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(&state, 0); + recv_byte(0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(&state, 0xFF); + recv_byte(0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(&state, 0x4A); + recv_byte(0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -76,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(&state, 2); - recv_byte(&state, 0x37); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(0x37); + recv_byte(0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -87,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(&state, 4); - recv_byte(&state, 0x37); - recv_byte(&state, 0x99); - recv_byte(&state, 0xFF); - recv_byte(&state, 0); + recv_byte(4); + recv_byte(0x37); + recv_byte(0x99); + recv_byte(0xFF); + recv_byte(0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -100,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(&state, 1); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(1); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -111,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(&state, 2); - recv_byte(&state, 5); - recv_byte(&state, 2); - recv_byte(&state, 3); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(5); + recv_byte(2); + recv_byte(3); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -130,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(&state, 2); - recv_byte(&state, 5); - recv_byte(&state, 1); - recv_byte(&state, 0); - recv_byte(&state, 2); - recv_byte(&state, 3); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(5); + recv_byte(1); + recv_byte(0); + recv_byte(2); + recv_byte(3); + recv_byte(0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -145,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(&state, 3); - recv_byte(&state, 1); - recv_byte(&state, 0); - recv_byte(&state, 3); - recv_byte(&state, 5); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(3); + recv_byte(1); + recv_byte(0); + recv_byte(3); + recv_byte(5); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -160,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(&state, 2); - recv_byte(&state, 9); - recv_byte(&state, 4); // This should have been zero - recv_byte(&state, 0); - recv_byte(&state, 3); - recv_byte(&state, 5); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(9); + recv_byte(4); // This should have been zero + recv_byte(0); + recv_byte(3); + recv_byte(5); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -180,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 0); + recv_byte(0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -198,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 2); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -218,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 1); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(1); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -243,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 8); - recv_byte(&state, 1); - recv_byte(&state, 2); - recv_byte(&state, 3); - recv_byte(&state, 4); - recv_byte(&state, 5); - recv_byte(&state, 6); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(8); + recv_byte(1); + recv_byte(2); + recv_byte(3); + recv_byte(4); + recv_byte(5); + recv_byte(6); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -269,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(&state, 1); + recv_byte(1); for(i=0;i Date: Sat, 20 Feb 2016 12:06:23 +0200 Subject: [PATCH 034/109] Add link parameter --- serial_link/protocol/byte_stuffer.c | 77 ++++---- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/protocol/frame_router.h | 2 +- serial_link/protocol/frame_validator.c | 8 +- serial_link/protocol/frame_validator.h | 4 +- serial_link/protocol/physical.h | 2 +- serial_link/tests/byte_stuffer_tests.c | 212 +++++++++++----------- serial_link/tests/frame_validator_tests.c | 24 +-- 8 files changed, 171 insertions(+), 162 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index e5a26c2c2e..05b7aa6f80 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -32,6 +32,7 @@ SOFTWARE. // http://www.stuartcheshire.org/papers/COBSforToN.pdf #define MAX_FRAME_SIZE 1024 +#define NUM_LINKS 2 typedef struct byte_stuffer_state { uint16_t next_zero; @@ -40,70 +41,78 @@ typedef struct byte_stuffer_state { uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; -static byte_stuffer_state_t state; +static byte_stuffer_state_t states[NUM_LINKS]; -void init_byte_stuffer(void) { - state.next_zero = 0; - state.data_pos = 0; - state.long_frame = false; +void init_byte_stuffer_state(byte_stuffer_state_t* state) { + state->next_zero = 0; + state->data_pos = 0; + state->long_frame = false; } -void recv_byte(uint8_t data) { +void init_byte_stuffer(void) { + int i; + for (i=0;inext_zero == 0) { + state->next_zero = data; + state->long_frame = data == 0xFF; + state->data_pos = 0; return; } - state.next_zero--; + state->next_zero--; if (data == 0) { - if (state.next_zero == 0) { + if (state->next_zero == 0) { // The frame is completed - if (state.data_pos > 0) { - validator_recv_frame(state.data, state.data_pos); + if (state->data_pos > 0) { + validator_recv_frame(link, state->data, state->data_pos); } } else { // The frame is invalid, so reset - init_byte_stuffer(); + init_byte_stuffer_state(state); } } else { - if (state.data_pos == MAX_FRAME_SIZE) { + if (state->data_pos == MAX_FRAME_SIZE) { // We exceeded our maximum frame size // therefore there's nothing else to do than reset to a new frame - state.next_zero = data; - state.long_frame = data == 0xFF; - state.data_pos = 0; + state->next_zero = data; + state->long_frame = data == 0xFF; + state->data_pos = 0; } - else if (state.next_zero == 0) { - if (state.long_frame) { + else if (state->next_zero == 0) { + if (state->long_frame) { // This is part of a long frame, so continue - state.next_zero = data; - state.long_frame = data == 0xFF; + state->next_zero = data; + state->long_frame = data == 0xFF; } else { // Special case for zeroes - state.next_zero = data; - state.data[state.data_pos++] = 0; + state->next_zero = data; + state->data[state->data_pos++] = 0; } } else { - state.data[state.data_pos++] = data; + state->data[state->data_pos++] = data; } } } -static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { - send_data(&num_non_zero, 1); +static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) { + send_data(link, &num_non_zero, 1); if (end > start) { - send_data(start, end-start); + send_data(link, start, end-start); } } -void send_frame(uint8_t* data, uint16_t size) { +void send_frame(uint8_t link, uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { uint16_t num_non_zero = 1; @@ -113,14 +122,14 @@ void send_frame(uint8_t* data, uint16_t size) { if (num_non_zero == 0xFF) { // There's more data after big non-zero block // So send it, and start a new block - send_block(start, data, num_non_zero); + send_block(link, start, data, num_non_zero); start = data; num_non_zero = 1; } else { if (*data == 0) { // A zero encountered, so send the block - send_block(start, data, num_non_zero); + send_block(link, start, data, num_non_zero); start = data + 1; num_non_zero = 1; } @@ -130,7 +139,7 @@ void send_frame(uint8_t* data, uint16_t size) { ++data; } } - send_block(start, data, num_non_zero); - send_data(&zero, 1); + send_block(link, start, data, num_non_zero); + send_data(link, &zero, 1); } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 4dcdd24c11..cd94b45828 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -23,5 +23,5 @@ SOFTWARE. */ void init_byte_stuffer(void); -void recv_byte(uint8_t data); -void send_frame(uint8_t* data, uint16_t size); +void recv_byte(uint8_t link, uint8_t data); +void send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index dd1bfb1230..3906b06c71 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void route_incoming_frame(uint8_t* data, uint16_t size); +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index f326097ffa..80e662b2e8 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -102,19 +102,19 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) return (crc ^ 0xffffffff); } -void validator_recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { if (size > 4) { uint32_t frame_crc; memcpy(&frame_crc, data + size -4, 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { - route_incoming_frame(data, size-4); + route_incoming_frame(link, data, size-4); } } } -void validator_send_frame(uint8_t* data, uint16_t size) { +void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) { uint32_t crc = crc32_byte(data, size); memcpy(data + size, &crc, 4); - send_frame(data, size + 4); + send_frame(link, data, size + 4); } diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index 2e518894fb..c35fc27260 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,6 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void validator_recv_frame(uint8_t* data, uint16_t size); +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size); // The buffer pointed to by the data needs 4 additional bytes -void validator_send_frame(uint8_t* data, uint16_t size); +void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h index 73a7855dfb..ee5883d36b 100644 --- a/serial_link/protocol/physical.h +++ b/serial_link/protocol/physical.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void send_data(const uint8_t* data, uint16_t size); +void send_data(uint8_t link, const uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index f29cc12993..0f58479cb9 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -39,34 +39,34 @@ BeforeEach(ByteStuffer) { } AfterEach(ByteStuffer) {} -void validator_recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } -void send_data(const uint8_t* data, uint16_t size) { +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { memcpy(sent_data + sent_data_size, data, size); sent_data_size += size; } Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(0); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(0xFF); + recv_byte(0, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(0x4A); + recv_byte(0, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -75,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(2); - recv_byte(0x37); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 0x37); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -86,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(4); - recv_byte(0x37); - recv_byte(0x99); - recv_byte(0xFF); - recv_byte(0); + recv_byte(0, 4); + recv_byte(0, 0x37); + recv_byte(0, 0x99); + recv_byte(0, 0xFF); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -99,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(1); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -110,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(2); - recv_byte(5); - recv_byte(2); - recv_byte(3); - recv_byte(1); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 5); + recv_byte(0, 2); + recv_byte(0, 3); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -129,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(2); - recv_byte(5); - recv_byte(1); - recv_byte(0); - recv_byte(2); - recv_byte(3); - recv_byte(0); + recv_byte(1, 2); + recv_byte(1, 5); + recv_byte(1, 1); + recv_byte(1, 0); + recv_byte(1, 2); + recv_byte(1, 3); + recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -144,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(3); - recv_byte(1); - recv_byte(0); - recv_byte(3); - recv_byte(5); - recv_byte(7); - recv_byte(0); + recv_byte(1, 3); + recv_byte(1, 1); + recv_byte(1, 0); + recv_byte(1, 3); + recv_byte(1, 5); + recv_byte(1, 7); + recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -159,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(2); - recv_byte(9); - recv_byte(4); // This should have been zero - recv_byte(0); - recv_byte(3); - recv_byte(5); - recv_byte(7); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 9); + recv_byte(0, 4); // This should have been zero + recv_byte(0, 0); + recv_byte(0, 3); + recv_byte(0, 5); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -179,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(0); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -197,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(2); - recv_byte(7); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -217,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(1); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -242,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(8); - recv_byte(1); - recv_byte(2); - recv_byte(3); - recv_byte(4); - recv_byte(5); - recv_byte(6); - recv_byte(7); - recv_byte(0); + recv_byte(0, 8); + recv_byte(0, 1); + recv_byte(0, 2); + recv_byte(0, 3); + recv_byte(0, 4); + recv_byte(0, 5); + recv_byte(0, 6); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -268,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(1); + recv_byte(0, 1); for(i=0;i #include "protocol/frame_validator.c" -void route_incoming_frame(uint8_t* data, uint16_t size) { +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } -void send_frame(uint8_t* data, uint16_t size) { +void send_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } @@ -41,10 +41,10 @@ AfterEach(FrameValidator) {} Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { never_expect(route_incoming_frame); uint8_t data[] = {1, 2}; - validator_recv_frame(0, 1); - validator_recv_frame(data, 2); - validator_recv_frame(data, 3); - validator_recv_frame(data, 4); + validator_recv_frame(0, 0, 1); + validator_recv_frame(0, data, 2); + validator_recv_frame(0, data, 3); + validator_recv_frame(0, data, 4); } Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { @@ -53,13 +53,13 @@ Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(data, 1)) ); - validator_recv_frame(data, 5); + validator_recv_frame(0, data, 5); } Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { uint8_t data[] = {0x44, 0, 0, 0, 0}; never_expect(route_incoming_frame); - validator_recv_frame(data, 5); + validator_recv_frame(1, data, 5); } Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { @@ -68,7 +68,7 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(data, 4)) ); - validator_recv_frame(data, 8); + validator_recv_frame(1, data, 8); } Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { @@ -77,7 +77,7 @@ Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { when(size, is_equal_to(5)), when(data, is_equal_to_contents_of(data, 5)) ); - validator_recv_frame(data, 9); + validator_recv_frame(0, data, 9); } Ensure(FrameValidator, sends_one_byte_with_correct_crc) { @@ -87,7 +87,7 @@ Ensure(FrameValidator, sends_one_byte_with_correct_crc) { when(size, is_equal_to(sizeof(expected))), when(data, is_equal_to_contents_of(expected, sizeof(expected))) ); - validator_send_frame(original, 1); + validator_send_frame(0, original, 1); } Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { @@ -97,5 +97,5 @@ Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { when(size, is_equal_to(sizeof(expected))), when(data, is_equal_to_contents_of(expected, sizeof(expected))) ); - validator_send_frame(original, 5); + validator_send_frame(0, original, 5); } From 4b9fccc75633b4ea297de373d5d7f01288d2fb11 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 13:59:48 +0200 Subject: [PATCH 035/109] Start implement router --- serial_link/protocol/byte_stuffer.c | 4 +- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/protocol/frame_router.c | 33 ++++ serial_link/protocol/frame_router.h | 4 + serial_link/protocol/frame_validator.c | 2 +- serial_link/tests/byte_stuffer_tests.c | 208 +++++++++++----------- serial_link/tests/frame_router_tests.c | 89 +++++++++ serial_link/tests/frame_validator_tests.c | 6 +- 8 files changed, 238 insertions(+), 112 deletions(-) create mode 100644 serial_link/protocol/frame_router.c create mode 100644 serial_link/tests/frame_router_tests.c diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 05b7aa6f80..8b529667fd 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -56,7 +56,7 @@ void init_byte_stuffer(void) { } } -void recv_byte(uint8_t link, uint8_t data) { +void byte_stuffer_recv_byte(uint8_t link, uint8_t data) { byte_stuffer_state_t* state = &states[link]; // Start of a new frame if (state->next_zero == 0) { @@ -112,7 +112,7 @@ static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_n } } -void send_frame(uint8_t link, uint8_t* data, uint16_t size) { +void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { uint16_t num_non_zero = 1; diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index cd94b45828..839a876fe8 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -23,5 +23,5 @@ SOFTWARE. */ void init_byte_stuffer(void); -void recv_byte(uint8_t link, uint8_t data); -void send_frame(uint8_t link, uint8_t* data, uint16_t size); +void byte_stuffer_recv_byte(uint8_t link, uint8_t data); +void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c new file mode 100644 index 0000000000..db6223a8d7 --- /dev/null +++ b/serial_link/protocol/frame_router.c @@ -0,0 +1,33 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/frame_router.h" + +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ + validator_send_frame(DOWN_LINK, data, size); +} + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + validator_send_frame(DOWN_LINK, data, size); +} diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index 3906b06c71..a0238079e8 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#define UP_LINK 0 +#define DOWN_LINK 1 + void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 80e662b2e8..a3face6506 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -116,5 +116,5 @@ void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) { uint32_t crc = crc32_byte(data, size); memcpy(data + size, &crc, 4); - send_frame(link, data, size + 4); + byte_stuffer_send_frame(link, data, size + 4); } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 0f58479cb9..912c4d3211 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -50,23 +50,23 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0x4A); + byte_stuffer_recv_byte(0, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -75,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(0, 2); - recv_byte(0, 0x37); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -86,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(0, 4); - recv_byte(0, 0x37); - recv_byte(0, 0x99); - recv_byte(0, 0xFF); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0x99); + byte_stuffer_recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -99,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(0, 1); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -110,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(0, 2); - recv_byte(0, 5); - recv_byte(0, 2); - recv_byte(0, 3); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -129,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(1, 2); - recv_byte(1, 5); - recv_byte(1, 1); - recv_byte(1, 0); - recv_byte(1, 2); - recv_byte(1, 3); - recv_byte(1, 0); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -144,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(1, 3); - recv_byte(1, 1); - recv_byte(1, 0); - recv_byte(1, 3); - recv_byte(1, 5); - recv_byte(1, 7); - recv_byte(1, 0); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 7); + byte_stuffer_recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -159,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(0, 2); - recv_byte(0, 9); - recv_byte(0, 4); // This should have been zero - recv_byte(0, 0); - recv_byte(0, 3); - recv_byte(0, 5); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 9); + byte_stuffer_recv_byte(0, 4); // This should have been zero + byte_stuffer_recv_byte(0, 0); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -179,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -197,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 2); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -217,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 1); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -242,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 8); - recv_byte(0, 1); - recv_byte(0, 2); - recv_byte(0, 3); - recv_byte(0, 4); - recv_byte(0, 5); - recv_byte(0, 6); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 8); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 6); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -268,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); for(i=0;i +#include +#include "protocol/byte_stuffer.c" +#include "protocol/frame_validator.c" +#include "protocol/frame_router.c" + +static uint8_t received_data[256]; +static uint16_t received_data_size; + +typedef struct { + uint8_t received_data[256]; + uint16_t sent_data_size; +} receive_buffer_t; + +typedef struct { + receive_buffer_t send_buffers[2]; +} router_buffer_t; + +router_buffer_t router_buffers[8]; + +router_buffer_t* current_router_buffer; + + +Describe(ByteStuffer); +BeforeEach(ByteStuffer) { + init_byte_stuffer(); + memset(router_buffers, 0, sizeof(router_buffers)); + current_router_buffer = 0; +} +AfterEach(ByteStuffer) {} + +typedef struct { + uint32_t data; + uint8_t extra[16]; +} frame_buffer_t; + + +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + receive_buffer_t* buffer = ¤t_router_buffer->send_buffers[link]; + memcpy(buffer->received_data + buffer->sent_data_size, data, size); + buffer->sent_data_size += size; +} + +static void receive_data(uint8_t link, uint8_t* data, uint16_t size) { + int i; + for(i=0;i Date: Sat, 20 Feb 2016 14:32:26 +0200 Subject: [PATCH 036/109] Add complete master broadcast test --- serial_link/protocol/frame_router.c | 8 +++++ serial_link/protocol/frame_router.h | 1 + serial_link/protocol/transport.h | 25 ++++++++++++++ serial_link/tests/frame_router_tests.c | 45 ++++++++++++++++++++++---- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 serial_link/protocol/transport.h diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index db6223a8d7..0675cea284 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -23,8 +23,16 @@ SOFTWARE. */ #include "protocol/frame_router.h" +#include "protocol/transport.h" + +static bool is_master; + +void router_set_master(bool master) { + is_master = master; +} void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ + transport_recv_frame(0, data, size); validator_send_frame(DOWN_LINK, data, size); } diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index a0238079e8..67db3122fa 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -25,5 +25,6 @@ SOFTWARE. #define UP_LINK 0 #define DOWN_LINK 1 +void router_set_master(bool master); void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h new file mode 100644 index 0000000000..a40d64d414 --- /dev/null +++ b/serial_link/protocol/transport.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 5ce6c9c0f5..eb0c710ea1 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -27,12 +27,13 @@ SOFTWARE. #include "protocol/byte_stuffer.c" #include "protocol/frame_validator.c" #include "protocol/frame_router.c" +#include "protocol/transport.h" static uint8_t received_data[256]; static uint16_t received_data_size; typedef struct { - uint8_t received_data[256]; + uint8_t sent_data[256]; uint16_t sent_data_size; } receive_buffer_t; @@ -61,7 +62,7 @@ typedef struct { void send_data(uint8_t link, const uint8_t* data, uint16_t size) { receive_buffer_t* buffer = ¤t_router_buffer->send_buffers[link]; - memcpy(buffer->received_data + buffer->sent_data_size, data, size); + memcpy(buffer->sent_data + buffer->sent_data_size, data, size); buffer->sent_data_size += size; } @@ -72,18 +73,50 @@ static void receive_data(uint8_t link, uint8_t* data, uint16_t size) { } } +static void activate_router(uint8_t num) { + current_router_buffer = router_buffers + num; + router_set_master(num==0); +} + +static void simulate_transport(uint8_t from, uint8_t to) { + activate_router(to); + if (from > to) { + } + else if(to > from) { + receive_data(UP_LINK, + router_buffers[from].send_buffers[DOWN_LINK].sent_data, + router_buffers[from].send_buffers[DOWN_LINK].sent_data_size); + } +} + +void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { + mock(from, data, size); +} + Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { frame_buffer_t data; data.data = 0xAB7055BB; - current_router_buffer = router_buffers + 0; + activate_router(0); router_send_frame(0xFF, (uint8_t*)&data, 4); assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); - current_router_buffer = router_buffers + 1; - receive_data(UP_LINK, router_buffers[0].send_buffers[DOWN_LINK].received_data, - router_buffers[0].send_buffers[DOWN_LINK].sent_data_size); + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(0, 1); assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 2); + assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } From 82b5037b9db628c3f34d8fb38848753ff3e41e2f Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 14:52:17 +0200 Subject: [PATCH 037/109] Add master send to specific destinations --- serial_link/protocol/frame_router.c | 8 +++++-- serial_link/tests/frame_router_tests.c | 32 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 0675cea284..861c0d7d29 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -32,10 +32,14 @@ void router_set_master(bool master) { } void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ - transport_recv_frame(0, data, size); + if (data[size-1] & 1) { + transport_recv_frame(0, data, size - 1); + } + data[size-1] >>= 1; validator_send_frame(DOWN_LINK, data, size); } void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { - validator_send_frame(DOWN_LINK, data, size); + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index eb0c710ea1..7a4ccb0c40 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -120,3 +120,35 @@ Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(ByteStuffer, master_send_is_received_by_targets) { + printf("Here\n"); + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(0); + router_send_frame((1 << 1) | (1 << 2), (uint8_t*)&data, 4); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + simulate_transport(0, 1); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 2); + assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(2, 3); + assert_that(router_buffers[3].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); +} From c28e19c5467e92f532f64db020ad4817b531dbec Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:02:53 +0200 Subject: [PATCH 038/109] First link sending to master --- serial_link/protocol/frame_router.c | 22 +++++++++++++----- serial_link/tests/frame_router_tests.c | 32 +++++++++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 861c0d7d29..480673f0c0 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -32,14 +32,24 @@ void router_set_master(bool master) { } void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ - if (data[size-1] & 1) { - transport_recv_frame(0, data, size - 1); + if (is_master) { + transport_recv_frame(1, data, size); + } + else { + if (data[size-1] & 1) { + transport_recv_frame(0, data, size - 1); + } + data[size-1] >>= 1; + validator_send_frame(DOWN_LINK, data, size); } - data[size-1] >>= 1; - validator_send_frame(DOWN_LINK, data, size); } void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { - data[size] = destination; - validator_send_frame(DOWN_LINK, data, size + 1); + if (destination == 0) { + validator_send_frame(UP_LINK, data, size); + } + else { + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); + } } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 7a4ccb0c40..02e6367d21 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -46,13 +46,13 @@ router_buffer_t router_buffers[8]; router_buffer_t* current_router_buffer; -Describe(ByteStuffer); -BeforeEach(ByteStuffer) { +Describe(FrameRouter); +BeforeEach(FrameRouter) { init_byte_stuffer(); memset(router_buffers, 0, sizeof(router_buffers)); current_router_buffer = 0; } -AfterEach(ByteStuffer) {} +AfterEach(FrameRouter) {} typedef struct { uint32_t data; @@ -81,6 +81,9 @@ static void activate_router(uint8_t num) { static void simulate_transport(uint8_t from, uint8_t to) { activate_router(to); if (from > to) { + receive_data(DOWN_LINK, + router_buffers[from].send_buffers[UP_LINK].sent_data, + router_buffers[from].send_buffers[UP_LINK].sent_data_size); } else if(to > from) { receive_data(UP_LINK, @@ -94,7 +97,7 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { } -Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { +Ensure(FrameRouter, master_broadcast_is_received_by_everyone) { frame_buffer_t data; data.data = 0xAB7055BB; activate_router(0); @@ -121,8 +124,7 @@ Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } -Ensure(ByteStuffer, master_send_is_received_by_targets) { - printf("Here\n"); +Ensure(FrameRouter, master_send_is_received_by_targets) { frame_buffer_t data; data.data = 0xAB7055BB; activate_router(0); @@ -152,3 +154,21 @@ Ensure(ByteStuffer, master_send_is_received_by_targets) { assert_that(router_buffers[3].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, first_sends_to_master) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(1)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 0); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); +} From 532f98eef6a29ae83d7ed61fbc4e25e23ad972d5 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:14:40 +0200 Subject: [PATCH 039/109] Support for other links sending to master --- serial_link/protocol/frame_router.c | 19 +++++++++++++------ serial_link/tests/frame_router_tests.c | 25 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 480673f0c0..c61c50ebd6 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -33,20 +33,27 @@ void router_set_master(bool master) { void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ if (is_master) { - transport_recv_frame(1, data, size); + transport_recv_frame(data[size-1], data, size - 1); } else { - if (data[size-1] & 1) { - transport_recv_frame(0, data, size - 1); + if (link == UP_LINK) { + if (data[size-1] & 1) { + transport_recv_frame(0, data, size - 1); + } + data[size-1] >>= 1; + validator_send_frame(DOWN_LINK, data, size); + } + else { + data[size-1]++; + validator_send_frame(UP_LINK, data, size); } - data[size-1] >>= 1; - validator_send_frame(DOWN_LINK, data, size); } } void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { if (destination == 0) { - validator_send_frame(UP_LINK, data, size); + data[size] = 1; + validator_send_frame(UP_LINK, data, size + 1); } else { data[size] = destination; diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 02e6367d21..6b00aca677 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -155,7 +155,7 @@ Ensure(FrameRouter, master_send_is_received_by_targets) { assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } -Ensure(FrameRouter, first_sends_to_master) { +Ensure(FrameRouter, first_link_sends_to_master) { frame_buffer_t data; data.data = 0xAB7055BB; activate_router(1); @@ -172,3 +172,26 @@ Ensure(FrameRouter, first_sends_to_master) { assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, second_link_sends_to_master) { + printf("Second to master start\n"); + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(2); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + simulate_transport(2, 1); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(2)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 0); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); +} From ea346125e8ef0c14e1566070380ec777d4a1a56a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:28:42 +0200 Subject: [PATCH 040/109] Add some validation for invalid router destinations --- serial_link/protocol/frame_router.c | 16 ++++++++---- serial_link/tests/frame_router_tests.c | 36 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index c61c50ebd6..890ebbe9ea 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -33,7 +33,9 @@ void router_set_master(bool master) { void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ if (is_master) { - transport_recv_frame(data[size-1], data, size - 1); + if (link == DOWN_LINK) { + transport_recv_frame(data[size-1], data, size - 1); + } } else { if (link == UP_LINK) { @@ -52,11 +54,15 @@ void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { if (destination == 0) { - data[size] = 1; - validator_send_frame(UP_LINK, data, size + 1); + if (!is_master) { + data[size] = 1; + validator_send_frame(UP_LINK, data, size + 1); + } } else { - data[size] = destination; - validator_send_frame(DOWN_LINK, data, size + 1); + if (is_master) { + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); + } } } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 6b00aca677..0b0ea6e7f4 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -174,7 +174,6 @@ Ensure(FrameRouter, first_link_sends_to_master) { } Ensure(FrameRouter, second_link_sends_to_master) { - printf("Second to master start\n"); frame_buffer_t data; data.data = 0xAB7055BB; activate_router(2); @@ -195,3 +194,38 @@ Ensure(FrameRouter, second_link_sends_to_master) { assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, master_sends_to_master_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(0); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} + +Ensure(FrameRouter, link_sends_to_other_link_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(2, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} + +Ensure(FrameRouter, master_receives_on_uplink_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + never_expect(transport_recv_frame); + activate_router(0); + receive_data(UP_LINK, + router_buffers[1].send_buffers[UP_LINK].sent_data, + router_buffers[1].send_buffers[UP_LINK].sent_data_size); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} From c78eefba498ffafc3cb577a72492256849384de6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:49:31 +0200 Subject: [PATCH 041/109] Add first transport tests --- serial_link/protocol/transport.c | 39 +++++++++++++++++++++++++++++ serial_link/protocol/transport.h | 2 ++ serial_link/tests/transport_tests.c | 39 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 serial_link/protocol/transport.c create mode 100644 serial_link/tests/transport_tests.c diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c new file mode 100644 index 0000000000..399894ff98 --- /dev/null +++ b/serial_link/protocol/transport.c @@ -0,0 +1,39 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/transport.h" + +static uint32_t current_send_frame; + +void init_transport(void) { + current_send_frame = 0; +} + +void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { + +} + +uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { + return ++current_send_frame; +} diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index a40d64d414..01119857d3 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -22,4 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +void init_transport(void); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); +uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c new file mode 100644 index 0000000000..a64e7446d8 --- /dev/null +++ b/serial_link/tests/transport_tests.c @@ -0,0 +1,39 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include "protocol/transport.c" + +Describe(Transport); +BeforeEach(Transport) { + init_transport(); +} +AfterEach(Transport) {} + +Ensure(Transport, packet_number_is_sequential) { + assert_that(transport_send_frame(0, NULL, 0), is_equal_to(1)); + assert_that(transport_send_frame(0, NULL, 0), is_equal_to(2)); + // It doesn't matter if the destination changes + assert_that(transport_send_frame(1, NULL, 0), is_equal_to(3)); +} From 50edb3d90c0076eb088f79564e38382452a21d03 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 22:22:42 +0200 Subject: [PATCH 042/109] Add first version of triple buffered object --- serial_link/protocol/triple_buffered_object.c | 33 ++++++++++++++ serial_link/protocol/triple_buffered_object.h | 37 ++++++++++++++++ .../tests/triple_buffered_object_tests.c | 44 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 serial_link/protocol/triple_buffered_object.c create mode 100644 serial_link/protocol/triple_buffered_object.h create mode 100644 serial_link/tests/triple_buffered_object_tests.c diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c new file mode 100644 index 0000000000..f5600364f4 --- /dev/null +++ b/serial_link/protocol/triple_buffered_object.c @@ -0,0 +1,33 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/triple_buffered_object.h" + +void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { + memcpy(object->buffer, src, object_size); +} + +void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { + memcpy(dst, object->buffer, object_size); +} diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h new file mode 100644 index 0000000000..ebdbcae792 --- /dev/null +++ b/serial_link/protocol/triple_buffered_object.h @@ -0,0 +1,37 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H +#define SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H + +typedef struct { + uint8_t state; + uint8_t buffer[]; +}triple_buffer_object_t; + +void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src); +void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); + + +#endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c new file mode 100644 index 0000000000..66dcefa7f3 --- /dev/null +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -0,0 +1,44 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include "protocol/triple_buffered_object.c" + +Describe(TripleBufferedObject); +BeforeEach(TripleBufferedObject) {} +AfterEach(TripleBufferedObject) {} + +typedef struct { + uint8_t state; + uint32_t buffer[3]; +}test_object_t; + +Ensure(TripleBufferedObject, writes_and_and_reads_object) { + test_object_t test_object; + uint32_t src = 0x3456ABCC; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(src)); +} From 4bb5733cdca62350c900ce094e1cbbaae59f2246 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 23:40:52 +0200 Subject: [PATCH 043/109] Updates to triple buffer --- serial_link/protocol/triple_buffered_object.c | 54 ++++++++++++++++++- .../tests/triple_buffered_object_tests.c | 48 ++++++++++++++--- 2 files changed, 94 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index f5600364f4..a82cef5a63 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -24,10 +24,60 @@ SOFTWARE. #include "protocol/triple_buffered_object.h" +#define GET_READ_INDEX() object->state & 3 +#define GET_WRITE1_INDEX() (object->state >> 2) & 3 +#define GET_WRITE2_INDEX() (object->state >> 4) & 3 +#define GET_FREE_INDEX() (object->state >> 6) & 3 + +#define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i) +#define SET_WRITE1_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) +#define SET_WRITE2_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) +#define SET_FREE_INDEX(i) object->state = ((object->state & ~(3 << 6)) | (i << 6)) + +void triple_buffer_init(triple_buffer_object_t* object) { + object->state = 0; + SET_WRITE1_INDEX(0); + SET_WRITE2_INDEX(0); + SET_READ_INDEX(1); + SET_FREE_INDEX(2); +} + +static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { + uint8_t newest = GET_WRITE2_INDEX(); + uint8_t free_index = GET_READ_INDEX(); + SET_READ_INDEX(newest); + SET_FREE_INDEX(free_index); +} + +static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { + uint8_t read_index = GET_READ_INDEX(); + memcpy(dst, object->buffer + object_size*read_index, object_size); +} + +static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* object) { +} + void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { - memcpy(object->buffer, src, object_size); + uint8_t write1_index = GET_WRITE1_INDEX(); + uint8_t write2_index = GET_WRITE2_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + uint8_t free_index = GET_FREE_INDEX(); + + if (write2_index == read_index) { + // We are reading from the other write index + SET_WRITE1_INDEX(free_index); + memcpy(object->buffer + object_size * free_index, src, object_size); + SET_WRITE2_INDEX(free_index); + } + else { + SET_WRITE1_INDEX(write2_index); + memcpy(object->buffer + object_size * write2_index, src, object_size); + SET_WRITE2_INDEX(write2_index); + } } void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - memcpy(dst, object->buffer, object_size); + triple_buffer_begin_read(object_size, object); + triple_buffer_actual_read(object_size, object, dst); + triple_buffer_end_read(object_size, object); } diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 66dcefa7f3..bf0e3ca67a 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -25,20 +25,56 @@ SOFTWARE. #include #include "protocol/triple_buffered_object.c" -Describe(TripleBufferedObject); -BeforeEach(TripleBufferedObject) {} -AfterEach(TripleBufferedObject) {} - typedef struct { uint8_t state; uint32_t buffer[3]; }test_object_t; -Ensure(TripleBufferedObject, writes_and_and_reads_object) { - test_object_t test_object; +test_object_t test_object; + +Describe(TripleBufferedObject); +BeforeEach(TripleBufferedObject) { + triple_buffer_init((triple_buffer_object_t*)&test_object); +} +AfterEach(TripleBufferedObject) {} + + +Ensure(TripleBufferedObject, writes_and_reads_object) { uint32_t src = 0x3456ABCC; uint32_t dst; triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(src)); } + +Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { + uint32_t src = 0x3456ABCC; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); + triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); + triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); + assert_that(dst, is_equal_to(src)); +} + +Ensure(TripleBufferedObject, writes_twice_and_reads_object) { + uint32_t src = 0x3456ABCC; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + src = 0x44778899; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(src)); +} + +Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { + uint32_t src = 1; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + src = 2; + triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); + triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); + assert_that(dst, is_equal_to(1)); +} From 8e2d70d11fd852cf6e33b48a027a5fba61c08779 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 00:44:25 +0200 Subject: [PATCH 044/109] Simplify the triple buffer implementation --- serial_link/protocol/triple_buffered_object.c | 46 ++++++++----------- .../tests/triple_buffered_object_tests.c | 18 ++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index a82cef5a63..77e45ec412 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -25,28 +25,28 @@ SOFTWARE. #include "protocol/triple_buffered_object.h" #define GET_READ_INDEX() object->state & 3 -#define GET_WRITE1_INDEX() (object->state >> 2) & 3 -#define GET_WRITE2_INDEX() (object->state >> 4) & 3 -#define GET_FREE_INDEX() (object->state >> 6) & 3 +#define GET_WRITE_INDEX() (object->state >> 2) & 3 +#define GET_SHARED_INDEX() (object->state >> 4) & 3 +#define GET_DATA_AVAILABLE() (object->state >> 6) & 1 #define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i) -#define SET_WRITE1_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) -#define SET_WRITE2_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) -#define SET_FREE_INDEX(i) object->state = ((object->state & ~(3 << 6)) | (i << 6)) +#define SET_WRITE_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) +#define SET_SHARED_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) +#define SET_DATA_AVAILABLE(i) object->state = ((object->state & ~(1 << 6)) | (i << 6)) void triple_buffer_init(triple_buffer_object_t* object) { object->state = 0; - SET_WRITE1_INDEX(0); - SET_WRITE2_INDEX(0); + SET_WRITE_INDEX(0); SET_READ_INDEX(1); - SET_FREE_INDEX(2); + SET_SHARED_INDEX(2); + SET_DATA_AVAILABLE(0); } static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { - uint8_t newest = GET_WRITE2_INDEX(); - uint8_t free_index = GET_READ_INDEX(); - SET_READ_INDEX(newest); - SET_FREE_INDEX(free_index); + uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + SET_READ_INDEX(shared_index); + SET_SHARED_INDEX(read_index); } static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { @@ -58,22 +58,12 @@ static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* } void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { - uint8_t write1_index = GET_WRITE1_INDEX(); - uint8_t write2_index = GET_WRITE2_INDEX(); - uint8_t read_index = GET_READ_INDEX(); - uint8_t free_index = GET_FREE_INDEX(); - if (write2_index == read_index) { - // We are reading from the other write index - SET_WRITE1_INDEX(free_index); - memcpy(object->buffer + object_size * free_index, src, object_size); - SET_WRITE2_INDEX(free_index); - } - else { - SET_WRITE1_INDEX(write2_index); - memcpy(object->buffer + object_size * write2_index, src, object_size); - SET_WRITE2_INDEX(write2_index); - } + uint8_t write_index = GET_WRITE_INDEX(); + memcpy(object->buffer + object_size * write_index, src, object_size); + uint8_t shared_index = GET_SHARED_INDEX(); + SET_SHARED_INDEX(write_index); + SET_WRITE_INDEX(shared_index); } void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index bf0e3ca67a..5fa1b8b62f 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -77,4 +77,22 @@ Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); assert_that(dst, is_equal_to(1)); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(2)); +} + +Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { + uint32_t src = 1; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); + src = 2; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + src = 3; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); + triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); + assert_that(dst, is_equal_to(1)); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(3)); } From b7059d35f90a7cc8b07944d78984afa832190bdc Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 01:07:44 +0200 Subject: [PATCH 045/109] Working triple buffering --- serial_link/protocol/triple_buffered_object.c | 37 ++++++++++++++----- serial_link/protocol/triple_buffered_object.h | 3 +- serial_link/system/system.h | 36 ++++++++++++++++++ .../tests/triple_buffered_object_tests.c | 8 ++++ 4 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 serial_link/system/system.h diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 77e45ec412..3fabb7d84b 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -23,6 +23,7 @@ SOFTWARE. */ #include "protocol/triple_buffered_object.h" +#include "system/system.h" #define GET_READ_INDEX() object->state & 3 #define GET_WRITE_INDEX() (object->state >> 2) & 3 @@ -42,11 +43,21 @@ void triple_buffer_init(triple_buffer_object_t* object) { SET_DATA_AVAILABLE(0); } -static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { - uint8_t shared_index = GET_SHARED_INDEX(); - uint8_t read_index = GET_READ_INDEX(); - SET_READ_INDEX(shared_index); - SET_SHARED_INDEX(read_index); +static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { + serial_link_lock(); + if (GET_DATA_AVAILABLE()) { + uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + SET_READ_INDEX(shared_index); + SET_SHARED_INDEX(read_index); + SET_DATA_AVAILABLE(false); + serial_link_unlock(); + return true; + } + else { + serial_link_unlock(); + return false; + } } static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { @@ -61,13 +72,21 @@ void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, v uint8_t write_index = GET_WRITE_INDEX(); memcpy(object->buffer + object_size * write_index, src, object_size); + serial_link_lock(); uint8_t shared_index = GET_SHARED_INDEX(); SET_SHARED_INDEX(write_index); SET_WRITE_INDEX(shared_index); + SET_DATA_AVAILABLE(true); + serial_link_unlock(); } -void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - triple_buffer_begin_read(object_size, object); - triple_buffer_actual_read(object_size, object, dst); - triple_buffer_end_read(object_size, object); +bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { + if (triple_buffer_begin_read(object_size, object)) { + triple_buffer_actual_read(object_size, object, dst); + triple_buffer_end_read(object_size, object); + return true; + } + else { + return false; + } } diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index ebdbcae792..705f0c49f9 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -30,8 +30,9 @@ typedef struct { uint8_t buffer[]; }triple_buffer_object_t; +void triple_buffer_init(triple_buffer_object_t* object); void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src); -void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); +bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); #endif diff --git a/serial_link/system/system.h b/serial_link/system/system.h new file mode 100644 index 0000000000..c798e64774 --- /dev/null +++ b/serial_link/system/system.h @@ -0,0 +1,36 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_SYSTEM_H +#define SERIAL_LINK_SYSTEM_H + +void serial_link_lock() { + +} + +void serial_link_unlock() { + +} + +#endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 5fa1b8b62f..cd3ecb6a22 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -47,6 +47,12 @@ Ensure(TripleBufferedObject, writes_and_reads_object) { assert_that(dst, is_equal_to(src)); } +Ensure(TripleBufferedObject, does_not_read_empty) { + uint32_t dst; + bool res = triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(res, is_equal_to(false)); +} + Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { uint32_t src = 0x3456ABCC; uint32_t dst; @@ -79,6 +85,7 @@ Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { assert_that(dst, is_equal_to(1)); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(2)); + assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); } Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { @@ -95,4 +102,5 @@ Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { assert_that(dst, is_equal_to(1)); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(3)); + assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); } From 4ee6eadf9e88b89f017c3c06e2d376cf953f6a42 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 12:53:51 +0200 Subject: [PATCH 046/109] Local and remote objects WIP --- serial_link/protocol/transport.h | 67 +++++++++++++++++++ serial_link/protocol/triple_buffered_object.h | 2 +- serial_link/tests/transport_tests.c | 26 +++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 01119857d3..6f2cf277fb 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -22,6 +22,73 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_TRANSPORT_H +#define SERIAL_LINK_TRANSPORT_H + +#include "protocol/triple_buffered_object.h" + +#define NUM_SLAVES 8 + +typedef struct { + uint16_t element_size; + uint16_t buffer_size; + uint8_t is_master; + uint8_t buffer[] __attribute__((aligned(4))); +} remote_object_t; + +typedef struct { + uint16_t element_size; + uint8_t destination; + uint8_t buffer[] __attribute__((aligned(4))); +} local_object_t; + +#define REMOTE_OBJECT_BUFFER(id, type) \ +typedef struct { \ + triple_buffer_object_t object; \ + type buffer[3]; \ +} remote_object_buffer_##id##_t; + +#define MASTER_REMOTE_OBJECT(id, type) \ +REMOTE_OBJECT_BUFFER(id, type) \ +typedef struct { \ + remote_object_t object; \ + remote_object_buffer_##id##_t buffer; \ +} master_remote_object_##id##_t; \ +master_remote_object_##id##_t remote_object_##id = { \ + .object = { \ + .element_size = sizeof(type), \ + .buffer_size = sizeof(remote_object_buffer_##id##_t), \ + .is_master = true \ + }}; + +#define SLAVE_REMOTE_OBJECT(id, type) \ +REMOTE_OBJECT_BUFFER(id, type) \ +typedef struct { \ + remote_object_t object; \ + remote_object_buffer_##id##_t buffer[NUM_SLAVES];\ +} slave_remote_object_##id##_t; \ +slave_remote_object_##id##_t remote_object_##id = { \ + .object = { \ + .element_size = sizeof(type), \ + .buffer_size = sizeof(remote_object_buffer_##id##_t), \ + .is_master = true \ + }}; + +#define LOCAL_OBJECT(id, type) \ +typedef struct { \ + uint32_t element_size; \ + uint8_t buffer[NUM_SLAVES][sizeof(type) + 16][3]; \ +} remote_object_##id##_t; \ +remote_object_##id##_t remote_object_##id = {.element_size = sizeof(type) + 16}; + +#define REMOTE_OBJECT(id) (remote_object_t*)&remote_object_##id + + void init_transport(void); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); + +void transport_register_master_remote_object(uint8_t id, void* ptr, uint16_t size); +void transport_register_slave_remote_object(uint8_t id, void* ptr, uint16_t size); + +#endif diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index 705f0c49f9..d224f36a2e 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -27,7 +27,7 @@ SOFTWARE. typedef struct { uint8_t state; - uint8_t buffer[]; + uint8_t buffer[] __attribute__((aligned(4))); }triple_buffer_object_t; void triple_buffer_init(triple_buffer_object_t* object); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index a64e7446d8..f9f5b47737 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -25,6 +25,32 @@ SOFTWARE. #include #include "protocol/transport.c" +typedef struct { + uint32_t test; +} test_object1_t; + +typedef struct { + uint32_t test1; + uint32_t test2; +} test_object2_t; + +MASTER_REMOTE_OBJECT(0, test_object1_t); +SLAVE_REMOTE_OBJECT(1, test_object1_t); +MASTER_REMOTE_OBJECT(2, test_object2_t); +SLAVE_REMOTE_OBJECT(3, test_object2_t); + +// We want +// master -> slave = 1 local(target all), 1 remote object +// slave -> master = 1 local(target 0), multiple remote objects +// master -> single slave (multiple local, target id), 1 remote object + +remote_object_t* remote_objects[] = { + REMOTE_OBJECT(0), + REMOTE_OBJECT(1), + REMOTE_OBJECT(2), + REMOTE_OBJECT(3), +}; + Describe(Transport); BeforeEach(Transport) { init_transport(); From 679bfe7c5452f69a790e648c7661bbbdb12ce4ae Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 17:33:31 +0200 Subject: [PATCH 047/109] More transport WIP --- serial_link/protocol/transport.c | 9 +-- serial_link/protocol/transport.h | 91 +++++++++++++++-------------- serial_link/tests/transport_tests.c | 29 +++------ 3 files changed, 61 insertions(+), 68 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 399894ff98..7b2c149504 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -24,10 +24,12 @@ SOFTWARE. #include "protocol/transport.h" -static uint32_t current_send_frame; +static remote_object_t* remote_objects; +static uint32_t num_remote_objects; -void init_transport(void) { - current_send_frame = 0; +void init_transport(remote_object_t* _remote_objects, uint32_t _num_remote_objects) { + remote_objects = _remote_objects; + num_remote_objects = _num_remote_objects; } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { @@ -35,5 +37,4 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { - return ++current_send_frame; } diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 6f2cf277fb..86086b8b8e 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -28,63 +28,66 @@ SOFTWARE. #include "protocol/triple_buffered_object.h" #define NUM_SLAVES 8 +#define LOCAL_OBJECT_EXTRA 16 + +// master -> slave = 1 local(target all), 1 remote object +// slave -> master = 1 local(target 0), multiple remote objects +// master -> single slave (multiple local, target id), 1 remote object +typedef enum { + MASTER_TO_ALL_SLAVES, + MASTER_TO_SINGLE_SLAVE, + SLAVE_TO_MASTER, +} remote_object_type; typedef struct { - uint16_t element_size; - uint16_t buffer_size; - uint8_t is_master; + remote_object_type object_type; + uint16_t object_size; uint8_t buffer[] __attribute__((aligned(4))); } remote_object_t; -typedef struct { - uint16_t element_size; - uint8_t destination; - uint8_t buffer[] __attribute__((aligned(4))); -} local_object_t; +#define REMOTE_OBJECT_SIZE(objectsize) \ + (sizeof(triple_buffer_object_t) + objectsize * 3) +#define LOCAL_OBJECT_SIZE(objectsize) \ + (sizeof(triple_buffer_object_t) + (objectsize + LOCAL_OBJECT_EXTRA) * 3) -#define REMOTE_OBJECT_BUFFER(id, type) \ -typedef struct { \ - triple_buffer_object_t object; \ - type buffer[3]; \ -} remote_object_buffer_##id##_t; - -#define MASTER_REMOTE_OBJECT(id, type) \ -REMOTE_OBJECT_BUFFER(id, type) \ +#define REMOTE_OBJECT_HELPER(name, type, num_local, num_remote) \ typedef struct { \ remote_object_t object; \ - remote_object_buffer_##id##_t buffer; \ -} master_remote_object_##id##_t; \ -master_remote_object_##id##_t remote_object_##id = { \ - .object = { \ - .element_size = sizeof(type), \ - .buffer_size = sizeof(remote_object_buffer_##id##_t), \ - .is_master = true \ - }}; + uint8_t buffer[ \ + num_remote * REMOTE_OBJECT_SIZE(sizeof(type)) + \ + num_local * LOCAL_OBJECT_SIZE(sizeof(type))]; \ +} remote_object_##name##_t; -#define SLAVE_REMOTE_OBJECT(id, type) \ -REMOTE_OBJECT_BUFFER(id, type) \ -typedef struct { \ - remote_object_t object; \ - remote_object_buffer_##id##_t buffer[NUM_SLAVES];\ -} slave_remote_object_##id##_t; \ -slave_remote_object_##id##_t remote_object_##id = { \ - .object = { \ - .element_size = sizeof(type), \ - .buffer_size = sizeof(remote_object_buffer_##id##_t), \ - .is_master = true \ - }}; +#define MASTER_TO_ALL_SLAVES_OBJECT(name, type) \ + REMOTE_OBJECT_HELPER(name, type, 1, 1) \ + remote_object_##name##_t remote_object_##name = { \ + .object = { \ + .object_type = MASTER_TO_ALL_SLAVES, \ + .object_size = sizeof(type), \ + } \ + }; -#define LOCAL_OBJECT(id, type) \ -typedef struct { \ - uint32_t element_size; \ - uint8_t buffer[NUM_SLAVES][sizeof(type) + 16][3]; \ -} remote_object_##id##_t; \ -remote_object_##id##_t remote_object_##id = {.element_size = sizeof(type) + 16}; +#define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ + REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \ + remote_object_##name##_t remote_object_##name = { \ + .object = { \ + .object_type = MASTER_TO_SINGLE_SLAVE, \ + .object_size = sizeof(type), \ + } \ + }; -#define REMOTE_OBJECT(id) (remote_object_t*)&remote_object_##id +#define SLAVE_TO_MASTER_OBJECT(name, type) \ + REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \ + remote_object_##name##_t remote_object_##name = { \ + .object = { \ + .object_type = SLAVE_TO_MASTER, \ + .object_size = sizeof(type), \ + } \ + }; +#define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(void); +void init_transport(remote_object_t* remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index f9f5b47737..31e7b2dc7e 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -34,32 +34,21 @@ typedef struct { uint32_t test2; } test_object2_t; -MASTER_REMOTE_OBJECT(0, test_object1_t); -SLAVE_REMOTE_OBJECT(1, test_object1_t); -MASTER_REMOTE_OBJECT(2, test_object2_t); -SLAVE_REMOTE_OBJECT(3, test_object2_t); +MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t); +MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t); +SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); -// We want -// master -> slave = 1 local(target all), 1 remote object -// slave -> master = 1 local(target 0), multiple remote objects -// master -> single slave (multiple local, target id), 1 remote object - -remote_object_t* remote_objects[] = { - REMOTE_OBJECT(0), - REMOTE_OBJECT(1), - REMOTE_OBJECT(2), - REMOTE_OBJECT(3), +remote_object_t* test_remote_objects[] = { + REMOTE_OBJECT(master_to_slave), + REMOTE_OBJECT(master_to_single_slave), + REMOTE_OBJECT(slave_to_master), }; Describe(Transport); BeforeEach(Transport) { - init_transport(); + init_transport(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t)); } AfterEach(Transport) {} -Ensure(Transport, packet_number_is_sequential) { - assert_that(transport_send_frame(0, NULL, 0), is_equal_to(1)); - assert_that(transport_send_frame(0, NULL, 0), is_equal_to(2)); - // It doesn't matter if the destination changes - assert_that(transport_send_frame(1, NULL, 0), is_equal_to(3)); +Ensure(Transport, write_to_local_signals_an_event) { } From 2710361cd818aae6dd349fa433ebff365908de06 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 18:30:04 +0200 Subject: [PATCH 048/109] Change the triple buffer object interface --- serial_link/protocol/triple_buffered_object.c | 34 +++----- serial_link/protocol/triple_buffered_object.h | 15 +++- .../tests/triple_buffered_object_tests.c | 78 +++++++------------ 3 files changed, 49 insertions(+), 78 deletions(-) diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 3fabb7d84b..6b3cf75adf 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -43,7 +43,7 @@ void triple_buffer_init(triple_buffer_object_t* object) { SET_DATA_AVAILABLE(0); } -static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { +void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) { serial_link_lock(); if (GET_DATA_AVAILABLE()) { uint8_t shared_index = GET_SHARED_INDEX(); @@ -52,41 +52,25 @@ static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_ SET_SHARED_INDEX(read_index); SET_DATA_AVAILABLE(false); serial_link_unlock(); - return true; + return object->buffer + object_size * shared_index; } else { serial_link_unlock(); - return false; + return NULL; } } -static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - uint8_t read_index = GET_READ_INDEX(); - memcpy(dst, object->buffer + object_size*read_index, object_size); -} - -static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* object) { -} - -void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { - +void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) { uint8_t write_index = GET_WRITE_INDEX(); - memcpy(object->buffer + object_size * write_index, src, object_size); + return object->buffer + object_size * write_index; +} + +void triple_buffer_end_write_internal(triple_buffer_object_t* object) { serial_link_lock(); uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t write_index = GET_WRITE_INDEX(); SET_SHARED_INDEX(write_index); SET_WRITE_INDEX(shared_index); SET_DATA_AVAILABLE(true); serial_link_unlock(); } - -bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - if (triple_buffer_begin_read(object_size, object)) { - triple_buffer_actual_read(object_size, object, dst); - triple_buffer_end_read(object_size, object); - return true; - } - else { - return false; - } -} diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index d224f36a2e..03209709cb 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -31,8 +31,19 @@ typedef struct { }triple_buffer_object_t; void triple_buffer_init(triple_buffer_object_t* object); -void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src); -bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); + +#define triple_buffer_begin_write(object) \ + (typeof(*object.buffer[0])*)triple_buffer_begin_write_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object) + +#define triple_buffer_end_write(object) \ + triple_buffer_end_write_internal((triple_buffer_object_t*)object) + +#define triple_buffer_read(object) \ + (typeof(*object.buffer[0])*)triple_buffer_read_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object) + +void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object); +void triple_buffer_end_write_internal(triple_buffer_object_t* object); +void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object); #endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index cd3ecb6a22..1017df8f5e 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -40,67 +40,43 @@ AfterEach(TripleBufferedObject) {} Ensure(TripleBufferedObject, writes_and_reads_object) { - uint32_t src = 0x3456ABCC; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(src)); + *triple_buffer_begin_write(&test_object) = 0x3456ABCC; + triple_buffer_end_write(&test_object); + assert_that(*triple_buffer_read(&test_object), is_equal_to(0x3456ABCC)); } Ensure(TripleBufferedObject, does_not_read_empty) { - uint32_t dst; - bool res = triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(res, is_equal_to(false)); -} - -Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { - uint32_t src = 0x3456ABCC; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); - triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); - triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); - assert_that(dst, is_equal_to(src)); + assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); } Ensure(TripleBufferedObject, writes_twice_and_reads_object) { - uint32_t src = 0x3456ABCC; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - src = 0x44778899; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(src)); + *triple_buffer_begin_write(&test_object) = 0x3456ABCC; + triple_buffer_end_write(&test_object); + *triple_buffer_begin_write(&test_object) = 0x44778899; + triple_buffer_end_write(&test_object); + assert_that(*triple_buffer_read(&test_object), is_equal_to(0x44778899)); } Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { - uint32_t src = 1; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - src = 2; - triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); - triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); - assert_that(dst, is_equal_to(1)); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(2)); - assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); + *triple_buffer_begin_write(&test_object) = 1; + triple_buffer_end_write(&test_object); + uint32_t* read = triple_buffer_read(&test_object); + *triple_buffer_begin_write(&test_object) = 2; + triple_buffer_end_write(&test_object); + assert_that(*read, is_equal_to(1)); + assert_that(*triple_buffer_read(&test_object), is_equal_to(2)); + assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); } Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { - uint32_t src = 1; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); - src = 2; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - src = 3; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); - triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); - assert_that(dst, is_equal_to(1)); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(3)); - assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); + *triple_buffer_begin_write(&test_object) = 1; + triple_buffer_end_write(&test_object); + uint32_t* read = triple_buffer_read(&test_object); + *triple_buffer_begin_write(&test_object) = 2; + triple_buffer_end_write(&test_object); + *triple_buffer_begin_write(&test_object) = 3; + triple_buffer_end_write(&test_object); + assert_that(*read, is_equal_to(1)); + assert_that(*triple_buffer_read(&test_object), is_equal_to(3)); + assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); } From fe1db50fc32c13bb1030676b1381125398bce5de Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 18:51:10 +0200 Subject: [PATCH 049/109] Writing signals events --- serial_link/protocol/transport.h | 22 +++++++++++++++++++--- serial_link/system/system.h | 4 ++-- serial_link/tests/transport_tests.c | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 86086b8b8e..c4c52947b2 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -26,6 +26,7 @@ SOFTWARE. #define SERIAL_LINK_TRANSPORT_H #include "protocol/triple_buffered_object.h" +#include "system/system.h" #define NUM_SLAVES 8 #define LOCAL_OBJECT_EXTRA 16 @@ -65,7 +66,12 @@ typedef struct { \ .object_type = MASTER_TO_ALL_SLAVES, \ .object_size = sizeof(type), \ } \ - }; + }; \ + type* begin_write_##name(void) { \ + }\ + void end_write_##name(void) { \ + signal_data_written(); \ + } #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \ @@ -74,7 +80,12 @@ typedef struct { \ .object_type = MASTER_TO_SINGLE_SLAVE, \ .object_size = sizeof(type), \ } \ - }; + }; \ + type* begin_write_##name(uint8_t slave) { \ + }\ + void end_write_##name(uint8_t slave) { \ + signal_data_written(); \ + } #define SLAVE_TO_MASTER_OBJECT(name, type) \ REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \ @@ -83,7 +94,12 @@ typedef struct { \ .object_type = SLAVE_TO_MASTER, \ .object_size = sizeof(type), \ } \ - }; + }; \ + type* begin_write_##name(void) { \ + }\ + void end_write_##name(void) { \ + signal_data_written(); \ + } #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name diff --git a/serial_link/system/system.h b/serial_link/system/system.h index c798e64774..163349953d 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -26,11 +26,11 @@ SOFTWARE. #define SERIAL_LINK_SYSTEM_H void serial_link_lock() { - } void serial_link_unlock() { - } +void signal_data_written(void); + #endif diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 31e7b2dc7e..61b9a65080 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -23,8 +23,13 @@ SOFTWARE. */ #include +#include #include "protocol/transport.c" +void signal_data_written(void) { + mock(); +} + typedef struct { uint32_t test; } test_object1_t; @@ -51,4 +56,13 @@ BeforeEach(Transport) { AfterEach(Transport) {} Ensure(Transport, write_to_local_signals_an_event) { + begin_write_master_to_slave(); + expect(signal_data_written); + end_write_master_to_slave(); + begin_write_slave_to_master(); + expect(signal_data_written); + end_write_slave_to_master(); + begin_write_master_to_single_slave(1); + expect(signal_data_written); + end_write_master_to_single_slave(1); } From 415ab91c9d2ed034bbbabf3cbb8502f525b91abb Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:04:42 +0200 Subject: [PATCH 050/109] Add master to slave transport --- serial_link/protocol/transport.c | 31 ++++++++++++++++++++++++++--- serial_link/protocol/transport.h | 18 +++++++++++++---- serial_link/tests/transport_tests.c | 28 +++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 7b2c149504..55dae9fa22 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -23,18 +23,43 @@ SOFTWARE. */ #include "protocol/transport.h" +#include "protocol/frame_router.h" +#include "protocol/triple_buffered_object.h" -static remote_object_t* remote_objects; +static remote_object_t** remote_objects; static uint32_t num_remote_objects; -void init_transport(remote_object_t* _remote_objects, uint32_t _num_remote_objects) { +void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { - + uint8_t id = data[size-1]; + remote_object_t* obj = remote_objects[id]; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); + } } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { } + +void update_transport(void) { + int i; + for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; + uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb); + if (ptr) { + ptr[obj->object_size] = i; + router_send_frame(0xFF, ptr, obj->object_size + 1); + } + } + } +} diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index c4c52947b2..fa2e00d2d3 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -68,9 +68,21 @@ typedef struct { \ } \ }; \ type* begin_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ @@ -103,11 +115,9 @@ typedef struct { \ #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(remote_object_t* remote_objects, uint32_t num_remote_objects); +void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); - -void transport_register_master_remote_object(uint8_t id, void* ptr, uint16_t size); -void transport_register_slave_remote_object(uint8_t id, void* ptr, uint16_t size); +void update_transport(void); #endif diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 61b9a65080..34d451de4d 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -25,11 +25,21 @@ SOFTWARE. #include #include #include "protocol/transport.c" +#include "protocol/triple_buffered_object.c" void signal_data_written(void) { mock(); } +static uint8_t sent_data[2048]; +static uint16_t sent_data_size; + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + mock(destination); + memcpy(sent_data + sent_data_size, data, size); + sent_data_size += size; +} + typedef struct { uint32_t test; } test_object1_t; @@ -51,7 +61,8 @@ remote_object_t* test_remote_objects[] = { Describe(Transport); BeforeEach(Transport) { - init_transport(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t)); + init_transport(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); + sent_data_size = 0; } AfterEach(Transport) {} @@ -66,3 +77,18 @@ Ensure(Transport, write_to_local_signals_an_event) { expect(signal_data_written); end_write_master_to_single_slave(1); } + +Ensure(Transport, writes_from_master_to_all_slaves) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 5; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame, + when(destination, is_equal_to(0xFF))); + update_transport(); + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(5)); +} From 64b7efeb72ff67d088641bda798226e619601a43 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:15:42 +0200 Subject: [PATCH 051/109] Add proper initialization of transport buffers --- serial_link/protocol/transport.c | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 55dae9fa22..c4019d7fed 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -32,6 +32,40 @@ static uint32_t num_remote_objects; void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; + int i; + for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; + triple_buffer_init(tb); + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + tb = (triple_buffer_object_t*)start; + triple_buffer_init(tb); + } + else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) { + uint8_t* start = obj->buffer; + int j; + for (j=0;jobject_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + triple_buffer_init(tb); + } + else { + uint8_t* start = obj->buffer; + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + triple_buffer_init(tb); + start += LOCAL_OBJECT_SIZE(obj->object_size); + int j; + for (j=0;jobject_size); + } + } + } } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { From 7b88b8b7e1fd50b1fc2a219c734a3069576bcbe2 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:32:01 +0200 Subject: [PATCH 052/109] Add slave to master transport --- serial_link/protocol/transport.c | 20 +++++++++++++------- serial_link/protocol/transport.h | 13 +++++++++++++ serial_link/tests/transport_tests.c | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index c4019d7fed..e49e9d69d9 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -71,13 +71,18 @@ void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_obje void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; remote_object_t* obj = remote_objects[id]; + uint8_t* start; if (obj->object_type == MASTER_TO_ALL_SLAVES) { - uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { @@ -87,12 +92,13 @@ void update_transport(void) { int i; for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) { triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb); if (ptr) { ptr[obj->object_size] = i; - router_send_frame(0xFF, ptr, obj->object_size + 1); + uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0; + router_send_frame(dest, ptr, obj->object_size + 1); } } } diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index fa2e00d2d3..94ff85312a 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -108,9 +108,22 @@ typedef struct { \ } \ }; \ type* begin_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name(uint8_t slave) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\ + start+=slave * REMOTE_OBJECT_SIZE(obj->object_size); \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 34d451de4d..334827507a 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -92,3 +92,19 @@ Ensure(Transport, writes_from_master_to_all_slaves) { assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2->test, is_equal_to(5)); } + +Ensure(Transport, writes_from_slave_to_master) { + update_transport(); + test_object1_t* obj = begin_write_slave_to_master(); + obj->test = 7; + expect(signal_data_written); + end_write_slave_to_master(); + expect(router_send_frame, + when(destination, is_equal_to(0))); + update_transport(); + transport_recv_frame(3, sent_data, sent_data_size); + test_object1_t* obj2 = read_slave_to_master(2); + assert_that(read_slave_to_master(0), is_equal_to(NULL)); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(7)); +} From 8cbfe79dd9ce2e8113a0f99e15d6ae4d3176b545 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:47:40 +0200 Subject: [PATCH 053/109] Add master to single slave transport --- serial_link/protocol/transport.c | 17 +++++++++++++++++ serial_link/protocol/transport.h | 16 ++++++++++++++++ serial_link/tests/transport_tests.c | 15 +++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index e49e9d69d9..03f83a8068 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -79,6 +79,9 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } triple_buffer_object_t* tb = (triple_buffer_object_t*)start; void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); memcpy(ptr, data, size -1); @@ -101,5 +104,19 @@ void update_transport(void) { router_send_frame(dest, ptr, obj->object_size + 1); } } + else { + uint8_t* start = obj->buffer; + int j; + for (j=0;jobject_size + LOCAL_OBJECT_EXTRA, tb); + if (ptr) { + ptr[obj->object_size] = i; + uint8_t dest = j + 1; + router_send_frame(dest, ptr, obj->object_size + 1); + } + start += LOCAL_OBJECT_SIZE(obj->object_size); + } + } } } diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 94ff85312a..a1a83b8f76 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -94,9 +94,25 @@ typedef struct { \ } \ }; \ type* begin_write_##name(uint8_t slave) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer;\ + start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(uint8_t slave) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer;\ + start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name() { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);\ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define SLAVE_TO_MASTER_OBJECT(name, type) \ diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 334827507a..3fa8eab4ac 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -108,3 +108,18 @@ Ensure(Transport, writes_from_slave_to_master) { assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2->test, is_equal_to(7)); } + +Ensure(Transport, writes_from_master_to_single_slave) { + update_transport(); + test_object1_t* obj = begin_write_master_to_single_slave(3); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_single_slave(3); + expect(router_send_frame, + when(destination, is_equal_to(4))); + update_transport(); + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_single_slave(); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(7)); +} From 6873b17117a41715c9a8ca63060974b64b4cdcf4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 23:17:59 +0200 Subject: [PATCH 054/109] Support for Chibios compilation Remove some warnings, change the include paths. --- serial_link.mk | 3 ++- serial_link/protocol/byte_stuffer.c | 8 ++++---- serial_link/protocol/byte_stuffer.h | 7 +++++++ serial_link/protocol/frame_router.c | 5 +++-- serial_link/protocol/frame_router.h | 8 ++++++++ serial_link/protocol/frame_validator.c | 7 ++++--- serial_link/protocol/frame_validator.h | 7 +++++++ serial_link/protocol/physical.h | 5 +++++ serial_link/protocol/transport.c | 20 +++++++++---------- serial_link/protocol/transport.h | 5 ++--- serial_link/protocol/triple_buffered_object.c | 6 ++++-- serial_link/protocol/triple_buffered_object.h | 2 ++ serial_link/system/system.h | 12 ++++++++--- serial_link/tests/Makefile | 2 +- serial_link/tests/byte_stuffer_tests.c | 8 ++++---- serial_link/tests/frame_router_tests.c | 8 ++++---- serial_link/tests/frame_validator_tests.c | 2 +- serial_link/tests/transport_tests.c | 4 ++-- .../tests/triple_buffered_object_tests.c | 2 +- 19 files changed, 79 insertions(+), 42 deletions(-) diff --git a/serial_link.mk b/serial_link.mk index de2364108a..e8915a33f9 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -20,4 +20,5 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -INC += $(SERIAL_DIR) \ No newline at end of file +INC += $(SERIAL_DIR) +SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) \ No newline at end of file diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 8b529667fd..fb4c45a8dc 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -22,10 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/byte_stuffer.h" -#include "protocol/frame_validator.h" -#include "protocol/physical.h" -#include +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/physical.h" +#include // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 839a876fe8..2cc88beb42 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,6 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_BYTE_STUFFER_H +#define SERIAL_LINK_BYTE_STUFFER_H + +#include + void init_byte_stuffer(void); void byte_stuffer_recv_byte(uint8_t link, uint8_t data); void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 890ebbe9ea..04b8c2e75c 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -22,8 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/frame_router.h" -#include "protocol/transport.h" +#include "serial_link/protocol/frame_router.h" +#include "serial_link/protocol/transport.h" +#include "serial_link/protocol/frame_validator.h" static bool is_master; diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index 67db3122fa..712250ff35 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,9 +22,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_FRAME_ROUTER_H +#define SERIAL_LINK_FRAME_ROUTER_H + +#include +#include + #define UP_LINK 0 #define DOWN_LINK 1 void router_set_master(bool master); void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index a3face6506..474f80ee8e 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -22,9 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/frame_validator.h" -#include "protocol/frame_router.h" -#include "protocol/byte_stuffer.h" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/frame_router.h" +#include "serial_link/protocol/byte_stuffer.h" +#include const uint32_t poly8_lookup[256] = { diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index c35fc27260..4a910d510b 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,6 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_FRAME_VALIDATOR_H +#define SERIAL_LINK_FRAME_VALIDATOR_H + +#include + void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size); // The buffer pointed to by the data needs 4 additional bytes void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h index ee5883d36b..425e06cdd2 100644 --- a/serial_link/protocol/physical.h +++ b/serial_link/protocol/physical.h @@ -22,4 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_PHYSICAL_H +#define SERIAL_LINK_PHYSICAL_H + void send_data(uint8_t link, const uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 03f83a8068..4542a7a050 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -22,9 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/transport.h" -#include "protocol/frame_router.h" -#include "protocol/triple_buffered_object.h" +#include "serial_link/protocol/transport.h" +#include "serial_link/protocol/frame_router.h" +#include "serial_link/protocol/triple_buffered_object.h" +#include static remote_object_t** remote_objects; static uint32_t num_remote_objects; @@ -32,7 +33,7 @@ static uint32_t num_remote_objects; void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; - int i; + unsigned int i; for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { @@ -44,7 +45,7 @@ void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_obje } else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) { uint8_t* start = obj->buffer; - int j; + unsigned int j; for (j=0;jobject_size); - int j; + unsigned int j; for (j=0;jobject_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) { @@ -106,7 +104,7 @@ void update_transport(void) { } else { uint8_t* start = obj->buffer; - int j; + unsigned int j; for (j=0;jobject_size + LOCAL_OBJECT_EXTRA, tb); diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index a1a83b8f76..9e9e22462c 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -25,8 +25,8 @@ SOFTWARE. #ifndef SERIAL_LINK_TRANSPORT_H #define SERIAL_LINK_TRANSPORT_H -#include "protocol/triple_buffered_object.h" -#include "system/system.h" +#include "serial_link/protocol/triple_buffered_object.h" +#include "serial_link/system/system.h" #define NUM_SLAVES 8 #define LOCAL_OBJECT_EXTRA 16 @@ -146,7 +146,6 @@ typedef struct { \ void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); -uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); void update_transport(void); #endif diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 6b3cf75adf..c6bf28af0a 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -22,8 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/triple_buffered_object.h" -#include "system/system.h" +#include "serial_link/protocol/triple_buffered_object.h" +#include "serial_link/system/system.h" +#include +#include #define GET_READ_INDEX() object->state & 3 #define GET_WRITE_INDEX() (object->state >> 2) & 3 diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index 03209709cb..2e57db3f50 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -25,6 +25,8 @@ SOFTWARE. #ifndef SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H #define SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H +#include + typedef struct { uint8_t state; uint8_t buffer[] __attribute__((aligned(4))); diff --git a/serial_link/system/system.h b/serial_link/system/system.h index 163349953d..1e4c610b1d 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -25,12 +25,18 @@ SOFTWARE. #ifndef SERIAL_LINK_SYSTEM_H #define SERIAL_LINK_SYSTEM_H -void serial_link_lock() { +inline void serial_link_lock(void) { } -void serial_link_unlock() { +inline void serial_link_unlock(void) { } -void signal_data_written(void); +void singal_data_written(void); + +#if defined(PROTOCOL_CHIBIOS) + +inline void signal_data_written(void) { +} +#endif #endif diff --git a/serial_link/tests/Makefile b/serial_link/tests/Makefile index 0d8ba4b7b6..1b072c6f1d 100644 --- a/serial_link/tests/Makefile +++ b/serial_link/tests/Makefile @@ -22,7 +22,7 @@ CC = gcc CFLAGS = -INCLUDES = -I. -I../ +INCLUDES = -I. -I../../ LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared LDLIBS = -lcgreen UNITOBJ = $(BUILDDIR)/serialtest/unitobj diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 912c4d3211..64b170e8c1 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -24,10 +24,10 @@ SOFTWARE. #include #include -#include "protocol/byte_stuffer.h" -#include "protocol/byte_stuffer.c" -#include "protocol/frame_validator.h" -#include "protocol/physical.h" +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/byte_stuffer.c" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/physical.h" static uint8_t sent_data[MAX_FRAME_SIZE*2]; static uint16_t sent_data_size; diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 0b0ea6e7f4..6c806fa939 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -24,10 +24,10 @@ SOFTWARE. #include #include -#include "protocol/byte_stuffer.c" -#include "protocol/frame_validator.c" -#include "protocol/frame_router.c" -#include "protocol/transport.h" +#include "serial_link/protocol/byte_stuffer.c" +#include "serial_link/protocol/frame_validator.c" +#include "serial_link/protocol/frame_router.c" +#include "serial_link/protocol/transport.h" static uint8_t received_data[256]; static uint16_t received_data_size; diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index f4abd14d1b..d20947e2c9 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -24,7 +24,7 @@ SOFTWARE. #include #include -#include "protocol/frame_validator.c" +#include "serial_link/protocol/frame_validator.c" void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 3fa8eab4ac..3e9bffdfa7 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -24,8 +24,8 @@ SOFTWARE. #include #include -#include "protocol/transport.c" -#include "protocol/triple_buffered_object.c" +#include "serial_link/protocol/transport.c" +#include "serial_link/protocol/triple_buffered_object.c" void signal_data_written(void) { mock(); diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 1017df8f5e..6f7c82b468 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -23,7 +23,7 @@ SOFTWARE. */ #include -#include "protocol/triple_buffered_object.c" +#include "serial_link/protocol/triple_buffered_object.c" typedef struct { uint8_t state; From 36e4869531b19b19eb4301af2016875b9f4f6e82 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 23:52:00 +0200 Subject: [PATCH 055/109] Create a thread for the serial link --- serial_link.mk | 3 ++- serial_link/system/system.c | 40 +++++++++++++++++++++++++++++++++++++ serial_link/system/system.h | 24 +++++++++++++++++----- 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 serial_link/system/system.c diff --git a/serial_link.mk b/serial_link.mk index e8915a33f9..434966a22a 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -21,4 +21,5 @@ # SOFTWARE. INC += $(SERIAL_DIR) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) \ No newline at end of file +SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) +SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) \ No newline at end of file diff --git a/serial_link/system/system.c b/serial_link/system/system.c new file mode 100644 index 0000000000..2001f4d5cc --- /dev/null +++ b/serial_link/system/system.c @@ -0,0 +1,40 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#include "serial_link/system/system.h" + +// TODO: Optimize the stack size, this is probably way too big +static THD_WORKING_AREA(serialThreadStack, 1024); +static THD_FUNCTION(serialThread, arg) { + (void)arg; +} + + +void init_serial_link(void) { + (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), + LOWPRIO, serialThread, NULL); +} + +void signal_data_written(void) { + +} diff --git a/serial_link/system/system.h b/serial_link/system/system.h index 1e4c610b1d..e8c1caec08 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -25,18 +25,32 @@ SOFTWARE. #ifndef SERIAL_LINK_SYSTEM_H #define SERIAL_LINK_SYSTEM_H + +void init_serial_link(void); + +#if defined(PROTOCOL_CHIBIOS) +#include "ch.h" + +static inline void serial_link_lock(void) { + chSysLock(); +} + +static inline void serial_link_unlock(void) { + chSysUnlock(); +} + +void signal_data_written(void); + +#else + inline void serial_link_lock(void) { } inline void serial_link_unlock(void) { } -void singal_data_written(void); +void signal_data_written(void); -#if defined(PROTOCOL_CHIBIOS) - -inline void signal_data_written(void) { -} #endif #endif From 725929ec8bb5d57b6c4f53fa7d15e04df15f2535 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Tue, 23 Feb 2016 09:37:24 +0200 Subject: [PATCH 056/109] Fix include paths for new tmk_core update --- visualizer.mk | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/visualizer.mk b/visualizer.mk index eef2d5cc8a..13c5d31587 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -23,16 +23,16 @@ GFXLIB = $(VISUALIZER_DIR)/ugfx ifdef LCD_ENABLE include $(GFXLIB)/gfx.mk -OPT_DEFS += -DLCD_ENABLE -OPT_LIBS += -lm +UDEFS += -DLCD_ENABLE +ULIBS += -lm endif SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c -INC += $(GFXINC) $(VISUALIZER_DIR) +UINCDIR += $(GFXINC) $(VISUALIZER_DIR) ifdef LCD_BACKLIGHT_ENABLE SRC += $(VISUALIZER_DIR)/lcd_backlight.c SRC += lcd_backlight_hal.c -OPT_DEFS += -DLCD_BACKLIGHT_ENABLE +UDEFS += -DLCD_BACKLIGHT_ENABLE endif ifndef VISUALIZER_USER From 17a1102a2a73e619d1a2573764fdfbb0dec9c9a9 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 24 Feb 2016 22:31:01 +0200 Subject: [PATCH 057/109] Fix include dir to use uincir instead of just inc --- serial_link.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_link.mk b/serial_link.mk index 434966a22a..f0cd60b046 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -20,6 +20,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -INC += $(SERIAL_DIR) +UINCDIR += $(SERIAL_DIR) SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) \ No newline at end of file From 9cb139adc4e08ff393b1cb8bf16203d670996e71 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 27 Feb 2016 22:28:35 +0200 Subject: [PATCH 058/109] Add simple loop for updating the serial link --- serial_link/system/system.c | 66 ++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 2001f4d5cc..2dbd9a4c15 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -22,19 +22,83 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "serial_link/system/system.h" +#include "hal.h" +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/transport.h" +#include "serial_link/protocol/frame_router.h" +#include + +static event_source_t new_data_event; + + +// Slow speed for testing +static SerialConfig config = { + .sc_speed = 38400 +}; + +static uint32_t read_from_serial(SerialDriver* driver) { + const uint32_t buffer_size = 16; + uint8_t buffer[buffer_size]; + uint32_t bytes_read = sdAsynchronousRead(driver, buffer, buffer_size); + uint8_t* current = buffer; + uint8_t* end = current + bytes_read; + while(current < end) { + byte_stuffer_recv_byte(0, *current); + current++; + } + return bytes_read; +} // TODO: Optimize the stack size, this is probably way too big static THD_WORKING_AREA(serialThreadStack, 1024); static THD_FUNCTION(serialThread, arg) { (void)arg; + event_listener_t new_data_listener; + event_listener_t sd1_listener; + event_listener_t sd2_listener; + chEvtRegister(&new_data_event, &new_data_listener, 0); + chEvtRegisterMaskWithFlags(chnGetEventSource(&SD1), + &sd1_listener, + EVENT_MASK(1), + CHN_INPUT_AVAILABLE); + chEvtRegisterMaskWithFlags(chnGetEventSource(&SD2), + &sd2_listener, + EVENT_MASK(2), + CHN_INPUT_AVAILABLE); + bool need_wait = false; + while(true) { + if (need_wait) { + chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); + } + bool is_master = usbGetDriverStateI(&USBD1) == USB_ACTIVE; + router_set_master(is_master); + + need_wait = true; + need_wait &= read_from_serial(&SD1) == 0; + need_wait &= read_from_serial(&SD2) == 0; + update_transport(); + } +} + +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + if (link == 0) { + sdWrite(&SD1, data, size); + } + else { + sdWrite(&SD2, data, size); + } } void init_serial_link(void) { + init_byte_stuffer(); + sdStart(&SD1, &config); + sdStart(&SD2, &config); + chEvtObjectInit(&new_data_event); (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), LOWPRIO, serialThread, NULL); } void signal_data_written(void) { - + chEvtBroadcast(&new_data_event); } From 46482460fa5ba7e1606656f651117cc30f643952 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 28 Feb 2016 15:52:03 +0200 Subject: [PATCH 059/109] Some small fixes for the serial update loop --- serial_link/system/system.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 2dbd9a4c15..e40a18cec7 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -36,14 +36,14 @@ static SerialConfig config = { .sc_speed = 38400 }; -static uint32_t read_from_serial(SerialDriver* driver) { +static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { const uint32_t buffer_size = 16; uint8_t buffer[buffer_size]; uint32_t bytes_read = sdAsynchronousRead(driver, buffer, buffer_size); uint8_t* current = buffer; uint8_t* end = current + bytes_read; while(current < end) { - byte_stuffer_recv_byte(0, *current); + byte_stuffer_recv_byte(link, *current); current++; } return bytes_read; @@ -74,14 +74,14 @@ static THD_FUNCTION(serialThread, arg) { router_set_master(is_master); need_wait = true; - need_wait &= read_from_serial(&SD1) == 0; - need_wait &= read_from_serial(&SD2) == 0; + need_wait &= read_from_serial(&SD2, UP_LINK) == 0; + need_wait &= read_from_serial(&SD1, DOWN_LINK) == 0; update_transport(); } } void send_data(uint8_t link, const uint8_t* data, uint16_t size) { - if (link == 0) { + if (link == DOWN_LINK) { sdWrite(&SD1, data, size); } else { From d8d4622802c1a50779830eade0f79a0680def34a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 28 Feb 2016 21:46:29 +0200 Subject: [PATCH 060/109] Improve serial link initialization, and add driver --- serial_link.mk | 4 +- serial_link/system/driver.h | 36 +++++++++++++++ serial_link/system/system.c | 90 +++++++++++++++++++++++++++++++++++++ serial_link/system/system.h | 1 - 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 serial_link/system/driver.h diff --git a/serial_link.mk b/serial_link.mk index f0cd60b046..e164cc5ff3 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -22,4 +22,6 @@ UINCDIR += $(SERIAL_DIR) SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) \ No newline at end of file +SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) +SRC += serial_link_hal.c +OPT_DEFS += -DUSE_SERIAL_LINK diff --git a/serial_link/system/driver.h b/serial_link/system/driver.h new file mode 100644 index 0000000000..76e2d682c8 --- /dev/null +++ b/serial_link/system/driver.h @@ -0,0 +1,36 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_DRIVER_H +#define SERIAL_LINK_DRIVER_H + +#include "host_driver.h" + +void init_serial_link(void); +void init_serial_link_hal(void); +bool is_serial_link_connected(void); +host_driver_t* get_serial_link_driver(void); +void serial_link_update(void); + +#endif diff --git a/serial_link/system/system.c b/serial_link/system/system.c index e40a18cec7..c38bcd87d0 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -21,14 +21,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "report.h" +#include "host_driver.h" #include "serial_link/system/system.h" +#include "serial_link/system/driver.h" #include "hal.h" #include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/transport.h" #include "serial_link/protocol/frame_router.h" #include +#include "print.h" static event_source_t new_data_event; +static bool serial_link_connected; + +static uint8_t keyboard_leds(void); +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); + +host_driver_t serial_driver = { + keyboard_leds, + send_keyboard, + send_mouse, + send_system, + send_consumer +}; // Slow speed for testing @@ -89,8 +108,25 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { } } +static systime_t last_update = 0; + +typedef struct { + uint32_t test; +} test_object1_t; + + +SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); +MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool); + +remote_object_t* test_remote_objects[] = { + REMOTE_OBJECT(serial_link_connected), + REMOTE_OBJECT(slave_to_master), +}; void init_serial_link(void) { + serial_link_connected = false; + init_serial_link_hal(); + init_transport(test_remote_objects, sizeof(test_remote_objects)/sizeof(remote_object_t*)); init_byte_stuffer(); sdStart(&SD1, &config); sdStart(&SD2, &config); @@ -99,6 +135,60 @@ void init_serial_link(void) { LOWPRIO, serialThread, NULL); } +void serial_link_update(void) { + systime_t current_time = chVTGetSystemTimeX(); + if (current_time - last_update > 1000) { + *begin_write_serial_link_connected() = true; + end_write_serial_link_connected(); + test_object1_t* obj = begin_write_slave_to_master(); + obj->test = current_time; + end_write_slave_to_master(); + xprintf("writing %d\n", current_time); + last_update = current_time; + } + test_object1_t* obj = read_slave_to_master(0); + if (obj) { + xprintf("%d\n", obj->test); + } + obj = read_slave_to_master(1); + if (obj) { + xprintf("%d\n", obj->test); + } + + if (read_serial_link_connected()) { + serial_link_connected = true; + } +} + void signal_data_written(void) { chEvtBroadcast(&new_data_event); } + +bool is_serial_link_connected(void) { + return serial_link_connected; +} + +host_driver_t* get_serial_link_driver(void) { + return &serial_driver; +} + +uint8_t keyboard_leds(void) { + return 0; +} + +void send_keyboard(report_keyboard_t *report) { + (void)report; +} + +void send_mouse(report_mouse_t *report) { + (void)report; +} + +void send_system(uint16_t data) { + (void)data; +} + +void send_consumer(uint16_t data) { + (void)data; +} + diff --git a/serial_link/system/system.h b/serial_link/system/system.h index e8c1caec08..fcc27425e1 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -26,7 +26,6 @@ SOFTWARE. #define SERIAL_LINK_SYSTEM_H -void init_serial_link(void); #if defined(PROTOCOL_CHIBIOS) #include "ch.h" From 6fe6d111bf563962e0d3cc2e4812b6d7959836a3 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 28 Feb 2016 22:52:30 +0200 Subject: [PATCH 061/109] Send the keyboard matrix over the serial link --- serial_link/system/system.c | 53 ++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index c38bcd87d0..68ccbdb45e 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -29,6 +29,7 @@ SOFTWARE. #include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/transport.h" #include "serial_link/protocol/frame_router.h" +#include "matrix.h" #include #include "print.h" @@ -111,16 +112,17 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { static systime_t last_update = 0; typedef struct { - uint32_t test; -} test_object1_t; + matrix_row_t rows[MATRIX_ROWS]; +} matrix_object_t; +static matrix_object_t last_matrix = {}; -SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); +SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t); MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool); remote_object_t* test_remote_objects[] = { REMOTE_OBJECT(serial_link_connected), - REMOTE_OBJECT(slave_to_master), + REMOTE_OBJECT(keyboard_matrix), }; void init_serial_link(void) { @@ -140,24 +142,42 @@ void serial_link_update(void) { if (current_time - last_update > 1000) { *begin_write_serial_link_connected() = true; end_write_serial_link_connected(); - test_object1_t* obj = begin_write_slave_to_master(); - obj->test = current_time; - end_write_slave_to_master(); - xprintf("writing %d\n", current_time); last_update = current_time; } - test_object1_t* obj = read_slave_to_master(0); - if (obj) { - xprintf("%d\n", obj->test); - } - obj = read_slave_to_master(1); - if (obj) { - xprintf("%d\n", obj->test); - } if (read_serial_link_connected()) { serial_link_connected = true; } + + matrix_object_t matrix; + bool changed = false; + for(uint8_t i=0;irows[i] = matrix.rows[i]; + } + end_write_keyboard_matrix(); + } + + matrix_object_t* m = read_keyboard_matrix(0); + if (m) { + xprintf("\nr/c 01234567\n"); + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + xprintf("%X0: ", row); + for (int col = 0; col < MATRIX_COLS; col++) { + if (m->rows[row] & (1< Date: Sun, 28 Feb 2016 23:58:47 +0200 Subject: [PATCH 062/109] Call remote matrix update, when the matrix changes --- serial_link/system/system.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 68ccbdb45e..9cf695a308 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -137,6 +137,8 @@ void init_serial_link(void) { LOWPRIO, serialThread, NULL); } +void matrix_set_remote(matrix_row_t* rows, uint8_t index); + void serial_link_update(void) { systime_t current_time = chVTGetSystemTimeX(); if (current_time - last_update > 1000) { @@ -166,17 +168,7 @@ void serial_link_update(void) { matrix_object_t* m = read_keyboard_matrix(0); if (m) { - xprintf("\nr/c 01234567\n"); - for (uint8_t row = 0; row < MATRIX_ROWS; row++) { - xprintf("%X0: ", row); - for (int col = 0; col < MATRIX_COLS; col++) { - if (m->rows[row] & (1<rows, 0); } } From 11bd4ba0dd39654318bd0a3ae495656c2b2187bb Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 19:35:09 +0200 Subject: [PATCH 063/109] Remote object callable from many places Change init_transport to add_remote_objects, so that it can be called many times from different places. --- serial_link/protocol/transport.c | 14 +++++++------- serial_link/protocol/transport.h | 2 +- serial_link/system/system.c | 4 ++-- serial_link/tests/transport_tests.c | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 4542a7a050..fbcb040bf1 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -27,15 +27,15 @@ SOFTWARE. #include "serial_link/protocol/triple_buffered_object.h" #include -static remote_object_t** remote_objects; -static uint32_t num_remote_objects; +#define MAX_REMOTE_OBJECTS 16 +static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS]; +static uint32_t num_remote_objects = 0; -void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { - remote_objects = _remote_objects; - num_remote_objects = _num_remote_objects; +void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { unsigned int i; - for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; triple_buffer_init(tb); diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 9e9e22462c..e518aaa6eb 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -144,7 +144,7 @@ typedef struct { \ #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); +void add_remote_objects(remote_object_t** remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); void update_transport(void); diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 9cf695a308..9e4ee5d95e 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -120,7 +120,7 @@ static matrix_object_t last_matrix = {}; SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t); MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool); -remote_object_t* test_remote_objects[] = { +static remote_object_t* remote_objects[] = { REMOTE_OBJECT(serial_link_connected), REMOTE_OBJECT(keyboard_matrix), }; @@ -128,7 +128,7 @@ remote_object_t* test_remote_objects[] = { void init_serial_link(void) { serial_link_connected = false; init_serial_link_hal(); - init_transport(test_remote_objects, sizeof(test_remote_objects)/sizeof(remote_object_t*)); + add_remote_objects(remote_objects, sizeof(remote_objects)/sizeof(remote_object_t*)); init_byte_stuffer(); sdStart(&SD1, &config); sdStart(&SD2, &config); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 3e9bffdfa7..02a7a10425 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -53,7 +53,7 @@ MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t); MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t); SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); -remote_object_t* test_remote_objects[] = { +static remote_object_t* test_remote_objects[] = { REMOTE_OBJECT(master_to_slave), REMOTE_OBJECT(master_to_single_slave), REMOTE_OBJECT(slave_to_master), @@ -61,7 +61,7 @@ remote_object_t* test_remote_objects[] = { Describe(Transport); BeforeEach(Transport) { - init_transport(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); + add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); sent_data_size = 0; } AfterEach(Transport) {} From 32f0171d393211cc29f57a0ed29327b45e9d3747 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 19:41:34 +0200 Subject: [PATCH 064/109] Fix crash when receiving unregistered remotes --- serial_link/protocol/transport.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index fbcb040bf1..efc00e79e0 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -71,22 +71,24 @@ void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; - remote_object_t* obj = remote_objects[id]; - uint8_t* start; - if (obj->object_type == MASTER_TO_ALL_SLAVES) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + if (id < num_remote_objects) { + remote_object_t* obj = remote_objects[id]; + uint8_t* start; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); } - else if(obj->object_type == SLAVE_TO_MASTER) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); - } - else { - start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); - } - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); } void update_transport(void) { From 315edb48265e6baedd07f34c9e6e323d28814b4e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 19:42:57 +0200 Subject: [PATCH 065/109] Add serial link support for visualizer --- visualizer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/visualizer.c b/visualizer.c index 402bbd1517..ca7bcb7765 100644 --- a/visualizer.c +++ b/visualizer.c @@ -42,6 +42,11 @@ SOFTWARE. #include "nodebug.h" #endif +#ifdef USE_SERIAL_LINK +#include "serial_link/protocol/transport.h" +#include "serial_link/system/driver.h" +#endif + static visualizer_keyboard_status_t current_status = { .layer = 0xFFFFFFFF, @@ -59,6 +64,16 @@ static bool visualizer_enabled = false; #define MAX_SIMULTANEOUS_ANIMATIONS 4 static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {}; +#ifdef USE_SERIAL_LINK +MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t); + +static remote_object_t* remote_objects[] = { + REMOTE_OBJECT(current_status), +}; + +#endif + + void start_keyframe_animation(keyframe_animation_t* animation) { animation->current_frame = -1; animation->time_left_in_frame = 0; @@ -328,6 +343,10 @@ void visualizer_init(void) { #ifdef LCD_BACKLIGHT_ENABLE lcd_backlight_init(); #endif + +#ifdef USE_SERIAL_LINK + add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); +#endif // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning chEvtObjectInit(&layer_changed_event); @@ -340,17 +359,45 @@ void visualizer_set_state(uint32_t default_state, uint32_t state, uint32_t leds) // a state where one of these are set but not the other. But this should // not really matter as it will be fixed during the next loop step. // Alternatively a mutex could be used instead of the volatile variables + bool changed = false; - visualizer_keyboard_status_t new_status = { - .layer = state, - .default_layer = default_state, - .leds = leds, - }; - if (!same_status(¤t_status, &new_status)) { - changed = true; +#ifdef USE_SERIAL_LINK + if (is_serial_link_connected ()) { + visualizer_keyboard_status_t* new_status = read_current_status(); + if (new_status) { + if (!same_status(¤t_status, new_status)) { + changed = true; + current_status = *new_status; + } + } + } + else { +#else + { +#endif + visualizer_keyboard_status_t new_status = { + .layer = state, + .default_layer = default_state, + .leds = leds, + }; + if (!same_status(¤t_status, &new_status)) { + changed = true; + current_status = new_status; + } } - current_status = new_status; if (changed) { chEvtBroadcast(&layer_changed_event); + } +#ifdef USE_SERIAL_LINK + static systime_t last_update = 0; + systime_t current_update = chVTGetSystemTimeX(); + systime_t delta = current_update - last_update; + if (changed || delta > MS2ST(10)) { + last_update = current_update; + visualizer_keyboard_status_t* r = begin_write_current_status(); + *r = current_status; + end_write_current_status(); + } +#endif } From d835ad91a35afd4aa01a07a6907b846d5a32323d Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 20:08:08 +0200 Subject: [PATCH 066/109] Resend the keyboard state every ms In case there's some errors on the link, and the packet gets lost --- serial_link/system/system.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 9e4ee5d95e..efd0991e9c 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -140,13 +140,6 @@ void init_serial_link(void) { void matrix_set_remote(matrix_row_t* rows, uint8_t index); void serial_link_update(void) { - systime_t current_time = chVTGetSystemTimeX(); - if (current_time - last_update > 1000) { - *begin_write_serial_link_connected() = true; - end_write_serial_link_connected(); - last_update = current_time; - } - if (read_serial_link_connected()) { serial_link_connected = true; } @@ -157,13 +150,19 @@ void serial_link_update(void) { matrix.rows[i] = matrix_get_row(i); changed |= matrix.rows[i] != last_matrix.rows[i]; } - if (changed) { + + systime_t current_time = chVTGetSystemTimeX(); + systime_t delta = current_time - last_update; + if (changed || delta > US2ST(1000)) { + last_update = current_time; last_matrix = matrix; matrix_object_t* m = begin_write_keyboard_matrix(); for(uint8_t i=0;irows[i] = matrix.rows[i]; } end_write_keyboard_matrix(); + *begin_write_serial_link_connected() = true; + end_write_serial_link_connected(); } matrix_object_t* m = read_keyboard_matrix(0); From f7275f0edea61aa0b52160f5dbdeeddb1f0e7176 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 22:37:04 +0200 Subject: [PATCH 067/109] Configurable baud setting --- serial_link/system/system.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index efd0991e9c..5406db3f48 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -50,10 +50,12 @@ host_driver_t serial_driver = { send_consumer }; +#ifndef SERIAL_LINK_BAUD +#error "Serial link baud is not set" +#endif -// Slow speed for testing static SerialConfig config = { - .sc_speed = 38400 + .sc_speed = SERIAL_LINK_BAUD }; static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { From b93d07198a18063594a59dd193d0961622087868 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 13 Mar 2016 17:54:45 +0200 Subject: [PATCH 068/109] Suspend support for the visualizer --- example_integration/visualizer_user.c | 2 +- visualizer.c | 116 ++++++++++++++++++++------ visualizer.h | 17 +++- 3 files changed, 104 insertions(+), 31 deletions(-) diff --git a/example_integration/visualizer_user.c b/example_integration/visualizer_user.c index 6c4619d954..fc09fe2eaf 100644 --- a/example_integration/visualizer_user.c +++ b/example_integration/visualizer_user.c @@ -65,7 +65,7 @@ static keyframe_animation_t startup_animation = { .num_frames = 4, .loop = false, .frame_lengths = {0, MS2ST(1000), MS2ST(5000), 0}, - .frame_functions = {display_welcome, keyframe_animate_backlight_color, keyframe_no_operation, user_visualizer_inited}, + .frame_functions = {display_welcome, keyframe_animate_backlight_color, keyframe_no_operation, enable_visualization}, }; // The color animation animates the LCD color when you change layers diff --git a/visualizer.c b/visualizer.c index ca7bcb7765..5b0d560ed9 100644 --- a/visualizer.c +++ b/visualizer.c @@ -52,10 +52,14 @@ static visualizer_keyboard_status_t current_status = { .layer = 0xFFFFFFFF, .default_layer = 0xFFFFFFFF, .leds = 0xFFFFFFFF, + .suspended = false, }; static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) { - return memcmp(status1, status2, sizeof(visualizer_keyboard_status_t)) == 0; + return status1->layer == status2->layer && + status1->default_layer == status2->default_layer && + status1->leds == status2->leds && + status1->suspended == status2->suspended; } static event_source_t layer_changed_event; @@ -104,6 +108,17 @@ void stop_keyframe_animation(keyframe_animation_t* animation) { } } +void stop_all_keyframe_animations(void) { + for (int i=0;icurrent_frame = animations[i]->num_frames; + animations[i]->time_left_in_frame = 0; + animations[i]->need_update = true; + animations[i] = NULL; + } + } +} + static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) { dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame, animation->time_left_in_frame, delta); @@ -252,7 +267,19 @@ bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_s } #endif // LCD_ENABLE -bool user_visualizer_inited(keyframe_animation_t* animation, visualizer_state_t* state) { +bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + (void)state; + return false; +} + +bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)animation; + (void)state; + return false; +} + +bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) { (void)animation; (void)state; dprint("User visualizer inited\n"); @@ -268,13 +295,15 @@ static THD_FUNCTION(visualizerThread, arg) { event_listener_t event_listener; chEvtRegister(&layer_changed_event, &event_listener, 0); - visualizer_state_t state = { - .status = { - .default_layer = 0xFFFFFFFF, - .layer = 0xFFFFFFFF, - .leds = 0xFFFFFFFF, - }, + visualizer_keyboard_status_t initial_status = { + .default_layer = 0xFFFFFFFF, + .layer = 0xFFFFFFFF, + .leds = 0xFFFFFFFF, + .suspended = false, + }; + visualizer_state_t state = { + .status = initial_status, .current_lcd_color = 0, #ifdef LCD_ENABLE .font_fixed5x8 = gdispOpenFont("fixed_5x8"), @@ -301,17 +330,36 @@ static THD_FUNCTION(visualizerThread, arg) { bool enabled = visualizer_enabled; if (!same_status(&state.status, ¤t_status)) { if (visualizer_enabled) { - state.status = current_status; - update_user_visualizer_state(&state); - state.prev_lcd_color = state.current_lcd_color; + if (current_status.suspended) { + stop_all_keyframe_animations(); + visualizer_enabled = false; + state.status = current_status; + user_visualizer_suspend(&state); + } + else { + state.status = current_status; + update_user_visualizer_state(&state); + state.prev_lcd_color = state.current_lcd_color; + } } } + if (!enabled && state.status.suspended && current_status.suspended == false) { + // Setting the status to the initial status will force an update + // when the visualizer is enabled again + state.status = initial_status; + state.status.suspended = false; + stop_all_keyframe_animations(); + user_visualizer_resume(&state); + } sleep_time = TIME_INFINITE; for (int i=0;i MS2ST(10)) { + last_update = current_update; + visualizer_keyboard_status_t* r = begin_write_current_status(); + *r = current_status; + end_write_current_status(); + } +#endif +} + +void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds) { // Note that there's a small race condition here, the thread could read // a state where one of these are set but not the other. But this should // not really matter as it will be fixed during the next loop step. @@ -379,25 +444,22 @@ void visualizer_set_state(uint32_t default_state, uint32_t state, uint32_t leds) .layer = state, .default_layer = default_state, .leds = leds, + .suspended = current_status.suspended, }; if (!same_status(¤t_status, &new_status)) { changed = true; current_status = new_status; } } - if (changed) { - chEvtBroadcast(&layer_changed_event); - - } -#ifdef USE_SERIAL_LINK - static systime_t last_update = 0; - systime_t current_update = chVTGetSystemTimeX(); - systime_t delta = current_update - last_update; - if (changed || delta > MS2ST(10)) { - last_update = current_update; - visualizer_keyboard_status_t* r = begin_write_current_status(); - *r = current_status; - end_write_current_status(); - } -#endif + update_status(changed); +} + +void visualizer_suspend(void) { + current_status.suspended = true; + update_status(true); +} + +void visualizer_resume(void) { + current_status.suspended = false; + update_status(true); } diff --git a/visualizer.h b/visualizer.h index b7b0a3a7d0..22798cda6b 100644 --- a/visualizer.h +++ b/visualizer.h @@ -38,8 +38,12 @@ SOFTWARE. // This need to be called once at the start void visualizer_init(void); -// This should be called before every matrix scan -void visualizer_set_state(uint32_t default_state, uint32_t state, uint32_t leds); +// This should be called at every matrix scan +void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds); +// This should be called when the keyboard goes to suspend state +void visualizer_suspend(void); +// This should be called when the keyboard wakes up from suspend state +void visualizer_resume(void); // If you need support for more than 8 keyframes per animation, you can change this #define MAX_VISUALIZER_KEY_FRAMES 8 @@ -50,6 +54,7 @@ typedef struct { uint32_t layer; uint32_t default_layer; uint32_t leds; // See led.h for available statuses + bool suspended; } visualizer_keyboard_status_t; // The state struct is used by the various keyframe functions @@ -108,13 +113,19 @@ bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_st bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state); // Displays a bitmap (0/1) of all the currently active layers bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state); + +bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state); +bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state); + // Call this once, when the initial animation has finished, alternatively you can call it // directly from the initalize_user_visualizer function (the animation can be null) -bool user_visualizer_inited(keyframe_animation_t* animation, visualizer_state_t* state); +bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state); // These two functions have to be implemented by the user void initialize_user_visualizer(visualizer_state_t* state); void update_user_visualizer_state(visualizer_state_t* state); +void user_visualizer_suspend(visualizer_state_t* state); +void user_visualizer_resume(visualizer_state_t* state); #endif /* VISUALIZER_H */ From f4c11740f7bac033194ab1d5f5a52721b1d7c6d2 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 13 Mar 2016 21:35:42 +0200 Subject: [PATCH 069/109] Suspend power off, fix backlight animation The backlight color animation was using the wrong frame number, so it didn't work properly. --- visualizer.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/visualizer.c b/visualizer.c index 5b0d560ed9..867a1d3347 100644 --- a/visualizer.c +++ b/visualizer.c @@ -174,7 +174,7 @@ bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* #ifdef LCD_BACKLIGHT_ENABLE bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) { - int frame_length = animation->frame_lengths[1]; + int frame_length = animation->frame_lengths[animation->current_frame]; int current_pos = frame_length - animation->time_left_in_frame; uint8_t t_h = LCD_HUE(state->target_lcd_color); uint8_t t_s = LCD_SAT(state->target_lcd_color); @@ -270,12 +270,21 @@ bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_s bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) { (void)animation; (void)state; +#ifdef LCD_ENABLE + gdispSetPowerMode(powerOff); +#endif +#ifdef LCD_BACKLIGHT_ENABLE + lcd_backlight_hal_color(0, 0, 0); +#endif return false; } bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) { (void)animation; (void)state; +#ifdef LCD_ENABLE + gdispSetPowerMode(powerOn); +#endif return false; } @@ -339,8 +348,8 @@ static THD_FUNCTION(visualizerThread, arg) { else { state.status = current_status; update_user_visualizer_state(&state); - state.prev_lcd_color = state.current_lcd_color; } + state.prev_lcd_color = state.current_lcd_color; } } if (!enabled && state.status.suspended && current_status.suspended == false) { @@ -350,6 +359,7 @@ static THD_FUNCTION(visualizerThread, arg) { state.status.suspended = false; stop_all_keyframe_animations(); user_visualizer_resume(&state); + state.prev_lcd_color = state.current_lcd_color; } sleep_time = TIME_INFINITE; for (int i=0;i Date: Sun, 13 Mar 2016 21:38:51 +0200 Subject: [PATCH 070/109] Master mode remains active It remains active even if the USB link is suspended or otherwise temporarilly goes down. --- serial_link/system/system.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 5406db3f48..f74d8c9e9e 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -88,11 +88,13 @@ static THD_FUNCTION(serialThread, arg) { EVENT_MASK(2), CHN_INPUT_AVAILABLE); bool need_wait = false; + bool is_master = false; while(true) { if (need_wait) { chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); } - bool is_master = usbGetDriverStateI(&USBD1) == USB_ACTIVE; + // Always stay as master, even if the USB goes into sleep mode + is_master |= usbGetDriverStateI(&USBD1) == USB_ACTIVE; router_set_master(is_master); need_wait = true; From 61f7761c034d46eb0856fb115260de2679abc419 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 9 Apr 2016 19:49:52 +0300 Subject: [PATCH 071/109] Update ugfx with ChibiOS 4 support. Also change the URL to fredizzimo bitbucket --- .gitmodules | 2 +- ugfx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 2ab25f6883..b320458c0e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "ugfx"] path = ugfx - url = https://bitbucket.org/Tectu/ugfx.git + url = https://bitbucket.org/fredizzimo/ugfx.git diff --git a/ugfx b/ugfx index 2b66ac524b..e221a69061 160000 --- a/ugfx +++ b/ugfx @@ -1 +1 @@ -Subproject commit 2b66ac524bd56853ba97b917683971f3ebc0104c +Subproject commit e221a690616e20f87e0b0088baffdbd5427be862 From c95b17b536b4437f001d5f5e8a54753969e36bb2 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 13:46:19 +0300 Subject: [PATCH 072/109] Add simple led support Also the first version of a led testing animation --- led_test.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ led_test.h | 35 +++++++++++++++++++++++++++++++++++ visualizer.c | 8 ++++++++ visualizer.h | 3 +++ visualizer.mk | 10 ++++++++-- 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 led_test.c create mode 100644 led_test.h diff --git a/led_test.c b/led_test.c new file mode 100644 index 0000000000..1aadd55414 --- /dev/null +++ b/led_test.c @@ -0,0 +1,45 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#include "led_test.h" +#include "gfx.h" + +keyframe_animation_t led_test_animation = { + .num_frames = 1, + .loop = true, + .frame_lengths = {MS2ST(1000)}, + .frame_functions = { + keyframe_fade_in_all_leds, + }, +}; + +bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + int frame_length = animation->frame_lengths[animation->current_frame]; + int current_pos = frame_length - animation->time_left_in_frame; + uint8_t luma = 0x255 * current_pos / frame_length; + color_t color = LUMA2COLOR(luma); + gdispGClear(LED_DISPLAY, color); + gdispGFlush(LED_DISPLAY); + return true; +} diff --git a/led_test.h b/led_test.h new file mode 100644 index 0000000000..521e052164 --- /dev/null +++ b/led_test.h @@ -0,0 +1,35 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef TMK_VISUALIZER_LED_TEST_H_ +#define TMK_VISUALIZER_LED_TEST_H_ + +#include "visualizer.h" + +bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); + +extern keyframe_animation_t led_test_animation; + + +#endif /* TMK_VISUALIZER_LED_TEST_H_ */ diff --git a/visualizer.c b/visualizer.c index 867a1d3347..ed5c9fa2c9 100644 --- a/visualizer.c +++ b/visualizer.c @@ -77,6 +77,9 @@ static remote_object_t* remote_objects[] = { #endif +GDisplay* LCDDisplay; +GDisplay* LEDDisplay; + void start_keyframe_animation(keyframe_animation_t* animation) { animation->current_frame = -1; @@ -405,6 +408,11 @@ void visualizer_init(void) { #ifdef USE_SERIAL_LINK add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); #endif + // TODO: Make sure these works when either of these are disabled + LCDDisplay = gdispGetDisplay(0); + LEDDisplay = gdispGetDisplay(1); + + // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning chEvtObjectInit(&layer_changed_event); diff --git a/visualizer.h b/visualizer.h index 22798cda6b..6a72fde1fd 100644 --- a/visualizer.h +++ b/visualizer.h @@ -99,6 +99,9 @@ typedef struct keyframe_animation_t { } keyframe_animation_t; +extern GDisplay* LCD_DISPLAY; +extern GDisplay* LED_DISPLAY; + void start_keyframe_animation(keyframe_animation_t* animation); void stop_keyframe_animation(keyframe_animation_t* animation); diff --git a/visualizer.mk b/visualizer.mk index 13c5d31587..96ca468e86 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -21,13 +21,14 @@ # SOFTWARE. GFXLIB = $(VISUALIZER_DIR)/ugfx +SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c +UINCDIR += $(GFXINC) $(VISUALIZER_DIR) + ifdef LCD_ENABLE include $(GFXLIB)/gfx.mk UDEFS += -DLCD_ENABLE ULIBS += -lm endif -SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c -UINCDIR += $(GFXINC) $(VISUALIZER_DIR) ifdef LCD_BACKLIGHT_ENABLE SRC += $(VISUALIZER_DIR)/lcd_backlight.c @@ -35,6 +36,11 @@ SRC += lcd_backlight_hal.c UDEFS += -DLCD_BACKLIGHT_ENABLE endif +ifdef LED_ENABLE +SRC += $(VISUALIZER_DIR)/led_test.c +UDEFS += -DLED_ENABLE +endif + ifndef VISUALIZER_USER VISUALIZER_USER = visualizer_user.c endif From a960a1b0066b84bbf279fbebe2d62dfee6ea3812 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 14:10:01 +0300 Subject: [PATCH 073/109] Add fade out for the led test --- led_test.c | 24 +++++++++++++++++++----- led_test.h | 1 + 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/led_test.c b/led_test.c index 1aadd55414..1fba32fa0d 100644 --- a/led_test.c +++ b/led_test.c @@ -25,21 +25,35 @@ SOFTWARE. #include "gfx.h" keyframe_animation_t led_test_animation = { - .num_frames = 1, + .num_frames = 3, .loop = true, - .frame_lengths = {MS2ST(1000)}, + .frame_lengths = {MS2ST(1000), MS2ST(1000), MS2ST(1000)}, .frame_functions = { keyframe_fade_in_all_leds, + keyframe_no_operation, + keyframe_fade_out_all_leds, }, }; -bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) { - (void)state; +static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) { int frame_length = animation->frame_lengths[animation->current_frame]; int current_pos = frame_length - animation->time_left_in_frame; - uint8_t luma = 0x255 * current_pos / frame_length; + int delta = to - from; + int luma = (delta * current_pos) / frame_length; + luma += from; color_t color = LUMA2COLOR(luma); gdispGClear(LED_DISPLAY, color); gdispGFlush(LED_DISPLAY); +} + +bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + keyframe_fade_all_leds_from_to(animation, 0, 255); + return true; +} + +bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + keyframe_fade_all_leds_from_to(animation, 255, 0); return true; } diff --git a/led_test.h b/led_test.h index 521e052164..a722cd9fe3 100644 --- a/led_test.h +++ b/led_test.h @@ -28,6 +28,7 @@ SOFTWARE. #include "visualizer.h" bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); +bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); extern keyframe_animation_t led_test_animation; From 0e0488623e8d8820a909a48a6c847866a65bf845 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 14:54:40 +0300 Subject: [PATCH 074/109] Add left to right gradient keyframe for leds --- led_test.c | 34 ++++++++++++++++++++++++++++++++-- led_test.h | 1 + 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/led_test.c b/led_test.c index 1fba32fa0d..197550fdd6 100644 --- a/led_test.c +++ b/led_test.c @@ -23,15 +23,21 @@ SOFTWARE. */ #include "led_test.h" #include "gfx.h" +#include "math.h" keyframe_animation_t led_test_animation = { - .num_frames = 3, + .num_frames = 4, .loop = true, - .frame_lengths = {MS2ST(1000), MS2ST(1000), MS2ST(1000)}, + .frame_lengths = { + MS2ST(1000), + MS2ST(1000), + MS2ST(1000), + MS2ST(3000)}, .frame_functions = { keyframe_fade_in_all_leds, keyframe_no_operation, keyframe_fade_out_all_leds, + keyframe_led_left_to_right_gradient, }, }; @@ -46,6 +52,18 @@ static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint gdispGFlush(LED_DISPLAY); } +// TODO: Should be customizable per keyboard +#define NUM_ROWS 7 +#define NUM_COLS 7 + +static uint8_t compute_gradient_color(float t, float index, float num) { + float d = fabs(index - t); + if (d > num / 2.0f) { + d = num - d; + } + return (uint8_t)(255.0f * d); +} + bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) { (void)state; keyframe_fade_all_leds_from_to(animation, 0, 255); @@ -57,3 +75,15 @@ bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_stat keyframe_fade_all_leds_from_to(animation, 255, 0); return true; } + +bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + int frame_length = animation->frame_lengths[animation->current_frame]; + int current_pos = frame_length - animation->time_left_in_frame; + float t = current_pos / frame_length; + for (int i=0; i< NUM_COLS; i++) { + uint8_t color = compute_gradient_color(t, i, NUM_COLS); + gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color)); + } + return true; +} diff --git a/led_test.h b/led_test.h index a722cd9fe3..03737a717d 100644 --- a/led_test.h +++ b/led_test.h @@ -29,6 +29,7 @@ SOFTWARE. bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); +bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state); extern keyframe_animation_t led_test_animation; From 444132edd056cd52a60e3551d1f6c1a07909c040 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 15:45:52 +0300 Subject: [PATCH 075/109] Add last and first update of frame for anims --- visualizer.c | 9 +++++++++ visualizer.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/visualizer.c b/visualizer.c index ed5c9fa2c9..219d44cd3c 100644 --- a/visualizer.c +++ b/visualizer.c @@ -103,6 +103,8 @@ void stop_keyframe_animation(keyframe_animation_t* animation) { animation->current_frame = animation->num_frames; animation->time_left_in_frame = 0; animation->need_update = true; + animation->first_update_of_frame = false; + animation->last_update_of_frame = false; for (int i=0;icurrent_frame = animations[i]->num_frames; animations[i]->time_left_in_frame = 0; animations[i]->need_update = true; + animations[i]->first_update_of_frame = false; + animations[i]->last_update_of_frame = false; animations[i] = NULL; } } @@ -133,16 +137,20 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize animation->current_frame = 0; animation->time_left_in_frame = animation->frame_lengths[0]; animation->need_update = true; + animation->first_update_of_frame = true; } else { animation->time_left_in_frame -= delta; while (animation->time_left_in_frame <= 0) { int left = animation->time_left_in_frame; if (animation->need_update) { animation->time_left_in_frame = 0; + animation->last_update_of_frame = true; (*animation->frame_functions[animation->current_frame])(animation, state); + animation->last_update_of_frame = false; } animation->current_frame++; animation->need_update = true; + animation->first_update_of_frame = true; if (animation->current_frame == animation->num_frames) { if (animation->loop) { animation->current_frame = 0; @@ -159,6 +167,7 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize } if (animation->need_update) { animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state); + animation->first_update_of_frame = false; } int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame; diff --git a/visualizer.h b/visualizer.h index 6a72fde1fd..82d7a71d37 100644 --- a/visualizer.h +++ b/visualizer.h @@ -95,6 +95,8 @@ typedef struct keyframe_animation_t { // keyframe update functions int current_frame; int time_left_in_frame; + bool first_update_of_frame; + bool last_update_of_frame; bool need_update; } keyframe_animation_t; From 891edbd533acdffec66416c59f4b6066e5e18aaa Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 16:19:31 +0300 Subject: [PATCH 076/109] Add function for running the next keyframe --- visualizer.c | 15 +++++++++++++++ visualizer.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/visualizer.c b/visualizer.c index 219d44cd3c..2ec6e34f58 100644 --- a/visualizer.c +++ b/visualizer.c @@ -178,6 +178,21 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize return true; } +void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) { + int next_frame = animation->current_frame + 1; + if (next_frame == animation->num_frames) { + next_frame = 0; + } + keyframe_animation_t temp_animation = *animation; + temp_animation.current_frame = next_frame; + temp_animation.time_left_in_frame = animation->frame_lengths[next_frame]; + temp_animation.first_update_of_frame = true; + temp_animation.last_update_of_frame = false; + temp_animation.need_update = false; + visualizer_state_t temp_state = *state; + (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state); +} + bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) { (void)animation; (void)state; diff --git a/visualizer.h b/visualizer.h index 82d7a71d37..5375e0130f 100644 --- a/visualizer.h +++ b/visualizer.h @@ -106,6 +106,9 @@ extern GDisplay* LED_DISPLAY; void start_keyframe_animation(keyframe_animation_t* animation); void stop_keyframe_animation(keyframe_animation_t* animation); +// This runs the next keyframe, but does not update the animation state +// Useful for crossfades for example +void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state); // Some predefined keyframe functions that can be used by the user code // Does nothing, useful for adding delays From 0530ebb77d6961a7edc14f3a5b943165a8b52497 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 16:20:00 +0300 Subject: [PATCH 077/109] Add led crossfading --- led_test.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++----- led_test.h | 2 ++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/led_test.c b/led_test.c index 197550fdd6..8c0de604a2 100644 --- a/led_test.c +++ b/led_test.c @@ -26,27 +26,41 @@ SOFTWARE. #include "math.h" keyframe_animation_t led_test_animation = { - .num_frames = 4, + .num_frames = 8, .loop = true, .frame_lengths = { - MS2ST(1000), - MS2ST(1000), - MS2ST(1000), - MS2ST(3000)}, + MS2ST(1000), // fade in + MS2ST(1000), // no op (leds on) + MS2ST(1000), // fade out + MS2ST(1000), // crossfade + MS2ST(3000), // left to rigt + MS2ST(1000), // crossfade + MS2ST(3000), // top_to_bottom + MS2ST(1000), // crossfade + }, .frame_functions = { keyframe_fade_in_all_leds, keyframe_no_operation, keyframe_fade_out_all_leds, + keyframe_led_crossfade, keyframe_led_left_to_right_gradient, + keyframe_led_crossfade, + keyframe_led_top_to_bottom_gradient, + keyframe_led_crossfade }, }; -static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) { +static uint8_t fade_led_color(keyframe_animation_t* animation, uint8_t from, uint8_t to) { int frame_length = animation->frame_lengths[animation->current_frame]; int current_pos = frame_length - animation->time_left_in_frame; int delta = to - from; int luma = (delta * current_pos) / frame_length; luma += from; + return luma; +} + +static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) { + uint8_t luma = fade_led_color(animation, from, to); color_t color = LUMA2COLOR(luma); gdispGClear(LED_DISPLAY, color); gdispGFlush(LED_DISPLAY); @@ -56,6 +70,9 @@ static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint #define NUM_ROWS 7 #define NUM_COLS 7 +static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS]; +static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS]; + static uint8_t compute_gradient_color(float t, float index, float num) { float d = fabs(index - t); if (d > num / 2.0f) { @@ -85,5 +102,44 @@ bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visual uint8_t color = compute_gradient_color(t, i, NUM_COLS); gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color)); } + gdispGFlush(LED_DISPLAY); + return true; +} + +bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + int frame_length = animation->frame_lengths[animation->current_frame]; + int current_pos = frame_length - animation->time_left_in_frame; + float t = current_pos / frame_length; + for (int i=0; i< NUM_ROWS; i++) { + uint8_t color = compute_gradient_color(t, i, NUM_ROWS); + gdispGDrawLine(LED_DISPLAY, 0, i, NUM_COLS - 1, i, LUMA2COLOR(color)); + } + gdispGFlush(LED_DISPLAY); + return true; +} + +static void copy_current_led_state(uint8_t* dest) { + for (int i=0;ifirst_update_of_frame) { + copy_current_led_state(&crossfade_start_frame[0][0]); + run_next_keyframe(animation, state); + copy_current_led_state(&crossfade_end_frame[0][0]); + } + for (int i=0;i Date: Sun, 24 Apr 2016 16:26:53 +0300 Subject: [PATCH 078/109] LEDS are flushed automatically After running the animation, instead of having to do it manually. This avoids duplicate flushing, and better support for cross-fades. --- led_test.c | 4 ---- visualizer.c | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/led_test.c b/led_test.c index 8c0de604a2..c987eca380 100644 --- a/led_test.c +++ b/led_test.c @@ -63,7 +63,6 @@ static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint uint8_t luma = fade_led_color(animation, from, to); color_t color = LUMA2COLOR(luma); gdispGClear(LED_DISPLAY, color); - gdispGFlush(LED_DISPLAY); } // TODO: Should be customizable per keyboard @@ -102,7 +101,6 @@ bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visual uint8_t color = compute_gradient_color(t, i, NUM_COLS); gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color)); } - gdispGFlush(LED_DISPLAY); return true; } @@ -115,7 +113,6 @@ bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visual uint8_t color = compute_gradient_color(t, i, NUM_ROWS); gdispGDrawLine(LED_DISPLAY, 0, i, NUM_COLS - 1, i, LUMA2COLOR(color)); } - gdispGFlush(LED_DISPLAY); return true; } @@ -140,6 +137,5 @@ bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* gdispGDrawPixel(LED_DISPLAY, j, i, color); } } - gdispGFlush(LED_DISPLAY); return true; } diff --git a/visualizer.c b/visualizer.c index 2ec6e34f58..81ec8298fd 100644 --- a/visualizer.c +++ b/visualizer.c @@ -127,6 +127,7 @@ void stop_all_keyframe_animations(void) { } static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) { + // TODO: Clean up this messy code dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame, animation->time_left_in_frame, delta); if (animation->current_frame == animation->num_frames) { @@ -394,6 +395,9 @@ static THD_FUNCTION(visualizerThread, arg) { update_keyframe_animation(animations[i], &state, delta, &sleep_time); } } +#ifdef LED_ENABLE + gdispGFlush(LED_DISPLAY); +#endif // The animation can enable the visualizer // And we might need to update the state when that happens // so don't sleep From 74baa4895c8efd409eb10eaf1e6cfc0e2677b45c Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 16:40:41 +0300 Subject: [PATCH 079/109] Run mirrored keyframes for led tests as well --- led_test.c | 34 ++++++++++++++++++++++++++++++---- led_test.h | 2 ++ visualizer.h | 4 ++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/led_test.c b/led_test.c index c987eca380..ce6c2e68ec 100644 --- a/led_test.c +++ b/led_test.c @@ -26,17 +26,24 @@ SOFTWARE. #include "math.h" keyframe_animation_t led_test_animation = { - .num_frames = 8, + .num_frames = 14, .loop = true, .frame_lengths = { MS2ST(1000), // fade in MS2ST(1000), // no op (leds on) MS2ST(1000), // fade out MS2ST(1000), // crossfade - MS2ST(3000), // left to rigt + MS2ST(3000), // left to rigt (outside in) MS2ST(1000), // crossfade MS2ST(3000), // top_to_bottom + 0, // mirror leds MS2ST(1000), // crossfade + MS2ST(3000), // left_to_right (mirrored, so inside out) + MS2ST(1000), // crossfade + MS2ST(3000), // top_to_bottom + 0, // normal leds + MS2ST(1000), // crossfade + }, .frame_functions = { keyframe_fade_in_all_leds, @@ -46,7 +53,13 @@ keyframe_animation_t led_test_animation = { keyframe_led_left_to_right_gradient, keyframe_led_crossfade, keyframe_led_top_to_bottom_gradient, - keyframe_led_crossfade + keyframe_mirror_led_orientation, + keyframe_led_crossfade, + keyframe_led_left_to_right_gradient, + keyframe_led_crossfade, + keyframe_led_top_to_bottom_gradient, + keyframe_normal_led_orientation, + keyframe_led_crossfade, }, }; @@ -123,7 +136,6 @@ static void copy_current_led_state(uint8_t* dest) { } } } - bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state) { (void)state; if (animation->first_update_of_frame) { @@ -139,3 +151,17 @@ bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* } return true; } + +bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + (void)animation; + gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_180); + return true; +} + +bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) { + (void)state; + (void)animation; + gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_0); + return true; +} diff --git a/led_test.h b/led_test.h index e14b25e796..5e2325753e 100644 --- a/led_test.h +++ b/led_test.h @@ -32,6 +32,8 @@ bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_stat bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state); bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state); bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state); +bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state); +bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state); extern keyframe_animation_t led_test_animation; diff --git a/visualizer.h b/visualizer.h index 5375e0130f..8a2772c6da 100644 --- a/visualizer.h +++ b/visualizer.h @@ -45,8 +45,8 @@ void visualizer_suspend(void); // This should be called when the keyboard wakes up from suspend state void visualizer_resume(void); -// If you need support for more than 8 keyframes per animation, you can change this -#define MAX_VISUALIZER_KEY_FRAMES 8 +// If you need support for more than 16 keyframes per animation, you can change this +#define MAX_VISUALIZER_KEY_FRAMES 16 struct keyframe_animation_t; From 6313cfd5597ffce0c70a472aa87ed68b6daa4419 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 24 Apr 2016 17:44:11 +0300 Subject: [PATCH 080/109] Fix some makefile issues, and symbol issues --- visualizer.c | 8 ++++---- visualizer.mk | 10 ++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/visualizer.c b/visualizer.c index 81ec8298fd..0684a26d8f 100644 --- a/visualizer.c +++ b/visualizer.c @@ -77,8 +77,8 @@ static remote_object_t* remote_objects[] = { #endif -GDisplay* LCDDisplay; -GDisplay* LEDDisplay; +GDisplay* LCD_DISPLAY = 0; +GDisplay* LED_DISPLAY = 0; void start_keyframe_animation(keyframe_animation_t* animation) { @@ -437,8 +437,8 @@ void visualizer_init(void) { add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); #endif // TODO: Make sure these works when either of these are disabled - LCDDisplay = gdispGetDisplay(0); - LEDDisplay = gdispGetDisplay(1); + LCD_DISPLAY = gdispGetDisplay(0); + LED_DISPLAY = gdispGetDisplay(1); // We are using a low priority thread, the idea is to have it run only diff --git a/visualizer.mk b/visualizer.mk index 96ca468e86..5cc199cf48 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -21,13 +21,13 @@ # SOFTWARE. GFXLIB = $(VISUALIZER_DIR)/ugfx -SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c +SRC += $(VISUALIZER_DIR)/visualizer.c UINCDIR += $(GFXINC) $(VISUALIZER_DIR) ifdef LCD_ENABLE -include $(GFXLIB)/gfx.mk UDEFS += -DLCD_ENABLE ULIBS += -lm +USE_UGFX = yes endif ifdef LCD_BACKLIGHT_ENABLE @@ -39,6 +39,12 @@ endif ifdef LED_ENABLE SRC += $(VISUALIZER_DIR)/led_test.c UDEFS += -DLED_ENABLE +USE_UGFX = yes +endif + +ifdef USE_UGFX +include $(GFXLIB)/gfx.mk +SRC += $(GFXSRC) endif ifndef VISUALIZER_USER From f0c8e7c495af2aad476f4c428b700ed26188a8e5 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 30 Apr 2016 22:45:41 +0300 Subject: [PATCH 081/109] Fix a few led test animation issues --- led_test.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/led_test.c b/led_test.c index ce6c2e68ec..ed65a0baa5 100644 --- a/led_test.c +++ b/led_test.c @@ -63,7 +63,7 @@ keyframe_animation_t led_test_animation = { }, }; -static uint8_t fade_led_color(keyframe_animation_t* animation, uint8_t from, uint8_t to) { +static uint8_t fade_led_color(keyframe_animation_t* animation, int from, int to) { int frame_length = animation->frame_lengths[animation->current_frame]; int current_pos = frame_length - animation->time_left_in_frame; int delta = to - from; @@ -107,8 +107,8 @@ bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_stat bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state) { (void)state; - int frame_length = animation->frame_lengths[animation->current_frame]; - int current_pos = frame_length - animation->time_left_in_frame; + float frame_length = animation->frame_lengths[animation->current_frame]; + float current_pos = frame_length - animation->time_left_in_frame; float t = current_pos / frame_length; for (int i=0; i< NUM_COLS; i++) { uint8_t color = compute_gradient_color(t, i, NUM_COLS); @@ -119,8 +119,8 @@ bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visual bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state) { (void)state; - int frame_length = animation->frame_lengths[animation->current_frame]; - int current_pos = frame_length - animation->time_left_in_frame; + float frame_length = animation->frame_lengths[animation->current_frame]; + float current_pos = frame_length - animation->time_left_in_frame; float t = current_pos / frame_length; for (int i=0; i< NUM_ROWS; i++) { uint8_t color = compute_gradient_color(t, i, NUM_ROWS); @@ -156,12 +156,12 @@ bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer (void)state; (void)animation; gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_180); - return true; + return false; } bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) { (void)state; (void)animation; gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_0); - return true; + return false; } From 25382cb6f21b6136b0f490a618ce8d494ca5cd38 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 11 May 2016 22:06:26 +0300 Subject: [PATCH 082/109] Fix compute_gradient_color --- led_test.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/led_test.c b/led_test.c index ed65a0baa5..d358ef81eb 100644 --- a/led_test.c +++ b/led_test.c @@ -86,10 +86,13 @@ static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS]; static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS]; static uint8_t compute_gradient_color(float t, float index, float num) { - float d = fabs(index - t); - if (d > num / 2.0f) { + const float target = t * (num - 1.0f); + const float half_num = num / 2.0f; + float d = fabs(index - target); + if (d > half_num) { d = num - d; } + d = 1.0f - (d / half_num); return (uint8_t)(255.0f * d); } From 3b422d2ac4340ecea6b2fc2f3a855581c737faf8 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 11:36:39 +0300 Subject: [PATCH 083/109] Add debug print for serial link errors This is disabled by default --- serial_link/system/system.c | 52 ++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index f74d8c9e9e..5a47d8b83c 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -58,6 +58,8 @@ static SerialConfig config = { .sc_speed = SERIAL_LINK_BAUD }; +//#define DEBUG_LINK_ERRORS + static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { const uint32_t buffer_size = 16; uint8_t buffer[buffer_size]; @@ -71,6 +73,37 @@ static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { return bytes_read; } +static void print_error(char* str, eventflags_t flags, SerialDriver* driver) { +#ifdef DEBUG_LINK_ERRORS + if (flags & SD_PARITY_ERROR) { + print(str); + print(" Parity error\n"); + } + if (flags & SD_FRAMING_ERROR) { + print(str); + print(" Framing error\n"); + } + if (flags & SD_OVERRUN_ERROR) { + print(str); + uint32_t size = qSpaceI(&(driver->iqueue)); + xprintf(" Overrun error, queue size %d\n", size); + + } + if (flags & SD_NOISE_ERROR) { + print(str); + print(" Noise error\n"); + } + if (flags & SD_BREAK_DETECTED) { + print(str); + print(" Break detected\n"); + } +#else + (void)str; + (void)flags; + (void)driver; +#endif +} + // TODO: Optimize the stack size, this is probably way too big static THD_WORKING_AREA(serialThreadStack, 1024); static THD_FUNCTION(serialThread, arg) { @@ -79,20 +112,33 @@ static THD_FUNCTION(serialThread, arg) { event_listener_t sd1_listener; event_listener_t sd2_listener; chEvtRegister(&new_data_event, &new_data_listener, 0); + eventflags_t events = CHN_INPUT_AVAILABLE + | SD_PARITY_ERROR | SD_FRAMING_ERROR | SD_OVERRUN_ERROR | SD_NOISE_ERROR | SD_BREAK_DETECTED; chEvtRegisterMaskWithFlags(chnGetEventSource(&SD1), &sd1_listener, EVENT_MASK(1), - CHN_INPUT_AVAILABLE); + events); chEvtRegisterMaskWithFlags(chnGetEventSource(&SD2), &sd2_listener, EVENT_MASK(2), - CHN_INPUT_AVAILABLE); + events); bool need_wait = false; bool is_master = false; while(true) { + eventflags_t flags1 = 0; + eventflags_t flags2 = 0; if (need_wait) { - chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); + eventmask_t mask = chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); + if (mask & EVENT_MASK(1)) { + flags1 = chEvtGetAndClearFlags(&sd1_listener); + print_error("DOWNLINK", flags1, &SD1); + } + if (mask & EVENT_MASK(2)) { + flags2 = chEvtGetAndClearFlags(&sd2_listener); + print_error("UPLINK", flags2, &SD2); + } } + // Always stay as master, even if the USB goes into sleep mode is_master |= usbGetDriverStateI(&USBD1) == USB_ACTIVE; router_set_master(is_master); From a08bcea9983cc97fb2f567c303622495f19a5a1e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 11:58:20 +0300 Subject: [PATCH 084/109] Don't accept remote objects with the wrong size Fixes memory corruption when the crc happens to match, but the size doesn't. --- serial_link/protocol/transport.c | 30 ++++++++++---------- serial_link/tests/transport_tests.c | 43 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index efc00e79e0..f418d11ceb 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -73,21 +73,23 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; if (id < num_remote_objects) { remote_object_t* obj = remote_objects[id]; - uint8_t* start; - if (obj->object_type == MASTER_TO_ALL_SLAVES) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + if (obj->object_size == size - 1) { + uint8_t* start; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size - 1); + triple_buffer_end_write_internal(tb); } - else if(obj->object_type == SLAVE_TO_MASTER) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); - } - else { - start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); - } - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); } } diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 02a7a10425..358e1b9fd4 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -123,3 +123,46 @@ Ensure(Transport, writes_from_master_to_single_slave) { assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2->test, is_equal_to(7)); } + +Ensure(Transport, ignores_object_with_invalid_id) { + update_transport(); + test_object1_t* obj = begin_write_master_to_single_slave(3); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_single_slave(3); + expect(router_send_frame, + when(destination, is_equal_to(4))); + update_transport(); + sent_data[sent_data_size - 1] = 44; + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_single_slave(); + assert_that(obj2, is_equal_to(NULL)); +} + +Ensure(Transport, ignores_object_with_size_too_small) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame); + update_transport(); + sent_data[sent_data_size - 2] = 0; + transport_recv_frame(0, sent_data, sent_data_size - 1); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_equal_to(NULL)); +} + +Ensure(Transport, ignores_object_with_size_too_big) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame); + update_transport(); + sent_data[sent_data_size + 21] = 0; + transport_recv_frame(0, sent_data, sent_data_size + 22); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_equal_to(NULL)); +} From b4ab61171e83eeca82fbc1930fcaab175b45c656 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 12:59:50 +0300 Subject: [PATCH 085/109] Configurable serializer thread priority --- serial_link/system/system.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 5a47d8b83c..f5af9baa74 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -32,6 +32,7 @@ SOFTWARE. #include "matrix.h" #include #include "print.h" +#include "config.h" static event_source_t new_data_event; static bool serial_link_connected; @@ -50,10 +51,15 @@ host_driver_t serial_driver = { send_consumer }; +// Define these in your Config.h file #ifndef SERIAL_LINK_BAUD #error "Serial link baud is not set" #endif +#ifndef SERIAL_LINK_THREAD_PRIORITY +#error "Serial link thread priority not set" +#endif + static SerialConfig config = { .sc_speed = SERIAL_LINK_BAUD }; @@ -184,7 +190,7 @@ void init_serial_link(void) { sdStart(&SD2, &config); chEvtObjectInit(&new_data_event); (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), - LOWPRIO, serialThread, NULL); + SERIAL_LINK_THREAD_PRIORITY, serialThread, NULL); } void matrix_set_remote(matrix_row_t* rows, uint8_t index); From a0297a892ee1fc571cd9bdc447d2c2726a210629 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 13:38:59 +0300 Subject: [PATCH 086/109] Rename system to serial_link Also combined driver.h with serial_link.h --- serial_link/protocol/transport.h | 2 +- serial_link/protocol/triple_buffered_object.c | 2 +- serial_link/system/driver.h | 36 ------------------- .../system/{system.c => serial_link.c} | 3 +- .../system/{system.h => serial_link.h} | 11 ++++-- 5 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 serial_link/system/driver.h rename serial_link/system/{system.c => serial_link.c} (98%) rename serial_link/system/{system.h => serial_link.h} (84%) diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index e518aaa6eb..9a052d8809 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -26,7 +26,7 @@ SOFTWARE. #define SERIAL_LINK_TRANSPORT_H #include "serial_link/protocol/triple_buffered_object.h" -#include "serial_link/system/system.h" +#include "serial_link/system/serial_link.h" #define NUM_SLAVES 8 #define LOCAL_OBJECT_EXTRA 16 diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index c6bf28af0a..e3e8989d30 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -23,7 +23,7 @@ SOFTWARE. */ #include "serial_link/protocol/triple_buffered_object.h" -#include "serial_link/system/system.h" +#include "serial_link/system/serial_link.h" #include #include diff --git a/serial_link/system/driver.h b/serial_link/system/driver.h deleted file mode 100644 index 76e2d682c8..0000000000 --- a/serial_link/system/driver.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -The MIT License (MIT) - -Copyright (c) 2016 Fred Sundvik - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#ifndef SERIAL_LINK_DRIVER_H -#define SERIAL_LINK_DRIVER_H - -#include "host_driver.h" - -void init_serial_link(void); -void init_serial_link_hal(void); -bool is_serial_link_connected(void); -host_driver_t* get_serial_link_driver(void); -void serial_link_update(void); - -#endif diff --git a/serial_link/system/system.c b/serial_link/system/serial_link.c similarity index 98% rename from serial_link/system/system.c rename to serial_link/system/serial_link.c index f5af9baa74..27e61a9e97 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/serial_link.c @@ -23,8 +23,7 @@ SOFTWARE. */ #include "report.h" #include "host_driver.h" -#include "serial_link/system/system.h" -#include "serial_link/system/driver.h" +#include "serial_link/system/serial_link.h" #include "hal.h" #include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/transport.h" diff --git a/serial_link/system/system.h b/serial_link/system/serial_link.h similarity index 84% rename from serial_link/system/system.h rename to serial_link/system/serial_link.h index fcc27425e1..132cb657ff 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/serial_link.h @@ -22,10 +22,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef SERIAL_LINK_SYSTEM_H -#define SERIAL_LINK_SYSTEM_H +#ifndef SERIAL_LINK_H +#define SERIAL_LINK_H +#include "host_driver.h" +#include +void init_serial_link(void); +void init_serial_link_hal(void); +bool is_serial_link_connected(void); +host_driver_t* get_serial_link_driver(void); +void serial_link_update(void); #if defined(PROTOCOL_CHIBIOS) #include "ch.h" From 5651be7a2f50857b1ddd120a4b0184a28cdca4aa Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 13:00:27 +0300 Subject: [PATCH 087/109] Configurable visualizer thread priority --- visualizer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/visualizer.c b/visualizer.c index 867a1d3347..4c47114990 100644 --- a/visualizer.c +++ b/visualizer.c @@ -24,6 +24,7 @@ SOFTWARE. #include "visualizer.h" #include "ch.h" +#include "config.h" #include #ifdef LCD_ENABLE @@ -47,6 +48,11 @@ SOFTWARE. #include "serial_link/system/driver.h" #endif +// Define this in config.h +#ifndef VISUALIZER_THREAD_PRIORITY +#define "Visualizer thread priority not defined" +#endif + static visualizer_keyboard_status_t current_status = { .layer = 0xFFFFFFFF, @@ -409,7 +415,7 @@ void visualizer_init(void) { // when the main thread is sleeping during the matrix scanning chEvtObjectInit(&layer_changed_event); (void)chThdCreateStatic(visualizerThreadStack, sizeof(visualizerThreadStack), - LOWPRIO, visualizerThread, NULL); + VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL); } void update_status(bool changed) { From 81f89cc6725d4e513f85ed1c841d25d0ac64bfe3 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 13:39:58 +0300 Subject: [PATCH 088/109] Update include dir for new serial_link include --- visualizer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/visualizer.c b/visualizer.c index 4c47114990..605be30594 100644 --- a/visualizer.c +++ b/visualizer.c @@ -45,7 +45,7 @@ SOFTWARE. #ifdef USE_SERIAL_LINK #include "serial_link/protocol/transport.h" -#include "serial_link/system/driver.h" +#include "serial_link/system/serial_link.h" #endif // Define this in config.h From a20d513e3cdacbf6e0e70a80402497ad10166434 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 19:28:15 +0300 Subject: [PATCH 089/109] Add function to check if the keyboard is a master --- serial_link/system/serial_link.c | 6 +++++- serial_link/system/serial_link.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/serial_link/system/serial_link.c b/serial_link/system/serial_link.c index 27e61a9e97..75c7e77a76 100644 --- a/serial_link/system/serial_link.c +++ b/serial_link/system/serial_link.c @@ -35,6 +35,7 @@ SOFTWARE. static event_source_t new_data_event; static bool serial_link_connected; +static bool is_master = false; static uint8_t keyboard_leds(void); static void send_keyboard(report_keyboard_t *report); @@ -109,6 +110,10 @@ static void print_error(char* str, eventflags_t flags, SerialDriver* driver) { #endif } +bool is_serial_link_master(void) { + return is_master; +} + // TODO: Optimize the stack size, this is probably way too big static THD_WORKING_AREA(serialThreadStack, 1024); static THD_FUNCTION(serialThread, arg) { @@ -128,7 +133,6 @@ static THD_FUNCTION(serialThread, arg) { EVENT_MASK(2), events); bool need_wait = false; - bool is_master = false; while(true) { eventflags_t flags1 = 0; eventflags_t flags2 = 0; diff --git a/serial_link/system/serial_link.h b/serial_link/system/serial_link.h index 132cb657ff..351e03877b 100644 --- a/serial_link/system/serial_link.h +++ b/serial_link/system/serial_link.h @@ -31,6 +31,7 @@ SOFTWARE. void init_serial_link(void); void init_serial_link_hal(void); bool is_serial_link_connected(void); +bool is_serial_link_master(void); host_driver_t* get_serial_link_driver(void); void serial_link_update(void); From 15bdef3ee92e6809ec5d0f25901f4a490cd91b58 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Mon, 16 May 2016 09:45:39 +0300 Subject: [PATCH 090/109] Makefile changes to support emulator build --- ugfx | 2 +- visualizer.mk | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ugfx b/ugfx index e221a69061..314a066d11 160000 --- a/ugfx +++ b/ugfx @@ -1 +1 @@ -Subproject commit e221a690616e20f87e0b0088baffdbd5427be862 +Subproject commit 314a066d11f09d295d42054a0b53fa1a95c0ba0a diff --git a/visualizer.mk b/visualizer.mk index 5cc199cf48..c51f8ba5d4 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -21,7 +21,9 @@ # SOFTWARE. GFXLIB = $(VISUALIZER_DIR)/ugfx +ifndef EMULATOR SRC += $(VISUALIZER_DIR)/visualizer.c +endif UINCDIR += $(GFXINC) $(VISUALIZER_DIR) ifdef LCD_ENABLE @@ -45,9 +47,13 @@ endif ifdef USE_UGFX include $(GFXLIB)/gfx.mk SRC += $(GFXSRC) +UDEFS += $(patsubst %,-D%,$(patsubst -D%,%,$(GFXDEFS))) +ULIBS += $(patsubst %,-l%,$(patsubst -l%,%,$(GFXLIBS))) endif ifndef VISUALIZER_USER +ifndef EMULATOR VISUALIZER_USER = visualizer_user.c endif +endif SRC += $(VISUALIZER_USER) \ No newline at end of file From d79e94adb1182ae867df0cc7621ef3d44d213bbc Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Tue, 17 May 2016 09:35:02 +0300 Subject: [PATCH 091/109] Use ugfx API instead of chibios --- ugfx | 2 +- visualizer.c | 34 ++++++++++++++++++---------------- visualizer.mk | 12 +++++++++--- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/ugfx b/ugfx index 314a066d11..7d7eeef0ad 160000 --- a/ugfx +++ b/ugfx @@ -1 +1 @@ -Subproject commit 314a066d11f09d295d42054a0b53fa1a95c0ba0a +Subproject commit 7d7eeef0ad0f1b28f4fb86ad931cb6774c7b9e81 diff --git a/visualizer.c b/visualizer.c index 579837edcd..ea84546fb6 100644 --- a/visualizer.c +++ b/visualizer.c @@ -23,7 +23,6 @@ SOFTWARE. */ #include "visualizer.h" -#include "ch.h" #include "config.h" #include @@ -68,7 +67,7 @@ static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboa status1->suspended == status2->suspended; } -static event_source_t layer_changed_event; +static GSourceHandle layer_changed_event; static bool visualizer_enabled = false; #define MAX_SIMULTANEOUS_ANIMATIONS 4 @@ -132,7 +131,7 @@ void stop_all_keyframe_animations(void) { } } -static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) { +static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) { // TODO: Clean up this messy code dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame, animation->time_left_in_frame, delta); @@ -331,12 +330,13 @@ bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* s } // TODO: Optimize the stack size, this is probably way too big -static THD_WORKING_AREA(visualizerThreadStack, 1024); -static THD_FUNCTION(visualizerThread, arg) { +static DECLARE_THREAD_STACK(visualizerThreadStack, 1024); +static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { (void)arg; - event_listener_t event_listener; - chEvtRegister(&layer_changed_event, &event_listener, 0); + GListener event_listener; + geventListenerInit(&event_listener); + geventAttachSource(&event_listener, layer_changed_event, 0); visualizer_keyboard_status_t initial_status = { .default_layer = 0xFFFFFFFF, @@ -363,12 +363,12 @@ static THD_FUNCTION(visualizerThread, arg) { LCD_INT(state.current_lcd_color)); #endif - systime_t sleep_time = TIME_INFINITE; - systime_t current_time = chVTGetSystemTimeX(); + systemticks_t sleep_time = TIME_INFINITE; + systemticks_t current_time = gfxSystemTicks(); while(true) { - systime_t new_time = chVTGetSystemTimeX(); - systime_t delta = new_time - current_time; + systemticks_t new_time = gfxSystemTicks(); + systemticks_t delta = new_time - current_time; current_time = new_time; bool enabled = visualizer_enabled; if (!same_status(&state.status, ¤t_status)) { @@ -411,7 +411,7 @@ static THD_FUNCTION(visualizerThread, arg) { sleep_time = 0; } - systime_t after_update = chVTGetSystemTimeX(); + systemticks_t after_update = gfxSystemTicks(); unsigned update_delta = after_update - current_time; if (sleep_time != TIME_INFINITE) { if (sleep_time > update_delta) { @@ -422,12 +422,14 @@ static THD_FUNCTION(visualizerThread, arg) { } } dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time); - chEvtWaitOneTimeout(EVENT_MASK(0), sleep_time); + geventEventWait(&event_listener, sleep_time); } #ifdef LCD_ENABLE gdispCloseFont(state.font_fixed5x8); gdispCloseFont(state.font_dejavusansbold12); #endif + + return 0; } void visualizer_init(void) { @@ -449,14 +451,14 @@ void visualizer_init(void) { // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning - chEvtObjectInit(&layer_changed_event); - (void)chThdCreateStatic(visualizerThreadStack, sizeof(visualizerThreadStack), + gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack), VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL); } void update_status(bool changed) { if (changed) { - chEvtBroadcast(&layer_changed_event); + GSourceListener* listener = geventGetSourceListener(layer_changed_event, NULL); + geventSendEvent(listener); } #ifdef USE_SERIAL_LINK static systime_t last_update = 0; diff --git a/visualizer.mk b/visualizer.mk index c51f8ba5d4..6788293290 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -21,9 +21,7 @@ # SOFTWARE. GFXLIB = $(VISUALIZER_DIR)/ugfx -ifndef EMULATOR SRC += $(VISUALIZER_DIR)/visualizer.c -endif UINCDIR += $(GFXINC) $(VISUALIZER_DIR) ifdef LCD_ENABLE @@ -33,13 +31,17 @@ USE_UGFX = yes endif ifdef LCD_BACKLIGHT_ENABLE +ifndef EMULATOR SRC += $(VISUALIZER_DIR)/lcd_backlight.c SRC += lcd_backlight_hal.c +endif UDEFS += -DLCD_BACKLIGHT_ENABLE endif ifdef LED_ENABLE +ifndef EMULATOR SRC += $(VISUALIZER_DIR)/led_test.c +endif UDEFS += -DLED_ENABLE USE_UGFX = yes endif @@ -56,4 +58,8 @@ ifndef EMULATOR VISUALIZER_USER = visualizer_user.c endif endif -SRC += $(VISUALIZER_USER) \ No newline at end of file +SRC += $(VISUALIZER_USER) + +ifdef EMULATOR +UINCDIR += $(TMK_DIR)/common +endif \ No newline at end of file From 4e89732617454a600189242d417f5e2ba0855683 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Tue, 17 May 2016 09:45:05 +0300 Subject: [PATCH 092/109] Enable and fix compilation of more files --- led_test.c | 24 ++++++++++++------------ visualizer.mk | 6 +----- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/led_test.c b/led_test.c index d358ef81eb..6d3f3b2ed7 100644 --- a/led_test.c +++ b/led_test.c @@ -29,20 +29,20 @@ keyframe_animation_t led_test_animation = { .num_frames = 14, .loop = true, .frame_lengths = { - MS2ST(1000), // fade in - MS2ST(1000), // no op (leds on) - MS2ST(1000), // fade out - MS2ST(1000), // crossfade - MS2ST(3000), // left to rigt (outside in) - MS2ST(1000), // crossfade - MS2ST(3000), // top_to_bottom + gfxMillisecondsToTicks(1000), // fade in + gfxMillisecondsToTicks(1000), // no op (leds on) + gfxMillisecondsToTicks(1000), // fade out + gfxMillisecondsToTicks(1000), // crossfade + gfxMillisecondsToTicks(3000), // left to rigt (outside in) + gfxMillisecondsToTicks(1000), // crossfade + gfxMillisecondsToTicks(3000), // top_to_bottom 0, // mirror leds - MS2ST(1000), // crossfade - MS2ST(3000), // left_to_right (mirrored, so inside out) - MS2ST(1000), // crossfade - MS2ST(3000), // top_to_bottom + gfxMillisecondsToTicks(1000), // crossfade + gfxMillisecondsToTicks(3000), // left_to_right (mirrored, so inside out) + gfxMillisecondsToTicks(1000), // crossfade + gfxMillisecondsToTicks(3000), // top_to_bottom 0, // normal leds - MS2ST(1000), // crossfade + gfxMillisecondsToTicks(1000), // crossfade }, .frame_functions = { diff --git a/visualizer.mk b/visualizer.mk index 6788293290..56525ffd93 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -31,17 +31,15 @@ USE_UGFX = yes endif ifdef LCD_BACKLIGHT_ENABLE -ifndef EMULATOR SRC += $(VISUALIZER_DIR)/lcd_backlight.c +ifndef EMULATOR SRC += lcd_backlight_hal.c endif UDEFS += -DLCD_BACKLIGHT_ENABLE endif ifdef LED_ENABLE -ifndef EMULATOR SRC += $(VISUALIZER_DIR)/led_test.c -endif UDEFS += -DLED_ENABLE USE_UGFX = yes endif @@ -54,10 +52,8 @@ ULIBS += $(patsubst %,-l%,$(patsubst -l%,%,$(GFXLIBS))) endif ifndef VISUALIZER_USER -ifndef EMULATOR VISUALIZER_USER = visualizer_user.c endif -endif SRC += $(VISUALIZER_USER) ifdef EMULATOR From 15300cb681f74672547aa5e53d165ac748d43a17 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Tue, 17 May 2016 09:51:27 +0300 Subject: [PATCH 093/109] Build lcd_backlight_hal_emulator.c When the emulator is set, instead of the lcd_backlight_hal.c file. --- visualizer.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/visualizer.mk b/visualizer.mk index 56525ffd93..3e361491c9 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -34,6 +34,8 @@ ifdef LCD_BACKLIGHT_ENABLE SRC += $(VISUALIZER_DIR)/lcd_backlight.c ifndef EMULATOR SRC += lcd_backlight_hal.c +else +SRC += lcd_backlight_hal_emulator.c endif UDEFS += -DLCD_BACKLIGHT_ENABLE endif From 07e412c53829c66e948eb147873a04cd27b0771b Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Tue, 17 May 2016 11:21:38 +0300 Subject: [PATCH 094/109] Fix crash when event listener not created --- visualizer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/visualizer.c b/visualizer.c index ea84546fb6..ff99e960fd 100644 --- a/visualizer.c +++ b/visualizer.c @@ -458,7 +458,9 @@ void visualizer_init(void) { void update_status(bool changed) { if (changed) { GSourceListener* listener = geventGetSourceListener(layer_changed_event, NULL); - geventSendEvent(listener); + if (listener) { + geventSendEvent(listener); + } } #ifdef USE_SERIAL_LINK static systime_t last_update = 0; From fa8feb21a4709dba552df4a96205c50a319f5e3b Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 18 May 2016 08:40:36 +0300 Subject: [PATCH 095/109] Add custom led and lcd display support --- visualizer.c | 18 +++++++++++++++--- visualizer.h | 5 +++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/visualizer.c b/visualizer.c index ff99e960fd..bbb00debcf 100644 --- a/visualizer.c +++ b/visualizer.c @@ -85,6 +85,15 @@ static remote_object_t* remote_objects[] = { GDisplay* LCD_DISPLAY = 0; GDisplay* LED_DISPLAY = 0; +__attribute__((weak)) +GDisplay* get_lcd_display(void) { + return gdispGetDisplay(0); +} + +__attribute__((weak)) +GDisplay* get_led_display(void) { + return gdispGetDisplay(1); +} void start_keyframe_animation(keyframe_animation_t* animation) { animation->current_frame = -1; @@ -444,10 +453,13 @@ void visualizer_init(void) { #ifdef USE_SERIAL_LINK add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); #endif - // TODO: Make sure these works when either of these are disabled - LCD_DISPLAY = gdispGetDisplay(0); - LED_DISPLAY = gdispGetDisplay(1); +#ifdef LCD_ENABLE + LCD_DISPLAY = get_lcd_display(); +#endif +#ifdef LED_ENABLE + LED_DISPLAY = get_led_display(); +#endif // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning diff --git a/visualizer.h b/visualizer.h index 8a2772c6da..4d6a61dda3 100644 --- a/visualizer.h +++ b/visualizer.h @@ -45,6 +45,11 @@ void visualizer_suspend(void); // This should be called when the keyboard wakes up from suspend state void visualizer_resume(void); +// These functions are week, so they can be overridden by the keyboard +// if needed +GDisplay* get_lcd_display(void); +GDisplay* get_led_display(void); + // If you need support for more than 16 keyframes per animation, you can change this #define MAX_VISUALIZER_KEY_FRAMES 16 From 94519e387a85e3b4ab72bd7d837ff590cc690eb9 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 18 May 2016 09:03:42 +0300 Subject: [PATCH 096/109] Add callback function for emulator drawing --- visualizer.c | 4 ++++ visualizer.h | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/visualizer.c b/visualizer.c index bbb00debcf..607a64566a 100644 --- a/visualizer.c +++ b/visualizer.c @@ -413,6 +413,10 @@ static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { #ifdef LED_ENABLE gdispGFlush(LED_DISPLAY); #endif + +#if EMULATOR + draw_emulator(); +#endif // The animation can enable the visualizer // And we might need to update the state when that happens // so don't sleep diff --git a/visualizer.h b/visualizer.h index 4d6a61dda3..a3828e35f4 100644 --- a/visualizer.h +++ b/visualizer.h @@ -50,6 +50,11 @@ void visualizer_resume(void); GDisplay* get_lcd_display(void); GDisplay* get_led_display(void); +// For emulator builds, this function need to be implemented +#if EMULATOR +void draw_emulator(void); +#endif + // If you need support for more than 16 keyframes per animation, you can change this #define MAX_VISUALIZER_KEY_FRAMES 16 @@ -134,7 +139,7 @@ bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualiz // directly from the initalize_user_visualizer function (the animation can be null) bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state); -// These two functions have to be implemented by the user +// These functions have to be implemented by the user void initialize_user_visualizer(visualizer_state_t* state); void update_user_visualizer_state(visualizer_state_t* state); void user_visualizer_suspend(visualizer_state_t* state); From 4d7e4a4780bc779a3960497a9084ef6c9d6ef87e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 28 May 2016 11:13:08 +0300 Subject: [PATCH 097/109] Don't include lcd_backlight_hal for emulator --- visualizer.mk | 2 -- 1 file changed, 2 deletions(-) diff --git a/visualizer.mk b/visualizer.mk index 3e361491c9..56525ffd93 100644 --- a/visualizer.mk +++ b/visualizer.mk @@ -34,8 +34,6 @@ ifdef LCD_BACKLIGHT_ENABLE SRC += $(VISUALIZER_DIR)/lcd_backlight.c ifndef EMULATOR SRC += lcd_backlight_hal.c -else -SRC += lcd_backlight_hal_emulator.c endif UDEFS += -DLCD_BACKLIGHT_ENABLE endif From 9c955145f50abebcbe5ad5e203a91ca83249fee6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 29 May 2016 02:08:46 +0300 Subject: [PATCH 098/109] Fix emulator #ifdef check --- visualizer.c | 2 +- visualizer.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/visualizer.c b/visualizer.c index 607a64566a..0e587221fc 100644 --- a/visualizer.c +++ b/visualizer.c @@ -414,7 +414,7 @@ static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { gdispGFlush(LED_DISPLAY); #endif -#if EMULATOR +#ifdef EMULATOR draw_emulator(); #endif // The animation can enable the visualizer diff --git a/visualizer.h b/visualizer.h index a3828e35f4..45cfa9aa97 100644 --- a/visualizer.h +++ b/visualizer.h @@ -51,7 +51,7 @@ GDisplay* get_lcd_display(void); GDisplay* get_led_display(void); // For emulator builds, this function need to be implemented -#if EMULATOR +#ifdef EMULATOR void draw_emulator(void); #endif From 0c3189055f049e6023471e75139d488b288aead9 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 29 May 2016 17:42:32 +0300 Subject: [PATCH 099/109] Make LED visualization times configurable --- led_test.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/led_test.c b/led_test.c index 6d3f3b2ed7..d53f0b7fb7 100644 --- a/led_test.c +++ b/led_test.c @@ -25,6 +25,9 @@ SOFTWARE. #include "gfx.h" #include "math.h" +#define CROSSFADE_TIME 1000 +#define GRADIENT_TIME 3000 + keyframe_animation_t led_test_animation = { .num_frames = 14, .loop = true, @@ -32,17 +35,17 @@ keyframe_animation_t led_test_animation = { gfxMillisecondsToTicks(1000), // fade in gfxMillisecondsToTicks(1000), // no op (leds on) gfxMillisecondsToTicks(1000), // fade out - gfxMillisecondsToTicks(1000), // crossfade - gfxMillisecondsToTicks(3000), // left to rigt (outside in) - gfxMillisecondsToTicks(1000), // crossfade - gfxMillisecondsToTicks(3000), // top_to_bottom + gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade + gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in) + gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade + gfxMillisecondsToTicks(GRADIENT_TIME), // top_to_bottom 0, // mirror leds - gfxMillisecondsToTicks(1000), // crossfade - gfxMillisecondsToTicks(3000), // left_to_right (mirrored, so inside out) - gfxMillisecondsToTicks(1000), // crossfade - gfxMillisecondsToTicks(3000), // top_to_bottom + gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade + gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out) + gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade + gfxMillisecondsToTicks(GRADIENT_TIME), // top_to_bottom 0, // normal leds - gfxMillisecondsToTicks(1000), // crossfade + gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade }, .frame_functions = { From 489288f67462e0c6fae8e54644af2fa4593e4406 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 29 May 2016 18:27:32 +0300 Subject: [PATCH 100/109] Add a nicer sine based gradient for the LEDs --- led_test.c | 13 +++++-------- ugfx | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/led_test.c b/led_test.c index d53f0b7fb7..c2ea30b55b 100644 --- a/led_test.c +++ b/led_test.c @@ -89,14 +89,11 @@ static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS]; static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS]; static uint8_t compute_gradient_color(float t, float index, float num) { - const float target = t * (num - 1.0f); - const float half_num = num / 2.0f; - float d = fabs(index - target); - if (d > half_num) { - d = num - d; - } - d = 1.0f - (d / half_num); - return (uint8_t)(255.0f * d); + const float two_pi = 2.0f * PI; + float normalized_index = (1.0f - index / (num - 1)) * two_pi; + float x = t * two_pi + normalized_index; + float v = 0.5 * (cosf(x) + 1.0f); + return (uint8_t)(255.0f * v); } bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) { diff --git a/ugfx b/ugfx index 7d7eeef0ad..dc5786acc2 160000 --- a/ugfx +++ b/ugfx @@ -1 +1 @@ -Subproject commit 7d7eeef0ad0f1b28f4fb86ad931cb6774c7b9e81 +Subproject commit dc5786acc246fb23503517647c386e43f1bfb247 From 73d890a2c9c34b905cd5e74e7146fdd4578dcb96 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 1 Jun 2016 09:22:47 +0300 Subject: [PATCH 101/109] Fix visualizer sleeping too long The documentation for ugfx gEventWait wait is wrong and the function takes the time in milliseconds, instead of system ticks. This caused the the thread to sleep way too long. It also caused somewhat random sleeping behaviour as the MS2ST function overflows at around 43 seconds sleep. The event source is also now initialized correctly, so that the thread actually can be woken up by events. --- visualizer.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/visualizer.c b/visualizer.c index 0e587221fc..c240734055 100644 --- a/visualizer.c +++ b/visualizer.c @@ -25,6 +25,9 @@ SOFTWARE. #include "visualizer.h" #include "config.h" #include +#ifdef PROTOCOL_CHIBIOS +#include "ch.h" +#endif #ifdef LCD_ENABLE #include "gfx.h" @@ -67,7 +70,6 @@ static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboa status1->suspended == status2->suspended; } -static GSourceHandle layer_changed_event; static bool visualizer_enabled = false; #define MAX_SIMULTANEOUS_ANIMATIONS 4 @@ -185,8 +187,8 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize animation->first_update_of_frame = false; } - int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame; - if ((unsigned)wanted_sleep < *sleep_time) { + systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame; + if (wanted_sleep < *sleep_time) { *sleep_time = wanted_sleep; } @@ -345,7 +347,7 @@ static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { GListener event_listener; geventListenerInit(&event_listener); - geventAttachSource(&event_listener, layer_changed_event, 0); + geventAttachSource(&event_listener, (GSourceHandle)¤t_status, 0); visualizer_keyboard_status_t initial_status = { .default_layer = 0xFFFFFFFF, @@ -435,6 +437,16 @@ static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { } } dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time); +#ifdef PROTOCOL_CHIBIOS + // The gEventWait function really takes milliseconds, even if the documentation says ticks. + // Unfortunately there's no generic ugfx conversion from system time to milliseconds, + // so let's do it in a platform dependent way. + + // On windows the system ticks is the same as milliseconds anyway + if (sleep_time != TIME_INFINITE) { + sleep_time = ST2MS(sleep_time); + } +#endif geventEventWait(&event_listener, sleep_time); } #ifdef LCD_ENABLE @@ -473,7 +485,7 @@ void visualizer_init(void) { void update_status(bool changed) { if (changed) { - GSourceListener* listener = geventGetSourceListener(layer_changed_event, NULL); + GSourceListener* listener = geventGetSourceListener((GSourceHandle)¤t_status, NULL); if (listener) { geventSendEvent(listener); } From 3577e26fd9916ceab58779ec6323d43da54eb3b5 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 6 Jul 2016 00:24:31 -0400 Subject: [PATCH 102/109] fix/annotate wait_us lines --- keyboards/ergodox_ez/matrix.c | 12 ++++++++++++ quantum/matrix.c | 14 ++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/keyboards/ergodox_ez/matrix.c b/keyboards/ergodox_ez/matrix.c index 9c1efa1d05..dc29cf5cd0 100644 --- a/keyboards/ergodox_ez/matrix.c +++ b/keyboards/ergodox_ez/matrix.c @@ -39,6 +39,17 @@ along with this program. If not, see . #include "timer.h" #endif +/* + * This constant define not debouncing time in msecs, but amount of matrix + * scan loops which should be made to get stable debounced results. + * + * On Ergodox matrix scan rate is relatively low, because of slow I2C. + * Now it's only 317 scans/second, or about 3.15 msec/scan. + * According to Cherry specs, debouncing time is 5 msec. + * + * And so, there is no sense to have DEBOUNCE higher than 2. + */ + #ifndef DEBOUNCE # define DEBOUNCE 5 #endif @@ -181,6 +192,7 @@ uint8_t matrix_scan(void) if (debouncing) { if (--debouncing) { wait_us(1); + // this should be wait_ms(1) but has been left as-is at EZ's request } else { for (uint8_t i = 0; i < MATRIX_ROWS; i++) { matrix[i] = matrix_debouncing[i]; diff --git a/quantum/matrix.c b/quantum/matrix.c index 0949170255..3174e07390 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -27,16 +27,6 @@ along with this program. If not, see . #include "matrix.h" /* Set 0 if debouncing isn't needed */ -/* - * This constant define not debouncing time in msecs, but amount of matrix - * scan loops which should be made to get stable debounced results. - * - * On Ergodox matrix scan rate is relatively low, because of slow I2C. - * Now it's only 317 scans/second, or about 3.15 msec/scan. - * According to Cherry specs, debouncing time is 5 msec. - * - * And so, there is no sense to have DEBOUNCE higher than 2. - */ #ifndef DEBOUNCING_DELAY # define DEBOUNCING_DELAY 5 @@ -168,7 +158,7 @@ uint8_t matrix_scan(void) if (debouncing) { if (--debouncing) { - wait_us(1); + wait_ms(1); } else { for (uint8_t i = 0; i < MATRIX_ROWS; i++) { matrix[i] = matrix_debouncing[i]; @@ -192,7 +182,7 @@ uint8_t matrix_scan(void) if (debouncing) { if (--debouncing) { - wait_us(1); + wait_ms(1); } else { for (uint8_t i = 0; i < MATRIX_COLS; i++) { matrix_reversed[i] = matrix_reversed_debouncing[i]; From 3ac52b2e7623152f177670549b780492a0447c12 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 13:09:51 +0300 Subject: [PATCH 103/109] Remove serial_link .gitignore and .gitmodules --- quantum/serial_link/.gitignore | 1 - quantum/serial_link/.gitmodules | 3 --- 2 files changed, 4 deletions(-) delete mode 100644 quantum/serial_link/.gitignore delete mode 100644 quantum/serial_link/.gitmodules diff --git a/quantum/serial_link/.gitignore b/quantum/serial_link/.gitignore deleted file mode 100644 index 2d68e206e8..0000000000 --- a/quantum/serial_link/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.stackdump diff --git a/quantum/serial_link/.gitmodules b/quantum/serial_link/.gitmodules deleted file mode 100644 index 991cfe96d7..0000000000 --- a/quantum/serial_link/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "cgreen/cgreen"] - path = cgreen/cgreen - url = http://github.com/cgreen-devs/cgreen From c5bf090d16cf1fc4d8186f1e8fbbd62457e72da6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 13:10:30 +0300 Subject: [PATCH 104/109] Remove CGreen make files --- quantum/serial_link/cgreen/Makefile | 38 ----------------------- quantum/serial_link/cgreen/Makefile.build | 33 -------------------- quantum/serial_link/cgreen/cgreen | 1 - 3 files changed, 72 deletions(-) delete mode 100644 quantum/serial_link/cgreen/Makefile delete mode 100644 quantum/serial_link/cgreen/Makefile.build delete mode 160000 quantum/serial_link/cgreen/cgreen diff --git a/quantum/serial_link/cgreen/Makefile b/quantum/serial_link/cgreen/Makefile deleted file mode 100644 index 6b31a3f923..0000000000 --- a/quantum/serial_link/cgreen/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# This Makefile ensures that the build is made out of source in a subdirectory called 'build' -# If it doesn't exist, it is created and a Makefile created there (from Makefile.build) -# -# This Makefile also contains delegation of the most common make commands -# -# If you have cmake installed you should be able to do: -# -# make -# make test -# make install -# make package -# -# That should build cgreen for C and C++, run some tests, install it locally and -# generate two distributable packages. - -all: build - cd $(CGREEN_BUILD_DIR); make all - -test: build - cd $(CGREEN_BUILD_DIR); make test - -clean: build - cd $(CGREEN_BUILD_DIR); make clean - -package: build - cd $(CGREEN_BUILD_DIR); make package - -install: - cd $(CGREEN_BUILD_DIR); make install - -############# Internal - -build: - mkdir -p $(CGREEN_BUILD_DIR) - cp Makefile.build $(CGREEN_BUILD_DIR)/Makefile - - -.SILENT: diff --git a/quantum/serial_link/cgreen/Makefile.build b/quantum/serial_link/cgreen/Makefile.build deleted file mode 100644 index f76165244c..0000000000 --- a/quantum/serial_link/cgreen/Makefile.build +++ /dev/null @@ -1,33 +0,0 @@ -# This Makefile is copied from the cgreen top directory (where it is -# named Makefile.build) and put in a subdirectory called 'build' where -# builds are made This Makefile then automatically creates -# subdirectories for C and C++ builds configuring them using the cmake -# command. Once created you can always tweak the cmake setup as with -# any cmake build directory - -all: build-c build-c++ - for d in build-* ; do cd $$d; make ; cd .. ; done - -clean: - for d in build-* ; do cd $$d; make clean ; cd .. ; done - -check test: - for d in build-* ; do cd $$d; make check ; cd .. ; done - -package: - for d in build-* ; do cd $$d; make package ; cd .. ; done - -install: - for d in build-* ; do cd $$d; make install ; cd .. ; done - -############ Internal - -build-c: - mkdir build-c - cd build-c; cmake -G "Unix Makefiles" $(CGREEN_DIR) - -build-c++: - mkdir build-c++ - cd build-c++; cmake -G "Unix Makefiles" -DWITH_CXX:bool=ON $(CGREEN_DIR) - -.SILENT: diff --git a/quantum/serial_link/cgreen/cgreen b/quantum/serial_link/cgreen/cgreen deleted file mode 160000 index d4d438dda1..0000000000 --- a/quantum/serial_link/cgreen/cgreen +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d4d438dda1b7131f0bd0530b2c258e9dea6a2a9f From d5e7603d551a31836bf0c59db259ddc3593a1aa7 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 13:26:20 +0300 Subject: [PATCH 105/109] Remove extra serial_link subdirectory --- quantum/serial_link/README.md | 2 +- .../{serial_link => }/protocol/byte_stuffer.c | 0 .../{serial_link => }/protocol/byte_stuffer.h | 0 .../{serial_link => }/protocol/frame_router.c | 0 .../{serial_link => }/protocol/frame_router.h | 0 .../protocol/frame_validator.c | 0 .../protocol/frame_validator.h | 0 .../{serial_link => }/protocol/physical.h | 0 .../{serial_link => }/protocol/transport.c | 0 .../{serial_link => }/protocol/transport.h | 0 .../protocol/triple_buffered_object.c | 0 .../protocol/triple_buffered_object.h | 0 quantum/serial_link/serial_link.mk | 27 --------------- quantum/serial_link/serial_link_tests.mk | 34 ------------------- .../{serial_link => }/system/serial_link.c | 0 .../{serial_link => }/system/serial_link.h | 0 .../{serial_link => }/tests/Makefile | 0 .../tests/byte_stuffer_tests.c | 0 .../tests/frame_router_tests.c | 0 .../tests/frame_validator_tests.c | 0 .../{serial_link => }/tests/transport_tests.c | 0 .../tests/triple_buffered_object_tests.c | 0 22 files changed, 1 insertion(+), 62 deletions(-) rename quantum/serial_link/{serial_link => }/protocol/byte_stuffer.c (100%) rename quantum/serial_link/{serial_link => }/protocol/byte_stuffer.h (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_router.c (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_router.h (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_validator.c (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_validator.h (100%) rename quantum/serial_link/{serial_link => }/protocol/physical.h (100%) rename quantum/serial_link/{serial_link => }/protocol/transport.c (100%) rename quantum/serial_link/{serial_link => }/protocol/transport.h (100%) rename quantum/serial_link/{serial_link => }/protocol/triple_buffered_object.c (100%) rename quantum/serial_link/{serial_link => }/protocol/triple_buffered_object.h (100%) delete mode 100644 quantum/serial_link/serial_link.mk delete mode 100644 quantum/serial_link/serial_link_tests.mk rename quantum/serial_link/{serial_link => }/system/serial_link.c (100%) rename quantum/serial_link/{serial_link => }/system/serial_link.h (100%) rename quantum/serial_link/{serial_link => }/tests/Makefile (100%) rename quantum/serial_link/{serial_link => }/tests/byte_stuffer_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/frame_router_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/frame_validator_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/transport_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/triple_buffered_object_tests.c (100%) diff --git a/quantum/serial_link/README.md b/quantum/serial_link/README.md index 94af9125ce..e8490e290d 100644 --- a/quantum/serial_link/README.md +++ b/quantum/serial_link/README.md @@ -1 +1 @@ -# tmk_serial_link \ No newline at end of file +# qmk_serial_link \ No newline at end of file diff --git a/quantum/serial_link/serial_link/protocol/byte_stuffer.c b/quantum/serial_link/protocol/byte_stuffer.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/byte_stuffer.c rename to quantum/serial_link/protocol/byte_stuffer.c diff --git a/quantum/serial_link/serial_link/protocol/byte_stuffer.h b/quantum/serial_link/protocol/byte_stuffer.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/byte_stuffer.h rename to quantum/serial_link/protocol/byte_stuffer.h diff --git a/quantum/serial_link/serial_link/protocol/frame_router.c b/quantum/serial_link/protocol/frame_router.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_router.c rename to quantum/serial_link/protocol/frame_router.c diff --git a/quantum/serial_link/serial_link/protocol/frame_router.h b/quantum/serial_link/protocol/frame_router.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_router.h rename to quantum/serial_link/protocol/frame_router.h diff --git a/quantum/serial_link/serial_link/protocol/frame_validator.c b/quantum/serial_link/protocol/frame_validator.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_validator.c rename to quantum/serial_link/protocol/frame_validator.c diff --git a/quantum/serial_link/serial_link/protocol/frame_validator.h b/quantum/serial_link/protocol/frame_validator.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_validator.h rename to quantum/serial_link/protocol/frame_validator.h diff --git a/quantum/serial_link/serial_link/protocol/physical.h b/quantum/serial_link/protocol/physical.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/physical.h rename to quantum/serial_link/protocol/physical.h diff --git a/quantum/serial_link/serial_link/protocol/transport.c b/quantum/serial_link/protocol/transport.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/transport.c rename to quantum/serial_link/protocol/transport.c diff --git a/quantum/serial_link/serial_link/protocol/transport.h b/quantum/serial_link/protocol/transport.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/transport.h rename to quantum/serial_link/protocol/transport.h diff --git a/quantum/serial_link/serial_link/protocol/triple_buffered_object.c b/quantum/serial_link/protocol/triple_buffered_object.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/triple_buffered_object.c rename to quantum/serial_link/protocol/triple_buffered_object.c diff --git a/quantum/serial_link/serial_link/protocol/triple_buffered_object.h b/quantum/serial_link/protocol/triple_buffered_object.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/triple_buffered_object.h rename to quantum/serial_link/protocol/triple_buffered_object.h diff --git a/quantum/serial_link/serial_link.mk b/quantum/serial_link/serial_link.mk deleted file mode 100644 index e164cc5ff3..0000000000 --- a/quantum/serial_link/serial_link.mk +++ /dev/null @@ -1,27 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016 Fred Sundvik -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -UINCDIR += $(SERIAL_DIR) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) -SRC += serial_link_hal.c -OPT_DEFS += -DUSE_SERIAL_LINK diff --git a/quantum/serial_link/serial_link_tests.mk b/quantum/serial_link/serial_link_tests.mk deleted file mode 100644 index e292f582a9..0000000000 --- a/quantum/serial_link/serial_link_tests.mk +++ /dev/null @@ -1,34 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016 Fred Sundvik -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a - -CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" -CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" -export CGREEN_DIR -export CGREEN_BUILD_DIR -$(CGREEN_LIB): - @make -C $(SERIAL_DIR)/cgreen - -.PHONY serialtest: -serialtest : $(CGREEN_LIB) - @make -C $(SERIAL_DIR)/serial_link/tests BUILDDIR=../../../$(BUILDDIR) \ No newline at end of file diff --git a/quantum/serial_link/serial_link/system/serial_link.c b/quantum/serial_link/system/serial_link.c similarity index 100% rename from quantum/serial_link/serial_link/system/serial_link.c rename to quantum/serial_link/system/serial_link.c diff --git a/quantum/serial_link/serial_link/system/serial_link.h b/quantum/serial_link/system/serial_link.h similarity index 100% rename from quantum/serial_link/serial_link/system/serial_link.h rename to quantum/serial_link/system/serial_link.h diff --git a/quantum/serial_link/serial_link/tests/Makefile b/quantum/serial_link/tests/Makefile similarity index 100% rename from quantum/serial_link/serial_link/tests/Makefile rename to quantum/serial_link/tests/Makefile diff --git a/quantum/serial_link/serial_link/tests/byte_stuffer_tests.c b/quantum/serial_link/tests/byte_stuffer_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/byte_stuffer_tests.c rename to quantum/serial_link/tests/byte_stuffer_tests.c diff --git a/quantum/serial_link/serial_link/tests/frame_router_tests.c b/quantum/serial_link/tests/frame_router_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/frame_router_tests.c rename to quantum/serial_link/tests/frame_router_tests.c diff --git a/quantum/serial_link/serial_link/tests/frame_validator_tests.c b/quantum/serial_link/tests/frame_validator_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/frame_validator_tests.c rename to quantum/serial_link/tests/frame_validator_tests.c diff --git a/quantum/serial_link/serial_link/tests/transport_tests.c b/quantum/serial_link/tests/transport_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/transport_tests.c rename to quantum/serial_link/tests/transport_tests.c diff --git a/quantum/serial_link/serial_link/tests/triple_buffered_object_tests.c b/quantum/serial_link/tests/triple_buffered_object_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/triple_buffered_object_tests.c rename to quantum/serial_link/tests/triple_buffered_object_tests.c From a7e3e4e652a2b9f172282f3876d74ee247a63105 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 14:29:46 +0300 Subject: [PATCH 106/109] Add serial_link compilation to Infinity Ergodox --- Makefile | 12 +++++++++++- keyboards/infinity_ergodox/Makefile | 1 + keyboards/infinity_ergodox/infinity_ergodox.c | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c285dabad4..79f9e8b42c 100644 --- a/Makefile +++ b/Makefile @@ -198,10 +198,20 @@ ifeq ($(strip $(RGBLIGHT_ENABLE)), yes) endif ifeq ($(strip $(TAP_DANCE_ENABLE)), yes) - OPT_DEFS += -DTAP_DANCE_ENABLE + OPT_DEFS += -DTAP_DANCE_ENABLE SRC += $(QUANTUM_DIR)/process_keycode/process_tap_dance.c endif +ifeq ($(strip $(SERIAL_LINK_ENABLE)), yes) + SERIAL_DIR = $(QUANTUM_DIR)/serial_link + SERIAL_PATH = $(QUANTUM_PATH)/serial_link + SERIAL_SRC = $(wildcard $(SERIAL_PATH)/protocol/*.c) + SERIAL_SRC += $(wildcard $(SERIAL_PATH)/system/*.c) + SRC += $(patsubst $(QUANTUM_PATH)/%,%,$(SERIAL_SRC)) + OPT_DEFS += -DUSE_SERIAL_LINK + VAPTH += $(SERIAL_PATH) +endif + # Optimize size but this may cause error "relocation truncated to fit" #EXTRALDFLAGS = -Wl,--relax diff --git a/keyboards/infinity_ergodox/Makefile b/keyboards/infinity_ergodox/Makefile index fb21aae5eb..efa32e037d 100644 --- a/keyboards/infinity_ergodox/Makefile +++ b/keyboards/infinity_ergodox/Makefile @@ -66,6 +66,7 @@ COMMAND_ENABLE ?= yes # Commands for debug and configuration SLEEP_LED_ENABLE ?= yes # Breathing sleep LED during USB suspend NKRO_ENABLE ?= yes # USB Nkey Rollover CUSTOM_MATRIX ?= yes # Custom matrix file +SERIAL_LINK_ENABLE = yes ifndef QUANTUM_DIR include ../../Makefile diff --git a/keyboards/infinity_ergodox/infinity_ergodox.c b/keyboards/infinity_ergodox/infinity_ergodox.c index 34aded1a73..85054775e8 100644 --- a/keyboards/infinity_ergodox/infinity_ergodox.c +++ b/keyboards/infinity_ergodox/infinity_ergodox.c @@ -1 +1,11 @@ #include "infinity_ergodox.h" +#include "ch.h" +#include "hal.h" +#include "serial_link/system/serial_link.h" + +void init_serial_link_hal(void) { + PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2); + PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2); + PORTE->PCR[0] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(3); + PORTE->PCR[1] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(3); +} From 4b45deb652045aac73e5fdd7412a73bcef19c0c8 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 15:22:34 +0300 Subject: [PATCH 107/109] Enable serial_link support for ChibiOS --- Makefile | 2 +- tmk_core/common/keyboard.c | 11 +++++++++-- tmk_core/protocol/chibios/main.c | 30 +++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 79f9e8b42c..cfbe0e994d 100644 --- a/Makefile +++ b/Makefile @@ -208,7 +208,7 @@ ifeq ($(strip $(SERIAL_LINK_ENABLE)), yes) SERIAL_SRC = $(wildcard $(SERIAL_PATH)/protocol/*.c) SERIAL_SRC += $(wildcard $(SERIAL_PATH)/system/*.c) SRC += $(patsubst $(QUANTUM_PATH)/%,%,$(SERIAL_SRC)) - OPT_DEFS += -DUSE_SERIAL_LINK + OPT_DEFS += -DSERIAL_LINK_ENABLE VAPTH += $(SERIAL_PATH) endif diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index 81df8eb73b..3a1262a9f9 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -49,6 +49,9 @@ along with this program. If not, see . #ifdef RGBLIGHT_ENABLE # include "rgblight.h" #endif +#ifdef SERIAL_LINK_ENABLE +# include "serial_link/system/serial_link.h" +#endif #ifdef MATRIX_HAS_GHOST static bool has_ghost_in_row(uint8_t row) @@ -167,11 +170,15 @@ MATRIX_LOOP_END: #endif #ifdef SERIAL_MOUSE_ENABLE - serial_mouse_task(); + serial_mouse_task(); #endif #ifdef ADB_MOUSE_ENABLE - adb_mouse_task(); + adb_mouse_task(); +#endif + +#ifdef SERIAL_LINK_ENABLE + serial_link_update(); #endif // update LED diff --git a/tmk_core/protocol/chibios/main.c b/tmk_core/protocol/chibios/main.c index 54bb6a8f55..aeb11752f4 100644 --- a/tmk_core/protocol/chibios/main.c +++ b/tmk_core/protocol/chibios/main.c @@ -35,6 +35,9 @@ #ifdef SLEEP_LED_ENABLE #include "sleep_led.h" #endif +#ifdef SERIAL_LINK_ENABLE +#include "serial_link/system/serial_link.h" +#endif #include "suspend.h" @@ -98,9 +101,27 @@ int main(void) { /* init printf */ init_printf(NULL,sendchar_pf); - /* Wait until the USB is active */ - while(USB_DRIVER.state != USB_ACTIVE) +#ifdef SERIAL_LINK_ENABLE + init_serial_link(); +#endif + + host_driver_t* driver = NULL; + + /* Wait until the USB or serial link is active */ + while (true) { + if(USB_DRIVER.state == USB_ACTIVE) { + driver = &chibios_driver; + break; + } +#ifdef SERIAL_LINK_ENABLE + if(is_serial_link_connected()) { + driver = get_serial_link_driver(); + break; + } + serial_link_update(); +#endif chThdSleepMilliseconds(50); + } /* Do need to wait here! * Otherwise the next print might start a transfer on console EP @@ -113,7 +134,7 @@ int main(void) { /* init TMK modules */ keyboard_init(); - host_set_driver(&chibios_driver); + host_set_driver(driver); #ifdef SLEEP_LED_ENABLE sleep_led_init(); @@ -128,6 +149,9 @@ int main(void) { print("[s]"); while(USB_DRIVER.state == USB_SUSPENDED) { /* Do this in the suspended state */ +#ifdef SERIAL_LINK_ENABLE + serial_link_update(); +#endif suspend_power_down(); // on AVR this deep sleeps for 15ms /* Remote wakeup */ if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) { From e5726b017a9de2922ea923818c6e215600f68a85 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 15:36:45 +0300 Subject: [PATCH 108/109] Add setting of the master side to the makefile --- Makefile | 14 ++++++++++++++ tmk_core/common.mk | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/Makefile b/Makefile index cfbe0e994d..7c00ce2cc8 100644 --- a/Makefile +++ b/Makefile @@ -59,6 +59,12 @@ ifndef KEYBOARD KEYBOARD=planck endif +MASTER ?= left +ifdef master + MASTER = $(master) +endif + + # converts things to keyboards/subproject ifneq (,$(findstring /,$(KEYBOARD))) TEMP:=$(KEYBOARD) @@ -212,6 +218,14 @@ ifeq ($(strip $(SERIAL_LINK_ENABLE)), yes) VAPTH += $(SERIAL_PATH) endif +ifeq ($(MASTER),right) + OPT_DEFS += -DMASTER_IS_ON_RIGHT +else + ifneq ($(MASTER),left) +$(error MASTER does not have a valid value(left/right)) + endif +endif + # Optimize size but this may cause error "relocation truncated to fit" #EXTRALDFLAGS = -Wl,--relax diff --git a/tmk_core/common.mk b/tmk_core/common.mk index d71fba9bc1..5bae0d762f 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -97,6 +97,15 @@ ifeq ($(strip $(KEYMAP_SECTION_ENABLE)), yes) endif endif +ifeq ($(MASTER),right) + OPT_DEFS += -DMASTER_IS_ON_RIGHT +else + ifneq ($(MASTER),left) +$(error MASTER does not have a valid value(left/right)) + endif +endif + + # Version string OPT_DEFS += -DVERSION=$(shell (git describe --always --dirty || echo 'unknown') 2> /dev/null) From f727801bc69b3db28f84b7b8986756193bbfd21e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 20:17:16 +0300 Subject: [PATCH 109/109] Delete .gitmodules from visualizer --- quantum/visualizer/.gitmodules | 3 --- quantum/visualizer/ugfx | 1 - 2 files changed, 4 deletions(-) delete mode 100644 quantum/visualizer/.gitmodules delete mode 160000 quantum/visualizer/ugfx diff --git a/quantum/visualizer/.gitmodules b/quantum/visualizer/.gitmodules deleted file mode 100644 index b320458c0e..0000000000 --- a/quantum/visualizer/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "ugfx"] - path = ugfx - url = https://bitbucket.org/fredizzimo/ugfx.git diff --git a/quantum/visualizer/ugfx b/quantum/visualizer/ugfx deleted file mode 160000 index e221a69061..0000000000 --- a/quantum/visualizer/ugfx +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e221a690616e20f87e0b0088baffdbd5427be862