Picadae hardware and control code
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

i2c_timer_pwm_2.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //| Copyright: (C) 2018-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. // w 60 0 1 10 : w i2c_addr SetPWM enable duty_val
  4. // w 60 5 12 8 32 : w i2c_addr write addrFl|src coarse_val
  5. // w 60 4 0 5 : w i2c_addr read src read_addr (set the read address to register 5)
  6. // r 60 4 3 : r i2c_addr <dum> cnt (read the first 3 reg's beginning w/ 5)
  7. /*
  8. AT TINY 85
  9. +--\/--+
  10. RESET _| 1 8 |_ +5V
  11. ~OC1B HOLD PINB3 _| 2 7 |_ SCL yellow
  12. OC1B ONSET PINB4 _| 3 6 |_ PINB1 LED
  13. GND _| 4 5 |_ SDA orange
  14. +------+
  15. * = Serial and/or programming pins on Arduino as ISP
  16. */
  17. // This program acts as the device (slave) for the control program i2c/a2a/c_ctl
  18. #define F_CPU 16000000L
  19. #include <stdio.h>
  20. #include <avr/io.h>
  21. #include <util/delay.h>
  22. #include <avr/interrupt.h>
  23. #include "usiTwiSlave.h"
  24. #define HOLD_DIR DDB3
  25. #define ATTK_DIR DDB4
  26. #define LED_DIR DDB1
  27. #define HOLD_PIN PINB3
  28. #define ATTK_PIN PINB4
  29. #define LED_PIN PINB1
  30. // Opcodes
  31. enum
  32. {
  33. kSetPwm_Op = 0, // Set PWM duty/hz/div 0 {<duty> {<freq> {<div>}}} div:2=2,3=4,4=8,5=16,6=32,7=64,8=128,9=256,10=512,11=1024,12=2048,13=4096,14=8192,15=16384
  34. kNoteOnVel_Op = 1, // Turn on note 3 {<vel>}
  35. kNoteOnUsec_Op = 2, // Turn on note 4 {<coarse> {<fine> {<prescale>}}}
  36. kNoteOff_Op = 3, // Turn off note 5
  37. kSetReadAddr_Op = 4, // Set a read addr. 6 {<src>} {<addr>} } src: 0=reg 1=table 2=eeprom
  38. kWrite_Op = 5, // Set write 7 {<addrfl|src> {addr} {<value0> ... {<valueN>}} addrFl:0x80 src: 4=reg 5=table 6=eeprom
  39. kWriteTable_Op = 6, // Write table to EEprom 9
  40. kHoldDelay_Op = 7, // Set hold delay {<coarse> {<fine>}}
  41. kFlags_Op = 8, // Set flags variable
  42. kInvalid_Op = 9 //
  43. };
  44. enum
  45. {
  46. kReg_Rd_Addr_idx = 0, // Next Reg Address to read
  47. kTable_Rd_Addr_idx = 1, // Next Table Address to read
  48. kEE_Rd_Addr_idx = 2, // Next EEPROM address to read
  49. kRead_Src_idx = 3, // kReg_Rd_Addr_idx=reg, kTable_Rd_Addr_idx=table, kEE_Rd_Addr_idx=eeprom
  50. kReg_Wr_Addr_idx = 4, // Next Reg Address to write
  51. kTable_Wr_Addr_idx = 5, // Next Table Address to write
  52. kEE_Wr_Addr_idx = 6, // Next EEPROM address to write
  53. kWrite_Dst_idx = 7, // kReg_Wr_Addr_idx=reg, kTable_Wr_Addr_idx=table, kEE_Wr_Addr_idx=eeprom
  54. kTmr_Coarse_idx = 8, //
  55. kTmr_Fine_idx = 9, //
  56. kTmr_Prescale_idx = 10, // Timer 0 clock divider: 1=1,2=8,3=64,4=256,5=1024 Default: 4 (16us)
  57. kPwm_Duty_idx = 11, //
  58. kPwm_Freq_idx = 12, //
  59. kPwm_Div_idx = 13, //
  60. kState_idx = 14, // 1=attk 2=hold
  61. kError_Code_idx = 15, // Error Code
  62. kMax_Coarse_Tmr_idx = 16, // Max. allowable coarse timer value
  63. kDelay_Coarse_idx = 17, // (17,18)=2000 (0,6)=100
  64. kDelay_Fine_idx = 18,
  65. kFlags_idx = 19,
  66. kMax_idx
  67. };
  68. enum
  69. {
  70. kState_Attk_Fl = 1,
  71. kState_Hold_Fl = 2
  72. };
  73. // ctl_regs[kFlags_idx] bits
  74. enum
  75. {
  76. kHoldOnAttk_Fl = 0x01
  77. };
  78. volatile uint8_t ctl_regs[] =
  79. {
  80. 0, // 0 (0-(kMax_idx-1)) Reg Read Addr
  81. 0, // 1 (0-255) Table Read Addr
  82. 0, // 2 (0-255) EE Read Addr
  83. kReg_Rd_Addr_idx, // 3 (0-2) Read source
  84. 0, // 4 (0-(kMax_idx-1)) Reg Write Addr
  85. 0, // 5 (0-255) Table Write Addr
  86. 0, // 6 (0-255) EE Write Addr
  87. kReg_Wr_Addr_idx, // 7 (0-2) Write source
  88. 5, // 8 (0-255) Timer 0 Coarse Value (20400 us)
  89. 0, // 9 (0-255) Timer 0 Fine Value
  90. 4, // 10 (1-5) 4=16us per tick
  91. 127, // 11 (0-255) Pwm Duty cycle
  92. 255, // 12 (0-255) Pwm Frequency (123 Hz)
  93. 5, // 13 (0-15) Pwm clock div
  94. 0, // 14 state flags 1=attk 2=hold (read/only)
  95. 0, // 15 (0-255) Error bit field
  96. 14, // 16 (0-255) Max allowable coarse timer count
  97. 0, // 17 (0-255) Hold coarse delay
  98. 250, // 18 (0-255) Hold fine delay 0,6=100us 0,124=2000us w/ 16us Tmr0 tick
  99. 0 // 19 (0-255) Flags 1=hold-on-attack
  100. };
  101. // These registers are saved to Eeprom
  102. uint8_t eeprom_addr[] =
  103. {
  104. kTmr_Prescale_idx,
  105. kPwm_Duty_idx,
  106. kPwm_Freq_idx,
  107. kPwm_Div_idx
  108. };
  109. #define tableN 256
  110. uint8_t table[ tableN ]; // [ coarse_0,fine_0, coarse_1, fine_1, .... coarse_127,fine_127]
  111. enum
  112. {
  113. kInvalid_Read_Src_ErrFl = 0x01,
  114. kInvalid_Write_Dst_ErrFl = 0x02,
  115. kInvalid_Coarse_Tmr_ErrFl = 0x04
  116. };
  117. #define set_error( flag ) ctl_regs[ kError_Code_idx ] |= (flag)
  118. //------------------------------------------------------------------------------
  119. //------------------------------------------------------------------------------
  120. //------------------------------------------------------------------------------
  121. //
  122. // EEPROM
  123. //
  124. void EEPROM_write(uint8_t ucAddress, uint8_t ucData)
  125. {
  126. // Wait for completion of previous write
  127. while(EECR & (1<<EEPE))
  128. {}
  129. EECR = (0<<EEPM1)|(0<<EEPM0); // Set Programming mode
  130. EEAR = ucAddress; // Set up address and data registers
  131. EEDR = ucData;
  132. EECR |= (1<<EEMPE); // Write logical one to EEMPE
  133. EECR |= (1<<EEPE); // Start eeprom write by setting EEPE
  134. }
  135. uint8_t EEPROM_read(uint8_t ucAddress)
  136. {
  137. // Wait for completion of previous write
  138. while(EECR & (1<<EEPE))
  139. {}
  140. EEAR = ucAddress; // Set up address register
  141. EECR |= (1<<EERE); // Start eeprom read by writing EERE
  142. return EEDR; // Return data from data register
  143. }
  144. void write_table()
  145. {
  146. uint8_t i;
  147. uint8_t regN = sizeof(eeprom_addr);
  148. // write the persistent registers
  149. for(i=0; i<regN; ++i)
  150. EEPROM_write( i, ctl_regs[ eeprom_addr[i] ] );
  151. // write the table
  152. for(i=0; i<tableN; ++i)
  153. EEPROM_write( regN+i, table[i] );
  154. }
  155. void load_table()
  156. {
  157. uint8_t i;
  158. uint8_t regN = sizeof(eeprom_addr);
  159. // read the persistent registers
  160. for(i=0; i<regN; ++i)
  161. ctl_regs[ eeprom_addr[i] ] = EEPROM_read(i);
  162. // read the tabke
  163. for(i=0; i<tableN; ++i)
  164. table[i] = EEPROM_read(regN + i);
  165. }
  166. //------------------------------------------------------------------------------
  167. //------------------------------------------------------------------------------
  168. //------------------------------------------------------------------------------
  169. //
  170. // Timer0
  171. //
  172. uint16_t stage1_cnt = 0;
  173. uint16_t stage2_cnt = 0;
  174. uint8_t stage1_coarse_cnt = 0;
  175. uint8_t stage1_fine_cnt = 0;
  176. uint8_t stage2_coarse_cnt = 0;
  177. uint8_t stage2_fine_cnt = 0;
  178. uint8_t hold_beg_first_fl = 0;
  179. volatile uint8_t tmr0_state = 0; // current timer mode: 0=disabled 1=coarse mode, 2=fine mode
  180. volatile uint8_t tmr0_coarse_cur = 0;
  181. #define set_attack() do { ctl_regs[kState_idx] |= kState_Attk_Fl; PORTB |= _BV(ATTK_PIN); } while(0)
  182. #define clear_attack() do { PORTB &= ~_BV(ATTK_PIN); ctl_regs[kState_idx] &= ~kState_Attk_Fl; } while(0)
  183. #define clear_hold() PORTB &= ~(_BV(HOLD_PIN))
  184. #define set_hold() PORTB |= _BV(HOLD_PIN)
  185. void hold_begin()
  186. {
  187. // Reset the PWM counter to to OCR1C (PWM TOP) so that it immediately triggers
  188. // set_hold() and latches any new value for OCR1B (See: 12.2.2 Timer/Counter1 in PWM Mode)
  189. // If this is not done and OCR1B was modified the first pulse will have the incorrect length.
  190. TCNT1 = ctl_regs[kPwm_Freq_idx];
  191. TIMSK |= _BV(OCIE1B) + _BV(TOIE1); // PWM interupt Enable interrupts
  192. TCCR1 |= ctl_regs[ kPwm_Div_idx]; // 32us period (512 divider) prescaler
  193. GTCCR |= _BV(PSR1); // Force the pre-scale to be latched by setting PSR1
  194. }
  195. void hold_end()
  196. {
  197. clear_attack();
  198. clear_hold();
  199. TIMSK &= ~_BV(OCIE0A); // Clear timer interrupt (shouldn't be necessary but doesn't hurt on during note-off message)
  200. TIMSK &= ~(_BV(OCIE1B) + _BV(TOIE1)); // PWM interupt disable interrupts
  201. TCCR1 = 0; // Stop the PWM timer by setting the pre-scale to 0
  202. GTCCR |= _BV(PSR1); // Force the pre-scale to be latched by setting PSR1
  203. }
  204. // Use the current tmr0 ctl_reg[] values to set the timer to the starting state.
  205. void tmr0_reset()
  206. {
  207. uint16_t delayCnt = ctl_regs[ kDelay_Coarse_idx ];
  208. delayCnt = (delayCnt<<8) + ctl_regs[ kDelay_Fine_idx ];
  209. uint16_t attkCnt = ctl_regs[ kTmr_Coarse_idx ];
  210. attkCnt = (attkCnt<<8) + ctl_regs[ kTmr_Fine_idx ];
  211. if( attkCnt > delayCnt )
  212. {
  213. stage1_cnt = attkCnt - delayCnt;
  214. stage2_cnt = delayCnt;
  215. hold_beg_first_fl = 1;
  216. }
  217. else
  218. {
  219. stage1_cnt = attkCnt;
  220. stage2_cnt = delayCnt - attkCnt;
  221. hold_beg_first_fl = 0;
  222. }
  223. stage1_coarse_cnt = stage1_cnt >> 8;
  224. stage1_fine_cnt = stage1_cnt & 0xff;
  225. stage2_coarse_cnt = stage2_cnt >> 8;
  226. stage2_fine_cnt = stage2_cnt & 0xff;
  227. tmr0_coarse_cur = 0; // clear the coarse time counter
  228. // always start in mode=1 because even if coarse count==0
  229. // because a COMPA interrupt will be fired immediately when the
  230. // timer starts
  231. tmr0_state = 1;
  232. OCR0A = 0xff;
  233. TCNT0 = 0;
  234. clear_hold(); // clear the hold pin
  235. set_attack(); // set the attack pin
  236. TIMSK |= _BV(OCIE0A); // enable the timer interrupt
  237. //if( ctl_regs[ kFlags_idx ] & kHoldOnAttk_Fl )
  238. // hold_begin();
  239. }
  240. ISR(TIMER0_COMPA_vect)
  241. {
  242. switch( tmr0_state )
  243. {
  244. case 0: // timer disabled
  245. break;
  246. case 1: // stage1 coarse mode
  247. // Note: the '+1' here is necessary to absorb an interrupt which is occurring
  248. // for an unknown reason. It must have something to do with resetting the
  249. // OCIE0A interrupt because it doesn't occur on the hold delay coarse timing.
  250. if( ++tmr0_coarse_cur >= stage1_coarse_cnt+1 )
  251. {
  252. tmr0_state = 2;
  253. OCR0A = stage1_fine_cnt;
  254. }
  255. break;
  256. case 2: // stage1 fine mode complete
  257. // if a coarse delay count exists then go into stage2 coarse mode
  258. if( stage2_coarse_cnt > 0 )
  259. {
  260. tmr0_state = 3;
  261. tmr0_coarse_cur = 0;
  262. OCR0A = 0xff;
  263. }
  264. else // otherwise go into fine mode
  265. {
  266. tmr0_state = 4;
  267. OCR0A = stage2_fine_cnt;
  268. }
  269. if( hold_beg_first_fl )
  270. hold_begin(); // start hold PWM
  271. else
  272. clear_attack();
  273. break;
  274. case 3: // stage2 coarse mode
  275. if( ++tmr0_coarse_cur >= stage2_coarse_cnt )
  276. {
  277. tmr0_state = 4;
  278. OCR0A = stage2_fine_cnt;
  279. }
  280. break;
  281. case 4: // stage2 fine mode complete
  282. TIMSK &= ~_BV(OCIE0A); // clear timer interrupt
  283. tmr0_state = 0;
  284. if( hold_beg_first_fl )
  285. clear_attack();
  286. else
  287. hold_begin();
  288. //if( !(ctl_regs[ kFlags_idx ] & kHoldOnAttk_Fl) )
  289. // hold_begin();
  290. break;
  291. }
  292. }
  293. void tmr0_init()
  294. {
  295. TIMSK &= ~_BV(OCIE0A); // Disable interrupt TIMER1_OVF
  296. TCCR0A = 0; // Set the timer control registers to their default value
  297. TCCR0B = 0;
  298. TCCR0A |= 0x02; // CTC mode
  299. TCCR0B |= ctl_regs[kTmr_Prescale_idx]; // set the prescaler
  300. GTCCR |= _BV(PSR0); // Trigger the pre-scaler to be reset to the selected value
  301. }
  302. //------------------------------------------------------------------------------
  303. //------------------------------------------------------------------------------
  304. //------------------------------------------------------------------------------
  305. //
  306. // Pwm
  307. //
  308. // PWM is optimized to use pins OC1A ,~OC1A, OC1B, ~OC1B
  309. // but since these pins are not available this code uses
  310. // ISR's to redirect the output to PIN3
  311. void pwm1_update()
  312. {
  313. OCR1B = ctl_regs[kPwm_Duty_idx]; // control duty cycle
  314. OCR1C = ctl_regs[kPwm_Freq_idx]; // PWM frequency pre-scaler
  315. }
  316. // Called when TCNT1 == OCR1C.
  317. // At this point TCNT1 is reset to 0, new OCR1B values are latched from temp. loctaion to OCR1B
  318. ISR(TIMER1_OVF_vect)
  319. {
  320. set_hold();
  321. }
  322. // Called when TCNT1 == OCR1B
  323. ISR(TIMER1_COMPB_vect)
  324. {
  325. clear_hold();
  326. }
  327. void pwm1_init()
  328. {
  329. DDRB |= _BV(HOLD_DIR); // setup PB3 as output
  330. TCCR1 = 0; // Set the control registers to their default
  331. GTCCR = 0;
  332. GTCCR |= _BV(PWM1B); // Enable PWM B and disconnect output pins
  333. pwm1_update();
  334. }
  335. //------------------------------------------------------------------------------
  336. //------------------------------------------------------------------------------
  337. //------------------------------------------------------------------------------
  338. // Tracks the current register pointer position
  339. volatile uint8_t reg_position = 0;
  340. const uint8_t reg_size = sizeof(ctl_regs);
  341. //
  342. // Read Request Handler
  343. //
  344. // This is called for each read request we receive, never put more
  345. // than one byte of data (with TinyWireS.send) to the send-buffer when
  346. // using this callback
  347. //
  348. void on_request()
  349. {
  350. uint8_t val = 0;
  351. switch( ctl_regs[ kRead_Src_idx ] )
  352. {
  353. case kReg_Rd_Addr_idx:
  354. val = ctl_regs[ ctl_regs[kReg_Rd_Addr_idx] ];
  355. break;
  356. case kTable_Rd_Addr_idx:
  357. val = table[ ctl_regs[kTable_Rd_Addr_idx] ];
  358. break;
  359. case kEE_Rd_Addr_idx:
  360. val = EEPROM_read(ctl_regs[kEE_Rd_Addr_idx]);
  361. break;
  362. default:
  363. set_error( kInvalid_Read_Src_ErrFl );
  364. return;
  365. }
  366. usiTwiTransmitByte(val);
  367. ctl_regs[ ctl_regs[ kRead_Src_idx ] ] += 1;
  368. }
  369. void _write_op( uint8_t* stack, uint8_t stackN )
  370. {
  371. uint8_t stack_idx = 0;
  372. if( stackN > 0 )
  373. {
  374. uint8_t src = stack[0] & 0x07;
  375. uint8_t addr_fl = stack[0] & 0x08;
  376. // verify the source value
  377. if( src < kReg_Wr_Addr_idx || src > kEE_Wr_Addr_idx )
  378. {
  379. set_error( kInvalid_Write_Dst_ErrFl );
  380. return;
  381. }
  382. // set the write source
  383. stack_idx = 1;
  384. ctl_regs[ kWrite_Dst_idx ] = src;
  385. // if an address value was passed also ....
  386. if( addr_fl && stackN > 1 )
  387. {
  388. stack_idx = 2;
  389. ctl_regs[ src ] = stack[1];
  390. }
  391. }
  392. //
  393. for(; stack_idx<stackN; ++stack_idx)
  394. {
  395. uint8_t addr_idx = ctl_regs[ ctl_regs[kWrite_Dst_idx] ]++;
  396. uint8_t val = stack[ stack_idx ];
  397. switch( ctl_regs[ kWrite_Dst_idx ] )
  398. {
  399. case kReg_Wr_Addr_idx: ctl_regs[ addr_idx ] = val; break;
  400. case kTable_Wr_Addr_idx: table[ addr_idx ] = val; break;
  401. case kEE_Wr_Addr_idx: EEPROM_write( table[ addr_idx ], val); break;
  402. default:
  403. set_error( kInvalid_Write_Dst_ErrFl );
  404. break;
  405. }
  406. }
  407. }
  408. //
  409. // The I2C data received -handler
  410. //
  411. // This needs to complete before the next incoming transaction (start,
  412. // data, restart/stop) on the bus does so be quick, set flags for long
  413. // running tasks to be called from the mainloop instead of running
  414. // them directly,
  415. //
  416. void on_receive( uint8_t byteN )
  417. {
  418. PINB = _BV(LED_PIN); // writes to PINB toggle the pins
  419. const uint8_t stackN = 16;
  420. uint8_t stack_idx = 0;
  421. uint8_t stack[ stackN ];
  422. uint8_t i;
  423. if (byteN < 1 || byteN > TWI_RX_BUFFER_SIZE)
  424. {
  425. // Sanity-check
  426. return;
  427. }
  428. // get the register index to read/write
  429. uint8_t op_id = usiTwiReceiveByte();
  430. byteN--;
  431. // If only one byte was received then this was a read request
  432. // and the buffer pointer (reg_position) is now set to return the byte
  433. // at this location on the subsequent call to on_request() ...
  434. if(byteN)
  435. {
  436. while( byteN-- )
  437. {
  438. stack[stack_idx] = usiTwiReceiveByte();
  439. ++stack_idx;
  440. }
  441. }
  442. switch( op_id )
  443. {
  444. case kSetPwm_Op:
  445. for(i=0; i<stack_idx && i<3; ++i)
  446. ctl_regs[ kPwm_Duty_idx + i ] = stack[i];
  447. pwm1_update();
  448. break;
  449. case kNoteOnUsec_Op:
  450. for(i=0; i<stack_idx && i<3; ++i)
  451. ctl_regs[ kTmr_Coarse_idx + i ] = stack[i];
  452. // validate the coarse error value
  453. if( ctl_regs[ kTmr_Coarse_idx ] > ctl_regs[ kMax_Coarse_Tmr_idx ])
  454. {
  455. ctl_regs[ kTmr_Coarse_idx ] = ctl_regs[ kMax_Coarse_Tmr_idx ];
  456. set_error( kInvalid_Coarse_Tmr_ErrFl );
  457. }
  458. // if a prescaler was included then the timer needs to be re-initialized
  459. if( i == 3 )
  460. {
  461. cli();
  462. tmr0_init();
  463. sei();
  464. }
  465. tmr0_reset();
  466. break;
  467. case kNoteOff_Op:
  468. hold_end();
  469. break;
  470. case kSetReadAddr_Op:
  471. if( stack_idx > 0 )
  472. {
  473. ctl_regs[ kRead_Src_idx ] = stack[0];
  474. if( stack_idx > 1 )
  475. ctl_regs[ ctl_regs[ kRead_Src_idx ] ] = stack[1];
  476. }
  477. break;
  478. case kWrite_Op:
  479. _write_op( stack, stack_idx );
  480. break;
  481. case kWriteTable_Op:
  482. write_table();
  483. break;
  484. case kHoldDelay_Op:
  485. for(i=0; i<stack_idx && i<2; ++i)
  486. ctl_regs[ kDelay_Coarse_idx + i ] = stack[i];
  487. break;
  488. case kFlags_Op:
  489. ctl_regs[ kFlags_idx ] = stack[0];
  490. }
  491. }
  492. int main(void)
  493. {
  494. cli(); // mask all interupts
  495. DDRB |= _BV(ATTK_DIR) + _BV(HOLD_DIR) + _BV(LED_DIR); // setup PB4,PB3,PB1 as output
  496. PORTB &= ~(_BV(ATTK_PIN) + _BV(HOLD_PIN) + _BV(LED_PIN)); // clear output pins
  497. tmr0_init();
  498. pwm1_init();
  499. // setup i2c library
  500. usi_onReceiverPtr = on_receive;
  501. usi_onRequestPtr = on_request;
  502. usiTwiSlaveInit(I2C_SLAVE_ADDRESS);
  503. sei();
  504. PINB = _BV(LED_PIN); // writes to PINB toggle the pins
  505. _delay_ms(1000);
  506. PINB = _BV(LED_PIN); // writes to PINB toggle the pins
  507. while(1)
  508. {
  509. if (!usi_onReceiverPtr)
  510. {
  511. // no onReceive callback, nothing to do...
  512. continue;
  513. }
  514. if (!(USISR & ( 1 << USIPF )))
  515. {
  516. // Stop not detected
  517. continue;
  518. }
  519. uint8_t amount = usiTwiAmountDataInReceiveBuffer();
  520. if (amount == 0)
  521. {
  522. // no data in buffer
  523. continue;
  524. }
  525. usi_onReceiverPtr(amount);
  526. }
  527. return 0;
  528. }