Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. /*
  2. * IIC Host Driver for Dragon12-plus
  3. * Author: Tom Almy
  4. * Date: January 29, 2008
  5.  
  6. * Eduardo Nebot July 2015
  7. Original code written for debug-12
  8. this code adapted for using with code warrior
  9. and small memory model ( interrupt )
  10. Simple method
  11. **********************
  12.  
  13. Resources Used: Timer 7
  14. Prescaler is 1; if changed we need to change
  15. the counter in:
  16. interrupt 15 void TC7_ISR(void)
  17. to 1 msec delay. Current value is 24,000
  18.  
  19. */
  20.  
  21.  
  22. #include <hidef.h> /* common defines and macros */
  23. #include "derivative.h" /* derivative-specific definitions */
  24.  
  25.  
  26.  
  27. typedef unsigned char uint8_t;
  28. typedef unsigned int uint16_t;
  29.  
  30. //#define TRUE (1)
  31. //#define FALSE (0)
  32.  
  33. // *** IIC interface ***
  34. void iicinit(void);
  35. int iicstart(uint8_t control); // return non-zero if fails
  36. int iicrestart(uint8_t control); // return non-zero if fails
  37. int iictransmit(uint8_t control); // return non-zero if fails
  38. void iicstop(void);
  39. void iicswrcv(void);
  40. int iicreceiveone(void); // return -1 if fails, 0->255 is valid received value
  41. int iicreceive(void); // return -1 if fails, 0->255 is valid received value
  42. int iicreceivem1(void); // return -1 if fails, 0->255 is valid received value
  43. int iicreceivelast(void); // return -1 if fails, 0->255 is valid received value
  44.  
  45. uint8_t iic_error_code;
  46. enum IIC_ERRORS {NO_ERROR, NO_RESPONSE, NAK_RESPONSE, IIB_CLEAR_TIMEOUT, IIB_SET_TIMEOUT,
  47. RECEIVE_TIMEOUT};
  48.  
  49. // *** timeout control ***
  50.  
  51. volatile uint8_t alarmSignaled = 0; /* Flag set when alarm 1 signaled */
  52.  
  53. volatile uint16_t currentTime = 0; /* variables private to timeout routines */
  54. uint16_t alarmTime = 0;
  55. volatile uint8_t alarmSet = 0;
  56.  
  57. // void INTERRUPT timer6(void);
  58.  
  59. void setAlarm(uint16_t msDelay);
  60. void delay(uint16_t msDelay);
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. // *** I2C Functions ***
  70.  
  71.  
  72. void iicinit(void) {
  73. IBFD = 0x23; // 100KHz operation
  74. //IBFD = 0x35; // 20KHz operation
  75. IBAD = 0x2; // Slave address is 1 (never address ourselves, please!)
  76. IBCR |= 0x80; // Set IBEN
  77. }
  78.  
  79. int iicresponse(void) { // wait to see what our response is
  80. setAlarm(1000);
  81. while ((IBSR & 0x2) == 0 && !alarmSignaled) {}; // Wait for IBIF to set
  82. if (alarmSignaled) {
  83. iic_error_code = NO_RESPONSE;
  84. iicstop();
  85. return 1;
  86. }
  87. IBSR &= 0x2; // Clear IBIF
  88. if (IBSR&1) { // NAK -- stop!
  89. iic_error_code = NAK_RESPONSE;
  90. iicstop();
  91. return 1;
  92. }
  93. return 0;
  94. }
  95.  
  96. /* Returns 1 on failure and sets iic_error_code, 0 on success */
  97. int iicstart(uint8_t control) {
  98. setAlarm(1000);
  99. while ((IBSR & 0x20)!= 0 && !alarmSignaled) {}; // Wait for IBB flag to clear
  100. if (alarmSignaled) {
  101. iic_error_code = IIB_CLEAR_TIMEOUT;
  102. return 1;
  103. }
  104. IBCR |= 0x30;
  105. IBDR = control;
  106. setAlarm(1000);
  107. while ((IBSR & 0x20)==0 && !alarmSignaled) {}; // Wait for IBB flag to set
  108. if (alarmSignaled) {
  109. iic_error_code = IIB_SET_TIMEOUT;
  110. return 2;
  111. }
  112. return iicresponse();
  113. }
  114.  
  115. void iicstop(void) {
  116. IBCR &= ~0x20;
  117. }
  118.  
  119. int iicrestart(uint8_t control) {
  120. IBCR |= 0x04;
  121. IBDR = control;
  122. return iicresponse();
  123. }
  124.  
  125. int iictransmit(uint8_t val) {
  126. IBDR = val;
  127. return iicresponse();
  128. }
  129.  
  130.  
  131. void iicswrcv(void) {
  132. IBCR &= ~0x10; // put in receive mode
  133. IBDR; // dummy read (better not be optimized out!)
  134. }
  135.  
  136. /* Returns -1 and error code set on failure */
  137. int iicreceive(void) {
  138. setAlarm(1000);
  139. while ((IBSR & 0x2) == 0 && !alarmSignaled) {};
  140. if (alarmSignaled) {
  141. iic_error_code = RECEIVE_TIMEOUT;
  142. return -1;
  143. }
  144. IBSR &= 0x2;
  145. return IBDR;
  146. }
  147.  
  148. /* Returns -1 and error code set on failure */
  149. int iicreceivem1(void) {
  150. setAlarm(1000);
  151. while ((IBSR & 0x2) == 0 && !alarmSignaled) {};
  152. if (alarmSignaled) {
  153. iic_error_code = RECEIVE_TIMEOUT;
  154. return -1;
  155. }
  156. IBSR &= 0x2;
  157. IBCR |= 0x8; // disable ACK for last read
  158. return IBDR;
  159. }
  160.  
  161. /* Returns -1 and error code set on failure */
  162. int iicreceivelast(void) {
  163. setAlarm(1000);
  164. while ((IBSR & 0x2) == 0 && !alarmSignaled) {};
  165. if (alarmSignaled) {
  166. iic_error_code = RECEIVE_TIMEOUT;
  167. return -1;
  168. }
  169. IBSR &= 0x2;
  170. IBCR &= ~0x8; // reenable ACK
  171. IBCR &= ~0x20; // generate STOP
  172. IBCR |= 0x10; // set transmit
  173. return IBDR;
  174. }
  175.  
  176. /* Returns -1 and error code set on failure */
  177. int iicreceiveone(void) {
  178. IBCR |= 0x8; // Disable ACK
  179. iicswrcv();
  180. return iicreceivelast();
  181. }
  182.  
  183.  
  184. // *** Alarms ***
  185.  
  186. void setAlarm(uint16_t msDelay)
  187. {
  188. alarmTime = currentTime + msDelay;
  189. alarmSet = 1;
  190. alarmSignaled = 0;
  191. }
  192.  
  193.  
  194. void delay(uint16_t msec)
  195. {
  196. TC7 = TCNT + 24000; // Set initial time
  197. setAlarm(msec);
  198. while(!alarmSignaled) {};
  199. }
  200.  
  201.  
  202.  
  203. /* Interrupt EMN */
  204.  
  205. // interrupt(((0x10000-Vtimch7)/2)-1) void TC7_ISR(void){
  206. // the line above is to make it portable between differen
  207. // Freescale processors
  208. // The symbols for each interrupt ( in this case Vtimch7 )'
  209. // are defined in the provided variable definition file
  210. // I am usign a much simpler definition ( vector number)
  211. // that is easier to understand
  212.  
  213. interrupt 15 void TC7_ISR(void) {
  214.  
  215. TC7 =TCNT + 24000; // interrupt every msec assuming prescaler =1
  216. TFLG1=TFLG1 | TFLG1_C7F_MASK;
  217. currentTime++;
  218. if (alarmSet && currentTime == alarmTime)
  219. {
  220. alarmSignaled = 1;
  221. alarmSet = 0;
  222. }
  223. // PORTB=PORTB+1; // count (debugging)
  224. }
  225.  
  226.  
  227.  
  228. void Init_TC7 (void) {
  229.  
  230. _asm SEI;
  231.  
  232. TSCR1=0x80;
  233. TSCR2=0x00; // prescaler 1, before 32 = 0x04
  234. TIOS=TIOS | TIOS_IOS7_MASK;
  235. TCTL1=0x40;
  236. TIE=TIE | 0x80;
  237.  
  238. _asm CLI;
  239.  
  240. }
  241.  
  242. /*
  243.  
  244. // Millisecond timer
  245. void INTERRUPT timer6(void)
  246. {
  247. TFLG1 &= 0x40; // reset interrupt flag
  248. TC6 += 24000; // interrupt every 1ms
  249. currentTime++;
  250. if (alarmSet && currentTime == alarmTime)
  251. {
  252. alarmSignaled = 1;
  253. alarmSet = 0;
  254. }
  255. }
  256.  
  257. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement