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.

243 lines
7.3 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. uint8_t device_addr = (matrix & 0x06) >> 1;
  122. ISSIDeviceStruct *device = issi_devices[device_addr];
  123. if(device == 0){
  124. return;
  125. }
  126. // xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
  127. uint8_t x = cx - 1; // funciton takes 1 based counts, but we need 0...
  128. uint8_t y = cy - 1; // creating them once for less confusion
  129. uint8_t control_reg = (y << 1) | (matrix & 0x01);
  130. if(pwm == 0){
  131. cbi(device->led_ctrl[control_reg], x);
  132. cbi(device->led_blink_ctrl[control_reg], x);
  133. }else{
  134. sbi(device->led_ctrl[control_reg], x);
  135. sbi(device->led_blink_ctrl[control_reg], x);
  136. }
  137. uint8_t pwm_reg = 0;
  138. switch(matrix & 0x01){
  139. case 0:
  140. pwm_reg = 0x00;
  141. break;
  142. case 1:
  143. pwm_reg = 0x08;
  144. break;
  145. }
  146. pwm_reg += (y << 4) + x;
  147. device->led_pwm[pwm_reg] = pwm;
  148. device->led_dirty = 1;
  149. }
  150. void update_issi(uint8_t device_addr, uint8_t blocking)
  151. {
  152. // This seems to take about 6ms
  153. ISSIDeviceStruct *device = issi_devices[device_addr];
  154. if(device != 0){
  155. if(device->fn_dirty){
  156. device->fn_dirty = 0;
  157. setFrame(device_addr, ISSI_BANK_FUNCTIONREG);
  158. TWITransmitData(&device->fn_device_addr, sizeof(device->fn_registers) + 2, 0, 1);
  159. }
  160. if(device->led_dirty){
  161. device->led_dirty = 0;
  162. setFrame(device_addr, 0);
  163. TWITransmitData(&device->led_device_addr, 0xB6, 0, blocking);
  164. }
  165. }
  166. }
  167. void issi_init(void)
  168. {
  169. TWIInit();
  170. for(uint8_t device_addr = 0; device_addr < 4; device_addr++){
  171. // If this device has been previously allocated, free it
  172. if(issi_devices[device_addr] != 0){
  173. free(issi_devices[device_addr]);
  174. }
  175. // Try to shutdown the device, if this fails skip this device
  176. writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
  177. while (!isTWIReady()){_delay_us(1);}
  178. if(TWIInfo.errorCode != 0xFF){
  179. xprintf("ISSI init failed %d %02X %02X\n", device_addr, TWIInfo.mode, TWIInfo.errorCode);
  180. continue;
  181. }
  182. // Allocate the device structure - calloc zeros it for us
  183. ISSIDeviceStruct *device = (ISSIDeviceStruct *)calloc(sizeof(ISSIDeviceStruct) * 2, 1);
  184. issi_devices[device_addr] = device;
  185. device->fn_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
  186. device->fn_register_addr = 0;
  187. device->led_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
  188. device->led_register_addr = 0;
  189. // set dirty bits so that all of the buffered data is written out
  190. device->fn_dirty = 1;
  191. device->led_dirty = 1;
  192. update_issi(device_addr, 1);
  193. // Set the function register to picture mode
  194. // device->fn_reg[ISSI_REG_CONFIG] = ISSI_REG_CONFIG_PICTUREMODE;
  195. writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
  196. }
  197. // Shutdown and set all registers to 0
  198. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
  199. // for(uint8_t bank = 0; bank <= 7; bank++){
  200. // for (uint8_t reg = 0x00; reg <= 0xB3; reg++) {
  201. // writeRegister8(device_addr, bank, reg, 0x00);
  202. // }
  203. // }
  204. // for (uint8_t reg = 0; reg <= 0x0C; reg++) {
  205. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, reg, 0x00);
  206. // }
  207. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE);
  208. // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
  209. // picture mode
  210. // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x01, 0x01);
  211. //Enable blink
  212. // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x05, 0x48B);
  213. //Enable Breath
  214. }
  215. #endif