A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

alix-usv.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /***************************************************************************
  2. * Copyright (C) 01/2009 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/io.h>
  20. #include <avr/interrupt.h>
  21. #include "alix-usv.h"
  22. #include "eeprom.h"
  23. #include "usi-i2c-slave.h"
  24. extern volatile struct ee_param params;
  25. static const uint8_t adc_mux[] = {
  26. CH_CURRENT_P, CH_CURRENT_N,
  27. CH_VOLTAGE_BAT, CH_VOLTAGE_SUP,
  28. };
  29. static volatile uint8_t adc_update;
  30. static volatile int16_t adc_value[3];
  31. enum {
  32. ADC_CURRENT = 0,
  33. ADC_UBAT = 1,
  34. ADC_UIN = 2,
  35. };
  36. static volatile uint8_t sys_state;
  37. enum {
  38. STATE_IDLE = 0x01,
  39. STATE_TEST = 0x02,
  40. STATE_CHARGE = 0x04,
  41. STATE_DISCHARGE = 0x08,
  42. STATE_POWEROFF = 0x10,
  43. };
  44. static uint32_t adc_buf[4];
  45. ISR(ADC_vect)
  46. {
  47. static uint8_t cnt;
  48. /* use result of second conversion (switching ADC-gain needs time..) */
  49. if (cnt & 0x01) {
  50. /* moving average filter */
  51. uint8_t ch = (cnt >> 1);
  52. adc_buf[ch] = ((adc_buf[ch] * 7) + ((uint32_t)ADC << 8)) /8;
  53. uint16_t value = (adc_buf[ch] >> 8) + ((adc_buf[ch] & 0x80) ? 1 : 0);
  54. if (ch == 0 && (adc_buf[0] >= adc_buf[1])) {
  55. /* charging: positive current = ADC * 1.25 */
  56. adc_value[ADC_CURRENT] = value + (value >> 2);
  57. } else if (ch == 1 && (adc_buf[0] < adc_buf[1])) {
  58. /* discharging: negative current = ADC * -1.25 */
  59. adc_value[ADC_CURRENT] = -(value + (value >> 2));
  60. } else if (ch == 2) {
  61. /* Ubat = (ADC * 21.28) - (Ishunt * 0.1) */
  62. adc_value[ADC_UBAT] = (value * 21) + ((value * 9) >> 5) - (adc_value[ADC_CURRENT] / 10);
  63. } else if (ch == 3) {
  64. /* Uin = ADC * 21.28 */
  65. adc_value[ADC_UIN] = (value * 21) + ((value * 9) >> 5);
  66. /* all values updated */
  67. adc_update = 1;
  68. }
  69. }
  70. /* trigger next conversion */
  71. cnt = (cnt +1) & 0x07;
  72. ADMUX = adc_mux[cnt >> 1];
  73. ADCSRA |= (1<<ADSC);
  74. }
  75. static uint8_t i2c_reg;
  76. void usi_write(uint8_t data, uint8_t bcnt)
  77. {
  78. /* select register */
  79. if (bcnt == 0) {
  80. i2c_reg = data;
  81. return;
  82. }
  83. bcnt--;
  84. /* reg 0x00, lowbyte -> write control */
  85. if (i2c_reg == 0x00 && bcnt == 0x00) {
  86. /* poweroff is always allowed */
  87. if (data & STATE_POWEROFF)
  88. sys_state = data;
  89. /* discharge cmd is never allowed */
  90. else if (data & STATE_DISCHARGE)
  91. return;
  92. /* during discharge nothing is allowed */
  93. else if (sys_state != STATE_DISCHARGE)
  94. sys_state = data;
  95. /* register word 0x20 - 0x26 eeprom parameters */
  96. } else if (i2c_reg >= 0x20 && i2c_reg <= 0x26) {
  97. uint8_t *ptr = (uint8_t *)&params;
  98. uint8_t index = ((i2c_reg - 0x20) << 1) + (bcnt & 0x01);
  99. ptr[index] = data;
  100. /* crc field */
  101. if (i2c_reg == 0x26 && bcnt == 0x01)
  102. write_parameters();
  103. }
  104. }
  105. uint8_t usi_read(uint8_t bcnt)
  106. {
  107. /* snapshot of adc value */
  108. static int16_t adc_copy;
  109. /* reg 0x00, lowbyte -> read status */
  110. if (i2c_reg == 0 && bcnt == 0) {
  111. return sys_state;
  112. /* register word 0x10 - 0x12 adc values */
  113. } else if (i2c_reg >= 0x10 && i2c_reg <= 0x12) {
  114. if (bcnt == 0x00) {
  115. adc_copy = adc_value[i2c_reg - 0x10];
  116. return adc_copy & 0xFF;
  117. } else {
  118. return (adc_copy >> 8) & 0xFF;
  119. }
  120. /* register word 0x20 - 0x25 eeprom parameters */
  121. } else if (i2c_reg >= 0x20 && i2c_reg <= 0x25) {
  122. uint8_t *ptr = (uint8_t *)&params;
  123. uint8_t index = ((i2c_reg - 0x20) << 1) + (bcnt & 0x01);
  124. return ptr[index];
  125. }
  126. return 0x00;
  127. }
  128. /*
  129. * the watchdog timer remains active even after a
  130. * system reset. So disable it as soon as possible.
  131. * automagically called on startup
  132. */
  133. void disable_wdt_timer(void) __attribute__((naked, section(".init3")));
  134. void disable_wdt_timer(void)
  135. {
  136. MCUSR = 0;
  137. WDTCSR = (1<<WDCE) | (1<<WDE);
  138. WDTCSR = (0<<WDE);
  139. }
  140. int main(void)
  141. {
  142. PORTA = I2C_SCL | I2C_SDA;
  143. DDRA = I2C_SCL | LED_GN;
  144. DDRB = EN_POWER | EN_CHARGER | EN_TEST;
  145. /* Disable Digital Inputs */
  146. DIDR0 = AIN_REF | AIN_CURRENT_N | AIN_CURRENT_P | AIN_VOLTAGE_BAT | AIN_VOLTAGE_SUP;
  147. /* ADC: freerunning with F_CPU/128 */
  148. ADMUX = adc_mux[0];
  149. ADCSRA = (1<<ADEN) | (1<<ADSC) | (1<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
  150. /* init TWI statemachine */
  151. usi_statemachine();
  152. /* read statemachine parameters from eeprom */
  153. read_parameters();
  154. sei();
  155. /* enable power */
  156. sys_state = STATE_IDLE;
  157. PORTB = EN_POWER;
  158. /* wait 1s for moving average filters */
  159. uint8_t i;
  160. for (i = 0; i < 50; i++)
  161. _delay_ms(20);
  162. while (1) {
  163. while (!adc_update);
  164. adc_update = 0;
  165. /* disable interrupts while using adc_value and params */
  166. cli();
  167. /* loss of external power => discharge */
  168. if (sys_state & (STATE_IDLE | STATE_TEST | STATE_CHARGE))
  169. if (adc_value[ADC_UIN] < params.uin_loss)
  170. sys_state = STATE_DISCHARGE;
  171. /* external power restored => charge */
  172. if (sys_state & (STATE_DISCHARGE | STATE_POWEROFF))
  173. if (adc_value[ADC_UIN] > params.uin_restore)
  174. sys_state = STATE_CHARGE;
  175. /* battery is low => charge */
  176. if (sys_state & (STATE_IDLE | STATE_TEST))
  177. if (adc_value[ADC_UBAT] < params.ubat_low)
  178. sys_state = STATE_CHARGE;
  179. switch (sys_state) {
  180. case STATE_IDLE:
  181. PORTB = EN_POWER;
  182. break;
  183. case STATE_TEST:
  184. PORTB = EN_POWER | EN_TEST;
  185. break;
  186. case STATE_CHARGE:
  187. PORTB = EN_POWER | EN_CHARGER;
  188. /* battery is full => idle */
  189. if (adc_value[ADC_UBAT] > params.ubat_full && adc_value[ADC_CURRENT] < params.ibat_full)
  190. sys_state = STATE_IDLE;
  191. break;
  192. case STATE_DISCHARGE:
  193. PORTB = EN_POWER;
  194. /* battery is critical low => poweroff */
  195. if (adc_value[ADC_UBAT] < params.ubat_critical)
  196. sys_state = STATE_POWEROFF;
  197. break;
  198. case STATE_POWEROFF:
  199. PORTB = 0x00;
  200. break;
  201. default:
  202. sys_state = STATE_IDLE;
  203. break;
  204. }
  205. /* re-enable interrupts */
  206. sei();
  207. }
  208. }