/* * uart_echo.c * * Created: 16.08.2018 17:39:49 * Author : jens */ #include #include #include #include #include #define ARDUINO_NANO #ifdef ARDUINO_NANO #define F_CPU (16000000UL) // MHz #else #define F_CPU (18432000UL) // MHz #endif #define USART_BAUDRATE 38400UL #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) #define TIMER_RELOAD (0xFFFF-(F_CPU/1024/4-1)) static uint8_t ledState = 0; static int16_t adc_value[16]; static size_t adc_ch = 0; enum MessageCode { NOP=0, Command, Timer, AdcComplete, 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; } void adc_start_conversion(int ch); ISR(ADC_vect) { int16_t value_low = (int16_t)ADCL; int16_t value_high = (int16_t)ADCH; ADCSRA |= (1< 1)) { Msg_t msg = {AdcComplete, adc_ch}; Fifo_push(&messageFifo, &msg); } adc_value[adc_ch] = value; adc_ch++; if (adc_ch == 16) { adc_ch = 0; } adc_start_conversion(adc_ch); } ISR(USART_RX_vect) { uint8_t rx_data = UDR0; if (rx_data == ' ') { Msg_t msg = {Error, rx_data}; Fifo_push(&messageFifo, &msg); } else if (rx_data >= '0' && rx_data <= '9') { Msg_t msg = {Command, rx_data}; Fifo_push(&messageFifo, &msg); } } ISR (TIMER1_OVF_vect) // Timer1 ISR { Msg_t msg = {Timer, ledState}; Fifo_push(&messageFifo, &msg); TCNT1 = TIMER_RELOAD; ledState++; if (ledState == 4) { ledState = 0; } } void uart_init() { // Set baud rate UBRR0L = (uint8_t)BAUD_PRESCALE; UBRR0H = (uint8_t)(BAUD_PRESCALE >> 8); // Enable receiver and transmitter UCSR0B = (1<> 8); *(buffer++) = (uint8_t)(dac_value >> 0); return 2; } size_t MCP47x6_write_volatile_mem(uint16_t dac_value, uint8_t *buffer, size_t size) { *(buffer++) = mask & 0x40; dac_value <<= 4; *(buffer++) = (uint8_t)(dac_value >> 8); *(buffer++) = (uint8_t)(dac_value >> 0); return 3; } int main(void) { Fifo_init(&messageFifo, 64, sizeof(Msg_t), "Fifo"); /* Replace with your application code */ uart_init(); timer_init(); adc_init(); i2c_init(); stdout = &uart_file; printf("UART test version 1.0\n\n"); uint8_t dac_cmd[] = {0x00, 0x00, 0x00}; // 6.1 Write Volatile DAC Register (C2:C0 = ‘00x’) size_t res = MCP47x6_write_volatile_mem(0x0000, dac_cmd, sizeof(dac_cmd)); i2c_send(0x60, I2C_WRITE, dac_cmd, res); sei(); adc_start_conversion(adc_ch); while (1) { Msg_t msg; if (!Fifo_isEmpty(&messageFifo)) { cli(); Fifo_pop(&messageFifo, &msg); sei(); switch(msg.code) { case Command: { printf("Command : %c\n", msg.data); uint8_t dac_cmd[] = {0x00, 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’) size_t res = MCP47x6_write_volatile_dac((uint16_t)(msg.data-'0')<<8, dac_cmd, sizeof(dac_cmd)); i2c_send(0x60, I2C_WRITE, dac_cmd, res); } break; case AdcComplete: { size_t ch = msg.data; if (ch == 6) { printf("ADC=%d\n", adc_value[ch]); uint8_t dac_cmd[] = {0x00, 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’) size_t res = MCP47x6_write_volatile_dac(adc_value[ch]<<2, dac_cmd, sizeof(dac_cmd)); i2c_send(0x60, I2C_WRITE, dac_cmd, res); } } break; case Timer: switch(msg.data) { case 0: DDRB = (DDRB & 0xFC) | 0x00; printf("----------- Timer -----------\n"); break; case 1: DDRB = (DDRB & 0xFC) | 0x01; break; case 2: DDRB = (DDRB & 0xFC) | 0x03; break; case 3: DDRB = (DDRB & 0xFC) | 0x02; break; } break; case Error: printf("Error!!!\n"); break; } } } }