From a6852177432cd3b09b935822534d9e18765369c6 Mon Sep 17 00:00:00 2001 From: Yannis Huber Date: Mon, 29 Jun 2020 18:09:48 +0200 Subject: [PATCH] maixbit: add I2C support --- src/machine/board_k210.go | 297 +++++++++++++++++++ src/machine/board_maixbit.go | 18 +- src/machine/board_maixbit_baremetal.go | 13 + src/machine/i2c.go | 2 +- src/machine/machine_k210.go | 378 ++++++++++++++++++------- 5 files changed, 587 insertions(+), 121 deletions(-) diff --git a/src/machine/board_k210.go b/src/machine/board_k210.go index 4474728d..2b934e5e 100644 --- a/src/machine/board_k210.go +++ b/src/machine/board_k210.go @@ -55,3 +55,300 @@ const ( P46 Pin = 46 P47 Pin = 47 ) + +type FPIOAFunction uint8 + +// Every pin on the Kendryte K210 is assigned to an FPIOA function. +// Each pin can be configured with every function below. +const ( + FUNC_JTAG_TCLK FPIOAFunction = 0 // JTAG Test Clock + FUNC_JTAG_TDI FPIOAFunction = 1 // JTAG Test Data In + FUNC_JTAG_TMS FPIOAFunction = 2 // JTAG Test Mode Select + FUNC_JTAG_TDO FPIOAFunction = 3 // JTAG Test Data Out + FUNC_SPI0_D0 FPIOAFunction = 4 // SPI0 Data 0 + FUNC_SPI0_D1 FPIOAFunction = 5 // SPI0 Data 1 + FUNC_SPI0_D2 FPIOAFunction = 6 // SPI0 Data 2 + FUNC_SPI0_D3 FPIOAFunction = 7 // SPI0 Data 3 + FUNC_SPI0_D4 FPIOAFunction = 8 // SPI0 Data 4 + FUNC_SPI0_D5 FPIOAFunction = 9 // SPI0 Data 5 + FUNC_SPI0_D6 FPIOAFunction = 10 // SPI0 Data 6 + FUNC_SPI0_D7 FPIOAFunction = 11 // SPI0 Data 7 + FUNC_SPI0_SS0 FPIOAFunction = 12 // SPI0 Chip Select 0 + FUNC_SPI0_SS1 FPIOAFunction = 13 // SPI0 Chip Select 1 + FUNC_SPI0_SS2 FPIOAFunction = 14 // SPI0 Chip Select 2 + FUNC_SPI0_SS3 FPIOAFunction = 15 // SPI0 Chip Select 3 + FUNC_SPI0_ARB FPIOAFunction = 16 // SPI0 Arbitration + FUNC_SPI0_SCLK FPIOAFunction = 17 // SPI0 Serial Clock + FUNC_UARTHS_RX FPIOAFunction = 18 // UART High speed Receiver + FUNC_UARTHS_TX FPIOAFunction = 19 // UART High speed Transmitter + FUNC_RESV6 FPIOAFunction = 20 // Reserved function + FUNC_RESV7 FPIOAFunction = 21 // Reserved function + FUNC_CLK_SPI1 FPIOAFunction = 22 // Clock SPI1 + FUNC_CLK_I2C1 FPIOAFunction = 23 // Clock I2C1 + FUNC_GPIOHS0 FPIOAFunction = 24 // GPIO High speed 0 + FUNC_GPIOHS1 FPIOAFunction = 25 // GPIO High speed 1 + FUNC_GPIOHS2 FPIOAFunction = 26 // GPIO High speed 2 + FUNC_GPIOHS3 FPIOAFunction = 27 // GPIO High speed 3 + FUNC_GPIOHS4 FPIOAFunction = 28 // GPIO High speed 4 + FUNC_GPIOHS5 FPIOAFunction = 29 // GPIO High speed 5 + FUNC_GPIOHS6 FPIOAFunction = 30 // GPIO High speed 6 + FUNC_GPIOHS7 FPIOAFunction = 31 // GPIO High speed 7 + FUNC_GPIOHS8 FPIOAFunction = 32 // GPIO High speed 8 + FUNC_GPIOHS9 FPIOAFunction = 33 // GPIO High speed 9 + FUNC_GPIOHS10 FPIOAFunction = 34 // GPIO High speed 10 + FUNC_GPIOHS11 FPIOAFunction = 35 // GPIO High speed 11 + FUNC_GPIOHS12 FPIOAFunction = 36 // GPIO High speed 12 + FUNC_GPIOHS13 FPIOAFunction = 37 // GPIO High speed 13 + FUNC_GPIOHS14 FPIOAFunction = 38 // GPIO High speed 14 + FUNC_GPIOHS15 FPIOAFunction = 39 // GPIO High speed 15 + FUNC_GPIOHS16 FPIOAFunction = 40 // GPIO High speed 16 + FUNC_GPIOHS17 FPIOAFunction = 41 // GPIO High speed 17 + FUNC_GPIOHS18 FPIOAFunction = 42 // GPIO High speed 18 + FUNC_GPIOHS19 FPIOAFunction = 43 // GPIO High speed 19 + FUNC_GPIOHS20 FPIOAFunction = 44 // GPIO High speed 20 + FUNC_GPIOHS21 FPIOAFunction = 45 // GPIO High speed 21 + FUNC_GPIOHS22 FPIOAFunction = 46 // GPIO High speed 22 + FUNC_GPIOHS23 FPIOAFunction = 47 // GPIO High speed 23 + FUNC_GPIOHS24 FPIOAFunction = 48 // GPIO High speed 24 + FUNC_GPIOHS25 FPIOAFunction = 49 // GPIO High speed 25 + FUNC_GPIOHS26 FPIOAFunction = 50 // GPIO High speed 26 + FUNC_GPIOHS27 FPIOAFunction = 51 // GPIO High speed 27 + FUNC_GPIOHS28 FPIOAFunction = 52 // GPIO High speed 28 + FUNC_GPIOHS29 FPIOAFunction = 53 // GPIO High speed 29 + FUNC_GPIOHS30 FPIOAFunction = 54 // GPIO High speed 30 + FUNC_GPIOHS31 FPIOAFunction = 55 // GPIO High speed 31 + FUNC_GPIO0 FPIOAFunction = 56 // GPIO pin 0 + FUNC_GPIO1 FPIOAFunction = 57 // GPIO pin 1 + FUNC_GPIO2 FPIOAFunction = 58 // GPIO pin 2 + FUNC_GPIO3 FPIOAFunction = 59 // GPIO pin 3 + FUNC_GPIO4 FPIOAFunction = 60 // GPIO pin 4 + FUNC_GPIO5 FPIOAFunction = 61 // GPIO pin 5 + FUNC_GPIO6 FPIOAFunction = 62 // GPIO pin 6 + FUNC_GPIO7 FPIOAFunction = 63 // GPIO pin 7 + FUNC_UART1_RX FPIOAFunction = 64 // UART1 Receiver + FUNC_UART1_TX FPIOAFunction = 65 // UART1 Transmitter + FUNC_UART2_RX FPIOAFunction = 66 // UART2 Receiver + FUNC_UART2_TX FPIOAFunction = 67 // UART2 Transmitter + FUNC_UART3_RX FPIOAFunction = 68 // UART3 Receiver + FUNC_UART3_TX FPIOAFunction = 69 // UART3 Transmitter + FUNC_SPI1_D0 FPIOAFunction = 70 // SPI1 Data 0 + FUNC_SPI1_D1 FPIOAFunction = 71 // SPI1 Data 1 + FUNC_SPI1_D2 FPIOAFunction = 72 // SPI1 Data 2 + FUNC_SPI1_D3 FPIOAFunction = 73 // SPI1 Data 3 + FUNC_SPI1_D4 FPIOAFunction = 74 // SPI1 Data 4 + FUNC_SPI1_D5 FPIOAFunction = 75 // SPI1 Data 5 + FUNC_SPI1_D6 FPIOAFunction = 76 // SPI1 Data 6 + FUNC_SPI1_D7 FPIOAFunction = 77 // SPI1 Data 7 + FUNC_SPI1_SS0 FPIOAFunction = 78 // SPI1 Chip Select 0 + FUNC_SPI1_SS1 FPIOAFunction = 79 // SPI1 Chip Select 1 + FUNC_SPI1_SS2 FPIOAFunction = 80 // SPI1 Chip Select 2 + FUNC_SPI1_SS3 FPIOAFunction = 81 // SPI1 Chip Select 3 + FUNC_SPI1_ARB FPIOAFunction = 82 // SPI1 Arbitration + FUNC_SPI1_SCLK FPIOAFunction = 83 // SPI1 Serial Clock + FUNC_SPI_SLAVE_D0 FPIOAFunction = 84 // SPI Slave Data 0 + FUNC_SPI_SLAVE_SS FPIOAFunction = 85 // SPI Slave Select + FUNC_SPI_SLAVE_SCLK FPIOAFunction = 86 // SPI Slave Serial Clock + FUNC_I2S0_MCLK FPIOAFunction = 87 // I2S0 Master Clock + FUNC_I2S0_SCLK FPIOAFunction = 88 // I2S0 Serial Clock(BCLK) + FUNC_I2S0_WS FPIOAFunction = 89 // I2S0 Word Select(LRCLK) + FUNC_I2S0_IN_D0 FPIOAFunction = 90 // I2S0 Serial Data Input 0 + FUNC_I2S0_IN_D1 FPIOAFunction = 91 // I2S0 Serial Data Input 1 + FUNC_I2S0_IN_D2 FPIOAFunction = 92 // I2S0 Serial Data Input 2 + FUNC_I2S0_IN_D3 FPIOAFunction = 93 // I2S0 Serial Data Input 3 + FUNC_I2S0_OUT_D0 FPIOAFunction = 94 // I2S0 Serial Data Output 0 + FUNC_I2S0_OUT_D1 FPIOAFunction = 95 // I2S0 Serial Data Output 1 + FUNC_I2S0_OUT_D2 FPIOAFunction = 96 // I2S0 Serial Data Output 2 + FUNC_I2S0_OUT_D3 FPIOAFunction = 97 // I2S0 Serial Data Output 3 + FUNC_I2S1_MCLK FPIOAFunction = 98 // I2S1 Master Clock + FUNC_I2S1_SCLK FPIOAFunction = 99 // I2S1 Serial Clock(BCLK) + FUNC_I2S1_WS FPIOAFunction = 100 // I2S1 Word Select(LRCLK) + FUNC_I2S1_IN_D0 FPIOAFunction = 101 // I2S1 Serial Data Input 0 + FUNC_I2S1_IN_D1 FPIOAFunction = 102 // I2S1 Serial Data Input 1 + FUNC_I2S1_IN_D2 FPIOAFunction = 103 // I2S1 Serial Data Input 2 + FUNC_I2S1_IN_D3 FPIOAFunction = 104 // I2S1 Serial Data Input 3 + FUNC_I2S1_OUT_D0 FPIOAFunction = 105 // I2S1 Serial Data Output 0 + FUNC_I2S1_OUT_D1 FPIOAFunction = 106 // I2S1 Serial Data Output 1 + FUNC_I2S1_OUT_D2 FPIOAFunction = 107 // I2S1 Serial Data Output 2 + FUNC_I2S1_OUT_D3 FPIOAFunction = 108 // I2S1 Serial Data Output 3 + FUNC_I2S2_MCLK FPIOAFunction = 109 // I2S2 Master Clock + FUNC_I2S2_SCLK FPIOAFunction = 110 // I2S2 Serial Clock(BCLK) + FUNC_I2S2_WS FPIOAFunction = 111 // I2S2 Word Select(LRCLK) + FUNC_I2S2_IN_D0 FPIOAFunction = 112 // I2S2 Serial Data Input 0 + FUNC_I2S2_IN_D1 FPIOAFunction = 113 // I2S2 Serial Data Input 1 + FUNC_I2S2_IN_D2 FPIOAFunction = 114 // I2S2 Serial Data Input 2 + FUNC_I2S2_IN_D3 FPIOAFunction = 115 // I2S2 Serial Data Input 3 + FUNC_I2S2_OUT_D0 FPIOAFunction = 116 // I2S2 Serial Data Output 0 + FUNC_I2S2_OUT_D1 FPIOAFunction = 117 // I2S2 Serial Data Output 1 + FUNC_I2S2_OUT_D2 FPIOAFunction = 118 // I2S2 Serial Data Output 2 + FUNC_I2S2_OUT_D3 FPIOAFunction = 119 // I2S2 Serial Data Output 3 + FUNC_RESV0 FPIOAFunction = 120 // Reserved function + FUNC_RESV1 FPIOAFunction = 121 // Reserved function + FUNC_RESV2 FPIOAFunction = 122 // Reserved function + FUNC_RESV3 FPIOAFunction = 123 // Reserved function + FUNC_RESV4 FPIOAFunction = 124 // Reserved function + FUNC_RESV5 FPIOAFunction = 125 // Reserved function + FUNC_I2C0_SCLK FPIOAFunction = 126 // I2C0 Serial Clock + FUNC_I2C0_SDA FPIOAFunction = 127 // I2C0 Serial Data + FUNC_I2C1_SCLK FPIOAFunction = 128 // I2C1 Serial Clock + FUNC_I2C1_SDA FPIOAFunction = 129 // I2C1 Serial Data + FUNC_I2C2_SCLK FPIOAFunction = 130 // I2C2 Serial Clock + FUNC_I2C2_SDA FPIOAFunction = 131 // I2C2 Serial Data + FUNC_CMOS_XCLK FPIOAFunction = 132 // DVP System Clock + FUNC_CMOS_RST FPIOAFunction = 133 // DVP System Reset + FUNC_CMOS_PWDN FPIOAFunction = 134 // DVP Power Down Mode + FUNC_CMOS_VSYNC FPIOAFunction = 135 // DVP Vertical Sync + FUNC_CMOS_HREF FPIOAFunction = 136 // DVP Horizontal Reference output + FUNC_CMOS_PCLK FPIOAFunction = 137 // Pixel Clock + FUNC_CMOS_D0 FPIOAFunction = 138 // Data Bit 0 + FUNC_CMOS_D1 FPIOAFunction = 139 // Data Bit 1 + FUNC_CMOS_D2 FPIOAFunction = 140 // Data Bit 2 + FUNC_CMOS_D3 FPIOAFunction = 141 // Data Bit 3 + FUNC_CMOS_D4 FPIOAFunction = 142 // Data Bit 4 + FUNC_CMOS_D5 FPIOAFunction = 143 // Data Bit 5 + FUNC_CMOS_D6 FPIOAFunction = 144 // Data Bit 6 + FUNC_CMOS_D7 FPIOAFunction = 145 // Data Bit 7 + FUNC_SCCB_SCLK FPIOAFunction = 146 // SCCB Serial Clock + FUNC_SCCB_SDA FPIOAFunction = 147 // SCCB Serial Data + FUNC_UART1_CTS FPIOAFunction = 148 // UART1 Clear To Send + FUNC_UART1_DSR FPIOAFunction = 149 // UART1 Data Set Ready + FUNC_UART1_DCD FPIOAFunction = 150 // UART1 Data Carrier Detect + FUNC_UART1_RI FPIOAFunction = 151 // UART1 Ring Indicator + FUNC_UART1_SIR_IN FPIOAFunction = 152 // UART1 Serial Infrared Input + FUNC_UART1_DTR FPIOAFunction = 153 // UART1 Data Terminal Ready + FUNC_UART1_RTS FPIOAFunction = 154 // UART1 Request To Send + FUNC_UART1_OUT2 FPIOAFunction = 155 // UART1 User-designated Output 2 + FUNC_UART1_OUT1 FPIOAFunction = 156 // UART1 User-designated Output 1 + FUNC_UART1_SIR_OUT FPIOAFunction = 157 // UART1 Serial Infrared Output + FUNC_UART1_BAUD FPIOAFunction = 158 // UART1 Transmit Clock Output + FUNC_UART1_RE FPIOAFunction = 159 // UART1 Receiver Output Enable + FUNC_UART1_DE FPIOAFunction = 160 // UART1 Driver Output Enable + FUNC_UART1_RS485_EN FPIOAFunction = 161 // UART1 RS485 Enable + FUNC_UART2_CTS FPIOAFunction = 162 // UART2 Clear To Send + FUNC_UART2_DSR FPIOAFunction = 163 // UART2 Data Set Ready + FUNC_UART2_DCD FPIOAFunction = 164 // UART2 Data Carrier Detect + FUNC_UART2_RI FPIOAFunction = 165 // UART2 Ring Indicator + FUNC_UART2_SIR_IN FPIOAFunction = 166 // UART2 Serial Infrared Input + FUNC_UART2_DTR FPIOAFunction = 167 // UART2 Data Terminal Ready + FUNC_UART2_RTS FPIOAFunction = 168 // UART2 Request To Send + FUNC_UART2_OUT2 FPIOAFunction = 169 // UART2 User-designated Output 2 + FUNC_UART2_OUT1 FPIOAFunction = 170 // UART2 User-designated Output 1 + FUNC_UART2_SIR_OUT FPIOAFunction = 171 // UART2 Serial Infrared Output + FUNC_UART2_BAUD FPIOAFunction = 172 // UART2 Transmit Clock Output + FUNC_UART2_RE FPIOAFunction = 173 // UART2 Receiver Output Enable + FUNC_UART2_DE FPIOAFunction = 174 // UART2 Driver Output Enable + FUNC_UART2_RS485_EN FPIOAFunction = 175 // UART2 RS485 Enable + FUNC_UART3_CTS FPIOAFunction = 176 // UART3 Clear To Send + FUNC_UART3_DSR FPIOAFunction = 177 // UART3 Data Set Ready + FUNC_UART3_DCD FPIOAFunction = 178 // UART3 Data Carrier Detect + FUNC_UART3_RI FPIOAFunction = 179 // UART3 Ring Indicator + FUNC_UART3_SIR_IN FPIOAFunction = 180 // UART3 Serial Infrared Input + FUNC_UART3_DTR FPIOAFunction = 181 // UART3 Data Terminal Ready + FUNC_UART3_RTS FPIOAFunction = 182 // UART3 Request To Send + FUNC_UART3_OUT2 FPIOAFunction = 183 // UART3 User-designated Output 2 + FUNC_UART3_OUT1 FPIOAFunction = 184 // UART3 User-designated Output 1 + FUNC_UART3_SIR_OUT FPIOAFunction = 185 // UART3 Serial Infrared Output + FUNC_UART3_BAUD FPIOAFunction = 186 // UART3 Transmit Clock Output + FUNC_UART3_RE FPIOAFunction = 187 // UART3 Receiver Output Enable + FUNC_UART3_DE FPIOAFunction = 188 // UART3 Driver Output Enable + FUNC_UART3_RS485_EN FPIOAFunction = 189 // UART3 RS485 Enable + FUNC_TIMER0_TOGGLE1 FPIOAFunction = 190 // TIMER0 Toggle Output 1 + FUNC_TIMER0_TOGGLE2 FPIOAFunction = 191 // TIMER0 Toggle Output 2 + FUNC_TIMER0_TOGGLE3 FPIOAFunction = 192 // TIMER0 Toggle Output 3 + FUNC_TIMER0_TOGGLE4 FPIOAFunction = 193 // TIMER0 Toggle Output 4 + FUNC_TIMER1_TOGGLE1 FPIOAFunction = 194 // TIMER1 Toggle Output 1 + FUNC_TIMER1_TOGGLE2 FPIOAFunction = 195 // TIMER1 Toggle Output 2 + FUNC_TIMER1_TOGGLE3 FPIOAFunction = 196 // TIMER1 Toggle Output 3 + FUNC_TIMER1_TOGGLE4 FPIOAFunction = 197 // TIMER1 Toggle Output 4 + FUNC_TIMER2_TOGGLE1 FPIOAFunction = 198 // TIMER2 Toggle Output 1 + FUNC_TIMER2_TOGGLE2 FPIOAFunction = 199 // TIMER2 Toggle Output 2 + FUNC_TIMER2_TOGGLE3 FPIOAFunction = 200 // TIMER2 Toggle Output 3 + FUNC_TIMER2_TOGGLE4 FPIOAFunction = 201 // TIMER2 Toggle Output 4 + FUNC_CLK_SPI2 FPIOAFunction = 202 // Clock SPI2 + FUNC_CLK_I2C2 FPIOAFunction = 203 // Clock I2C2 + FUNC_INTERNAL0 FPIOAFunction = 204 // Internal function signal 0 + FUNC_INTERNAL1 FPIOAFunction = 205 // Internal function signal 1 + FUNC_INTERNAL2 FPIOAFunction = 206 // Internal function signal 2 + FUNC_INTERNAL3 FPIOAFunction = 207 // Internal function signal 3 + FUNC_INTERNAL4 FPIOAFunction = 208 // Internal function signal 4 + FUNC_INTERNAL5 FPIOAFunction = 209 // Internal function signal 5 + FUNC_INTERNAL6 FPIOAFunction = 210 // Internal function signal 6 + FUNC_INTERNAL7 FPIOAFunction = 211 // Internal function signal 7 + FUNC_INTERNAL8 FPIOAFunction = 212 // Internal function signal 8 + FUNC_INTERNAL9 FPIOAFunction = 213 // Internal function signal 9 + FUNC_INTERNAL10 FPIOAFunction = 214 // Internal function signal 10 + FUNC_INTERNAL11 FPIOAFunction = 215 // Internal function signal 11 + FUNC_INTERNAL12 FPIOAFunction = 216 // Internal function signal 12 + FUNC_INTERNAL13 FPIOAFunction = 217 // Internal function signal 13 + FUNC_INTERNAL14 FPIOAFunction = 218 // Internal function signal 14 + FUNC_INTERNAL15 FPIOAFunction = 219 // Internal function signal 15 + FUNC_INTERNAL16 FPIOAFunction = 220 // Internal function signal 16 + FUNC_INTERNAL17 FPIOAFunction = 221 // Internal function signal 17 + FUNC_CONSTANT FPIOAFunction = 222 // Constant function + FUNC_INTERNAL18 FPIOAFunction = 223 // Internal function signal 18 + FUNC_DEBUG0 FPIOAFunction = 224 // Debug function 0 + FUNC_DEBUG1 FPIOAFunction = 225 // Debug function 1 + FUNC_DEBUG2 FPIOAFunction = 226 // Debug function 2 + FUNC_DEBUG3 FPIOAFunction = 227 // Debug function 3 + FUNC_DEBUG4 FPIOAFunction = 228 // Debug function 4 + FUNC_DEBUG5 FPIOAFunction = 229 // Debug function 5 + FUNC_DEBUG6 FPIOAFunction = 230 // Debug function 6 + FUNC_DEBUG7 FPIOAFunction = 231 // Debug function 7 + FUNC_DEBUG8 FPIOAFunction = 232 // Debug function 8 + FUNC_DEBUG9 FPIOAFunction = 233 // Debug function 9 + FUNC_DEBUG10 FPIOAFunction = 234 // Debug function 10 + FUNC_DEBUG11 FPIOAFunction = 235 // Debug function 11 + FUNC_DEBUG12 FPIOAFunction = 236 // Debug function 12 + FUNC_DEBUG13 FPIOAFunction = 237 // Debug function 13 + FUNC_DEBUG14 FPIOAFunction = 238 // Debug function 14 + FUNC_DEBUG15 FPIOAFunction = 239 // Debug function 15 + FUNC_DEBUG16 FPIOAFunction = 240 // Debug function 16 + FUNC_DEBUG17 FPIOAFunction = 241 // Debug function 17 + FUNC_DEBUG18 FPIOAFunction = 242 // Debug function 18 + FUNC_DEBUG19 FPIOAFunction = 243 // Debug function 19 + FUNC_DEBUG20 FPIOAFunction = 244 // Debug function 20 + FUNC_DEBUG21 FPIOAFunction = 245 // Debug function 21 + FUNC_DEBUG22 FPIOAFunction = 246 // Debug function 22 + FUNC_DEBUG23 FPIOAFunction = 247 // Debug function 23 + FUNC_DEBUG24 FPIOAFunction = 248 // Debug function 24 + FUNC_DEBUG25 FPIOAFunction = 249 // Debug function 25 + FUNC_DEBUG26 FPIOAFunction = 250 // Debug function 26 + FUNC_DEBUG27 FPIOAFunction = 251 // Debug function 27 + FUNC_DEBUG28 FPIOAFunction = 252 // Debug function 28 + FUNC_DEBUG29 FPIOAFunction = 253 // Debug function 29 + FUNC_DEBUG30 FPIOAFunction = 254 // Debug function 30 + FUNC_DEBUG31 FPIOAFunction = 255 // Debug function 31 +) + +// These are the default FPIOA values for each function. +// (source: https://github.com/kendryte/kendryte-standalone-sdk/blob/develop/lib/drivers/fpioa.c#L69) +var fpioaFuncDefaults [256]uint32 = [256]uint32{ + 0x00900000, 0x00900001, 0x00900002, 0x00001f03, 0x00b03f04, 0x00b03f05, 0x00b03f06, 0x00b03f07, 0x00b03f08, + 0x00b03f09, 0x00b03f0a, 0x00b03f0b, 0x00001f0c, 0x00001f0d, 0x00001f0e, 0x00001f0f, 0x03900010, 0x00001f11, + 0x00900012, 0x00001f13, 0x00900014, 0x00900015, 0x00001f16, 0x00001f17, 0x00901f18, 0x00901f19, 0x00901f1a, + 0x00901f1b, 0x00901f1c, 0x00901f1d, 0x00901f1e, 0x00901f1f, 0x00901f20, 0x00901f21, 0x00901f22, 0x00901f23, + 0x00901f24, 0x00901f25, 0x00901f26, 0x00901f27, 0x00901f28, 0x00901f29, 0x00901f2a, 0x00901f2b, 0x00901f2c, + 0x00901f2d, 0x00901f2e, 0x00901f2f, 0x00901f30, 0x00901f31, 0x00901f32, 0x00901f33, 0x00901f34, 0x00901f35, + 0x00901f36, 0x00901f37, 0x00901f38, 0x00901f39, 0x00901f3a, 0x00901f3b, 0x00901f3c, 0x00901f3d, 0x00901f3e, + 0x00901f3f, 0x00900040, 0x00001f41, 0x00900042, 0x00001f43, 0x00900044, 0x00001f45, 0x00b03f46, 0x00b03f47, + 0x00b03f48, 0x00b03f49, 0x00b03f4a, 0x00b03f4b, 0x00b03f4c, 0x00b03f4d, 0x00001f4e, 0x00001f4f, 0x00001f50, + 0x00001f51, 0x03900052, 0x00001f53, 0x00b03f54, 0x00900055, 0x00900056, 0x00001f57, 0x00001f58, 0x00001f59, + 0x0090005a, 0x0090005b, 0x0090005c, 0x0090005d, 0x00001f5e, 0x00001f5f, 0x00001f60, 0x00001f61, 0x00001f62, + 0x00001f63, 0x00001f64, 0x00900065, 0x00900066, 0x00900067, 0x00900068, 0x00001f69, 0x00001f6a, 0x00001f6b, + 0x00001f6c, 0x00001f6d, 0x00001f6e, 0x00001f6f, 0x00900070, 0x00900071, 0x00900072, 0x00900073, 0x00001f74, + 0x00001f75, 0x00001f76, 0x00001f77, 0x00000078, 0x00000079, 0x0000007a, 0x0000007b, 0x0000007c, 0x0000007d, + 0x0099107e, 0x0099107f, 0x00991080, 0x00991081, 0x00991082, 0x00991083, 0x00001f84, 0x00001f85, 0x00001f86, + 0x00900087, 0x00900088, 0x00900089, 0x0090008a, 0x0090008b, 0x0090008c, 0x0090008d, 0x0090008e, 0x0090008f, + 0x00900090, 0x00900091, 0x00993092, 0x00993093, 0x00900094, 0x00900095, 0x00900096, 0x00900097, 0x00900098, + 0x00001f99, 0x00001f9a, 0x00001f9b, 0x00001f9c, 0x00001f9d, 0x00001f9e, 0x00001f9f, 0x00001fa0, 0x00001fa1, + 0x009000a2, 0x009000a3, 0x009000a4, 0x009000a5, 0x009000a6, 0x00001fa7, 0x00001fa8, 0x00001fa9, 0x00001faa, + 0x00001fab, 0x00001fac, 0x00001fad, 0x00001fae, 0x00001faf, 0x009000b0, 0x009000b1, 0x009000b2, 0x009000b3, + 0x009000b4, 0x00001fb5, 0x00001fb6, 0x00001fb7, 0x00001fb8, 0x00001fb9, 0x00001fba, 0x00001fbb, 0x00001fbc, + 0x00001fbd, 0x00001fbe, 0x00001fbf, 0x00001fc0, 0x00001fc1, 0x00001fc2, 0x00001fc3, 0x00001fc4, 0x00001fc5, + 0x00001fc6, 0x00001fc7, 0x00001fc8, 0x00001fc9, 0x00001fca, 0x00001fcb, 0x00001fcc, 0x00001fcd, 0x00001fce, + 0x00001fcf, 0x00001fd0, 0x00001fd1, 0x00001fd2, 0x00001fd3, 0x00001fd4, 0x009000d5, 0x009000d6, 0x009000d7, + 0x009000d8, 0x009100d9, 0x00991fda, 0x009000db, 0x009000dc, 0x009000dd, 0x000000de, 0x009000df, 0x00001fe0, + 0x00001fe1, 0x00001fe2, 0x00001fe3, 0x00001fe4, 0x00001fe5, 0x00001fe6, 0x00001fe7, 0x00001fe8, 0x00001fe9, + 0x00001fea, 0x00001feb, 0x00001fec, 0x00001fed, 0x00001fee, 0x00001fef, 0x00001ff0, 0x00001ff1, 0x00001ff2, + 0x00001ff3, 0x00001ff4, 0x00001ff5, 0x00001ff6, 0x00001ff7, 0x00001ff8, 0x00001ff9, 0x00001ffa, 0x00001ffb, + 0x00001ffc, 0x00001ffd, 0x00001ffe, 0x00001fff, +} diff --git a/src/machine/board_maixbit.go b/src/machine/board_maixbit.go index 2c60fc98..c1d002f3 100644 --- a/src/machine/board_maixbit.go +++ b/src/machine/board_maixbit.go @@ -40,18 +40,6 @@ const ( D33 = P33 // GPIOHS17 D34 = P34 // GPIOHS18 D35 = P35 // GPIOHS19 - D36 = P36 // GPIOHS20 - D37 = P37 // GPIOHS21 - D38 = P38 // GPIOHS22 - D39 = P39 // GPIOHS23 - D40 = P40 // GPIOHS24 - D41 = P41 // GPIOHS25 - D42 = P42 // GPIOHS26 - D43 = P43 // GPIOHS27 - D44 = P44 // GPIOHS28 - D45 = P45 // GPIOHS29 - D46 = P46 // GPIOHS30 - D47 = P47 // GPIOHS31 ) const ( @@ -76,3 +64,9 @@ const ( SPI0_MOSI_PIN = D28 SPI0_MISO_PIN = D26 ) + +// I2C pins. +const ( + I2C0_SDA_PIN = D34 + I2C0_SCL_PIN = D35 +) diff --git a/src/machine/board_maixbit_baremetal.go b/src/machine/board_maixbit_baremetal.go index 98733bee..3c75a4df 100644 --- a/src/machine/board_maixbit_baremetal.go +++ b/src/machine/board_maixbit_baremetal.go @@ -13,3 +13,16 @@ var ( Bus: kendryte.SPI1, } ) + +// I2C on the MAix Bit. +var ( + I2C0 = I2C{ + Bus: kendryte.I2C0, + } + I2C1 = I2C{ + Bus: kendryte.I2C1, + } + I2C2 = I2C{ + Bus: kendryte.I2C2, + } +) diff --git a/src/machine/i2c.go b/src/machine/i2c.go index 977c9bad..3bab81d0 100644 --- a/src/machine/i2c.go +++ b/src/machine/i2c.go @@ -1,4 +1,4 @@ -// +build avr nrf sam stm32,!stm32f4disco fe310 +// +build avr nrf sam stm32,!stm32f4disco fe310 k210 package machine diff --git a/src/machine/machine_k210.go b/src/machine/machine_k210.go index 247db080..243ae54c 100644 --- a/src/machine/machine_k210.go +++ b/src/machine/machine_k210.go @@ -40,21 +40,12 @@ const ( PinLow = 8 ) -// FPIOA functions. -const ( - fpioaFuncSpi0Sclk = 17 - fpioaFuncSpi0D0 = 4 - fpioaFuncSpi0D1 = 5 - fpioaFuncSpi1Sclk = 83 - fpioaFuncSpi1D0 = 70 - fpioaFuncSpi1D1 = 71 -) - var ( - ErrUnsupportedSPIController = errors.New("SPI controller not supported. Use SPI0 or SPI1.") + errUnsupportedSPIController = errors.New("SPI controller not supported. Use SPI0 or SPI1.") + errI2CTxAbort = errors.New("I2C transmition has been aborted.") ) -func (p Pin) fpioaSetIOPull(pull fpioaPullMode) { +func (p Pin) setFPIOAIOPull(pull fpioaPullMode) { switch pull { case fpioaPullNone: kendryte.FPIOA.IO[uint8(p)].ClearBits(kendryte.FPIOA_IO_PU & kendryte.FPIOA_IO_PD) @@ -67,42 +58,55 @@ func (p Pin) fpioaSetIOPull(pull fpioaPullMode) { } } +// SetFPIOAFunction is used to configure the pin for one of the FPIOA functions. +// Each pin on the Kendryte K210 can be configured with any of the available FPIOA functions. +func (p Pin) SetFPIOAFunction(f FPIOAFunction) { + kendryte.FPIOA.IO[uint8(p)].Set(fpioaFuncDefaults[uint8(f)]) +} + +// FPIOAFunction returns the current FPIOA function of the pin. +func (p Pin) FPIOAFunction() FPIOAFunction { + return FPIOAFunction((kendryte.FPIOA.IO[uint8(p)].Get() & kendryte.FPIOA_IO_CH_SEL_Msk)) +} + // Configure this pin with the given configuration. +// The pin must already be set as GPIO or GPIOHS pin. func (p Pin) Configure(config PinConfig) { var input bool - kendryte.FPIOA.IO[uint8(p)].SetBits(kendryte.FPIOA_IO_OE_EN | kendryte.FPIOA_IO_IE_EN | kendryte.FPIOA_IO_ST | kendryte.FPIOA_IO_DS_Msk) - switch config.Mode { - case PinInput: - p.fpioaSetIOPull(fpioaPullNone) - input = true - - case PinInputPullUp: - p.fpioaSetIOPull(fpioaPullUp) - input = true - - case PinInputPullDown: - p.fpioaSetIOPull(fpioaPullDown) - input = true - - case PinOutput: - p.fpioaSetIOPull(fpioaPullNone) - input = false - + // Check if the current pin's FPIOA function is either GPIO or GPIOHS. + f := p.FPIOAFunction() + if f < FUNC_GPIOHS0 || f > FUNC_GPIO7 { + return // The pin is not configured as GPIO or GPIOHS. } - if p >= P08 && p <= P15 { - // Converts the IO pin number in the effective GPIO number (assuming default FPIOA function). - gpioPin := uint8(p) - 8 + switch config.Mode { + case PinInput: + p.setFPIOAIOPull(fpioaPullNone) + input = true + case PinInputPullUp: + p.setFPIOAIOPull(fpioaPullUp) + input = true + case PinInputPullDown: + p.setFPIOAIOPull(fpioaPullDown) + input = true + case PinOutput: + p.setFPIOAIOPull(fpioaPullNone) + input = false + } + + if f >= FUNC_GPIO0 && f <= FUNC_GPIO7 { + // Converts the IO pin number in the effective GPIO number (based on the FPIOA function). + gpioPin := uint8(f - FUNC_GPIO0) if input { kendryte.GPIO.DIRECTION.ClearBits(1 << gpioPin) } else { kendryte.GPIO.DIRECTION.SetBits(1 << gpioPin) } - } else if p >= P16 && p <= P47 { - // Converts the IO pin number in the effective GPIOHS number (assuming default FPIOA function). - gpioPin := uint8(p) - 16 + } else if f >= FUNC_GPIOHS0 && f <= FUNC_GPIOHS31 { + // Converts the IO pin number in the effective GPIOHS number (based on the FPIOA function). + gpioPin := uint8(f - FUNC_GPIOHS0) if input { kendryte.GPIOHS.INPUT_EN.SetBits(1 << gpioPin) @@ -116,16 +120,23 @@ func (p Pin) Configure(config PinConfig) { // Set the pin to high or low. func (p Pin) Set(high bool) { - if p >= P08 && p <= P15 { - gpioPin := uint8(p) - 8 + + // Check if the current pin's FPIOA function is either GPIO or GPIOHS. + f := p.FPIOAFunction() + if f < FUNC_GPIOHS0 || f > FUNC_GPIO7 { + return // The pin is not configured as GPIO or GPIOHS. + } + + if f >= FUNC_GPIO0 && f <= FUNC_GPIO7 { + gpioPin := uint8(f - FUNC_GPIO0) if high { kendryte.GPIO.DATA_OUTPUT.SetBits(1 << gpioPin) } else { kendryte.GPIO.DATA_OUTPUT.ClearBits(1 << gpioPin) } - } else if p >= P16 && p <= P47 { - gpioPin := uint8(p) - 16 + } else if f >= FUNC_GPIOHS0 && f <= FUNC_GPIOHS31 { + gpioPin := uint8(f - FUNC_GPIOHS0) if high { kendryte.GPIOHS.OUTPUT_VAL.SetBits(1 << gpioPin) @@ -137,12 +148,19 @@ func (p Pin) Set(high bool) { // Get returns the current value of a GPIO pin. func (p Pin) Get() bool { + + // Check if the current pin's FPIOA function is either GPIO or GPIOHS. + f := p.FPIOAFunction() + if f < FUNC_GPIOHS0 || f > FUNC_GPIO7 { + return false // The pin is not configured as GPIO or GPIOHS. + } + var val uint32 - if p >= P08 && p <= P15 { - gpioPin := uint8(p) - 8 + if f >= FUNC_GPIO0 && f <= FUNC_GPIO7 { + gpioPin := uint8(f - FUNC_GPIO0) val = kendryte.GPIO.DATA_INPUT.Get() & (1 << gpioPin) - } else if p >= P16 && p <= P47 { - gpioPin := uint8(p) - 16 + } else if f >= FUNC_GPIOHS0 && f <= FUNC_GPIOHS31 { + gpioPin := uint8(f - FUNC_GPIOHS0) val = kendryte.GPIOHS.INPUT_VAL.Get() & (1 << gpioPin) } return (val > 0) @@ -161,11 +179,12 @@ var pinCallbacks [32]func(Pin) func (p Pin) SetInterrupt(change PinChange, callback func(Pin)) error { // Check if the pin is a GPIOHS pin. - if p < P16 && p > P47 { + f := p.FPIOAFunction() + if f >= FUNC_GPIOHS0 && f <= FUNC_GPIOHS31 { return ErrInvalidDataPin } - gpioPin := uint8(p) - 16 + gpioPin := uint8(f - FUNC_GPIOHS0) // Clear all interrupts. kendryte.GPIOHS.RISE_IE.ClearBits(1 << gpioPin) @@ -242,70 +261,70 @@ func (p Pin) SetInterrupt(change PinChange, callback func(Pin)) error { var ir interrupt.Interrupt - switch p { - case P16: + switch f { + case FUNC_GPIOHS0: ir = interrupt.New(kendryte.IRQ_GPIOHS0, handleInterrupt) - case P17: + case FUNC_GPIOHS1: ir = interrupt.New(kendryte.IRQ_GPIOHS1, handleInterrupt) - case P18: + case FUNC_GPIOHS2: ir = interrupt.New(kendryte.IRQ_GPIOHS2, handleInterrupt) - case P19: + case FUNC_GPIOHS3: ir = interrupt.New(kendryte.IRQ_GPIOHS3, handleInterrupt) - case P20: + case FUNC_GPIOHS4: ir = interrupt.New(kendryte.IRQ_GPIOHS4, handleInterrupt) - case P21: + case FUNC_GPIOHS5: ir = interrupt.New(kendryte.IRQ_GPIOHS5, handleInterrupt) - case P22: + case FUNC_GPIOHS6: ir = interrupt.New(kendryte.IRQ_GPIOHS6, handleInterrupt) - case P23: + case FUNC_GPIOHS7: ir = interrupt.New(kendryte.IRQ_GPIOHS7, handleInterrupt) - case P24: + case FUNC_GPIOHS8: ir = interrupt.New(kendryte.IRQ_GPIOHS8, handleInterrupt) - case P25: + case FUNC_GPIOHS9: ir = interrupt.New(kendryte.IRQ_GPIOHS9, handleInterrupt) - case P26: + case FUNC_GPIOHS10: ir = interrupt.New(kendryte.IRQ_GPIOHS10, handleInterrupt) - case P27: + case FUNC_GPIOHS11: ir = interrupt.New(kendryte.IRQ_GPIOHS11, handleInterrupt) - case P28: + case FUNC_GPIOHS12: ir = interrupt.New(kendryte.IRQ_GPIOHS12, handleInterrupt) - case P29: + case FUNC_GPIOHS13: ir = interrupt.New(kendryte.IRQ_GPIOHS13, handleInterrupt) - case P30: + case FUNC_GPIOHS14: ir = interrupt.New(kendryte.IRQ_GPIOHS14, handleInterrupt) - case P31: + case FUNC_GPIOHS15: ir = interrupt.New(kendryte.IRQ_GPIOHS15, handleInterrupt) - case P32: + case FUNC_GPIOHS16: ir = interrupt.New(kendryte.IRQ_GPIOHS16, handleInterrupt) - case P33: + case FUNC_GPIOHS17: ir = interrupt.New(kendryte.IRQ_GPIOHS17, handleInterrupt) - case P34: + case FUNC_GPIOHS18: ir = interrupt.New(kendryte.IRQ_GPIOHS18, handleInterrupt) - case P35: + case FUNC_GPIOHS19: ir = interrupt.New(kendryte.IRQ_GPIOHS19, handleInterrupt) - case P36: + case FUNC_GPIOHS20: ir = interrupt.New(kendryte.IRQ_GPIOHS20, handleInterrupt) - case P37: + case FUNC_GPIOHS21: ir = interrupt.New(kendryte.IRQ_GPIOHS21, handleInterrupt) - case P38: + case FUNC_GPIOHS22: ir = interrupt.New(kendryte.IRQ_GPIOHS22, handleInterrupt) - case P39: + case FUNC_GPIOHS23: ir = interrupt.New(kendryte.IRQ_GPIOHS23, handleInterrupt) - case P40: + case FUNC_GPIOHS24: ir = interrupt.New(kendryte.IRQ_GPIOHS24, handleInterrupt) - case P41: + case FUNC_GPIOHS25: ir = interrupt.New(kendryte.IRQ_GPIOHS25, handleInterrupt) - case P42: + case FUNC_GPIOHS26: ir = interrupt.New(kendryte.IRQ_GPIOHS26, handleInterrupt) - case P43: + case FUNC_GPIOHS27: ir = interrupt.New(kendryte.IRQ_GPIOHS27, handleInterrupt) - case P44: + case FUNC_GPIOHS28: ir = interrupt.New(kendryte.IRQ_GPIOHS28, handleInterrupt) - case P45: + case FUNC_GPIOHS29: ir = interrupt.New(kendryte.IRQ_GPIOHS29, handleInterrupt) - case P46: + case FUNC_GPIOHS30: ir = interrupt.New(kendryte.IRQ_GPIOHS30, handleInterrupt) - case P47: + case FUNC_GPIOHS31: ir = interrupt.New(kendryte.IRQ_GPIOHS31, handleInterrupt) } @@ -326,7 +345,22 @@ var ( ) func (uart UART) Configure(config UARTConfig) { - div := CPUFrequency()/115200 - 1 + + // Use default baudrate if not set. + if config.BaudRate == 0 { + config.BaudRate = 115200 + } + + // Use default pins if not set. + if config.TX == 0 && config.RX == 0 { + config.TX = UART_TX_PIN + config.RX = UART_RX_PIN + } + + config.TX.SetFPIOAFunction(FUNC_UARTHS_TX) + config.RX.SetFPIOAFunction(FUNC_UARTHS_RX) + + div := CPUFrequency()/config.BaudRate - 1 uart.Bus.DIV.Set(div) uart.Bus.TXCTRL.Set(kendryte.UARTHS_TXCTRL_TXEN) @@ -384,45 +418,31 @@ func (spi SPI) Configure(config SPIConfig) error { config.MISO = SPI0_MISO_PIN } - // Enable pins for SPI. - sckBits := uint32(kendryte.FPIOA_IO_OE_EN | kendryte.FPIOA_IO_DS_Msk) - dataBits := uint32(kendryte.FPIOA_IO_OE_EN | kendryte.FPIOA_IO_OE_INV | kendryte.FPIOA_IO_IE_EN | - kendryte.FPIOA_IO_IE_INV | kendryte.FPIOA_IO_ST | kendryte.FPIOA_IO_DS_Msk) - // Enable APB2 clock. kendryte.SYSCTL.CLK_EN_CENT.SetBits(kendryte.SYSCTL_CLK_EN_CENT_APB2_CLK_EN) - var fpioaFuncSclk uint32 - var fpioaFuncD0 uint32 - var fpioaFuncD1 uint32 - switch spi.Bus { case kendryte.SPI0: - // Initialize FPIOA values. - fpioaFuncSclk = fpioaFuncSpi0Sclk - fpioaFuncD0 = fpioaFuncSpi0D0 - fpioaFuncD1 = fpioaFuncSpi0D1 - // Initialize SPI clock. kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_SPI0_CLK_EN) kendryte.SYSCTL.CLK_TH1.ClearBits(kendryte.SYSCTL_CLK_TH1_SPI0_CLK_Msk) - case kendryte.SPI1: - // Initialize FPIOA values. - fpioaFuncSclk = fpioaFuncSpi1Sclk - fpioaFuncD0 = fpioaFuncSpi1D0 - fpioaFuncD1 = fpioaFuncSpi1D1 + // Initialize pins. + config.SCK.SetFPIOAFunction(FUNC_SPI0_SCLK) + config.MOSI.SetFPIOAFunction(FUNC_SPI0_D0) + config.MISO.SetFPIOAFunction(FUNC_SPI0_D1) + case kendryte.SPI1: // Initialize SPI clock. kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_SPI1_CLK_EN) kendryte.SYSCTL.CLK_TH1.ClearBits(kendryte.SYSCTL_CLK_TH1_SPI1_CLK_Msk) - default: - return ErrUnsupportedSPIController - } - // Set FPIOA functins for selected pins. - kendryte.FPIOA.IO[uint8(config.SCK)].Set(uint32(sckBits | fpioaFuncSclk)) - kendryte.FPIOA.IO[uint8(config.MOSI)].Set(uint32(dataBits | fpioaFuncD0)) - kendryte.FPIOA.IO[uint8(config.MISO)].Set(uint32(dataBits | fpioaFuncD1)) + // Initialize pins. + config.SCK.SetFPIOAFunction(FUNC_SPI1_SCLK) + config.MOSI.SetFPIOAFunction(FUNC_SPI1_D0) + config.MISO.SetFPIOAFunction(FUNC_SPI1_D1) + default: + return errUnsupportedSPIController + } // Set default frequency. if config.Frequency == 0 { @@ -430,7 +450,6 @@ func (spi SPI) Configure(config SPIConfig) error { } baudr := CPUFrequency() / config.Frequency - print(baudr) spi.Bus.BAUDR.Set(baudr) // Configure SPI mode 0, standard frame format, 8-bit data, little-endian. @@ -473,5 +492,148 @@ func (spi SPI) Transfer(w byte) (byte, error) { } return byte(spi.Bus.DR0.Get()), nil - +} + +// I2C on the K210. +type I2C struct { + Bus *kendryte.I2C_Type +} + +// I2CConfig is used to store config info for I2C. +type I2CConfig struct { + Frequency uint32 + SCL Pin + SDA Pin +} + +// Configure is intended to setup the I2C interface. +func (i2c I2C) Configure(config I2CConfig) error { + + if config.Frequency == 0 { + config.Frequency = TWI_FREQ_100KHZ + } + + if config.SDA == 0 && config.SCL == 0 { + config.SDA = I2C0_SDA_PIN + config.SCL = I2C0_SCL_PIN + } + + // Enable APB0 clock. + kendryte.SYSCTL.CLK_EN_CENT.SetBits(kendryte.SYSCTL_CLK_EN_CENT_APB0_CLK_EN) + + switch i2c.Bus { + case kendryte.I2C0: + // Initialize I2C0 clock. + kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_I2C0_CLK_EN) + kendryte.SYSCTL.CLK_TH5.ReplaceBits(0x03, kendryte.SYSCTL_CLK_TH5_I2C0_CLK_Msk, kendryte.SYSCTL_CLK_TH5_I2C0_CLK_Pos) + + // Initialize pins. + config.SDA.SetFPIOAFunction(FUNC_I2C0_SDA) + config.SCL.SetFPIOAFunction(FUNC_I2C0_SCLK) + case kendryte.I2C1: + // Initialize I2C1 clock. + kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_I2C1_CLK_EN) + kendryte.SYSCTL.CLK_TH5.ReplaceBits(0x03, kendryte.SYSCTL_CLK_TH5_I2C1_CLK_Msk, kendryte.SYSCTL_CLK_TH5_I2C1_CLK_Pos) + + // Initialize pins. + config.SDA.SetFPIOAFunction(FUNC_I2C1_SDA) + config.SCL.SetFPIOAFunction(FUNC_I2C1_SCLK) + case kendryte.I2C2: + // Initialize I2C2 clock. + kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_I2C2_CLK_EN) + kendryte.SYSCTL.CLK_TH5.ReplaceBits(0x03, kendryte.SYSCTL_CLK_TH5_I2C2_CLK_Msk, kendryte.SYSCTL_CLK_TH5_I2C2_CLK_Pos) + + // Initialize pins. + config.SDA.SetFPIOAFunction(FUNC_I2C2_SDA) + config.SCL.SetFPIOAFunction(FUNC_I2C2_SCLK) + } + + div := CPUFrequency() / config.Frequency / 16 + + // Disable controller before setting the prescale register. + i2c.Bus.ENABLE.Set(0) + + i2c.Bus.CON.Set(0x63) + + // Set prescaler registers. + i2c.Bus.SS_SCL_HCNT.Set(uint32(div)) + i2c.Bus.SS_SCL_LCNT.Set(uint32(div)) + + i2c.Bus.INTR_MASK.Set(0) + i2c.Bus.DMA_CR.Set(0x03) + i2c.Bus.DMA_RDLR.Set(0) + i2c.Bus.DMA_TDLR.Set(0x4) + + return nil +} + +// Tx does a single I2C transaction at the specified address. +// It clocks out the given address, writes the bytes in w, reads back len(r) +// bytes and stores them in r, and generates a stop condition on the bus. +func (i2c I2C) Tx(addr uint16, w, r []byte) error { + // Set slave address. + i2c.Bus.TAR.Set(uint32(addr)) + // Enable controller. + i2c.Bus.ENABLE.Set(1) + + if len(w) != 0 { + i2c.Bus.CLR_TX_ABRT.Set(i2c.Bus.CLR_TX_ABRT.Get()) + dataLen := uint32(len(w)) + di := 0 + + for dataLen != 0 { + fifoLen := 8 - i2c.Bus.TXFLR.Get() + if dataLen < fifoLen { + fifoLen = dataLen + } + + for i := uint32(0); i < fifoLen; i++ { + i2c.Bus.DATA_CMD.Set(uint32(w[di])) + di += 1 + } + if i2c.Bus.TX_ABRT_SOURCE.Get() != 0 { + return errI2CTxAbort + } + dataLen -= fifoLen + } + + // Wait for transmition to complete. + for i2c.Bus.STATUS.HasBits(kendryte.I2C_STATUS_ACTIVITY) || !i2c.Bus.STATUS.HasBits(kendryte.I2C_STATUS_TFE) { + } + + if i2c.Bus.TX_ABRT_SOURCE.Get() != 0 { + return errI2CTxAbort + } + } + if len(r) != 0 { + dataLen := uint32(len(r)) + cmdLen := uint32(len(r)) + di := 0 + + for dataLen != 0 || cmdLen != 0 { + fifoLen := 8 - i2c.Bus.RXFLR.Get() + if dataLen < fifoLen { + fifoLen = dataLen + } + for i := uint32(0); i < fifoLen; i++ { + r[di] = byte(i2c.Bus.DATA_CMD.Get()) + di += 1 + } + dataLen -= fifoLen + + fifoLen = 8 - i2c.Bus.TXFLR.Get() + if cmdLen < fifoLen { + fifoLen = cmdLen + } + for i := uint32(0); i < fifoLen; i++ { + i2c.Bus.DATA_CMD.Set(0x100) + } + if i2c.Bus.TX_ABRT_SOURCE.Get() != 0 { + return errI2CTxAbort + } + cmdLen -= fifoLen + } + } + + return nil }