board_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-12-12 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #include <rtdevice.h>
  14. #include "board.h"
  15. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  16. #if defined(RT_USING_SFUD)
  17. #include "dev_spi_flash.h"
  18. #include "dev_spi_flash_sfud.h"
  19. #endif
  20. #include "drv_qspi.h"
  21. #define W25X_REG_READSTATUS (0x05)
  22. #define W25X_REG_READSTATUS2 (0x35)
  23. #define W25X_REG_WRITEENABLE (0x06)
  24. #define W25X_REG_WRITESTATUS (0x01)
  25. #define W25X_REG_QUADENABLE (0x02)
  26. static rt_uint8_t SpiFlash_ReadStatusReg(struct rt_qspi_device *qspi_device)
  27. {
  28. rt_uint8_t u8Val;
  29. rt_err_t result = RT_EOK;
  30. rt_uint8_t w25x_txCMD1 = W25X_REG_READSTATUS;
  31. result = rt_qspi_send_then_recv(qspi_device, &w25x_txCMD1, 1, &u8Val, 1);
  32. RT_ASSERT(result > 0);
  33. return u8Val;
  34. }
  35. static rt_uint8_t SpiFlash_ReadStatusReg2(struct rt_qspi_device *qspi_device)
  36. {
  37. rt_uint8_t u8Val;
  38. rt_err_t result = RT_EOK;
  39. rt_uint8_t w25x_txCMD1 = W25X_REG_READSTATUS2;
  40. result = rt_qspi_send_then_recv(qspi_device, &w25x_txCMD1, 1, &u8Val, 1);
  41. RT_ASSERT(result > 0);
  42. return u8Val;
  43. }
  44. static rt_err_t SpiFlash_WriteStatusReg(struct rt_qspi_device *qspi_device, uint8_t u8Value1, uint8_t u8Value2)
  45. {
  46. rt_uint8_t w25x_txCMD1;
  47. rt_uint8_t au8Val[2];
  48. rt_err_t result;
  49. struct rt_qspi_message qspi_message = {0};
  50. /* Enable WE */
  51. w25x_txCMD1 = W25X_REG_WRITEENABLE;
  52. result = rt_qspi_send(qspi_device, &w25x_txCMD1, sizeof(w25x_txCMD1));
  53. if (result != sizeof(w25x_txCMD1))
  54. goto exit_SpiFlash_WriteStatusReg;
  55. /* Prepare status-1, 2 data */
  56. au8Val[0] = u8Value1;
  57. au8Val[1] = u8Value2;
  58. /* 1-bit mode: Instruction+payload */
  59. qspi_message.instruction.content = W25X_REG_WRITESTATUS;
  60. qspi_message.instruction.qspi_lines = 1;
  61. qspi_message.qspi_data_lines = 1;
  62. qspi_message.parent.cs_take = 1;
  63. qspi_message.parent.cs_release = 1;
  64. qspi_message.parent.send_buf = &au8Val[0];
  65. qspi_message.parent.length = sizeof(au8Val);
  66. qspi_message.parent.next = RT_NULL;
  67. if (rt_qspi_transfer_message(qspi_device, &qspi_message) != sizeof(au8Val))
  68. {
  69. result = -RT_ERROR;
  70. }
  71. result = RT_EOK;
  72. exit_SpiFlash_WriteStatusReg:
  73. return result;
  74. }
  75. static void SpiFlash_WaitReady(struct rt_qspi_device *qspi_device)
  76. {
  77. volatile uint8_t u8ReturnValue;
  78. do
  79. {
  80. u8ReturnValue = SpiFlash_ReadStatusReg(qspi_device);
  81. u8ReturnValue = u8ReturnValue & 1;
  82. }
  83. while (u8ReturnValue != 0); // check the BUSY bit
  84. }
  85. static void SpiFlash_EnterQspiMode(struct rt_qspi_device *qspi_device)
  86. {
  87. rt_err_t result = RT_EOK;
  88. uint8_t u8Status1 = SpiFlash_ReadStatusReg(qspi_device);
  89. uint8_t u8Status2 = SpiFlash_ReadStatusReg2(qspi_device);
  90. u8Status2 |= W25X_REG_QUADENABLE;
  91. result = SpiFlash_WriteStatusReg(qspi_device, u8Status1, u8Status2);
  92. RT_ASSERT(result == RT_EOK);
  93. SpiFlash_WaitReady(qspi_device);
  94. }
  95. static void SpiFlash_ExitQspiMode(struct rt_qspi_device *qspi_device)
  96. {
  97. rt_err_t result = RT_EOK;
  98. uint8_t u8Status1 = SpiFlash_ReadStatusReg(qspi_device);
  99. uint8_t u8Status2 = SpiFlash_ReadStatusReg2(qspi_device);
  100. u8Status2 &= ~W25X_REG_QUADENABLE;
  101. result = SpiFlash_WriteStatusReg(qspi_device, u8Status1, u8Status2);
  102. RT_ASSERT(result == RT_EOK);
  103. SpiFlash_WaitReady(qspi_device);
  104. }
  105. static int rt_hw_spiflash_init(void)
  106. {
  107. if (nu_qspi_bus_attach_device("qspi0", "qspi01", 4, SpiFlash_EnterQspiMode, SpiFlash_ExitQspiMode) != RT_EOK)
  108. return -1;
  109. #if defined(RT_USING_SFUD)
  110. if (rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "qspi01") == RT_NULL)
  111. {
  112. return -(RT_ERROR);
  113. }
  114. #endif
  115. return 0;
  116. }
  117. INIT_COMPONENT_EXPORT(rt_hw_spiflash_init);
  118. #endif /* BOARD_USING_STORAGE_SPIFLASH */
  119. #if defined(RT_USING_MTD_NAND) && defined(BSP_USING_FMINAND)
  120. struct rt_mtd_nand_device mtd_partitions[MTD_FMINAND_PARTITION_NUM] =
  121. {
  122. [0] =
  123. {
  124. /*nand0: U-boot, env, rtthread*/
  125. .block_start = 0,
  126. .block_end = 63,
  127. .block_total = 64,
  128. },
  129. [1] =
  130. {
  131. /*nand1: for filesystem mounting*/
  132. .block_start = 64,
  133. .block_end = 1023,
  134. .block_total = 960,
  135. },
  136. [2] =
  137. {
  138. /*nand2: Whole blocks size, overlay*/
  139. .block_start = 0,
  140. .block_end = 1023,
  141. .block_total = 1024,
  142. }
  143. };
  144. #endif
  145. #if defined(BOARD_USING_NAU8822) && defined(NU_PKG_USING_NAU8822)
  146. #include <acodec_nau8822.h>
  147. S_NU_NAU8822_CONFIG sCodecConfig =
  148. {
  149. .i2c_bus_name = "i2c0",
  150. .i2s_bus_name = "sound0",
  151. .pin_phonejack_en = 0,
  152. .pin_phonejack_det = 0,
  153. };
  154. int rt_hw_nau8822_port(void)
  155. {
  156. if (nu_hw_nau8822_init(&sCodecConfig) != RT_EOK)
  157. return -1;
  158. return 0;
  159. }
  160. INIT_COMPONENT_EXPORT(rt_hw_nau8822_port);
  161. #endif /* BOARD_USING_NAU8822 */
  162. #if defined(NU_PKG_USING_ADC_TOUCH)
  163. #include "adc_touch.h"
  164. S_CALIBRATION_MATRIX g_sCalMat = { 13321, -53, -1069280, 96, 8461, -1863312, 65536 };
  165. #endif
  166. #if defined(NU_PKG_USING_TPC_GT911) && defined(BOARD_USING_GT911)
  167. #include "drv_gpio.h"
  168. #include "gt911.h"
  169. #define TPC_RST_PIN NU_GET_PININDEX(NU_PG, 4)
  170. #define TPC_IRQ_PIN NU_GET_PININDEX(NU_PG, 5)
  171. extern int tpc_sample(const char *name);
  172. int rt_hw_gt911_port(void)
  173. {
  174. struct rt_touch_config cfg;
  175. rt_uint8_t rst_pin;
  176. rst_pin = TPC_RST_PIN;
  177. cfg.dev_name = "i2c0";
  178. cfg.irq_pin.pin = TPC_IRQ_PIN;
  179. cfg.irq_pin.mode = PIN_MODE_INPUT_PULLDOWN;
  180. cfg.user_data = &rst_pin;
  181. rt_hw_gt911_init("gt911", &cfg);
  182. return tpc_sample("gt911");
  183. }
  184. INIT_ENV_EXPORT(rt_hw_gt911_port);
  185. #endif /* if defined(NU_PKG_USING_TPC_GT911) && defined(BOARD_USING_GT911) */
  186. #if defined(NU_PKG_USING_TPC_FT5446) && defined(BOARD_USING_FT5446)
  187. #include "drv_gpio.h"
  188. #include "ft5446.h"
  189. #define TPC_RST_PIN NU_GET_PININDEX(NU_PG, 4)
  190. #define TPC_IRQ_PIN NU_GET_PININDEX(NU_PG, 5)
  191. extern int tpc_sample(const char *name);
  192. int rt_hw_gt911_port(void)
  193. {
  194. struct rt_touch_config cfg;
  195. rt_uint8_t rst_pin;
  196. rst_pin = TPC_RST_PIN;
  197. cfg.dev_name = "i2c0";
  198. cfg.irq_pin.pin = TPC_IRQ_PIN;
  199. cfg.irq_pin.mode = PIN_MODE_INPUT;//PIN_MODE_INPUT_PULLDOWN;
  200. cfg.user_data = &rst_pin;
  201. rt_hw_ft5446_init("ft5446", &cfg);
  202. return tpc_sample("ft5446");
  203. }
  204. INIT_ENV_EXPORT(rt_hw_gt911_port);
  205. #endif /* if defined(NU_PKG_USING_TPC_FT5446) && defined(BOARD_USING_FT5446) */
  206. #if defined(BOARD_USING_BUZZER)
  207. #define PWM_DEV_NAME "pwm0"
  208. #define PWM_DEV_CHANNEL (1)
  209. static void PlayRingTone(void)
  210. {
  211. struct rt_device_pwm *pwm_dev;
  212. rt_uint32_t period;
  213. int i, j;
  214. period = 1000;
  215. if ((pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME)) != RT_NULL)
  216. {
  217. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, period);
  218. rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
  219. for (j = 0; j < 3; j++)
  220. {
  221. for (i = 0; i < 10; i++)
  222. {
  223. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, period);
  224. rt_thread_mdelay(50);
  225. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, period / 2);
  226. rt_thread_mdelay(50);
  227. }
  228. /* Mute 2 seconds */
  229. rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, period);
  230. rt_thread_mdelay(2000);
  231. }
  232. rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL);
  233. }
  234. else
  235. {
  236. rt_kprintf("Can't find %s\n", PWM_DEV_NAME);
  237. }
  238. }
  239. #if defined(BOARD_USING_LCM)
  240. #if defined(PKG_USING_GUIENGINE)
  241. #include <rtgui/driver.h>
  242. #endif
  243. #if defined(RT_USING_PIN)
  244. #include <drv_gpio.h>
  245. /* defined the LCM_BLEN pin: PH3 */
  246. #define LCM_BACKLIGHT_CTRL NU_GET_PININDEX(NU_PH, 3)
  247. #endif
  248. #define PWM_DEV_NAME "pwm0"
  249. #define LCM_PWM_CHANNEL (0)
  250. void nu_lcd_backlight_on(void)
  251. {
  252. struct rt_device_pwm *pwm_dev;
  253. if ((pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME)) != RT_NULL)
  254. {
  255. rt_pwm_enable(pwm_dev, LCM_PWM_CHANNEL);
  256. rt_pwm_set(pwm_dev, LCM_PWM_CHANNEL, 100000, 100);
  257. }
  258. else
  259. {
  260. rt_kprintf("Can't find %s\n", PWM_DEV_NAME);
  261. }
  262. rt_pin_mode(LCM_BACKLIGHT_CTRL, PIN_MODE_OUTPUT);
  263. rt_pin_write(LCM_BACKLIGHT_CTRL, PIN_HIGH);
  264. }
  265. void nu_lcd_backlight_off(void)
  266. {
  267. struct rt_device_pwm *pwm_dev;
  268. if ((pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME)) != RT_NULL)
  269. {
  270. rt_pwm_disable(pwm_dev, LCM_PWM_CHANNEL);
  271. }
  272. else
  273. {
  274. rt_kprintf("Can't find %s\n", PWM_DEV_NAME);
  275. }
  276. rt_pin_mode(LCM_BACKLIGHT_CTRL, PIN_MODE_OUTPUT);
  277. rt_pin_write(LCM_BACKLIGHT_CTRL, PIN_LOW);
  278. }
  279. int rt_hw_lcm_port(void)
  280. {
  281. #if defined(PKG_USING_GUIENGINE)
  282. rt_device_t lcm_vpost;
  283. lcm_vpost = rt_device_find("lcd");
  284. if (lcm_vpost)
  285. {
  286. rtgui_graphic_set_device(lcm_vpost);
  287. }
  288. #endif
  289. return 0;
  290. }
  291. INIT_COMPONENT_EXPORT(rt_hw_lcm_port);
  292. #endif /* BOARD_USING_LCM */
  293. int buzzer_test(void)
  294. {
  295. PlayRingTone();
  296. return 0;
  297. }
  298. #ifdef FINSH_USING_MSH
  299. MSH_CMD_EXPORT(buzzer_test, Buzzer - Play ring tone);
  300. #endif
  301. #endif /* BOARD_USING_BUZZER */
  302. #if defined(BOARD_USING_RS485)
  303. #include <drv_uart.h>
  304. int test_rs485(int argc, char **argv)
  305. {
  306. rt_device_t serial;
  307. char txbuf[16];
  308. rt_err_t ret;
  309. int str_len;
  310. if (argc < 2)
  311. goto exit_test_rs485;
  312. serial = rt_device_find(argv[1]);
  313. if (!serial)
  314. {
  315. rt_kprintf("Can't find %s. EXIT.\n", argv[1]);
  316. goto exit_test_rs485;
  317. }
  318. /* Interrupt RX */
  319. ret = rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  320. RT_ASSERT(ret == RT_EOK);
  321. /* Nuvoton private command */
  322. nu_uart_set_rs485aud((struct rt_serial_device *)serial, RT_FALSE);
  323. rt_snprintf(&txbuf[0], sizeof(txbuf), "Hello World!\r\n");
  324. str_len = rt_strlen(txbuf);
  325. /* Say Hello */
  326. ret = rt_device_write(serial, 0, &txbuf[0], str_len);
  327. RT_ASSERT(ret == str_len);
  328. ret = rt_device_close(serial);
  329. RT_ASSERT(ret == RT_EOK);
  330. return 0;
  331. exit_test_rs485:
  332. return -1;
  333. }
  334. MSH_CMD_EXPORT(test_rs485, test rs485 communication);
  335. #endif //defined(BOARD_USING_RS485)