uC chip interface arduino  0.9.0
A interface for async and neuromrphic IC testing
Loading...
Searching...
No Matches
core_ring_buffer.cpp
Go to the documentation of this file.
1/*
2 This file is part of the Firmware project to interface with small Async or Neuromorphic chips
3 Copyright (C) 2022-2023 Ole Richter - University of Groningen
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include "core_ring_buffer.h"
20
22volatile uint16_t input_ring_buffer_start = 0;
23volatile uint16_t input_ring_buffer_next_free = 0;
25volatile uint16_t output_ring_buffer_start = 0;
26volatile uint16_t output_ring_buffer_next_free = 0;
27volatile bool output_buffer_overflow = 0;
28volatile bool output_buffer_read = 1;
29
30volatile unsigned long offset_time = 0; // offset time is in microseconds, 0 also doubles as no recording
31volatile bool read_active = 0;
33
34volatile bool is_realtime = 1;
35
37 //input_ring_buffer = (packet_t*) malloc(sizeof(packet_t)*INPUT_BUFFER_SIZE);
38 //output_ring_buffer = (packet_t*) malloc(sizeof(packet_t)*OUTPUT_BUFFER_SIZE);
43}
44
45void set_read_on_request(uint8_t state){
46 output_buffer_read = (state > 0);
47 //send confirmation
49}
50
52 #if defined(ARDUINO_TEENSY41) || defined(ARDUINO_TEENSY40)
53 NVIC_DISABLE_IRQ(IRQ_GPIO6789);
54 #elif defined(ARDUINO_ARCH_SAMD)
55 NVIC_DisableIRQ(EIC_IRQn);
56 #else
57 //this will not work as the serial requires interrupts
58 noInterrupts();
59 #endif
60}
62 #if defined(ARDUINO_TEENSY41) || defined(ARDUINO_TEENSY40)
63 NVIC_ENABLE_IRQ(IRQ_GPIO6789);
64 #elif defined(ARDUINO_ARCH_SAMD)
65 NVIC_EnableIRQ(EIC_IRQn);
66 #else
67 //this will not work as the serial requires interrupts
68 interrupt();
69 #endif
70}
71
73 uint32_t free_spots = 0;
76 }
77 else{
79 }
80 // do most likey case first to speed up the request.
81 if (free_spots > 5 ) return true;
82 else if (free_spots > 1) {
85 if (is_realtime){
86 noInterrupts();
91 is_realtime = false;
92 interrupts();
93 }
94 return true;
95 }
96 else if (free_spots == 1) {
97 noInterrupts();
104 interrupts();
105 return false;
106 }
107 else {
108 noInterrupts();
115 interrupts();
116 return false;
117 }
118}
119
121 uint32_t free_spots = 0;
122 noInterrupts();
125 }
126 else{
128 }
129 interrupts();
130 if (free_spots == 0) return false;
131 else return true;
132}
133
135 packet_t data_packet;
137 data_packet.data.exec_time = micros()-offset_time;
138 uint32_t free_spots = 0;
139 noInterrupts();
142 }
143 else{
145 }
146 interrupts();
147 data_packet.data.value = free_spots;
148 uint8_t position;
149 // due to different storage alignment need to write data bytes individual
150 for (position = 0; position < sizeof(packet_t); position++) Serial.write(data_packet.bytes[position]);
151 //send confirmation
153
154}
155
156void add_input_packet(packet_t* input_packet){
158 noInterrupts();
161 interrupts();
162 }
163 // if instruction buffer is full send error, error_message not used because send without que
164 else {
166 }
167}
168
170 noInterrupts();
171 if (!read_active){
172 read_active = true;
173 interrupts();
175 noInterrupts();
178 interrupts();
179 uint8_t position;
180 // due to different storage alignment need to write data bytes individual
181 for (position = 0; position < sizeof(packet_t); position++) Serial.write(out.bytes[position]);
182 }
183 read_active = false;
184 //send confirmation
185 send_data32(IN_READ,0,true);
187 }
188}
189
194 interrupts();
195 uint8_t position;
196 // due to different storage alignment need to write data bytes individual
197 for (position = 0; position < sizeof(packet_t); position++) {
198 Serial.write(out.bytes[position]);
199 }
200 }
201}
202
204 if (!read_active){
205 noInterrupts();
207 interrupts();
208 uint8_t position;
209 // due to different storage alignment need to write data bytes individual
210 for (position = 0; position < sizeof(packet_t); position++) Serial.write(out.bytes[position]);
211 //send confirmation
212 if (conf){
215 }
216 }
217}
218
220
221 uint32_t ring_buffer_start = input_ring_buffer_start;
222 interrupts();
223 while (ring_buffer_start != input_ring_buffer_next_free){
224
225 noInterrupts();
226 packet_t out = copy_packet(&input_ring_buffer[ring_buffer_start]);
227 ring_buffer_start = (ring_buffer_start + 1) % INPUT_BUFFER_SIZE;
228 interrupts();
229 uint8_t position;
230 // due to different storage alignment need to write data bytes individual
231 for (position = 0; position < sizeof(packet_t); position++) Serial.write(out.bytes[position]);
232 }
233 //send confirmation
235}
236
237void send_data32(uint8_t header, uint32_t value, bool is_confirmation){
238 if (is_output_buffer_not_full() && (is_confirmation || offset_time != 0)) {
239 noInterrupts();
244 interrupts();
245 }
246}
247
248void send_data_i2c(uint8_t header,uint8_t device_address_8, uint8_t register_address, uint8_t value_ms, uint8_t value_ls, bool is_confirmation){
249 if (is_output_buffer_not_full() && (is_confirmation || offset_time != 0)) {
250 noInterrupts();
258 interrupts();
259 }
260}
261
262void send_pin(uint8_t header, uint8_t id, uint8_t value, bool is_confirmation){
263 if (is_output_buffer_not_full() && (is_confirmation || offset_time != 0)) {
264 noInterrupts();
270 interrupts();
271 }
272}
273
274void send_config(uint8_t header, uint8_t config_header, uint8_t value){
276 noInterrupts();
282 interrupts();
283 }
284}
285
286void error_message(uint8_t error_header, uint8_t source_header, uint32_t value, uint8_t sub_source_header){
288 noInterrupts();
294 interrupts();
295 }
296}
297
298void error_message(uint8_t error_header, uint8_t source_header){
299 error_message(error_header,source_header,micros()-offset_time,255);
300}
301
302void error_message_bypass_buffer(uint8_t error_header, uint8_t source_header, uint32_t value, uint8_t sub_source_header){
303 packet_t error_packet;
304 error_packet.error.header = error_header;
305 error_packet.error.org_header = source_header;
306 error_packet.error.value = value;
307 error_packet.error.sub_header = sub_source_header;
308
309 uint8_t position;
310 // due to different storage alignment need to write data bytes individual
311 for (position = 0; position < sizeof(packet_t); position++) Serial.write(error_packet.bytes[position]);
312}
void send_output_ring_buffer()
void set_read_on_request(uint8_t state)
volatile uint16_t output_ring_buffer_next_free
volatile packet_t input_ring_buffer[INPUT_BUFFER_SIZE]
volatile unsigned long offset_time
void send_input_ring_buffer()
void send_output_ring_buffer_last(bool conf)
void error_message(uint8_t error_header, uint8_t source_header, uint32_t value, uint8_t sub_source_header)
volatile uint16_t input_ring_buffer_start
volatile bool output_buffer_read
bool is_output_buffer_not_full()
bool is_input_buffer_not_full()
volatile packet_t output_ring_buffer[OUTPUT_BUFFER_SIZE]
void send_data32(uint8_t header, uint32_t value, bool is_confirmation)
volatile uint16_t input_ring_buffer_next_free
void enable_gpio_interrupt()
volatile bool read_active
void send_input_ring_buffer_free_spots()
volatile uint16_t loop_runs_without_gpio_interrups
void send_data_i2c(uint8_t header, uint8_t device_address_8, uint8_t register_address, uint8_t value_ms, uint8_t value_ls, bool is_confirmation)
void send_pin(uint8_t header, uint8_t id, uint8_t value, bool is_confirmation)
void send_output_ring_buffer_first()
volatile bool is_realtime
void send_config(uint8_t header, uint8_t config_header, uint8_t value)
volatile uint16_t output_ring_buffer_start
void disable_gpio_interrupt()
volatile bool output_buffer_overflow
void setup_ring_buffer()
void add_input_packet(packet_t *input_packet)
void error_message_bypass_buffer(uint8_t error_header, uint8_t source_header, uint32_t value, uint8_t sub_source_header)
packet_t copy_packet(volatile packet_t *in)
Definition: datatypes.cpp:23
@ IN_READ_INSTRUCTIONS
Definition: datatypes.h:63
@ IN_READ
Definition: datatypes.h:37
@ IN_CONF_READ_ON_REQUEST
Definition: datatypes.h:75
@ IN_READ_LAST
Definition: datatypes.h:42
@ IN_FREE_INSTRUCTION_SPOTS
Definition: datatypes.h:67
@ CONF_NONE
Definition: datatypes.h:673
@ OUT_ERROR_INPUT_FULL
Definition: datatypes.h:537
@ OUT_FREE_INSTRUCTION_SPOTS
Definition: datatypes.h:402
@ OUT_WARNING_DATA_COLLECTION_SQUEUED
Definition: datatypes.h:593
@ OUT_ERROR_OUTPUT_FULL
Definition: datatypes.h:542
uint8_t header
Definition: datatypes.h:759
uint8_t value
Definition: datatypes.h:762
uint32_t exec_time
Definition: datatypes.h:760
uint8_t config_header
Definition: datatypes.h:761
uint8_t header
Definition: datatypes.h:726
uint32_t exec_time
Definition: datatypes.h:727
uint32_t value
Definition: datatypes.h:728
uint8_t header
Definition: datatypes.h:746
uint8_t register_address
Definition: datatypes.h:749
uint8_t value_ms
Definition: datatypes.h:750
uint8_t value_ls
Definition: datatypes.h:751
uint32_t exec_time
Definition: datatypes.h:747
uint8_t component_address
Definition: datatypes.h:748
uint8_t sub_header
Definition: datatypes.h:784
uint8_t header
Definition: datatypes.h:781
uint32_t value
Definition: datatypes.h:783
uint8_t org_header
Definition: datatypes.h:782
uint8_t id
Definition: datatypes.h:772
uint8_t header
Definition: datatypes.h:770
uint8_t value
Definition: datatypes.h:773
uint32_t exec_time
Definition: datatypes.h:771
#define OUTPUT_BUFFER_SIZE
Definition: uc_boards.h:108
#define INPUT_BUFFER_SIZE
Definition: uc_boards.h:107
config_t config
Definition: datatypes.h:796
data_i2c_t data_i2c
Definition: datatypes.h:795
uint8_t bytes[1+4+4]
Definition: datatypes.h:793
error_t error
Definition: datatypes.h:798
data32_t data
Definition: datatypes.h:794
pin_t pin
Definition: datatypes.h:797