blinky.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * blinky.c
  3. *
  4. * LED blink test to validate STM32F103C8 chips. This test will find
  5. * remarked and cloned low-density devices with:
  6. * - Less than 20kB RAM
  7. * - Less than 64kB Flash
  8. * - Missing peripherals TIM1-4, I2C1-2, SPI1-2
  9. *
  10. * As the LED blinks, a character is written to USART1 at 9600 baud (8n1).
  11. *
  12. * Written & released by Keir Fraser <keir.xen@gmail.com>
  13. *
  14. * This is free and unencumbered software released into the public domain.
  15. * See the file COPYING for more details, or visit <http://unlicense.org>.
  16. */
  17. #define FLASH_KB 64
  18. #define SRAM_KB 20
  19. #define NR_I2C 2
  20. #define NR_SPI 2
  21. #define NR_TIM 4
  22. int EXC_reset(void) __attribute__((alias("main")));
  23. void IRQ_30(void) __attribute__((alias("IRQ_tim4")));
  24. #define IRQ_TIM4 30
  25. void IRQ_11(void) __attribute__((alias("IRQ_dma_tc")));
  26. #define IRQ_DMA 11
  27. /* All exceptions print a simple report and then hang. */
  28. void EXC_nmi(void) __attribute__((alias("EXC_blinky")));
  29. void EXC_hard_fault(void) __attribute__((alias("EXC_blinky")));
  30. void EXC_memory_management_fault(void) __attribute__((alias("EXC_blinky")));
  31. void EXC_bus_fault(void) __attribute__((alias("EXC_blinky")));
  32. void EXC_usage_fault(void) __attribute__((alias("EXC_blinky")));
  33. void EXC_7(void) __attribute__((alias("EXC_blinky")));
  34. void EXC_8(void) __attribute__((alias("EXC_blinky")));
  35. void EXC_9(void) __attribute__((alias("EXC_blinky")));
  36. void EXC_10(void) __attribute__((alias("EXC_blinky")));
  37. void EXC_sv_call(void) __attribute__((alias("EXC_blinky")));
  38. void EXC_12(void) __attribute__((alias("EXC_blinky")));
  39. void EXC_13(void) __attribute__((alias("EXC_blinky")));
  40. void EXC_pend_sv(void) __attribute__((alias("EXC_blinky")));
  41. void EXC_systick(void) __attribute__((alias("EXC_blinky")));
  42. static void EXC_blinky(void)
  43. {
  44. uint8_t exc = (uint8_t)read_special(psr);
  45. /* Extinguish the LED(s) permanently. */
  46. IRQ_global_disable();
  47. printk("**FAILED** [Exception #%u]\n", exc);
  48. gpio_write_pin(gpiob, 12, HIGH);
  49. gpio_write_pin(gpioc, 13, HIGH);
  50. for (;;);
  51. }
  52. static bool_t failed;
  53. static void report(bool_t ok)
  54. {
  55. if (ok) {
  56. printk("OK\n");
  57. } else {
  58. /* Extinguish the LED(s) permanently. */
  59. failed = TRUE;
  60. printk("**FAILED**\n");
  61. gpio_write_pin(gpiob, 12, HIGH);
  62. gpio_write_pin(gpioc, 13, HIGH);
  63. }
  64. }
  65. static void IRQ_tim4(void)
  66. {
  67. static bool_t x;
  68. /* Quiesce the IRQ source. */
  69. tim4->sr = 0;
  70. if (failed) {
  71. IRQx_disable(IRQ_TIM4);
  72. return;
  73. }
  74. /* Blink the LED. */
  75. gpio_write_pin(gpiob, 12, x);
  76. gpio_write_pin(gpioc, 13, x);
  77. x ^= 1;
  78. /* Write to the serial line. */
  79. printk(".");
  80. }
  81. static volatile int dmac;
  82. static void IRQ_dma_tc(void)
  83. {
  84. dma1->ifcr = DMA_IFCR_CGIF(1);
  85. dma1->ch1.ccr = 0;
  86. dmac++;
  87. }
  88. /* Pseudorandom LFSR. */
  89. static uint32_t srand = 0x87a2263c;
  90. static uint32_t rand(void)
  91. {
  92. uint32_t x = srand;
  93. x ^= x << 13;
  94. x ^= x >> 17;
  95. x ^= x << 5;
  96. srand = x;
  97. return x;
  98. }
  99. static void i2c_test(I2C i2c, int nr)
  100. {
  101. printk("Testing I2C%d... ", nr);
  102. i2c->cr1 = 0;
  103. i2c->oar1 = 0x10;
  104. i2c->cr2 = (I2C_CR2_FREQ(36) |
  105. I2C_CR2_ITERREN |
  106. I2C_CR2_ITEVTEN |
  107. I2C_CR2_ITBUFEN);
  108. i2c->cr1 = I2C_CR1_ACK | I2C_CR1_PE;
  109. /*i2c->cr1 = I2C_CR1_ACK | I2C_CR1_PE;*/
  110. /* Fake chips may not latch I2C_CR1_ACK on this 1st write */
  111. if ((i2c->oar1 == 0x10) && (i2c->cr1 == I2C_CR1_PE))
  112. printk("[Fake Chip?] ");
  113. report((i2c->oar1 == 0x10) && (i2c->cr1 == (I2C_CR1_ACK | I2C_CR1_PE)));
  114. }
  115. static void spi_test(SPI spi, int nr)
  116. {
  117. printk("Testing SPI%d... ", nr);
  118. spi->cr2 = (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  119. spi->cr1 = (SPI_CR1_MSTR | /* master */
  120. SPI_CR1_SSM | SPI_CR1_SSI | /* software NSS */
  121. SPI_CR1_SPE | /* enable */
  122. SPI_CR1_DFF | /* 16-bit */
  123. SPI_CR1_CPHA |
  124. SPI_CR1_BR_DIV4);
  125. report(spi->cr2 == (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
  126. }
  127. static void tim_test(TIM tim, int nr)
  128. {
  129. /* Configure to overflow at 2Hz. */
  130. printk("Testing TIM%d... ", nr);
  131. tim->psc = sysclk_us(100)-1;
  132. tim->arr = 5000-1;
  133. tim->dier = TIM_DIER_UIE;
  134. tim->cr2 = 0;
  135. tim->cr1 = TIM_CR1_URS | TIM_CR1_CEN;
  136. report(tim->arr == (5000-1));
  137. }
  138. static void dma_test(void *a, void *b, int nr)
  139. {
  140. /* Fake chips seem to be fussy about completion IRQs: They give extra
  141. * spurious interrupts and get upset if they are disabled (CCR=0) too
  142. * quickly. */
  143. printk("DMA Test #%u... ", nr);
  144. dma1->ifcr = DMA_IFCR_CGIF(1);
  145. dma1->ch1.ccr = 0;
  146. dma1->ch1.cmar = (uint32_t)(unsigned long)a;
  147. dma1->ch1.cpar = (uint32_t)(unsigned long)b;
  148. dma1->ch1.cndtr = 1024;
  149. memset(b, 0x12, 1024); /* scratch the destination */
  150. dmac = 0;
  151. dma1->ch1.ccr = (DMA_CCR_MSIZE_8BIT |
  152. DMA_CCR_PSIZE_8BIT |
  153. DMA_CCR_MINC |
  154. DMA_CCR_PINC |
  155. DMA_CCR_DIR_M2P |
  156. DMA_CCR_MEM2MEM |
  157. DMA_CCR_TCIE |
  158. DMA_CCR_EN);
  159. while (!dmac)
  160. continue;
  161. if (dmac > 1)
  162. printk("[Spurious IRQ: Fake Chip?] ", dmac-1);
  163. if (memcmp(a, b, 1024))
  164. printk("[Bad Data] ");
  165. report((dmac == 1) && !memcmp(a, b, 1024));
  166. }
  167. static void flash_test(void)
  168. {
  169. uint32_t fp, *p;
  170. int i;
  171. /* Erase and write the last page of Flash. Check it reads back okay. */
  172. printk("Testing %ukB Flash... ", FLASH_KB);
  173. fpec_init();
  174. fp = (uint32_t)(_stext + FLASH_KB*1024 - FLASH_PAGE_SIZE);
  175. fpec_page_erase(fp);
  176. memset(_ebss, 0xff, FLASH_PAGE_SIZE);
  177. if (memcmp((void *)fp, _ebss, FLASH_PAGE_SIZE))
  178. goto fail; /* didn't erase ok */
  179. p = (uint32_t *)_ebss;
  180. for (i = 0; i < FLASH_PAGE_SIZE/4; i++)
  181. *p++ = rand();
  182. fpec_write(_ebss, FLASH_PAGE_SIZE, fp);
  183. if (memcmp((void *)fp, _ebss, FLASH_PAGE_SIZE))
  184. goto fail; /* didn't write ok */
  185. report(TRUE);
  186. return;
  187. fail:
  188. report(FALSE);
  189. }
  190. int main(void)
  191. {
  192. uint32_t id, dev_id, rev_id;
  193. /* Relocate DATA. Initialise BSS. */
  194. if (_sdat != _ldat)
  195. memcpy(_sdat, _ldat, _edat-_sdat);
  196. memset(_sbss, 0, _ebss-_sbss);
  197. stm32_init();
  198. rcc->apb1enr |= RCC_APB1ENR_BKPEN | RCC_APB1ENR_PWREN;
  199. console_init();
  200. printk("\n** Blinky Test **\n");
  201. printk("** Keir Fraser <keir.xen@gmail.com>\n");
  202. printk("** https://github.com/keirf/Greaseweazle\n");
  203. /* Configure LED pin(s). LED is connected to VDD. */
  204. gpio_configure_pin(gpiob, 12, GPO_opendrain(_2MHz, HIGH));
  205. gpio_configure_pin(gpioc, 13, GPO_opendrain(_2MHz, HIGH));
  206. printk("Serial = %04x:%04x:%04x:%04x:%04x:%04x\n",
  207. *(volatile uint16_t *)0x1ffff7e8,
  208. *(volatile uint16_t *)0x1ffff7ea,
  209. *(volatile uint16_t *)0x1ffff7ec,
  210. *(volatile uint16_t *)0x1ffff7ee,
  211. *(volatile uint16_t *)0x1ffff7f0,
  212. *(volatile uint16_t *)0x1ffff7f2);
  213. printk("Flash Size = %ukB\n", *(volatile uint16_t *)0x1ffff7e0);
  214. id = dbg->mcu_idcode;
  215. dev_id = id & 0xfff;
  216. rev_id = id >> 16;
  217. printk("Device ID = 0x%04x\n", dev_id);
  218. printk("Revision = 0x%04x\n", rev_id);
  219. if (id != 0) {
  220. /* Erratum 2.3 in STM32F10xx8/B Errata Sheet. */
  221. /* I feel bad outright failing on this, so just warn. */
  222. printk("**WARNING**: 10xx8/B device returned valid IDCODE! Fake?\n");
  223. }
  224. /* Test I2C peripherals. */
  225. #if NR_I2C >= 1
  226. rcc->apb1enr |= RCC_APB1ENR_I2C1EN;
  227. i2c_test(i2c1, 1);
  228. #endif
  229. #if NR_I2C >= 2
  230. rcc->apb1enr |= RCC_APB1ENR_I2C2EN;
  231. i2c_test(i2c2, 2);
  232. #endif
  233. /* Test SPI peripherals. */
  234. #if NR_SPI >= 1
  235. rcc->apb2enr |= RCC_APB2ENR_SPI1EN;
  236. spi_test(spi1, 1);
  237. #endif
  238. #if NR_SPI >= 2
  239. rcc->apb1enr |= RCC_APB1ENR_SPI2EN;
  240. spi_test(spi2, 2);
  241. #endif
  242. /* Test TIM peripherals, set up to overflow at 2Hz. */
  243. #if NR_TIM >= 1
  244. tim_test(tim1, 1);
  245. #endif
  246. #if NR_TIM >= 2
  247. tim_test(tim2, 2);
  248. #endif
  249. #if NR_TIM >= 3
  250. tim_test(tim3, 3);
  251. #endif
  252. #if NR_TIM >= 4
  253. tim_test(tim4, 4);
  254. #endif
  255. /* DMA tests (just simple memory-to-memory). */
  256. dma1->ifcr = DMA_IFCR_CGIF(1);
  257. IRQx_set_prio(IRQ_DMA, TIMER_IRQ_PRI);
  258. IRQx_clear_pending(IRQ_DMA);
  259. IRQx_enable(IRQ_DMA);
  260. dma_test(_stext, _ebss, 1);
  261. dma_test(_stext+1, _ebss, 2);
  262. dma_test(_stext, _ebss+1, 3);
  263. dma_test(_stext+1, _ebss+1, 4);
  264. /* Test Flash. */
  265. flash_test();
  266. /* Enable TIM4 IRQ, to be triggered at 2Hz. */
  267. printk("Enable TIM4 IRQ... ");
  268. IRQx_set_prio(IRQ_TIM4, TIMER_IRQ_PRI);
  269. IRQx_clear_pending(IRQ_TIM4);
  270. IRQx_enable(IRQ_TIM4);
  271. report(TRUE);
  272. /* Endlessly test SRAM by filling with pseudorandom junk and then
  273. * testing the values read back okay. */
  274. printk("Testing %ukB SRAM (endless loop)...", SRAM_KB);
  275. for (;;) {
  276. uint32_t *p = (uint32_t *)_ebss, sr = srand;
  277. while (p < (uint32_t *)(0x20000000 + SRAM_KB*1024))
  278. *p++ = rand();
  279. srand = sr;
  280. p = (uint32_t *)_ebss;
  281. while (p < (uint32_t *)(0x20000000 + SRAM_KB*1024)) {
  282. if (*p++ != rand()) {
  283. report(FALSE);
  284. IRQ_global_disable();
  285. for (;;);
  286. }
  287. }
  288. }
  289. return 0;
  290. }
  291. /*
  292. * Local variables:
  293. * mode: C
  294. * c-file-style: "Linux"
  295. * c-basic-offset: 4
  296. * tab-width: 4
  297. * indent-tabs-mode: nil
  298. * End:
  299. */