Picadae hardware and control code
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.

i2c_timer_pwm.c 17KB

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