main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* STM32L031 Eval Board: I2C Test */
  2. #include "stm32l031xx.h"
  3. #include "delay.h"
  4. #include "u8x8.h"
  5. /*=======================================================================*/
  6. /* external functions */
  7. uint8_t u8x8_gpio_and_delay_stm32l0(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  8. /*=======================================================================*/
  9. /* global variables */
  10. u8x8_t u8x8; // u8x8 object
  11. uint8_t u8x8_x, u8x8_y; // current position on the screen
  12. volatile unsigned long SysTickCount = 0;
  13. /*=======================================================================*/
  14. void __attribute__ ((interrupt, used)) SysTick_Handler(void)
  15. {
  16. SysTickCount++;
  17. }
  18. void setHSIClock()
  19. {
  20. /* test if the current clock source is something else than HSI */
  21. if ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  22. {
  23. /* enable HSI */
  24. RCC->CR |= RCC_CR_HSION;
  25. /* wait until HSI becomes ready */
  26. while ( (RCC->CR & RCC_CR_HSIRDY) == 0 )
  27. ;
  28. /* enable the HSI "divide by 4" bit */
  29. RCC->CR |= (uint32_t)(RCC_CR_HSIDIVEN);
  30. /* wait until the "divide by 4" flag is enabled */
  31. while((RCC->CR & RCC_CR_HSIDIVF) == 0)
  32. ;
  33. /* then use the HSI clock */
  34. RCC->CFGR = (RCC->CFGR & (uint32_t) (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;
  35. /* wait until HSI clock is used */
  36. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  37. ;
  38. }
  39. /* disable PLL */
  40. RCC->CR &= (uint32_t)(~RCC_CR_PLLON);
  41. /* wait until PLL is inactive */
  42. while((RCC->CR & RCC_CR_PLLRDY) != 0)
  43. ;
  44. /* set latency to 1 wait state */
  45. FLASH->ACR |= FLASH_ACR_LATENCY;
  46. /* At this point the HSI runs with 4 MHz */
  47. /* Multiply by 16 device by 2 --> 32 MHz */
  48. RCC->CFGR = (RCC->CFGR & (~(RCC_CFGR_PLLMUL| RCC_CFGR_PLLDIV ))) | (RCC_CFGR_PLLMUL16 | RCC_CFGR_PLLDIV2);
  49. /* enable PLL */
  50. RCC->CR |= RCC_CR_PLLON;
  51. /* wait until the PLL is ready */
  52. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  53. ;
  54. /* use the PLL has clock source */
  55. RCC->CFGR |= (uint32_t) (RCC_CFGR_SW_PLL);
  56. /* wait until the PLL source is active */
  57. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
  58. ;
  59. }
  60. /*
  61. Enable several power regions: PWR, GPIOA
  62. This must be executed after each reset.
  63. */
  64. void startUp(void)
  65. {
  66. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  67. RCC->APB1ENR |= RCC_APB1ENR_PWREN; /* enable power interface (PWR) */
  68. PWR->CR |= PWR_CR_DBP; /* activate write access to RCC->CSR and RTC */
  69. SysTick->LOAD = (SystemCoreClock/1000)*50 - 1; /* 50ms task */
  70. SysTick->VAL = 0;
  71. SysTick->CTRL = 7; /* enable, generate interrupt (SysTick_Handler), do not divide by 2 */
  72. }
  73. /*=======================================================================*/
  74. /* u8x8 display procedures */
  75. void initDisplay(void)
  76. {
  77. u8x8_Setup(&u8x8, u8x8_d_ssd1306_128x64_noname, u8x8_cad_ssd13xx_i2c, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_stm32l0);
  78. u8x8_InitDisplay(&u8x8);
  79. u8x8_ClearDisplay(&u8x8);
  80. u8x8_SetPowerSave(&u8x8, 0);
  81. u8x8_SetFont(&u8x8, u8x8_font_amstrad_cpc_extended_r);
  82. u8x8_x = 0;
  83. u8x8_y = 0;
  84. }
  85. void outChar(uint8_t c)
  86. {
  87. if ( u8x8_x >= u8x8_GetCols(&u8x8) )
  88. {
  89. u8x8_x = 0;
  90. u8x8_y++;
  91. }
  92. u8x8_DrawGlyph(&u8x8, u8x8_x, u8x8_y, c);
  93. u8x8_x++;
  94. }
  95. void outStr(const char *s)
  96. {
  97. while( *s )
  98. outChar(*s++);
  99. }
  100. void outHexHalfByte(uint8_t b)
  101. {
  102. b &= 0x0f;
  103. if ( b < 10 )
  104. outChar(b+'0');
  105. else
  106. outChar(b+'a'-10);
  107. }
  108. void outHex8(uint8_t b)
  109. {
  110. outHexHalfByte(b >> 4);
  111. outHexHalfByte(b);
  112. }
  113. void outHex16(uint16_t v)
  114. {
  115. outHex8(v>>8);
  116. outHex8(v);
  117. }
  118. void outHex32(uint32_t v)
  119. {
  120. outHex16(v>>16);
  121. outHex16(v);
  122. }
  123. void setRow(uint8_t r)
  124. {
  125. u8x8_x = 0;
  126. u8x8_y = r;
  127. }
  128. /*==============================================*/
  129. volatile unsigned char i2c_mem[256]; /* contains data, which read or written */
  130. volatile unsigned char i2c_idx; /* the current index into i2c_mem */
  131. volatile unsigned char i2c_is_write_idx; /* write state */
  132. volatile uint16_t i2c_total_irq_cnt;
  133. volatile uint16_t i2c_TXIS_cnt;
  134. volatile uint16_t i2c_RXNE_cnt;
  135. void i2c_mem_reset_write(void)
  136. {
  137. i2c_is_write_idx = 1;
  138. }
  139. void i2c_mem_init(void)
  140. {
  141. i2c_idx = 0;
  142. i2c_mem_reset_write();
  143. }
  144. void i2c_mem_set_index(unsigned char value)
  145. {
  146. i2c_idx = value;
  147. i2c_is_write_idx = 0;
  148. }
  149. void i2c_mem_write_via_index(unsigned char value)
  150. {
  151. i2c_mem[i2c_idx++] = value;
  152. }
  153. unsigned char i2c_mem_read(void)
  154. {
  155. i2c_mem_reset_write();
  156. i2c_idx++;
  157. return i2c_mem[i2c_idx];
  158. }
  159. void i2c_mem_write(unsigned char value)
  160. {
  161. if ( i2c_is_write_idx != 0 )
  162. {
  163. i2c_mem_set_index(value);
  164. }
  165. else
  166. {
  167. i2c_is_write_idx = 0;
  168. i2c_mem_write_via_index(value);
  169. }
  170. }
  171. /* address: I2C address multiplied by 2 */
  172. /* Pins PA9 (SCL) and PA10 (SDA) */
  173. void i2c_hw_init(unsigned char address)
  174. {
  175. RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; /* Enable clock for I2C */
  176. RCC->IOPENR |= RCC_IOPENR_IOPAEN; /* Enable clock for GPIO Port A */
  177. __NOP(); /* extra delay for clock stabilization required? */
  178. __NOP();
  179. /* configure io */
  180. GPIOA->MODER &= ~GPIO_MODER_MODE9; /* clear mode for PA9 */
  181. GPIOA->MODER |= GPIO_MODER_MODE9_1; /* alt fn */
  182. GPIOA->OTYPER |= GPIO_OTYPER_OT_9; /* open drain */
  183. GPIOA->AFR[1] &= ~(15<<4); /* Clear Alternate Function PA9 */
  184. GPIOA->AFR[1] |= 1<<4; /* I2C Alternate Function PA9 */
  185. GPIOA->MODER &= ~GPIO_MODER_MODE10; /* clear mode for PA10 */
  186. GPIOA->MODER |= GPIO_MODER_MODE10_1; /* alt fn */
  187. GPIOA->OTYPER |= GPIO_OTYPER_OT_10; /* open drain */
  188. GPIOA->AFR[1] &= ~(15<<8); /* Clear Alternate Function PA10 */
  189. GPIOA->AFR[1] |= 1<<8; /* I2C Alternate Function PA10 */
  190. RCC->CCIPR &= ~RCC_CCIPR_I2C1SEL; /* write 00 to the I2C clk selection register */
  191. RCC->CCIPR |= RCC_CCIPR_I2C1SEL_0; /* select system clock (01) */
  192. /* I2C init flow chart: Clear PE bit */
  193. I2C1->CR1 &= ~I2C_CR1_PE;
  194. /* I2C init flow chart: Configure filter */
  195. /* leave at defaults */
  196. /* I2C init flow chart: Configure timing */
  197. /*
  198. standard mode 100kHz configuration
  199. SYSCLK = I2CCLK = 32 MHz
  200. PRESC = 6 bits 28..31
  201. SCLL = 0x13 bits 0..7
  202. SCLH = 0x0f bits 8..15
  203. SDADEL = 0x02 bits 16..19
  204. SCLDEL = 0x04 bits 20..23
  205. */
  206. I2C1->TIMINGR = 0x60420f13;
  207. /* I2C init flow chart: Configure NOSTRECH */
  208. I2C1->CR1 |= I2C_CR1_NOSTRETCH;
  209. /* I2C init flow chart: Enable I2C */
  210. I2C1->CR1 |= I2C_CR1_PE;
  211. /* disable OAR1 for reconfiguration */
  212. I2C1->OAR1 &= ~I2C_OAR1_OA1EN;
  213. I2C1->OAR1 = address;
  214. I2C1->OAR1 |= I2C_OAR1_OA1EN;
  215. /* enable interrupts */
  216. I2C1->CR1 |= I2C_CR1_STOPIE;
  217. I2C1->CR1 |= I2C_CR1_NACKIE;
  218. //I2C1->CR1 |= I2C_CR1_ADDRIE;
  219. I2C1->CR1 |= I2C_CR1_RXIE;
  220. I2C1->CR1 |= I2C_CR1_TXIE;
  221. /* load first value into TXDR register */
  222. I2C1->TXDR = i2c_mem[i2c_idx];
  223. /* enable IRQ in NVIC */
  224. NVIC_SetPriority(I2C1_IRQn, 0);
  225. NVIC_EnableIRQ(I2C1_IRQn);
  226. }
  227. void i2c_init()
  228. {
  229. i2c_mem_init();
  230. i2c_hw_init(7*2);
  231. }
  232. void __attribute__ ((interrupt, used)) I2C1_IRQHandler(void)
  233. {
  234. unsigned long isr = I2C1->ISR;
  235. i2c_total_irq_cnt ++;
  236. if ( isr & I2C_ISR_TXIS )
  237. {
  238. i2c_TXIS_cnt++;
  239. I2C1->TXDR = i2c_mem_read();
  240. }
  241. else if ( isr & I2C_ISR_RXNE )
  242. {
  243. i2c_RXNE_cnt++;
  244. i2c_mem_write(I2C1->RXDR);
  245. I2C1->ISR |= I2C_ISR_TXE; // allow overwriting the TCDR with new data
  246. I2C1->TXDR = i2c_mem[i2c_idx];
  247. }
  248. else if ( isr & I2C_ISR_STOPF )
  249. {
  250. I2C1->ICR = I2C_ICR_STOPCF;
  251. I2C1->ISR |= I2C_ISR_TXE; // allow overwriting the TCDR with new data
  252. I2C1->TXDR = i2c_mem[i2c_idx];
  253. i2c_mem_reset_write();
  254. }
  255. else if ( isr & I2C_ISR_NACKF )
  256. {
  257. I2C1->ICR = I2C_ICR_NACKCF;
  258. I2C1->ISR |= I2C_ISR_TXE; // allow overwriting the TCDR with new data
  259. I2C1->TXDR = i2c_mem[i2c_idx];
  260. i2c_mem_reset_write();
  261. }
  262. else if ( isr & I2C_ISR_ADDR )
  263. {
  264. /* not required, the addr match interrupt is not enabled */
  265. I2C1->ICR = I2C_ICR_ADDRCF;
  266. I2C1->ISR |= I2C_ISR_TXE; // allow overwriting the TCDR with new data
  267. I2C1->TXDR = i2c_mem[i2c_idx];
  268. i2c_mem_reset_write();
  269. }
  270. /* if at any time the addr match is set, clear the flag */
  271. /* not sure, whether this is required */
  272. if ( isr & I2C_ISR_ADDR )
  273. {
  274. I2C1->ICR = I2C_ICR_ADDRCF;
  275. }
  276. }
  277. /*==============================================*/
  278. int main()
  279. {
  280. setHSIClock();
  281. startUp();
  282. initDisplay(); /* aktivate display */
  283. i2c_init();
  284. __enable_irq();
  285. setRow(0); outStr("Hello World!");
  286. for(;;)
  287. {
  288. setRow(2); outHex32(SysTickCount);
  289. setRow(3); outHex16(i2c_total_irq_cnt);
  290. setRow(4); outHex16(i2c_TXIS_cnt); outStr(" "); outHex16(i2c_RXNE_cnt);
  291. setRow(5); outStr("I2C_ISR:"); outHex32(I2C1->ISR);
  292. setRow(6); outStr("idx: "); outHex8(i2c_idx);
  293. setRow(7); outHex8(i2c_mem[0]); outStr(" "); outHex8(i2c_mem[1]); outStr(" "); outHex8(i2c_mem[2]);
  294. }
  295. }