devices.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* devices.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Minimalist BSP for IoT-Safe example based on
  22. * ST P-L596G-CELL02 + Quectel BG96 modem
  23. */
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include "devices.h"
  28. #define RTSCTS 0
  29. #define AUTOBR 0
  30. #define NVIC_UART1_IRQN (37)
  31. #define NVIC_UART2_IRQN (38)
  32. static char modem_rx_buf[256];
  33. static uint32_t modem_rx_idx = 0;
  34. static uint32_t modem_parser_idx = 0;
  35. static void usart1_init(void)
  36. {
  37. uint32_t reg;
  38. /* Enable PWR */
  39. RCC_APB1_ENR |= PWR_APB1_CLOCK_ER_VAL;
  40. /* Enable GPIOG */
  41. RCC_AHB2_ENR |= GPIOG_AHB2_CLOCK_ER_VAL;
  42. /* Enable VDDIO2 */
  43. while ((PWR_CR2 & PWR_CR2_IOSV) == 0) {
  44. PWR_CR2 |= PWR_CR2_IOSV;
  45. printf("Turning on VDDIO2\n");
  46. sleep_ms(1000);
  47. }
  48. /* Enable GPIOB */
  49. RCC_AHB2_ENR |= GPIOB_AHB2_CLOCK_ER_VAL;
  50. /* Set mode = AF */
  51. reg = GPIO_MODE(GPIOG_BASE) & ~(0x03 << (USART1_PIN_RX * 2));
  52. GPIO_MODE(GPIOG_BASE) = reg | (0x02 << (USART1_PIN_RX * 2));
  53. reg = GPIO_MODE(GPIOB_BASE) & ~(0x03 << (USART1_PIN_TX * 2));
  54. GPIO_MODE(GPIOB_BASE) = reg | (0x02 << (USART1_PIN_TX * 2));
  55. reg = GPIO_PUPD(GPIOG_BASE) & (0x03 << (USART1_PIN_RX * 2));
  56. reg = GPIO_PUPD(GPIOB_BASE) & (0x03 << (USART1_PIN_TX * 2));
  57. GPIO_PUPD(GPIOB_BASE) = reg | (0x01 << (USART1_PIN_TX * 2));
  58. #if RTSCTS
  59. reg = GPIO_MODE(GPIOG_BASE) & ~(0x03 << (USART1_PIN_RTS * 2));
  60. GPIO_MODE(GPIOG_BASE) = reg | (0x02 << (USART1_PIN_RTS * 2));
  61. GPIO_PUPD(GPIOG_BASE) &= (0x03 << (USART1_PIN_RTS * 2));
  62. reg = GPIO_MODE(GPIOG_BASE) & ~(0x03 << (USART1_PIN_CTS * 2));
  63. GPIO_MODE(GPIOG_BASE) = reg | (0x02 << (USART1_PIN_CTS * 2));
  64. GPIO_PUPD(GPIOG_BASE) &= (0x03 << (USART1_PIN_CTS * 2));
  65. #endif
  66. /* Set alternate functions */
  67. reg = GPIO_AFH(GPIOG_BASE) & ~(0xf << ((USART1_PIN_RX - 8) * 4));
  68. GPIO_AFH(GPIOG_BASE) = reg | (7 << ((USART1_PIN_RX - 8) * 4));
  69. reg = GPIO_AFL(GPIOB_BASE) & ~(0xf << ((USART1_PIN_TX) * 4));
  70. GPIO_AFL(GPIOB_BASE) = reg | (7 << ((USART1_PIN_TX) * 4));
  71. #if RTSCTS
  72. /* RTS/CTS alt fn */
  73. reg = GPIO_AFH(GPIOG_BASE) & ~(0xf << ((USART1_PIN_RTS - 8) * 4));
  74. GPIO_AFH(GPIOG_BASE) = reg | (USART1_AF << ((USART1_PIN_RTS - 8) * 4));
  75. reg = GPIO_AFH(GPIOG_BASE) & ~(0xf << ((USART1_PIN_CTS - 8) * 4));
  76. GPIO_AFH(GPIOG_BASE) = reg | (USART1_AF << ((USART1_PIN_CTS - 8) * 4));
  77. #endif
  78. /* Enable USART clock */
  79. RCC_APB2_ENR |= USART1_APB2_CLOCK_ER_VAL;
  80. }
  81. static void usart2_init(void)
  82. {
  83. uint32_t reg;
  84. RCC_AHB2_ENR |= GPIOD_AHB2_CLOCK_ER_VAL | GPIOA_AHB2_CLOCK_ER_VAL;
  85. /* Set mode = AF */
  86. reg = GPIO_MODE(GPIOD_BASE) & ~(0x03 << (USART2_PIN_RX * 2));
  87. GPIO_MODE(GPIOD_BASE) = reg | (0x02 << (USART2_PIN_RX * 2));
  88. reg = GPIO_MODE(GPIOA_BASE) & ~(0x03 << (USART2_PIN_TX * 2));
  89. GPIO_MODE(GPIOA_BASE) = reg | (0x02 << (USART2_PIN_TX * 2));
  90. /* Set alternate functions */
  91. reg = GPIO_AFL(GPIOD_BASE) & ~(0xf << ((USART2_PIN_RX) * 4));
  92. GPIO_AFL(GPIOD_BASE) = reg | (7 << ((USART2_PIN_RX) * 4));
  93. reg = GPIO_AFL(GPIOA_BASE) & ~(0xf << ((USART2_PIN_TX) * 4));
  94. GPIO_AFL(GPIOA_BASE) = reg | (7 << ((USART2_PIN_TX) * 4));
  95. /* Enable USART clock */
  96. RCC_APB1_ENR |= USART2_APB1_CLOCK_ER_VAL;
  97. }
  98. int usart_tx(uint32_t dev, const uint8_t c)
  99. {
  100. volatile uint32_t reg;
  101. do {
  102. reg = USART_ISR(dev);
  103. } while ((reg & USART_ISR_TXE) == 0);
  104. USART_TDR(dev) = c;
  105. return 1;
  106. }
  107. int usart_rx(uint32_t dev, uint8_t *c)
  108. {
  109. int ret = 0;
  110. if (dev == USART1_BASE) {
  111. if (modem_rx_idx > modem_parser_idx) {
  112. *c = (uint8_t)(modem_rx_buf[modem_parser_idx++]);
  113. if (modem_rx_idx == modem_parser_idx) {
  114. modem_rx_idx = 0;
  115. modem_parser_idx = 0;
  116. }
  117. ret = 1;
  118. }
  119. } else {
  120. volatile uint32_t reg = USART_ISR(dev);
  121. if ((reg & USART_ISR_RXNE) != 0) {
  122. reg = USART_RDR(dev);
  123. *c = (uint8_t)(reg & 0xff);
  124. ret = 1;
  125. }
  126. }
  127. return ret;
  128. }
  129. int usart_init(uint32_t dev, uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
  130. {
  131. int rtscts = 0;
  132. if (dev == USART1_BASE) {
  133. usart1_init();
  134. #if RTSCTS
  135. rtscts = 1;
  136. #endif
  137. }
  138. else if (dev == USART2_BASE)
  139. usart2_init();
  140. else
  141. return -1;
  142. /* Turn off the device */
  143. USART_CR1(dev) &= ~(USART_CR1_ENABLE);
  144. /* Configure for TX + RX */
  145. USART_CR1(dev) |= (USART_CR1_TX_ENABLE | USART_CR1_RX_ENABLE);
  146. /* Configure clock */
  147. USART_BRR(dev) = CLOCK_SPEED / (bitrate);
  148. /* Configure data bits */
  149. if (data == 8)
  150. USART_CR1(dev) &= ~USART_CR1_SYMBOL_LEN;
  151. else
  152. USART_CR1(dev) |= USART_CR1_SYMBOL_LEN;
  153. /* Configure parity */
  154. switch (parity) {
  155. case 'O':
  156. USART_CR1(dev) |= USART_CR1_PARITY_ODD;
  157. /* fall through to enable parity */
  158. /* FALL THROUGH */
  159. case 'E':
  160. USART_CR1(dev) |= USART_CR1_PARITY_ENABLED;
  161. break;
  162. default:
  163. USART_CR1(dev) &= ~(USART_CR1_PARITY_ENABLED | USART_CR1_PARITY_ODD);
  164. }
  165. /* Set stop bits (not supported) */
  166. (void)stop;
  167. /* Set rtscts */
  168. if (rtscts)
  169. USART_CR3(dev) |= USART_CR3_CTSE | USART_CR3_RTSE;
  170. #if AUTOBR
  171. /* Enable ABR */
  172. USART_CR2(dev) |= USART_CR2_ABREN;
  173. #endif
  174. if (dev == USART1_BASE) {
  175. USART_CR1(dev) |= USART_CR1_RXNEIE | USART_CR1_PEIE;
  176. USART_CR3(dev) |= USART_CR3_EIE;
  177. nvic_irq_enable(NVIC_UART1_IRQN);
  178. nvic_irq_setprio(NVIC_UART1_IRQN, 0);
  179. }
  180. /* Turn on uart */
  181. USART_CR1(dev) |= USART_CR1_ENABLE;
  182. return 0;
  183. }
  184. /* STDOUT on USART2 */
  185. int _write(void *r, uint8_t *text, int len)
  186. {
  187. char *p = (char *)text;
  188. (void)r;
  189. while(*p && (p < (char *)(text + len))) {
  190. usart_tx(USART2_BASE, *p);
  191. p++;
  192. }
  193. return len;
  194. }
  195. /* newlib backend calls */
  196. extern unsigned int _start_heap;
  197. void * _sbrk(unsigned int incr)
  198. {
  199. static unsigned char *heap = NULL;
  200. void *old_heap = heap;
  201. if (((incr >> 2) << 2) != incr)
  202. incr = ((incr >> 2) + 1) << 2;
  203. if (old_heap == NULL)
  204. old_heap = heap = (unsigned char *)&_start_heap;
  205. heap += incr;
  206. return old_heap;
  207. }
  208. void * _sbrk_r(unsigned int incr)
  209. {
  210. static unsigned char *heap = NULL;
  211. void *old_heap = heap;
  212. if (((incr >> 2) << 2) != incr)
  213. incr = ((incr >> 2) + 1) << 2;
  214. if (old_heap == NULL)
  215. old_heap = heap = (unsigned char *)&_start_heap;
  216. heap += incr;
  217. return old_heap;
  218. }
  219. int _close(int fd)
  220. {
  221. return -1;
  222. }
  223. int _fstat(int fd)
  224. {
  225. return -1;
  226. }
  227. int _lseek(int fd, int whence, int off)
  228. {
  229. return -1;
  230. }
  231. int _read(uint8_t *buf, int len)
  232. {
  233. return -1;
  234. }
  235. int _isatty(int fd)
  236. {
  237. return 1;
  238. }
  239. /* Clock + waitstates settings */
  240. static void flash_set_waitstates(unsigned int waitstates)
  241. {
  242. uint32_t reg = FLASH_ACR;
  243. if ((reg & FLASH_ACR_LATENCY_MASK) != waitstates)
  244. FLASH_ACR |= ((reg & ~FLASH_ACR_LATENCY_MASK) | waitstates);
  245. }
  246. void clock_pll_on(void)
  247. {
  248. uint32_t reg32;
  249. uint32_t cpu_freq;
  250. uint32_t hpre, ppre1, ppre2;
  251. uint32_t flash_waitstates;
  252. /* Select clock parameters (CPU Speed = 80MHz) */
  253. cpu_freq = 80000000;
  254. flash_waitstates = 4;
  255. flash_set_waitstates(flash_waitstates);
  256. /* Configure + enable internal high-speed oscillator. */
  257. RCC_CR = (RCC_CR & (~RCC_CR_MSIRANGE_Msk)) | RCC_CR_MSIRANGE_6;
  258. RCC_CR |= RCC_CR_MSIRGSEL;
  259. RCC_CR |= RCC_CR_MSION;
  260. DMB();
  261. while ((RCC_CR & RCC_CR_MSIRDY) == 0)
  262. ;
  263. /* Select MSI as SYSCLK source. */
  264. reg32 = RCC_CFGR;
  265. reg32 &= ~(RCC_CFGR_SW_MASK);
  266. RCC_CFGR = (reg32 | RCC_CFGR_SW_MSI);
  267. DMB();
  268. /*
  269. * Set prescalers
  270. */
  271. hpre = RCC_PRESCALER_DIV_NONE;
  272. ppre1 = RCC_PRESCALER_DIV_NONE;
  273. ppre2 = RCC_PRESCALER_DIV_NONE;
  274. reg32 = RCC_CFGR;
  275. reg32 &= ~(RCC_CFGR_HPRE_MASK << RCC_CFGR_HPRE_SHIFT);
  276. RCC_CFGR = (hpre & RCC_CFGR_HPRE_MASK) << RCC_CFGR_HPRE_SHIFT;
  277. DMB();
  278. reg32 = RCC_CFGR;
  279. reg32 &= ~(RCC_CFGR_PPRE1_MASK << RCC_CFGR_PPRE1_SHIFT);
  280. RCC_CFGR = (reg32 | (ppre1 << RCC_CFGR_PPRE1_SHIFT));
  281. DMB();
  282. reg32 &= ~(RCC_CFGR_PPRE2_MASK << RCC_CFGR_PPRE2_SHIFT);
  283. RCC_CFGR = (reg32 | (ppre2 << RCC_CFGR_PPRE2_SHIFT));
  284. DMB();
  285. /* Set PLLCFGR parameter */
  286. RCC_PLLCFGR = PLLCFGR_PLLM | PLLCFGR_PLLN |
  287. PLLCFGR_PLLP | PLLCFGR_PLLQ |
  288. PLLCFGR_PLLR | RCC_PLLCFGR_PLLP_EN |
  289. RCC_PLLCFGR_PLLQ_EN | RCC_PLLCFGR_PLLR_EN |
  290. RCC_PLLCFGR_PLLSRC_MSI;
  291. /* Enable PLL oscillator and wait for it to stabilize. */
  292. RCC_CR |= RCC_CR_PLLON;
  293. DMB();
  294. while ((RCC_CR & RCC_CR_PLLRDY) == 0)
  295. ;
  296. /* Select PLL as SYSCLK source. */
  297. reg32 = RCC_CFGR;
  298. reg32 &= ~(RCC_CFGR_SW_MASK);
  299. RCC_CFGR = (reg32 | RCC_CFGR_SW_PLL);
  300. DMB();
  301. /* Wait for PLL clock to be selected (via SWS, bits 3:2) */
  302. while (((RCC_CFGR >> 2) & RCC_CFGR_SW_MASK) != RCC_CFGR_SW_PLL)
  303. ;
  304. RCC_CCIPR |= (1 << 26);
  305. PWR_CR1 |= PWR_CR1_DBP;
  306. }
  307. /* Modem via STMod+ connector */
  308. static int stmod_en_init = 0;
  309. static void stmod_pin_init(void)
  310. {
  311. uint32_t reg;
  312. RCC_AHB2_ENR |=
  313. GPIOA_AHB2_CLOCK_ER_VAL |
  314. GPIOB_AHB2_CLOCK_ER_VAL |
  315. GPIOC_AHB2_CLOCK_ER_VAL |
  316. GPIOD_AHB2_CLOCK_ER_VAL |
  317. GPIOI_AHB2_CLOCK_ER_VAL;
  318. /* 'enable' pin */
  319. reg = GPIO_MODE(STMOD_EN_PORT) & ~(0x03 << (STMOD_EN_PIN * 2));
  320. GPIO_MODE(STMOD_EN_PORT) = reg | (0x01 << (STMOD_EN_PIN * 2));
  321. /* RST pin */
  322. reg = GPIO_MODE(STMOD_MODEM_RST_PORT) & ~(0x03 << (STMOD_MODEM_RST_PIN * 2));
  323. GPIO_MODE(STMOD_MODEM_RST_PORT) = reg | (0x01 << (STMOD_MODEM_RST_PIN * 2));
  324. /* DTR pin */
  325. reg = GPIO_MODE(STMOD_MODEM_DTR_PORT) & ~(0x03 << (STMOD_MODEM_DTR_PIN * 2));
  326. GPIO_MODE(STMOD_MODEM_DTR_PORT) = reg | (0x01 << (STMOD_MODEM_DTR_PIN * 2));
  327. /* Sim select pins */
  328. reg = GPIO_MODE(STMOD_SIM_SELECT0_PORT) & ~(0x03 << (STMOD_SIM_SELECT0_PIN * 2));
  329. GPIO_MODE(STMOD_SIM_SELECT0_PORT) = reg | (0x01 << (STMOD_SIM_SELECT0_PIN * 2));
  330. reg = GPIO_MODE(STMOD_SIM_SELECT1_PORT) & ~(0x03 << (STMOD_SIM_SELECT1_PIN * 2));
  331. GPIO_MODE(STMOD_SIM_SELECT1_PORT) = reg | (0x01 << (STMOD_SIM_SELECT1_PIN * 2));
  332. }
  333. void stmod_modem_enable(void)
  334. {
  335. if (!stmod_en_init) {
  336. stmod_pin_init();
  337. stmod_en_init = 1;
  338. }
  339. /* initial pin state */
  340. gpio_set(STMOD_EN_PORT, STMOD_EN_PIN);
  341. gpio_set(STMOD_MODEM_RST_PORT, STMOD_MODEM_RST_PIN);
  342. gpio_set(STMOD_MODEM_DTR_PORT, STMOD_MODEM_DTR_PIN);
  343. sleep_ms(200);
  344. gpio_clear(STMOD_MODEM_RST_PORT, STMOD_MODEM_RST_PIN);
  345. gpio_clear(STMOD_EN_PORT, STMOD_EN_PIN);
  346. sleep_ms(2500);
  347. /* ON/OFF sequence to clear state */
  348. gpio_set(STMOD_EN_PORT, STMOD_EN_PIN);
  349. sleep_ms(700);
  350. gpio_clear(STMOD_EN_PORT, STMOD_EN_PIN);
  351. sleep_ms(1000);
  352. gpio_set(STMOD_EN_PORT, STMOD_EN_PIN);
  353. sleep_ms(50);
  354. gpio_clear(STMOD_EN_PORT, STMOD_EN_PIN);
  355. sleep_ms(30);
  356. gpio_set(STMOD_EN_PORT, STMOD_EN_PIN); /* Modem is on. */
  357. printf("Modem booting...\n");
  358. sleep_ms(5000);
  359. printf("Modem is on.\r\n");
  360. }
  361. void stmod_modem_disable(void)
  362. {
  363. if (!stmod_en_init) {
  364. stmod_pin_init();
  365. stmod_en_init = 1;
  366. }
  367. gpio_clear(STMOD_EN_PORT, STMOD_EN_PIN);
  368. }
  369. extern volatile unsigned jiffies;
  370. void systick_enable(void)
  371. {
  372. SYSTICK_RVR = ((CLOCK_SPEED / 1000) - 1);
  373. SYSTICK_CVR = 0;
  374. SYSTICK_CSR |= 0x07;
  375. }
  376. void sleep_ms(unsigned ms)
  377. {
  378. unsigned end = jiffies + ms;
  379. while(jiffies < end)
  380. __asm__ volatile("wfi");
  381. }
  382. void isr_usart1(void)
  383. {
  384. uint32_t reg;
  385. reg = USART_ISR(USART1_BASE);
  386. if (reg & USART_ISR_RXNE) {
  387. modem_rx_buf[modem_rx_idx++] = (char)USART_RDR(USART1_BASE);
  388. } else {
  389. USART_ICR(USART1_BASE) |= 2 | USART_ICR_CMCF; /* FECF + CMCF*/
  390. }
  391. }