board_dev.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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-1-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtdevice.h>
  13. #include <drv_gpio.h>
  14. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  15. #if defined(RT_USING_SFUD)
  16. #include "spi_flash.h"
  17. #include "spi_flash_sfud.h"
  18. #endif
  19. #include "drv_qspi.h"
  20. #define W25X_REG_READSTATUS (0x05)
  21. #define W25X_REG_READSTATUS2 (0x35)
  22. #define W25X_REG_WRITEENABLE (0x06)
  23. #define W25X_REG_WRITESTATUS (0x01)
  24. #define W25X_REG_QUADENABLE (0x02)
  25. static rt_uint8_t SpiFlash_ReadStatusReg(struct rt_qspi_device *qspi_device)
  26. {
  27. rt_uint8_t u8Val;
  28. rt_err_t result = RT_EOK;
  29. rt_uint8_t w25x_txCMD1 = W25X_REG_READSTATUS;
  30. result = rt_qspi_send_then_recv(qspi_device, &w25x_txCMD1, 1, &u8Val, 1);
  31. RT_ASSERT(result > 0);
  32. return u8Val;
  33. }
  34. static rt_uint8_t SpiFlash_ReadStatusReg2(struct rt_qspi_device *qspi_device)
  35. {
  36. rt_uint8_t u8Val;
  37. rt_err_t result = RT_EOK;
  38. rt_uint8_t w25x_txCMD1 = W25X_REG_READSTATUS2;
  39. result = rt_qspi_send_then_recv(qspi_device, &w25x_txCMD1, 1, &u8Val, 1);
  40. RT_ASSERT(result > 0);
  41. return u8Val;
  42. }
  43. static rt_err_t SpiFlash_WriteStatusReg(struct rt_qspi_device *qspi_device, uint8_t u8Value1, uint8_t u8Value2)
  44. {
  45. rt_uint8_t w25x_txCMD1;
  46. rt_uint8_t au8Val[2];
  47. rt_err_t result;
  48. struct rt_qspi_message qspi_message = {0};
  49. /* Enable WE */
  50. w25x_txCMD1 = W25X_REG_WRITEENABLE;
  51. result = rt_qspi_send(qspi_device, &w25x_txCMD1, sizeof(w25x_txCMD1));
  52. if (result != sizeof(w25x_txCMD1))
  53. goto exit_SpiFlash_WriteStatusReg;
  54. /* Prepare status-1, 2 data */
  55. au8Val[0] = u8Value1;
  56. au8Val[1] = u8Value2;
  57. /* 1-bit mode: Instruction+payload */
  58. qspi_message.instruction.content = W25X_REG_WRITESTATUS;
  59. qspi_message.instruction.qspi_lines = 1;
  60. qspi_message.qspi_data_lines = 1;
  61. qspi_message.parent.cs_take = 1;
  62. qspi_message.parent.cs_release = 1;
  63. qspi_message.parent.send_buf = &au8Val[0];
  64. qspi_message.parent.length = sizeof(au8Val);
  65. qspi_message.parent.next = RT_NULL;
  66. if (rt_qspi_transfer_message(qspi_device, &qspi_message) != sizeof(au8Val))
  67. {
  68. result = -RT_ERROR;
  69. }
  70. result = RT_EOK;
  71. exit_SpiFlash_WriteStatusReg:
  72. return result;
  73. }
  74. static void SpiFlash_WaitReady(struct rt_qspi_device *qspi_device)
  75. {
  76. volatile uint8_t u8ReturnValue;
  77. do
  78. {
  79. u8ReturnValue = SpiFlash_ReadStatusReg(qspi_device);
  80. u8ReturnValue = u8ReturnValue & 1;
  81. }
  82. while (u8ReturnValue != 0); // check the BUSY bit
  83. }
  84. static void SpiFlash_EnterQspiMode(struct rt_qspi_device *qspi_device)
  85. {
  86. rt_err_t result = RT_EOK;
  87. uint8_t u8Status1 = SpiFlash_ReadStatusReg(qspi_device);
  88. uint8_t u8Status2 = SpiFlash_ReadStatusReg2(qspi_device);
  89. u8Status2 |= W25X_REG_QUADENABLE;
  90. result = SpiFlash_WriteStatusReg(qspi_device, u8Status1, u8Status2);
  91. RT_ASSERT(result == RT_EOK);
  92. SpiFlash_WaitReady(qspi_device);
  93. }
  94. static void SpiFlash_ExitQspiMode(struct rt_qspi_device *qspi_device)
  95. {
  96. rt_err_t result = RT_EOK;
  97. uint8_t u8Status1 = SpiFlash_ReadStatusReg(qspi_device);
  98. uint8_t u8Status2 = SpiFlash_ReadStatusReg2(qspi_device);
  99. u8Status2 &= ~W25X_REG_QUADENABLE;
  100. result = SpiFlash_WriteStatusReg(qspi_device, u8Status1, u8Status2);
  101. RT_ASSERT(result == RT_EOK);
  102. SpiFlash_WaitReady(qspi_device);
  103. }
  104. static int rt_hw_spiflash_init(void)
  105. {
  106. /* Here, we use Dual I/O to drive the SPI flash by default. */
  107. /* If you want to use Quad I/O, you can modify to 4 from 2 and crossover D2/D3 pin of SPI flash. */
  108. if (nu_qspi_bus_attach_device("qspi0", "qspi01", 2, SpiFlash_EnterQspiMode, SpiFlash_ExitQspiMode) != RT_EOK)
  109. return -1;
  110. #if defined(RT_USING_SFUD)
  111. if (rt_sfud_flash_probe("flash0", "qspi01") == RT_NULL)
  112. {
  113. return -(RT_ERROR);
  114. }
  115. #endif
  116. return 0;
  117. }
  118. INIT_COMPONENT_EXPORT(rt_hw_spiflash_init);
  119. #endif /* BOARD_USING_STORAGE_SPIFLASH */
  120. #if defined(BOARD_USING_SRAM0_AS_MEMHEAP)
  121. /*
  122. In Advance board design, SRAM address bus A16/A17/A18 are GPIO-controlled by SW, not EBI.
  123. So we just remap 128KB only to RTT memory heap, due to it is out of control.
  124. AD0~AD15: 2^16*16bit = 128KB
  125. */
  126. #include <drv_ebi.h>
  127. #include "NuMicro.h"
  128. static struct rt_memheap system_heap;
  129. int nu_use_exsram_as_heap(void)
  130. {
  131. rt_err_t ret;
  132. /* Open ebi bank1 */
  133. ret = nu_ebi_init(EBI_BANK1, EBI_BUSWIDTH_16BIT, EBI_TIMING_SLOWEST, EBI_OPMODE_NORMAL, EBI_CS_ACTIVE_LOW);
  134. if (ret != RT_EOK)
  135. return ret;
  136. /* Initial sram as heap */
  137. return rt_memheap_init(&system_heap, "nu_sram_heap", (void *)EBI_BANK1_BASE_ADDR, 128 * 1024);
  138. }
  139. INIT_BOARD_EXPORT(nu_use_exsram_as_heap);
  140. #endif /* BOARD_USING_SRAM0_AS_MEMHEAP */
  141. #if defined(BOARD_USING_MAX31875)
  142. #include <sensor_max31875.h>
  143. int rt_hw_max31875_port(void)
  144. {
  145. struct rt_sensor_config cfg;
  146. cfg.intf.dev_name = "i2c1";
  147. cfg.intf.user_data = (void *)MAX31875_I2C_SLAVE_ADR_R0;
  148. cfg.irq_pin.pin = RT_PIN_NONE;
  149. rt_hw_max31875_init("max31875", &cfg);
  150. return 0;
  151. }
  152. INIT_APP_EXPORT(rt_hw_max31875_port);
  153. #endif /* BOARD_USING_MAX31875 */
  154. #if defined(BOARD_USING_MPU6500)
  155. #include <sensor_inven_mpu6xxx.h>
  156. int rt_hw_mpu6500_port(void)
  157. {
  158. struct rt_sensor_config cfg;
  159. cfg.intf.dev_name = "i2c2";
  160. cfg.intf.user_data = (void *)MPU6XXX_ADDR_DEFAULT;
  161. cfg.irq_pin.pin = RT_PIN_NONE;
  162. rt_hw_mpu6xxx_init("mpu", &cfg);
  163. return 0;
  164. }
  165. INIT_APP_EXPORT(rt_hw_mpu6500_port);
  166. #endif /* BOARD_USING_MPU6500 */
  167. #if defined(BOARD_USING_LCD_ILI9341) && defined(NU_PKG_USING_ILI9341_EBI)
  168. #include <lcd_ili9341.h>
  169. #if defined(PKG_USING_GUIENGINE)
  170. #include <rtgui/driver.h>
  171. #endif
  172. int rt_hw_ili9341_port(void)
  173. {
  174. rt_err_t ret = RT_EOK;
  175. /* Open ebi BOARD_USING_ILI9341_EBI_PORT */
  176. ret = nu_ebi_init(BOARD_USING_ILI9341_EBI_PORT, EBI_BUSWIDTH_16BIT, EBI_TIMING_NORMAL, EBI_OPMODE_NORMAL, EBI_CS_ACTIVE_LOW);
  177. if (ret != RT_EOK)
  178. return ret;
  179. switch (BOARD_USING_ILI9341_EBI_PORT)
  180. {
  181. case 0:
  182. EBI->CTL0 |= EBI_CTL0_CACCESS_Msk;
  183. EBI->TCTL0 |= (EBI_TCTL0_WAHDOFF_Msk | EBI_TCTL0_RAHDOFF_Msk);
  184. break;
  185. case 1:
  186. EBI->CTL1 |= EBI_CTL1_CACCESS_Msk;
  187. EBI->TCTL1 |= (EBI_TCTL1_WAHDOFF_Msk | EBI_TCTL1_RAHDOFF_Msk);
  188. break;
  189. case 2:
  190. EBI->CTL2 |= EBI_CTL2_CACCESS_Msk;
  191. EBI->TCTL2 |= (EBI_TCTL2_WAHDOFF_Msk | EBI_TCTL2_RAHDOFF_Msk);
  192. break;
  193. default:
  194. return -1;
  195. }
  196. if (rt_hw_lcd_ili9341_ebi_init(EBI_BANK0_BASE_ADDR + BOARD_USING_ILI9341_EBI_PORT * EBI_MAX_SIZE) != RT_EOK)
  197. return -1;
  198. rt_hw_lcd_ili9341_init();
  199. #if defined(PKG_USING_GUIENGINE)
  200. rt_device_t lcd_ili9341;
  201. lcd_ili9341 = rt_device_find("lcd");
  202. if (lcd_ili9341)
  203. {
  204. rtgui_graphic_set_device(lcd_ili9341);
  205. }
  206. #endif
  207. return 0;
  208. }
  209. INIT_COMPONENT_EXPORT(rt_hw_ili9341_port);
  210. #endif /* BOARD_USING_LCD_ILI9341 */
  211. #if defined(BOARD_USING_NAU88L25) && defined(NU_PKG_USING_NAU88L25)
  212. #include <acodec_nau88l25.h>
  213. S_NU_NAU88L25_CONFIG sCodecConfig =
  214. {
  215. .i2c_bus_name = "i2c2",
  216. .i2s_bus_name = "sound0",
  217. .pin_phonejack_en = NU_GET_PININDEX(NU_PE, 13),
  218. .pin_phonejack_det = 0,
  219. };
  220. int rt_hw_nau88l25_port(void)
  221. {
  222. if (nu_hw_nau88l25_init(&sCodecConfig) != RT_EOK)
  223. return -1;
  224. return 0;
  225. }
  226. INIT_COMPONENT_EXPORT(rt_hw_nau88l25_port);
  227. #endif /* BOARD_USING_NAU88L25 */
  228. #if defined(BOARD_USING_BUZZER)
  229. #define BPWM_DEV_NAME "bpwm0"
  230. #define BPWM_DEV_CHANNEL (5)
  231. static void PlayRingTone(void)
  232. {
  233. struct rt_device_pwm *bpwm_dev;
  234. rt_uint32_t period;
  235. int i, j;
  236. period = 1000;
  237. if ((bpwm_dev = (struct rt_device_pwm *)rt_device_find(BPWM_DEV_NAME)) != RT_NULL)
  238. {
  239. rt_pwm_set(bpwm_dev, BPWM_DEV_CHANNEL, period, period);
  240. rt_pwm_enable(bpwm_dev, BPWM_DEV_CHANNEL);
  241. for (j = 0; j < 5; j++)
  242. {
  243. for (i = 0; i < 10; i++)
  244. {
  245. rt_pwm_set(bpwm_dev, BPWM_DEV_CHANNEL, period, period);
  246. rt_thread_mdelay(50);
  247. rt_pwm_set(bpwm_dev, BPWM_DEV_CHANNEL, period, period / 2);
  248. rt_thread_mdelay(50);
  249. }
  250. /* Mute 2 seconds */
  251. rt_pwm_set(bpwm_dev, BPWM_DEV_CHANNEL, period, period);
  252. rt_thread_mdelay(2000);
  253. }
  254. rt_pwm_disable(bpwm_dev, BPWM_DEV_CHANNEL);
  255. }
  256. else
  257. {
  258. rt_kprintf("Can't find %s\n", BPWM_DEV_NAME);
  259. }
  260. }
  261. int buzzer_test(void)
  262. {
  263. PlayRingTone();
  264. return 0;
  265. }
  266. #ifdef FINSH_USING_MSH
  267. MSH_CMD_EXPORT(buzzer_test, Buzzer - Play ring tone);
  268. #endif
  269. #endif /* BOARD_USING_BUZZER */