You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.9 KiB
79 lines
2.9 KiB
![]()
9 years ago
|
/*
|
||
|
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.
|
||
|
*/
|
||
|
|
||
![]()
9 years ago
|
#include "serial_link/protocol/triple_buffered_object.h"
|
||
![]()
8 years ago
|
#include "serial_link/system/serial_link.h"
|
||
![]()
9 years ago
|
#include <stdbool.h>
|
||
|
#include <stddef.h>
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
#define GET_READ_INDEX() object->state & 3
|
||
![]()
9 years ago
|
#define GET_WRITE_INDEX() (object->state >> 2) & 3
|
||
|
#define GET_SHARED_INDEX() (object->state >> 4) & 3
|
||
|
#define GET_DATA_AVAILABLE() (object->state >> 6) & 1
|
||
![]()
9 years ago
|
|
||
|
#define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i)
|
||
![]()
9 years ago
|
#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))
|
||
![]()
9 years ago
|
|
||
|
void triple_buffer_init(triple_buffer_object_t* object) {
|
||
|
object->state = 0;
|
||
![]()
9 years ago
|
SET_WRITE_INDEX(0);
|
||
![]()
9 years ago
|
SET_READ_INDEX(1);
|
||
![]()
9 years ago
|
SET_SHARED_INDEX(2);
|
||
|
SET_DATA_AVAILABLE(0);
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) {
|
||
![]()
9 years ago
|
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();
|
||
![]()
9 years ago
|
return object->buffer + object_size * shared_index;
|
||
![]()
9 years ago
|
}
|
||
|
else {
|
||
|
serial_link_unlock();
|
||
![]()
9 years ago
|
return NULL;
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) {
|
||
|
uint8_t write_index = GET_WRITE_INDEX();
|
||
|
return object->buffer + object_size * write_index;
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
void triple_buffer_end_write_internal(triple_buffer_object_t* object) {
|
||
![]()
9 years ago
|
serial_link_lock();
|
||
![]()
9 years ago
|
uint8_t shared_index = GET_SHARED_INDEX();
|
||
![]()
9 years ago
|
uint8_t write_index = GET_WRITE_INDEX();
|
||
![]()
9 years ago
|
SET_SHARED_INDEX(write_index);
|
||
|
SET_WRITE_INDEX(shared_index);
|
||
![]()
9 years ago
|
SET_DATA_AVAILABLE(true);
|
||
|
serial_link_unlock();
|
||
![]()
9 years ago
|
}
|