1 #ifndef LIBPROPELLER_SERIAL_H_
2 #define LIBPROPELLER_SERIAL_H_
6 #include "libpropeller/numbers/numbers.h"
8 #include "libpropeller/streaminterface/streaminterface.h"
10 #include "libpropeller/printstream/printstream.h"
12 extern char _load_start_serial_cog[];
64 bool Start(
const int rxpin,
const int txpin,
const int rate,
const int ctspin = -1) {
67 volatile void * asm_driver_reference = NULL;
68 __asm__
volatile (
"mov %[asm_driver_reference], #Fds_entry \n\t"
69 : [asm_driver_reference]
"+r" (asm_driver_reference));
73 extern char * Masktx
asm(
"Masktx");
74 extern char * Maskrx
asm(
"Maskrx");
75 extern char * Ctra_val
asm(
"Ctra_val");
76 extern char * Ctrb_val
asm(
"Ctrb_val");
77 extern char * Period_ptr
asm(
"Period_ptr");
78 extern char * Rx_head_ptr
asm(
"Rx_head_ptr");
79 extern char * Rx_end_ptr
asm(
"Rx_end_ptr");
80 extern char * Update_head_ptr
asm(
"Update_head_ptr");
81 extern char * Maskcts
asm(
"Maskcts");
84 SetDriverLong(&Masktx, 0);
85 SetDriverLong(&Ctra_val, 0);
88 SetDriverLong(&Masktx, 1 << txpin);
89 SetDriverLong(&Ctra_val, 0x10000000 | txpin);
91 SetDriverLong(&Maskrx, 0);
92 SetDriverLong(&Ctrb_val, 0);
94 DIRA &= ~(1 << rxpin);
96 SetDriverLong(&Maskrx, 1 << rxpin);
97 SetDriverLong(&Ctrb_val, 0x54000000 | rxpin);
101 SetDriverLong(&Maskcts, 0);
104 DIRA &= ~(1 << ctspin);
106 SetDriverLong(&Maskcts, 1 << ctspin);
112 SetDriverLong(&Period_ptr, (
int) &half_bit_period_);
115 SetDriverLong(&Rx_head_ptr, (
int) &rx_buffer_);
116 SetDriverLong(&Rx_end_ptr, (
int) &rx_buffer_ +
kBufferLength);
121 SetDriverLong(&Update_head_ptr, (
int) &rx_head_);
123 cog_ = 1 +
cognew((
int) (&(*(
int *) &_load_start_serial_cog[0])), (
int) (&write_buf_ptr_));
125 WaitForTransmissionCompletion();
134 WaitForTransmissionCompletion();
161 bool SetBaudClock(
const unsigned int rate,
const unsigned int sysclock) {
162 WaitForTransmissionCompletion();
165 int got_rate = ((sysclock >> 1) + (rate >> 1)) / rate;
168 half_bit_period_ = got_rate > kMinimumHalfPeriod ? got_rate : kMinimumHalfPeriod;
171 return got_rate >= kMinimumHalfPeriod;
178 void Put(
const char character) {
179 WaitForTransmissionCompletion();
180 send_temp_ = character;
181 write_buf_ptr_ = (int) (&send_temp_);
191 int Put(
const char * buffer_ptr,
const int count) {
192 WaitForTransmissionCompletion();
193 for (
int i = 0; i <
count; i++) {
206 int Put(
const char * buffer_ptr) {
207 return Put(buffer_ptr, strlen(buffer_ptr));
231 while ((rxbyte = CheckBuffer()) < 0);
232 return (
char) rxbyte;
235 return CheckBuffer();
238 unsigned int elapsed_cycles = 0;
240 int previous_cnt =
CNT;
242 rxbyte = CheckBuffer();
243 int current_cnt =
CNT;
244 elapsed_cycles += current_cnt - previous_cnt;
245 previous_cnt = current_cnt;
246 }
while (rxbyte < 0 && elapsed_cycles < total_cycles);
261 int Get(
char *
const buffer,
const int length,
const int timeout = -1) {
263 for (character_count = 0; character_count < length; ++character_count) {
265 if (character <= -1) {
266 return character_count;
268 buffer[character_count] = (
char) character;
270 return character_count;
284 int Get(
char *
const buffer,
const char terminator =
'\n') {
286 char received_character = terminator + 1;
287 for (character_count = 0; received_character != terminator; ++character_count) {
288 received_character = (
char)
Get();
289 buffer[character_count] = received_character;
291 buffer[character_count] =
'\0';
292 return character_count;
304 const int tail = rx_tail_;
305 const int head = rx_head_;
313 int PutFormatted(
const char * formatString, ...){
317 va_start(list, formatString);
318 int result = ps.Format(formatString, list);
329 static const int kMinimumHalfPeriod = 86;
331 volatile int write_buf_ptr_;
332 volatile int send_temp_;
333 volatile int half_bit_period_;
334 volatile short rx_head_;
335 volatile short rx_tail_;
342 int CheckBuffer(
void) {
344 if (rx_tail_ != rx_head_) {
345 rxbyte = rx_buffer_[rx_tail_];
346 rx_buffer_[rx_tail_] = 0;
360 void WaitForTransmissionCompletion(
void) {
361 while (write_buf_ptr_) {
365 void SetDriverLong(
const int index,
const int value) {
366 ((
int *) &_load_start_serial_cog[index])[0] = value;
370 void SetDriverLong(
char ** index,
const int value) {
371 SetDriverLong((
int) index, value);
411 #endif // LIBPROPELLER_SERIAL_H_