- introduced enterCritical, exitCritial

git-svn-id: http://moon:8086/svn/projects/HendiControl@36 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-03-02 14:28:06 +00:00
parent a94413670a
commit c092db8909
5 changed files with 89 additions and 41 deletions
+23 -5
View File
@@ -36,10 +36,14 @@ void fifo_free(Fifo *pObj)
} }
int fifo_push(Fifo *pObj, void *pItem) int fifo_push(Fifo *pObj, void *pItem)
{
int success = 0;
enterCritical();
do
{ {
if (pObj->fill >= pObj->capacity) if (pObj->fill >= pObj->capacity)
{ {
return 0; break;
} }
for (size_t i=0; i < pObj->itemSize; i++) for (size_t i=0; i < pObj->itemSize; i++)
{ {
@@ -51,14 +55,21 @@ int fifo_push(Fifo *pObj, void *pItem)
{ {
pObj->wi = 0; pObj->wi = 0;
} }
return 1; success = 1;
} while (0);
exitCritial();
return success;
} }
int fifo_pop(Fifo *pObj, void *pItem) int fifo_pop(Fifo *pObj, void *pItem)
{
int success = 0;
enterCritical();
do
{ {
if (!pObj->fill) if (!pObj->fill)
{ {
return 0; break;
} }
for (size_t i=0; i < pObj->itemSize; i++) for (size_t i=0; i < pObj->itemSize; i++)
{ {
@@ -70,11 +81,18 @@ int fifo_pop(Fifo *pObj, void *pItem)
{ {
pObj->ri = 0; pObj->ri = 0;
} }
return 1; success = 1;
} while(0);
exitCritial();
return success;
} }
int fifo_isEmpty(Fifo *pObj) int fifo_isEmpty(Fifo *pObj)
{ {
return pObj->fill == 0; enterCritical();
int isEmpty = (pObj->fill == 0);
exitCritial();
return isEmpty;
} }
+20
View File
@@ -10,6 +10,7 @@
#define FIFO_H_ #define FIFO_H_
#include <stdlib.h> #include <stdlib.h>
#include <avr/interrupt.h>
typedef struct _sFifo typedef struct _sFifo
{ {
@@ -28,4 +29,23 @@ int fifo_push(Fifo *pObj, void *pItem);
int fifo_pop(Fifo *pObj, void *pItem); int fifo_pop(Fifo *pObj, void *pItem);
int fifo_isEmpty(Fifo *pObj); int fifo_isEmpty(Fifo *pObj);
static volatile size_t criticalCount = 0;
static inline void enterCritical()
{
cli();
criticalCount++;
}
static inline void exitCritial()
{
if (criticalCount)
{
criticalCount--;
if (!criticalCount)
{
sei();
}
}
}
#endif /* FIFO_H_ */ #endif /* FIFO_H_ */
+5 -5
View File
@@ -123,10 +123,7 @@ int main(void)
Msg_t msg; Msg_t msg;
if (!fifo_isEmpty(&messageFifo)) if (!fifo_isEmpty(&messageFifo))
{ {
cli();
fifo_pop(&messageFifo, &msg); fifo_pop(&messageFifo, &msg);
sei();
switch(msg.code) switch(msg.code)
{ {
case Uart: case Uart:
@@ -135,6 +132,9 @@ int main(void)
cmdStr[cmdStrSize] = 0; cmdStr[cmdStrSize] = 0;
char c = msg.data; char c = msg.data;
putchar(c);
break;
// Command finished with CR/LF // Command finished with CR/LF
if (c == 0x0A || c == 0x0D) if (c == 0x0A || c == 0x0D)
{ {
@@ -147,7 +147,7 @@ int main(void)
if (state == StateNormal || state == StateRemote) if (state == StateNormal || state == StateRemote)
{ {
// Power Control // Power Control
if (toupper(cmdStr[0]) == 'P') if (toupper(cmdStr[0]) == 'Z')
{ {
uint16_t arg = (uint16_t)strtol(cmdStr+1, (char **)NULL, 10); uint16_t arg = (uint16_t)strtol(cmdStr+1, (char **)NULL, 10);
setPower(arg); setPower(arg);
@@ -170,7 +170,7 @@ int main(void)
{ {
if (cmdStrSize < (sizeof(cmdStr)-1)) if (cmdStrSize < (sizeof(cmdStr)-1))
{ {
printf("%c", c); putchar(c);
cmdStr[cmdStrSize++] = c; cmdStr[cmdStrSize++] = c;
} }
} }
+2 -2
View File
@@ -51,9 +51,9 @@ void timer_init(Fifo *pFifo, uint16_t TIMER_RELOAD, TimerClockSel clockSel)
void timer_start(TimerId timerId, uint16_t count) void timer_start(TimerId timerId, uint16_t count)
{ {
cli(); enterCritical();
user_timer[timerId].count = count; user_timer[timerId].count = count;
user_timer[timerId].isRunning = 1; user_timer[timerId].isRunning = 1;
sei(); exitCritial();
} }
+12 -2
View File
@@ -14,11 +14,21 @@
static Fifo *g_pFifo = NULL; static Fifo *g_pFifo = NULL;
ISR(USART_RX_vect) ISR(USART_RX_vect)
{
uint8_t status = UCSR0A;
if (status & (1 << RXC0))
{ {
uint8_t rx_data = UDR0; uint8_t rx_data = UDR0;
Msg_t msg = {Uart, rx_data}; Msg_t msg = {Uart, rx_data};
fifo_push(g_pFifo, &msg); fifo_push(g_pFifo, &msg);
} }
else
{
Msg_t msg = {NOP, 0};
fifo_push(g_pFifo, &msg);
}
}
void uart_init(Fifo *pFifo, uint16_t BAUD_PRESCALE) void uart_init(Fifo *pFifo, uint16_t BAUD_PRESCALE)
{ {
@@ -38,12 +48,12 @@ void uart_init(Fifo *pFifo, uint16_t BAUD_PRESCALE)
void uart_putc(char c) void uart_putc(char c)
{ {
cli(); enterCritical();
while((UCSR0A & (1<<UDRE0)) == 0) while((UCSR0A & (1<<UDRE0)) == 0)
{ {
} }
UDR0 = c; UDR0 = c;
sei(); exitCritial();
} }
int uart_putchar(char c, FILE *stream) int uart_putchar(char c, FILE *stream)