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.

286 lines
9.1 KiB

  1. #ifdef ISSI_ENABLE
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <util/delay.h>
  5. #include <avr/sfr_defs.h>
  6. #include <avr/io.h>
  7. #include <util/twi.h>
  8. #include "issi.h"
  9. #include "print.h"
  10. #include "TWIlib.h"
  11. #define ISSI_ADDR_DEFAULT 0xE8
  12. #define ISSI_REG_CONFIG 0x00
  13. #define ISSI_REG_CONFIG_PICTUREMODE 0x00
  14. #define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08
  15. #define ISSI_CONF_PICTUREMODE 0x00
  16. #define ISSI_CONF_AUTOFRAMEMODE 0x04
  17. #define ISSI_CONF_AUDIOMODE 0x08
  18. #define ISSI_REG_PICTUREFRAME 0x01
  19. #define ISSI_REG_SHUTDOWN 0x0A
  20. #define ISSI_REG_AUDIOSYNC 0x06
  21. #define ISSI_COMMANDREGISTER 0xFD
  22. #define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine'
  23. uint8_t control[8][9] = {
  24. {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
  25. {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
  26. {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
  27. {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
  28. };
  29. ISSIDeviceStruct *issi_devices[4] = {0, 0, 0, 0};
  30. #ifndef cbi
  31. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  32. #endif
  33. #ifndef sbi
  34. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  35. #endif
  36. #define I2C_WRITE 0
  37. #define F_SCL 400000UL // SCL frequency
  38. #define Prescaler 1
  39. #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
  40. uint8_t i2c_start(uint8_t address)
  41. {
  42. // reset TWI control register
  43. TWCR = 0;
  44. // transmit START condition
  45. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  46. // wait for end of transmission
  47. while( !(TWCR & (1<<TWINT)) );
  48. // check if the start condition was successfully transmitted
  49. if((TWSR & 0xF8) != TW_START){ return 1; }
  50. // load slave address into data register
  51. TWDR = address;
  52. // start transmission of address
  53. TWCR = (1<<TWINT) | (1<<TWEN);
  54. // wait for end of transmission
  55. while( !(TWCR & (1<<TWINT)) );
  56. // check if the device has acknowledged the READ / WRITE mode
  57. uint8_t twst = TW_STATUS & 0xF8;
  58. if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
  59. return 0;
  60. }
  61. uint8_t i2c_write(uint8_t data)
  62. {
  63. // load data into data register
  64. TWDR = data;
  65. // start transmission of data
  66. TWCR = (1 << TWINT) | (1 << TWEN);
  67. // wait for end of transmission
  68. while (!(TWCR & (1 << TWINT)))
  69. ;
  70. if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
  76. {
  77. TWBR = (uint8_t)TWBR_val;
  78. if (i2c_start(address | I2C_WRITE))
  79. return 1;
  80. for (uint16_t i = 0; i < length; i++) {
  81. if (i2c_write(data[i]))
  82. return 1;
  83. }
  84. // transmit STOP condition
  85. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  86. return 0;
  87. }
  88. void setFrame(uint8_t device, uint8_t frame)
  89. {
  90. static uint8_t current_frame = -1;
  91. if(current_frame != frame){
  92. uint8_t payload[] = {
  93. ISSI_ADDR_DEFAULT | device << 1,
  94. ISSI_COMMANDREGISTER,
  95. frame
  96. };
  97. TWITransmitData(payload, sizeof(payload), 0, 1);
  98. }
  99. // static uint8_t current_frame = 0xFF;
  100. // if(current_frame == frame){
  101. // // return;
  102. // }
  103. // uint8_t payload[2] = { ISSI_COMMANDREGISTER, frame };
  104. // i2c_transmit(ISSI_ADDR_DEFAULT | device << 1, payload, 2);
  105. // current_frame = frame;
  106. }
  107. void writeRegister8(uint8_t device, uint8_t frame, uint8_t reg, uint8_t data)
  108. {
  109. // Set the frame
  110. setFrame(device, frame);
  111. // Write to the register
  112. uint8_t payload[] = {
  113. ISSI_ADDR_DEFAULT | device << 1,
  114. reg,
  115. data
  116. };
  117. TWITransmitData(payload, sizeof(payload), 0, 1);
  118. }
  119. // void activateLED(uint8_t matrix, uint8_t cx, uint8_t cy, uint8_t pwm)
  120. // {
  121. // xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
  122. // uint8_t x = cx - 1; // funciton takes 1 based counts, but we need 0...
  123. // uint8_t y = cy - 1; // creating them once for less confusion
  124. // if(pwm == 0){
  125. // cbi(control[matrix][y], x);
  126. // }else{
  127. // sbi(control[matrix][y], x);
  128. // }
  129. // uint8_t device = (matrix & 0x06) >> 1;
  130. // uint8_t control_reg = (y << 1) | (matrix & 0x01);
  131. // uint8_t pwm_reg = 0;
  132. // switch(matrix & 0x01){
  133. // case 0:
  134. // pwm_reg = 0x24;
  135. // break;
  136. // case 1:
  137. // pwm_reg = 0x2C;
  138. // break;
  139. // }
  140. // pwm_reg += (y << 4) + x;
  141. // xprintf(" device: %02X\n", device);
  142. // xprintf(" control: %02X %02X\n", control_reg, control[matrix][y]);
  143. // xprintf(" pwm: %02X %02X\n", pwm_reg, pwm);
  144. // writeRegister8(device, 0, control_reg, control[matrix][y]);
  145. // writeRegister8(device, 0, control_reg + 0x12, control[matrix][y]);
  146. // writeRegister8(device, 0, pwm_reg, pwm);
  147. // }
  148. void activateLED(uint8_t matrix, uint8_t cx, uint8_t cy, uint8_t pwm)
  149. {
  150. uint8_t device_addr = (matrix & 0x06) >> 1;
  151. ISSIDeviceStruct *device = issi_devices[device_addr];
  152. if(device == 0){
  153. return;
  154. }
  155. // xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
  156. uint8_t x = cx - 1; // funciton takes 1 based counts, but we need 0...
  157. uint8_t y = cy - 1; // creating them once for less confusion
  158. uint8_t control_reg = (y << 1) | (matrix & 0x01);
  159. if(pwm == 0){
  160. cbi(device->led_ctrl[control_reg], x);
  161. cbi(device->led_blink_ctrl[control_reg], x);
  162. }else{
  163. sbi(device->led_ctrl[control_reg], x);
  164. sbi(device->led_blink_ctrl[control_reg], x);
  165. }
  166. uint8_t pwm_reg = 0;
  167. switch(matrix & 0x01){
  168. case 0:
  169. pwm_reg = 0x00;
  170. break;
  171. case 1:
  172. pwm_reg = 0x08;
  173. break;
  174. }
  175. pwm_reg += (y << 4) + x;
  176. // xprintf(" device_addr: %02X\n", device_addr);
  177. // xprintf(" control: %02X %02X\n", control_reg, control[matrix][y]);
  178. // xprintf(" pwm: %02X %02X\n", pwm_reg, pwm);
  179. // writeRegister8(device_addr, 0, control_reg, control[matrix][y]);
  180. device->led_pwm[pwm_reg] = pwm;
  181. device->led_dirty = 1;
  182. // writeRegister8(device_addr, 0, control_reg + 0x12, control[matrix][y]);
  183. // writeRegister8(device_addr, 0, pwm_reg, pwm);
  184. }
  185. void update_issi(uint8_t device_addr, uint8_t blocking)
  186. {
  187. // This seems to take about 6ms
  188. ISSIDeviceStruct *device = issi_devices[device_addr];
  189. if(device != 0){
  190. if(device->fn_dirty){
  191. device->fn_dirty = 0;
  192. setFrame(device_addr, ISSI_BANK_FUNCTIONREG);
  193. TWITransmitData(&device->fn_device_addr, sizeof(device->fn_registers) + 2, 0, 1);
  194. }
  195. if(device->led_dirty){
  196. device->led_dirty = 0;
  197. setFrame(device_addr, 0);
  198. TWITransmitData(&device->led_device_addr, 0xB6, 0, blocking);
  199. }
  200. }
  201. }
  202. void issi_init(void)
  203. {
  204. // Set LED_EN/SDB high to enable the chip
  205. xprintf("Enabing SDB on pin: %d\n", LED_EN_PIN);
  206. _SFR_IO8((LED_EN_PIN >> 4) + 1) &= ~_BV(LED_EN_PIN & 0xF); // IN
  207. _SFR_IO8((LED_EN_PIN >> 4) + 2) |= _BV(LED_EN_PIN & 0xF); // HI
  208. TWIInit();
  209. for(uint8_t device_addr = 0; device_addr < 4; device_addr++){
  210. xprintf("ISSI Init device: %d\n", device_addr);
  211. // If this device has been previously allocated, free it
  212. if(issi_devices[device_addr] != 0){
  213. free(issi_devices[device_addr]);
  214. }
  215. // Try to shutdown the device, if this fails skip this device
  216. writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
  217. while (!isTWIReady()){_delay_us(1);}
  218. if(TWIInfo.errorCode != 0xFF){
  219. xprintf("ISSI init failed %d %02X %02X\n", device_addr, TWIInfo.mode, TWIInfo.errorCode);
  220. continue;
  221. }
  222. // Allocate the device structure - calloc zeros it for us
  223. ISSIDeviceStruct *device = (ISSIDeviceStruct *)calloc(sizeof(ISSIDeviceStruct) * 2, 1);
  224. issi_devices[device_addr] = device;
  225. device->fn_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
  226. device->fn_register_addr = 0;
  227. device->led_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
  228. device->led_register_addr = 0;
  229. // set dirty bits so that all of the buffered data is written out
  230. device->fn_dirty = 1;
  231. device->led_dirty = 1;
  232. update_issi(device_addr, 1);
  233. // Set the function register to picture mode
  234. // device->fn_reg[ISSI_REG_CONFIG] = ISSI_REG_CONFIG_PICTUREMODE;
  235. writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
  236. }
  237. // Shutdown and set all registers to 0
  238. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
  239. // for(uint8_t bank = 0; bank <= 7; bank++){
  240. // for (uint8_t reg = 0x00; reg <= 0xB3; reg++) {
  241. // writeRegister8(device_addr, bank, reg, 0x00);
  242. // }
  243. // }
  244. // for (uint8_t reg = 0; reg <= 0x0C; reg++) {
  245. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, reg, 0x00);
  246. // }
  247. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE);
  248. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
  249. // picture mode
  250. // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x01, 0x01);
  251. //Enable blink
  252. // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x05, 0x48B);
  253. //Enable Breath
  254. }
  255. #endif