git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@16379 27425a3e-05d8-49a3-a47f-9c15f0e5edd8

This commit is contained in:
Giovanni Di Sirio 2023-08-24 12:15:08 +00:00
parent 473728e4bd
commit afd4ffdd57
4 changed files with 94 additions and 28 deletions

View File

@ -117,9 +117,11 @@
#define SB_VSPI_RECEIVE 3
#define SB_VSPI_SEND 4
#define SB_VSPI_EXCHANGE 5
#define SB_VSPI_GETCLRSTS 6
#define SB_VSPI_SETCFG 0
#define SB_VSPI_SELECT 1
#define SB_VSPI_UNSELECT 2
#define SB_VSPI_GETSTS 3
/** @} */
/**

View File

@ -126,13 +126,16 @@ void sb_sysc_vio_spi(struct port_extctx *ectxp) {
msg_t msg;
uint32_t n = ectxp->r1;
void *rxbuf = (void *)ectxp->r2;
size_t bufsize;
if (n == 0U) {
ectxp->r0 = CH_RET_EINVAL;
break;
}
if (!sb_is_valid_write_range(sbp, rxbuf, n)) { /* TODO frame size */
bufsize = n * spiGetFrameSizeX(unitp->spip);
if (!sb_is_valid_write_range(sbp, rxbuf, bufsize)) {
ectxp->r0 = (uint32_t)0;
/* TODO enforce fault instead.*/
break;
@ -155,13 +158,16 @@ void sb_sysc_vio_spi(struct port_extctx *ectxp) {
msg_t msg;
uint32_t n = ectxp->r1;
const void *txbuf = (const void *)ectxp->r2;
size_t bufsize;
if (n == 0U) {
ectxp->r0 = CH_RET_EINVAL;
break;
}
if (!sb_is_valid_read_range(sbp, txbuf, n)) { /* TODO frame size */
bufsize = n * spiGetFrameSizeX(unitp->spip);
if (!sb_is_valid_read_range(sbp, txbuf, bufsize)) {
ectxp->r0 = (uint32_t)0;
/* TODO enforce fault instead.*/
break;
@ -182,22 +188,25 @@ void sb_sysc_vio_spi(struct port_extctx *ectxp) {
case SB_VSPI_EXCHANGE:
{
msg_t msg;
uint32_t n = ectxp->r1;
size_t n = (size_t)ectxp->r1;
void *rxbuf = (void *)ectxp->r2;
const void *txbuf = (const void *)ectxp->r3;
size_t bufsize;
if (n == 0U) {
ectxp->r0 = CH_RET_EINVAL;
break;
}
if (!sb_is_valid_write_range(sbp, rxbuf, n)) { /* TODO frame size */
bufsize = n * spiGetFrameSizeX(unitp->spip);
if (!sb_is_valid_write_range(sbp, rxbuf, bufsize)) {
ectxp->r0 = (uint32_t)0;
/* TODO enforce fault instead.*/
break;
}
if (!sb_is_valid_read_range(sbp, txbuf, n)) { /* TODO frame size */
if (!sb_is_valid_read_range(sbp, txbuf, bufsize)) {
ectxp->r0 = (uint32_t)0;
/* TODO enforce fault instead.*/
break;
@ -215,6 +224,18 @@ void sb_sysc_vio_spi(struct port_extctx *ectxp) {
ectxp->r0 = (uint32_t)msg;
break;
}
case SB_VSPI_GETCLRSTS:
{
drv_status_t mask = (size_t)ectxp->r1;
drv_status_t sts;
chSysLock();
sts = drvGetAndClearStatusI(unitp->spip, mask);
chSysUnlock();
ectxp->r0 = (uint32_t)sts;
break;
}
default:
ectxp->r0 = (uint32_t)CH_RET_ENOSYS;
break;
@ -272,6 +293,11 @@ void sb_fastc_vio_spi(struct port_extctx *ectxp) {
ectxp->r0 = (uint32_t)HAL_RET_SUCCESS;
break;
}
case SB_VSPI_GETSTS:
{
ectxp->r0 = (uint32_t)drvGetStatusX(unitp->spip);
break;
}
default:
/* Silently ignored.*/
break;

View File

@ -9,24 +9,30 @@
<public>
<definitions_early>
<group description="SPI operation modes">
<define name="SPI_MODE_CIRCULAR" value="1U &lt;&lt; 0">
<brief>Enables the circular buffer mode.</brief>
<define name="SPI_MODE_FSIZE_POS" value="0U">
<brief>Memory buffers frame size bit position.</brief>
</define>
<define name="SPI_MODE_SLAVE" value="1U &lt;&lt; 1">
<brief>Enables the slave mode.</brief>
<define name="SPI_MODE_FSIZE_MASK" value="(3U &lt;&lt; SPI_MODE_FSIZE_POS)">
<brief>Memory buffers frame size mask.</brief>
</define>
<define name="SPI_MODE_FSIZE_MASK" value="3U &lt;&lt; 2">
<brief>Memory buffers frame size.</brief>
</define>
<define name="SPI_MODE_FSIZE_8" value="0U &lt;&lt; 2">
<define name="SPI_MODE_FSIZE_8" value="(0U &lt;&lt; SPI_MODE_FSIZE_POS)">
<brief>Memory frame size is 8 bits</brief>
</define>
<define name="SPI_MODE_FSIZE_16" value="1U &lt;&lt; 2">
<define name="SPI_MODE_FSIZE_16" value="(1U &lt;&lt; SPI_MODE_FSIZE_POS)">
<brief>Memory frame size is 16 bits</brief>
</define>
<define name="SPI_MODE_FSIZE_32" value="2U &lt;&lt; 2">
<define name="SPI_MODE_FSIZE_32" value="(2U &lt;&lt; SPI_MODE_FSIZE_POS)">
<brief>Memory frame size is 32 bits</brief>
</define>
<define name="SPI_MODE_FSIZE_64" value="(3U &lt;&lt; SPI_MODE_FSIZE_POS)">
<brief>Memory frame size is 64 bits</brief>
</define>
<define name="SPI_MODE_CIRCULAR" value="(1U &lt;&lt; 2)">
<brief>Enables the circular buffer mode.</brief>
</define>
<define name="SPI_MODE_SLAVE" value="(1U &lt;&lt; 3)">
<brief>Enables the slave mode.</brief>
</define>
</group>
<group description="SPI CS modes">
<define name="SPI_SELECT_MODE_NONE" value="0">
@ -739,7 +745,13 @@ return msg;]]></implementation>
</condition>
</regular>
<inline>
<condition check="SPI_SELECT_MODE == SPI_SELECT_MODE_LLD">
<method name="spiGetFrameSizeX" ctype="size_t">
<brief>Returns the configured size, in bytes, of data frames.</brief>
<implementation><![CDATA[
return (size_t)(1U << ((__spi_getfield(self, mode) & SPI_MODE_FSIZE_MASK) >> SPI_MODE_FSIZE_POS));
]]></implementation>
</method><condition check="SPI_SELECT_MODE == SPI_SELECT_MODE_LLD">
<method name="spiSelectX" ctype="void">
<brief>Asserts the slave select signal and prepares for
transfers.</brief>
@ -889,6 +901,7 @@ __cbdrv_invoke_cb_with_transition(self,
HAL_DRV_STATE_READY);
__spi_wakeup_isr(self, msg);]]></implementation>
</method>
</inline>
<override>
<method shortname="start">

View File

@ -38,34 +38,44 @@
* @{
*/
/**
* @brief Enables the circular buffer mode.
* @brief Memory buffers frame size bit position.
*/
#define SPI_MODE_CIRCULAR 1U << 0
#define SPI_MODE_FSIZE_POS 0U
/**
* @brief Enables the slave mode.
* @brief Memory buffers frame size mask.
*/
#define SPI_MODE_SLAVE 1U << 1
/**
* @brief Memory buffers frame size.
*/
#define SPI_MODE_FSIZE_MASK 3U << 2
#define SPI_MODE_FSIZE_MASK (3U << SPI_MODE_FSIZE_POS)
/**
* @brief Memory frame size is 8 bits.
*/
#define SPI_MODE_FSIZE_8 0U << 2
#define SPI_MODE_FSIZE_8 (0U << SPI_MODE_FSIZE_POS)
/**
* @brief Memory frame size is 16 bits.
*/
#define SPI_MODE_FSIZE_16 1U << 2
#define SPI_MODE_FSIZE_16 (1U << SPI_MODE_FSIZE_POS)
/**
* @brief Memory frame size is 32 bits.
*/
#define SPI_MODE_FSIZE_32 2U << 2
#define SPI_MODE_FSIZE_32 (2U << SPI_MODE_FSIZE_POS)
/**
* @brief Memory frame size is 64 bits.
*/
#define SPI_MODE_FSIZE_64 (3U << SPI_MODE_FSIZE_POS)
/**
* @brief Enables the circular buffer mode.
*/
#define SPI_MODE_CIRCULAR (1U << 2)
/**
* @brief Enables the slave mode.
*/
#define SPI_MODE_SLAVE (1U << 3)
/** @} */
/**
@ -430,6 +440,21 @@ static inline hal_spi_driver_c *spiObjectInit(hal_spi_driver_c *self) {
* @name Inline methods of hal_spi_driver_c
* @{
*/
/**
* @memberof hal_spi_driver_c
* @public
*
* @brief Returns the configured size, in bytes, of data frames.
*
* @param[in,out] ip Pointer to a @p hal_spi_driver_c instance.
*/
CC_FORCE_INLINE
static inline size_t spiGetFrameSizeX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
return (size_t)(1U << ((__spi_getfield(self, mode) & SPI_MODE_FSIZE_MASK) >> SPI_MODE_FSIZE_POS));
}
#if (SPI_SELECT_MODE == SPI_SELECT_MODE_LLD) || defined (__DOXYGEN__)
/**
* @memberof hal_spi_driver_c