drv_sci.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-24 Vandoul first version
  9. * 2023-09-27 Vandoul add sci uart
  10. */
  11. #include "drv_sci.h"
  12. #ifdef BSP_USING_SCI
  13. //#define DRV_DEBUG
  14. #define DBG_TAG "drv.sci"
  15. #ifdef DRV_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif /* DRV_DEBUG */
  20. #include <rtdbg.h>
  21. #ifdef R_SCI_B_SPI_H
  22. #define R_SCI_SPI_Write R_SCI_B_SPI_Write
  23. #define R_SCI_SPI_Read R_SCI_B_SPI_Read
  24. #define R_SCI_SPI_WriteRead R_SCI_B_SPI_WriteRead
  25. #define R_SCI_SPI_Open R_SCI_B_SPI_Open
  26. #define R_SCI_SPI_Close R_SCI_B_SPI_Close
  27. #define R_SCI_SPI_CallbackSet R_SCI_B_SPI_CallbackSet
  28. #endif
  29. enum
  30. {
  31. #ifdef BSP_USING_SCI0
  32. RA_SCI_INDEX0,
  33. #endif
  34. #ifdef BSP_USING_SCI1
  35. RA_SCI_INDEX1,
  36. #endif
  37. #ifdef BSP_USING_SCI2
  38. RA_SCI_INDEX2,
  39. #endif
  40. #ifdef BSP_USING_SCI3
  41. RA_SCI_INDEX3,
  42. #endif
  43. #ifdef BSP_USING_SCI4
  44. RA_SCI_INDEX4,
  45. #endif
  46. #ifdef BSP_USING_SCI5
  47. RA_SCI_INDEX5,
  48. #endif
  49. #ifdef BSP_USING_SCI6
  50. RA_SCI_INDEX6,
  51. #endif
  52. #ifdef BSP_USING_SCI7
  53. RA_SCI_INDEX7,
  54. #endif
  55. #ifdef BSP_USING_SCI8
  56. RA_SCI_INDEX8,
  57. #endif
  58. #ifdef BSP_USING_SCI9
  59. RA_SCI_INDEX9,
  60. #endif
  61. RA_SCI_INDEX_MAX,
  62. };
  63. struct ra_sci_param
  64. {
  65. const char bus_name[RT_NAME_MAX];
  66. const void *sci_ctrl;
  67. const void *sci_cfg;
  68. const void *ops;
  69. };
  70. #ifdef RT_USING_I2C
  71. rt_weak const struct rt_i2c_bus_device_ops sci_ops_i2c;
  72. #endif
  73. #ifdef RT_USING_SPI
  74. rt_weak const struct rt_spi_ops sci_ops_spi;
  75. #endif
  76. #ifdef RT_USING_UART
  77. rt_weak const struct rt_uart_ops sci_ops_uart;
  78. #endif
  79. struct ra_sci_object
  80. {
  81. union
  82. {
  83. #ifdef RT_USING_SPI
  84. struct
  85. {
  86. struct rt_spi_bus sbus;
  87. struct rt_spi_configuration *spi_cfg;
  88. };
  89. #endif
  90. #ifdef RT_USING_I2C
  91. struct
  92. {
  93. struct rt_i2c_bus_device ibus;
  94. };
  95. #endif
  96. #ifdef RT_USING_UART
  97. struct
  98. {
  99. struct rt_serial_device ubus;
  100. };
  101. #endif
  102. };
  103. const struct ra_sci_param *param;
  104. struct rt_event event;
  105. };
  106. #define _TO_STR(_a) #_a
  107. #define CONCAT3STR(_a,_b,_c) _TO_STR(_a##_b##_c)
  108. #define RA_SCI_EVENT_ABORTED 1
  109. #define RA_SCI_EVENT_RX_COMPLETE 2
  110. #define RA_SCI_EVENT_TX_COMPLETE 4
  111. #define RA_SCI_EVENT_ERROR 8
  112. #define RA_SCI_EVENT_ALL 15
  113. #if defined(SOC_SERIES_R7FA4M2)
  114. #define RA_SCI_HANDLE_ITEM(idx,type,id) {.bus_name=CONCAT3STR(sci_,type,idx),.sci_ctrl=&g_sci##idx##_ctrl,.sci_cfg=&g_sci##idx##_cfg,.ops=&sci_ops_##type}
  115. #else
  116. #define RA_SCI_HANDLE_ITEM(idx,type,id) {.bus_name=CONCAT3STR(sci_,type,idx),.sci_ctrl=&g_##type##idx##_ctrl,.sci_cfg=&g_##type##idx##_cfg,.ops=&sci_ops_##type}
  117. #endif
  118. const static struct ra_sci_param sci_param[] =
  119. {
  120. #ifdef BSP_USING_SCI0
  121. #ifdef BSP_USING_SCI0_SPI
  122. RA_SCI_HANDLE_ITEM(0, spi, s),
  123. #elif defined(BSP_USING_SCI0_I2C)
  124. RA_SCI_HANDLE_ITEM(0, i2c, i),
  125. #elif defined(BSP_USING_SCI0_UART)
  126. RA_SCI_HANDLE_ITEM(0, uart, u),
  127. #endif
  128. #endif
  129. #ifdef BSP_USING_SCI1
  130. #ifdef BSP_USING_SCI1_SPI
  131. RA_SCI_HANDLE_ITEM(1, spi, s),
  132. #elif defined(BSP_USING_SCI1_I2C)
  133. RA_SCI_HANDLE_ITEM(1, i2c, i),
  134. #elif defined(BSP_USING_SCI1_UART)
  135. RA_SCI_HANDLE_ITEM(1, uart, u),
  136. #endif
  137. #endif
  138. #ifdef BSP_USING_SCI2
  139. #ifdef BSP_USING_SCI2_SPI
  140. RA_SCI_HANDLE_ITEM(2, spi, s),
  141. #elif defined(BSP_USING_SCI2_I2C)
  142. RA_SCI_HANDLE_ITEM(2, i2c, i),
  143. #elif defined(BSP_USING_SCI2_UART)
  144. RA_SCI_HANDLE_ITEM(2, uart, u),
  145. #endif
  146. #endif
  147. #ifdef BSP_USING_SCI3
  148. #ifdef BSP_USING_SCI3_SPI
  149. RA_SCI_HANDLE_ITEM(3, spi, s),
  150. #elif defined(BSP_USING_SCI3_I2C)
  151. RA_SCI_HANDLE_ITEM(3, i2c, i),
  152. #elif defined(BSP_USING_SCI3_UART)
  153. RA_SCI_HANDLE_ITEM(3, uart, u),
  154. #endif
  155. #endif
  156. #ifdef BSP_USING_SCI4
  157. #ifdef BSP_USING_SCI4_SPI
  158. RA_SCI_HANDLE_ITEM(4, spi, s),
  159. #elif defined(BSP_USING_SCI4_I2C)
  160. RA_SCI_HANDLE_ITEM(4, i2c, i),
  161. #elif defined(BSP_USING_SCI4_UART)
  162. RA_SCI_HANDLE_ITEM(4, uart, u),
  163. #endif
  164. #endif
  165. #ifdef BSP_USING_SCI5
  166. #ifdef BSP_USING_SCI5_SPI
  167. RA_SCI_HANDLE_ITEM(5, spi, s),
  168. #elif defined(BSP_USING_SCI5_I2C)
  169. RA_SCI_HANDLE_ITEM(5, i2c, i),
  170. #elif defined(BSP_USING_SCI5_UART)
  171. RA_SCI_HANDLE_ITEM(5, uart, u),
  172. #endif
  173. #endif
  174. #ifdef BSP_USING_SCI6
  175. #ifdef BSP_USING_SCI6_SPI
  176. RA_SCI_HANDLE_ITEM(6, spi, s),
  177. #elif defined(BSP_USING_SCI6_I2C)
  178. RA_SCI_HANDLE_ITEM(6, i2c, i),
  179. #elif defined(BSP_USING_SCI6_UART)
  180. RA_SCI_HANDLE_ITEM(6, uart, u),
  181. #endif
  182. #endif
  183. #ifdef BSP_USING_SCI7
  184. #ifdef BSP_USING_SCI7_SPI
  185. RA_SCI_HANDLE_ITEM(7, spi, s),
  186. #elif defined(BSP_USING_SCI7_I2C)
  187. RA_SCI_HANDLE_ITEM(7, i2c, i),
  188. #elif defined(BSP_USING_SCI7_UART)
  189. RA_SCI_HANDLE_ITEM(7, uart, u),
  190. #endif
  191. #endif
  192. #ifdef BSP_USING_SCI8
  193. #ifdef BSP_USING_SCI8_SPI
  194. RA_SCI_HANDLE_ITEM(8, spi, s),
  195. #elif defined(BSP_USING_SCI8_I2C)
  196. RA_SCI_HANDLE_ITEM(8, i2c, i),
  197. #elif defined(BSP_USING_SCI8_UART)
  198. RA_SCI_HANDLE_ITEM(8, uart, u),
  199. #endif
  200. #endif
  201. #ifdef BSP_USING_SCI9
  202. #ifdef BSP_USING_SCI9_SPI
  203. RA_SCI_HANDLE_ITEM(9, spi, s),
  204. #elif defined(BSP_USING_SCI9_I2C)
  205. RA_SCI_HANDLE_ITEM(9, i2c, i),
  206. #elif defined(BSP_USING_SCI9_UART)
  207. RA_SCI_HANDLE_ITEM(9, uart, u),
  208. #endif
  209. #endif
  210. };
  211. static struct ra_sci_object sci_obj[RA_SCI_INDEX_MAX];
  212. rt_used static rt_err_t ra_wait_complete(struct ra_sci_object *obj)
  213. {
  214. rt_uint32_t event = 0;
  215. if (RT_EOK != rt_event_recv(&obj->event, RA_SCI_EVENT_ALL, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, (rt_int32_t)rt_tick_from_millisecond(400), &event))
  216. {
  217. return -RT_ETIMEOUT;
  218. }
  219. if ((event & (RA_SCI_EVENT_ABORTED | RA_SCI_EVENT_ERROR)) == 0)
  220. {
  221. return RT_EOK;
  222. }
  223. return -RT_ERROR;
  224. }
  225. /**
  226. * @brief SCI UART
  227. * @defgroup SCI_UART
  228. * @{
  229. */
  230. #ifdef BSP_USING_SCIn_UART
  231. const static int uart_buff_size[][2] =
  232. {
  233. #ifdef BSP_USING_SCI0_UART
  234. {BSP_SCI0_UART_RX_BUFSIZE, BSP_SCI0_UART_TX_BUFSIZE},
  235. #endif
  236. #ifdef BSP_USING_SCI1_UART
  237. {BSP_SCI1_UART_RX_BUFSIZE, BSP_SCI1_UART_TX_BUFSIZE},
  238. #endif
  239. #ifdef BSP_USING_SCI2_UART
  240. {BSP_SCI2_UART_RX_BUFSIZE, BSP_SCI2_UART_TX_BUFSIZE},
  241. #endif
  242. #ifdef BSP_USING_SCI3_UART
  243. {BSP_SCI3_UART_RX_BUFSIZE, BSP_SCI3_UART_TX_BUFSIZE},
  244. #endif
  245. #ifdef BSP_USING_SCI4_UART
  246. {BSP_SCI4_UART_RX_BUFSIZE, BSP_SCI4_UART_TX_BUFSIZE},
  247. #endif
  248. #ifdef BSP_USING_SCI5_UART
  249. {BSP_SCI5_UART_RX_BUFSIZE, BSP_SCI5_UART_TX_BUFSIZE},
  250. #endif
  251. #ifdef BSP_USING_SCI6_UART
  252. {BSP_SCI6_UART_RX_BUFSIZE, BSP_SCI6_UART_TX_BUFSIZE},
  253. #endif
  254. #ifdef BSP_USING_SCI7_UART
  255. {BSP_SCI7_UART_RX_BUFSIZE, BSP_SCI7_UART_TX_BUFSIZE},
  256. #endif
  257. #ifdef BSP_USING_SCI8_UART
  258. {BSP_SCI8_UART_RX_BUFSIZE, BSP_SCI8_UART_TX_BUFSIZE},
  259. #endif
  260. #ifdef BSP_USING_SCI9_UART
  261. {BSP_SCI9_UART_RX_BUFSIZE, BSP_SCI9_UART_TX_BUFSIZE},
  262. #endif
  263. {0, 0},
  264. };
  265. void sci_uart_irq_callback(uart_callback_args_t *p_args)
  266. {
  267. rt_interrupt_enter();
  268. if (NULL != p_args)
  269. {
  270. struct ra_sci_object *obj = (struct ra_sci_object *)p_args->p_context;
  271. RT_ASSERT(obj != RT_NULL);
  272. if (UART_EVENT_RX_CHAR == p_args->event)
  273. {
  274. struct rt_serial_device *serial = &obj->ubus;
  275. struct rt_serial_rx_fifo *rx_fifo;
  276. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  277. RT_ASSERT(rx_fifo != RT_NULL);
  278. rt_ringbuffer_putchar(&(rx_fifo->rb), (rt_uint8_t)p_args->data);
  279. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  280. }
  281. }
  282. rt_interrupt_leave();
  283. }
  284. static rt_err_t ra_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  285. {
  286. struct ra_sci_object *obj;
  287. const struct ra_sci_param *param;
  288. RT_ASSERT(serial != RT_NULL);
  289. RT_ASSERT(cfg != RT_NULL);
  290. fsp_err_t err = FSP_SUCCESS;
  291. obj = rt_container_of(serial, struct ra_sci_object, ubus);
  292. param = obj->param;
  293. RT_ASSERT(param != RT_NULL);
  294. err = R_SCI_UART_Open((uart_ctrl_t *const)param->sci_ctrl, (uart_cfg_t *const)param->sci_cfg);
  295. if (FSP_SUCCESS != err)
  296. {
  297. return -RT_ERROR;
  298. }
  299. err = R_SCI_UART_CallbackSet((uart_ctrl_t *const)param->sci_ctrl, sci_uart_irq_callback, obj, NULL);
  300. if (FSP_SUCCESS != err)
  301. {
  302. //LOG_W("R_SCI_UART_CallbackSet API failed,%d", err);
  303. }
  304. return RT_EOK;
  305. }
  306. static rt_err_t ra_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  307. {
  308. return RT_EOK;
  309. }
  310. static int ra_uart_putc(struct rt_serial_device *serial, char c)
  311. {
  312. struct ra_sci_object *obj;
  313. const struct ra_sci_param *param;
  314. RT_ASSERT(serial != RT_NULL);
  315. obj = rt_container_of(serial, struct ra_sci_object, ubus);
  316. param = obj->param;
  317. RT_ASSERT(param != RT_NULL);
  318. sci_uart_instance_ctrl_t *p_ctrl = (sci_uart_instance_ctrl_t *)param->sci_ctrl;
  319. p_ctrl->p_reg->TDR = c;
  320. while ((p_ctrl->p_reg->SSR_b.TEND) == 0);
  321. return RT_EOK;
  322. }
  323. static int ra_uart_getc(struct rt_serial_device *serial)
  324. {
  325. return RT_EOK;
  326. }
  327. static rt_ssize_t ra_uart_transmit(struct rt_serial_device *serial,
  328. rt_uint8_t *buf,
  329. rt_size_t size,
  330. rt_uint32_t tx_flag)
  331. {
  332. RT_ASSERT(serial != RT_NULL);
  333. RT_ASSERT(buf != RT_NULL);
  334. return 0;
  335. }
  336. const struct rt_uart_ops sci_ops_uart =
  337. {
  338. .configure = ra_uart_configure,
  339. .control = ra_uart_control,
  340. .putc = ra_uart_putc,
  341. .getc = ra_uart_getc,
  342. .transmit = ra_uart_transmit,
  343. };
  344. #else
  345. void sci_uart_irq_callback(uart_callback_args_t *p_args)
  346. {
  347. }
  348. #endif
  349. /**
  350. * @}
  351. */
  352. /**
  353. * @brief SCI I2C
  354. * @defgroup SCI_I2C
  355. * @{
  356. */
  357. #ifdef BSP_USING_SCIn_I2C
  358. void sci_i2c_irq_callback(i2c_master_callback_args_t *p_args)
  359. {
  360. rt_interrupt_enter();
  361. if (NULL != p_args)
  362. {
  363. /* capture callback event for validating the i2c transfer event*/
  364. struct ra_sci_object *obj = (struct ra_sci_object *)p_args->p_context;
  365. uint32_t event = 0;
  366. RT_ASSERT(obj != RT_NULL);
  367. switch (p_args->event)
  368. {
  369. case I2C_MASTER_EVENT_ABORTED:
  370. event |= RA_SCI_EVENT_ABORTED;
  371. break;
  372. case I2C_MASTER_EVENT_RX_COMPLETE:
  373. event |= RA_SCI_EVENT_RX_COMPLETE;
  374. break;
  375. case I2C_MASTER_EVENT_TX_COMPLETE:
  376. event |= RA_SCI_EVENT_TX_COMPLETE;
  377. break;
  378. }
  379. rt_event_send(&obj->event, event);
  380. LOG_D("event:%x", p_args->event);
  381. }
  382. rt_interrupt_leave();
  383. LOG_D("p_args:%p", p_args);
  384. }
  385. static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  386. struct rt_i2c_msg msgs[],
  387. rt_uint32_t num)
  388. {
  389. rt_size_t i;
  390. RT_ASSERT(bus != RT_NULL);
  391. struct ra_sci_object *obj = rt_container_of(bus, struct ra_sci_object, ibus);
  392. const struct ra_sci_param *param = obj->param;
  393. i2c_master_ctrl_t *master_ctrl = (i2c_master_ctrl_t *)param->sci_ctrl;
  394. int err = FSP_SUCCESS;
  395. bool restart = false;
  396. for (i = 0; i < num; i++)
  397. {
  398. struct rt_i2c_msg *msg = &msgs[i];
  399. if (msg->flags & RT_I2C_NO_STOP)
  400. {
  401. restart = true;
  402. }
  403. else
  404. {
  405. restart = false;
  406. }
  407. if (msg->flags & RT_I2C_ADDR_10BIT)
  408. {
  409. //LOG_E("10Bit not support");
  410. //break;
  411. #ifdef SOC_SERIES_R7FA8M85
  412. R_SCI_B_I2C_SlaveAddressSet(master_ctrl, msg->addr, I2C_MASTER_ADDR_MODE_10BIT);
  413. #else
  414. R_SCI_I2C_SlaveAddressSet(master_ctrl, msg->addr, I2C_MASTER_ADDR_MODE_10BIT);
  415. #endif
  416. }
  417. else
  418. {
  419. //master_ctrl->slave = msg->addr;
  420. #ifdef SOC_SERIES_R7FA8M85
  421. R_SCI_B_I2C_SlaveAddressSet(master_ctrl, msg->addr, I2C_MASTER_ADDR_MODE_7BIT);
  422. #else
  423. R_SCI_I2C_SlaveAddressSet(master_ctrl, msg->addr, I2C_MASTER_ADDR_MODE_7BIT);
  424. #endif
  425. }
  426. if (msg->flags & RT_I2C_RD)
  427. {
  428. #ifdef SOC_SERIES_R7FA8M85
  429. err = R_SCI_B_I2C_Read(master_ctrl, msg->buf, msg->len, restart);
  430. #else
  431. err = R_SCI_I2C_Read(master_ctrl, msg->buf, msg->len, restart);
  432. #endif
  433. }
  434. else
  435. {
  436. #ifdef SOC_SERIES_R7FA8M85
  437. err = R_SCI_B_I2C_Write(master_ctrl, msg->buf, msg->len, restart);
  438. #else
  439. err = R_SCI_I2C_Write(master_ctrl, msg->buf, msg->len, restart);
  440. #endif
  441. }
  442. if (FSP_SUCCESS == err)
  443. {
  444. /* handle error */
  445. err = ra_wait_complete(obj);
  446. if (RT_EOK != err)
  447. {
  448. //LOG_E("POWER_CTL reg I2C write failed,%d,%d", err, i);
  449. break;
  450. }
  451. }
  452. /* handle error */
  453. else
  454. {
  455. /* Write API returns itself is not successful */
  456. LOG_E("R_IIC_MASTER_Write/Read API failed,%d", i);
  457. break;
  458. }
  459. }
  460. return (rt_ssize_t)i;
  461. }
  462. const struct rt_i2c_bus_device_ops sci_ops_i2c =
  463. {
  464. .master_xfer = ra_i2c_mst_xfer,
  465. .slave_xfer = RT_NULL,
  466. .i2c_bus_control = RT_NULL
  467. };
  468. #endif
  469. /**
  470. * @}
  471. */
  472. /**
  473. * @brief SCI SPI
  474. * @defgroup SCI_SPI
  475. * @{
  476. */
  477. #ifdef BSP_USING_SCIn_SPI
  478. void sci_spi_irq_callback(spi_callback_args_t *p_args)
  479. {
  480. rt_interrupt_enter();
  481. if (NULL != p_args)
  482. {
  483. /* capture callback event for validating the i2c transfer event*/
  484. struct ra_sci_object *obj = (struct ra_sci_object *)p_args->p_context;
  485. uint32_t event = 0;
  486. switch (p_args->event)
  487. {
  488. case SPI_EVENT_ERR_MODE_FAULT :
  489. case SPI_EVENT_ERR_READ_OVERFLOW:
  490. case SPI_EVENT_ERR_PARITY :
  491. case SPI_EVENT_ERR_OVERRUN :
  492. case SPI_EVENT_ERR_FRAMING :
  493. case SPI_EVENT_ERR_MODE_UNDERRUN:
  494. event |= RA_SCI_EVENT_ERROR;
  495. break;
  496. case SPI_EVENT_TRANSFER_ABORTED :
  497. event |= RA_SCI_EVENT_ABORTED;
  498. break;
  499. case SPI_EVENT_TRANSFER_COMPLETE:
  500. event |= RA_SCI_EVENT_TX_COMPLETE;
  501. break;
  502. }
  503. rt_event_send(&obj->event, event);
  504. LOG_D("event:%x", p_args->event);
  505. }
  506. rt_interrupt_leave();
  507. LOG_D("p_args:%p", p_args);
  508. }
  509. static spi_bit_width_t ra_width_shift(rt_uint8_t data_width)
  510. {
  511. spi_bit_width_t bit_width = SPI_BIT_WIDTH_8_BITS;
  512. if (data_width == 1)
  513. bit_width = SPI_BIT_WIDTH_8_BITS;
  514. else if (data_width == 2)
  515. bit_width = SPI_BIT_WIDTH_16_BITS;
  516. else if (data_width == 4)
  517. bit_width = SPI_BIT_WIDTH_32_BITS;
  518. return bit_width;
  519. }
  520. static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
  521. {
  522. RT_ASSERT(device != NULL);
  523. RT_ASSERT(send_buf != NULL);
  524. RT_ASSERT(len > 0);
  525. rt_err_t err = RT_EOK;
  526. struct ra_sci_object *obj = rt_container_of(device->bus, struct ra_sci_object, sbus);
  527. const struct ra_sci_param *param = obj->param;
  528. spi_bit_width_t bit_width = ra_width_shift(obj->spi_cfg->data_width);
  529. /**< send msessage */
  530. err = R_SCI_SPI_Write((spi_ctrl_t *)param->sci_ctrl, send_buf, len, bit_width);
  531. if (RT_EOK != err)
  532. {
  533. LOG_E("%s write failed. %d", param->bus_name, err);
  534. return -RT_ERROR;
  535. }
  536. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  537. ra_wait_complete(obj);
  538. return len;
  539. }
  540. static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
  541. {
  542. RT_ASSERT(device != NULL);
  543. RT_ASSERT(recv_buf != NULL);
  544. RT_ASSERT(len > 0);
  545. rt_err_t err = RT_EOK;
  546. struct ra_sci_object *obj = rt_container_of(device->bus, struct ra_sci_object, sbus);
  547. const struct ra_sci_param *param = obj->param;
  548. spi_bit_width_t bit_width = ra_width_shift(obj->spi_cfg->data_width);
  549. /**< receive message */
  550. err = R_SCI_SPI_Read((spi_ctrl_t *)param->sci_ctrl, recv_buf, len, bit_width);
  551. if (RT_EOK != err)
  552. {
  553. LOG_E("%s write failed. %d", param->bus_name, err);
  554. return -RT_ERROR;
  555. }
  556. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  557. ra_wait_complete(obj);
  558. return len;
  559. }
  560. static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
  561. {
  562. RT_ASSERT(device != NULL);
  563. RT_ASSERT(message != NULL);
  564. RT_ASSERT(message->length > 0);
  565. rt_err_t err = RT_EOK;
  566. struct ra_sci_object *obj = rt_container_of(device->bus, struct ra_sci_object, sbus);
  567. const struct ra_sci_param *param = obj->param;
  568. spi_bit_width_t bit_width = ra_width_shift(obj->spi_cfg->data_width);
  569. /**< write and receive message */
  570. err = R_SCI_SPI_WriteRead((spi_ctrl_t *)param->sci_ctrl, message->send_buf, message->recv_buf, message->length, bit_width);
  571. if (RT_EOK != err)
  572. {
  573. LOG_E("%s write and read failed. %d", param->bus_name, err);
  574. return -RT_ERROR;
  575. }
  576. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  577. ra_wait_complete(obj);
  578. return message->length;
  579. }
  580. /**< init spi TODO : MSB does not support modification */
  581. static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
  582. struct rt_spi_configuration *configuration)
  583. {
  584. RT_ASSERT(device != NULL);
  585. RT_ASSERT(configuration != NULL);
  586. rt_err_t err = RT_EOK;
  587. struct ra_sci_object *obj = rt_container_of(device->bus, struct ra_sci_object, sbus);
  588. const struct ra_sci_param *param = obj->param;
  589. const spi_cfg_t *cfg = (const spi_cfg_t *)param->sci_cfg;
  590. /**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
  591. rt_uint8_t data_width = configuration->data_width / 8;
  592. RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
  593. configuration->data_width = configuration->data_width / 8;
  594. obj->spi_cfg = configuration;
  595. #ifdef R_SCI_B_SPI_H
  596. sci_b_spi_extended_cfg_t spi_cfg = *(sci_b_spi_extended_cfg_t *)cfg->p_extend;
  597. #else
  598. sci_spi_extended_cfg_t *spi_cfg = (sci_spi_extended_cfg_t *)cfg->p_extend;
  599. #endif
  600. /**< Configure Select Line */
  601. rt_pin_write(device->cs_pin, PIN_HIGH);
  602. /**< config bitrate */
  603. #ifdef R_SCI_B_SPI_H
  604. R_SCI_B_SPI_CalculateBitrate(obj->spi_cfg->max_hz, SCI_B_SPI_SOURCE_CLOCK_PCLK, &spi_cfg.clk_div);
  605. #else
  606. #if defined(SOC_SERIES_R7FA4M2)
  607. R_SCI_SPI_CalculateBitrate(obj->spi_cfg->max_hz, &cfg_ext->clk_div, false);
  608. #else
  609. R_SCI_SPI_CalculateBitrate(obj->spi_cfg->max_hz, &spi_cfg->clk_div, false);
  610. #endif
  611. #endif
  612. /**< init */
  613. err = R_SCI_SPI_Open((spi_ctrl_t *)param->sci_ctrl, cfg);
  614. /* handle error */
  615. if (err == FSP_ERR_IN_USE)
  616. {
  617. R_SCI_SPI_Close((spi_ctrl_t *)param->sci_ctrl);
  618. err = R_SCI_SPI_Open((spi_ctrl_t *)param->sci_ctrl, cfg);
  619. }
  620. if (RT_EOK != err)
  621. {
  622. LOG_E("%s init failed. %d", param->bus_name, err);
  623. return -RT_ERROR;
  624. }
  625. err = R_SCI_SPI_CallbackSet((spi_ctrl_t *)param->sci_ctrl, sci_spi_irq_callback, obj, NULL);
  626. if (FSP_SUCCESS != err)
  627. {
  628. LOG_E("R_SCI_I2C_CallbackSet API failed,%d", err);
  629. }
  630. return RT_EOK;
  631. }
  632. static rt_ssize_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  633. {
  634. RT_ASSERT(device != RT_NULL);
  635. RT_ASSERT(device->bus != RT_NULL);
  636. RT_ASSERT(message != RT_NULL);
  637. rt_err_t err = RT_EOK;
  638. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  639. {
  640. if (device->config.mode & RT_SPI_CS_HIGH)
  641. rt_pin_write(device->cs_pin, PIN_HIGH);
  642. else
  643. rt_pin_write(device->cs_pin, PIN_LOW);
  644. }
  645. if (message->length > 0)
  646. {
  647. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  648. {
  649. /**< receive message */
  650. err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
  651. }
  652. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  653. {
  654. /**< send message */
  655. err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
  656. }
  657. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  658. {
  659. /**< send and receive message */
  660. err = ra_write_read_message(device, message);
  661. }
  662. }
  663. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  664. {
  665. if (device->config.mode & RT_SPI_CS_HIGH)
  666. rt_pin_write(device->cs_pin, PIN_LOW);
  667. else
  668. rt_pin_write(device->cs_pin, PIN_HIGH);
  669. }
  670. return err;
  671. }
  672. const struct rt_spi_ops sci_ops_spi =
  673. {
  674. .configure = ra_hw_spi_configure,
  675. .xfer = ra_spixfer,
  676. };
  677. #endif
  678. /**
  679. * @}
  680. */
  681. static int ra_hw_sci_init(void)
  682. {
  683. for (rt_uint8_t idx = 0; idx < RA_SCI_INDEX_MAX; idx++)
  684. {
  685. struct ra_sci_object *obj = &sci_obj[idx];
  686. const struct ra_sci_param *param = &sci_param[idx];
  687. obj->param = param;
  688. rt_err_t err;
  689. #ifdef BSP_USING_SCIn_SPI
  690. if ((uint32_t)param->ops == (uint32_t)&sci_ops_spi)
  691. {
  692. /**< register spi bus */
  693. err = rt_spi_bus_register(&obj->sbus, param->bus_name, param->ops);
  694. if (RT_EOK != err)
  695. {
  696. LOG_E("bus %s register failed. %d", param->bus_name, err);
  697. return -RT_ERROR;
  698. }
  699. }
  700. else
  701. #endif
  702. #ifdef BSP_USING_SCIn_I2C
  703. if ((uint32_t)param->ops == (uint32_t)&sci_ops_i2c)
  704. {
  705. obj->ibus.ops = param->ops;
  706. obj->ibus.priv = 0;
  707. /* opening IIC master module */
  708. #ifdef SOC_SERIES_R7FA8M85
  709. err = R_SCI_B_I2C_Open((i2c_master_ctrl_t *)param->sci_ctrl, param->sci_cfg);
  710. #else
  711. err = R_SCI_I2C_Open((i2c_master_ctrl_t *)param->sci_ctrl, param->sci_cfg);
  712. #endif
  713. if (err != FSP_SUCCESS)
  714. {
  715. LOG_E("R_IIC_MASTER_Open API failed,%d", err);
  716. continue;
  717. }
  718. #ifdef SOC_SERIES_R7FA8M85
  719. err = R_SCI_B_I2C_CallbackSet((i2c_master_ctrl_t *)param->sci_ctrl, sci_i2c_irq_callback, obj, NULL);
  720. #else
  721. err = R_SCI_I2C_CallbackSet((i2c_master_ctrl_t *)param->sci_ctrl, sci_i2c_irq_callback, obj, NULL);
  722. #endif
  723. /* handle error */
  724. if (FSP_SUCCESS != err)
  725. {
  726. LOG_E("R_SCI_I2C_CallbackSet API failed,%d", err);
  727. continue;
  728. }
  729. err = rt_i2c_bus_device_register(&obj->ibus, param->bus_name);
  730. if (RT_EOK != err)
  731. {
  732. LOG_E("i2c bus %s register failed,%d", param->bus_name, err);
  733. continue;
  734. }
  735. }
  736. else
  737. #endif
  738. #ifdef BSP_USING_SCIn_UART
  739. if ((uint32_t)param->ops == (uint32_t)&sci_ops_uart)
  740. {
  741. if (rt_device_find(param->bus_name) != RT_NULL)
  742. {
  743. continue;
  744. }
  745. struct rt_serial_device *serial = &obj->ubus;
  746. obj->ubus.ops = param->ops;
  747. serial->config.rx_bufsz = uart_buff_size[bufsz_idx][0];
  748. serial->config.tx_bufsz = uart_buff_size[bufsz_idx][1];
  749. bufsz_idx ++;
  750. err = rt_hw_serial_register(serial, param->bus_name, RT_DEVICE_FLAG_RDWR, RT_NULL);
  751. if (RT_EOK != err)
  752. {
  753. LOG_E("uart %s register failed,%d", param->bus_name, err);
  754. continue;
  755. }
  756. }
  757. #endif
  758. {
  759. }
  760. if (RT_EOK != rt_event_init(&obj->event, param->bus_name, RT_IPC_FLAG_PRIO))
  761. {
  762. LOG_E("sci event init fail!");
  763. return -RT_ERROR;
  764. }
  765. }
  766. return RT_EOK;
  767. }
  768. INIT_BOARD_EXPORT(ra_hw_sci_init);
  769. #ifdef BSP_USING_SCIn_UART
  770. rt_weak int rt_hw_usart_init(void)
  771. {
  772. int bufsz_idx = 0;
  773. for (rt_uint8_t idx = 0; idx < RA_SCI_INDEX_MAX; idx++)
  774. {
  775. struct ra_sci_object *obj = &sci_obj[idx];
  776. const struct ra_sci_param *param = &sci_param[idx];
  777. obj->param = param;
  778. rt_err_t err;
  779. if ((uint32_t)param->ops == (uint32_t)&sci_ops_uart)
  780. {
  781. if (rt_device_find(param->bus_name) != RT_NULL)
  782. {
  783. continue;
  784. }
  785. struct rt_serial_device *serial = &obj->ubus;
  786. obj->ubus.ops = param->ops;
  787. serial->config.rx_bufsz = uart_buff_size[bufsz_idx][0];
  788. serial->config.tx_bufsz = uart_buff_size[bufsz_idx][1];
  789. bufsz_idx ++;
  790. err = rt_hw_serial_register(serial, param->bus_name, RT_DEVICE_FLAG_RDWR, RT_NULL);
  791. if (RT_EOK != err)
  792. {
  793. continue;
  794. }
  795. if (RT_EOK != rt_event_init(&obj->event, param->bus_name, RT_IPC_FLAG_PRIO))
  796. {
  797. return -RT_ERROR;
  798. }
  799. }
  800. }
  801. return RT_EOK;
  802. }
  803. #endif
  804. /**
  805. * Attach the spi device to SPI bus, this function must be used after initialization.
  806. */
  807. #ifdef BSP_USING_SCIn_SPI
  808. rt_err_t rt_hw_sci_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  809. {
  810. RT_ASSERT(bus_name != RT_NULL);
  811. RT_ASSERT(device_name != RT_NULL);
  812. rt_err_t result;
  813. struct rt_spi_device *spi_device;
  814. /* attach the device to spi bus*/
  815. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  816. RT_ASSERT(spi_device != RT_NULL);
  817. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  818. if (result != RT_EOK)
  819. {
  820. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  821. }
  822. LOG_D("%s attach to %s done", device_name, bus_name);
  823. return result;
  824. }
  825. #endif
  826. #endif /* BSP_USING_SCI */