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 15KB

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