1
0

Fix softwere serial

This commit is contained in:
tmk 2013-02-22 19:38:06 +09:00
parent 11b25580cb
commit deebebf614
2 changed files with 18 additions and 27 deletions

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
/* host role */ /* host role */
void serial_init(void); void serial_init(void);
uint8_t serial_recv(void); uint8_t serial_recv(void);
int16_t serial_recv2(void);
void serial_send(uint8_t data); void serial_send(uint8_t data);
#endif #endif

View File

@ -48,23 +48,6 @@ POSSIBILITY OF SUCH DAMAGE.
#define WAIT_US (1000000/SERIAL_BAUD) #define WAIT_US (1000000/SERIAL_BAUD)
#if 1
#define WAIT_TICK (1000000/SERIAL_BAUD)
#define WAIT4(tick) _delay_us(tick)
#else
#define WAIT_TICK ((16000000/SERIAL_BAUD)/4 - 5)
static inline void WAIT4(uint8_t tick)
{
__asm__ __volatile__ (
"1: dec %0" "\n\t"
"nop" "\n\t"
"brne 1b"
:
: "r" (tick)
);
}
#endif
void serial_init(void) void serial_init(void)
{ {
SERIAL_RXD_INIT(); SERIAL_RXD_INIT();
@ -90,6 +73,18 @@ uint8_t serial_recv(void)
return data; return data;
} }
int16_t serial_recv2(void)
{
uint8_t data = 0;
if (rbuf_head == rbuf_tail) {
return -1;
}
data = rbuf[rbuf_tail];
rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
return data;
}
void serial_send(uint8_t data) void serial_send(uint8_t data)
{ {
/* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */ /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */
@ -139,13 +134,11 @@ PORTD ^= 1<<7;
#endif #endif
/* to center of start bit */ /* to center of start bit */
//_delay_us(WAIT_US/2); _delay_us(WAIT_US/2);
WAIT4(WAIT_TICK/2);
PORTD ^= 1<<7; PORTD ^= 1<<7;
do { do {
/* to center of next bit */ /* to center of next bit */
//_delay_us(WAIT_US); _delay_us(WAIT_US);
WAIT4(WAIT_TICK);
PORTD ^= 1<<7; PORTD ^= 1<<7;
if (SERIAL_RXD_READ()) { if (SERIAL_RXD_READ()) {
@ -160,18 +153,15 @@ PORTD ^= 1<<7;
} while (mask); } while (mask);
/* to center of parity bit */ /* to center of parity bit */
//_delay_us(WAIT_US); _delay_us(WAIT_US);
WAIT4(WAIT_TICK);
if (SERIAL_RXD_READ()) { parity ^= 1; } if (SERIAL_RXD_READ()) { parity ^= 1; }
PORTD ^= 1<<7; PORTD ^= 1<<7;
/* to center of stop bit */ /* to center of stop bit */
//_delay_us(WAIT_US); _delay_us(WAIT_US);
WAIT4(WAIT_TICK);
uint8_t next = (rbuf_head + 1) % RBUF_SIZE; uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
//if (parity && next != rbuf_tail) { if (parity && next != rbuf_tail) {
if (next != rbuf_tail) {
rbuf[rbuf_head] = data; rbuf[rbuf_head] = data;
rbuf_head = next; rbuf_head = next;
} }