board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <string.h>
  17. #ifdef ENABLE_FMRADIO
  18. #include "app/fm.h"
  19. #endif
  20. #include "board.h"
  21. #include "bsp/dp32g030/gpio.h"
  22. #include "bsp/dp32g030/portcon.h"
  23. #include "bsp/dp32g030/saradc.h"
  24. #include "bsp/dp32g030/syscon.h"
  25. #include "driver/adc.h"
  26. #include "driver/backlight.h"
  27. #ifdef ENABLE_FMRADIO
  28. #include "driver/bk1080.h"
  29. #endif
  30. #include "driver/crc.h"
  31. #include "driver/eeprom.h"
  32. #include "driver/flash.h"
  33. #include "driver/gpio.h"
  34. #include "driver/system.h"
  35. #include "driver/st7565.h"
  36. #include "frequencies.h"
  37. #include "helper/battery.h"
  38. #include "misc.h"
  39. #include "settings.h"
  40. #if defined(ENABLE_OVERLAY)
  41. #include "sram-overlay.h"
  42. #endif
  43. #if defined(ENABLE_OVERLAY)
  44. void BOARD_FLASH_Init(void)
  45. {
  46. FLASH_Init(FLASH_READ_MODE_1_CYCLE);
  47. FLASH_ConfigureTrimValues();
  48. SYSTEM_ConfigureClocks();
  49. overlay_FLASH_MainClock = 48000000;
  50. overlay_FLASH_ClockMultiplier = 48;
  51. FLASH_Init(FLASH_READ_MODE_2_CYCLE);
  52. }
  53. #endif
  54. void BOARD_GPIO_Init(void)
  55. {
  56. GPIOA->DIR |= 0
  57. // A7 = UART1 TX default as OUTPUT from bootloader!
  58. // A8 = UART1 RX default as INPUT from bootloader!
  59. // Key pad + I2C
  60. | GPIO_DIR_10_BITS_OUTPUT
  61. // Key pad + I2C
  62. | GPIO_DIR_11_BITS_OUTPUT
  63. // Key pad + Voice chip
  64. | GPIO_DIR_12_BITS_OUTPUT
  65. // Key pad + Voice chip
  66. | GPIO_DIR_13_BITS_OUTPUT
  67. ;
  68. GPIOA->DIR &= ~(0
  69. // Key pad
  70. | GPIO_DIR_3_MASK // INPUT
  71. // Key pad
  72. | GPIO_DIR_4_MASK // INPUT
  73. // Key pad
  74. | GPIO_DIR_5_MASK // INPUT
  75. // Key pad
  76. | GPIO_DIR_6_MASK // INPUT
  77. );
  78. GPIOB->DIR |= 0
  79. // ST7565
  80. | GPIO_DIR_9_BITS_OUTPUT
  81. // ST7565 + SWD IO
  82. | GPIO_DIR_11_BITS_OUTPUT
  83. // B14 = SWD_CLK assumed INPUT by default
  84. // BK1080
  85. | GPIO_DIR_15_BITS_OUTPUT
  86. ;
  87. GPIOC->DIR |= 0
  88. // BK4819 SCN
  89. | GPIO_DIR_0_BITS_OUTPUT
  90. // BK4819 SCL
  91. | GPIO_DIR_1_BITS_OUTPUT
  92. // BK4819 SDA
  93. | GPIO_DIR_2_BITS_OUTPUT
  94. // Flash light
  95. | GPIO_DIR_3_BITS_OUTPUT
  96. // Speaker
  97. | GPIO_DIR_4_BITS_OUTPUT
  98. ;
  99. GPIOC->DIR &= ~(0
  100. // PTT button
  101. | GPIO_DIR_5_MASK // INPUT
  102. );
  103. #if defined(ENABLE_FMRADIO)
  104. GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_BK1080);
  105. #endif
  106. }
  107. void BOARD_PORTCON_Init(void)
  108. {
  109. // PORT A pin selection
  110. PORTCON_PORTA_SEL0 &= ~(0
  111. // Key pad
  112. | PORTCON_PORTA_SEL0_A3_MASK
  113. // Key pad
  114. | PORTCON_PORTA_SEL0_A4_MASK
  115. // Key pad
  116. | PORTCON_PORTA_SEL0_A5_MASK
  117. // Key pad
  118. | PORTCON_PORTA_SEL0_A6_MASK
  119. );
  120. PORTCON_PORTA_SEL0 |= 0
  121. // Key pad
  122. | PORTCON_PORTA_SEL0_A3_BITS_GPIOA3
  123. // Key pad
  124. | PORTCON_PORTA_SEL0_A4_BITS_GPIOA4
  125. // Key pad
  126. | PORTCON_PORTA_SEL0_A5_BITS_GPIOA5
  127. // Key pad
  128. | PORTCON_PORTA_SEL0_A6_BITS_GPIOA6
  129. // UART1 TX, wasn't cleared in previous step / relying on default value!
  130. | PORTCON_PORTA_SEL0_A7_BITS_UART1_TX
  131. ;
  132. PORTCON_PORTA_SEL1 &= ~(0
  133. // Key pad + I2C
  134. | PORTCON_PORTA_SEL1_A10_MASK
  135. // Key pad + I2C
  136. | PORTCON_PORTA_SEL1_A11_MASK
  137. // Key pad + Voice chip
  138. | PORTCON_PORTA_SEL1_A12_MASK
  139. // Key pad + Voice chip
  140. | PORTCON_PORTA_SEL1_A13_MASK
  141. );
  142. PORTCON_PORTA_SEL1 |= 0
  143. // UART1 RX, wasn't cleared in previous step / relying on default value!
  144. | PORTCON_PORTA_SEL1_A8_BITS_UART1_RX
  145. // Battery voltage, wasn't cleared in previous step / relying on default value!
  146. | PORTCON_PORTA_SEL1_A9_BITS_SARADC_CH4
  147. // Key pad + I2C
  148. | PORTCON_PORTA_SEL1_A10_BITS_GPIOA10
  149. // Key pad + I2C
  150. | PORTCON_PORTA_SEL1_A11_BITS_GPIOA11
  151. // Key pad + Voice chip
  152. | PORTCON_PORTA_SEL1_A12_BITS_GPIOA12
  153. // Key pad + Voice chip
  154. | PORTCON_PORTA_SEL1_A13_BITS_GPIOA13
  155. // Battery Current, wasn't cleared in previous step / relying on default value!
  156. | PORTCON_PORTA_SEL1_A14_BITS_SARADC_CH9
  157. ;
  158. // PORT B pin selection
  159. PORTCON_PORTB_SEL0 &= ~(0
  160. // SPI0 SSN
  161. | PORTCON_PORTB_SEL0_B7_MASK
  162. );
  163. PORTCON_PORTB_SEL0 |= 0
  164. // SPI0 SSN
  165. | PORTCON_PORTB_SEL0_B7_BITS_SPI0_SSN
  166. ;
  167. PORTCON_PORTB_SEL1 &= ~(0
  168. // ST7565
  169. | PORTCON_PORTB_SEL1_B9_MASK
  170. // ST7565 + SWD IO
  171. | PORTCON_PORTB_SEL1_B11_MASK
  172. // SWD CLK
  173. | PORTCON_PORTB_SEL1_B14_MASK
  174. // BK1080
  175. | PORTCON_PORTB_SEL1_B15_MASK
  176. );
  177. PORTCON_PORTB_SEL1 |= 0
  178. // SPI0 CLK, wasn't cleared in previous step / relying on default value!
  179. | PORTCON_PORTB_SEL1_B8_BITS_SPI0_CLK
  180. // ST7565
  181. | PORTCON_PORTB_SEL1_B9_BITS_GPIOB9
  182. // SPI0 MOSI, wasn't cleared in previous step / relying on default value!
  183. | PORTCON_PORTB_SEL1_B10_BITS_SPI0_MOSI
  184. #if defined(ENABLE_SWD)
  185. // SWD IO
  186. | PORTCON_PORTB_SEL1_B11_BITS_SWDIO
  187. // SWD CLK
  188. | PORTCON_PORTB_SEL1_B14_BITS_SWCLK
  189. #else
  190. // ST7565
  191. | PORTCON_PORTB_SEL1_B11_BITS_GPIOB11
  192. #endif
  193. ;
  194. // PORT C pin selection
  195. PORTCON_PORTC_SEL0 &= ~(0
  196. // BK4819 SCN
  197. | PORTCON_PORTC_SEL0_C0_MASK
  198. // BK4819 SCL
  199. | PORTCON_PORTC_SEL0_C1_MASK
  200. // BK4819 SDA
  201. | PORTCON_PORTC_SEL0_C2_MASK
  202. // Flash light
  203. | PORTCON_PORTC_SEL0_C3_MASK
  204. // Speaker
  205. | PORTCON_PORTC_SEL0_C4_MASK
  206. // PTT button
  207. | PORTCON_PORTC_SEL0_C5_MASK
  208. );
  209. // PORT A pin configuration
  210. PORTCON_PORTA_IE |= 0
  211. // Keypad
  212. | PORTCON_PORTA_IE_A3_BITS_ENABLE
  213. // Keypad
  214. | PORTCON_PORTA_IE_A4_BITS_ENABLE
  215. // Keypad
  216. | PORTCON_PORTA_IE_A5_BITS_ENABLE
  217. // Keypad
  218. | PORTCON_PORTA_IE_A6_BITS_ENABLE
  219. // A7 = UART1 TX disabled by default
  220. // UART1 RX
  221. | PORTCON_PORTA_IE_A8_BITS_ENABLE
  222. ;
  223. PORTCON_PORTA_IE &= ~(0
  224. // Keypad + I2C
  225. | PORTCON_PORTA_IE_A10_MASK
  226. // Keypad + I2C
  227. | PORTCON_PORTA_IE_A11_MASK
  228. // Keypad + Voice chip
  229. | PORTCON_PORTA_IE_A12_MASK
  230. // Keypad + Voice chip
  231. | PORTCON_PORTA_IE_A13_MASK
  232. );
  233. PORTCON_PORTA_PU |= 0
  234. // Keypad
  235. | PORTCON_PORTA_PU_A3_BITS_ENABLE
  236. // Keypad
  237. | PORTCON_PORTA_PU_A4_BITS_ENABLE
  238. // Keypad
  239. | PORTCON_PORTA_PU_A5_BITS_ENABLE
  240. // Keypad
  241. | PORTCON_PORTA_PU_A6_BITS_ENABLE
  242. ;
  243. PORTCON_PORTA_PU &= ~(0
  244. // Keypad + I2C
  245. | PORTCON_PORTA_PU_A10_MASK
  246. // Keypad + I2C
  247. | PORTCON_PORTA_PU_A11_MASK
  248. // Keypad + Voice chip
  249. | PORTCON_PORTA_PU_A12_MASK
  250. // Keypad + Voice chip
  251. | PORTCON_PORTA_PU_A13_MASK
  252. );
  253. PORTCON_PORTA_PD &= ~(0
  254. // Keypad
  255. | PORTCON_PORTA_PD_A3_MASK
  256. // Keypad
  257. | PORTCON_PORTA_PD_A4_MASK
  258. // Keypad
  259. | PORTCON_PORTA_PD_A5_MASK
  260. // Keypad
  261. | PORTCON_PORTA_PD_A6_MASK
  262. // Keypad + I2C
  263. | PORTCON_PORTA_PD_A10_MASK
  264. // Keypad + I2C
  265. | PORTCON_PORTA_PD_A11_MASK
  266. // Keypad + Voice chip
  267. | PORTCON_PORTA_PD_A12_MASK
  268. // Keypad + Voice chip
  269. | PORTCON_PORTA_PD_A13_MASK
  270. );
  271. PORTCON_PORTA_OD |= 0
  272. // Keypad
  273. | PORTCON_PORTA_OD_A3_BITS_ENABLE
  274. // Keypad
  275. | PORTCON_PORTA_OD_A4_BITS_ENABLE
  276. // Keypad
  277. | PORTCON_PORTA_OD_A5_BITS_ENABLE
  278. // Keypad
  279. | PORTCON_PORTA_OD_A6_BITS_ENABLE
  280. ;
  281. PORTCON_PORTA_OD &= ~(0
  282. // Keypad + I2C
  283. | PORTCON_PORTA_OD_A10_MASK
  284. // Keypad + I2C
  285. | PORTCON_PORTA_OD_A11_MASK
  286. // Keypad + Voice chip
  287. | PORTCON_PORTA_OD_A12_MASK
  288. // Keypad + Voice chip
  289. | PORTCON_PORTA_OD_A13_MASK
  290. );
  291. // PORT B pin configuration
  292. PORTCON_PORTB_IE |= 0
  293. | PORTCON_PORTB_IE_B14_BITS_ENABLE
  294. ;
  295. PORTCON_PORTB_IE &= ~(0
  296. // Back light
  297. | PORTCON_PORTB_IE_B6_MASK
  298. // UART1
  299. | PORTCON_PORTB_IE_B7_MASK
  300. | PORTCON_PORTB_IE_B8_MASK
  301. // ST7565
  302. | PORTCON_PORTB_IE_B9_MASK
  303. // SPI0 MOSI
  304. | PORTCON_PORTB_IE_B10_MASK
  305. #if !defined(ENABLE_SWD)
  306. // ST7565
  307. | PORTCON_PORTB_IE_B11_MASK
  308. #endif
  309. // BK1080
  310. | PORTCON_PORTB_IE_B15_MASK
  311. );
  312. PORTCON_PORTB_PU &= ~(0
  313. // Back light
  314. | PORTCON_PORTB_PU_B6_MASK
  315. // ST7565
  316. | PORTCON_PORTB_PU_B9_MASK
  317. // ST7565 + SWD IO
  318. | PORTCON_PORTB_PU_B11_MASK
  319. // SWD CLK
  320. | PORTCON_PORTB_PU_B14_MASK
  321. // BK1080
  322. | PORTCON_PORTB_PU_B15_MASK
  323. );
  324. PORTCON_PORTB_PD &= ~(0
  325. // Back light
  326. | PORTCON_PORTB_PD_B6_MASK
  327. // ST7565
  328. | PORTCON_PORTB_PD_B9_MASK
  329. // ST7565 + SWD IO
  330. | PORTCON_PORTB_PD_B11_MASK
  331. // SWD CLK
  332. | PORTCON_PORTB_PD_B14_MASK
  333. // BK1080
  334. | PORTCON_PORTB_PD_B15_MASK
  335. );
  336. PORTCON_PORTB_OD &= ~(0
  337. // Back light
  338. | PORTCON_PORTB_OD_B6_MASK
  339. // ST7565
  340. | PORTCON_PORTB_OD_B9_MASK
  341. // ST7565 + SWD IO
  342. | PORTCON_PORTB_OD_B11_MASK
  343. // BK1080
  344. | PORTCON_PORTB_OD_B15_MASK
  345. );
  346. PORTCON_PORTB_OD |= 0
  347. // SWD CLK
  348. | PORTCON_PORTB_OD_B14_BITS_ENABLE
  349. ;
  350. // PORT C pin configuration
  351. PORTCON_PORTC_IE |= 0
  352. // PTT button
  353. | PORTCON_PORTC_IE_C5_BITS_ENABLE
  354. ;
  355. PORTCON_PORTC_IE &= ~(0
  356. // BK4819 SCN
  357. | PORTCON_PORTC_IE_C0_MASK
  358. // BK4819 SCL
  359. | PORTCON_PORTC_IE_C1_MASK
  360. // BK4819 SDA
  361. | PORTCON_PORTC_IE_C2_MASK
  362. // Flash Light
  363. | PORTCON_PORTC_IE_C3_MASK
  364. // Speaker
  365. | PORTCON_PORTC_IE_C4_MASK
  366. );
  367. PORTCON_PORTC_PU |= 0
  368. // PTT button
  369. | PORTCON_PORTC_PU_C5_BITS_ENABLE
  370. ;
  371. PORTCON_PORTC_PU &= ~(0
  372. // BK4819 SCN
  373. | PORTCON_PORTC_PU_C0_MASK
  374. // BK4819 SCL
  375. | PORTCON_PORTC_PU_C1_MASK
  376. // BK4819 SDA
  377. | PORTCON_PORTC_PU_C2_MASK
  378. // Flash Light
  379. | PORTCON_PORTC_PU_C3_MASK
  380. // Speaker
  381. | PORTCON_PORTC_PU_C4_MASK
  382. );
  383. PORTCON_PORTC_PD &= ~(0
  384. // BK4819 SCN
  385. | PORTCON_PORTC_PD_C0_MASK
  386. // BK4819 SCL
  387. | PORTCON_PORTC_PD_C1_MASK
  388. // BK4819 SDA
  389. | PORTCON_PORTC_PD_C2_MASK
  390. // Flash Light
  391. | PORTCON_PORTC_PD_C3_MASK
  392. // Speaker
  393. | PORTCON_PORTC_PD_C4_MASK
  394. // PTT Button
  395. | PORTCON_PORTC_PD_C5_MASK
  396. );
  397. PORTCON_PORTC_OD &= ~(0
  398. // BK4819 SCN
  399. | PORTCON_PORTC_OD_C0_MASK
  400. // BK4819 SCL
  401. | PORTCON_PORTC_OD_C1_MASK
  402. // BK4819 SDA
  403. | PORTCON_PORTC_OD_C2_MASK
  404. // Flash Light
  405. | PORTCON_PORTC_OD_C3_MASK
  406. // Speaker
  407. | PORTCON_PORTC_OD_C4_MASK
  408. );
  409. PORTCON_PORTC_OD |= 0
  410. // BK4819 SCN
  411. | PORTCON_PORTC_OD_C0_BITS_DISABLE
  412. // BK4819 SCL
  413. | PORTCON_PORTC_OD_C1_BITS_DISABLE
  414. // BK4819 SDA
  415. | PORTCON_PORTC_OD_C2_BITS_DISABLE
  416. // Flash Light
  417. | PORTCON_PORTC_OD_C3_BITS_DISABLE
  418. // Speaker
  419. | PORTCON_PORTC_OD_C4_BITS_DISABLE
  420. // PTT button
  421. | PORTCON_PORTC_OD_C5_BITS_ENABLE
  422. ;
  423. }
  424. void BOARD_ADC_Init(void)
  425. {
  426. ADC_Config_t Config;
  427. Config.CLK_SEL = SYSCON_CLK_SEL_W_SARADC_SMPL_VALUE_DIV2;
  428. Config.CH_SEL = ADC_CH4 | ADC_CH9;
  429. Config.AVG = SARADC_CFG_AVG_VALUE_8_SAMPLE;
  430. Config.CONT = SARADC_CFG_CONT_VALUE_SINGLE;
  431. Config.MEM_MODE = SARADC_CFG_MEM_MODE_VALUE_CHANNEL;
  432. Config.SMPL_CLK = SARADC_CFG_SMPL_CLK_VALUE_INTERNAL;
  433. Config.SMPL_WIN = SARADC_CFG_SMPL_WIN_VALUE_15_CYCLE;
  434. Config.SMPL_SETUP = SARADC_CFG_SMPL_SETUP_VALUE_1_CYCLE;
  435. Config.ADC_TRIG = SARADC_CFG_ADC_TRIG_VALUE_CPU;
  436. Config.CALIB_KD_VALID = SARADC_CALIB_KD_VALID_VALUE_YES;
  437. Config.CALIB_OFFSET_VALID = SARADC_CALIB_OFFSET_VALID_VALUE_YES;
  438. Config.DMA_EN = SARADC_CFG_DMA_EN_VALUE_DISABLE;
  439. Config.IE_CHx_EOC = SARADC_IE_CHx_EOC_VALUE_NONE;
  440. Config.IE_FIFO_FULL = SARADC_IE_FIFO_FULL_VALUE_DISABLE;
  441. Config.IE_FIFO_HFULL = SARADC_IE_FIFO_HFULL_VALUE_DISABLE;
  442. ADC_Configure(&Config);
  443. ADC_Enable();
  444. ADC_SoftReset();
  445. }
  446. void BOARD_ADC_GetBatteryInfo(uint16_t *pVoltage, uint16_t *pCurrent)
  447. {
  448. ADC_Start();
  449. while (!ADC_CheckEndOfConversion(ADC_CH9)) {}
  450. *pVoltage = ADC_GetValue(ADC_CH4);
  451. *pCurrent = ADC_GetValue(ADC_CH9);
  452. }
  453. void BOARD_Init(void)
  454. {
  455. BOARD_PORTCON_Init();
  456. BOARD_GPIO_Init();
  457. BACKLIGHT_InitHardware();
  458. BOARD_ADC_Init();
  459. ST7565_Init();
  460. #ifdef ENABLE_FMRADIO
  461. BK1080_Init0();
  462. #endif
  463. #if defined(ENABLE_UART) || defined(ENABLED_AIRCOPY)
  464. CRC_Init();
  465. #endif
  466. }