1 #ifndef LIBPROPELLER_SERIAL_H_
2 #define LIBPROPELLER_SERIAL_H_
60 volatile void * asm_driver_reference = NULL;
61 __asm__
volatile (
"mov %[asm_driver_reference], #Fds_entry \n\t"
62 : [asm_driver_reference]
"+r" (asm_driver_reference));
66 extern char * Masktx
asm(
"Masktx");
67 extern char *
Maskrx asm(
"Maskrx");
68 extern char * Ctra_val
asm(
"Ctra_val");
69 extern char * Ctrb_val
asm(
"Ctrb_val");
70 extern char * Period_ptr
asm(
"Period_ptr");
71 extern char * Rx_head_ptr
asm(
"Rx_head_ptr");
72 extern char * Rx_end_ptr
asm(
"Rx_end_ptr");
73 extern char * Update_head_ptr
asm(
"Update_head_ptr");
74 extern char * Maskcts
asm(
"Maskcts");
77 SetDriverLong(&Masktx, 0);
78 SetDriverLong(&Ctra_val, 0);
81 SetDriverLong(&Masktx, 1 << txpin);
82 SetDriverLong(&Ctra_val, 0x10000000 | txpin);
85 SetDriverLong(&Ctrb_val, 0);
89 SetDriverLong(&
Maskrx, 1 << rxpin);
90 SetDriverLong(&Ctrb_val, 0x54000000 | rxpin);
94 SetDriverLong(&Maskcts, 0);
99 SetDriverLong(&Maskcts, 1 <<
ctspin);
105 SetDriverLong(&Period_ptr, (
int) &half_bit_period_);
108 SetDriverLong(&Rx_head_ptr, (
int) &rx_buffer_);
109 SetDriverLong(&Rx_end_ptr, (
int) &rx_buffer_ +
kBufferLength);
114 SetDriverLong(&Update_head_ptr, (
int) &rx_head_);
118 WaitForTransmissionCompletion();
127 WaitForTransmissionCompletion();
154 bool SetBaudClock(
const unsigned int rate,
const unsigned int sysclock) {
155 WaitForTransmissionCompletion();
158 int got_rate = ((sysclock >> 1) + (rate >> 1)) / rate;
161 half_bit_period_ = got_rate > kMinimumHalfPeriod ? got_rate : kMinimumHalfPeriod;
164 return got_rate >= kMinimumHalfPeriod;
171 void Put(
const char character) {
172 WaitForTransmissionCompletion();
173 send_temp_ = character;
174 write_buf_ptr_ = (int) (&send_temp_);
184 int Put(
const char * buffer_ptr,
const int count) {
185 WaitForTransmissionCompletion();
186 for (
int i = 0; i < count; i++) {
199 int Put(
const char * buffer_ptr) {
200 return Put(buffer_ptr, strlen(buffer_ptr));
237 if (format == NULL) {
241 int bytesWritten = 0;
244 va_start(list, format);
247 for (
int stringIndex = 0; format[stringIndex] != 0; stringIndex++) {
249 if (format[stringIndex] ==
'%') {
253 bool padZero =
false;
255 if (format[stringIndex] ==
'0') {
259 if (format[stringIndex] >=
'1' and format[stringIndex] <=
'9') {
260 char paddingBuffer[5];
261 int paddingIndex = 0;
265 if (format[stringIndex] >=
'0' and format[stringIndex] <=
'9') {
266 paddingBuffer[paddingIndex++] = format[stringIndex++];
267 if (format[stringIndex] >=
'0' and format[stringIndex] <=
'9') {
268 paddingBuffer[paddingIndex++] = format[stringIndex++];
269 if (format[stringIndex] >=
'0' and format[stringIndex] <=
'9') {
270 paddingBuffer[paddingIndex++] = format[stringIndex++];
271 if (format[stringIndex] >=
'0' and format[stringIndex] <=
'9') {
272 paddingBuffer[paddingIndex++] = format[stringIndex++];
286 paddingBuffer[paddingIndex] = 0;
298 if (format[stringIndex] == 0) {
302 if (format[stringIndex] ==
'd' || format[stringIndex] ==
'i') {
303 int number = va_arg(list,
int);
306 for (
int i = padAmount - digits; i > 0; --i) {
312 bytesWritten += digits;
313 }
else if (format[stringIndex] ==
'x' || format[stringIndex] ==
'X') {
314 int number = va_arg(list,
int);
317 for (
int i = padAmount - digits; i > 0; --i) {
327 bytesWritten += digits;
328 }
else if (format[stringIndex] ==
'c') {
329 char character = (char) (va_arg(list,
int));
332 }
else if (format[stringIndex] ==
's') {
333 char *
string = (
char *) (va_arg(list,
int));
334 while (*
string != 0) {
338 }
else if (format[stringIndex] ==
'%') {
344 Put(format[stringIndex]);
367 int Get(
const int timeout = -1) {
372 while ((rxbyte = CheckBuffer()) < 0);
373 return (
char) rxbyte;
375 unsigned int total_cycles = (CLKFREQ / 1000) * timeout;
376 unsigned int elapsed_cycles = 0;
378 int previous_cnt = CNT;
380 rxbyte = CheckBuffer();
381 int current_cnt = CNT;
382 elapsed_cycles += current_cnt - previous_cnt;
383 previous_cnt = current_cnt;
384 }
while (rxbyte < 0 && elapsed_cycles < total_cycles);
399 int Get(
char *
const buffer,
const int length,
const int timeout = -1) {
401 for (character_count = 0; character_count < length; ++character_count) {
402 int character =
Get(timeout);
403 if (character <= -1) {
404 return character_count;
406 buffer[character_count] = (char) character;
408 return character_count;
422 int Get(
char *
const buffer,
const char terminator =
'\n') {
424 char received_character = terminator + 1;
425 for (character_count = 0; received_character != terminator; ++character_count) {
426 received_character = (char)
Get();
427 buffer[character_count] = received_character;
429 buffer[character_count] =
'\0';
430 return character_count;
442 const int tail = rx_tail_;
443 const int head = rx_head_;
457 static const int kMinimumHalfPeriod = 86;
459 volatile int write_buf_ptr_;
460 volatile int send_temp_;
461 volatile int half_bit_period_;
462 volatile short rx_head_;
463 volatile short rx_tail_;
470 int CheckBuffer(
void) {
472 if (rx_tail_ != rx_head_) {
473 rxbyte = rx_buffer_[rx_tail_];
474 rx_buffer_[rx_tail_] = 0;
480 void WaitForTransmissionCompletion(
void) {
481 while (write_buf_ptr_) {
485 void SetDriverLong(
const int index,
const int value) {
486 ((
int *) &_load_start_serial_cog[index])[0] = value;
490 void SetDriverLong(
char ** index,
const int value) {
491 SetDriverLong((
int) index, value);
532 #endif // LIBPROPELLER_SERIAL_H_