stm32和外設(shè)通信的時(shí)候几于,需要對(duì)外設(shè)發(fā)來的串行數(shù)據(jù)做同步潭流。參考過下面這個(gè)鏈接的方法:串口通信幀的同步方法(識(shí)別一幀數(shù)據(jù)的起始結(jié)束)
- FIFO隊(duì)列的幀同步方法贷祈,比較簡單滓侍,準(zhǔn)確度又高狈醉。
/**
* USART2_IRQHandler
*/
void USART2_IRQHandler(void)
{
uint8_t value = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
value = USART_ReceiveData(USART2);
if(1 == syncFlag)
{
if(cnt <= 7)
{
pm_buf[cnt++] = value;
}
else
{
cnt = 0;
syncFlag = 0;
syncHead[1] = 0xFF;
syncHead[0] = 0xFF;
}
}
else
{
//Syncing......
syncHead[1] = syncHead[0];
syncHead[0] = value;
if(syncHead[1] == 0xAA && syncHead[0] == 0xC0)
syncFlag = 1;
}
}
}
然而這樣做有個(gè)問題廉油。中斷服務(wù)程序需要做大量的工作,在這短暫的時(shí)間中苗傅,就可能會(huì)丟失串口發(fā)來的數(shù)據(jù)抒线,因?yàn)榇诘臄?shù)據(jù)是源源不斷的。所以應(yīng)該需要一個(gè)類似緩沖區(qū)的東西渣慕。這樣的話嘶炭,IRQHandler只負(fù)責(zé)相應(yīng)的數(shù)據(jù),而中斷服務(wù)程序去完成真正的操作逊桦。
在查看很多文檔之后眨猎,發(fā)現(xiàn)這個(gè)就歸屬于經(jīng)典的生產(chǎn)者和消費(fèi)者問題。我認(rèn)為FIFO隊(duì)列和循環(huán)緩沖區(qū)應(yīng)該是一種互補(bǔ)的關(guān)系强经。
緩沖區(qū)的價(jià)值在于能使得生產(chǎn)者產(chǎn)生的數(shù)據(jù)不至于零散地只能接收到其中的一部分睡陪,比如AA C0 11 22 33 44 55 66,在AA C0同步幀同步完之后匿情,不會(huì)說只收到了11 22 33 55 66兰迫,這就完全錯(cuò)掉了。
-
如果有緩沖區(qū)炬称,像一個(gè)隊(duì)列一樣汁果,先進(jìn)先出,讀寫互相不干擾玲躯,寫優(yōu)先級(jí)高于讀据德,如果緩沖區(qū)滿了鳄乏,就只好丟棄新產(chǎn)生的數(shù)據(jù)。
- 在我看來覆蓋老數(shù)據(jù)會(huì)有無法覆蓋同步的風(fēng)險(xiǎn)棘利,比如橱野,你不能保證剛好從同步幀頭那里覆蓋。因?yàn)橐WC設(shè)備產(chǎn)生AA C0 的時(shí)候赡译,剛好覆蓋到緩沖區(qū)的AA C0仲吏,這樣的工作量想必不小。
- 如果真的可能的話蝌焚,從哪里覆蓋最老的數(shù)據(jù)是合適的呢裹唆?消費(fèi)者正在讀數(shù)據(jù),總不能把正要讀的數(shù)據(jù)給抹掉吧只洒?
- 而且覆蓋了老數(shù)據(jù)许帐,會(huì)產(chǎn)生時(shí)間線混亂的問題,本來隊(duì)列是從隊(duì)列頭到隊(duì)列尾是老數(shù)據(jù)>>>>新數(shù)據(jù)毕谴,這樣一來成畦,完全被破壞了。
用了循環(huán)緩沖區(qū)之后涝开,中斷里面就換成下面這樣的操作方式了循帐,中斷只是簡單地向循環(huán)緩沖區(qū)寫入數(shù)據(jù)-如果可寫的話。
/**
* USART2_IRQHandler
*/
void USART2_IRQHandler(void)
{
uint8_t value = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
value = USART_ReceiveData(USART2);
if(rb_can_write(&u_ring_buff) > 0)
{
rb_write(&u_ring_buff, &value, 1);
//printf("Interrupt\n\r");
}
}
}
- 參考的循環(huán)緩沖區(qū)實(shí)現(xiàn)的代碼:
#define min(a, b) (a)<(b)?(a):(b)
void rb_new(RingBuffer* rb)
{
//RingBuffer *rb = (RingBuffer *)buff;//malloc(sizeof(RingBuffer) + capacity);
//if (rb == NULL) return NULL;
rb->rb_capacity = MAX_RINGBUFFER_LEN;//-sizeof(RingBuffer);//capacity;
//rb->rb_buff = buff+sizeof(RingBuffer);//(char*)rb + sizeof(RingBuffer);
rb->rb_head = rb->rb_buff;
rb->rb_tail = rb->rb_buff;
//return rb;
};
void rb_free(RingBuffer *rb)
{
//free((char*)rb);
}
size_t rb_capacity(RingBuffer *rb)
{
//assert(rb != NULL);
return rb->rb_capacity;
}
size_t rb_can_read(RingBuffer *rb)
{
//assert(rb != NULL);
if (rb->rb_head == rb->rb_tail) return 0;
if (rb->rb_head < rb->rb_tail) return rb->rb_tail - rb->rb_head;
return rb_capacity(rb) - (rb->rb_head - rb->rb_tail);
}
size_t rb_can_write(RingBuffer *rb)
{
//assert(rb != NULL);
return rb_capacity(rb) - rb_can_read(rb);
}
size_t rb_read(RingBuffer *rb, void *data, size_t count)
{
//assert(rb != NULL);
//assert(data != NULL);
if (rb->rb_head < rb->rb_tail)
{
int copy_sz = min(count, rb_can_read(rb));
memcpy(data, rb->rb_head, copy_sz);
rb->rb_head += copy_sz;
return copy_sz;
}
else
{
if (count < rb_capacity(rb)-(rb->rb_head - rb->rb_buff))
{
int copy_sz = count;
memcpy(data, rb->rb_head, copy_sz);
rb->rb_head += copy_sz;
return copy_sz;
}
else
{
int copy_sz = rb_capacity(rb) - (rb->rb_head - rb->rb_buff);
memcpy(data, rb->rb_head, copy_sz);
rb->rb_head = rb->rb_buff;
copy_sz += rb_read(rb, (char*)data+copy_sz, count-copy_sz);
return copy_sz;
}
}
}
size_t rb_write(RingBuffer *rb, const void *data, size_t count)
{
//assert(rb != NULL);
//assert(data != NULL);
if (count >= rb_can_write(rb))
return -1;
if (rb->rb_head <= rb->rb_tail)
{
int tail_avail_sz = rb_capacity(rb) - (rb->rb_tail - rb->rb_buff);
if (count <= tail_avail_sz)
{
memcpy(rb->rb_tail, data, count);
rb->rb_tail += count;
if (rb->rb_tail == rb->rb_buff+rb_capacity(rb))
rb->rb_tail = rb->rb_buff;
return count;
}
else
{
memcpy(rb->rb_tail, data, tail_avail_sz);
rb->rb_tail = rb->rb_buff;
return tail_avail_sz + rb_write(rb, (char*)data+tail_avail_sz, count-tail_avail_sz);
}
}
else
{
memcpy(rb->rb_tail, data, count);
rb->rb_tail += count;
return count;
}
}
typedef struct {
size_t rb_capacity;
char *rb_head;
char *rb_tail;
char rb_buff[64];
}RingBuffer;
//struct RingBuffer;
// RingBuffer* rb_new(size_t capacity);
void rb_new(RingBuffer* rb);
void rb_free(RingBuffer *rb);
size_t rb_capacity(RingBuffer *rb);
size_t rb_can_read(RingBuffer *rb);
size_t rb_can_write(RingBuffer *rb);
size_t rb_read(RingBuffer *rb, void *data, size_t count);
size_t rb_write(RingBuffer *rb, const void *data, size_t count);
- 獲取新數(shù)據(jù)幀的代碼:
static void GetFrame()
{
if(rb_can_read(&u_ring_buff) >= 1)
{
if(1 == syncFlag)
{
if(cnt <= 7)
{
rb_read(&u_ring_buff, &curValue, 1);
//printf("cnt:%d\t",cnt);
pm_buf[cnt++] = curValue;
//printf("Interrupt_value:%2X\r\n\r\n",curValue);
}
else
{
//memset(pm_buf, 0, 8);
cnt = 0;
syncFlag = 0;
syncHead[1] = 0xFF;
syncHead[0] = 0xFF;
}
}
else
{
//printf("Syncing......\r\n");
rb_read(&u_ring_buff, &curValue, 1);
syncHead[1] = syncHead[0];
syncHead[0] = curValue;
if(syncHead[1] == 0xAA && syncHead[0] == 0xC0)
syncFlag = 1;
}
}
}
用了循環(huán)緩沖區(qū)后舀武,發(fā)現(xiàn)用到一段時(shí)間后拄养,之前還能正常提取數(shù)據(jù)的數(shù)據(jù)幀,過了一段時(shí)間數(shù)據(jù)就亂掉了银舱,邏輯沒搞清楚嗎瘪匿?