- increased I2C bus speed
- added message Fifo git-svn-id: http://moon:8086/svn/projects/HendiControl@12 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#define ARDUINO_NANO
|
#define ARDUINO_NANO
|
||||||
@@ -35,6 +37,89 @@ enum MessageCode
|
|||||||
Error = 0xFF
|
Error = 0xFF
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct _sMsg_t
|
||||||
|
{
|
||||||
|
uint8_t code;
|
||||||
|
uint8_t data;
|
||||||
|
} Msg_t;
|
||||||
|
|
||||||
|
typedef struct _sFifo
|
||||||
|
{
|
||||||
|
void **ppData;
|
||||||
|
size_t capacity;
|
||||||
|
size_t itemSize;
|
||||||
|
size_t fill;
|
||||||
|
size_t wi;
|
||||||
|
size_t ri;
|
||||||
|
const char *pName;
|
||||||
|
} Fifo;
|
||||||
|
|
||||||
|
static Fifo messageFifo;
|
||||||
|
|
||||||
|
void Fifo_init(Fifo *pObj, size_t capacity, size_t itemSize, const char *pName)
|
||||||
|
{
|
||||||
|
pObj->wi = 0;
|
||||||
|
pObj->ri = 0;
|
||||||
|
pObj->fill = 0;
|
||||||
|
pObj->capacity = capacity;
|
||||||
|
pObj->itemSize = itemSize;
|
||||||
|
pObj->ppData = (void**)malloc(capacity);
|
||||||
|
|
||||||
|
for (int i=0; i < capacity; i++)
|
||||||
|
{
|
||||||
|
pObj->ppData[i] = (void*)malloc(itemSize);
|
||||||
|
}
|
||||||
|
pObj->pName = pName;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Fifo_free(Fifo *pObj)
|
||||||
|
{
|
||||||
|
for (int i=0; i < pObj->capacity; i++)
|
||||||
|
{
|
||||||
|
free(pObj->ppData[i]);
|
||||||
|
}
|
||||||
|
free (pObj->ppData);
|
||||||
|
pObj->itemSize = 0;
|
||||||
|
pObj->capacity = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Fifo_push(Fifo *pObj, void *pItem)
|
||||||
|
{
|
||||||
|
if (pObj->fill >= pObj->capacity)
|
||||||
|
{
|
||||||
|
printf("%s: Fifo is full\n", pObj->pName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memcpy(pObj->ppData[pObj->wi], pItem, pObj->itemSize);
|
||||||
|
pObj->wi++;
|
||||||
|
pObj->fill++;
|
||||||
|
if (pObj->wi == pObj->capacity)
|
||||||
|
{
|
||||||
|
pObj->wi = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Fifo_pop(Fifo *pObj, void *pItem)
|
||||||
|
{
|
||||||
|
if (!pObj->fill)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
memcpy(pItem, pObj->ppData[pObj->ri], pObj->itemSize);
|
||||||
|
pObj->ri++;
|
||||||
|
pObj->fill--;
|
||||||
|
if (pObj->ri == pObj->capacity)
|
||||||
|
{
|
||||||
|
pObj->ri = 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Fifo_isEmpty(Fifo *pObj)
|
||||||
|
{
|
||||||
|
return pObj->fill == 0;
|
||||||
|
}
|
||||||
|
|
||||||
ISR(ADC_vect)
|
ISR(ADC_vect)
|
||||||
{
|
{
|
||||||
int16_t value_low = (int16_t)ADCL;
|
int16_t value_low = (int16_t)ADCL;
|
||||||
@@ -44,8 +129,8 @@ ISR(ADC_vect)
|
|||||||
|
|
||||||
if (((adc_value[adc_ch] - value) < -1) || ((adc_value[adc_ch] - value) > 1))
|
if (((adc_value[adc_ch] - value) < -1) || ((adc_value[adc_ch] - value) > 1))
|
||||||
{
|
{
|
||||||
msgCode = AdcComplete;
|
Msg_t msg = {AdcComplete, adc_ch};
|
||||||
msgData = adc_ch;
|
Fifo_push(&messageFifo, &msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
adc_value[adc_ch] = value;
|
adc_value[adc_ch] = value;
|
||||||
@@ -68,15 +153,17 @@ ISR(USART_RX_vect)
|
|||||||
}
|
}
|
||||||
else if (rx_data >= '0' && rx_data <= '9')
|
else if (rx_data >= '0' && rx_data <= '9')
|
||||||
{
|
{
|
||||||
msgCode = Command;
|
Msg_t msg = {Command, rx_data};
|
||||||
msgData = rx_data;
|
Fifo_push(&messageFifo, &msg);
|
||||||
}
|
}
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR (TIMER1_OVF_vect) // Timer1 ISR
|
ISR (TIMER1_OVF_vect) // Timer1 ISR
|
||||||
{
|
{
|
||||||
msgCode = Timer;
|
Msg_t msg = {Timer, ledState};
|
||||||
|
Fifo_push(&messageFifo, &msg);
|
||||||
|
|
||||||
TCNT1 = TIMER_RELOAD;
|
TCNT1 = TIMER_RELOAD;
|
||||||
ledState++;
|
ledState++;
|
||||||
if (ledState == 4)
|
if (ledState == 4)
|
||||||
@@ -126,7 +213,7 @@ enum I2C
|
|||||||
void i2c_init()
|
void i2c_init()
|
||||||
{
|
{
|
||||||
TWBR = 0xFF;
|
TWBR = 0xFF;
|
||||||
TWSR = 0x03;
|
TWSR = 0x00;
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_send(uint8_t addr, uint8_t rw, uint8_t *data, size_t size)
|
void i2c_send(uint8_t addr, uint8_t rw, uint8_t *data, size_t size)
|
||||||
@@ -259,6 +346,7 @@ FILE uart_file = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
Fifo_init(&messageFifo, 64, sizeof(Msg_t), "Fifo");
|
||||||
|
|
||||||
/* Replace with your application code */
|
/* Replace with your application code */
|
||||||
uart_init();
|
uart_init();
|
||||||
@@ -276,25 +364,24 @@ int main(void)
|
|||||||
adc_start_conversion(adc_ch);
|
adc_start_conversion(adc_ch);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
Msg_t msg;
|
||||||
if (msgCode)
|
if (!Fifo_isEmpty(&messageFifo))
|
||||||
{
|
{
|
||||||
cli();
|
cli();
|
||||||
uint8_t _msgCode = msgCode;
|
Fifo_pop(&messageFifo, &msg);
|
||||||
uint8_t _msgData = msgData;
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
switch(_msgCode)
|
switch(msg.code)
|
||||||
{
|
{
|
||||||
case Command:
|
case Command:
|
||||||
printf("Command : %c\n", _msgData);
|
printf("Command : %c\n", msg.data);
|
||||||
uint8_t write_dac[] = {0x00+(_msgData-'0'), 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’)
|
uint8_t write_dac[] = {0x00+(msg.data-'0'), 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’)
|
||||||
i2c_send(0x60, I2C_WRITE, write_dac, sizeof(write_dac));
|
i2c_send(0x60, I2C_WRITE, write_dac, sizeof(write_dac));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AdcComplete:
|
case AdcComplete:
|
||||||
{
|
{
|
||||||
size_t ch = _msgData;
|
size_t ch = msg.data;
|
||||||
if (ch == 6)
|
if (ch == 6)
|
||||||
{
|
{
|
||||||
printf("ADC=%d\n", adc_value[ch]);
|
printf("ADC=%d\n", adc_value[ch]);
|
||||||
@@ -303,7 +390,7 @@ int main(void)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case Timer:
|
case Timer:
|
||||||
switch(ledState)
|
switch(msg.data)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
DDRB = (DDRB & 0xFC) | 0x00;
|
DDRB = (DDRB & 0xFC) | 0x00;
|
||||||
|
|||||||
Reference in New Issue
Block a user