Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file megaio.c \brief MegaIO Control/Access function library. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'megaio.c' |
||
5 | // Title : MegaIO Control/Access function library |
||
6 | // Author : Pascal Stang - Copyright (C) 2004 |
||
7 | // Created : 5/18/2004 |
||
8 | // Revised : 5/18/2004 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // This code is distributed under the GNU Public License |
||
14 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
15 | // |
||
16 | //***************************************************************************** |
||
17 | |||
18 | #include "buffer.h" // include buffer support |
||
19 | #include "i2c.h" // include I2C functions |
||
20 | #include "megaio/megaioreg.h" // include MegaIO register definitions |
||
21 | |||
22 | #include "megaio.h" |
||
23 | |||
24 | // MegaIO local receive buffer size |
||
25 | #define MEGAIO_UART_RX_BUFFER_SIZE 0x80 |
||
26 | // MegaIO local receive buffer data array |
||
27 | static char megaioUartRxData[MEGAIO_UART_RX_BUFFER_SIZE]; |
||
28 | // MegaIO local receive buffer |
||
29 | cBuffer megaioUartRxBuffer; |
||
30 | |||
31 | //! initialize the MegaIO interface |
||
32 | u08 megaioInit(void) |
||
33 | { |
||
34 | // initialize the UART receive buffer |
||
35 | bufferInit(&megaioUartRxBuffer, megaioUartRxData, MEGAIO_UART_RX_BUFFER_SIZE); |
||
36 | // initialize i2c interface |
||
37 | i2cInit(); |
||
38 | i2cSetBitrate(30); |
||
39 | // check for presence of megaio chip |
||
40 | if( megaioReadReg(MEGAIOREG_IDSTRING, 1) == 'M' ) |
||
41 | { |
||
42 | // megaio responded correctly |
||
43 | // initialization succeeded |
||
44 | return TRUE; |
||
45 | } |
||
46 | else |
||
47 | { |
||
48 | // megaio responded incorrectly |
||
49 | // initialization failed |
||
50 | return FALSE; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | //! write an 8-32 bit number to a MegaIO register |
||
55 | void megaioWriteReg(unsigned char regnum, unsigned char nbytes, unsigned long data) |
||
56 | { |
||
57 | u08 packet[5]; |
||
58 | |||
59 | // construct I2c data packet |
||
60 | // first byte is register address |
||
61 | // following bytes are the data that will be written to that register |
||
62 | packet[0] = regnum; |
||
63 | packet[1] = data; |
||
64 | packet[2] = data>>8; |
||
65 | packet[3] = data>>16; |
||
66 | packet[4] = data>>24; |
||
67 | // send 2 bytes (register and data) to MegaIO |
||
68 | i2cMasterSend(MEGAIO_I2C_ADDR, 1+nbytes, packet); |
||
69 | } |
||
70 | |||
71 | //! read an 8-32 bit number from a MegaIO register |
||
72 | unsigned long megaioReadReg(unsigned char regnum, unsigned char nbytes) |
||
73 | { |
||
74 | unsigned long data = 0; |
||
75 | |||
76 | // first select the register by writing 1 byte (register) |
||
77 | i2cMasterSend(MEGAIO_I2C_ADDR, 1, ®num); |
||
78 | // then read n byte(s) from the selected MegaIO register |
||
79 | i2cMasterReceive(MEGAIO_I2C_ADDR, nbytes, (u08*)&data); |
||
80 | // return the results |
||
81 | return data; |
||
82 | } |
||
83 | |||
84 | //! set the baudrate of the megaio serial port |
||
85 | void megaioSetBaudRate(u32 baudrate) |
||
86 | { |
||
87 | megaioWriteReg(MEGAIOREG_UARTBAUD, 4, baudrate); |
||
88 | } |
||
89 | |||
90 | //! send a byte out the megaio serial port |
||
91 | void megaioSendByte(u08 data) |
||
92 | { |
||
93 | megaioWriteReg(MEGAIOREG_UARTDATA, 1, data); |
||
94 | } |
||
95 | |||
96 | //! get a byte from the megaio serial port |
||
97 | int megaioGetByte(void) |
||
98 | { |
||
99 | u08 data; |
||
100 | |||
101 | // check the number of bytes in the megaio receive buffer |
||
102 | if( megaioReadReg(MEGAIOREG_UARTRXBUFBYTES, 1) ) |
||
103 | { |
||
104 | // one or more bytes are available |
||
105 | // get first byte |
||
106 | data = megaioReadReg(MEGAIOREG_UARTDATA, 1); |
||
107 | return data; |
||
108 | } |
||
109 | else |
||
110 | { |
||
111 | // no bytes were available |
||
112 | // (no bytes have arrived and are waiting to be read) |
||
113 | return -1; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | //! returns the receive buffer structure |
||
118 | cBuffer* megaioGetRxBuffer(void) |
||
119 | { |
||
120 | u08 nbytes; |
||
121 | // get the number of bytes waiting in the MegaIO buffer |
||
122 | nbytes = megaioReadReg(MEGAIOREG_UARTRXBUFBYTES, 1); |
||
123 | // get all available bytes from the MegaIO chip |
||
124 | // and add them to the receive buffer |
||
125 | while(megaioReadReg(MEGAIOREG_UARTRXBUFBYTES, 1)) |
||
126 | { |
||
127 | bufferAddToEnd(&megaioUartRxBuffer, megaioReadReg(MEGAIOREG_UARTDATA, 1)); |
||
128 | nbytes--; |
||
129 | } |
||
130 | // return rx buffer pointer |
||
131 | return &megaioUartRxBuffer; |
||
132 | } |
||
133 | |||
134 | //! turn on megaio PWM and set for bitRes resolution |
||
135 | void megaioPWMInit(u08 bitRes) |
||
136 | { |
||
137 | megaioWriteReg(MEGAIOREG_PWM1CTRL, 1, bitRes); |
||
138 | } |
||
139 | |||
140 | //! turn off megaio PWM |
||
141 | void megaioPWMOff(void) |
||
142 | { |
||
143 | megaioWriteReg(MEGAIOREG_PWM1CTRL, 1, 0); |
||
144 | } |
||
145 | |||
146 | //! set megaio PWM1A duty cycle |
||
147 | void megaioPWMASet(u16 pwmDuty) |
||
148 | { |
||
149 | megaioWriteReg(MEGAIOREG_PWM1ADUTY, 2, pwmDuty); |
||
150 | } |
||
151 | |||
152 | //! set megaio PWM1B duty cycle |
||
153 | void megaioPWMBSet(u16 pwmDuty) |
||
154 | { |
||
155 | megaioWriteReg(MEGAIOREG_PWM1BDUTY, 2, pwmDuty); |
||
156 | } |
||
157 | |||
158 | //! set megaio prescaler division rate |
||
159 | void megaioSetPrescaler(u08 prescaleDiv) |
||
160 | { |
||
161 | megaioWriteReg(MEGAIOREG_PWM1FREQ, 1, prescaleDiv); |
||
162 | } |
||
163 | |||
164 | //! do A/D conversion on channel [ch] and return result |
||
165 | u16 megaioA2DConvert(u08 ch) |
||
166 | { |
||
167 | // set channel |
||
168 | megaioWriteReg(MEGAIOREG_ADCCHSEL, 1, ch); |
||
169 | // start single conversion |
||
170 | megaioWriteReg(MEGAIOREG_ADCCTRL, 1, 0x01); |
||
171 | // wait for conversion to be complete |
||
172 | while( megaioReadReg(MEGAIOREG_ADCCTRL, 1) ); |
||
173 | // get result and return it |
||
174 | return megaioReadReg(MEGAIOREG_ADCRESULT, 2); |
||
175 | } |
Powered by WebSVN v2.8.3