- fixed ports
- added power switch and command - temporarly disabled command and ADC event git-svn-id: http://moon:8086/svn/projects/HendiControl@23 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
* Author : jens
|
* Author : jens
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -19,9 +20,12 @@
|
|||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
#include "mcp42x6.h"
|
#include "mcp42x6.h"
|
||||||
|
|
||||||
#define ARDUINO_NANO
|
#define ARDUINO_NANO 1
|
||||||
|
#define WITH_ADC_EVENTS 0
|
||||||
|
#define WITH_COMMAND_EVENTS 0
|
||||||
|
|
||||||
#ifdef ARDUINO_NANO
|
#define PRINT_PROMPT printf("\n:")
|
||||||
|
#if ARDUINO_NANO
|
||||||
#define F_CPU (16000000UL) // MHz
|
#define F_CPU (16000000UL) // MHz
|
||||||
#else
|
#else
|
||||||
#define F_CPU (18432000UL) // MHz
|
#define F_CPU (18432000UL) // MHz
|
||||||
@@ -37,10 +41,10 @@ static Fifo messageFifo;
|
|||||||
enum MessageCode
|
enum MessageCode
|
||||||
{
|
{
|
||||||
NOP=0,
|
NOP=0,
|
||||||
Command,
|
|
||||||
Uart,
|
Uart,
|
||||||
Timer,
|
Timer,
|
||||||
AdcComplete,
|
AdcComplete,
|
||||||
|
Command,
|
||||||
Error = 0xFF
|
Error = 0xFF
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -52,6 +56,7 @@ typedef struct _sMsg_t
|
|||||||
|
|
||||||
ISR(ADC_vect)
|
ISR(ADC_vect)
|
||||||
{
|
{
|
||||||
|
#if WITH_ADC_EVENTS
|
||||||
int16_t value_low = (int16_t)ADCL;
|
int16_t value_low = (int16_t)ADCL;
|
||||||
int16_t value_high = (int16_t)ADCH;
|
int16_t value_high = (int16_t)ADCH;
|
||||||
ADCSRA |= (1<<ADIF);
|
ADCSRA |= (1<<ADIF);
|
||||||
@@ -70,11 +75,13 @@ ISR(ADC_vect)
|
|||||||
adc_ch = 0;
|
adc_ch = 0;
|
||||||
}
|
}
|
||||||
adc_start_conversion(adc_ch);
|
adc_start_conversion(adc_ch);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(USART_RX_vect)
|
ISR(USART_RX_vect)
|
||||||
{
|
{
|
||||||
uint8_t rx_data = UDR0;
|
uint8_t rx_data = UDR0;
|
||||||
|
#if WITH_COMMAND_EVENTS
|
||||||
if (rx_data == ' ')
|
if (rx_data == ' ')
|
||||||
{
|
{
|
||||||
Msg_t msg = {Error, rx_data};
|
Msg_t msg = {Error, rx_data};
|
||||||
@@ -85,6 +92,7 @@ ISR(USART_RX_vect)
|
|||||||
Msg_t msg = {Command, rx_data};
|
Msg_t msg = {Command, rx_data};
|
||||||
fifo_push(&messageFifo, &msg);
|
fifo_push(&messageFifo, &msg);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
Msg_t msg = {Uart, rx_data};
|
Msg_t msg = {Uart, rx_data};
|
||||||
fifo_push(&messageFifo, &msg);
|
fifo_push(&messageFifo, &msg);
|
||||||
}
|
}
|
||||||
@@ -105,6 +113,7 @@ ISR (TIMER1_OVF_vect) // Timer1 ISR
|
|||||||
FILE uart_file = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
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");
|
fifo_init(&messageFifo, 64, sizeof(Msg_t), "Fifo");
|
||||||
|
|
||||||
@@ -113,9 +122,10 @@ int main(void)
|
|||||||
timer_init(TIMER_RELOAD);
|
timer_init(TIMER_RELOAD);
|
||||||
adc_init();
|
adc_init();
|
||||||
i2c_init();
|
i2c_init();
|
||||||
|
port_init();
|
||||||
|
|
||||||
stdout = &uart_file;
|
stdout = &uart_file;
|
||||||
printf("UART test version 1.0\n\n");
|
printf("\nHendi Control v1.0-beta\n");
|
||||||
|
|
||||||
uint8_t dac_cmd[] = {0x00, 0x00, 0x00}; // 6.1 Write Volatile DAC Register (C2:C0 = ‘00x’)
|
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));
|
size_t res = MCP47x6_write_volatile_mem(0x0000, dac_cmd, sizeof(dac_cmd));
|
||||||
@@ -126,6 +136,7 @@ int main(void)
|
|||||||
|
|
||||||
char cmdStr[16];
|
char cmdStr[16];
|
||||||
size_t cmdStrSize = 0;
|
size_t cmdStrSize = 0;
|
||||||
|
PRINT_PROMPT;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
Msg_t msg;
|
Msg_t msg;
|
||||||
@@ -139,18 +150,37 @@ int main(void)
|
|||||||
{
|
{
|
||||||
case Uart:
|
case Uart:
|
||||||
{
|
{
|
||||||
|
// Process Uart message
|
||||||
cmdStr[cmdStrSize] = 0;
|
cmdStr[cmdStrSize] = 0;
|
||||||
if (cmdStrSize < sizeof(cmdStr))
|
if (cmdStrSize < sizeof(cmdStr))
|
||||||
{
|
{
|
||||||
char c = msg.data;
|
char c = msg.data;
|
||||||
|
|
||||||
|
// Command finished with CR/LF
|
||||||
if (c == 0x0A || c == 0x0D)
|
if (c == 0x0A || c == 0x0D)
|
||||||
{
|
{
|
||||||
printf("Command : %s\n", cmdStr);
|
// Power Control
|
||||||
|
if (toupper(cmdStr[0]) == 'P')
|
||||||
|
{
|
||||||
|
uint16_t arg = atol(cmdStr+1);
|
||||||
|
uint8_t dac_cmd[] = {0x00, 0x00}; // 6.2 Write Volatile Memory (C2:C0 = ‘010’)
|
||||||
|
size_t res = MCP47x6_write_volatile_dac(arg, dac_cmd, sizeof(dac_cmd));
|
||||||
|
i2c_send(0x60, I2C_WRITE, dac_cmd, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch Control
|
||||||
|
if (toupper(cmdStr[0]) == 'S')
|
||||||
|
{
|
||||||
|
uint16_t arg = atol(cmdStr+1);
|
||||||
|
portSet(PwrSwitch, arg != 0);
|
||||||
|
}
|
||||||
cmdStrSize = 0;
|
cmdStrSize = 0;
|
||||||
|
PRINT_PROMPT;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cmdStr[cmdStrSize++] = c;
|
cmdStr[cmdStrSize++] = c;
|
||||||
|
printf("%c", c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -187,17 +217,16 @@ int main(void)
|
|||||||
switch(msg.data)
|
switch(msg.data)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
DDRB = (DDRB & 0xFC) | 0x00;
|
PORTB = (PORTB & 0xFC) | 0x00;
|
||||||
printf("----------- Timer -----------\n");
|
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
DDRB = (DDRB & 0xFC) | 0x01;
|
PORTB = (PORTB & 0xFC) | 0x01;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
DDRB = (DDRB & 0xFC) | 0x03;
|
PORTB = (PORTB & 0xFC) | 0x03;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
DDRB = (DDRB & 0xFC) | 0x02;
|
PORTB = (PORTB & 0xFC) | 0x02;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -10,7 +10,47 @@
|
|||||||
|
|
||||||
void port_init()
|
void port_init()
|
||||||
{
|
{
|
||||||
|
DDRB = 0x03;
|
||||||
|
DDRC = 0x01;
|
||||||
PORTB = PORTB | 0x03;
|
PORTB = PORTB | 0x03;
|
||||||
DDRB = DDB1 | DDB2;
|
PORTC = PORTC | 0x01;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct _sPort_t
|
||||||
|
{
|
||||||
|
uint8_t port;
|
||||||
|
uint8_t pin;
|
||||||
|
uint8_t pol;
|
||||||
|
} Port_t;
|
||||||
|
|
||||||
|
const Port_t portItems[NUM_ITEMS] =
|
||||||
|
{
|
||||||
|
{0x05, 0, 1},
|
||||||
|
{0x05, 1, 1},
|
||||||
|
{0x05, 2, 1},
|
||||||
|
{0x08, 0, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
void portSet(int portItem, int enable)
|
||||||
|
{
|
||||||
|
uint8_t addr = portItems[portItem].port;
|
||||||
|
uint8_t pin_mask = 1 << portItems[portItem].pin;
|
||||||
|
|
||||||
|
if (enable ^ portItems[portItem].pol)
|
||||||
|
{
|
||||||
|
_SFR_IO8(addr) |= pin_mask;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_SFR_IO8(addr) &= ~pin_mask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int portGet(int portItem)
|
||||||
|
{
|
||||||
|
uint8_t addr = portItems[portItem].port;
|
||||||
|
uint8_t pin_mask = 1 << portItems[portItem].pin;
|
||||||
|
int enable = (_SFR_IO8(addr) & pin_mask) != portItems[portItem].pol;
|
||||||
|
return enable;
|
||||||
|
}
|
||||||
@@ -9,6 +9,17 @@
|
|||||||
#ifndef PORT_H_
|
#ifndef PORT_H_
|
||||||
#define PORT_H_
|
#define PORT_H_
|
||||||
|
|
||||||
|
enum PortItem
|
||||||
|
{
|
||||||
|
Led0 = 0,
|
||||||
|
Led1 = 1,
|
||||||
|
Led2 = 2,
|
||||||
|
PwrSwitch = 3,
|
||||||
|
NUM_ITEMS
|
||||||
|
};
|
||||||
|
|
||||||
void port_init();
|
void port_init();
|
||||||
|
void portSet(int portItem, int enable);
|
||||||
|
int portGet(int portItem);
|
||||||
|
|
||||||
#endif /* PORT_H_ */
|
#endif /* PORT_H_ */
|
||||||
Reference in New Issue
Block a user