1 #include "peripherals/sc18is600.h"
5 #include <stm32/exti.h>
6 #include <stm32/misc.h>
8 #include <stm32/gpio.h>
11 #define Sc18Is600_Cmd_Write 0x00
12 #define Sc18Is600_Cmd_Read 0x01
13 #define Sc18Is600_Cmd_Read_After_Write 0x02
14 #define Sc18Is600_Cmd_Write_After_Write 0x03
15 #define Sc18Is600_Cmd_Read_Buffer 0x06
16 #define Sc18Is600_Cmd_Write_To_Reg 0x20
17 #define Sc18Is600_Cmd_Read_From_Reg 0x21
18 #define Sc18Is600_Cmd_Power_Down 0x30
30 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
31 GPIO_InitTypeDef GPIO_InitStructure;
32 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
33 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
34 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
35 GPIO_Init(
GPIOB, &GPIO_InitStructure);
38 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
39 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
40 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
41 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
42 GPIO_Init(
GPIOD, &GPIO_InitStructure);
44 EXTI_InitTypeDef EXTI_InitStructure;
45 GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource2);
46 EXTI_InitStructure.EXTI_Line = EXTI_Line2;
47 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
48 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
49 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
50 EXTI_Init(&EXTI_InitStructure);
52 NVIC_InitTypeDef NVIC_InitStructure;
53 NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
54 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
55 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
56 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
58 NVIC_Init(&NVIC_InitStructure);
61 NVIC_InitTypeDef NVIC_init_struct = {
62 .NVIC_IRQChannel = DMA1_Channel4_IRQn,
63 .NVIC_IRQChannelPreemptionPriority = 0,
64 .NVIC_IRQChannelSubPriority = 0,
65 .NVIC_IRQChannelCmd = ENABLE
67 NVIC_Init(&NVIC_init_struct);
69 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
72 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
73 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
74 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
75 GPIO_Init(
GPIOB, &GPIO_InitStructure);
77 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
81 SPI_InitTypeDef SPI_InitStructure;
82 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
83 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
84 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
85 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
86 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
87 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
88 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
89 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
90 SPI_InitStructure.SPI_CRCPolynomial = 7;
91 SPI_Init(SPI2, &SPI_InitStructure);
94 SPI_Cmd(SPI2, ENABLE);
97 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
104 DMA_DeInit(DMA1_Channel4);
105 DMA_InitTypeDef DMA_initStructure_4 = {
106 .DMA_PeripheralBaseAddr = (
uint32_t)(SPI2_BASE + 0x0C),
108 .DMA_DIR = DMA_DIR_PeripheralSRC,
109 .DMA_BufferSize = _len,
110 .DMA_PeripheralInc = DMA_PeripheralInc_Disable,
111 .DMA_MemoryInc = DMA_MemoryInc_Enable,
112 .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
113 .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
114 .DMA_Mode = DMA_Mode_Normal,
115 .DMA_Priority = DMA_Priority_VeryHigh,
116 .DMA_M2M = DMA_M2M_Disable
118 DMA_Init(DMA1_Channel4, &DMA_initStructure_4);
120 DMA_DeInit(DMA1_Channel5);
121 DMA_InitTypeDef DMA_initStructure_5 = {
122 .DMA_PeripheralBaseAddr = (
uint32_t)(SPI2_BASE + 0x0C),
124 .DMA_DIR = DMA_DIR_PeripheralDST,
125 .DMA_BufferSize = _len,
126 .DMA_PeripheralInc = DMA_PeripheralInc_Disable,
127 .DMA_MemoryInc = DMA_MemoryInc_Enable,
128 .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
129 .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
130 .DMA_Mode = DMA_Mode_Normal,
131 .DMA_Priority = DMA_Priority_Medium,
132 .DMA_M2M = DMA_M2M_Disable
134 DMA_Init(DMA1_Channel5, &DMA_initStructure_5);
137 SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
139 DMA_Cmd(DMA1_Channel4, ENABLE);
142 SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
144 DMA_Cmd(DMA1_Channel5, ENABLE);
147 DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
206 #define ReadI2CStatReg() { \
207 sc18is600.priv_tx_buf[0] = Sc18Is600_Cmd_Read_From_Reg; \
208 sc18is600.priv_tx_buf[1] = Sc18Is600_I2CStat; \
209 sc18is600.priv_tx_buf[2] = 0; \
211 sc18is600_setup_SPI_DMA(3); \
218 if (EXTI_GetITStatus(EXTI_Line2) !=
RESET) {
219 EXTI_ClearITPendingBit(EXTI_Line2);
245 DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, DISABLE);
247 SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, DISABLE);
248 SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, DISABLE);
250 DMA_Cmd(DMA1_Channel4, DISABLE);
251 DMA_Cmd(DMA1_Channel5, DISABLE);
void sc18is600_transmit(uint8_t addr, uint8_t len)
void sc18is600_arch_init(void)
static void sc18is600_setup_SPI_DMA(uint8_t _len)
void sc18is600_write_to_register(uint8_t addr, uint8_t value)
#define Sc18Is600_Cmd_Read_After_Write
#define Sc18Is600_Cmd_Write
struct Sc18Is600 sc18is600
void sc18is600_read_from_register(uint8_t addr)
enum Sc18Is600Transaction transaction
#define Sc18Is600_Cmd_Read_From_Reg
void exti2_irq_handler(void)
#define Sc18Is600_Cmd_Read_Buffer
#define Sc18Is600Unselect()
uint8_t priv_rx_buf[SC18IS600_BUF_LEN]
void sc18is600_tranceive(uint8_t addr, uint8_t len_tx, uint8_t len_rx)
void dma1_c4_irq_handler(void)
#define Sc18Is600_Cmd_Write_To_Reg
void sc18is600_receive(uint8_t addr, uint8_t len)
uint8_t priv_tx_buf[SC18IS600_BUF_LEN]
enum Sc18Is600Status status
#define Sc18Is600Select()