AVR based brushless motor controller
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.

211 lines
5.0 KiB

  1. /***************************************************************************
  2. * Copyright (C) 09/2007 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 <avr/wdt.h>
  22. #include "main.h"
  23. #include "blmc.h"
  24. #include "eeprom.h"
  25. extern struct blmc_ blmc;
  26. extern struct ee_param params;
  27. /* SLA+R */
  28. #define CMD_WAIT 0x00
  29. #define CMD_READ_VERSION 0x01
  30. #define CMD_READ_MEMORY 0x02
  31. /* internal mappings */
  32. #define CMD_READ_CHIPINFO (0x10 | CMD_READ_MEMORY)
  33. #define CMD_READ_FLASH (0x20 | CMD_READ_MEMORY)
  34. #define CMD_READ_EEPROM (0x30 | CMD_READ_MEMORY)
  35. #define CMD_READ_PARAMETERS (0x40 | CMD_READ_MEMORY) /* only in APP */
  36. /* SLA+W */
  37. #define CMD_SWITCH_APPLICATION CMD_READ_VERSION
  38. #define CMD_WRITE_MEMORY CMD_READ_MEMORY
  39. /* internal mappings */
  40. #define CMD_BOOT_BOOTLOADER (0x10 | CMD_SWITCH_APPLICATION) /* only in APP */
  41. #define CMD_BOOT_APPLICATION (0x20 | CMD_SWITCH_APPLICATION)
  42. #define CMD_WRITE_CHIPINFO (0x10 | CMD_WRITE_MEMORY) /* invalid */
  43. #define CMD_WRITE_FLASH (0x20 | CMD_WRITE_MEMORY)
  44. #define CMD_WRITE_EEPROM (0x30 | CMD_WRITE_MEMORY)
  45. #define CMD_WRITE_PARAMETERS (0x40 | CMD_WRITE_MEMORY) /* only in APP */
  46. /* CMD_SWITCH_APPLICATION parameter */
  47. #define BOOTTYPE_BOOTLOADER 0x00 /* only in APP */
  48. #define BOOTTYPE_APPLICATION 0x80
  49. /* CMD_{READ|WRITE}_* parameter */
  50. #define MEMTYPE_CHIPINFO 0x00
  51. #define MEMTYPE_FLASH 0x01
  52. #define MEMTYPE_EEPROM 0x02
  53. #define MEMTYPE_PARAMETERS 0x03 /* only in APP */
  54. const static uint8_t info[16] = "blmc m8-v1.2 ";
  55. ISR(TWI_vect)
  56. {
  57. static uint8_t cmd;
  58. static uint8_t bcnt;
  59. uint8_t data;
  60. uint8_t ack = (1<<TWEA);
  61. switch (TWSR & 0xF8) {
  62. /* SLA + W received, ACK returned -> receive Data and ACK */
  63. case 0x60:
  64. bcnt = 0;
  65. TWCR |= (1<<TWINT) | (1<<TWEA);
  66. break;
  67. /* prev. SLA + W, data received, ACK returned -> receive Data and ACK */
  68. case 0x80:
  69. data = TWDR;
  70. switch (bcnt) {
  71. case 0:
  72. switch (data) {
  73. case CMD_SWITCH_APPLICATION:
  74. case CMD_WRITE_MEMORY:
  75. cmd = data;
  76. bcnt++;
  77. break;
  78. default:
  79. blmc.flags |= FLAG_I2C_ACTIVE;
  80. setpwm(data);
  81. cmd = CMD_WAIT;
  82. bcnt = 0;
  83. break;
  84. }
  85. break;
  86. case 1:
  87. switch (cmd) {
  88. case CMD_SWITCH_APPLICATION:
  89. if (data == BOOTTYPE_BOOTLOADER) {
  90. wdt_enable(WDTO_15MS);
  91. } else {
  92. ack = (0<<TWEA);
  93. }
  94. break;
  95. case CMD_WRITE_MEMORY:
  96. bcnt++;
  97. if (data == MEMTYPE_PARAMETERS) {
  98. cmd = CMD_WRITE_PARAMETERS;
  99. } else {
  100. ack = (0<<TWEA);
  101. }
  102. break;
  103. default:
  104. ack = (0<<TWEA);
  105. break;
  106. }
  107. break;
  108. case 2: /* ignore address bytes */
  109. case 3:
  110. bcnt++;
  111. break;
  112. default:
  113. switch (cmd) {
  114. case CMD_WRITE_PARAMETERS:
  115. ((uint8_t *)&params)[bcnt++ -4] = data;
  116. if (bcnt == sizeof(params)) {
  117. write_parameters();
  118. ack = (0<<TWEA);
  119. }
  120. break;
  121. default:
  122. ack = (0<<TWEA);
  123. break;
  124. }
  125. break;
  126. }
  127. if (ack == 0x00)
  128. bcnt = 0;
  129. TWCR |= (1<<TWINT) | ack;
  130. break;
  131. /* SLA+R received, ACK returned -> send data */
  132. case 0xA8:
  133. bcnt = 0;
  134. /* prev. SLA+R, data sent, ACK returned -> send data */
  135. case 0xB8:
  136. switch (cmd) {
  137. case CMD_WAIT:
  138. switch (bcnt++) {
  139. case 0:
  140. data = blmc.current & 0xFF;
  141. break;
  142. case 1:
  143. data = 0xFF - blmc.pwm_limit;
  144. break;
  145. default:
  146. data = 0xFF;
  147. break;
  148. }
  149. break;
  150. case CMD_READ_VERSION:
  151. data = info[bcnt++];
  152. bcnt %= sizeof(info);
  153. break;
  154. case CMD_READ_PARAMETERS:
  155. data = ((uint8_t *)&params)[bcnt++];
  156. if (bcnt > sizeof(params))
  157. bcnt = 0;
  158. break;
  159. default:
  160. data = 0xFF;
  161. break;
  162. }
  163. TWDR = data;
  164. TWCR |= (1<<TWINT) | (1<<TWEA);
  165. break;
  166. /* STOP or repeated START */
  167. case 0xA0:
  168. /* Data transmitted, NACK returned */
  169. case 0xC0:
  170. TWCR |= (1<<TWINT) | (1<<TWEA);
  171. break;
  172. /* failsave -> reset Hardware */
  173. default:
  174. TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
  175. break;
  176. }
  177. }