drv_sci.c 26 KB

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